blob: 881e34562d1981462650f25a46e9b98433ff170e [file] [log] [blame]
Kevin O'Connor114592f2009-09-28 21:32:08 -04001// Code for handling UHCI USB controllers.
2//
3// Copyright (C) 2009 Kevin O'Connor <kevin@koconnor.net>
4//
5// This file may be distributed under the terms of the GNU LGPLv3 license.
6
7#include "util.h" // dprintf
8#include "pci.h" // pci_bdf_to_bus
9#include "config.h" // CONFIG_*
10#include "ioport.h" // outw
11#include "usb-uhci.h" // USBLEGSUP
12#include "pci_regs.h" // PCI_BASE_ADDRESS_4
13#include "usb.h" // struct usb_s
14#include "farptr.h" // GET_FLATPTR
Kevin O'Connor114592f2009-09-28 21:32:08 -040015
16static void
17reset_uhci(struct usb_s *cntl)
18{
19 // XXX - don't reset if not needed.
20
21 // Reset PIRQ and SMI
22 pci_config_writew(cntl->bdf, USBLEGSUP, USBLEGSUP_RWC);
23
24 // Reset the HC
Kevin O'Connor59f02832009-10-12 10:09:15 -040025 outw(USBCMD_HCRESET, cntl->uhci.iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -040026 udelay(5);
27
28 // Disable interrupts and commands (just to be safe).
Kevin O'Connor59f02832009-10-12 10:09:15 -040029 outw(0, cntl->uhci.iobase + USBINTR);
30 outw(0, cntl->uhci.iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -040031}
32
33static void
34configure_uhci(struct usb_s *cntl)
35{
36 // Allocate ram for schedule storage
37 struct uhci_td *term_td = malloc_high(sizeof(*term_td));
38 struct uhci_framelist *fl = memalign_high(sizeof(*fl), sizeof(*fl));
Kevin O'Connor991eaff2010-02-13 21:51:47 -050039 struct uhci_qh *intr_qh = malloc_high(sizeof(*intr_qh));
40 struct uhci_qh *data_qh = malloc_high(sizeof(*data_qh));
Kevin O'Connor114592f2009-09-28 21:32:08 -040041 struct uhci_qh *term_qh = malloc_high(sizeof(*term_qh));
Kevin O'Connor991eaff2010-02-13 21:51:47 -050042 if (!term_td || !fl || !intr_qh || !data_qh || !term_qh) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -050043 warn_noalloc();
Kevin O'Connor991eaff2010-02-13 21:51:47 -050044 free(term_td);
45 free(fl);
46 free(intr_qh);
47 free(data_qh);
48 free(term_qh);
Kevin O'Connor114592f2009-09-28 21:32:08 -040049 return;
50 }
51
52 // Work around for PIIX errata
53 memset(term_td, 0, sizeof(*term_td));
54 term_td->link = UHCI_PTR_TERM;
55 term_td->token = (uhci_explen(0) | (0x7f << TD_TOKEN_DEVADDR_SHIFT)
56 | USB_PID_IN);
57 memset(term_qh, 0, sizeof(*term_qh));
58 term_qh->element = (u32)term_td;
59 term_qh->link = UHCI_PTR_TERM;
60
61 // Setup primary queue head.
62 memset(data_qh, 0, sizeof(*data_qh));
63 data_qh->element = UHCI_PTR_TERM;
64 data_qh->link = (u32)term_qh | UHCI_PTR_QH;
Kevin O'Connor59f02832009-10-12 10:09:15 -040065 cntl->uhci.qh = data_qh;
Kevin O'Connor114592f2009-09-28 21:32:08 -040066
Kevin O'Connor991eaff2010-02-13 21:51:47 -050067 // Set schedule to point to primary intr queue head
68 memset(intr_qh, 0, sizeof(*intr_qh));
69 intr_qh->element = UHCI_PTR_TERM;
70 intr_qh->link = (u32)data_qh | UHCI_PTR_QH;
Kevin O'Connor114592f2009-09-28 21:32:08 -040071 int i;
Kevin O'Connor991eaff2010-02-13 21:51:47 -050072 for (i=0; i<ARRAY_SIZE(fl->links); i++)
73 fl->links[i] = (u32)intr_qh | UHCI_PTR_QH;
74 cntl->uhci.framelist = fl;
Kevin O'Connor95714392010-02-17 22:59:53 -050075 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -040076
77 // Set the frame length to the default: 1 ms exactly
Kevin O'Connor59f02832009-10-12 10:09:15 -040078 outb(USBSOF_DEFAULT, cntl->uhci.iobase + USBSOF);
Kevin O'Connor114592f2009-09-28 21:32:08 -040079
80 // Store the frame list base address
Kevin O'Connor59f02832009-10-12 10:09:15 -040081 outl((u32)fl->links, cntl->uhci.iobase + USBFLBASEADD);
Kevin O'Connor114592f2009-09-28 21:32:08 -040082
83 // Set the current frame number
Kevin O'Connor59f02832009-10-12 10:09:15 -040084 outw(0, cntl->uhci.iobase + USBFRNUM);
Kevin O'Connor114592f2009-09-28 21:32:08 -040085}
86
87static void
88start_uhci(struct usb_s *cntl)
89{
90 // Mark as configured and running with a 64-byte max packet.
Kevin O'Connor59f02832009-10-12 10:09:15 -040091 outw(USBCMD_RS | USBCMD_CF | USBCMD_MAXP, cntl->uhci.iobase + USBCMD);
Kevin O'Connor114592f2009-09-28 21:32:08 -040092}
93
94// Find any devices connected to the root hub.
95static int
96check_ports(struct usb_s *cntl)
97{
Kevin O'Connorba94a682010-02-14 19:05:35 -050098 // XXX - if just powered up, need to wait for USB_TIME_SIGATT?
Kevin O'Connor59f02832009-10-12 10:09:15 -040099 u16 port1 = inw(cntl->uhci.iobase + USBPORTSC1);
100 u16 port2 = inw(cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400101
102 if (!((port1 & USBPORTSC_CCS) || (port2 & USBPORTSC_CCS)))
103 // No devices
104 return 0;
105
Kevin O'Connorba94a682010-02-14 19:05:35 -0500106 // XXX - if just powered up, need to wait for USB_TIME_ATTDB?
107
Kevin O'Connor114592f2009-09-28 21:32:08 -0400108 // reset ports
109 if (port1 & USBPORTSC_CCS)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400110 outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC1);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400111 if (port2 & USBPORTSC_CCS)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400112 outw(USBPORTSC_PR, cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -0500113 msleep(USB_TIME_DRSTR);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400114
115 // Configure ports
116 int totalcount = 0;
Kevin O'Connorba94a682010-02-14 19:05:35 -0500117 outw(0, cntl->uhci.iobase + USBPORTSC1);
118 udelay(6); // 64 high-speed bit times
Kevin O'Connor59f02832009-10-12 10:09:15 -0400119 port1 = inw(cntl->uhci.iobase + USBPORTSC1);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400120 if (port1 & USBPORTSC_CCS) {
Kevin O'Connor59f02832009-10-12 10:09:15 -0400121 outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC1);
Kevin O'Connorba94a682010-02-14 19:05:35 -0500122 msleep(USB_TIME_RSTRCY);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400123 int count = configure_usb_device(cntl, !!(port1 & USBPORTSC_LSDA));
124 if (! count)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400125 outw(0, cntl->uhci.iobase + USBPORTSC1);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400126 totalcount += count;
127 }
Kevin O'Connorba94a682010-02-14 19:05:35 -0500128 outw(0, cntl->uhci.iobase + USBPORTSC2);
129 udelay(6);
Kevin O'Connor59f02832009-10-12 10:09:15 -0400130 port2 = inw(cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400131 if (port2 & USBPORTSC_CCS) {
Kevin O'Connor59f02832009-10-12 10:09:15 -0400132 outw(USBPORTSC_PE, cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connorba94a682010-02-14 19:05:35 -0500133 msleep(USB_TIME_RSTRCY);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400134 int count = configure_usb_device(cntl, !!(port2 & USBPORTSC_LSDA));
135 if (! count)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400136 outw(0, cntl->uhci.iobase + USBPORTSC2);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400137 totalcount += count;
138 }
139 return totalcount;
140}
141
Kevin O'Connora5826b52009-10-24 17:57:29 -0400142void
143uhci_init(void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400144{
145 if (! CONFIG_USB_UHCI)
Kevin O'Connora5826b52009-10-24 17:57:29 -0400146 return;
147 struct usb_s *cntl = data;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400148
Kevin O'Connorad901592009-12-13 11:25:25 -0500149 // XXX - don't call pci_config_XXX from a thread
Kevin O'Connor59f02832009-10-12 10:09:15 -0400150 cntl->type = USB_TYPE_UHCI;
151 cntl->uhci.iobase = (pci_config_readl(cntl->bdf, PCI_BASE_ADDRESS_4)
152 & PCI_BASE_ADDRESS_IO_MASK);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400153
154 dprintf(3, "UHCI init on dev %02x:%02x.%x (io=%x)\n"
155 , pci_bdf_to_bus(cntl->bdf), pci_bdf_to_dev(cntl->bdf)
Kevin O'Connor59f02832009-10-12 10:09:15 -0400156 , pci_bdf_to_fn(cntl->bdf), cntl->uhci.iobase);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400157
Kevin O'Connor59f02832009-10-12 10:09:15 -0400158 pci_config_maskw(cntl->bdf, PCI_COMMAND, 0, PCI_COMMAND_MASTER);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400159
160 reset_uhci(cntl);
161 configure_uhci(cntl);
162 start_uhci(cntl);
163
164 int count = check_ports(cntl);
165 if (! count) {
166 // XXX - no devices; free data structures.
Kevin O'Connor114592f2009-09-28 21:32:08 -0400167 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400168}
169
170static int
Kevin O'Connorfcc116f2009-10-30 23:16:07 -0400171wait_qh(struct usb_s *cntl, struct uhci_qh *qh)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400172{
173 // XXX - 500ms just a guess
174 u64 end = calc_future_tsc(500);
175 for (;;) {
176 if (qh->element & UHCI_PTR_TERM)
177 return 0;
Kevin O'Connor89eb6242009-10-22 22:30:37 -0400178 if (check_time(end)) {
Kevin O'Connorcfdc13f2010-02-14 13:07:54 -0500179 warn_timeout();
Kevin O'Connorfcc116f2009-10-30 23:16:07 -0400180 struct uhci_td *td = (void*)(qh->element & ~UHCI_PTR_BITS);
181 dprintf(1, "Timeout on wait_qh %p (td=%p s=%x c=%x/%x)\n"
182 , qh, td, td->status
183 , inw(cntl->uhci.iobase + USBCMD)
184 , inw(cntl->uhci.iobase + USBSTS));
Kevin O'Connor114592f2009-09-28 21:32:08 -0400185 return -1;
186 }
Kevin O'Connor229e3be2009-10-31 13:55:59 -0400187 yield();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400188 }
189}
190
Kevin O'Connor95714392010-02-17 22:59:53 -0500191static void
192uhci_waittick(void)
193{
194 // XXX - implement real tick detection.
195 msleep(2);
196}
197
Kevin O'Connor114592f2009-09-28 21:32:08 -0400198int
199uhci_control(u32 endp, int dir, const void *cmd, int cmdsize
200 , void *data, int datasize)
201{
202 if (! CONFIG_USB_UHCI)
Kevin O'Connor1c46a542009-10-17 23:53:32 -0400203 return -1;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400204
205 dprintf(5, "uhci_control %x\n", endp);
206 struct usb_s *cntl = endp2cntl(endp);
207 int maxpacket = endp2maxsize(endp);
208 int lowspeed = endp2speed(endp);
209 int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
210
211 // Setup transfer descriptors
212 int count = 2 + DIV_ROUND_UP(datasize, maxpacket);
213 struct uhci_td *tds = malloc_tmphigh(sizeof(*tds) * count);
214
215 tds[0].link = (u32)&tds[1] | UHCI_PTR_DEPTH;
216 tds[0].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
217 | TD_CTRL_ACTIVE);
218 tds[0].token = (uhci_explen(cmdsize) | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
219 | USB_PID_SETUP);
220 tds[0].buffer = (void*)cmd;
221 int toggle = TD_TOKEN_TOGGLE;
222 int i;
223 for (i=1; i<count-1; i++) {
224 tds[i].link = (u32)&tds[i+1] | UHCI_PTR_DEPTH;
225 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
226 | TD_CTRL_ACTIVE);
227 int len = (i == count-2 ? (datasize - (i-1)*maxpacket) : maxpacket);
228 tds[i].token = (uhci_explen(len) | toggle
229 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
230 | (dir ? USB_PID_IN : USB_PID_OUT));
231 tds[i].buffer = data + (i-1) * maxpacket;
232 toggle ^= TD_TOKEN_TOGGLE;
233 }
234 tds[i].link = UHCI_PTR_TERM;
235 tds[i].status = (uhci_maxerr(0) | (lowspeed ? TD_CTRL_LS : 0)
236 | TD_CTRL_ACTIVE);
237 tds[i].token = (uhci_explen(0) | TD_TOKEN_TOGGLE
238 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
239 | (dir ? USB_PID_OUT : USB_PID_IN));
240 tds[i].buffer = 0;
241
242 // Transfer data
Kevin O'Connor59f02832009-10-12 10:09:15 -0400243 struct uhci_qh *data_qh = cntl->uhci.qh;
Kevin O'Connor95714392010-02-17 22:59:53 -0500244 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400245 data_qh->element = (u32)&tds[0];
Kevin O'Connorfcc116f2009-10-30 23:16:07 -0400246 int ret = wait_qh(cntl, data_qh);
Kevin O'Connorad901592009-12-13 11:25:25 -0500247 if (ret) {
248 data_qh->element = UHCI_PTR_TERM;
Kevin O'Connor95714392010-02-17 22:59:53 -0500249 uhci_waittick();
Kevin O'Connorad901592009-12-13 11:25:25 -0500250 }
251 free(tds);
Kevin O'Connor95714392010-02-17 22:59:53 -0500252 return ret;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400253}
254
Kevin O'Connor59f02832009-10-12 10:09:15 -0400255struct usb_pipe *
Kevin O'Connor7149fc82010-02-17 23:24:42 -0500256uhci_alloc_bulk_pipe(u32 endp)
257{
258 if (! CONFIG_USB_UHCI)
259 return NULL;
260 struct usb_s *cntl = endp2cntl(endp);
261 dprintf(7, "uhci_alloc_bulk_pipe %x\n", endp);
262
263 // Allocate a queue head.
264 struct uhci_qh *qh = malloc_low(sizeof(*qh));
265 if (!qh) {
266 warn_noalloc();
267 return NULL;
268 }
269 qh->element = UHCI_PTR_TERM;
270 qh->next_td = 0;
271 qh->pipe.endp = endp;
272
273 // Add queue head to controller list.
274 struct uhci_qh *data_qh = cntl->uhci.qh;
275 qh->link = data_qh->link;
276 barrier();
277 data_qh->link = (u32)qh | UHCI_PTR_QH;
278
279 return &qh->pipe;
280}
281
282static int
283wait_td(struct uhci_td *td)
284{
285 u64 end = calc_future_tsc(5000); // XXX - lookup real time.
286 u32 status;
287 for (;;) {
288 status = td->status;
289 if (!(status & TD_CTRL_ACTIVE))
290 break;
291 if (check_time(end)) {
292 warn_timeout();
293 return -1;
294 }
295 yield();
296 }
297 if (status & TD_CTRL_ANY_ERROR) {
298 dprintf(1, "wait_td error - status=%x\n", status);
299 return -2;
300 }
301 return 0;
302}
303
304#define STACKTDS 4
305#define TDALIGN 16
306
307int
308uhci_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize)
309{
310 struct uhci_qh *qh = container_of(pipe, struct uhci_qh, pipe);
311 u32 endp = GET_FLATPTR(qh->pipe.endp);
312 dprintf(7, "uhci_send_bulk qh=%p endp=%x dir=%d data=%p size=%d\n"
313 , qh, endp, dir, data, datasize);
314 int maxpacket = endp2maxsize(endp);
315 int lowspeed = endp2speed(endp);
316 int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
317 int toggle = (u32)GET_FLATPTR(qh->next_td); // XXX
318
319 // Allocate 4 tds on stack (16byte aligned)
320 u8 tdsbuf[sizeof(struct uhci_td) * STACKTDS + TDALIGN - 1];
321 struct uhci_td *tds = (void*)ALIGN((u32)tdsbuf, TDALIGN);
322 memset(tds, 0, sizeof(*tds) * STACKTDS);
323
324 // Enable tds
325 SET_FLATPTR(qh->element, (u32)MAKE_FLATPTR(GET_SEG(SS), tds));
326
327 int tdpos = 0;
328 while (datasize) {
329 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
330 int ret = wait_td(td);
331 if (ret)
332 goto fail;
333
334 int transfer = datasize;
335 if (transfer > maxpacket)
336 transfer = maxpacket;
337 struct uhci_td *nexttd_fl = MAKE_FLATPTR(GET_SEG(SS)
338 , &tds[tdpos % STACKTDS]);
339 td->link = (transfer==datasize ? UHCI_PTR_TERM : (u32)nexttd_fl);
340 td->token = (uhci_explen(transfer) | toggle
341 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
342 | (dir ? USB_PID_IN : USB_PID_OUT));
343 td->buffer = data;
344 barrier();
345 td->status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
346 | TD_CTRL_ACTIVE);
347 toggle ^= TD_TOKEN_TOGGLE;
348
349 data += transfer;
350 datasize -= transfer;
351 }
352 int i;
353 for (i=0; i<STACKTDS; i++) {
354 struct uhci_td *td = &tds[tdpos++ % STACKTDS];
355 int ret = wait_td(td);
356 if (ret)
357 goto fail;
358 }
359
360 SET_FLATPTR(qh->next_td, (void*)toggle); // XXX
361 return 0;
362fail:
363 dprintf(1, "uhci_send_bulk failed\n");
364 SET_FLATPTR(qh->element, UHCI_PTR_TERM);
365 uhci_waittick();
366 return -1;
367}
368
369struct usb_pipe *
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500370uhci_alloc_intr_pipe(u32 endp, int frameexp)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400371{
372 if (! CONFIG_USB_UHCI)
373 return NULL;
374
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500375 dprintf(7, "uhci_alloc_intr_pipe %x %d\n", endp, frameexp);
376 if (frameexp > 10)
377 frameexp = 10;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400378 struct usb_s *cntl = endp2cntl(endp);
379 int maxpacket = endp2maxsize(endp);
380 int lowspeed = endp2speed(endp);
381 int devaddr = endp2devaddr(endp) | (endp2ep(endp) << 7);
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500382 // Determine number of entries needed for 2 timer ticks.
383 int ms = 1<<frameexp;
384 int count = DIV_ROUND_UP(PIT_TICK_INTERVAL * 1000 * 2, PIT_TICK_RATE * ms);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400385 struct uhci_qh *qh = malloc_low(sizeof(*qh));
386 struct uhci_td *tds = malloc_low(sizeof(*tds) * count);
Kevin O'Connor95714392010-02-17 22:59:53 -0500387 if (!qh || !tds) {
388 warn_noalloc();
389 goto fail;
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500390 }
Kevin O'Connor95714392010-02-17 22:59:53 -0500391 if (maxpacket > sizeof(tds[0].data))
392 goto fail;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400393 qh->element = (u32)tds;
394 int toggle = 0;
395 int i;
396 for (i=0; i<count; i++) {
397 tds[i].link = (i==count-1 ? (u32)&tds[0] : (u32)&tds[i+1]);
398 tds[i].status = (uhci_maxerr(3) | (lowspeed ? TD_CTRL_LS : 0)
399 | TD_CTRL_ACTIVE);
400 tds[i].token = (uhci_explen(maxpacket) | toggle
401 | (devaddr << TD_TOKEN_DEVADDR_SHIFT)
402 | USB_PID_IN);
403 tds[i].buffer = &tds[i].data;
404 toggle ^= TD_TOKEN_TOGGLE;
405 }
406
407 qh->next_td = &tds[0];
Kevin O'Connor59f02832009-10-12 10:09:15 -0400408 qh->pipe.endp = endp;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400409
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500410 // Add to interrupt schedule.
411 struct uhci_framelist *fl = cntl->uhci.framelist;
412 if (frameexp == 0) {
413 // Add to existing interrupt entry.
414 struct uhci_qh *intr_qh = (void*)(fl->links[0] & ~UHCI_PTR_BITS);
415 qh->link = intr_qh->link;
Kevin O'Connor95714392010-02-17 22:59:53 -0500416 barrier();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500417 intr_qh->link = (u32)qh | UHCI_PTR_QH;
418 } else {
419 int startpos = 1<<(frameexp-1);
420 qh->link = fl->links[startpos];
Kevin O'Connor95714392010-02-17 22:59:53 -0500421 barrier();
Kevin O'Connor991eaff2010-02-13 21:51:47 -0500422 for (i=startpos; i<ARRAY_SIZE(fl->links); i+=ms)
423 fl->links[i] = (u32)qh | UHCI_PTR_QH;
424 }
Kevin O'Connor114592f2009-09-28 21:32:08 -0400425
Kevin O'Connor59f02832009-10-12 10:09:15 -0400426 return &qh->pipe;
Kevin O'Connor95714392010-02-17 22:59:53 -0500427fail:
428 free(qh);
429 free(tds);
430 return NULL;
Kevin O'Connor114592f2009-09-28 21:32:08 -0400431}
432
433int
Kevin O'Connor59f02832009-10-12 10:09:15 -0400434uhci_poll_intr(struct usb_pipe *pipe, void *data)
Kevin O'Connor114592f2009-09-28 21:32:08 -0400435{
436 ASSERT16();
437 if (! CONFIG_USB_UHCI)
438 return -1;
439
Kevin O'Connor59f02832009-10-12 10:09:15 -0400440 struct uhci_qh *qh = container_of(pipe, struct uhci_qh, pipe);
Kevin O'Connor114592f2009-09-28 21:32:08 -0400441 struct uhci_td *td = GET_FLATPTR(qh->next_td);
442 u32 status = GET_FLATPTR(td->status);
443 u32 token = GET_FLATPTR(td->token);
444 if (status & TD_CTRL_ACTIVE)
445 // No intrs found.
446 return -1;
447 // XXX - check for errors.
448
449 // Copy data.
450 memcpy_far(GET_SEG(SS), data
451 , FLATPTR_TO_SEG(td->data), (void*)FLATPTR_TO_OFFSET(td->data)
452 , uhci_expected_length(token));
453
454 // Reenable this td.
455 u32 next = GET_FLATPTR(td->link);
Kevin O'Connor95714392010-02-17 22:59:53 -0500456 barrier();
Kevin O'Connor114592f2009-09-28 21:32:08 -0400457 SET_FLATPTR(td->status, (uhci_maxerr(0) | (status & TD_CTRL_LS)
458 | TD_CTRL_ACTIVE));
459 SET_FLATPTR(qh->next_td, (void*)(next & ~UHCI_PTR_BITS));
460
461 return 0;
462}