]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/uwb/hwa-rc.c
Merge git://git.infradead.org/iommu-2.6
[linux-2.6-omap-h63xx.git] / drivers / uwb / hwa-rc.c
1 /*
2  * WUSB Host Wire Adapter: Radio Control Interface (WUSB[8.6])
3  * Radio Control command/event transport
4  *
5  * Copyright (C) 2005-2006 Intel Corporation
6  * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License version
10  * 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301, USA.
21  *
22  *
23  * Initialize the Radio Control interface Driver.
24  *
25  * For each device probed, creates an 'struct hwarc' which contains
26  * just the representation of the UWB Radio Controller, and the logic
27  * for reading notifications and passing them to the UWB Core.
28  *
29  * So we initialize all of those, register the UWB Radio Controller
30  * and setup the notification/event handle to pipe the notifications
31  * to the UWB management Daemon.
32  *
33  * Command and event filtering.
34  *
35  * This is the driver for the Radio Control Interface described in WUSB
36  * 1.0. The core UWB module assumes that all drivers are compliant to the
37  * WHCI 0.95 specification. We thus create a filter that parses all
38  * incoming messages from the (WUSB 1.0) device and manipulate them to
39  * conform to the WHCI 0.95 specification. Similarly, outgoing messages
40  * are parsed and manipulated to conform to the WUSB 1.0 compliant messages
41  * that the device expects. Only a few messages are affected:
42  * Affected events:
43  *    UWB_RC_EVT_BEACON
44  *    UWB_RC_EVT_BP_SLOT_CHANGE
45  *    UWB_RC_EVT_DRP_AVAIL
46  *    UWB_RC_EVT_DRP
47  * Affected commands:
48  *    UWB_RC_CMD_SCAN
49  *    UWB_RC_CMD_SET_DRP_IE
50  *
51  *
52  *
53  */
54 #include <linux/version.h>
55 #include <linux/init.h>
56 #include <linux/module.h>
57 #include <linux/usb.h>
58 #include <linux/usb/wusb.h>
59 #include <linux/usb/wusb-wa.h>
60 #include <linux/uwb.h>
61 #include "uwb-internal.h"
62 #define D_LOCAL 1
63 #include <linux/uwb/debug.h>
64
65 /* The device uses commands and events from the WHCI specification, although
66  * reporting itself as WUSB compliant. */
67 #define WUSB_QUIRK_WHCI_CMD_EVT         0x01
68
69 /**
70  * Descriptor for an instance of the UWB Radio Control Driver that
71  * attaches to the RCI interface of the Host Wired Adapter.
72  *
73  * Unless there is a lock specific to the 'data members', all access
74  * is protected by uwb_rc->mutex.
75  *
76  * The NEEP (Notification/Event EndPoint) URB (@neep_urb) writes to
77  * @rd_buffer. Note there is no locking because it is perfectly (heh!)
78  * serialized--probe() submits an URB, callback is called, processes
79  * the data (synchronously), submits another URB, and so on. There is
80  * no concurrent access to the buffer.
81  */
82 struct hwarc {
83         struct usb_device *usb_dev;
84         struct usb_interface *usb_iface;
85         struct uwb_rc *uwb_rc;          /* UWB host controller */
86         struct urb *neep_urb;           /* Notification endpoint handling */
87         struct edc neep_edc;
88         void *rd_buffer;                /* NEEP read buffer */
89 };
90
91
92 /* Beacon received notification (WUSB 1.0 [8.6.3.2]) */
93 struct uwb_rc_evt_beacon_WUSB_0100 {
94         struct uwb_rceb rceb;
95         u8      bChannelNumber;
96         __le16  wBPSTOffset;
97         u8      bLQI;
98         u8      bRSSI;
99         __le16  wBeaconInfoLength;
100         u8      BeaconInfo[];
101 } __attribute__((packed));
102
103 /**
104  * Filter WUSB 1.0 BEACON RCV notification to be WHCI 0.95
105  *
106  * @header: the incoming event
107  * @buf_size: size of buffer containing incoming event
108  * @new_size: size of event after filtering completed
109  *
110  * The WHCI 0.95 spec has a "Beacon Type" field. This value is unknown at
111  * the time we receive the beacon from WUSB so we just set it to
112  * UWB_RC_BEACON_TYPE_NEIGHBOR as a default.
113  * The solution below allocates memory upon receipt of every beacon from a
114  * WUSB device. This will deteriorate performance. What is the right way to
115  * do this?
116  */
117 static
118 int hwarc_filter_evt_beacon_WUSB_0100(struct uwb_rc *rc,
119                                       struct uwb_rceb **header,
120                                       const size_t buf_size,
121                                       size_t *new_size)
122 {
123         struct uwb_rc_evt_beacon_WUSB_0100 *be;
124         struct uwb_rc_evt_beacon *newbe;
125         size_t bytes_left, ielength;
126         struct device *dev = &rc->uwb_dev.dev;
127
128         be = container_of(*header, struct uwb_rc_evt_beacon_WUSB_0100, rceb);
129         bytes_left = buf_size;
130         if (bytes_left < sizeof(*be)) {
131                 dev_err(dev, "Beacon Received Notification: Not enough data "
132                         "to decode for filtering (%zu vs %zu bytes needed)\n",
133                         bytes_left, sizeof(*be));
134                 return -EINVAL;
135         }
136         bytes_left -= sizeof(*be);
137         ielength = le16_to_cpu(be->wBeaconInfoLength);
138         if (bytes_left < ielength) {
139                 dev_err(dev, "Beacon Received Notification: Not enough data "
140                         "to decode IEs (%zu vs %zu bytes needed)\n",
141                         bytes_left, ielength);
142                 return -EINVAL;
143         }
144         newbe = kzalloc(sizeof(*newbe) + ielength, GFP_ATOMIC);
145         if (newbe == NULL)
146                 return -ENOMEM;
147         newbe->rceb = be->rceb;
148         newbe->bChannelNumber = be->bChannelNumber;
149         newbe->bBeaconType = UWB_RC_BEACON_TYPE_NEIGHBOR;
150         newbe->wBPSTOffset = be->wBPSTOffset;
151         newbe->bLQI = be->bLQI;
152         newbe->bRSSI = be->bRSSI;
153         newbe->wBeaconInfoLength = be->wBeaconInfoLength;
154         memcpy(newbe->BeaconInfo, be->BeaconInfo, ielength);
155         *header = &newbe->rceb;
156         *new_size = sizeof(*newbe) + ielength;
157         return 1;  /* calling function will free memory */
158 }
159
160
161 /* DRP Availability change notification (WUSB 1.0 [8.6.3.8]) */
162 struct uwb_rc_evt_drp_avail_WUSB_0100 {
163         struct uwb_rceb rceb;
164         __le16 wIELength;
165         u8 IEData[];
166 } __attribute__((packed));
167
168 /**
169  * Filter WUSB 1.0 DRP AVAILABILITY CHANGE notification to be WHCI 0.95
170  *
171  * @header: the incoming event
172  * @buf_size: size of buffer containing incoming event
173  * @new_size: size of event after filtering completed
174  */
175 static
176 int hwarc_filter_evt_drp_avail_WUSB_0100(struct uwb_rc *rc,
177                                          struct uwb_rceb **header,
178                                          const size_t buf_size,
179                                          size_t *new_size)
180 {
181         struct uwb_rc_evt_drp_avail_WUSB_0100 *da;
182         struct uwb_rc_evt_drp_avail *newda;
183         struct uwb_ie_hdr *ie_hdr;
184         size_t bytes_left, ielength;
185         struct device *dev = &rc->uwb_dev.dev;
186
187
188         da = container_of(*header, struct uwb_rc_evt_drp_avail_WUSB_0100, rceb);
189         bytes_left = buf_size;
190         if (bytes_left < sizeof(*da)) {
191                 dev_err(dev, "Not enough data to decode DRP Avail "
192                         "Notification for filtering. Expected %zu, "
193                         "received %zu.\n", (size_t)sizeof(*da), bytes_left);
194                 return -EINVAL;
195         }
196         bytes_left -= sizeof(*da);
197         ielength = le16_to_cpu(da->wIELength);
198         if (bytes_left < ielength) {
199                 dev_err(dev, "DRP Avail Notification filter: IE length "
200                         "[%zu bytes] does not match actual length "
201                         "[%zu bytes].\n", ielength, bytes_left);
202                 return -EINVAL;
203         }
204         if (ielength < sizeof(*ie_hdr)) {
205                 dev_err(dev, "DRP Avail Notification filter: Not enough "
206                         "data to decode IE [%zu bytes, %zu needed]\n",
207                         ielength, sizeof(*ie_hdr));
208                 return -EINVAL;
209         }
210         ie_hdr = (void *) da->IEData;
211         if (ie_hdr->length > 32) {
212                 dev_err(dev, "DRP Availability Change event has unexpected "
213                         "length for filtering. Expected < 32 bytes, "
214                         "got %zu bytes.\n", (size_t)ie_hdr->length);
215                 return -EINVAL;
216         }
217         newda = kzalloc(sizeof(*newda), GFP_ATOMIC);
218         if (newda == NULL)
219                 return -ENOMEM;
220         newda->rceb = da->rceb;
221         memcpy(newda->bmp, (u8 *) ie_hdr + sizeof(*ie_hdr), ie_hdr->length);
222         *header = &newda->rceb;
223         *new_size = sizeof(*newda);
224         return 1; /* calling function will free memory */
225 }
226
227
228 /* DRP notification (WUSB 1.0 [8.6.3.9]) */
229 struct uwb_rc_evt_drp_WUSB_0100 {
230         struct uwb_rceb rceb;
231         struct uwb_dev_addr wSrcAddr;
232         u8 bExplicit;
233         __le16 wIELength;
234         u8 IEData[];
235 } __attribute__((packed));
236
237 /**
238  * Filter WUSB 1.0 DRP Notification to be WHCI 0.95
239  *
240  * @header: the incoming event
241  * @buf_size: size of buffer containing incoming event
242  * @new_size: size of event after filtering completed
243  *
244  * It is hard to manage DRP reservations without having a Reason code.
245  * Unfortunately there is none in the WUSB spec. We just set the default to
246  * DRP IE RECEIVED.
247  * We do not currently use the bBeaconSlotNumber value, so we set this to
248  * zero for now.
249  */
250 static
251 int hwarc_filter_evt_drp_WUSB_0100(struct uwb_rc *rc,
252                                    struct uwb_rceb **header,
253                                    const size_t buf_size,
254                                    size_t *new_size)
255 {
256         struct uwb_rc_evt_drp_WUSB_0100 *drpev;
257         struct uwb_rc_evt_drp *newdrpev;
258         size_t bytes_left, ielength;
259         struct device *dev = &rc->uwb_dev.dev;
260
261         drpev = container_of(*header, struct uwb_rc_evt_drp_WUSB_0100, rceb);
262         bytes_left = buf_size;
263         if (bytes_left < sizeof(*drpev)) {
264                 dev_err(dev, "Not enough data to decode DRP Notification "
265                         "for filtering. Expected %zu, received %zu.\n",
266                         (size_t)sizeof(*drpev), bytes_left);
267                 return -EINVAL;
268         }
269         ielength = le16_to_cpu(drpev->wIELength);
270         bytes_left -= sizeof(*drpev);
271         if (bytes_left < ielength) {
272                 dev_err(dev, "DRP Notification filter: header length [%zu "
273                         "bytes] does not match actual length [%zu "
274                         "bytes].\n", ielength, bytes_left);
275                 return -EINVAL;
276         }
277         newdrpev = kzalloc(sizeof(*newdrpev) + ielength, GFP_ATOMIC);
278         if (newdrpev == NULL)
279                 return -ENOMEM;
280         newdrpev->rceb = drpev->rceb;
281         newdrpev->src_addr = drpev->wSrcAddr;
282         newdrpev->reason = UWB_DRP_NOTIF_DRP_IE_RCVD;
283         newdrpev->beacon_slot_number = 0;
284         newdrpev->ie_length = drpev->wIELength;
285         memcpy(newdrpev->ie_data, drpev->IEData, ielength);
286         *header = &newdrpev->rceb;
287         *new_size = sizeof(*newdrpev) + ielength;
288         return 1; /* calling function will free memory */
289 }
290
291
292 /* Scan Command (WUSB 1.0 [8.6.2.5]) */
293 struct uwb_rc_cmd_scan_WUSB_0100 {
294         struct uwb_rccb rccb;
295         u8 bChannelNumber;
296         u8 bScanState;
297 } __attribute__((packed));
298
299 /**
300  * Filter WHCI 0.95 SCAN command to be WUSB 1.0 SCAN command
301  *
302  * @header:   command sent to device (compliant to WHCI 0.95)
303  * @size:     size of command sent to device
304  *
305  * We only reduce the size by two bytes because the WUSB 1.0 scan command
306  * does not have the last field (wStarttime). Also, make sure we don't send
307  * the device an unexpected scan type.
308  */
309 static
310 int hwarc_filter_cmd_scan_WUSB_0100(struct uwb_rc *rc,
311                                     struct uwb_rccb **header,
312                                     size_t *size)
313 {
314         struct uwb_rc_cmd_scan *sc;
315
316         sc = container_of(*header, struct uwb_rc_cmd_scan, rccb);
317
318         if (sc->bScanState == UWB_SCAN_ONLY_STARTTIME)
319                 sc->bScanState = UWB_SCAN_ONLY;
320         /* Don't send the last two bytes. */
321         *size -= 2;
322         return 0;
323 }
324
325
326 /* SET DRP IE command (WUSB 1.0 [8.6.2.7]) */
327 struct uwb_rc_cmd_set_drp_ie_WUSB_0100 {
328         struct uwb_rccb rccb;
329         u8 bExplicit;
330         __le16 wIELength;
331         struct uwb_ie_drp IEData[];
332 } __attribute__((packed));
333
334 /**
335  * Filter WHCI 0.95 SET DRP IE command to be WUSB 1.0 SET DRP IE command
336  *
337  * @header:   command sent to device (compliant to WHCI 0.95)
338  * @size:     size of command sent to device
339  *
340  * WUSB has an extra bExplicit field - we assume always explicit
341  * negotiation so this field is set. The command expected by the device is
342  * thus larger than the one prepared by the driver so we need to
343  * reallocate memory to accommodate this.
344  * We trust the driver to send us the correct data so no checking is done
345  * on incoming data - evn though it is variable length.
346  */
347 static
348 int hwarc_filter_cmd_set_drp_ie_WUSB_0100(struct uwb_rc *rc,
349                                           struct uwb_rccb **header,
350                                           size_t *size)
351 {
352         struct uwb_rc_cmd_set_drp_ie *orgcmd;
353         struct uwb_rc_cmd_set_drp_ie_WUSB_0100 *cmd;
354         size_t ielength;
355
356         orgcmd = container_of(*header, struct uwb_rc_cmd_set_drp_ie, rccb);
357         ielength = le16_to_cpu(orgcmd->wIELength);
358         cmd = kzalloc(sizeof(*cmd) + ielength, GFP_KERNEL);
359         if (cmd == NULL)
360                 return -ENOMEM;
361         cmd->rccb = orgcmd->rccb;
362         cmd->bExplicit = 0;
363         cmd->wIELength = orgcmd->wIELength;
364         memcpy(cmd->IEData, orgcmd->IEData, ielength);
365         *header = &cmd->rccb;
366         *size = sizeof(*cmd) + ielength;
367         return 1; /* calling function will free memory */
368 }
369
370
371 /**
372  * Filter data from WHCI driver to WUSB device
373  *
374  * @header: WHCI 0.95 compliant command from driver
375  * @size:   length of command
376  *
377  * The routine managing commands to the device (uwb_rc_cmd()) will call the
378  * filtering function pointer (if it exists) before it passes any data to
379  * the device. At this time the command has been formatted according to
380  * WHCI 0.95 and is ready to be sent to the device.
381  *
382  * The filter function will be provided with the current command and its
383  * length. The function will manipulate the command if necessary and
384  * potentially reallocate memory for a command that needed more memory that
385  * the given command. If new memory was created the function will return 1
386  * to indicate to the calling function that the memory need to be freed
387  * when not needed any more. The size will contain the new length of the
388  * command.
389  * If memory has not been allocated we rely on the original mechanisms to
390  * free the memory of the command - even when we reduce the value of size.
391  */
392 static
393 int hwarc_filter_cmd_WUSB_0100(struct uwb_rc *rc, struct uwb_rccb **header,
394                                size_t *size)
395 {
396         int result;
397         struct uwb_rccb *rccb = *header;
398         int cmd = le16_to_cpu(rccb->wCommand);
399         switch (cmd) {
400         case UWB_RC_CMD_SCAN:
401                 result = hwarc_filter_cmd_scan_WUSB_0100(rc, header, size);
402                 break;
403         case UWB_RC_CMD_SET_DRP_IE:
404                 result = hwarc_filter_cmd_set_drp_ie_WUSB_0100(rc, header, size);
405                 break;
406         default:
407                 result = -ENOANO;
408                 break;
409         }
410         return result;
411 }
412
413
414 /**
415  * Filter data from WHCI driver to WUSB device
416  *
417  * @header: WHCI 0.95 compliant command from driver
418  * @size:   length of command
419  *
420  * Filter commands based on which protocol the device supports. The WUSB
421  * errata should be the same as WHCI 0.95 so we do not filter that here -
422  * only WUSB 1.0.
423  */
424 static
425 int hwarc_filter_cmd(struct uwb_rc *rc, struct uwb_rccb **header,
426                      size_t *size)
427 {
428         int result = -ENOANO;
429         if (rc->version == 0x0100)
430                 result = hwarc_filter_cmd_WUSB_0100(rc, header, size);
431         return result;
432 }
433
434
435 /**
436  * Compute return value as sum of incoming value and value at given offset
437  *
438  * @rceb:      event for which we compute the size, it contains a variable
439  *             length field.
440  * @core_size: size of the "non variable" part of the event
441  * @offset:    place in event where the length of the variable part is stored
442  * @buf_size: total length of buffer in which event arrived - we need to make
443  *             sure we read the offset in memory that is still part of the event
444  */
445 static
446 ssize_t hwarc_get_event_size(struct uwb_rc *rc, const struct uwb_rceb *rceb,
447                              size_t core_size, size_t offset,
448                              const size_t buf_size)
449 {
450         ssize_t size = -ENOSPC;
451         const void *ptr = rceb;
452         size_t type_size = sizeof(__le16);
453         struct device *dev = &rc->uwb_dev.dev;
454
455         if (offset + type_size >= buf_size) {
456                 dev_err(dev, "Not enough data to read extra size of event "
457                         "0x%02x/%04x/%02x, only got %zu bytes.\n",
458                         rceb->bEventType, le16_to_cpu(rceb->wEvent),
459                         rceb->bEventContext, buf_size);
460                 goto out;
461         }
462         ptr += offset;
463         size = core_size + le16_to_cpu(*(__le16 *)ptr);
464 out:
465         return size;
466 }
467
468
469 /* Beacon slot change notification (WUSB 1.0 [8.6.3.5]) */
470 struct uwb_rc_evt_bp_slot_change_WUSB_0100 {
471         struct uwb_rceb rceb;
472         u8 bSlotNumber;
473 } __attribute__((packed));
474
475
476 /**
477  * Filter data from WUSB device to WHCI driver
478  *
479  * @header:      incoming event
480  * @buf_size:    size of buffer in which event arrived
481  * @_event_size: actual size of event in the buffer
482  * @new_size:    size of event after filtered
483  *
484  * We don't know how the buffer is constructed - there may be more than one
485  * event in it so buffer length does not determine event length. We first
486  * determine the expected size of the incoming event. This value is passed
487  * back only if the actual filtering succeeded (so we know the computed
488  * expected size is correct). This value will be zero if
489  * the event did not need any filtering.
490  *
491  * WHCI interprets the BP Slot Change event's data differently than
492  * WUSB. The event sizes are exactly the same. The data field
493  * indicates the new beacon slot in which a RC is transmitting its
494  * beacon. The maximum value of this is 96 (wMacBPLength ECMA-368
495  * 17.16 (Table 117)). We thus know that the WUSB value will not set
496  * the bit bNoSlot, so we don't really do anything (placeholder).
497  */
498 static
499 int hwarc_filter_event_WUSB_0100(struct uwb_rc *rc, struct uwb_rceb **header,
500                                  const size_t buf_size, size_t *_real_size,
501                                  size_t *_new_size)
502 {
503         int result = -ENOANO;
504         struct uwb_rceb *rceb = *header;
505         int event = le16_to_cpu(rceb->wEvent);
506         size_t event_size;
507         size_t core_size, offset;
508
509         if (rceb->bEventType != UWB_RC_CET_GENERAL)
510                 goto out;
511         switch (event) {
512         case UWB_RC_EVT_BEACON:
513                 core_size = sizeof(struct uwb_rc_evt_beacon_WUSB_0100);
514                 offset = offsetof(struct uwb_rc_evt_beacon_WUSB_0100,
515                                   wBeaconInfoLength);
516                 event_size = hwarc_get_event_size(rc, rceb, core_size,
517                                                   offset, buf_size);
518                 if (event_size < 0)
519                         goto out;
520                 *_real_size = event_size;
521                 result = hwarc_filter_evt_beacon_WUSB_0100(rc, header,
522                                                            buf_size, _new_size);
523                 break;
524         case UWB_RC_EVT_BP_SLOT_CHANGE:
525                 *_new_size = *_real_size =
526                         sizeof(struct uwb_rc_evt_bp_slot_change_WUSB_0100);
527                 result = 0;
528                 break;
529
530         case UWB_RC_EVT_DRP_AVAIL:
531                 core_size = sizeof(struct uwb_rc_evt_drp_avail_WUSB_0100);
532                 offset = offsetof(struct uwb_rc_evt_drp_avail_WUSB_0100,
533                                   wIELength);
534                 event_size = hwarc_get_event_size(rc, rceb, core_size,
535                                                   offset, buf_size);
536                 if (event_size < 0)
537                         goto out;
538                 *_real_size = event_size;
539                 result = hwarc_filter_evt_drp_avail_WUSB_0100(
540                         rc, header, buf_size, _new_size);
541                 break;
542
543         case UWB_RC_EVT_DRP:
544                 core_size = sizeof(struct uwb_rc_evt_drp_WUSB_0100);
545                 offset = offsetof(struct uwb_rc_evt_drp_WUSB_0100, wIELength);
546                 event_size = hwarc_get_event_size(rc, rceb, core_size,
547                                                   offset, buf_size);
548                 if (event_size < 0)
549                         goto out;
550                 *_real_size = event_size;
551                 result = hwarc_filter_evt_drp_WUSB_0100(rc, header,
552                                                         buf_size, _new_size);
553                 break;
554
555         default:
556                 break;
557         }
558 out:
559         return result;
560 }
561
562 /**
563  * Filter data from WUSB device to WHCI driver
564  *
565  * @header:      incoming event
566  * @buf_size:    size of buffer in which event arrived
567  * @_event_size: actual size of event in the buffer
568  * @_new_size:   size of event after filtered
569  *
570  * Filter events based on which protocol the device supports. The WUSB
571  * errata should be the same as WHCI 0.95 so we do not filter that here -
572  * only WUSB 1.0.
573  *
574  * If we don't handle it, we return -ENOANO (why the weird error code?
575  * well, so if I get it, I can pinpoint in the code that raised
576  * it...after all, not too many places use the higher error codes).
577  */
578 static
579 int hwarc_filter_event(struct uwb_rc *rc, struct uwb_rceb **header,
580                        const size_t buf_size, size_t *_real_size,
581                        size_t *_new_size)
582 {
583         int result = -ENOANO;
584         if (rc->version == 0x0100)
585                 result =  hwarc_filter_event_WUSB_0100(
586                         rc, header, buf_size, _real_size, _new_size);
587         return result;
588 }
589
590
591 /**
592  * Execute an UWB RC command on HWA
593  *
594  * @rc:       Instance of a Radio Controller that is a HWA
595  * @cmd:      Buffer containing the RCCB and payload to execute
596  * @cmd_size: Size of the command buffer.
597  *
598  * NOTE: rc's mutex has to be locked
599  */
600 static
601 int hwarc_cmd(struct uwb_rc *uwb_rc, const struct uwb_rccb *cmd, size_t cmd_size)
602 {
603         struct hwarc *hwarc = uwb_rc->priv;
604         return usb_control_msg(
605                 hwarc->usb_dev, usb_sndctrlpipe(hwarc->usb_dev, 0),
606                 WA_EXEC_RC_CMD, USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
607                 0, hwarc->usb_iface->cur_altsetting->desc.bInterfaceNumber,
608                 (void *) cmd, cmd_size, 100 /* FIXME: this is totally arbitrary */);
609 }
610
611 static
612 int hwarc_reset(struct uwb_rc *uwb_rc)
613 {
614         struct hwarc *hwarc = uwb_rc->priv;
615         return usb_reset_device(hwarc->usb_dev);
616 }
617
618 /**
619  * Callback for the notification and event endpoint
620  *
621  * Check's that everything is fine and then passes the read data to
622  * the notification/event handling mechanism (neh).
623  */
624 static
625 void hwarc_neep_cb(struct urb *urb)
626 {
627         struct hwarc *hwarc = urb->context;
628         struct usb_interface *usb_iface = hwarc->usb_iface;
629         struct device *dev = &usb_iface->dev;
630         int result;
631
632         switch (result = urb->status) {
633         case 0:
634                 d_printf(3, dev, "NEEP: receive stat %d, %zu bytes\n",
635                          urb->status, (size_t)urb->actual_length);
636                 uwb_rc_neh_grok(hwarc->uwb_rc, urb->transfer_buffer,
637                                 urb->actual_length);
638                 break;
639         case -ECONNRESET:       /* Not an error, but a controlled situation; */
640         case -ENOENT:           /* (we killed the URB)...so, no broadcast */
641                 d_printf(2, dev, "NEEP: URB reset/noent %d\n", urb->status);
642                 goto out;
643         case -ESHUTDOWN:        /* going away! */
644                 d_printf(2, dev, "NEEP: URB down %d\n", urb->status);
645                 goto out;
646         default:                /* On general errors, retry unless it gets ugly */
647                 if (edc_inc(&hwarc->neep_edc, EDC_MAX_ERRORS,
648                             EDC_ERROR_TIMEFRAME))
649                         goto error_exceeded;
650                 dev_err(dev, "NEEP: URB error %d\n", urb->status);
651         }
652         result = usb_submit_urb(urb, GFP_ATOMIC);
653         d_printf(3, dev, "NEEP: submit %d\n", result);
654         if (result < 0) {
655                 dev_err(dev, "NEEP: Can't resubmit URB (%d) resetting device\n",
656                         result);
657                 goto error;
658         }
659 out:
660         return;
661
662 error_exceeded:
663         dev_err(dev, "NEEP: URB max acceptable errors "
664                 "exceeded, resetting device\n");
665 error:
666         uwb_rc_neh_error(hwarc->uwb_rc, result);
667         uwb_rc_reset_all(hwarc->uwb_rc);
668         return;
669 }
670
671 static void hwarc_init(struct hwarc *hwarc)
672 {
673         edc_init(&hwarc->neep_edc);
674 }
675
676 /**
677  * Initialize the notification/event endpoint stuff
678  *
679  * Note this is effectively a parallel thread; it knows that
680  * hwarc->uwb_rc always exists because the existence of a 'hwarc'
681  * means that there is a reverence on the hwarc->uwb_rc (see
682  * _probe()), and thus _neep_cb() can execute safely.
683  */
684 static int hwarc_neep_init(struct uwb_rc *rc)
685 {
686         struct hwarc *hwarc = rc->priv;
687         struct usb_interface *iface = hwarc->usb_iface;
688         struct usb_device *usb_dev = interface_to_usbdev(iface);
689         struct device *dev = &iface->dev;
690         int result;
691         struct usb_endpoint_descriptor *epd;
692
693         epd = &iface->cur_altsetting->endpoint[0].desc;
694         hwarc->rd_buffer = (void *) __get_free_page(GFP_KERNEL);
695         if (hwarc->rd_buffer == NULL) {
696                 dev_err(dev, "Unable to allocate notification's read buffer\n");
697                 goto error_rd_buffer;
698         }
699         hwarc->neep_urb = usb_alloc_urb(0, GFP_KERNEL);
700         if (hwarc->neep_urb == NULL) {
701                 dev_err(dev, "Unable to allocate notification URB\n");
702                 goto error_urb_alloc;
703         }
704         usb_fill_int_urb(hwarc->neep_urb, usb_dev,
705                          usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
706                          hwarc->rd_buffer, PAGE_SIZE,
707                          hwarc_neep_cb, hwarc, epd->bInterval);
708         result = usb_submit_urb(hwarc->neep_urb, GFP_ATOMIC);
709         if (result < 0) {
710                 dev_err(dev, "Cannot submit notification URB: %d\n", result);
711                 goto error_neep_submit;
712         }
713         return 0;
714
715 error_neep_submit:
716         usb_free_urb(hwarc->neep_urb);
717 error_urb_alloc:
718         free_page((unsigned long)hwarc->rd_buffer);
719 error_rd_buffer:
720         return -ENOMEM;
721 }
722
723
724 /** Clean up all the notification endpoint resources */
725 static void hwarc_neep_release(struct uwb_rc *rc)
726 {
727         struct hwarc *hwarc = rc->priv;
728
729         usb_kill_urb(hwarc->neep_urb);
730         usb_free_urb(hwarc->neep_urb);
731         free_page((unsigned long)hwarc->rd_buffer);
732 }
733
734 /**
735  * Get the version from class-specific descriptor
736  *
737  * NOTE: this descriptor comes with the big bundled configuration
738  *       descriptor that includes the interfaces' and endpoints', so
739  *       we just look for it in the cached copy kept by the USB stack.
740  *
741  * NOTE2: We convert LE fields to CPU order.
742  */
743 static int hwarc_get_version(struct uwb_rc *rc)
744 {
745         int result;
746
747         struct hwarc *hwarc = rc->priv;
748         struct uwb_rc_control_intf_class_desc *descr;
749         struct device *dev = &rc->uwb_dev.dev;
750         struct usb_device *usb_dev = hwarc->usb_dev;
751         char *itr;
752         struct usb_descriptor_header *hdr;
753         size_t itr_size, actconfig_idx;
754         u16 version;
755
756         actconfig_idx = (usb_dev->actconfig - usb_dev->config) /
757                 sizeof(usb_dev->config[0]);
758         itr = usb_dev->rawdescriptors[actconfig_idx];
759         itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
760         while (itr_size >= sizeof(*hdr)) {
761                 hdr = (struct usb_descriptor_header *) itr;
762                 d_printf(3, dev, "Extra device descriptor: "
763                          "type %02x/%u bytes @ %zu (%zu left)\n",
764                          hdr->bDescriptorType, hdr->bLength,
765                          (itr - usb_dev->rawdescriptors[actconfig_idx]),
766                          itr_size);
767                 if (hdr->bDescriptorType == USB_DT_CS_RADIO_CONTROL)
768                         goto found;
769                 itr += hdr->bLength;
770                 itr_size -= hdr->bLength;
771         }
772         dev_err(dev, "cannot find Radio Control Interface Class descriptor\n");
773         return -ENODEV;
774
775 found:
776         result = -EINVAL;
777         if (hdr->bLength > itr_size) {  /* is it available? */
778                 dev_err(dev, "incomplete Radio Control Interface Class "
779                         "descriptor (%zu bytes left, %u needed)\n",
780                         itr_size, hdr->bLength);
781                 goto error;
782         }
783         if (hdr->bLength < sizeof(*descr)) {
784                 dev_err(dev, "short Radio Control Interface Class "
785                         "descriptor\n");
786                 goto error;
787         }
788         descr = (struct uwb_rc_control_intf_class_desc *) hdr;
789         /* Make LE fields CPU order */
790         version = __le16_to_cpu(descr->bcdRCIVersion);
791         if (version != 0x0100) {
792                 dev_err(dev, "Device reports protocol version 0x%04x. We "
793                         "do not support that. \n", version);
794                 result = -EINVAL;
795                 goto error;
796         }
797         rc->version = version;
798         d_printf(3, dev, "Device supports WUSB protocol version 0x%04x \n",
799                  rc->version);
800         result = 0;
801 error:
802         return result;
803 }
804
805 /*
806  * By creating a 'uwb_rc', we have a reference on it -- that reference
807  * is the one we drop when we disconnect.
808  *
809  * No need to switch altsettings; according to WUSB1.0[8.6.1.1], there
810  * is only one altsetting allowed.
811  */
812 static int hwarc_probe(struct usb_interface *iface,
813                        const struct usb_device_id *id)
814 {
815         int result;
816         struct uwb_rc *uwb_rc;
817         struct hwarc *hwarc;
818         struct device *dev = &iface->dev;
819
820         result = -ENOMEM;
821         uwb_rc = uwb_rc_alloc();
822         if (uwb_rc == NULL) {
823                 dev_err(dev, "unable to allocate RC instance\n");
824                 goto error_rc_alloc;
825         }
826         hwarc = kzalloc(sizeof(*hwarc), GFP_KERNEL);
827         if (hwarc == NULL) {
828                 dev_err(dev, "unable to allocate HWA RC instance\n");
829                 goto error_alloc;
830         }
831         hwarc_init(hwarc);
832         hwarc->usb_dev = usb_get_dev(interface_to_usbdev(iface));
833         hwarc->usb_iface = usb_get_intf(iface);
834         hwarc->uwb_rc = uwb_rc;
835
836         uwb_rc->owner = THIS_MODULE;
837         uwb_rc->start = hwarc_neep_init;
838         uwb_rc->stop  = hwarc_neep_release;
839         uwb_rc->cmd   = hwarc_cmd;
840         uwb_rc->reset = hwarc_reset;
841         if (id->driver_info & WUSB_QUIRK_WHCI_CMD_EVT) {
842                 uwb_rc->filter_cmd   = NULL;
843                 uwb_rc->filter_event = NULL;
844         } else {
845                 uwb_rc->filter_cmd   = hwarc_filter_cmd;
846                 uwb_rc->filter_event = hwarc_filter_event;
847         }
848
849         result = uwb_rc_add(uwb_rc, dev, hwarc);
850         if (result < 0)
851                 goto error_rc_add;
852         result = hwarc_get_version(uwb_rc);
853         if (result < 0) {
854                 dev_err(dev, "cannot retrieve version of RC \n");
855                 goto error_get_version;
856         }
857         usb_set_intfdata(iface, hwarc);
858         return 0;
859
860 error_get_version:
861         uwb_rc_rm(uwb_rc);
862 error_rc_add:
863         usb_put_intf(iface);
864         usb_put_dev(hwarc->usb_dev);
865 error_alloc:
866         uwb_rc_put(uwb_rc);
867 error_rc_alloc:
868         return result;
869 }
870
871 static void hwarc_disconnect(struct usb_interface *iface)
872 {
873         struct hwarc *hwarc = usb_get_intfdata(iface);
874         struct uwb_rc *uwb_rc = hwarc->uwb_rc;
875
876         usb_set_intfdata(hwarc->usb_iface, NULL);
877         uwb_rc_rm(uwb_rc);
878         usb_put_intf(hwarc->usb_iface);
879         usb_put_dev(hwarc->usb_dev);
880         d_printf(1, &hwarc->usb_iface->dev, "freed hwarc %p\n", hwarc);
881         kfree(hwarc);
882         uwb_rc_put(uwb_rc);     /* when creating the device, refcount = 1 */
883 }
884
885 /** USB device ID's that we handle */
886 static struct usb_device_id hwarc_id_table[] = {
887         /* D-Link DUB-1210 */
888         { USB_DEVICE_AND_INTERFACE_INFO(0x07d1, 0x3d02, 0xe0, 0x01, 0x02),
889           .driver_info = WUSB_QUIRK_WHCI_CMD_EVT },
890         /* Intel i1480 (using firmware 1.3PA2-20070828) */
891         { USB_DEVICE_AND_INTERFACE_INFO(0x8086, 0x0c3b, 0xe0, 0x01, 0x02),
892           .driver_info = WUSB_QUIRK_WHCI_CMD_EVT },
893         /* Generic match for the Radio Control interface */
894         { USB_INTERFACE_INFO(0xe0, 0x01, 0x02), },
895         { },
896 };
897 MODULE_DEVICE_TABLE(usb, hwarc_id_table);
898
899 static struct usb_driver hwarc_driver = {
900         .name =         "hwa-rc",
901         .probe =        hwarc_probe,
902         .disconnect =   hwarc_disconnect,
903         .id_table =     hwarc_id_table,
904 };
905
906 static int __init hwarc_driver_init(void)
907 {
908         int result;
909         result = usb_register(&hwarc_driver);
910         if (result < 0)
911                 printk(KERN_ERR "HWA-RC: Cannot register USB driver: %d\n",
912                        result);
913         return result;
914
915 }
916 module_init(hwarc_driver_init);
917
918 static void __exit hwarc_driver_exit(void)
919 {
920         usb_deregister(&hwarc_driver);
921 }
922 module_exit(hwarc_driver_exit);
923
924 MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
925 MODULE_DESCRIPTION("Host Wireless Adapter Radio Control Driver");
926 MODULE_LICENSE("GPL");