1 /******************************************************************************
2 * arch/xen/drivers/usbif/backend/main.c
4 * Backend for the Xen virtual USB driver - provides an abstraction of a
5 * USB host controller to the corresponding frontend driver.
8 * Copyright (c) 2004 Intel Research Cambridge
9 * Copyright (c) 2004, 2005 Mark Williamson
11 * Based on arch/xen/drivers/blkif/backend/main.c
12 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
18 #include <linux/list.h>
19 #include <linux/usb.h>
20 #include <linux/spinlock.h>
21 #include <linux/module.h>
22 #include <linux/tqueue.h>
25 * This is rather arbitrary.
27 #define MAX_PENDING_REQS 4
28 #define BATCH_PER_DOMAIN 1
30 static unsigned long mmap_vstart;
32 /* Needs to be sufficiently large that we can map the (large) buffers
33 * the USB mass storage driver wants. */
34 #define MMAP_PAGES_PER_REQUEST \
37 (MAX_PENDING_REQS * MMAP_PAGES_PER_REQUEST)
39 #define MMAP_VADDR(_req,_seg) \
41 ((_req) * MMAP_PAGES_PER_REQUEST * PAGE_SIZE) + \
45 static spinlock_t owned_ports_lock;
46 LIST_HEAD(owned_ports);
48 /* A list of these structures is used to track ownership of physical USB
52 usbif_priv_t *usbif_priv;
56 struct list_head list;
57 unsigned long guest_address; /* The USB device address that has been
58 * assigned by the guest. */
59 int dev_present; /* Is there a device present? */
60 struct usb_device * dev;
61 unsigned long ifaces; /* What interfaces are present on this device? */
66 * Each outstanding request that we've passed to the lower device layers has a
67 * 'pending_req' allocated to it. The request is complete, the specified
68 * domain has a response queued for it, with the saved 'id' passed back.
71 usbif_priv_t *usbif_priv;
74 unsigned short operation;
79 * We can't allocate pending_req's in order, since they may complete out of
80 * order. We therefore maintain an allocation ring. This ring also indicates
81 * when enough work has been passed down -- at that point the allocation ring
84 static pending_req_t pending_reqs[MAX_PENDING_REQS];
85 static unsigned char pending_ring[MAX_PENDING_REQS];
86 static spinlock_t pend_prod_lock;
88 /* NB. We use a different index type to differentiate from shared usb rings. */
89 typedef unsigned int PEND_RING_IDX;
90 #define MASK_PEND_IDX(_i) ((_i)&(MAX_PENDING_REQS-1))
91 static PEND_RING_IDX pending_prod, pending_cons;
92 #define NR_PENDING_REQS (MAX_PENDING_REQS - pending_prod + pending_cons)
94 static int do_usb_io_op(usbif_priv_t *usbif, int max_to_do);
95 static void make_response(usbif_priv_t *usbif, unsigned long id,
96 unsigned short op, int st, int inband,
97 unsigned long actual_length);
98 static void dispatch_usb_probe(usbif_priv_t *up, unsigned long id, unsigned long port);
99 static void dispatch_usb_io(usbif_priv_t *up, usbif_request_t *req);
100 static void dispatch_usb_reset(usbif_priv_t *up, unsigned long portid);
101 static owned_port_t *usbif_find_port(char *);
103 /******************************************************************
104 * PRIVATE DEBUG FUNCTIONS
110 static void dump_port(owned_port_t *p)
112 printk(KERN_DEBUG "owned_port_t @ %p\n"
116 " guest_address: %ld\n"
120 p, p->usbif_priv, p->path, p->guest_port, p->guest_address,
121 p->dev_present, p->dev, p->ifaces);
125 static void dump_request(usbif_request_t *req)
127 printk(KERN_DEBUG "id = 0x%lx\n"
133 "transfer_buffer 0x%lx\n"
135 "transfer_flags 0x%lx\n"
136 "setup = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x }\n"
137 "iso_schedule = 0x%lx\n"
139 req->id, req->devnum, req->endpoint, req->direction, req->speed,
140 req->pipe_type, req->transfer_buffer, req->length,
141 req->transfer_flags, req->setup[0], req->setup[1], req->setup[2],
142 req->setup[3], req->setup[4], req->setup[5], req->setup[6],
143 req->setup[7], req->iso_schedule, req->num_iso);
146 static void dump_urb(struct urb *urb)
148 printk(KERN_DEBUG "dumping urb @ %p\n", urb);
150 #define DUMP_URB_FIELD(name, format) \
151 printk(KERN_DEBUG " " # name " " format "\n", urb-> name)
153 DUMP_URB_FIELD(pipe, "0x%x");
154 DUMP_URB_FIELD(status, "%d");
155 DUMP_URB_FIELD(transfer_flags, "0x%x");
156 DUMP_URB_FIELD(transfer_buffer, "%p");
157 DUMP_URB_FIELD(transfer_buffer_length, "%d");
158 DUMP_URB_FIELD(actual_length, "%d");
161 static void dump_response(usbif_response_t *resp)
163 printk(KERN_DEBUG "usbback: Sending response:\n"
169 resp->id, resp->op, resp->status, resp->data, resp->length);
174 #define dump_port(blah) ((void)0)
175 #define dump_request(blah) ((void)0)
176 #define dump_urb(blah) ((void)0)
177 #define dump_response(blah) ((void)0)
181 /******************************************************************
185 static void fast_flush_area(int idx, int nr_pages)
187 multicall_entry_t mcl[MMAP_PAGES_PER_REQUEST];
190 for ( i = 0; i < nr_pages; i++ )
192 mcl[i].op = __HYPERVISOR_update_va_mapping;
193 mcl[i].args[0] = MMAP_VADDR(idx, i);
198 mcl[nr_pages-1].args[2] = UVMF_TLB_FLUSH|UVMF_ALL;
199 if ( unlikely(HYPERVISOR_multicall(mcl, nr_pages) != 0) )
204 /******************************************************************
205 * USB INTERFACE SCHEDULER LIST MAINTENANCE
208 static struct list_head usbio_schedule_list;
209 static spinlock_t usbio_schedule_list_lock;
211 static int __on_usbif_list(usbif_priv_t *up)
213 return up->usbif_list.next != NULL;
216 void remove_from_usbif_list(usbif_priv_t *up)
219 if ( !__on_usbif_list(up) ) return;
220 spin_lock_irqsave(&usbio_schedule_list_lock, flags);
221 if ( __on_usbif_list(up) )
223 list_del(&up->usbif_list);
224 up->usbif_list.next = NULL;
227 spin_unlock_irqrestore(&usbio_schedule_list_lock, flags);
230 static void add_to_usbif_list_tail(usbif_priv_t *up)
233 if ( __on_usbif_list(up) ) return;
234 spin_lock_irqsave(&usbio_schedule_list_lock, flags);
235 if ( !__on_usbif_list(up) && (up->status == CONNECTED) )
237 list_add_tail(&up->usbif_list, &usbio_schedule_list);
240 spin_unlock_irqrestore(&usbio_schedule_list_lock, flags);
243 void free_pending(int pending_idx)
247 /* Free the pending request. */
248 spin_lock_irqsave(&pend_prod_lock, flags);
249 pending_ring[MASK_PEND_IDX(pending_prod++)] = pending_idx;
250 spin_unlock_irqrestore(&pend_prod_lock, flags);
253 /******************************************************************
254 * COMPLETION CALLBACK -- Called as urb->complete()
257 static void maybe_trigger_usbio_schedule(void);
259 static void __end_usb_io_op(struct urb *purb)
261 pending_req_t *pending_req;
264 pending_req = purb->context;
266 pending_idx = pending_req - pending_reqs;
268 ASSERT(purb->actual_length <= purb->transfer_buffer_length);
269 ASSERT(purb->actual_length <= pending_req->nr_pages * PAGE_SIZE);
271 /* An error fails the entire request. */
274 printk(KERN_WARNING "URB @ %p failed. Status %d\n", purb, purb->status);
277 if ( usb_pipetype(purb->pipe) == 0 )
280 usbif_iso_t *sched = (usbif_iso_t *)MMAP_VADDR(pending_idx, pending_req->nr_pages - 1);
282 /* If we're dealing with an iso pipe, we need to copy back the schedule. */
283 for ( i = 0; i < purb->number_of_packets; i++ )
285 sched[i].length = purb->iso_frame_desc[i].actual_length;
286 ASSERT(sched[i].buffer_offset ==
287 purb->iso_frame_desc[i].offset);
288 sched[i].status = purb->iso_frame_desc[i].status;
292 fast_flush_area(pending_req - pending_reqs, pending_req->nr_pages);
294 kfree(purb->setup_packet);
296 make_response(pending_req->usbif_priv, pending_req->id,
297 pending_req->operation, pending_req->status, 0, purb->actual_length);
298 usbif_put(pending_req->usbif_priv);
302 free_pending(pending_idx);
306 /* Check for anything still waiting in the rings, having freed a request... */
307 maybe_trigger_usbio_schedule();
310 /******************************************************************
311 * SCHEDULER FUNCTIONS
314 static DECLARE_WAIT_QUEUE_HEAD(usbio_schedule_wait);
316 static int usbio_schedule(void *arg)
318 DECLARE_WAITQUEUE(wq, current);
321 struct list_head *ent;
327 /* Wait for work to do. */
328 add_wait_queue(&usbio_schedule_wait, &wq);
329 set_current_state(TASK_INTERRUPTIBLE);
330 if ( (NR_PENDING_REQS == MAX_PENDING_REQS) ||
331 list_empty(&usbio_schedule_list) )
333 __set_current_state(TASK_RUNNING);
334 remove_wait_queue(&usbio_schedule_wait, &wq);
336 /* Queue up a batch of requests. */
337 while ( (NR_PENDING_REQS < MAX_PENDING_REQS) &&
338 !list_empty(&usbio_schedule_list) )
340 ent = usbio_schedule_list.next;
341 up = list_entry(ent, usbif_priv_t, usbif_list);
343 remove_from_usbif_list(up);
344 if ( do_usb_io_op(up, BATCH_PER_DOMAIN) )
345 add_to_usbif_list_tail(up);
351 static void maybe_trigger_usbio_schedule(void)
354 * Needed so that two processes, who together make the following predicate
355 * true, don't both read stale values and evaluate the predicate
356 * incorrectly. Incredibly unlikely to stall the scheduler on x86, but...
360 if ( !list_empty(&usbio_schedule_list) )
361 wake_up(&usbio_schedule_wait);
365 /******************************************************************************
366 * NOTIFICATION FROM GUEST OS.
369 irqreturn_t usbif_be_int(int irq, void *dev_id, struct pt_regs *regs)
371 usbif_priv_t *up = dev_id;
375 add_to_usbif_list_tail(up);
377 /* Will in fact /always/ trigger an io schedule in this case. */
378 maybe_trigger_usbio_schedule();
385 /******************************************************************
386 * DOWNWARD CALLS -- These interface with the usb-device layer proper.
389 static int do_usb_io_op(usbif_priv_t *up, int max_to_do)
391 usbif_back_ring_t *usb_ring = &up->usb_ring;
392 usbif_request_t *req;
396 rp = usb_ring->sring->req_prod;
397 rmb(); /* Ensure we see queued requests up to 'rp'. */
399 /* Take items off the comms ring, taking care not to overflow. */
400 for ( i = usb_ring->req_cons;
401 (i != rp) && !RING_REQUEST_CONS_OVERFLOW(usb_ring, i);
404 if ( (max_to_do-- == 0) || (NR_PENDING_REQS == MAX_PENDING_REQS) )
410 req = RING_GET_REQUEST(usb_ring, i);
412 switch ( req->operation )
415 dispatch_usb_probe(up, req->id, req->port);
419 /* Assemble an appropriate URB. */
420 dispatch_usb_io(up, req);
424 dispatch_usb_reset(up, req->port);
428 DPRINTK("error: unknown USB io operation [%d]\n",
430 make_response(up, req->id, req->operation, -EINVAL, 0, 0);
435 usb_ring->req_cons = i;
440 static owned_port_t *find_guest_port(usbif_priv_t *up, int port)
445 spin_lock_irqsave(&owned_ports_lock, flags);
446 list_for_each(l, &owned_ports)
448 owned_port_t *p = list_entry(l, owned_port_t, list);
449 if(p->usbif_priv == up && p->guest_port == port)
451 spin_unlock_irqrestore(&owned_ports_lock, flags);
455 spin_unlock_irqrestore(&owned_ports_lock, flags);
460 static void dispatch_usb_reset(usbif_priv_t *up, unsigned long portid)
462 owned_port_t *port = find_guest_port(up, portid);
466 /* Allowing the guest to actually reset the device causes more problems
467 * than it's worth. We just fake it out in software but we will do a real
468 * reset when the interface is destroyed. */
472 port->guest_address = 0;
473 /* If there's an attached device then the port is now enabled. */
474 if ( port->dev_present )
479 make_response(up, 0, USBIF_OP_RESET, ret, 0, 0);
482 static void dispatch_usb_probe(usbif_priv_t *up, unsigned long id, unsigned long portid)
484 owned_port_t *port = find_guest_port(up, portid);
488 ret = port->dev_present;
492 printk(KERN_INFO "dispatch_usb_probe(): invalid port probe request "
493 "(port %ld)\n", portid);
496 /* Probe result is sent back in-band. Probes don't have an associated id
498 make_response(up, id, USBIF_OP_PROBE, ret, portid, 0);
502 * check_iso_schedule - safety check the isochronous schedule for an URB
503 * @purb : the URB in question
505 static int check_iso_schedule(struct urb *purb)
508 unsigned long total_length = 0;
510 for ( i = 0; i < purb->number_of_packets; i++ )
512 struct usb_iso_packet_descriptor *desc = &purb->iso_frame_desc[i];
514 if ( desc->offset >= purb->transfer_buffer_length
515 || ( desc->offset + desc->length) > purb->transfer_buffer_length )
518 total_length += desc->length;
520 if ( total_length > purb->transfer_buffer_length )
527 owned_port_t *find_port_for_request(usbif_priv_t *up, usbif_request_t *req);
529 static void dispatch_usb_io(usbif_priv_t *up, usbif_request_t *req)
531 unsigned long buffer_mach;
532 int i = 0, offset = 0,
533 pending_idx = pending_ring[MASK_PEND_IDX(pending_cons)];
534 pending_req_t *pending_req;
535 unsigned long remap_prot;
536 multicall_entry_t mcl[MMAP_PAGES_PER_REQUEST];
537 struct urb *purb = NULL;
539 unsigned char *setup;
543 if ( NR_PENDING_REQS == MAX_PENDING_REQS )
545 printk(KERN_WARNING "usbback: Max requests already queued. "
551 port = find_port_for_request(up, req);
555 printk(KERN_WARNING "No such device! (%d)\n", req->devnum);
558 make_response(up, req->id, req->operation, -ENODEV, 0, 0);
561 else if ( !port->dev_present )
563 /* In normal operation, we'll only get here if a device is unplugged
564 * and the frontend hasn't noticed yet. */
565 make_response(up, req->id, req->operation, -ENODEV, 0, 0);
570 setup = kmalloc(8, GFP_KERNEL);
575 /* Copy request out for safety. */
576 memcpy(setup, req->setup, 8);
578 if( setup[0] == 0x0 && setup[1] == 0x5)
580 /* To virtualise the USB address space, we need to intercept
581 * set_address messages and emulate. From the USB specification:
582 * bmRequestType = 0x0;
583 * Brequest = SET_ADDRESS (i.e. 0x5)
584 * wValue = device address
589 /* Store into the guest transfer buffer using cpu_to_le16 */
590 port->guest_address = le16_to_cpu(*(u16 *)(setup + 2));
591 /* Make a successful response. That was easy! */
593 make_response(up, req->id, req->operation, 0, 0, 0);
598 else if ( setup[0] == 0x0 && setup[1] == 0x9 )
600 /* The host kernel needs to know what device configuration is in use
601 * because various error checks get confused otherwise. We just do
602 * configuration settings here, under controlled conditions.
605 /* Ignore configuration setting and hope that the host kernel
607 /* usb_set_configuration(port->dev, setup[2]); */
609 make_response(up, req->id, req->operation, 0, 0, 0);
614 else if ( setup[0] == 0x1 && setup[1] == 0xB )
616 /* The host kernel needs to know what device interface is in use
617 * because various error checks get confused otherwise. We just do
618 * configuration settings here, under controlled conditions.
620 usb_set_interface(port->dev, (setup[4] | setup[5] << 8),
621 (setup[2] | setup[3] << 8) );
623 make_response(up, req->id, req->operation, 0, 0, 0);
629 if ( ( req->transfer_buffer - (req->transfer_buffer & PAGE_MASK)
631 > MMAP_PAGES_PER_REQUEST * PAGE_SIZE )
633 printk(KERN_WARNING "usbback: request of %lu bytes too large\n",
635 make_response(up, req->id, req->operation, -EINVAL, 0, 0);
640 buffer_mach = req->transfer_buffer;
642 if( buffer_mach == 0 )
645 ASSERT((req->length >> PAGE_SHIFT) <= MMAP_PAGES_PER_REQUEST);
648 /* Always map writeable for now. */
649 remap_prot = _PAGE_PRESENT|_PAGE_DIRTY|_PAGE_ACCESSED|_PAGE_RW;
651 for ( i = 0, offset = 0; offset < req->length;
652 i++, offset += PAGE_SIZE )
654 mcl[i].op = __HYPERVISOR_update_va_mapping_otherdomain;
655 mcl[i].args[0] = MMAP_VADDR(pending_idx, i);
656 mcl[i].args[1] = ((buffer_mach & PAGE_MASK) + offset) | remap_prot;
658 mcl[i].args[3] = up->domid;
660 phys_to_machine_mapping[__pa(MMAP_VADDR(pending_idx, i))>>PAGE_SHIFT] =
661 FOREIGN_FRAME((buffer_mach + offset) >> PAGE_SHIFT);
663 ASSERT(virt_to_machine(MMAP_VADDR(pending_idx, i))
664 == buffer_mach + i << PAGE_SHIFT);
667 if ( req->pipe_type == 0 && req->num_iso > 0 ) /* Maybe schedule ISO... */
669 /* Map in ISO schedule, if necessary. */
670 mcl[i].op = __HYPERVISOR_update_va_mapping_otherdomain;
671 mcl[i].args[0] = MMAP_VADDR(pending_idx, i);
672 mcl[i].args[1] = (req->iso_schedule & PAGE_MASK) | remap_prot;
674 mcl[i].args[3] = up->domid;
676 phys_to_machine_mapping[__pa(MMAP_VADDR(pending_idx, i))>>PAGE_SHIFT] =
677 FOREIGN_FRAME(req->iso_schedule >> PAGE_SHIFT);
682 if ( unlikely(HYPERVISOR_multicall(mcl, i) != 0) )
687 for ( j = 0; j < i; j++ )
689 if ( unlikely(mcl[j].args[5] != 0) )
692 "invalid buffer %d -- could not remap it\n", j);
693 fast_flush_area(pending_idx, i);
701 ASSERT(i <= MMAP_PAGES_PER_REQUEST);
702 ASSERT(i * PAGE_SIZE >= req->length);
704 /* We have to do this because some things might complete out of order. */
705 pending_req = &pending_reqs[pending_idx];
706 pending_req->usbif_priv= up;
707 pending_req->id = req->id;
708 pending_req->operation = req->operation;
709 pending_req->nr_pages = i;
715 /* Fill out an actual request for the USB layer. */
716 purb = usb_alloc_urb(req->num_iso);
721 free_pending(pending_idx);
725 purb->dev = port->dev;
726 purb->context = pending_req;
727 purb->transfer_buffer =
728 (void *)(MMAP_VADDR(pending_idx, 0) + (buffer_mach & ~PAGE_MASK));
730 purb->transfer_buffer = NULL;
731 purb->complete = __end_usb_io_op;
732 purb->transfer_buffer_length = req->length;
733 purb->transfer_flags = req->transfer_flags;
736 purb->pipe |= req->direction << 7;
737 purb->pipe |= port->dev->devnum << 8;
738 purb->pipe |= req->speed << 26;
739 purb->pipe |= req->pipe_type << 30;
740 purb->pipe |= req->endpoint << 15;
742 purb->number_of_packets = req->num_iso;
744 if ( purb->number_of_packets * sizeof(usbif_iso_t) > PAGE_SIZE )
747 /* Make sure there's always some kind of timeout. */
748 purb->timeout = ( req->timeout > 0 ) ? (req->timeout * HZ) / 1000
751 purb->setup_packet = setup;
753 if ( req->pipe_type == 0 ) /* ISO */
756 usbif_iso_t *iso_sched = (usbif_iso_t *)MMAP_VADDR(pending_idx, i - 1);
758 /* If we're dealing with an iso pipe, we need to copy in a schedule. */
759 for ( j = 0; j < purb->number_of_packets; j++ )
761 purb->iso_frame_desc[j].length = iso_sched[j].length;
762 purb->iso_frame_desc[j].offset = iso_sched[j].buffer_offset;
763 iso_sched[j].status = 0;
767 if ( check_iso_schedule(purb) != 0 )
770 if ( usb_submit_urb(purb) != 0 )
778 free_pending(pending_idx);
784 make_response(up, req->id, req->operation, -EINVAL, 0, 0);
790 make_response(up, req->id, req->operation, -ENOMEM, 0, 0);
796 /******************************************************************
797 * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING
801 static void make_response(usbif_priv_t *up, unsigned long id,
802 unsigned short op, int st, int inband,
803 unsigned long length)
805 usbif_response_t *resp;
807 usbif_back_ring_t *usb_ring = &up->usb_ring;
809 /* Place on the response ring for the relevant domain. */
810 spin_lock_irqsave(&up->usb_ring_lock, flags);
811 resp = RING_GET_RESPONSE(usb_ring, usb_ring->rsp_prod_pvt);
813 resp->operation = op;
816 resp->length = length;
817 wmb(); /* Ensure other side can see the response fields. */
821 usb_ring->rsp_prod_pvt++;
822 RING_PUSH_RESPONSES(usb_ring);
823 spin_unlock_irqrestore(&up->usb_ring_lock, flags);
825 /* Kick the relevant domain. */
826 notify_via_evtchn(up->evtchn);
830 * usbif_claim_port - claim devices on a port on behalf of guest
832 * Once completed, this will ensure that any device attached to that
833 * port is claimed by this driver for use by the guest.
835 int usbif_claim_port(usbif_be_claim_port_t *msg)
840 if ( usbif_find_port(msg->path) != NULL )
842 printk(KERN_WARNING "usbback: Attempted to claim USB port "
843 "we already own!\n");
847 /* No need for a slab cache - this should be infrequent. */
848 o_p = kmalloc(sizeof(owned_port_t), GFP_KERNEL);
854 o_p->usbif_priv = usbif_find(msg->domid);
855 o_p->guest_port = msg->usbif_port;
856 o_p->dev_present = 0;
857 o_p->guest_address = 0; /* Default address. */
859 strcpy(o_p->path, msg->path);
861 spin_lock_irq(&owned_ports_lock);
863 list_add(&o_p->list, &owned_ports);
865 spin_unlock_irq(&owned_ports_lock);
867 printk(KERN_INFO "usbback: Claimed USB port (%s) for %d.%d\n", o_p->path,
868 msg->domid, msg->usbif_port);
870 /* Force a reprobe for unclaimed devices. */
876 owned_port_t *find_port_for_request(usbif_priv_t *up, usbif_request_t *req)
879 struct list_head *port;
881 /* I'm assuming this is not called from IRQ context - correct? I think
882 * it's probably only called in response to control messages or plug events
883 * in the USB hub kernel thread, so should be OK. */
884 spin_lock_irqsave(&owned_ports_lock, flags);
885 list_for_each(port, &owned_ports)
887 owned_port_t *p = list_entry(port, owned_port_t, list);
888 if(p->usbif_priv == up && p->guest_address == req->devnum && p->enabled )
892 spin_unlock_irqrestore(&owned_ports_lock, flags);
896 spin_unlock_irqrestore(&owned_ports_lock, flags);
901 owned_port_t *__usbif_find_port(char *path)
903 struct list_head *port;
905 list_for_each(port, &owned_ports)
907 owned_port_t *p = list_entry(port, owned_port_t, list);
908 if(!strcmp(path, p->path))
917 owned_port_t *usbif_find_port(char *path)
922 spin_lock_irqsave(&owned_ports_lock, flags);
923 ret = __usbif_find_port(path);
924 spin_unlock_irqrestore(&owned_ports_lock, flags);
930 static void *probe(struct usb_device *dev, unsigned iface,
931 const struct usb_device_id *id)
935 /* We don't care what the device is - if we own the port, we want it. We
936 * don't deal with device-specifics in this driver, so we don't care what
937 * the device actually is ;-) */
938 if ( ( p = usbif_find_port(dev->devpath) ) != NULL )
940 printk(KERN_INFO "usbback: claimed device attached to owned port\n");
944 set_bit(iface, &p->ifaces);
946 return p->usbif_priv;
949 printk(KERN_INFO "usbback: hotplug for non-owned port (%s), ignoring\n",
956 static void disconnect(struct usb_device *dev, void *usbif)
958 /* Note the device is removed so we can tell the guest when it probes. */
959 owned_port_t *port = usbif_find_port(dev->devpath);
960 port->dev_present = 0;
966 struct usb_driver driver =
968 .owner = THIS_MODULE,
969 .name = "Xen USB Backend",
971 .disconnect = disconnect,
975 /* __usbif_release_port - internal mechanics for releasing a port */
976 void __usbif_release_port(owned_port_t *p)
980 for ( i = 0; p->ifaces != 0; i++)
981 if ( p->ifaces & 1 << i )
983 usb_driver_release_interface(&driver, usb_ifnum_to_if(p->dev, i));
984 clear_bit(i, &p->ifaces);
988 /* Reset the real device. We don't simulate disconnect / probe for other
989 * drivers in this kernel because we assume the device is completely under
990 * the control of ourselves (i.e. the guest!). This should ensure that the
991 * device is in a sane state for the next customer ;-) */
993 /* MAW NB: we're not resetting the real device here. This looks perfectly
994 * valid to me but it causes memory corruption. We seem to get away with not
995 * resetting for now, although it'd be nice to have this tracked down. */
996 /* if ( p->dev != NULL) */
997 /* usb_reset_device(p->dev); */
1004 * usbif_release_port - stop claiming devices on a port on behalf of guest
1006 void usbif_release_port(usbif_be_release_port_t *msg)
1010 spin_lock_irq(&owned_ports_lock);
1011 p = __usbif_find_port(msg->path);
1012 __usbif_release_port(p);
1013 spin_unlock_irq(&owned_ports_lock);
1016 void usbif_release_ports(usbif_priv_t *up)
1018 struct list_head *port, *tmp;
1019 unsigned long flags;
1021 spin_lock_irqsave(&owned_ports_lock, flags);
1022 list_for_each_safe(port, tmp, &owned_ports)
1024 owned_port_t *p = list_entry(port, owned_port_t, list);
1025 if ( p->usbif_priv == up )
1026 __usbif_release_port(p);
1028 spin_unlock_irqrestore(&owned_ports_lock, flags);
1031 static int __init usbif_init(void)
1035 if ( !(xen_start_info.flags & SIF_INITDOMAIN) &&
1036 !(xen_start_info.flags & SIF_USB_BE_DOMAIN) )
1039 if ( (mmap_vstart = allocate_empty_lowmem_region(MMAP_PAGES)) == 0 )
1043 pending_prod = MAX_PENDING_REQS;
1044 memset(pending_reqs, 0, sizeof(pending_reqs));
1045 for ( i = 0; i < MAX_PENDING_REQS; i++ )
1046 pending_ring[i] = i;
1048 spin_lock_init(&pend_prod_lock);
1050 spin_lock_init(&owned_ports_lock);
1051 INIT_LIST_HEAD(&owned_ports);
1053 spin_lock_init(&usbio_schedule_list_lock);
1054 INIT_LIST_HEAD(&usbio_schedule_list);
1056 if ( kernel_thread(usbio_schedule, 0, CLONE_FS | CLONE_FILES) < 0 )
1059 usbif_interface_init();
1061 usbif_ctrlif_init();
1063 usb_register(&driver);
1065 printk(KERN_INFO "Xen USB Backend Initialised");
1070 __initcall(usbif_init);