Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 4 | * Copyright (C) 2008-2010 coresystems GmbH |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 30 | //#define USB_DEBUG |
| 31 | |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 32 | #include <arch/virtual.h> |
Jordan Crouse | 29061a5 | 2008-09-11 17:29:00 +0000 | [diff] [blame] | 33 | #include <usb/usb.h> |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 34 | #include "uhci.h" |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 35 | #include "uhci_private.h" |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 36 | |
| 37 | static void uhci_start (hci_t *controller); |
| 38 | static void uhci_stop (hci_t *controller); |
| 39 | static void uhci_reset (hci_t *controller); |
| 40 | static void uhci_shutdown (hci_t *controller); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 41 | static int uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize); |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 42 | static int uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 43 | int dalen, u8 *data); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 44 | static void* uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming); |
| 45 | static void uhci_destroy_intr_queue (endpoint_t *ep, void *queue); |
| 46 | static u8* uhci_poll_intr_queue (void *queue); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 47 | |
| 48 | #if 0 |
| 49 | /* dump uhci */ |
| 50 | static void |
| 51 | uhci_dump (hci_t *controller) |
| 52 | { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame^] | 53 | usb_debug ("dump:\nUSBCMD: %x\n", uhci_reg_read16 (controller, USBCMD)); |
| 54 | usb_debug ("USBSTS: %x\n", uhci_reg_read16 (controller, USBSTS)); |
| 55 | usb_debug ("USBINTR: %x\n", uhci_reg_read16 (controller, USBINTR)); |
| 56 | usb_debug ("FRNUM: %x\n", uhci_reg_read16 (controller, FRNUM)); |
| 57 | usb_debug ("FLBASEADD: %x\n", uhci_reg_read32 (controller, FLBASEADD)); |
| 58 | usb_debug ("SOFMOD: %x\n", uhci_reg_read8 (controller, SOFMOD)); |
| 59 | usb_debug ("PORTSC1: %x\n", uhci_reg_read16 (controller, PORTSC1)); |
| 60 | usb_debug ("PORTSC2: %x\n", uhci_reg_read16 (controller, PORTSC2)); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 61 | } |
| 62 | #endif |
| 63 | |
| 64 | static void |
| 65 | td_dump (td_t *td) |
| 66 | { |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 67 | char td_value[3]; |
Patrick Georgi | 7f96583 | 2011-04-21 18:57:16 +0200 | [diff] [blame] | 68 | const char *td_type; |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 69 | switch (td->token & TD_PID_MASK) { |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 70 | case UHCI_SETUP: |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 71 | td_type="SETUP"; |
| 72 | break; |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 73 | case UHCI_IN: |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 74 | td_type="IN"; |
| 75 | break; |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 76 | case UHCI_OUT: |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 77 | td_type="OUT"; |
| 78 | break; |
| 79 | default: |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 80 | sprintf(td_value, "%x", td->token & TD_PID_MASK); |
Stefan Reinauer | d233f36 | 2009-04-30 16:46:12 +0000 | [diff] [blame] | 81 | td_type=td_value; |
| 82 | } |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame^] | 83 | usb_debug ("%s packet (at %lx) to %x.%x failed\n", td_type, |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 84 | virt_to_phys (td), (td->token & TD_DEVADDR_MASK) >> TD_DEVADDR_SHIFT, |
| 85 | (td->token & TD_EP_MASK) >> TD_EP_SHIFT); |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame^] | 86 | usb_debug ("td (counter at %x) returns: ", td->ctrlsts >> TD_COUNTER_SHIFT); |
| 87 | usb_debug (" bitstuff err: %x, ", !!(td->ctrlsts & TD_STATUS_BITSTUFF_ERR)); |
| 88 | usb_debug (" CRC err: %x, ", !!(td->ctrlsts & TD_STATUS_CRC_ERR)); |
| 89 | usb_debug (" NAK rcvd: %x, ", !!(td->ctrlsts & TD_STATUS_NAK_RCVD)); |
| 90 | usb_debug (" Babble: %x, ", !!(td->ctrlsts & TD_STATUS_BABBLE)); |
| 91 | usb_debug (" Data Buffer err: %x, ", !!(td->ctrlsts & TD_STATUS_DATABUF_ERR)); |
| 92 | usb_debug (" Stalled: %x, ", !!(td->ctrlsts & TD_STATUS_STALLED)); |
| 93 | usb_debug (" Active: %x\n", !!(td->ctrlsts & TD_STATUS_ACTIVE)); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 94 | if (td->ctrlsts & TD_STATUS_BABBLE) |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame^] | 95 | usb_debug (" Babble because of %s\n", |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 96 | (td->ctrlsts & TD_STATUS_BITSTUFF_ERR) ? "host" : "device"); |
| 97 | if (td->ctrlsts & TD_STATUS_ACTIVE) |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame^] | 98 | usb_debug (" still active - timeout?\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static void |
| 102 | uhci_reset (hci_t *controller) |
| 103 | { |
| 104 | /* reset */ |
Nico Huber | bb1c42b | 2012-05-21 14:23:03 +0200 | [diff] [blame] | 105 | uhci_reg_write16 (controller, USBCMD, 4); /* Global Reset */ |
| 106 | mdelay (50); /* uhci spec 2.1.1: at least 10ms */ |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 107 | uhci_reg_write16 (controller, USBCMD, 0); |
| 108 | mdelay (10); |
Nico Huber | bb1c42b | 2012-05-21 14:23:03 +0200 | [diff] [blame] | 109 | uhci_reg_write16 (controller, USBCMD, 2); /* Host Controller Reset */ |
| 110 | /* wait for controller to finish reset */ |
| 111 | /* TOTEST: how long to wait? 100ms for now */ |
| 112 | int timeout = 200; /* time out after 200 * 500us == 100ms */ |
| 113 | while (((uhci_reg_read16 (controller, USBCMD) & 2) != 0) && timeout--) |
| 114 | udelay (500); |
| 115 | if (timeout < 0) |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame^] | 116 | usb_debug ("Warning: uhci: host controller reset timed out.\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 117 | |
| 118 | uhci_reg_write32 (controller, FLBASEADD, |
| 119 | (u32) virt_to_phys (UHCI_INST (controller)-> |
| 120 | framelistptr)); |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame^] | 121 | //usb_debug ("framelist at %p\n",UHCI_INST(controller)->framelistptr); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 122 | |
| 123 | /* disable irqs */ |
| 124 | uhci_reg_write16 (controller, USBINTR, 0); |
| 125 | |
| 126 | /* reset framelist index */ |
| 127 | uhci_reg_write16 (controller, FRNUM, 0); |
| 128 | |
Patrick Georgi | f42fdab | 2011-11-18 14:44:16 +0100 | [diff] [blame] | 129 | uhci_reg_write16(controller, USBCMD, |
| 130 | uhci_reg_read16(controller, USBCMD) | 0xc0); // max packets, configure flag |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 131 | |
| 132 | uhci_start (controller); |
| 133 | } |
| 134 | |
| 135 | hci_t * |
| 136 | uhci_init (pcidev_t addr) |
| 137 | { |
| 138 | int i; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 139 | u16 reg16; |
| 140 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 141 | hci_t *controller = new_controller (); |
| 142 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 143 | if (!controller) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 144 | fatal("Could not create USB controller instance.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 145 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 146 | controller->instance = malloc (sizeof (uhci_t)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 147 | if(!controller->instance) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 148 | fatal("Not enough memory creating USB controller instance.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 149 | |
Anton Kochkov | 1c36ead | 2012-06-28 08:30:15 +0400 | [diff] [blame] | 150 | controller->type = UHCI; |
| 151 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 152 | controller->start = uhci_start; |
| 153 | controller->stop = uhci_stop; |
| 154 | controller->reset = uhci_reset; |
| 155 | controller->shutdown = uhci_shutdown; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 156 | controller->bulk = uhci_bulk; |
| 157 | controller->control = uhci_control; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 158 | controller->create_intr_queue = uhci_create_intr_queue; |
| 159 | controller->destroy_intr_queue = uhci_destroy_intr_queue; |
| 160 | controller->poll_intr_queue = uhci_poll_intr_queue; |
Stefan Reinauer | 219cece | 2009-07-18 15:17:40 +0000 | [diff] [blame] | 161 | for (i = 0; i < 128; i++) { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 162 | controller->devices[i] = 0; |
| 163 | } |
| 164 | init_device_entry (controller, 0); |
| 165 | UHCI_INST (controller)->roothub = controller->devices[0]; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 166 | |
| 167 | controller->bus_address = addr; |
| 168 | controller->reg_base = pci_read_config32 (controller->bus_address, 0x20) & ~1; /* ~1 clears the register type indicator that is set to 1 for IO space */ |
| 169 | |
| 170 | /* kill legacy support handler */ |
| 171 | uhci_stop (controller); |
| 172 | mdelay (1); |
| 173 | uhci_reg_write16 (controller, USBSTS, 0x3f); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 174 | reg16 = pci_read_config16(controller->bus_address, 0xc0); |
| 175 | reg16 &= 0xdf80; |
| 176 | pci_write_config16 (controller->bus_address, 0xc0, reg16); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 177 | |
Anton Kochkov | f6c8080 | 2012-09-20 10:24:01 +0200 | [diff] [blame] | 178 | UHCI_INST (controller)->framelistptr = memalign (0x1000, 1024 * sizeof (flistp_t)); /* 4kb aligned to 4kb */ |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 179 | if (! UHCI_INST (controller)->framelistptr) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 180 | fatal("Not enough memory for USB frame list pointer.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 181 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 182 | memset (UHCI_INST (controller)->framelistptr, 0, |
| 183 | 1024 * sizeof (flistp_t)); |
| 184 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 185 | /* According to the *BSD UHCI code, this one is needed on some |
| 186 | PIIX chips, because otherwise they misbehave. It must be |
| 187 | added to the last chain. |
| 188 | |
| 189 | FIXME: this leaks, if the driver should ever be reinited |
| 190 | for some reason. Not a problem now. |
| 191 | */ |
| 192 | td_t *antiberserk = memalign(16, sizeof(td_t)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 193 | if (!antiberserk) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 194 | fatal("Not enough memory for chipset workaround.\n"); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 195 | memset(antiberserk, 0, sizeof(td_t)); |
| 196 | |
| 197 | UHCI_INST (controller)->qh_prei = memalign (16, sizeof (qh_t)); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 198 | UHCI_INST (controller)->qh_intr = memalign (16, sizeof (qh_t)); |
| 199 | UHCI_INST (controller)->qh_data = memalign (16, sizeof (qh_t)); |
| 200 | UHCI_INST (controller)->qh_last = memalign (16, sizeof (qh_t)); |
| 201 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 202 | if (! UHCI_INST (controller)->qh_prei || |
| 203 | ! UHCI_INST (controller)->qh_intr || |
| 204 | ! UHCI_INST (controller)->qh_data || |
| 205 | ! UHCI_INST (controller)->qh_last) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 206 | fatal("Not enough memory for USB controller queues.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 207 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 208 | UHCI_INST (controller)->qh_prei->headlinkptr = |
| 209 | virt_to_phys (UHCI_INST (controller)->qh_intr) | FLISTP_QH; |
| 210 | UHCI_INST (controller)->qh_prei->elementlinkptr = 0 | FLISTP_TERMINATE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 211 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 212 | UHCI_INST (controller)->qh_intr->headlinkptr = |
| 213 | virt_to_phys (UHCI_INST (controller)->qh_data) | FLISTP_QH; |
| 214 | UHCI_INST (controller)->qh_intr->elementlinkptr = 0 | FLISTP_TERMINATE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 215 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 216 | UHCI_INST (controller)->qh_data->headlinkptr = |
| 217 | virt_to_phys (UHCI_INST (controller)->qh_last) | FLISTP_QH; |
| 218 | UHCI_INST (controller)->qh_data->elementlinkptr = 0 | FLISTP_TERMINATE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 219 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 220 | UHCI_INST (controller)->qh_last->headlinkptr = virt_to_phys (UHCI_INST (controller)->qh_data) | FLISTP_TERMINATE; |
| 221 | UHCI_INST (controller)->qh_last->elementlinkptr = virt_to_phys (antiberserk) | FLISTP_TERMINATE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 222 | |
| 223 | for (i = 0; i < 1024; i++) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 224 | UHCI_INST (controller)->framelistptr[i] = |
| 225 | virt_to_phys (UHCI_INST (controller)->qh_prei) | FLISTP_QH; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 226 | } |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 227 | controller->devices[0]->controller = controller; |
| 228 | controller->devices[0]->init = uhci_rh_init; |
| 229 | controller->devices[0]->init (controller->devices[0]); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 230 | uhci_reset (controller); |
| 231 | return controller; |
| 232 | } |
| 233 | |
| 234 | static void |
| 235 | uhci_shutdown (hci_t *controller) |
| 236 | { |
| 237 | if (controller == 0) |
| 238 | return; |
| 239 | detach_controller (controller); |
| 240 | UHCI_INST (controller)->roothub->destroy (UHCI_INST (controller)-> |
| 241 | roothub); |
Patrick Georgi | f42fdab | 2011-11-18 14:44:16 +0100 | [diff] [blame] | 242 | uhci_reg_write16(controller, USBCMD, |
| 243 | uhci_reg_read16(controller, USBCMD) & 0); // stop work |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 244 | free (UHCI_INST (controller)->framelistptr); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 245 | free (UHCI_INST (controller)->qh_prei); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 246 | free (UHCI_INST (controller)->qh_intr); |
| 247 | free (UHCI_INST (controller)->qh_data); |
| 248 | free (UHCI_INST (controller)->qh_last); |
| 249 | free (UHCI_INST (controller)); |
| 250 | free (controller); |
| 251 | } |
| 252 | |
| 253 | static void |
| 254 | uhci_start (hci_t *controller) |
| 255 | { |
Patrick Georgi | f42fdab | 2011-11-18 14:44:16 +0100 | [diff] [blame] | 256 | uhci_reg_write16(controller, USBCMD, |
| 257 | uhci_reg_read16(controller, USBCMD) | 1); // start work on schedule |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static void |
| 261 | uhci_stop (hci_t *controller) |
| 262 | { |
Patrick Georgi | f42fdab | 2011-11-18 14:44:16 +0100 | [diff] [blame] | 263 | uhci_reg_write16(controller, USBCMD, |
| 264 | uhci_reg_read16(controller, USBCMD) & ~1); // stop work on schedule |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | #define GET_TD(x) ((void*)(((unsigned int)(x))&~0xf)) |
| 268 | |
| 269 | static td_t * |
| 270 | wait_for_completed_qh (hci_t *controller, qh_t *qh) |
| 271 | { |
Mathias Krause | 7b7b566 | 2012-05-29 16:19:19 +0200 | [diff] [blame] | 272 | int timeout = 1000; /* max 30 ms. */ |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 273 | void *current = GET_TD (qh->elementlinkptr); |
| 274 | while (((qh->elementlinkptr & FLISTP_TERMINATE) == 0) && (timeout-- > 0)) { |
| 275 | if (current != GET_TD (qh->elementlinkptr)) { |
| 276 | current = GET_TD (qh->elementlinkptr); |
Mathias Krause | 7b7b566 | 2012-05-29 16:19:19 +0200 | [diff] [blame] | 277 | timeout = 1000; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 278 | } |
Patrick Georgi | f42fdab | 2011-11-18 14:44:16 +0100 | [diff] [blame] | 279 | uhci_reg_write16(controller, USBSTS, |
| 280 | uhci_reg_read16(controller, USBSTS) | 0); // clear resettable registers |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 281 | udelay (30); |
| 282 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 283 | return (GET_TD (qh->elementlinkptr) == |
| 284 | 0) ? 0 : GET_TD (phys_to_virt (qh->elementlinkptr)); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 287 | static int |
| 288 | maxlen (int size) |
| 289 | { |
| 290 | return (size - 1) & 0x7ff; |
| 291 | } |
| 292 | |
| 293 | static int |
| 294 | min (int a, int b) |
| 295 | { |
| 296 | if (a < b) |
| 297 | return a; |
| 298 | else |
| 299 | return b; |
| 300 | } |
| 301 | |
| 302 | static int |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 303 | uhci_control (usbdev_t *dev, direction_t dir, int drlen, void *devreq, int dalen, |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 304 | unsigned char *data) |
| 305 | { |
| 306 | int endp = 0; /* this is control: always 0 */ |
| 307 | int mlen = dev->endpoints[0].maxpacketsize; |
| 308 | int count = (2 + (dalen + mlen - 1) / mlen); |
| 309 | unsigned short req = ((unsigned short *) devreq)[0]; |
| 310 | int i; |
| 311 | td_t *tds = memalign (16, sizeof (td_t) * count); |
| 312 | memset (tds, 0, sizeof (td_t) * count); |
| 313 | count--; /* to compensate for 0-indexed array */ |
| 314 | for (i = 0; i < count; i++) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 315 | tds[i].ptr = virt_to_phys (&tds[i + 1]) | TD_DEPTH_FIRST; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 316 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 317 | tds[count].ptr = 0 | TD_DEPTH_FIRST | TD_TERMINATE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 318 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 319 | tds[0].token = UHCI_SETUP | |
| 320 | dev->address << TD_DEVADDR_SHIFT | |
| 321 | endp << TD_EP_SHIFT | |
| 322 | TD_TOGGLE_DATA0 | |
| 323 | maxlen(drlen) << TD_MAXLEN_SHIFT; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 324 | tds[0].bufptr = virt_to_phys (devreq); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 325 | tds[0].ctrlsts = (3 << TD_COUNTER_SHIFT) | |
| 326 | (dev->speed?TD_LOWSPEED:0) | |
| 327 | TD_STATUS_ACTIVE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 328 | |
| 329 | int toggle = 1; |
| 330 | for (i = 1; i < count; i++) { |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 331 | switch (dir) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 332 | case SETUP: tds[i].token = UHCI_SETUP; break; |
| 333 | case IN: tds[i].token = UHCI_IN; break; |
| 334 | case OUT: tds[i].token = UHCI_OUT; break; |
Patrick Georgi | d78691d | 2010-06-07 13:58:17 +0000 | [diff] [blame] | 335 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 336 | tds[i].token |= dev->address << TD_DEVADDR_SHIFT | |
| 337 | endp << TD_EP_SHIFT | |
| 338 | maxlen (min (mlen, dalen)) << TD_MAXLEN_SHIFT | |
| 339 | toggle << TD_TOGGLE_SHIFT; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 340 | tds[i].bufptr = virt_to_phys (data); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 341 | tds[i].ctrlsts = (3 << TD_COUNTER_SHIFT) | |
| 342 | (dev->speed?TD_LOWSPEED:0) | |
| 343 | TD_STATUS_ACTIVE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 344 | toggle ^= 1; |
| 345 | dalen -= mlen; |
| 346 | data += mlen; |
| 347 | } |
| 348 | |
Nico Huber | cefec0e | 2012-05-16 15:04:27 +0200 | [diff] [blame] | 349 | tds[count].token = ((dir == OUT) ? UHCI_IN : UHCI_OUT) | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 350 | dev->address << TD_DEVADDR_SHIFT | |
| 351 | endp << TD_EP_SHIFT | |
| 352 | maxlen(0) << TD_MAXLEN_SHIFT | |
| 353 | TD_TOGGLE_DATA1; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 354 | tds[count].bufptr = 0; |
Nico Huber | cefec0e | 2012-05-16 15:04:27 +0200 | [diff] [blame] | 355 | tds[count].ctrlsts = (0 << TD_COUNTER_SHIFT) | /* as Linux 2.4.10 does */ |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 356 | (dev->speed?TD_LOWSPEED:0) | |
| 357 | TD_STATUS_ACTIVE; |
| 358 | UHCI_INST (dev->controller)->qh_data->elementlinkptr = |
| 359 | virt_to_phys (tds) & ~(FLISTP_QH | FLISTP_TERMINATE); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 360 | td_t *td = wait_for_completed_qh (dev->controller, |
| 361 | UHCI_INST (dev->controller)-> |
| 362 | qh_data); |
| 363 | int result; |
| 364 | if (td == 0) { |
| 365 | result = 0; |
| 366 | } else { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame^] | 367 | usb_debug ("control packet, req %x\n", req); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 368 | td_dump (td); |
| 369 | result = 1; |
| 370 | } |
| 371 | free (tds); |
| 372 | return result; |
| 373 | } |
| 374 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 375 | static td_t * |
| 376 | create_schedule (int numpackets) |
| 377 | { |
| 378 | if (numpackets == 0) |
| 379 | return 0; |
| 380 | td_t *tds = memalign (16, sizeof (td_t) * numpackets); |
| 381 | memset (tds, 0, sizeof (td_t) * numpackets); |
| 382 | int i; |
| 383 | for (i = 0; i < numpackets; i++) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 384 | tds[i].ptr = virt_to_phys (&tds[i + 1]) | TD_DEPTH_FIRST; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 385 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 386 | tds[numpackets - 1].ptr = 0 | TD_TERMINATE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 387 | return tds; |
| 388 | } |
| 389 | |
| 390 | static void |
| 391 | fill_schedule (td_t *td, endpoint_t *ep, int length, unsigned char *data, |
| 392 | int *toggle) |
| 393 | { |
Patrick Georgi | cfaa081 | 2010-06-11 14:25:40 +0000 | [diff] [blame] | 394 | switch (ep->direction) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 395 | case IN: td->token = UHCI_IN; break; |
| 396 | case OUT: td->token = UHCI_OUT; break; |
| 397 | case SETUP: td->token = UHCI_SETUP; break; |
Patrick Georgi | cfaa081 | 2010-06-11 14:25:40 +0000 | [diff] [blame] | 398 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 399 | td->token |= ep->dev->address << TD_DEVADDR_SHIFT | |
| 400 | (ep->endpoint & 0xf) << TD_EP_SHIFT | |
| 401 | maxlen (length) << TD_MAXLEN_SHIFT | |
| 402 | (*toggle & 1) << TD_TOGGLE_SHIFT; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 403 | td->bufptr = virt_to_phys (data); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 404 | td->ctrlsts = ((ep->direction == SETUP?3:0) << TD_COUNTER_SHIFT) | |
Nico Huber | cefec0e | 2012-05-16 15:04:27 +0200 | [diff] [blame] | 405 | (ep->dev->speed?TD_LOWSPEED:0) | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 406 | TD_STATUS_ACTIVE; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 407 | *toggle ^= 1; |
| 408 | } |
| 409 | |
| 410 | static int |
| 411 | run_schedule (usbdev_t *dev, td_t *td) |
| 412 | { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 413 | UHCI_INST (dev->controller)->qh_data->elementlinkptr = |
Anton Kochkov | efcb8de | 2012-10-02 00:04:29 +0400 | [diff] [blame] | 414 | virt_to_phys (td) & ~(FLISTP_QH | FLISTP_TERMINATE); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 415 | td = wait_for_completed_qh (dev->controller, |
| 416 | UHCI_INST (dev->controller)->qh_data); |
| 417 | if (td == 0) { |
| 418 | return 0; |
| 419 | } else { |
| 420 | td_dump (td); |
| 421 | return 1; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /* finalize == 1: if data is of packet aligned size, add a zero length packet */ |
| 426 | static int |
| 427 | uhci_bulk (endpoint_t *ep, int size, u8 *data, int finalize) |
| 428 | { |
| 429 | int maxpsize = ep->maxpacketsize; |
| 430 | if (maxpsize == 0) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 431 | fatal("MaxPacketSize == 0!!!"); |
Patrick Georgi | d2cb7ea | 2012-10-03 08:23:56 +0200 | [diff] [blame] | 432 | int numpackets = (size + maxpsize - 1) / maxpsize; |
| 433 | if (finalize && ((size % maxpsize) == 0)) { |
| 434 | numpackets++; |
| 435 | } |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 436 | if (numpackets == 0) |
| 437 | return 0; |
| 438 | td_t *tds = create_schedule (numpackets); |
| 439 | int i = 0, toggle = ep->toggle; |
| 440 | while ((size > 0) || ((size == 0) && (finalize != 0))) { |
| 441 | fill_schedule (&tds[i], ep, min (size, maxpsize), data, |
| 442 | &toggle); |
| 443 | i++; |
| 444 | data += maxpsize; |
| 445 | size -= maxpsize; |
| 446 | } |
| 447 | if (run_schedule (ep->dev, tds) == 1) { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame^] | 448 | usb_debug("Stalled. Trying to clean up.\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 449 | clear_stall (ep); |
| 450 | free (tds); |
| 451 | return 1; |
| 452 | } |
| 453 | ep->toggle = toggle; |
| 454 | free (tds); |
| 455 | return 0; |
| 456 | } |
| 457 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 458 | typedef struct { |
| 459 | qh_t *qh; |
| 460 | td_t *tds; |
| 461 | td_t *last_td; |
| 462 | u8 *data; |
| 463 | int lastread; |
| 464 | int total; |
| 465 | int reqsize; |
| 466 | } intr_q; |
| 467 | |
| 468 | /* create and hook-up an intr queue into device schedule */ |
| 469 | static void* |
| 470 | uhci_create_intr_queue (endpoint_t *ep, int reqsize, int reqcount, int reqtiming) |
| 471 | { |
| 472 | u8 *data = malloc(reqsize*reqcount); |
| 473 | td_t *tds = memalign(16, sizeof(td_t) * reqcount); |
| 474 | qh_t *qh = memalign(16, sizeof(qh_t)); |
| 475 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 476 | if (!data || !tds || !qh) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 477 | fatal("Not enough memory to create USB intr queue prerequisites.\n"); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 478 | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 479 | qh->elementlinkptr = virt_to_phys(tds); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 480 | |
| 481 | intr_q *q = malloc(sizeof(intr_q)); |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 482 | if (!q) |
Patrick Georgi | 2e768e7 | 2011-11-04 11:50:03 +0100 | [diff] [blame] | 483 | fatal("Not enough memory to create USB intr queue.\n"); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 484 | q->qh = qh; |
| 485 | q->tds = tds; |
| 486 | q->data = data; |
| 487 | q->lastread = 0; |
| 488 | q->total = reqcount; |
| 489 | q->reqsize = reqsize; |
| 490 | q->last_td = &tds[reqcount - 1]; |
| 491 | |
| 492 | memset (tds, 0, sizeof (td_t) * reqcount); |
| 493 | int i; |
| 494 | for (i = 0; i < reqcount; i++) { |
| 495 | tds[i].ptr = virt_to_phys (&tds[i + 1]); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 496 | |
Patrick Georgi | cfaa081 | 2010-06-11 14:25:40 +0000 | [diff] [blame] | 497 | switch (ep->direction) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 498 | case IN: tds[i].token = UHCI_IN; break; |
| 499 | case OUT: tds[i].token = UHCI_OUT; break; |
| 500 | case SETUP: tds[i].token = UHCI_SETUP; break; |
Patrick Georgi | cfaa081 | 2010-06-11 14:25:40 +0000 | [diff] [blame] | 501 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 502 | tds[i].token |= ep->dev->address << TD_DEVADDR_SHIFT | |
| 503 | (ep->endpoint & 0xf) << TD_EP_SHIFT | |
| 504 | maxlen (reqsize) << TD_MAXLEN_SHIFT | |
| 505 | (ep->toggle & 1) << TD_TOGGLE_SHIFT; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 506 | tds[i].bufptr = virt_to_phys (data); |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 507 | tds[i].ctrlsts = (0 << TD_COUNTER_SHIFT) | |
Nico Huber | cefec0e | 2012-05-16 15:04:27 +0200 | [diff] [blame] | 508 | (ep->dev->speed?TD_LOWSPEED:0) | |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 509 | TD_STATUS_ACTIVE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 510 | ep->toggle ^= 1; |
| 511 | data += reqsize; |
| 512 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 513 | tds[reqcount - 1].ptr = 0 | TD_TERMINATE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 514 | for (i = reqtiming; i < 1024; i += reqtiming) { |
| 515 | /* FIXME: wrap in another qh, one for each occurance of the qh in the framelist */ |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 516 | qh->headlinkptr = UHCI_INST (ep->dev->controller)->framelistptr[i] & ~FLISTP_TERMINATE; |
| 517 | UHCI_INST (ep->dev->controller)->framelistptr[i] = virt_to_phys(qh) | FLISTP_QH; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 518 | } |
| 519 | return q; |
| 520 | } |
| 521 | |
| 522 | /* remove queue from device schedule, dropping all data that came in */ |
| 523 | static void |
| 524 | uhci_destroy_intr_queue (endpoint_t *ep, void *q_) |
| 525 | { |
| 526 | intr_q *q = (intr_q*)q_; |
| 527 | u32 val = virt_to_phys (q->qh); |
| 528 | u32 end = virt_to_phys (UHCI_INST (ep->dev->controller)->qh_intr); |
| 529 | int i; |
| 530 | for (i=0; i<1024; i++) { |
| 531 | u32 oldptr = 0; |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 532 | u32 ptr = UHCI_INST (ep->dev->controller)->framelistptr[i]; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 533 | while (ptr != end) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 534 | if (((qh_t*)phys_to_virt(ptr))->elementlinkptr == val) { |
| 535 | ((qh_t*)phys_to_virt(oldptr))->headlinkptr = ((qh_t*)phys_to_virt(ptr))->headlinkptr; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 536 | free(phys_to_virt(ptr)); |
| 537 | break; |
| 538 | } |
| 539 | oldptr = ptr; |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 540 | ptr = ((qh_t*)phys_to_virt(ptr))->headlinkptr; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | free(q->data); |
| 544 | free(q->tds); |
| 545 | free(q->qh); |
| 546 | free(q); |
| 547 | } |
| 548 | |
| 549 | /* read one intr-packet from queue, if available. extend the queue for new input. |
| 550 | return NULL if nothing new available. |
| 551 | Recommended use: while (data=poll_intr_queue(q)) process(data); |
| 552 | */ |
| 553 | static u8* |
| 554 | uhci_poll_intr_queue (void *q_) |
| 555 | { |
| 556 | intr_q *q = (intr_q*)q_; |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 557 | if ((q->tds[q->lastread].ctrlsts & TD_STATUS_ACTIVE) == 0) { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 558 | /* FIXME: handle errors */ |
| 559 | int current = q->lastread; |
| 560 | int previous; |
| 561 | if (q->lastread == 0) { |
| 562 | previous = q->total - 1; |
| 563 | } else { |
| 564 | previous = q->lastread - 1; |
| 565 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 566 | q->tds[previous].ctrlsts &= ~TD_STATUS_MASK; |
| 567 | q->tds[previous].ptr = 0 | TD_TERMINATE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 568 | if (q->last_td != &q->tds[previous]) { |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 569 | q->last_td->ptr = virt_to_phys(&q->tds[previous]) & ~TD_TERMINATE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 570 | q->last_td = &q->tds[previous]; |
| 571 | } |
Patrick Georgi | b0b4a52 | 2011-11-24 11:55:46 +0100 | [diff] [blame] | 572 | q->tds[previous].ctrlsts |= TD_STATUS_ACTIVE; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 573 | q->lastread = (q->lastread + 1) % q->total; |
| 574 | return &q->data[current*q->reqsize]; |
| 575 | } |
| 576 | return NULL; |
| 577 | } |
| 578 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 579 | void |
| 580 | uhci_reg_write32 (hci_t *ctrl, usbreg reg, u32 value) |
| 581 | { |
| 582 | outl (value, ctrl->reg_base + reg); |
| 583 | } |
| 584 | |
| 585 | u32 |
| 586 | uhci_reg_read32 (hci_t *ctrl, usbreg reg) |
| 587 | { |
| 588 | return inl (ctrl->reg_base + reg); |
| 589 | } |
| 590 | |
| 591 | void |
| 592 | uhci_reg_write16 (hci_t *ctrl, usbreg reg, u16 value) |
| 593 | { |
| 594 | outw (value, ctrl->reg_base + reg); |
| 595 | } |
| 596 | |
| 597 | u16 |
| 598 | uhci_reg_read16 (hci_t *ctrl, usbreg reg) |
| 599 | { |
| 600 | return inw (ctrl->reg_base + reg); |
| 601 | } |
| 602 | |
| 603 | void |
| 604 | uhci_reg_write8 (hci_t *ctrl, usbreg reg, u8 value) |
| 605 | { |
| 606 | outb (value, ctrl->reg_base + reg); |
| 607 | } |
| 608 | |
| 609 | u8 |
| 610 | uhci_reg_read8 (hci_t *ctrl, usbreg reg) |
| 611 | { |
| 612 | return inb (ctrl->reg_base + reg); |
| 613 | } |