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