1 /*
2 * (C) Copyright Linus Torvalds 1999
3 * (C) Copyright Johannes Erdfelt 1999-2001
4 * (C) Copyright Andreas Gal 1999
5 * (C) Copyright Gregory P. Smith 1999
6 * (C) Copyright Deti Fliegl 1999
7 * (C) Copyright Randy Dunlap 2000
8 * (C) Copyright David Brownell 2000-2002
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 * for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
25 #include <linux/config.h>
26
27 #ifdef CONFIG_USB_DEBUG
28 #define DEBUG
29 #endif
30
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/completion.h>
36 #include <linux/utsname.h>
37 #include <linux/mm.h>
38 #include <asm/io.h>
39 #include <asm/scatterlist.h>
40 #include <linux/device.h>
41 #include <linux/dma-mapping.h>
42 #include <asm/irq.h>
43 #include <asm/byteorder.h>
44
45 #include <linux/usb.h>
46
47 #include "usb.h"
48 #include "hcd.h"
49
50
51 // #define USB_BANDWIDTH_MESSAGES
52
53 /*-------------------------------------------------------------------------*/
54
55 /*
56 * USB Host Controller Driver framework
57 *
58 * Plugs into usbcore (usb_bus) and lets HCDs share code, minimizing
59 * HCD-specific behaviors/bugs.
60 *
61 * This does error checks, tracks devices and urbs, and delegates to a
62 * "hc_driver" only for code (and data) that really needs to know about
63 * hardware differences. That includes root hub registers, i/o queues,
64 * and so on ... but as little else as possible.
65 *
66 * Shared code includes most of the "root hub" code (these are emulated,
67 * though each HC's hardware works differently) and PCI glue, plus request
68 * tracking overhead. The HCD code should only block on spinlocks or on
69 * hardware handshaking; blocking on software events (such as other kernel
70 * threads releasing resources, or completing actions) is all generic.
71 *
72 * Happens the USB 2.0 spec says this would be invisible inside the "USBD",
73 * and includes mostly a "HCDI" (HCD Interface) along with some APIs used
74 * only by the hub driver ... and that neither should be seen or used by
75 * usb client device drivers.
76 *
77 * Contributors of ideas or unattributed patches include: David Brownell,
78 * Roman Weissgaerber, Rory Bolt, Greg Kroah-Hartman, ...
79 *
80 * HISTORY:
81 * 2002-02-21 Pull in most of the usb_bus support from usb.c; some
82 * associated cleanup. "usb_hcd" still != "usb_bus".
83 * 2001-12-12 Initial patch version for Linux 2.5.1 kernel.
84 */
85
86 /*-------------------------------------------------------------------------*/
87
88 /* host controllers we manage */
89 LIST_HEAD (usb_bus_list);
90
91 /* used when allocating bus numbers */
92 #define USB_MAXBUS 64
93 struct usb_busmap {
94 unsigned long busmap [USB_MAXBUS / (8*sizeof (unsigned long))];
95 };
96 static struct usb_busmap busmap;
97
98 /* used when updating list of hcds */
99 DECLARE_MUTEX (usb_bus_list_lock); /* exported only for usbfs */
100
101 /* used when updating hcd data */
102 static DEFINE_SPINLOCK(hcd_data_lock);
103
104 /* wait queue for synchronous unlinks */
105 DECLARE_WAIT_QUEUE_HEAD(usb_kill_urb_queue);
106
107 /*-------------------------------------------------------------------------*/
108
109 /*
110 * Sharable chunks of root hub code.
111 */
112
113 /*-------------------------------------------------------------------------*/
114
115 #define KERNEL_REL ((LINUX_VERSION_CODE >> 16) & 0x0ff)
116 #define KERNEL_VER ((LINUX_VERSION_CODE >> 8) & 0x0ff)
117
118 /* usb 2.0 root hub device descriptor */
119 static const u8 usb2_rh_dev_descriptor [18] = {
120 0x12, /* __u8 bLength; */
121 0x01, /* __u8 bDescriptorType; Device */
122 0x00, 0x02, /* __le16 bcdUSB; v2.0 */
123
124 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
125 0x00, /* __u8 bDeviceSubClass; */
126 0x01, /* __u8 bDeviceProtocol; [ usb 2.0 single TT ]*/
127 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
128
129 0x00, 0x00, /* __le16 idVendor; */
130 0x00, 0x00, /* __le16 idProduct; */
131 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
132
133 0x03, /* __u8 iManufacturer; */
134 0x02, /* __u8 iProduct; */
135 0x01, /* __u8 iSerialNumber; */
136 0x01 /* __u8 bNumConfigurations; */
137 };
138
139 /* no usb 2.0 root hub "device qualifier" descriptor: one speed only */
140
141 /* usb 1.1 root hub device descriptor */
142 static const u8 usb11_rh_dev_descriptor [18] = {
143 0x12, /* __u8 bLength; */
144 0x01, /* __u8 bDescriptorType; Device */
145 0x10, 0x01, /* __le16 bcdUSB; v1.1 */
146
147 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
148 0x00, /* __u8 bDeviceSubClass; */
149 0x00, /* __u8 bDeviceProtocol; [ low/full speeds only ] */
150 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
151
152 0x00, 0x00, /* __le16 idVendor; */
153 0x00, 0x00, /* __le16 idProduct; */
154 KERNEL_VER, KERNEL_REL, /* __le16 bcdDevice */
155
156 0x03, /* __u8 iManufacturer; */
157 0x02, /* __u8 iProduct; */
158 0x01, /* __u8 iSerialNumber; */
159 0x01 /* __u8 bNumConfigurations; */
160 };
161
162
163 /*-------------------------------------------------------------------------*/
164
165 /* Configuration descriptors for our root hubs */
166
167 static const u8 fs_rh_config_descriptor [] = {
168
169 /* one configuration */
170 0x09, /* __u8 bLength; */
171 0x02, /* __u8 bDescriptorType; Configuration */
172 0x19, 0x00, /* __le16 wTotalLength; */
173 0x01, /* __u8 bNumInterfaces; (1) */
174 0x01, /* __u8 bConfigurationValue; */
175 0x00, /* __u8 iConfiguration; */
176 0xc0, /* __u8 bmAttributes;
177 Bit 7: must be set,
178 6: Self-powered,
179 5: Remote wakeup,
180 4..0: resvd */
181 0x00, /* __u8 MaxPower; */
182
183 /* USB 1.1:
184 * USB 2.0, single TT organization (mandatory):
185 * one interface, protocol 0
186 *
187 * USB 2.0, multiple TT organization (optional):
188 * two interfaces, protocols 1 (like single TT)
189 * and 2 (multiple TT mode) ... config is
190 * sometimes settable
191 * NOT IMPLEMENTED
192 */
193
194 /* one interface */
195 0x09, /* __u8 if_bLength; */
196 0x04, /* __u8 if_bDescriptorType; Interface */
197 0x00, /* __u8 if_bInterfaceNumber; */
198 0x00, /* __u8 if_bAlternateSetting; */
199 0x01, /* __u8 if_bNumEndpoints; */
200 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
201 0x00, /* __u8 if_bInterfaceSubClass; */
202 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
203 0x00, /* __u8 if_iInterface; */
204
205 /* one endpoint (status change endpoint) */
206 0x07, /* __u8 ep_bLength; */
207 0x05, /* __u8 ep_bDescriptorType; Endpoint */
208 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
209 0x03, /* __u8 ep_bmAttributes; Interrupt */
210 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
211 0xff /* __u8 ep_bInterval; (255ms -- usb 2.0 spec) */
212 };
213
214 static const u8 hs_rh_config_descriptor [] = {
215
216 /* one configuration */
217 0x09, /* __u8 bLength; */
218 0x02, /* __u8 bDescriptorType; Configuration */
219 0x19, 0x00, /* __le16 wTotalLength; */
220 0x01, /* __u8 bNumInterfaces; (1) */
221 0x01, /* __u8 bConfigurationValue; */
222 0x00, /* __u8 iConfiguration; */
223 0xc0, /* __u8 bmAttributes;
224 Bit 7: must be set,
225 6: Self-powered,
226 5: Remote wakeup,
227 4..0: resvd */
228 0x00, /* __u8 MaxPower; */
229
230 /* USB 1.1:
231 * USB 2.0, single TT organization (mandatory):
232 * one interface, protocol 0
233 *
234 * USB 2.0, multiple TT organization (optional):
235 * two interfaces, protocols 1 (like single TT)
236 * and 2 (multiple TT mode) ... config is
237 * sometimes settable
238 * NOT IMPLEMENTED
239 */
240
241 /* one interface */
242 0x09, /* __u8 if_bLength; */
243 0x04, /* __u8 if_bDescriptorType; Interface */
244 0x00, /* __u8 if_bInterfaceNumber; */
245 0x00, /* __u8 if_bAlternateSetting; */
246 0x01, /* __u8 if_bNumEndpoints; */
247 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
248 0x00, /* __u8 if_bInterfaceSubClass; */
249 0x00, /* __u8 if_bInterfaceProtocol; [usb1.1 or single tt] */
250 0x00, /* __u8 if_iInterface; */
251
252 /* one endpoint (status change endpoint) */
253 0x07, /* __u8 ep_bLength; */
254 0x05, /* __u8 ep_bDescriptorType; Endpoint */
255 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
256 0x03, /* __u8 ep_bmAttributes; Interrupt */
257 0x02, 0x00, /* __le16 ep_wMaxPacketSize; 1 + (MAX_ROOT_PORTS / 8) */
258 0x0c /* __u8 ep_bInterval; (256ms -- usb 2.0 spec) */
259 };
260
261 /*-------------------------------------------------------------------------*/
262
263 /*
264 * helper routine for returning string descriptors in UTF-16LE
265 * input can actually be ISO-8859-1; ASCII is its 7-bit subset
266 */
267 static int ascii2utf (char *s, u8 *utf, int utfmax)
268 {
269 int retval;
270
271 for (retval = 0; *s && utfmax > 1; utfmax -= 2, retval += 2) {
272 *utf++ = *s++;
273 *utf++ = 0;
274 }
275 return retval;
276 }
277
278 /*
279 * rh_string - provides manufacturer, product and serial strings for root hub
280 * @id: the string ID number (1: serial number, 2: product, 3: vendor)
281 * @hcd: the host controller for this root hub
282 * @type: string describing our driver
283 * @data: return packet in UTF-16 LE
284 * @len: length of the return packet
285 *
286 * Produces either a manufacturer, product or serial number string for the
287 * virtual root hub device.
288 */
289 static int rh_string (
290 int id,
291 struct usb_hcd *hcd,
292 u8 *data,
293 int len
294 ) {
295 char buf [100];
296
297 // language ids
298 if (id == 0) {
299 *data++ = 4; *data++ = 3; /* 4 bytes string data */
300 *data++ = 0x09; *data++ = 0x04; /* MSFT-speak for "en-us" */
301 return 4;
302
303 // serial number
304 } else if (id == 1) {
305 strcpy (buf, hcd->self.bus_name);
306
307 // product description
308 } else if (id == 2) {
309 strcpy (buf, hcd->product_desc);
310
311 // id 3 == vendor description
312 } else if (id == 3) {
313 sprintf (buf, "%s %s %s", system_utsname.sysname,
314 system_utsname.release, hcd->driver->description);
315
316 // unsupported IDs --> "protocol stall"
317 } else
318 return 0;
319
320 data [0] = 2 * (strlen (buf) + 1);
321 data [1] = 3; /* type == string */
322 return 2 + ascii2utf (buf, data + 2, len - 2);
323 }
324
325
326 /* Root hub control transfers execute synchronously */
327 static int rh_call_control (struct usb_hcd *hcd, struct urb *urb)
328 {
329 struct usb_ctrlrequest *cmd;
330 u16 typeReq, wValue, wIndex, wLength;
331 const u8 *bufp = NULL;
332 u8 *ubuf = urb->transfer_buffer;
333 int len = 0;
334 int patch_wakeup = 0;
335 unsigned long flags;
336
337 cmd = (struct usb_ctrlrequest *) urb->setup_packet;
338 typeReq = (cmd->bRequestType << 8) | cmd->bRequest;
339 wValue = le16_to_cpu (cmd->wValue);
340 wIndex = le16_to_cpu (cmd->wIndex);
341 wLength = le16_to_cpu (cmd->wLength);
342
343 if (wLength > urb->transfer_buffer_length)
344 goto error;
345
346 /* set up for success */
347 urb->status = 0;
348 urb->actual_length = wLength;
349 switch (typeReq) {
350
351 /* DEVICE REQUESTS */
352
353 case DeviceRequest | USB_REQ_GET_STATUS:
354 ubuf [0] = (hcd->remote_wakeup << USB_DEVICE_REMOTE_WAKEUP)
355 | (1 << USB_DEVICE_SELF_POWERED);
356 ubuf [1] = 0;
357 break;
358 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
359 if (wValue == USB_DEVICE_REMOTE_WAKEUP)
360 hcd->remote_wakeup = 0;
361 else
362 goto error;
363 break;
364 case DeviceOutRequest | USB_REQ_SET_FEATURE:
365 if (hcd->can_wakeup && wValue == USB_DEVICE_REMOTE_WAKEUP)
366 hcd->remote_wakeup = 1;
367 else
368 goto error;
369 break;
370 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
371 ubuf [0] = 1;
372 /* FALLTHROUGH */
373 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
374 break;
375 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
376 switch (wValue & 0xff00) {
377 case USB_DT_DEVICE << 8:
378 if (hcd->driver->flags & HCD_USB2)
379 bufp = usb2_rh_dev_descriptor;
380 else if (hcd->driver->flags & HCD_USB11)
381 bufp = usb11_rh_dev_descriptor;
382 else
383 goto error;
384 len = 18;
385 break;
386 case USB_DT_CONFIG << 8:
387 if (hcd->driver->flags & HCD_USB2) {
388 bufp = hs_rh_config_descriptor;
389 len = sizeof hs_rh_config_descriptor;
390 } else {
391 bufp = fs_rh_config_descriptor;
392 len = sizeof fs_rh_config_descriptor;
393 }
394 if (hcd->can_wakeup)
395 patch_wakeup = 1;
396 break;
397 case USB_DT_STRING << 8:
398 urb->actual_length = rh_string (
399 wValue & 0xff, hcd,
400 ubuf, wLength);
401 break;
402 default:
403 goto error;
404 }
405 break;
406 case DeviceRequest | USB_REQ_GET_INTERFACE:
407 ubuf [0] = 0;
408 /* FALLTHROUGH */
409 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
410 break;
411 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
412 // wValue == urb->dev->devaddr
413 dev_dbg (hcd->self.controller, "root hub device address %d\n",
414 wValue);
415 break;
416
417 /* INTERFACE REQUESTS (no defined feature/status flags) */
418
419 /* ENDPOINT REQUESTS */
420
421 case EndpointRequest | USB_REQ_GET_STATUS:
422 // ENDPOINT_HALT flag
423 ubuf [0] = 0;
424 ubuf [1] = 0;
425 /* FALLTHROUGH */
426 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
427 case EndpointOutRequest | USB_REQ_SET_FEATURE:
428 dev_dbg (hcd->self.controller, "no endpoint features yet\n");
429 break;
430
431 /* CLASS REQUESTS (and errors) */
432
433 default:
434 /* non-generic request */
435 if (HCD_IS_SUSPENDED (hcd->state))
436 urb->status = -EAGAIN;
437 else
438 urb->status = hcd->driver->hub_control (hcd,
439 typeReq, wValue, wIndex,
440 ubuf, wLength);
441 break;
442 error:
443 /* "protocol stall" on error */
444 urb->status = -EPIPE;
445 }
446 if (urb->status) {
447 urb->actual_length = 0;
448 if (urb->status != -EPIPE) {
449 dev_dbg (hcd->self.controller,
450 "CTRL: TypeReq=0x%x val=0x%x "
451 "idx=0x%x len=%d ==> %d\n",
452 typeReq, wValue, wIndex,
453 wLength, urb->status);
454 }
455 }
456 if (bufp) {
457 if (urb->transfer_buffer_length < len)
458 len = urb->transfer_buffer_length;
459 urb->actual_length = len;
460 // always USB_DIR_IN, toward host
461 memcpy (ubuf, bufp, len);
462
463 /* report whether RH hardware supports remote wakeup */
464 if (patch_wakeup)
465 ((struct usb_config_descriptor *)ubuf)->bmAttributes
466 |= USB_CONFIG_ATT_WAKEUP;
467 }
468
469 /* any errors get returned through the urb completion */
470 local_irq_save (flags);
471 usb_hcd_giveback_urb (hcd, urb, NULL);
472 local_irq_restore (flags);
473 return 0;
474 }
475
476 /*-------------------------------------------------------------------------*/
477
478 /*
479 * Root Hub interrupt transfers are synthesized with a timer.
480 * Completions are called in_interrupt() but not in_irq().
481 *
482 * Note: some root hubs (including common UHCI based designs) can't
483 * correctly issue port change IRQs. They're the ones that _need_ a
484 * timer; most other root hubs don't. Some systems could save a
485 * lot of battery power by eliminating these root hub timer IRQs.
486 */
487
488 static void rh_report_status (unsigned long ptr);
489
490 static int rh_status_urb (struct usb_hcd *hcd, struct urb *urb)
491 {
492 int len = 1 + (urb->dev->maxchild / 8);
493
494 /* rh_timer protected by hcd_data_lock */
495 if (hcd->rh_timer.data || urb->transfer_buffer_length < len) {
496 dev_dbg (hcd->self.controller,
497 "not queuing rh status urb, stat %d\n",
498 urb->status);
499 return -EINVAL;
500 }
501
502 init_timer (&hcd->rh_timer);
503 hcd->rh_timer.function = rh_report_status;
504 hcd->rh_timer.data = (unsigned long) urb;
505 /* USB 2.0 spec says 256msec; this is close enough */
506 hcd->rh_timer.expires = jiffies + HZ/4;
507 add_timer (&hcd->rh_timer);
508 urb->hcpriv = hcd; /* nonzero to indicate it's queued */
509 return 0;
510 }
511
512 /* timer callback */
513
514 static void rh_report_status (unsigned long ptr)
515 {
516 struct urb *urb;
517 struct usb_hcd *hcd;
518 int length = 0;
519 unsigned long flags;
520
521 urb = (struct urb *) ptr;
522 local_irq_save (flags);
523 spin_lock (&urb->lock);
524
525 /* do nothing if the urb's been unlinked */
526 if (!urb->dev
527 || urb->status != -EINPROGRESS
528 || (hcd = urb->dev->bus->hcpriv) == NULL) {
529 spin_unlock (&urb->lock);
530 local_irq_restore (flags);
531 return;
532 }
533
534 /* complete the status urb, or retrigger the timer */
535 spin_lock (&hcd_data_lock);
536 if (urb->dev->state == USB_STATE_CONFIGURED) {
537 length = hcd->driver->hub_status_data (
538 hcd, urb->transfer_buffer);
539 if (length > 0) {
540 hcd->rh_timer.data = 0;
541 urb->actual_length = length;
542 urb->status = 0;
543 urb->hcpriv = NULL;
544 } else
545 mod_timer (&hcd->rh_timer, jiffies + HZ/4);
546 }
547 spin_unlock (&hcd_data_lock);
548 spin_unlock (&urb->lock);
549
550 /* local irqs are always blocked in completions */
551 if (length > 0)
552 usb_hcd_giveback_urb (hcd, urb, NULL);
553 local_irq_restore (flags);
554 }
555
556 /*-------------------------------------------------------------------------*/
557
558 static int rh_urb_enqueue (struct usb_hcd *hcd, struct urb *urb)
559 {
560 if (usb_pipeint (urb->pipe)) {
561 int retval;
562 unsigned long flags;
563
564 spin_lock_irqsave (&hcd_data_lock, flags);
565 retval = rh_status_urb (hcd, urb);
566 spin_unlock_irqrestore (&hcd_data_lock, flags);
567 return retval;
568 }
569 if (usb_pipecontrol (urb->pipe))
570 return rh_call_control (hcd, urb);
571 else
572 return -EINVAL;
573 }
574
575 /*-------------------------------------------------------------------------*/
576
577 static int usb_rh_urb_dequeue (struct usb_hcd *hcd, struct urb *urb)
578 {
579 unsigned long flags;
580
581 /* note: always a synchronous unlink */
582 if ((unsigned long) urb == hcd->rh_timer.data) {
583 del_timer_sync (&hcd->rh_timer);
584 hcd->rh_timer.data = 0;
585
586 local_irq_save (flags);
587 urb->hcpriv = NULL;
588 usb_hcd_giveback_urb (hcd, urb, NULL);
589 local_irq_restore (flags);
590
591 } else if (usb_pipeendpoint(urb->pipe) == 0) {
592 spin_lock_irq(&urb->lock); /* from usb_kill_urb */
593 ++urb->reject;
594 spin_unlock_irq(&urb->lock);
595
596 wait_event(usb_kill_urb_queue,
597 atomic_read(&urb->use_count) == 0);
598
599 spin_lock_irq(&urb->lock);
600 --urb->reject;
601 spin_unlock_irq(&urb->lock);
602 } else
603 return -EINVAL;
604
605 return 0;
606 }
607
608 /*-------------------------------------------------------------------------*/
609
610 /* exported only within usbcore */
611 struct usb_bus *usb_bus_get (struct usb_bus *bus)
612 {
613 struct class_device *tmp;
614
615 if (!bus)
616 return NULL;
617
618 tmp = class_device_get(&bus->class_dev);
619 if (tmp)
620 return to_usb_bus(tmp);
621 else
622 return NULL;
623 }
624
625 /* exported only within usbcore */
626 void usb_bus_put (struct usb_bus *bus)
627 {
628 if (bus)
629 class_device_put(&bus->class_dev);
630 }
631
632 /*-------------------------------------------------------------------------*/
633
634 static void usb_host_release(struct class_device *class_dev)
635 {
636 struct usb_bus *bus = to_usb_bus(class_dev);
637
638 if (bus->release)
639 bus->release(bus);
640 }
641
642 static struct class usb_host_class = {
643 .name = "usb_host",
644 .release = &usb_host_release,
645 };
646
647 int usb_host_init(void)
648 {
649 return class_register(&usb_host_class);
650 }
651
652 void usb_host_cleanup(void)
653 {
654 class_unregister(&usb_host_class);
655 }
656
657 /**
658 * usb_bus_init - shared initialization code
659 * @bus: the bus structure being initialized
660 *
661 * This code is used to initialize a usb_bus structure, memory for which is
662 * separately managed.
663 */
664 void usb_bus_init (struct usb_bus *bus)
665 {
666 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
667
668 bus->devnum_next = 1;
669
670 bus->root_hub = NULL;
671 bus->hcpriv = NULL;
672 bus->busnum = -1;
673 bus->bandwidth_allocated = 0;
674 bus->bandwidth_int_reqs = 0;
675 bus->bandwidth_isoc_reqs = 0;
676
677 INIT_LIST_HEAD (&bus->bus_list);
678
679 class_device_initialize(&bus->class_dev);
680 bus->class_dev.class = &usb_host_class;
681 }
682 EXPORT_SYMBOL (usb_bus_init);
683
684 /**
685 * usb_alloc_bus - creates a new USB host controller structure
686 * @op: pointer to a struct usb_operations that this bus structure should use
687 * Context: !in_interrupt()
688 *
689 * Creates a USB host controller bus structure with the specified
690 * usb_operations and initializes all the necessary internal objects.
691 *
692 * If no memory is available, NULL is returned.
693 *
694 * The caller should call usb_put_bus() when it is finished with the structure.
695 */
696 struct usb_bus *usb_alloc_bus (struct usb_operations *op)
697 {
698 struct usb_bus *bus;
699
700 bus = kmalloc (sizeof *bus, GFP_KERNEL);
701 if (!bus)
702 return NULL;
703 memset(bus, 0, sizeof(struct usb_bus));
704 usb_bus_init (bus);
705 bus->op = op;
706 return bus;
707 }
708 EXPORT_SYMBOL (usb_alloc_bus);
709
710 /*-------------------------------------------------------------------------*/
711
712 /**
713 * usb_register_bus - registers the USB host controller with the usb core
714 * @bus: pointer to the bus to register
715 * Context: !in_interrupt()
716 *
717 * Assigns a bus number, and links the controller into usbcore data
718 * structures so that it can be seen by scanning the bus list.
719 */
720 int usb_register_bus(struct usb_bus *bus)
721 {
722 int busnum;
723 int retval;
724
725 down (&usb_bus_list_lock);
726 busnum = find_next_zero_bit (busmap.busmap, USB_MAXBUS, 1);
727 if (busnum < USB_MAXBUS) {
728 set_bit (busnum, busmap.busmap);
729 bus->busnum = busnum;
730 } else {
731 printk (KERN_ERR "%s: too many buses\n", usbcore_name);
732 up(&usb_bus_list_lock);
733 return -E2BIG;
734 }
735
736 snprintf(bus->class_dev.class_id, BUS_ID_SIZE, "usb%d", busnum);
737 bus->class_dev.dev = bus->controller;
738 retval = class_device_add(&bus->class_dev);
739 if (retval) {
740 clear_bit(busnum, busmap.busmap);
741 up(&usb_bus_list_lock);
742 return retval;
743 }
744
745 /* Add it to the local list of buses */
746 list_add (&bus->bus_list, &usb_bus_list);
747 up (&usb_bus_list_lock);
748
749 usbfs_add_bus (bus);
750
751 dev_info (bus->controller, "new USB bus registered, assigned bus number %d\n", bus->busnum);
752 return 0;
753 }
754 EXPORT_SYMBOL (usb_register_bus);
755
756 /**
757 * usb_deregister_bus - deregisters the USB host controller
758 * @bus: pointer to the bus to deregister
759 * Context: !in_interrupt()
760 *
761 * Recycles the bus number, and unlinks the controller from usbcore data
762 * structures so that it won't be seen by scanning the bus list.
763 */
764 void usb_deregister_bus (struct usb_bus *bus)
765 {
766 dev_info (bus->controller, "USB bus %d deregistered\n", bus->busnum);
767
768 /*
769 * NOTE: make sure that all the devices are removed by the
770 * controller code, as well as having it call this when cleaning
771 * itself up
772 */
773 down (&usb_bus_list_lock);
774 list_del (&bus->bus_list);
775 up (&usb_bus_list_lock);
776
777 usbfs_remove_bus (bus);
778
779 clear_bit (bus->busnum, busmap.busmap);
780
781 class_device_unregister(&bus->class_dev);
782 }
783 EXPORT_SYMBOL (usb_deregister_bus);
784
785 /**
786 * usb_register_root_hub - called by HCD to register its root hub
787 * @usb_dev: the usb root hub device to be registered.
788 * @parent_dev: the parent device of this root hub.
789 *
790 * The USB host controller calls this function to register the root hub
791 * properly with the USB subsystem. It sets up the device properly in
792 * the device tree and stores the root_hub pointer in the bus structure,
793 * then calls usb_new_device() to register the usb device. It also
794 * assigns the root hub's USB address (always 1).
795 */
796 int usb_register_root_hub (struct usb_device *usb_dev, struct device *parent_dev)
797 {
798 const int devnum = 1;
799 int retval;
800
801 usb_dev->devnum = devnum;
802 usb_dev->bus->devnum_next = devnum + 1;
803 memset (&usb_dev->bus->devmap.devicemap, 0,
804 sizeof usb_dev->bus->devmap.devicemap);
805 set_bit (devnum, usb_dev->bus->devmap.devicemap);
806 usb_set_device_state(usb_dev, USB_STATE_ADDRESS);
807
808 down (&usb_bus_list_lock);
809 usb_dev->bus->root_hub = usb_dev;
810
811 usb_dev->ep0.desc.wMaxPacketSize = __constant_cpu_to_le16(64);
812 retval = usb_get_device_descriptor(usb_dev, USB_DT_DEVICE_SIZE);
813 if (retval != sizeof usb_dev->descriptor) {
814 usb_dev->bus->root_hub = NULL;
815 up (&usb_bus_list_lock);
816 dev_dbg (parent_dev, "can't read %s device descriptor %d\n",
817 usb_dev->dev.bus_id, retval);
818 return (retval < 0) ? retval : -EMSGSIZE;
819 }
820
821 usb_lock_device (usb_dev);
822 retval = usb_new_device (usb_dev);
823 usb_unlock_device (usb_dev);
824 if (retval) {
825 usb_dev->bus->root_hub = NULL;
826 dev_err (parent_dev, "can't register root hub for %s, %d\n",
827 usb_dev->dev.bus_id, retval);
828 }
829 up (&usb_bus_list_lock);
830 return retval;
831 }
832 EXPORT_SYMBOL (usb_register_root_hub);
833
834
835 /*-------------------------------------------------------------------------*/
836
837 /**
838 * usb_calc_bus_time - approximate periodic transaction time in nanoseconds
839 * @speed: from dev->speed; USB_SPEED_{LOW,FULL,HIGH}
840 * @is_input: true iff the transaction sends data to the host
841 * @isoc: true for isochronous transactions, false for interrupt ones
842 * @bytecount: how many bytes in the transaction.
843 *
844 * Returns approximate bus time in nanoseconds for a periodic transaction.
845 * See USB 2.0 spec section 5.11.3; only periodic transfers need to be
846 * scheduled in software, this function is only used for such scheduling.
847 */
848 long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
849 {
850 unsigned long tmp;
851
852 switch (speed) {
853 case USB_SPEED_LOW: /* INTR only */
854 if (is_input) {
855 tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
856 return (64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
857 } else {
858 tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
859 return (64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp);
860 }
861 case USB_SPEED_FULL: /* ISOC or INTR */
862 if (isoc) {
863 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
864 return (((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp);
865 } else {
866 tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
867 return (9107L + BW_HOST_DELAY + tmp);
868 }
869 case USB_SPEED_HIGH: /* ISOC or INTR */
870 // FIXME adjust for input vs output
871 if (isoc)
872 tmp = HS_USECS (bytecount);
873 else
874 tmp = HS_USECS_ISO (bytecount);
875 return tmp;
876 default:
877 pr_debug ("%s: bogus device speed!\n", usbcore_name);
878 return -1;
879 }
880 }
881 EXPORT_SYMBOL (usb_calc_bus_time);
882
883 /*
884 * usb_check_bandwidth():
885 *
886 * old_alloc is from host_controller->bandwidth_allocated in microseconds;
887 * bustime is from calc_bus_time(), but converted to microseconds.
888 *
889 * returns <bustime in us> if successful,
890 * or -ENOSPC if bandwidth request fails.
891 *
892 * FIXME:
893 * This initial implementation does not use Endpoint.bInterval
894 * in managing bandwidth allocation.
895 * It probably needs to be expanded to use Endpoint.bInterval.
896 * This can be done as a later enhancement (correction).
897 *
898 * This will also probably require some kind of
899 * frame allocation tracking...meaning, for example,
900 * that if multiple drivers request interrupts every 10 USB frames,
901 * they don't all have to be allocated at
902 * frame numbers N, N+10, N+20, etc. Some of them could be at
903 * N+11, N+21, N+31, etc., and others at
904 * N+12, N+22, N+32, etc.
905 *
906 * Similarly for isochronous transfers...
907 *
908 * Individual HCDs can schedule more directly ... this logic
909 * is not correct for high speed transfers.
910 */
911 int usb_check_bandwidth (struct usb_device *dev, struct urb *urb)
912 {
913 unsigned int pipe = urb->pipe;
914 long bustime;
915 int is_in = usb_pipein (pipe);
916 int is_iso = usb_pipeisoc (pipe);
917 int old_alloc = dev->bus->bandwidth_allocated;
918 int new_alloc;
919
920
921 bustime = NS_TO_US (usb_calc_bus_time (dev->speed, is_in, is_iso,
922 usb_maxpacket (dev, pipe, !is_in)));
923 if (is_iso)
924 bustime /= urb->number_of_packets;
925
926 new_alloc = old_alloc + (int) bustime;
927 if (new_alloc > FRAME_TIME_MAX_USECS_ALLOC) {
928 #ifdef DEBUG
929 char *mode =
930 #ifdef CONFIG_USB_BANDWIDTH
931 "";
932 #else
933 "would have ";
934 #endif
935 dev_dbg (&dev->dev, "usb_check_bandwidth %sFAILED: %d + %ld = %d usec\n",
936 mode, old_alloc, bustime, new_alloc);
937 #endif
938 #ifdef CONFIG_USB_BANDWIDTH
939 bustime = -ENOSPC; /* report error */
940 #endif
941 }
942
943 return bustime;
944 }
945 EXPORT_SYMBOL (usb_check_bandwidth);
946
947
948 /**
949 * usb_claim_bandwidth - records bandwidth for a periodic transfer
950 * @dev: source/target of request
951 * @urb: request (urb->dev == dev)
952 * @bustime: bandwidth consumed, in (average) microseconds per frame
953 * @isoc: true iff the request is isochronous
954 *
955 * Bus bandwidth reservations are recorded purely for diagnostic purposes.
956 * HCDs are expected not to overcommit periodic bandwidth, and to record such
957 * reservations whenever endpoints are added to the periodic schedule.
958 *
959 * FIXME averaging per-frame is suboptimal. Better to sum over the HCD's
960 * entire periodic schedule ... 32 frames for OHCI, 1024 for UHCI, settable
961 * for EHCI (256/512/1024 frames, default 1024) and have the bus expose how
962 * large its periodic schedule is.
963 */
964 void usb_claim_bandwidth (struct usb_device *dev, struct urb *urb, int bustime, int isoc)
965 {
966 dev->bus->bandwidth_allocated += bustime;
967 if (isoc)
968 dev->bus->bandwidth_isoc_reqs++;
969 else
970 dev->bus->bandwidth_int_reqs++;
971 urb->bandwidth = bustime;
972
973 #ifdef USB_BANDWIDTH_MESSAGES
974 dev_dbg (&dev->dev, "bandwidth alloc increased by %d (%s) to %d for %d requesters\n",
975 bustime,
976 isoc ? "ISOC" : "INTR",
977 dev->bus->bandwidth_allocated,
978 dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
979 #endif
980 }
981 EXPORT_SYMBOL (usb_claim_bandwidth);
982
983
984 /**
985 * usb_release_bandwidth - reverses effect of usb_claim_bandwidth()
986 * @dev: source/target of request
987 * @urb: request (urb->dev == dev)
988 * @isoc: true iff the request is isochronous
989 *
990 * This records that previously allocated bandwidth has been released.
991 * Bandwidth is released when endpoints are removed from the host controller's
992 * periodic schedule.
993 */
994 void usb_release_bandwidth (struct usb_device *dev, struct urb *urb, int isoc)
995 {
996 dev->bus->bandwidth_allocated -= urb->bandwidth;
997 if (isoc)
998 dev->bus->bandwidth_isoc_reqs--;
999 else
1000 dev->bus->bandwidth_int_reqs--;
1001
1002 #ifdef USB_BANDWIDTH_MESSAGES
1003 dev_dbg (&dev->dev, "bandwidth alloc reduced by %d (%s) to %d for %d requesters\n",
1004 urb->bandwidth,
1005 isoc ? "ISOC" : "INTR",
1006 dev->bus->bandwidth_allocated,
1007 dev->bus->bandwidth_int_reqs + dev->bus->bandwidth_isoc_reqs);
1008 #endif
1009 urb->bandwidth = 0;
1010 }
1011 EXPORT_SYMBOL (usb_release_bandwidth);
1012
1013
1014 /*-------------------------------------------------------------------------*/
1015
1016 /*
1017 * Generic HC operations.
1018 */
1019
1020 /*-------------------------------------------------------------------------*/
1021
1022 static void urb_unlink (struct urb *urb)
1023 {
1024 unsigned long flags;
1025
1026 /* Release any periodic transfer bandwidth */
1027 if (urb->bandwidth)
1028 usb_release_bandwidth (urb->dev, urb,
1029 usb_pipeisoc (urb->pipe));
1030
1031 /* clear all state linking urb to this dev (and hcd) */
1032
1033 spin_lock_irqsave (&hcd_data_lock, flags);
1034 list_del_init (&urb->urb_list);
1035 spin_unlock_irqrestore (&hcd_data_lock, flags);
1036 usb_put_dev (urb->dev);
1037 }
1038
1039
1040 /* may be called in any context with a valid urb->dev usecount
1041 * caller surrenders "ownership" of urb
1042 * expects usb_submit_urb() to have sanity checked and conditioned all
1043 * inputs in the urb
1044 */
1045 static int hcd_submit_urb (struct urb *urb, int mem_flags)
1046 {
1047 int status;
1048 struct usb_hcd *hcd = urb->dev->bus->hcpriv;
1049 struct usb_host_endpoint *ep;
1050 unsigned long flags;
1051
1052 ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
1053 [usb_pipeendpoint(urb->pipe)];
1054 if (!hcd || !ep)
1055 return -ENODEV;
1056
1057 /*
1058 * FIXME: make urb timeouts be generic, keeping the HCD cores
1059 * as simple as possible.
1060 */
1061
1062 // NOTE: a generic device/urb monitoring hook would go here.
1063 // hcd_monitor_hook(MONITOR_URB_SUBMIT, urb)
1064 // It would catch submission paths for all urbs.
1065
1066 /*
1067 * Atomically queue the urb, first to our records, then to the HCD.
1068 * Access to urb->status is controlled by urb->lock ... changes on
1069 * i/o completion (normal or fault) or unlinking.
1070 */
1071
1072 // FIXME: verify that quiescing hc works right (RH cleans up)
1073
1074 spin_lock_irqsave (&hcd_data_lock, flags);
1075 if (unlikely (urb->reject))
1076 status = -EPERM;
1077 else switch (hcd->state) {
1078 case USB_STATE_RUNNING:
1079 case USB_STATE_RESUMING:
1080 usb_get_dev (urb->dev);
1081 list_add_tail (&urb->urb_list, &ep->urb_list);
1082 status = 0;
1083 break;
1084 default:
1085 status = -ESHUTDOWN;
1086 break;
1087 }
1088 spin_unlock_irqrestore (&hcd_data_lock, flags);
1089 if (status) {
1090 INIT_LIST_HEAD (&urb->urb_list);
1091 return status;
1092 }
1093
1094 /* increment urb's reference count as part of giving it to the HCD
1095 * (which now controls it). HCD guarantees that it either returns
1096 * an error or calls giveback(), but not both.
1097 */
1098 urb = usb_get_urb (urb);
1099 atomic_inc (&urb->use_count);
1100
1101 if (urb->dev == hcd->self.root_hub) {
1102 /* NOTE: requirement on hub callers (usbfs and the hub
1103 * driver, for now) that URBs' urb->transfer_buffer be
1104 * valid and usb_buffer_{sync,unmap}() not be needed, since
1105 * they could clobber root hub response data.
1106 */
1107 urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP
1108 | URB_NO_SETUP_DMA_MAP);
1109 status = rh_urb_enqueue (hcd, urb);
1110 goto done;
1111 }
1112
1113 /* lower level hcd code should use *_dma exclusively,
1114 * unless it uses pio or talks to another transport.
1115 */
1116 if (hcd->self.controller->dma_mask) {
1117 if (usb_pipecontrol (urb->pipe)
1118 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1119 urb->setup_dma = dma_map_single (
1120 hcd->self.controller,
1121 urb->setup_packet,
1122 sizeof (struct usb_ctrlrequest),
1123 DMA_TO_DEVICE);
1124 if (urb->transfer_buffer_length != 0
1125 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
1126 urb->transfer_dma = dma_map_single (
1127 hcd->self.controller,
1128 urb->transfer_buffer,
1129 urb->transfer_buffer_length,
1130 usb_pipein (urb->pipe)
1131 ? DMA_FROM_DEVICE
1132 : DMA_TO_DEVICE);
1133 }
1134
1135 status = hcd->driver->urb_enqueue (hcd, ep, urb, mem_flags);
1136 done:
1137 if (unlikely (status)) {
1138 urb_unlink (urb);
1139 atomic_dec (&urb->use_count);
1140 if (urb->reject)
1141 wake_up (&usb_kill_urb_queue);
1142 usb_put_urb (urb);
1143 }
1144 return status;
1145 }
1146
1147 /*-------------------------------------------------------------------------*/
1148
1149 /* called in any context */
1150 static int hcd_get_frame_number (struct usb_device *udev)
1151 {
1152 struct usb_hcd *hcd = (struct usb_hcd *)udev->bus->hcpriv;
1153 if (!HCD_IS_RUNNING (hcd->state))
1154 return -ESHUTDOWN;
1155 return hcd->driver->get_frame_number (hcd);
1156 }
1157
1158 /*-------------------------------------------------------------------------*/
1159
1160 /* this makes the hcd giveback() the urb more quickly, by kicking it
1161 * off hardware queues (which may take a while) and returning it as
1162 * soon as practical. we've already set up the urb's return status,
1163 * but we can't know if the callback completed already.
1164 */
1165 static int
1166 unlink1 (struct usb_hcd *hcd, struct urb *urb)
1167 {
1168 int value;
1169
1170 if (urb->dev == hcd->self.root_hub)
1171 value = usb_rh_urb_dequeue (hcd, urb);
1172 else {
1173
1174 /* The only reason an HCD might fail this call is if
1175 * it has not yet fully queued the urb to begin with.
1176 * Such failures should be harmless. */
1177 value = hcd->driver->urb_dequeue (hcd, urb);
1178 }
1179
1180 if (value != 0)
1181 dev_dbg (hcd->self.controller, "dequeue %p --> %d\n",
1182 urb, value);
1183 return value;
1184 }
1185
1186 /*
1187 * called in any context
1188 *
1189 * caller guarantees urb won't be recycled till both unlink()
1190 * and the urb's completion function return
1191 */
1192 static int hcd_unlink_urb (struct urb *urb, int status)
1193 {
1194 struct usb_host_endpoint *ep;
1195 struct usb_hcd *hcd = NULL;
1196 struct device *sys = NULL;
1197 unsigned long flags;
1198 struct list_head *tmp;
1199 int retval;
1200
1201 if (!urb)
1202 return -EINVAL;
1203 if (!urb->dev || !urb->dev->bus)
1204 return -ENODEV;
1205 ep = (usb_pipein(urb->pipe) ? urb->dev->ep_in : urb->dev->ep_out)
1206 [usb_pipeendpoint(urb->pipe)];
1207 if (!ep)
1208 return -ENODEV;
1209
1210 /*
1211 * we contend for urb->status with the hcd core,
1212 * which changes it while returning the urb.
1213 *
1214 * Caller guaranteed that the urb pointer hasn't been freed, and
1215 * that it was submitted. But as a rule it can't know whether or
1216 * not it's already been unlinked ... so we respect the reversed
1217 * lock sequence needed for the usb_hcd_giveback_urb() code paths
1218 * (urb lock, then hcd_data_lock) in case some other CPU is now
1219 * unlinking it.
1220 */
1221 spin_lock_irqsave (&urb->lock, flags);
1222 spin_lock (&hcd_data_lock);
1223
1224 sys = &urb->dev->dev;
1225 hcd = urb->dev->bus->hcpriv;
1226 if (hcd == NULL) {
1227 retval = -ENODEV;
1228 goto done;
1229 }
1230
1231 /* running ~= hc unlink handshake works (irq, timer, etc)
1232 * halted ~= no unlink handshake is needed
1233 * suspended, resuming == should never happen
1234 */
1235 WARN_ON (!HCD_IS_RUNNING (hcd->state) && hcd->state != USB_STATE_HALT);
1236
1237 /* insist the urb is still queued */
1238 list_for_each(tmp, &ep->urb_list) {
1239 if (tmp == &urb->urb_list)
1240 break;
1241 }
1242 if (tmp != &urb->urb_list) {
1243 retval = -EIDRM;
1244 goto done;
1245 }
1246
1247 /* Any status except -EINPROGRESS means something already started to
1248 * unlink this URB from the hardware. So there's no more work to do.
1249 */
1250 if (urb->status != -EINPROGRESS) {
1251 retval = -EBUSY;
1252 goto done;
1253 }
1254
1255 /* IRQ setup can easily be broken so that USB controllers
1256 * never get completion IRQs ... maybe even the ones we need to
1257 * finish unlinking the initial failed usb_set_address()
1258 * or device descriptor fetch.
1259 */
1260 if (!hcd->saw_irq && hcd->self.root_hub != urb->dev) {
1261 dev_warn (hcd->self.controller, "Unlink after no-IRQ? "
1262 "Controller is probably using the wrong IRQ."
1263 "\n");
1264 hcd->saw_irq = 1;
1265 }
1266
1267 urb->status = status;
1268
1269 spin_unlock (&hcd_data_lock);
1270 spin_unlock_irqrestore (&urb->lock, flags);
1271
1272 retval = unlink1 (hcd, urb);
1273 if (retval == 0)
1274 retval = -EINPROGRESS;
1275 return retval;
1276
1277 done:
1278 spin_unlock (&hcd_data_lock);
1279 spin_unlock_irqrestore (&urb->lock, flags);
1280 if (retval != -EIDRM && sys && sys->driver)
1281 dev_dbg (sys, "hcd_unlink_urb %p fail %d\n", urb, retval);
1282 return retval;
1283 }
1284
1285 /*-------------------------------------------------------------------------*/
1286
1287 /* disables the endpoint: cancels any pending urbs, then synchronizes with
1288 * the hcd to make sure all endpoint state is gone from hardware. use for
1289 * set_configuration, set_interface, driver removal, physical disconnect.
1290 *
1291 * example: a qh stored in ep->hcpriv, holding state related to endpoint
1292 * type, maxpacket size, toggle, halt status, and scheduling.
1293 */
1294 static void
1295 hcd_endpoint_disable (struct usb_device *udev, struct usb_host_endpoint *ep)
1296 {
1297 struct usb_hcd *hcd;
1298 struct urb *urb;
1299
1300 hcd = udev->bus->hcpriv;
1301
1302 WARN_ON (!HCD_IS_RUNNING (hcd->state) && hcd->state != USB_STATE_HALT);
1303
1304 local_irq_disable ();
1305
1306 /* FIXME move most of this into message.c as part of its
1307 * endpoint disable logic
1308 */
1309
1310 /* ep is already gone from udev->ep_{in,out}[]; no more submits */
1311 rescan:
1312 spin_lock (&hcd_data_lock);
1313 list_for_each_entry (urb, &ep->urb_list, urb_list) {
1314 int tmp;
1315
1316 /* another cpu may be in hcd, spinning on hcd_data_lock
1317 * to giveback() this urb. the races here should be
1318 * small, but a full fix needs a new "can't submit"
1319 * urb state.
1320 * FIXME urb->reject should allow that...
1321 */
1322 if (urb->status != -EINPROGRESS)
1323 continue;
1324 usb_get_urb (urb);
1325 spin_unlock (&hcd_data_lock);
1326
1327 spin_lock (&urb->lock);
1328 tmp = urb->status;
1329 if (tmp == -EINPROGRESS)
1330 urb->status = -ESHUTDOWN;
1331 spin_unlock (&urb->lock);
1332
1333 /* kick hcd unless it's already returning this */
1334 if (tmp == -EINPROGRESS) {
1335 tmp = urb->pipe;
1336 unlink1 (hcd, urb);
1337 dev_dbg (hcd->self.controller,
1338 "shutdown urb %p pipe %08x ep%d%s%s\n",
1339 urb, tmp, usb_pipeendpoint (tmp),
1340 (tmp & USB_DIR_IN) ? "in" : "out",
1341 ({ char *s; \
1342 switch (usb_pipetype (tmp)) { \
1343 case PIPE_CONTROL: s = ""; break; \
1344 case PIPE_BULK: s = "-bulk"; break; \
1345 case PIPE_INTERRUPT: s = "-intr"; break; \
1346 default: s = "-iso"; break; \
1347 }; s;}));
1348 }
1349 usb_put_urb (urb);
1350
1351 /* list contents may have changed */
1352 goto rescan;
1353 }
1354 spin_unlock (&hcd_data_lock);
1355 local_irq_enable ();
1356
1357 /* synchronize with the hardware, so old configuration state
1358 * clears out immediately (and will be freed).
1359 */
1360 might_sleep ();
1361 if (hcd->driver->endpoint_disable)
1362 hcd->driver->endpoint_disable (hcd, ep);
1363 }
1364
1365 /*-------------------------------------------------------------------------*/
1366
1367 #ifdef CONFIG_USB_SUSPEND
1368
1369 static int hcd_hub_suspend (struct usb_bus *bus)
1370 {
1371 struct usb_hcd *hcd;
1372
1373 hcd = container_of (bus, struct usb_hcd, self);
1374 if (hcd->driver->hub_suspend)
1375 return hcd->driver->hub_suspend (hcd);
1376 return 0;
1377 }
1378
1379 static int hcd_hub_resume (struct usb_bus *bus)
1380 {
1381 struct usb_hcd *hcd;
1382
1383 hcd = container_of (bus, struct usb_hcd, self);
1384 if (hcd->driver->hub_resume)
1385 return hcd->driver->hub_resume (hcd);
1386 return 0;
1387 }
1388
1389 #endif
1390
1391 /*-------------------------------------------------------------------------*/
1392
1393 #ifdef CONFIG_USB_OTG
1394
1395 /**
1396 * usb_bus_start_enum - start immediate enumeration (for OTG)
1397 * @bus: the bus (must use hcd framework)
1398 * @port: 1-based number of port; usually bus->otg_port
1399 * Context: in_interrupt()
1400 *
1401 * Starts enumeration, with an immediate reset followed later by
1402 * khubd identifying and possibly configuring the device.
1403 * This is needed by OTG controller drivers, where it helps meet
1404 * HNP protocol timing requirements for starting a port reset.
1405 */
1406 int usb_bus_start_enum(struct usb_bus *bus, unsigned port_num)
1407 {
1408 struct usb_hcd *hcd;
1409 int status = -EOPNOTSUPP;
1410
1411 /* NOTE: since HNP can't start by grabbing the bus's address0_sem,
1412 * boards with root hubs hooked up to internal devices (instead of
1413 * just the OTG port) may need more attention to resetting...
1414 */
1415 hcd = container_of (bus, struct usb_hcd, self);
1416 if (port_num && hcd->driver->start_port_reset)
1417 status = hcd->driver->start_port_reset(hcd, port_num);
1418
1419 /* run khubd shortly after (first) root port reset finishes;
1420 * it may issue others, until at least 50 msecs have passed.
1421 */
1422 if (status == 0)
1423 mod_timer(&hcd->rh_timer, jiffies + msecs_to_jiffies(10));
1424 return status;
1425 }
1426 EXPORT_SYMBOL (usb_bus_start_enum);
1427
1428 #endif
1429
1430 /*-------------------------------------------------------------------------*/
1431
1432 /*
1433 * usb_hcd_operations - adapts usb_bus framework to HCD framework (bus glue)
1434 */
1435 static struct usb_operations usb_hcd_operations = {
1436 .get_frame_number = hcd_get_frame_number,
1437 .submit_urb = hcd_submit_urb,
1438 .unlink_urb = hcd_unlink_urb,
1439 .buffer_alloc = hcd_buffer_alloc,
1440 .buffer_free = hcd_buffer_free,
1441 .disable = hcd_endpoint_disable,
1442 #ifdef CONFIG_USB_SUSPEND
1443 .hub_suspend = hcd_hub_suspend,
1444 .hub_resume = hcd_hub_resume,
1445 #endif
1446 };
1447
1448 /*-------------------------------------------------------------------------*/
1449
1450 /**
1451 * usb_hcd_giveback_urb - return URB from HCD to device driver
1452 * @hcd: host controller returning the URB
1453 * @urb: urb being returned to the USB device driver.
1454 * @regs: pt_regs, passed down to the URB completion handler
1455 * Context: in_interrupt()
1456 *
1457 * This hands the URB from HCD to its USB device driver, using its
1458 * completion function. The HCD has freed all per-urb resources
1459 * (and is done using urb->hcpriv). It also released all HCD locks;
1460 * the device driver won't cause problems if it frees, modifies,
1461 * or resubmits this URB.
1462 */
1463 void usb_hcd_giveback_urb (struct usb_hcd *hcd, struct urb *urb, struct pt_regs *regs)
1464 {
1465 urb_unlink (urb);
1466
1467 // NOTE: a generic device/urb monitoring hook would go here.
1468 // hcd_monitor_hook(MONITOR_URB_FINISH, urb, dev)
1469 // It would catch exit/unlink paths for all urbs.
1470
1471 /* lower level hcd code should use *_dma exclusively */
1472 if (hcd->self.controller->dma_mask) {
1473 if (usb_pipecontrol (urb->pipe)
1474 && !(urb->transfer_flags & URB_NO_SETUP_DMA_MAP))
1475 dma_unmap_single (hcd->self.controller, urb->setup_dma,
1476 sizeof (struct usb_ctrlrequest),
1477 DMA_TO_DEVICE);
1478 if (urb->transfer_buffer_length != 0
1479 && !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP))
1480 dma_unmap_single (hcd->self.controller,
1481 urb->transfer_dma,
1482 urb->transfer_buffer_length,
1483 usb_pipein (urb->pipe)
1484 ? DMA_FROM_DEVICE
1485 : DMA_TO_DEVICE);
1486 }
1487
1488 /* pass ownership to the completion handler */
1489 urb->complete (urb, regs);
1490 atomic_dec (&urb->use_count);
1491 if (unlikely (urb->reject))
1492 wake_up (&usb_kill_urb_queue);
1493 usb_put_urb (urb);
1494 }
1495 EXPORT_SYMBOL (usb_hcd_giveback_urb);
1496
1497 /*-------------------------------------------------------------------------*/
1498
1499 /**
1500 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
1501 * @irq: the IRQ being raised
1502 * @__hcd: pointer to the HCD whose IRQ is beinng signaled
1503 * @r: saved hardware registers
1504 *
1505 * When registering a USB bus through the HCD framework code, use this
1506 * to handle interrupts. The PCI glue layer does so automatically; only
1507 * bus glue for non-PCI system busses will need to use this.
1508 */
1509 irqreturn_t usb_hcd_irq (int irq, void *__hcd, struct pt_regs * r)
1510 {
1511 struct usb_hcd *hcd = __hcd;
1512 int start = hcd->state;
1513
1514 if (start == USB_STATE_HALT)
1515 return IRQ_NONE;
1516 if (hcd->driver->irq (hcd, r) == IRQ_NONE)
1517 return IRQ_NONE;
1518
1519 hcd->saw_irq = 1;
1520 if (hcd->state != start && hcd->state == USB_STATE_HALT)
1521 usb_hc_died (hcd);
1522 return IRQ_HANDLED;
1523 }
1524 EXPORT_SYMBOL (usb_hcd_irq);
1525
1526 /*-------------------------------------------------------------------------*/
1527
1528 /**
1529 * usb_hc_died - report abnormal shutdown of a host controller (bus glue)
1530 * @hcd: pointer to the HCD representing the controller
1531 *
1532 * This is called by bus glue to report a USB host controller that died
1533 * while operations may still have been pending. It's called automatically
1534 * by the PCI glue, so only glue for non-PCI busses should need to call it.
1535 */
1536 void usb_hc_died (struct usb_hcd *hcd)
1537 {
1538 dev_err (hcd->self.controller, "HC died; cleaning up\n");
1539
1540 /* make khubd clean up old urbs and devices */
1541 usb_set_device_state(hcd->self.root_hub, USB_STATE_NOTATTACHED);
1542 mod_timer(&hcd->rh_timer, jiffies);
1543 }
1544
1545 /*-------------------------------------------------------------------------*/
1546
1547 static void hcd_release (struct usb_bus *bus)
1548 {
1549 struct usb_hcd *hcd;
1550
1551 hcd = container_of(bus, struct usb_hcd, self);
1552 kfree(hcd);
1553 }
1554
1555 /**
1556 * usb_create_hcd - create and initialize an HCD structure
1557 * @driver: HC driver that will use this hcd
1558 * Context: !in_interrupt()
1559 *
1560 * Allocate a struct usb_hcd, with extra space at the end for the
1561 * HC driver's private data. Initialize the generic members of the
1562 * hcd structure.
1563 *
1564 * If memory is unavailable, returns NULL.
1565 */
1566 struct usb_hcd *usb_create_hcd (const struct hc_driver *driver)
1567 {
1568 struct usb_hcd *hcd;
1569
1570 hcd = kcalloc(1, sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
1571 if (!hcd)
1572 return NULL;
1573
1574 usb_bus_init(&hcd->self);
1575 hcd->self.op = &usb_hcd_operations;
1576 hcd->self.hcpriv = hcd;
1577 hcd->self.release = &hcd_release;
1578
1579 init_timer(&hcd->rh_timer);
1580
1581 hcd->driver = driver;
1582 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
1583 "USB Host Controller";
1584 hcd->state = USB_STATE_HALT;
1585
1586 return hcd;
1587 }
1588 EXPORT_SYMBOL (usb_create_hcd);
1589
1590 void usb_put_hcd (struct usb_hcd *hcd)
1591 {
1592 usb_bus_put(&hcd->self);
1593 }
1594 EXPORT_SYMBOL (usb_put_hcd);
1595
|
This page was automatically generated by the
LXR engine.
|