2 * WUSB Host Wire Adapter: Radio Control Interface (WUSB[8.6])
3 * Radio Control command/event transport
5 * Copyright (C) 2005-2006 Intel Corporation
6 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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
23 * Initialize the Radio Control interface Driver.
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.
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.
33 * Command and event filtering.
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:
44 * UWB_RC_EVT_BP_SLOT_CHANGE
45 * UWB_RC_EVT_DRP_AVAIL
49 * UWB_RC_CMD_SET_DRP_IE
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"
63 #include <linux/uwb/debug.h>
67 * Descriptor for an instance of the UWB Radio Control Driver that
68 * attaches to the RCI interface of the Host Wired Adapter.
70 * Unless there is a lock specific to the 'data members', all access
71 * is protected by uwb_rc->mutex.
73 * The NEEP (Notification/Event EndPoint) URB (@neep_urb) writes to
74 * @rd_buffer. Note there is no locking because it is perfectly (heh!)
75 * serialized--probe() submits an URB, callback is called, processes
76 * the data (synchronously), submits another URB, and so on. There is
77 * no concurrent access to the buffer.
80 struct usb_device *usb_dev;
81 struct usb_interface *usb_iface;
82 struct uwb_rc *uwb_rc; /* UWB host controller */
83 struct urb *neep_urb; /* Notification endpoint handling */
85 void *rd_buffer; /* NEEP read buffer */
89 /* Beacon received notification (WUSB 1.0 [8.6.3.2]) */
90 struct uwb_rc_evt_beacon_WUSB_0100 {
96 __le16 wBeaconInfoLength;
98 } __attribute__((packed));
101 * Filter WUSB 1.0 BEACON RCV notification to be WHCI 0.95
103 * @header: the incoming event
104 * @buf_size: size of buffer containing incoming event
105 * @new_size: size of event after filtering completed
107 * The WHCI 0.95 spec has a "Beacon Type" field. This value is unknown at
108 * the time we receive the beacon from WUSB so we just set it to
109 * UWB_RC_BEACON_TYPE_NEIGHBOR as a default.
110 * The solution below allocates memory upon receipt of every beacon from a
111 * WUSB device. This will deteriorate performance. What is the right way to
115 int hwarc_filter_evt_beacon_WUSB_0100(struct uwb_rc *rc,
116 struct uwb_rceb **header,
117 const size_t buf_size,
120 struct uwb_rc_evt_beacon_WUSB_0100 *be;
121 struct uwb_rc_evt_beacon *newbe;
122 size_t bytes_left, ielength;
123 struct device *dev = &rc->uwb_dev.dev;
125 be = container_of(*header, struct uwb_rc_evt_beacon_WUSB_0100, rceb);
126 bytes_left = buf_size;
127 if (bytes_left < sizeof(*be)) {
128 dev_err(dev, "Beacon Received Notification: Not enough data "
129 "to decode for filtering (%zu vs %zu bytes needed)\n",
130 bytes_left, sizeof(*be));
133 bytes_left -= sizeof(*be);
134 ielength = le16_to_cpu(be->wBeaconInfoLength);
135 if (bytes_left < ielength) {
136 dev_err(dev, "Beacon Received Notification: Not enough data "
137 "to decode IEs (%zu vs %zu bytes needed)\n",
138 bytes_left, ielength);
141 newbe = kzalloc(sizeof(*newbe) + ielength, GFP_ATOMIC);
144 newbe->rceb = be->rceb;
145 newbe->bChannelNumber = be->bChannelNumber;
146 newbe->bBeaconType = UWB_RC_BEACON_TYPE_NEIGHBOR;
147 newbe->wBPSTOffset = be->wBPSTOffset;
148 newbe->bLQI = be->bLQI;
149 newbe->bRSSI = be->bRSSI;
150 newbe->wBeaconInfoLength = be->wBeaconInfoLength;
151 memcpy(newbe->BeaconInfo, be->BeaconInfo, ielength);
152 *header = &newbe->rceb;
153 *new_size = sizeof(*newbe) + ielength;
154 return 1; /* calling function will free memory */
158 /* DRP Availability change notification (WUSB 1.0 [8.6.3.8]) */
159 struct uwb_rc_evt_drp_avail_WUSB_0100 {
160 struct uwb_rceb rceb;
163 } __attribute__((packed));
166 * Filter WUSB 1.0 DRP AVAILABILITY CHANGE notification to be WHCI 0.95
168 * @header: the incoming event
169 * @buf_size: size of buffer containing incoming event
170 * @new_size: size of event after filtering completed
173 int hwarc_filter_evt_drp_avail_WUSB_0100(struct uwb_rc *rc,
174 struct uwb_rceb **header,
175 const size_t buf_size,
178 struct uwb_rc_evt_drp_avail_WUSB_0100 *da;
179 struct uwb_rc_evt_drp_avail *newda;
180 struct uwb_ie_hdr *ie_hdr;
181 size_t bytes_left, ielength;
182 struct device *dev = &rc->uwb_dev.dev;
185 da = container_of(*header, struct uwb_rc_evt_drp_avail_WUSB_0100, rceb);
186 bytes_left = buf_size;
187 if (bytes_left < sizeof(*da)) {
188 dev_err(dev, "Not enough data to decode DRP Avail "
189 "Notification for filtering. Expected %zu, "
190 "received %zu.\n", (size_t)sizeof(*da), bytes_left);
193 bytes_left -= sizeof(*da);
194 ielength = le16_to_cpu(da->wIELength);
195 if (bytes_left < ielength) {
196 dev_err(dev, "DRP Avail Notification filter: IE length "
197 "[%zu bytes] does not match actual length "
198 "[%zu bytes].\n", ielength, bytes_left);
201 if (ielength < sizeof(*ie_hdr)) {
202 dev_err(dev, "DRP Avail Notification filter: Not enough "
203 "data to decode IE [%zu bytes, %zu needed]\n",
204 ielength, sizeof(*ie_hdr));
207 ie_hdr = (void *) da->IEData;
208 if (ie_hdr->length > 32) {
209 dev_err(dev, "DRP Availability Change event has unexpected "
210 "length for filtering. Expected < 32 bytes, "
211 "got %zu bytes.\n", (size_t)ie_hdr->length);
214 newda = kzalloc(sizeof(*newda), GFP_ATOMIC);
217 newda->rceb = da->rceb;
218 memcpy(newda->bmp, (u8 *) ie_hdr + sizeof(*ie_hdr), ie_hdr->length);
219 *header = &newda->rceb;
220 *new_size = sizeof(*newda);
221 return 1; /* calling function will free memory */
225 /* DRP notification (WUSB 1.0 [8.6.3.9]) */
226 struct uwb_rc_evt_drp_WUSB_0100 {
227 struct uwb_rceb rceb;
228 struct uwb_dev_addr wSrcAddr;
232 } __attribute__((packed));
235 * Filter WUSB 1.0 DRP Notification to be WHCI 0.95
237 * @header: the incoming event
238 * @buf_size: size of buffer containing incoming event
239 * @new_size: size of event after filtering completed
241 * It is hard to manage DRP reservations without having a Reason code.
242 * Unfortunately there is none in the WUSB spec. We just set the default to
244 * We do not currently use the bBeaconSlotNumber value, so we set this to
248 int hwarc_filter_evt_drp_WUSB_0100(struct uwb_rc *rc,
249 struct uwb_rceb **header,
250 const size_t buf_size,
253 struct uwb_rc_evt_drp_WUSB_0100 *drpev;
254 struct uwb_rc_evt_drp *newdrpev;
255 size_t bytes_left, ielength;
256 struct device *dev = &rc->uwb_dev.dev;
258 drpev = container_of(*header, struct uwb_rc_evt_drp_WUSB_0100, rceb);
259 bytes_left = buf_size;
260 if (bytes_left < sizeof(*drpev)) {
261 dev_err(dev, "Not enough data to decode DRP Notification "
262 "for filtering. Expected %zu, received %zu.\n",
263 (size_t)sizeof(*drpev), bytes_left);
266 ielength = le16_to_cpu(drpev->wIELength);
267 bytes_left -= sizeof(*drpev);
268 if (bytes_left < ielength) {
269 dev_err(dev, "DRP Notification filter: header length [%zu "
270 "bytes] does not match actual length [%zu "
271 "bytes].\n", ielength, bytes_left);
274 newdrpev = kzalloc(sizeof(*newdrpev) + ielength, GFP_ATOMIC);
275 if (newdrpev == NULL)
277 newdrpev->rceb = drpev->rceb;
278 newdrpev->src_addr = drpev->wSrcAddr;
279 newdrpev->reason = UWB_DRP_NOTIF_DRP_IE_RCVD;
280 newdrpev->beacon_slot_number = 0;
281 newdrpev->ie_length = drpev->wIELength;
282 memcpy(newdrpev->ie_data, drpev->IEData, ielength);
283 *header = &newdrpev->rceb;
284 *new_size = sizeof(*newdrpev) + ielength;
285 return 1; /* calling function will free memory */
289 /* Scan Command (WUSB 1.0 [8.6.2.5]) */
290 struct uwb_rc_cmd_scan_WUSB_0100 {
291 struct uwb_rccb rccb;
294 } __attribute__((packed));
297 * Filter WHCI 0.95 SCAN command to be WUSB 1.0 SCAN command
299 * @header: command sent to device (compliant to WHCI 0.95)
300 * @size: size of command sent to device
302 * We only reduce the size by two bytes because the WUSB 1.0 scan command
303 * does not have the last field (wStarttime). Also, make sure we don't send
304 * the device an unexpected scan type.
307 int hwarc_filter_cmd_scan_WUSB_0100(struct uwb_rc *rc,
308 struct uwb_rccb **header,
311 struct uwb_rc_cmd_scan *sc;
313 sc = container_of(*header, struct uwb_rc_cmd_scan, rccb);
315 if (sc->bScanState == UWB_SCAN_ONLY_STARTTIME)
316 sc->bScanState = UWB_SCAN_ONLY;
317 /* Don't send the last two bytes. */
323 /* SET DRP IE command (WUSB 1.0 [8.6.2.7]) */
324 struct uwb_rc_cmd_set_drp_ie_WUSB_0100 {
325 struct uwb_rccb rccb;
328 struct uwb_ie_drp IEData[];
329 } __attribute__((packed));
332 * Filter WHCI 0.95 SET DRP IE command to be WUSB 1.0 SET DRP IE command
334 * @header: command sent to device (compliant to WHCI 0.95)
335 * @size: size of command sent to device
337 * WUSB has an extra bExplicit field - we assume always explicit
338 * negotiation so this field is set. The command expected by the device is
339 * thus larger than the one prepared by the driver so we need to
340 * reallocate memory to accommodate this.
341 * We trust the driver to send us the correct data so no checking is done
342 * on incoming data - evn though it is variable length.
345 int hwarc_filter_cmd_set_drp_ie_WUSB_0100(struct uwb_rc *rc,
346 struct uwb_rccb **header,
349 struct uwb_rc_cmd_set_drp_ie *orgcmd;
350 struct uwb_rc_cmd_set_drp_ie_WUSB_0100 *cmd;
353 orgcmd = container_of(*header, struct uwb_rc_cmd_set_drp_ie, rccb);
354 ielength = le16_to_cpu(orgcmd->wIELength);
355 cmd = kzalloc(sizeof(*cmd) + ielength, GFP_KERNEL);
358 cmd->rccb = orgcmd->rccb;
360 cmd->wIELength = orgcmd->wIELength;
361 memcpy(cmd->IEData, orgcmd->IEData, ielength);
362 *header = &cmd->rccb;
363 *size = sizeof(*cmd) + ielength;
364 return 1; /* calling function will free memory */
369 * Filter data from WHCI driver to WUSB device
371 * @header: WHCI 0.95 compliant command from driver
372 * @size: length of command
374 * The routine managing commands to the device (uwb_rc_cmd()) will call the
375 * filtering function pointer (if it exists) before it passes any data to
376 * the device. At this time the command has been formatted according to
377 * WHCI 0.95 and is ready to be sent to the device.
379 * The filter function will be provided with the current command and its
380 * length. The function will manipulate the command if necessary and
381 * potentially reallocate memory for a command that needed more memory that
382 * the given command. If new memory was created the function will return 1
383 * to indicate to the calling function that the memory need to be freed
384 * when not needed any more. The size will contain the new length of the
386 * If memory has not been allocated we rely on the original mechanisms to
387 * free the memory of the command - even when we reduce the value of size.
390 int hwarc_filter_cmd_WUSB_0100(struct uwb_rc *rc, struct uwb_rccb **header,
394 struct uwb_rccb *rccb = *header;
395 int cmd = le16_to_cpu(rccb->wCommand);
397 case UWB_RC_CMD_SCAN:
398 result = hwarc_filter_cmd_scan_WUSB_0100(rc, header, size);
400 case UWB_RC_CMD_SET_DRP_IE:
401 result = hwarc_filter_cmd_set_drp_ie_WUSB_0100(rc, header, size);
412 * Filter data from WHCI driver to WUSB device
414 * @header: WHCI 0.95 compliant command from driver
415 * @size: length of command
417 * Filter commands based on which protocol the device supports. The WUSB
418 * errata should be the same as WHCI 0.95 so we do not filter that here -
422 int hwarc_filter_cmd(struct uwb_rc *rc, struct uwb_rccb **header,
425 int result = -ENOANO;
426 if (rc->version == 0x0100)
427 result = hwarc_filter_cmd_WUSB_0100(rc, header, size);
433 * Compute return value as sum of incoming value and value at given offset
435 * @rceb: event for which we compute the size, it contains a variable
437 * @core_size: size of the "non variable" part of the event
438 * @offset: place in event where the length of the variable part is stored
439 * @buf_size: total length of buffer in which event arrived - we need to make
440 * sure we read the offset in memory that is still part of the event
443 ssize_t hwarc_get_event_size(struct uwb_rc *rc, const struct uwb_rceb *rceb,
444 size_t core_size, size_t offset,
445 const size_t buf_size)
447 ssize_t size = -ENOSPC;
448 const void *ptr = rceb;
449 size_t type_size = sizeof(__le16);
450 struct device *dev = &rc->uwb_dev.dev;
452 if (offset + type_size >= buf_size) {
453 dev_err(dev, "Not enough data to read extra size of event "
454 "0x%02x/%04x/%02x, only got %zu bytes.\n",
455 rceb->bEventType, le16_to_cpu(rceb->wEvent),
456 rceb->bEventContext, buf_size);
460 size = core_size + le16_to_cpu(*(__le16 *)ptr);
466 /* Beacon slot change notification (WUSB 1.0 [8.6.3.5]) */
467 struct uwb_rc_evt_bp_slot_change_WUSB_0100 {
468 struct uwb_rceb rceb;
470 } __attribute__((packed));
474 * Filter data from WUSB device to WHCI driver
476 * @header: incoming event
477 * @buf_size: size of buffer in which event arrived
478 * @_event_size: actual size of event in the buffer
479 * @new_size: size of event after filtered
481 * We don't know how the buffer is constructed - there may be more than one
482 * event in it so buffer length does not determine event length. We first
483 * determine the expected size of the incoming event. This value is passed
484 * back only if the actual filtering succeeded (so we know the computed
485 * expected size is correct). This value will be zero if
486 * the event did not need any filtering.
488 * WHCI interprets the BP Slot Change event's data differently than
489 * WUSB. The event sizes are exactly the same. The data field
490 * indicates the new beacon slot in which a RC is transmitting its
491 * beacon. The maximum value of this is 96 (wMacBPLength ECMA-368
492 * 17.16 (Table 117)). We thus know that the WUSB value will not set
493 * the bit bNoSlot, so we don't really do anything (placeholder).
496 int hwarc_filter_event_WUSB_0100(struct uwb_rc *rc, struct uwb_rceb **header,
497 const size_t buf_size, size_t *_real_size,
500 int result = -ENOANO;
501 struct uwb_rceb *rceb = *header;
502 int event = le16_to_cpu(rceb->wEvent);
504 size_t core_size, offset;
506 if (rceb->bEventType != UWB_RC_CET_GENERAL)
509 case UWB_RC_EVT_BEACON:
510 core_size = sizeof(struct uwb_rc_evt_beacon_WUSB_0100);
511 offset = offsetof(struct uwb_rc_evt_beacon_WUSB_0100,
513 event_size = hwarc_get_event_size(rc, rceb, core_size,
517 *_real_size = event_size;
518 result = hwarc_filter_evt_beacon_WUSB_0100(rc, header,
519 buf_size, _new_size);
521 case UWB_RC_EVT_BP_SLOT_CHANGE:
522 *_new_size = *_real_size =
523 sizeof(struct uwb_rc_evt_bp_slot_change_WUSB_0100);
527 case UWB_RC_EVT_DRP_AVAIL:
528 core_size = sizeof(struct uwb_rc_evt_drp_avail_WUSB_0100);
529 offset = offsetof(struct uwb_rc_evt_drp_avail_WUSB_0100,
531 event_size = hwarc_get_event_size(rc, rceb, core_size,
535 *_real_size = event_size;
536 result = hwarc_filter_evt_drp_avail_WUSB_0100(
537 rc, header, buf_size, _new_size);
541 core_size = sizeof(struct uwb_rc_evt_drp_WUSB_0100);
542 offset = offsetof(struct uwb_rc_evt_drp_WUSB_0100, wIELength);
543 event_size = hwarc_get_event_size(rc, rceb, core_size,
547 *_real_size = event_size;
548 result = hwarc_filter_evt_drp_WUSB_0100(rc, header,
549 buf_size, _new_size);
560 * Filter data from WUSB device to WHCI driver
562 * @header: incoming event
563 * @buf_size: size of buffer in which event arrived
564 * @_event_size: actual size of event in the buffer
565 * @_new_size: size of event after filtered
567 * Filter events based on which protocol the device supports. The WUSB
568 * errata should be the same as WHCI 0.95 so we do not filter that here -
571 * If we don't handle it, we return -ENOANO (why the weird error code?
572 * well, so if I get it, I can pinpoint in the code that raised
573 * it...after all, not too many places use the higher error codes).
576 int hwarc_filter_event(struct uwb_rc *rc, struct uwb_rceb **header,
577 const size_t buf_size, size_t *_real_size,
580 int result = -ENOANO;
581 if (rc->version == 0x0100)
582 result = hwarc_filter_event_WUSB_0100(
583 rc, header, buf_size, _real_size, _new_size);
589 * Execute an UWB RC command on HWA
591 * @rc: Instance of a Radio Controller that is a HWA
592 * @cmd: Buffer containing the RCCB and payload to execute
593 * @cmd_size: Size of the command buffer.
595 * NOTE: rc's mutex has to be locked
598 int hwarc_cmd(struct uwb_rc *uwb_rc, const struct uwb_rccb *cmd, size_t cmd_size)
600 struct hwarc *hwarc = uwb_rc->priv;
601 return usb_control_msg(
602 hwarc->usb_dev, usb_sndctrlpipe(hwarc->usb_dev, 0),
603 WA_EXEC_RC_CMD, USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
604 0, hwarc->usb_iface->cur_altsetting->desc.bInterfaceNumber,
605 (void *) cmd, cmd_size, 100 /* FIXME: this is totally arbitrary */);
609 int hwarc_reset(struct uwb_rc *uwb_rc)
611 struct hwarc *hwarc = uwb_rc->priv;
612 return usb_reset_device(hwarc->usb_dev);
616 * Callback for the notification and event endpoint
618 * Check's that everything is fine and then passes the read data to
619 * the notification/event handling mechanism (neh).
622 void hwarc_neep_cb(struct urb *urb)
624 struct hwarc *hwarc = urb->context;
625 struct usb_interface *usb_iface = hwarc->usb_iface;
626 struct device *dev = &usb_iface->dev;
629 switch (result = urb->status) {
631 d_printf(3, dev, "NEEP: receive stat %d, %zu bytes\n",
632 urb->status, (size_t)urb->actual_length);
633 uwb_rc_neh_grok(hwarc->uwb_rc, urb->transfer_buffer,
636 case -ECONNRESET: /* Not an error, but a controlled situation; */
637 case -ENOENT: /* (we killed the URB)...so, no broadcast */
638 d_printf(2, dev, "NEEP: URB reset/noent %d\n", urb->status);
640 case -ESHUTDOWN: /* going away! */
641 d_printf(2, dev, "NEEP: URB down %d\n", urb->status);
643 default: /* On general errors, retry unless it gets ugly */
644 if (edc_inc(&hwarc->neep_edc, EDC_MAX_ERRORS,
645 EDC_ERROR_TIMEFRAME))
647 dev_err(dev, "NEEP: URB error %d\n", urb->status);
649 result = usb_submit_urb(urb, GFP_ATOMIC);
650 d_printf(3, dev, "NEEP: submit %d\n", result);
652 dev_err(dev, "NEEP: Can't resubmit URB (%d) resetting device\n",
660 dev_err(dev, "NEEP: URB max acceptable errors "
661 "exceeded, resetting device\n");
663 uwb_rc_neh_error(hwarc->uwb_rc, result);
664 uwb_rc_reset_all(hwarc->uwb_rc);
668 static void hwarc_init(struct hwarc *hwarc)
670 edc_init(&hwarc->neep_edc);
674 * Initialize the notification/event endpoint stuff
676 * Note this is effectively a parallel thread; it knows that
677 * hwarc->uwb_rc always exists because the existence of a 'hwarc'
678 * means that there is a reverence on the hwarc->uwb_rc (see
679 * _probe()), and thus _neep_cb() can execute safely.
681 static int hwarc_neep_init(struct uwb_rc *rc)
683 struct hwarc *hwarc = rc->priv;
684 struct usb_interface *iface = hwarc->usb_iface;
685 struct usb_device *usb_dev = interface_to_usbdev(iface);
686 struct device *dev = &iface->dev;
688 struct usb_endpoint_descriptor *epd;
690 epd = &iface->cur_altsetting->endpoint[0].desc;
691 hwarc->rd_buffer = (void *) __get_free_page(GFP_KERNEL);
692 if (hwarc->rd_buffer == NULL) {
693 dev_err(dev, "Unable to allocate notification's read buffer\n");
694 goto error_rd_buffer;
696 hwarc->neep_urb = usb_alloc_urb(0, GFP_KERNEL);
697 if (hwarc->neep_urb == NULL) {
698 dev_err(dev, "Unable to allocate notification URB\n");
699 goto error_urb_alloc;
701 usb_fill_int_urb(hwarc->neep_urb, usb_dev,
702 usb_rcvintpipe(usb_dev, epd->bEndpointAddress),
703 hwarc->rd_buffer, PAGE_SIZE,
704 hwarc_neep_cb, hwarc, epd->bInterval);
705 result = usb_submit_urb(hwarc->neep_urb, GFP_ATOMIC);
707 dev_err(dev, "Cannot submit notification URB: %d\n", result);
708 goto error_neep_submit;
713 usb_free_urb(hwarc->neep_urb);
715 free_page((unsigned long)hwarc->rd_buffer);
721 /** Clean up all the notification endpoint resources */
722 static void hwarc_neep_release(struct uwb_rc *rc)
724 struct hwarc *hwarc = rc->priv;
726 usb_kill_urb(hwarc->neep_urb);
727 usb_free_urb(hwarc->neep_urb);
728 free_page((unsigned long)hwarc->rd_buffer);
732 * Get the version from class-specific descriptor
734 * NOTE: this descriptor comes with the big bundled configuration
735 * descriptor that includes the interfaces' and endpoints', so
736 * we just look for it in the cached copy kept by the USB stack.
738 * NOTE2: We convert LE fields to CPU order.
740 static int hwarc_get_version(struct uwb_rc *rc)
744 struct hwarc *hwarc = rc->priv;
745 struct uwb_rc_control_intf_class_desc *descr;
746 struct device *dev = &rc->uwb_dev.dev;
747 struct usb_device *usb_dev = hwarc->usb_dev;
749 struct usb_descriptor_header *hdr;
750 size_t itr_size, actconfig_idx;
753 actconfig_idx = (usb_dev->actconfig - usb_dev->config) /
754 sizeof(usb_dev->config[0]);
755 itr = usb_dev->rawdescriptors[actconfig_idx];
756 itr_size = le16_to_cpu(usb_dev->actconfig->desc.wTotalLength);
757 while (itr_size >= sizeof(*hdr)) {
758 hdr = (struct usb_descriptor_header *) itr;
759 d_printf(3, dev, "Extra device descriptor: "
760 "type %02x/%u bytes @ %zu (%zu left)\n",
761 hdr->bDescriptorType, hdr->bLength,
762 (itr - usb_dev->rawdescriptors[actconfig_idx]),
764 if (hdr->bDescriptorType == USB_DT_CS_RADIO_CONTROL)
767 itr_size -= hdr->bLength;
769 dev_err(dev, "cannot find Radio Control Interface Class descriptor\n");
774 if (hdr->bLength > itr_size) { /* is it available? */
775 dev_err(dev, "incomplete Radio Control Interface Class "
776 "descriptor (%zu bytes left, %u needed)\n",
777 itr_size, hdr->bLength);
780 if (hdr->bLength < sizeof(*descr)) {
781 dev_err(dev, "short Radio Control Interface Class "
785 descr = (struct uwb_rc_control_intf_class_desc *) hdr;
786 /* Make LE fields CPU order */
787 version = __le16_to_cpu(descr->bcdRCIVersion);
788 if (version != 0x0100) {
789 dev_err(dev, "Device reports protocol version 0x%04x. We "
790 "do not support that. \n", version);
794 rc->version = version;
795 d_printf(3, dev, "Device supports WUSB protocol version 0x%04x \n",
803 * By creating a 'uwb_rc', we have a reference on it -- that reference
804 * is the one we drop when we disconnect.
806 * No need to switch altsettings; according to WUSB1.0[8.6.1.1], there
807 * is only one altsetting allowed.
809 static int hwarc_probe(struct usb_interface *iface,
810 const struct usb_device_id *id)
813 struct uwb_rc *uwb_rc;
815 struct device *dev = &iface->dev;
818 uwb_rc = uwb_rc_alloc();
819 if (uwb_rc == NULL) {
820 dev_err(dev, "unable to allocate RC instance\n");
823 hwarc = kzalloc(sizeof(*hwarc), GFP_KERNEL);
825 dev_err(dev, "unable to allocate HWA RC instance\n");
829 hwarc->usb_dev = usb_get_dev(interface_to_usbdev(iface));
830 hwarc->usb_iface = usb_get_intf(iface);
831 hwarc->uwb_rc = uwb_rc;
833 uwb_rc->owner = THIS_MODULE;
834 uwb_rc->start = hwarc_neep_init;
835 uwb_rc->stop = hwarc_neep_release;
836 uwb_rc->cmd = hwarc_cmd;
837 uwb_rc->reset = hwarc_reset;
838 uwb_rc->filter_cmd = hwarc_filter_cmd;
839 uwb_rc->filter_event = hwarc_filter_event;
841 result = uwb_rc_add(uwb_rc, dev, hwarc);
844 result = hwarc_get_version(uwb_rc);
846 dev_err(dev, "cannot retrieve version of RC \n");
847 goto error_get_version;
849 usb_set_intfdata(iface, hwarc);
856 usb_put_dev(hwarc->usb_dev);
863 static void hwarc_disconnect(struct usb_interface *iface)
865 struct hwarc *hwarc = usb_get_intfdata(iface);
866 struct uwb_rc *uwb_rc = hwarc->uwb_rc;
868 usb_set_intfdata(hwarc->usb_iface, NULL);
870 usb_put_intf(hwarc->usb_iface);
871 usb_put_dev(hwarc->usb_dev);
872 d_printf(1, &hwarc->usb_iface->dev, "freed hwarc %p\n", hwarc);
874 uwb_rc_put(uwb_rc); /* when creating the device, refcount = 1 */
877 /** USB device ID's that we handle */
878 static struct usb_device_id hwarc_id_table[] = {
879 { USB_INTERFACE_INFO(0xe0, 0x01, 0x02), },
882 MODULE_DEVICE_TABLE(usb, hwarc_id_table);
884 static struct usb_driver hwarc_driver = {
886 .probe = hwarc_probe,
887 .disconnect = hwarc_disconnect,
888 .id_table = hwarc_id_table,
891 static int __init hwarc_driver_init(void)
894 result = usb_register(&hwarc_driver);
896 printk(KERN_ERR "HWA-RC: Cannot register USB driver: %d\n",
901 module_init(hwarc_driver_init);
903 static void __exit hwarc_driver_exit(void)
905 usb_deregister(&hwarc_driver);
907 module_exit(hwarc_driver_exit);
909 MODULE_AUTHOR("Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>");
910 MODULE_DESCRIPTION("Host Wireless Adapter Radio Control Driver");
911 MODULE_LICENSE("GPL");