drm/radeon/kms: enable use of unmappable VRAM V2
[pandora-kernel.git] / drivers / net / wimax / i2400m / usb-notif.c
1 /*
2  * Intel Wireless WiMAX Connection 2400m over USB
3  * Notification handling
4  *
5  *
6  * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *   * Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in
16  *     the documentation and/or other materials provided with the
17  *     distribution.
18  *   * Neither the name of Intel Corporation nor the names of its
19  *     contributors may be used to endorse or promote products derived
20  *     from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  *
35  * Intel Corporation <linux-wimax@intel.com>
36  * Yanir Lubetkin <yanirx.lubetkin@intel.com>
37  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
38  *  - Initial implementation
39  *
40  *
41  * The notification endpoint is active when the device is not in boot
42  * mode; in here we just read and get notifications; based on those,
43  * we act to either reinitialize the device after a reboot or to
44  * submit a RX request.
45  *
46  * ROADMAP
47  *
48  * i2400mu_usb_notification_setup()
49  *
50  * i2400mu_usb_notification_release()
51  *
52  * i2400mu_usb_notification_cb()        Called when a URB is ready
53  *   i2400mu_notif_grok()
54  *     i2400m_is_boot_barker()
55  *     i2400m_dev_reset_handle()
56  *     i2400mu_rx_kick()
57  */
58 #include <linux/usb.h>
59 #include "i2400m-usb.h"
60
61
62 #define D_SUBMODULE notif
63 #include "usb-debug-levels.h"
64
65
66 static const
67 __le32 i2400m_ZERO_BARKER[4] = { 0, 0, 0, 0 };
68
69
70 /*
71  * Process a received notification
72  *
73  * In normal operation mode, we can only receive two types of payloads
74  * on the notification endpoint:
75  *
76  *   - a reboot barker, we do a bootstrap (the device has reseted).
77  *
78  *   - a block of zeroes: there is pending data in the IN endpoint
79  */
80 static
81 int i2400mu_notification_grok(struct i2400mu *i2400mu, const void *buf,
82                                  size_t buf_len)
83 {
84         int ret;
85         struct device *dev = &i2400mu->usb_iface->dev;
86         struct i2400m *i2400m = &i2400mu->i2400m;
87
88         d_fnstart(4, dev, "(i2400m %p buf %p buf_len %zu)\n",
89                   i2400mu, buf, buf_len);
90         ret = -EIO;
91         if (buf_len < sizeof(i2400m_ZERO_BARKER))
92                 /* Not a bug, just ignore */
93                 goto error_bad_size;
94         ret = 0;
95         if (!memcmp(i2400m_ZERO_BARKER, buf, sizeof(i2400m_ZERO_BARKER))) {
96                 i2400mu_rx_kick(i2400mu);
97                 goto out;
98         }
99         ret = i2400m_is_boot_barker(i2400m, buf, buf_len);
100         if (unlikely(ret >= 0))
101                 ret = i2400m_dev_reset_handle(i2400m, "device rebooted");
102         else    /* Unknown or unexpected data in the notif message */
103                 i2400m_unknown_barker(i2400m, buf, buf_len);
104 error_bad_size:
105 out:
106         d_fnend(4, dev, "(i2400m %p buf %p buf_len %zu) = %d\n",
107                 i2400mu, buf, buf_len, ret);
108         return ret;
109 }
110
111
112 /*
113  * URB callback for the notification endpoint
114  *
115  * @urb: the urb received from the notification endpoint
116  *
117  * This function will just process the USB side of the transaction,
118  * checking everything is fine, pass the processing to
119  * i2400m_notification_grok() and resubmit the URB.
120  */
121 static
122 void i2400mu_notification_cb(struct urb *urb)
123 {
124         int ret;
125         struct i2400mu *i2400mu = urb->context;
126         struct device *dev = &i2400mu->usb_iface->dev;
127
128         d_fnstart(4, dev, "(urb %p status %d actual_length %d)\n",
129                   urb, urb->status, urb->actual_length);
130         ret = urb->status;
131         switch (ret) {
132         case 0:
133                 ret = i2400mu_notification_grok(i2400mu, urb->transfer_buffer,
134                                                 urb->actual_length);
135                 if (ret == -EIO && edc_inc(&i2400mu->urb_edc, EDC_MAX_ERRORS,
136                                            EDC_ERROR_TIMEFRAME))
137                         goto error_exceeded;
138                 if (ret == -ENOMEM)     /* uff...power cycle? shutdown? */
139                         goto error_exceeded;
140                 break;
141         case -EINVAL:                   /* while removing driver */
142         case -ENODEV:                   /* dev disconnect ... */
143         case -ENOENT:                   /* ditto */
144         case -ESHUTDOWN:                /* URB killed */
145         case -ECONNRESET:               /* disconnection */
146                 goto out;               /* Notify around */
147         default:                        /* Some error? */
148                 if (edc_inc(&i2400mu->urb_edc,
149                             EDC_MAX_ERRORS, EDC_ERROR_TIMEFRAME))
150                         goto error_exceeded;
151                 dev_err(dev, "notification: URB error %d, retrying\n",
152                         urb->status);
153         }
154         usb_mark_last_busy(i2400mu->usb_dev);
155         ret = usb_submit_urb(i2400mu->notif_urb, GFP_ATOMIC);
156         switch (ret) {
157         case 0:
158         case -EINVAL:                   /* while removing driver */
159         case -ENODEV:                   /* dev disconnect ... */
160         case -ENOENT:                   /* ditto */
161         case -ESHUTDOWN:                /* URB killed */
162         case -ECONNRESET:               /* disconnection */
163                 break;                  /* just ignore */
164         default:                        /* Some error? */
165                 dev_err(dev, "notification: cannot submit URB: %d\n", ret);
166                 goto error_submit;
167         }
168         d_fnend(4, dev, "(urb %p status %d actual_length %d) = void\n",
169                 urb, urb->status, urb->actual_length);
170         return;
171
172 error_exceeded:
173         dev_err(dev, "maximum errors in notification URB exceeded; "
174                 "resetting device\n");
175 error_submit:
176         usb_queue_reset_device(i2400mu->usb_iface);
177 out:
178         d_fnend(4, dev, "(urb %p status %d actual_length %d) = void\n",
179                 urb, urb->status, urb->actual_length);
180         return;
181 }
182
183
184 /*
185  * setup the notification endpoint
186  *
187  * @i2400m: device descriptor
188  *
189  * This procedure prepares the notification urb and handler for receiving
190  * unsolicited barkers from the device.
191  */
192 int i2400mu_notification_setup(struct i2400mu *i2400mu)
193 {
194         struct device *dev = &i2400mu->usb_iface->dev;
195         int usb_pipe, ret = 0;
196         struct usb_endpoint_descriptor *epd;
197         char *buf;
198
199         d_fnstart(4, dev, "(i2400m %p)\n", i2400mu);
200         buf = kmalloc(I2400MU_MAX_NOTIFICATION_LEN, GFP_KERNEL | GFP_DMA);
201         if (buf == NULL) {
202                 dev_err(dev, "notification: buffer allocation failed\n");
203                 ret = -ENOMEM;
204                 goto error_buf_alloc;
205         }
206
207         i2400mu->notif_urb = usb_alloc_urb(0, GFP_KERNEL);
208         if (!i2400mu->notif_urb) {
209                 ret = -ENOMEM;
210                 dev_err(dev, "notification: cannot allocate URB\n");
211                 goto error_alloc_urb;
212         }
213         epd = usb_get_epd(i2400mu->usb_iface,
214                           i2400mu->endpoint_cfg.notification);
215         usb_pipe = usb_rcvintpipe(i2400mu->usb_dev, epd->bEndpointAddress);
216         usb_fill_int_urb(i2400mu->notif_urb, i2400mu->usb_dev, usb_pipe,
217                          buf, I2400MU_MAX_NOTIFICATION_LEN,
218                          i2400mu_notification_cb, i2400mu, epd->bInterval);
219         ret = usb_submit_urb(i2400mu->notif_urb, GFP_KERNEL);
220         if (ret != 0) {
221                 dev_err(dev, "notification: cannot submit URB: %d\n", ret);
222                 goto error_submit;
223         }
224         d_fnend(4, dev, "(i2400m %p) = %d\n", i2400mu, ret);
225         return ret;
226
227 error_submit:
228         usb_free_urb(i2400mu->notif_urb);
229 error_alloc_urb:
230         kfree(buf);
231 error_buf_alloc:
232         d_fnend(4, dev, "(i2400m %p) = %d\n", i2400mu, ret);
233         return ret;
234 }
235
236
237 /*
238  * Tear down of the notification mechanism
239  *
240  * @i2400m: device descriptor
241  *
242  * Kill the interrupt endpoint urb, free any allocated resources.
243  *
244  * We need to check if we have done it before as for example,
245  * _suspend() call this; if after a suspend() we get a _disconnect()
246  * (as the case is when hibernating), nothing bad happens.
247  */
248 void i2400mu_notification_release(struct i2400mu *i2400mu)
249 {
250         struct device *dev = &i2400mu->usb_iface->dev;
251
252         d_fnstart(4, dev, "(i2400mu %p)\n", i2400mu);
253         if (i2400mu->notif_urb != NULL) {
254                 usb_kill_urb(i2400mu->notif_urb);
255                 kfree(i2400mu->notif_urb->transfer_buffer);
256                 usb_free_urb(i2400mu->notif_urb);
257                 i2400mu->notif_urb = NULL;
258         }
259         d_fnend(4, dev, "(i2400mu %p)\n", i2400mu);
260 }