2
* Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
30
#include <ipxe/malloc.h>
38
* USB Universal Host Controller Interface (UHCI) driver
42
/******************************************************************************
46
******************************************************************************
50
* Check that address is reachable
54
* @ret rc Return status code
56
static inline __attribute__ (( always_inline)) int
57
uhci_reachable ( void *addr, size_t len ) {
58
physaddr_t phys = virt_to_phys ( addr );
60
/* Always reachable in a 32-bit build */
61
if ( sizeof ( physaddr_t ) <= sizeof ( uint32_t ) )
64
/* Reachable if below 4GB */
65
if ( ( ( phys + len - 1 ) & ~0xffffffffULL ) == 0 )
71
/******************************************************************************
75
******************************************************************************
83
static void uhci_run ( struct uhci_device *uhci ) {
86
/* Set run/stop bit */
87
usbcmd = inw ( uhci->regs + UHCI_USBCMD );
88
usbcmd |= ( UHCI_USBCMD_RUN | UHCI_USBCMD_MAX64 );
89
outw ( usbcmd, uhci->regs + UHCI_USBCMD );
96
* @ret rc Return status code
98
static int uhci_stop ( struct uhci_device *uhci ) {
103
/* Clear run/stop bit */
104
usbcmd = inw ( uhci->regs + UHCI_USBCMD );
105
usbcmd &= ~UHCI_USBCMD_RUN;
106
outw ( usbcmd, uhci->regs + UHCI_USBCMD );
108
/* Wait for device to stop */
109
for ( i = 0 ; i < UHCI_STOP_MAX_WAIT_MS ; i++ ) {
111
/* Check if device is stopped */
112
usbsts = inw ( uhci->regs + UHCI_USBSTS );
113
if ( usbsts & UHCI_USBSTS_HCHALTED )
120
DBGC ( uhci, "UHCI %s timed out waiting for stop\n", uhci->name );
127
* @v uhci UHCI device
128
* @ret rc Return status code
130
static int uhci_reset ( struct uhci_device *uhci ) {
135
/* The UHCI specification states that resetting a running
136
* device may result in undefined behaviour, so try stopping
139
if ( ( rc = uhci_stop ( uhci ) ) != 0 ) {
140
/* Ignore errors and attempt to reset the device anyway */
144
outw ( UHCI_USBCMD_HCRESET, uhci->regs + UHCI_USBCMD );
146
/* Wait for reset to complete */
147
for ( i = 0 ; i < UHCI_RESET_MAX_WAIT_MS ; i++ ) {
149
/* Check if reset is complete */
150
usbcmd = inw ( uhci->regs + UHCI_USBCMD );
151
if ( ! ( usbcmd & UHCI_USBCMD_HCRESET ) )
158
DBGC ( uhci, "UHCI %s timed out waiting for reset\n", uhci->name );
162
/******************************************************************************
164
* Transfer descriptor rings
166
******************************************************************************
170
* Allocate transfer ring
172
* @v ring Transfer ring
173
* @ret rc Return status code
175
static int uhci_ring_alloc ( struct uhci_ring *ring ) {
178
/* Initialise structure */
179
memset ( ring, 0, sizeof ( *ring ) );
181
/* Allocate queue head */
182
ring->head = malloc_dma ( sizeof ( *ring->head ), UHCI_ALIGN );
183
if ( ! ring->head ) {
187
if ( ( rc = uhci_reachable ( ring->head,
188
sizeof ( *ring->head ) ) ) != 0 )
189
goto err_unreachable;
191
/* Initialise queue head */
192
ring->head->current = cpu_to_le32 ( UHCI_LINK_TERMINATE );
197
free_dma ( ring->head, sizeof ( *ring->head ) );
205
* @v ring Transfer ring
207
static void uhci_ring_free ( struct uhci_ring *ring ) {
211
assert ( uhci_ring_fill ( ring ) == 0 );
212
for ( i = 0 ; i < UHCI_RING_COUNT ; i++ )
213
assert ( ring->xfer[i] == NULL );
215
/* Free queue head */
216
free_dma ( ring->head, sizeof ( *ring->head ) );
220
* Enqueue new transfer
222
* @v ring Transfer ring
223
* @v iobuf I/O buffer
224
* @v count Number of descriptors
225
* @ret rc Return status code
227
static int uhci_enqueue ( struct uhci_ring *ring, struct io_buffer *iobuf,
228
unsigned int count ) {
229
struct uhci_transfer *xfer;
230
struct uhci_transfer *end;
231
struct uhci_transfer_descriptor *desc;
232
unsigned int index = ( ring->prod % UHCI_RING_COUNT );
238
assert ( count > 0 );
239
assert ( iobuf != NULL );
241
/* Check for space in ring */
242
if ( ! uhci_ring_remaining ( ring ) ) {
247
/* Check for reachability of I/O buffer */
248
if ( ( rc = uhci_reachable ( iobuf->data, iob_len ( iobuf ) ) ) != 0 )
249
goto err_unreachable_iobuf;
251
/* Allocate transfer */
252
xfer = malloc ( sizeof ( *xfer ) );
258
/* Initialise transfer */
264
/* Allocate transfer descriptors */
265
len = ( count * sizeof ( xfer->desc[0] ) );
266
xfer->desc = malloc_dma ( len, UHCI_ALIGN );
267
if ( ! xfer->desc ) {
271
if ( ( rc = uhci_reachable ( xfer->desc, len ) ) != 0 )
272
goto err_unreachable_desc;
274
/* Initialise transfer descriptors */
275
memset ( xfer->desc, 0, len );
277
for ( ; --count ; desc++ ) {
278
link = ( virt_to_phys ( desc + 1 ) | UHCI_LINK_DEPTH_FIRST );
279
desc->link = cpu_to_le32 ( link );
280
desc->flags = ring->flags;
282
desc->link = cpu_to_le32 ( UHCI_LINK_TERMINATE );
283
desc->flags = ( ring->flags | UHCI_FL_IOC );
287
link = virt_to_phys ( xfer->desc );
288
if ( uhci_ring_fill ( ring ) > 0 ) {
290
end->desc[ end->prod - 1 ].link = cpu_to_le32 ( link );
292
ring->head->current = cpu_to_le32 ( link );
294
assert ( ring->xfer[index] == NULL );
295
ring->xfer[index] = xfer;
301
err_unreachable_desc:
302
free_dma ( xfer->desc, len );
306
err_unreachable_iobuf:
314
* @v ring Transfer ring
316
* @v len Length of data
319
static void uhci_describe ( struct uhci_ring *ring, void *data,
320
size_t len, uint8_t pid ) {
321
struct uhci_transfer *xfer = ring->end;
322
struct uhci_transfer_descriptor *desc;
327
/* Calculate fragment length */
329
if ( frag_len > ring->mtu )
330
frag_len = ring->mtu;
332
/* Populate descriptor */
333
desc = &xfer->desc[xfer->prod++];
334
if ( pid == USB_PID_IN )
335
desc->flags |= UHCI_FL_SPD;
336
control = ( ring->control | UHCI_CONTROL_PID ( pid ) |
337
UHCI_CONTROL_LEN ( frag_len ) );
338
desc->control = cpu_to_le32 ( control );
340
desc->data = virt_to_phys ( data );
342
desc->status = UHCI_STATUS_ACTIVE;
344
/* Update data toggle */
345
ring->control ^= UHCI_CONTROL_TOGGLE;
347
/* Move to next descriptor */
357
* @v ring Transfer ring
358
* @ret iobuf I/O buffer
360
static struct io_buffer * uhci_dequeue ( struct uhci_ring *ring ) {
361
unsigned int index = ( ring->cons % UHCI_RING_COUNT );
362
struct io_buffer *iobuf;
363
struct uhci_transfer *xfer;
367
assert ( uhci_ring_fill ( ring ) > 0 );
369
/* Consume transfer */
370
xfer = ring->xfer[index];
371
assert ( xfer != NULL );
372
assert ( xfer->desc != NULL );
374
assert ( iobuf != NULL );
375
ring->xfer[index] = NULL;
378
/* Free transfer descriptors */
379
len = ( xfer->prod * sizeof ( xfer->desc[0] ) );
380
free_dma ( xfer->desc, len );
391
* @v ring Transfer ring
392
* @v toggle Expected data toggle for next descriptor
394
static void uhci_restart ( struct uhci_ring *ring, uint32_t toggle ) {
395
struct uhci_transfer *xfer;
396
struct uhci_transfer_descriptor *desc;
397
struct uhci_transfer_descriptor *first;
403
assert ( ring->head->current == cpu_to_le32 ( UHCI_LINK_TERMINATE ) );
405
/* If ring is empty, then just update the data toggle for the
408
if ( uhci_ring_fill ( ring ) == 0 ) {
409
ring->control &= ~UHCI_CONTROL_TOGGLE;
410
ring->control |= toggle;
414
/* If expected toggle does not match the toggle in the first
415
* unconsumed descriptor, then invert all toggles.
417
xfer = ring->xfer[ ring->cons % UHCI_RING_COUNT ];
418
assert ( xfer != NULL );
419
assert ( xfer->cons == 0 );
420
first = &xfer->desc[0];
421
if ( ( le32_to_cpu ( first->control ) ^ toggle ) & UHCI_CONTROL_TOGGLE){
423
/* Invert toggle on all unconsumed transfer descriptors */
424
for ( i = ring->cons ; i != ring->prod ; i++ ) {
425
xfer = ring->xfer[ i % UHCI_RING_COUNT ];
426
assert ( xfer != NULL );
427
assert ( xfer->cons == 0 );
428
for ( j = 0 ; j < xfer->prod ; j++ ) {
429
desc = &xfer->desc[j];
431
cpu_to_le32 ( UHCI_CONTROL_TOGGLE );
435
/* Invert toggle for next descriptor to be enqueued */
436
ring->control ^= UHCI_CONTROL_TOGGLE;
439
/* Restart ring at first unconsumed transfer */
440
link = virt_to_phys ( first );
442
ring->head->current = cpu_to_le32 ( link );
445
/******************************************************************************
447
* Schedule management
449
******************************************************************************
453
* Get link value for a queue head
455
* @v queue Queue head
456
* @ret link Link value
458
static inline uint32_t uhci_link_qh ( struct uhci_queue_head *queue ) {
460
return ( virt_to_phys ( queue ) | UHCI_LINK_TYPE_QH );
464
* (Re)build asynchronous schedule
466
* @v uhci UHCI device
468
static void uhci_async_schedule ( struct uhci_device *uhci ) {
469
struct uhci_endpoint *endpoint;
470
struct uhci_queue_head *queue;
474
/* Build schedule in reverse order of execution. Provided
475
* that we only ever add or remove single endpoints, this can
476
* safely run concurrently with hardware execution of the
479
link = end = uhci_link_qh ( uhci->head );
480
list_for_each_entry_reverse ( endpoint, &uhci->async, schedule ) {
481
queue = endpoint->ring.head;
482
queue->link = cpu_to_le32 ( link );
484
link = uhci_link_qh ( queue );
487
link = UHCI_LINK_TERMINATE;
488
uhci->head->link = cpu_to_le32 ( link );
493
* Add endpoint to asynchronous schedule
495
* @v endpoint Endpoint
497
static void uhci_async_add ( struct uhci_endpoint *endpoint ) {
498
struct uhci_device *uhci = endpoint->uhci;
500
/* Add to end of schedule */
501
list_add_tail ( &endpoint->schedule, &uhci->async );
503
/* Rebuild schedule */
504
uhci_async_schedule ( uhci );
508
* Remove endpoint from asynchronous schedule
510
* @v endpoint Endpoint
512
static void uhci_async_del ( struct uhci_endpoint *endpoint ) {
513
struct uhci_device *uhci = endpoint->uhci;
515
/* Remove from schedule */
516
list_check_contains_entry ( endpoint, &uhci->async, schedule );
517
list_del ( &endpoint->schedule );
519
/* Rebuild schedule */
520
uhci_async_schedule ( uhci );
522
/* Delay for a whole USB frame (with a 100% safety margin) */
527
* (Re)build periodic schedule
529
* @v uhci UHCI device
531
static void uhci_periodic_schedule ( struct uhci_device *uhci ) {
532
struct uhci_endpoint *endpoint;
533
struct uhci_queue_head *queue;
536
unsigned int max_interval;
539
/* Build schedule in reverse order of execution. Provided
540
* that we only ever add or remove single endpoints, this can
541
* safely run concurrently with hardware execution of the
544
DBGCP ( uhci, "UHCI %s periodic schedule: ", uhci->name );
545
link = end = uhci_link_qh ( uhci->head );
546
list_for_each_entry_reverse ( endpoint, &uhci->periodic, schedule ) {
547
queue = endpoint->ring.head;
548
queue->link = cpu_to_le32 ( link );
550
DBGCP ( uhci, "%s%d", ( ( link == end ) ? "" : "<-" ),
551
endpoint->ep->interval );
552
link = uhci_link_qh ( queue );
554
DBGCP ( uhci, "\n" );
556
/* Populate periodic frame list */
557
DBGCP ( uhci, "UHCI %s periodic frame list:", uhci->name );
558
for ( i = 0 ; i < UHCI_FRAMES ; i++ ) {
560
/* Calculate maximum interval (in microframes) which
561
* may appear as part of this frame list.
564
/* Start of list: include all endpoints */
567
/* Calculate highest power-of-two frame interval */
568
max_interval = ( 1 << ( ffs ( i ) - 1 ) );
569
/* Convert to microframes */
571
/* Round up to nearest 2^n-1 */
572
max_interval = ( ( max_interval << 1 ) - 1 );
575
/* Find first endpoint in schedule satisfying this
576
* maximum interval constraint.
578
link = uhci_link_qh ( uhci->head );
579
list_for_each_entry ( endpoint, &uhci->periodic, schedule ) {
580
if ( endpoint->ep->interval <= max_interval ) {
581
queue = endpoint->ring.head;
582
link = uhci_link_qh ( queue );
583
DBGCP ( uhci, " %d:%d",
584
i, endpoint->ep->interval );
588
uhci->frame->link[i] = cpu_to_le32 ( link );
591
DBGCP ( uhci, "\n" );
595
* Add endpoint to periodic schedule
597
* @v endpoint Endpoint
599
static void uhci_periodic_add ( struct uhci_endpoint *endpoint ) {
600
struct uhci_device *uhci = endpoint->uhci;
601
struct uhci_endpoint *before;
602
unsigned int interval = endpoint->ep->interval;
604
/* Find first endpoint with a smaller interval */
605
list_for_each_entry ( before, &uhci->periodic, schedule ) {
606
if ( before->ep->interval < interval )
609
list_add_tail ( &endpoint->schedule, &before->schedule );
611
/* Rebuild schedule */
612
uhci_periodic_schedule ( uhci );
616
* Remove endpoint from periodic schedule
618
* @v endpoint Endpoint
620
static void uhci_periodic_del ( struct uhci_endpoint *endpoint ) {
621
struct uhci_device *uhci = endpoint->uhci;
623
/* Remove from schedule */
624
list_check_contains_entry ( endpoint, &uhci->periodic, schedule );
625
list_del ( &endpoint->schedule );
627
/* Rebuild schedule */
628
uhci_periodic_schedule ( uhci );
630
/* Delay for a whole USB frame (with a 100% safety margin) */
635
* Add endpoint to appropriate schedule
637
* @v endpoint Endpoint
639
static void uhci_schedule_add ( struct uhci_endpoint *endpoint ) {
640
struct usb_endpoint *ep = endpoint->ep;
641
unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
643
if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
644
uhci_periodic_add ( endpoint );
646
uhci_async_add ( endpoint );
651
* Remove endpoint from appropriate schedule
653
* @v endpoint Endpoint
655
static void uhci_schedule_del ( struct uhci_endpoint *endpoint ) {
656
struct usb_endpoint *ep = endpoint->ep;
657
unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
659
if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
660
uhci_periodic_del ( endpoint );
662
uhci_async_del ( endpoint );
666
/******************************************************************************
668
* Endpoint operations
670
******************************************************************************
677
* @ret rc Return status code
679
static int uhci_endpoint_open ( struct usb_endpoint *ep ) {
680
struct usb_device *usb = ep->usb;
681
struct uhci_device *uhci = usb_get_hostdata ( usb );
682
struct uhci_endpoint *endpoint;
685
/* Allocate and initialise structure */
686
endpoint = zalloc ( sizeof ( *endpoint ) );
691
endpoint->uhci = uhci;
693
usb_endpoint_set_hostdata ( ep, endpoint );
695
/* Initialise descriptor ring */
696
if ( ( rc = uhci_ring_alloc ( &endpoint->ring ) ) != 0 )
698
endpoint->ring.mtu = ep->mtu;
699
endpoint->ring.flags = UHCI_FL_CERR_MAX;
700
if ( usb->speed < USB_SPEED_FULL )
701
endpoint->ring.flags |= UHCI_FL_LS;
702
endpoint->ring.control = ( UHCI_CONTROL_DEVICE ( usb->address ) |
703
UHCI_CONTROL_ENDPOINT ( ep->address ) );
705
/* Add to list of endpoints */
706
list_add_tail ( &endpoint->list, &uhci->endpoints );
708
/* Add to schedule */
709
uhci_schedule_add ( endpoint );
713
uhci_ring_free ( &endpoint->ring );
725
static void uhci_endpoint_close ( struct usb_endpoint *ep ) {
726
struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
727
struct io_buffer *iobuf;
729
/* Remove from schedule */
730
uhci_schedule_del ( endpoint );
732
/* Cancel any incomplete transfers */
733
while ( uhci_ring_fill ( &endpoint->ring ) ) {
734
iobuf = uhci_dequeue ( &endpoint->ring );
736
usb_complete_err ( ep, iobuf, -ECANCELED );
739
/* Remove from list of endpoints */
740
list_del ( &endpoint->list );
742
/* Free descriptor ring */
743
uhci_ring_free ( &endpoint->ring );
753
* @ret rc Return status code
755
static int uhci_endpoint_reset ( struct usb_endpoint *ep ) {
756
struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
757
struct uhci_ring *ring = &endpoint->ring;
760
uhci_restart ( ring, 0 );
769
* @ret rc Return status code
771
static int uhci_endpoint_mtu ( struct usb_endpoint *ep ) {
772
struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
774
/* Update endpoint MTU */
775
endpoint->ring.mtu = ep->mtu;
781
* Enqueue message transfer
784
* @v iobuf I/O buffer
785
* @ret rc Return status code
787
static int uhci_endpoint_message ( struct usb_endpoint *ep,
788
struct io_buffer *iobuf ) {
789
struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
790
struct uhci_ring *ring = &endpoint->ring;
791
struct usb_setup_packet *packet;
797
/* Calculate number of descriptors */
798
assert ( iob_len ( iobuf ) >= sizeof ( *packet ) );
799
len = ( iob_len ( iobuf ) - sizeof ( *packet ) );
800
count = ( 1 /* setup stage */ +
801
( ( len + ring->mtu - 1 ) / ring->mtu ) /* data stage */ +
802
1 /* status stage */ );
804
/* Enqueue transfer */
805
if ( ( rc = uhci_enqueue ( ring, iobuf, count ) ) != 0 )
808
/* Describe setup stage */
809
packet = iobuf->data;
810
ring->control &= ~UHCI_CONTROL_TOGGLE;
811
uhci_describe ( ring, packet, sizeof ( *packet ), USB_PID_SETUP );
812
iob_pull ( iobuf, sizeof ( *packet ) );
814
/* Describe data stage, if applicable */
815
assert ( ring->control & UHCI_CONTROL_TOGGLE );
816
input = ( packet->request & cpu_to_le16 ( USB_DIR_IN ) );
818
uhci_describe ( ring, iobuf->data, len,
819
( input ? USB_PID_IN : USB_PID_OUT ) );
822
/* Describe status stage */
823
ring->control |= UHCI_CONTROL_TOGGLE;
824
uhci_describe ( ring, NULL, 0,
825
( ( len && input ) ? USB_PID_OUT : USB_PID_IN ) );
828
assert ( ring->end->prod == count );
834
* Enqueue stream transfer
837
* @v iobuf I/O buffer
838
* @v zlp Append a zero-length packet
839
* @ret rc Return status code
841
static int uhci_endpoint_stream ( struct usb_endpoint *ep,
842
struct io_buffer *iobuf, int zlp ) {
843
struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
844
struct uhci_ring *ring = &endpoint->ring;
850
/* Calculate number of descriptors */
851
len = iob_len ( iobuf );
852
count = ( ( ( len + ring->mtu - 1 ) / ring->mtu ) + ( zlp ? 1 : 0 ) );
854
/* Enqueue transfer */
855
if ( ( rc = uhci_enqueue ( ring, iobuf, count ) ) != 0 )
858
/* Describe data packet */
859
input = ( ep->address & USB_DIR_IN );
860
uhci_describe ( ring, iobuf->data, len,
861
( input ? USB_PID_IN : USB_PID_OUT ) );
863
/* Describe zero-length packet, if applicable */
865
uhci_describe ( ring, NULL, 0, USB_PID_OUT );
868
assert ( ring->end->prod == count );
874
* Check if transfer is a message transfer
876
* @v xfer UHCI transfer
877
* @ret is_message Transfer is a message transfer
879
static inline int uhci_is_message ( struct uhci_transfer *xfer ) {
880
struct uhci_transfer_descriptor *desc = &xfer->desc[0];
882
return ( ( desc->control & cpu_to_le32 ( UHCI_CONTROL_PID_MASK ) ) ==
883
cpu_to_le32 ( UHCI_CONTROL_PID ( USB_PID_SETUP ) ) );
887
* Poll for completions
889
* @v endpoint Endpoint
891
static void uhci_endpoint_poll ( struct uhci_endpoint *endpoint ) {
892
struct uhci_ring *ring = &endpoint->ring;
893
struct uhci_device *uhci = endpoint->uhci;
894
struct usb_endpoint *ep = endpoint->ep;
895
struct usb_device *usb = ep->usb;
896
struct uhci_transfer *xfer;
897
struct uhci_transfer_descriptor *desc;
898
struct io_buffer *iobuf;
906
/* Consume all completed descriptors */
907
while ( uhci_ring_fill ( ring ) ) {
909
/* Stop if we reach an uncompleted descriptor */
910
index = ( ring->cons % UHCI_RING_COUNT );
911
xfer = ring->xfer[index];
912
assert ( xfer != NULL );
913
assert ( xfer->cons < xfer->prod );
914
desc = &xfer->desc[xfer->cons];
916
if ( desc->status & UHCI_STATUS_ACTIVE )
918
control = le32_to_cpu ( desc->control );
919
actual = le16_to_cpu ( desc->actual );
921
/* Update data length, if applicable */
922
if ( UHCI_DATA_PACKET ( control ) )
923
xfer->len += UHCI_ACTUAL_LEN ( actual );
925
/* If we have encountered an error, then deactivate
926
* the queue head (to prevent further hardware
927
* accesses to this transfer), consume the transfer,
928
* and report the error to the USB core.
930
if ( desc->status & UHCI_STATUS_STALLED ) {
931
DBGC ( uhci, "UHCI %s %s completion %d.%d failed "
932
"(status %02x)\n", usb->name,
933
usb_endpoint_name ( ep ), index,
934
xfer->cons, desc->status );
935
link = UHCI_LINK_TERMINATE;
936
ring->head->current = cpu_to_le32 ( link );
938
iobuf = uhci_dequeue ( ring );
939
usb_complete_err ( ep, iobuf, -EIO );
943
/* Consume this descriptor */
946
/* Check for short packets */
947
if ( UHCI_SHORT_PACKET ( control, actual ) ) {
950
assert ( desc->flags & UHCI_FL_SPD );
951
link = virt_to_phys ( desc );
952
assert ( ( le32_to_cpu ( ring->head->current ) &
953
~( UHCI_ALIGN - 1 ) ) == link );
955
/* If this is a message transfer, then restart
956
* at the status stage.
958
if ( uhci_is_message ( xfer ) ) {
959
xfer->cons = ( xfer->prod - 1 );
960
link = virt_to_phys ( &xfer->desc[xfer->cons] );
961
ring->head->current = cpu_to_le32 ( link );
965
/* Otherwise, this is a stream transfer.
966
* First, prevent further hardware access to
969
link = UHCI_LINK_TERMINATE;
970
ring->head->current = cpu_to_le32 ( link );
973
/* Determine expected data toggle for next descriptor */
974
toggle = ( ( control ^ UHCI_CONTROL_TOGGLE ) &
975
UHCI_CONTROL_TOGGLE );
977
/* Consume this transfer */
979
iobuf = uhci_dequeue ( ring );
981
/* Update packet length */
982
assert ( len <= iob_len ( iobuf ) );
983
iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
986
uhci_restart ( ring, toggle );
988
} else if ( xfer->cons == xfer->prod ) {
990
/* Completed a transfer: consume it */
992
iobuf = uhci_dequeue ( ring );
993
assert ( len == iob_len ( iobuf ) );
997
/* Not a short packet and not yet complete:
998
* continue processing.
1003
/* Report completion to USB core */
1004
usb_complete ( ep, iobuf );
1008
/******************************************************************************
1012
******************************************************************************
1019
* @ret rc Return status code
1021
static int uhci_device_open ( struct usb_device *usb ) {
1022
struct uhci_device *uhci = usb_bus_get_hostdata ( usb->port->hub->bus );
1024
usb_set_hostdata ( usb, uhci );
1033
static void uhci_device_close ( struct usb_device *usb ) {
1034
struct uhci_device *uhci = usb_get_hostdata ( usb );
1035
struct usb_bus *bus = uhci->bus;
1037
/* Free device address, if assigned */
1039
usb_free_address ( bus, usb->address );
1043
* Assign device address
1046
* @ret rc Return status code
1048
static int uhci_device_address ( struct usb_device *usb ) {
1049
struct uhci_device *uhci = usb_get_hostdata ( usb );
1050
struct usb_bus *bus = uhci->bus;
1051
struct usb_endpoint *ep0 = usb_endpoint ( usb, USB_EP0_ADDRESS );
1052
struct uhci_endpoint *endpoint0 = usb_endpoint_get_hostdata ( ep0 );
1057
assert ( usb->address == 0 );
1058
assert ( ep0 != NULL );
1060
/* Allocate device address */
1061
address = usb_alloc_address ( bus );
1062
if ( address < 0 ) {
1064
DBGC ( uhci, "UHCI %s could not allocate address: %s\n",
1065
usb->name, strerror ( rc ) );
1066
goto err_alloc_address;
1070
if ( ( rc = usb_set_address ( usb, address ) ) != 0 )
1071
goto err_set_address;
1073
/* Update device address */
1074
usb->address = address;
1075
endpoint0->ring.control |= UHCI_CONTROL_DEVICE ( address );
1080
usb_free_address ( bus, address );
1085
/******************************************************************************
1089
******************************************************************************
1096
* @ret rc Return status code
1098
static int uhci_hub_open ( struct usb_hub *hub __unused ) {
1109
static void uhci_hub_close ( struct usb_hub *hub __unused ) {
1114
/******************************************************************************
1116
* Root hub operations
1118
******************************************************************************
1125
* @ret rc Return status code
1127
static int uhci_root_open ( struct usb_hub *hub ) {
1128
struct usb_bus *bus = hub->bus;
1129
struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
1131
/* Record hub driver private data */
1132
usb_hub_set_drvdata ( hub, uhci );
1142
static void uhci_root_close ( struct usb_hub *hub ) {
1144
/* Clear hub driver private data */
1145
usb_hub_set_drvdata ( hub, NULL );
1153
* @ret rc Return status code
1155
static int uhci_root_enable ( struct usb_hub *hub, struct usb_port *port ) {
1156
struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
1161
portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
1162
portsc |= UHCI_PORTSC_PR;
1163
outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1164
mdelay ( USB_RESET_DELAY_MS );
1165
portsc &= ~UHCI_PORTSC_PR;
1166
outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1167
mdelay ( USB_RESET_RECOVER_DELAY_MS );
1170
portsc |= UHCI_PORTSC_PED;
1171
outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1172
mdelay ( USB_RESET_RECOVER_DELAY_MS );
1174
/* Wait for port to become enabled */
1175
for ( i = 0 ; i < UHCI_PORT_ENABLE_MAX_WAIT_MS ; i++ ) {
1177
/* Check port status */
1178
portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
1179
if ( portsc & UHCI_PORTSC_PED )
1186
DBGC ( uhci, "UHCI %s-%d timed out waiting for port to enable "
1187
"(status %04x)\n", uhci->name, port->address, portsc );
1196
* @ret rc Return status code
1198
static int uhci_root_disable ( struct usb_hub *hub, struct usb_port *port ) {
1199
struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
1203
portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
1204
portsc &= ~UHCI_PORTSC_PED;
1205
outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1211
* Update root hub port speed
1215
* @ret rc Return status code
1217
static int uhci_root_speed ( struct usb_hub *hub, struct usb_port *port ) {
1218
struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
1219
struct pci_device pci;
1223
/* Read port status */
1224
portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
1225
if ( ! ( portsc & UHCI_PORTSC_CCS ) ) {
1226
/* Port not connected */
1227
speed = USB_SPEED_NONE;
1228
} else if ( uhci->companion &&
1229
! find_usb_bus_by_location ( BUS_TYPE_PCI,
1230
uhci->companion ) ) {
1231
/* Defer connection detection until companion
1232
* controller has been enumerated.
1234
pci_init ( &pci, uhci->companion );
1235
DBGC ( uhci, "UHCI %s-%d deferring for companion " PCI_FMT "\n",
1236
uhci->name, port->address, PCI_ARGS ( &pci ) );
1237
speed = USB_SPEED_NONE;
1238
} else if ( portsc & UHCI_PORTSC_LS ) {
1239
/* Low-speed device */
1240
speed = USB_SPEED_LOW;
1242
/* Full-speed device */
1243
speed = USB_SPEED_FULL;
1245
port->speed = speed;
1247
/* Record disconnections and clear changes */
1248
port->disconnected |= ( portsc & UHCI_PORTSC_CSC );
1249
outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1255
* Clear transaction translator buffer
1259
* @v ep USB endpoint
1260
* @ret rc Return status code
1262
static int uhci_root_clear_tt ( struct usb_hub *hub, struct usb_port *port,
1263
struct usb_endpoint *ep ) {
1264
struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
1266
/* Should never be called; this is a root hub */
1267
DBGC ( uhci, "UHCI %s-%d nonsensical CLEAR_TT for %s %s\n", uhci->name,
1268
port->address, ep->usb->name, usb_endpoint_name ( ep ) );
1274
* Poll for port status changes
1279
static void uhci_root_poll ( struct usb_hub *hub, struct usb_port *port ) {
1280
struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
1284
/* Do nothing unless something has changed */
1285
portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
1286
change = ( portsc & UHCI_PORTSC_CHANGE );
1290
/* Record disconnections and clear changes */
1291
port->disconnected |= ( portsc & UHCI_PORTSC_CSC );
1292
outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
1294
/* Report port status change */
1295
usb_port_changed ( port );
1298
/******************************************************************************
1302
******************************************************************************
1309
* @ret rc Return status code
1311
static int uhci_bus_open ( struct usb_bus *bus ) {
1312
struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
1316
assert ( list_empty ( &uhci->async ) );
1317
assert ( list_empty ( &uhci->periodic ) );
1319
/* Allocate and initialise asynchronous queue head */
1320
uhci->head = malloc_dma ( sizeof ( *uhci->head ), UHCI_ALIGN );
1321
if ( ! uhci->head ) {
1323
goto err_alloc_head;
1325
if ( ( rc = uhci_reachable ( uhci->head, sizeof ( *uhci->head ) ) ) !=0)
1326
goto err_unreachable_head;
1327
memset ( uhci->head, 0, sizeof ( *uhci->head ) );
1328
uhci->head->current = cpu_to_le32 ( UHCI_LINK_TERMINATE );
1329
uhci_async_schedule ( uhci );
1331
/* Allocate periodic frame list */
1332
uhci->frame = malloc_dma ( sizeof ( *uhci->frame ),
1333
sizeof ( *uhci->frame ) );
1334
if ( ! uhci->frame ) {
1336
goto err_alloc_frame;
1338
if ( ( rc = uhci_reachable ( uhci->frame,
1339
sizeof ( *uhci->frame ) ) ) != 0 )
1340
goto err_unreachable_frame;
1341
uhci_periodic_schedule ( uhci );
1342
outl ( virt_to_phys ( uhci->frame ), uhci->regs + UHCI_FLBASEADD );
1344
/* Start controller */
1350
err_unreachable_frame:
1351
free_dma ( uhci->frame, sizeof ( *uhci->frame ) );
1353
err_unreachable_head:
1354
free_dma ( uhci->head, sizeof ( *uhci->head ) );
1364
static void uhci_bus_close ( struct usb_bus *bus ) {
1365
struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
1368
assert ( list_empty ( &uhci->async ) );
1369
assert ( list_empty ( &uhci->periodic ) );
1371
/* Stop controller */
1374
/* Free periodic frame list */
1375
free_dma ( uhci->frame, sizeof ( *uhci->frame ) );
1377
/* Free asynchronous schedule */
1378
free_dma ( uhci->head, sizeof ( *uhci->head ) );
1386
static void uhci_bus_poll ( struct usb_bus *bus ) {
1387
struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
1388
struct usb_hub *hub = bus->hub;
1389
struct uhci_endpoint *endpoint;
1392
/* UHCI defers interrupts (including short packet detection)
1393
* until the end of the frame. This can result in bulk IN
1394
* endpoints remaining halted for much of the time, waiting
1395
* for software action to reset the data toggles. We
1396
* therefore ignore USBSTS and unconditionally poll all
1397
* endpoints for completed transfer descriptors.
1399
* As with EHCI, we trust that completion handlers are minimal
1400
* and will not do anything that could plausibly affect the
1401
* endpoint list itself.
1403
list_for_each_entry ( endpoint, &uhci->endpoints, list )
1404
uhci_endpoint_poll ( endpoint );
1406
/* UHCI provides no single bit to indicate that a port status
1407
* change has occurred. We therefore unconditionally iterate
1408
* over all ports looking for status changes.
1410
for ( i = 1 ; i <= UHCI_PORTS ; i++ )
1411
uhci_root_poll ( hub, usb_port ( hub, i ) );
1414
/******************************************************************************
1418
******************************************************************************
1421
/** USB host controller operations */
1422
static struct usb_host_operations uhci_operations = {
1424
.open = uhci_endpoint_open,
1425
.close = uhci_endpoint_close,
1426
.reset = uhci_endpoint_reset,
1427
.mtu = uhci_endpoint_mtu,
1428
.message = uhci_endpoint_message,
1429
.stream = uhci_endpoint_stream,
1432
.open = uhci_device_open,
1433
.close = uhci_device_close,
1434
.address = uhci_device_address,
1437
.open = uhci_bus_open,
1438
.close = uhci_bus_close,
1439
.poll = uhci_bus_poll,
1442
.open = uhci_hub_open,
1443
.close = uhci_hub_close,
1446
.open = uhci_root_open,
1447
.close = uhci_root_close,
1448
.enable = uhci_root_enable,
1449
.disable = uhci_root_disable,
1450
.speed = uhci_root_speed,
1451
.clear_tt = uhci_root_clear_tt,
1456
* Locate EHCI companion controller (when no EHCI support is present)
1459
* @ret busdevfn EHCI companion controller bus:dev.fn (if any)
1461
__weak unsigned int ehci_companion ( struct pci_device *pci __unused ) {
1469
* @ret rc Return status code
1471
static int uhci_probe ( struct pci_device *pci ) {
1472
struct uhci_device *uhci;
1473
struct usb_port *port;
1477
/* Allocate and initialise structure */
1478
uhci = zalloc ( sizeof ( *uhci ) );
1483
uhci->name = pci->dev.name;
1484
INIT_LIST_HEAD ( &uhci->endpoints );
1485
INIT_LIST_HEAD ( &uhci->async );
1486
INIT_LIST_HEAD ( &uhci->periodic );
1488
/* Fix up PCI device */
1489
adjust_pci_device ( pci );
1491
/* Identify EHCI companion controller, if any */
1492
uhci->companion = ehci_companion ( pci );
1494
/* Claim ownership from BIOS. (There is no release mechanism
1497
pci_write_config_word ( pci, UHCI_USBLEGSUP, UHCI_USBLEGSUP_DEFAULT );
1500
uhci->regs = pci->ioaddr;
1501
if ( ! uhci->regs ) {
1507
if ( ( rc = uhci_reset ( uhci ) ) != 0 )
1510
/* Allocate USB bus */
1511
uhci->bus = alloc_usb_bus ( &pci->dev, UHCI_PORTS, UHCI_MTU,
1513
if ( ! uhci->bus ) {
1517
usb_bus_set_hostdata ( uhci->bus, uhci );
1518
usb_hub_set_drvdata ( uhci->bus->hub, uhci );
1520
/* Set port protocols */
1521
for ( i = 1 ; i <= UHCI_PORTS ; i++ ) {
1522
port = usb_port ( uhci->bus->hub, i );
1523
port->protocol = USB_PROTO_2_0;
1526
/* Register USB bus */
1527
if ( ( rc = register_usb_bus ( uhci->bus ) ) != 0 )
1530
pci_set_drvdata ( pci, uhci );
1533
unregister_usb_bus ( uhci->bus );
1535
free_usb_bus ( uhci->bus );
1537
uhci_reset ( uhci );
1550
static void uhci_remove ( struct pci_device *pci ) {
1551
struct uhci_device *uhci = pci_get_drvdata ( pci );
1552
struct usb_bus *bus = uhci->bus;
1554
unregister_usb_bus ( bus );
1555
assert ( list_empty ( &uhci->async ) );
1556
assert ( list_empty ( &uhci->periodic ) );
1557
free_usb_bus ( bus );
1558
uhci_reset ( uhci );
1562
/** UHCI PCI device IDs */
1563
static struct pci_device_id uhci_ids[] = {
1564
PCI_ROM ( 0xffff, 0xffff, "uhci", "UHCI", 0 ),
1567
/** UHCI PCI driver */
1568
struct pci_driver uhci_driver __pci_driver = {
1570
.id_count = ( sizeof ( uhci_ids ) / sizeof ( uhci_ids[0] ) ),
1571
.class = PCI_CLASS_ID ( PCI_CLASS_SERIAL, PCI_CLASS_SERIAL_USB,
1572
PCI_CLASS_SERIAL_USB_UHCI ),
1573
.probe = uhci_probe,
1574
.remove = uhci_remove,