iPXE
ehci.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 FILE_SECBOOT ( PERMITTED );
26 
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <errno.h>
32 #include <byteswap.h>
33 #include <ipxe/malloc.h>
34 #include <ipxe/pci.h>
35 #include <ipxe/usb.h>
36 #include <ipxe/init.h>
37 #include "ehci.h"
38 
39 /** @file
40  *
41  * USB Enhanced Host Controller Interface (EHCI) driver
42  *
43  */
44 
45 /**
46  * Construct error code from transfer descriptor status
47  *
48  * @v status Transfer descriptor status
49  * @ret rc Error code
50  *
51  * Bits 2-5 of the status code provide some indication as to the root
52  * cause of the error. We incorporate these into the error code as
53  * reported to usb_complete_err().
54  */
55 #define EIO_STATUS( status ) EUNIQ ( EINFO_EIO, ( ( (status) >> 2 ) & 0xf ) )
56 
57 /******************************************************************************
58  *
59  * Register access
60  *
61  ******************************************************************************
62  */
63 
64 /**
65  * Initialise device
66  *
67  * @v ehci EHCI device
68  * @v regs MMIO registers
69  */
70 static void ehci_init ( struct ehci_device *ehci, void *regs ) {
71  uint32_t hcsparams;
72  uint32_t hccparams;
73  size_t caplength;
74 
75  /* Locate capability and operational registers */
76  ehci->cap = regs;
77  caplength = readb ( ehci->cap + EHCI_CAP_CAPLENGTH );
78  ehci->op = ( ehci->cap + caplength );
79  DBGC2 ( ehci, "EHCI %s cap %08lx op %08lx\n", ehci->name,
80  virt_to_phys ( ehci->cap ), virt_to_phys ( ehci->op ) );
81 
82  /* Read structural parameters */
83  hcsparams = readl ( ehci->cap + EHCI_CAP_HCSPARAMS );
84  ehci->ports = EHCI_HCSPARAMS_PORTS ( hcsparams );
85  DBGC ( ehci, "EHCI %s has %d ports\n", ehci->name, ehci->ports );
86 
87  /* Read capability parameters 1 */
88  hccparams = readl ( ehci->cap + EHCI_CAP_HCCPARAMS );
89  ehci->addr64 = EHCI_HCCPARAMS_ADDR64 ( hccparams );
90  ehci->flsize = ( EHCI_HCCPARAMS_FLSIZE ( hccparams ) ?
92  ehci->eecp = EHCI_HCCPARAMS_EECP ( hccparams );
93  DBGC2 ( ehci, "EHCI %s %d-bit flsize %d\n", ehci->name,
94  ( ehci->addr64 ? 64 : 32 ), ehci->flsize );
95 }
96 
97 /**
98  * Find extended capability
99  *
100  * @v ehci EHCI device
101  * @v pci PCI device
102  * @v id Capability ID
103  * @v offset Offset to previous extended capability instance, or zero
104  * @ret offset Offset to extended capability, or zero if not found
105  */
106 static unsigned int ehci_extended_capability ( struct ehci_device *ehci,
107  struct pci_device *pci,
108  unsigned int id,
109  unsigned int offset ) {
110  uint32_t eecp;
111 
112  /* Locate the extended capability */
113  while ( 1 ) {
114 
115  /* Locate first or next capability as applicable */
116  if ( offset ) {
117  pci_read_config_dword ( pci, offset, &eecp );
118  offset = EHCI_EECP_NEXT ( eecp );
119  } else {
120  offset = ehci->eecp;
121  }
122  if ( ! offset )
123  return 0;
124 
125  /* Check if this is the requested capability */
126  pci_read_config_dword ( pci, offset, &eecp );
127  if ( EHCI_EECP_ID ( eecp ) == id )
128  return offset;
129  }
130 }
131 
132 /**
133  * Calculate buffer alignment
134  *
135  * @v len Length
136  * @ret align Buffer alignment
137  *
138  * Determine alignment required for a buffer which must be aligned to
139  * at least EHCI_MIN_ALIGN and which must not cross a page boundary.
140  */
141 static inline size_t ehci_align ( size_t len ) {
142  size_t align;
143 
144  /* Align to own length (rounded up to a power of two) */
145  align = ( 1 << fls ( len - 1 ) );
146 
147  /* Round up to EHCI_MIN_ALIGN if needed */
148  if ( align < EHCI_MIN_ALIGN )
149  align = EHCI_MIN_ALIGN;
150 
151  return align;
152 }
153 
154 /**
155  * Check control data structure reachability
156  *
157  * @v ehci EHCI device
158  * @v ptr Data structure pointer
159  * @ret rc Return status code
160  */
161 static int ehci_ctrl_reachable ( struct ehci_device *ehci, void *ptr ) {
162  physaddr_t phys = virt_to_phys ( ptr );
164 
165  /* Always reachable in a 32-bit build */
166  if ( sizeof ( physaddr_t ) <= sizeof ( uint32_t ) )
167  return 0;
168 
169  /* Reachable only if control segment matches in a 64-bit build */
170  segment = ( ( ( uint64_t ) phys ) >> 32 );
171  if ( segment == ehci->ctrldssegment )
172  return 0;
173 
174  return -ENOTSUP;
175 }
176 
177 /******************************************************************************
178  *
179  * Diagnostics
180  *
181  ******************************************************************************
182  */
183 
184 /**
185  * Dump host controller registers
186  *
187  * @v ehci EHCI device
188  */
189 static __unused void ehci_dump ( struct ehci_device *ehci ) {
190  uint8_t caplength;
191  uint16_t hciversion;
192  uint32_t hcsparams;
193  uint32_t hccparams;
194  uint32_t usbcmd;
195  uint32_t usbsts;
196  uint32_t usbintr;
197  uint32_t frindex;
198  uint32_t ctrldssegment;
199  uint32_t periodiclistbase;
200  uint32_t asynclistaddr;
201  uint32_t configflag;
202 
203  /* Do nothing unless debugging is enabled */
204  if ( ! DBG_LOG )
205  return;
206 
207  /* Dump capability registers */
208  caplength = readb ( ehci->cap + EHCI_CAP_CAPLENGTH );
209  hciversion = readw ( ehci->cap + EHCI_CAP_HCIVERSION );
210  hcsparams = readl ( ehci->cap + EHCI_CAP_HCSPARAMS );
211  hccparams = readl ( ehci->cap + EHCI_CAP_HCCPARAMS );
212  DBGC ( ehci, "EHCI %s caplen %02x hciversion %04x hcsparams %08x "
213  "hccparams %08x\n", ehci->name, caplength, hciversion,
214  hcsparams, hccparams );
215 
216  /* Dump operational registers */
217  usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
218  usbsts = readl ( ehci->op + EHCI_OP_USBSTS );
219  usbintr = readl ( ehci->op + EHCI_OP_USBINTR );
220  frindex = readl ( ehci->op + EHCI_OP_FRINDEX );
221  ctrldssegment = readl ( ehci->op + EHCI_OP_CTRLDSSEGMENT );
222  periodiclistbase = readl ( ehci->op + EHCI_OP_PERIODICLISTBASE );
223  asynclistaddr = readl ( ehci->op + EHCI_OP_ASYNCLISTADDR );
224  configflag = readl ( ehci->op + EHCI_OP_CONFIGFLAG );
225  DBGC ( ehci, "EHCI %s usbcmd %08x usbsts %08x usbint %08x frindx "
226  "%08x\n", ehci->name, usbcmd, usbsts, usbintr, frindex );
227  DBGC ( ehci, "EHCI %s ctrlds %08x period %08x asyncl %08x cfgflg "
228  "%08x\n", ehci->name, ctrldssegment, periodiclistbase,
229  asynclistaddr, configflag );
230 }
231 
232 /******************************************************************************
233  *
234  * USB legacy support
235  *
236  ******************************************************************************
237  */
238 
239 /** Prevent the release of ownership back to BIOS */
241 
242 /**
243  * Initialise USB legacy support
244  *
245  * @v ehci EHCI device
246  * @v pci PCI device
247  */
248 static void ehci_legacy_init ( struct ehci_device *ehci,
249  struct pci_device *pci ) {
250  unsigned int legacy;
251  uint8_t bios;
252 
253  /* Locate USB legacy support capability (if present) */
254  legacy = ehci_extended_capability ( ehci, pci, EHCI_EECP_ID_LEGACY, 0 );
255  if ( ! legacy ) {
256  /* Not an error; capability may not be present */
257  DBGC ( ehci, "EHCI %s has no USB legacy support capability\n",
258  ehci->name );
259  return;
260  }
261 
262  /* Check if legacy USB support is enabled */
263  pci_read_config_byte ( pci, ( legacy + EHCI_USBLEGSUP_BIOS ), &bios );
264  if ( ! ( bios & EHCI_USBLEGSUP_BIOS_OWNED ) ) {
265  /* Not an error; already owned by OS */
266  DBGC ( ehci, "EHCI %s USB legacy support already disabled\n",
267  ehci->name );
268  return;
269  }
270 
271  /* Record presence of USB legacy support capability */
272  ehci->legacy = legacy;
273 }
274 
275 /**
276  * Claim ownership from BIOS
277  *
278  * @v ehci EHCI device
279  * @v pci PCI device
280  */
281 static void ehci_legacy_claim ( struct ehci_device *ehci,
282  struct pci_device *pci ) {
283  unsigned int legacy = ehci->legacy;
284  uint32_t ctlsts;
285  uint8_t bios;
286  unsigned int i;
287 
288  /* Do nothing unless legacy support capability is present */
289  if ( ! legacy )
290  return;
291 
292  /* Dump original SMI usage */
293  pci_read_config_dword ( pci, ( legacy + EHCI_USBLEGSUP_CTLSTS ),
294  &ctlsts );
295  if ( ctlsts ) {
296  DBGC ( ehci, "EHCI %s BIOS using SMIs: %08x\n",
297  ehci->name, ctlsts );
298  }
299 
300  /* Claim ownership */
301  pci_write_config_byte ( pci, ( legacy + EHCI_USBLEGSUP_OS ),
303 
304  /* Wait for BIOS to release ownership */
305  for ( i = 0 ; i < EHCI_USBLEGSUP_MAX_WAIT_MS ; i++ ) {
306 
307  /* Check if BIOS has released ownership */
308  pci_read_config_byte ( pci, ( legacy + EHCI_USBLEGSUP_BIOS ),
309  &bios );
310  if ( ! ( bios & EHCI_USBLEGSUP_BIOS_OWNED ) ) {
311  DBGC ( ehci, "EHCI %s claimed ownership from BIOS\n",
312  ehci->name );
313  pci_read_config_dword ( pci, ( legacy +
315  &ctlsts );
316  if ( ctlsts ) {
317  DBGC ( ehci, "EHCI %s warning: BIOS retained "
318  "SMIs: %08x\n", ehci->name, ctlsts );
319  }
320  return;
321  }
322 
323  /* Delay */
324  mdelay ( 1 );
325  }
326 
327  /* BIOS did not release ownership. Claim it forcibly by
328  * disabling all SMIs.
329  */
330  DBGC ( ehci, "EHCI %s could not claim ownership from BIOS: forcibly "
331  "disabling SMIs\n", ehci->name );
332  pci_write_config_dword ( pci, ( legacy + EHCI_USBLEGSUP_CTLSTS ), 0 );
333 }
334 
335 /**
336  * Release ownership back to BIOS
337  *
338  * @v ehci EHCI device
339  * @v pci PCI device
340  */
341 static void ehci_legacy_release ( struct ehci_device *ehci,
342  struct pci_device *pci ) {
343  unsigned int legacy = ehci->legacy;
344  uint32_t ctlsts;
345 
346  /* Do nothing unless legacy support capability is present */
347  if ( ! legacy )
348  return;
349 
350  /* Do nothing if releasing ownership is prevented */
352  DBGC ( ehci, "EHCI %s not releasing ownership to BIOS\n",
353  ehci->name );
354  return;
355  }
356 
357  /* Release ownership */
358  pci_write_config_byte ( pci, ( legacy + EHCI_USBLEGSUP_OS ), 0 );
359  DBGC ( ehci, "EHCI %s released ownership to BIOS\n", ehci->name );
360 
361  /* Dump restored SMI usage */
362  pci_read_config_dword ( pci, ( legacy + EHCI_USBLEGSUP_CTLSTS ),
363  &ctlsts );
364  DBGC ( ehci, "EHCI %s BIOS reclaimed SMIs: %08x\n",
365  ehci->name, ctlsts );
366 }
367 
368 /******************************************************************************
369  *
370  * Companion controllers
371  *
372  ******************************************************************************
373  */
374 
375 /**
376  * Poll child companion controllers
377  *
378  * @v ehci EHCI device
379  */
380 static void ehci_poll_companions ( struct ehci_device *ehci ) {
381  struct usb_bus *bus;
382  struct device_description *desc;
383 
384  /* Poll any USB buses belonging to child companion controllers */
385  for_each_usb_bus ( bus ) {
386 
387  /* Get underlying devices description */
388  desc = &bus->dev->desc;
389 
390  /* Skip buses that are not PCI devices */
391  if ( desc->bus_type != BUS_TYPE_PCI )
392  continue;
393 
394  /* Skip buses that are not part of the same PCI device */
395  if ( PCI_FIRST_FUNC ( desc->location ) !=
396  PCI_FIRST_FUNC ( ehci->bus->dev->desc.location ) )
397  continue;
398 
399  /* Skip buses that are not UHCI or OHCI PCI devices */
400  if ( ( desc->class != PCI_CLASS ( PCI_CLASS_SERIAL,
403  ( desc->class != PCI_CLASS ( PCI_CLASS_SERIAL,
406  continue;
407 
408  /* Poll child companion controller bus */
409  DBGC2 ( ehci, "EHCI %s polling companion %s\n",
410  ehci->name, bus->name );
411  usb_poll ( bus );
412  }
413 }
414 
415 /**
416  * Locate EHCI companion controller
417  *
418  * @v pci PCI device
419  * @ret busdevfn EHCI companion controller bus:dev.fn (if any)
420  */
421 unsigned int ehci_companion ( struct pci_device *pci ) {
422  struct pci_device tmp;
423  unsigned int busdevfn;
424  int rc;
425 
426  /* Look for an EHCI function on the same PCI device */
427  busdevfn = pci->busdevfn;
428  while ( ++busdevfn <= PCI_LAST_FUNC ( pci->busdevfn ) ) {
429  pci_init ( &tmp, busdevfn );
430  if ( ( rc = pci_read_config ( &tmp ) ) != 0 )
431  continue;
432  if ( tmp.class == PCI_CLASS ( PCI_CLASS_SERIAL,
435  return busdevfn;
436  }
437 
438  return 0;
439 }
440 
441 /******************************************************************************
442  *
443  * Run / stop / reset
444  *
445  ******************************************************************************
446  */
447 
448 /**
449  * Start EHCI device
450  *
451  * @v ehci EHCI device
452  */
453 static void ehci_run ( struct ehci_device *ehci ) {
454  uint32_t usbcmd;
455 
456  /* Set run/stop bit */
457  usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
458  usbcmd &= ~EHCI_USBCMD_FLSIZE_MASK;
459  usbcmd |= ( EHCI_USBCMD_RUN | EHCI_USBCMD_FLSIZE ( ehci->flsize ) |
461  writel ( usbcmd, ehci->op + EHCI_OP_USBCMD );
462 }
463 
464 /**
465  * Stop EHCI device
466  *
467  * @v ehci EHCI device
468  * @ret rc Return status code
469  */
470 static int ehci_stop ( struct ehci_device *ehci ) {
471  uint32_t usbcmd;
472  uint32_t usbsts;
473  unsigned int i;
474 
475  /* Clear run/stop bit */
476  usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
477  usbcmd &= ~( EHCI_USBCMD_RUN | EHCI_USBCMD_PERIODIC |
479  writel ( usbcmd, ehci->op + EHCI_OP_USBCMD );
480 
481  /* Wait for device to stop */
482  for ( i = 0 ; i < EHCI_STOP_MAX_WAIT_MS ; i++ ) {
483 
484  /* Check if device is stopped */
485  usbsts = readl ( ehci->op + EHCI_OP_USBSTS );
486  if ( usbsts & EHCI_USBSTS_HCH )
487  return 0;
488 
489  /* Delay */
490  mdelay ( 1 );
491  }
492 
493  DBGC ( ehci, "EHCI %s timed out waiting for stop\n", ehci->name );
494  return -ETIMEDOUT;
495 }
496 
497 /**
498  * Reset EHCI device
499  *
500  * @v ehci EHCI device
501  * @ret rc Return status code
502  */
503 static int ehci_reset ( struct ehci_device *ehci ) {
504  uint32_t usbcmd;
505  unsigned int i;
506  int rc;
507 
508  /* The EHCI specification states that resetting a running
509  * device may result in undefined behaviour, so try stopping
510  * it first.
511  */
512  if ( ( rc = ehci_stop ( ehci ) ) != 0 ) {
513  /* Ignore errors and attempt to reset the device anyway */
514  }
515 
516  /* Reset device */
518 
519  /* Wait for reset to complete */
520  for ( i = 0 ; i < EHCI_RESET_MAX_WAIT_MS ; i++ ) {
521 
522  /* Check if reset is complete */
523  usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
524  if ( ! ( usbcmd & EHCI_USBCMD_HCRST ) )
525  return 0;
526 
527  /* Delay */
528  mdelay ( 1 );
529  }
530 
531  DBGC ( ehci, "EHCI %s timed out waiting for reset\n", ehci->name );
532  return -ETIMEDOUT;
533 }
534 
535 /******************************************************************************
536  *
537  * Transfer descriptor rings
538  *
539  ******************************************************************************
540  */
541 
542 /**
543  * Allocate transfer descriptor ring
544  *
545  * @v ehci EHCI device
546  * @v ring Transfer descriptor ring
547  * @ret rc Return status code
548  */
549 static int ehci_ring_alloc ( struct ehci_device *ehci,
550  struct ehci_ring *ring ) {
553  unsigned int i;
554  size_t len;
555  uint32_t link;
556  int rc;
557 
558  /* Initialise structure */
559  memset ( ring, 0, sizeof ( *ring ) );
560 
561  /* Allocate I/O buffers */
562  ring->iobuf = zalloc ( EHCI_RING_COUNT * sizeof ( ring->iobuf[0] ) );
563  if ( ! ring->iobuf ) {
564  rc = -ENOMEM;
565  goto err_alloc_iobuf;
566  }
567 
568  /* Allocate queue head */
569  ring->head = malloc_phys ( sizeof ( *ring->head ),
570  ehci_align ( sizeof ( *ring->head ) ) );
571  if ( ! ring->head ) {
572  rc = -ENOMEM;
573  goto err_alloc_queue;
574  }
575  if ( ( rc = ehci_ctrl_reachable ( ehci, ring->head ) ) != 0 ) {
576  DBGC ( ehci, "EHCI %s queue head unreachable\n", ehci->name );
577  goto err_unreachable_queue;
578  }
579  memset ( ring->head, 0, sizeof ( *ring->head ) );
580 
581  /* Allocate transfer descriptors */
582  len = ( EHCI_RING_COUNT * sizeof ( ring->desc[0] ) );
583  ring->desc = malloc_phys ( len, sizeof ( ring->desc[0] ) );
584  if ( ! ring->desc ) {
585  rc = -ENOMEM;
586  goto err_alloc_desc;
587  }
588  memset ( ring->desc, 0, len );
589 
590  /* Initialise transfer descriptors */
591  for ( i = 0 ; i < EHCI_RING_COUNT ; i++ ) {
592  desc = &ring->desc[i];
593  if ( ( rc = ehci_ctrl_reachable ( ehci, desc ) ) != 0 ) {
594  DBGC ( ehci, "EHCI %s descriptor unreachable\n",
595  ehci->name );
596  goto err_unreachable_desc;
597  }
598  next = &ring->desc[ ( i + 1 ) % EHCI_RING_COUNT ];
599  link = virt_to_phys ( next );
600  desc->next = cpu_to_le32 ( link );
601  desc->alt = cpu_to_le32 ( link );
602  }
603 
604  /* Initialise queue head */
605  link = virt_to_phys ( &ring->desc[0] );
606  ring->head->cache.next = cpu_to_le32 ( link );
607 
608  return 0;
609 
610  err_unreachable_desc:
611  free_phys ( ring->desc, len );
612  err_alloc_desc:
613  err_unreachable_queue:
614  free_phys ( ring->head, sizeof ( *ring->head ) );
615  err_alloc_queue:
616  free ( ring->iobuf );
617  err_alloc_iobuf:
618  return rc;
619 }
620 
621 /**
622  * Free transfer descriptor ring
623  *
624  * @v ring Transfer descriptor ring
625  */
626 static void ehci_ring_free ( struct ehci_ring *ring ) {
627  unsigned int i;
628 
629  /* Sanity checks */
630  assert ( ehci_ring_fill ( ring ) == 0 );
631  for ( i = 0 ; i < EHCI_RING_COUNT ; i++ )
632  assert ( ring->iobuf[i] == NULL );
633 
634  /* Free transfer descriptors */
635  free_phys ( ring->desc, ( EHCI_RING_COUNT *
636  sizeof ( ring->desc[0] ) ) );
637 
638  /* Free queue head */
639  free_phys ( ring->head, sizeof ( *ring->head ) );
640 
641  /* Free I/O buffers */
642  free ( ring->iobuf );
643 }
644 
645 /**
646  * Enqueue transfer descriptors
647  *
648  * @v ehci EHCI device
649  * @v ring Transfer descriptor ring
650  * @v iobuf I/O buffer
651  * @v xfers Transfers
652  * @v count Number of transfers
653  * @ret rc Return status code
654  */
655 static int ehci_enqueue ( struct ehci_device *ehci, struct ehci_ring *ring,
656  struct io_buffer *iobuf,
657  const struct ehci_transfer *xfer,
658  unsigned int count ) {
661  void *data;
662  size_t len;
663  size_t offset;
664  size_t frag_len;
665  unsigned int toggle;
666  unsigned int index;
667  unsigned int i;
668 
669  /* Sanity check */
670  assert ( iobuf != NULL );
671  assert ( count > 0 );
672 
673  /* Fail if ring does not have sufficient space */
674  if ( ehci_ring_remaining ( ring ) < count )
675  return -ENOBUFS;
676 
677  /* Fail if any portion is unreachable */
678  for ( i = 0 ; i < count ; i++ ) {
679  if ( ! xfer[i].len )
680  continue;
681  phys = ( virt_to_phys ( xfer[i].data ) + xfer[i].len - 1 );
682  if ( ( phys > 0xffffffffUL ) && ( ! ehci->addr64 ) )
683  return -ENOTSUP;
684  }
685 
686  /* Enqueue each transfer, recording the I/O buffer with the last */
687  for ( ; count ; ring->prod++, xfer++ ) {
688 
689  /* Populate descriptor header */
690  index = ( ring->prod % EHCI_RING_COUNT );
691  desc = &ring->desc[index];
692  toggle = ( xfer->flags & EHCI_FL_TOGGLE );
693  assert ( xfer->len <= EHCI_LEN_MASK );
695  desc->len = cpu_to_le16 ( xfer->len | toggle );
696  desc->flags = ( xfer->flags | EHCI_FL_CERR_MAX );
697 
698  /* Populate buffer pointers */
699  data = xfer->data;
700  len = xfer->len;
701  for ( i = 0 ; len ; i++ ) {
702 
703  /* Calculate length of this fragment */
704  phys = virt_to_phys ( data );
705  offset = ( phys & ( EHCI_PAGE_ALIGN - 1 ) );
706  frag_len = ( EHCI_PAGE_ALIGN - offset );
707  if ( frag_len > len )
708  frag_len = len;
709 
710  /* Sanity checks */
711  assert ( ( i == 0 ) || ( offset == 0 ) );
712  assert ( i < ( sizeof ( desc->low ) /
713  sizeof ( desc->low[0] ) ) );
714 
715  /* Populate buffer pointer */
716  desc->low[i] = cpu_to_le32 ( phys );
717  if ( sizeof ( physaddr_t ) > sizeof ( uint32_t ) ) {
718  desc->high[i] =
719  cpu_to_le32 ( ((uint64_t) phys) >> 32 );
720  }
721 
722  /* Move to next fragment */
723  data += frag_len;
724  len -= frag_len;
725  }
726 
727  /* Ensure everything is valid before activating descriptor */
728  wmb();
729  desc->status = EHCI_STATUS_ACTIVE;
730 
731  /* Record I/O buffer against last ring index */
732  if ( --count == 0 )
733  ring->iobuf[index] = iobuf;
734  }
735 
736  return 0;
737 }
738 
739 /**
740  * Dequeue a transfer descriptor
741  *
742  * @v ring Transfer descriptor ring
743  * @ret iobuf I/O buffer (or NULL)
744  */
745 static struct io_buffer * ehci_dequeue ( struct ehci_ring *ring ) {
747  struct io_buffer *iobuf;
748  unsigned int index = ( ring->cons % EHCI_RING_COUNT );
749 
750  /* Sanity check */
751  assert ( ehci_ring_fill ( ring ) > 0 );
752 
753  /* Mark descriptor as inactive (and not halted) */
754  desc = &ring->desc[index];
755  desc->status = 0;
756 
757  /* Retrieve I/O buffer */
758  iobuf = ring->iobuf[index];
759  ring->iobuf[index] = NULL;
760 
761  /* Update consumer counter */
762  ring->cons++;
763 
764  return iobuf;
765 }
766 
767 /******************************************************************************
768  *
769  * Schedule management
770  *
771  ******************************************************************************
772  */
773 
774 /**
775  * Get link value for a queue head
776  *
777  * @v queue Queue head
778  * @ret link Link value
779  */
780 static inline uint32_t ehci_link_qh ( struct ehci_queue_head *queue ) {
781 
782  return ( virt_to_phys ( queue ) | EHCI_LINK_TYPE_QH );
783 }
784 
785 /**
786  * (Re)build asynchronous schedule
787  *
788  * @v ehci EHCI device
789  */
790 static void ehci_async_schedule ( struct ehci_device *ehci ) {
791  struct ehci_endpoint *endpoint;
792  struct ehci_queue_head *queue;
793  uint32_t link;
794 
795  /* Build schedule in reverse order of execution. Provided
796  * that we only ever add or remove single endpoints, this can
797  * safely run concurrently with hardware execution of the
798  * schedule.
799  */
800  link = ehci_link_qh ( ehci->head );
801  list_for_each_entry_reverse ( endpoint, &ehci->async, schedule ) {
802  queue = endpoint->ring.head;
803  queue->link = cpu_to_le32 ( link );
804  wmb();
805  link = ehci_link_qh ( queue );
806  }
807  ehci->head->link = cpu_to_le32 ( link );
808  wmb();
809 }
810 
811 /**
812  * Add endpoint to asynchronous schedule
813  *
814  * @v endpoint Endpoint
815  */
816 static void ehci_async_add ( struct ehci_endpoint *endpoint ) {
817  struct ehci_device *ehci = endpoint->ehci;
818 
819  /* Add to end of schedule */
820  list_add_tail ( &endpoint->schedule, &ehci->async );
821 
822  /* Rebuild schedule */
823  ehci_async_schedule ( ehci );
824 }
825 
826 /**
827  * Remove endpoint from asynchronous schedule
828  *
829  * @v endpoint Endpoint
830  * @ret rc Return status code
831  */
832 static int ehci_async_del ( struct ehci_endpoint *endpoint ) {
833  struct ehci_device *ehci = endpoint->ehci;
834  uint32_t usbcmd;
835  uint32_t usbsts;
836  unsigned int i;
837 
838  /* Remove from schedule */
839  list_check_contains_entry ( endpoint, &ehci->async, schedule );
840  list_del ( &endpoint->schedule );
841 
842  /* Rebuild schedule */
843  ehci_async_schedule ( ehci );
844 
845  /* Request notification when asynchronous schedule advances */
846  usbcmd = readl ( ehci->op + EHCI_OP_USBCMD );
847  usbcmd |= EHCI_USBCMD_ASYNC_ADVANCE;
848  writel ( usbcmd, ehci->op + EHCI_OP_USBCMD );
849 
850  /* Wait for asynchronous schedule to advance */
851  for ( i = 0 ; i < EHCI_ASYNC_ADVANCE_MAX_WAIT_MS ; i++ ) {
852 
853  /* Check for asynchronous schedule advancing */
854  usbsts = readl ( ehci->op + EHCI_OP_USBSTS );
855  if ( usbsts & EHCI_USBSTS_ASYNC_ADVANCE ) {
856  usbsts &= ~EHCI_USBSTS_CHANGE;
857  usbsts |= EHCI_USBSTS_ASYNC_ADVANCE;
858  writel ( usbsts, ehci->op + EHCI_OP_USBSTS );
859  return 0;
860  }
861 
862  /* Delay */
863  mdelay ( 1 );
864  }
865 
866  /* Bad things will probably happen now */
867  DBGC ( ehci, "EHCI %s timed out waiting for asynchronous schedule "
868  "to advance\n", ehci->name );
869  return -ETIMEDOUT;
870 }
871 
872 /**
873  * (Re)build periodic schedule
874  *
875  * @v ehci EHCI device
876  */
877 static void ehci_periodic_schedule ( struct ehci_device *ehci ) {
878  struct ehci_endpoint *endpoint;
879  struct ehci_queue_head *queue;
880  uint32_t link;
881  unsigned int frames;
882  unsigned int max_interval;
883  unsigned int i;
884 
885  /* Build schedule in reverse order of execution. Provided
886  * that we only ever add or remove single endpoints, this can
887  * safely run concurrently with hardware execution of the
888  * schedule.
889  */
890  DBGCP ( ehci, "EHCI %s periodic schedule: ", ehci->name );
892  list_for_each_entry_reverse ( endpoint, &ehci->periodic, schedule ) {
893  queue = endpoint->ring.head;
894  queue->link = cpu_to_le32 ( link );
895  wmb();
896  DBGCP ( ehci, "%s%d",
897  ( ( link == EHCI_LINK_TERMINATE ) ? "" : "<-" ),
898  endpoint->ep->interval );
899  link = ehci_link_qh ( queue );
900  }
901  DBGCP ( ehci, "\n" );
902 
903  /* Populate periodic frame list */
904  DBGCP ( ehci, "EHCI %s periodic frame list:", ehci->name );
905  frames = EHCI_PERIODIC_FRAMES ( ehci->flsize );
906  for ( i = 0 ; i < frames ; i++ ) {
907 
908  /* Calculate maximum interval (in microframes) which
909  * may appear as part of this frame list.
910  */
911  if ( i == 0 ) {
912  /* Start of list: include all endpoints */
913  max_interval = -1U;
914  } else {
915  /* Calculate highest power-of-two frame interval */
916  max_interval = ( 1 << ( ffs ( i ) - 1 ) );
917  /* Convert to microframes */
918  max_interval <<= 3;
919  /* Round up to nearest 2^n-1 */
920  max_interval = ( ( max_interval << 1 ) - 1 );
921  }
922 
923  /* Find first endpoint in schedule satisfying this
924  * maximum interval constraint.
925  */
927  list_for_each_entry ( endpoint, &ehci->periodic, schedule ) {
928  if ( endpoint->ep->interval <= max_interval ) {
929  queue = endpoint->ring.head;
930  link = ehci_link_qh ( queue );
931  DBGCP ( ehci, " %d:%d",
932  i, endpoint->ep->interval );
933  break;
934  }
935  }
936  ehci->frame[i].link = cpu_to_le32 ( link );
937  }
938  wmb();
939  DBGCP ( ehci, "\n" );
940 }
941 
942 /**
943  * Add endpoint to periodic schedule
944  *
945  * @v endpoint Endpoint
946  */
947 static void ehci_periodic_add ( struct ehci_endpoint *endpoint ) {
948  struct ehci_device *ehci = endpoint->ehci;
949  struct ehci_endpoint *before;
950  unsigned int interval = endpoint->ep->interval;
951 
952  /* Find first endpoint with a smaller interval */
954  if ( before->ep->interval < interval )
955  break;
956  }
957  list_add_tail ( &endpoint->schedule, &before->schedule );
958 
959  /* Rebuild schedule */
961 }
962 
963 /**
964  * Remove endpoint from periodic schedule
965  *
966  * @v endpoint Endpoint
967  * @ret rc Return status code
968  */
969 static int ehci_periodic_del ( struct ehci_endpoint *endpoint ) {
970  struct ehci_device *ehci = endpoint->ehci;
971 
972  /* Remove from schedule */
973  list_check_contains_entry ( endpoint, &ehci->periodic, schedule );
974  list_del ( &endpoint->schedule );
975 
976  /* Rebuild schedule */
977  ehci_periodic_schedule ( ehci );
978 
979  /* Delay for a whole USB frame (with a 100% safety margin) */
980  mdelay ( 2 );
981 
982  return 0;
983 }
984 
985 /**
986  * Add endpoint to appropriate schedule
987  *
988  * @v endpoint Endpoint
989  */
990 static void ehci_schedule_add ( struct ehci_endpoint *endpoint ) {
991  struct usb_endpoint *ep = endpoint->ep;
992  unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
993 
995  ehci_periodic_add ( endpoint );
996  } else {
997  ehci_async_add ( endpoint );
998  }
999 }
1000 
1001 /**
1002  * Remove endpoint from appropriate schedule
1003  *
1004  * @v endpoint Endpoint
1005  * @ret rc Return status code
1006  */
1007 static int ehci_schedule_del ( struct ehci_endpoint *endpoint ) {
1008  struct usb_endpoint *ep = endpoint->ep;
1009  unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
1010 
1011  if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
1012  return ehci_periodic_del ( endpoint );
1013  } else {
1014  return ehci_async_del ( endpoint );
1015  }
1016 }
1017 
1018 /******************************************************************************
1019  *
1020  * Endpoint operations
1021  *
1022  ******************************************************************************
1023  */
1024 
1025 /**
1026  * Determine endpoint characteristics
1027  *
1028  * @v ep USB endpoint
1029  * @ret chr Endpoint characteristics
1030  */
1032  struct usb_device *usb = ep->usb;
1033  unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
1034  uint32_t chr;
1035 
1036  /* Determine basic characteristics */
1037  chr = ( EHCI_CHR_ADDRESS ( usb->address ) |
1039  EHCI_CHR_MAX_LEN ( ep->mtu ) );
1040 
1041  /* Control endpoints require manual control of the data toggle */
1043  chr |= EHCI_CHR_TOGGLE;
1044 
1045  /* Determine endpoint speed */
1046  if ( usb->speed == USB_SPEED_HIGH ) {
1047  chr |= EHCI_CHR_EPS_HIGH;
1048  } else {
1049  if ( usb->speed == USB_SPEED_FULL ) {
1050  chr |= EHCI_CHR_EPS_FULL;
1051  } else {
1052  chr |= EHCI_CHR_EPS_LOW;
1053  }
1055  chr |= EHCI_CHR_CONTROL;
1056  }
1057 
1058  return chr;
1059 }
1060 
1061 /**
1062  * Determine endpoint capabilities
1063  *
1064  * @v ep USB endpoint
1065  * @ret cap Endpoint capabilities
1066  */
1068  struct usb_device *usb = ep->usb;
1069  struct usb_port *tt = usb_transaction_translator ( usb );
1070  unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
1071  uint32_t cap;
1072  unsigned int i;
1073 
1074  /* Determine basic capabilities */
1075  cap = EHCI_CAP_MULT ( ep->burst + 1 );
1076 
1077  /* Determine interrupt schedule mask, if applicable */
1078  if ( ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) &&
1079  ( ( ep->interval != 0 ) /* avoid infinite loop */ ) ) {
1080  for ( i = 0 ; i < 8 /* microframes per frame */ ;
1081  i += ep->interval ) {
1082  cap |= EHCI_CAP_INTR_SCHED ( i );
1083  }
1084  }
1085 
1086  /* Set transaction translator hub address and port, if applicable */
1087  if ( tt ) {
1088  assert ( tt->hub->usb );
1089  cap |= ( EHCI_CAP_TT_HUB ( tt->hub->usb->address ) |
1090  EHCI_CAP_TT_PORT ( tt->address ) );
1093  }
1094 
1095  return cap;
1096 }
1097 
1098 /**
1099  * Update endpoint characteristics and capabilities
1100  *
1101  * @v ep USB endpoint
1102  */
1103 static void ehci_endpoint_update ( struct usb_endpoint *ep ) {
1104  struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
1105  struct ehci_queue_head *head;
1106 
1107  /* Update queue characteristics and capabilities */
1108  head = endpoint->ring.head;
1110  head->cap = cpu_to_le32 ( ehci_endpoint_capabilities ( ep ) );
1111 }
1112 
1113 /**
1114  * Open endpoint
1115  *
1116  * @v ep USB endpoint
1117  * @ret rc Return status code
1118  */
1119 static int ehci_endpoint_open ( struct usb_endpoint *ep ) {
1120  struct usb_device *usb = ep->usb;
1121  struct ehci_device *ehci = usb_get_hostdata ( usb );
1122  struct ehci_endpoint *endpoint;
1123  int rc;
1124 
1125  /* Allocate and initialise structure */
1126  endpoint = zalloc ( sizeof ( *endpoint ) );
1127  if ( ! endpoint ) {
1128  rc = -ENOMEM;
1129  goto err_alloc;
1130  }
1131  endpoint->ehci = ehci;
1132  endpoint->ep = ep;
1133  usb_endpoint_set_hostdata ( ep, endpoint );
1134 
1135  /* Initialise descriptor ring */
1136  if ( ( rc = ehci_ring_alloc ( ehci, &endpoint->ring ) ) != 0 )
1137  goto err_ring_alloc;
1138 
1139  /* Update queue characteristics and capabilities */
1141 
1142  /* Add to list of endpoints */
1143  list_add_tail ( &endpoint->list, &ehci->endpoints );
1144 
1145  /* Add to schedule */
1146  ehci_schedule_add ( endpoint );
1147 
1148  return 0;
1149 
1150  ehci_ring_free ( &endpoint->ring );
1151  err_ring_alloc:
1152  free ( endpoint );
1153  err_alloc:
1154  return rc;
1155 }
1156 
1157 /**
1158  * Close endpoint
1159  *
1160  * @v ep USB endpoint
1161  */
1162 static void ehci_endpoint_close ( struct usb_endpoint *ep ) {
1163  struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
1164  struct ehci_device *ehci = endpoint->ehci;
1165  struct usb_device *usb = ep->usb;
1166  struct io_buffer *iobuf;
1167  int rc;
1168 
1169  /* Remove from schedule */
1170  if ( ( rc = ehci_schedule_del ( endpoint ) ) != 0 ) {
1171  /* No way to prevent hardware from continuing to
1172  * access the memory, so leak it.
1173  */
1174  DBGC ( ehci, "EHCI %s %s could not unschedule: %s\n",
1175  usb->name, usb_endpoint_name ( ep ), strerror ( rc ) );
1176  return;
1177  }
1178 
1179  /* Cancel any incomplete transfers */
1180  while ( ehci_ring_fill ( &endpoint->ring ) ) {
1181  iobuf = ehci_dequeue ( &endpoint->ring );
1182  if ( iobuf )
1183  usb_complete_err ( ep, iobuf, -ECANCELED );
1184  }
1185 
1186  /* Remove from list of endpoints */
1187  list_del ( &endpoint->list );
1188 
1189  /* Free descriptor ring */
1190  ehci_ring_free ( &endpoint->ring );
1191 
1192  /* Free endpoint */
1193  free ( endpoint );
1194 }
1195 
1196 /**
1197  * Reset endpoint
1198  *
1199  * @v ep USB endpoint
1200  * @ret rc Return status code
1201  */
1202 static int ehci_endpoint_reset ( struct usb_endpoint *ep ) {
1203  struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
1204  struct ehci_ring *ring = &endpoint->ring;
1205  struct ehci_transfer_descriptor *cache = &ring->head->cache;
1206  uint32_t link;
1207 
1208  /* Sanity checks */
1209  assert ( ! ( cache->status & EHCI_STATUS_ACTIVE ) );
1210  assert ( cache->status & EHCI_STATUS_HALTED );
1211 
1212  /* Reset residual count */
1213  ring->residual = 0;
1214 
1215  /* Reset data toggle */
1216  cache->len = 0;
1217 
1218  /* Prepare to restart at next unconsumed descriptor */
1219  link = virt_to_phys ( &ring->desc[ ring->cons % EHCI_RING_COUNT ] );
1220  cache->next = cpu_to_le32 ( link );
1221 
1222  /* Restart ring */
1223  wmb();
1224  cache->status = 0;
1225 
1226  return 0;
1227 }
1228 
1229 /**
1230  * Update MTU
1231  *
1232  * @v ep USB endpoint
1233  * @ret rc Return status code
1234  */
1235 static int ehci_endpoint_mtu ( struct usb_endpoint *ep ) {
1236 
1237  /* Update endpoint characteristics and capabilities */
1238  ehci_endpoint_update ( ep );
1239 
1240  return 0;
1241 }
1242 
1243 /**
1244  * Enqueue message transfer
1245  *
1246  * @v ep USB endpoint
1247  * @v iobuf I/O buffer
1248  * @ret rc Return status code
1249  */
1250 static int ehci_endpoint_message ( struct usb_endpoint *ep,
1251  struct io_buffer *iobuf ) {
1252  struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
1253  struct ehci_device *ehci = endpoint->ehci;
1254  struct usb_setup_packet *packet;
1255  unsigned int input;
1256  struct ehci_transfer xfers[3];
1257  struct ehci_transfer *xfer = xfers;
1258  size_t len;
1259  int rc;
1260 
1261  /* Construct setup stage */
1262  assert ( iob_len ( iobuf ) >= sizeof ( *packet ) );
1263  packet = iobuf->data;
1264  iob_pull ( iobuf, sizeof ( *packet ) );
1265  xfer->data = packet;
1266  xfer->len = sizeof ( *packet );
1267  xfer->flags = EHCI_FL_PID_SETUP;
1268  xfer++;
1269 
1270  /* Construct data stage, if applicable */
1271  len = iob_len ( iobuf );
1272  input = ( packet->request & cpu_to_le16 ( USB_DIR_IN ) );
1273  if ( len ) {
1274  xfer->data = iobuf->data;
1275  xfer->len = len;
1276  xfer->flags = ( EHCI_FL_TOGGLE |
1278  xfer++;
1279  }
1280 
1281  /* Construct status stage */
1282  xfer->data = NULL;
1283  xfer->len = 0;
1284  xfer->flags = ( EHCI_FL_TOGGLE | EHCI_FL_IOC |
1285  ( ( len && input ) ? EHCI_FL_PID_OUT : EHCI_FL_PID_IN));
1286  xfer++;
1287 
1288  /* Enqueue transfer */
1289  if ( ( rc = ehci_enqueue ( ehci, &endpoint->ring, iobuf, xfers,
1290  ( xfer - xfers ) ) ) != 0 )
1291  return rc;
1292 
1293  return 0;
1294 }
1295 
1296 /**
1297  * Calculate number of transfer descriptors
1298  *
1299  * @v len Length of data
1300  * @v zlp Append a zero-length packet
1301  * @ret count Number of transfer descriptors
1302  */
1303 static unsigned int ehci_endpoint_count ( size_t len, int zlp ) {
1304  unsigned int count;
1305 
1306  /* Split into 16kB transfers. A single transfer can handle up
1307  * to 20kB if it happens to be page-aligned, or up to 16kB
1308  * with arbitrary alignment. We simplify the code by assuming
1309  * that we can fit only 16kB into each transfer.
1310  */
1311  count = ( ( len + EHCI_MTU - 1 ) / EHCI_MTU );
1312 
1313  /* Append a zero-length transfer if applicable */
1314  if ( zlp || ( count == 0 ) )
1315  count++;
1316 
1317  return count;
1318 }
1319 
1320 /**
1321  * Enqueue stream transfer
1322  *
1323  * @v ep USB endpoint
1324  * @v iobuf I/O buffer
1325  * @v zlp Append a zero-length packet
1326  * @ret rc Return status code
1327  */
1328 static int ehci_endpoint_stream ( struct usb_endpoint *ep,
1329  struct io_buffer *iobuf, int zlp ) {
1330  struct ehci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
1331  struct ehci_device *ehci = endpoint->ehci;
1332  void *data = iobuf->data;
1333  size_t len = iob_len ( iobuf );
1334  unsigned int count = ehci_endpoint_count ( len, zlp );
1335  unsigned int input = ( ep->address & USB_DIR_IN );
1336  unsigned int flags = ( input ? EHCI_FL_PID_IN : EHCI_FL_PID_OUT );
1337  struct ehci_transfer xfers[count];
1338  struct ehci_transfer *xfer = xfers;
1339  size_t xfer_len;
1340  unsigned int i;
1341  int rc;
1342 
1343  /* Create transfers */
1344  for ( i = 0 ; i < count ; i++ ) {
1345 
1346  /* Calculate transfer length */
1347  xfer_len = EHCI_MTU;
1348  if ( xfer_len > len )
1349  xfer_len = len;
1350 
1351  /* Create transfer */
1352  xfer->data = data;
1353  xfer->len = xfer_len;
1354  xfer->flags = flags;
1355 
1356  /* Move to next transfer */
1357  data += xfer_len;
1358  len -= xfer_len;
1359  xfer++;
1360  }
1361  xfer[-1].flags |= EHCI_FL_IOC;
1362 
1363  /* Enqueue transfer */
1364  if ( ( rc = ehci_enqueue ( ehci, &endpoint->ring, iobuf, xfers,
1365  count ) ) != 0 )
1366  return rc;
1367 
1368  return 0;
1369 }
1370 
1371 /**
1372  * Poll for completions
1373  *
1374  * @v endpoint Endpoint
1375  */
1376 static void ehci_endpoint_poll ( struct ehci_endpoint *endpoint ) {
1377  struct ehci_device *ehci = endpoint->ehci;
1378  struct ehci_ring *ring = &endpoint->ring;
1380  struct usb_endpoint *ep = endpoint->ep;
1381  struct usb_device *usb = ep->usb;
1382  struct io_buffer *iobuf;
1383  unsigned int index;
1384  unsigned int status;
1385  int rc;
1386 
1387  /* Consume all completed descriptors */
1388  while ( ehci_ring_fill ( &endpoint->ring ) ) {
1389 
1390  /* Stop if we reach an uncompleted descriptor */
1391  rmb();
1392  index = ( ring->cons % EHCI_RING_COUNT );
1393  desc = &ring->desc[index];
1394  status = desc->status;
1395  if ( status & EHCI_STATUS_ACTIVE )
1396  break;
1397 
1398  /* Consume this descriptor */
1399  iobuf = ehci_dequeue ( ring );
1400 
1401  /* If we have encountered an error, then consume all
1402  * remaining descriptors in this transaction, report
1403  * the error to the USB core, and stop further
1404  * processing.
1405  */
1406  if ( status & EHCI_STATUS_HALTED ) {
1407  rc = -EIO_STATUS ( status );
1408  DBGC ( ehci, "EHCI %s %s completion %d failed (status "
1409  "%02x): %s\n", usb->name,
1410  usb_endpoint_name ( ep ), index, status,
1411  strerror ( rc ) );
1412  while ( ! iobuf )
1413  iobuf = ehci_dequeue ( ring );
1414  usb_complete_err ( endpoint->ep, iobuf, rc );
1415  return;
1416  }
1417 
1418  /* Accumulate residual data count */
1419  ring->residual += ( le16_to_cpu ( desc->len ) & EHCI_LEN_MASK );
1420 
1421  /* If this is not the end of a transaction (i.e. has
1422  * no I/O buffer), then continue to next descriptor.
1423  */
1424  if ( ! iobuf )
1425  continue;
1426 
1427  /* Update I/O buffer length */
1428  iob_unput ( iobuf, ring->residual );
1429  ring->residual = 0;
1430 
1431  /* Report completion to USB core */
1432  usb_complete ( endpoint->ep, iobuf );
1433  }
1434 }
1435 
1436 /******************************************************************************
1437  *
1438  * Device operations
1439  *
1440  ******************************************************************************
1441  */
1442 
1443 /**
1444  * Open device
1445  *
1446  * @v usb USB device
1447  * @ret rc Return status code
1448  */
1449 static int ehci_device_open ( struct usb_device *usb ) {
1450  struct ehci_device *ehci = usb_bus_get_hostdata ( usb->port->hub->bus );
1451 
1452  usb_set_hostdata ( usb, ehci );
1453  return 0;
1454 }
1455 
1456 /**
1457  * Close device
1458  *
1459  * @v usb USB device
1460  */
1461 static void ehci_device_close ( struct usb_device *usb ) {
1462  struct ehci_device *ehci = usb_get_hostdata ( usb );
1463  struct usb_bus *bus = ehci->bus;
1464 
1465  /* Free device address, if assigned */
1466  if ( usb->address )
1467  usb_free_address ( bus, usb->address );
1468 }
1469 
1470 /**
1471  * Assign device address
1472  *
1473  * @v usb USB device
1474  * @ret rc Return status code
1475  */
1476 static int ehci_device_address ( struct usb_device *usb ) {
1477  struct ehci_device *ehci = usb_get_hostdata ( usb );
1478  struct usb_bus *bus = ehci->bus;
1479  struct usb_endpoint *ep0 = usb_endpoint ( usb, USB_EP0_ADDRESS );
1480  int address;
1481  int rc;
1482 
1483  /* Sanity checks */
1484  assert ( usb->address == 0 );
1485  assert ( ep0 != NULL );
1486 
1487  /* Allocate device address */
1489  if ( address < 0 ) {
1490  rc = address;
1491  DBGC ( ehci, "EHCI %s could not allocate address: %s\n",
1492  usb->name, strerror ( rc ) );
1493  goto err_alloc_address;
1494  }
1495 
1496  /* Set address */
1497  if ( ( rc = usb_set_address ( usb, address ) ) != 0 )
1498  goto err_set_address;
1499 
1500  /* Update device address */
1501  usb->address = address;
1502 
1503  /* Update control endpoint characteristics and capabilities */
1504  ehci_endpoint_update ( ep0 );
1505 
1506  return 0;
1507 
1508  err_set_address:
1510  err_alloc_address:
1511  return rc;
1512 }
1513 
1514 /******************************************************************************
1515  *
1516  * Hub operations
1517  *
1518  ******************************************************************************
1519  */
1520 
1521 /**
1522  * Open hub
1523  *
1524  * @v hub USB hub
1525  * @ret rc Return status code
1526  */
1527 static int ehci_hub_open ( struct usb_hub *hub __unused ) {
1528 
1529  /* Nothing to do */
1530  return 0;
1531 }
1532 
1533 /**
1534  * Close hub
1535  *
1536  * @v hub USB hub
1537  */
1538 static void ehci_hub_close ( struct usb_hub *hub __unused ) {
1539 
1540  /* Nothing to do */
1541 }
1542 
1543 /******************************************************************************
1544  *
1545  * Root hub operations
1546  *
1547  ******************************************************************************
1548  */
1549 
1550 /**
1551  * Open root hub
1552  *
1553  * @v hub USB hub
1554  * @ret rc Return status code
1555  */
1556 static int ehci_root_open ( struct usb_hub *hub ) {
1557  struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1558  uint32_t portsc;
1559  unsigned int i;
1560 
1561  /* Route all ports to EHCI controller */
1563 
1564  /* Enable power to all ports */
1565  for ( i = 1 ; i <= ehci->ports ; i++ ) {
1566  portsc = readl ( ehci->op + EHCI_OP_PORTSC ( i ) );
1567  portsc &= ~EHCI_PORTSC_CHANGE;
1568  portsc |= EHCI_PORTSC_PP;
1569  writel ( portsc, ehci->op + EHCI_OP_PORTSC ( i ) );
1570  }
1571 
1572  /* Wait 20ms after potentially enabling power to a port */
1574 
1575  return 0;
1576 }
1577 
1578 /**
1579  * Close root hub
1580  *
1581  * @v hub USB hub
1582  */
1583 static void ehci_root_close ( struct usb_hub *hub ) {
1584  struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1585 
1586  /* Route all ports back to companion controllers */
1587  writel ( 0, ehci->op + EHCI_OP_CONFIGFLAG );
1588 }
1589 
1590 /**
1591  * Enable port
1592  *
1593  * @v hub USB hub
1594  * @v port USB port
1595  * @ret rc Return status code
1596  */
1597 static int ehci_root_enable ( struct usb_hub *hub, struct usb_port *port ) {
1598  struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1599  uint32_t portsc;
1600  unsigned int line;
1601  unsigned int i;
1602 
1603  /* Check for a low-speed device */
1604  portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
1605  line = EHCI_PORTSC_LINE_STATUS ( portsc );
1606  if ( line == EHCI_PORTSC_LINE_STATUS_LOW ) {
1607  DBGC ( ehci, "EHCI %s-%d detected low-speed device: "
1608  "disowning\n", ehci->name, port->address );
1609  goto disown;
1610  }
1611 
1612  /* Reset port */
1613  portsc &= ~( EHCI_PORTSC_PED | EHCI_PORTSC_CHANGE );
1614  portsc |= EHCI_PORTSC_PR;
1615  writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1617  portsc &= ~EHCI_PORTSC_PR;
1618  writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1619 
1620  /* Wait for reset to complete */
1621  for ( i = 0 ; i < EHCI_PORT_RESET_MAX_WAIT_MS ; i++ ) {
1622 
1623  /* Check port status */
1624  portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
1625  if ( ! ( portsc & EHCI_PORTSC_PR ) ) {
1626  if ( portsc & EHCI_PORTSC_PED )
1627  return 0;
1628  DBGC ( ehci, "EHCI %s-%d not enabled after reset: "
1629  "disowning\n", ehci->name, port->address );
1630  goto disown;
1631  }
1632 
1633  /* Delay */
1634  mdelay ( 1 );
1635  }
1636 
1637  DBGC ( ehci, "EHCI %s-%d timed out waiting for port to reset\n",
1638  ehci->name, port->address );
1639  return -ETIMEDOUT;
1640 
1641  disown:
1642  /* Disown port */
1643  portsc &= ~EHCI_PORTSC_CHANGE;
1644  portsc |= EHCI_PORTSC_OWNER;
1645  writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1646 
1647  /* Delay to allow child companion controllers to settle */
1649 
1650  /* Poll child companion controllers */
1651  ehci_poll_companions ( ehci );
1652 
1653  return -ENODEV;
1654 }
1655 
1656 /**
1657  * Disable port
1658  *
1659  * @v hub USB hub
1660  * @v port USB port
1661  * @ret rc Return status code
1662  */
1663 static int ehci_root_disable ( struct usb_hub *hub, struct usb_port *port ) {
1664  struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1665  uint32_t portsc;
1666 
1667  /* Disable port */
1668  portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
1669  portsc &= ~( EHCI_PORTSC_PED | EHCI_PORTSC_CHANGE );
1670  writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1671 
1672  return 0;
1673 }
1674 
1675 /**
1676  * Update root hub port speed
1677  *
1678  * @v hub USB hub
1679  * @v port USB port
1680  * @ret rc Return status code
1681  */
1682 static int ehci_root_speed ( struct usb_hub *hub, struct usb_port *port ) {
1683  struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1684  uint32_t portsc;
1685  unsigned int speed;
1686  unsigned int line;
1687  int ccs;
1688  int csc;
1689  int ped;
1690 
1691  /* Read port status */
1692  portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
1693  DBGC2 ( ehci, "EHCI %s-%d status is %08x\n",
1694  ehci->name, port->address, portsc );
1695  ccs = ( portsc & EHCI_PORTSC_CCS );
1696  csc = ( portsc & EHCI_PORTSC_CSC );
1697  ped = ( portsc & EHCI_PORTSC_PED );
1698  line = EHCI_PORTSC_LINE_STATUS ( portsc );
1699 
1700  /* Record disconnections and clear changes */
1701  port->disconnected |= csc;
1702  writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1703 
1704  /* Determine port speed */
1705  if ( ! ccs ) {
1706  /* Port not connected */
1707  speed = USB_SPEED_NONE;
1708  } else if ( line == EHCI_PORTSC_LINE_STATUS_LOW ) {
1709  /* Detected as low-speed */
1710  speed = USB_SPEED_LOW;
1711  } else if ( ped ) {
1712  /* Port already enabled: must be high-speed */
1713  speed = USB_SPEED_HIGH;
1714  } else {
1715  /* Not low-speed and not yet enabled. Could be either
1716  * full-speed or high-speed; we can't yet tell.
1717  */
1718  speed = USB_SPEED_FULL;
1719  }
1720  port->speed = speed;
1721  return 0;
1722 }
1723 
1724 /**
1725  * Clear transaction translator buffer
1726  *
1727  * @v hub USB hub
1728  * @v port USB port
1729  * @v ep USB endpoint
1730  * @ret rc Return status code
1731  */
1732 static int ehci_root_clear_tt ( struct usb_hub *hub, struct usb_port *port,
1733  struct usb_endpoint *ep ) {
1734  struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1735 
1736  /* Should never be called; this is a root hub */
1737  DBGC ( ehci, "EHCI %s-%d nonsensical CLEAR_TT for %s %s\n", ehci->name,
1738  port->address, ep->usb->name, usb_endpoint_name ( ep ) );
1739 
1740  return -ENOTSUP;
1741 }
1742 
1743 /**
1744  * Poll for port status changes
1745  *
1746  * @v hub USB hub
1747  * @v port USB port
1748  */
1749 static void ehci_root_poll ( struct usb_hub *hub, struct usb_port *port ) {
1750  struct ehci_device *ehci = usb_hub_get_drvdata ( hub );
1751  uint32_t portsc;
1752  uint32_t change;
1753 
1754  /* Do nothing unless something has changed */
1755  portsc = readl ( ehci->op + EHCI_OP_PORTSC ( port->address ) );
1756  change = ( portsc & EHCI_PORTSC_CHANGE );
1757  if ( ! change )
1758  return;
1759 
1760  /* Record disconnections and clear changes */
1761  port->disconnected |= ( portsc & EHCI_PORTSC_CSC );
1762  writel ( portsc, ehci->op + EHCI_OP_PORTSC ( port->address ) );
1763 
1764  /* Report port status change */
1765  usb_port_changed ( port );
1766 }
1767 
1768 /******************************************************************************
1769  *
1770  * Bus operations
1771  *
1772  ******************************************************************************
1773  */
1774 
1775 /**
1776  * Open USB bus
1777  *
1778  * @v bus USB bus
1779  * @ret rc Return status code
1780  */
1781 static int ehci_bus_open ( struct usb_bus *bus ) {
1782  struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
1783  unsigned int frames;
1784  size_t len;
1785  int rc;
1786 
1787  /* Sanity checks */
1788  assert ( list_empty ( &ehci->async ) );
1789  assert ( list_empty ( &ehci->periodic ) );
1790 
1791  /* Allocate and initialise asynchronous queue head */
1792  ehci->head = malloc_phys ( sizeof ( *ehci->head ),
1793  ehci_align ( sizeof ( *ehci->head ) ) );
1794  if ( ! ehci->head ) {
1795  rc = -ENOMEM;
1796  goto err_alloc_head;
1797  }
1798  memset ( ehci->head, 0, sizeof ( *ehci->head ) );
1799  ehci->head->chr = cpu_to_le32 ( EHCI_CHR_HEAD );
1802  ehci_async_schedule ( ehci );
1803  writel ( virt_to_phys ( ehci->head ),
1804  ehci->op + EHCI_OP_ASYNCLISTADDR );
1805 
1806  /* Use async queue head to determine control data structure segment */
1807  ehci->ctrldssegment =
1808  ( ( ( uint64_t ) virt_to_phys ( ehci->head ) ) >> 32 );
1809  if ( ehci->addr64 ) {
1810  writel ( ehci->ctrldssegment, ehci->op + EHCI_OP_CTRLDSSEGMENT);
1811  } else if ( ehci->ctrldssegment ) {
1812  DBGC ( ehci, "EHCI %s CTRLDSSEGMENT not supported\n",
1813  ehci->name );
1814  rc = -ENOTSUP;
1815  goto err_ctrldssegment;
1816  }
1817 
1818  /* Allocate periodic frame list */
1819  frames = EHCI_PERIODIC_FRAMES ( ehci->flsize );
1820  len = ( frames * sizeof ( ehci->frame[0] ) );
1821  ehci->frame = malloc_phys ( len, EHCI_PAGE_ALIGN );
1822  if ( ! ehci->frame ) {
1823  rc = -ENOMEM;
1824  goto err_alloc_frame;
1825  }
1826  if ( ( rc = ehci_ctrl_reachable ( ehci, ehci->frame ) ) != 0 ) {
1827  DBGC ( ehci, "EHCI %s frame list unreachable\n", ehci->name );
1828  goto err_unreachable_frame;
1829  }
1830  ehci_periodic_schedule ( ehci );
1831  writel ( virt_to_phys ( ehci->frame ),
1832  ehci->op + EHCI_OP_PERIODICLISTBASE );
1833 
1834  /* Start controller */
1835  ehci_run ( ehci );
1836 
1837  return 0;
1838 
1839  ehci_stop ( ehci );
1840  err_unreachable_frame:
1841  free_phys ( ehci->frame, len );
1842  err_alloc_frame:
1843  err_ctrldssegment:
1844  free_phys ( ehci->head, sizeof ( *ehci->head ) );
1845  err_alloc_head:
1846  return rc;
1847 }
1848 
1849 /**
1850  * Close USB bus
1851  *
1852  * @v bus USB bus
1853  */
1854 static void ehci_bus_close ( struct usb_bus *bus ) {
1855  struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
1856  unsigned int frames = EHCI_PERIODIC_FRAMES ( ehci->flsize );
1857 
1858  /* Sanity checks */
1859  assert ( list_empty ( &ehci->async ) );
1860  assert ( list_empty ( &ehci->periodic ) );
1861 
1862  /* Stop controller */
1863  ehci_stop ( ehci );
1864 
1865  /* Free periodic frame list */
1866  free_phys ( ehci->frame, ( frames * sizeof ( ehci->frame[0] ) ) );
1867 
1868  /* Free asynchronous schedule */
1869  free_phys ( ehci->head, sizeof ( *ehci->head ) );
1870 }
1871 
1872 /**
1873  * Poll USB bus
1874  *
1875  * @v bus USB bus
1876  */
1877 static void ehci_bus_poll ( struct usb_bus *bus ) {
1878  struct ehci_device *ehci = usb_bus_get_hostdata ( bus );
1879  struct usb_hub *hub = bus->hub;
1880  struct ehci_endpoint *endpoint;
1881  unsigned int i;
1882  uint32_t usbsts;
1883  uint32_t change;
1884 
1885  /* Do nothing unless something has changed */
1886  usbsts = readl ( ehci->op + EHCI_OP_USBSTS );
1887  assert ( usbsts & EHCI_USBSTS_ASYNC );
1888  assert ( usbsts & EHCI_USBSTS_PERIODIC );
1889  assert ( ! ( usbsts & EHCI_USBSTS_HCH ) );
1890  change = ( usbsts & EHCI_USBSTS_CHANGE );
1891  if ( ! change )
1892  return;
1893 
1894  /* Acknowledge changes */
1895  writel ( usbsts, ehci->op + EHCI_OP_USBSTS );
1896 
1897  /* Process completions, if applicable */
1898  if ( change & ( EHCI_USBSTS_USBINT | EHCI_USBSTS_USBERRINT ) ) {
1899 
1900  /* Iterate over all endpoints looking for completed
1901  * descriptors. We trust that completion handlers are
1902  * minimal and will not do anything that could
1903  * plausibly affect the endpoint list itself.
1904  */
1905  list_for_each_entry ( endpoint, &ehci->endpoints, list )
1906  ehci_endpoint_poll ( endpoint );
1907  }
1908 
1909  /* Process port status changes, if applicable */
1910  if ( change & EHCI_USBSTS_PORT ) {
1911 
1912  /* Iterate over all ports looking for status changes */
1913  for ( i = 1 ; i <= ehci->ports ; i++ )
1914  ehci_root_poll ( hub, usb_port ( hub, i ) );
1915  }
1916 
1917  /* Report fatal errors */
1918  if ( change & EHCI_USBSTS_SYSERR )
1919  DBGC ( ehci, "EHCI %s host system error\n", ehci->name );
1920 }
1921 
1922 /******************************************************************************
1923  *
1924  * PCI interface
1925  *
1926  ******************************************************************************
1927  */
1928 
1929 /** USB host controller operations */
1931  .endpoint = {
1933  .close = ehci_endpoint_close,
1934  .reset = ehci_endpoint_reset,
1935  .mtu = ehci_endpoint_mtu,
1936  .message = ehci_endpoint_message,
1937  .stream = ehci_endpoint_stream,
1938  },
1939  .device = {
1940  .open = ehci_device_open,
1941  .close = ehci_device_close,
1942  .address = ehci_device_address,
1943  },
1944  .bus = {
1945  .open = ehci_bus_open,
1946  .close = ehci_bus_close,
1947  .poll = ehci_bus_poll,
1948  },
1949  .hub = {
1950  .open = ehci_hub_open,
1951  .close = ehci_hub_close,
1952  },
1953  .root = {
1954  .open = ehci_root_open,
1955  .close = ehci_root_close,
1956  .enable = ehci_root_enable,
1957  .disable = ehci_root_disable,
1958  .speed = ehci_root_speed,
1959  .clear_tt = ehci_root_clear_tt,
1960  },
1961 };
1962 
1963 /**
1964  * Probe PCI device
1965  *
1966  * @v pci PCI device
1967  * @ret rc Return status code
1968  */
1969 static int ehci_probe ( struct pci_device *pci ) {
1970  struct ehci_device *ehci;
1971  struct usb_port *port;
1972  unsigned long bar_start;
1973  size_t bar_size;
1974  unsigned int i;
1975  int rc;
1976 
1977  /* Allocate and initialise structure */
1978  ehci = zalloc ( sizeof ( *ehci ) );
1979  if ( ! ehci ) {
1980  rc = -ENOMEM;
1981  goto err_alloc;
1982  }
1983  ehci->name = pci->dev.name;
1984  INIT_LIST_HEAD ( &ehci->endpoints );
1985  INIT_LIST_HEAD ( &ehci->async );
1986  INIT_LIST_HEAD ( &ehci->periodic );
1987 
1988  /* Fix up PCI device */
1989  adjust_pci_device ( pci );
1990 
1991  /* Map registers */
1992  bar_start = pci_bar_start ( pci, EHCI_BAR );
1993  bar_size = pci_bar_size ( pci, EHCI_BAR );
1994  ehci->regs = pci_ioremap ( pci, bar_start, bar_size );
1995  if ( ! ehci->regs ) {
1996  rc = -ENODEV;
1997  goto err_ioremap;
1998  }
1999 
2000  /* Initialise EHCI device */
2001  ehci_init ( ehci, ehci->regs );
2002 
2003  /* Initialise USB legacy support and claim ownership */
2004  ehci_legacy_init ( ehci, pci );
2005  ehci_legacy_claim ( ehci, pci );
2006 
2007  /* Reset device */
2008  if ( ( rc = ehci_reset ( ehci ) ) != 0 )
2009  goto err_reset;
2010 
2011  /* Allocate USB bus */
2012  ehci->bus = alloc_usb_bus ( &pci->dev, ehci->ports, EHCI_MTU,
2013  &ehci_operations );
2014  if ( ! ehci->bus ) {
2015  rc = -ENOMEM;
2016  goto err_alloc_bus;
2017  }
2018  usb_bus_set_hostdata ( ehci->bus, ehci );
2019  usb_hub_set_drvdata ( ehci->bus->hub, ehci );
2020 
2021  /* Set port protocols */
2022  for ( i = 1 ; i <= ehci->ports ; i++ ) {
2023  port = usb_port ( ehci->bus->hub, i );
2024  port->protocol = USB_PROTO_2_0;
2025  }
2026 
2027  /* Register USB bus */
2028  if ( ( rc = register_usb_bus ( ehci->bus ) ) != 0 )
2029  goto err_register;
2030 
2031  pci_set_drvdata ( pci, ehci );
2032  return 0;
2033 
2034  unregister_usb_bus ( ehci->bus );
2035  err_register:
2036  free_usb_bus ( ehci->bus );
2037  err_alloc_bus:
2038  ehci_reset ( ehci );
2039  err_reset:
2040  ehci_legacy_release ( ehci, pci );
2041  iounmap ( ehci->regs );
2042  err_ioremap:
2043  free ( ehci );
2044  err_alloc:
2045  return rc;
2046 }
2047 
2048 /**
2049  * Remove PCI device
2050  *
2051  * @v pci PCI device
2052  */
2053 static void ehci_remove ( struct pci_device *pci ) {
2054  struct ehci_device *ehci = pci_get_drvdata ( pci );
2055  struct usb_bus *bus = ehci->bus;
2056 
2057  unregister_usb_bus ( bus );
2058  assert ( list_empty ( &ehci->async ) );
2059  assert ( list_empty ( &ehci->periodic ) );
2060  free_usb_bus ( bus );
2061  ehci_reset ( ehci );
2062  ehci_legacy_release ( ehci, pci );
2063  iounmap ( ehci->regs );
2064  free ( ehci );
2065 }
2066 
2067 /** EHCI PCI device IDs */
2068 static struct pci_device_id ehci_ids[] = {
2069  PCI_ROM ( 0xffff, 0xffff, "ehci", "EHCI", 0 ),
2070 };
2071 
2072 /** EHCI PCI driver */
2073 struct pci_driver ehci_driver __pci_driver = {
2074  .ids = ehci_ids,
2075  .id_count = ( sizeof ( ehci_ids ) / sizeof ( ehci_ids[0] ) ),
2078  .probe = ehci_probe,
2079  .remove = ehci_remove,
2080 };
2081 
2082 /**
2083  * Prepare for exit
2084  *
2085  * @v booting System is shutting down for OS boot
2086  */
2087 static void ehci_shutdown ( int booting ) {
2088  /* If we are shutting down to boot an OS, then prevent the
2089  * release of ownership back to BIOS.
2090  */
2091  ehci_legacy_prevent_release = booting;
2092 }
2093 
2094 /** Startup/shutdown function */
2095 struct startup_fn ehci_startup __startup_fn ( STARTUP_LATE ) = {
2096  .name = "ehci",
2097  .shutdown = ehci_shutdown,
2098 };
#define EHCI_USBLEGSUP_BIOS_OWNED
USB legacy support BIOS ownership flag.
Definition: ehci.h:76
unsigned int cons
Consumer counter.
Definition: ehci.h:374
#define iob_pull(iobuf, len)
Definition: iobuf.h:107
static void ehci_legacy_claim(struct ehci_device *ehci, struct pci_device *pci)
Claim ownership from BIOS.
Definition: ehci.c:281
static void ehci_run(struct ehci_device *ehci)
Start EHCI device.
Definition: ehci.c:453
static void usb_endpoint_set_hostdata(struct usb_endpoint *ep, void *priv)
Set USB endpoint host controller private data.
Definition: usb.h:576
uint16_t segment
Code segment.
Definition: librm.h:138
#define EIO_STATUS(status)
Construct error code from transfer descriptor status.
Definition: ehci.c:55
#define PCI_CLASS_ID(base, sub, progif)
Construct PCI class ID.
Definition: pci.h:203
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define EHCI_USBLEGSUP_BIOS
USB legacy support BIOS owned semaphore.
Definition: ehci.h:73
unsigned short uint16_t
Definition: stdint.h:11
#define EHCI_USBCMD_ASYNC_ADVANCE
Asyncchronous schedule advance doorbell.
Definition: ehci.h:118
#define EHCI_USBSTS_SYSERR
Host system error.
Definition: ehci.h:136
#define EHCI_USBCMD_HCRST
Host controller reset.
Definition: ehci.h:94
wmb()
#define EHCI_PERIODIC_FRAMES(flsize)
Number of elements in frame list.
Definition: ehci.h:109
unsigned int ports
Number of ports.
Definition: ehci.h:496
static struct io_buffer * ehci_dequeue(struct ehci_ring *ring)
Dequeue a transfer descriptor.
Definition: ehci.c:745
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
static int ehci_stop(struct ehci_device *ehci)
Stop EHCI device.
Definition: ehci.c:470
uint8_t readb(volatile uint8_t *io_addr)
Read byte from memory-mapped device.
static size_t ehci_align(size_t len)
Calculate buffer alignment.
Definition: ehci.c:141
A transfer descriptor ring.
Definition: ehci.h:370
A PCI driver.
Definition: pci.h:252
static int ehci_endpoint_message(struct usb_endpoint *ep, struct io_buffer *iobuf)
Enqueue message transfer.
Definition: ehci.c:1250
struct list_head periodic
Periodic schedule.
Definition: ehci.h:522
A USB hub.
Definition: usb.h:841
void * op
Operational registers.
Definition: ehci.h:493
uint32_t ctrldssegment
Control data structure segment.
Definition: ehci.h:508
#define EHCI_STATUS_ACTIVE
Active.
Definition: ehci.h:262
static int ehci_device_open(struct usb_device *usb)
Open device.
Definition: ehci.c:1449
#define EHCI_CHR_CONTROL
Control endpoint flag.
Definition: ehci.h:333
size_t residual
Residual untransferred data.
Definition: ehci.h:377
#define EHCI_USBSTS_ASYNC
Asynchronous schedule enabled.
Definition: ehci.h:145
#define EHCI_HCCPARAMS_ADDR64(params)
64-bit addressing capability
Definition: ehci.h:55
static int ehci_endpoint_stream(struct usb_endpoint *ep, struct io_buffer *iobuf, int zlp)
Enqueue stream transfer.
Definition: ehci.c:1328
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:2095
Error codes.
Low speed (1.5Mbps)
Definition: usb.h:49
#define USB_ENDPOINT_ATTR_TYPE_MASK
Endpoint attribute transfer type mask.
Definition: usb.h:281
USB 2.0.
Definition: usb.h:23
static void ehci_endpoint_close(struct usb_endpoint *ep)
Close endpoint.
Definition: ehci.c:1162
static int ehci_endpoint_reset(struct usb_endpoint *ep)
Reset endpoint.
Definition: ehci.c:1202
uint16_t readw(volatile uint16_t *io_addr)
Read 16-bit word from memory-mapped device.
static struct pci_device_id ehci_ids[]
EHCI PCI device IDs.
Definition: ehci.c:2068
#define EHCI_CAP_TT_HUB(address)
Transaction translator hub address.
Definition: ehci.h:361
static int ehci_root_clear_tt(struct usb_hub *hub, struct usb_port *port, struct usb_endpoint *ep)
Clear transaction translator buffer.
Definition: ehci.c:1732
#define EHCI_FL_PID_SETUP
SETUP token.
Definition: ehci.h:274
static uint32_t ehci_endpoint_capabilities(struct usb_endpoint *ep)
Determine endpoint capabilities.
Definition: ehci.c:1067
#define EHCI_FL_CERR_MAX
Error counter maximum value.
Definition: ehci.h:280
struct pci_device_id * ids
PCI ID table.
Definition: pci.h:254
uint64_t address
Base address.
Definition: ena.h:24
#define EHCI_CAP_CAPLENGTH
Capability register length.
Definition: ehci.h:40
static void usb_set_hostdata(struct usb_device *usb, void *priv)
Set USB device host controller private data.
Definition: usb.h:784
#define EHCI_CHR_TOGGLE
Explicit data toggles.
Definition: ehci.h:324
uint8_t attr
Type and attributes.
Definition: librm.h:142
#define EHCI_USBSTS_CHANGE
USB status change mask.
Definition: ehci.h:151
#define EHCI_DISOWN_DELAY_MS
Time to delay after releasing ownership of a port.
Definition: ehci.h:432
#define EHCI_USBCMD_RUN
Run/stop.
Definition: ehci.h:91
static void ehci_endpoint_update(struct usb_endpoint *ep)
Update endpoint characteristics and capabilities.
Definition: ehci.c:1103
#define EHCI_HCSPARAMS_PORTS(params)
Number of ports.
Definition: ehci.h:49
#define EHCI_USBSTS_PORT
Port change detect.
Definition: ehci.h:130
#define EHCI_HCCPARAMS_EECP(params)
EHCI extended capabilities pointer.
Definition: ehci.h:61
uint32_t readl(volatile uint32_t *io_addr)
Read 32-bit dword from memory-mapped device.
#define DBGC(...)
Definition: compiler.h:505
uint16_t len
Transfer length.
Definition: ehci.h:240
#define USB_RESET_DELAY_MS
Minimum reset time.
Definition: usb.h:1328
unsigned long pci_bar_size(struct pci_device *pci, unsigned int reg)
Get the size of a PCI BAR.
Definition: pci.c:164
An EHCI endpoint.
Definition: ehci.h:529
static int ehci_legacy_prevent_release
Prevent the release of ownership back to BIOS.
Definition: ehci.c:240
char name[40]
Name.
Definition: device.h:79
struct usb_bus * bus
USB bus.
Definition: ehci.h:525
int32_t before
Initial microcode version.
Definition: ucode.h:16
Definition: bnxt_hsi.h:68
long index
Definition: bigint.h:65
static int ehci_ring_alloc(struct ehci_device *ehci, struct ehci_ring *ring)
Allocate transfer descriptor ring.
Definition: ehci.c:549
#define EHCI_USBSTS_ASYNC_ADVANCE
Asynchronous schedule advanced.
Definition: ehci.h:139
#define EHCI_PORT_RESET_MAX_WAIT_MS
Maximum time to wait for a port reset to complete.
Definition: ehci.h:462
uint32_t link
First queue head.
Definition: ehci.h:226
struct usb_device * usb
Currently attached device (if in use)
Definition: usb.h:835
unsigned long long uint64_t
Definition: stdint.h:13
struct list_head schedule
Endpoint schedule.
Definition: ehci.h:537
static void ehci_shutdown(int booting)
Prepare for exit.
Definition: ehci.c:2087
uint8_t head
Head number.
Definition: int13.h:34
#define EHCI_LINK_TYPE_QH
Queue head type.
Definition: ehci.h:221
#define EHCI_LEN_MASK
Length mask.
Definition: ehci.h:286
static void ehci_bus_close(struct usb_bus *bus)
Close USB bus.
Definition: ehci.c:1854
static void ehci_root_poll(struct usb_hub *hub, struct usb_port *port)
Poll for port status changes.
Definition: ehci.c:1749
static int ehci_hub_open(struct usb_hub *hub __unused)
Open hub.
Definition: ehci.c:1527
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition: pci.c:241
const char * name
Definition: init.h:44
#define EHCI_USBLEGSUP_CTLSTS
USB legacy support control/status.
Definition: ehci.h:85
struct device dev
Generic device.
Definition: pci.h:213
unsigned int address
Device address, if assigned.
Definition: usb.h:733
static void ehci_hub_close(struct usb_hub *hub __unused)
Close hub.
Definition: ehci.c:1538
uint32_t next
Next transfer descriptor.
Definition: ehci.h:232
unsigned int speed
Device speed.
Definition: usb.h:729
An EHCI transfer.
Definition: ehci.h:465
#define ECANCELED
Operation canceled.
Definition: errno.h:344
#define ENOTSUP
Operation not supported.
Definition: errno.h:590
#define STARTUP_LATE
Late startup.
Definition: init.h:66
void usb_port_changed(struct usb_port *port)
Report port status change.
Definition: usb.c:1858
A hardware device description.
Definition: device.h:20
#define PCI_CLASS_SERIAL
Definition: Pci22.h:267
Dynamic memory allocation.
#define EHCI_CHR_ENDPOINT(address)
Endpoint number.
Definition: ehci.h:309
#define EHCI_CAP_SPLIT_SCHED_DEFAULT
Default split completion schedule mask.
Definition: ehci.h:355
#define EHCI_PORTSC_CHANGE
Port status change mask.
Definition: ehci.h:211
static void usb_bus_set_hostdata(struct usb_bus *bus, void *priv)
Set USB bus host controller private data.
Definition: usb.h:1051
struct ehci_queue_head * head
Queue head.
Definition: ehci.h:383
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:137
#define EHCI_MTU
Maximum transfer size.
Definition: ehci.h:31
#define EHCI_USBLEGSUP_MAX_WAIT_MS
Maximum time to wait for BIOS to release ownership.
Definition: ehci.h:438
unsigned long tmp
Definition: linux_pci.h:65
struct ehci_queue_head * head
Asynchronous queue head.
Definition: ehci.h:510
#define EHCI_PORTSC_PED
Port enabled.
Definition: ehci.h:187
static int usb_set_address(struct usb_device *usb, unsigned int address)
Set address.
Definition: usb.h:1159
struct io_buffer ** iobuf
I/O buffers.
Definition: ehci.h:380
#define for_each_usb_bus(bus)
Iterate over all USB buses.
Definition: usb.h:1077
#define EHCI_RESET_MAX_WAIT_MS
Maximum time to wait for reset to complete.
Definition: ehci.h:456
A startup/shutdown function.
Definition: init.h:43
#define list_del(list)
Delete an entry from a list.
Definition: list.h:120
unsigned int legacy
USB legacy support capability (if present and enabled)
Definition: ehci.h:505
struct ena_llq_option desc
Descriptor counts.
Definition: ena.h:20
#define EHCI_OP_USBCMD
USB command register.
Definition: ehci.h:88
A USB port.
Definition: usb.h:813
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition: pci.h:366
uint16_t busdevfn
PCI bus:dev.fn address.
Definition: ena.h:28
#define rmb()
Definition: io.h:545
#define ENOMEM
Not enough space.
Definition: errno.h:535
A USB endpoint.
Definition: usb.h:404
struct usb_device * usb
Underlying USB device, if any.
Definition: usb.h:847
static int ehci_root_open(struct usb_hub *hub)
Open root hub.
Definition: ehci.c:1556
static signed char phys[4]
Definition: epic100.c:88
struct ehci_device * ehci
EHCI device.
Definition: ehci.h:531
u8 port
Port number.
Definition: CIB_PRM.h:31
static int ehci_endpoint_mtu(struct usb_endpoint *ep)
Update MTU.
Definition: ehci.c:1235
FILE_SECBOOT(PERMITTED)
#define BUS_TYPE_PCI
PCI bus type.
Definition: device.h:44
#define EHCI_PORTSC_CCS
Current connect status.
Definition: ehci.h:181
void unregister_usb_bus(struct usb_bus *bus)
Unregister USB bus.
Definition: usb.c:2171
struct usb_port * port
USB port.
Definition: usb.h:727
int addr64
64-bit addressing capability
Definition: ehci.h:498
unsigned int prod
Producer counter.
Definition: ehci.h:372
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define EHCI_USBSTS_USBERRINT
USB error interrupt.
Definition: ehci.h:127
struct device * dev
Underlying hardware device.
Definition: usb.h:970
#define EHCI_PORTSC_PP
Port power.
Definition: ehci.h:205
int pci_read_config_dword(struct pci_device *pci, unsigned int where, uint32_t *value)
Read 32-bit dword from PCI configuration space.
Not connected.
Definition: usb.h:47
struct usb_endpoint * ep
USB endpoint.
Definition: ehci.h:533
#define PCI_FIRST_FUNC(busdevfn)
Definition: pci.h:287
#define EHCI_ASYNC_ADVANCE_MAX_WAIT_MS
Maximum time to wait for asynchronous schedule to advance.
Definition: ehci.h:444
#define EHCI_MIN_ALIGN
Minimum alignment required for data structures.
Definition: ehci.h:24
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition: list.h:432
#define EHCI_BAR
EHCI PCI BAR.
Definition: ehci.h:37
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
void * cap
Capability registers.
Definition: ehci.h:491
#define EHCI_USBCMD_FLSIZE(flsize)
Frame list size.
Definition: ehci.h:97
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:94
#define EHCI_FL_IOC
Interrupt on completion.
Definition: ehci.h:283
static int ehci_probe(struct pci_device *pci)
Probe PCI device.
Definition: ehci.c:1969
ring len
Length.
Definition: dwmac.h:231
void writel(uint32_t data, volatile uint32_t *io_addr)
Write 32-bit dword to memory-mapped device.
#define list_for_each_entry_reverse(pos, head, member)
Iterate over entries in a list in reverse order.
Definition: list.h:445
unsigned int burst
Maximum burst size.
Definition: usb.h:414
#define EHCI_PAGE_ALIGN
Page-alignment required for some data structures.
Definition: ehci.h:34
static void ehci_periodic_add(struct ehci_endpoint *endpoint)
Add endpoint to periodic schedule.
Definition: ehci.c:947
#define PCI_CLASS(base, sub, progif)
Construct PCI class.
Definition: pci.h:167
u32 link
Link to next descriptor.
Definition: ar9003_mac.h:25
static void ehci_remove(struct pci_device *pci)
Remove PCI device.
Definition: ehci.c:2053
static unsigned int count
Number of entries.
Definition: dwmac.h:225
unsigned long pci_bar_start(struct pci_device *pci, unsigned int reg)
Find the start of a PCI BAR.
Definition: pci.c:97
unsigned int flsize
Frame list size.
Definition: ehci.h:500
struct ehci_transfer_descriptor cache
Transfer descriptor cache.
Definition: ehci.h:302
static void ehci_endpoint_poll(struct ehci_endpoint *endpoint)
Poll for completions.
Definition: ehci.c:1376
#define EHCI_FLSIZE_SMALL
Smallest allowed frame list size.
Definition: ehci.h:106
#define EHCI_USBSTS_HCH
Host controller halted.
Definition: ehci.h:148
const char * name
Name.
Definition: ehci.h:488
static struct usb_host_operations ehci_operations
USB host controller operations.
Definition: ehci.c:1930
#define USB_DIR_IN
Data transfer is from device to host.
Definition: usb.h:98
#define EHCI_CAP_MULT(mult)
High-bandwidth pipe multiplier.
Definition: ehci.h:367
static unsigned int ehci_extended_capability(struct ehci_device *ehci, struct pci_device *pci, unsigned int id, unsigned int offset)
Find extended capability.
Definition: ehci.c:106
char name[32]
Name.
Definition: usb.h:725
struct ehci_ring ring
Transfer descriptor ring.
Definition: ehci.h:540
unsigned int location
Location.
Definition: device.h:30
static int ehci_device_address(struct usb_device *usb)
Assign device address.
Definition: ehci.c:1476
#define EHCI_USBCMD_ASYNC
Asynchronous schedule enable.
Definition: ehci.h:115
#define EHCI_FL_PID_IN
IN token.
Definition: ehci.h:271
#define cpu_to_le32(value)
Definition: byteswap.h:108
A USB device.
Definition: usb.h:723
A queue head.
Definition: ehci.h:292
#define EHCI_PORTSC_LINE_STATUS_LOW
Line status: low-speed device.
Definition: ehci.h:202
static __unused void ehci_dump(struct ehci_device *ehci)
Dump host controller registers.
Definition: ehci.c:189
static unsigned int ehci_endpoint_count(size_t len, int zlp)
Calculate number of transfer descriptors.
Definition: ehci.c:1303
static void usb_poll(struct usb_bus *bus)
Poll USB bus.
Definition: usb.h:1072
static struct usb_endpoint * usb_endpoint(struct usb_device *usb, unsigned int address)
Get USB endpoint.
Definition: usb.h:806
USB Enhanced Host Controller Interface (EHCI) driver.
#define iob_unput(iobuf, len)
Definition: iobuf.h:140
#define USB_ENDPOINT_ATTR_INTERRUPT
Interrupt endpoint transfer type.
Definition: usb.h:293
#define EHCI_OP_USBINTR
USB interrupt enable register.
Definition: ehci.h:157
uint8_t flags
Flags.
Definition: ena.h:18
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
#define EHCI_PORT_POWER_DELAY_MS
Time to delay after enabling power to a port.
Definition: ehci.h:426
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:55
int pci_write_config_byte(struct pci_device *pci, unsigned int where, uint8_t value)
Write byte to PCI configuration space.
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
size_t len
Length.
Definition: ehci.h:469
static int ehci_reset(struct ehci_device *ehci)
Reset EHCI device.
Definition: ehci.c:503
#define PCI_CLASS_SERIAL_USB
Definition: Pci22.h:273
PCI bus.
#define PCI_CLASS_SERIAL_USB_OHCI
OHCI USB controller.
Definition: pci.h:139
#define EHCI_PORTSC_PR
Port reset.
Definition: ehci.h:196
A PCI device.
Definition: pci.h:211
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:160
#define EHCI_PORTSC_OWNER
Port owner.
Definition: ehci.h:208
#define EHCI_OP_CONFIGFLAG
Configure flag register.
Definition: ehci.h:172
#define EHCI_OP_PORTSC(port)
Port status and control register.
Definition: ehci.h:178
#define EHCI_USBCMD_PERIODIC
Periodic schedule enable.
Definition: ehci.h:112
#define EHCI_HCCPARAMS_FLSIZE(params)
Programmable frame list flag.
Definition: ehci.h:58
static int ehci_schedule_del(struct ehci_endpoint *endpoint)
Remove endpoint from appropriate schedule.
Definition: ehci.c:1007
static unsigned int ehci_ring_remaining(struct ehci_ring *ring)
Calculate space remaining in transfer descriptor ring.
Definition: ehci.h:416
#define EHCI_OP_ASYNCLISTADDR
Current asynchronous list address register.
Definition: ehci.h:169
void usb_free_address(struct usb_bus *bus, unsigned int address)
Free device address.
Definition: usb.c:2284
int pci_read_config(struct pci_device *pci)
Read PCI device configuration.
Definition: pci.c:269
#define PCI_CLASS_SERIAL_USB_EHCI
ECHI USB controller.
Definition: pci.h:140
static void ehci_async_add(struct ehci_endpoint *endpoint)
Add endpoint to asynchronous schedule.
Definition: ehci.c:816
#define EHCI_FL_PID_OUT
OUT token.
Definition: ehci.h:268
#define ENODEV
No such device.
Definition: errno.h:510
static uint32_t ehci_link_qh(struct ehci_queue_head *queue)
Get link value for a queue head.
Definition: ehci.c:780
#define EHCI_PORTSC_LINE_STATUS(portsc)
Line status.
Definition: ehci.h:199
unsigned char uint8_t
Definition: stdint.h:10
#define EHCI_PORTSC_CSC
Connect status change.
Definition: ehci.h:184
static void * usb_get_hostdata(struct usb_device *usb)
Get USB device host controller private data.
Definition: usb.h:795
struct ehci_periodic_frame * frame
Periodic frame list.
Definition: ehci.h:512
unsigned int flags
Flags.
Definition: ehci.h:477
A PCI device ID list entry.
Definition: pci.h:175
#define EHCI_CAP_HCSPARAMS
Structural parameters.
Definition: ehci.h:46
#define le16_to_cpu(value)
Definition: byteswap.h:113
unsigned int uint32_t
Definition: stdint.h:12
uint32_t next
Next descriptor address.
Definition: dwmac.h:22
#define ffs(x)
Find first (i.e.
Definition: strings.h:141
A USB setup data packet.
Definition: usb.h:83
const char * usb_endpoint_name(struct usb_endpoint *ep)
Get USB endpoint name (for debugging)
Definition: usb.c:221
#define EHCI_LINK_TERMINATE
List terminator.
Definition: ehci.h:215
struct i386_regs regs
Definition: registers.h:15
#define EHCI_CAP_TT_PORT(port)
Transaction translator port number.
Definition: ehci.h:364
#define EHCI_OP_CTRLDSSEGMENT
Control data structure segment register.
Definition: ehci.h:163
#define EHCI_CHR_MAX_LEN(len)
Maximum packet length.
Definition: ehci.h:330
#define EHCI_USBSTS_USBINT
USB interrupt.
Definition: ehci.h:124
size_t mtu
Maximum transfer size.
Definition: usb.h:412
#define EHCI_FL_TOGGLE
Set initial data toggle.
Definition: ehci.h:481
uint8_t status
Status.
Definition: ena.h:16
uint16_t request
Request.
Definition: usb.h:85
static void ehci_bus_poll(struct usb_bus *bus)
Poll USB bus.
Definition: ehci.c:1877
static uint32_t ehci_endpoint_characteristics(struct usb_endpoint *ep)
Determine endpoint characteristics.
Definition: ehci.c:1031
static void ehci_async_schedule(struct ehci_device *ehci)
(Re)build asynchronous schedule
Definition: ehci.c:790
#define EHCI_CHR_ADDRESS(address)
Device address.
Definition: ehci.h:306
#define PCI_LAST_FUNC(busdevfn)
Definition: pci.h:288
struct usb_port * usb_transaction_translator(struct usb_device *usb)
Get USB transaction translator.
Definition: usb.c:2372
unsigned long physaddr_t
Definition: stdint.h:20
#define EHCI_USBLEGSUP_OS_OWNED
USB legacy support OS ownership flag.
Definition: ehci.h:82
static void * pci_get_drvdata(struct pci_device *pci)
Get PCI driver-private data.
Definition: pci.h:376
#define EHCI_OP_USBSTS
USB status register.
Definition: ehci.h:121
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition: timer.c:79
#define EHCI_EECP_NEXT(eecp)
Next EHCI extended capability pointer.
Definition: ehci.h:67
#define EHCI_USBSTS_PERIODIC
Periodic schedule enabled.
Definition: ehci.h:142
uint32_t busdevfn
Segment, bus, device, and function (bus:dev.fn) number.
Definition: pci.h:238
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:46
uint8_t status
Status.
Definition: ehci.h:236
#define EHCI_CHR_EPS_FULL
Full-speed endpoint.
Definition: ehci.h:315
static void * usb_bus_get_hostdata(struct usb_bus *bus)
Get USB bus host controller private data.
Definition: usb.h:1062
struct list_head endpoints
List of all endpoints.
Definition: ehci.h:515
#define ENOBUFS
No buffer space available.
Definition: errno.h:499
#define DBGC2(...)
Definition: compiler.h:522
#define EHCI_LEN_TOGGLE
Data toggle.
Definition: ehci.h:289
static void ehci_schedule_add(struct ehci_endpoint *endpoint)
Add endpoint to appropriate schedule.
Definition: ehci.c:990
Universal Serial Bus (USB)
struct startup_fn ehci_startup __startup_fn(STARTUP_LATE)
Startup/shutdown function.
int register_usb_bus(struct usb_bus *bus)
Register USB bus.
Definition: usb.c:2131
#define EHCI_OP_PERIODICLISTBASE
Periodic frame list base address register.
Definition: ehci.h:166
#define EHCI_CAP_HCCPARAMS
Capability parameters.
Definition: ehci.h:52
static void usb_complete(struct usb_endpoint *ep, struct io_buffer *iobuf)
Complete transfer (without error)
Definition: usb.h:1087
unsigned int address
Port address.
Definition: usb.h:817
static void ehci_legacy_init(struct ehci_device *ehci, struct pci_device *pci)
Initialise USB legacy support.
Definition: ehci.c:248
void * data
Start of data.
Definition: iobuf.h:53
static void ehci_init(struct ehci_device *ehci, void *regs)
Initialise device.
Definition: ehci.c:70
static int ehci_ctrl_reachable(struct ehci_device *ehci, void *ptr)
Check control data structure reachability.
Definition: ehci.c:161
struct usb_hub * hub
USB hub.
Definition: usb.h:815
#define EHCI_CHR_EPS_HIGH
High-speed endpoint.
Definition: ehci.h:321
void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
Definition: malloc.c:723
High speed (480Mbps)
Definition: usb.h:53
struct usb_endpoint * ep[32]
Endpoint list.
Definition: usb.h:745
unsigned int interval
Interval (in microframes)
Definition: usb.h:416
unsigned int eecp
EHCI extended capabilities offset.
Definition: ehci.h:502
static int ehci_root_speed(struct usb_hub *hub, struct usb_port *port)
Update root hub port speed.
Definition: ehci.c:1682
uint32_t link
Horizontal link pointer.
Definition: ehci.h:294
struct usb_endpoint_host_operations endpoint
Endpoint operations.
Definition: usb.h:1033
int pci_write_config_dword(struct pci_device *pci, unsigned int where, uint32_t value)
Write 32-bit dword to PCI configuration space.
static void ehci_ring_free(struct ehci_ring *ring)
Free transfer descriptor ring.
Definition: ehci.c:626
#define cpu_to_le16(value)
Definition: byteswap.h:107
#define EHCI_CHR_EPS_LOW
Low-speed endpoint.
Definition: ehci.h:318
#define EHCI_USBLEGSUP_OS
USB legacy support OS owned semaphore.
Definition: ehci.h:79
A transfer descriptor.
Definition: ehci.h:230
static void ehci_legacy_release(struct ehci_device *ehci, struct pci_device *pci)
Release ownership back to BIOS.
Definition: ehci.c:341
void iounmap(volatile const void *io_addr)
Unmap I/O address.
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define PCI_CLASS_SERIAL_USB_UHCI
UHCI USB controller.
Definition: pci.h:138
struct device_description desc
Device description.
Definition: device.h:83
USB host controller operations.
Definition: usb.h:1031
#define USB_ENDPOINT_ATTR_CONTROL
Control endpoint transfer type.
Definition: usb.h:287
#define DBGCP(...)
Definition: compiler.h:539
static unsigned int ehci_ring_fill(struct ehci_ring *ring)
Calculate space used in transfer descriptor ring.
Definition: ehci.h:401
struct usb_hub * hub
Root hub.
Definition: usb.h:995
static int ehci_bus_open(struct usb_bus *bus)
Open USB bus.
Definition: ehci.c:1781
static int ehci_periodic_del(struct ehci_endpoint *endpoint)
Remove endpoint from periodic schedule.
Definition: ehci.c:969
static void usb_hub_set_drvdata(struct usb_hub *hub, void *priv)
Set USB hub driver private data.
Definition: usb.h:937
#define EHCI_CAP_HCIVERSION
Host controller interface version number.
Definition: ehci.h:43
struct usb_device * usb
USB device.
Definition: usb.h:406
void * regs
Registers.
Definition: ehci.h:486
void * data
Data buffer.
Definition: ehci.h:467
#define EHCI_OP_FRINDEX
Frame index register.
Definition: ehci.h:160
static struct usb_port * usb_port(struct usb_hub *hub, unsigned int address)
Get USB port.
Definition: usb.h:960
#define EHCI_STATUS_HALTED
Halted.
Definition: ehci.h:259
struct list_head async
Asynchronous schedule.
Definition: ehci.h:517
static void ehci_device_close(struct usb_device *usb)
Close device.
Definition: ehci.c:1461
#define EHCI_CHR_HEAD
Head of reclamation list flag.
Definition: ehci.h:327
#define EHCI_EECP_ID_LEGACY
USB legacy support extended capability.
Definition: ehci.h:70
#define EHCI_EECP_ID(eecp)
EHCI extended capability ID.
Definition: ehci.h:64
static int ehci_enqueue(struct ehci_device *ehci, struct ehci_ring *ring, struct io_buffer *iobuf, const struct ehci_transfer *xfer, unsigned int count)
Enqueue transfer descriptors.
Definition: ehci.c:655
#define EHCI_FLSIZE_DEFAULT
Default frame list size.
Definition: ehci.h:103
int(* open)(struct usb_endpoint *ep)
Open endpoint.
Definition: usb.h:450
unsigned int ehci_companion(struct pci_device *pci)
Locate EHCI companion controller.
Definition: ehci.c:421
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
#define EHCI_CAP_INTR_SCHED(uframe)
Interrupt schedule mask.
Definition: ehci.h:336
static void pci_init(struct pci_device *pci, unsigned int busdevfn)
Initialise PCI device.
Definition: pci.h:341
static int ehci_root_disable(struct usb_hub *hub, struct usb_port *port)
Disable port.
Definition: ehci.c:1663
static void ehci_root_close(struct usb_hub *hub)
Close root hub.
Definition: ehci.c:1583
void * pci_ioremap(struct pci_device *pci, unsigned long bus_addr, size_t len)
Map PCI bus address as an I/O address.
struct usb_bus * bus
USB bus.
Definition: usb.h:845
#define fls(x)
Find last (i.e.
Definition: strings.h:167
#define EHCI_RING_COUNT
Number of transfer descriptors in a ring.
Definition: ehci.h:392
struct list_head list
List of all endpoints.
Definition: ehci.h:535
uint16_t queue
Queue ID.
Definition: ena.h:22
#define list_check_contains_entry(entry, head, member)
Check list contains a specified entry.
Definition: list.h:550
#define DBG_LOG
Definition: compiler.h:317
#define USB_EP0_ADDRESS
Control endpoint address.
Definition: usb.h:501
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:670
String functions.
int usb_alloc_address(struct usb_bus *bus)
Allocate device address.
Definition: usb.c:2264
static void ehci_poll_companions(struct ehci_device *ehci)
Poll child companion controllers.
Definition: ehci.c:380
#define PCI_ROM(_vendor, _device, _name, _description, _data)
Definition: pci.h:308
A USB bus.
Definition: usb.h:966
unsigned int attributes
Attributes.
Definition: usb.h:410
static void ehci_periodic_schedule(struct ehci_device *ehci)
(Re)build periodic schedule
Definition: ehci.c:877
uint8_t bus
Bus.
Definition: edd.h:15
struct pci_driver ehci_driver __pci_driver
EHCI PCI driver.
Definition: ehci.c:2073
Full speed (12Mbps)
Definition: usb.h:51
struct ehci_transfer_descriptor * desc
Transfer descriptors.
Definition: ehci.h:385
unsigned int address
Endpoint address.
Definition: usb.h:408
void free_usb_bus(struct usb_bus *bus)
Free USB bus.
Definition: usb.c:2195
#define EHCI_CONFIGFLAG_CF
Configure flag.
Definition: ehci.h:175
void usb_complete_err(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete transfer (possibly with error)
Definition: usb.c:587
static void * usb_hub_get_drvdata(struct usb_hub *hub)
Get USB hub driver private data.
Definition: usb.h:948
uint32_t chr
Endpoint characteristics.
Definition: ehci.h:296
void * malloc_phys(size_t size, size_t phys_align)
Allocate memory with specified physical alignment.
Definition: malloc.c:707
static void * usb_endpoint_get_hostdata(struct usb_endpoint *ep)
Get USB endpoint host controller private data.
Definition: usb.h:587
An EHCI device.
Definition: ehci.h:484
#define EHCI_USBCMD_FLSIZE_MASK
Frame list size mask.
Definition: ehci.h:100
String functions.
static int ehci_endpoint_open(struct usb_endpoint *ep)
Open endpoint.
Definition: ehci.c:1119
#define EHCI_STOP_MAX_WAIT_MS
Maximum time to wait for host controller to stop.
Definition: ehci.h:450
static int ehci_root_enable(struct usb_hub *hub, struct usb_port *port)
Enable port.
Definition: ehci.c:1597
void * memset(void *dest, int character, size_t len) __nonnull
int pci_read_config_byte(struct pci_device *pci, unsigned int where, uint8_t *value)
Read byte from PCI configuration space.
A persistent I/O buffer.
Definition: iobuf.h:38
static int ehci_async_del(struct ehci_endpoint *endpoint)
Remove endpoint from asynchronous schedule.
Definition: ehci.c:832