iPXE
uhci.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2015 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
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25FILE_SECBOOT ( PERMITTED );
26
27#include <strings.h>
28#include <unistd.h>
29#include <errno.h>
30#include <byteswap.h>
31#include <ipxe/malloc.h>
32#include <ipxe/pci.h>
33#include <ipxe/usb.h>
34#include "ehci.h"
35#include "uhci.h"
36
37/** @file
38 *
39 * USB Universal Host Controller Interface (UHCI) driver
40 *
41 */
42
43/******************************************************************************
44 *
45 * Register access
46 *
47 ******************************************************************************
48 */
49
50/**
51 * Check that address is reachable
52 *
53 * @v addr Address
54 * @v len Length
55 * @ret rc Return status code
56 */
57static inline __attribute__ (( always_inline)) int
58uhci_reachable ( void *addr, size_t len ) {
59 physaddr_t phys = virt_to_phys ( addr );
60
61 /* Always reachable in a 32-bit build */
62 if ( sizeof ( physaddr_t ) <= sizeof ( uint32_t ) )
63 return 0;
64
65 /* Reachable if below 4GB */
66 if ( ( ( phys + len - 1 ) & ~0xffffffffULL ) == 0 )
67 return 0;
68
69 return -ENOTSUP;
70}
71
72/******************************************************************************
73 *
74 * Run / stop / reset
75 *
76 ******************************************************************************
77 */
78
79/**
80 * Start UHCI device
81 *
82 * @v uhci UHCI device
83 */
84static void uhci_run ( struct uhci_device *uhci ) {
85 uint16_t usbcmd;
86
87 /* Set run/stop bit */
88 usbcmd = inw ( uhci->regs + UHCI_USBCMD );
89 usbcmd |= ( UHCI_USBCMD_RUN | UHCI_USBCMD_MAX64 );
90 outw ( usbcmd, uhci->regs + UHCI_USBCMD );
91}
92
93/**
94 * Stop UHCI device
95 *
96 * @v uhci UHCI device
97 * @ret rc Return status code
98 */
99static int uhci_stop ( struct uhci_device *uhci ) {
100 uint16_t usbcmd;
101 uint16_t usbsts;
102 unsigned int i;
103
104 /* Clear run/stop bit */
105 usbcmd = inw ( uhci->regs + UHCI_USBCMD );
106 usbcmd &= ~UHCI_USBCMD_RUN;
107 outw ( usbcmd, uhci->regs + UHCI_USBCMD );
108
109 /* Wait for device to stop */
110 for ( i = 0 ; i < UHCI_STOP_MAX_WAIT_MS ; i++ ) {
111
112 /* Check if device is stopped */
113 usbsts = inw ( uhci->regs + UHCI_USBSTS );
114 if ( usbsts & UHCI_USBSTS_HCHALTED )
115 return 0;
116
117 /* Delay */
118 mdelay ( 1 );
119 }
120
121 DBGC ( uhci, "UHCI %s timed out waiting for stop\n", uhci->name );
122 return -ETIMEDOUT;
123}
124
125/**
126 * Reset UHCI device
127 *
128 * @v uhci UHCI device
129 * @ret rc Return status code
130 */
131static int uhci_reset ( struct uhci_device *uhci ) {
132 uint16_t usbcmd;
133 unsigned int i;
134 int rc;
135
136 /* The UHCI specification states that resetting a running
137 * device may result in undefined behaviour, so try stopping
138 * it first.
139 */
140 if ( ( rc = uhci_stop ( uhci ) ) != 0 ) {
141 /* Ignore errors and attempt to reset the device anyway */
142 }
143
144 /* Reset device */
146
147 /* Wait for reset to complete */
148 for ( i = 0 ; i < UHCI_RESET_MAX_WAIT_MS ; i++ ) {
149
150 /* Check if reset is complete */
151 usbcmd = inw ( uhci->regs + UHCI_USBCMD );
152 if ( ! ( usbcmd & UHCI_USBCMD_HCRESET ) )
153 return 0;
154
155 /* Delay */
156 mdelay ( 1 );
157 }
158
159 DBGC ( uhci, "UHCI %s timed out waiting for reset\n", uhci->name );
160 return -ETIMEDOUT;
161}
162
163/******************************************************************************
164 *
165 * Transfer descriptor rings
166 *
167 ******************************************************************************
168 */
169
170/**
171 * Allocate transfer ring
172 *
173 * @v ring Transfer ring
174 * @ret rc Return status code
175 */
176static int uhci_ring_alloc ( struct uhci_ring *ring ) {
177 int rc;
178
179 /* Initialise structure */
180 memset ( ring, 0, sizeof ( *ring ) );
181
182 /* Allocate queue head */
183 ring->head = malloc_phys ( sizeof ( *ring->head ), UHCI_ALIGN );
184 if ( ! ring->head ) {
185 rc = -ENOMEM;
186 goto err_alloc;
187 }
188 if ( ( rc = uhci_reachable ( ring->head,
189 sizeof ( *ring->head ) ) ) != 0 )
190 goto err_unreachable;
191
192 /* Initialise queue head */
194
195 return 0;
196
197 err_unreachable:
198 free_phys ( ring->head, sizeof ( *ring->head ) );
199 err_alloc:
200 return rc;
201}
202
203/**
204 * Free transfer ring
205 *
206 * @v ring Transfer ring
207 */
208static void uhci_ring_free ( struct uhci_ring *ring ) {
209 unsigned int i;
210
211 /* Sanity checks */
212 assert ( uhci_ring_fill ( ring ) == 0 );
213 for ( i = 0 ; i < UHCI_RING_COUNT ; i++ )
214 assert ( ring->xfer[i] == NULL );
215
216 /* Free queue head */
217 free_phys ( ring->head, sizeof ( *ring->head ) );
218}
219
220/**
221 * Enqueue new transfer
222 *
223 * @v ring Transfer ring
224 * @v iobuf I/O buffer
225 * @v count Number of descriptors
226 * @ret rc Return status code
227 */
228static int uhci_enqueue ( struct uhci_ring *ring, struct io_buffer *iobuf,
229 unsigned int count ) {
230 struct uhci_transfer *xfer;
231 struct uhci_transfer *end;
233 unsigned int index = ( ring->prod % UHCI_RING_COUNT );
235 size_t len;
236 int rc;
237
238 /* Sanity check */
239 assert ( count > 0 );
240 assert ( iobuf != NULL );
241
242 /* Check for space in ring */
243 if ( ! uhci_ring_remaining ( ring ) ) {
244 rc = -ENOBUFS;
245 goto err_ring_full;
246 }
247
248 /* Check for reachability of I/O buffer */
249 if ( ( rc = uhci_reachable ( iobuf->data, iob_len ( iobuf ) ) ) != 0 )
250 goto err_unreachable_iobuf;
251
252 /* Allocate transfer */
253 xfer = malloc ( sizeof ( *xfer ) );
254 if ( ! xfer ) {
255 rc = -ENOMEM;
256 goto err_alloc_xfer;
257 }
258
259 /* Initialise transfer */
260 xfer->prod = 0;
261 xfer->cons = 0;
262 xfer->len = 0;
263 xfer->iobuf = iobuf;
264
265 /* Allocate transfer descriptors */
266 len = ( count * sizeof ( xfer->desc[0] ) );
267 xfer->desc = malloc_phys ( len, UHCI_ALIGN );
268 if ( ! xfer->desc ) {
269 rc = -ENOMEM;
270 goto err_alloc_desc;
271 }
272 if ( ( rc = uhci_reachable ( xfer->desc, len ) ) != 0 )
273 goto err_unreachable_desc;
274
275 /* Initialise transfer descriptors */
276 memset ( xfer->desc, 0, len );
277 desc = xfer->desc;
278 for ( ; --count ; desc++ ) {
279 link = ( virt_to_phys ( desc + 1 ) | UHCI_LINK_DEPTH_FIRST );
280 desc->link = cpu_to_le32 ( link );
281 desc->flags = ring->flags;
282 }
284 desc->flags = ( ring->flags | UHCI_FL_IOC );
285
286 /* Add to ring */
287 wmb();
288 link = virt_to_phys ( xfer->desc );
289 if ( uhci_ring_fill ( ring ) > 0 ) {
290 end = ring->end;
291 end->desc[ end->prod - 1 ].link = cpu_to_le32 ( link );
292 } else {
293 ring->head->current = cpu_to_le32 ( link );
294 }
295 assert ( ring->xfer[index] == NULL );
296 ring->xfer[index] = xfer;
297 ring->end = xfer;
298 ring->prod++;
299
300 return 0;
301
302 err_unreachable_desc:
303 free_phys ( xfer->desc, len );
304 err_alloc_desc:
305 free ( xfer );
306 err_alloc_xfer:
307 err_unreachable_iobuf:
308 err_ring_full:
309 return rc;
310}
311
312/**
313 * Describe transfer
314 *
315 * @v ring Transfer ring
316 * @v data Data
317 * @v len Length of data
318 * @v pid Packet ID
319 */
320static void uhci_describe ( struct uhci_ring *ring, void *data,
321 size_t len, uint8_t pid ) {
322 struct uhci_transfer *xfer = ring->end;
324 size_t frag_len;
326
327 do {
328 /* Calculate fragment length */
329 frag_len = len;
330 if ( frag_len > ring->mtu )
331 frag_len = ring->mtu;
332
333 /* Populate descriptor */
334 desc = &xfer->desc[xfer->prod++];
335 if ( pid == USB_PID_IN )
336 desc->flags |= UHCI_FL_SPD;
337 control = ( ring->control | UHCI_CONTROL_PID ( pid ) |
338 UHCI_CONTROL_LEN ( frag_len ) );
339 desc->control = cpu_to_le32 ( control );
340 if ( data )
341 desc->data = virt_to_phys ( data );
342 wmb();
343 desc->status = UHCI_STATUS_ACTIVE;
344
345 /* Update data toggle */
347
348 /* Move to next descriptor */
349 data += frag_len;
350 len -= frag_len;
351
352 } while ( len );
353}
354
355/**
356 * Dequeue transfer
357 *
358 * @v ring Transfer ring
359 * @ret iobuf I/O buffer
360 */
361static struct io_buffer * uhci_dequeue ( struct uhci_ring *ring ) {
362 unsigned int index = ( ring->cons % UHCI_RING_COUNT );
363 struct io_buffer *iobuf;
364 struct uhci_transfer *xfer;
365 size_t len;
366
367 /* Sanity checks */
368 assert ( uhci_ring_fill ( ring ) > 0 );
369
370 /* Consume transfer */
371 xfer = ring->xfer[index];
372 assert ( xfer != NULL );
373 assert ( xfer->desc != NULL );
374 iobuf = xfer->iobuf;
375 assert ( iobuf != NULL );
376 ring->xfer[index] = NULL;
377 ring->cons++;
378
379 /* Free transfer descriptors */
380 len = ( xfer->prod * sizeof ( xfer->desc[0] ) );
381 free_phys ( xfer->desc, len );
382
383 /* Free transfer */
384 free ( xfer );
385
386 return iobuf;
387}
388
389/**
390 * Restart ring
391 *
392 * @v ring Transfer ring
393 * @v toggle Expected data toggle for next descriptor
394 */
395static void uhci_restart ( struct uhci_ring *ring, uint32_t toggle ) {
396 struct uhci_transfer *xfer;
400 unsigned int i;
401 unsigned int j;
402
403 /* Sanity check */
405
406 /* If ring is empty, then just update the data toggle for the
407 * next descriptor.
408 */
409 if ( uhci_ring_fill ( ring ) == 0 ) {
411 ring->control |= toggle;
412 return;
413 }
414
415 /* If expected toggle does not match the toggle in the first
416 * unconsumed descriptor, then invert all toggles.
417 */
418 xfer = ring->xfer[ ring->cons % UHCI_RING_COUNT ];
419 assert ( xfer != NULL );
420 assert ( xfer->cons == 0 );
421 first = &xfer->desc[0];
422 if ( ( le32_to_cpu ( first->control ) ^ toggle ) & UHCI_CONTROL_TOGGLE){
423
424 /* Invert toggle on all unconsumed transfer descriptors */
425 for ( i = ring->cons ; i != ring->prod ; i++ ) {
426 xfer = ring->xfer[ i % UHCI_RING_COUNT ];
427 assert ( xfer != NULL );
428 assert ( xfer->cons == 0 );
429 for ( j = 0 ; j < xfer->prod ; j++ ) {
430 desc = &xfer->desc[j];
431 desc->control ^=
433 }
434 }
435
436 /* Invert toggle for next descriptor to be enqueued */
438 }
439
440 /* Restart ring at first unconsumed transfer */
441 link = virt_to_phys ( first );
442 wmb();
443 ring->head->current = cpu_to_le32 ( link );
444}
445
446/******************************************************************************
447 *
448 * Schedule management
449 *
450 ******************************************************************************
451 */
452
453/**
454 * Get link value for a queue head
455 *
456 * @v queue Queue head
457 * @ret link Link value
458 */
459static inline uint32_t uhci_link_qh ( struct uhci_queue_head *queue ) {
460
461 return ( virt_to_phys ( queue ) | UHCI_LINK_TYPE_QH );
462}
463
464/**
465 * (Re)build asynchronous schedule
466 *
467 * @v uhci UHCI device
468 */
469static void uhci_async_schedule ( struct uhci_device *uhci ) {
470 struct uhci_endpoint *endpoint;
471 struct uhci_queue_head *queue;
474
475 /* Build schedule in reverse order of execution. Provided
476 * that we only ever add or remove single endpoints, this can
477 * safely run concurrently with hardware execution of the
478 * schedule.
479 */
480 link = end = uhci_link_qh ( uhci->head );
481 list_for_each_entry_reverse ( endpoint, &uhci->async, schedule ) {
482 queue = endpoint->ring.head;
483 queue->link = cpu_to_le32 ( link );
484 wmb();
485 link = uhci_link_qh ( queue );
486 }
487 if ( link == end )
489 uhci->head->link = cpu_to_le32 ( link );
490 wmb();
491}
492
493/**
494 * Add endpoint to asynchronous schedule
495 *
496 * @v endpoint Endpoint
497 */
498static void uhci_async_add ( struct uhci_endpoint *endpoint ) {
499 struct uhci_device *uhci = endpoint->uhci;
500
501 /* Add to end of schedule */
502 list_add_tail ( &endpoint->schedule, &uhci->async );
503
504 /* Rebuild schedule */
505 uhci_async_schedule ( uhci );
506}
507
508/**
509 * Remove endpoint from asynchronous schedule
510 *
511 * @v endpoint Endpoint
512 */
513static void uhci_async_del ( struct uhci_endpoint *endpoint ) {
514 struct uhci_device *uhci = endpoint->uhci;
515
516 /* Remove from schedule */
517 list_check_contains_entry ( endpoint, &uhci->async, schedule );
518 list_del ( &endpoint->schedule );
519
520 /* Rebuild schedule */
521 uhci_async_schedule ( uhci );
522
523 /* Delay for a whole USB frame (with a 100% safety margin) */
524 mdelay ( 2 );
525}
526
527/**
528 * (Re)build periodic schedule
529 *
530 * @v uhci UHCI device
531 */
532static void uhci_periodic_schedule ( struct uhci_device *uhci ) {
533 struct uhci_endpoint *endpoint;
534 struct uhci_queue_head *queue;
537 unsigned int max_interval;
538 unsigned int i;
539
540 /* Build schedule in reverse order of execution. Provided
541 * that we only ever add or remove single endpoints, this can
542 * safely run concurrently with hardware execution of the
543 * schedule.
544 */
545 DBGCP ( uhci, "UHCI %s periodic schedule: ", uhci->name );
546 link = end = uhci_link_qh ( uhci->head );
547 list_for_each_entry_reverse ( endpoint, &uhci->periodic, schedule ) {
548 queue = endpoint->ring.head;
549 queue->link = cpu_to_le32 ( link );
550 wmb();
551 DBGCP ( uhci, "%s%d", ( ( link == end ) ? "" : "<-" ),
552 endpoint->ep->interval );
553 link = uhci_link_qh ( queue );
554 }
555 DBGCP ( uhci, "\n" );
556
557 /* Populate periodic frame list */
558 DBGCP ( uhci, "UHCI %s periodic frame list:", uhci->name );
559 for ( i = 0 ; i < UHCI_FRAMES ; i++ ) {
560
561 /* Calculate maximum interval (in microframes) which
562 * may appear as part of this frame list.
563 */
564 if ( i == 0 ) {
565 /* Start of list: include all endpoints */
566 max_interval = -1U;
567 } else {
568 /* Calculate highest power-of-two frame interval */
569 max_interval = ( 1 << ( ffs ( i ) - 1 ) );
570 /* Convert to microframes */
571 max_interval <<= 3;
572 /* Round up to nearest 2^n-1 */
573 max_interval = ( ( max_interval << 1 ) - 1 );
574 }
575
576 /* Find first endpoint in schedule satisfying this
577 * maximum interval constraint.
578 */
579 link = uhci_link_qh ( uhci->head );
580 list_for_each_entry ( endpoint, &uhci->periodic, schedule ) {
581 if ( endpoint->ep->interval <= max_interval ) {
582 queue = endpoint->ring.head;
583 link = uhci_link_qh ( queue );
584 DBGCP ( uhci, " %d:%d",
585 i, endpoint->ep->interval );
586 break;
587 }
588 }
589 uhci->frame->link[i] = cpu_to_le32 ( link );
590 }
591 wmb();
592 DBGCP ( uhci, "\n" );
593}
594
595/**
596 * Add endpoint to periodic schedule
597 *
598 * @v endpoint Endpoint
599 */
600static void uhci_periodic_add ( struct uhci_endpoint *endpoint ) {
601 struct uhci_device *uhci = endpoint->uhci;
602 struct uhci_endpoint *before;
603 unsigned int interval = endpoint->ep->interval;
604
605 /* Find first endpoint with a smaller interval */
607 if ( before->ep->interval < interval )
608 break;
609 }
610 list_add_tail ( &endpoint->schedule, &before->schedule );
611
612 /* Rebuild schedule */
614}
615
616/**
617 * Remove endpoint from periodic schedule
618 *
619 * @v endpoint Endpoint
620 */
621static void uhci_periodic_del ( struct uhci_endpoint *endpoint ) {
622 struct uhci_device *uhci = endpoint->uhci;
623
624 /* Remove from schedule */
625 list_check_contains_entry ( endpoint, &uhci->periodic, schedule );
626 list_del ( &endpoint->schedule );
627
628 /* Rebuild schedule */
629 uhci_periodic_schedule ( uhci );
630
631 /* Delay for a whole USB frame (with a 100% safety margin) */
632 mdelay ( 2 );
633}
634
635/**
636 * Add endpoint to appropriate schedule
637 *
638 * @v endpoint Endpoint
639 */
640static void uhci_schedule_add ( struct uhci_endpoint *endpoint ) {
641 struct usb_endpoint *ep = endpoint->ep;
642 unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
643
645 uhci_periodic_add ( endpoint );
646 } else {
647 uhci_async_add ( endpoint );
648 }
649}
650
651/**
652 * Remove endpoint from appropriate schedule
653 *
654 * @v endpoint Endpoint
655 */
656static void uhci_schedule_del ( struct uhci_endpoint *endpoint ) {
657 struct usb_endpoint *ep = endpoint->ep;
658 unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
659
661 uhci_periodic_del ( endpoint );
662 } else {
663 uhci_async_del ( endpoint );
664 }
665}
666
667/******************************************************************************
668 *
669 * Endpoint operations
670 *
671 ******************************************************************************
672 */
673
674/**
675 * Open endpoint
676 *
677 * @v ep USB endpoint
678 * @ret rc Return status code
679 */
680static int uhci_endpoint_open ( struct usb_endpoint *ep ) {
681 struct usb_device *usb = ep->usb;
682 struct uhci_device *uhci = usb_get_hostdata ( usb );
683 struct uhci_endpoint *endpoint;
684 int rc;
685
686 /* Allocate and initialise structure */
687 endpoint = zalloc ( sizeof ( *endpoint ) );
688 if ( ! endpoint ) {
689 rc = -ENOMEM;
690 goto err_alloc;
691 }
692 endpoint->uhci = uhci;
693 endpoint->ep = ep;
694 usb_endpoint_set_hostdata ( ep, endpoint );
695
696 /* Initialise descriptor ring */
697 if ( ( rc = uhci_ring_alloc ( &endpoint->ring ) ) != 0 )
698 goto err_ring_alloc;
699 endpoint->ring.mtu = ep->mtu;
700 endpoint->ring.flags = UHCI_FL_CERR_MAX;
701 if ( usb->speed < USB_SPEED_FULL )
702 endpoint->ring.flags |= UHCI_FL_LS;
703 endpoint->ring.control = ( UHCI_CONTROL_DEVICE ( usb->address ) |
705
706 /* Add to list of endpoints */
707 list_add_tail ( &endpoint->list, &uhci->endpoints );
708
709 /* Add to schedule */
710 uhci_schedule_add ( endpoint );
711
712 return 0;
713
714 uhci_ring_free ( &endpoint->ring );
715 err_ring_alloc:
716 free ( endpoint );
717 err_alloc:
718 return rc;
719}
720
721/**
722 * Close endpoint
723 *
724 * @v ep USB endpoint
725 */
726static void uhci_endpoint_close ( struct usb_endpoint *ep ) {
727 struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
728 struct io_buffer *iobuf;
729
730 /* Remove from schedule */
731 uhci_schedule_del ( endpoint );
732
733 /* Cancel any incomplete transfers */
734 while ( uhci_ring_fill ( &endpoint->ring ) ) {
735 iobuf = uhci_dequeue ( &endpoint->ring );
736 if ( iobuf )
737 usb_complete_err ( ep, iobuf, -ECANCELED );
738 }
739
740 /* Remove from list of endpoints */
741 list_del ( &endpoint->list );
742
743 /* Free descriptor ring */
744 uhci_ring_free ( &endpoint->ring );
745
746 /* Free endpoint */
747 free ( endpoint );
748}
749
750/**
751 * Reset endpoint
752 *
753 * @v ep USB endpoint
754 * @ret rc Return status code
755 */
756static int uhci_endpoint_reset ( struct usb_endpoint *ep ) {
757 struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
758 struct uhci_ring *ring = &endpoint->ring;
759
760 /* Restart ring */
761 uhci_restart ( ring, 0 );
762
763 return 0;
764}
765
766/**
767 * Update MTU
768 *
769 * @v ep USB endpoint
770 * @ret rc Return status code
771 */
772static int uhci_endpoint_mtu ( struct usb_endpoint *ep ) {
773 struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
774
775 /* Update endpoint MTU */
776 endpoint->ring.mtu = ep->mtu;
777
778 return 0;
779}
780
781/**
782 * Enqueue message transfer
783 *
784 * @v ep USB endpoint
785 * @v iobuf I/O buffer
786 * @ret rc Return status code
787 */
789 struct io_buffer *iobuf ) {
790 struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
791 struct uhci_ring *ring = &endpoint->ring;
792 struct usb_setup_packet *packet;
793 unsigned int count;
794 size_t len;
795 int input;
796 int rc;
797
798 /* Calculate number of descriptors */
799 assert ( iob_len ( iobuf ) >= sizeof ( *packet ) );
800 len = ( iob_len ( iobuf ) - sizeof ( *packet ) );
801 count = ( 1 /* setup stage */ +
802 ( ( len + ring->mtu - 1 ) / ring->mtu ) /* data stage */ +
803 1 /* status stage */ );
804
805 /* Enqueue transfer */
806 if ( ( rc = uhci_enqueue ( ring, iobuf, count ) ) != 0 )
807 return rc;
808
809 /* Describe setup stage */
810 packet = iobuf->data;
812 uhci_describe ( ring, packet, sizeof ( *packet ), USB_PID_SETUP );
813 iob_pull ( iobuf, sizeof ( *packet ) );
814
815 /* Describe data stage, if applicable */
817 input = ( packet->request & cpu_to_le16 ( USB_DIR_IN ) );
818 if ( len ) {
819 uhci_describe ( ring, iobuf->data, len,
820 ( input ? USB_PID_IN : USB_PID_OUT ) );
821 }
822
823 /* Describe status stage */
825 uhci_describe ( ring, NULL, 0,
826 ( ( len && input ) ? USB_PID_OUT : USB_PID_IN ) );
827
828 /* Sanity check */
829 assert ( ring->end->prod == count );
830
831 return 0;
832}
833
834/**
835 * Enqueue stream transfer
836 *
837 * @v ep USB endpoint
838 * @v iobuf I/O buffer
839 * @v zlp Append a zero-length packet
840 * @ret rc Return status code
841 */
842static int uhci_endpoint_stream ( struct usb_endpoint *ep,
843 struct io_buffer *iobuf, int zlp ) {
844 struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
845 struct uhci_ring *ring = &endpoint->ring;
846 unsigned int count;
847 size_t len;
848 int input;
849 int rc;
850
851 /* Calculate number of descriptors */
852 len = iob_len ( iobuf );
853 count = ( ( ( len + ring->mtu - 1 ) / ring->mtu ) + ( zlp ? 1 : 0 ) );
854
855 /* Enqueue transfer */
856 if ( ( rc = uhci_enqueue ( ring, iobuf, count ) ) != 0 )
857 return rc;
858
859 /* Describe data packet */
860 input = ( ep->address & USB_DIR_IN );
861 uhci_describe ( ring, iobuf->data, len,
862 ( input ? USB_PID_IN : USB_PID_OUT ) );
863
864 /* Describe zero-length packet, if applicable */
865 if ( zlp )
866 uhci_describe ( ring, NULL, 0, USB_PID_OUT );
867
868 /* Sanity check */
869 assert ( ring->end->prod == count );
870
871 return 0;
872}
873
874/**
875 * Check if transfer is a message transfer
876 *
877 * @v xfer UHCI transfer
878 * @ret is_message Transfer is a message transfer
879 */
880static inline int uhci_is_message ( struct uhci_transfer *xfer ) {
881 struct uhci_transfer_descriptor *desc = &xfer->desc[0];
882
883 return ( ( desc->control & cpu_to_le32 ( UHCI_CONTROL_PID_MASK ) ) ==
885}
886
887/**
888 * Poll for completions
889 *
890 * @v endpoint Endpoint
891 */
892static void uhci_endpoint_poll ( struct uhci_endpoint *endpoint ) {
893 struct uhci_ring *ring = &endpoint->ring;
894 struct uhci_device *uhci = endpoint->uhci;
895 struct usb_endpoint *ep = endpoint->ep;
896 struct usb_device *usb = ep->usb;
897 struct uhci_transfer *xfer;
899 struct io_buffer *iobuf;
900 unsigned int index;
902 uint32_t toggle;
904 uint16_t actual;
905 size_t len;
906
907 /* Consume all completed descriptors */
908 while ( uhci_ring_fill ( ring ) ) {
909
910 /* Stop if we reach an uncompleted descriptor */
911 index = ( ring->cons % UHCI_RING_COUNT );
912 xfer = ring->xfer[index];
913 assert ( xfer != NULL );
914 assert ( xfer->cons < xfer->prod );
915 desc = &xfer->desc[xfer->cons];
916 rmb();
917 if ( desc->status & UHCI_STATUS_ACTIVE )
918 break;
919 control = le32_to_cpu ( desc->control );
920 actual = le16_to_cpu ( desc->actual );
921
922 /* Update data length, if applicable */
923 if ( UHCI_DATA_PACKET ( control ) )
924 xfer->len += UHCI_ACTUAL_LEN ( actual );
925
926 /* If we have encountered an error, then deactivate
927 * the queue head (to prevent further hardware
928 * accesses to this transfer), consume the transfer,
929 * and report the error to the USB core.
930 */
931 if ( desc->status & UHCI_STATUS_STALLED ) {
932 DBGC ( uhci, "UHCI %s %s completion %d.%d failed "
933 "(status %02x)\n", usb->name,
934 usb_endpoint_name ( ep ), index,
935 xfer->cons, desc->status );
937 ring->head->current = cpu_to_le32 ( link );
938 wmb();
939 iobuf = uhci_dequeue ( ring );
940 usb_complete_err ( ep, iobuf, -EIO );
941 break;
942 }
943
944 /* Consume this descriptor */
945 xfer->cons++;
946
947 /* Check for short packets */
948 if ( UHCI_SHORT_PACKET ( control, actual ) ) {
949
950 /* Sanity checks */
951 assert ( desc->flags & UHCI_FL_SPD );
952 link = virt_to_phys ( desc );
953 assert ( ( le32_to_cpu ( ring->head->current ) &
954 ~( UHCI_ALIGN - 1 ) ) == link );
955
956 /* If this is a message transfer, then restart
957 * at the status stage.
958 */
959 if ( uhci_is_message ( xfer ) ) {
960 xfer->cons = ( xfer->prod - 1 );
961 link = virt_to_phys ( &xfer->desc[xfer->cons] );
962 ring->head->current = cpu_to_le32 ( link );
963 break;
964 }
965
966 /* Otherwise, this is a stream transfer.
967 * First, prevent further hardware access to
968 * this transfer.
969 */
971 ring->head->current = cpu_to_le32 ( link );
972 wmb();
973
974 /* Determine expected data toggle for next descriptor */
975 toggle = ( ( control ^ UHCI_CONTROL_TOGGLE ) &
977
978 /* Consume this transfer */
979 len = xfer->len;
980 iobuf = uhci_dequeue ( ring );
981
982 /* Update packet length */
983 assert ( len <= iob_len ( iobuf ) );
984 iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
985
986 /* Restart ring */
987 uhci_restart ( ring, toggle );
988
989 } else if ( xfer->cons == xfer->prod ) {
990
991 /* Completed a transfer: consume it */
992 len = xfer->len;
993 iobuf = uhci_dequeue ( ring );
994 assert ( len == iob_len ( iobuf ) );
995
996 } else {
997
998 /* Not a short packet and not yet complete:
999 * continue processing.
1000 */
1001 continue;
1002 }
1003
1004 /* Report completion to USB core */
1005 usb_complete ( ep, iobuf );
1006 }
1007}
1008
1009/******************************************************************************
1010 *
1011 * Device operations
1012 *
1013 ******************************************************************************
1014 */
1015
1016/**
1017 * Open device
1018 *
1019 * @v usb USB device
1020 * @ret rc Return status code
1021 */
1022static int uhci_device_open ( struct usb_device *usb ) {
1023 struct uhci_device *uhci = usb_bus_get_hostdata ( usb->port->hub->bus );
1024
1025 usb_set_hostdata ( usb, uhci );
1026 return 0;
1027}
1028
1029/**
1030 * Close device
1031 *
1032 * @v usb USB device
1033 */
1034static void uhci_device_close ( struct usb_device *usb ) {
1035 struct uhci_device *uhci = usb_get_hostdata ( usb );
1036 struct usb_bus *bus = uhci->bus;
1037
1038 /* Free device address, if assigned */
1039 if ( usb->address )
1040 usb_free_address ( bus, usb->address );
1041}
1042
1043/**
1044 * Assign device address
1045 *
1046 * @v usb USB device
1047 * @ret rc Return status code
1048 */
1049static int uhci_device_address ( struct usb_device *usb ) {
1050 struct uhci_device *uhci = usb_get_hostdata ( usb );
1051 struct usb_bus *bus = uhci->bus;
1052 struct usb_endpoint *ep0 = usb_endpoint ( usb, USB_EP0_ADDRESS );
1053 struct uhci_endpoint *endpoint0 = usb_endpoint_get_hostdata ( ep0 );
1054 int address;
1055 int rc;
1056
1057 /* Sanity checks */
1058 assert ( usb->address == 0 );
1059 assert ( ep0 != NULL );
1060
1061 /* Allocate device address */
1063 if ( address < 0 ) {
1064 rc = address;
1065 DBGC ( uhci, "UHCI %s could not allocate address: %s\n",
1066 usb->name, strerror ( rc ) );
1067 goto err_alloc_address;
1068 }
1069
1070 /* Set address */
1071 if ( ( rc = usb_set_address ( usb, address ) ) != 0 )
1072 goto err_set_address;
1073
1074 /* Update device address */
1075 usb->address = address;
1076 endpoint0->ring.control |= UHCI_CONTROL_DEVICE ( address );
1077
1078 return 0;
1079
1080 err_set_address:
1082 err_alloc_address:
1083 return rc;
1084}
1085
1086/******************************************************************************
1087 *
1088 * Hub operations
1089 *
1090 ******************************************************************************
1091 */
1092
1093/**
1094 * Open hub
1095 *
1096 * @v hub USB hub
1097 * @ret rc Return status code
1098 */
1099static int uhci_hub_open ( struct usb_hub *hub __unused ) {
1100
1101 /* Nothing to do */
1102 return 0;
1103}
1104
1105/**
1106 * Close hub
1107 *
1108 * @v hub USB hub
1109 */
1110static void uhci_hub_close ( struct usb_hub *hub __unused ) {
1111
1112 /* Nothing to do */
1113}
1114
1115/******************************************************************************
1116 *
1117 * Root hub operations
1118 *
1119 ******************************************************************************
1120 */
1121
1122/**
1123 * Open root hub
1124 *
1125 * @v hub USB hub
1126 * @ret rc Return status code
1127 */
1128static int uhci_root_open ( struct usb_hub *hub __unused) {
1129
1130 /* Nothing to do */
1131 return 0;
1132}
1133
1134/**
1135 * Close root hub
1136 *
1137 * @v hub USB hub
1138 */
1139static void uhci_root_close ( struct usb_hub *hub __unused ) {
1140
1141 /* Nothing to do */
1142}
1143
1144/**
1145 * Enable port
1146 *
1147 * @v hub USB hub
1148 * @v port USB port
1149 * @ret rc Return status code
1150 */
1151static int uhci_root_enable ( struct usb_hub *hub, struct usb_port *port ) {
1152 struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
1153 uint16_t portsc;
1154 unsigned int i;
1155
1156 /* Reset port */
1157 portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
1158 portsc |= UHCI_PORTSC_PR;
1159 outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1161 portsc &= ~UHCI_PORTSC_PR;
1162 outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1164
1165 /* Enable port */
1166 portsc |= UHCI_PORTSC_PED;
1167 outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1169
1170 /* Wait for port to become enabled */
1171 for ( i = 0 ; i < UHCI_PORT_ENABLE_MAX_WAIT_MS ; i++ ) {
1172
1173 /* Check port status */
1174 portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
1175 if ( portsc & UHCI_PORTSC_PED )
1176 return 0;
1177
1178 /* Delay */
1179 mdelay ( 1 );
1180 }
1181
1182 DBGC ( uhci, "UHCI %s-%d timed out waiting for port to enable "
1183 "(status %04x)\n", uhci->name, port->address, portsc );
1184 return -ETIMEDOUT;
1185}
1186
1187/**
1188 * Disable port
1189 *
1190 * @v hub USB hub
1191 * @v port USB port
1192 * @ret rc Return status code
1193 */
1194static int uhci_root_disable ( struct usb_hub *hub, struct usb_port *port ) {
1195 struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
1196 uint16_t portsc;
1197
1198 /* Disable port */
1199 portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
1200 portsc &= ~UHCI_PORTSC_PED;
1201 outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1202
1203 return 0;
1204}
1205
1206/**
1207 * Update root hub port speed
1208 *
1209 * @v hub USB hub
1210 * @v port USB port
1211 * @ret rc Return status code
1212 */
1213static int uhci_root_speed ( struct usb_hub *hub, struct usb_port *port ) {
1214 struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
1215 struct pci_device pci;
1216 uint16_t portsc;
1217 unsigned int speed;
1218
1219 /* Read port status */
1220 portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
1221 if ( ! ( portsc & UHCI_PORTSC_CCS ) ) {
1222 /* Port not connected */
1223 speed = USB_SPEED_NONE;
1224 } else if ( uhci->companion &&
1226 uhci->companion ) ) {
1227 /* Defer connection detection until companion
1228 * controller has been enumerated.
1229 */
1230 pci_init ( &pci, uhci->companion );
1231 DBGC ( uhci, "UHCI %s-%d deferring for companion " PCI_FMT "\n",
1232 uhci->name, port->address, PCI_ARGS ( &pci ) );
1233 speed = USB_SPEED_NONE;
1234 } else if ( portsc & UHCI_PORTSC_LS ) {
1235 /* Low-speed device */
1236 speed = USB_SPEED_LOW;
1237 } else {
1238 /* Full-speed device */
1239 speed = USB_SPEED_FULL;
1240 }
1241 port->speed = speed;
1242
1243 /* Record disconnections and clear changes */
1244 port->disconnected |= ( portsc & UHCI_PORTSC_CSC );
1245 outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1246
1247 return 0;
1248}
1249
1250/**
1251 * Clear transaction translator buffer
1252 *
1253 * @v hub USB hub
1254 * @v port USB port
1255 * @v ep USB endpoint
1256 * @ret rc Return status code
1257 */
1258static int uhci_root_clear_tt ( struct usb_hub *hub, struct usb_port *port,
1259 struct usb_endpoint *ep ) {
1260 struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
1261
1262 /* Should never be called; this is a root hub */
1263 DBGC ( uhci, "UHCI %s-%d nonsensical CLEAR_TT for %s %s\n", uhci->name,
1264 port->address, ep->usb->name, usb_endpoint_name ( ep ) );
1265
1266 return -ENOTSUP;
1267}
1268
1269/**
1270 * Poll for port status changes
1271 *
1272 * @v hub USB hub
1273 * @v port USB port
1274 */
1275static void uhci_root_poll ( struct usb_hub *hub, struct usb_port *port ) {
1276 struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
1277 uint16_t portsc;
1278 uint16_t change;
1279
1280 /* Do nothing unless something has changed */
1281 portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
1282 change = ( portsc & UHCI_PORTSC_CHANGE );
1283 if ( ! change )
1284 return;
1285
1286 /* Record disconnections and clear changes */
1287 port->disconnected |= ( portsc & UHCI_PORTSC_CSC );
1288 outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1289
1290 /* Report port status change */
1292}
1293
1294/******************************************************************************
1295 *
1296 * Bus operations
1297 *
1298 ******************************************************************************
1299 */
1300
1301/**
1302 * Open USB bus
1303 *
1304 * @v bus USB bus
1305 * @ret rc Return status code
1306 */
1307static int uhci_bus_open ( struct usb_bus *bus ) {
1308 struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
1309 int rc;
1310
1311 /* Sanity checks */
1312 assert ( list_empty ( &uhci->async ) );
1313 assert ( list_empty ( &uhci->periodic ) );
1314
1315 /* Allocate and initialise asynchronous queue head */
1316 uhci->head = malloc_phys ( sizeof ( *uhci->head ), UHCI_ALIGN );
1317 if ( ! uhci->head ) {
1318 rc = -ENOMEM;
1319 goto err_alloc_head;
1320 }
1321 if ( ( rc = uhci_reachable ( uhci->head, sizeof ( *uhci->head ) ) ) !=0)
1322 goto err_unreachable_head;
1323 memset ( uhci->head, 0, sizeof ( *uhci->head ) );
1325 uhci_async_schedule ( uhci );
1326
1327 /* Allocate periodic frame list */
1328 uhci->frame = malloc_phys ( sizeof ( *uhci->frame ),
1329 sizeof ( *uhci->frame ) );
1330 if ( ! uhci->frame ) {
1331 rc = -ENOMEM;
1332 goto err_alloc_frame;
1333 }
1334 if ( ( rc = uhci_reachable ( uhci->frame,
1335 sizeof ( *uhci->frame ) ) ) != 0 )
1336 goto err_unreachable_frame;
1337 uhci_periodic_schedule ( uhci );
1338 outl ( virt_to_phys ( uhci->frame ), uhci->regs + UHCI_FLBASEADD );
1339
1340 /* Start controller */
1341 uhci_run ( uhci );
1342
1343 return 0;
1344
1345 uhci_stop ( uhci );
1346 err_unreachable_frame:
1347 free_phys ( uhci->frame, sizeof ( *uhci->frame ) );
1348 err_alloc_frame:
1349 err_unreachable_head:
1350 free_phys ( uhci->head, sizeof ( *uhci->head ) );
1351 err_alloc_head:
1352 return rc;
1353}
1354
1355/**
1356 * Close USB bus
1357 *
1358 * @v bus USB bus
1359 */
1360static void uhci_bus_close ( struct usb_bus *bus ) {
1361 struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
1362
1363 /* Sanity checks */
1364 assert ( list_empty ( &uhci->async ) );
1365 assert ( list_empty ( &uhci->periodic ) );
1366
1367 /* Stop controller */
1368 uhci_stop ( uhci );
1369
1370 /* Free periodic frame list */
1371 free_phys ( uhci->frame, sizeof ( *uhci->frame ) );
1372
1373 /* Free asynchronous schedule */
1374 free_phys ( uhci->head, sizeof ( *uhci->head ) );
1375}
1376
1377/**
1378 * Poll USB bus
1379 *
1380 * @v bus USB bus
1381 */
1382static void uhci_bus_poll ( struct usb_bus *bus ) {
1383 struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
1384 struct usb_hub *hub = bus->hub;
1385 struct uhci_endpoint *endpoint;
1386 unsigned int i;
1387
1388 /* UHCI defers interrupts (including short packet detection)
1389 * until the end of the frame. This can result in bulk IN
1390 * endpoints remaining halted for much of the time, waiting
1391 * for software action to reset the data toggles. We
1392 * therefore ignore USBSTS and unconditionally poll all
1393 * endpoints for completed transfer descriptors.
1394 *
1395 * As with EHCI, we trust that completion handlers are minimal
1396 * and will not do anything that could plausibly affect the
1397 * endpoint list itself.
1398 */
1399 list_for_each_entry ( endpoint, &uhci->endpoints, list )
1400 uhci_endpoint_poll ( endpoint );
1401
1402 /* UHCI provides no single bit to indicate that a port status
1403 * change has occurred. We therefore unconditionally iterate
1404 * over all ports looking for status changes.
1405 */
1406 for ( i = 1 ; i <= UHCI_PORTS ; i++ )
1407 uhci_root_poll ( hub, usb_port ( hub, i ) );
1408}
1409
1410/******************************************************************************
1411 *
1412 * PCI interface
1413 *
1414 ******************************************************************************
1415 */
1416
1417/** USB host controller operations */
1419 .endpoint = {
1420 .open = uhci_endpoint_open,
1421 .close = uhci_endpoint_close,
1422 .reset = uhci_endpoint_reset,
1423 .mtu = uhci_endpoint_mtu,
1424 .message = uhci_endpoint_message,
1425 .stream = uhci_endpoint_stream,
1426 },
1427 .device = {
1428 .open = uhci_device_open,
1429 .close = uhci_device_close,
1430 .address = uhci_device_address,
1431 },
1432 .bus = {
1433 .open = uhci_bus_open,
1434 .close = uhci_bus_close,
1435 .poll = uhci_bus_poll,
1436 },
1437 .hub = {
1438 .open = uhci_hub_open,
1439 .close = uhci_hub_close,
1440 },
1441 .root = {
1442 .open = uhci_root_open,
1443 .close = uhci_root_close,
1444 .enable = uhci_root_enable,
1445 .disable = uhci_root_disable,
1446 .speed = uhci_root_speed,
1447 .clear_tt = uhci_root_clear_tt,
1448 },
1449};
1450
1451/**
1452 * Locate EHCI companion controller (when no EHCI support is present)
1453 *
1454 * @v pci PCI device
1455 * @ret busdevfn EHCI companion controller bus:dev.fn (if any)
1456 */
1457__weak unsigned int ehci_companion ( struct pci_device *pci __unused ) {
1458 return 0;
1459}
1460
1461/**
1462 * Probe PCI device
1463 *
1464 * @v pci PCI device
1465 * @ret rc Return status code
1466 */
1467static int uhci_probe ( struct pci_device *pci ) {
1468 struct uhci_device *uhci;
1469 struct usb_port *port;
1470 unsigned int i;
1471 int rc;
1472
1473 /* Allocate and initialise structure */
1474 uhci = zalloc ( sizeof ( *uhci ) );
1475 if ( ! uhci ) {
1476 rc = -ENOMEM;
1477 goto err_alloc;
1478 }
1479 uhci->name = pci->dev.name;
1480 INIT_LIST_HEAD ( &uhci->endpoints );
1481 INIT_LIST_HEAD ( &uhci->async );
1482 INIT_LIST_HEAD ( &uhci->periodic );
1483
1484 /* Fix up PCI device */
1485 adjust_pci_device ( pci );
1486
1487 /* Identify EHCI companion controller, if any */
1488 uhci->companion = ehci_companion ( pci );
1489
1490 /* Claim ownership from BIOS. (There is no release mechanism
1491 * for UHCI.)
1492 */
1494
1495 /* Map registers */
1496 uhci->regs = pci->ioaddr;
1497 if ( ! uhci->regs ) {
1498 rc = -ENODEV;
1499 goto err_ioremap;
1500 }
1501
1502 /* Reset device */
1503 if ( ( rc = uhci_reset ( uhci ) ) != 0 )
1504 goto err_reset;
1505
1506 /* Allocate USB bus */
1507 uhci->bus = alloc_usb_bus ( &pci->dev, UHCI_PORTS, UHCI_MTU,
1508 &uhci_operations );
1509 if ( ! uhci->bus ) {
1510 rc = -ENOMEM;
1511 goto err_alloc_bus;
1512 }
1513 usb_bus_set_hostdata ( uhci->bus, uhci );
1514 usb_hub_set_drvdata ( uhci->bus->hub, uhci );
1515
1516 /* Set port protocols */
1517 for ( i = 1 ; i <= UHCI_PORTS ; i++ ) {
1518 port = usb_port ( uhci->bus->hub, i );
1519 port->protocol = USB_PROTO_2_0;
1520 }
1521
1522 /* Register USB bus */
1523 if ( ( rc = register_usb_bus ( uhci->bus ) ) != 0 )
1524 goto err_register;
1525
1526 pci_set_drvdata ( pci, uhci );
1527 return 0;
1528
1529 unregister_usb_bus ( uhci->bus );
1530 err_register:
1531 free_usb_bus ( uhci->bus );
1532 err_alloc_bus:
1533 uhci_reset ( uhci );
1534 err_reset:
1535 err_ioremap:
1536 free ( uhci );
1537 err_alloc:
1538 return rc;
1539}
1540
1541/**
1542 * Remove PCI device
1543 *
1544 * @v pci PCI device
1545 */
1546static void uhci_remove ( struct pci_device *pci ) {
1547 struct uhci_device *uhci = pci_get_drvdata ( pci );
1548 struct usb_bus *bus = uhci->bus;
1549
1551 assert ( list_empty ( &uhci->async ) );
1552 assert ( list_empty ( &uhci->periodic ) );
1553 free_usb_bus ( bus );
1554 uhci_reset ( uhci );
1555 free ( uhci );
1556}
1557
1558/** UHCI PCI device IDs */
1559static struct pci_device_id uhci_ids[] = {
1560 PCI_ROM ( 0xffff, 0xffff, "uhci", "UHCI", 0 ),
1561};
1562
1563/** UHCI PCI driver */
1564struct pci_driver uhci_driver __pci_driver = {
1565 .ids = uhci_ids,
1566 .id_count = ( sizeof ( uhci_ids ) / sizeof ( uhci_ids[0] ) ),
1569 .probe = uhci_probe,
1570 .remove = uhci_remove,
1571};
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
u8 port
Port number.
Definition CIB_PRM.h:3
#define PCI_CLASS_SERIAL
Definition Pci22.h:267
#define PCI_CLASS_SERIAL_USB
Definition Pci22.h:273
u32 link
Link to next descriptor.
Definition ar9003_mac.h:1
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
unsigned short uint16_t
Definition stdint.h:11
unsigned int uint32_t
Definition stdint.h:12
unsigned long physaddr_t
Definition stdint.h:20
unsigned char uint8_t
Definition stdint.h:10
long index
Definition bigint.h:65
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
#define BUS_TYPE_PCI
PCI bus type.
Definition device.h:44
ring len
Length.
Definition dwmac.h:226
uint32_t addr
Buffer address.
Definition dwmac.h:9
uint8_t bus
Bus.
Definition edd.h:1
USB Enhanced Host Controller Interface (EHCI) driver.
uint8_t data[48]
Additional event data.
Definition ena.h:11
uint16_t queue
Queue ID.
Definition ena.h:11
uint64_t address
Base address.
Definition ena.h:13
struct ena_llq_option desc
Descriptor counts.
Definition ena.h:9
static signed char phys[4]
Definition epic100.c:88
Error codes.
#define __unused
Declare a variable or data structure as unused.
Definition compiler.h:573
#define DBGCP(...)
Definition compiler.h:539
#define DBGC(...)
Definition compiler.h:505
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define ETIMEDOUT
Connection timed out.
Definition errno.h:670
#define ENOMEM
Not enough space.
Definition errno.h:535
#define EIO
Input/output error.
Definition errno.h:434
#define ENOTSUP
Operation not supported.
Definition errno.h:590
#define ECANCELED
Operation canceled.
Definition errno.h:344
#define ENOBUFS
No buffer space available.
Definition errno.h:499
#define ENODEV
No such device.
Definition errno.h:510
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define le16_to_cpu(value)
Definition byteswap.h:113
#define le32_to_cpu(value)
Definition byteswap.h:114
#define cpu_to_le32(value)
Definition byteswap.h:108
#define cpu_to_le16(value)
Definition byteswap.h:107
#define __weak
Declare a function as weak (use before the definition)
Definition compiler.h:219
#define __attribute__(x)
Definition compiler.h:10
#define rmb()
Definition io.h:545
#define inw(io_addr)
Definition io.h:292
#define outw(data, io_addr)
Definition io.h:320
#define wmb()
Definition io.h:546
#define outl(data, io_addr)
Definition io.h:330
int pci_write_config_word(struct pci_device *pci, unsigned int where, uint16_t value)
Write 16-bit word to PCI configuration space.
Universal Serial Bus (USB)
@ USB_PROTO_2_0
USB 2.0.
Definition usb.h:23
static void * usb_hub_get_drvdata(struct usb_hub *hub)
Get USB hub driver private data.
Definition usb.h:948
#define USB_DIR_IN
Data transfer is from device to host.
Definition usb.h:98
static struct usb_port * usb_port(struct usb_hub *hub, unsigned int address)
Get USB port.
Definition usb.h:960
static int usb_set_address(struct usb_device *usb, unsigned int address)
Set address.
Definition usb.h:1159
static void * usb_endpoint_get_hostdata(struct usb_endpoint *ep)
Get USB endpoint host controller private data.
Definition usb.h:587
static void usb_complete(struct usb_endpoint *ep, struct io_buffer *iobuf)
Complete transfer (without error)
Definition usb.h:1087
static void * usb_get_hostdata(struct usb_device *usb)
Get USB device host controller private data.
Definition usb.h:795
static struct usb_endpoint * usb_endpoint(struct usb_device *usb, unsigned int address)
Get USB endpoint.
Definition usb.h:806
static void usb_endpoint_set_hostdata(struct usb_endpoint *ep, void *priv)
Set USB endpoint host controller private data.
Definition usb.h:576
#define USB_ENDPOINT_ATTR_INTERRUPT
Interrupt endpoint transfer type.
Definition usb.h:293
#define USB_RESET_RECOVER_DELAY_MS
Reset recovery time.
Definition usb.h:1335
static void * usb_bus_get_hostdata(struct usb_bus *bus)
Get USB bus host controller private data.
Definition usb.h:1062
#define USB_ENDPOINT_ATTR_TYPE_MASK
Endpoint attribute transfer type mask.
Definition usb.h:281
@ USB_SPEED_LOW
Low speed (1.5Mbps)
Definition usb.h:49
@ USB_SPEED_FULL
Full speed (12Mbps)
Definition usb.h:51
@ USB_SPEED_NONE
Not connected.
Definition usb.h:47
@ USB_PID_IN
IN PID.
Definition usb.h:75
@ USB_PID_OUT
OUT PID.
Definition usb.h:77
@ USB_PID_SETUP
SETUP PID.
Definition usb.h:79
static void usb_set_hostdata(struct usb_device *usb, void *priv)
Set USB device host controller private data.
Definition usb.h:784
#define USB_EP0_ADDRESS
Control endpoint address.
Definition usb.h:501
static void usb_hub_set_drvdata(struct usb_hub *hub, void *priv)
Set USB hub driver private data.
Definition usb.h:937
static void usb_bus_set_hostdata(struct usb_bus *bus, void *priv)
Set USB bus host controller private data.
Definition usb.h:1051
#define USB_RESET_DELAY_MS
Minimum reset time.
Definition usb.h:1328
void * memset(void *dest, int character, size_t len) __nonnull
String functions.
#define ffs(x)
Find first (i.e.
Definition strings.h:141
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition iobuf.h:160
#define iob_pull(iobuf, len)
Definition iobuf.h:107
#define iob_unput(iobuf, len)
Definition iobuf.h:140
uint8_t attr
Type and attributes.
Definition librm.h:7
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition list.h:94
#define list_for_each_entry(pos, head, member)
Iterate over entries in a list.
Definition list.h:432
#define list_del(list)
Delete an entry from a list.
Definition list.h:120
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition list.h:46
#define list_empty(list)
Test whether a list is empty.
Definition list.h:137
#define list_for_each_entry_reverse(pos, head, member)
Iterate over entries in a list in reverse order.
Definition list.h:445
#define list_check_contains_entry(entry, head, member)
Check list contains a specified entry.
Definition list.h:550
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
void * malloc_phys(size_t size, size_t phys_align)
Allocate memory with specified physical alignment.
Definition malloc.c:707
void * malloc(size_t size)
Allocate memory.
Definition malloc.c:621
void free_phys(void *ptr, size_t size)
Free memory allocated with malloc_phys()
Definition malloc.c:723
Dynamic memory allocation.
uint32_t control
Control.
Definition myson.h:3
uint32_t end
Ending offset.
Definition netvsc.h:7
uint32_t first
First block in range.
Definition pccrr.h:1
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition pci.c:241
PCI bus.
#define PCI_FMT
PCI device debug message format.
Definition pci.h:312
#define __pci_driver
Declare a PCI driver.
Definition pci.h:278
#define PCI_ARGS(pci)
PCI device debug message arguments.
Definition pci.h:315
static void pci_init(struct pci_device *pci, unsigned int busdevfn)
Initialise PCI device.
Definition pci.h:341
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition pci.h:366
#define PCI_ROM(_vendor, _device, _name, _description, _data)
Definition pci.h:308
#define PCI_CLASS_SERIAL_USB_UHCI
UHCI USB controller.
Definition pci.h:138
static void * pci_get_drvdata(struct pci_device *pci)
Get PCI driver-private data.
Definition pci.h:376
#define PCI_CLASS_ID(base, sub, progif)
Construct PCI class ID.
Definition pci.h:203
static void(* free)(struct refcnt *refcnt))
Definition refcnt.h:55
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
char name[40]
Name.
Definition device.h:79
A persistent I/O buffer.
Definition iobuf.h:38
void * data
Start of data.
Definition iobuf.h:53
A PCI device ID list entry.
Definition pci.h:175
A PCI device.
Definition pci.h:211
unsigned long ioaddr
I/O address.
Definition pci.h:226
struct device dev
Generic device.
Definition pci.h:213
A PCI driver.
Definition pci.h:252
A UHCI device.
Definition uhci.h:308
struct list_head async
Asynchronous schedule.
Definition uhci.h:325
struct uhci_queue_head * head
Asynchronous queue head.
Definition uhci.h:318
const char * name
Name.
Definition uhci.h:312
struct usb_bus * bus
USB bus.
Definition uhci.h:333
struct list_head periodic
Periodic schedule.
Definition uhci.h:330
struct list_head endpoints
List of all endpoints.
Definition uhci.h:323
unsigned int companion
EHCI companion controller bus:dev.fn address (if any)
Definition uhci.h:315
struct uhci_frame_list * frame
Frame list.
Definition uhci.h:320
unsigned long regs
Registers.
Definition uhci.h:310
A UHCI endpoint.
Definition uhci.h:337
struct usb_endpoint * ep
USB endpoint.
Definition uhci.h:341
struct uhci_device * uhci
UHCI device.
Definition uhci.h:339
struct uhci_ring ring
Transfer ring.
Definition uhci.h:348
struct list_head schedule
Endpoint schedule.
Definition uhci.h:345
struct list_head list
List of all endpoints.
Definition uhci.h:343
uint32_t link[UHCI_FRAMES]
Link pointer.
Definition uhci.h:96
A queue head.
Definition uhci.h:193
uint32_t current
Current transfer descriptor.
Definition uhci.h:197
uint32_t link
Horizontal link pointer.
Definition uhci.h:195
A transfer ring.
Definition uhci.h:232
uint8_t flags
Base flags.
Definition uhci.h:244
unsigned int prod
Producer counter.
Definition uhci.h:234
struct uhci_transfer * xfer[UHCI_RING_COUNT]
Transfers.
Definition uhci.h:253
size_t mtu
Maximum packet length.
Definition uhci.h:239
struct uhci_queue_head * head
Queue head.
Definition uhci.h:258
uint32_t control
Base control word.
Definition uhci.h:250
struct uhci_transfer * end
End of transfer ring (if non-empty)
Definition uhci.h:255
unsigned int cons
Consumer counter.
Definition uhci.h:236
A transfer descriptor.
Definition uhci.h:100
A single UHCI transfer.
Definition uhci.h:210
struct uhci_transfer_descriptor * desc
Transfer descriptors.
Definition uhci.h:219
size_t len
Completed data length.
Definition uhci.h:216
unsigned int prod
Producer counter.
Definition uhci.h:212
unsigned int cons
Consumer counter.
Definition uhci.h:214
struct io_buffer * iobuf
I/O buffer.
Definition uhci.h:222
A USB bus.
Definition usb.h:966
struct usb_hub * hub
Root hub.
Definition usb.h:995
A USB device.
Definition usb.h:723
char name[32]
Name.
Definition usb.h:725
struct usb_port * port
USB port.
Definition usb.h:727
unsigned int address
Device address, if assigned.
Definition usb.h:733
struct usb_endpoint * ep[32]
Endpoint list.
Definition usb.h:745
unsigned int speed
Device speed.
Definition usb.h:729
A USB endpoint.
Definition usb.h:404
size_t mtu
Maximum transfer size.
Definition usb.h:412
struct usb_device * usb
USB device.
Definition usb.h:406
unsigned int attributes
Attributes.
Definition usb.h:410
unsigned int interval
Interval (in microframes)
Definition usb.h:416
unsigned int address
Endpoint address.
Definition usb.h:408
USB host controller operations.
Definition usb.h:1031
A USB hub.
Definition usb.h:841
struct usb_bus * bus
USB bus.
Definition usb.h:845
A USB port.
Definition usb.h:813
struct usb_hub * hub
USB hub.
Definition usb.h:815
A USB setup data packet.
Definition usb.h:83
uint16_t request
Request.
Definition usb.h:85
void mdelay(unsigned long msecs)
Delay for a fixed number of milliseconds.
Definition timer.c:79
int32_t before
Initial microcode version.
Definition ucode.h:5
static void uhci_endpoint_close(struct usb_endpoint *ep)
Close endpoint.
Definition uhci.c:726
static void uhci_device_close(struct usb_device *usb)
Close device.
Definition uhci.c:1034
static int uhci_root_speed(struct usb_hub *hub, struct usb_port *port)
Update root hub port speed.
Definition uhci.c:1213
static int uhci_reset(struct uhci_device *uhci)
Reset UHCI device.
Definition uhci.c:131
static void uhci_async_schedule(struct uhci_device *uhci)
(Re)build asynchronous schedule
Definition uhci.c:469
static struct pci_device_id uhci_ids[]
UHCI PCI device IDs.
Definition uhci.c:1559
static int uhci_enqueue(struct uhci_ring *ring, struct io_buffer *iobuf, unsigned int count)
Enqueue new transfer.
Definition uhci.c:228
static int uhci_endpoint_message(struct usb_endpoint *ep, struct io_buffer *iobuf)
Enqueue message transfer.
Definition uhci.c:788
static void uhci_periodic_add(struct uhci_endpoint *endpoint)
Add endpoint to periodic schedule.
Definition uhci.c:600
static int uhci_probe(struct pci_device *pci)
Probe PCI device.
Definition uhci.c:1467
static int uhci_device_address(struct usb_device *usb)
Assign device address.
Definition uhci.c:1049
static int uhci_endpoint_mtu(struct usb_endpoint *ep)
Update MTU.
Definition uhci.c:772
static void uhci_describe(struct uhci_ring *ring, void *data, size_t len, uint8_t pid)
Describe transfer.
Definition uhci.c:320
static int uhci_ring_alloc(struct uhci_ring *ring)
Allocate transfer ring.
Definition uhci.c:176
static void uhci_bus_poll(struct usb_bus *bus)
Poll USB bus.
Definition uhci.c:1382
static void uhci_async_add(struct uhci_endpoint *endpoint)
Add endpoint to asynchronous schedule.
Definition uhci.c:498
static int uhci_hub_open(struct usb_hub *hub __unused)
Open hub.
Definition uhci.c:1099
static void uhci_root_close(struct usb_hub *hub __unused)
Close root hub.
Definition uhci.c:1139
static void uhci_hub_close(struct usb_hub *hub __unused)
Close hub.
Definition uhci.c:1110
static void uhci_endpoint_poll(struct uhci_endpoint *endpoint)
Poll for completions.
Definition uhci.c:892
static void uhci_periodic_schedule(struct uhci_device *uhci)
(Re)build periodic schedule
Definition uhci.c:532
static void uhci_schedule_add(struct uhci_endpoint *endpoint)
Add endpoint to appropriate schedule.
Definition uhci.c:640
static void uhci_root_poll(struct usb_hub *hub, struct usb_port *port)
Poll for port status changes.
Definition uhci.c:1275
static int uhci_endpoint_open(struct usb_endpoint *ep)
Open endpoint.
Definition uhci.c:680
static void uhci_ring_free(struct uhci_ring *ring)
Free transfer ring.
Definition uhci.c:208
static int uhci_root_clear_tt(struct usb_hub *hub, struct usb_port *port, struct usb_endpoint *ep)
Clear transaction translator buffer.
Definition uhci.c:1258
static void uhci_restart(struct uhci_ring *ring, uint32_t toggle)
Restart ring.
Definition uhci.c:395
static int uhci_is_message(struct uhci_transfer *xfer)
Check if transfer is a message transfer.
Definition uhci.c:880
static int uhci_bus_open(struct usb_bus *bus)
Open USB bus.
Definition uhci.c:1307
static int uhci_root_enable(struct usb_hub *hub, struct usb_port *port)
Enable port.
Definition uhci.c:1151
static int uhci_device_open(struct usb_device *usb)
Open device.
Definition uhci.c:1022
static void uhci_remove(struct pci_device *pci)
Remove PCI device.
Definition uhci.c:1546
__weak unsigned int ehci_companion(struct pci_device *pci __unused)
Locate EHCI companion controller (when no EHCI support is present)
Definition uhci.c:1457
static int uhci_endpoint_reset(struct usb_endpoint *ep)
Reset endpoint.
Definition uhci.c:756
static void uhci_schedule_del(struct uhci_endpoint *endpoint)
Remove endpoint from appropriate schedule.
Definition uhci.c:656
static int uhci_root_disable(struct usb_hub *hub, struct usb_port *port)
Disable port.
Definition uhci.c:1194
static int uhci_endpoint_stream(struct usb_endpoint *ep, struct io_buffer *iobuf, int zlp)
Enqueue stream transfer.
Definition uhci.c:842
static uint32_t uhci_link_qh(struct uhci_queue_head *queue)
Get link value for a queue head.
Definition uhci.c:459
static int uhci_root_open(struct usb_hub *hub __unused)
Open root hub.
Definition uhci.c:1128
static struct io_buffer * uhci_dequeue(struct uhci_ring *ring)
Dequeue transfer.
Definition uhci.c:361
static void uhci_bus_close(struct usb_bus *bus)
Close USB bus.
Definition uhci.c:1360
static void uhci_periodic_del(struct uhci_endpoint *endpoint)
Remove endpoint from periodic schedule.
Definition uhci.c:621
static int uhci_reachable(void *addr, size_t len)
Check that address is reachable.
Definition uhci.c:58
static struct usb_host_operations uhci_operations
USB host controller operations.
Definition uhci.c:1418
static void uhci_async_del(struct uhci_endpoint *endpoint)
Remove endpoint from asynchronous schedule.
Definition uhci.c:513
static void uhci_run(struct uhci_device *uhci)
Start UHCI device.
Definition uhci.c:84
static int uhci_stop(struct uhci_device *uhci)
Stop UHCI device.
Definition uhci.c:99
USB Universal Host Controller Interface (UHCI) driver.
#define UHCI_MTU
Maximum transfer size.
Definition uhci.h:28
#define UHCI_CONTROL_LEN(len)
Data length.
Definition uhci.h:173
#define UHCI_USBLEGSUP
USB legacy support register (in PCI configuration space)
Definition uhci.h:187
#define UHCI_LINK_DEPTH_FIRST
Depth-first processing.
Definition uhci.h:82
#define UHCI_RESET_MAX_WAIT_MS
Maximum time to wait for reset to complete.
Definition uhci.h:299
#define UHCI_FL_CERR_MAX
Error counter maximum value.
Definition uhci.h:149
#define UHCI_PORTSC_CCS
Current connect status.
Definition uhci.h:76
#define UHCI_USBLEGSUP_DEFAULT
USB legacy support default value.
Definition uhci.h:190
#define UHCI_CONTROL_TOGGLE
Data toggle.
Definition uhci.h:170
#define UHCI_LINK_TYPE_QH
Queue head type.
Definition uhci.h:85
#define UHCI_SHORT_PACKET(control, actual)
Check for short packet.
Definition uhci.h:183
static unsigned int uhci_ring_remaining(struct uhci_ring *ring)
Calculate space remaining in transfer ring.
Definition uhci.h:283
#define UHCI_USBCMD_MAX64
Max packet is 64 bytes.
Definition uhci.h:37
#define UHCI_FL_SPD
Short packet detect.
Definition uhci.h:143
#define UHCI_PORTSC_PED
Port enabled.
Definition uhci.h:70
#define UHCI_FL_IOC
Interrupt on completion.
Definition uhci.h:155
#define UHCI_PORTS
Number of ports.
Definition uhci.h:25
#define UHCI_STATUS_STALLED
Stalled.
Definition uhci.h:125
#define UHCI_RING_COUNT
Number of transfer descriptors in a ring.
Definition uhci.h:229
#define UHCI_FRAMES
Number of frames in frame list.
Definition uhci.h:91
#define UHCI_DATA_PACKET(control)
Check for data packet.
Definition uhci.h:180
#define UHCI_PORTSC_LS
Low-speed device attached.
Definition uhci.h:64
#define UHCI_PORTSC_PR
Port reset.
Definition uhci.h:61
#define UHCI_CONTROL_PID(pid)
Packet ID.
Definition uhci.h:158
#define UHCI_USBCMD_HCRESET
Host controller reset.
Definition uhci.h:40
#define UHCI_PORTSC_CHANGE
Port status change mask.
Definition uhci.h:79
#define UHCI_CONTROL_ENDPOINT(address)
Endpoint address.
Definition uhci.h:167
#define UHCI_FLBASEADD
Frame list base address register.
Definition uhci.h:55
#define UHCI_CONTROL_PID_MASK
Packet ID mask.
Definition uhci.h:161
#define UHCI_ALIGN
Minimum alignment required for data structures.
Definition uhci.h:22
#define UHCI_USBSTS_HCHALTED
Host controller halted.
Definition uhci.h:49
#define UHCI_ACTUAL_LEN(actual)
Actual length.
Definition uhci.h:119
#define UHCI_FL_LS
Low speed device.
Definition uhci.h:152
#define UHCI_PORT_ENABLE_MAX_WAIT_MS
Maximum time to wait for a port to be enabled.
Definition uhci.h:305
#define UHCI_STATUS_ACTIVE
Active.
Definition uhci.h:122
#define UHCI_USBSTS
USB status register.
Definition uhci.h:46
#define UHCI_PORTSC(port)
Port status and control register.
Definition uhci.h:58
#define UHCI_CONTROL_DEVICE(address)
Device address.
Definition uhci.h:164
static unsigned int uhci_ring_fill(struct uhci_ring *ring)
Calculate space used in transfer ring.
Definition uhci.h:268
#define UHCI_PORTSC_CSC
Connect status change.
Definition uhci.h:73
#define UHCI_STOP_MAX_WAIT_MS
Maximum time to wait for host controller to stop.
Definition uhci.h:293
#define UHCI_LINK_TERMINATE
List terminator.
Definition uhci.h:88
#define UHCI_USBCMD_RUN
Run/stop.
Definition uhci.h:43
#define UHCI_USBCMD
USB command register.
Definition uhci.h:34
struct usb_bus * find_usb_bus_by_location(unsigned int bus_type, unsigned int location)
Find USB bus by device location.
Definition usb.c:2238
int usb_alloc_address(struct usb_bus *bus)
Allocate device address.
Definition usb.c:2264
void usb_free_address(struct usb_bus *bus, unsigned int address)
Free device address.
Definition usb.c:2284
const char * usb_endpoint_name(struct usb_endpoint *ep)
Get USB endpoint name (for debugging)
Definition usb.c:221
void usb_port_changed(struct usb_port *port)
Report port status change.
Definition usb.c:1858
void unregister_usb_bus(struct usb_bus *bus)
Unregister USB bus.
Definition usb.c:2171
int register_usb_bus(struct usb_bus *bus)
Register USB bus.
Definition usb.c:2131
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
void free_usb_bus(struct usb_bus *bus)
Free USB bus.
Definition usb.c:2195
void usb_complete_err(struct usb_endpoint *ep, struct io_buffer *iobuf, int rc)
Complete transfer (possibly with error)
Definition usb.c:587