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  * 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  size_t max = ( len ? ( len - 1 /* NUL */ ) : 0 );
918  struct {
920  uint16_t character[max];
921  } __attribute__ (( packed )) *desc;
922  unsigned int actual;
923  unsigned int i;
924  int rc;
925 
926  /* Use default language ID, if applicable */
927  if ( ( language == 0 ) && ( index != 0 ) ) {
928  if ( ! usb->language )
929  usb->language = usb_get_default_language ( usb );
930  language = usb->language;
931  }
932 
933  /* Allocate buffer for string */
934  desc = malloc ( sizeof ( *desc ) );
935  if ( ! desc ) {
936  rc = -ENOMEM;
937  goto err_alloc;
938  }
939 
940  /* Get descriptor */
941  if ( ( rc = usb_get_descriptor ( usb, 0, USB_STRING_DESCRIPTOR, index,
942  language, &desc->header,
943  sizeof ( *desc ) ) ) != 0 )
944  goto err_get_descriptor;
945 
946  /* Calculate string length */
947  if ( desc->header.len < sizeof ( desc->header ) ) {
948  rc = -EINVAL;
949  goto err_len;
950  }
951  actual = ( ( desc->header.len - sizeof ( desc->header ) ) /
952  sizeof ( desc->character[0] ) );
953 
954  /* Copy to buffer */
955  for ( i = 0 ; ( ( i < actual ) && ( i < max ) ) ; i++ )
956  buf[i] = le16_to_cpu ( desc->character[i] );
957  if ( len )
958  buf[i] = '\0';
959 
960  /* Free buffer */
961  free ( desc );
962 
963  return actual;
964 
965  err_len:
966  err_get_descriptor:
967  free ( desc );
968  err_alloc:
969  return rc;
970 }
971 
972 /******************************************************************************
973  *
974  * USB device driver
975  *
976  ******************************************************************************
977  */
978 
979 /**
980  * Get USB configuration descriptor
981  *
982  * @v usb USB device
983  * @v index Configuration index
984  * @ret config Configuration descriptor
985  * @ret rc Return status code
986  *
987  * The configuration descriptor is dynamically allocated and must
988  * eventually be freed by the caller.
989  */
990 static int
991 usb_config_descriptor ( struct usb_device *usb, unsigned int index,
992  struct usb_configuration_descriptor **config ) {
993  struct usb_configuration_descriptor partial;
994  size_t len;
995  int rc;
996 
997  /* Read first part of configuration descriptor to get size */
998  if ( ( rc = usb_get_config_descriptor ( usb, index, &partial,
999  sizeof ( partial ) ) ) != 0 ) {
1000  DBGC ( usb, "USB %s could not get configuration descriptor %d: "
1001  "%s\n", usb->name, index, strerror ( rc ) );
1002  goto err_get_partial;
1003  }
1004  len = le16_to_cpu ( partial.len );
1005  if ( len < sizeof ( partial ) ) {
1006  DBGC ( usb, "USB %s underlength configuraton descriptor %d\n",
1007  usb->name, index );
1008  rc = -EINVAL;
1009  goto err_partial_len;
1010  }
1011 
1012  /* Allocate buffer for whole configuration descriptor */
1013  *config = malloc ( len );
1014  if ( ! *config ) {
1015  rc = -ENOMEM;
1016  goto err_alloc_config;
1017  }
1018 
1019  /* Read whole configuration descriptor */
1020  if ( ( rc = usb_get_config_descriptor ( usb, index, *config,
1021  len ) ) != 0 ) {
1022  DBGC ( usb, "USB %s could not get configuration descriptor %d: "
1023  "%s\n", usb->name, index, strerror ( rc ) );
1024  goto err_get_config_descriptor;
1025  }
1026  if ( (*config)->len != partial.len ) {
1027  DBGC ( usb, "USB %s bad configuration descriptor %d length\n",
1028  usb->name, index );
1029  rc = -EINVAL;
1030  goto err_config_len;
1031  }
1032 
1033  return 0;
1034 
1035  err_config_len:
1036  err_get_config_descriptor:
1037  free ( *config );
1038  err_alloc_config:
1039  err_partial_len:
1040  err_get_partial:
1041  return rc;
1042 }
1043 
1044 /**
1045  * Describe USB function
1046  *
1047  * @v usb USB device
1048  * @v config Configuration descriptor
1049  * @v first First interface number
1050  * @v interfaces Interface list to fill in
1051  * @v desc Function descriptor to fill in
1052  * @ret rc Return status code
1053  */
1054 static int usb_describe ( struct usb_device *usb,
1056  unsigned int first, uint8_t *interfaces,
1057  struct usb_function_descriptor *desc ) {
1058  struct usb_interface_association_descriptor *association;
1060  struct cdc_union_descriptor *cdc_union;
1061  unsigned int i;
1062 
1063  /* Fill in vendor and product ID */
1064  memset ( desc, 0, sizeof ( *desc ) );
1065  desc->vendor = le16_to_cpu ( usb->device.vendor );
1066  desc->product = le16_to_cpu ( usb->device.product );
1067 
1068  /* First, look for an interface association descriptor */
1069  association = usb_interface_association_descriptor ( config, first );
1070  if ( association ) {
1071 
1072  /* Sanity check */
1073  assert ( association->first == first );
1074  if ( ( first + association->count ) > config->interfaces ) {
1075  DBGC ( usb, "USB %s has invalid association [%d-%d)\n",
1076  usb->name, first, ( first + association->count));
1077  return -ERANGE;
1078  }
1079 
1080  /* Describe function */
1081  memcpy ( &desc->class.class, &association->class,
1082  sizeof ( desc->class.class ) );
1083  desc->count = association->count;
1084  for ( i = 0 ; i < association->count ; i++ )
1085  interfaces[i] = ( first + i );
1086  return 0;
1087  }
1088 
1089  /* Next, look for an interface descriptor */
1090  interface = usb_interface_descriptor ( config, first, 0 );
1091  if ( ! interface ) {
1092  DBGC ( usb, "USB %s has no descriptor for interface %d\n",
1093  usb->name, first );
1094  return -ENOENT;
1095  }
1096 
1097  /* Describe function */
1098  memcpy ( &desc->class.class, &interface->class,
1099  sizeof ( desc->class.class ) );
1100  desc->count = 1;
1101  interfaces[0] = first;
1102 
1103  /* Look for a CDC union descriptor, if applicable */
1104  if ( ( desc->class.class.class == USB_CLASS_CDC ) &&
1105  ( cdc_union = cdc_union_descriptor ( config, interface ) ) ) {
1106 
1107  /* Determine interface count */
1108  desc->count = ( ( cdc_union->header.len -
1109  offsetof ( typeof ( *cdc_union ),
1110  interface[0] ) ) /
1111  sizeof ( cdc_union->interface[0] ) );
1112  if ( desc->count > config->interfaces ) {
1113  DBGC ( usb, "USB %s has invalid union functional "
1114  "descriptor with %d interfaces\n",
1115  usb->name, desc->count );
1116  return -ERANGE;
1117  }
1118 
1119  /* Describe function */
1120  for ( i = 0 ; i < desc->count ; i++ ) {
1121  if ( cdc_union->interface[i] >= config->interfaces ) {
1122  DBGC ( usb, "USB %s has invalid union "
1123  "functional descriptor covering "
1124  "interface %d\n", usb->name,
1125  cdc_union->interface[i] );
1126  return -ERANGE;
1127  }
1128  interfaces[i] = cdc_union->interface[i];
1129  }
1130 
1131  return 0;
1132  }
1133 
1134  return 0;
1135 }
1136 
1137 /**
1138  * Update list of used interface
1139  *
1140  * @v usb USB device
1141  * @v count Number of interfaces
1142  * @v interface List of interfaces
1143  * @v used List of already-used interfaces
1144  * @ret rc Return status code
1145  */
1146 static int usb_used ( struct usb_device *usb, unsigned int count,
1147  uint8_t *interface, uint8_t *used ) {
1148  unsigned int i;
1149 
1150  for ( i = 0 ; i < count ; i++ ) {
1151  if ( used[interface[i]] ) {
1152  DBGC ( usb, "USB %s interface %d already in use\n",
1153  usb->name, interface[i] );
1154  return -EINVAL;
1155  }
1156  used[interface[i]] = 1;
1157  }
1158  return 0;
1159 }
1160 
1161 /**
1162  * Find USB device driver
1163  *
1164  * @v desc Function descriptor
1165  * @ret id USB device ID, or NULL
1166  * @ret driver USB device driver, or NULL
1167  */
1169  struct usb_device_id **id ) {
1170  struct usb_driver *driver;
1171  unsigned int i;
1172 
1173  /* Look for a matching driver */
1174  for_each_table_entry ( driver, USB_DRIVERS ) {
1175  for ( i = 0 ; i < driver->id_count ; i++ ) {
1176 
1177  /* Ignore non-matching driver class */
1178  if ( ( driver->class.class.scalar ^ desc->class.scalar )
1179  & driver->class.mask.scalar )
1180  continue;
1181 
1182  /* Look for a matching ID */
1183  *id = &driver->ids[i];
1184  if ( ( ( (*id)->vendor == desc->vendor ) ||
1185  ( (*id)->vendor == USB_ANY_ID ) ) &&
1186  ( ( (*id)->product == desc->product ) ||
1187  ( (*id)->product == USB_ANY_ID ) ) )
1188  return driver;
1189  }
1190  }
1191 
1192  /* Not found */
1193  *id = NULL;
1194  return NULL;
1195 }
1196 
1197 /**
1198  * Get USB device configuration score
1199  *
1200  * @v usb USB device
1201  * @v config Configuration descriptor
1202  * @ret score Device configuration score, or negative error
1203  */
1204 static int usb_score ( struct usb_device *usb,
1205  struct usb_configuration_descriptor *config ) {
1206  uint8_t used[config->interfaces];
1207  uint8_t interface[config->interfaces];
1208  struct usb_function_descriptor desc;
1209  struct usb_driver *driver;
1210  struct usb_device_id *id;
1211  unsigned int first;
1212  unsigned int score = 0;
1213  int rc;
1214 
1215  /* Identify each function in turn */
1216  memset ( used, 0, sizeof ( used ) );
1217  for ( first = 0 ; first < config->interfaces ; first++ ) {
1218 
1219  /* Skip interfaces already used */
1220  if ( used[first] )
1221  continue;
1222 
1223  /* Describe function */
1224  if ( ( rc = usb_describe ( usb, config, first, interface,
1225  &desc ) ) != 0 )
1226  return rc;
1227 
1228  /* Update used interfaces */
1229  if ( ( rc = usb_used ( usb, desc.count, interface,
1230  used ) ) != 0 )
1231  return rc;
1232 
1233  /* Look for a driver for this function */
1234  driver = usb_find_driver ( &desc, &id );
1235  if ( driver )
1236  score += driver->score;
1237  }
1238 
1239  return score;
1240 }
1241 
1242 /**
1243  * Probe USB device driver
1244  *
1245  * @v func USB function
1246  * @v config Configuration descriptor
1247  * @ret rc Return status code
1248  */
1249 static int usb_probe ( struct usb_function *func,
1250  struct usb_configuration_descriptor *config ) {
1251  struct usb_device *usb = func->usb;
1252  struct usb_driver *driver;
1253  struct usb_device_id *id;
1254  int rc;
1255 
1256  /* Identify driver */
1257  driver = usb_find_driver ( &func->desc, &id );
1258  if ( ! driver ) {
1259  DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d has no driver\n",
1260  func->name, func->desc.vendor, func->desc.product,
1261  func->desc.class.class.class,
1262  func->desc.class.class.subclass,
1263  func->desc.class.class.protocol );
1264  return -ENOENT;
1265  }
1266 
1267  /* Record driver */
1268  func->driver = driver;
1269  func->id = id;
1270  func->dev.driver_name = id->name;
1271 
1272  /* Probe driver */
1273  if ( ( rc = driver->probe ( func, config ) ) != 0 ) {
1274  DBGC ( usb, "USB %s failed to probe driver %s: %s\n",
1275  func->name, id->name, strerror ( rc ) );
1276  return rc;
1277  }
1278 
1279  return 0;
1280 }
1281 
1282 /**
1283  * Remove USB device driver
1284  *
1285  * @v func USB function
1286  */
1287 static void usb_remove ( struct usb_function *func ) {
1288 
1289  /* Remove driver */
1290  func->driver->remove ( func );
1291 }
1292 
1293 /**
1294  * Probe all USB device drivers
1295  *
1296  * @v usb USB device
1297  * @v config Configuration descriptor
1298  */
1299 static void
1301  struct usb_configuration_descriptor *config ) {
1302  struct usb_bus *bus = usb->port->hub->bus;
1303  struct usb_function *func;
1304  uint8_t used[config->interfaces];
1305  unsigned int first;
1306  unsigned int i;
1307  int rc;
1308 
1309  /* Identify each function in turn */
1310  memset ( used, 0, sizeof ( used ) );
1311  for ( first = 0 ; first < config->interfaces ; first++ ) {
1312 
1313  /* Skip interfaces already used */
1314  if ( used[first] )
1315  continue;
1316 
1317  /* Allocate and initialise structure */
1318  func = zalloc ( sizeof ( *func ) +
1319  ( config->interfaces *
1320  sizeof ( func->interface[0] ) ) );
1321  if ( ! func )
1322  goto err_alloc;
1323  func->name = func->dev.name;
1324  func->usb = usb;
1325  func->dev.desc.bus_type = BUS_TYPE_USB;
1326  func->dev.desc.location = usb->address;
1327  func->dev.desc.vendor = le16_to_cpu ( usb->device.vendor );
1328  func->dev.desc.device = le16_to_cpu ( usb->device.product );
1329  snprintf ( func->dev.name, sizeof ( func->dev.name ),
1330  "%s-%d.%d", usb->name, config->config, first );
1331  INIT_LIST_HEAD ( &func->dev.children );
1332  func->dev.parent = bus->dev;
1333  list_add_tail ( &func->list, &usb->functions );
1334 
1335  /* Identify function */
1336  if ( ( rc = usb_describe ( usb, config, first, func->interface,
1337  &func->desc ) ) != 0 )
1338  goto err_describe;
1339  assert ( func->desc.count <= config->interfaces );
1340 
1341  /* Mark interfaces as used */
1342  if ( ( rc = usb_used ( usb, func->desc.count, func->interface,
1343  used ) ) != 0 )
1344  goto err_used;
1345 
1346  /* Probe device driver */
1347  if ( ( rc = usb_probe ( func, config ) ) != 0 )
1348  goto err_probe;
1349  DBGC ( usb, "USB %s %04x:%04x class %d:%d:%d interfaces ",
1350  func->name, func->desc.vendor, func->desc.product,
1351  func->desc.class.class.class,
1352  func->desc.class.class.subclass,
1353  func->desc.class.class.protocol );
1354  for ( i = 0 ; i < func->desc.count ; i++ )
1355  DBGC ( usb, "%s%d", ( i ? "," : "" ),
1356  func->interface[i] );
1357  DBGC ( usb, " using driver %s\n", func->dev.driver_name );
1358 
1359  /* Add to device hierarchy */
1360  list_add_tail ( &func->dev.siblings, &bus->dev->children );
1361 
1362  continue;
1363 
1364  list_del ( &func->dev.siblings );
1365  usb_remove ( func );
1366  err_probe:
1367  err_used:
1368  err_describe:
1369  list_del ( &func->list );
1370  free ( func );
1371  err_alloc:
1372  /* Continue registering other functions */
1373  continue;
1374  }
1375 }
1376 
1377 /**
1378  * Remove all device drivers
1379  *
1380  * @v usb USB device
1381  */
1382 static void usb_remove_all ( struct usb_device *usb ) {
1383  struct usb_function *func;
1384  struct usb_function *tmp;
1385 
1386  /* Remove all functions */
1388 
1389  /* Remove device driver */
1390  usb_remove ( func );
1391 
1392  /* Remove from device hierarchy */
1393  assert ( list_empty ( &func->dev.children ) );
1394  list_del ( &func->dev.siblings );
1395 
1396  /* Remove from list of functions */
1397  list_del ( &func->list );
1398 
1399  /* Free function */
1400  free ( func );
1401  }
1402 }
1403 
1404 /**
1405  * Clear USB device configuration
1406  *
1407  * @v usb USB device
1408  */
1409 static void usb_deconfigure ( struct usb_device *usb ) {
1410  unsigned int i;
1411 
1412  /* Remove device drivers */
1413  usb_remove_all ( usb );
1414 
1415  /* Sanity checks */
1416  for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++){
1417  if ( i != USB_ENDPOINT_IDX ( USB_EP0_ADDRESS ) )
1418  assert ( usb->ep[i] == NULL );
1419  }
1420 
1421  /* Clear device configuration */
1422  usb_set_configuration ( usb, 0 );
1423 }
1424 
1425 /**
1426  * Choose our preferred USB device configuration
1427  *
1428  * @v usb USB device
1429  * @ret rc Return status code
1430  */
1431 static int usb_autoconfigure ( struct usb_device *usb ) {
1433  unsigned int preferred = 0;
1434  unsigned int index;
1435  int score;
1436  int best = 0;
1437  int rc;
1438 
1439  /* Calculate driver score for each configuration index */
1440  for ( index = 0 ; index < usb->device.configurations ; index++ ) {
1441 
1442  /* Read configuration descriptor */
1443  if ( ( rc = usb_config_descriptor ( usb, index,
1444  &config ) ) != 0 )
1445  goto err_config;
1446 
1447  /* Get score for this configuration */
1448  score = usb_score ( usb, config );
1449  if ( score < 0 ) {
1450  rc = score;
1451  goto err_score;
1452  }
1453  DBGC2 ( usb, "USB %s configuration %d score %d\n",
1454  usb->name, config->config, score );
1455 
1456  /* Record as preferred configuration, if applicable */
1457  if ( score > best ) {
1458  best = score;
1459  preferred = index;
1460  }
1461 
1462  /* Free configuration descriptor */
1463  free ( config );
1464  config = NULL;
1465  }
1466 
1467  /* Read preferred configuration descriptor */
1468  if ( ( rc = usb_config_descriptor ( usb, preferred, &config ) ) != 0 )
1469  goto err_preferred;
1470 
1471  /* Set configuration */
1472  if ( ( rc = usb_set_configuration ( usb, config->config ) ) != 0){
1473  DBGC ( usb, "USB %s could not set configuration %d: %s\n",
1474  usb->name, config->config, strerror ( rc ) );
1475  goto err_set_configuration;
1476  }
1477 
1478  /* Probe USB device drivers */
1479  usb_probe_all ( usb, config );
1480 
1481  /* Free configuration descriptor */
1482  free ( config );
1483 
1484  return 0;
1485 
1486  usb_remove_all ( usb );
1487  usb_set_configuration ( usb, 0 );
1488  err_set_configuration:
1489  free ( config );
1490  err_preferred:
1491  return rc;
1492 
1493  err_score:
1494  free ( config );
1495  err_config:
1496  return rc;
1497 }
1498 
1499 /******************************************************************************
1500  *
1501  * USB device
1502  *
1503  ******************************************************************************
1504  */
1505 
1506 /**
1507  * Allocate USB device
1508  *
1509  * @v port USB port
1510  * @ret usb USB device, or NULL on allocation failure
1511  */
1512 static struct usb_device * alloc_usb ( struct usb_port *port ) {
1513  struct usb_hub *hub = port->hub;
1514  struct usb_bus *bus = hub->bus;
1515  struct usb_device *usb;
1516 
1517  /* Allocate and initialise structure */
1518  usb = zalloc ( sizeof ( *usb ) );
1519  if ( ! usb )
1520  return NULL;
1521  snprintf ( usb->name, sizeof ( usb->name ), "%s%c%d", hub->name,
1522  ( hub->usb ? '.' : '-' ), port->address );
1523  usb->port = port;
1524  INIT_LIST_HEAD ( &usb->functions );
1525  usb->host = &bus->op->device;
1527  INIT_LIST_HEAD ( &usb->complete );
1528 
1529  return usb;
1530 }
1531 
1532 /**
1533  * Register USB device
1534  *
1535  * @v usb USB device
1536  * @ret rc Return status code
1537  */
1538 static int register_usb ( struct usb_device *usb ) {
1539  struct usb_port *port = usb->port;
1540  struct usb_hub *hub = port->hub;
1541  struct usb_bus *bus = hub->bus;
1542  unsigned int protocol;
1543  size_t mtu;
1544  int rc;
1545 
1546  /* Add to port */
1547  if ( port->usb != NULL ) {
1548  DBGC ( hub, "USB hub %s port %d is already registered to %s\n",
1549  hub->name, port->address, port->usb->name );
1550  rc = -EALREADY;
1551  goto err_already;
1552  }
1553  port->usb = usb;
1554 
1555  /* Add to bus device list */
1556  list_add_tail ( &usb->list, &bus->devices );
1557 
1558  /* Enable device */
1559  if ( ( rc = hub->driver->enable ( hub, port ) ) != 0 ) {
1560  DBGC ( hub, "USB hub %s port %d could not enable: %s\n",
1561  hub->name, port->address, strerror ( rc ) );
1562  goto err_enable;
1563  }
1564 
1565  /* Allow recovery interval since port may have been reset */
1567 
1568  /* Get device speed */
1569  if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
1570  DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
1571  hub->name, port->address, strerror ( rc ) );
1572  goto err_speed;
1573  }
1574  usb->speed = port->speed;
1575  DBGC2 ( usb, "USB %s attached as %s-speed device\n",
1576  usb->name, usb_speed_name ( usb->speed ) );
1577 
1578  /* Open device */
1579  if ( ( rc = usb->host->open ( usb ) ) != 0 ) {
1580  DBGC ( usb, "USB %s could not open: %s\n",
1581  usb->name, strerror ( rc ) );
1582  goto err_open;
1583  }
1584 
1585  /* Describe control endpoint */
1586  mtu = USB_EP0_DEFAULT_MTU ( usb->speed );
1589  USB_EP0_INTERVAL );
1590 
1591  /* Open control endpoint */
1592  if ( ( rc = usb_endpoint_open ( &usb->control ) ) != 0 )
1593  goto err_open_control;
1594  assert ( usb_endpoint ( usb, USB_EP0_ADDRESS ) == &usb->control );
1595 
1596  /* Assign device address */
1597  if ( ( rc = usb->host->address ( usb ) ) != 0 ) {
1598  DBGC ( usb, "USB %s could not set address: %s\n",
1599  usb->name, strerror ( rc ) );
1600  goto err_address;
1601  }
1602  DBGC2 ( usb, "USB %s assigned address %d\n", usb->name, usb->address );
1603 
1604  /* Allow recovery interval after Set Address command */
1606 
1607  /* Read first part of device descriptor to get EP0 MTU */
1608  if ( ( rc = usb_get_mtu ( usb, &usb->device ) ) != 0 ) {
1609  DBGC ( usb, "USB %s could not get MTU: %s\n",
1610  usb->name, strerror ( rc ) );
1611  goto err_get_mtu;
1612  }
1613 
1614  /* Calculate EP0 MTU */
1615  protocol = le16_to_cpu ( usb->device.protocol );
1616  mtu = ( ( protocol < USB_PROTO_3_0 ) ?
1617  usb->device.mtu : ( 1 << usb->device.mtu ) );
1618  DBGC2 ( usb, "USB %s has control MTU %zd (guessed %zd)\n",
1619  usb->name, mtu, usb->control.mtu );
1620 
1621  /* Update MTU */
1622  if ( ( rc = usb_endpoint_mtu ( &usb->control, mtu ) ) != 0 )
1623  goto err_mtu;
1624 
1625  /* Read whole device descriptor */
1626  if ( ( rc = usb_get_device_descriptor ( usb, &usb->device ) ) != 0 ) {
1627  DBGC ( usb, "USB %s could not get device descriptor: %s\n",
1628  usb->name, strerror ( rc ) );
1629  goto err_get_device_descriptor;
1630  }
1631  DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d (v%s, %s-speed, "
1632  "MTU %zd)\n", usb->name, usb->address,
1633  le16_to_cpu ( usb->device.vendor ),
1634  le16_to_cpu ( usb->device.product ), usb->device.class.class,
1636  usb_bcd ( le16_to_cpu ( usb->device.protocol ) ),
1637  usb_speed_name ( usb->speed ), usb->control.mtu );
1638 
1639  /* Configure device */
1640  if ( ( rc = usb_autoconfigure ( usb ) ) != 0 )
1641  goto err_autoconfigure;
1642 
1643  return 0;
1644 
1645  usb_deconfigure ( usb );
1646  err_autoconfigure:
1647  err_get_device_descriptor:
1648  err_mtu:
1649  err_get_mtu:
1650  err_address:
1651  usb_endpoint_close ( &usb->control );
1652  err_open_control:
1653  usb->host->close ( usb );
1654  err_open:
1655  err_speed:
1656  /* Leave port enabled on failure, to avoid an endless loop of
1657  * failed device registrations.
1658  */
1659  err_enable:
1660  list_del ( &usb->list );
1661  port->usb = NULL;
1662  err_already:
1663  return rc;
1664 }
1665 
1666 /**
1667  * Unregister USB device
1668  *
1669  * @v usb USB device
1670  */
1671 static void unregister_usb ( struct usb_device *usb ) {
1672  struct usb_port *port = usb->port;
1673  struct usb_hub *hub = port->hub;
1674  struct io_buffer *iobuf;
1675  struct io_buffer *tmp;
1676 
1677  DBGC ( usb, "USB %s addr %d %04x:%04x class %d:%d:%d removed\n",
1678  usb->name, usb->address, le16_to_cpu ( usb->device.vendor ),
1679  le16_to_cpu ( usb->device.product ), usb->device.class.class,
1680  usb->device.class.subclass, usb->device.class.protocol );
1681 
1682  /* Sanity checks */
1683  assert ( port->usb == usb );
1684 
1685  /* Clear device configuration */
1686  usb_deconfigure ( usb );
1687 
1688  /* Close control endpoint */
1689  usb_endpoint_close ( &usb->control );
1690 
1691  /* Discard any stale control completions */
1692  list_for_each_entry_safe ( iobuf, tmp, &usb->complete, list ) {
1693  list_del ( &iobuf->list );
1694  free_iob ( iobuf );
1695  }
1696 
1697  /* Close device */
1698  usb->host->close ( usb );
1699 
1700  /* Disable port */
1701  hub->driver->disable ( hub, port );
1702 
1703  /* Remove from bus device list */
1704  list_del ( &usb->list );
1705 
1706  /* Remove from port */
1707  port->usb = NULL;
1708 }
1709 
1710 /**
1711  * Free USB device
1712  *
1713  * @v usb USB device
1714  */
1715 static void free_usb ( struct usb_device *usb ) {
1716  unsigned int i;
1717 
1718  /* Sanity checks */
1719  for ( i = 0 ; i < ( sizeof ( usb->ep ) / sizeof ( usb->ep[0] ) ) ; i++ )
1720  assert ( usb->ep[i] == NULL );
1721  assert ( list_empty ( &usb->functions ) );
1722  assert ( list_empty ( &usb->complete ) );
1723 
1724  /* Free device */
1725  free ( usb );
1726 }
1727 
1728 /******************************************************************************
1729  *
1730  * USB device hotplug event handling
1731  *
1732  ******************************************************************************
1733  */
1734 
1735 /**
1736  * Handle newly attached USB device
1737  *
1738  * @v port USB port
1739  * @ret rc Return status code
1740  */
1741 static int usb_attached ( struct usb_port *port ) {
1742  struct usb_device *usb;
1743  int rc;
1744 
1745  /* Mark port as attached */
1746  port->attached = 1;
1747 
1748  /* Sanity checks */
1749  assert ( port->usb == NULL );
1750 
1751  /* Allocate USB device */
1752  usb = alloc_usb ( port );
1753  if ( ! usb ) {
1754  rc = -ENOMEM;
1755  goto err_alloc;
1756  }
1757 
1758  /* Register USB device */
1759  if ( ( rc = register_usb ( usb ) ) != 0 )
1760  goto err_register;
1761 
1762  return 0;
1763 
1764  unregister_usb ( usb );
1765  err_register:
1766  free_usb ( usb );
1767  err_alloc:
1768  return rc;
1769 }
1770 
1771 /**
1772  * Handle newly detached USB device
1773  *
1774  * @v port USB port
1775  */
1776 static void usb_detached ( struct usb_port *port ) {
1777  struct usb_device *usb = port->usb;
1778 
1779  /* Mark port as detached */
1780  port->attached = 0;
1781 
1782  /* Do nothing if we have no USB device */
1783  if ( ! usb )
1784  return;
1785 
1786  /* Unregister USB device */
1787  unregister_usb ( usb );
1788 
1789  /* Free USB device */
1790  free_usb ( usb );
1791 }
1792 
1793 /**
1794  * Handle newly attached or detached USB device
1795  *
1796  * @v port USB port
1797  * @ret rc Return status code
1798  */
1799 static int usb_hotplugged ( struct usb_port *port ) {
1800  struct usb_hub *hub = port->hub;
1801  int rc;
1802 
1803  /* Get current port speed */
1804  if ( ( rc = hub->driver->speed ( hub, port ) ) != 0 ) {
1805  DBGC ( hub, "USB hub %s port %d could not get speed: %s\n",
1806  hub->name, port->address, strerror ( rc ) );
1807  /* Treat as a disconnection */
1808  port->disconnected = 1;
1809  port->speed = USB_SPEED_NONE;
1810  }
1811 
1812  /* Detach device, if applicable */
1813  if ( port->attached && ( port->disconnected || ! port->speed ) )
1814  usb_detached ( port );
1815 
1816  /* Clear any recorded disconnections */
1817  port->disconnected = 0;
1818 
1819  /* Attach device, if applicable */
1820  if ( port->speed && ( ! port->attached ) &&
1821  ( ( rc = usb_attached ( port ) ) != 0 ) )
1822  return rc;
1823 
1824  return 0;
1825 }
1826 
1827 /******************************************************************************
1828  *
1829  * USB process
1830  *
1831  ******************************************************************************
1832  */
1833 
1834 /**
1835  * Report port status change
1836  *
1837  * @v port USB port
1838  */
1839 void usb_port_changed ( struct usb_port *port ) {
1840 
1841  /* Record hub port status change */
1842  list_del ( &port->changed );
1843  list_add_tail ( &port->changed, &usb_changed );
1844 }
1845 
1846 /**
1847  * Handle newly attached or detached USB device
1848  *
1849  */
1850 static void usb_hotplug ( void ) {
1851  struct usb_port *port;
1852 
1853  /* Handle any changed ports, allowing for the fact that the
1854  * port list may change as we perform hotplug actions.
1855  */
1856  while ( ! list_empty ( &usb_changed ) ) {
1857 
1858  /* Get first changed port */
1860  changed );
1861  assert ( port != NULL );
1862 
1863  /* Remove from list of changed ports */
1864  list_del ( &port->changed );
1865  INIT_LIST_HEAD ( &port->changed );
1866 
1867  /* Perform appropriate hotplug action */
1868  usb_hotplugged ( port );
1869  }
1870 }
1871 
1872 /**
1873  * USB process
1874  *
1875  * @v process USB process
1876  */
1877 static void usb_step ( struct process *process __unused ) {
1878  struct usb_bus *bus;
1879  struct usb_endpoint *ep;
1880 
1881  /* Poll all buses */
1882  for_each_usb_bus ( bus )
1883  usb_poll ( bus );
1884 
1885  /* Attempt to reset first halted endpoint in list, if any. We
1886  * do not attempt to process the complete list, since this
1887  * would require extra code to allow for the facts that the
1888  * halted endpoint list may change as we do so, and that
1889  * resetting an endpoint may fail.
1890  */
1891  if ( ( ep = list_first_entry ( &usb_halted, struct usb_endpoint,
1892  halted ) ) != NULL )
1893  usb_endpoint_reset ( ep );
1894 
1895  /* Handle any changed ports */
1896  usb_hotplug();
1897 }
1898 
1899 /** USB process */
1900 PERMANENT_PROCESS ( usb_process, usb_step );
1901 
1902 /******************************************************************************
1903  *
1904  * USB hub
1905  *
1906  ******************************************************************************
1907  */
1908 
1909 /**
1910  * Allocate USB hub
1911  *
1912  * @v bus USB bus
1913  * @v usb Underlying USB device, if any
1914  * @v ports Number of ports
1915  * @v driver Hub driver operations
1916  * @ret hub USB hub, or NULL on allocation failure
1917  */
1918 struct usb_hub * alloc_usb_hub ( struct usb_bus *bus, struct usb_device *usb,
1919  unsigned int ports,
1920  struct usb_hub_driver_operations *driver ) {
1921  struct usb_hub *hub;
1922  struct usb_port *port;
1923  unsigned int i;
1924 
1925  /* Allocate and initialise structure */
1926  hub = zalloc ( sizeof ( *hub ) + ( ports * sizeof ( hub->port[0] ) ) );
1927  if ( ! hub )
1928  return NULL;
1929  hub->name = ( usb ? usb->name : bus->name );
1930  hub->bus = bus;
1931  hub->usb = usb;
1932  if ( usb )
1933  hub->protocol = usb->port->protocol;
1934  hub->ports = ports;
1935  hub->driver = driver;
1936  hub->host = &bus->op->hub;
1937 
1938  /* Initialise port list */
1939  for ( i = 1 ; i <= hub->ports ; i++ ) {
1940  port = usb_port ( hub, i );
1941  port->hub = hub;
1942  port->address = i;
1943  if ( usb )
1944  port->protocol = usb->port->protocol;
1945  INIT_LIST_HEAD ( &port->changed );
1946  }
1947 
1948  return hub;
1949 }
1950 
1951 /**
1952  * Register USB hub
1953  *
1954  * @v hub USB hub
1955  * @ret rc Return status code
1956  */
1957 int register_usb_hub ( struct usb_hub *hub ) {
1958  struct usb_bus *bus = hub->bus;
1959  struct usb_port *port;
1960  unsigned int i;
1961  int rc;
1962 
1963  /* Add to hub list */
1964  list_add_tail ( &hub->list, &bus->hubs );
1965 
1966  /* Open hub (host controller) */
1967  if ( ( rc = hub->host->open ( hub ) ) != 0 ) {
1968  DBGC ( hub, "USB hub %s could not open: %s\n",
1969  hub->name, strerror ( rc ) );
1970  goto err_host_open;
1971  }
1972 
1973  /* Open hub (driver) */
1974  if ( ( rc = hub->driver->open ( hub ) ) != 0 ) {
1975  DBGC ( hub, "USB hub %s could not open: %s\n",
1976  hub->name, strerror ( rc ) );
1977  goto err_driver_open;
1978  }
1979 
1980  /* Delay to allow ports to stabilise */
1982 
1983  /* Mark all ports as changed */
1984  for ( i = 1 ; i <= hub->ports ; i++ ) {
1985  port = usb_port ( hub, i );
1986  usb_port_changed ( port );
1987  }
1988 
1989  /* Some hubs seem to defer reporting device connections until
1990  * their interrupt endpoint is polled for the first time.
1991  * Poll the bus once now in order to pick up any such
1992  * connections.
1993  */
1994  usb_poll ( bus );
1995 
1996  return 0;
1997 
1998  hub->driver->close ( hub );
1999  err_driver_open:
2000  hub->host->close ( hub );
2001  err_host_open:
2002  list_del ( &hub->list );
2003  return rc;
2004 }
2005 
2006 /**
2007  * Unregister USB hub
2008  *
2009  * @v hub USB hub
2010  */
2011 void unregister_usb_hub ( struct usb_hub *hub ) {
2012  struct usb_port *port;
2013  unsigned int i;
2014 
2015  /* Detach all devices */
2016  for ( i = 1 ; i <= hub->ports ; i++ ) {
2017  port = usb_port ( hub, i );
2018  if ( port->attached )
2019  usb_detached ( port );
2020  }
2021 
2022  /* Close hub (driver) */
2023  hub->driver->close ( hub );
2024 
2025  /* Close hub (host controller) */
2026  hub->host->close ( hub );
2027 
2028  /* Cancel any pending port status changes */
2029  for ( i = 1 ; i <= hub->ports ; i++ ) {
2030  port = usb_port ( hub, i );
2031  list_del ( &port->changed );
2032  INIT_LIST_HEAD ( &port->changed );
2033  }
2034 
2035  /* Remove from hub list */
2036  list_del ( &hub->list );
2037 }
2038 
2039 /**
2040  * Free USB hub
2041  *
2042  * @v hub USB hub
2043  */
2044 void free_usb_hub ( struct usb_hub *hub ) {
2045  struct usb_port *port;
2046  unsigned int i;
2047 
2048  /* Sanity checks */
2049  for ( i = 1 ; i <= hub->ports ; i++ ) {
2050  port = usb_port ( hub, i );
2051  assert ( ! port->attached );
2052  assert ( port->usb == NULL );
2053  assert ( list_empty ( &port->changed ) );
2054  }
2055 
2056  /* Free hub */
2057  free ( hub );
2058 }
2059 
2060 /******************************************************************************
2061  *
2062  * USB bus
2063  *
2064  ******************************************************************************
2065  */
2066 
2067 /**
2068  * Allocate USB bus
2069  *
2070  * @v dev Underlying hardware device
2071  * @v ports Number of root hub ports
2072  * @v mtu Largest transfer allowed on the bus
2073  * @v op Host controller operations
2074  * @ret bus USB bus, or NULL on allocation failure
2075  */
2076 struct usb_bus * alloc_usb_bus ( struct device *dev, unsigned int ports,
2077  size_t mtu, struct usb_host_operations *op ) {
2078  struct usb_bus *bus;
2079 
2080  /* Allocate and initialise structure */
2081  bus = zalloc ( sizeof ( *bus ) );
2082  if ( ! bus )
2083  goto err_alloc_bus;
2084  bus->name = dev->name;
2085  bus->dev = dev;
2086  bus->mtu = mtu;
2087  bus->op = op;
2088  INIT_LIST_HEAD ( &bus->devices );
2089  INIT_LIST_HEAD ( &bus->hubs );
2090  bus->host = &bus->op->bus;
2091 
2092  /* Allocate root hub */
2093  bus->hub = alloc_usb_hub ( bus, NULL, ports, &op->root );
2094  if ( ! bus->hub )
2095  goto err_alloc_hub;
2096 
2097  return bus;
2098 
2099  free_usb_hub ( bus->hub );
2100  err_alloc_hub:
2101  free ( bus );
2102  err_alloc_bus:
2103  return NULL;
2104 }
2105 
2106 /**
2107  * Register USB bus
2108  *
2109  * @v bus USB bus
2110  * @ret rc Return status code
2111  */
2112 int register_usb_bus ( struct usb_bus *bus ) {
2113  int rc;
2114 
2115  /* Sanity checks */
2116  assert ( bus->hub != NULL );
2117 
2118  /* Open bus */
2119  if ( ( rc = bus->host->open ( bus ) ) != 0 )
2120  goto err_open;
2121 
2122  /* Add to list of USB buses */
2123  list_add_tail ( &bus->list, &usb_buses );
2124 
2125  /* Register root hub */
2126  if ( ( rc = register_usb_hub ( bus->hub ) ) != 0 )
2127  goto err_register_hub;
2128 
2129  /* Attach any devices already present */
2130  usb_hotplug();
2131 
2132  return 0;
2133 
2134  unregister_usb_hub ( bus->hub );
2135  err_register_hub:
2136  list_del ( &bus->list );
2137  bus->host->close ( bus );
2138  err_open:
2139  return rc;
2140 }
2141 
2142 /**
2143  * Unregister USB bus
2144  *
2145  * @v bus USB bus
2146  */
2147 void unregister_usb_bus ( struct usb_bus *bus ) {
2148 
2149  /* Sanity checks */
2150  assert ( bus->hub != NULL );
2151 
2152  /* Unregister root hub */
2153  unregister_usb_hub ( bus->hub );
2154 
2155  /* Remove from list of USB buses */
2156  list_del ( &bus->list );
2157 
2158  /* Close bus */
2159  bus->host->close ( bus );
2160 
2161  /* Sanity checks */
2162  assert ( list_empty ( &bus->devices ) );
2163  assert ( list_empty ( &bus->hubs ) );
2164 }
2165 
2166 /**
2167  * Free USB bus
2168  *
2169  * @v bus USB bus
2170  */
2171 void free_usb_bus ( struct usb_bus *bus ) {
2172  struct usb_endpoint *ep;
2173  struct usb_port *port;
2174 
2175  /* Sanity checks */
2176  assert ( list_empty ( &bus->devices ) );
2177  assert ( list_empty ( &bus->hubs ) );
2178  list_for_each_entry ( ep, &usb_halted, halted )
2179  assert ( ep->usb->port->hub->bus != bus );
2181  assert ( port->hub->bus != bus );
2182 
2183  /* Free root hub */
2184  free_usb_hub ( bus->hub );
2185 
2186  /* Free bus */
2187  free ( bus );
2188 }
2189 
2190 /**
2191  * Find USB bus by device location
2192  *
2193  * @v bus_type Bus type
2194  * @v location Bus location
2195  * @ret bus USB bus, or NULL
2196  */
2197 struct usb_bus * find_usb_bus_by_location ( unsigned int bus_type,
2198  unsigned int location ) {
2199  struct usb_bus *bus;
2200 
2201  for_each_usb_bus ( bus ) {
2202  if ( ( bus->dev->desc.bus_type == bus_type ) &&
2203  ( bus->dev->desc.location == location ) )
2204  return bus;
2205  }
2206 
2207  return NULL;
2208 }
2209 
2210 /******************************************************************************
2211  *
2212  * USB address assignment
2213  *
2214  ******************************************************************************
2215  */
2216 
2217 /**
2218  * Allocate device address
2219  *
2220  * @v bus USB bus
2221  * @ret address Device address, or negative error
2222  */
2223 int usb_alloc_address ( struct usb_bus *bus ) {
2224  unsigned int address;
2225 
2226  /* Find first free device address */
2227  address = ffsll ( ~bus->addresses );
2228  if ( ! address )
2229  return -ENOENT;
2230 
2231  /* Mark address as used */
2232  bus->addresses |= ( 1ULL << ( address - 1 ) );
2233 
2234  return address;
2235 }
2236 
2237 /**
2238  * Free device address
2239  *
2240  * @v bus USB bus
2241  * @v address Device address
2242  */
2243 void usb_free_address ( struct usb_bus *bus, unsigned int address ) {
2244 
2245  /* Sanity check */
2246  assert ( address > 0 );
2247  assert ( bus->addresses & ( 1ULL << ( address - 1 ) ) );
2248 
2249  /* Mark address as free */
2250  bus->addresses &= ~( 1ULL << ( address - 1 ) );
2251 }
2252 
2253 /******************************************************************************
2254  *
2255  * USB bus topology
2256  *
2257  ******************************************************************************
2258  */
2259 
2260 /**
2261  * Get USB route string
2262  *
2263  * @v usb USB device
2264  * @ret route USB route string
2265  */
2266 unsigned int usb_route_string ( struct usb_device *usb ) {
2267  struct usb_device *parent;
2268  unsigned int route;
2269 
2270  /* Navigate up to root hub, constructing route string as we go */
2271  for ( route = 0 ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
2272  route <<= 4;
2273  route |= ( ( usb->port->address > 0xf ) ?
2274  0xf : usb->port->address );
2275  }
2276 
2277  return route;
2278 }
2279 
2280 /**
2281  * Get USB root hub port
2282  *
2283  * @v usb USB device
2284  * @ret port Root hub port
2285  */
2287  struct usb_device *parent;
2288 
2289  /* Navigate up to root hub */
2290  while ( ( parent = usb->port->hub->usb ) )
2291  usb = parent;
2292 
2293  return usb->port;
2294 }
2295 
2296 /**
2297  * Get USB transaction translator
2298  *
2299  * @v usb USB device
2300  * @ret port Transaction translator port, or NULL
2301  */
2303  struct usb_device *parent;
2304 
2305  /* Navigate up to root hub. If we find a low-speed or
2306  * full-speed device with a higher-speed parent hub, then that
2307  * device's port is the transaction translator.
2308  */
2309  for ( ; ( parent = usb->port->hub->usb ) ; usb = parent ) {
2310  if ( ( usb->speed <= USB_SPEED_FULL ) &&
2311  ( parent->speed > USB_SPEED_FULL ) )
2312  return usb->port;
2313  }
2314 
2315  return NULL;
2316 }
2317 
2318 /* Drag in objects via register_usb_bus() */
2320 
2321 /* Drag in USB configuration */
2322 REQUIRE_OBJECT ( config_usb );
2323 
2324 /* Drag in hub driver */
2325 REQUIRE_OBJECT ( usbhub );
A process.
Definition: process.h:17
#define iob_pull(iobuf, len)
Definition: iobuf.h:102
A USB driver.
Definition: usb.h:1381
#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:1335
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:1850
#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:481
static void usb_detached(struct usb_port *port)
Handle newly detached USB device.
Definition: usb.c:1776
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:883
static int usb_set_configuration(struct usb_device *usb, unsigned int index)
Set USB configuration.
Definition: usb.h:1221
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:682
const char * name
Name.
Definition: usb.h:661
A USB hub.
Definition: usb.h:826
int(* mtu)(struct usb_endpoint *ep)
Update MTU.
Definition: usb.h:453
unsigned int count
Number of interfaces.
Definition: usb.h:650
#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:2197
struct usb_hub_host_operations * host
Host controller operations.
Definition: usb.h:842
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:496
void unregister_usb_hub(struct usb_hub *hub)
Unregister USB hub.
Definition: usb.c:2011
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:2076
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:1309
#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:1741
#define iob_push(iobuf, len)
Definition: iobuf.h:84
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:146
uint64_t address
Base address.
Definition: ena.h:24
uint32_t type
Operating system type.
Definition: ena.h:12
struct usb_descriptor_header header
Descriptor header.
Definition: usb.h:232
union usb_class_descriptor mask
Class mask.
Definition: usb.h:1354
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:1205
#define DBGC(...)
Definition: compiler.h:505
uint16_t vendor
Vendor ID.
Definition: usb.h:644
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:672
#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_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:820
#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: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:804
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:1146
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:754
#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: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:822
int(* clear_tt)(struct usb_hub *hub, struct usb_port *port, struct usb_endpoint *ep)
Clear transaction translator buffer.
Definition: usb.h:911
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: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: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:648
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:871
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:1671
unsigned int address
Device address, if assigned.
Definition: usb.h:718
static int usb_autoconfigure(struct usb_device *usb)
Choose our preferred USB device configuration.
Definition: usb.c:1431
unsigned int speed
Device speed.
Definition: usb.h:714
int(* probe)(struct usb_function *func, struct usb_configuration_descriptor *config)
Probe device.
Definition: usb.h:1401
void usb_port_changed(struct usb_port *port)
Report port status change.
Definition: usb.c:1839
A doubly-linked list entry (or list head)
Definition: list.h:18
int(* open)(struct usb_hub *hub)
Open hub.
Definition: usb.h:862
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
union usb_class_descriptor class
Class.
Definition: usb.h:1352
struct usb_device_id * id
Driver device ID.
Definition: usb.h:676
#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:1056
#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:646
A USB port.
Definition: usb.h:798
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:832
#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:491
A USB interface descriptor.
Definition: usb.h:230
A USB function descriptor.
Definition: usb.h:642
PERMANENT_PROCESS(usb_process, usb_step)
USB process.
void unregister_usb_bus(struct usb_bus *bus)
Unregister USB bus.
Definition: usb.c:2147
struct usb_port * port
USB port.
Definition: usb.h:712
static void usb_probe_all(struct usb_device *usb, struct usb_configuration_descriptor *config)
Probe all USB device drivers.
Definition: usb.c:1300
Assertions.
struct usb_driver * usb_find_driver(struct usb_function_descriptor *desc, struct usb_device_id **id)
Find USB device driver.
Definition: usb.c:1168
#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:844
void(* close)(struct usb_hub *hub)
Close hub.
Definition: usb.h:867
#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:955
struct usb_descriptor_header header
Descriptor header.
Definition: usb.h:316
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:1106
struct usb_class_id class
Class ID.
Definition: usb.h:1387
#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:889
#define USB_CONTROL_MAX_WAIT_MS
Maximum time to wait for a control transaction to complete.
Definition: usb.h:1316
#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:710
unsigned int ports
Number of ports.
Definition: usb.h:836
static void usb_step(struct process *process __unused)
USB process.
Definition: usb.c:1877
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:828
size_t reserve
Refill buffer reserved header length.
Definition: usb.h:421
void(* close)(struct usb_device *usb)
Close device.
Definition: usb.h:753
#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:708
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:733
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:1051
#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:716
unsigned int score
Driver score.
Definition: usb.h:1393
unsigned int usb_route_string(struct usb_device *usb)
Get USB route string.
Definition: usb.c:2266
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: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:2243
#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:1173
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:882
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:406
static int usb_score(struct usb_device *usb, struct usb_configuration_descriptor *config)
Get USB device configuration score.
Definition: usb.c:1204
static void usb_remove(struct usb_function *func)
Remove USB device driver.
Definition: usb.c:1287
struct usb_device_descriptor device
Device descriptor.
Definition: usb.h:720
Standard Endpoint Descriptor USB 2.0 spec, Section 9.6.6.
Definition: Usb.h:163
struct usb_device * usb
USB device.
Definition: usb.h:663
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:633
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:2286
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:1715
A USB descriptor header.
Definition: usb.h:158
void free_usb_hub(struct usb_hub *hub)
Free USB hub.
Definition: usb.c:2044
static void usb_remove_all(struct usb_device *usb)
Remove all device drivers.
Definition: usb.c:1382
struct usb_port * usb_transaction_translator(struct usb_device *usb)
Get USB transaction translator.
Definition: usb.c:2302
#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:896
static int usb_hotplugged(struct usb_port *port)
Handle newly attached or detached USB device.
Definition: usb.c:1799
struct usb_host_operations * op
Host controller operations set.
Definition: usb.h:957
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:1189
static void usb_deconfigure(struct usb_device *usb)
Clear USB device configuration.
Definition: usb.c:1409
#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: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:1538
static int usb_config_descriptor(struct usb_device *usb, unsigned int index, struct usb_configuration_descriptor **config)
Get USB configuration descriptor.
Definition: usb.c:991
unsigned int language
Default language ID (if known)
Definition: usb.h:738
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: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:40
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:735
#define DBGC2(...)
Definition: compiler.h:522
#define USB_ANY_ID
Match-anything ID.
Definition: usb.h:1347
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:2112
struct list_head list
List of hubs.
Definition: usb.h:839
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:802
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:800
unsigned int id_count
Number of entries in ID table.
Definition: usb.h:1385
uint16_t count
Number of entries.
Definition: ena.h:22
#define USB_SET_ADDRESS_RECOVER_DELAY_MS
Set address recovery time.
Definition: usb.h:1323
High speed (480Mbps)
Definition: usb.h:52
void(* remove)(struct usb_function *func)
Remove device.
Definition: usb.h:1408
struct usb_endpoint * ep[32]
Endpoint list.
Definition: usb.h:730
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:1012
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:759
struct device_description desc
Device description.
Definition: device.h:79
USB host controller operations.
Definition: usb.h:1010
#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:1957
#define USB_PORT_DELAY_MS
Time to wait for ports to stabilise.
Definition: usb.h:1332
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:974
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:1412
struct usb_function_descriptor desc
Function descriptor.
Definition: usb.h:665
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:945
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
typeof(acpi_finder=acpi_find)
ACPI table finder.
Definition: acpi.c:45
#define USB_ENDPOINT_MTU(sizes)
USB endpoint MTU.
Definition: usb.h:293
struct usb_endpoint_driver_operations * driver
Driver operations.
Definition: usb.h:416
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:719
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
unsigned int protocol
Hub protocol.
Definition: usb.h:834
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:1156
struct usb_device_host_operations * host
Host controller operations.
Definition: usb.h:725
uint64_t index
Index of the first segment within the content.
Definition: pccrc.h:21
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:667
A USB function.
Definition: usb.h:659
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:830
struct list_head functions
List of functions.
Definition: usb.h:722
static struct usb_device * alloc_usb(struct usb_port *port)
Allocate USB device.
Definition: usb.c:1512
#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:903
#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:669
int usb_alloc_address(struct usb_bus *bus)
Allocate device address.
Definition: usb.c:2223
A USB bus.
Definition: usb.h:951
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:1054
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:2171
uint8_t burst
Maximum burst size.
Definition: usb.h:303
struct usb_class class
Class.
Definition: usb.h:631
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:1249
int(* open)(struct usb_hub *hub)
Open hub.
Definition: usb.h:877
struct usb_device_id * ids
USB ID table.
Definition: usb.h:1383
int(* open)(struct usb_device *usb)
Open device.
Definition: usb.h:748
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: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:1918
struct usb_port port[0]
Port list.
Definition: usb.h:852