iPXE
usb.c
Go to the documentation of this file.
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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <assert.h>
33 #include <byteswap.h>
34 #include <ipxe/usb.h>
35 #include <ipxe/cdc.h>
36 
37 /** @file
38  *
39  * Universal Serial Bus (USB)
40  *
41  */
42 
43 /** List of USB buses */
45 
46 /** List of changed ports */
48 
49 /** List of halted endpoints */
51 
52 /******************************************************************************
53  *
54  * Utility functions
55  *
56  ******************************************************************************
57  */
58 
59 /**
60  * Get USB speed name (for debugging)
61  *
62  * @v speed Speed
63  * @ret name Speed name
64  */
65 static inline const char * usb_speed_name ( unsigned int speed ) {
66  static const char *exponents[4] = { "", "k", "M", "G" };
67  static char buf[ 10 /* "xxxxxXbps" + NUL */ ];
68  unsigned int mantissa;
69  unsigned int exponent;
70 
71  /* Extract mantissa and exponent */
72  mantissa = USB_SPEED_MANTISSA ( speed );
73  exponent = USB_SPEED_EXPONENT ( speed );
74 
75  /* Name speed */
76  switch ( speed ) {
77  case USB_SPEED_NONE: return "DETACHED";
78  case USB_SPEED_LOW: return "low";
79  case USB_SPEED_FULL: return "full";
80  case USB_SPEED_HIGH: return "high";
81  case USB_SPEED_SUPER: return "super";
82  default:
83  snprintf ( buf, sizeof ( buf ), "%d%sbps",
84  mantissa, exponents[exponent] );
85  return buf;
86  }
87 }
88 
89 /**
90  * Transcribe USB BCD-coded value (for debugging)
91  *
92  * @v bcd BCD-coded value
93  * @ret string Transcribed value
94  */
95 static inline const char * usb_bcd ( uint16_t bcd ) {
96  static char buf[ 6 /* "xx.xx" + NUL */ ];
97  uint8_t high = ( bcd >> 8 );
98  uint8_t low = ( bcd >> 0 );
99 
100  snprintf ( buf, sizeof ( buf ), "%x.%02x", high, low );
101  return buf;
102 }
103 
104 /******************************************************************************
105  *
106  * USB descriptors
107  *
108  ******************************************************************************
109  */
110 
111 /**
112  * Locate USB interface association descriptor
113  *
114  * @v config Configuraton descriptor
115  * @v first First interface number
116  * @ret desc Interface association descriptor, or NULL if not found
117  */
120  *config,
121  unsigned int first ) {
123 
124  /* Find a matching interface association descriptor */
125  for_each_config_descriptor ( desc, config ) {
126  if ( ( desc->header.type ==
128  ( desc->first == first ) )
129  return desc;
130  }
131  return NULL;
132 }
133 
134 /**
135  * Locate USB interface descriptor
136  *
137  * @v config Configuraton descriptor
138  * @v interface Interface number
139  * @v alternate Alternate setting
140  * @ret desc Interface descriptor, or NULL if not found
141  */
144  unsigned int interface, unsigned int alternate ) {
146 
147  /* Find a matching interface descriptor */
148  for_each_config_descriptor ( desc, config ) {
149  if ( ( desc->header.type == USB_INTERFACE_DESCRIPTOR ) &&
150  ( desc->interface == interface ) &&
151  ( desc->alternate == alternate ) )
152  return desc;
153  }
154  return NULL;
155 }
156 
157 /**
158  * Locate USB endpoint descriptor
159  *
160  * @v config Configuration descriptor
161  * @v interface Interface descriptor
162  * @v type Endpoint (internal) type
163  * @v index Endpoint index
164  * @ret desc Descriptor, or NULL if not found
165  */
169  unsigned int type, unsigned int index ) {
171  unsigned int attributes = ( type & USB_ENDPOINT_ATTR_TYPE_MASK );
172  unsigned int direction = ( type & USB_DIR_IN );
173 
174  /* Find a matching endpoint descriptor */
176  if ( ( desc->header.type == USB_ENDPOINT_DESCRIPTOR ) &&
177  ( ( desc->attributes &
179  ( ( desc->endpoint & USB_DIR_IN ) == direction ) &&
180  ( index-- == 0 ) )
181  return desc;
182  }
183  return NULL;
184 }
185 
186 /**
187  * Locate USB endpoint companion descriptor
188  *
189  * @v config Configuration descriptor
190  * @v desc Endpoint descriptor
191  * @ret descx Companion descriptor, or NULL if not found
192  */
195  struct usb_endpoint_descriptor *desc ) {
196  struct usb_endpoint_companion_descriptor *descx;
197 
198  /* Get companion descriptor, if present */
199  descx = container_of ( usb_next_descriptor ( &desc->header ),
201  header );
202  return ( ( usb_is_within_config ( config, &descx->header ) &&
204  ? descx : NULL );
205 }
206 
207 /******************************************************************************
208  *
209  * USB endpoint
210  *
211  ******************************************************************************
212  */
213 
214 /**
215  * Get USB endpoint name (for debugging)
216  *
217  * @v ep USB endpoint
218  * @ret name Endpoint name
219  */
220 const char * usb_endpoint_name ( struct usb_endpoint *ep ) {
221  static char buf[ 9 /* "EPxx OUT" + NUL */ ];
222  unsigned int address = ep->address;
223 
224  snprintf ( buf, sizeof ( buf ), "EP%d%s",
226  ( address ?
227  ( ( address & USB_ENDPOINT_IN ) ? " IN" : " OUT" ) : "" ));
228  return buf;
229 }
230 
231 /**
232  * Describe USB endpoint from device configuration
233  *
234  * @v ep USB endpoint
235  * @v config Configuration descriptor
236  * @v interface Interface descriptor
237  * @v type Endpoint (internal) type
238  * @v index Endpoint index
239  * @ret rc Return status code
240  */
242  struct usb_configuration_descriptor *config,
244  unsigned int type, unsigned int index ) {
245  struct usb_device *usb = ep->usb;
247  struct usb_endpoint_companion_descriptor *descx;
248  unsigned int sizes;
249  unsigned int burst;
250  unsigned int interval;
251  size_t mtu;
252 
253  /* Locate endpoint descriptor */
255  if ( ! desc )
256  return -ENOENT;
257 
258  /* Locate companion descriptor, if any */
259  descx = usb_endpoint_companion_descriptor ( config, desc );
260 
261  /* Calculate MTU and burst size */
262  sizes = le16_to_cpu ( desc->sizes );
263  mtu = USB_ENDPOINT_MTU ( sizes );
264  burst = ( descx ? descx->burst : USB_ENDPOINT_BURST ( sizes ) );
265 
266  /* Calculate interval */
267  if ( ( type & USB_ENDPOINT_ATTR_TYPE_MASK ) ==
269  if ( usb->speed >= USB_SPEED_HIGH ) {
270  /* 2^(desc->interval-1) is a microframe count */
271  interval = ( 1 << ( desc->interval - 1 ) );
272  } else {
273  /* desc->interval is a (whole) frame count */
274  interval = ( desc->interval << 3 );
275  }
276  } else {
277  /* desc->interval is a microframe count */
278  interval = desc->interval;
279  }
280 
281  /* Describe endpoint */
282  usb_endpoint_describe ( ep, desc->endpoint, desc->attributes,
283  mtu, burst, interval );
284  return 0;
285 }
286 
287 /**
288  * Open USB endpoint
289  *
290  * @v ep USB endpoint
291  * @ret rc Return status code
292  */
293 int usb_endpoint_open ( struct usb_endpoint *ep ) {
294  struct usb_device *usb = ep->usb;
295  unsigned int idx = USB_ENDPOINT_IDX ( ep->address );
296  int rc;
297 
298  /* Populate host controller operations */
299  ep->host = &usb->port->hub->bus->op->endpoint;
300 
301  /* Add to endpoint list */
302  if ( usb->ep[idx] != NULL ) {
303  DBGC ( usb, "USB %s %s is already open\n",
304  usb->name, usb_endpoint_name ( ep ) );
305  rc = -EALREADY;
306  goto err_already;
307  }
308  usb->ep[idx] = ep;
309  INIT_LIST_HEAD ( &ep->halted );
310 
311  /* Open endpoint */
312  if ( ( rc = ep->host->open ( ep ) ) != 0 ) {
313  DBGC ( usb, "USB %s %s could not open: %s\n", usb->name,
314  usb_endpoint_name ( ep ), strerror ( rc ) );
315  goto err_open;
316  }
317  ep->open = 1;
318 
319  DBGC2 ( usb, "USB %s %s opened with MTU %zd, burst %d, interval %d\n",
320  usb->name, usb_endpoint_name ( ep ), ep->mtu, ep->burst,
321  ep->interval );
322  return 0;
323 
324  ep->open = 0;
325  ep->host->close ( ep );
326  err_open:
327  usb->ep[idx] = NULL;
328  err_already:
329  if ( ep->max )
330  usb_flush ( ep );
331  return rc;
332 }
333 
334 /**
335  * Clear transaction translator (if applicable)
336  *
337  * @v ep USB endpoint
338  * @ret rc Return status code
339  */
340 static int usb_endpoint_clear_tt ( struct usb_endpoint *ep ) {
341  struct usb_device *usb = ep->usb;
342  struct usb_port *tt;
343  int rc;
344 
345  /* Do nothing if this is a periodic endpoint */
347  return 0;
348 
349  /* Do nothing if this endpoint is not behind a transaction translator */
351  if ( ! tt )
352  return 0;
353 
354  /* Clear transaction translator buffer */
355  if ( ( rc = tt->hub->driver->clear_tt ( tt->hub, tt, ep ) ) != 0 ) {
356  DBGC ( usb, "USB %s %s could not clear transaction translator: "
357  "%s\n", usb->name, usb_endpoint_name ( ep ),
358  strerror ( rc ) );
359  return rc;
360  }
361 
362  return 0;
363 }
364 
365 /**
366  * Clear endpoint halt (if applicable)
367  *
368  * @v ep USB endpoint
369  * @ret rc Return status code
370  */
372  struct usb_device *usb = ep->usb;
373  unsigned int type;
374  int rc;
375 
376  /* Clear transaction translator, if applicable */
377  if ( ( rc = usb_endpoint_clear_tt ( ep ) ) != 0 )
378  return rc;
379 
380  /* Clear endpoint halt (if applicable) */
382  if ( ( type != USB_ENDPOINT_ATTR_CONTROL ) &&
385  ep->address ) ) != 0 ) ) {
386  DBGC ( usb, "USB %s %s could not clear endpoint halt: %s\n",
387  usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
388  return rc;
389  }
390 
391  return 0;
392 }
393 
394 /**
395  * Close USB endpoint
396  *
397  * @v ep USB endpoint
398  */
400  struct usb_device *usb = ep->usb;
401  unsigned int idx = USB_ENDPOINT_IDX ( ep->address );
402 
403  /* Sanity checks */
404  assert ( usb->ep[idx] == ep );
405 
406  /* Close endpoint */
407  ep->open = 0;
408  ep->host->close ( ep );
409  assert ( ep->fill == 0 );
410 
411  /* Remove from endpoint list */
412  usb->ep[idx] = NULL;
413  list_del ( &ep->halted );
414 
415  /* Discard any recycled buffers, if applicable */
416  if ( ep->max )
417  usb_flush ( ep );
418 
419  /* Clear transaction translator, if applicable */
421 }
422 
423 /**
424  * Reset USB endpoint
425  *
426  * @v ep USB endpoint
427  * @ret rc Return status code
428  */
429 static int usb_endpoint_reset ( struct usb_endpoint *ep ) {
430  struct usb_device *usb = ep->usb;
431  int rc;
432 
433  /* Sanity check */
434  assert ( ! list_empty ( &ep->halted ) );
435 
436  /* Clear device halt, if applicable */
437  if ( ( rc = usb_endpoint_clear_halt ( ep ) ) != 0 )
438  return rc;
439 
440  /* Reset endpoint */
441  if ( ( rc = ep->host->reset ( ep ) ) != 0 ) {
442  DBGC ( usb, "USB %s %s could not reset: %s\n",
443  usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
444  return rc;
445  }
446 
447  /* Remove from list of halted endpoints */
448  list_del ( &ep->halted );
449  INIT_LIST_HEAD ( &ep->halted );
450 
451  DBGC ( usb, "USB %s %s reset\n",
452  usb->name, usb_endpoint_name ( ep ) );
453  return 0;
454 }
455 
456 /**
457  * Update endpoint MTU
458  *
459  * @v ep USB endpoint
460  * @v mtu New MTU
461  * @ret rc Return status code
462  */
463 static int usb_endpoint_mtu ( struct usb_endpoint *ep, size_t mtu ) {
464  struct usb_device *usb = ep->usb;
465  int rc;
466 
467  /* Update MTU */
468  ep->mtu = mtu;
469  if ( ( rc = ep->host->mtu ( ep ) ) != 0 ) {
470  DBGC ( usb, "USB %s %s could not update MTU: %s\n",
471  usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
472  return rc;
473  }
474 
475  return 0;
476 }
477 
478 /**
479  * Enqueue USB message transfer
480  *
481  * @v ep USB endpoint
482  * @v request Request
483  * @v value Value parameter
484  * @v index Index parameter
485  * @v iobuf I/O buffer
486  * @ret rc Return status code
487  *
488  * The I/O buffer must have sufficient headroom to contain a setup
489  * packet.
490  */
491 int usb_message ( struct usb_endpoint *ep, unsigned int request,
492  unsigned int value, unsigned int index,
493  struct io_buffer *iobuf ) {
494  struct usb_device *usb = ep->usb;
495  struct usb_port *port = usb->port;
496  struct usb_setup_packet *packet;
497  size_t len = iob_len ( iobuf );
498  int rc;
499 
500  /* Sanity check */
501  assert ( iob_headroom ( iobuf ) >= sizeof ( *packet ) );
502 
503  /* Fail immediately if device has been unplugged */
504  if ( port->disconnected )
505  return -ENODEV;
506 
507  /* Reset endpoint if required */
508  if ( ( ! list_empty ( &ep->halted ) ) &&
509  ( ( rc = usb_endpoint_reset ( ep ) ) != 0 ) )
510  return rc;
511 
512  /* Zero input data buffer (if applicable) */
513  if ( request & USB_DIR_IN )
514  memset ( iobuf->data, 0, len );
515 
516  /* Construct setup packet */
517  packet = iob_push ( iobuf, sizeof ( *packet ) );
518  packet->request = cpu_to_le16 ( request );
519  packet->value = cpu_to_le16 ( value );
520  packet->index = cpu_to_le16 ( index );
521  packet->len = cpu_to_le16 ( len );
522 
523  /* Enqueue message transfer */
524  if ( ( rc = ep->host->message ( ep, iobuf ) ) != 0 ) {
525  DBGC ( usb, "USB %s %s could not enqueue message transfer: "
526  "%s\n", usb->name, usb_endpoint_name ( ep ),
527  strerror ( rc ) );
528  return rc;
529  }
530 
531  /* Increment fill level */
532  ep->fill++;
533 
534  return 0;
535 }
536 
537 /**
538  * Enqueue USB stream transfer
539  *
540  * @v ep USB endpoint
541  * @v iobuf I/O buffer
542  * @v terminate Terminate using a short packet
543  * @ret rc Return status code
544  */
545 int usb_stream ( struct usb_endpoint *ep, struct io_buffer *iobuf,
546  int terminate ) {
547  struct usb_device *usb = ep->usb;
548  struct usb_port *port = usb->port;
549  int zlp;
550  int rc;
551 
552  /* Fail immediately if device has been unplugged */
553  if ( port->disconnected )
554  return -ENODEV;
555 
556  /* Reset endpoint if required */
557  if ( ( ! list_empty ( &ep->halted ) ) &&
558  ( ( rc = usb_endpoint_reset ( ep ) ) != 0 ) )
559  return rc;
560 
561  /* Append a zero-length packet if necessary */
562  zlp = terminate;
563  if ( iob_len ( iobuf ) & ( ep->mtu - 1 ) )
564  zlp = 0;
565 
566  /* Enqueue stream transfer */
567  if ( ( rc = ep->host->stream ( ep, iobuf, zlp ) ) != 0 ) {
568  DBGC ( usb, "USB %s %s could not enqueue stream transfer: %s\n",
569  usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
570  return rc;
571  }
572 
573  /* Increment fill level */
574  ep->fill++;
575 
576  return 0;
577 }
578 
579 /**
580  * Complete transfer (possibly with error)
581  *
582  * @v ep USB endpoint
583  * @v iobuf I/O buffer
584  * @v rc Completion status code
585  */
586 void usb_complete_err ( struct usb_endpoint *ep, struct io_buffer *iobuf,
587  int rc ) {
588  struct usb_device *usb = ep->usb;
589 
590  /* Decrement fill level */
591  assert ( ep->fill > 0 );
592  ep->fill--;
593 
594  /* Schedule reset, if applicable */
595  if ( ( rc != 0 ) && ep->open ) {
596  DBGC ( usb, "USB %s %s completion failed: %s\n",
597  usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
598  list_del ( &ep->halted );
600  }
601 
602  /* Report completion */
603  ep->driver->complete ( ep, iobuf, rc );
604 }
605 
606 /******************************************************************************
607  *
608  * Endpoint refilling
609  *
610  ******************************************************************************
611  */
612 
613 /**
614  * Prefill endpoint recycled buffer list
615  *
616  * @v ep USB endpoint
617  * @ret rc Return status code
618  */
619 int usb_prefill ( struct usb_endpoint *ep ) {
620  struct io_buffer *iobuf;
621  size_t reserve = ep->reserve;
622  size_t len = ( ep->len ? ep->len : ep->mtu );
623  unsigned int fill;
624  int rc;
625 
626  /* Sanity checks */
627  assert ( ep->fill == 0 );
628  assert ( ep->max > 0 );
629  assert ( list_empty ( &ep->recycled ) );
630 
631  /* Fill recycled buffer list */
632  for ( fill = 0 ; fill < ep->max ; fill++ ) {
633 
634  /* Allocate I/O buffer */
635  iobuf = alloc_iob ( reserve + len );
636  if ( ! iobuf ) {
637  rc = -ENOMEM;
638  goto err_alloc;
639  }
640  iob_reserve ( iobuf, reserve );
641 
642  /* Add to recycled buffer list */
643  list_add_tail ( &iobuf->list, &ep->recycled );
644  }
645 
646  return 0;
647 
648  err_alloc:
649  usb_flush ( ep );
650  return rc;
651 }
652 
653 /**
654  * Refill endpoint up to specified limit
655  *
656  * @v ep USB endpoint
657  * @v max Fill limit
658  * @ret rc Return status code
659  */
660 int usb_refill_limit ( struct usb_endpoint *ep, unsigned int max ) {
661  struct io_buffer *iobuf;
662  size_t reserve = ep->reserve;
663  size_t len = ( ep->len ? ep->len : ep->mtu );
664  int rc;
665 
666  /* Sanity checks */
667  assert ( ep->open );
668  assert ( ep->max > 0 );
669 
670  /* Refill endpoint */
671  if ( max > ep->max )
672  max = ep->max;
673  while ( ep->fill < max ) {
674 
675  /* Get or allocate buffer */
676  if ( list_empty ( &ep->recycled ) ) {
677  /* Recycled buffer list is empty; allocate new buffer */
678  iobuf = alloc_iob ( reserve + len );
679  if ( ! iobuf )
680  return -ENOMEM;
681  iob_reserve ( iobuf, reserve );
682  } else {
683  /* Get buffer from recycled buffer list */
684  iobuf = list_first_entry ( &ep->recycled,
685  struct io_buffer, list );
686  assert ( iobuf != NULL );
687  list_del ( &iobuf->list );
688  }
689 
690  /* Reset buffer to maximum size */
691  assert ( iob_len ( iobuf ) <= len );
692  iob_put ( iobuf, ( len - iob_len ( iobuf ) ) );
693 
694  /* Enqueue buffer */
695  if ( ( rc = usb_stream ( ep, iobuf, 0 ) ) != 0 ) {
696  list_add ( &iobuf->list, &ep->recycled );
697  return rc;
698  }
699  }
700 
701  return 0;
702 }
703 
704 /**
705  * Refill endpoint
706  *
707  * @v ep USB endpoint
708  * @ret rc Return status code
709  */
710 int usb_refill ( struct usb_endpoint *ep ) {
711  return usb_refill_limit ( ep, ep->max );
712 }
713 
714 /**
715  * Discard endpoint recycled buffer list
716  *
717  * @v ep USB endpoint
718  */
719 void usb_flush ( struct usb_endpoint *ep ) {
720  struct io_buffer *iobuf;
721  struct io_buffer *tmp;
722 
723  /* Sanity checks */
724  assert ( ! ep->open );
725  assert ( ep->max > 0 );
726 
727  /* Free all I/O buffers */
728  list_for_each_entry_safe ( iobuf, tmp, &ep->recycled, list ) {
729  list_del ( &iobuf->list );
730  free_iob ( iobuf );
731  }
732 }
733 
734 /******************************************************************************
735  *
736  * Control endpoint
737  *
738  ******************************************************************************
739  */
740 
741 /** USB control transfer pseudo-header */
743  /** Completion status */
744  int rc;
745 };
746 
747 /**
748  * Complete USB control transfer
749  *
750  * @v ep USB endpoint
751  * @v iobuf I/O buffer
752  * @v rc Completion status code
753  */
754 static void usb_control_complete ( struct usb_endpoint *ep,
755  struct io_buffer *iobuf, int rc ) {
756  struct usb_device *usb = ep->usb;
757  struct usb_control_pseudo_header *pshdr;
758 
759  /* Record completion status in buffer */
760  pshdr = iob_push ( iobuf, sizeof ( *pshdr ) );
761  pshdr->rc = rc;
762 
763  /* Add to list of completed I/O buffers */
764  list_add_tail ( &iobuf->list, &usb->complete );
765 }
766 
767 /** USB control endpoint driver operations */
770 };
771 
772 /**
773  * Issue USB control transaction
774  *
775  * @v usb USB device
776  * @v request Request
777  * @v value Value parameter
778  * @v index Index parameter
779  * @v data Data buffer (if any)
780  * @v len Length of data
781  * @ret rc Return status code
782  */
783 int usb_control ( struct usb_device *usb, unsigned int request,
784  unsigned int value, unsigned int index, void *data,
785  size_t len ) {
786  struct usb_bus *bus = usb->port->hub->bus;
787  struct usb_endpoint *ep = &usb->control;
788  struct io_buffer *iobuf;
789  struct io_buffer *cmplt;
790  union {
791  struct usb_setup_packet setup;
792  struct usb_control_pseudo_header pshdr;
793  } *headroom;
794  struct usb_control_pseudo_header *pshdr;
795  unsigned int i;
796  int rc;
797 
798  /* Allocate I/O buffer */
799  iobuf = alloc_iob ( sizeof ( *headroom ) + len );
800  if ( ! iobuf ) {
801  rc = -ENOMEM;
802  goto err_alloc;
803  }
804  iob_reserve ( iobuf, sizeof ( *headroom ) );
805  iob_put ( iobuf, len );
806  if ( request & USB_DIR_IN ) {
807  memset ( data, 0, len );
808  } else {
809  memcpy ( iobuf->data, data, len );
810  }
811 
812  /* Enqueue message */
813  if ( ( rc = usb_message ( ep, request, value, index, iobuf ) ) != 0 )
814  goto err_message;
815 
816  /* Wait for completion */
817  for ( i = 0 ; i < USB_CONTROL_MAX_WAIT_MS ; i++ ) {
818 
819  /* Poll bus */
820  usb_poll ( bus );
821 
822  /* Check for completion */
823  while ( ( cmplt = list_first_entry ( &usb->complete,
824  struct io_buffer,
825  list ) ) ) {
826 
827  /* Remove from completion list */
828  list_del ( &cmplt->list );
829 
830  /* Extract and strip completion status */
831  pshdr = cmplt->data;
832  iob_pull ( cmplt, sizeof ( *pshdr ) );
833  rc = pshdr->rc;
834 
835  /* Discard stale completions */
836  if ( cmplt != iobuf ) {
837  DBGC ( usb, "USB %s stale control completion: "
838  "%s\n", usb->name, strerror ( rc ) );
839  DBGC_HDA ( usb, 0, cmplt->data,
840  iob_len ( cmplt ) );
841  free_iob ( cmplt );
842  continue;
843  }
844 
845  /* Fail immediately if completion was in error */
846  if ( rc != 0 ) {
847  DBGC ( usb, "USB %s control %04x:%04x:%04x "
848  "failed: %s\n", usb->name, request,
849  value, index, strerror ( rc ) );
850  free_iob ( cmplt );
851  usb_endpoint_reset ( ep );
852  return rc;
853  }
854 
855  /* Copy completion to data buffer, if applicable */
856  assert ( iob_len ( cmplt ) <= len );
857  if ( request & USB_DIR_IN )
858  memcpy ( data, cmplt->data, iob_len ( cmplt ) );
859  free_iob ( cmplt );
860  return 0;
861  }
862 
863  /* Delay */
864  mdelay ( 1 );
865  }
866 
867  DBGC ( usb, "USB %s timed out waiting for control %04x:%04x:%04x\n",
868  usb->name, request, value, index );
869  return -ETIMEDOUT;
870 
871  err_message:
872  free_iob ( iobuf );
873  err_alloc:
874  return rc;
875 }
876 
877 /**
878  * Get default language ID
879  *
880  * @v usb USB device
881  * @ret language Language ID
882  */
883 static unsigned int usb_get_default_language ( struct usb_device *usb ) {
884  struct {
886  uint16_t language[1];
887  } __attribute__ (( packed )) desc;
888  unsigned int language;
889  int rc;
890 
891  /* Get descriptor */
892  if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, 0, 0,
893  &desc.header, sizeof ( desc ) ) ) !=0){
894  DBGC ( usb, "USB %s has no default language: %s\n",
895  usb->name, strerror ( rc ) );
896  return USB_LANG_ENGLISH;
897  }
898 
899  /* Use first language ID */
900  language = le16_to_cpu ( desc.language[0] );
901  DBGC2 ( usb, "USB %s default language %#04x\n", usb->name, language );
902  return language;
903 }
904 
905 /**
906  * Get USB string descriptor
907  *
908  * @v usb USB device
909  * @v index String index
910  * @v language Language ID, or 0 to use default
911  * @v buf Data buffer
912  * @v len Length of buffer
913  * @ret len String length (excluding NUL), or negative error
914  */
915 int usb_get_string_descriptor ( struct usb_device *usb, unsigned int index,
916  unsigned int language, char *buf, size_t len ) {
917  struct {
919  uint16_t character[len];
920  } __attribute__ (( packed )) *desc;
921  unsigned int actual;
922  unsigned int i;
923  int rc;
924 
925  /* Use default language ID, if applicable */
926  if ( ( language == 0 ) && ( index != 0 ) ) {
927  if ( ! usb->language )
928  usb->language = usb_get_default_language ( usb );
929  language = usb->language;
930  }
931 
932  /* Allocate buffer for string */
933  desc = malloc ( sizeof ( *desc ) );
934  if ( ! desc ) {
935  rc = -ENOMEM;
936  goto err_alloc;
937  }
938 
939  /* Get descriptor */
940  if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, index,
941  language, &desc->header,
942  sizeof ( *desc ) ) ) != 0 )
943  goto err_get_descriptor;
944 
945  /* Calculate string length */
946  if ( desc->header.len < sizeof ( desc->header ) ) {
947  rc = -EINVAL;
948  goto err_len;
949  }
950  actual = ( ( desc->header.len - sizeof ( desc->header ) ) /
951  sizeof ( desc->character[0] ) );
952 
953  /* Copy to buffer */
954  memset ( buf, 0, len );
955  for ( i = 0 ; ( ( i < actual ) && ( i < len ) ) ; i++ )
956  buf[i] = le16_to_cpu ( desc->character[i] );
957 
958  /* Free buffer */
959  free ( desc );
960 
961  return actual;
962 
963  err_len:
964  err_get_descriptor:
965  free ( desc );
966  err_alloc:
967  return rc;
968 }
969 
970 /******************************************************************************
971  *
972  * USB device driver
973  *
974  ******************************************************************************
975  */
976 
977 /**
978  * Get USB configuration descriptor
979  *
980  * @v usb USB device
981  * @v index Configuration index
982  * @ret config Configuration descriptor
983  * @ret rc Return status code
984  *
985  * The configuration descriptor is dynamically allocated and must
986  * eventually be freed by the caller.
987  */
988 static int
989 usb_config_descriptor ( struct usb_device *usb, unsigned int index,
990  struct usb_configuration_descriptor **config ) {
991  struct usb_configuration_descriptor partial;
992  size_t len;
993  int rc;
994 
995  /* Read first part of configuration descriptor to get size */
996  if ( ( rc = usb_get_config_descriptor ( usb, index, &partial,
997  sizeof ( partial ) ) ) != 0 ) {
998  DBGC ( usb, "USB %s could not get configuration descriptor %d: "
999  "%s\n", usb->name, index, strerror ( rc ) );
1000  goto err_get_partial;
1001  }
1002  len = le16_to_cpu ( partial.len );
1003  if ( len < sizeof ( partial ) ) {
1004  DBGC ( usb, "USB %s underlength configuraton descriptor %d\n",
1005  usb->name, index );
1006  rc = -EINVAL;
1007  goto err_partial_len;
1008  }
1009 
1010  /* Allocate buffer for whole configuration descriptor */
1011  *config = malloc ( len );
1012  if ( ! *config ) {
1013  rc = -ENOMEM;
1014  goto err_alloc_config;
1015  }
1016 
1017  /* Read whole configuration descriptor */
1018  if ( ( rc = usb_get_config_descriptor ( usb, index, *config,
1019  len ) ) != 0 ) {
1020  DBGC ( usb, "USB %s could not get configuration descriptor %d: "
1021  "%s\n", usb->name, index, strerror ( rc ) );
1022  goto err_get_config_descriptor;
1023  }
1024  if ( (*config)->len != partial.len ) {
1025  DBGC ( usb, "USB %s bad configuration descriptor %d length\n",
1026  usb->name, index );
1027  rc = -EINVAL;
1028  goto err_config_len;
1029  }
1030 
1031  return 0;
1032 
1033  err_config_len:
1034  err_get_config_descriptor:
1035  free ( *config );
1036  err_alloc_config:
1037  err_partial_len:
1038  err_get_partial:
1039  return rc;
1040 }
1041 
1042 /**
1043  * Describe USB function
1044  *
1045  * @v usb USB device
1046  * @v config Configuration descriptor
1047  * @v first First interface number
1048  * @v interfaces Interface list to fill in
1049  * @v desc Function descriptor to fill in
1050  * @ret rc Return status code
1051  */
1052 static int usb_describe ( struct usb_device *usb,
1054  unsigned int first, uint8_t *interfaces,
1055  struct usb_function_descriptor *desc ) {
1056  struct usb_interface_association_descriptor *association;
1058  struct cdc_union_descriptor *cdc_union;
1059  unsigned int i;
1060 
1061  /* Fill in vendor and product ID */
1062  memset ( desc, 0, sizeof ( *desc ) );
1063  desc->vendor = le16_to_cpu ( usb->device.vendor );
1064  desc->product = le16_to_cpu ( usb->device.product );
1065 
1066  /* First, look for an interface association descriptor */
1067  association = usb_interface_association_descriptor ( config, first );
1068  if ( association ) {
1069 
1070  /* Sanity check */
1071  assert ( association->first == first );
1072  if ( ( first + association->count ) > config->interfaces ) {
1073  DBGC ( usb, "USB %s has invalid association [%d-%d)\n",
1074  usb->name, first, ( first + association->count));
1075  return -ERANGE;
1076  }
1077 
1078  /* Describe function */
1079  memcpy ( &desc->class.class, &association->class,
1080  sizeof ( desc->class.class ) );
1081  desc->count = association->count;
1082  for ( i = 0 ; i < association->count ; i++ )
1083  interfaces[i] = ( first + i );
1084  return 0;
1085  }
1086 
1087  /* Next, look for an interface descriptor */
1088  interface = usb_interface_descriptor ( config, first, 0 );
1089  if ( ! interface ) {
1090  DBGC ( usb, "USB %s has no descriptor for interface %d\n",
1091  usb->name, first );
1092  return -ENOENT;
1093  }
1094 
1095  /* Describe function */
1096  memcpy ( &desc->class.class, &interface->class,
1097  sizeof ( desc->class.class ) );
1098  desc->count = 1;
1099  interfaces[0] = first;
1100 
1101  /* Look for a CDC union descriptor, if applicable */
1102  if ( ( desc->class.class.class == USB_CLASS_CDC ) &&
1103  ( cdc_union = cdc_union_descriptor ( config, interface ) ) ) {
1104 
1105  /* Determine interface count */
1106  desc->count = ( ( cdc_union->header.len -
1107  offsetof ( typeof ( *cdc_union ),
1108  interface[0] ) ) /
1109  sizeof ( cdc_union->interface[0] ) );
1110  if ( desc->count > config->interfaces ) {
1111  DBGC ( usb, "USB %s has invalid union functional "
1112  "descriptor with %d interfaces\n",
1113  usb->name, desc->count );
1114  return -ERANGE;
1115  }
1116 
1117  /* Describe function */
1118  for ( i = 0 ; i < desc->count ; i++ ) {
1119  if ( cdc_union->interface[i] >= config->interfaces ) {
1120  DBGC ( usb, "USB %s has invalid union "
1121  "functional descriptor covering "
1122  "interface %d\n", usb->name,
1123  cdc_union->interface[i] );
1124  return -ERANGE;
1125  }
1126  interfaces[i] = cdc_union->interface[i];
1127  }
1128 
1129  return 0;
1130  }
1131 
1132  return 0;
1133 }
1134 
1135 /**
1136  * Update list of used interface
1137  *
1138  * @v usb USB device
1139  * @v count Number of interfaces
1140  * @v interface List of interfaces
1141  * @v used List of already-used interfaces
1142  * @ret rc Return status code
1143  */
1144 static int usb_used ( struct usb_device *usb, unsigned int count,
1145  uint8_t *interface, uint8_t *used ) {
1146  unsigned int i;
1147 
1148  for ( i = 0 ; i < count ; i++ ) {
1149  if ( used[interface[i]] ) {
1150  DBGC ( usb, "USB %s interface %d already in use\n",
1151  usb->name, interface[i] );
1152  return -EINVAL;
1153  }
1154  used[interface[i]] = 1;
1155  }
1156  return 0;
1157 }
1158 
1159 /**
1160  * Find USB device driver
1161  *
1162  * @v desc Function descriptor
1163  * @ret id USB device ID, or NULL
1164  * @ret driver USB device driver, or NULL
1165  */
1167  struct usb_device_id **id ) {
1168  struct usb_driver *driver;
1169  unsigned int i;
1170 
1171  /* Look for a matching driver */
1172  for_each_table_entry ( driver, USB_DRIVERS ) {
1173  for ( i = 0 ; i < driver->id_count ; i++ ) {
1174 
1175  /* Ignore non-matching driver class */
1176  if ( ( driver->class.class.scalar ^ desc->class.scalar )
1177  & driver->class.mask.scalar )
1178  continue;
1179 
1180  /* Look for a matching ID */
1181  *id = &driver->ids[i];
1182  if ( ( ( (*id)->vendor == desc->vendor ) ||
1183  ( (*id)->vendor == USB_ANY_ID ) ) &&
1184  ( ( (*id)->product == desc->product ) ||
1185  ( (*id)->product == USB_ANY_ID ) ) )
1186  return driver;
1187  }
1188  }
1189 
1190  /* Not found */
1191  *id = NULL;
1192  return NULL;
1193 }
1194 
1195 /**
1196  * Get USB device configuration score
1197  *
1198  * @v usb USB device
1199  * @v config Configuration descriptor
1200  * @ret score Device configuration score, or negative error
1201  */
1202 static int usb_score ( struct usb_device *usb,
1203  struct usb_configuration_descriptor *config ) {
1204  uint8_t used[config->interfaces];
1205  uint8_t interface[config->interfaces];
1207  struct usb_driver *driver;
1208  struct usb_device_id *id;
1209  unsigned int first;
1210  unsigned int score = 0;
1211  int rc;
1212 
1213  /* Identify each function in turn */
1214  memset ( used, 0, sizeof ( used ) );
1215  for ( first = 0 ; first < config->interfaces ; first++ ) {
1216 
1217  /* Skip interfaces already used */
1218  if ( used[first] )
1219  continue;
1220 
1221  /* Describe function */
1222  if ( ( rc = usb_describe ( usb, config, first, interface,
1223  &desc ) ) != 0 )
1224  return rc;
1225 
1226  /* Update used interfaces */
1227  if ( ( rc = usb_used ( usb, desc.count, interface,
1228  used ) ) != 0 )
1229  return rc;
1230 
1231  /* Look for a driver for this function */
1232  driver = usb_find_driver ( &desc, &id );
1233  if ( driver )
1234  score += driver->score;
1235  }
1236 
1237  return score;
1238 }
1239 
1240 /**
1241  * Probe USB device driver
1242  *
1243  * @v func USB function
1244  * @v config Configuration descriptor
1245  * @ret rc Return status code
1246  */
1247 static int usb_probe ( struct usb_function *func,
1248  struct usb_configuration_descriptor *config ) {
1249  struct usb_device *usb = func->usb;
1250  struct usb_driver *driver;
1251  struct usb_device_id *id;
1252  int rc;
1253 
1254  /* Identify driver */
1255  driver = usb_find_driver ( &func->desc, &id );
1256  if ( ! driver ) {
1257  DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d has no driver\n",
1258  func->name, func->desc.vendor, func->desc.product,
1259  func->desc.class.class.class,
1260  func->desc.class.class.subclass,
1261  func->desc.class.class.protocol );
1262  return -ENOENT;
1263  }
1264 
1265  /* Record driver */
1266  func->driver = driver;
1267  func->id = id;
1268  func->dev.driver_name = id->name;
1269 
1270  /* Probe driver */
1271  if ( ( rc = driver->probe ( func, config ) ) != 0 ) {
1272  DBGC ( usb, "USB %s failed to probe driver %s: %s\n",
1273  func->name, id->name, strerror ( rc ) );
1274  return rc;
1275  }
1276 
1277  return 0;
1278 }
1279 
1280 /**
1281  * Remove USB device driver
1282  *
1283  * @v func USB function
1284  */
1285 static void usb_remove ( struct usb_function *func ) {
1286 
1287  /* Remove driver */
1288  func->driver->remove ( func );
1289 }
1290 
1291 /**
1292  * Probe all USB device drivers
1293  *
1294  * @v usb USB device
1295  * @v config Configuration descriptor
1296  */
1297 static void
1299  struct usb_configuration_descriptor *config ) {
1300  struct usb_bus *bus = usb->port->hub->bus;
1301  struct usb_function *func;
1302  uint8_t used[config->interfaces];
1303  unsigned int first;
1304  unsigned int i;
1305  int rc;
1306 
1307  /* Identify each function in turn */
1308  memset ( used, 0, sizeof ( used ) );
1309  for ( first = 0 ; first < config->interfaces ; first++ ) {
1310 
1311  /* Skip interfaces already used */
1312  if ( used[first] )
1313  continue;
1314 
1315  /* Allocate and initialise structure */
1316  func = zalloc ( sizeof ( *func ) +
1317  ( config->interfaces *
1318  sizeof ( func->interface[0] ) ) );
1319  if ( ! func )
1320  goto err_alloc;
1321  func->name = func->dev.name;
1322  func->usb = usb;
1323  func->dev.desc.bus_type = BUS_TYPE_USB;
1324  func->dev.desc.location =
1325  USB_BUSDEV ( bus->address, usb->address );
1326  func->dev.desc.vendor = le16_to_cpu ( usb->device.vendor );
1327  func->dev.desc.device = le16_to_cpu ( usb->device.product );
1328  snprintf ( func->dev.name, sizeof ( func->dev.name ),
1329  "%s-%d.%d", usb->name, config->config, first );
1330  INIT_LIST_HEAD ( &func->dev.children );
1331  func->dev.parent = bus->dev;
1332  list_add_tail ( &func->list, &usb->functions );
1333 
1334  /* Identify function */
1335  if ( ( rc = usb_describe ( usb, config, first, func->interface,
1336  &func->desc ) ) != 0 )
1337  goto err_describe;
1338  assert ( func->desc.count <= config->interfaces );
1339 
1340  /* Mark interfaces as used */
1341  if ( ( rc = usb_used ( usb, func->desc.count, func->interface,
1342  used ) ) != 0 )
1343  goto err_used;
1344 
1345  /* Probe device driver */
1346  if ( ( rc = usb_probe ( func, config ) ) != 0 )
1347  goto err_probe;
1348  DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d interfaces ",
1349  func->name, func->desc.vendor, func->desc.product,
1350  func->desc.class.class.class,
1351  func->desc.class.class.subclass,
1352  func->desc.class.class.protocol );
1353  for ( i = 0 ; i < func->desc.count ; i++ )
1354  DBGC ( usb, "%s%d", ( i ? "," : "" ),
1355  func->interface[i] );
1356  DBGC ( usb, " using driver %s\n", func->dev.driver_name );
1357 
1358  /* Add to device hierarchy */
1359  list_add_tail ( &func->dev.siblings, &bus->dev->children );
1360 
1361  continue;
1362 
1363  list_del ( &func->dev.siblings );
1364  usb_remove ( func );
1365  err_probe:
1366  err_used:
1367  err_describe:
1368  list_del ( &func->list );
1369  free ( func );
1370  err_alloc:
1371  /* Continue registering other functions */
1372  continue;
1373  }
1374 }
1375 
1376 /**
1377  * Remove all device drivers
1378  *
1379  * @v usb USB device
1380  */
1381 static void usb_remove_all ( struct usb_device *usb ) {
1382  struct usb_function *func;
1383  struct usb_function *tmp;
1384 
1385  /* Remove all functions */
1387 
1388  /* Remove device driver */
1389  usb_remove ( func );
1390 
1391  /* Remove from device hierarchy */
1392  assert ( list_empty ( &func->dev.children ) );
1393  list_del ( &func->dev.siblings );
1394 
1395  /* Remove from list of functions */
1396  list_del ( &func->list );
1397 
1398  /* Free function */
1399  free ( func );
1400  }
1401 }
1402 
1403 /**
1404  * Clear USB device configuration
1405  *
1406  * @v usb USB device
1407  */
1408 static void usb_deconfigure ( struct usb_device *usb ) {
1409  unsigned int i;
1410 
1411  /* Remove device drivers */
1412  usb_remove_all ( usb );
1413 
1414  /* Sanity checks */
1415  for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++){
1416  if ( i != USB_ENDPOINT_IDX ( USB_EP0_ADDRESS ) )
1417  assert ( usb->ep[i] == NULL );
1418  }
1419 
1420  /* Clear device configuration */
1421  usb_set_configuration ( usb, 0 );
1422 }
1423 
1424 /**
1425  * Choose our preferred USB device configuration
1426  *
1427  * @v usb USB device
1428  * @ret rc Return status code
1429  */
1430 static int usb_autoconfigure ( struct usb_device *usb ) {
1432  unsigned int preferred = 0;
1433  unsigned int index;
1434  int score;
1435  int best = 0;
1436  int rc;
1437 
1438  /* Calculate driver score for each configuration index */
1439  for ( index = 0 ; index < usb->device.configurations ; index++ ) {
1440 
1441  /* Read configuration descriptor */
1442  if ( ( rc = usb_config_descriptor ( usb, index,
1443  &config ) ) != 0 )
1444  goto err_config;
1445 
1446  /* Get score for this configuration */
1447  score = usb_score ( usb, config );
1448  if ( score < 0 ) {
1449  rc = score;
1450  goto err_score;
1451  }
1452  DBGC2 ( usb, "USB %s configuration %d score %d\n",
1453  usb->name, config->config, score );
1454 
1455  /* Record as preferred configuration, if applicable */
1456  if ( score > best ) {
1457  best = score;
1458  preferred = index;
1459  }
1460 
1461  /* Free configuration descriptor */
1462  free ( config );
1463  config = NULL;
1464  }
1465 
1466  /* Read preferred configuration descriptor */
1467  if ( ( rc = usb_config_descriptor ( usb, preferred, &config ) ) != 0 )
1468  goto err_preferred;
1469 
1470  /* Set configuration */
1471  if ( ( rc = usb_set_configuration ( usb, config->config ) ) != 0){
1472  DBGC ( usb, "USB %s could not set configuration %d: %s\n",
1473  usb->name, config->config, strerror ( rc ) );
1474  goto err_set_configuration;
1475  }
1476 
1477  /* Probe USB device drivers */
1478  usb_probe_all ( usb, config );
1479 
1480  /* Free configuration descriptor */
1481  free ( config );
1482 
1483  return 0;
1484 
1485  usb_remove_all ( usb );
1486  usb_set_configuration ( usb, 0 );
1487  err_set_configuration:
1488  free ( config );
1489  err_preferred:
1490  return rc;
1491 
1492  err_score:
1493  free ( config );
1494  err_config:
1495  return rc;
1496 }
1497 
1498 /******************************************************************************
1499  *
1500  * USB device
1501  *
1502  ******************************************************************************
1503  */
1504 
1505 /**
1506  * Allocate USB device
1507  *
1508  * @v port USB port
1509  * @ret usb USB device, or NULL on allocation failure
1510  */
1511 static struct usb_device * alloc_usb ( struct usb_port *port ) {
1512  struct usb_hub *hub = port->hub;
1513  struct usb_bus *bus = hub->bus;
1514  struct usb_device *usb;
1515 
1516  /* Allocate and initialise structure */
1517  usb = zalloc ( sizeof ( *usb ) );
1518  if ( ! usb )
1519  return NULL;
1520  snprintf ( usb->name, sizeof ( usb->name ), "%s%c%d", hub->name,
1521  ( hub->usb ? '.' : '-' ), port->address );
1522  usb->port = port;
1523  INIT_LIST_HEAD ( &usb->functions );
1524  usb->host = &bus->op->device;
1526  INIT_LIST_HEAD ( &usb->complete );
1527 
1528  return usb;
1529 }
1530 
1531 /**
1532  * Register USB device
1533  *
1534  * @v usb USB device
1535  * @ret rc Return status code
1536  */
1537 static int register_usb ( struct usb_device *usb ) {
1538  struct usb_port *port = usb->port;
1539  struct usb_hub *hub = port->hub;
1540  struct usb_bus *bus = hub->bus;
1541  unsigned int protocol;
1542  size_t mtu;
1543  int rc;
1544 
1545  /* Add to port */
1546  if ( port->usb != NULL ) {
1547  DBGC ( hub, "USB hub %s port %d is already registered to %s\n",
1548  hub->name, port->address, port->usb->name );
1549  rc = -EALREADY;
1550  goto err_already;
1551  }
1552  port->usb = usb;
1553 
1554  /* Add to bus device list */
1555  list_add_tail ( &usb->list, &bus->devices );
1556 
1557  /* Enable device */
1558  if ( ( rc = hub->driver->enable ( hub, port ) ) != 0 ) {
1559  DBGC ( hub, "USB hub %s port %d could not enable: %s\n",
1560  hub->name, port->address, strerror ( rc ) );
1561  goto err_enable;
1562  }
1563 
1564  /* Allow recovery interval since port may have been reset */
1566 
1567  /* Get device speed */
1568  if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
1569  DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
1570  hub->name, port->address, strerror ( rc ) );
1571  goto err_speed;
1572  }
1573  usb->speed = port->speed;
1574  DBGC2 ( usb, "USB %s attached as %s-speed device\n",
1575  usb->name, usb_speed_name ( usb->speed ) );
1576 
1577  /* Open device */
1578  if ( ( rc = usb->host->open ( usb ) ) != 0 ) {
1579  DBGC ( usb, "USB %s could not open: %s\n",
1580  usb->name, strerror ( rc ) );
1581  goto err_open;
1582  }
1583 
1584  /* Describe control endpoint */
1585  mtu = USB_EP0_DEFAULT_MTU ( usb->speed );
1588  USB_EP0_INTERVAL );
1589 
1590  /* Open control endpoint */
1591  if ( ( rc = usb_endpoint_open ( &usb->control ) ) != 0 )
1592  goto err_open_control;
1593  assert ( usb_endpoint ( usb, USB_EP0_ADDRESS ) == &usb->control );
1594 
1595  /* Assign device address */
1596  if ( ( rc = usb->host->address ( usb ) ) != 0 ) {
1597  DBGC ( usb, "USB %s could not set address: %s\n",
1598  usb->name, strerror ( rc ) );
1599  goto err_address;
1600  }
1601  DBGC2 ( usb, "USB %s assigned address %d\n", usb->name, usb->address );
1602 
1603  /* Allow recovery interval after Set Address command */
1605 
1606  /* Read first part of device descriptor to get EP0 MTU */
1607  if ( ( rc = usb_get_mtu ( usb, &usb->device ) ) != 0 ) {
1608  DBGC ( usb, "USB %s could not get MTU: %s\n",
1609  usb->name, strerror ( rc ) );
1610  goto err_get_mtu;
1611  }
1612 
1613  /* Calculate EP0 MTU */
1614  protocol = le16_to_cpu ( usb->device.protocol );
1615  mtu = ( ( protocol < USB_PROTO_3_0 ) ?
1616  usb->device.mtu : ( 1 << usb->device.mtu ) );
1617  DBGC2 ( usb, "USB %s has control MTU %zd (guessed %zd)\n",
1618  usb->name, mtu, usb->control.mtu );
1619 
1620  /* Update MTU */
1621  if ( ( rc = usb_endpoint_mtu ( &usb->control, mtu ) ) != 0 )
1622  goto err_mtu;
1623 
1624  /* Read whole device descriptor */
1625  if ( ( rc = usb_get_device_descriptor ( usb, &usb->device ) ) != 0 ) {
1626  DBGC ( usb, "USB %s could not get device descriptor: %s\n",
1627  usb->name, strerror ( rc ) );
1628  goto err_get_device_descriptor;
1629  }
1630  DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d (v%s, %s-speed, "
1631  "MTU %zd)\n", usb->name, usb->address,
1632  le16_to_cpu ( usb->device.vendor ),
1633  le16_to_cpu ( usb->device.product ), usb->device.class.class,
1635  usb_bcd ( le16_to_cpu ( usb->device.protocol ) ),
1636  usb_speed_name ( usb->speed ), usb->control.mtu );
1637 
1638  /* Configure device */
1639  if ( ( rc = usb_autoconfigure ( usb ) ) != 0 )
1640  goto err_autoconfigure;
1641 
1642  return 0;
1643 
1644  usb_deconfigure ( usb );
1645  err_autoconfigure:
1646  err_get_device_descriptor:
1647  err_mtu:
1648  err_get_mtu:
1649  err_address:
1650  usb_endpoint_close ( &usb->control );
1651  err_open_control:
1652  usb->host->close ( usb );
1653  err_open:
1654  err_speed:
1655  /* Leave port enabled on failure, to avoid an endless loop of
1656  * failed device registrations.
1657  */
1658  err_enable:
1659  list_del ( &usb->list );
1660  port->usb = NULL;
1661  err_already:
1662  return rc;
1663 }
1664 
1665 /**
1666  * Unregister USB device
1667  *
1668  * @v usb USB device
1669  */
1670 static void unregister_usb ( struct usb_device *usb ) {
1671  struct usb_port *port = usb->port;
1672  struct usb_hub *hub = port->hub;
1673  struct io_buffer *iobuf;
1674  struct io_buffer *tmp;
1675 
1676  DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d removed\n",
1677  usb->name, usb->address, le16_to_cpu ( usb->device.vendor ),
1678  le16_to_cpu ( usb->device.product ), usb->device.class.class,
1679  usb->device.class.subclass, usb->device.class.protocol );
1680 
1681  /* Sanity checks */
1682  assert ( port->usb == usb );
1683 
1684  /* Clear device configuration */
1685  usb_deconfigure ( usb );
1686 
1687  /* Close control endpoint */
1688  usb_endpoint_close ( &usb->control );
1689 
1690  /* Discard any stale control completions */
1691  list_for_each_entry_safe ( iobuf, tmp, &usb->complete, list ) {
1692  list_del ( &iobuf->list );
1693  free_iob ( iobuf );
1694  }
1695 
1696  /* Close device */
1697  usb->host->close ( usb );
1698 
1699  /* Disable port */
1700  hub->driver->disable ( hub, port );
1701 
1702  /* Remove from bus device list */
1703  list_del ( &usb->list );
1704 
1705  /* Remove from port */
1706  port->usb = NULL;
1707 }
1708 
1709 /**
1710  * Free USB device
1711  *
1712  * @v usb USB device
1713  */
1714 static void free_usb ( struct usb_device *usb ) {
1715  unsigned int i;
1716 
1717  /* Sanity checks */
1718  for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++ )
1719  assert ( usb->ep[i] == NULL );
1720  assert ( list_empty ( &usb->functions ) );
1721  assert ( list_empty ( &usb->complete ) );
1722 
1723  /* Free device */
1724  free ( usb );
1725 }
1726 
1727 /**
1728  * Find USB device by address
1729  *
1730  * @v bus USB bus
1731  * @v address Device address
1732  * @ret usb USB device, or NULL if not found
1733  */
1734 struct usb_device * find_usb ( struct usb_bus *bus, unsigned int address ) {
1735  struct usb_device *usb;
1736 
1737  /* Search for a matching non-zero address */
1738  list_for_each_entry ( usb, &bus->devices, list ) {
1739  if ( address && ( usb->address == address ) )
1740  return usb;
1741  }
1742 
1743  return NULL;
1744 }
1745 
1746 /******************************************************************************
1747  *
1748  * USB device hotplug event handling
1749  *
1750  ******************************************************************************
1751  */
1752 
1753 /**
1754  * Handle newly attached USB device
1755  *
1756  * @v port USB port
1757  * @ret rc Return status code
1758  */
1759 static int usb_attached ( struct usb_port *port ) {
1760  struct usb_device *usb;
1761  int rc;
1762 
1763  /* Mark port as attached */
1764  port->attached = 1;
1765 
1766  /* Sanity checks */
1767  assert ( port->usb == NULL );
1768 
1769  /* Allocate USB device */
1770  usb = alloc_usb ( port );
1771  if ( ! usb ) {
1772  rc = -ENOMEM;
1773  goto err_alloc;
1774  }
1775 
1776  /* Register USB device */
1777  if ( ( rc = register_usb ( usb ) ) != 0 )
1778  goto err_register;
1779 
1780  return 0;
1781 
1782  unregister_usb ( usb );
1783  err_register:
1784  free_usb ( usb );
1785  err_alloc:
1786  return rc;
1787 }
1788 
1789 /**
1790  * Handle newly detached USB device
1791  *
1792  * @v port USB port
1793  */
1794 static void usb_detached ( struct usb_port *port ) {
1795  struct usb_device *usb = port->usb;
1796 
1797  /* Mark port as detached */
1798  port->attached = 0;
1799 
1800  /* Do nothing if we have no USB device */
1801  if ( ! usb )
1802  return;
1803 
1804  /* Unregister USB device */
1805  unregister_usb ( usb );
1806 
1807  /* Free USB device */
1808  free_usb ( usb );
1809 }
1810 
1811 /**
1812  * Handle newly attached or detached USB device
1813  *
1814  * @v port USB port
1815  * @ret rc Return status code
1816  */
1817 static int usb_hotplugged ( struct usb_port *port ) {
1818  struct usb_hub *hub = port->hub;
1819  int rc;
1820 
1821  /* Get current port speed */
1822  if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
1823  DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
1824  hub->name, port->address, strerror ( rc ) );
1825  /* Treat as a disconnection */
1826  port->disconnected = 1;
1827  port->speed = USB_SPEED_NONE;
1828  }
1829 
1830  /* Detach device, if applicable */
1831  if ( port->attached && ( port->disconnected || ! port->speed ) )
1832  usb_detached ( port );
1833 
1834  /* Clear any recorded disconnections */
1835  port->disconnected = 0;
1836 
1837  /* Attach device, if applicable */
1838  if ( port->speed && ( ! port->attached ) &&
1839  ( ( rc = usb_attached ( port ) ) != 0 ) )
1840  return rc;
1841 
1842  return 0;
1843 }
1844 
1845 /******************************************************************************
1846  *
1847  * USB process
1848  *
1849  ******************************************************************************
1850  */
1851 
1852 /**
1853  * Report port status change
1854  *
1855  * @v port USB port
1856  */
1857 void usb_port_changed ( struct usb_port *port ) {
1858 
1859  /* Record hub port status change */
1860  list_del ( &port->changed );
1861  list_add_tail ( &port->changed, &usb_changed );
1862 }
1863 
1864 /**
1865  * Handle newly attached or detached USB device
1866  *
1867  */
1868 static void usb_hotplug ( void ) {
1869  struct usb_port *port;
1870 
1871  /* Handle any changed ports, allowing for the fact that the
1872  * port list may change as we perform hotplug actions.
1873  */
1874  while ( ! list_empty ( &usb_changed ) ) {
1875 
1876  /* Get first changed port */
1878  changed );
1879  assert ( port != NULL );
1880 
1881  /* Remove from list of changed ports */
1882  list_del ( &port->changed );
1883  INIT_LIST_HEAD ( &port->changed );
1884 
1885  /* Perform appropriate hotplug action */
1886  usb_hotplugged ( port );
1887  }
1888 }
1889 
1890 /**
1891  * USB process
1892  *
1893  * @v process USB process
1894  */
1895 static void usb_step ( struct process *process __unused ) {
1896  struct usb_bus *bus;
1897  struct usb_endpoint *ep;
1898 
1899  /* Poll all buses */
1900  for_each_usb_bus ( bus )
1901  usb_poll ( bus );
1902 
1903  /* Attempt to reset first halted endpoint in list, if any. We
1904  * do not attempt to process the complete list, since this
1905  * would require extra code to allow for the facts that the
1906  * halted endpoint list may change as we do so, and that
1907  * resetting an endpoint may fail.
1908  */
1909  if ( ( ep = list_first_entry ( &usb_halted, struct usb_endpoint,
1910  halted ) ) != NULL )
1911  usb_endpoint_reset ( ep );
1912 
1913  /* Handle any changed ports */
1914  usb_hotplug();
1915 }
1916 
1917 /** USB process */
1918 PERMANENT_PROCESS ( usb_process, usb_step );
1919 
1920 /******************************************************************************
1921  *
1922  * USB hub
1923  *
1924  ******************************************************************************
1925  */
1926 
1927 /**
1928  * Allocate USB hub
1929  *
1930  * @v bus USB bus
1931  * @v usb Underlying USB device, if any
1932  * @v ports Number of ports
1933  * @v driver Hub driver operations
1934  * @ret hub USB hub, or NULL on allocation failure
1935  */
1936 struct usb_hub * alloc_usb_hub ( struct usb_bus *bus, struct usb_device *usb,
1937  unsigned int ports,
1938  struct usb_hub_driver_operations *driver ) {
1939  struct usb_hub *hub;
1940  struct usb_port *port;
1941  unsigned int i;
1942 
1943  /* Allocate and initialise structure */
1944  hub = zalloc ( sizeof ( *hub ) + ( ports * sizeof ( hub->port[0] ) ) );
1945  if ( ! hub )
1946  return NULL;
1947  hub->name = ( usb ? usb->name : bus->name );
1948  hub->bus = bus;
1949  hub->usb = usb;
1950  if ( usb )
1951  hub->protocol = usb->port->protocol;
1952  hub->ports = ports;
1953  hub->driver = driver;
1954  hub->host = &bus->op->hub;
1955 
1956  /* Initialise port list */
1957  for ( i = 1 ; i <= hub->ports ; i++ ) {
1958  port = usb_port ( hub, i );
1959  port->hub = hub;
1960  port->address = i;
1961  if ( usb )
1962  port->protocol = usb->port->protocol;
1963  INIT_LIST_HEAD ( &port->changed );
1964  }
1965 
1966  return hub;
1967 }
1968 
1969 /**
1970  * Register USB hub
1971  *
1972  * @v hub USB hub
1973  * @ret rc Return status code
1974  */
1975 int register_usb_hub ( struct usb_hub *hub ) {
1976  struct usb_bus *bus = hub->bus;
1977  struct usb_port *port;
1978  unsigned int i;
1979  int rc;
1980 
1981  /* Add to hub list */
1982  list_add_tail ( &hub->list, &bus->hubs );
1983 
1984  /* Open hub (host controller) */
1985  if ( ( rc = hub->host->open ( hub ) ) != 0 ) {
1986  DBGC ( hub, "USB hub %s could not open: %s\n",
1987  hub->name, strerror ( rc ) );
1988  goto err_host_open;
1989  }
1990 
1991  /* Open hub (driver) */
1992  if ( ( rc = hub->driver->open ( hub ) ) != 0 ) {
1993  DBGC ( hub, "USB hub %s could not open: %s\n",
1994  hub->name, strerror ( rc ) );
1995  goto err_driver_open;
1996  }
1997 
1998  /* Delay to allow ports to stabilise */
2000 
2001  /* Mark all ports as changed */
2002  for ( i = 1 ; i <= hub->ports ; i++ ) {
2003  port = usb_port ( hub, i );
2004  usb_port_changed ( port );
2005  }
2006 
2007  /* Some hubs seem to defer reporting device connections until
2008  * their interrupt endpoint is polled for the first time.
2009  * Poll the bus once now in order to pick up any such
2010  * connections.
2011  */
2012  usb_poll ( bus );
2013 
2014  return 0;
2015 
2016  hub->driver->close ( hub );
2017  err_driver_open:
2018  hub->host->close ( hub );
2019  err_host_open:
2020  list_del ( &hub->list );
2021  return rc;
2022 }
2023 
2024 /**
2025  * Unregister USB hub
2026  *
2027  * @v hub USB hub
2028  */
2029 void unregister_usb_hub ( struct usb_hub *hub ) {
2030  struct usb_port *port;
2031  unsigned int i;
2032 
2033  /* Detach all devices */
2034  for ( i = 1 ; i <= hub->ports ; i++ ) {
2035  port = usb_port ( hub, i );
2036  if ( port->attached )
2037  usb_detached ( port );
2038  }
2039 
2040  /* Close hub (driver) */
2041  hub->driver->close ( hub );
2042 
2043  /* Close hub (host controller) */
2044  hub->host->close ( hub );
2045 
2046  /* Cancel any pending port status changes */
2047  for ( i = 1 ; i <= hub->ports ; i++ ) {
2048  port = usb_port ( hub, i );
2049  list_del ( &port->changed );
2050  INIT_LIST_HEAD ( &port->changed );
2051  }
2052 
2053  /* Remove from hub list */
2054  list_del ( &hub->list );
2055 }
2056 
2057 /**
2058  * Free USB hub
2059  *
2060  * @v hub USB hub
2061  */
2062 void free_usb_hub ( struct usb_hub *hub ) {
2063  struct usb_port *port;
2064  unsigned int i;
2065 
2066  /* Sanity checks */
2067  for ( i = 1 ; i <= hub->ports ; i++ ) {
2068  port = usb_port ( hub, i );
2069  assert ( ! port->attached );
2070  assert ( port->usb == NULL );
2071  assert ( list_empty ( &port->changed ) );
2072  }
2073 
2074  /* Free hub */
2075  free ( hub );
2076 }
2077 
2078 /******************************************************************************
2079  *
2080  * USB bus
2081  *
2082  ******************************************************************************
2083  */
2084 
2085 /**
2086  * Allocate USB bus
2087  *
2088  * @v dev Underlying hardware device
2089  * @v ports Number of root hub ports
2090  * @v mtu Largest transfer allowed on the bus
2091  * @v op Host controller operations
2092  * @ret bus USB bus, or NULL on allocation failure
2093  */
2094 struct usb_bus * alloc_usb_bus ( struct device *dev, unsigned int ports,
2095  size_t mtu, struct usb_host_operations *op ) {
2096  struct usb_bus *bus;
2097 
2098  /* Allocate and initialise structure */
2099  bus = zalloc ( sizeof ( *bus ) );
2100  if ( ! bus )
2101  goto err_alloc_bus;
2102  bus->name = dev->name;
2103  bus->dev = dev;
2104  bus->mtu = mtu;
2105  bus->op = op;
2106  INIT_LIST_HEAD ( &bus->devices );
2107  INIT_LIST_HEAD ( &bus->hubs );
2108  bus->host = &bus->op->bus;
2109 
2110  /* Allocate root hub */
2111  bus->hub = alloc_usb_hub ( bus, NULL, ports, &op->root );
2112  if ( ! bus->hub )
2113  goto err_alloc_hub;
2114 
2115  return bus;
2116 
2117  free_usb_hub ( bus->hub );
2118  err_alloc_hub:
2119  free ( bus );
2120  err_alloc_bus:
2121  return NULL;
2122 }
2123 
2124 /**
2125  * Register USB bus
2126  *
2127  * @v bus USB bus
2128  * @ret rc Return status code
2129  */
2130 int register_usb_bus ( struct usb_bus *bus ) {
2131  int rc;
2132 
2133  /* Sanity checks */
2134  assert ( bus->hub != NULL );
2135 
2136  /* Assign the first available bus address */
2137  bus->address = 0;
2138  while ( find_usb_bus ( bus->address ) != NULL )
2139  bus->address++;
2140 
2141  /* Open bus */
2142  if ( ( rc = bus->host->open ( bus ) ) != 0 )
2143  goto err_open;
2144 
2145  /* Add to list of USB buses */
2146  list_add_tail ( &bus->list, &usb_buses );
2147 
2148  /* Register root hub */
2149  if ( ( rc = register_usb_hub ( bus->hub ) ) != 0 )
2150  goto err_register_hub;
2151 
2152  /* Attach any devices already present */
2153  usb_hotplug();
2154 
2155  return 0;
2156 
2157  unregister_usb_hub ( bus->hub );
2158  err_register_hub:
2159  list_del ( &bus->list );
2160  bus->host->close ( bus );
2161  err_open:
2162  return rc;
2163 }
2164 
2165 /**
2166  * Unregister USB bus
2167  *
2168  * @v bus USB bus
2169  */
2170 void unregister_usb_bus ( struct usb_bus *bus ) {
2171 
2172  /* Sanity checks */
2173  assert ( bus->hub != NULL );
2174 
2175  /* Unregister root hub */
2176  unregister_usb_hub ( bus->hub );
2177 
2178  /* Remove from list of USB buses */
2179  list_del ( &bus->list );
2180 
2181  /* Close bus */
2182  bus->host->close ( bus );
2183 
2184  /* Sanity checks */
2185  assert ( list_empty ( &bus->devices ) );
2186  assert ( list_empty ( &bus->hubs ) );
2187 }
2188 
2189 /**
2190  * Free USB bus
2191  *
2192  * @v bus USB bus
2193  */
2194 void free_usb_bus ( struct usb_bus *bus ) {
2195  struct usb_endpoint *ep;
2196  struct usb_port *port;
2197 
2198  /* Sanity checks */
2199  assert ( list_empty ( &bus->devices ) );
2200  assert ( list_empty ( &bus->hubs ) );
2201  list_for_each_entry ( ep, &usb_halted, halted )
2202  assert ( ep->usb->port->hub->bus != bus );
2204  assert ( port->hub->bus != bus );
2205 
2206  /* Free root hub */
2207  free_usb_hub ( bus->hub );
2208 
2209  /* Free bus */
2210  free ( bus );
2211 }
2212 
2213 /**
2214  * Find USB bus by address
2215  *
2216  * @v address Bus address
2217  * @ret bus USB bus, or NULL
2218  */
2219 struct usb_bus * find_usb_bus ( unsigned int address ) {
2220  struct usb_bus *bus;
2221 
2222  for_each_usb_bus ( bus ) {
2223  if ( bus->address == address )
2224  return bus;
2225  }
2226 
2227  return NULL;
2228 }
2229 
2230 /**
2231  * Find USB bus by device location
2232  *
2233  * @v bus_type Bus type
2234  * @v location Bus location
2235  * @ret bus USB bus, or NULL
2236  */
2237 struct usb_bus * find_usb_bus_by_location ( unsigned int bus_type,
2238  unsigned int location ) {
2239  struct usb_bus *bus;
2240 
2241  for_each_usb_bus ( bus ) {
2242  if ( ( bus->dev->desc.bus_type == bus_type ) &&
2243  ( bus->dev->desc.location == location ) )
2244  return bus;
2245  }
2246 
2247  return NULL;
2248 }
2249 
2250 /******************************************************************************
2251  *
2252  * USB device addressing
2253  *
2254  ******************************************************************************
2255  */
2256 
2257 /**
2258  * Allocate device address
2259  *
2260  * @v bus USB bus
2261  * @ret address Device address, or negative error
2262  */
2263 int usb_alloc_address ( struct usb_bus *bus ) {
2264  unsigned int address;
2265 
2266  /* Find first free device address */
2267  address = ffsll ( ~bus->addresses );
2268  if ( ! address )
2269  return -ENOENT;
2270 
2271  /* Mark address as used */
2272  bus->addresses |= ( 1ULL << ( address - 1 ) );
2273 
2274  return address;
2275 }
2276 
2277 /**
2278  * Free device address
2279  *
2280  * @v bus USB bus
2281  * @v address Device address
2282  */
2283 void usb_free_address ( struct usb_bus *bus, unsigned int address ) {
2284 
2285  /* Sanity check */
2286  assert ( address > 0 );
2287  assert ( bus->addresses & ( 1ULL << ( address - 1 ) ) );
2288 
2289  /* Mark address as free */
2290  bus->addresses &= ~( 1ULL << ( address - 1 ) );
2291 }
2292 
2293 /**
2294  * Find next USB device
2295  *
2296  * @v usb USB device to fill in
2297  * @v busdev Starting bus:dev address
2298  * @ret busdev Bus:dev address of next USB device
2299  * @ret rc Return status code
2300  */
2301 int usb_find_next ( struct usb_device **usb, uint16_t *busdev ) {
2302  struct usb_bus *bus;
2303 
2304  do {
2305  /* Find USB bus, if any */
2306  bus = find_usb_bus ( USB_BUS ( *busdev ) );
2307  if ( ! bus ) {
2308  *busdev |= ( USB_BUS ( 1 ) - 1 );
2309  continue;
2310  }
2311 
2312  /* Find USB device, if any */
2313  *usb = find_usb ( bus, USB_DEV ( *busdev ) );
2314  if ( *usb )
2315  return 0;
2316 
2317  } while ( ++(*busdev) );
2318 
2319  return -ENODEV;
2320 }
2321 
2322 /******************************************************************************
2323  *
2324  * USB bus topology
2325  *
2326  ******************************************************************************
2327  */
2328 
2329 /**
2330  * Get USB route string
2331  *
2332  * @v usb USB device
2333  * @ret route USB route string
2334  */
2335 unsigned int usb_route_string ( struct usb_device *usb ) {
2336  struct usb_device *parent;
2337  unsigned int route;
2338 
2339  /* Navigate up to root hub, constructing route string as we go */
2340  for ( route = 0 ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
2341  route <<= 4;
2342  route |= ( ( usb->port->address > 0xf ) ?
2343  0xf : usb->port->address );
2344  }
2345 
2346  return route;
2347 }
2348 
2349 /**
2350  * Get USB root hub port
2351  *
2352  * @v usb USB device
2353  * @ret port Root hub port
2354  */
2356  struct usb_device *parent;
2357 
2358  /* Navigate up to root hub */
2359  while ( ( parent = usb->port->hub->usb ) )
2360  usb = parent;
2361 
2362  return usb->port;
2363 }
2364 
2365 /**
2366  * Get USB transaction translator
2367  *
2368  * @v usb USB device
2369  * @ret port Transaction translator port, or NULL
2370  */
2372  struct usb_device *parent;
2373 
2374  /* Navigate up to root hub. If we find a low-speed or
2375  * full-speed device with a higher-speed parent hub, then that
2376  * device's port is the transaction translator.
2377  */
2378  for ( ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
2379  if ( ( usb->speed <= USB_SPEED_FULL ) &&
2380  ( parent->speed > USB_SPEED_FULL ) )
2381  return usb->port;
2382  }
2383 
2384  return NULL;
2385 }
2386 
2387 /* Drag in objects via register_usb_bus() */
2389 
2390 /* Drag in USB configuration */
2391 REQUIRE_OBJECT ( config_usb );
2392 
2393 /* Drag in hub driver */
2394 REQUIRE_OBJECT ( usbhub );
A process.
Definition: process.h:17
#define iob_pull(iobuf, len)
Definition: iobuf.h:102
A USB driver.
Definition: usb.h:1406
#define __attribute__(x)
Definition: compiler.h:10
#define USB_BUS(busdev)
Extract USB bus address.
Definition: usb.h:66
#define EINVAL
Invalid argument.
Definition: errno.h:428
#define USB_SPEED_EXPONENT(speed)
Extract USB speed exponent.
Definition: usb.h:41
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned short uint16_t
Definition: stdint.h:11
void(* close)(struct usb_endpoint *ep)
Close endpoint.
Definition: usb.h:454
A USB device ID.
Definition: usb.h:1360
uint32_t low
Low 16 bits of address.
Definition: myson.h:19
static void usb_hotplug(void)
Handle newly attached or detached USB device.
Definition: usb.c:1868
#define iob_put(iobuf, len)
Definition: iobuf.h:120
void(* complete)(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete transfer.
Definition: usb.h:495
static void usb_detached(struct usb_port *port)
Handle newly detached USB device.
Definition: usb.c:1794
uint8_t class
Class code.
Definition: usb.h:161
static unsigned int usb_get_default_language(struct usb_device *usb)
Get default language ID.
Definition: usb.c:883
static int usb_set_configuration(struct usb_device *usb, unsigned int index)
Set USB configuration.
Definition: usb.h:1241
static void usb_endpoint_describe(struct usb_endpoint *ep, unsigned int address, unsigned int attributes, size_t mtu, unsigned int burst, unsigned int interval)
Describe USB endpoint.
Definition: usb.h:557
uint8_t len
Length of descriptor.
Definition: usb.h:174
#define max(x, y)
Definition: ath.h:39
uint8_t interface[0]
List of interface numbers.
Definition: usb.h:696
const char * name
Name.
Definition: usb.h:675
A USB hub.
Definition: usb.h:840
struct usb_bus * find_usb_bus(unsigned int address)
Find USB bus by address.
Definition: usb.c:2219
int(* mtu)(struct usb_endpoint *ep)
Update MTU.
Definition: usb.h:467
unsigned int count
Number of interfaces.
Definition: usb.h:664
#define USB_STRING_DESCRIPTOR
A USB string descriptor.
Definition: usb.h:238
uint16_t vendor
Vendor ID.
Definition: usb.h:190
#define list_add(new, head)
Add a new entry to the head of a list.
Definition: list.h:69
A USB interface association descriptor.
Definition: usb.h:328
struct usb_bus * find_usb_bus_by_location(unsigned int bus_type, unsigned int location)
Find USB bus by device location.
Definition: usb.c:2237
struct usb_hub_host_operations * host
Host controller operations.
Definition: usb.h:856
int usb_refill_limit(struct usb_endpoint *ep, unsigned int max)
Refill endpoint up to specified limit.
Definition: usb.c:660
#define USB_EP0_DEFAULT_MTU(speed)
Calculate default MTU based on device speed.
Definition: usb.h:510
void unregister_usb_hub(struct usb_hub *hub)
Unregister USB hub.
Definition: usb.c:2029
uint32_t first
First block in range.
Definition: pccrr.h:14
struct usb_bus * alloc_usb_bus(struct device *dev, unsigned int ports, size_t mtu, struct usb_host_operations *op)
Allocate USB bus.
Definition: usb.c:2094
Error codes.
Low speed (1.5Mbps)
Definition: usb.h:48
struct list_head recycled
Recycled I/O buffer list.
Definition: usb.h:433
#define USB_RESET_RECOVER_DELAY_MS
Reset recovery time.
Definition: usb.h:1334
#define USB_ENDPOINT_ATTR_TYPE_MASK
Endpoint attribute transfer type mask.
Definition: usb.h:280
static int usb_attached(struct usb_port *port)
Handle newly attached USB device.
Definition: usb.c:1759
#define iob_push(iobuf, len)
Definition: iobuf.h:84
struct usb_descriptor_header header
Descriptor header.
Definition: usb.h:315
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
uint64_t address
Base address.
Definition: ena.h:24
uint32_t type
Operating system type.
Definition: ena.h:12
union usb_class_descriptor mask
Class mask.
Definition: usb.h:1379
static struct usb_descriptor_header * usb_next_descriptor(struct usb_descriptor_header *desc)
Get next USB descriptor.
Definition: usb.h:357
size_t len
Refill buffer payload length.
Definition: usb.h:437
uint64_t desc
Microcode descriptor list physical address.
Definition: ucode.h:12
static const char * usb_bcd(uint16_t bcd)
Transcribe USB BCD-coded value (for debugging)
Definition: usb.c:95
static int usb_get_config_descriptor(struct usb_device *usb, unsigned int index, struct usb_configuration_descriptor *data, size_t len)
Get USB configuration descriptor.
Definition: usb.h:1225
#define DBGC(...)
Definition: compiler.h:505
struct usb_device * find_usb(struct usb_bus *bus, unsigned int address)
Find USB device by address.
Definition: usb.c:1734
uint16_t vendor
Vendor ID.
Definition: usb.h:658
char name[40]
Name.
Definition: device.h:75
#define USB_ENDPOINT_MAX
Maximum endpoint number.
Definition: usb.h:521
struct usb_driver * driver
Driver.
Definition: usb.h:686
long index
Definition: bigint.h:62
#define ENOENT
No such file or directory.
Definition: errno.h:514
Standard Interface Descriptor USB 2.0 spec, Section 9.6.5.
Definition: Usb.h:157
int usb_endpoint_clear_halt(struct usb_endpoint *ep)
Clear endpoint halt (if applicable)
Definition: usb.c:371
int usb_stream(struct usb_endpoint *ep, struct io_buffer *iobuf, int terminate)
Enqueue USB stream transfer.
Definition: usb.c:545
struct usb_device * usb
Currently attached device (if in use)
Definition: usb.h:834
#define USB_SPEED_MANTISSA(speed)
Extract USB speed mantissa.
Definition: usb.h:38
int usb_prefill(struct usb_endpoint *ep)
Prefill endpoint recycled buffer list.
Definition: usb.c:619
int usb_endpoint_open(struct usb_endpoint *ep)
Open USB endpoint.
Definition: usb.c:293
#define for_each_interface_descriptor(desc, config, interface)
Iterate over all configuration descriptors within an interface descriptor.
Definition: usb.h:393
uint8_t interfaces
Number of interfaces.
Definition: usb.h:215
#define for_each_config_descriptor(desc, config)
Iterate over all configuration descriptors.
Definition: usb.h:385
unsigned int protocol
Port protocol.
Definition: usb.h:818
int(* reset)(struct usb_endpoint *ep)
Reset endpoint.
Definition: usb.h:461
uint8_t direction
Direction.
Definition: ena.h:14
#define USB_ENDPOINT_ATTR_PERIODIC
Endpoint periodic type.
Definition: usb.h:283
static int usb_used(struct usb_device *usb, unsigned int count, uint8_t *interface, uint8_t *used)
Update list of used interface.
Definition: usb.c:1144
static void usb_control_complete(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete USB control transfer.
Definition: usb.c:754
#define USB_ENDPOINT_COMPANION_DESCRIPTOR
A USB endpoint companion descriptor.
Definition: usb.h:325
static int usb_endpoint_mtu(struct usb_endpoint *ep, size_t mtu)
Update endpoint MTU.
Definition: usb.c:463
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
struct list_head changed
List of changed ports.
Definition: usb.h:836
int(* clear_tt)(struct usb_hub *hub, struct usb_port *port, struct usb_endpoint *ep)
Clear transaction translator buffer.
Definition: usb.h:925
Super speed (5Gbps)
Definition: usb.h:54
void usb_endpoint_close(struct usb_endpoint *ep)
Close USB endpoint.
Definition: usb.c:399
int open
Endpoint is open.
Definition: usb.h:418
int usb_endpoint_described(struct usb_endpoint *ep, struct usb_configuration_descriptor *config, struct usb_interface_descriptor *interface, unsigned int type, unsigned int index)
Describe USB endpoint from device configuration.
Definition: usb.c:241
int rc
Completion status.
Definition: usb.c:744
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:129
union usb_class_descriptor class
Class.
Definition: usb.h:662
unsigned int vendor
Vendor ID.
Definition: device.h:31
struct device * parent
Bus device.
Definition: device.h:85
USB hub driver operations.
Definition: usb.h:885
int usb_refill(struct usb_endpoint *ep)
Refill endpoint.
Definition: usb.c:710
int usb_control(struct usb_device *usb, unsigned int request, unsigned int value, unsigned int index, void *data, size_t len)
Issue USB control transaction.
Definition: usb.c:783
static void unregister_usb(struct usb_device *usb)
Unregister USB device.
Definition: usb.c:1670
unsigned int address
Device address, if assigned.
Definition: usb.h:732
static int usb_autoconfigure(struct usb_device *usb)
Choose our preferred USB device configuration.
Definition: usb.c:1430
unsigned int speed
Device speed.
Definition: usb.h:728
int(* probe)(struct usb_function *func, struct usb_configuration_descriptor *config)
Probe device.
Definition: usb.h:1426
void usb_port_changed(struct usb_port *port)
Report port status change.
Definition: usb.c:1857
A doubly-linked list entry (or list head)
Definition: list.h:18
int(* open)(struct usb_hub *hub)
Open hub.
Definition: usb.h:876
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
unsigned long tmp
Definition: linux_pci.h:63
#define USB_BUSDEV(bus, dev)
Define a USB bus:device address.
Definition: usb.h:63
union usb_class_descriptor class
Class.
Definition: usb.h:1377
struct usb_device_id * id
Driver device ID.
Definition: usb.h:690
#define list_first_entry(list, type, member)
Get the container of the first entry in a list.
Definition: list.h:333
#define for_each_usb_bus(bus)
Iterate over all USB buses.
Definition: usb.h:1076
#define USB_EP0_ATTRIBUTES
Control endpoint attributes.
Definition: usb.h:503
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
uint16_t product
Product ID.
Definition: usb.h:660
A USB port.
Definition: usb.h:812
uint16_t product
Product ID.
Definition: usb.h:192
#define ENOMEM
Not enough space.
Definition: errno.h:534
A hardware device.
Definition: device.h:73
A USB endpoint.
Definition: usb.h:403
struct usb_device * usb
Underlying USB device, if any.
Definition: usb.h:846
int usb_find_next(struct usb_device **usb, uint16_t *busdev)
Find next USB device.
Definition: usb.c:2301
#define USB_EP0_BURST
Control endpoint maximum burst size.
Definition: usb.h:515
void * memcpy(void *dest, const void *src, size_t len) __nonnull
#define USB_RECIP_ENDPOINT
Request recipient is an endpoint.
Definition: usb.h:118
u8 port
Port number.
Definition: CIB_PRM.h:31
int usb_message(struct usb_endpoint *ep, unsigned int request, unsigned int value, unsigned int index, struct io_buffer *iobuf)
Enqueue USB message transfer.
Definition: usb.c:491
A USB interface descriptor.
Definition: usb.h:244
A USB function descriptor.
Definition: usb.h:656
PERMANENT_PROCESS(usb_process, usb_step)
USB process.
void unregister_usb_bus(struct usb_bus *bus)
Unregister USB bus.
Definition: usb.c:2170
struct usb_port * port
USB port.
Definition: usb.h:726
static void usb_probe_all(struct usb_device *usb, struct usb_configuration_descriptor *config)
Probe all USB device drivers.
Definition: usb.c:1298
Assertions.
struct usb_driver * usb_find_driver(struct usb_function_descriptor *desc, struct usb_device_id **id)
Find USB device driver.
Definition: usb.c:1166
#define ffsll(x)
Find first (i.e.
Definition: strings.h:122
static struct usb_endpoint_driver_operations usb_control_operations
USB control endpoint driver operations.
Definition: usb.c:768
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
struct usb_hub_driver_operations * driver
Driver operations.
Definition: usb.h:858
void(* close)(struct usb_hub *hub)
Close hub.
Definition: usb.h:881
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
USB 3.0.
Definition: usb.h:24
struct device * dev
Underlying hardware device.
Definition: usb.h:969
An object interface.
Definition: interface.h:124
static int usb_clear_feature(struct usb_device *usb, unsigned int type, unsigned int feature, unsigned int index)
Clear feature.
Definition: usb.h:1126
struct usb_class_id class
Class ID.
Definition: usb.h:1412
#define USB_ENDPOINT_BURST(sizes)
USB endpoint maximum burst size.
Definition: usb.h:310
Not connected.
Definition: usb.h:46
uint16_t index
Index parameter.
Definition: usb.h:88
struct ib_cm_path alternate
Alternate path.
Definition: ib_mad.h:42
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:431
static const char * usb_speed_name(unsigned int speed)
Get USB speed name (for debugging)
Definition: usb.c:65
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define DBGC_HDA(...)
Definition: compiler.h:506
struct usb_descriptor_header header
Descriptor header.
Definition: cdc.h:30
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
Union functional descriptor.
Definition: cdc.h:28
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
uint8_t configurations
Number of possible configurations.
Definition: usb.h:202
struct list_head halted
List of halted endpoints.
Definition: usb.h:423
struct usb_class class
Device class.
Definition: usb.h:186
unsigned int burst
Maximum burst size.
Definition: usb.h:413
void route(void)
Print routing table.
Definition: route.c:39
int(* enable)(struct usb_hub *hub, struct usb_port *port)
Enable port.
Definition: usb.h:903
uint16_t count
Number of entries.
Definition: ena.h:22
#define USB_CONTROL_MAX_WAIT_MS
Maximum time to wait for a control transaction to complete.
Definition: usb.h:1341
#define USB_DIR_IN
Data transfer is from device to host.
Definition: usb.h:97
REQUIRING_SYMBOL(register_usb_bus)
char name[32]
Name.
Definition: usb.h:724
unsigned int ports
Number of ports.
Definition: usb.h:850
static void usb_step(struct process *process __unused)
USB process.
Definition: usb.c:1895
const char * driver_name
Driver name.
Definition: device.h:77
unsigned int location
Location.
Definition: device.h:29
const char * name
Name.
Definition: usb.h:842
size_t reserve
Refill buffer reserved header length.
Definition: usb.h:435
void(* close)(struct usb_device *usb)
Close device.
Definition: usb.h:767
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition: list.h:458
uint8_t id
Request identifier.
Definition: ena.h:12
A USB device.
Definition: usb.h:722
uint8_t type
Descriptor type.
Definition: usb.h:176
struct usb_interface_descriptor * usb_interface_descriptor(struct usb_configuration_descriptor *config, unsigned int interface, unsigned int alternate)
Locate USB interface descriptor.
Definition: usb.c:143
struct usb_endpoint control
Control endpoint.
Definition: usb.h:747
uint32_t high
High 32 bits of address.
Definition: myson.h:20
static void usb_poll(struct usb_bus *bus)
Poll USB bus.
Definition: usb.h:1071
#define USB_ENDPOINT_IN
Endpoint direction is in.
Definition: usb.h:524
#define USB_ENDPOINT_ATTR_INTERRUPT
Interrupt endpoint transfer type.
Definition: usb.h:292
Standard Interface Association Descriptor USB 3.0 spec, Section 9.6.4.
Definition: Usb.h:142
#define ERANGE
Result too large.
Definition: errno.h:639
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
struct list_head list
List of devices on this bus.
Definition: usb.h:730
unsigned int score
Driver score.
Definition: usb.h:1418
unsigned int usb_route_string(struct usb_device *usb)
Get USB route string.
Definition: usb.c:2335
static const char * interfaces[2]
Definition: smc9000.c:52
struct list_head siblings
Devices on the same bus.
Definition: device.h:81
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:155
#define USB_LANG_ENGLISH
Language ID for English.
Definition: usb.h:241
void usb_free_address(struct usb_bus *bus, unsigned int address)
Free device address.
Definition: usb.c:2283
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:385
#define ENODEV
No such device.
Definition: errno.h:509
static int usb_endpoint_reset(struct usb_endpoint *ep)
Reset USB endpoint.
Definition: usb.c:429
static int usb_get_mtu(struct usb_device *usb, struct usb_device_descriptor *data)
Get first part of USB device descriptor (up to and including MTU)
Definition: usb.h:1193
uint8_t attributes
Attributes.
Definition: usb.h:269
unsigned char uint8_t
Definition: stdint.h:10
void(* close)(struct usb_hub *hub)
Close hub.
Definition: usb.h:896
USB control transfer pseudo-header.
Definition: usb.c:742
struct list_head usb_buses
List of USB buses.
Definition: usb.c:44
unsigned int fill
Buffer fill level.
Definition: usb.h:420
static int usb_score(struct usb_device *usb, struct usb_configuration_descriptor *config)
Get USB device configuration score.
Definition: usb.c:1202
static void usb_remove(struct usb_function *func)
Remove USB device driver.
Definition: usb.c:1285
struct usb_device_descriptor device
Device descriptor.
Definition: usb.h:734
Standard Endpoint Descriptor USB 2.0 spec, Section 9.6.6.
Definition: Usb.h:173
struct usb_device * usb
USB device.
Definition: usb.h:677
static void usb_endpoint_init(struct usb_endpoint *ep, struct usb_device *usb, struct usb_endpoint_driver_operations *driver)
Initialise USB endpoint.
Definition: usb.h:539
#define le16_to_cpu(value)
Definition: byteswap.h:112
uint32_t scalar
Scalar value.
Definition: usb.h:647
struct usb_endpoint_companion_descriptor * usb_endpoint_companion_descriptor(struct usb_configuration_descriptor *config, struct usb_endpoint_descriptor *desc)
Locate USB endpoint companion descriptor.
Definition: usb.c:194
void * malloc(size_t size)
Allocate memory.
Definition: malloc.c:583
A USB setup data packet.
Definition: usb.h:82
#define USB_ENDPOINT_IDX(address)
Construct endpoint index from endpoint address.
Definition: usb.h:527
#define EALREADY
Connection already in progress.
Definition: errno.h:323
const char * usb_endpoint_name(struct usb_endpoint *ep)
Get USB endpoint name (for debugging)
Definition: usb.c:220
struct usb_port * usb_root_hub_port(struct usb_device *usb)
Get USB root hub port.
Definition: usb.c:2355
uint8_t protocol
Protocol code.
Definition: usb.h:165
#define USB_EP0_INTERVAL
Control endpoint interval.
Definition: usb.h:518
static int usb_endpoint_clear_tt(struct usb_endpoint *ep)
Clear transaction translator (if applicable)
Definition: usb.c:340
size_t mtu
Maximum transfer size.
Definition: usb.h:411
uint8_t count
Interface count.
Definition: usb.h:334
#define USB_CLASS_CDC
Class code for communications devices.
Definition: cdc.h:15
uint16_t request
Request.
Definition: usb.h:84
static void free_usb(struct usb_device *usb)
Free USB device.
Definition: usb.c:1714
A USB descriptor header.
Definition: usb.h:172
void free_usb_hub(struct usb_hub *hub)
Free USB hub.
Definition: usb.c:2062
static void usb_remove_all(struct usb_device *usb)
Remove all device drivers.
Definition: usb.c:1381
struct usb_port * usb_transaction_translator(struct usb_device *usb)
Get USB transaction translator.
Definition: usb.c:2371
int(* disable)(struct usb_hub *hub, struct usb_port *port)
Disable port.
Definition: usb.h:910
static int usb_hotplugged(struct usb_port *port)
Handle newly attached or detached USB device.
Definition: usb.c:1817
struct usb_host_operations * op
Host controller operations set.
Definition: usb.h:971
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:78
static int usb_get_device_descriptor(struct usb_device *usb, struct usb_device_descriptor *data)
Get USB device descriptor.
Definition: usb.h:1209
static void usb_deconfigure(struct usb_device *usb)
Clear USB device configuration.
Definition: usb.c:1408
#define iob_reserve(iobuf, len)
Definition: iobuf.h:67
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
unsigned int max
Maximum fill level.
Definition: usb.h:439
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
A USB configuration descriptor.
Definition: usb.h:209
static int register_usb(struct usb_device *usb)
Register USB device.
Definition: usb.c:1537
static int usb_config_descriptor(struct usb_device *usb, unsigned int index, struct usb_configuration_descriptor **config)
Get USB configuration descriptor.
Definition: usb.c:989
unsigned int language
Default language ID (if known)
Definition: usb.h:752
static size_t iob_headroom(struct io_buffer *iobuf)
Calculate available space at start of an I/O buffer.
Definition: iobuf.h:165
static struct list_head usb_halted
List of halted endpoints.
Definition: usb.c:50
uint8_t subclass
Subclass code.
Definition: usb.h:163
struct usb_endpoint_descriptor * usb_endpoint_descriptor(struct usb_configuration_descriptor *config, struct usb_interface_descriptor *interface, unsigned int type, unsigned int index)
Locate USB endpoint descriptor.
Definition: usb.c:167
unsigned int bus_type
Bus type.
Definition: device.h:24
A USB endpoint descriptor.
Definition: usb.h:263
uint8_t first
First interface number.
Definition: usb.h:332
struct list_head list
List of which this buffer is a member.
Definition: iobuf.h:40
uint8_t config
Configuration value.
Definition: usb.h:217
struct list_head complete
Completed control transfers.
Definition: usb.h:749
#define DBGC2(...)
Definition: compiler.h:522
#define USB_ANY_ID
Match-anything ID.
Definition: usb.h:1372
uint16_t protocol
USB specification release number in BCD.
Definition: usb.h:184
Universal Serial Bus (USB)
int register_usb_bus(struct usb_bus *bus)
Register USB bus.
Definition: usb.c:2130
struct list_head list
List of hubs.
Definition: usb.h:853
unsigned int device
Device ID.
Definition: device.h:33
uint8_t mtu
Maximum packet size for endpoint zero.
Definition: usb.h:188
uint32_t mtu
Maximum MTU.
Definition: ena.h:28
A USB endpoint companion descriptor.
Definition: usb.h:313
unsigned int address
Port address.
Definition: usb.h:816
void * data
Start of data.
Definition: iobuf.h:48
struct list_head children
Devices attached to this device.
Definition: device.h:83
struct usb_hub * hub
USB hub.
Definition: usb.h:814
unsigned int id_count
Number of entries in ID table.
Definition: usb.h:1410
#define USB_SET_ADDRESS_RECOVER_DELAY_MS
Set address recovery time.
Definition: usb.h:1348
High speed (480Mbps)
Definition: usb.h:52
void(* remove)(struct usb_function *func)
Remove device.
Definition: usb.h:1433
struct usb_endpoint * ep[32]
Endpoint list.
Definition: usb.h:744
unsigned int interval
Interval (in microframes)
Definition: usb.h:415
struct ena_aq_header header
Header.
Definition: ena.h:12
u8 request[0]
List of IEs requested.
Definition: ieee80211.h:16
struct usb_endpoint_host_operations endpoint
Endpoint operations.
Definition: usb.h:1032
#define cpu_to_le16(value)
Definition: byteswap.h:106
int(* address)(struct usb_device *usb)
Assign device address.
Definition: usb.h:773
uint8_t data[48]
Additional event data.
Definition: ena.h:22
struct device_description desc
Device description.
Definition: device.h:79
USB host controller operations.
Definition: usb.h:1030
#define USB_ENDPOINT_ATTR_CONTROL
Control endpoint transfer type.
Definition: usb.h:286
int register_usb_hub(struct usb_hub *hub)
Register USB hub.
Definition: usb.c:1975
#define USB_PORT_DELAY_MS
Time to wait for ports to stabilise.
Definition: usb.h:1357
struct usb_class class
Association class.
Definition: usb.h:336
USB Communications Device Class (CDC)
struct usb_hub * hub
Root hub.
Definition: usb.h:994
static struct usb_interface_association_descriptor * usb_interface_association_descriptor(struct usb_configuration_descriptor *config, unsigned int first)
Locate USB interface association descriptor.
Definition: usb.c:119
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
REQUIRE_OBJECT(config_usb)
int snprintf(char *buf, size_t size, const char *fmt,...)
Write a formatted string to a buffer.
Definition: vsprintf.c:382
uint16_t len
Total length.
Definition: usb.h:213
uint16_t len
Length of data stage.
Definition: usb.h:90
#define USB_DRIVERS
USB driver table.
Definition: usb.h:1437
struct usb_function_descriptor desc
Function descriptor.
Definition: usb.h:679
struct usb_device * usb
USB device.
Definition: usb.h:405
static struct usb_port * usb_port(struct usb_hub *hub, unsigned int address)
Get USB port.
Definition: usb.h:959
uint16_t protocol
Protocol ID.
Definition: stp.h:18
static int usb_is_within_config(struct usb_configuration_descriptor *config, struct usb_descriptor_header *desc)
Check that descriptor lies within a configuration descriptor.
Definition: usb.h:370
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:45
#define USB_ENDPOINT_MTU(sizes)
USB endpoint MTU.
Definition: usb.h:307
struct usb_endpoint_driver_operations * driver
Driver operations.
Definition: usb.h:430
int(* message)(struct usb_endpoint *ep, struct io_buffer *iobuf)
Enqueue message transfer.
Definition: usb.h:474
int(* open)(struct usb_endpoint *ep)
Open endpoint.
Definition: usb.h:449
void usb_flush(struct usb_endpoint *ep)
Discard endpoint recycled buffer list.
Definition: usb.c:719
unsigned int protocol
Hub protocol.
Definition: usb.h:848
struct usb_endpoint_host_operations * host
Host controller operations.
Definition: usb.h:426
static int usb_get_descriptor(struct usb_device *usb, unsigned int type, unsigned int desc, unsigned int index, unsigned int language, struct usb_descriptor_header *data, size_t len)
Get USB descriptor.
Definition: usb.h:1176
struct usb_device_host_operations * host
Host controller operations.
Definition: usb.h:739
static struct list_head usb_changed
List of changed ports.
Definition: usb.c:47
USB endpoint driver operations.
Definition: usb.h:488
#define LIST_HEAD_INIT(list)
Initialise a static list head.
Definition: list.h:30
struct device dev
Generic device.
Definition: usb.h:681
A USB function.
Definition: usb.h:673
uint16_t value
Value parameter.
Definition: usb.h:86
#define BUS_TYPE_USB
USB bus type.
Definition: device.h:70
struct usb_bus * bus
USB bus.
Definition: usb.h:844
struct list_head functions
List of functions.
Definition: usb.h:736
static struct usb_device * alloc_usb(struct usb_port *port)
Allocate USB device.
Definition: usb.c:1511
#define USB_ENDPOINT_HALT
Endpoint halt feature.
Definition: usb.h:156
#define USB_EP0_ADDRESS
Control endpoint address.
Definition: usb.h:500
int(* speed)(struct usb_hub *hub, struct usb_port *port)
Update port speed.
Definition: usb.h:917
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
String functions.
struct list_head list
List of functions within this USB device.
Definition: usb.h:683
int usb_alloc_address(struct usb_bus *bus)
Allocate device address.
Definition: usb.c:2263
A USB bus.
Definition: usb.h:965
unsigned int attributes
Attributes.
Definition: usb.h:409
static int fill
Definition: string.h:208
uint8_t interface
Interface number.
Definition: usb.h:248
static int usb_describe(struct usb_device *usb, struct usb_configuration_descriptor *config, unsigned int first, uint8_t *interfaces, struct usb_function_descriptor *desc)
Describe USB function.
Definition: usb.c:1052
uint8_t bus
Bus.
Definition: edd.h:14
Full speed (12Mbps)
Definition: usb.h:50
uint8_t interface[1]
Interfaces (variable-length)
Definition: cdc.h:34
unsigned int address
Endpoint address.
Definition: usb.h:407
void free_usb_bus(struct usb_bus *bus)
Free USB bus.
Definition: usb.c:2194
uint8_t burst
Maximum burst size.
Definition: usb.h:317
struct usb_class class
Class.
Definition: usb.h:645
void usb_complete_err(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete transfer (possibly with error)
Definition: usb.c:586
static int usb_probe(struct usb_function *func, struct usb_configuration_descriptor *config)
Probe USB device driver.
Definition: usb.c:1247
int(* open)(struct usb_hub *hub)
Open hub.
Definition: usb.h:891
struct usb_device_id * ids
USB ID table.
Definition: usb.h:1408
int(* open)(struct usb_device *usb)
Open device.
Definition: usb.h:762
String functions.
int usb_get_string_descriptor(struct usb_device *usb, unsigned int index, unsigned int language, char *buf, size_t len)
Get USB string descriptor.
Definition: usb.c:915
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:33
int(* stream)(struct usb_endpoint *ep, struct io_buffer *iobuf, int zlp)
Enqueue stream transfer.
Definition: usb.h:483
#define USB_DEV(busdev)
Extract USB device address.
Definition: usb.h:69
struct usb_hub * alloc_usb_hub(struct usb_bus *bus, struct usb_device *usb, unsigned int ports, struct usb_hub_driver_operations *driver)
Allocate USB hub.
Definition: usb.c:1936
struct usb_port port[0]
Port list.
Definition: usb.h:866