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