debuggers.hg

view linux-2.6.10-xen-sparse/drivers/xen/usbfront/usbfront.c @ 3692:03c49d1bcd6b

bitkeeper revision 1.1159.243.1 (42042ba8OAh4EZUckgdGWWSqRLutaQ)

Various cleanups, including a move to the new ring macros.
author mwilli2@equilibrium.research
date Sat Feb 05 02:12:56 2005 +0000 (2005-02-05)
parents d295396360fb
children 924777207448
line source
1 /*
2 * Xen Virtual USB Frontend Driver
3 *
4 * This file contains the first version of the Xen virtual USB hub
5 * that I've managed not to delete by mistake (3rd time lucky!).
6 *
7 * Based on Linux's uhci.c, original copyright notices are displayed
8 * below. Portions also (c) 2004 Intel Research Cambridge
9 * and (c) 2004 Mark Williamson
10 *
11 * Contact <mark.williamson@cl.cam.ac.uk> or
12 * <xen-devel@lists.sourceforge.net> regarding this code.
13 *
14 * Still to be (maybe) implemented:
15 * - multiple port
16 * - multiple interfaces
17 * - migration / backend restart support?
18 * - unloading support
19 *
20 * Differences to a normal host controller:
21 * - the backend does most of the mucky stuff so we don't have to do various
22 * things that are necessary for a normal host controller (e.g. FSBR).
23 * - we don't have any hardware, so status registers are simulated in software.
24 */
26 /*
27 * Universal Host Controller Interface driver for USB.
28 *
29 * Maintainer: Johannes Erdfelt <johannes@erdfelt.com>
30 *
31 * (C) Copyright 1999 Linus Torvalds
32 * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
33 * (C) Copyright 1999 Randy Dunlap
34 * (C) Copyright 1999 Georg Acher, acher@in.tum.de
35 * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
36 * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
37 * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
38 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
39 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
40 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
41 *
42 * Intel documents this fairly well, and as far as I know there
43 * are no royalties or anything like that, but even so there are
44 * people who decided that they want to do the same thing in a
45 * completely different way.
46 *
47 * WARNING! The USB documentation is downright evil. Most of it
48 * is just crap, written by a committee. You're better off ignoring
49 * most of it, the important stuff is:
50 * - the low-level protocol (fairly simple but lots of small details)
51 * - working around the horridness of the rest
52 */
54 #include <linux/config.h>
55 #include <linux/module.h>
56 #include <linux/kernel.h>
57 #include <linux/init.h>
58 #include <linux/delay.h>
59 #include <linux/ioport.h>
60 #include <linux/sched.h>
61 #include <linux/slab.h>
62 #include <linux/smp_lock.h>
63 #include <linux/errno.h>
64 #include <linux/unistd.h>
65 #include <linux/interrupt.h>
66 #include <linux/spinlock.h>
67 #ifdef CONFIG_USB_DEBUG
68 #define DEBUG
69 #else
70 #undef DEBUG
71 #endif
72 #include <linux/usb.h>
74 #include <asm/uaccess.h>
75 #include <asm/irq.h>
76 #include <asm/system.h>
78 #include "xhci.h"
80 #include <linux/pm.h>
82 #include "../../../../../drivers/usb/hcd.h"
84 #include <asm-xen/xen-public/io/usbif.h>
85 #include <asm/ctrl_if.h>
86 #include <asm/xen-public/io/domain_controller.h>
88 /*
89 * Version Information
90 */
91 #define DRIVER_VERSION "v1.0"
92 #define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, Mark Williamson"
93 #define DRIVER_DESC "Xen Virtual USB Host Controller Interface driver"
95 /*
96 * debug = 0, no debugging messages
97 * debug = 1, dump failed URB's except for stalls
98 * debug = 2, dump all failed URB's (including stalls)
99 */
100 #ifdef DEBUG
101 static int debug = 1;
102 #else
103 static int debug = 0;
104 #endif
105 MODULE_PARM(debug, "i");
106 MODULE_PARM_DESC(debug, "Debug level");
107 static char *errbuf;
108 #define ERRBUF_LEN (PAGE_SIZE * 8)
110 static kmem_cache_t *xhci_up_cachep; /* urb_priv */
112 static int rh_submit_urb(struct urb *urb);
113 static int rh_unlink_urb(struct urb *urb);
114 //static int xhci_get_current_frame_number(struct usb_device *dev);
115 static int xhci_unlink_urb(struct urb *urb);
116 static void xhci_unlink_generic(struct urb *urb);
117 static void xhci_call_completion(struct urb *urb);
118 static void xhci_drain_ring(void);
120 #define MAX_URB_LOOP 2048 /* Maximum number of linked URB's */
122 static struct xhci *xhci;
124 enum { USBIF_STATE_CONNECTED = 2,
125 USBIF_STATE_DISCONNECTED = 1,
126 USBIF_STATE_CLOSED =0
127 };
129 static int awaiting_reset = 0;
131 #ifdef DEBUG
133 static void dump_urb(struct urb *urb)
134 {
135 printk(KERN_DEBUG "dumping urb @ %p\n"
136 " hcpriv = %p\n"
137 " next = %p\n"
138 " dev = %p\n"
139 " pipe = 0x%lx\n"
140 " status = %d\n"
141 " transfer_flags = 0x%lx\n"
142 " transfer_buffer = %p\n"
143 " transfer_buffer_length = %d\n"
144 " actual_length = %d\n"
145 " bandwidth = %d\n"
146 " setup_packet = %p\n",
147 urb, urb->hcpriv, urb->next, urb->dev, urb->pipe, urb->status,
148 urb->transfer_flags, urb->transfer_buffer, urb->transfer_buffer_length,
149 urb->actual_length, urb->bandwidth, urb->setup_packet);
150 if ( urb->setup_packet != NULL )
151 printk(KERN_DEBUG
152 "setup = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x }\n",
153 urb->setup_packet[0], urb->setup_packet[1], urb->setup_packet[2], urb->setup_packet[3],
154 urb->setup_packet[4], urb->setup_packet[5], urb->setup_packet[6], urb->setup_packet[7]);
155 printk(KERN_DEBUG "complete = %p\n"
156 "interval = %d\n", urb->complete, urb->interval);
158 }
160 static void xhci_show_resp(usbif_response_t *r)
161 {
162 printk(KERN_DEBUG "dumping response @ %p\n"
163 " id=0x%lx\n"
164 " op=0x%x\n"
165 " data=0x%x\n"
166 " status=0x%x\n"
167 " length=0x%lx\n",
168 r->id, r->operation, r->data, r->status, r->length);
169 }
171 #define DPRINK(...) printk(KERN_DEBUG __VA_ARGS__)
173 #else /* DEBUG */
175 #define dump_urb(blah) ((void)0)
176 #define xhci_show_resp(blah) ((void)0)
177 #define DPRINTK(blah,...) ((void)0)
179 #endif /* DEBUG */
181 /**
182 * xhci_construct_isoc - add isochronous information to a request
183 */
184 static int xhci_construct_isoc(usbif_request_t *req, struct urb *urb)
185 {
186 usbif_iso_t *schedule;
187 int i;
188 struct urb_priv *urb_priv = urb->hcpriv;
190 req->num_iso = urb->number_of_packets;
191 schedule = (usbif_iso_t *)__get_free_page(GFP_KERNEL);
193 if ( schedule == NULL )
194 return -ENOMEM;
196 for ( i = 0; i < req->num_iso; i++ )
197 {
198 schedule[i].buffer_offset = urb->iso_frame_desc[i].offset;
199 schedule[i].length = urb->iso_frame_desc[i].length;
200 }
202 urb_priv->schedule = schedule;
203 req->iso_schedule = virt_to_machine(schedule);
205 return 0;
206 }
208 static int xhci_queue_req(struct urb *urb)
209 {
210 usbif_request_t *req;
211 usbif_front_ring_t *usb_ring = &xhci->usb_ring;
213 #if DEBUG
214 printk(KERN_DEBUG
215 "usbif = %p, req_prod = %d (@ 0x%lx), resp_prod = %d, resp_cons = %d\n",
216 usbif, usbif->req_prod, virt_to_machine(&usbif->req_prod),
217 usbif->resp_prod, xhci->usb_resp_cons);
218 #endif
221 if ( RING_FULL(USBIF_RING, usb_ring) )
222 {
223 printk(KERN_WARNING
224 "xhci_queue_req(): USB ring full, not queuing request\n");
225 return -ENOBUFS;
226 }
228 /* Stick something in the shared communications ring. */
229 req = RING_GET_REQUEST(USBIF_RING, usb_ring, usb_ring->req_prod_pvt);
231 req->operation = USBIF_OP_IO;
232 req->port = 0; /* We don't care what the port is. */
233 req->id = (unsigned long) urb->hcpriv;
234 req->transfer_buffer = virt_to_machine(urb->transfer_buffer);
235 req->devnum = usb_pipedevice(urb->pipe);
236 req->direction = usb_pipein(urb->pipe);
237 req->speed = usb_pipeslow(urb->pipe);
238 req->pipe_type = usb_pipetype(urb->pipe);
239 req->length = urb->transfer_buffer_length;
240 req->transfer_flags = urb->transfer_flags;
241 req->endpoint = usb_pipeendpoint(urb->pipe);
242 req->speed = usb_pipeslow(urb->pipe);
243 req->timeout = urb->timeout * (1000 / HZ);
245 if ( usb_pipetype(urb->pipe) == 0 ) /* ISO */
246 {
247 int ret = xhci_construct_isoc(req, urb);
248 if ( ret != 0 )
249 return ret;
250 }
252 if(urb->setup_packet != NULL)
253 memcpy(req->setup, urb->setup_packet, 8);
254 else
255 memset(req->setup, 0, 8);
257 usb_ring->req_prod_pvt++;
258 RING_PUSH_REQUESTS(USBIF_RING, usb_ring);
260 notify_via_evtchn(xhci->evtchn);
262 DPRINTK("Queued request for an URB.\n");
263 dump_urb(urb);
265 return -EINPROGRESS;
266 }
268 static inline usbif_request_t *xhci_queue_probe(usbif_vdev_t port)
269 {
270 usbif_request_t *req;
271 usbif_front_ring_t *usb_ring = &xhci->usb_ring;
273 #if DEBUG
274 printk(KERN_DEBUG
275 "queuing probe: req_prod = %d (@ 0x%lx), resp_prod = %d, resp_cons = %d\n",
276 usbif->req_prod, virt_to_machine(&usbif->req_prod),
277 usbif->resp_prod, xhci->usb_resp_cons);
278 #endif
280 if ( RING_FULL(USBIF_RING, usb_ring) )
281 {
282 printk(KERN_WARNING
283 "xhci_queue_probe(): USB ring full, not queuing request\n");
284 return NULL;
285 }
287 /* Stick something in the shared communications ring. */
288 req = RING_GET_REQUEST(USBIF_RING, usb_ring, usb_ring->req_prod_pvt);
290 req->operation = USBIF_OP_PROBE;
291 req->port = port;
292 req->id = 0;
293 req->transfer_buffer = 0;
294 req->devnum = 0;
295 req->direction = 0;
296 req->speed = 0;
297 req->pipe_type = 0;
298 req->length = 0;
299 req->transfer_flags = 0;
300 req->endpoint = 0;
301 req->speed = 0;
303 usb_ring->req_prod_pvt++;
304 RING_PUSH_REQUESTS(USBIF_RING, usb_ring);
306 notify_via_evtchn(xhci->evtchn);
308 return req;
309 }
311 static int xhci_port_reset(usbif_vdev_t port)
312 {
313 usbif_request_t *req;
314 usbif_front_ring_t *usb_ring = &xhci->usb_ring;
316 /* We only reset one port at a time, so we only need one variable per
317 * hub. */
318 awaiting_reset = 1;
320 /* Stick something in the shared communications ring. */
321 req = RING_GET_REQUEST(USBIF_RING, usb_ring, usb_ring->req_prod_pvt);
323 req->operation = USBIF_OP_RESET;
324 req->port = port;
326 usb_ring->req_prod_pvt++;
327 RING_PUSH_REQUESTS(USBIF_RING, usb_ring);
329 notify_via_evtchn(xhci->evtchn);
331 while ( awaiting_reset > 0 )
332 {
333 mdelay(1);
334 xhci_drain_ring();
335 }
337 return awaiting_reset;
338 }
341 /*
342 * Only the USB core should call xhci_alloc_dev and xhci_free_dev
343 */
344 static int xhci_alloc_dev(struct usb_device *dev)
345 {
346 return 0;
347 }
349 static int xhci_free_dev(struct usb_device *dev)
350 {
351 return 0;
352 }
354 static inline void xhci_add_complete(struct urb *urb)
355 {
356 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
357 unsigned long flags;
359 spin_lock_irqsave(&xhci->complete_list_lock, flags);
360 list_add_tail(&urbp->complete_list, &xhci->complete_list);
361 spin_unlock_irqrestore(&xhci->complete_list_lock, flags);
362 }
364 /* When this returns, the owner of the URB may free its
365 * storage.
366 *
367 * We spin and wait for the URB to complete before returning.
368 */
369 static void xhci_delete_urb(struct urb *urb)
370 {
371 struct urb_priv *urbp;
373 urbp = urb->hcpriv;
375 /* If there's no urb_priv structure for this URB then it can't have
376 * been submitted at all. */
377 if ( urbp == NULL )
378 return;
380 /* For now we just spin until the URB completes. It shouldn't take too
381 * long and we don't expect to have to do this very often. */
382 while ( urb->status == -EINPROGRESS )
383 {
384 xhci_drain_ring();
385 mdelay(1);
386 }
388 /* Now we know that further transfers to the buffer won't
389 * occur, so we can safely return. */
390 }
392 static struct urb_priv *xhci_alloc_urb_priv(struct urb *urb)
393 {
394 struct urb_priv *urbp;
396 urbp = kmem_cache_alloc(xhci_up_cachep, SLAB_ATOMIC);
397 if (!urbp) {
398 err("xhci_alloc_urb_priv: couldn't allocate memory for urb_priv\n");
399 return NULL;
400 }
402 memset((void *)urbp, 0, sizeof(*urbp));
404 urbp->inserttime = jiffies;
405 urbp->urb = urb;
406 urbp->dev = urb->dev;
408 INIT_LIST_HEAD(&urbp->complete_list);
410 urb->hcpriv = urbp;
412 return urbp;
413 }
415 /*
416 * MUST be called with urb->lock acquired
417 */
418 /* When is this called? Do we need to stop the transfer (as we
419 * currently do)? */
420 static void xhci_destroy_urb_priv(struct urb *urb)
421 {
422 struct urb_priv *urbp;
424 urbp = (struct urb_priv *)urb->hcpriv;
425 if (!urbp)
426 return;
428 if (!list_empty(&urb->urb_list))
429 warn("xhci_destroy_urb_priv: urb %p still on xhci->urb_list or xhci->remove_list", urb);
431 if (!list_empty(&urbp->complete_list))
432 warn("xhci_destroy_urb_priv: urb %p still on xhci->complete_list", urb);
434 kmem_cache_free(xhci_up_cachep, urb->hcpriv);
436 urb->hcpriv = NULL;
437 }
439 /**
440 * Try to find URBs in progress on the same pipe to the same device.
441 *
442 * MUST be called with xhci->urb_list_lock acquired
443 */
444 static struct urb *xhci_find_urb_ep(struct xhci *xhci, struct urb *urb)
445 {
446 struct list_head *tmp, *head;
448 /* We don't match Isoc transfers since they are special */
449 if (usb_pipeisoc(urb->pipe))
450 return NULL;
452 head = &xhci->urb_list;
453 tmp = head->next;
454 while (tmp != head) {
455 struct urb *u = list_entry(tmp, struct urb, urb_list);
457 tmp = tmp->next;
459 if (u->dev == urb->dev && u->pipe == urb->pipe &&
460 u->status == -EINPROGRESS)
461 return u;
462 }
464 return NULL;
465 }
467 static int xhci_submit_urb(struct urb *urb)
468 {
469 int ret = -EINVAL;
470 unsigned long flags;
471 struct urb *eurb;
472 int bustime;
474 DPRINTK("URB submitted to XHCI driver.\n");
475 dump_urb(urb);
477 if (!urb)
478 return -EINVAL;
480 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv) {
481 warn("xhci_submit_urb: urb %p belongs to disconnected device or bus?", urb);
482 return -ENODEV;
483 }
485 if ( urb->dev->devpath == NULL )
486 BUG();
488 usb_inc_dev_use(urb->dev);
490 spin_lock_irqsave(&xhci->urb_list_lock, flags);
491 spin_lock(&urb->lock);
493 if (urb->status == -EINPROGRESS || urb->status == -ECONNRESET ||
494 urb->status == -ECONNABORTED) {
495 dbg("xhci_submit_urb: urb not available to submit (status = %d)", urb->status);
496 /* Since we can have problems on the out path */
497 spin_unlock(&urb->lock);
498 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
499 usb_dec_dev_use(urb->dev);
501 return ret;
502 }
504 INIT_LIST_HEAD(&urb->urb_list);
505 if (!xhci_alloc_urb_priv(urb)) {
506 ret = -ENOMEM;
508 goto out;
509 }
511 ( (struct urb_priv *)urb->hcpriv )->in_progress = 1;
513 eurb = xhci_find_urb_ep(xhci, urb);
514 if (eurb && !(urb->transfer_flags & USB_QUEUE_BULK)) {
515 ret = -ENXIO;
517 goto out;
518 }
520 /* Short circuit the virtual root hub */
521 if (urb->dev == xhci->rh.dev) {
522 ret = rh_submit_urb(urb);
524 goto out;
525 }
527 switch (usb_pipetype(urb->pipe)) {
528 case PIPE_CONTROL:
529 ret = xhci_queue_req(urb);
530 break;
531 case PIPE_INTERRUPT:
532 if (urb->bandwidth == 0) { /* not yet checked/allocated */
533 bustime = usb_check_bandwidth(urb->dev, urb);
534 if (bustime < 0)
535 ret = bustime;
536 else {
537 ret = xhci_queue_req(urb);
538 if (ret == -EINPROGRESS)
539 usb_claim_bandwidth(urb->dev, urb, bustime, 0);
540 }
541 } else /* bandwidth is already set */
542 ret = xhci_queue_req(urb);
543 break;
544 case PIPE_BULK:
545 ret = xhci_queue_req(urb);
546 break;
547 case PIPE_ISOCHRONOUS:
548 if (urb->bandwidth == 0) { /* not yet checked/allocated */
549 if (urb->number_of_packets <= 0) {
550 ret = -EINVAL;
551 break;
552 }
553 bustime = usb_check_bandwidth(urb->dev, urb);
554 if (bustime < 0) {
555 ret = bustime;
556 break;
557 }
559 ret = xhci_queue_req(urb);
560 if (ret == -EINPROGRESS)
561 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
562 } else /* bandwidth is already set */
563 ret = xhci_queue_req(urb);
564 break;
565 }
567 out:
568 urb->status = ret;
570 if (ret == -EINPROGRESS) {
571 /* We use _tail to make find_urb_ep more efficient */
572 list_add_tail(&urb->urb_list, &xhci->urb_list);
574 spin_unlock(&urb->lock);
575 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
577 return 0;
578 }
580 xhci_unlink_generic(urb);
582 spin_unlock(&urb->lock);
583 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
585 /* Only call completion if it was successful */
586 if (!ret)
587 xhci_call_completion(urb);
589 return ret;
590 }
592 /*
593 * Return the result of a transfer
594 *
595 * MUST be called with urb_list_lock acquired
596 */
597 static void xhci_transfer_result(struct xhci *xhci, struct urb *urb)
598 {
599 int ret = 0;
600 unsigned long flags;
601 struct urb_priv *urbp;
603 /* The root hub is special */
604 if (urb->dev == xhci->rh.dev)
605 return;
607 spin_lock_irqsave(&urb->lock, flags);
609 urbp = (struct urb_priv *)urb->hcpriv;
611 if ( ( (struct urb_priv *)urb->hcpriv )->in_progress )
612 ret = -EINPROGRESS;
614 if (urb->actual_length < urb->transfer_buffer_length) {
615 if (urb->transfer_flags & USB_DISABLE_SPD) {
616 ret = -EREMOTEIO;
617 }
618 }
620 if (urb->status == -EPIPE)
621 {
622 ret = urb->status;
623 /* endpoint has stalled - mark it halted */
624 usb_endpoint_halt(urb->dev, usb_pipeendpoint(urb->pipe),
625 usb_pipeout(urb->pipe));
626 }
628 if ((debug == 1 && ret != 0 && ret != -EPIPE) ||
629 (ret != 0 && debug > 1)) {
630 /* Some debugging code */
631 dbg("xhci_result_interrupt/bulk() failed with status %x",
632 status);
633 }
635 if (ret == -EINPROGRESS)
636 goto out;
638 switch (usb_pipetype(urb->pipe)) {
639 case PIPE_CONTROL:
640 case PIPE_BULK:
641 case PIPE_ISOCHRONOUS:
642 /* Release bandwidth for Interrupt or Isoc. transfers */
643 /* Spinlock needed ? */
644 if (urb->bandwidth)
645 usb_release_bandwidth(urb->dev, urb, 1);
646 xhci_unlink_generic(urb);
647 break;
648 case PIPE_INTERRUPT:
649 /* Interrupts are an exception */
650 if (urb->interval)
651 goto out_complete;
653 /* Release bandwidth for Interrupt or Isoc. transfers */
654 /* Spinlock needed ? */
655 if (urb->bandwidth)
656 usb_release_bandwidth(urb->dev, urb, 0);
657 xhci_unlink_generic(urb);
658 break;
659 default:
660 info("xhci_transfer_result: unknown pipe type %d for urb %p\n",
661 usb_pipetype(urb->pipe), urb);
662 }
664 /* Remove it from xhci->urb_list */
665 list_del_init(&urb->urb_list);
667 out_complete:
668 xhci_add_complete(urb);
670 out:
671 spin_unlock_irqrestore(&urb->lock, flags);
672 }
674 /*
675 * MUST be called with urb->lock acquired
676 */
677 static void xhci_unlink_generic(struct urb *urb)
678 {
679 struct urb_priv *urbp = urb->hcpriv;
681 /* We can get called when urbp allocation fails, so check */
682 if (!urbp)
683 return;
685 /* ??? This function is now so minimal it doesn't do much. Do we really
686 * need it? */
688 xhci_delete_urb(urb);
689 }
691 static int xhci_unlink_urb(struct urb *urb)
692 {
693 unsigned long flags;
694 struct urb_priv *urbp = urb->hcpriv;
696 if (!urb)
697 return -EINVAL;
699 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv)
700 return -ENODEV;
702 spin_lock_irqsave(&xhci->urb_list_lock, flags);
703 spin_lock(&urb->lock);
705 /* Release bandwidth for Interrupt or Isoc. transfers */
706 /* Spinlock needed ? */
707 if (urb->bandwidth) {
708 switch (usb_pipetype(urb->pipe)) {
709 case PIPE_INTERRUPT:
710 usb_release_bandwidth(urb->dev, urb, 0);
711 break;
712 case PIPE_ISOCHRONOUS:
713 usb_release_bandwidth(urb->dev, urb, 1);
714 break;
715 default:
716 break;
717 }
718 }
720 if (urb->status != -EINPROGRESS) {
721 spin_unlock(&urb->lock);
722 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
723 return 0;
724 }
726 list_del_init(&urb->urb_list);
728 xhci_unlink_generic(urb);
730 /* Short circuit the virtual root hub */
731 if (urb->dev == xhci->rh.dev) {
732 rh_unlink_urb(urb);
734 spin_unlock(&urb->lock);
735 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
737 xhci_call_completion(urb);
738 } else {
739 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
740 urbp->status = urb->status = -ECONNABORTED;
742 spin_lock(&xhci->urb_remove_list_lock);
744 list_add(&urb->urb_list, &xhci->urb_remove_list);
746 spin_unlock(&xhci->urb_remove_list_lock);
748 spin_unlock(&urb->lock);
749 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
751 } else {
752 urb->status = -ENOENT;
754 spin_unlock(&urb->lock);
755 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
757 if (in_interrupt()) { /* wait at least 1 frame */
758 static int errorcount = 10;
760 if (errorcount--)
761 dbg("xhci_unlink_urb called from interrupt for urb %p", urb);
762 udelay(1000);
763 } else
764 schedule_timeout(1+1*HZ/1000);
766 xhci_call_completion(urb);
767 }
768 }
770 return 0;
771 }
774 static struct usb_operations xhci_device_operations = {
775 .allocate = xhci_alloc_dev,
776 .deallocate = xhci_free_dev,
777 /* It doesn't look like any drivers actually care what the frame number
778 * is at the moment! If necessary, we could approximate the current
779 * frame nubmer by passing it from the backend in response messages. */
780 .get_frame_number = NULL,
781 .submit_urb = xhci_submit_urb,
782 .unlink_urb = xhci_unlink_urb
783 };
785 /* Virtual Root Hub */
787 static __u8 root_hub_dev_des[] =
788 {
789 0x12, /* __u8 bLength; */
790 0x01, /* __u8 bDescriptorType; Device */
791 0x00, /* __u16 bcdUSB; v1.0 */
792 0x01,
793 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
794 0x00, /* __u8 bDeviceSubClass; */
795 0x00, /* __u8 bDeviceProtocol; */
796 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
797 0x00, /* __u16 idVendor; */
798 0x00,
799 0x00, /* __u16 idProduct; */
800 0x00,
801 0x00, /* __u16 bcdDevice; */
802 0x00,
803 0x00, /* __u8 iManufacturer; */
804 0x02, /* __u8 iProduct; */
805 0x01, /* __u8 iSerialNumber; */
806 0x01 /* __u8 bNumConfigurations; */
807 };
810 /* Configuration descriptor */
811 static __u8 root_hub_config_des[] =
812 {
813 0x09, /* __u8 bLength; */
814 0x02, /* __u8 bDescriptorType; Configuration */
815 0x19, /* __u16 wTotalLength; */
816 0x00,
817 0x01, /* __u8 bNumInterfaces; */
818 0x01, /* __u8 bConfigurationValue; */
819 0x00, /* __u8 iConfiguration; */
820 0x40, /* __u8 bmAttributes;
821 Bit 7: Bus-powered, 6: Self-powered,
822 Bit 5 Remote-wakeup, 4..0: resvd */
823 0x00, /* __u8 MaxPower; */
825 /* interface */
826 0x09, /* __u8 if_bLength; */
827 0x04, /* __u8 if_bDescriptorType; Interface */
828 0x00, /* __u8 if_bInterfaceNumber; */
829 0x00, /* __u8 if_bAlternateSetting; */
830 0x01, /* __u8 if_bNumEndpoints; */
831 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
832 0x00, /* __u8 if_bInterfaceSubClass; */
833 0x00, /* __u8 if_bInterfaceProtocol; */
834 0x00, /* __u8 if_iInterface; */
836 /* endpoint */
837 0x07, /* __u8 ep_bLength; */
838 0x05, /* __u8 ep_bDescriptorType; Endpoint */
839 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
840 0x03, /* __u8 ep_bmAttributes; Interrupt */
841 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
842 0x00,
843 0xff /* __u8 ep_bInterval; 255 ms */
844 };
846 static __u8 root_hub_hub_des[] =
847 {
848 0x09, /* __u8 bLength; */
849 0x29, /* __u8 bDescriptorType; Hub-descriptor */
850 0x02, /* __u8 bNbrPorts; */
851 0x00, /* __u16 wHubCharacteristics; */
852 0x00,
853 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
854 0x00, /* __u8 bHubContrCurrent; 0 mA */
855 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
856 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
857 };
859 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
860 static int rh_send_irq(struct urb *urb)
861 {
862 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
863 xhci_port_t *ports = xhci->rh.ports;
864 unsigned long flags;
865 int i, len = 1;
866 __u16 data = 0;
868 spin_lock_irqsave(&urb->lock, flags);
869 for (i = 0; i < xhci->rh.numports; i++) {
870 /* MAW: No idea what the old code was doing here or why it worked.
871 * This implementation sets a bit if anything at all has changed on the
872 * port, as per USB spec 11.12 */
873 data |= (ports[i].cs_chg || ports[i].pe_chg )
874 ? (1 << (i + 1))
875 : 0;
877 len = (i + 1) / 8 + 1;
878 }
880 *(__u16 *) urb->transfer_buffer = cpu_to_le16(data);
881 urb->actual_length = len;
882 urbp->status = 0;
884 spin_unlock_irqrestore(&urb->lock, flags);
886 if ((data > 0) && (xhci->rh.send != 0)) {
887 dbg("root-hub INT complete: data: %x", data);
888 xhci_call_completion(urb);
889 }
891 return 0;
892 }
894 /* Virtual Root Hub INTs are polled by this timer every "interval" ms */
895 static int rh_init_int_timer(struct urb *urb);
897 static void rh_int_timer_do(unsigned long ptr)
898 {
899 struct urb *urb = (struct urb *)ptr;
900 struct list_head list, *tmp, *head;
901 unsigned long flags;
902 int i;
904 for ( i = 0; i < xhci->rh.numports; i++)
905 xhci_queue_probe(i);
907 if (xhci->rh.send)
908 rh_send_irq(urb);
910 INIT_LIST_HEAD(&list);
912 spin_lock_irqsave(&xhci->urb_list_lock, flags);
913 head = &xhci->urb_list;
914 tmp = head->next;
915 while (tmp != head) {
916 struct urb *u = list_entry(tmp, struct urb, urb_list);
917 struct urb_priv *up = (struct urb_priv *)u->hcpriv;
919 tmp = tmp->next;
921 spin_lock(&u->lock);
923 /* Check if the URB timed out */
924 if (u->timeout && time_after_eq(jiffies, up->inserttime + u->timeout)) {
925 list_del(&u->urb_list);
926 list_add_tail(&u->urb_list, &list);
927 }
929 spin_unlock(&u->lock);
930 }
931 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
933 head = &list;
934 tmp = head->next;
935 while (tmp != head) {
936 struct urb *u = list_entry(tmp, struct urb, urb_list);
938 tmp = tmp->next;
940 u->transfer_flags |= USB_ASYNC_UNLINK | USB_TIMEOUT_KILLED;
941 xhci_unlink_urb(u);
942 }
944 rh_init_int_timer(urb);
945 }
947 /* Root Hub INTs are polled by this timer */
948 static int rh_init_int_timer(struct urb *urb)
949 {
950 xhci->rh.interval = urb->interval;
951 init_timer(&xhci->rh.rh_int_timer);
952 xhci->rh.rh_int_timer.function = rh_int_timer_do;
953 xhci->rh.rh_int_timer.data = (unsigned long)urb;
954 xhci->rh.rh_int_timer.expires = jiffies + (HZ * (urb->interval < 30 ? 30 : urb->interval)) / 1000;
955 add_timer(&xhci->rh.rh_int_timer);
957 return 0;
958 }
960 #define OK(x) len = (x); break
962 /* Root Hub Control Pipe */
963 static int rh_submit_urb(struct urb *urb)
964 {
965 unsigned int pipe = urb->pipe;
966 struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *)urb->setup_packet;
967 void *data = urb->transfer_buffer;
968 int leni = urb->transfer_buffer_length;
969 int len = 0;
970 xhci_port_t *status;
971 int stat = 0;
972 int i;
973 int retstatus;
974 unsigned long flags;
976 __u16 cstatus;
977 __u16 bmRType_bReq;
978 __u16 wValue;
979 __u16 wIndex;
980 __u16 wLength;
982 if (usb_pipetype(pipe) == PIPE_INTERRUPT) {
983 xhci->rh.urb = urb;
984 xhci->rh.send = 1;
985 xhci->rh.interval = urb->interval;
986 rh_init_int_timer(urb);
988 return -EINPROGRESS;
989 }
991 bmRType_bReq = cmd->bRequestType | cmd->bRequest << 8;
992 wValue = le16_to_cpu(cmd->wValue);
993 wIndex = le16_to_cpu(cmd->wIndex);
994 wLength = le16_to_cpu(cmd->wLength);
996 for (i = 0; i < 8; i++)
997 xhci->rh.c_p_r[i] = 0;
999 status = &xhci->rh.ports[wIndex - 1];
1001 spin_lock_irqsave(&xhci->rh.port_state_lock, flags);
1003 switch (bmRType_bReq) {
1004 /* Request Destination:
1005 without flags: Device,
1006 RH_INTERFACE: interface,
1007 RH_ENDPOINT: endpoint,
1008 RH_CLASS means HUB here,
1009 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1010 */
1012 case RH_GET_STATUS:
1013 *(__u16 *)data = cpu_to_le16(1);
1014 OK(2);
1015 case RH_GET_STATUS | RH_INTERFACE:
1016 *(__u16 *)data = cpu_to_le16(0);
1017 OK(2);
1018 case RH_GET_STATUS | RH_ENDPOINT:
1019 *(__u16 *)data = cpu_to_le16(0);
1020 OK(2);
1021 case RH_GET_STATUS | RH_CLASS:
1022 *(__u32 *)data = cpu_to_le32(0);
1023 OK(4); /* hub power */
1024 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1025 cstatus = (status->cs_chg) |
1026 (status->pe_chg << 1) |
1027 (xhci->rh.c_p_r[wIndex - 1] << 4);
1028 retstatus = (status->ccs) |
1029 (status->pe << 1) |
1030 (status->susp << 2) |
1031 (status->pr << 8) |
1032 (1 << 8) | /* power on */
1033 (status->lsda << 9);
1034 *(__u16 *)data = cpu_to_le16(retstatus);
1035 *(__u16 *)(data + 2) = cpu_to_le16(cstatus);
1036 OK(4);
1037 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1038 switch (wValue) {
1039 case RH_ENDPOINT_STALL:
1040 OK(0);
1042 break;
1043 case RH_CLEAR_FEATURE | RH_CLASS:
1044 switch (wValue) {
1045 case RH_C_HUB_OVER_CURRENT:
1046 OK(0); /* hub power over current */
1048 break;
1049 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1050 switch (wValue) {
1051 case RH_PORT_ENABLE:
1052 status->pe = 0;
1053 OK(0);
1054 case RH_PORT_SUSPEND:
1055 status->susp = 0;
1056 OK(0);
1057 case RH_PORT_POWER:
1058 OK(0); /* port power */
1059 case RH_C_PORT_CONNECTION:
1060 status->cs_chg = 0;
1061 OK(0);
1062 case RH_C_PORT_ENABLE:
1063 status->pe_chg = 0;
1064 OK(0);
1065 case RH_C_PORT_SUSPEND:
1066 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1067 OK(0);
1068 case RH_C_PORT_OVER_CURRENT:
1069 OK(0); /* port power over current */
1070 case RH_C_PORT_RESET:
1071 xhci->rh.c_p_r[wIndex - 1] = 0;
1072 OK(0);
1074 break;
1075 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1076 switch (wValue) {
1077 case RH_PORT_SUSPEND:
1078 status->susp = 1;
1079 OK(0);
1080 case RH_PORT_RESET:
1082 int ret;
1083 xhci->rh.c_p_r[wIndex - 1] = 1;
1084 status->pr = 0;
1085 status->pe = 1;
1086 ret = xhci_port_reset(wIndex - 1);
1087 /* XXX MAW: should probably cancel queued transfers during reset... *\/ */
1088 if ( ret == 0 ) { OK(0); }
1089 else { return ret; }
1091 break;
1092 case RH_PORT_POWER:
1093 OK(0); /* port power ** */
1094 case RH_PORT_ENABLE:
1095 status->pe = 1;
1096 OK(0);
1098 break;
1099 case RH_SET_ADDRESS:
1100 xhci->rh.devnum = wValue;
1101 OK(0);
1102 case RH_GET_DESCRIPTOR:
1103 switch ((wValue & 0xff00) >> 8) {
1104 case 0x01: /* device descriptor */
1105 len = min_t(unsigned int, leni,
1106 min_t(unsigned int,
1107 sizeof(root_hub_dev_des), wLength));
1108 memcpy(data, root_hub_dev_des, len);
1109 OK(len);
1110 case 0x02: /* configuration descriptor */
1111 len = min_t(unsigned int, leni,
1112 min_t(unsigned int,
1113 sizeof(root_hub_config_des), wLength));
1114 memcpy (data, root_hub_config_des, len);
1115 OK(len);
1116 case 0x03: /* string descriptors */
1117 len = usb_root_hub_string (wValue & 0xff,
1118 0, "XHCI-alt",
1119 data, wLength);
1120 if (len > 0) {
1121 OK(min_t(int, leni, len));
1122 } else
1123 stat = -EPIPE;
1125 break;
1126 case RH_GET_DESCRIPTOR | RH_CLASS:
1127 root_hub_hub_des[2] = xhci->rh.numports;
1128 len = min_t(unsigned int, leni,
1129 min_t(unsigned int, sizeof(root_hub_hub_des), wLength));
1130 memcpy(data, root_hub_hub_des, len);
1131 OK(len);
1132 case RH_GET_CONFIGURATION:
1133 *(__u8 *)data = 0x01;
1134 OK(1);
1135 case RH_SET_CONFIGURATION:
1136 OK(0);
1137 case RH_GET_INTERFACE | RH_INTERFACE:
1138 *(__u8 *)data = 0x00;
1139 OK(1);
1140 case RH_SET_INTERFACE | RH_INTERFACE:
1141 OK(0);
1142 default:
1143 stat = -EPIPE;
1146 spin_unlock_irqrestore(&xhci->rh.port_state_lock, flags);
1148 urb->actual_length = len;
1150 return stat;
1153 /*
1154 * MUST be called with urb->lock acquired
1155 */
1156 static int rh_unlink_urb(struct urb *urb)
1158 if (xhci->rh.urb == urb) {
1159 urb->status = -ENOENT;
1160 xhci->rh.send = 0;
1161 xhci->rh.urb = NULL;
1162 del_timer(&xhci->rh.rh_int_timer);
1164 return 0;
1167 static void xhci_call_completion(struct urb *urb)
1169 struct urb_priv *urbp;
1170 struct usb_device *dev = urb->dev;
1171 int is_ring = 0, killed, resubmit_interrupt, status;
1172 struct urb *nurb;
1173 unsigned long flags;
1175 spin_lock_irqsave(&urb->lock, flags);
1177 urbp = (struct urb_priv *)urb->hcpriv;
1178 if (!urbp || !urb->dev) {
1179 spin_unlock_irqrestore(&urb->lock, flags);
1180 return;
1183 killed = (urb->status == -ENOENT || urb->status == -ECONNABORTED ||
1184 urb->status == -ECONNRESET);
1185 resubmit_interrupt = (usb_pipetype(urb->pipe) == PIPE_INTERRUPT &&
1186 urb->interval);
1188 nurb = urb->next;
1189 if (nurb && !killed) {
1190 int count = 0;
1192 while (nurb && nurb != urb && count < MAX_URB_LOOP) {
1193 if (nurb->status == -ENOENT ||
1194 nurb->status == -ECONNABORTED ||
1195 nurb->status == -ECONNRESET) {
1196 killed = 1;
1197 break;
1200 nurb = nurb->next;
1201 count++;
1204 if (count == MAX_URB_LOOP)
1205 err("xhci_call_completion: too many linked URB's, loop? (first loop)");
1207 /* Check to see if chain is a ring */
1208 is_ring = (nurb == urb);
1211 status = urbp->status;
1212 if (!resubmit_interrupt || killed)
1213 /* We don't need urb_priv anymore */
1214 xhci_destroy_urb_priv(urb);
1216 if (!killed)
1217 urb->status = status;
1219 spin_unlock_irqrestore(&urb->lock, flags);
1221 if (urb->complete)
1222 urb->complete(urb);
1224 if (resubmit_interrupt)
1225 /* Recheck the status. The completion handler may have */
1226 /* unlinked the resubmitting interrupt URB */
1227 killed = (urb->status == -ENOENT ||
1228 urb->status == -ECONNABORTED ||
1229 urb->status == -ECONNRESET);
1231 if (resubmit_interrupt && !killed) {
1232 if ( urb->dev != xhci->rh.dev )
1233 xhci_queue_req(urb); /* XXX What if this fails? */
1234 /* Don't need to resubmit URBs for the virtual root dev. */
1235 } else {
1236 if (is_ring && !killed) {
1237 urb->dev = dev;
1238 xhci_submit_urb(urb);
1239 } else {
1240 /* We decrement the usage count after we're done */
1241 /* with everything */
1242 usb_dec_dev_use(dev);
1247 static void xhci_finish_completion(void)
1249 struct list_head *tmp, *head;
1250 unsigned long flags;
1252 spin_lock_irqsave(&xhci->complete_list_lock, flags);
1253 head = &xhci->complete_list;
1254 tmp = head->next;
1255 while (tmp != head) {
1256 struct urb_priv *urbp = list_entry(tmp, struct urb_priv, complete_list);
1257 struct urb *urb = urbp->urb;
1259 list_del_init(&urbp->complete_list);
1260 spin_unlock_irqrestore(&xhci->complete_list_lock, flags);
1262 xhci_call_completion(urb);
1264 spin_lock_irqsave(&xhci->complete_list_lock, flags);
1265 head = &xhci->complete_list;
1266 tmp = head->next;
1268 spin_unlock_irqrestore(&xhci->complete_list_lock, flags);
1271 static void receive_usb_reset(usbif_response_t *resp)
1273 awaiting_reset = resp->status;
1274 rmb();
1278 static void receive_usb_probe(usbif_response_t *resp)
1280 spin_lock(&xhci->rh.port_state_lock);
1282 if ( resp->status > 0 )
1284 if ( resp->status == 1 )
1286 /* If theres a device there and there wasn't one before there must
1287 * have been a connection status change. */
1288 if( xhci->rh.ports[resp->data].cs == 0 )
1290 xhci->rh.ports[resp->data].cs = 1;
1291 xhci->rh.ports[resp->data].ccs = 1;
1292 xhci->rh.ports[resp->data].cs_chg = 1;
1295 else
1296 printk(KERN_WARNING "receive_usb_probe(): unexpected status %d for port %d\n",
1297 resp->status, resp->data);
1299 else if ( resp->status < 0)
1300 printk(KERN_WARNING "receive_usb_probe(): got error status %d\n", resp->status);
1302 spin_unlock(&xhci->rh.port_state_lock);
1305 static void receive_usb_io(usbif_response_t *resp)
1307 struct urb_priv *urbp = (struct urb_priv *)resp->id;
1308 struct urb *urb = urbp->urb;
1310 urb->actual_length = resp->length;
1311 urb->status = resp->status;
1312 urbp->status = resp->status;
1313 urbp->in_progress = 0;
1315 if( usb_pipetype(urb->pipe) == 0 ) /* ISO */
1317 int i;
1319 /* Copy ISO schedule results back in. */
1321 for ( i = 0; i < urb->number_of_packets; i++ )
1323 urb->iso_frame_desc[i].status
1324 = urbp->schedule[i].status;
1325 urb->iso_frame_desc[i].actual_length
1326 = urbp->schedule[i].length;
1328 free_page((unsigned long)urbp->schedule);
1332 static void xhci_drain_ring(void)
1334 struct list_head *tmp, *head;
1335 usbif_front_ring_t *usb_ring = &xhci->usb_ring;
1336 usbif_response_t *resp;
1337 RING_IDX i, rp;
1339 /* Walk the ring here to get responses, updating URBs to show what
1340 * completed. */
1342 rp = usb_ring->sring->rsp_prod;
1343 rmb(); /* Ensure we see queued requests up to 'rp'. */
1345 /* Take items off the comms ring, taking care not to overflow. */
1346 for ( i = usb_ring->rsp_cons; i != rp; i++ )
1348 resp = RING_GET_RESPONSE(USBIF_RING, usb_ring, i);
1350 /* May need to deal with batching and with putting a ceiling on
1351 the number dispatched for performance and anti-dos reasons */
1353 xhci_show_resp(resp);
1355 switch ( resp->operation )
1357 case USBIF_OP_PROBE:
1358 receive_usb_probe(resp);
1359 break;
1361 case USBIF_OP_IO:
1362 receive_usb_io(resp);
1363 break;
1365 case USBIF_OP_RESET:
1366 receive_usb_reset(resp);
1367 break;
1369 default:
1370 printk(KERN_WARNING
1371 "error: unknown USB io operation response [%d]\n",
1372 resp->operation);
1373 break;
1377 usb_ring->rsp_cons = i;
1379 /* Walk the list of pending URB's to see which ones completed and do
1380 * callbacks, etc. */
1381 spin_lock(&xhci->urb_list_lock);
1382 head = &xhci->urb_list;
1383 tmp = head->next;
1384 while (tmp != head) {
1386 struct urb *urb = list_entry(tmp, struct urb, urb_list);
1388 tmp = tmp->next;
1390 /* Checks the status and does all of the magic necessary */
1391 xhci_transfer_result(xhci, urb);
1393 spin_unlock(&xhci->urb_list_lock);
1395 xhci_finish_completion();
1399 static void xhci_interrupt(int irq, void *__xhci, struct pt_regs *regs)
1401 xhci_drain_ring();
1404 static void free_xhci(struct xhci *xhci)
1406 kfree(xhci);
1409 /**
1410 * Initialise a new virtual root hub for a new USB device channel.
1411 */
1412 static int alloc_xhci(void)
1414 int retval;
1415 struct usb_bus *bus;
1417 retval = -EBUSY;
1419 xhci = kmalloc(sizeof(*xhci), GFP_KERNEL);
1420 if (!xhci) {
1421 err("couldn't allocate xhci structure");
1422 retval = -ENOMEM;
1423 goto err_alloc_xhci;
1426 /* Reset here so we don't get any interrupts from an old setup */
1427 /* or broken setup */
1428 // reset_hc(xhci);
1431 xhci->state = USBIF_STATE_CLOSED;
1432 xhci->is_suspended = 0;
1434 spin_lock_init(&xhci->urb_remove_list_lock);
1435 INIT_LIST_HEAD(&xhci->urb_remove_list);
1437 spin_lock_init(&xhci->urb_list_lock);
1438 INIT_LIST_HEAD(&xhci->urb_list);
1440 spin_lock_init(&xhci->complete_list_lock);
1441 INIT_LIST_HEAD(&xhci->complete_list);
1443 spin_lock_init(&xhci->frame_list_lock);
1445 /* We need exactly one page (per XHCI specs), how convenient */
1446 /* We assume that one page is atleast 4k (1024 frames * 4 bytes) */
1447 #if PAGE_SIZE < (4 * 1024)
1448 #error PAGE_SIZE is not atleast 4k
1449 #endif
1450 bus = usb_alloc_bus(&xhci_device_operations);
1451 if (!bus) {
1452 err("unable to allocate bus");
1453 goto err_alloc_bus;
1456 xhci->bus = bus;
1457 bus->bus_name = "XHCI";
1458 bus->hcpriv = xhci;
1460 usb_register_bus(xhci->bus);
1462 /* Initialize the root hub */
1464 xhci->rh.numports = 0;
1466 xhci->bus->root_hub = xhci->rh.dev = usb_alloc_dev(NULL, xhci->bus);
1467 if (!xhci->rh.dev) {
1468 err("unable to allocate root hub");
1469 goto err_alloc_root_hub;
1472 xhci->state = 0;
1474 return 0;
1476 /*
1477 * error exits:
1478 */
1479 err_alloc_root_hub:
1480 usb_free_bus(xhci->bus);
1481 xhci->bus = NULL;
1483 err_alloc_bus:
1484 free_xhci(xhci);
1486 err_alloc_xhci:
1487 return retval;
1490 static void usbif_status_change(usbif_fe_interface_status_changed_t *status)
1492 ctrl_msg_t cmsg;
1493 usbif_fe_interface_connect_t up;
1494 long rc;
1495 usbif_sring_t *sring;
1497 switch ( status->status )
1499 case USBIF_INTERFACE_STATUS_DESTROYED:
1500 printk(KERN_WARNING "Unexpected usbif-DESTROYED message in state %d\n",
1501 xhci->state);
1502 break;
1504 case USBIF_INTERFACE_STATUS_DISCONNECTED:
1505 if ( xhci->state != USBIF_STATE_CLOSED )
1507 printk(KERN_WARNING "Unexpected usbif-DISCONNECTED message"
1508 " in state %d\n", xhci->state);
1509 break;
1510 /* Not bothering to do recovery here for now. Keep things
1511 * simple. */
1514 /* Move from CLOSED to DISCONNECTED state. */
1515 sring = (usbif_sring_t *)__get_free_page(GFP_KERNEL);
1516 SHARED_RING_INIT(USBIF_RING, sring);
1517 FRONT_RING_INIT(USBIF_RING, &xhci->usb_ring, sring);
1518 xhci->state = USBIF_STATE_DISCONNECTED;
1520 /* Construct an interface-CONNECT message for the domain controller. */
1521 cmsg.type = CMSG_USBIF_FE;
1522 cmsg.subtype = CMSG_USBIF_FE_INTERFACE_CONNECT;
1523 cmsg.length = sizeof(usbif_fe_interface_connect_t);
1524 up.shmem_frame = virt_to_machine(sring) >> PAGE_SHIFT;
1525 memcpy(cmsg.msg, &up, sizeof(up));
1527 /* Tell the controller to bring up the interface. */
1528 ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
1529 break;
1531 case USBIF_INTERFACE_STATUS_CONNECTED:
1532 if ( xhci->state == USBIF_STATE_CLOSED )
1534 printk(KERN_WARNING "Unexpected usbif-CONNECTED message"
1535 " in state %d\n", xhci->state);
1536 break;
1539 xhci->evtchn = status->evtchn;
1540 xhci->irq = bind_evtchn_to_irq(xhci->evtchn);
1541 xhci->bandwidth = status->bandwidth;
1542 xhci->rh.numports = status->num_ports;
1544 xhci->rh.ports = kmalloc (sizeof(xhci_port_t) * xhci->rh.numports, GFP_KERNEL);
1545 memset(xhci->rh.ports, 0, sizeof(xhci_port_t) * xhci->rh.numports);
1547 usb_connect(xhci->rh.dev);
1549 if (usb_new_device(xhci->rh.dev) != 0) {
1550 err("unable to start root hub");
1553 /* Allocate the appropriate USB bandwidth here... Need to
1554 * somehow know what the total available is thought to be so we
1555 * can calculate the reservation correctly. */
1556 usb_claim_bandwidth(xhci->rh.dev, xhci->rh.urb,
1557 1000 - xhci->bandwidth, 0);
1559 if ( (rc = request_irq(xhci->irq, xhci_interrupt,
1560 SA_SAMPLE_RANDOM, "usbif", xhci)) )
1561 printk(KERN_ALERT"usbfront request_irq failed (%ld)\n",rc);
1563 DPRINTK(KERN_INFO __FILE__ ": USB XHCI: SHM at %p (0x%lx), EVTCHN %d IRQ %d\n",
1564 xhci->usb_ring.sring, virt_to_machine(xhci->usbif), xhci->evtchn, xhci->irq);
1566 xhci->state = USBIF_STATE_CONNECTED;
1568 break;
1570 default:
1571 printk(KERN_WARNING "Status change to unknown value %d\n",
1572 status->status);
1573 break;
1578 static void usbif_ctrlif_rx(ctrl_msg_t *msg, unsigned long id)
1580 switch ( msg->subtype )
1582 case CMSG_USBIF_FE_INTERFACE_STATUS_CHANGED:
1583 if ( msg->length != sizeof(usbif_fe_interface_status_changed_t) )
1584 goto parse_error;
1585 usbif_status_change((usbif_fe_interface_status_changed_t *)
1586 &msg->msg[0]);
1587 break;
1589 /* New interface...? */
1590 default:
1591 goto parse_error;
1594 ctrl_if_send_response(msg);
1595 return;
1597 parse_error:
1598 msg->length = 0;
1599 ctrl_if_send_response(msg);
1603 static int __init xhci_hcd_init(void)
1605 int retval = -ENOMEM, i;
1606 usbif_fe_interface_status_changed_t st;
1607 control_msg_t cmsg;
1609 if ( (xen_start_info.flags & SIF_INITDOMAIN)
1610 || (xen_start_info.flags & SIF_USB_BE_DOMAIN) )
1611 return 0;
1613 info(DRIVER_DESC " " DRIVER_VERSION);
1615 if (debug) {
1616 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
1617 if (!errbuf)
1618 goto errbuf_failed;
1621 xhci_up_cachep = kmem_cache_create("xhci_urb_priv",
1622 sizeof(struct urb_priv), 0, 0, NULL, NULL);
1623 if (!xhci_up_cachep)
1624 goto up_failed;
1626 /* Lazily avoid unloading issues for now. ;-)*/
1627 MOD_INC_USE_COUNT;
1629 /* Let the domain controller know we're here. For now we wait until
1630 * connection, as for the block and net drivers. This is only strictly
1631 * necessary if we're going to boot off a USB device. */
1632 printk(KERN_INFO "Initialising Xen virtual USB hub\n");
1634 (void)ctrl_if_register_receiver(CMSG_USBIF_FE, usbif_ctrlif_rx,
1635 CALLBACK_IN_BLOCKING_CONTEXT);
1637 alloc_xhci();
1639 /* Send a driver-UP notification to the domain controller. */
1640 cmsg.type = CMSG_USBIF_FE;
1641 cmsg.subtype = CMSG_USBIF_FE_DRIVER_STATUS_CHANGED;
1642 cmsg.length = sizeof(usbif_fe_driver_status_changed_t);
1643 st.status = USBIF_DRIVER_STATUS_UP;
1644 memcpy(cmsg.msg, &st, sizeof(st));
1645 ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
1647 /*
1648 * We should read 'nr_interfaces' from response message and wait
1649 * for notifications before proceeding. For now we assume that we
1650 * will be notified of exactly one interface.
1651 */
1652 for ( i=0; (xhci->state != USBIF_STATE_CONNECTED) && (i < 10*HZ); i++ )
1654 set_current_state(TASK_INTERRUPTIBLE);
1655 schedule_timeout(1);
1658 if (xhci->state != USBIF_STATE_CONNECTED)
1659 printk(KERN_WARNING "Timeout connecting USB frontend driver!\n");
1661 return 0;
1663 up_failed:
1665 if (errbuf)
1666 kfree(errbuf);
1668 errbuf_failed:
1670 return retval;
1673 static void __exit xhci_hcd_cleanup(void)
1675 if (kmem_cache_destroy(xhci_up_cachep))
1676 printk(KERN_WARNING "xhci: not all urb_priv's were freed\n");
1678 // release_xhci(); do some calls here
1681 if (errbuf)
1682 kfree(errbuf);
1685 module_init(xhci_hcd_init);
1686 module_exit(xhci_hcd_cleanup);
1688 MODULE_AUTHOR(DRIVER_AUTHOR);
1689 MODULE_DESCRIPTION(DRIVER_DESC);
1690 MODULE_LICENSE("GPL");