iPXE
uhci.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <strings.h>
00027 #include <unistd.h>
00028 #include <errno.h>
00029 #include <byteswap.h>
00030 #include <ipxe/malloc.h>
00031 #include <ipxe/pci.h>
00032 #include <ipxe/usb.h>
00033 #include "ehci.h"
00034 #include "uhci.h"
00035 
00036 /** @file
00037  *
00038  * USB Universal Host Controller Interface (UHCI) driver
00039  *
00040  */
00041 
00042 /******************************************************************************
00043  *
00044  * Register access
00045  *
00046  ******************************************************************************
00047  */
00048 
00049 /**
00050  * Check that address is reachable
00051  *
00052  * @v addr              Address
00053  * @v len               Length
00054  * @ret rc              Return status code
00055  */
00056 static inline __attribute__ (( always_inline)) int
00057 uhci_reachable ( void *addr, size_t len ) {
00058         physaddr_t phys = virt_to_phys ( addr );
00059 
00060         /* Always reachable in a 32-bit build */
00061         if ( sizeof ( physaddr_t ) <= sizeof ( uint32_t ) )
00062                 return 0;
00063 
00064         /* Reachable if below 4GB */
00065         if ( ( ( phys + len - 1 ) & ~0xffffffffULL ) == 0 )
00066                 return 0;
00067 
00068         return -ENOTSUP;
00069 }
00070 
00071 /******************************************************************************
00072  *
00073  * Run / stop / reset
00074  *
00075  ******************************************************************************
00076  */
00077 
00078 /**
00079  * Start UHCI device
00080  *
00081  * @v uhci              UHCI device
00082  */
00083 static void uhci_run ( struct uhci_device *uhci ) {
00084         uint16_t usbcmd;
00085 
00086         /* Set run/stop bit */
00087         usbcmd = inw ( uhci->regs + UHCI_USBCMD );
00088         usbcmd |= ( UHCI_USBCMD_RUN | UHCI_USBCMD_MAX64 );
00089         outw ( usbcmd, uhci->regs + UHCI_USBCMD );
00090 }
00091 
00092 /**
00093  * Stop UHCI device
00094  *
00095  * @v uhci              UHCI device
00096  * @ret rc              Return status code
00097  */
00098 static int uhci_stop ( struct uhci_device *uhci ) {
00099         uint16_t usbcmd;
00100         uint16_t usbsts;
00101         unsigned int i;
00102 
00103         /* Clear run/stop bit */
00104         usbcmd = inw ( uhci->regs + UHCI_USBCMD );
00105         usbcmd &= ~UHCI_USBCMD_RUN;
00106         outw ( usbcmd, uhci->regs + UHCI_USBCMD );
00107 
00108         /* Wait for device to stop */
00109         for ( i = 0 ; i < UHCI_STOP_MAX_WAIT_MS ; i++ ) {
00110 
00111                 /* Check if device is stopped */
00112                 usbsts = inw ( uhci->regs + UHCI_USBSTS );
00113                 if ( usbsts & UHCI_USBSTS_HCHALTED )
00114                         return 0;
00115 
00116                 /* Delay */
00117                 mdelay ( 1 );
00118         }
00119 
00120         DBGC ( uhci, "UHCI %s timed out waiting for stop\n", uhci->name );
00121         return -ETIMEDOUT;
00122 }
00123 
00124 /**
00125  * Reset UHCI device
00126  *
00127  * @v uhci              UHCI device
00128  * @ret rc              Return status code
00129  */
00130 static int uhci_reset ( struct uhci_device *uhci ) {
00131         uint16_t usbcmd;
00132         unsigned int i;
00133         int rc;
00134 
00135         /* The UHCI specification states that resetting a running
00136          * device may result in undefined behaviour, so try stopping
00137          * it first.
00138          */
00139         if ( ( rc = uhci_stop ( uhci ) ) != 0 ) {
00140                 /* Ignore errors and attempt to reset the device anyway */
00141         }
00142 
00143         /* Reset device */
00144         outw ( UHCI_USBCMD_HCRESET, uhci->regs + UHCI_USBCMD );
00145 
00146         /* Wait for reset to complete */
00147         for ( i = 0 ; i < UHCI_RESET_MAX_WAIT_MS ; i++ ) {
00148 
00149                 /* Check if reset is complete */
00150                 usbcmd = inw ( uhci->regs + UHCI_USBCMD );
00151                 if ( ! ( usbcmd & UHCI_USBCMD_HCRESET ) )
00152                         return 0;
00153 
00154                 /* Delay */
00155                 mdelay ( 1 );
00156         }
00157 
00158         DBGC ( uhci, "UHCI %s timed out waiting for reset\n", uhci->name );
00159         return -ETIMEDOUT;
00160 }
00161 
00162 /******************************************************************************
00163  *
00164  * Transfer descriptor rings
00165  *
00166  ******************************************************************************
00167  */
00168 
00169 /**
00170  * Allocate transfer ring
00171  *
00172  * @v ring              Transfer ring
00173  * @ret rc              Return status code
00174  */
00175 static int uhci_ring_alloc ( struct uhci_ring *ring ) {
00176         int rc;
00177 
00178         /* Initialise structure */
00179         memset ( ring, 0, sizeof ( *ring ) );
00180 
00181         /* Allocate queue head */
00182         ring->head = malloc_dma ( sizeof ( *ring->head ), UHCI_ALIGN );
00183         if ( ! ring->head ) {
00184                 rc = -ENOMEM;
00185                 goto err_alloc;
00186         }
00187         if ( ( rc = uhci_reachable ( ring->head,
00188                                      sizeof ( *ring->head ) ) ) != 0 )
00189                 goto err_unreachable;
00190 
00191         /* Initialise queue head */
00192         ring->head->current = cpu_to_le32 ( UHCI_LINK_TERMINATE );
00193 
00194         return 0;
00195 
00196  err_unreachable:
00197         free_dma ( ring->head, sizeof ( *ring->head ) );
00198  err_alloc:
00199         return rc;
00200 }
00201 
00202 /**
00203  * Free transfer ring
00204  *
00205  * @v ring              Transfer ring
00206  */
00207 static void uhci_ring_free ( struct uhci_ring *ring ) {
00208         unsigned int i;
00209 
00210         /* Sanity checks */
00211         assert ( uhci_ring_fill ( ring ) == 0 );
00212         for ( i = 0 ; i < UHCI_RING_COUNT ; i++ )
00213                 assert ( ring->xfer[i] == NULL );
00214 
00215         /* Free queue head */
00216         free_dma ( ring->head, sizeof ( *ring->head ) );
00217 }
00218 
00219 /**
00220  * Enqueue new transfer
00221  *
00222  * @v ring              Transfer ring
00223  * @v iobuf             I/O buffer
00224  * @v count             Number of descriptors
00225  * @ret rc              Return status code
00226  */
00227 static int uhci_enqueue ( struct uhci_ring *ring, struct io_buffer *iobuf,
00228                           unsigned int count ) {
00229         struct uhci_transfer *xfer;
00230         struct uhci_transfer *end;
00231         struct uhci_transfer_descriptor *desc;
00232         unsigned int index = ( ring->prod % UHCI_RING_COUNT );
00233         uint32_t link;
00234         size_t len;
00235         int rc;
00236 
00237         /* Sanity check */
00238         assert ( count > 0 );
00239         assert ( iobuf != NULL );
00240 
00241         /* Check for space in ring */
00242         if ( ! uhci_ring_remaining ( ring ) ) {
00243                 rc = -ENOBUFS;
00244                 goto err_ring_full;
00245         }
00246 
00247         /* Check for reachability of I/O buffer */
00248         if ( ( rc = uhci_reachable ( iobuf->data, iob_len ( iobuf ) ) ) != 0 )
00249                 goto err_unreachable_iobuf;
00250 
00251         /* Allocate transfer */
00252         xfer = malloc ( sizeof ( *xfer ) );
00253         if ( ! xfer ) {
00254                 rc = -ENOMEM;
00255                 goto err_alloc_xfer;
00256         }
00257 
00258         /* Initialise transfer */
00259         xfer->prod = 0;
00260         xfer->cons = 0;
00261         xfer->len = 0;
00262         xfer->iobuf = iobuf;
00263 
00264         /* Allocate transfer descriptors */
00265         len = ( count * sizeof ( xfer->desc[0] ) );
00266         xfer->desc = malloc_dma ( len, UHCI_ALIGN );
00267         if ( ! xfer->desc ) {
00268                 rc = -ENOMEM;
00269                 goto err_alloc_desc;
00270         }
00271         if ( ( rc = uhci_reachable ( xfer->desc, len ) ) != 0 )
00272                 goto err_unreachable_desc;
00273 
00274         /* Initialise transfer descriptors */
00275         memset ( xfer->desc, 0, len );
00276         desc = xfer->desc;
00277         for ( ; --count ; desc++ ) {
00278                 link = ( virt_to_phys ( desc + 1 ) | UHCI_LINK_DEPTH_FIRST );
00279                 desc->link = cpu_to_le32 ( link );
00280                 desc->flags = ring->flags;
00281         }
00282         desc->link = cpu_to_le32 ( UHCI_LINK_TERMINATE );
00283         desc->flags = ( ring->flags | UHCI_FL_IOC );
00284 
00285         /* Add to ring */
00286         wmb();
00287         link = virt_to_phys ( xfer->desc );
00288         if ( uhci_ring_fill ( ring ) > 0 ) {
00289                 end = ring->end;
00290                 end->desc[ end->prod - 1 ].link = cpu_to_le32 ( link );
00291         } else {
00292                 ring->head->current = cpu_to_le32 ( link );
00293         }
00294         assert ( ring->xfer[index] == NULL );
00295         ring->xfer[index] = xfer;
00296         ring->end = xfer;
00297         ring->prod++;
00298 
00299         return 0;
00300 
00301  err_unreachable_desc:
00302         free_dma ( xfer->desc, len );
00303  err_alloc_desc:
00304         free ( xfer );
00305  err_alloc_xfer:
00306  err_unreachable_iobuf:
00307  err_ring_full:
00308         return rc;
00309 }
00310 
00311 /**
00312  * Describe transfer
00313  *
00314  * @v ring              Transfer ring
00315  * @v data              Data
00316  * @v len               Length of data
00317  * @v pid               Packet ID
00318  */
00319 static void uhci_describe ( struct uhci_ring *ring, void *data,
00320                             size_t len, uint8_t pid ) {
00321         struct uhci_transfer *xfer = ring->end;
00322         struct uhci_transfer_descriptor *desc;
00323         size_t frag_len;
00324         uint32_t control;
00325 
00326         do {
00327                 /* Calculate fragment length */
00328                 frag_len = len;
00329                 if ( frag_len > ring->mtu )
00330                         frag_len = ring->mtu;
00331 
00332                 /* Populate descriptor */
00333                 desc = &xfer->desc[xfer->prod++];
00334                 if ( pid == USB_PID_IN )
00335                         desc->flags |= UHCI_FL_SPD;
00336                 control = ( ring->control | UHCI_CONTROL_PID ( pid ) |
00337                             UHCI_CONTROL_LEN ( frag_len ) );
00338                 desc->control = cpu_to_le32 ( control );
00339                 if ( data )
00340                         desc->data = virt_to_phys ( data );
00341                 wmb();
00342                 desc->status = UHCI_STATUS_ACTIVE;
00343 
00344                 /* Update data toggle */
00345                 ring->control ^= UHCI_CONTROL_TOGGLE;
00346 
00347                 /* Move to next descriptor */
00348                 data += frag_len;
00349                 len -= frag_len;
00350 
00351         } while ( len );
00352 }
00353 
00354 /**
00355  * Dequeue transfer
00356  *
00357  * @v ring              Transfer ring
00358  * @ret iobuf           I/O buffer
00359  */
00360 static struct io_buffer * uhci_dequeue ( struct uhci_ring *ring ) {
00361         unsigned int index = ( ring->cons % UHCI_RING_COUNT );
00362         struct io_buffer *iobuf;
00363         struct uhci_transfer *xfer;
00364         size_t len;
00365 
00366         /* Sanity checks */
00367         assert ( uhci_ring_fill ( ring ) > 0 );
00368 
00369         /* Consume transfer */
00370         xfer = ring->xfer[index];
00371         assert ( xfer != NULL );
00372         assert ( xfer->desc != NULL );
00373         iobuf = xfer->iobuf;
00374         assert ( iobuf != NULL );
00375         ring->xfer[index] = NULL;
00376         ring->cons++;
00377 
00378         /* Free transfer descriptors */
00379         len = ( xfer->prod * sizeof ( xfer->desc[0] ) );
00380         free_dma ( xfer->desc, len );
00381 
00382         /* Free transfer */
00383         free ( xfer );
00384 
00385         return iobuf;
00386 }
00387 
00388 /**
00389  * Restart ring
00390  *
00391  * @v ring              Transfer ring
00392  * @v toggle            Expected data toggle for next descriptor
00393  */
00394 static void uhci_restart ( struct uhci_ring *ring, uint32_t toggle ) {
00395         struct uhci_transfer *xfer;
00396         struct uhci_transfer_descriptor *desc;
00397         struct uhci_transfer_descriptor *first;
00398         uint32_t link;
00399         unsigned int i;
00400         unsigned int j;
00401 
00402         /* Sanity check */
00403         assert ( ring->head->current == cpu_to_le32 ( UHCI_LINK_TERMINATE ) );
00404 
00405         /* If ring is empty, then just update the data toggle for the
00406          * next descriptor.
00407          */
00408         if ( uhci_ring_fill ( ring ) == 0 ) {
00409                 ring->control &= ~UHCI_CONTROL_TOGGLE;
00410                 ring->control |= toggle;
00411                 return;
00412         }
00413 
00414         /* If expected toggle does not match the toggle in the first
00415          * unconsumed descriptor, then invert all toggles.
00416          */
00417         xfer = ring->xfer[ ring->cons % UHCI_RING_COUNT ];
00418         assert ( xfer != NULL );
00419         assert ( xfer->cons == 0 );
00420         first = &xfer->desc[0];
00421         if ( ( le32_to_cpu ( first->control ) ^ toggle ) & UHCI_CONTROL_TOGGLE){
00422 
00423                 /* Invert toggle on all unconsumed transfer descriptors */
00424                 for ( i = ring->cons ; i != ring->prod ; i++ ) {
00425                         xfer = ring->xfer[ i % UHCI_RING_COUNT ];
00426                         assert ( xfer != NULL );
00427                         assert ( xfer->cons == 0 );
00428                         for ( j = 0 ; j < xfer->prod ; j++ ) {
00429                                 desc = &xfer->desc[j];
00430                                 desc->control ^=
00431                                         cpu_to_le32 ( UHCI_CONTROL_TOGGLE );
00432                         }
00433                 }
00434 
00435                 /* Invert toggle for next descriptor to be enqueued */
00436                 ring->control ^= UHCI_CONTROL_TOGGLE;
00437         }
00438 
00439         /* Restart ring at first unconsumed transfer */
00440         link = virt_to_phys ( first );
00441         wmb();
00442         ring->head->current = cpu_to_le32 ( link );
00443 }
00444 
00445 /******************************************************************************
00446  *
00447  * Schedule management
00448  *
00449  ******************************************************************************
00450  */
00451 
00452 /**
00453  * Get link value for a queue head
00454  *
00455  * @v queue             Queue head
00456  * @ret link            Link value
00457  */
00458 static inline uint32_t uhci_link_qh ( struct uhci_queue_head *queue ) {
00459 
00460         return ( virt_to_phys ( queue ) | UHCI_LINK_TYPE_QH );
00461 }
00462 
00463 /**
00464  * (Re)build asynchronous schedule
00465  *
00466  * @v uhci              UHCI device
00467  */
00468 static void uhci_async_schedule ( struct uhci_device *uhci ) {
00469         struct uhci_endpoint *endpoint;
00470         struct uhci_queue_head *queue;
00471         uint32_t end;
00472         uint32_t link;
00473 
00474         /* Build schedule in reverse order of execution.  Provided
00475          * that we only ever add or remove single endpoints, this can
00476          * safely run concurrently with hardware execution of the
00477          * schedule.
00478          */
00479         link = end = uhci_link_qh ( uhci->head );
00480         list_for_each_entry_reverse ( endpoint, &uhci->async, schedule ) {
00481                 queue = endpoint->ring.head;
00482                 queue->link = cpu_to_le32 ( link );
00483                 wmb();
00484                 link = uhci_link_qh ( queue );
00485         }
00486         if ( link == end )
00487                 link = UHCI_LINK_TERMINATE;
00488         uhci->head->link = cpu_to_le32 ( link );
00489         wmb();
00490 }
00491 
00492 /**
00493  * Add endpoint to asynchronous schedule
00494  *
00495  * @v endpoint          Endpoint
00496  */
00497 static void uhci_async_add ( struct uhci_endpoint *endpoint ) {
00498         struct uhci_device *uhci = endpoint->uhci;
00499 
00500         /* Add to end of schedule */
00501         list_add_tail ( &endpoint->schedule, &uhci->async );
00502 
00503         /* Rebuild schedule */
00504         uhci_async_schedule ( uhci );
00505 }
00506 
00507 /**
00508  * Remove endpoint from asynchronous schedule
00509  *
00510  * @v endpoint          Endpoint
00511  */
00512 static void uhci_async_del ( struct uhci_endpoint *endpoint ) {
00513         struct uhci_device *uhci = endpoint->uhci;
00514 
00515         /* Remove from schedule */
00516         list_check_contains_entry ( endpoint, &uhci->async, schedule );
00517         list_del ( &endpoint->schedule );
00518 
00519         /* Rebuild schedule */
00520         uhci_async_schedule ( uhci );
00521 
00522         /* Delay for a whole USB frame (with a 100% safety margin) */
00523         mdelay ( 2 );
00524 }
00525 
00526 /**
00527  * (Re)build periodic schedule
00528  *
00529  * @v uhci              UHCI device
00530  */
00531 static void uhci_periodic_schedule ( struct uhci_device *uhci ) {
00532         struct uhci_endpoint *endpoint;
00533         struct uhci_queue_head *queue;
00534         uint32_t link;
00535         uint32_t end;
00536         unsigned int max_interval;
00537         unsigned int i;
00538 
00539         /* Build schedule in reverse order of execution.  Provided
00540          * that we only ever add or remove single endpoints, this can
00541          * safely run concurrently with hardware execution of the
00542          * schedule.
00543          */
00544         DBGCP ( uhci, "UHCI %s periodic schedule: ", uhci->name );
00545         link = end = uhci_link_qh ( uhci->head );
00546         list_for_each_entry_reverse ( endpoint, &uhci->periodic, schedule ) {
00547                 queue = endpoint->ring.head;
00548                 queue->link = cpu_to_le32 ( link );
00549                 wmb();
00550                 DBGCP ( uhci, "%s%d", ( ( link == end ) ? "" : "<-" ),
00551                         endpoint->ep->interval );
00552                 link = uhci_link_qh ( queue );
00553         }
00554         DBGCP ( uhci, "\n" );
00555 
00556         /* Populate periodic frame list */
00557         DBGCP ( uhci, "UHCI %s periodic frame list:", uhci->name );
00558         for ( i = 0 ; i < UHCI_FRAMES ; i++ ) {
00559 
00560                 /* Calculate maximum interval (in microframes) which
00561                  * may appear as part of this frame list.
00562                  */
00563                 if ( i == 0 ) {
00564                         /* Start of list: include all endpoints */
00565                         max_interval = -1U;
00566                 } else {
00567                         /* Calculate highest power-of-two frame interval */
00568                         max_interval = ( 1 << ( ffs ( i ) - 1 ) );
00569                         /* Convert to microframes */
00570                         max_interval <<= 3;
00571                         /* Round up to nearest 2^n-1 */
00572                         max_interval = ( ( max_interval << 1 ) - 1 );
00573                 }
00574 
00575                 /* Find first endpoint in schedule satisfying this
00576                  * maximum interval constraint.
00577                  */
00578                 link = uhci_link_qh ( uhci->head );
00579                 list_for_each_entry ( endpoint, &uhci->periodic, schedule ) {
00580                         if ( endpoint->ep->interval <= max_interval ) {
00581                                 queue = endpoint->ring.head;
00582                                 link = uhci_link_qh ( queue );
00583                                 DBGCP ( uhci, " %d:%d",
00584                                         i, endpoint->ep->interval );
00585                                 break;
00586                         }
00587                 }
00588                 uhci->frame->link[i] = cpu_to_le32 ( link );
00589         }
00590         wmb();
00591         DBGCP ( uhci, "\n" );
00592 }
00593 
00594 /**
00595  * Add endpoint to periodic schedule
00596  *
00597  * @v endpoint          Endpoint
00598  */
00599 static void uhci_periodic_add ( struct uhci_endpoint *endpoint ) {
00600         struct uhci_device *uhci = endpoint->uhci;
00601         struct uhci_endpoint *before;
00602         unsigned int interval = endpoint->ep->interval;
00603 
00604         /* Find first endpoint with a smaller interval */
00605         list_for_each_entry ( before, &uhci->periodic, schedule ) {
00606                 if ( before->ep->interval < interval )
00607                         break;
00608         }
00609         list_add_tail ( &endpoint->schedule, &before->schedule );
00610 
00611         /* Rebuild schedule */
00612         uhci_periodic_schedule ( uhci );
00613 }
00614 
00615 /**
00616  * Remove endpoint from periodic schedule
00617  *
00618  * @v endpoint          Endpoint
00619  */
00620 static void uhci_periodic_del ( struct uhci_endpoint *endpoint ) {
00621         struct uhci_device *uhci = endpoint->uhci;
00622 
00623         /* Remove from schedule */
00624         list_check_contains_entry ( endpoint, &uhci->periodic, schedule );
00625         list_del ( &endpoint->schedule );
00626 
00627         /* Rebuild schedule */
00628         uhci_periodic_schedule ( uhci );
00629 
00630         /* Delay for a whole USB frame (with a 100% safety margin) */
00631         mdelay ( 2 );
00632 }
00633 
00634 /**
00635  * Add endpoint to appropriate schedule
00636  *
00637  * @v endpoint          Endpoint
00638  */
00639 static void uhci_schedule_add ( struct uhci_endpoint *endpoint ) {
00640         struct usb_endpoint *ep = endpoint->ep;
00641         unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
00642 
00643         if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
00644                 uhci_periodic_add ( endpoint );
00645         } else {
00646                 uhci_async_add ( endpoint );
00647         }
00648 }
00649 
00650 /**
00651  * Remove endpoint from appropriate schedule
00652  *
00653  * @v endpoint          Endpoint
00654  */
00655 static void uhci_schedule_del ( struct uhci_endpoint *endpoint ) {
00656         struct usb_endpoint *ep = endpoint->ep;
00657         unsigned int attr = ( ep->attributes & USB_ENDPOINT_ATTR_TYPE_MASK );
00658 
00659         if ( attr == USB_ENDPOINT_ATTR_INTERRUPT ) {
00660                 uhci_periodic_del ( endpoint );
00661         } else {
00662                 uhci_async_del ( endpoint );
00663         }
00664 }
00665 
00666 /******************************************************************************
00667  *
00668  * Endpoint operations
00669  *
00670  ******************************************************************************
00671  */
00672 
00673 /**
00674  * Open endpoint
00675  *
00676  * @v ep                USB endpoint
00677  * @ret rc              Return status code
00678  */
00679 static int uhci_endpoint_open ( struct usb_endpoint *ep ) {
00680         struct usb_device *usb = ep->usb;
00681         struct uhci_device *uhci = usb_get_hostdata ( usb );
00682         struct uhci_endpoint *endpoint;
00683         int rc;
00684 
00685         /* Allocate and initialise structure */
00686         endpoint = zalloc ( sizeof ( *endpoint ) );
00687         if ( ! endpoint ) {
00688                 rc = -ENOMEM;
00689                 goto err_alloc;
00690         }
00691         endpoint->uhci = uhci;
00692         endpoint->ep = ep;
00693         usb_endpoint_set_hostdata ( ep, endpoint );
00694 
00695         /* Initialise descriptor ring */
00696         if ( ( rc = uhci_ring_alloc ( &endpoint->ring ) ) != 0 )
00697                 goto err_ring_alloc;
00698         endpoint->ring.mtu = ep->mtu;
00699         endpoint->ring.flags = UHCI_FL_CERR_MAX;
00700         if ( usb->speed < USB_SPEED_FULL )
00701                 endpoint->ring.flags |= UHCI_FL_LS;
00702         endpoint->ring.control = ( UHCI_CONTROL_DEVICE ( usb->address ) |
00703                                    UHCI_CONTROL_ENDPOINT ( ep->address ) );
00704 
00705         /* Add to list of endpoints */
00706         list_add_tail ( &endpoint->list, &uhci->endpoints );
00707 
00708         /* Add to schedule */
00709         uhci_schedule_add ( endpoint );
00710 
00711         return 0;
00712 
00713         uhci_ring_free ( &endpoint->ring );
00714  err_ring_alloc:
00715         free ( endpoint );
00716  err_alloc:
00717         return rc;
00718 }
00719 
00720 /**
00721  * Close endpoint
00722  *
00723  * @v ep                USB endpoint
00724  */
00725 static void uhci_endpoint_close ( struct usb_endpoint *ep ) {
00726         struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
00727         struct io_buffer *iobuf;
00728 
00729         /* Remove from schedule */
00730         uhci_schedule_del ( endpoint );
00731 
00732         /* Cancel any incomplete transfers */
00733         while ( uhci_ring_fill ( &endpoint->ring ) ) {
00734                 iobuf = uhci_dequeue ( &endpoint->ring );
00735                 if ( iobuf )
00736                         usb_complete_err ( ep, iobuf, -ECANCELED );
00737         }
00738 
00739         /* Remove from list of endpoints */
00740         list_del ( &endpoint->list );
00741 
00742         /* Free descriptor ring */
00743         uhci_ring_free ( &endpoint->ring );
00744 
00745         /* Free endpoint */
00746         free ( endpoint );
00747 }
00748 
00749 /**
00750  * Reset endpoint
00751  *
00752  * @v ep                USB endpoint
00753  * @ret rc              Return status code
00754  */
00755 static int uhci_endpoint_reset ( struct usb_endpoint *ep ) {
00756         struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
00757         struct uhci_ring *ring = &endpoint->ring;
00758 
00759         /* Restart ring */
00760         uhci_restart ( ring, 0 );
00761 
00762         return 0;
00763 }
00764 
00765 /**
00766  * Update MTU
00767  *
00768  * @v ep                USB endpoint
00769  * @ret rc              Return status code
00770  */
00771 static int uhci_endpoint_mtu ( struct usb_endpoint *ep ) {
00772         struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
00773 
00774         /* Update endpoint MTU */
00775         endpoint->ring.mtu = ep->mtu;
00776 
00777         return 0;
00778 }
00779 
00780 /**
00781  * Enqueue message transfer
00782  *
00783  * @v ep                USB endpoint
00784  * @v iobuf             I/O buffer
00785  * @ret rc              Return status code
00786  */
00787 static int uhci_endpoint_message ( struct usb_endpoint *ep,
00788                                    struct io_buffer *iobuf ) {
00789         struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
00790         struct uhci_ring *ring = &endpoint->ring;
00791         struct usb_setup_packet *packet;
00792         unsigned int count;
00793         size_t len;
00794         int input;
00795         int rc;
00796 
00797         /* Calculate number of descriptors */
00798         assert ( iob_len ( iobuf ) >= sizeof ( *packet ) );
00799         len = ( iob_len ( iobuf ) - sizeof ( *packet ) );
00800         count = ( 1 /* setup stage */ +
00801                   ( ( len + ring->mtu - 1 ) / ring->mtu ) /* data stage */ +
00802                   1 /* status stage */ );
00803 
00804         /* Enqueue transfer */
00805         if ( ( rc = uhci_enqueue ( ring, iobuf, count ) ) != 0 )
00806                 return rc;
00807 
00808         /* Describe setup stage */
00809         packet = iobuf->data;
00810         ring->control &= ~UHCI_CONTROL_TOGGLE;
00811         uhci_describe ( ring, packet, sizeof ( *packet ), USB_PID_SETUP );
00812         iob_pull ( iobuf, sizeof ( *packet ) );
00813 
00814         /* Describe data stage, if applicable */
00815         assert ( ring->control & UHCI_CONTROL_TOGGLE );
00816         input = ( packet->request & cpu_to_le16 ( USB_DIR_IN ) );
00817         if ( len ) {
00818                 uhci_describe ( ring, iobuf->data, len,
00819                                 ( input ? USB_PID_IN : USB_PID_OUT ) );
00820         }
00821 
00822         /* Describe status stage */
00823         ring->control |= UHCI_CONTROL_TOGGLE;
00824         uhci_describe ( ring, NULL, 0,
00825                         ( ( len && input ) ? USB_PID_OUT : USB_PID_IN ) );
00826 
00827         /* Sanity check */
00828         assert ( ring->end->prod == count );
00829 
00830         return 0;
00831 }
00832 
00833 /**
00834  * Enqueue stream transfer
00835  *
00836  * @v ep                USB endpoint
00837  * @v iobuf             I/O buffer
00838  * @v zlp               Append a zero-length packet
00839  * @ret rc              Return status code
00840  */
00841 static int uhci_endpoint_stream ( struct usb_endpoint *ep,
00842                                   struct io_buffer *iobuf, int zlp ) {
00843         struct uhci_endpoint *endpoint = usb_endpoint_get_hostdata ( ep );
00844         struct uhci_ring *ring = &endpoint->ring;
00845         unsigned int count;
00846         size_t len;
00847         int input;
00848         int rc;
00849 
00850         /* Calculate number of descriptors */
00851         len = iob_len ( iobuf );
00852         count = ( ( ( len + ring->mtu - 1 ) / ring->mtu ) + ( zlp ? 1 : 0 ) );
00853 
00854         /* Enqueue transfer */
00855         if ( ( rc = uhci_enqueue ( ring, iobuf, count ) ) != 0 )
00856                 return rc;
00857 
00858         /* Describe data packet */
00859         input = ( ep->address & USB_DIR_IN );
00860         uhci_describe ( ring, iobuf->data, len,
00861                         ( input ? USB_PID_IN : USB_PID_OUT ) );
00862 
00863         /* Describe zero-length packet, if applicable */
00864         if ( zlp )
00865                 uhci_describe ( ring, NULL, 0, USB_PID_OUT );
00866 
00867         /* Sanity check */
00868         assert ( ring->end->prod == count );
00869 
00870         return 0;
00871 }
00872 
00873 /**
00874  * Check if transfer is a message transfer
00875  *
00876  * @v xfer              UHCI transfer
00877  * @ret is_message      Transfer is a message transfer
00878  */
00879 static inline int uhci_is_message ( struct uhci_transfer *xfer ) {
00880         struct uhci_transfer_descriptor *desc = &xfer->desc[0];
00881 
00882         return ( ( desc->control & cpu_to_le32 ( UHCI_CONTROL_PID_MASK ) ) ==
00883                  cpu_to_le32 ( UHCI_CONTROL_PID ( USB_PID_SETUP ) ) );
00884 }
00885 
00886 /**
00887  * Poll for completions
00888  *
00889  * @v endpoint          Endpoint
00890  */
00891 static void uhci_endpoint_poll ( struct uhci_endpoint *endpoint ) {
00892         struct uhci_ring *ring = &endpoint->ring;
00893         struct uhci_device *uhci = endpoint->uhci;
00894         struct usb_endpoint *ep = endpoint->ep;
00895         struct usb_device *usb = ep->usb;
00896         struct uhci_transfer *xfer;
00897         struct uhci_transfer_descriptor *desc;
00898         struct io_buffer *iobuf;
00899         unsigned int index;
00900         uint32_t link;
00901         uint32_t toggle;
00902         uint32_t control;
00903         uint16_t actual;
00904         size_t len;
00905 
00906         /* Consume all completed descriptors */
00907         while ( uhci_ring_fill ( ring ) ) {
00908 
00909                 /* Stop if we reach an uncompleted descriptor */
00910                 index = ( ring->cons % UHCI_RING_COUNT );
00911                 xfer = ring->xfer[index];
00912                 assert ( xfer != NULL );
00913                 assert ( xfer->cons < xfer->prod );
00914                 desc = &xfer->desc[xfer->cons];
00915                 rmb();
00916                 if ( desc->status & UHCI_STATUS_ACTIVE )
00917                         break;
00918                 control = le32_to_cpu ( desc->control );
00919                 actual = le16_to_cpu ( desc->actual );
00920 
00921                 /* Update data length, if applicable */
00922                 if ( UHCI_DATA_PACKET ( control ) )
00923                         xfer->len += UHCI_ACTUAL_LEN ( actual );
00924 
00925                 /* If we have encountered an error, then deactivate
00926                  * the queue head (to prevent further hardware
00927                  * accesses to this transfer), consume the transfer,
00928                  * and report the error to the USB core.
00929                  */
00930                 if ( desc->status & UHCI_STATUS_STALLED ) {
00931                         DBGC ( uhci, "UHCI %s %s completion %d.%d failed "
00932                                "(status %02x)\n", usb->name,
00933                                usb_endpoint_name ( ep ), index,
00934                                xfer->cons, desc->status );
00935                         link = UHCI_LINK_TERMINATE;
00936                         ring->head->current = cpu_to_le32 ( link );
00937                         wmb();
00938                         iobuf = uhci_dequeue ( ring );
00939                         usb_complete_err ( ep, iobuf, -EIO );
00940                         break;
00941                 }
00942 
00943                 /* Consume this descriptor */
00944                 xfer->cons++;
00945 
00946                 /* Check for short packets */
00947                 if ( UHCI_SHORT_PACKET ( control, actual ) ) {
00948 
00949                         /* Sanity checks */
00950                         assert ( desc->flags & UHCI_FL_SPD );
00951                         link = virt_to_phys ( desc );
00952                         assert ( ( le32_to_cpu ( ring->head->current ) &
00953                                    ~( UHCI_ALIGN - 1 ) ) == link );
00954 
00955                         /* If this is a message transfer, then restart
00956                          * at the status stage.
00957                          */
00958                         if ( uhci_is_message ( xfer ) ) {
00959                                 xfer->cons = ( xfer->prod - 1 );
00960                                 link = virt_to_phys ( &xfer->desc[xfer->cons] );
00961                                 ring->head->current = cpu_to_le32 ( link );
00962                                 break;
00963                         }
00964 
00965                         /* Otherwise, this is a stream transfer.
00966                          * First, prevent further hardware access to
00967                          * this transfer.
00968                          */
00969                         link = UHCI_LINK_TERMINATE;
00970                         ring->head->current = cpu_to_le32 ( link );
00971                         wmb();
00972 
00973                         /* Determine expected data toggle for next descriptor */
00974                         toggle = ( ( control ^ UHCI_CONTROL_TOGGLE ) &
00975                                    UHCI_CONTROL_TOGGLE );
00976 
00977                         /* Consume this transfer */
00978                         len = xfer->len;
00979                         iobuf = uhci_dequeue ( ring );
00980 
00981                         /* Update packet length */
00982                         assert ( len <= iob_len ( iobuf ) );
00983                         iob_unput ( iobuf, ( iob_len ( iobuf ) - len ) );
00984 
00985                         /* Restart ring */
00986                         uhci_restart ( ring, toggle );
00987 
00988                 } else if ( xfer->cons == xfer->prod ) {
00989 
00990                         /* Completed a transfer: consume it */
00991                         len = xfer->len;
00992                         iobuf = uhci_dequeue ( ring );
00993                         assert ( len == iob_len ( iobuf ) );
00994 
00995                 } else {
00996 
00997                         /* Not a short packet and not yet complete:
00998                          * continue processing.
00999                          */
01000                         continue;
01001                 }
01002 
01003                 /* Report completion to USB core */
01004                 usb_complete ( ep, iobuf );
01005         }
01006 }
01007 
01008 /******************************************************************************
01009  *
01010  * Device operations
01011  *
01012  ******************************************************************************
01013  */
01014 
01015 /**
01016  * Open device
01017  *
01018  * @v usb               USB device
01019  * @ret rc              Return status code
01020  */
01021 static int uhci_device_open ( struct usb_device *usb ) {
01022         struct uhci_device *uhci = usb_bus_get_hostdata ( usb->port->hub->bus );
01023 
01024         usb_set_hostdata ( usb, uhci );
01025         return 0;
01026 }
01027 
01028 /**
01029  * Close device
01030  *
01031  * @v usb               USB device
01032  */
01033 static void uhci_device_close ( struct usb_device *usb ) {
01034         struct uhci_device *uhci = usb_get_hostdata ( usb );
01035         struct usb_bus *bus = uhci->bus;
01036 
01037         /* Free device address, if assigned */
01038         if ( usb->address )
01039                 usb_free_address ( bus, usb->address );
01040 }
01041 
01042 /**
01043  * Assign device address
01044  *
01045  * @v usb               USB device
01046  * @ret rc              Return status code
01047  */
01048 static int uhci_device_address ( struct usb_device *usb ) {
01049         struct uhci_device *uhci = usb_get_hostdata ( usb );
01050         struct usb_bus *bus = uhci->bus;
01051         struct usb_endpoint *ep0 = usb_endpoint ( usb, USB_EP0_ADDRESS );
01052         struct uhci_endpoint *endpoint0 = usb_endpoint_get_hostdata ( ep0 );
01053         int address;
01054         int rc;
01055 
01056         /* Sanity checks */
01057         assert ( usb->address == 0 );
01058         assert ( ep0 != NULL );
01059 
01060         /* Allocate device address */
01061         address = usb_alloc_address ( bus );
01062         if ( address < 0 ) {
01063                 rc = address;
01064                 DBGC ( uhci, "UHCI %s could not allocate address: %s\n",
01065                        usb->name, strerror ( rc ) );
01066                 goto err_alloc_address;
01067         }
01068 
01069         /* Set address */
01070         if ( ( rc = usb_set_address ( usb, address ) ) != 0 )
01071                 goto err_set_address;
01072 
01073         /* Update device address */
01074         usb->address = address;
01075         endpoint0->ring.control |= UHCI_CONTROL_DEVICE ( address );
01076 
01077         return 0;
01078 
01079  err_set_address:
01080         usb_free_address ( bus, address );
01081  err_alloc_address:
01082         return rc;
01083 }
01084 
01085 /******************************************************************************
01086  *
01087  * Hub operations
01088  *
01089  ******************************************************************************
01090  */
01091 
01092 /**
01093  * Open hub
01094  *
01095  * @v hub               USB hub
01096  * @ret rc              Return status code
01097  */
01098 static int uhci_hub_open ( struct usb_hub *hub __unused ) {
01099 
01100         /* Nothing to do */
01101         return 0;
01102 }
01103 
01104 /**
01105  * Close hub
01106  *
01107  * @v hub               USB hub
01108  */
01109 static void uhci_hub_close ( struct usb_hub *hub __unused ) {
01110 
01111         /* Nothing to do */
01112 }
01113 
01114 /******************************************************************************
01115  *
01116  * Root hub operations
01117  *
01118  ******************************************************************************
01119  */
01120 
01121 /**
01122  * Open root hub
01123  *
01124  * @v hub               USB hub
01125  * @ret rc              Return status code
01126  */
01127 static int uhci_root_open ( struct usb_hub *hub ) {
01128         struct usb_bus *bus = hub->bus;
01129         struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
01130 
01131         /* Record hub driver private data */
01132         usb_hub_set_drvdata ( hub, uhci );
01133 
01134         return 0;
01135 }
01136 
01137 /**
01138  * Close root hub
01139  *
01140  * @v hub               USB hub
01141  */
01142 static void uhci_root_close ( struct usb_hub *hub ) {
01143 
01144         /* Clear hub driver private data */
01145         usb_hub_set_drvdata ( hub, NULL );
01146 }
01147 
01148 /**
01149  * Enable port
01150  *
01151  * @v hub               USB hub
01152  * @v port              USB port
01153  * @ret rc              Return status code
01154  */
01155 static int uhci_root_enable ( struct usb_hub *hub, struct usb_port *port ) {
01156         struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
01157         uint16_t portsc;
01158         unsigned int i;
01159 
01160         /* Reset port */
01161         portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
01162         portsc |= UHCI_PORTSC_PR;
01163         outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
01164         mdelay ( USB_RESET_DELAY_MS );
01165         portsc &= ~UHCI_PORTSC_PR;
01166         outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
01167         mdelay ( USB_RESET_RECOVER_DELAY_MS );
01168 
01169         /* Enable port */
01170         portsc |= UHCI_PORTSC_PED;
01171         outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
01172         mdelay ( USB_RESET_RECOVER_DELAY_MS );
01173 
01174         /* Wait for port to become enabled */
01175         for ( i = 0 ; i < UHCI_PORT_ENABLE_MAX_WAIT_MS ; i++ ) {
01176 
01177                 /* Check port status */
01178                 portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
01179                 if ( portsc & UHCI_PORTSC_PED )
01180                         return 0;
01181 
01182                 /* Delay */
01183                 mdelay ( 1 );
01184         }
01185 
01186         DBGC ( uhci, "UHCI %s-%d timed out waiting for port to enable "
01187                "(status %04x)\n",  uhci->name, port->address, portsc );
01188         return -ETIMEDOUT;
01189 }
01190 
01191 /**
01192  * Disable port
01193  *
01194  * @v hub               USB hub
01195  * @v port              USB port
01196  * @ret rc              Return status code
01197  */
01198 static int uhci_root_disable ( struct usb_hub *hub, struct usb_port *port ) {
01199         struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
01200         uint16_t portsc;
01201 
01202         /* Disable port */
01203         portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
01204         portsc &= ~UHCI_PORTSC_PED;
01205         outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
01206 
01207         return 0;
01208 }
01209 
01210 /**
01211  * Update root hub port speed
01212  *
01213  * @v hub               USB hub
01214  * @v port              USB port
01215  * @ret rc              Return status code
01216  */
01217 static int uhci_root_speed ( struct usb_hub *hub, struct usb_port *port ) {
01218         struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
01219         struct pci_device pci;
01220         uint16_t portsc;
01221         unsigned int speed;
01222 
01223         /* Read port status */
01224         portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
01225         if ( ! ( portsc & UHCI_PORTSC_CCS ) ) {
01226                 /* Port not connected */
01227                 speed = USB_SPEED_NONE;
01228         } else if ( uhci->companion &&
01229                     ! find_usb_bus_by_location ( BUS_TYPE_PCI,
01230                                                  uhci->companion ) ) {
01231                 /* Defer connection detection until companion
01232                  * controller has been enumerated.
01233                  */
01234                 pci_init ( &pci, uhci->companion );
01235                 DBGC ( uhci, "UHCI %s-%d deferring for companion " PCI_FMT "\n",
01236                        uhci->name, port->address, PCI_ARGS ( &pci ) );
01237                 speed = USB_SPEED_NONE;
01238         } else if ( portsc & UHCI_PORTSC_LS ) {
01239                 /* Low-speed device */
01240                 speed = USB_SPEED_LOW;
01241         } else {
01242                 /* Full-speed device */
01243                 speed = USB_SPEED_FULL;
01244         }
01245         port->speed = speed;
01246 
01247         /* Record disconnections and clear changes */
01248         port->disconnected |= ( portsc & UHCI_PORTSC_CSC );
01249         outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
01250 
01251         return 0;
01252 }
01253 
01254 /**
01255  * Clear transaction translator buffer
01256  *
01257  * @v hub               USB hub
01258  * @v port              USB port
01259  * @v ep                USB endpoint
01260  * @ret rc              Return status code
01261  */
01262 static int uhci_root_clear_tt ( struct usb_hub *hub, struct usb_port *port,
01263                                 struct usb_endpoint *ep ) {
01264         struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
01265 
01266         /* Should never be called; this is a root hub */
01267         DBGC ( uhci, "UHCI %s-%d nonsensical CLEAR_TT for %s %s\n", uhci->name,
01268                port->address, ep->usb->name, usb_endpoint_name ( ep ) );
01269 
01270         return -ENOTSUP;
01271 }
01272 
01273 /**
01274  * Poll for port status changes
01275  *
01276  * @v hub               USB hub
01277  * @v port              USB port
01278  */
01279 static void uhci_root_poll ( struct usb_hub *hub, struct usb_port *port ) {
01280         struct uhci_device *uhci = usb_hub_get_drvdata ( hub );
01281         uint16_t portsc;
01282         uint16_t change;
01283 
01284         /* Do nothing unless something has changed */
01285         portsc = inw ( uhci->regs + UHCI_PORTSC ( port->address ) );
01286         change = ( portsc & UHCI_PORTSC_CHANGE );
01287         if ( ! change )
01288                 return;
01289 
01290         /* Record disconnections and clear changes */
01291         port->disconnected |= ( portsc & UHCI_PORTSC_CSC );
01292         outw ( portsc, uhci->regs + UHCI_PORTSC ( port->address ) );
01293 
01294         /* Report port status change */
01295         usb_port_changed ( port );
01296 }
01297 
01298 /******************************************************************************
01299  *
01300  * Bus operations
01301  *
01302  ******************************************************************************
01303  */
01304 
01305 /**
01306  * Open USB bus
01307  *
01308  * @v bus               USB bus
01309  * @ret rc              Return status code
01310  */
01311 static int uhci_bus_open ( struct usb_bus *bus ) {
01312         struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
01313         int rc;
01314 
01315         /* Sanity checks */
01316         assert ( list_empty ( &uhci->async ) );
01317         assert ( list_empty ( &uhci->periodic ) );
01318 
01319         /* Allocate and initialise asynchronous queue head */
01320         uhci->head = malloc_dma ( sizeof ( *uhci->head ), UHCI_ALIGN );
01321         if ( ! uhci->head ) {
01322                 rc = -ENOMEM;
01323                 goto err_alloc_head;
01324         }
01325         if ( ( rc = uhci_reachable ( uhci->head, sizeof ( *uhci->head ) ) ) !=0)
01326                 goto err_unreachable_head;
01327         memset ( uhci->head, 0, sizeof ( *uhci->head ) );
01328         uhci->head->current = cpu_to_le32 ( UHCI_LINK_TERMINATE );
01329         uhci_async_schedule ( uhci );
01330 
01331         /* Allocate periodic frame list */
01332         uhci->frame = malloc_dma ( sizeof ( *uhci->frame ),
01333                                    sizeof ( *uhci->frame ) );
01334         if ( ! uhci->frame ) {
01335                 rc = -ENOMEM;
01336                 goto err_alloc_frame;
01337         }
01338         if ( ( rc = uhci_reachable ( uhci->frame,
01339                                      sizeof ( *uhci->frame ) ) ) != 0 )
01340                 goto err_unreachable_frame;
01341         uhci_periodic_schedule ( uhci );
01342         outl ( virt_to_phys ( uhci->frame ), uhci->regs + UHCI_FLBASEADD );
01343 
01344         /* Start controller */
01345         uhci_run ( uhci );
01346 
01347         return 0;
01348 
01349         uhci_stop ( uhci );
01350  err_unreachable_frame:
01351         free_dma ( uhci->frame, sizeof ( *uhci->frame ) );
01352  err_alloc_frame:
01353  err_unreachable_head:
01354         free_dma ( uhci->head, sizeof ( *uhci->head ) );
01355  err_alloc_head:
01356         return rc;
01357 }
01358 
01359 /**
01360  * Close USB bus
01361  *
01362  * @v bus               USB bus
01363  */
01364 static void uhci_bus_close ( struct usb_bus *bus ) {
01365         struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
01366 
01367         /* Sanity checks */
01368         assert ( list_empty ( &uhci->async ) );
01369         assert ( list_empty ( &uhci->periodic ) );
01370 
01371         /* Stop controller */
01372         uhci_stop ( uhci );
01373 
01374         /* Free periodic frame list */
01375         free_dma ( uhci->frame, sizeof ( *uhci->frame ) );
01376 
01377         /* Free asynchronous schedule */
01378         free_dma ( uhci->head, sizeof ( *uhci->head ) );
01379 }
01380 
01381 /**
01382  * Poll USB bus
01383  *
01384  * @v bus               USB bus
01385  */
01386 static void uhci_bus_poll ( struct usb_bus *bus ) {
01387         struct uhci_device *uhci = usb_bus_get_hostdata ( bus );
01388         struct usb_hub *hub = bus->hub;
01389         struct uhci_endpoint *endpoint;
01390         unsigned int i;
01391 
01392         /* UHCI defers interrupts (including short packet detection)
01393          * until the end of the frame.  This can result in bulk IN
01394          * endpoints remaining halted for much of the time, waiting
01395          * for software action to reset the data toggles.  We
01396          * therefore ignore USBSTS and unconditionally poll all
01397          * endpoints for completed transfer descriptors.
01398          *
01399          * As with EHCI, we trust that completion handlers are minimal
01400          * and will not do anything that could plausibly affect the
01401          * endpoint list itself.
01402          */
01403         list_for_each_entry ( endpoint, &uhci->endpoints, list )
01404                 uhci_endpoint_poll ( endpoint );
01405 
01406         /* UHCI provides no single bit to indicate that a port status
01407          * change has occurred.  We therefore unconditionally iterate
01408          * over all ports looking for status changes.
01409          */
01410         for ( i = 1 ; i <= UHCI_PORTS ; i++ )
01411                 uhci_root_poll ( hub, usb_port ( hub, i ) );
01412 }
01413 
01414 /******************************************************************************
01415  *
01416  * PCI interface
01417  *
01418  ******************************************************************************
01419  */
01420 
01421 /** USB host controller operations */
01422 static struct usb_host_operations uhci_operations = {
01423         .endpoint = {
01424                 .open = uhci_endpoint_open,
01425                 .close = uhci_endpoint_close,
01426                 .reset = uhci_endpoint_reset,
01427                 .mtu = uhci_endpoint_mtu,
01428                 .message = uhci_endpoint_message,
01429                 .stream = uhci_endpoint_stream,
01430         },
01431         .device = {
01432                 .open = uhci_device_open,
01433                 .close = uhci_device_close,
01434                 .address = uhci_device_address,
01435         },
01436         .bus = {
01437                 .open = uhci_bus_open,
01438                 .close = uhci_bus_close,
01439                 .poll = uhci_bus_poll,
01440         },
01441         .hub = {
01442                 .open = uhci_hub_open,
01443                 .close = uhci_hub_close,
01444         },
01445         .root = {
01446                 .open = uhci_root_open,
01447                 .close = uhci_root_close,
01448                 .enable = uhci_root_enable,
01449                 .disable = uhci_root_disable,
01450                 .speed = uhci_root_speed,
01451                 .clear_tt = uhci_root_clear_tt,
01452         },
01453 };
01454 
01455 /**
01456  * Locate EHCI companion controller (when no EHCI support is present)
01457  *
01458  * @v pci               PCI device
01459  * @ret busdevfn        EHCI companion controller bus:dev.fn (if any)
01460  */
01461 __weak unsigned int ehci_companion ( struct pci_device *pci __unused ) {
01462         return 0;
01463 }
01464 
01465 /**
01466  * Probe PCI device
01467  *
01468  * @v pci               PCI device
01469  * @ret rc              Return status code
01470  */
01471 static int uhci_probe ( struct pci_device *pci ) {
01472         struct uhci_device *uhci;
01473         struct usb_port *port;
01474         unsigned int i;
01475         int rc;
01476 
01477         /* Allocate and initialise structure */
01478         uhci = zalloc ( sizeof ( *uhci ) );
01479         if ( ! uhci ) {
01480                 rc = -ENOMEM;
01481                 goto err_alloc;
01482         }
01483         uhci->name = pci->dev.name;
01484         INIT_LIST_HEAD ( &uhci->endpoints );
01485         INIT_LIST_HEAD ( &uhci->async );
01486         INIT_LIST_HEAD ( &uhci->periodic );
01487 
01488         /* Fix up PCI device */
01489         adjust_pci_device ( pci );
01490 
01491         /* Identify EHCI companion controller, if any */
01492         uhci->companion = ehci_companion ( pci );
01493 
01494         /* Claim ownership from BIOS.  (There is no release mechanism
01495          * for UHCI.)
01496          */
01497         pci_write_config_word ( pci, UHCI_USBLEGSUP, UHCI_USBLEGSUP_DEFAULT );
01498 
01499         /* Map registers */
01500         uhci->regs = pci->ioaddr;
01501         if ( ! uhci->regs ) {
01502                 rc = -ENODEV;
01503                 goto err_ioremap;
01504         }
01505 
01506         /* Reset device */
01507         if ( ( rc = uhci_reset ( uhci ) ) != 0 )
01508                 goto err_reset;
01509 
01510         /* Allocate USB bus */
01511         uhci->bus = alloc_usb_bus ( &pci->dev, UHCI_PORTS, UHCI_MTU,
01512                                     &uhci_operations );
01513         if ( ! uhci->bus ) {
01514                 rc = -ENOMEM;
01515                 goto err_alloc_bus;
01516         }
01517         usb_bus_set_hostdata ( uhci->bus, uhci );
01518         usb_hub_set_drvdata ( uhci->bus->hub, uhci );
01519 
01520         /* Set port protocols */
01521         for ( i = 1 ; i <= UHCI_PORTS ; i++ ) {
01522                 port = usb_port ( uhci->bus->hub, i );
01523                 port->protocol = USB_PROTO_2_0;
01524         }
01525 
01526         /* Register USB bus */
01527         if ( ( rc = register_usb_bus ( uhci->bus ) ) != 0 )
01528                 goto err_register;
01529 
01530         pci_set_drvdata ( pci, uhci );
01531         return 0;
01532 
01533         unregister_usb_bus ( uhci->bus );
01534  err_register:
01535         free_usb_bus ( uhci->bus );
01536  err_alloc_bus:
01537         uhci_reset ( uhci );
01538  err_reset:
01539  err_ioremap:
01540         free ( uhci );
01541  err_alloc:
01542         return rc;
01543 }
01544 
01545 /**
01546  * Remove PCI device
01547  *
01548  * @v pci               PCI device
01549  */
01550 static void uhci_remove ( struct pci_device *pci ) {
01551         struct uhci_device *uhci = pci_get_drvdata ( pci );
01552         struct usb_bus *bus = uhci->bus;
01553 
01554         unregister_usb_bus ( bus );
01555         assert ( list_empty ( &uhci->async ) );
01556         assert ( list_empty ( &uhci->periodic ) );
01557         free_usb_bus ( bus );
01558         uhci_reset ( uhci );
01559         free ( uhci );
01560 }
01561 
01562 /** UHCI PCI device IDs */
01563 static struct pci_device_id uhci_ids[] = {
01564         PCI_ROM ( 0xffff, 0xffff, "uhci", "UHCI", 0 ),
01565 };
01566 
01567 /** UHCI PCI driver */
01568 struct pci_driver uhci_driver __pci_driver = {
01569         .ids = uhci_ids,
01570         .id_count = ( sizeof ( uhci_ids ) / sizeof ( uhci_ids[0] ) ),
01571         .class = PCI_CLASS_ID ( PCI_CLASS_SERIAL, PCI_CLASS_SERIAL_USB,
01572                                 PCI_CLASS_SERIAL_USB_UHCI ),
01573         .probe = uhci_probe,
01574         .remove = uhci_remove,
01575 };