[usb] Try multiple USB device configurations
[ipxe.git] / src / drivers / bus / usb.c
1 /*
2 * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <byteswap.h>
29 #include <ipxe/usb.h>
30
31 /** @file
32 *
33 * Universal Serial Bus (USB)
34 *
35 */
36
37 /******************************************************************************
38 *
39 * Utility functions
40 *
41 ******************************************************************************
42 */
43
44 /**
45 * Get USB endpoint name (for debugging)
46 *
47 * @v address Endpoint address
48 * @ret name Endpoint name
49 */
50 static inline const char * usb_endpoint_name ( unsigned int address ) {
51 static char buf[ 9 /* "EPxx OUT" + NUL */ ];
52
53 snprintf ( buf, sizeof ( buf ), "EP%d%s",
54 ( address & USB_ENDPOINT_MAX ),
55 ( address ?
56 ( ( address & USB_ENDPOINT_IN ) ? " IN" : " OUT" ) : "" ));
57 return buf;
58 }
59
60 /**
61 * Get USB speed name (for debugging)
62 *
63 * @v speed Speed
64 * @ret name Speed name
65 */
66 static inline const char * usb_speed_name ( unsigned int speed ) {
67 static const char *exponents[4] = { "", "k", "M", "G" };
68 static char buf[ 10 /* "xxxxxXbps" + NUL */ ];
69 unsigned int mantissa;
70 unsigned int exponent;
71
72 /* Extract mantissa and exponent */
73 mantissa = USB_SPEED_MANTISSA ( speed );
74 exponent = USB_SPEED_EXPONENT ( speed );
75
76 /* Name speed */
77 switch ( speed ) {
78 case USB_SPEED_NONE: return "DETACHED";
79 case USB_SPEED_LOW: return "low";
80 case USB_SPEED_FULL: return "full";
81 case USB_SPEED_HIGH: return "high";
82 case USB_SPEED_SUPER: return "super";
83 default:
84 snprintf ( buf, sizeof ( buf ), "%d%sbps",
85 mantissa, exponents[exponent] );
86 return buf;
87 }
88 }
89
90 /**
91 * Transcribe USB BCD-coded value (for debugging)
92 *
93 * @v bcd BCD-coded value
94 * @ret string Transcribed value
95 */
96 static inline const char * usb_bcd ( uint16_t bcd ) {
97 static char buf[ 6 /* "xx.xx" + NUL */ ];
98 uint8_t high = ( bcd >> 8 );
99 uint8_t low = ( bcd >> 0 );
100
101 snprintf ( buf, sizeof ( buf ), "%x.%02x", high, low );
102 return buf;
103 }
104
105 /******************************************************************************
106 *
107 * USB descriptors
108 *
109 ******************************************************************************
110 */
111
112 /**
113 * Locate USB interface association descriptor
114 *
115 * @v config Configuraton descriptor
116 * @v first First interface number
117 * @ret desc Interface association descriptor, or NULL if not found
118 */
119 static struct usb_interface_association_descriptor *
120 usb_interface_association_descriptor ( struct usb_configuration_descriptor
121 *config,
122 unsigned int first ) {
123 struct usb_interface_association_descriptor *desc;
124
125 /* Find a matching interface association descriptor */
126 for_each_config_descriptor ( desc, config ) {
127 if ( ( desc->header.type ==
128 USB_INTERFACE_ASSOCIATION_DESCRIPTOR ) &&
129 ( desc->first == first ) )
130 return desc;
131 }
132 return NULL;
133 }
134
135 /**
136 * Locate USB interface descriptor
137 *
138 * @v config Configuraton descriptor
139 * @v interface Interface number
140 * @v alternate Alternate setting
141 * @ret desc Interface descriptor, or NULL if not found
142 */
143 struct usb_interface_descriptor *
144 usb_interface_descriptor ( struct usb_configuration_descriptor *config,
145 unsigned int interface, unsigned int alternate ) {
146 struct usb_interface_descriptor *desc;
147
148 /* Find a matching interface descriptor */
149 for_each_config_descriptor ( desc, config ) {
150 if ( ( desc->header.type == USB_INTERFACE_DESCRIPTOR ) &&
151 ( desc->interface == interface ) &&
152 ( desc->alternate == alternate ) )
153 return desc;
154 }
155 return NULL;
156 }
157
158 /**
159 * Locate USB endpoint descriptor
160 *
161 * @v config Configuration descriptor
162 * @v interface Interface descriptor
163 * @v type Endpoint (internal) type
164 * @v index Endpoint index
165 * @ret desc Descriptor, or NULL if not found
166 */
167 struct usb_endpoint_descriptor *
168 usb_endpoint_descriptor ( struct usb_configuration_descriptor *config,
169 struct usb_interface_descriptor *interface,
170 unsigned int type, unsigned int index ) {
171 struct usb_endpoint_descriptor *desc;
172 unsigned int attributes = ( type & USB_ENDPOINT_ATTR_TYPE_MASK );
173 unsigned int direction = ( type & USB_DIR_IN );
174
175 /* Find a matching endpoint descriptor */
176 for_each_interface_descriptor ( desc, config, interface ) {
177 if ( ( desc->header.type == USB_ENDPOINT_DESCRIPTOR ) &&
178 ( ( desc->attributes &
179 USB_ENDPOINT_ATTR_TYPE_MASK ) == attributes ) &&
180 ( ( desc->endpoint & USB_DIR_IN ) == direction ) &&
181 ( index-- == 0 ) )
182 return desc;
183 }
184 return NULL;
185 }
186
187 /**
188 * Locate USB endpoint companion descriptor
189 *
190 * @v config Configuration descriptor
191 * @v desc Endpoint descriptor
192 * @ret descx Companion descriptor, or NULL if not found
193 */
194 struct usb_endpoint_companion_descriptor *
195 usb_endpoint_companion_descriptor ( struct usb_configuration_descriptor *config,
196 struct usb_endpoint_descriptor *desc ) {
197 struct usb_endpoint_companion_descriptor *descx;
198
199 /* Get companion descriptor, if present */
200 descx = container_of ( usb_next_descriptor ( &desc->header ),
201 struct usb_endpoint_companion_descriptor,
202 header );
203 return ( ( usb_is_within_config ( config, &descx->header ) &&
204 descx->header.type == USB_ENDPOINT_COMPANION_DESCRIPTOR )
205 ? descx : NULL );
206 }
207
208 /******************************************************************************
209 *
210 * USB endpoint
211 *
212 ******************************************************************************
213 */
214
215 /**
216 * Describe USB endpoint from device configuration
217 *
218 * @v ep USB endpoint
219 * @v config Configuration descriptor
220 * @v interface Interface descriptor
221 * @v type Endpoint (internal) type
222 * @v index Endpoint index
223 * @ret rc Return status code
224 */
225 int usb_endpoint_described ( struct usb_endpoint *ep,
226 struct usb_configuration_descriptor *config,
227 struct usb_interface_descriptor *interface,
228 unsigned int type, unsigned int index ) {
229 struct usb_endpoint_descriptor *desc;
230 struct usb_endpoint_companion_descriptor *descx;
231 unsigned int sizes;
232 unsigned int burst;
233 size_t mtu;
234
235 /* Locate endpoint descriptor */
236 desc = usb_endpoint_descriptor ( config, interface, type, index );
237 if ( ! desc )
238 return -ENOENT;
239
240 /* Locate companion descriptor, if any */
241 descx = usb_endpoint_companion_descriptor ( config, desc );
242
243 /* Calculate MTU and burst size */
244 sizes = le16_to_cpu ( desc->sizes );
245 mtu = USB_ENDPOINT_MTU ( sizes );
246 burst = ( descx ? descx->burst : USB_ENDPOINT_BURST ( sizes ) );
247
248 /* Describe endpoint */
249 usb_endpoint_describe ( ep, desc->endpoint, desc->attributes,
250 mtu, burst );
251 return 0;
252 }
253
254 /**
255 * Open USB endpoint
256 *
257 * @v ep USB endpoint
258 * @ret rc Return status code
259 */
260 int usb_endpoint_open ( struct usb_endpoint *ep ) {
261 struct usb_device *usb = ep->usb;
262 unsigned int idx = USB_ENDPOINT_IDX ( ep->address );
263 int rc;
264
265 /* Populate host controller operations */
266 ep->host = &usb->port->hub->bus->op->endpoint;
267
268 /* Add to endpoint list */
269 if ( usb->ep[idx] != NULL ) {
270 DBGC ( usb, "USB %s %s is already open\n",
271 usb->name, usb_endpoint_name ( ep->address ) );
272 rc = -EALREADY;
273 goto err_already;
274 }
275 usb->ep[idx] = ep;
276
277 /* Clear any stale error status */
278 ep->rc = 0;
279
280 /* Open endpoint */
281 if ( ( rc = ep->host->open ( ep ) ) != 0 ) {
282 DBGC ( usb, "USB %s %s could not open: %s\n", usb->name,
283 usb_endpoint_name ( ep->address ), strerror ( rc ) );
284 goto err_open;
285 }
286 ep->open = 1;
287
288 DBGC2 ( usb, "USB %s %s opened with MTU %zd (burst %d)\n", usb->name,
289 usb_endpoint_name ( ep->address ), ep->mtu, ep->burst );
290 return 0;
291
292 ep->open = 0;
293 ep->host->close ( ep );
294 err_open:
295 usb->ep[idx] = NULL;
296 err_already:
297 return rc;
298 }
299
300 /**
301 * Close USB endpoint
302 *
303 * @v ep USB endpoint
304 */
305 void usb_endpoint_close ( struct usb_endpoint *ep ) {
306 struct usb_device *usb = ep->usb;
307 unsigned int idx = USB_ENDPOINT_IDX ( ep->address );
308
309 /* Sanity checks */
310 assert ( usb->ep[idx] == ep );
311
312 /* Close endpoint */
313 ep->open = 0;
314 ep->host->close ( ep );
315
316 /* Remove from endpoint list */
317 usb->ep[idx] = NULL;
318 }
319
320 /**
321 * Reset USB endpoint
322 *
323 * @v ep USB endpoint
324 * @ret rc Return status code
325 */
326 static int usb_endpoint_reset ( struct usb_endpoint *ep ) {
327 struct usb_device *usb = ep->usb;
328 unsigned int type;
329 int rc;
330
331 /* Reset endpoint */
332 if ( ( rc = ep->host->reset ( ep ) ) != 0 ) {
333 DBGC ( usb, "USB %s %s could not reset: %s\n",
334 usb->name, usb_endpoint_name ( ep->address ),
335 strerror ( rc ) );
336 return rc;
337 }
338
339 /* Clear endpoint halt, if applicable */
340 type = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
341 if ( ( type != USB_ENDPOINT_ATTR_CONTROL ) &&
342 ( ( rc = usb_clear_feature ( usb, USB_RECIP_ENDPOINT,
343 USB_ENDPOINT_HALT,
344 ep->address ) ) != 0 ) ) {
345 DBGC ( usb, "USB %s %s could not clear endpoint halt: %s\n",
346 usb->name, usb_endpoint_name ( ep->address ),
347 strerror ( rc ) );
348 return rc;
349 }
350
351 /* Clear recorded error */
352 ep->rc = 0;
353
354 DBGC ( usb, "USB %s %s reset\n",
355 usb->name, usb_endpoint_name ( ep->address ) );
356 return 0;
357 }
358
359 /**
360 * Update endpoint MTU
361 *
362 * @v ep USB endpoint
363 * @v mtu New MTU
364 * @ret rc Return status code
365 */
366 static int usb_endpoint_mtu ( struct usb_endpoint *ep, size_t mtu ) {
367 struct usb_device *usb = ep->usb;
368 int rc;
369
370 /* Update MTU */
371 ep->mtu = mtu;
372 if ( ( rc = ep->host->mtu ( ep ) ) != 0 ) {
373 DBGC ( usb, "USB %s %s could not update MTU: %s\n",
374 usb->name, usb_endpoint_name ( ep->address ),
375 strerror ( rc ) );
376 return rc;
377 }
378
379 return 0;
380 }
381
382 /**
383 * Enqueue USB message transfer
384 *
385 * @v ep USB endpoint
386 * @v request Request
387 * @v value Value parameter
388 * @v index Index parameter
389 * @v iobuf I/O buffer
390 * @ret rc Return status code
391 */
392 int usb_message ( struct usb_endpoint *ep, unsigned int request,
393 unsigned int value, unsigned int index,
394 struct io_buffer *iobuf ) {
395 struct usb_device *usb = ep->usb;
396 struct usb_port *port = usb->port;
397 struct usb_setup_packet packet;
398 size_t len = iob_len ( iobuf );
399 int rc;
400
401 /* Fail immediately if device has been unplugged */
402 if ( port->speed == USB_SPEED_NONE )
403 return -ENODEV;
404
405 /* Reset endpoint if required */
406 if ( ( ep->rc != 0 ) && ( ( rc = usb_endpoint_reset ( ep ) ) != 0 ) )
407 return rc;
408
409 /* Zero input data buffer (if applicable) */
410 if ( request & USB_DIR_IN )
411 memset ( iobuf->data, 0, len );
412
413 /* Construct setup packet */
414 packet.request = cpu_to_le16 ( request );
415 packet.value = cpu_to_le16 ( value );
416 packet.index = cpu_to_le16 ( index );
417 packet.len = cpu_to_le16 ( len );
418
419 /* Enqueue message transfer */
420 if ( ( rc = ep->host->message ( ep, &packet, iobuf ) ) != 0 ) {
421 DBGC ( usb, "USB %s %s could not enqueue message transfer: "
422 "%s\n", usb->name, usb_endpoint_name ( ep->address ),
423 strerror ( rc ) );
424 return rc;
425 }
426
427 return 0;
428 }
429
430 /**
431 * Enqueue USB stream transfer
432 *
433 * @v ep USB endpoint
434 * @v iobuf I/O buffer
435 * @ret rc Return status code
436 */
437 int usb_stream ( struct usb_endpoint *ep, struct io_buffer *iobuf ) {
438 struct usb_device *usb = ep->usb;
439 struct usb_port *port = usb->port;
440 int rc;
441
442 /* Fail immediately if device has been unplugged */
443 if ( port->speed == USB_SPEED_NONE )
444 return -ENODEV;
445
446 /* Reset endpoint if required */
447 if ( ( ep->rc != 0 ) && ( ( rc = usb_endpoint_reset ( ep ) ) != 0 ) )
448 return rc;
449
450 /* Enqueue stream transfer */
451 if ( ( rc = ep->host->stream ( ep, iobuf ) ) != 0 ) {
452 DBGC ( usb, "USB %s %s could not enqueue stream transfer: %s\n",
453 usb->name, usb_endpoint_name ( ep->address ),
454 strerror ( rc ) );
455 return rc;
456 }
457
458 return 0;
459 }
460
461 /**
462 * Complete transfer (possibly with error)
463 *
464 * @v ep USB endpoint
465 * @v iobuf I/O buffer
466 * @v rc Completion status code
467 */
468 void usb_complete_err ( struct usb_endpoint *ep, struct io_buffer *iobuf,
469 int rc ) {
470 struct usb_device *usb = ep->usb;
471
472 /* Record error (if any) */
473 ep->rc = rc;
474 if ( ( rc != 0 ) && ep->open ) {
475 DBGC ( usb, "USB %s %s completion failed: %s\n",
476 usb->name, usb_endpoint_name ( ep->address ),
477 strerror ( rc ) );
478 }
479
480 /* Report completion */
481 ep->driver->complete ( ep, iobuf, rc );
482 }
483
484 /******************************************************************************
485 *
486 * Control endpoint
487 *
488 ******************************************************************************
489 */
490
491 /**
492 * Complete USB control transfer
493 *
494 * @v ep USB endpoint
495 * @v iobuf I/O buffer
496 * @v rc Completion status code
497 */
498 static void usb_control_complete ( struct usb_endpoint *ep,
499 struct io_buffer *iobuf, int rc ) {
500 struct usb_device *usb = ep->usb;
501
502 /* Check for failures */
503 if ( rc != 0 ) {
504 DBGC ( usb, "USB %s control transaction failed: %s\n",
505 usb->name, strerror ( rc ) );
506 free_iob ( iobuf );
507 return;
508 }
509
510 /* Add to list of completed I/O buffers */
511 list_add_tail ( &iobuf->list, &usb->complete );
512 }
513
514 /** USB control endpoint driver operations */
515 static struct usb_endpoint_driver_operations usb_control_operations = {
516 .complete = usb_control_complete,
517 };
518
519 /**
520 * Issue USB control transaction
521 *
522 * @v usb USB device
523 * @v request Request
524 * @v value Value parameter
525 * @v index Index parameter
526 * @v data Data buffer (if any)
527 * @v len Length of data
528 * @ret rc Return status code
529 */
530 int usb_control ( struct usb_device *usb, unsigned int request,
531 unsigned int value, unsigned int index, void *data,
532 size_t len ) {
533 struct usb_bus *bus = usb->port->hub->bus;
534 struct usb_endpoint *ep = &usb->control;
535 struct io_buffer *iobuf;
536 struct io_buffer *cmplt;
537 unsigned int i;
538 int rc;
539
540 /* Allocate I/O buffer */
541 iobuf = alloc_iob ( len );
542 if ( ! iobuf ) {
543 rc = -ENOMEM;
544 goto err_alloc;
545 }
546 iob_put ( iobuf, len );
547 if ( request & USB_DIR_IN ) {
548 memset ( data, 0, len );
549 } else {
550 memcpy ( iobuf->data, data, len );
551 }
552
553 /* Enqueue message */
554 if ( ( rc = usb_message ( ep, request, value, index, iobuf ) ) != 0 )
555 goto err_message;
556
557 /* Wait for completion */
558 for ( i = 0 ; i < USB_CONTROL_MAX_WAIT_MS ; i++ ) {
559
560 /* Poll bus */
561 usb_poll ( bus );
562
563 /* Check for completion */
564 while ( ( cmplt = list_first_entry ( &usb->complete,
565 struct io_buffer,
566 list ) ) ) {
567
568 /* Remove from completion list */
569 list_del ( &cmplt->list );
570
571 /* Discard stale completions */
572 if ( cmplt != iobuf ) {
573 DBGC ( usb, "USB %s stale control "
574 "completion:\n", usb->name );
575 DBGC_HDA ( usb, 0, cmplt->data,
576 iob_len ( cmplt ) );
577 free_iob ( cmplt );
578 continue;
579 }
580
581 /* Copy completion to data buffer, if applicable */
582 assert ( iob_len ( cmplt ) <= len );
583 if ( request & USB_DIR_IN )
584 memcpy ( data, cmplt->data, iob_len ( cmplt ) );
585 free_iob ( cmplt );
586 return 0;
587 }
588
589 /* Fail immediately if endpoint is in an error state */
590 if ( ep->rc )
591 return ep->rc;
592
593 /* Delay */
594 mdelay ( 1 );
595 }
596
597 DBGC ( usb, "USB %s timed out waiting for control transaction\n",
598 usb->name );
599 return -ETIMEDOUT;
600
601 err_message:
602 free_iob ( iobuf );
603 err_alloc:
604 return rc;
605 }
606
607 /**
608 * Get USB string descriptor
609 *
610 * @v usb USB device
611 * @v index String index
612 * @v language Language ID
613 * @v buf Data buffer
614 * @v len Length of buffer
615 * @ret len String length (excluding NUL), or negative error
616 */
617 int usb_get_string_descriptor ( struct usb_device *usb, unsigned int index,
618 unsigned int language, char *buf, size_t len ) {
619 size_t max = ( len ? ( len - 1 /* NUL */ ) : 0 );
620 struct {
621 struct usb_descriptor_header header;
622 uint16_t character[max];
623 } __attribute__ (( packed )) *desc;
624 unsigned int actual;
625 unsigned int i;
626 int rc;
627
628 /* Allocate buffer for string */
629 desc = malloc ( sizeof ( *desc ) );
630 if ( ! desc ) {
631 rc = -ENOMEM;
632 goto err_alloc;
633 }
634
635 /* Get descriptor */
636 if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, index,
637 language, &desc->header,
638 sizeof ( *desc ) ) ) != 0 )
639 goto err_get_descriptor;
640
641 /* Copy to buffer */
642 actual = ( ( desc->header.len - sizeof ( desc->header ) ) /
643 sizeof ( desc->character[0] ) );
644 for ( i = 0 ; ( ( i < actual ) && ( i < max ) ) ; i++ )
645 buf[i] = le16_to_cpu ( desc->character[i] );
646 if ( len )
647 buf[i] = '\0';
648
649 /* Free buffer */
650 free ( desc );
651
652 return actual;
653
654 err_get_descriptor:
655 free ( desc );
656 err_alloc:
657 return rc;
658 }
659
660 /******************************************************************************
661 *
662 * USB device driver
663 *
664 ******************************************************************************
665 */
666
667 /**
668 * Describe USB function
669 *
670 * @v func USB function
671 * @v config Configuration descriptor
672 * @v first First interface number
673 * @ret rc Return status code
674 */
675 static int usb_function ( struct usb_function *func,
676 struct usb_configuration_descriptor *config,
677 unsigned int first ) {
678 struct usb_device *usb = func->usb;
679 struct usb_interface_association_descriptor *association;
680 struct usb_interface_descriptor *interface;
681 unsigned int i;
682
683 /* First, look for an interface association descriptor */
684 association = usb_interface_association_descriptor ( config, first );
685 if ( association ) {
686
687 /* Sanity check */
688 if ( ( association->first + association->count ) >
689 config->interfaces ) {
690 DBGC ( usb, "USB %s has invalid association [%d-%d)\n",
691 func->name, association->first,
692 ( association->first + association->count ) );
693 return -ERANGE;
694 }
695
696 /* Describe function */
697 memcpy ( &func->class, &association->class,
698 sizeof ( func->class ) );
699 func->count = association->count;
700 for ( i = 0 ; i < association->count ; i++ )
701 func->interface[i] = ( association->first + i );
702 return 0;
703 }
704
705 /* Next, look for an interface descriptor */
706 interface = usb_interface_descriptor ( config, first, 0 );
707 if ( ! interface ) {
708 DBGC ( usb, "USB %s has no interface descriptor\n",
709 func->name );
710 return -ENOENT;
711 }
712
713 /* Describe function */
714 memcpy ( &func->class, &interface->class, sizeof ( func->class ) );
715 func->count = 1;
716 func->interface[0] = first;
717 return 0;
718 }
719
720 /**
721 * Check for a USB device ID match
722 *
723 * @v func USB function
724 * @v id Device ID
725 * @ret matches Device ID matches
726 */
727 static int
728 usb_device_id_matches ( struct usb_function *func, struct usb_device_id *id ) {
729
730 return ( ( ( id->vendor == func->dev.desc.vendor ) ||
731 ( id->vendor == USB_ANY_ID ) ) &&
732 ( ( id->product == func->dev.desc.device ) ||
733 ( id->product == USB_ANY_ID ) ) &&
734 ( id->class.class == func->class.class ) &&
735 ( id->class.subclass == func->class.subclass ) &&
736 ( id->class.protocol == func->class.protocol ) );
737 }
738
739 /**
740 * Probe USB device driver
741 *
742 * @v func USB function
743 * @v config Configuration descriptor
744 * @ret rc Return status code
745 */
746 static int usb_probe ( struct usb_function *func,
747 struct usb_configuration_descriptor *config ) {
748 struct usb_device *usb = func->usb;
749 struct usb_driver *driver;
750 struct usb_device_id *id;
751 unsigned int i;
752 int rc;
753
754 /* Look for a matching driver */
755 for_each_table_entry ( driver, USB_DRIVERS ) {
756 for ( i = 0 ; i < driver->id_count ; i++ ) {
757
758 /* Check for a matching ID */
759 id = &driver->ids[i];
760 if ( ! usb_device_id_matches ( func, id ) )
761 continue;
762
763 /* Probe driver */
764 if ( ( rc = driver->probe ( func, config ) ) != 0 ) {
765 DBGC ( usb, "USB %s failed to probe driver %s: "
766 "%s\n", func->name, id->name,
767 strerror ( rc ) );
768 /* Continue trying other drivers */
769 continue;
770 }
771
772 /* Record driver */
773 func->driver = driver;
774 func->dev.driver_name = id->name;
775 return 0;
776 }
777 }
778
779 /* No driver found */
780 DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d has no driver\n",
781 func->name, func->dev.desc.vendor, func->dev.desc.device,
782 func->class.class, func->class.subclass, func->class.protocol );
783 return -ENOENT;
784 }
785
786 /**
787 * Remove USB device driver
788 *
789 * @v func USB function
790 */
791 static void usb_remove ( struct usb_function *func ) {
792
793 /* Remove driver */
794 func->driver->remove ( func );
795 }
796
797 /**
798 * Probe all USB device drivers
799 *
800 * @v usb USB device
801 * @v config Configuration descriptor
802 */
803 static void
804 usb_probe_all ( struct usb_device *usb,
805 struct usb_configuration_descriptor *config ) {
806 struct usb_bus *bus = usb->port->hub->bus;
807 struct usb_function *func;
808 uint8_t used[config->interfaces];
809 unsigned int first;
810 unsigned int i;
811 int rc;
812
813 /* Identify each function in turn */
814 memset ( used, 0, sizeof ( used ) );
815 for ( first = 0 ; first < config->interfaces ; first++ ) {
816
817 /* Skip interfaces already used */
818 if ( used[first] )
819 continue;
820
821 /* Allocate and initialise structure */
822 func = zalloc ( sizeof ( *func ) +
823 ( config->interfaces *
824 sizeof ( func->interface[0] ) ) );
825 if ( ! func )
826 goto err_alloc;
827 func->name = func->dev.name;
828 func->usb = usb;
829 func->dev.desc.bus_type = BUS_TYPE_USB;
830 func->dev.desc.location = usb->address;
831 func->dev.desc.vendor = le16_to_cpu ( usb->device.vendor );
832 func->dev.desc.device = le16_to_cpu ( usb->device.product );
833 snprintf ( func->dev.name, sizeof ( func->dev.name ),
834 "%s-%d.%d", usb->name, config->config, first );
835 INIT_LIST_HEAD ( &func->dev.children );
836 func->dev.parent = bus->dev;
837
838 /* Identify function */
839 if ( ( rc = usb_function ( func, config, first ) ) != 0 )
840 goto err_function;
841 assert ( func->count <= config->interfaces );
842
843 /* Mark interfaces as used */
844 for ( i = 0 ; i < func->count ; i++ ) {
845 assert ( func->interface[i] < config->interfaces );
846 used[ func->interface[i] ] = 1;
847 }
848
849 /* Probe device driver */
850 if ( ( rc = usb_probe ( func, config ) ) != 0 )
851 goto err_probe;
852 DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d interfaces ",
853 func->name, func->dev.desc.vendor, func->dev.desc.device,
854 func->class.class, func->class.subclass,
855 func->class.protocol );
856 for ( i = 0 ; i < func->count ; i++ )
857 DBGC ( usb, "%s%d", ( i ? "," : "" ),
858 func->interface[i] );
859 DBGC ( usb, " using driver %s\n", func->dev.driver_name );
860
861 /* Add to list of functions */
862 list_add ( &func->list, &usb->functions );
863
864 /* Add to device hierarchy */
865 list_add_tail ( &func->dev.siblings, &bus->dev->children );
866
867 continue;
868
869 list_del ( &func->dev.siblings );
870 list_del ( &func->list );
871 usb_remove ( func );
872 err_probe:
873 free ( func );
874 err_alloc:
875 err_function:
876 /* Continue registering other functions */
877 continue;
878 }
879 }
880
881 /**
882 * Remove all device drivers
883 *
884 * @v usb USB device
885 */
886 static void usb_remove_all ( struct usb_device *usb ) {
887 struct usb_function *func;
888 struct usb_function *tmp;
889
890 /* Remove all functions */
891 list_for_each_entry_safe ( func, tmp, &usb->functions, list ) {
892
893 /* Remove device driver */
894 usb_remove ( func );
895
896 /* Remove from device hierarchy */
897 assert ( list_empty ( &func->dev.children ) );
898 list_del ( &func->dev.siblings );
899
900 /* Remove from list of functions */
901 list_del ( &func->list );
902
903 /* Free function */
904 free ( func );
905 }
906 }
907
908 /**
909 * Select USB device configuration
910 *
911 * @v usb USB device
912 * @v index Configuration index
913 * @ret rc Return status code
914 */
915 static int usb_configure ( struct usb_device *usb, unsigned int index ) {
916 struct usb_configuration_descriptor partial;
917 struct usb_configuration_descriptor *config;
918 size_t len;
919 int rc;
920
921 /* Read first part of configuration descriptor to get size */
922 if ( ( rc = usb_get_config_descriptor ( usb, index, &partial,
923 sizeof ( partial ) ) ) != 0 ) {
924 DBGC ( usb, "USB %s could not get configuration descriptor %d: "
925 "%s\n", usb->name, index, strerror ( rc ) );
926 goto err_get_partial;
927 }
928 len = le16_to_cpu ( partial.len );
929 if ( len < sizeof ( partial ) ) {
930 DBGC ( usb, "USB %s underlength configuraton descriptor %d\n",
931 usb->name, index );
932 rc = -EINVAL;
933 goto err_partial_len;
934 }
935
936 /* Allocate buffer for whole configuration descriptor */
937 config = malloc ( len );
938 if ( ! config ) {
939 rc = -ENOMEM;
940 goto err_alloc_config;
941 }
942
943 /* Read whole configuration descriptor */
944 if ( ( rc = usb_get_config_descriptor ( usb, index, config,
945 len ) ) != 0 ) {
946 DBGC ( usb, "USB %s could not get configuration descriptor %d: "
947 "%s\n", usb->name, index, strerror ( rc ) );
948 goto err_get_config_descriptor;
949 }
950 if ( config->len != partial.len ) {
951 DBGC ( usb, "USB %s bad configuration descriptor %d length\n",
952 usb->name, index );
953 rc = -EINVAL;
954 goto err_config_len;
955 }
956
957 /* Set configuration */
958 if ( ( rc = usb_set_configuration ( usb, config->config ) ) != 0){
959 DBGC ( usb, "USB %s could not set configuration %d: %s\n",
960 usb->name, config->config, strerror ( rc ) );
961 goto err_set_configuration;
962 }
963
964 /* Probe USB device drivers */
965 usb_probe_all ( usb, config );
966
967 /* Free configuration descriptor */
968 free ( config );
969
970 return 0;
971
972 usb_remove_all ( usb );
973 usb_set_configuration ( usb, 0 );
974 err_set_configuration:
975 err_config_len:
976 err_get_config_descriptor:
977 free ( config );
978 err_alloc_config:
979 err_partial_len:
980 err_get_partial:
981 return rc;
982 }
983
984 /**
985 * Clear USB device configuration
986 *
987 * @v usb USB device
988 */
989 static void usb_deconfigure ( struct usb_device *usb ) {
990 unsigned int i;
991
992 /* Remove device drivers */
993 usb_remove_all ( usb );
994
995 /* Sanity checks */
996 for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++){
997 if ( i != USB_ENDPOINT_IDX ( USB_EP0_ADDRESS ) )
998 assert ( usb->ep[i] == NULL );
999 }
1000
1001 /* Clear device configuration */
1002 usb_set_configuration ( usb, 0 );
1003 }
1004
1005 /**
1006 * Find and select a supported USB device configuration
1007 *
1008 * @v usb USB device
1009 * @ret rc Return status code
1010 */
1011 static int usb_configure_any ( struct usb_device *usb ) {
1012 unsigned int index;
1013 int rc = -ENOENT;
1014
1015 /* Attempt all configuration indexes */
1016 for ( index = 0 ; index < usb->device.configurations ; index++ ) {
1017
1018 /* Attempt this configuration index */
1019 if ( ( rc = usb_configure ( usb, index ) ) != 0 )
1020 continue;
1021
1022 /* If we have no drivers, then try the next configuration */
1023 if ( list_empty ( &usb->functions ) ) {
1024 rc = -ENOTSUP;
1025 usb_deconfigure ( usb );
1026 continue;
1027 }
1028
1029 return 0;
1030 }
1031
1032 return rc;
1033 }
1034
1035 /******************************************************************************
1036 *
1037 * USB device
1038 *
1039 ******************************************************************************
1040 */
1041
1042 /**
1043 * Allocate USB device
1044 *
1045 * @v port USB port
1046 * @ret usb USB device, or NULL on allocation failure
1047 */
1048 static struct usb_device * alloc_usb ( struct usb_port *port ) {
1049 struct usb_hub *hub = port->hub;
1050 struct usb_bus *bus = hub->bus;
1051 struct usb_device *usb;
1052
1053 /* Allocate and initialise structure */
1054 usb = zalloc ( sizeof ( *usb ) );
1055 if ( ! usb )
1056 return NULL;
1057 snprintf ( usb->name, sizeof ( usb->name ), "%s%c%d", hub->name,
1058 ( hub->usb ? '.' : '-' ), port->address );
1059 usb->port = port;
1060 INIT_LIST_HEAD ( &usb->functions );
1061 usb->host = &bus->op->device;
1062 usb_endpoint_init ( &usb->control, usb, &usb_control_operations );
1063 INIT_LIST_HEAD ( &usb->complete );
1064
1065 return usb;
1066 }
1067
1068 /**
1069 * Register USB device
1070 *
1071 * @v usb USB device
1072 * @ret rc Return status code
1073 */
1074 static int register_usb ( struct usb_device *usb ) {
1075 struct usb_port *port = usb->port;
1076 struct usb_hub *hub = port->hub;
1077 struct usb_bus *bus = hub->bus;
1078 unsigned int protocol;
1079 size_t mtu;
1080 int rc;
1081
1082 /* Add to port */
1083 if ( port->usb != NULL ) {
1084 DBGC ( hub, "USB hub %s port %d is already registered to %s\n",
1085 hub->name, port->address, port->usb->name );
1086 rc = -EALREADY;
1087 goto err_already;
1088 }
1089 port->usb = usb;
1090
1091 /* Add to bus device list */
1092 list_add_tail ( &usb->list, &bus->devices );
1093
1094 /* Enable device */
1095 if ( ( rc = hub->driver->enable ( hub, port ) ) != 0 ) {
1096 DBGC ( hub, "USB hub %s port %d could not enable: %s\n",
1097 hub->name, port->address, strerror ( rc ) );
1098 goto err_enable;
1099 }
1100
1101 /* Get device speed */
1102 if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
1103 DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
1104 hub->name, port->address, strerror ( rc ) );
1105 goto err_speed;
1106 }
1107 DBGC2 ( usb, "USB %s attached as %s-speed device\n",
1108 usb->name, usb_speed_name ( port->speed ) );
1109
1110 /* Open device */
1111 if ( ( rc = usb->host->open ( usb ) ) != 0 ) {
1112 DBGC ( usb, "USB %s could not open: %s\n",
1113 usb->name, strerror ( rc ) );
1114 goto err_open;
1115 }
1116
1117 /* Describe control endpoint */
1118 mtu = USB_EP0_DEFAULT_MTU ( port->speed );
1119 usb_endpoint_describe ( &usb->control, USB_EP0_ADDRESS,
1120 USB_EP0_ATTRIBUTES, mtu, USB_EP0_BURST );
1121
1122 /* Open control endpoint */
1123 if ( ( rc = usb_endpoint_open ( &usb->control ) ) != 0 )
1124 goto err_open_control;
1125 assert ( usb_endpoint ( usb, USB_EP0_ADDRESS ) == &usb->control );
1126
1127 /* Assign device address */
1128 if ( ( rc = usb->host->address ( usb ) ) != 0 ) {
1129 DBGC ( usb, "USB %s could not set address: %s\n",
1130 usb->name, strerror ( rc ) );
1131 goto err_address;
1132 }
1133 DBGC2 ( usb, "USB %s assigned address %d\n", usb->name, usb->address );
1134
1135 /* Read first part of device descriptor to get EP0 MTU */
1136 if ( ( rc = usb_get_mtu ( usb, &usb->device ) ) != 0 ) {
1137 DBGC ( usb, "USB %s could not get MTU: %s\n",
1138 usb->name, strerror ( rc ) );
1139 goto err_get_mtu;
1140 }
1141
1142 /* Calculate EP0 MTU */
1143 protocol = le16_to_cpu ( usb->device.protocol );
1144 mtu = ( ( protocol < USB_PROTO_3_0 ) ?
1145 usb->device.mtu : ( 1 << usb->device.mtu ) );
1146 DBGC2 ( usb, "USB %s has control MTU %zd (guessed %zd)\n",
1147 usb->name, mtu, usb->control.mtu );
1148
1149 /* Update MTU */
1150 if ( ( rc = usb_endpoint_mtu ( &usb->control, mtu ) ) != 0 )
1151 goto err_mtu;
1152
1153 /* Read whole device descriptor */
1154 if ( ( rc = usb_get_device_descriptor ( usb, &usb->device ) ) != 0 ) {
1155 DBGC ( usb, "USB %s could not get device descriptor: %s\n",
1156 usb->name, strerror ( rc ) );
1157 goto err_get_device_descriptor;
1158 }
1159 DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d (v%s, %s-speed, "
1160 "MTU %zd)\n", usb->name, usb->address,
1161 le16_to_cpu ( usb->device.vendor ),
1162 le16_to_cpu ( usb->device.product ), usb->device.class.class,
1163 usb->device.class.subclass, usb->device.class.protocol,
1164 usb_bcd ( le16_to_cpu ( usb->device.protocol ) ),
1165 usb_speed_name ( port->speed ), usb->control.mtu );
1166
1167 /* Configure device */
1168 if ( ( rc = usb_configure_any ( usb ) ) != 0 )
1169 goto err_configure_any;
1170
1171 return 0;
1172
1173 usb_deconfigure ( usb );
1174 err_configure_any:
1175 err_get_device_descriptor:
1176 err_mtu:
1177 err_get_mtu:
1178 err_address:
1179 usb_endpoint_close ( &usb->control );
1180 err_open_control:
1181 usb->host->close ( usb );
1182 err_open:
1183 err_speed:
1184 hub->driver->disable ( hub, port );
1185 err_enable:
1186 list_del ( &usb->list );
1187 port->usb = NULL;
1188 err_already:
1189 return rc;
1190 }
1191
1192 /**
1193 * Unregister USB device
1194 *
1195 * @v usb USB device
1196 */
1197 static void unregister_usb ( struct usb_device *usb ) {
1198 struct usb_port *port = usb->port;
1199 struct usb_hub *hub = port->hub;
1200 struct io_buffer *iobuf;
1201 struct io_buffer *tmp;
1202
1203 /* Sanity checks */
1204 assert ( port->usb == usb );
1205
1206 /* Clear device configuration */
1207 usb_deconfigure ( usb );
1208
1209 /* Close control endpoint */
1210 usb_endpoint_close ( &usb->control );
1211
1212 /* Discard any stale control completions */
1213 list_for_each_entry_safe ( iobuf, tmp, &usb->complete, list ) {
1214 list_del ( &iobuf->list );
1215 free_iob ( iobuf );
1216 }
1217
1218 /* Close device */
1219 usb->host->close ( usb );
1220
1221 /* Disable port */
1222 hub->driver->disable ( hub, port );
1223
1224 /* Remove from bus device list */
1225 list_del ( &usb->list );
1226
1227 /* Remove from port */
1228 port->usb = NULL;
1229 }
1230
1231 /**
1232 * Free USB device
1233 *
1234 * @v usb USB device
1235 */
1236 static void free_usb ( struct usb_device *usb ) {
1237 unsigned int i;
1238
1239 /* Sanity checks */
1240 for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++ )
1241 assert ( usb->ep[i] == NULL );
1242 assert ( list_empty ( &usb->functions ) );
1243 assert ( list_empty ( &usb->complete ) );
1244
1245 /* Free device */
1246 free ( usb );
1247 }
1248
1249 /******************************************************************************
1250 *
1251 * USB device hotplug event handling
1252 *
1253 ******************************************************************************
1254 */
1255
1256 /**
1257 * Handle newly attached USB device
1258 *
1259 * @v port USB port
1260 * @ret rc Return status code
1261 */
1262 static int usb_attached ( struct usb_port *port ) {
1263 struct usb_device *usb;
1264 int rc;
1265
1266 /* Sanity checks */
1267 assert ( port->usb == NULL );
1268
1269 /* Allocate USB device */
1270 usb = alloc_usb ( port );
1271 if ( ! usb ) {
1272 rc = -ENOMEM;
1273 goto err_alloc;
1274 }
1275
1276 /* Register USB device */
1277 if ( ( rc = register_usb ( usb ) ) != 0 )
1278 goto err_register;
1279
1280 return 0;
1281
1282 unregister_usb ( usb );
1283 err_register:
1284 free_usb ( usb );
1285 err_alloc:
1286 return rc;
1287 }
1288
1289 /**
1290 * Handle newly detached USB device
1291 *
1292 * @v port USB port
1293 */
1294 static void usb_detached ( struct usb_port *port ) {
1295 struct usb_device *usb = port->usb;
1296
1297 /* Sanity checks */
1298 assert ( port->usb != NULL );
1299
1300 /* Unregister USB device */
1301 unregister_usb ( usb );
1302
1303 /* Free USB device */
1304 free_usb ( usb );
1305 }
1306
1307 /**
1308 * Handle newly attached or detached USB devices
1309 *
1310 * @v port USB port
1311 * @ret rc Return status code
1312 */
1313 static int usb_hotplug ( struct usb_port *port ) {
1314 struct usb_hub *hub = port->hub;
1315 int rc;
1316
1317 /* Get current port speed */
1318 if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
1319 DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
1320 hub->name, port->address, strerror ( rc ) );
1321 return rc;
1322 }
1323
1324 /* Handle attached/detached device as applicable */
1325 if ( port->speed && ! port->usb ) {
1326 /* Newly attached device */
1327 return usb_attached ( port );
1328 } else if ( port->usb && ! port->speed ) {
1329 /* Newly detached device */
1330 usb_detached ( port );
1331 return 0;
1332 } else {
1333 /* Ignore */
1334 return 0;
1335 }
1336 }
1337
1338 /******************************************************************************
1339 *
1340 * USB process
1341 *
1342 ******************************************************************************
1343 */
1344
1345 /**
1346 * Report port status change
1347 *
1348 * @v port USB port
1349 */
1350 void usb_port_changed ( struct usb_port *port ) {
1351 struct usb_hub *hub = port->hub;
1352 struct usb_bus *bus = hub->bus;
1353
1354 /* Record hub port status change */
1355 list_del ( &port->list );
1356 list_add_tail ( &port->list, &bus->changed );
1357 }
1358
1359 /**
1360 * USB process
1361 *
1362 * @v bus USB bus
1363 */
1364 static void usb_step ( struct usb_bus *bus ) {
1365 struct usb_port *port;
1366
1367 /* Poll bus */
1368 usb_poll ( bus );
1369
1370 /* Handle any changed ports, allowing for the fact that the
1371 * port list may change as we perform hotplug actions.
1372 */
1373 while ( ! list_empty ( &bus->changed ) ) {
1374
1375 /* Get first changed port */
1376 port = list_first_entry ( &bus->changed, struct usb_port, list);
1377 assert ( port != NULL );
1378
1379 /* Remove from list of changed ports */
1380 list_del ( &port->list );
1381 INIT_LIST_HEAD ( &port->list );
1382
1383 /* Perform appropriate hotplug action */
1384 usb_hotplug ( port );
1385 }
1386 }
1387
1388 /** USB process */
1389 static struct process_descriptor usb_process_desc =
1390 PROC_DESC ( struct usb_bus, process, usb_step );
1391
1392 /******************************************************************************
1393 *
1394 * USB hub
1395 *
1396 ******************************************************************************
1397 */
1398
1399 /**
1400 * Allocate USB hub
1401 *
1402 * @v bus USB bus
1403 * @v usb Underlying USB device, if any
1404 * @v ports Number of ports
1405 * @v driver Hub driver operations
1406 * @ret hub USB hub, or NULL on allocation failure
1407 */
1408 struct usb_hub * alloc_usb_hub ( struct usb_bus *bus, struct usb_device *usb,
1409 unsigned int ports,
1410 struct usb_hub_driver_operations *driver ) {
1411 struct usb_hub *hub;
1412 struct usb_port *port;
1413 unsigned int i;
1414
1415 /* Allocate and initialise structure */
1416 hub = zalloc ( sizeof ( *hub ) + ( ports * sizeof ( hub->port[0] ) ) );
1417 if ( ! hub )
1418 return NULL;
1419 hub->name = ( usb ? usb->name : bus->name );
1420 hub->bus = bus;
1421 hub->usb = usb;
1422 if ( usb )
1423 hub->protocol = usb->port->protocol;
1424 hub->ports = ports;
1425 hub->driver = driver;
1426
1427 /* Initialise port list */
1428 for ( i = 1 ; i <= hub->ports ; i++ ) {
1429 port = usb_port ( hub, i );
1430 port->hub = hub;
1431 port->address = i;
1432 if ( usb )
1433 port->protocol = usb->port->protocol;
1434 INIT_LIST_HEAD ( &port->list );
1435 }
1436
1437 return hub;
1438 }
1439
1440 /**
1441 * Register USB hub
1442 *
1443 * @v hub USB hub
1444 * @ret rc Return status code
1445 */
1446 int register_usb_hub ( struct usb_hub *hub ) {
1447 struct usb_bus *bus = hub->bus;
1448 struct usb_port *port;
1449 unsigned int i;
1450 int rc;
1451
1452 /* Add to hub list */
1453 list_add_tail ( &hub->list, &bus->hubs );
1454
1455 /* Open hub */
1456 if ( ( rc = hub->driver->open ( hub ) ) != 0 ) {
1457 DBGC ( hub, "USB hub %s could not open: %s\n",
1458 hub->name, strerror ( rc ) );
1459 goto err_open;
1460 }
1461
1462 /* Delay to allow ports to stabilise */
1463 mdelay ( USB_PORT_DELAY_MS );
1464
1465 /* Attach any devices already present */
1466 for ( i = 1 ; i <= hub->ports ; i++ ) {
1467 port = usb_port ( hub, i );
1468 usb_hotplug ( port );
1469 }
1470
1471 /* Some hubs seem to defer reporting device connections until
1472 * their interrupt endpoint is polled for the first time.
1473 * Poll the bus once now in order to pick up any such
1474 * connections.
1475 */
1476 usb_step ( bus );
1477
1478 return 0;
1479
1480 hub->driver->close ( hub );
1481 err_open:
1482 list_del ( &hub->list );
1483 return rc;
1484 }
1485
1486 /**
1487 * Unregister USB hub
1488 *
1489 * @v hub USB hub
1490 */
1491 void unregister_usb_hub ( struct usb_hub *hub ) {
1492 struct usb_port *port;
1493 unsigned int i;
1494
1495 /* Detach all devices */
1496 for ( i = 1 ; i <= hub->ports ; i++ ) {
1497 port = usb_port ( hub, i );
1498 if ( port->usb )
1499 usb_detached ( port );
1500 }
1501
1502 /* Close hub */
1503 hub->driver->close ( hub );
1504
1505 /* Cancel any pending port status changes */
1506 for ( i = 1 ; i <= hub->ports ; i++ ) {
1507 port = usb_port ( hub, i );
1508 list_del ( &port->list );
1509 INIT_LIST_HEAD ( &port->list );
1510 }
1511
1512 /* Remove from hub list */
1513 list_del ( &hub->list );
1514 }
1515
1516 /**
1517 * Free USB hub
1518 *
1519 * @v hub USB hub
1520 */
1521 void free_usb_hub ( struct usb_hub *hub ) {
1522 struct usb_port *port;
1523 unsigned int i;
1524
1525 /* Sanity checks */
1526 for ( i = 1 ; i <= hub->ports ; i++ ) {
1527 port = usb_port ( hub, i );
1528 assert ( port->usb == NULL );
1529 assert ( list_empty ( &port->list ) );
1530 }
1531
1532 /* Free hub */
1533 free ( hub );
1534 }
1535
1536 /******************************************************************************
1537 *
1538 * USB bus
1539 *
1540 ******************************************************************************
1541 */
1542
1543 /**
1544 * Allocate USB bus
1545 *
1546 * @v dev Underlying hardware device
1547 * @v ports Number of root hub ports
1548 * @v op Host controller operations
1549 * @ret bus USB bus, or NULL on allocation failure
1550 */
1551 struct usb_bus * alloc_usb_bus ( struct device *dev, unsigned int ports,
1552 struct usb_host_operations *op ) {
1553 struct usb_bus *bus;
1554
1555 /* Allocate and initialise structure */
1556 bus = zalloc ( sizeof ( *bus ) );
1557 if ( ! bus )
1558 goto err_alloc_bus;
1559 bus->name = dev->name;
1560 bus->dev = dev;
1561 bus->op = op;
1562 INIT_LIST_HEAD ( &bus->devices );
1563 INIT_LIST_HEAD ( &bus->hubs );
1564 INIT_LIST_HEAD ( &bus->changed );
1565 process_init_stopped ( &bus->process, &usb_process_desc, NULL );
1566 bus->host = &bus->op->bus;
1567
1568 /* Allocate root hub */
1569 bus->hub = alloc_usb_hub ( bus, NULL, ports, &op->hub );
1570 if ( ! bus->hub )
1571 goto err_alloc_hub;
1572
1573 return bus;
1574
1575 free_usb_hub ( bus->hub );
1576 err_alloc_hub:
1577 free ( bus );
1578 err_alloc_bus:
1579 return NULL;
1580 }
1581
1582 /**
1583 * Register USB bus
1584 *
1585 * @v bus USB bus
1586 * @ret rc Return status code
1587 */
1588 int register_usb_bus ( struct usb_bus *bus ) {
1589 int rc;
1590
1591 /* Sanity checks */
1592 assert ( bus->hub != NULL );
1593
1594 /* Open bus */
1595 if ( ( rc = bus->host->open ( bus ) ) != 0 )
1596 goto err_open;
1597
1598 /* Register root hub */
1599 if ( ( rc = register_usb_hub ( bus->hub ) ) != 0 )
1600 goto err_register_hub;
1601
1602 /* Start bus process */
1603 process_add ( &bus->process );
1604
1605 return 0;
1606
1607 unregister_usb_hub ( bus->hub );
1608 err_register_hub:
1609 bus->host->close ( bus );
1610 err_open:
1611 return rc;
1612 }
1613
1614 /**
1615 * Unregister USB bus
1616 *
1617 * @v bus USB bus
1618 */
1619 void unregister_usb_bus ( struct usb_bus *bus ) {
1620
1621 /* Sanity checks */
1622 assert ( bus->hub != NULL );
1623 assert ( process_running ( &bus->process ) );
1624
1625 /* Stop bus process */
1626 process_del ( &bus->process );
1627
1628 /* Unregister root hub */
1629 unregister_usb_hub ( bus->hub );
1630
1631 /* Close bus */
1632 bus->host->close ( bus );
1633
1634 /* Sanity checks */
1635 assert ( list_empty ( &bus->devices ) );
1636 assert ( list_empty ( &bus->hubs ) );
1637 assert ( ! process_running ( &bus->process ) );
1638 }
1639
1640 /**
1641 * Free USB bus
1642 *
1643 * @v bus USB bus
1644 */
1645 void free_usb_bus ( struct usb_bus *bus ) {
1646
1647 /* Sanity checks */
1648 assert ( list_empty ( &bus->devices ) );
1649 assert ( list_empty ( &bus->hubs ) );
1650 assert ( ! process_running ( &bus->process ) );
1651
1652 /* Free root hub */
1653 free_usb_hub ( bus->hub );
1654
1655 /* Free bus */
1656 free ( bus );
1657 }
1658
1659 /******************************************************************************
1660 *
1661 * USB bus topology
1662 *
1663 ******************************************************************************
1664 */
1665
1666 /**
1667 * Get USB route string
1668 *
1669 * @v usb USB device
1670 * @ret route USB route string
1671 */
1672 unsigned int usb_route_string ( struct usb_device *usb ) {
1673 unsigned int route;
1674
1675 /* Navigate up to root hub, constructing route string as we go */
1676 for ( route = 0 ; usb->port->hub->usb ; usb = usb->port->hub->usb ) {
1677 route <<= 4;
1678 route |= ( ( usb->port->address > 0xf ) ?
1679 0xf : usb->port->address );
1680 }
1681 return route;
1682 }
1683
1684 /**
1685 * Get USB depth
1686 *
1687 * @v usb USB device
1688 * @ret depth Hub depth
1689 */
1690 unsigned int usb_depth ( struct usb_device *usb ) {
1691 unsigned int depth;
1692
1693 /* Navigate up to root hub, constructing depth as we go */
1694 for ( depth = 0 ; usb->port->hub->usb ; usb = usb->port->hub->usb )
1695 depth++;
1696
1697 return depth;
1698 }
1699
1700 /**
1701 * Get USB root hub port
1702 *
1703 * @v usb USB device
1704 * @ret port Root hub port
1705 */
1706 struct usb_port * usb_root_hub_port ( struct usb_device *usb ) {
1707
1708 /* Navigate up to root hub */
1709 while ( usb->port->hub->usb )
1710 usb = usb->port->hub->usb;
1711
1712 return usb->port;
1713 }
1714
1715 /* Drag in hub driver */
1716 REQUIRE_OBJECT ( usbhub );