ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/usbfront/usbfront.c @ 6294:1a0723cd37f1

Fix many uses of machine addresses in XenLinux. Primarily
this fixes users of virt_to_machine/machine_to_virt to
use virt_to_mfn/mfn_to_virt where that is more appropriate.

This should be a big step to improved PAE stability.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Fri Aug 19 16:06:43 2005 +0000 (2005-08-19)
parents f46bb706a38d
children f51fe43c5d1c 5f4724c13040 81576d3d1ca8 3a8f27c6d56c
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, 2005 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 * - migration / backend restart support?
16 * - support for building / using as a module
17 */
19 /*
20 * Universal Host Controller Interface driver for USB.
21 *
22 * Maintainer: Johannes Erdfelt <johannes@erdfelt.com>
23 *
24 * (C) Copyright 1999 Linus Torvalds
25 * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com
26 * (C) Copyright 1999 Randy Dunlap
27 * (C) Copyright 1999 Georg Acher, acher@in.tum.de
28 * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de
29 * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch
30 * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at
31 * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface
32 * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com).
33 * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c)
34 *
35 * Intel documents this fairly well, and as far as I know there
36 * are no royalties or anything like that, but even so there are
37 * people who decided that they want to do the same thing in a
38 * completely different way.
39 *
40 * WARNING! The USB documentation is downright evil. Most of it
41 * is just crap, written by a committee. You're better off ignoring
42 * most of it, the important stuff is:
43 * - the low-level protocol (fairly simple but lots of small details)
44 * - working around the horridness of the rest
45 */
47 #include <linux/config.h>
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/init.h>
51 #include <linux/sched.h>
52 #include <linux/delay.h>
53 #include <linux/slab.h>
54 #include <linux/smp_lock.h>
55 #include <linux/errno.h>
56 #include <linux/interrupt.h>
57 #include <linux/spinlock.h>
58 #ifdef CONFIG_USB_DEBUG
59 #define DEBUG
60 #else
61 #undef DEBUG
62 #endif
63 #include <linux/usb.h>
65 #include <asm/irq.h>
66 #include <asm/system.h>
68 #include "xhci.h"
70 #include "../../../../../drivers/usb/hcd.h"
72 #include <asm-xen/xen-public/io/usbif.h>
73 #include <asm/ctrl_if.h>
74 #include <asm/xen-public/io/domain_controller.h>
76 /*
77 * Version Information
78 */
79 #define DRIVER_VERSION "v1.0"
80 #define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, " \
81 "Randy Dunlap, Georg Acher, Deti Fliegl, " \
82 "Thomas Sailer, Roman Weissgaerber, Mark Williamson"
83 #define DRIVER_DESC "Xen Virtual USB Host Controller Interface"
85 /*
86 * debug = 0, no debugging messages
87 * debug = 1, dump failed URB's except for stalls
88 * debug = 2, dump all failed URB's (including stalls)
89 */
90 #ifdef DEBUG
91 static int debug = 1;
92 #else
93 static int debug = 0;
94 #endif
95 MODULE_PARM(debug, "i");
96 MODULE_PARM_DESC(debug, "Debug level");
97 static char *errbuf;
98 #define ERRBUF_LEN (PAGE_SIZE * 8)
100 static int rh_submit_urb(struct urb *urb);
101 static int rh_unlink_urb(struct urb *urb);
102 static int xhci_unlink_urb(struct urb *urb);
103 static void xhci_call_completion(struct urb *urb);
104 static void xhci_drain_ring(void);
105 static void xhci_transfer_result(struct xhci *xhci, struct urb *urb);
106 static void xhci_finish_completion(void);
108 #define MAX_URB_LOOP 2048 /* Maximum number of linked URB's */
110 static kmem_cache_t *xhci_up_cachep; /* urb_priv cache */
111 static struct xhci *xhci; /* XHCI structure for the interface */
113 /******************************************************************************
114 * DEBUGGING
115 */
117 #ifdef DEBUG
119 static void dump_urb(struct urb *urb)
120 {
121 printk(KERN_DEBUG "dumping urb @ %p\n"
122 " hcpriv = %p\n"
123 " next = %p\n"
124 " dev = %p\n"
125 " pipe = 0x%lx\n"
126 " status = %d\n"
127 " transfer_flags = 0x%lx\n"
128 " transfer_buffer = %p\n"
129 " transfer_buffer_length = %d\n"
130 " actual_length = %d\n"
131 " bandwidth = %d\n"
132 " setup_packet = %p\n",
133 urb, urb->hcpriv, urb->next, urb->dev, urb->pipe, urb->status,
134 urb->transfer_flags, urb->transfer_buffer,
135 urb->transfer_buffer_length, urb->actual_length, urb->bandwidth,
136 urb->setup_packet);
137 if ( urb->setup_packet != NULL )
138 printk(KERN_DEBUG
139 "setup = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x }\n",
140 urb->setup_packet[0], urb->setup_packet[1],
141 urb->setup_packet[2], urb->setup_packet[3],
142 urb->setup_packet[4], urb->setup_packet[5],
143 urb->setup_packet[6], urb->setup_packet[7]);
144 printk(KERN_DEBUG "complete = %p\n"
145 "interval = %d\n", urb->complete, urb->interval);
147 }
149 static void xhci_show_resp(usbif_response_t *r)
150 {
151 printk(KERN_DEBUG "dumping response @ %p\n"
152 " id=0x%lx\n"
153 " op=0x%x\n"
154 " data=0x%x\n"
155 " status=0x%x\n"
156 " length=0x%lx\n",
157 r->id, r->operation, r->data, r->status, r->length);
158 }
160 #define DPRINK(...) printk(KERN_DEBUG __VA_ARGS__)
162 #else /* DEBUG */
164 #define dump_urb(blah) ((void)0)
165 #define xhci_show_resp(blah) ((void)0)
166 #define DPRINTK(blah,...) ((void)0)
168 #endif /* DEBUG */
170 /******************************************************************************
171 * RING REQUEST HANDLING
172 */
174 #define RING_PLUGGED(_hc) ( RING_FULL(&_hc->usb_ring) || _hc->recovery )
176 /**
177 * xhci_construct_isoc - add isochronous information to a request
178 */
179 static int xhci_construct_isoc(usbif_request_t *req, struct urb *urb)
180 {
181 usbif_iso_t *schedule;
182 int i;
183 struct urb_priv *urb_priv = urb->hcpriv;
185 req->num_iso = urb->number_of_packets;
186 schedule = (usbif_iso_t *)__get_free_page(GFP_KERNEL);
188 if ( schedule == NULL )
189 return -ENOMEM;
191 for ( i = 0; i < req->num_iso; i++ )
192 {
193 schedule[i].buffer_offset = urb->iso_frame_desc[i].offset;
194 schedule[i].length = urb->iso_frame_desc[i].length;
195 }
197 urb_priv->schedule = schedule;
198 req->iso_schedule = virt_to_mfn(schedule) << PAGE_SHIFT;
200 return 0;
201 }
203 /**
204 * xhci_queue_req - construct and queue request for an URB
205 */
206 static int xhci_queue_req(struct urb *urb)
207 {
208 unsigned long flags;
209 usbif_request_t *req;
210 usbif_front_ring_t *usb_ring = &xhci->usb_ring;
212 #if DEBUG
213 printk(KERN_DEBUG
214 "usbif = %p, req_prod = %d (@ 0x%lx), resp_prod = %d, resp_cons = %d\n",
215 usbif, usbif->req_prod, virt_to_mfn(&usbif->req_prod),
216 usbif->resp_prod, xhci->usb_resp_cons);
217 #endif
219 spin_lock_irqsave(&xhci->ring_lock, flags);
221 if ( RING_PLUGGED(xhci) )
222 {
223 printk(KERN_WARNING
224 "xhci_queue_req(): USB ring plugged, not queuing request\n");
225 spin_unlock_irqrestore(&xhci->ring_lock, flags);
226 return -ENOBUFS;
227 }
229 /* Stick something in the shared communications ring. */
230 req = RING_GET_REQUEST(usb_ring, usb_ring->req_prod_pvt);
232 req->operation = USBIF_OP_IO;
233 req->port = 0; /* We don't care what the port is. */
234 req->id = (unsigned long) urb->hcpriv;
235 req->transfer_buffer = virt_to_mfn(urb->transfer_buffer) << PAGE_SHIFT;
236 req->devnum = usb_pipedevice(urb->pipe);
237 req->direction = usb_pipein(urb->pipe);
238 req->speed = usb_pipeslow(urb->pipe);
239 req->pipe_type = usb_pipetype(urb->pipe);
240 req->length = urb->transfer_buffer_length;
241 req->transfer_flags = urb->transfer_flags;
242 req->endpoint = usb_pipeendpoint(urb->pipe);
243 req->speed = usb_pipeslow(urb->pipe);
244 req->timeout = urb->timeout * (1000 / HZ);
246 if ( usb_pipetype(urb->pipe) == 0 ) /* ISO */
247 {
248 int ret = xhci_construct_isoc(req, urb);
249 if ( ret != 0 )
250 return ret;
251 }
253 if(urb->setup_packet != NULL)
254 memcpy(req->setup, urb->setup_packet, 8);
255 else
256 memset(req->setup, 0, 8);
258 usb_ring->req_prod_pvt++;
259 RING_PUSH_REQUESTS(usb_ring);
261 spin_unlock_irqrestore(&xhci->ring_lock, flags);
263 notify_via_evtchn(xhci->evtchn);
265 DPRINTK("Queued request for an URB.\n");
266 dump_urb(urb);
268 return -EINPROGRESS;
269 }
271 /**
272 * xhci_queue_probe - queue a probe request for a particular port
273 */
274 static inline usbif_request_t *xhci_queue_probe(usbif_vdev_t port)
275 {
276 usbif_request_t *req;
277 usbif_front_ring_t *usb_ring = &xhci->usb_ring;
279 #if DEBUG
280 printk(KERN_DEBUG
281 "queuing probe: req_prod = %d (@ 0x%lx), resp_prod = %d, "
282 "resp_cons = %d\n", usbif->req_prod,
283 virt_to_mfn(&usbif->req_prod),
284 usbif->resp_prod, xhci->usb_resp_cons);
285 #endif
287 /* This is always called from the timer interrupt. */
288 spin_lock(&xhci->ring_lock);
290 if ( RING_PLUGGED(xhci) )
291 {
292 printk(KERN_WARNING
293 "xhci_queue_probe(): ring full, not queuing request\n");
294 spin_unlock(&xhci->ring_lock);
295 return NULL;
296 }
298 /* Stick something in the shared communications ring. */
299 req = RING_GET_REQUEST(usb_ring, usb_ring->req_prod_pvt);
301 memset(req, 0, sizeof(*req));
303 req->operation = USBIF_OP_PROBE;
304 req->port = port;
306 usb_ring->req_prod_pvt++;
307 RING_PUSH_REQUESTS(usb_ring);
309 spin_unlock(&xhci->ring_lock);
311 notify_via_evtchn(xhci->evtchn);
313 return req;
314 }
316 /**
317 * xhci_port_reset - queue a reset request for a particular port
318 */
319 static int xhci_port_reset(usbif_vdev_t port)
320 {
321 usbif_request_t *req;
322 usbif_front_ring_t *usb_ring = &xhci->usb_ring;
324 /* Only ever happens from process context (hub thread). */
325 spin_lock_irq(&xhci->ring_lock);
327 if ( RING_PLUGGED(xhci) )
328 {
329 printk(KERN_WARNING
330 "xhci_port_reset(): ring plugged, not queuing request\n");
331 spin_unlock_irq(&xhci->ring_lock);
332 return -ENOBUFS;
333 }
335 /* We only reset one port at a time, so we only need one variable per
336 * hub. */
337 xhci->awaiting_reset = 1;
339 /* Stick something in the shared communications ring. */
340 req = RING_GET_REQUEST(usb_ring, usb_ring->req_prod_pvt);
342 memset(req, 0, sizeof(*req));
344 req->operation = USBIF_OP_RESET;
345 req->port = port;
347 usb_ring->req_prod_pvt++;
348 RING_PUSH_REQUESTS(usb_ring);
350 spin_unlock_irq(&xhci->ring_lock);
352 notify_via_evtchn(xhci->evtchn);
354 while ( xhci->awaiting_reset > 0 )
355 {
356 mdelay(1);
357 xhci_drain_ring();
358 }
360 xhci->rh.ports[port].pe = 1;
361 xhci->rh.ports[port].pe_chg = 1;
363 return xhci->awaiting_reset;
364 }
367 /******************************************************************************
368 * RING RESPONSE HANDLING
369 */
371 static void receive_usb_reset(usbif_response_t *resp)
372 {
373 xhci->awaiting_reset = resp->status;
374 rmb();
376 }
378 static void receive_usb_probe(usbif_response_t *resp)
379 {
380 spin_lock(&xhci->rh.port_state_lock);
382 if ( resp->status >= 0 )
383 {
384 if ( resp->status == 1 )
385 {
386 /* If theres a device there and there wasn't one before there must
387 * have been a connection status change. */
388 if( xhci->rh.ports[resp->data].cs == 0 )
389 {
390 xhci->rh.ports[resp->data].cs = 1;
391 xhci->rh.ports[resp->data].cs_chg = 1;
392 }
393 }
394 else if ( resp->status == 0 )
395 {
396 if(xhci->rh.ports[resp->data].cs == 1 )
397 {
398 xhci->rh.ports[resp->data].cs = 0;
399 xhci->rh.ports[resp->data].cs_chg = 1;
400 xhci->rh.ports[resp->data].pe = 0;
401 /* According to USB Spec v2.0, 11.24.2.7.2.2, we don't need
402 * to set pe_chg since an error has not occurred. */
403 }
404 }
405 else
406 printk(KERN_WARNING "receive_usb_probe(): unexpected status %d "
407 "for port %d\n", resp->status, resp->data);
408 }
409 else if ( resp->status < 0)
410 printk(KERN_WARNING "receive_usb_probe(): got error status %d\n",
411 resp->status);
413 spin_unlock(&xhci->rh.port_state_lock);
414 }
416 static void receive_usb_io(usbif_response_t *resp)
417 {
418 struct urb_priv *urbp = (struct urb_priv *)resp->id;
419 struct urb *urb = urbp->urb;
421 urb->actual_length = resp->length;
422 urbp->in_progress = 0;
424 if( usb_pipetype(urb->pipe) == 0 ) /* ISO */
425 {
426 int i;
428 /* Copy ISO schedule results back in. */
429 for ( i = 0; i < urb->number_of_packets; i++ )
430 {
431 urb->iso_frame_desc[i].status
432 = urbp->schedule[i].status;
433 urb->iso_frame_desc[i].actual_length
434 = urbp->schedule[i].length;
435 }
436 free_page((unsigned long)urbp->schedule);
437 }
439 /* Only set status if it's not been changed since submission. It might
440 * have been changed if the URB has been unlinked asynchronously, for
441 * instance. */
442 if ( urb->status == -EINPROGRESS )
443 urbp->status = urb->status = resp->status;
444 }
446 /**
447 * xhci_drain_ring - drain responses from the ring, calling handlers
448 *
449 * This may be called from interrupt context when an event is received from the
450 * backend domain, or sometimes in process context whilst waiting for a port
451 * reset or URB completion.
452 */
453 static void xhci_drain_ring(void)
454 {
455 struct list_head *tmp, *head;
456 usbif_front_ring_t *usb_ring = &xhci->usb_ring;
457 usbif_response_t *resp;
458 RING_IDX i, rp;
460 /* Walk the ring here to get responses, updating URBs to show what
461 * completed. */
463 rp = usb_ring->sring->rsp_prod;
464 rmb(); /* Ensure we see queued requests up to 'rp'. */
466 /* Take items off the comms ring, taking care not to overflow. */
467 for ( i = usb_ring->rsp_cons; i != rp; i++ )
468 {
469 resp = RING_GET_RESPONSE(usb_ring, i);
471 /* May need to deal with batching and with putting a ceiling on
472 the number dispatched for performance and anti-dos reasons */
474 xhci_show_resp(resp);
476 switch ( resp->operation )
477 {
478 case USBIF_OP_PROBE:
479 receive_usb_probe(resp);
480 break;
482 case USBIF_OP_IO:
483 receive_usb_io(resp);
484 break;
486 case USBIF_OP_RESET:
487 receive_usb_reset(resp);
488 break;
490 default:
491 printk(KERN_WARNING
492 "error: unknown USB io operation response [%d]\n",
493 resp->operation);
494 break;
495 }
496 }
498 usb_ring->rsp_cons = i;
500 /* Walk the list of pending URB's to see which ones completed and do
501 * callbacks, etc. */
502 spin_lock(&xhci->urb_list_lock);
503 head = &xhci->urb_list;
504 tmp = head->next;
505 while (tmp != head) {
506 struct urb *urb = list_entry(tmp, struct urb, urb_list);
508 tmp = tmp->next;
510 /* Checks the status and does all of the magic necessary */
511 xhci_transfer_result(xhci, urb);
512 }
513 spin_unlock(&xhci->urb_list_lock);
515 xhci_finish_completion();
516 }
519 static void xhci_interrupt(int irq, void *__xhci, struct pt_regs *regs)
520 {
521 xhci_drain_ring();
522 }
524 /******************************************************************************
525 * HOST CONTROLLER FUNCTIONALITY
526 */
528 /**
529 * no-op implementation of private device alloc / free routines
530 */
531 static int xhci_do_nothing_dev(struct usb_device *dev)
532 {
533 return 0;
534 }
536 static inline void xhci_add_complete(struct urb *urb)
537 {
538 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
539 unsigned long flags;
541 spin_lock_irqsave(&xhci->complete_list_lock, flags);
542 list_add_tail(&urbp->complete_list, &xhci->complete_list);
543 spin_unlock_irqrestore(&xhci->complete_list_lock, flags);
544 }
546 /* When this returns, the owner of the URB may free its
547 * storage.
548 *
549 * We spin and wait for the URB to complete before returning.
550 *
551 * Call with urb->lock acquired.
552 */
553 static void xhci_delete_urb(struct urb *urb)
554 {
555 struct urb_priv *urbp;
557 urbp = urb->hcpriv;
559 /* If there's no urb_priv structure for this URB then it can't have
560 * been submitted at all. */
561 if ( urbp == NULL )
562 return;
564 /* For now we just spin until the URB completes. It shouldn't take too
565 * long and we don't expect to have to do this very often. */
566 while ( urb->status == -EINPROGRESS )
567 {
568 xhci_drain_ring();
569 mdelay(1);
570 }
572 /* Now we know that further transfers to the buffer won't
573 * occur, so we can safely return. */
574 }
576 static struct urb_priv *xhci_alloc_urb_priv(struct urb *urb)
577 {
578 struct urb_priv *urbp;
580 urbp = kmem_cache_alloc(xhci_up_cachep, SLAB_ATOMIC);
581 if (!urbp) {
582 err("xhci_alloc_urb_priv: couldn't allocate memory for urb_priv\n");
583 return NULL;
584 }
586 memset((void *)urbp, 0, sizeof(*urbp));
588 urbp->inserttime = jiffies;
589 urbp->urb = urb;
590 urbp->dev = urb->dev;
592 INIT_LIST_HEAD(&urbp->complete_list);
594 urb->hcpriv = urbp;
596 return urbp;
597 }
599 /*
600 * MUST be called with urb->lock acquired
601 */
602 /* When is this called? Do we need to stop the transfer (as we
603 * currently do)? */
604 static void xhci_destroy_urb_priv(struct urb *urb)
605 {
606 struct urb_priv *urbp;
608 urbp = (struct urb_priv *)urb->hcpriv;
609 if (!urbp)
610 return;
612 if (!list_empty(&urb->urb_list))
613 warn("xhci_destroy_urb_priv: urb %p still on xhci->urb_list", urb);
615 if (!list_empty(&urbp->complete_list))
616 warn("xhci_destroy_urb_priv: urb %p still on xhci->complete_list", urb);
618 kmem_cache_free(xhci_up_cachep, urb->hcpriv);
620 urb->hcpriv = NULL;
621 }
623 /**
624 * Try to find URBs in progress on the same pipe to the same device.
625 *
626 * MUST be called with xhci->urb_list_lock acquired
627 */
628 static struct urb *xhci_find_urb_ep(struct xhci *xhci, struct urb *urb)
629 {
630 struct list_head *tmp, *head;
632 /* We don't match Isoc transfers since they are special */
633 if (usb_pipeisoc(urb->pipe))
634 return NULL;
636 head = &xhci->urb_list;
637 tmp = head->next;
638 while (tmp != head) {
639 struct urb *u = list_entry(tmp, struct urb, urb_list);
641 tmp = tmp->next;
643 if (u->dev == urb->dev && u->pipe == urb->pipe &&
644 u->status == -EINPROGRESS)
645 return u;
646 }
648 return NULL;
649 }
651 static int xhci_submit_urb(struct urb *urb)
652 {
653 int ret = -EINVAL;
654 unsigned long flags;
655 struct urb *eurb;
656 int bustime;
658 DPRINTK("URB submitted to XHCI driver.\n");
659 dump_urb(urb);
661 if (!urb)
662 return -EINVAL;
664 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv) {
665 warn("xhci_submit_urb: urb %p belongs to disconnected device or bus?", urb);
666 return -ENODEV;
667 }
669 if ( urb->dev->devpath == NULL )
670 BUG();
672 usb_inc_dev_use(urb->dev);
674 spin_lock_irqsave(&xhci->urb_list_lock, flags);
675 spin_lock(&urb->lock);
677 if (urb->status == -EINPROGRESS || urb->status == -ECONNRESET ||
678 urb->status == -ECONNABORTED) {
679 dbg("xhci_submit_urb: urb not available to submit (status = %d)", urb->status);
680 /* Since we can have problems on the out path */
681 spin_unlock(&urb->lock);
682 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
683 usb_dec_dev_use(urb->dev);
685 return ret;
686 }
688 INIT_LIST_HEAD(&urb->urb_list);
689 if (!xhci_alloc_urb_priv(urb)) {
690 ret = -ENOMEM;
692 goto out;
693 }
695 ( (struct urb_priv *)urb->hcpriv )->in_progress = 1;
697 eurb = xhci_find_urb_ep(xhci, urb);
698 if (eurb && !(urb->transfer_flags & USB_QUEUE_BULK)) {
699 ret = -ENXIO;
701 goto out;
702 }
704 /* Short circuit the virtual root hub */
705 if (urb->dev == xhci->rh.dev) {
706 ret = rh_submit_urb(urb);
708 goto out;
709 }
711 switch (usb_pipetype(urb->pipe)) {
712 case PIPE_CONTROL:
713 case PIPE_BULK:
714 ret = xhci_queue_req(urb);
715 break;
717 case PIPE_INTERRUPT:
718 if (urb->bandwidth == 0) { /* not yet checked/allocated */
719 bustime = usb_check_bandwidth(urb->dev, urb);
720 if (bustime < 0)
721 ret = bustime;
722 else {
723 ret = xhci_queue_req(urb);
724 if (ret == -EINPROGRESS)
725 usb_claim_bandwidth(urb->dev, urb,
726 bustime, 0);
727 }
728 } else /* bandwidth is already set */
729 ret = xhci_queue_req(urb);
730 break;
732 case PIPE_ISOCHRONOUS:
733 if (urb->bandwidth == 0) { /* not yet checked/allocated */
734 if (urb->number_of_packets <= 0) {
735 ret = -EINVAL;
736 break;
737 }
738 bustime = usb_check_bandwidth(urb->dev, urb);
739 if (bustime < 0) {
740 ret = bustime;
741 break;
742 }
744 ret = xhci_queue_req(urb);
745 if (ret == -EINPROGRESS)
746 usb_claim_bandwidth(urb->dev, urb, bustime, 1);
747 } else /* bandwidth is already set */
748 ret = xhci_queue_req(urb);
749 break;
750 }
751 out:
752 urb->status = ret;
754 if (ret == -EINPROGRESS) {
755 /* We use _tail to make find_urb_ep more efficient */
756 list_add_tail(&urb->urb_list, &xhci->urb_list);
758 spin_unlock(&urb->lock);
759 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
761 return 0;
762 }
764 xhci_delete_urb(urb);
766 spin_unlock(&urb->lock);
767 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
769 /* Only call completion if it was successful */
770 if (!ret)
771 xhci_call_completion(urb);
773 return ret;
774 }
776 /*
777 * Return the result of a transfer
778 *
779 * MUST be called with urb_list_lock acquired
780 */
781 static void xhci_transfer_result(struct xhci *xhci, struct urb *urb)
782 {
783 int ret = 0;
784 unsigned long flags;
785 struct urb_priv *urbp;
787 /* The root hub is special */
788 if (urb->dev == xhci->rh.dev)
789 return;
791 spin_lock_irqsave(&urb->lock, flags);
793 urbp = (struct urb_priv *)urb->hcpriv;
795 if ( ( (struct urb_priv *)urb->hcpriv )->in_progress )
796 ret = -EINPROGRESS;
798 if (urb->actual_length < urb->transfer_buffer_length) {
799 if (urb->transfer_flags & USB_DISABLE_SPD) {
800 ret = -EREMOTEIO;
801 }
802 }
804 if (urb->status == -EPIPE)
805 {
806 ret = urb->status;
807 /* endpoint has stalled - mark it halted */
808 usb_endpoint_halt(urb->dev, usb_pipeendpoint(urb->pipe),
809 usb_pipeout(urb->pipe));
810 }
812 if ((debug == 1 && ret != 0 && ret != -EPIPE) ||
813 (ret != 0 && debug > 1)) {
814 /* Some debugging code */
815 dbg("xhci_result_interrupt/bulk() failed with status %x",
816 status);
817 }
819 if (ret == -EINPROGRESS)
820 goto out;
822 switch (usb_pipetype(urb->pipe)) {
823 case PIPE_CONTROL:
824 case PIPE_BULK:
825 case PIPE_ISOCHRONOUS:
826 /* Release bandwidth for Interrupt or Isoc. transfers */
827 /* Spinlock needed ? */
828 if (urb->bandwidth)
829 usb_release_bandwidth(urb->dev, urb, 1);
830 xhci_delete_urb(urb);
831 break;
832 case PIPE_INTERRUPT:
833 /* Interrupts are an exception */
834 if (urb->interval)
835 goto out_complete;
837 /* Release bandwidth for Interrupt or Isoc. transfers */
838 /* Spinlock needed ? */
839 if (urb->bandwidth)
840 usb_release_bandwidth(urb->dev, urb, 0);
841 xhci_delete_urb(urb);
842 break;
843 default:
844 info("xhci_transfer_result: unknown pipe type %d for urb %p\n",
845 usb_pipetype(urb->pipe), urb);
846 }
848 /* Remove it from xhci->urb_list */
849 list_del_init(&urb->urb_list);
851 out_complete:
852 xhci_add_complete(urb);
854 out:
855 spin_unlock_irqrestore(&urb->lock, flags);
856 }
858 static int xhci_unlink_urb(struct urb *urb)
859 {
860 unsigned long flags;
861 struct urb_priv *urbp = urb->hcpriv;
863 if (!urb)
864 return -EINVAL;
866 if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv)
867 return -ENODEV;
869 spin_lock_irqsave(&xhci->urb_list_lock, flags);
870 spin_lock(&urb->lock);
872 /* Release bandwidth for Interrupt or Isoc. transfers */
873 /* Spinlock needed ? */
874 if (urb->bandwidth) {
875 switch (usb_pipetype(urb->pipe)) {
876 case PIPE_INTERRUPT:
877 usb_release_bandwidth(urb->dev, urb, 0);
878 break;
879 case PIPE_ISOCHRONOUS:
880 usb_release_bandwidth(urb->dev, urb, 1);
881 break;
882 default:
883 break;
884 }
885 }
887 if (urb->status != -EINPROGRESS) {
888 spin_unlock(&urb->lock);
889 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
890 return 0;
891 }
893 list_del_init(&urb->urb_list);
895 /* Short circuit the virtual root hub */
896 if (urb->dev == xhci->rh.dev) {
897 rh_unlink_urb(urb);
899 spin_unlock(&urb->lock);
900 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
902 xhci_call_completion(urb);
903 } else {
904 if (urb->transfer_flags & USB_ASYNC_UNLINK) {
905 /* We currently don't currently attempt to cancel URBs
906 * that have been queued in the ring. We handle async
907 * unlinked URBs when they complete. */
908 urbp->status = urb->status = -ECONNABORTED;
909 spin_unlock(&urb->lock);
910 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
911 } else {
912 urb->status = -ENOENT;
914 spin_unlock(&urb->lock);
915 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
917 if (in_interrupt()) { /* wait at least 1 frame */
918 static int errorcount = 10;
920 if (errorcount--)
921 dbg("xhci_unlink_urb called from interrupt for urb %p", urb);
922 udelay(1000);
923 } else
924 schedule_timeout(1+1*HZ/1000);
926 xhci_delete_urb(urb);
928 xhci_call_completion(urb);
929 }
930 }
932 return 0;
933 }
935 static void xhci_call_completion(struct urb *urb)
936 {
937 struct urb_priv *urbp;
938 struct usb_device *dev = urb->dev;
939 int is_ring = 0, killed, resubmit_interrupt, status;
940 struct urb *nurb;
941 unsigned long flags;
943 spin_lock_irqsave(&urb->lock, flags);
945 urbp = (struct urb_priv *)urb->hcpriv;
946 if (!urbp || !urb->dev) {
947 spin_unlock_irqrestore(&urb->lock, flags);
948 return;
949 }
951 killed = (urb->status == -ENOENT || urb->status == -ECONNABORTED ||
952 urb->status == -ECONNRESET);
953 resubmit_interrupt = (usb_pipetype(urb->pipe) == PIPE_INTERRUPT &&
954 urb->interval);
956 nurb = urb->next;
957 if (nurb && !killed) {
958 int count = 0;
960 while (nurb && nurb != urb && count < MAX_URB_LOOP) {
961 if (nurb->status == -ENOENT ||
962 nurb->status == -ECONNABORTED ||
963 nurb->status == -ECONNRESET) {
964 killed = 1;
965 break;
966 }
968 nurb = nurb->next;
969 count++;
970 }
972 if (count == MAX_URB_LOOP)
973 err("xhci_call_completion: too many linked URB's, loop? (first loop)");
975 /* Check to see if chain is a ring */
976 is_ring = (nurb == urb);
977 }
979 status = urbp->status;
980 if (!resubmit_interrupt || killed)
981 /* We don't need urb_priv anymore */
982 xhci_destroy_urb_priv(urb);
984 if (!killed)
985 urb->status = status;
987 spin_unlock_irqrestore(&urb->lock, flags);
989 if (urb->complete)
990 urb->complete(urb);
992 if (resubmit_interrupt)
993 /* Recheck the status. The completion handler may have */
994 /* unlinked the resubmitting interrupt URB */
995 killed = (urb->status == -ENOENT ||
996 urb->status == -ECONNABORTED ||
997 urb->status == -ECONNRESET);
999 if (resubmit_interrupt && !killed) {
1000 if ( urb->dev != xhci->rh.dev )
1001 xhci_queue_req(urb); /* XXX What if this fails? */
1002 /* Don't need to resubmit URBs for the virtual root dev. */
1003 } else {
1004 if (is_ring && !killed) {
1005 urb->dev = dev;
1006 xhci_submit_urb(urb);
1007 } else {
1008 /* We decrement the usage count after we're done */
1009 /* with everything */
1010 usb_dec_dev_use(dev);
1015 static void xhci_finish_completion(void)
1017 struct list_head *tmp, *head;
1018 unsigned long flags;
1020 spin_lock_irqsave(&xhci->complete_list_lock, flags);
1021 head = &xhci->complete_list;
1022 tmp = head->next;
1023 while (tmp != head) {
1024 struct urb_priv *urbp = list_entry(tmp, struct urb_priv,
1025 complete_list);
1026 struct urb *urb = urbp->urb;
1028 list_del_init(&urbp->complete_list);
1029 spin_unlock_irqrestore(&xhci->complete_list_lock, flags);
1031 xhci_call_completion(urb);
1033 spin_lock_irqsave(&xhci->complete_list_lock, flags);
1034 head = &xhci->complete_list;
1035 tmp = head->next;
1037 spin_unlock_irqrestore(&xhci->complete_list_lock, flags);
1040 static struct usb_operations xhci_device_operations = {
1041 .allocate = xhci_do_nothing_dev,
1042 .deallocate = xhci_do_nothing_dev,
1043 /* It doesn't look like any drivers actually care what the frame number
1044 * is at the moment! If necessary, we could approximate the current
1045 * frame nubmer by passing it from the backend in response messages. */
1046 .get_frame_number = NULL,
1047 .submit_urb = xhci_submit_urb,
1048 .unlink_urb = xhci_unlink_urb
1049 };
1051 /******************************************************************************
1052 * VIRTUAL ROOT HUB EMULATION
1053 */
1055 static __u8 root_hub_dev_des[] =
1057 0x12, /* __u8 bLength; */
1058 0x01, /* __u8 bDescriptorType; Device */
1059 0x00, /* __u16 bcdUSB; v1.0 */
1060 0x01,
1061 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */
1062 0x00, /* __u8 bDeviceSubClass; */
1063 0x00, /* __u8 bDeviceProtocol; */
1064 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */
1065 0x00, /* __u16 idVendor; */
1066 0x00,
1067 0x00, /* __u16 idProduct; */
1068 0x00,
1069 0x00, /* __u16 bcdDevice; */
1070 0x00,
1071 0x00, /* __u8 iManufacturer; */
1072 0x02, /* __u8 iProduct; */
1073 0x01, /* __u8 iSerialNumber; */
1074 0x01 /* __u8 bNumConfigurations; */
1075 };
1078 /* Configuration descriptor */
1079 static __u8 root_hub_config_des[] =
1081 0x09, /* __u8 bLength; */
1082 0x02, /* __u8 bDescriptorType; Configuration */
1083 0x19, /* __u16 wTotalLength; */
1084 0x00,
1085 0x01, /* __u8 bNumInterfaces; */
1086 0x01, /* __u8 bConfigurationValue; */
1087 0x00, /* __u8 iConfiguration; */
1088 0x40, /* __u8 bmAttributes;
1089 Bit 7: Bus-powered, 6: Self-powered,
1090 Bit 5 Remote-wakeup, 4..0: resvd */
1091 0x00, /* __u8 MaxPower; */
1093 /* interface */
1094 0x09, /* __u8 if_bLength; */
1095 0x04, /* __u8 if_bDescriptorType; Interface */
1096 0x00, /* __u8 if_bInterfaceNumber; */
1097 0x00, /* __u8 if_bAlternateSetting; */
1098 0x01, /* __u8 if_bNumEndpoints; */
1099 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */
1100 0x00, /* __u8 if_bInterfaceSubClass; */
1101 0x00, /* __u8 if_bInterfaceProtocol; */
1102 0x00, /* __u8 if_iInterface; */
1104 /* endpoint */
1105 0x07, /* __u8 ep_bLength; */
1106 0x05, /* __u8 ep_bDescriptorType; Endpoint */
1107 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */
1108 0x03, /* __u8 ep_bmAttributes; Interrupt */
1109 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */
1110 0x00,
1111 0xff /* __u8 ep_bInterval; 255 ms */
1112 };
1114 static __u8 root_hub_hub_des[] =
1116 0x09, /* __u8 bLength; */
1117 0x29, /* __u8 bDescriptorType; Hub-descriptor */
1118 0x02, /* __u8 bNbrPorts; */
1119 0x00, /* __u16 wHubCharacteristics; */
1120 0x00,
1121 0x01, /* __u8 bPwrOn2pwrGood; 2ms */
1122 0x00, /* __u8 bHubContrCurrent; 0 mA */
1123 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */
1124 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */
1125 };
1127 /* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */
1128 static int rh_send_irq(struct urb *urb)
1130 struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv;
1131 xhci_port_t *ports = xhci->rh.ports;
1132 unsigned long flags;
1133 int i, len = 1;
1134 __u16 data = 0;
1136 spin_lock_irqsave(&urb->lock, flags);
1137 for (i = 0; i < xhci->rh.numports; i++) {
1138 /* Set a bit if anything at all has changed on the port, as per
1139 * USB spec 11.12 */
1140 data |= (ports[i].cs_chg || ports[i].pe_chg )
1141 ? (1 << (i + 1))
1142 : 0;
1144 len = (i + 1) / 8 + 1;
1147 *(__u16 *) urb->transfer_buffer = cpu_to_le16(data);
1148 urb->actual_length = len;
1149 urbp->status = 0;
1151 spin_unlock_irqrestore(&urb->lock, flags);
1153 if ((data > 0) && (xhci->rh.send != 0)) {
1154 dbg("root-hub INT complete: data: %x", data);
1155 xhci_call_completion(urb);
1158 return 0;
1161 /* Virtual Root Hub INTs are polled by this timer every "interval" ms */
1162 static int rh_init_int_timer(struct urb *urb);
1164 static void rh_int_timer_do(unsigned long ptr)
1166 struct urb *urb = (struct urb *)ptr;
1167 struct list_head list, *tmp, *head;
1168 unsigned long flags;
1169 int i;
1171 for ( i = 0; i < xhci->rh.numports; i++)
1172 xhci_queue_probe(i);
1174 if (xhci->rh.send)
1175 rh_send_irq(urb);
1177 INIT_LIST_HEAD(&list);
1179 spin_lock_irqsave(&xhci->urb_list_lock, flags);
1180 head = &xhci->urb_list;
1181 tmp = head->next;
1182 while (tmp != head) {
1183 struct urb *u = list_entry(tmp, struct urb, urb_list);
1184 struct urb_priv *up = (struct urb_priv *)u->hcpriv;
1186 tmp = tmp->next;
1188 spin_lock(&u->lock);
1190 /* Check if the URB timed out */
1191 if (u->timeout && time_after_eq(jiffies,
1192 up->inserttime + u->timeout)) {
1193 list_del(&u->urb_list);
1194 list_add_tail(&u->urb_list, &list);
1197 spin_unlock(&u->lock);
1199 spin_unlock_irqrestore(&xhci->urb_list_lock, flags);
1201 head = &list;
1202 tmp = head->next;
1203 while (tmp != head) {
1204 struct urb *u = list_entry(tmp, struct urb, urb_list);
1206 tmp = tmp->next;
1208 u->transfer_flags |= USB_ASYNC_UNLINK | USB_TIMEOUT_KILLED;
1209 xhci_unlink_urb(u);
1212 rh_init_int_timer(urb);
1215 /* Root Hub INTs are polled by this timer */
1216 static int rh_init_int_timer(struct urb *urb)
1218 xhci->rh.interval = urb->interval;
1219 init_timer(&xhci->rh.rh_int_timer);
1220 xhci->rh.rh_int_timer.function = rh_int_timer_do;
1221 xhci->rh.rh_int_timer.data = (unsigned long)urb;
1222 xhci->rh.rh_int_timer.expires = jiffies
1223 + (HZ * (urb->interval < 30 ? 30 : urb->interval)) / 1000;
1224 add_timer(&xhci->rh.rh_int_timer);
1226 return 0;
1229 #define OK(x) len = (x); break
1231 /* Root Hub Control Pipe */
1232 static int rh_submit_urb(struct urb *urb)
1234 unsigned int pipe = urb->pipe;
1235 struct usb_ctrlrequest *cmd =
1236 (struct usb_ctrlrequest *)urb->setup_packet;
1237 void *data = urb->transfer_buffer;
1238 int leni = urb->transfer_buffer_length;
1239 int len = 0;
1240 xhci_port_t *status;
1241 int stat = 0;
1242 int i;
1243 int retstatus;
1244 unsigned long flags;
1246 __u16 cstatus;
1247 __u16 bmRType_bReq;
1248 __u16 wValue;
1249 __u16 wIndex;
1250 __u16 wLength;
1252 if (usb_pipetype(pipe) == PIPE_INTERRUPT) {
1253 xhci->rh.urb = urb;
1254 xhci->rh.send = 1;
1255 xhci->rh.interval = urb->interval;
1256 rh_init_int_timer(urb);
1258 return -EINPROGRESS;
1261 bmRType_bReq = cmd->bRequestType | cmd->bRequest << 8;
1262 wValue = le16_to_cpu(cmd->wValue);
1263 wIndex = le16_to_cpu(cmd->wIndex);
1264 wLength = le16_to_cpu(cmd->wLength);
1266 for (i = 0; i < 8; i++)
1267 xhci->rh.c_p_r[i] = 0;
1269 status = &xhci->rh.ports[wIndex - 1];
1271 spin_lock_irqsave(&xhci->rh.port_state_lock, flags);
1273 switch (bmRType_bReq) {
1274 /* Request Destination:
1275 without flags: Device,
1276 RH_INTERFACE: interface,
1277 RH_ENDPOINT: endpoint,
1278 RH_CLASS means HUB here,
1279 RH_OTHER | RH_CLASS almost ever means HUB_PORT here
1280 */
1282 case RH_GET_STATUS:
1283 *(__u16 *)data = cpu_to_le16(1);
1284 OK(2);
1285 case RH_GET_STATUS | RH_INTERFACE:
1286 *(__u16 *)data = cpu_to_le16(0);
1287 OK(2);
1288 case RH_GET_STATUS | RH_ENDPOINT:
1289 *(__u16 *)data = cpu_to_le16(0);
1290 OK(2);
1291 case RH_GET_STATUS | RH_CLASS:
1292 *(__u32 *)data = cpu_to_le32(0);
1293 OK(4); /* hub power */
1294 case RH_GET_STATUS | RH_OTHER | RH_CLASS:
1295 cstatus = (status->cs_chg) |
1296 (status->pe_chg << 1) |
1297 (xhci->rh.c_p_r[wIndex - 1] << 4);
1298 retstatus = (status->cs) |
1299 (status->pe << 1) |
1300 (status->susp << 2) |
1301 (1 << 8) | /* power on */
1302 (status->lsda << 9);
1303 *(__u16 *)data = cpu_to_le16(retstatus);
1304 *(__u16 *)(data + 2) = cpu_to_le16(cstatus);
1305 OK(4);
1306 case RH_CLEAR_FEATURE | RH_ENDPOINT:
1307 switch (wValue) {
1308 case RH_ENDPOINT_STALL:
1309 OK(0);
1311 break;
1312 case RH_CLEAR_FEATURE | RH_CLASS:
1313 switch (wValue) {
1314 case RH_C_HUB_OVER_CURRENT:
1315 OK(0); /* hub power over current */
1317 break;
1318 case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS:
1319 switch (wValue) {
1320 case RH_PORT_ENABLE:
1321 status->pe = 0;
1322 OK(0);
1323 case RH_PORT_SUSPEND:
1324 status->susp = 0;
1325 OK(0);
1326 case RH_PORT_POWER:
1327 OK(0); /* port power */
1328 case RH_C_PORT_CONNECTION:
1329 status->cs_chg = 0;
1330 OK(0);
1331 case RH_C_PORT_ENABLE:
1332 status->pe_chg = 0;
1333 OK(0);
1334 case RH_C_PORT_SUSPEND:
1335 /*** WR_RH_PORTSTAT(RH_PS_PSSC); */
1336 OK(0);
1337 case RH_C_PORT_OVER_CURRENT:
1338 OK(0); /* port power over current */
1339 case RH_C_PORT_RESET:
1340 xhci->rh.c_p_r[wIndex - 1] = 0;
1341 OK(0);
1343 break;
1344 case RH_SET_FEATURE | RH_OTHER | RH_CLASS:
1345 switch (wValue) {
1346 case RH_PORT_SUSPEND:
1347 status->susp = 1;
1348 OK(0);
1349 case RH_PORT_RESET:
1351 int ret;
1352 xhci->rh.c_p_r[wIndex - 1] = 1;
1353 status->pr = 0;
1354 status->pe = 1;
1355 ret = xhci_port_reset(wIndex - 1);
1356 /* XXX MAW: should probably cancel queued transfers during reset... *\/ */
1357 if ( ret == 0 ) { OK(0); }
1358 else { return ret; }
1360 break;
1361 case RH_PORT_POWER:
1362 OK(0); /* port power ** */
1363 case RH_PORT_ENABLE:
1364 status->pe = 1;
1365 OK(0);
1367 break;
1368 case RH_SET_ADDRESS:
1369 xhci->rh.devnum = wValue;
1370 OK(0);
1371 case RH_GET_DESCRIPTOR:
1372 switch ((wValue & 0xff00) >> 8) {
1373 case 0x01: /* device descriptor */
1374 len = min_t(unsigned int, leni,
1375 min_t(unsigned int,
1376 sizeof(root_hub_dev_des), wLength));
1377 memcpy(data, root_hub_dev_des, len);
1378 OK(len);
1379 case 0x02: /* configuration descriptor */
1380 len = min_t(unsigned int, leni,
1381 min_t(unsigned int,
1382 sizeof(root_hub_config_des), wLength));
1383 memcpy (data, root_hub_config_des, len);
1384 OK(len);
1385 case 0x03: /* string descriptors */
1386 len = usb_root_hub_string (wValue & 0xff,
1387 0, "XHCI-alt",
1388 data, wLength);
1389 if (len > 0) {
1390 OK(min_t(int, leni, len));
1391 } else
1392 stat = -EPIPE;
1394 break;
1395 case RH_GET_DESCRIPTOR | RH_CLASS:
1396 root_hub_hub_des[2] = xhci->rh.numports;
1397 len = min_t(unsigned int, leni,
1398 min_t(unsigned int, sizeof(root_hub_hub_des), wLength));
1399 memcpy(data, root_hub_hub_des, len);
1400 OK(len);
1401 case RH_GET_CONFIGURATION:
1402 *(__u8 *)data = 0x01;
1403 OK(1);
1404 case RH_SET_CONFIGURATION:
1405 OK(0);
1406 case RH_GET_INTERFACE | RH_INTERFACE:
1407 *(__u8 *)data = 0x00;
1408 OK(1);
1409 case RH_SET_INTERFACE | RH_INTERFACE:
1410 OK(0);
1411 default:
1412 stat = -EPIPE;
1415 spin_unlock_irqrestore(&xhci->rh.port_state_lock, flags);
1417 urb->actual_length = len;
1419 return stat;
1422 /*
1423 * MUST be called with urb->lock acquired
1424 */
1425 static int rh_unlink_urb(struct urb *urb)
1427 if (xhci->rh.urb == urb) {
1428 urb->status = -ENOENT;
1429 xhci->rh.send = 0;
1430 xhci->rh.urb = NULL;
1431 del_timer(&xhci->rh.rh_int_timer);
1433 return 0;
1436 /******************************************************************************
1437 * CONTROL PLANE FUNCTIONALITY
1438 */
1440 /**
1441 * alloc_xhci - initialise a new virtual root hub for a new USB device channel
1442 */
1443 static int alloc_xhci(void)
1445 int retval;
1446 struct usb_bus *bus;
1448 retval = -EBUSY;
1450 xhci = kmalloc(sizeof(*xhci), GFP_KERNEL);
1451 if (!xhci) {
1452 err("couldn't allocate xhci structure");
1453 retval = -ENOMEM;
1454 goto err_alloc_xhci;
1457 xhci->state = USBIF_STATE_CLOSED;
1459 spin_lock_init(&xhci->urb_list_lock);
1460 INIT_LIST_HEAD(&xhci->urb_list);
1462 spin_lock_init(&xhci->complete_list_lock);
1463 INIT_LIST_HEAD(&xhci->complete_list);
1465 spin_lock_init(&xhci->frame_list_lock);
1467 bus = usb_alloc_bus(&xhci_device_operations);
1469 if (!bus) {
1470 err("unable to allocate bus");
1471 goto err_alloc_bus;
1474 xhci->bus = bus;
1475 bus->bus_name = "XHCI";
1476 bus->hcpriv = xhci;
1478 usb_register_bus(xhci->bus);
1480 /* Initialize the root hub */
1482 xhci->rh.numports = 0;
1484 xhci->bus->root_hub = xhci->rh.dev = usb_alloc_dev(NULL, xhci->bus);
1485 if (!xhci->rh.dev) {
1486 err("unable to allocate root hub");
1487 goto err_alloc_root_hub;
1490 xhci->state = 0;
1492 return 0;
1494 /*
1495 * error exits:
1496 */
1497 err_alloc_root_hub:
1498 usb_deregister_bus(xhci->bus);
1499 usb_free_bus(xhci->bus);
1500 xhci->bus = NULL;
1502 err_alloc_bus:
1503 kfree(xhci);
1505 err_alloc_xhci:
1506 return retval;
1509 /**
1510 * usbif_status_change - deal with an incoming USB_INTERFACE_STATUS_ message
1511 */
1512 static void usbif_status_change(usbif_fe_interface_status_changed_t *status)
1514 ctrl_msg_t cmsg;
1515 usbif_fe_interface_connect_t up;
1516 long rc;
1517 usbif_sring_t *sring;
1519 switch ( status->status )
1521 case USBIF_INTERFACE_STATUS_DESTROYED:
1522 printk(KERN_WARNING "Unexpected usbif-DESTROYED message in state %d\n",
1523 xhci->state);
1524 break;
1526 case USBIF_INTERFACE_STATUS_DISCONNECTED:
1527 if ( xhci->state != USBIF_STATE_CLOSED )
1529 printk(KERN_WARNING "Unexpected usbif-DISCONNECTED message"
1530 " in state %d\n", xhci->state);
1531 break;
1532 /* Not bothering to do recovery here for now. Keep things
1533 * simple. */
1535 spin_lock_irq(&xhci->ring_lock);
1537 /* Clean up resources. */
1538 free_page((unsigned long)xhci->usb_ring.sring);
1539 unbind_evtchn_from_irqhandler(xhci->evtchn, xhci);
1541 /* Plug the ring. */
1542 xhci->recovery = 1;
1543 wmb();
1545 spin_unlock_irq(&xhci->ring_lock);
1548 /* Move from CLOSED to DISCONNECTED state. */
1549 sring = (usbif_sring_t *)__get_free_page(GFP_KERNEL);
1550 SHARED_RING_INIT(sring);
1551 FRONT_RING_INIT(&xhci->usb_ring, sring, PAGE_SIZE);
1552 xhci->state = USBIF_STATE_DISCONNECTED;
1554 /* Construct an interface-CONNECT message for the domain controller. */
1555 cmsg.type = CMSG_USBIF_FE;
1556 cmsg.subtype = CMSG_USBIF_FE_INTERFACE_CONNECT;
1557 cmsg.length = sizeof(usbif_fe_interface_connect_t);
1558 up.shmem_frame = virt_to_mfn(sring);
1559 memcpy(cmsg.msg, &up, sizeof(up));
1561 /* Tell the controller to bring up the interface. */
1562 ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
1563 break;
1565 case USBIF_INTERFACE_STATUS_CONNECTED:
1566 if ( xhci->state == USBIF_STATE_CLOSED )
1568 printk(KERN_WARNING "Unexpected usbif-CONNECTED message"
1569 " in state %d\n", xhci->state);
1570 break;
1573 xhci->evtchn = status->evtchn;
1574 xhci->bandwidth = status->bandwidth;
1575 xhci->rh.numports = status->num_ports;
1577 xhci->rh.ports = kmalloc (sizeof(xhci_port_t) * xhci->rh.numports, GFP_KERNEL);
1579 if ( xhci->rh.ports == NULL )
1580 goto alloc_ports_nomem;
1582 memset(xhci->rh.ports, 0, sizeof(xhci_port_t) * xhci->rh.numports);
1584 usb_connect(xhci->rh.dev);
1586 if (usb_new_device(xhci->rh.dev) != 0) {
1587 err("unable to start root hub");
1590 /* Allocate the appropriate USB bandwidth here... Need to
1591 * somehow know what the total available is thought to be so we
1592 * can calculate the reservation correctly. */
1593 usb_claim_bandwidth(xhci->rh.dev, xhci->rh.urb,
1594 1000 - xhci->bandwidth, 0);
1596 if ( (rc = bind_evtchn_to_irqhandler(xhci->evtchn, xhci_interrupt,
1597 SA_SAMPLE_RANDOM, "usbif", xhci)) )
1598 printk(KERN_ALERT"usbfront request_irq failed (%ld)\n",rc);
1600 DPRINTK(KERN_INFO __FILE__
1601 ": USB XHCI: SHM at %p (0x%lx), EVTCHN %d\n",
1602 xhci->usb_ring.sring, virt_to_mfn(xhci->usbif),
1603 xhci->evtchn);
1605 xhci->state = USBIF_STATE_CONNECTED;
1607 break;
1609 default:
1610 printk(KERN_WARNING "Status change to unknown value %d\n",
1611 status->status);
1612 break;
1615 return;
1617 alloc_ports_nomem:
1618 printk(KERN_WARNING "Failed to allocate port memory, XHCI failed to connect.\n");
1619 return;
1622 /**
1623 * usbif_ctrlif_rx - demux control messages by subtype
1624 */
1625 static void usbif_ctrlif_rx(ctrl_msg_t *msg, unsigned long id)
1627 switch ( msg->subtype )
1629 case CMSG_USBIF_FE_INTERFACE_STATUS_CHANGED:
1630 usbif_status_change((usbif_fe_interface_status_changed_t *)
1631 &msg->msg[0]);
1632 break;
1634 /* New interface...? */
1635 default:
1636 msg->length = 0;
1637 break;
1640 ctrl_if_send_response(msg);
1643 static void send_driver_up(void)
1645 control_msg_t cmsg;
1646 usbif_fe_interface_status_changed_t st;
1648 /* Send a driver-UP notification to the domain controller. */
1649 cmsg.type = CMSG_USBIF_FE;
1650 cmsg.subtype = CMSG_USBIF_FE_DRIVER_STATUS_CHANGED;
1651 cmsg.length = sizeof(usbif_fe_driver_status_changed_t);
1652 st.status = USBIF_DRIVER_STATUS_UP;
1653 memcpy(cmsg.msg, &st, sizeof(st));
1654 ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE);
1657 void usbif_resume(void)
1659 int i;
1661 /* Fake disconnection on all virtual USB ports (suspending / migrating
1662 * will destroy hard state associated will the USB devices anyhow). */
1663 /* No need to lock here. */
1664 for ( i = 0; i < xhci->rh.numports; i++ )
1666 xhci->rh.ports[i].cs = 0;
1667 xhci->rh.ports[i].cs_chg = 1;
1668 xhci->rh.ports[i].pe = 0;
1671 send_driver_up();
1674 static int __init xhci_hcd_init(void)
1676 int retval = -ENOMEM, i;
1678 if ( (xen_start_info.flags & SIF_INITDOMAIN)
1679 || (xen_start_info.flags & SIF_USB_BE_DOMAIN) )
1680 return 0;
1682 info(DRIVER_DESC " " DRIVER_VERSION);
1684 if (debug) {
1685 errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL);
1686 if (!errbuf)
1687 goto errbuf_failed;
1690 xhci_up_cachep = kmem_cache_create("xhci_urb_priv",
1691 sizeof(struct urb_priv), 0, 0, NULL, NULL);
1692 if (!xhci_up_cachep)
1693 goto up_failed;
1695 /* Let the domain controller know we're here. For now we wait until
1696 * connection, as for the block and net drivers. This is only strictly
1697 * necessary if we're going to boot off a USB device. */
1698 printk(KERN_INFO "Initialising Xen virtual USB hub\n");
1700 (void)ctrl_if_register_receiver(CMSG_USBIF_FE, usbif_ctrlif_rx,
1701 CALLBACK_IN_BLOCKING_CONTEXT);
1703 alloc_xhci();
1705 send_driver_up();
1707 /*
1708 * We should read 'nr_interfaces' from response message and wait
1709 * for notifications before proceeding. For now we assume that we
1710 * will be notified of exactly one interface.
1711 */
1712 for ( i=0; (xhci->state != USBIF_STATE_CONNECTED) && (i < 10*HZ); i++ )
1714 set_current_state(TASK_INTERRUPTIBLE);
1715 schedule_timeout(1);
1718 if (xhci->state != USBIF_STATE_CONNECTED)
1719 printk(KERN_WARNING "Timeout connecting USB frontend driver!\n");
1721 return 0;
1723 up_failed:
1724 if (errbuf)
1725 kfree(errbuf);
1727 errbuf_failed:
1728 return retval;
1731 module_init(xhci_hcd_init);
1733 MODULE_AUTHOR(DRIVER_AUTHOR);
1734 MODULE_DESCRIPTION(DRIVER_DESC);
1735 MODULE_LICENSE("GPL");