ia64/xen-unstable
changeset 3546:d295396360fb
bitkeeper revision 1.1159.212.45 (41f7969elyNTzsGsx57vqxgKK9SrRQ)
This change set moves the USB back and front end driver source code from
the 2.4 sparse tree to the 2.6 sparse tree in preparation for porting the
usb virtualization to 2.6.
This change set makes changes to the 2.4 mkbuildtree necessary to link
the files back into the 2.4 tree to preserve the previous usb
functionality for the 2.4 kernel.
The usbif.h file is also moved to be consistent with the location of the
netif.h file and necessary changes made to #include directives.
The usb code in the 2.6 tree is not yet integrated with the 2.6 build
process so the 2.6 build does not attempt to build it.
Tested with make world for both *2.4* and *2.6*.
Signed-off-by: butterwo@uk.ibm.com
This change set moves the USB back and front end driver source code from
the 2.4 sparse tree to the 2.6 sparse tree in preparation for porting the
usb virtualization to 2.6.
This change set makes changes to the 2.4 mkbuildtree necessary to link
the files back into the 2.4 tree to preserve the previous usb
functionality for the 2.4 kernel.
The usbif.h file is also moved to be consistent with the location of the
netif.h file and necessary changes made to #include directives.
The usb code in the 2.6 tree is not yet integrated with the 2.6 build
process so the 2.6 build does not attempt to build it.
Tested with make world for both *2.4* and *2.6*.
Signed-off-by: butterwo@uk.ibm.com
author | harry@dory.(none) |
---|---|
date | Wed Jan 26 13:09:50 2005 +0000 (2005-01-26) |
parents | 1b6a9577c36d |
children | 30ee9c427a5b |
files | .rootkeys BitKeeper/etc/logging_ok linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/common.h linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/control.c linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/interface.c linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/main.c linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/frontend/main.c linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/frontend/xhci.h linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/usbif.h linux-2.4.29-xen-sparse/mkbuildtree linux-2.6.10-xen-sparse/drivers/xen/usbback/common.h linux-2.6.10-xen-sparse/drivers/xen/usbback/control.c linux-2.6.10-xen-sparse/drivers/xen/usbback/interface.c linux-2.6.10-xen-sparse/drivers/xen/usbback/usbback.c linux-2.6.10-xen-sparse/drivers/xen/usbfront/usbfront.c linux-2.6.10-xen-sparse/drivers/xen/usbfront/xhci.h xen/include/public/io/usbif.h |
line diff
1.1 --- a/.rootkeys Wed Jan 26 10:56:34 2005 +0000 1.2 +++ b/.rootkeys Wed Jan 26 13:09:50 2005 +0000 1.3 @@ -65,14 +65,7 @@ 4083dc16UmHXxS9g_UFVnkUpN-oP2Q linux-2.4 1.4 405853f2wg7JXZJNltspMwOZJklxgw linux-2.4.29-xen-sparse/arch/xen/drivers/netif/frontend/Makefile 1.5 41ee5e8b_2rt-qHzbDXtIoBzOli0EA linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/Makefile 1.6 41ee5e8bUhF4tH7OoJaVbUxdXqneVw linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/Makefile 1.7 -41ee5e8bYDQkjRVKnFn5uFyy0KreCw linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/common.h 1.8 -41ee5e8bt7xeBUJqG5XJS-ofukdsgA linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/control.c 1.9 -41ee5e8bSs3BGC7yegM_ek2Tn0Ahvw linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/interface.c 1.10 -41ee5e8bglvqKvZSY5uJ5JGQejEwyQ linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/main.c 1.11 41ee5e8bSPpxzhGO6TrY20TegW3cZg linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/frontend/Makefile 1.12 -41ee5e8ckZ9xVNvu9NHIZDK7JqApmQ linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/frontend/main.c 1.13 -41ee5e8ck9scpGirfqEZRARbGDyTXA linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/frontend/xhci.h 1.14 -41ee5e8c6mLxIx82KPsbpt_uts_vSA linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/usbif.h 1.15 3e5a4e65lWzkiPXsZdzPt2RNnJGG1g linux-2.4.29-xen-sparse/arch/xen/kernel/Makefile 1.16 3e5a4e65_hqfuxtGG8IUy6wRM86Ecg linux-2.4.29-xen-sparse/arch/xen/kernel/entry.S 1.17 3e5a4e65Hy_1iUvMTPsNqGNXd9uFpg linux-2.4.29-xen-sparse/arch/xen/kernel/head.S 1.18 @@ -228,6 +221,12 @@ 40f56239Wd4k_ycG_mFsSO1r5xKdtQ linux-2.6 1.19 405853f6nbeazrNyEWNHBuoSg2PiPA linux-2.6.10-xen-sparse/drivers/xen/netfront/netfront.c 1.20 4108f5c1ppFXVpQzCOAZ6xXYubsjKA linux-2.6.10-xen-sparse/drivers/xen/privcmd/Makefile 1.21 3e5a4e65IUfzzMu2kZFlGEB8-rpTaA linux-2.6.10-xen-sparse/drivers/xen/privcmd/privcmd.c 1.22 +41ee5e8bYDQkjRVKnFn5uFyy0KreCw linux-2.6.10-xen-sparse/drivers/xen/usbback/common.h 1.23 +41ee5e8bt7xeBUJqG5XJS-ofukdsgA linux-2.6.10-xen-sparse/drivers/xen/usbback/control.c 1.24 +41ee5e8bSs3BGC7yegM_ek2Tn0Ahvw linux-2.6.10-xen-sparse/drivers/xen/usbback/interface.c 1.25 +41ee5e8bglvqKvZSY5uJ5JGQejEwyQ linux-2.6.10-xen-sparse/drivers/xen/usbback/usbback.c 1.26 +41ee5e8ckZ9xVNvu9NHIZDK7JqApmQ linux-2.6.10-xen-sparse/drivers/xen/usbfront/usbfront.c 1.27 +41ee5e8ck9scpGirfqEZRARbGDyTXA linux-2.6.10-xen-sparse/drivers/xen/usbfront/xhci.h 1.28 412f47e4RKD-R5IS5gEXvcT8L4v8gA linux-2.6.10-xen-sparse/include/asm-generic/pgtable.h 1.29 40f56239YAjS52QG2FIAQpHDZAdGHg linux-2.6.10-xen-sparse/include/asm-xen/asm-i386/desc.h 1.30 4107adf1E5O4ztGHNGMzCCNhcvqNow linux-2.6.10-xen-sparse/include/asm-xen/asm-i386/dma-mapping.h 1.31 @@ -1053,6 +1052,7 @@ 40dc4076pVeE1kEEWzcUaNZin65kCA xen/inclu 1.32 41c0c412FLc0gunlJl91qMYscFtXVA xen/include/public/io/ioreq.h 1.33 40f5623cTZ80EwjWUBlh44A9F9i_Lg xen/include/public/io/netif.h 1.34 41d40e9b8zCk5VDqhVbuQyhc7G3lqA xen/include/public/io/ring.h 1.35 +41ee5e8c6mLxIx82KPsbpt_uts_vSA xen/include/public/io/usbif.h 1.36 4051db79512nOCGweabrFWO2M2h5ng xen/include/public/physdev.h 1.37 40589968wmhPmV5-ENbBYmMjnedgKw xen/include/public/sched_ctl.h 1.38 404f3d2eR2Owk-ZcGOx9ULGHg3nrww xen/include/public/trace.h
2.1 --- a/BitKeeper/etc/logging_ok Wed Jan 26 10:56:34 2005 +0000 2.2 +++ b/BitKeeper/etc/logging_ok Wed Jan 26 13:09:50 2005 +0000 2.3 @@ -17,6 +17,7 @@ cwc22@centipede.cl.cam.ac.uk 2.4 djm@kirby.fc.hp.com 2.5 gm281@boulderdash.cl.cam.ac.uk 2.6 gm281@tetrapod.cl.cam.ac.uk 2.7 +harry@dory.(none) 2.8 iap10@freefall.cl.cam.ac.uk 2.9 iap10@labyrinth.cl.cam.ac.uk 2.10 iap10@nidd.cl.cam.ac.uk
3.1 --- a/linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/common.h Wed Jan 26 10:56:34 2005 +0000 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,87 +0,0 @@ 3.4 - 3.5 -#ifndef __USBIF__BACKEND__COMMON_H__ 3.6 -#define __USBIF__BACKEND__COMMON_H__ 3.7 - 3.8 -#include <linux/config.h> 3.9 -#include <linux/version.h> 3.10 -#include <linux/module.h> 3.11 -#include <linux/rbtree.h> 3.12 -#include <linux/interrupt.h> 3.13 -#include <linux/slab.h> 3.14 -#include <linux/blkdev.h> 3.15 -#include <asm/io.h> 3.16 -#include <asm/setup.h> 3.17 -#include <asm/pgalloc.h> 3.18 -#include <asm-xen/ctrl_if.h> 3.19 -#include <asm-xen/hypervisor.h> 3.20 - 3.21 -#include "../usbif.h" 3.22 - 3.23 -#if 0 3.24 -#define ASSERT(_p) \ 3.25 - if ( !(_p) ) { printk("Assertion '%s' failed, line %d, file %s", #_p , \ 3.26 - __LINE__, __FILE__); *(int*)0=0; } 3.27 -#define DPRINTK(_f, _a...) printk(KERN_ALERT "(file=%s, line=%d) " _f, \ 3.28 - __FILE__ , __LINE__ , ## _a ) 3.29 -#else 3.30 -#define ASSERT(_p) ((void)0) 3.31 -#define DPRINTK(_f, _a...) ((void)0) 3.32 -#endif 3.33 - 3.34 -typedef struct usbif_priv_st usbif_priv_t; 3.35 - 3.36 -struct usbif_priv_st { 3.37 - /* Unique identifier for this interface. */ 3.38 - domid_t domid; 3.39 - unsigned int handle; 3.40 - /* Physical parameters of the comms window. */ 3.41 - unsigned long shmem_frame; 3.42 - unsigned int evtchn; 3.43 - int irq; 3.44 - /* Comms information. */ 3.45 - usbif_t *usb_ring_base; /* ioremap()'ed ptr to shmem_frame. */ 3.46 - USBIF_RING_IDX usb_req_cons; /* Request consumer. */ 3.47 - USBIF_RING_IDX usb_resp_prod; /* Private version of resp. producer. */ 3.48 - /* Private fields. */ 3.49 - enum { DISCONNECTED, DISCONNECTING, CONNECTED } status; 3.50 - /* 3.51 - * DISCONNECT response is deferred until pending requests are ack'ed. 3.52 - * We therefore need to store the id from the original request. 3.53 - */ 3.54 - u8 disconnect_rspid; 3.55 - usbif_priv_t *hash_next; 3.56 - struct list_head usbif_list; 3.57 - spinlock_t usb_ring_lock; 3.58 - atomic_t refcnt; 3.59 - atomic_t work_scheduled; 3.60 - 3.61 - struct work_struct work; 3.62 -}; 3.63 - 3.64 -void usbif_create(usbif_be_create_t *create); 3.65 -void usbif_destroy(usbif_be_destroy_t *destroy); 3.66 -void usbif_connect(usbif_be_connect_t *connect); 3.67 -int usbif_disconnect(usbif_be_disconnect_t *disconnect, u8 rsp_id); 3.68 -void usbif_disconnect_complete(usbif_priv_t *up); 3.69 - 3.70 -void usbif_release_port(usbif_be_release_port_t *msg); 3.71 -int usbif_claim_port(usbif_be_claim_port_t *msg); 3.72 -void usbif_release_ports(usbif_priv_t *up); 3.73 - 3.74 -usbif_priv_t *usbif_find(domid_t domid); 3.75 -#define usbif_get(_b) (atomic_inc(&(_b)->refcnt)) 3.76 -#define usbif_put(_b) \ 3.77 - do { \ 3.78 - if ( atomic_dec_and_test(&(_b)->refcnt) ) \ 3.79 - usbif_disconnect_complete(_b); \ 3.80 - } while (0) 3.81 - 3.82 - 3.83 -void usbif_interface_init(void); 3.84 -void usbif_ctrlif_init(void); 3.85 - 3.86 -void usbif_deschedule(usbif_priv_t *usbif); 3.87 - 3.88 -irqreturn_t usbif_be_int(int irq, void *dev_id, struct pt_regs *regs); 3.89 - 3.90 -#endif /* __USBIF__BACKEND__COMMON_H__ */
4.1 --- a/linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/control.c Wed Jan 26 10:56:34 2005 +0000 4.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 4.3 @@ -1,77 +0,0 @@ 4.4 -/****************************************************************************** 4.5 - * arch/xen/drivers/usbif/backend/control.c 4.6 - * 4.7 - * Routines for interfacing with the control plane. 4.8 - * 4.9 - * Copyright (c) 2004, Keir Fraser 4.10 - */ 4.11 - 4.12 -#include "common.h" 4.13 - 4.14 -static void usbif_ctrlif_rx(ctrl_msg_t *msg, unsigned long id) 4.15 -{ 4.16 - DPRINTK("Received usbif backend message, subtype=%d\n", msg->subtype); 4.17 - 4.18 - switch ( msg->subtype ) 4.19 - { 4.20 - case CMSG_USBIF_BE_CREATE: 4.21 - if ( msg->length != sizeof(usbif_be_create_t) ) 4.22 - goto parse_error; 4.23 - usbif_create((usbif_be_create_t *)&msg->msg[0]); 4.24 - break; 4.25 - case CMSG_USBIF_BE_DESTROY: 4.26 - if ( msg->length != sizeof(usbif_be_destroy_t) ) 4.27 - goto parse_error; 4.28 - usbif_destroy((usbif_be_destroy_t *)&msg->msg[0]); 4.29 - break; 4.30 - case CMSG_USBIF_BE_CONNECT: 4.31 - if ( msg->length != sizeof(usbif_be_connect_t) ) 4.32 - goto parse_error; 4.33 - usbif_connect((usbif_be_connect_t *)&msg->msg[0]); 4.34 - break; 4.35 - case CMSG_USBIF_BE_DISCONNECT: 4.36 - if ( msg->length != sizeof(usbif_be_disconnect_t) ) 4.37 - goto parse_error; 4.38 - if ( !usbif_disconnect((usbif_be_disconnect_t *)&msg->msg[0],msg->id) ) 4.39 - return; /* Sending the response is deferred until later. */ 4.40 - break; 4.41 - case CMSG_USBIF_BE_CLAIM_PORT: 4.42 - if ( msg->length != sizeof(usbif_be_claim_port_t) ) 4.43 - goto parse_error; 4.44 - usbif_claim_port((usbif_be_claim_port_t *)&msg->msg[0]); 4.45 - break; 4.46 - case CMSG_USBIF_BE_RELEASE_PORT: 4.47 - if ( msg->length != sizeof(usbif_be_release_port_t) ) 4.48 - goto parse_error; 4.49 - usbif_release_port((usbif_be_release_port_t *)&msg->msg[0]); 4.50 - break; 4.51 - default: 4.52 - goto parse_error; 4.53 - } 4.54 - 4.55 - ctrl_if_send_response(msg); 4.56 - return; 4.57 - 4.58 - parse_error: 4.59 - DPRINTK("Parse error while reading message subtype %d, len %d\n", 4.60 - msg->subtype, msg->length); 4.61 - msg->length = 0; 4.62 - ctrl_if_send_response(msg); 4.63 -} 4.64 - 4.65 -void usbif_ctrlif_init(void) 4.66 -{ 4.67 - ctrl_msg_t cmsg; 4.68 - usbif_be_driver_status_changed_t st; 4.69 - 4.70 - (void)ctrl_if_register_receiver(CMSG_USBIF_BE, usbif_ctrlif_rx, 4.71 - CALLBACK_IN_BLOCKING_CONTEXT); 4.72 - 4.73 - /* Send a driver-UP notification to the domain controller. */ 4.74 - cmsg.type = CMSG_USBIF_BE; 4.75 - cmsg.subtype = CMSG_USBIF_BE_DRIVER_STATUS_CHANGED; 4.76 - cmsg.length = sizeof(usbif_be_driver_status_changed_t); 4.77 - st.status = USBIF_DRIVER_STATUS_UP; 4.78 - memcpy(cmsg.msg, &st, sizeof(st)); 4.79 - ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE); 4.80 -}
5.1 --- a/linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/interface.c Wed Jan 26 10:56:34 2005 +0000 5.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 5.3 @@ -1,248 +0,0 @@ 5.4 -/****************************************************************************** 5.5 - * arch/xen/drivers/usbif/backend/interface.c 5.6 - * 5.7 - * USB device interface management. 5.8 - * 5.9 - * by Mark Williamson, Copyright (c) 2004 5.10 - */ 5.11 - 5.12 - 5.13 -/****************************************************************************** 5.14 - * arch/xen/drivers/blkif/backend/interface.c 5.15 - * 5.16 - * Block-device interface management. 5.17 - * 5.18 - * Copyright (c) 2004, Keir Fraser 5.19 - */ 5.20 - 5.21 -#include "common.h" 5.22 - 5.23 -#define USBIF_HASHSZ 1024 5.24 -#define USBIF_HASH(_d) (((int)(_d))&(USBIF_HASHSZ-1)) 5.25 - 5.26 -static kmem_cache_t *usbif_priv_cachep; 5.27 -static usbif_priv_t *usbif_priv_hash[USBIF_HASHSZ]; 5.28 - 5.29 -usbif_priv_t *usbif_find(domid_t domid) 5.30 -{ 5.31 - usbif_priv_t *up = usbif_priv_hash[USBIF_HASH(domid)]; 5.32 - while ( (up != NULL ) && ( up->domid != domid ) ) 5.33 - up = up->hash_next; 5.34 - return up; 5.35 -} 5.36 - 5.37 -static void __usbif_disconnect_complete(void *arg) 5.38 -{ 5.39 - usbif_priv_t *usbif = (usbif_priv_t *)arg; 5.40 - ctrl_msg_t cmsg; 5.41 - usbif_be_disconnect_t disc; 5.42 - 5.43 - /* 5.44 - * These can't be done in usbif_disconnect() because at that point there 5.45 - * may be outstanding requests at the device whose asynchronous responses 5.46 - * must still be notified to the remote driver. 5.47 - */ 5.48 - unbind_evtchn_from_irq(usbif->evtchn); 5.49 - vfree(usbif->usb_ring_base); 5.50 - 5.51 - /* Construct the deferred response message. */ 5.52 - cmsg.type = CMSG_USBIF_BE; 5.53 - cmsg.subtype = CMSG_USBIF_BE_DISCONNECT; 5.54 - cmsg.id = usbif->disconnect_rspid; 5.55 - cmsg.length = sizeof(usbif_be_disconnect_t); 5.56 - disc.domid = usbif->domid; 5.57 - disc.status = USBIF_BE_STATUS_OKAY; 5.58 - memcpy(cmsg.msg, &disc, sizeof(disc)); 5.59 - 5.60 - /* 5.61 - * Make sure message is constructed /before/ status change, because 5.62 - * after the status change the 'usbif' structure could be deallocated at 5.63 - * any time. Also make sure we send the response /after/ status change, 5.64 - * as otherwise a subsequent CONNECT request could spuriously fail if 5.65 - * another CPU doesn't see the status change yet. 5.66 - */ 5.67 - mb(); 5.68 - if ( usbif->status != DISCONNECTING ) 5.69 - BUG(); 5.70 - usbif->status = DISCONNECTED; 5.71 - mb(); 5.72 - 5.73 - /* Send the successful response. */ 5.74 - ctrl_if_send_response(&cmsg); 5.75 -} 5.76 - 5.77 -void usbif_disconnect_complete(usbif_priv_t *up) 5.78 -{ 5.79 - INIT_WORK(&up->work, __usbif_disconnect_complete, (void *)up); 5.80 - schedule_work(&up->work); 5.81 -} 5.82 - 5.83 -void usbif_create(usbif_be_create_t *create) 5.84 -{ 5.85 - domid_t domid = create->domid; 5.86 - usbif_priv_t **pup, *up; 5.87 - 5.88 - if ( (up = kmem_cache_alloc(usbif_priv_cachep, GFP_KERNEL)) == NULL ) 5.89 - { 5.90 - DPRINTK("Could not create usbif: out of memory\n"); 5.91 - create->status = USBIF_BE_STATUS_OUT_OF_MEMORY; 5.92 - return; 5.93 - } 5.94 - 5.95 - memset(up, 0, sizeof(*up)); 5.96 - up->domid = domid; 5.97 - up->status = DISCONNECTED; 5.98 - spin_lock_init(&up->usb_ring_lock); 5.99 - atomic_set(&up->refcnt, 0); 5.100 - 5.101 - pup = &usbif_priv_hash[USBIF_HASH(domid)]; 5.102 - while ( *pup != NULL ) 5.103 - { 5.104 - if ( (*pup)->domid == domid ) 5.105 - { 5.106 - create->status = USBIF_BE_STATUS_INTERFACE_EXISTS; 5.107 - kmem_cache_free(usbif_priv_cachep, up); 5.108 - return; 5.109 - } 5.110 - pup = &(*pup)->hash_next; 5.111 - } 5.112 - 5.113 - up->hash_next = *pup; 5.114 - *pup = up; 5.115 - 5.116 - create->status = USBIF_BE_STATUS_OKAY; 5.117 -} 5.118 - 5.119 -void usbif_destroy(usbif_be_destroy_t *destroy) 5.120 -{ 5.121 - domid_t domid = destroy->domid; 5.122 - usbif_priv_t **pup, *up; 5.123 - 5.124 - pup = &usbif_priv_hash[USBIF_HASH(domid)]; 5.125 - while ( (up = *pup) != NULL ) 5.126 - { 5.127 - if ( up->domid == domid ) 5.128 - { 5.129 - if ( up->status != DISCONNECTED ) 5.130 - goto still_connected; 5.131 - goto destroy; 5.132 - } 5.133 - pup = &up->hash_next; 5.134 - } 5.135 - 5.136 - destroy->status = USBIF_BE_STATUS_INTERFACE_NOT_FOUND; 5.137 - return; 5.138 - 5.139 - still_connected: 5.140 - destroy->status = USBIF_BE_STATUS_INTERFACE_CONNECTED; 5.141 - return; 5.142 - 5.143 - destroy: 5.144 - *pup = up->hash_next; 5.145 - usbif_release_ports(up); 5.146 - kmem_cache_free(usbif_priv_cachep, up); 5.147 - destroy->status = USBIF_BE_STATUS_OKAY; 5.148 -} 5.149 - 5.150 -void usbif_connect(usbif_be_connect_t *connect) 5.151 -{ 5.152 - domid_t domid = connect->domid; 5.153 - unsigned int evtchn = connect->evtchn; 5.154 - unsigned long shmem_frame = connect->shmem_frame; 5.155 - struct vm_struct *vma; 5.156 - pgprot_t prot; 5.157 - int error; 5.158 - usbif_priv_t *up; 5.159 - 5.160 - up = usbif_find(domid); 5.161 - if ( unlikely(up == NULL) ) 5.162 - { 5.163 - DPRINTK("usbif_connect attempted for non-existent usbif (%u)\n", 5.164 - connect->domid); 5.165 - connect->status = USBIF_BE_STATUS_INTERFACE_NOT_FOUND; 5.166 - return; 5.167 - } 5.168 - 5.169 - if ( (vma = get_vm_area(PAGE_SIZE, VM_IOREMAP)) == NULL ) 5.170 - { 5.171 - connect->status = USBIF_BE_STATUS_OUT_OF_MEMORY; 5.172 - return; 5.173 - } 5.174 - 5.175 - prot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED); 5.176 - error = direct_remap_area_pages(&init_mm, VMALLOC_VMADDR(vma->addr), 5.177 - shmem_frame<<PAGE_SHIFT, PAGE_SIZE, 5.178 - prot, domid); 5.179 - if ( error != 0 ) 5.180 - { 5.181 - if ( error == -ENOMEM ) 5.182 - connect->status = USBIF_BE_STATUS_OUT_OF_MEMORY; 5.183 - else if ( error == -EFAULT ) 5.184 - connect->status = USBIF_BE_STATUS_MAPPING_ERROR; 5.185 - else 5.186 - connect->status = USBIF_BE_STATUS_ERROR; 5.187 - vfree(vma->addr); 5.188 - return; 5.189 - } 5.190 - 5.191 - if ( up->status != DISCONNECTED ) 5.192 - { 5.193 - connect->status = USBIF_BE_STATUS_INTERFACE_CONNECTED; 5.194 - vfree(vma->addr); 5.195 - return; 5.196 - } 5.197 - 5.198 - up->evtchn = evtchn; 5.199 - up->irq = bind_evtchn_to_irq(evtchn); 5.200 - up->shmem_frame = shmem_frame; 5.201 - up->usb_ring_base = (usbif_t *)vma->addr; 5.202 - up->status = CONNECTED; 5.203 - usbif_get(up); 5.204 - 5.205 - request_irq(up->irq, usbif_be_int, 0, "usbif-backend", up); 5.206 - 5.207 - connect->status = USBIF_BE_STATUS_OKAY; 5.208 -} 5.209 - 5.210 -/* Remove URBs for this interface before destroying it. */ 5.211 -void usbif_deschedule(usbif_priv_t *up) 5.212 -{ 5.213 - remove_from_usbif_list(up); 5.214 -} 5.215 - 5.216 -int usbif_disconnect(usbif_be_disconnect_t *disconnect, u8 rsp_id) 5.217 -{ 5.218 - domid_t domid = disconnect->domid; 5.219 - usbif_priv_t *up; 5.220 - 5.221 - up = usbif_find(domid); 5.222 - if ( unlikely(up == NULL) ) 5.223 - { 5.224 - DPRINTK("usbif_disconnect attempted for non-existent usbif" 5.225 - " (%u)\n", disconnect->domid); 5.226 - disconnect->status = USBIF_BE_STATUS_INTERFACE_NOT_FOUND; 5.227 - return 1; /* Caller will send response error message. */ 5.228 - } 5.229 - 5.230 - if ( up->status == CONNECTED ) 5.231 - { 5.232 - up->status = DISCONNECTING; 5.233 - up->disconnect_rspid = rsp_id; 5.234 - wmb(); /* Let other CPUs see the status change. */ 5.235 - free_irq(up->irq, up); 5.236 - usbif_deschedule(up); 5.237 - usbif_put(up); 5.238 - return 0; /* Caller should not send response message. */ 5.239 - } 5.240 - 5.241 - disconnect->status = USBIF_BE_STATUS_OKAY; 5.242 - return 1; 5.243 -} 5.244 - 5.245 -void __init usbif_interface_init(void) 5.246 -{ 5.247 - usbif_priv_cachep = kmem_cache_create("usbif_priv_cache", 5.248 - sizeof(usbif_priv_t), 5.249 - 0, 0, NULL, NULL); 5.250 - memset(usbif_priv_hash, 0, sizeof(usbif_priv_hash)); 5.251 -}
6.1 --- a/linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/backend/main.c Wed Jan 26 10:56:34 2005 +0000 6.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 6.3 @@ -1,1011 +0,0 @@ 6.4 -/****************************************************************************** 6.5 - * arch/xen/drivers/usbif/backend/main.c 6.6 - * 6.7 - * Backend for the Xen virtual USB driver - provides an abstraction of a 6.8 - * USB host controller to the corresponding frontend driver. 6.9 - * 6.10 - * by Mark Williamson, Copyright (c) 2004 Intel Research Cambridge 6.11 - * 6.12 - * Based on arch/xen/drivers/blkif/backend/main.c 6.13 - * Copyright (c) 2003-2004, Keir Fraser & Steve Hand 6.14 - */ 6.15 - 6.16 -#include "common.h" 6.17 - 6.18 - 6.19 -#include <linux/list.h> 6.20 -#include <linux/usb.h> 6.21 -#include <linux/spinlock.h> 6.22 -#include <linux/module.h> 6.23 -#include <linux/tqueue.h> 6.24 - 6.25 -/* 6.26 - * This is rather arbitrary. 6.27 - */ 6.28 -#define MAX_PENDING_REQS 4 6.29 -#define BATCH_PER_DOMAIN 1 6.30 - 6.31 -static unsigned long mmap_vstart; 6.32 - 6.33 -/* Needs to be sufficiently large that we can map the (large) buffers 6.34 - * the USB mass storage driver wants. */ 6.35 -#define MMAP_PAGES_PER_REQUEST \ 6.36 - (128) 6.37 -#define MMAP_PAGES \ 6.38 - (MAX_PENDING_REQS * MMAP_PAGES_PER_REQUEST) 6.39 - 6.40 -#define MMAP_VADDR(_req,_seg) \ 6.41 - (mmap_vstart + \ 6.42 - ((_req) * MMAP_PAGES_PER_REQUEST * PAGE_SIZE) + \ 6.43 - ((_seg) * PAGE_SIZE)) 6.44 - 6.45 -#define MIN(x,y) ( ( x < y ) ? x : y ) 6.46 - 6.47 -static spinlock_t owned_ports_lock; 6.48 -LIST_HEAD(owned_ports); 6.49 - 6.50 -/* A list of these structures is used to track ownership of physical USB 6.51 - * ports. */ 6.52 -typedef struct 6.53 -{ 6.54 - usbif_priv_t *usbif_priv; 6.55 - char path[16]; 6.56 - int guest_port; 6.57 - int enabled; 6.58 - struct list_head list; 6.59 - unsigned long guest_address; /* The USB device address that has been 6.60 - * assigned by the guest. */ 6.61 - int dev_present; /* Is there a device present? */ 6.62 - struct usb_device * dev; 6.63 - unsigned long ifaces; /* What interfaces are present on this device? */ 6.64 -} owned_port_t; 6.65 - 6.66 - 6.67 -/* 6.68 - * Each outstanding request that we've passed to the lower device layers has a 6.69 - * 'pending_req' allocated to it. The request is complete, the specified 6.70 - * domain has a response queued for it, with the saved 'id' passed back. 6.71 - */ 6.72 -typedef struct { 6.73 - usbif_priv_t *usbif_priv; 6.74 - usbif_iso_t *iso_sched; 6.75 - unsigned long id; 6.76 - int nr_pages; 6.77 - unsigned short operation; 6.78 - int status; 6.79 -} pending_req_t; 6.80 - 6.81 -/* 6.82 - * We can't allocate pending_req's in order, since they may complete out of 6.83 - * order. We therefore maintain an allocation ring. This ring also indicates 6.84 - * when enough work has been passed down -- at that point the allocation ring 6.85 - * will be empty. 6.86 - */ 6.87 -static pending_req_t pending_reqs[MAX_PENDING_REQS]; 6.88 -static unsigned char pending_ring[MAX_PENDING_REQS]; 6.89 -static spinlock_t pend_prod_lock = SPIN_LOCK_UNLOCKED; 6.90 - 6.91 -/* NB. We use a different index type to differentiate from shared blk rings. */ 6.92 -typedef unsigned int PEND_RING_IDX; 6.93 -#define MASK_PEND_IDX(_i) ((_i)&(MAX_PENDING_REQS-1)) 6.94 -static PEND_RING_IDX pending_prod, pending_cons; 6.95 -#define NR_PENDING_REQS (MAX_PENDING_REQS - pending_prod + pending_cons) 6.96 - 6.97 -static int do_usb_io_op(usbif_priv_t *usbif, int max_to_do); 6.98 -static void make_response(usbif_priv_t *usbif, unsigned long id, 6.99 - unsigned short op, int st, int inband, 6.100 - unsigned long actual_length); 6.101 -static void dispatch_usb_probe(usbif_priv_t *up, unsigned long id, unsigned long port); 6.102 -static void dispatch_usb_io(usbif_priv_t *up, usbif_request_t *req); 6.103 -static void dispatch_usb_reset(usbif_priv_t *up, unsigned long portid); 6.104 -static owned_port_t *usbif_find_port(char *); 6.105 - 6.106 - 6.107 -void dump_port(owned_port_t *p) 6.108 -{ 6.109 - printk("owned_port_t @ %p\n", p); 6.110 - printk(" usbif_priv @ %p\n", p->usbif_priv); 6.111 - printk(" path: %s\n", p->path); 6.112 - printk(" guest_port: %d\n", p->guest_port); 6.113 - printk(" guest_address: %ld\n", p->guest_address); 6.114 - printk(" dev_present: %d\n", p->dev_present); 6.115 - printk(" dev @ %p\n", p->dev); 6.116 - printk(" ifaces: 0x%lx\n", p->ifaces); 6.117 -} 6.118 - 6.119 - 6.120 - 6.121 -static void fast_flush_area(int idx, int nr_pages) 6.122 -{ 6.123 - multicall_entry_t mcl[MMAP_PAGES_PER_REQUEST]; 6.124 - int i; 6.125 - 6.126 - for ( i = 0; i < nr_pages; i++ ) 6.127 - { 6.128 - mcl[i].op = __HYPERVISOR_update_va_mapping; 6.129 - mcl[i].args[0] = MMAP_VADDR(idx, i) >> PAGE_SHIFT; 6.130 - mcl[i].args[1] = 0; 6.131 - mcl[i].args[2] = 0; 6.132 - } 6.133 - 6.134 - mcl[nr_pages-1].args[2] = UVMF_FLUSH_TLB; 6.135 - if ( unlikely(HYPERVISOR_multicall(mcl, nr_pages) != 0) ) 6.136 - BUG(); 6.137 -} 6.138 - 6.139 - 6.140 -/****************************************************************** 6.141 - * USB INTERFACE SCHEDULER LIST MAINTENANCE 6.142 - */ 6.143 - 6.144 -static struct list_head usbio_schedule_list; 6.145 -static spinlock_t usbio_schedule_list_lock; 6.146 - 6.147 -static int __on_usbif_list(usbif_priv_t *up) 6.148 -{ 6.149 - return up->usbif_list.next != NULL; 6.150 -} 6.151 - 6.152 -void remove_from_usbif_list(usbif_priv_t *up) 6.153 -{ 6.154 - unsigned long flags; 6.155 - if ( !__on_usbif_list(up) ) return; 6.156 - spin_lock_irqsave(&usbio_schedule_list_lock, flags); 6.157 - if ( __on_usbif_list(up) ) 6.158 - { 6.159 - list_del(&up->usbif_list); 6.160 - up->usbif_list.next = NULL; 6.161 - usbif_put(up); 6.162 - } 6.163 - spin_unlock_irqrestore(&usbio_schedule_list_lock, flags); 6.164 -} 6.165 - 6.166 -static void add_to_usbif_list_tail(usbif_priv_t *up) 6.167 -{ 6.168 - unsigned long flags; 6.169 - if ( __on_usbif_list(up) ) return; 6.170 - spin_lock_irqsave(&usbio_schedule_list_lock, flags); 6.171 - if ( !__on_usbif_list(up) && (up->status == CONNECTED) ) 6.172 - { 6.173 - list_add_tail(&up->usbif_list, &usbio_schedule_list); 6.174 - usbif_get(up); 6.175 - } 6.176 - spin_unlock_irqrestore(&usbio_schedule_list_lock, flags); 6.177 -} 6.178 - 6.179 - 6.180 -/****************************************************************** 6.181 - * COMPLETION CALLBACK -- Called as urb->complete() 6.182 - */ 6.183 - 6.184 -static void maybe_trigger_usbio_schedule(void); 6.185 - 6.186 -static void __end_usb_io_op(struct urb *purb) 6.187 -{ 6.188 - unsigned long flags; 6.189 - pending_req_t *pending_req; 6.190 - int pending_idx; 6.191 - 6.192 - pending_req = purb->context; 6.193 - 6.194 -/* printk("Completed for id = %p to 0x%lx - 0x%lx\n", pending_req->id, */ 6.195 -/* virt_to_machine(purb->transfer_buffer), */ 6.196 -/* virt_to_machine(purb->transfer_buffer) */ 6.197 -/* + pending_req->nr_pages * PAGE_SIZE); */ 6.198 - 6.199 - pending_idx = pending_req - pending_reqs; 6.200 - 6.201 - ASSERT(purb->actual_length <= purb->transfer_buffer_length); 6.202 - ASSERT(purb->actual_length <= pending_req->nr_pages * PAGE_SIZE); 6.203 - 6.204 - /* An error fails the entire request. */ 6.205 - if ( purb->status ) 6.206 - { 6.207 - printk("URB @ %p failed. Status %d\n", purb, purb->status); 6.208 - } 6.209 - 6.210 - if ( usb_pipetype(purb->pipe) == 0 ) 6.211 - { 6.212 - int i; 6.213 - usbif_iso_t *sched = (usbif_iso_t *)MMAP_VADDR(pending_idx, pending_req->nr_pages - 1); 6.214 - 6.215 - ASSERT(sched == pending_req->sched); 6.216 - 6.217 - // printk("writing back schedule at %p\n", sched); 6.218 - 6.219 - /* If we're dealing with an iso pipe, we need to copy back the schedule. */ 6.220 - for ( i = 0; i < purb->number_of_packets; i++ ) 6.221 - { 6.222 - sched[i].length = purb->iso_frame_desc[i].actual_length; 6.223 - ASSERT(sched[i].buffer_offset == 6.224 - purb->iso_frame_desc[i].offset); 6.225 - sched[i].status = purb->iso_frame_desc[i].status; 6.226 - } 6.227 - } 6.228 - 6.229 - // printk("Flushing %d pages\n", pending_req->nr_pages); 6.230 - fast_flush_area(pending_req - pending_reqs, pending_req->nr_pages); 6.231 - 6.232 - kfree(purb->setup_packet); 6.233 - 6.234 - spin_lock_irqsave(&pending_req->usbif_priv->usb_ring_lock, flags); 6.235 - make_response(pending_req->usbif_priv, pending_req->id, 6.236 - pending_req->operation, pending_req->status, 0, purb->actual_length); 6.237 - spin_unlock_irqrestore(&pending_req->usbif_priv->usb_ring_lock, flags); 6.238 - usbif_put(pending_req->usbif_priv); 6.239 - 6.240 - usb_free_urb(purb); 6.241 - 6.242 - /* Free the pending request. */ 6.243 - spin_lock_irqsave(&pend_prod_lock, flags); 6.244 - pending_ring[MASK_PEND_IDX(pending_prod++)] = pending_idx; 6.245 - spin_unlock_irqrestore(&pend_prod_lock, flags); 6.246 - 6.247 - rmb(); 6.248 - 6.249 - /* Check for anything still waiting in the rings, having freed a request... */ 6.250 - maybe_trigger_usbio_schedule(); 6.251 -} 6.252 - 6.253 -/****************************************************************** 6.254 - * SCHEDULER FUNCTIONS 6.255 - */ 6.256 - 6.257 -static DECLARE_WAIT_QUEUE_HEAD(usbio_schedule_wait); 6.258 - 6.259 -static int usbio_schedule(void *arg) 6.260 -{ 6.261 - DECLARE_WAITQUEUE(wq, current); 6.262 - 6.263 - usbif_priv_t *up; 6.264 - struct list_head *ent; 6.265 - 6.266 - daemonize(); 6.267 - 6.268 - for ( ; ; ) 6.269 - { 6.270 - /* Wait for work to do. */ 6.271 - add_wait_queue(&usbio_schedule_wait, &wq); 6.272 - set_current_state(TASK_INTERRUPTIBLE); 6.273 - if ( (NR_PENDING_REQS == MAX_PENDING_REQS) || 6.274 - list_empty(&usbio_schedule_list) ) 6.275 - schedule(); 6.276 - __set_current_state(TASK_RUNNING); 6.277 - remove_wait_queue(&usbio_schedule_wait, &wq); 6.278 - 6.279 - /* Queue up a batch of requests. */ 6.280 - while ( (NR_PENDING_REQS < MAX_PENDING_REQS) && 6.281 - !list_empty(&usbio_schedule_list) ) 6.282 - { 6.283 - ent = usbio_schedule_list.next; 6.284 - up = list_entry(ent, usbif_priv_t, usbif_list); 6.285 - usbif_get(up); 6.286 - remove_from_usbif_list(up); 6.287 - if ( do_usb_io_op(up, BATCH_PER_DOMAIN) ) 6.288 - add_to_usbif_list_tail(up); 6.289 - usbif_put(up); 6.290 - } 6.291 - } 6.292 -} 6.293 - 6.294 -static void maybe_trigger_usbio_schedule(void) 6.295 -{ 6.296 - /* 6.297 - * Needed so that two processes, who together make the following predicate 6.298 - * true, don't both read stale values and evaluate the predicate 6.299 - * incorrectly. Incredibly unlikely to stall the scheduler on x86, but... 6.300 - */ 6.301 - smp_mb(); 6.302 - 6.303 - if ( !list_empty(&usbio_schedule_list) ) 6.304 - wake_up(&usbio_schedule_wait); 6.305 -} 6.306 - 6.307 - 6.308 -/****************************************************************************** 6.309 - * NOTIFICATION FROM GUEST OS. 6.310 - */ 6.311 - 6.312 -irqreturn_t usbif_be_int(int irq, void *dev_id, struct pt_regs *regs) 6.313 -{ 6.314 - usbif_priv_t *up = dev_id; 6.315 - 6.316 - smp_mb(); 6.317 - 6.318 - add_to_usbif_list_tail(up); 6.319 - 6.320 - /* Will in fact /always/ trigger an io schedule in this case. */ 6.321 - maybe_trigger_usbio_schedule(); 6.322 - 6.323 - return IRQ_HANDLED; 6.324 -} 6.325 - 6.326 - 6.327 - 6.328 -/****************************************************************** 6.329 - * DOWNWARD CALLS -- These interface with the usb-device layer proper. 6.330 - */ 6.331 - 6.332 -static int do_usb_io_op(usbif_priv_t *up, int max_to_do) 6.333 -{ 6.334 - usbif_t *usb_ring = up->usb_ring_base; 6.335 - usbif_request_t *req; 6.336 - USBIF_RING_IDX i, rp; 6.337 - int more_to_do = 0; 6.338 - unsigned long flags; 6.339 - 6.340 - spin_lock_irqsave(&up->usb_ring_lock, flags); 6.341 - 6.342 - rp = usb_ring->req_prod; 6.343 - rmb(); /* Ensure we see queued requests up to 'rp'. */ 6.344 - 6.345 - /* Take items off the comms ring, taking care not to overflow. */ 6.346 - for ( i = up->usb_req_cons; 6.347 - (i != rp) && ((i-up->usb_resp_prod) != USBIF_RING_SIZE); 6.348 - i++ ) 6.349 - { 6.350 - if ( (max_to_do-- == 0) || (NR_PENDING_REQS == MAX_PENDING_REQS) ) 6.351 - { 6.352 - more_to_do = 1; 6.353 - break; 6.354 - } 6.355 - 6.356 - req = &usb_ring->ring[MASK_USBIF_IDX(i)].req; 6.357 - 6.358 - switch ( req->operation ) 6.359 - { 6.360 - case USBIF_OP_PROBE: 6.361 - dispatch_usb_probe(up, req->id, req->port); 6.362 - break; 6.363 - 6.364 - case USBIF_OP_IO: 6.365 - /* Assemble an appropriate URB. */ 6.366 - dispatch_usb_io(up, req); 6.367 - break; 6.368 - 6.369 - case USBIF_OP_RESET: 6.370 - dispatch_usb_reset(up, req->port); 6.371 - break; 6.372 - 6.373 - default: 6.374 - DPRINTK("error: unknown USB io operation [%d]\n", 6.375 - req->operation); 6.376 - make_response(up, req->id, req->operation, -EINVAL, 0, 0); 6.377 - break; 6.378 - } 6.379 - } 6.380 - 6.381 - up->usb_req_cons = i; 6.382 - 6.383 - spin_unlock_irqrestore(&up->usb_ring_lock, flags); 6.384 - 6.385 - return more_to_do; 6.386 -} 6.387 - 6.388 -static owned_port_t *find_guest_port(usbif_priv_t *up, int port) 6.389 -{ 6.390 - unsigned long flags; 6.391 - struct list_head *l; 6.392 - 6.393 - spin_lock_irqsave(&owned_ports_lock, flags); 6.394 - list_for_each(l, &owned_ports) 6.395 - { 6.396 - owned_port_t *p = list_entry(l, owned_port_t, list); 6.397 - if(p->usbif_priv == up && p->guest_port == port) 6.398 - { 6.399 - spin_unlock_irqrestore(&owned_ports_lock, flags); 6.400 - return p; 6.401 - } 6.402 - } 6.403 - spin_unlock_irqrestore(&owned_ports_lock, flags); 6.404 - 6.405 - return NULL; 6.406 -} 6.407 - 6.408 -static void dispatch_usb_reset(usbif_priv_t *up, unsigned long portid) 6.409 -{ 6.410 - owned_port_t *port = find_guest_port(up, portid); 6.411 - int ret = 0; 6.412 - 6.413 - 6.414 - /* Allowing the guest to actually reset the device causes more problems 6.415 - * than it's worth. We just fake it out in software but we will do a real 6.416 - * reset when the interface is destroyed. */ 6.417 - 6.418 -#if 0 6.419 - printk("Reset port %d\n", portid); 6.420 - 6.421 - dump_port(port); 6.422 -#endif 6.423 - 6.424 - port->guest_address = 0; 6.425 - /* If there's an attached device then the port is now enabled. */ 6.426 - if ( port->dev_present ) 6.427 - port->enabled = 1; 6.428 - else 6.429 - port->enabled = 0; 6.430 - 6.431 - make_response(up, 0, USBIF_OP_RESET, ret, 0, 0); 6.432 -} 6.433 - 6.434 -static void dispatch_usb_probe(usbif_priv_t *up, unsigned long id, unsigned long portid) 6.435 -{ 6.436 - owned_port_t *port = find_guest_port(up, portid); 6.437 - int ret; 6.438 - 6.439 - if ( port != NULL ) 6.440 - ret = port->dev_present; 6.441 - else 6.442 - { 6.443 - ret = -EINVAL; 6.444 - printk("dispatch_usb_probe(): invalid port probe request (port %ld)\n", 6.445 - portid); 6.446 - } 6.447 - 6.448 - /* Probe result is sent back in-band. Probes don't have an associated id 6.449 - * right now... */ 6.450 - make_response(up, id, USBIF_OP_PROBE, ret, portid, 0); 6.451 -} 6.452 - 6.453 -owned_port_t *find_port_for_request(usbif_priv_t *up, usbif_request_t *req); 6.454 - 6.455 -static void dump_request(usbif_request_t *req) 6.456 -{ 6.457 - printk("id = 0x%lx\n", req->id); 6.458 - 6.459 - printk("devnum %d\n", req->devnum); 6.460 - printk("endpoint 0x%x\n", req->endpoint); 6.461 - printk("direction %d\n", req->direction); 6.462 - printk("speed %d\n", req->speed); 6.463 - printk("pipe_type 0x%x\n", req->pipe_type); 6.464 - printk("transfer_buffer 0x%lx\n", req->transfer_buffer); 6.465 - printk("length 0x%lx\n", req->length); 6.466 - printk("transfer_flags 0x%lx\n", req->transfer_flags); 6.467 - printk("setup = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", 6.468 - req->setup[0], req->setup[1], req->setup[2], req->setup[3], 6.469 - req->setup[4], req->setup[5], req->setup[6], req->setup[7]); 6.470 - printk("iso_schedule = 0x%lx\n", req->iso_schedule); 6.471 - printk("num_iso %ld\n", req->num_iso); 6.472 -} 6.473 - 6.474 -void dump_urb(struct urb *urb) 6.475 -{ 6.476 - printk("dumping urb @ %p\n", urb); 6.477 - 6.478 -#define DUMP_URB_FIELD(name, format) printk(" " # name " " format "\n", urb-> name) 6.479 - 6.480 - DUMP_URB_FIELD(pipe, "0x%x"); 6.481 - DUMP_URB_FIELD(status, "%d"); 6.482 - DUMP_URB_FIELD(transfer_flags, "0x%x"); 6.483 - DUMP_URB_FIELD(transfer_buffer, "%p"); 6.484 - DUMP_URB_FIELD(transfer_buffer_length, "%d"); 6.485 - DUMP_URB_FIELD(actual_length, "%d"); 6.486 -} 6.487 - 6.488 - 6.489 -static void dispatch_usb_io(usbif_priv_t *up, usbif_request_t *req) 6.490 -{ 6.491 - unsigned long buffer_mach; 6.492 - int i = 0, offset = 0, 6.493 - pending_idx = pending_ring[MASK_PEND_IDX(pending_cons)]; 6.494 - pending_req_t *pending_req; 6.495 - unsigned long remap_prot; 6.496 - multicall_entry_t mcl[MMAP_PAGES_PER_REQUEST]; 6.497 - struct urb *purb = NULL; 6.498 - owned_port_t *port; 6.499 - unsigned char *setup; 6.500 - 6.501 -// dump_request(req); 6.502 - 6.503 - if ( NR_PENDING_REQS == MAX_PENDING_REQS ) 6.504 - { 6.505 - printk("usbback: Max requests already queued. Now giving up!\n"); 6.506 - 6.507 - return; 6.508 - } 6.509 - 6.510 - port = find_port_for_request(up, req); 6.511 - 6.512 - if(port == NULL) 6.513 - { 6.514 - printk("No such device! (%d)\n", req->devnum); 6.515 - dump_request(req); 6.516 - 6.517 - make_response(up, req->id, req->operation, -ENODEV, 0, 0); 6.518 - return; 6.519 - } 6.520 - 6.521 - setup = kmalloc(8, GFP_ATOMIC | GFP_NOIO); 6.522 - 6.523 - if ( setup == NULL ) 6.524 - goto no_mem; 6.525 - 6.526 - /* Copy request out for safety. */ 6.527 - memcpy(setup, req->setup, 8); 6.528 - 6.529 - if( setup[0] == 0x0 && setup[1] == 0x5) 6.530 - { 6.531 - /* To virtualise the USB address space, we need to intercept 6.532 - * set_address messages and emulate. From the USB specification: 6.533 - * bmRequestType = 0x0; 6.534 - * Brequest = SET_ADDRESS (i.e. 0x5) 6.535 - * wValue = device address 6.536 - * wIndex = 0 6.537 - * wLength = 0 6.538 - * data = None 6.539 - */ 6.540 - /* Store into the guest transfer buffer using cpu_to_le16 */ 6.541 - port->guest_address = le16_to_cpu(*(u16 *)(setup + 2)); 6.542 - /* Make a successful response. That was easy! */ 6.543 - 6.544 - make_response(up, req->id, req->operation, 0, 0, 0); 6.545 - 6.546 - kfree(setup); 6.547 - return; 6.548 - } 6.549 - else if ( setup[0] == 0x0 && setup[1] == 0x9 ) 6.550 - { 6.551 - /* The host kernel needs to know what device configuration is in use 6.552 - * because various error checks get confused otherwise. We just do 6.553 - * configuration settings here, under controlled conditions. 6.554 - */ 6.555 - usb_set_configuration(port->dev, setup[2]); 6.556 - 6.557 - make_response(up, req->id, req->operation, 0, 0, 0); 6.558 - 6.559 - kfree(setup); 6.560 - return; 6.561 - } 6.562 - 6.563 - else if ( setup[0] == 0x1 && setup[1] == 0xB ) 6.564 - { 6.565 - /* The host kernel needs to know what device interface is in use 6.566 - * because various error checks get confused otherwise. We just do 6.567 - * configuration settings here, under controlled conditions. 6.568 - */ 6.569 - usb_set_interface(port->dev, (setup[4] | setup[5] << 8), 6.570 - (setup[2] | setup[3] << 8) ); 6.571 - 6.572 - make_response(up, req->id, req->operation, 0, 0, 0); 6.573 - 6.574 - kfree(setup); 6.575 - return; 6.576 - } 6.577 - 6.578 - if ( ( req->transfer_buffer - (req->transfer_buffer & PAGE_MASK) 6.579 - + req->length ) 6.580 - > MMAP_PAGES_PER_REQUEST * PAGE_SIZE ) 6.581 - { 6.582 - printk("usbback: request of %d bytes too large, failing it\n", req->length); 6.583 - make_response(up, req->id, req->operation, -EINVAL, 0, 0); 6.584 - kfree(setup); 6.585 - return; 6.586 - } 6.587 - 6.588 - buffer_mach = req->transfer_buffer; 6.589 - 6.590 - if( buffer_mach == 0 ) 6.591 - goto no_remap; 6.592 - 6.593 - ASSERT((req->length >> PAGE_SHIFT) <= MMAP_PAGES_PER_REQUEST); 6.594 - ASSERT(buffer_mach); 6.595 - 6.596 - /* Always map writeable for now. */ 6.597 - remap_prot = _PAGE_PRESENT|_PAGE_DIRTY|_PAGE_ACCESSED|_PAGE_RW; 6.598 - 6.599 - for ( i = 0, offset = 0; offset < req->length; 6.600 - i++, offset += PAGE_SIZE ) 6.601 - { 6.602 - // printk("length = %d, offset = %d, looping!\n", req->length, offset); 6.603 - 6.604 - mcl[i].op = __HYPERVISOR_update_va_mapping_otherdomain; 6.605 - mcl[i].args[0] = MMAP_VADDR(pending_idx, i) >> PAGE_SHIFT; 6.606 - mcl[i].args[1] = ((buffer_mach & PAGE_MASK) + offset) | remap_prot; 6.607 - mcl[i].args[2] = 0; 6.608 - mcl[i].args[3] = up->domid; 6.609 - 6.610 - phys_to_machine_mapping[__pa(MMAP_VADDR(pending_idx, i))>>PAGE_SHIFT] = 6.611 - FOREIGN_FRAME((buffer_mach + offset) >> PAGE_SHIFT); 6.612 - // printk("i = %d\n", i); 6.613 - 6.614 - ASSERT(virt_to_machine(MMAP_VADDR(pending_idx, i)) 6.615 - == buffer_mach + i << PAGE_SHIFT); 6.616 - } 6.617 - 6.618 - if ( req->pipe_type == 0 && req->num_iso > 0 ) /* Maybe schedule ISO... */ 6.619 - { 6.620 - // printk("for iso, i = %d\n", i); 6.621 - /* Map in ISO schedule, if necessary. */ 6.622 - mcl[i].op = __HYPERVISOR_update_va_mapping_otherdomain; 6.623 - mcl[i].args[0] = MMAP_VADDR(pending_idx, i) >> PAGE_SHIFT; 6.624 - mcl[i].args[1] = (req->iso_schedule & PAGE_MASK) | remap_prot; 6.625 - mcl[i].args[2] = 0; 6.626 - mcl[i].args[3] = up->domid; 6.627 - 6.628 - phys_to_machine_mapping[__pa(MMAP_VADDR(pending_idx, i))>>PAGE_SHIFT] = 6.629 - FOREIGN_FRAME(req->iso_schedule >> PAGE_SHIFT); 6.630 - 6.631 - // printk("Mapped iso at %p\n", MMAP_VADDR(pending_idx, i)); 6.632 - i++; 6.633 - } 6.634 - 6.635 - // printk("Well we got this far!\n"); 6.636 - 6.637 - if ( unlikely(HYPERVISOR_multicall(mcl, i) != 0) ) 6.638 - BUG(); 6.639 - 6.640 - { 6.641 - int j; 6.642 - for ( j = 0; j < i; j++ ) 6.643 - { 6.644 - if ( unlikely(mcl[j].args[5] != 0) ) 6.645 - { 6.646 - printk("invalid buffer %d -- could not remap it\n", j); 6.647 - fast_flush_area(pending_idx, i); 6.648 - printk("sending invalid descriptor\n"); 6.649 - goto bad_descriptor; 6.650 - } 6.651 - } 6.652 - } 6.653 - 6.654 - no_remap: 6.655 - 6.656 - ASSERT(i <= MMAP_PAGES_PER_REQUEST); 6.657 - ASSERT(i * PAGE_SIZE >= req->length); 6.658 - 6.659 - /* We have to do this because some things might complete out of order. */ 6.660 - pending_req = &pending_reqs[pending_idx]; 6.661 - pending_req->usbif_priv= up; 6.662 - pending_req->id = req->id; 6.663 - pending_req->operation = req->operation; 6.664 - pending_req->nr_pages = i; 6.665 - 6.666 - 6.667 - 6.668 - pending_cons++; 6.669 - 6.670 - usbif_get(up); 6.671 - 6.672 - /* Fill out an actual request for the USB layer. */ 6.673 - purb = usb_alloc_urb(req->num_iso); 6.674 - 6.675 - if ( purb == NULL ) 6.676 - goto no_mem; 6.677 - 6.678 - purb->dev = port->dev; 6.679 - purb->context = pending_req; 6.680 - purb->transfer_buffer = (void *)MMAP_VADDR(pending_idx, 0) + (buffer_mach & ~PAGE_MASK); 6.681 - if(buffer_mach == 0) 6.682 - purb->transfer_buffer = NULL; 6.683 - purb->complete = __end_usb_io_op; 6.684 - purb->transfer_buffer_length = req->length; 6.685 - purb->transfer_flags = req->transfer_flags; 6.686 - 6.687 -/* if ( req->transfer_flags != 0 ) */ 6.688 -/* dump_request(req); */ 6.689 - 6.690 - purb->pipe = 0; 6.691 - purb->pipe |= req->direction << 7; 6.692 - purb->pipe |= port->dev->devnum << 8; 6.693 - purb->pipe |= req->speed << 26; 6.694 - purb->pipe |= req->pipe_type << 30; 6.695 - purb->pipe |= req->endpoint << 15; 6.696 - 6.697 - purb->number_of_packets = req->num_iso; 6.698 - 6.699 - /* Make sure there's always some kind of timeout. */ 6.700 - purb->timeout = ( req->timeout > 0 ) ? (req->timeout * HZ) / 1000 6.701 - : 1000; 6.702 - 6.703 - purb->setup_packet = setup; 6.704 - 6.705 - if ( req->pipe_type == 0 ) /* ISO */ 6.706 - { 6.707 - int j; 6.708 - usbif_iso_t *iso_sched = (usbif_iso_t *)MMAP_VADDR(pending_idx, i - 1); 6.709 - 6.710 - // printk("Reading iso sched at %p\n", iso_sched); 6.711 - 6.712 - /* If we're dealing with an iso pipe, we need to copy in a schedule. */ 6.713 - for ( j = 0; j < req->num_iso; j++ ) 6.714 - { 6.715 - purb->iso_frame_desc[j].length = iso_sched[j].length; 6.716 - purb->iso_frame_desc[j].offset = iso_sched[j].buffer_offset; 6.717 - iso_sched[j].status = 0; 6.718 - } 6.719 - pending_req->iso_sched = iso_sched; 6.720 - } 6.721 - 6.722 - { 6.723 - int ret; 6.724 - ret = usb_submit_urb(purb); 6.725 - 6.726 - // dump_urb(purb); 6.727 - 6.728 - if ( ret != 0 ) 6.729 - goto bad_descriptor; /* XXX free pending here! */ 6.730 - } 6.731 - 6.732 - return; 6.733 - 6.734 - bad_descriptor: 6.735 - kfree ( setup ); 6.736 - if ( purb != NULL ) 6.737 - usb_free_urb(purb); 6.738 - make_response(up, req->id, req->operation, -EINVAL, 0, 0); 6.739 - return; 6.740 - 6.741 - no_mem: 6.742 - if ( setup != NULL ) 6.743 - kfree(setup); 6.744 - make_response(up, req->id, req->operation, -ENOMEM, 0, 0); 6.745 - return; 6.746 -} 6.747 - 6.748 - 6.749 - 6.750 -/****************************************************************** 6.751 - * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING 6.752 - */ 6.753 - 6.754 - 6.755 -static void make_response(usbif_priv_t *up, unsigned long id, 6.756 - unsigned short op, int st, int inband, 6.757 - unsigned long length) 6.758 -{ 6.759 - usbif_response_t *resp; 6.760 - unsigned long flags; 6.761 - 6.762 -#if 0 6.763 - printk("usbback: Sending response:\n"); 6.764 - printk(" id = 0x%x\n", id); 6.765 - printk(" op = %d\n", op); 6.766 - printk(" status = %d\n", st); 6.767 - printk(" data = %d\n", inband); 6.768 - printk(" length = %d\n", length); 6.769 -#endif 6.770 - 6.771 - /* Place on the response ring for the relevant domain. */ 6.772 - spin_lock_irqsave(&up->usb_ring_lock, flags); 6.773 - resp = &up->usb_ring_base-> 6.774 - ring[MASK_USBIF_IDX(up->usb_resp_prod)].resp; 6.775 - resp->id = id; 6.776 - resp->operation = op; 6.777 - resp->status = st; 6.778 - resp->data = inband; 6.779 - resp->length = length; 6.780 - wmb(); /* Ensure other side can see the response fields. */ 6.781 - up->usb_ring_base->resp_prod = ++up->usb_resp_prod; 6.782 - spin_unlock_irqrestore(&up->usb_ring_lock, flags); 6.783 - 6.784 - /* Kick the relevant domain. */ 6.785 - notify_via_evtchn(up->evtchn); 6.786 -} 6.787 - 6.788 -/** 6.789 - * usbif_claim_port - claim devices on a port on behalf of guest 6.790 - * 6.791 - * Once completed, this will ensure that any device attached to that 6.792 - * port is claimed by this driver for use by the guest. 6.793 - */ 6.794 -int usbif_claim_port(usbif_be_claim_port_t *msg) 6.795 -{ 6.796 - owned_port_t *o_p; 6.797 - 6.798 - /* Sanity... */ 6.799 - if ( usbif_find_port(msg->path) != NULL ) 6.800 - { 6.801 - printk("usbback: Attempted to claim USB port " 6.802 - "we already own!\n"); 6.803 - return -EINVAL; 6.804 - } 6.805 - 6.806 - spin_lock_irq(&owned_ports_lock); 6.807 - 6.808 - /* No need for a slab cache - this should be infrequent. */ 6.809 - o_p = kmalloc(sizeof(owned_port_t), GFP_KERNEL); 6.810 - 6.811 - o_p->enabled = 0; 6.812 - o_p->usbif_priv = usbif_find(msg->domid); 6.813 - o_p->guest_port = msg->usbif_port; 6.814 - o_p->dev_present = 0; 6.815 - o_p->guest_address = 0; /* Default address. */ 6.816 - 6.817 - strcpy(o_p->path, msg->path); 6.818 - 6.819 - list_add(&o_p->list, &owned_ports); 6.820 - 6.821 - printk("usbback: Claimed USB port (%s) for %d.%d\n", o_p->path, 6.822 - msg->domid, msg->usbif_port); 6.823 - 6.824 - spin_unlock_irq(&owned_ports_lock); 6.825 - 6.826 - /* Force a reprobe for unclaimed devices. */ 6.827 - usb_scan_devices(); 6.828 - 6.829 - return 0; 6.830 -} 6.831 - 6.832 -owned_port_t *find_port_for_request(usbif_priv_t *up, usbif_request_t *req) 6.833 -{ 6.834 - unsigned long flags; 6.835 - struct list_head *port; 6.836 - 6.837 - /* I'm assuming this is not called from IRQ context - correct? I think 6.838 - * it's probably only called in response to control messages or plug events 6.839 - * in the USB hub kernel thread, so should be OK. */ 6.840 - spin_lock_irqsave(&owned_ports_lock, flags); 6.841 - list_for_each(port, &owned_ports) 6.842 - { 6.843 - owned_port_t *p = list_entry(port, owned_port_t, list); 6.844 - if(p->usbif_priv == up && p->guest_address == req->devnum && p->enabled ) 6.845 - { 6.846 -#if 0 6.847 - printk("Found port for devnum %d\n", req->devnum); 6.848 - 6.849 - dump_port(p); 6.850 -#endif 6.851 - return p; 6.852 - } 6.853 - } 6.854 - spin_unlock_irqrestore(&owned_ports_lock, flags); 6.855 - 6.856 - return NULL; 6.857 -} 6.858 - 6.859 -owned_port_t *usbif_find_port(char *path) 6.860 -{ 6.861 - struct list_head *port; 6.862 - unsigned long flags; 6.863 - 6.864 - spin_lock_irqsave(&owned_ports_lock, flags); 6.865 - list_for_each(port, &owned_ports) 6.866 - { 6.867 - owned_port_t *p = list_entry(port, owned_port_t, list); 6.868 - if(!strcmp(path, p->path)) 6.869 - { 6.870 - spin_unlock_irqrestore(&owned_ports_lock, flags); 6.871 - return p; 6.872 - } 6.873 - } 6.874 - spin_unlock_irqrestore(&owned_ports_lock, flags); 6.875 - 6.876 - return NULL; 6.877 -} 6.878 - 6.879 - 6.880 -static void *probe(struct usb_device *dev, unsigned iface, 6.881 - const struct usb_device_id *id) 6.882 -{ 6.883 - owned_port_t *p; 6.884 - 6.885 - /* We don't care what the device is - if we own the port, we want it. We 6.886 - * don't deal with device-specifics in this driver, so we don't care what 6.887 - * the device actually is ;-) */ 6.888 - if ( ( p = usbif_find_port(dev->devpath) ) != NULL ) 6.889 - { 6.890 - printk("usbback: claimed device attached to owned port\n"); 6.891 - 6.892 - p->dev_present = 1; 6.893 - p->dev = dev; 6.894 - set_bit(iface, &p->ifaces); 6.895 - 6.896 - return p->usbif_priv; 6.897 - } 6.898 - else 6.899 - printk("usbback: hotplug for non-owned port (%s), ignoring\n", dev->devpath); 6.900 - 6.901 - 6.902 - return NULL; 6.903 -} 6.904 - 6.905 -static void disconnect(struct usb_device *dev, void *usbif) 6.906 -{ 6.907 - /* Note the device is removed so we can tell the guest when it probes. */ 6.908 - owned_port_t *port = usbif_find_port(dev->devpath); 6.909 - port->dev_present = 0; 6.910 - port->dev = NULL; 6.911 - port->ifaces = 0; 6.912 -} 6.913 - 6.914 - 6.915 -struct usb_driver driver = 6.916 -{ 6.917 - .owner = THIS_MODULE, 6.918 - .name = "Xen USB Backend", 6.919 - .probe = probe, 6.920 - .disconnect = disconnect, 6.921 - .id_table = NULL, 6.922 -}; 6.923 - 6.924 -/* __usbif_release_port - internal mechanics for releasing a port */ 6.925 -void __usbif_release_port(owned_port_t *p) 6.926 -{ 6.927 - int i; 6.928 - 6.929 - for ( i = 0; p->ifaces != 0; i++) 6.930 - if ( p->ifaces & 1 << i ) 6.931 - { 6.932 - usb_driver_release_interface(&driver, usb_ifnum_to_if(p->dev, i)); 6.933 - clear_bit(i, &p->ifaces); 6.934 - } 6.935 - list_del(&p->list); 6.936 - 6.937 - /* Reset the real device. We don't simulate disconnect / probe for other 6.938 - * drivers in this kernel because we assume the device is completely under 6.939 - * the control of ourselves (i.e. the guest!). This should ensure that the 6.940 - * device is in a sane state for the next customer ;-) */ 6.941 -/* if ( p->dev != NULL) */ 6.942 -/* usb_reset_device(p->dev); */ 6.943 - 6.944 - kfree(p); 6.945 -} 6.946 - 6.947 - 6.948 -/** 6.949 - * usbif_release_port - stop claiming devices on a port on behalf of guest 6.950 - */ 6.951 -void usbif_release_port(usbif_be_release_port_t *msg) 6.952 -{ 6.953 - owned_port_t *p; 6.954 - 6.955 - spin_lock_irq(&owned_ports_lock); 6.956 - p = usbif_find_port(msg->path); 6.957 - __usbif_release_port(p); 6.958 - spin_unlock_irq(&owned_ports_lock); 6.959 -} 6.960 - 6.961 -void usbif_release_ports(usbif_priv_t *up) 6.962 -{ 6.963 - struct list_head *port, *tmp; 6.964 - unsigned long flags; 6.965 - 6.966 - spin_lock_irqsave(&owned_ports_lock, flags); 6.967 - list_for_each_safe(port, tmp, &owned_ports) 6.968 - { 6.969 - owned_port_t *p = list_entry(port, owned_port_t, list); 6.970 - if ( p->usbif_priv == up ) 6.971 - __usbif_release_port(p); 6.972 - } 6.973 - spin_unlock_irqrestore(&owned_ports_lock, flags); 6.974 -} 6.975 - 6.976 -static int __init usbif_init(void) 6.977 -{ 6.978 - int i; 6.979 - 6.980 - if ( !(xen_start_info.flags & SIF_INITDOMAIN) && 6.981 - !(xen_start_info.flags & SIF_USB_BE_DOMAIN) ) 6.982 - return 0; 6.983 - 6.984 - INIT_LIST_HEAD(&owned_ports); 6.985 - 6.986 - usb_register(&driver); 6.987 - 6.988 - usbif_interface_init(); 6.989 - 6.990 - if ( (mmap_vstart = allocate_empty_lowmem_region(MMAP_PAGES)) == 0 ) 6.991 - BUG(); 6.992 - 6.993 - pending_cons = 0; 6.994 - pending_prod = MAX_PENDING_REQS; 6.995 - memset(pending_reqs, 0, sizeof(pending_reqs)); 6.996 - for ( i = 0; i < MAX_PENDING_REQS; i++ ) 6.997 - pending_ring[i] = i; 6.998 - 6.999 - spin_lock_init(&usbio_schedule_list_lock); 6.1000 - INIT_LIST_HEAD(&usbio_schedule_list); 6.1001 - 6.1002 - if ( kernel_thread(usbio_schedule, 0, CLONE_FS | CLONE_FILES) < 0 ) 6.1003 - BUG(); 6.1004 - 6.1005 - usbif_ctrlif_init(); 6.1006 - 6.1007 - spin_lock_init(&owned_ports_lock); 6.1008 - 6.1009 - printk("Xen USB Backend Initialised"); 6.1010 - 6.1011 - return 0; 6.1012 -} 6.1013 - 6.1014 -__initcall(usbif_init);
7.1 --- a/linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/frontend/main.c Wed Jan 26 10:56:34 2005 +0000 7.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 7.3 @@ -1,1720 +0,0 @@ 7.4 -/* 7.5 - * Xen Virtual USB Frontend Driver 7.6 - * 7.7 - * This file contains the first version of the Xen virtual USB hub 7.8 - * that I've managed not to delete by mistake (3rd time lucky!). 7.9 - * 7.10 - * Based on Linux's uhci.c, original copyright notices are displayed 7.11 - * below. Portions also (c) 2004 Intel Research Cambridge 7.12 - * and (c) 2004 Mark Williamson 7.13 - * 7.14 - * Contact <mark.williamson@cl.cam.ac.uk> or 7.15 - * <xen-devel@lists.sourceforge.net> regarding this code. 7.16 - * 7.17 - * Still to be (maybe) implemented: 7.18 - * - multiple port 7.19 - * - multiple interfaces 7.20 - * - migration / backend restart support? 7.21 - * - unloading support 7.22 - * 7.23 - * Differences to a normal host controller: 7.24 - * - the backend does most of the mucky stuff so we don't have to do various 7.25 - * things that are necessary for a normal host controller (e.g. FSBR). 7.26 - * - we don't have any hardware, so status registers are simulated in software. 7.27 - */ 7.28 - 7.29 -/* 7.30 - * Universal Host Controller Interface driver for USB. 7.31 - * 7.32 - * Maintainer: Johannes Erdfelt <johannes@erdfelt.com> 7.33 - * 7.34 - * (C) Copyright 1999 Linus Torvalds 7.35 - * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com 7.36 - * (C) Copyright 1999 Randy Dunlap 7.37 - * (C) Copyright 1999 Georg Acher, acher@in.tum.de 7.38 - * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de 7.39 - * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch 7.40 - * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at 7.41 - * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface 7.42 - * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com). 7.43 - * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c) 7.44 - * 7.45 - * Intel documents this fairly well, and as far as I know there 7.46 - * are no royalties or anything like that, but even so there are 7.47 - * people who decided that they want to do the same thing in a 7.48 - * completely different way. 7.49 - * 7.50 - * WARNING! The USB documentation is downright evil. Most of it 7.51 - * is just crap, written by a committee. You're better off ignoring 7.52 - * most of it, the important stuff is: 7.53 - * - the low-level protocol (fairly simple but lots of small details) 7.54 - * - working around the horridness of the rest 7.55 - */ 7.56 - 7.57 -#include <linux/config.h> 7.58 -#include <linux/module.h> 7.59 -#include <linux/kernel.h> 7.60 -#include <linux/init.h> 7.61 -#include <linux/delay.h> 7.62 -#include <linux/ioport.h> 7.63 -#include <linux/sched.h> 7.64 -#include <linux/slab.h> 7.65 -#include <linux/smp_lock.h> 7.66 -#include <linux/errno.h> 7.67 -#include <linux/unistd.h> 7.68 -#include <linux/interrupt.h> 7.69 -#include <linux/spinlock.h> 7.70 -#ifdef CONFIG_USB_DEBUG 7.71 -#define DEBUG 7.72 -#else 7.73 -#undef DEBUG 7.74 -#endif 7.75 -#include <linux/usb.h> 7.76 - 7.77 -#include <asm/uaccess.h> 7.78 -#include <asm/irq.h> 7.79 -#include <asm/system.h> 7.80 - 7.81 -#include "xhci.h" 7.82 - 7.83 -#include <linux/pm.h> 7.84 - 7.85 -#include "../../../../../drivers/usb/hcd.h" 7.86 - 7.87 -#include "../usbif.h" 7.88 -#include <asm/ctrl_if.h> 7.89 -#include <asm/xen-public/io/domain_controller.h> 7.90 - 7.91 -/* 7.92 - * Version Information 7.93 - */ 7.94 -#define DRIVER_VERSION "v1.0" 7.95 -#define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, Mark Williamson" 7.96 -#define DRIVER_DESC "Xen Virtual USB Host Controller Interface driver" 7.97 - 7.98 -/* 7.99 - * debug = 0, no debugging messages 7.100 - * debug = 1, dump failed URB's except for stalls 7.101 - * debug = 2, dump all failed URB's (including stalls) 7.102 - */ 7.103 -#ifdef DEBUG 7.104 -static int debug = 1; 7.105 -#else 7.106 -static int debug = 0; 7.107 -#endif 7.108 -MODULE_PARM(debug, "i"); 7.109 -MODULE_PARM_DESC(debug, "Debug level"); 7.110 -static char *errbuf; 7.111 -#define ERRBUF_LEN (PAGE_SIZE * 8) 7.112 - 7.113 -static kmem_cache_t *xhci_up_cachep; /* urb_priv */ 7.114 - 7.115 -static int rh_submit_urb(struct urb *urb); 7.116 -static int rh_unlink_urb(struct urb *urb); 7.117 -//static int xhci_get_current_frame_number(struct usb_device *dev); 7.118 -static int xhci_unlink_urb(struct urb *urb); 7.119 -static void xhci_unlink_generic(struct urb *urb); 7.120 -static void xhci_call_completion(struct urb *urb); 7.121 -static void xhci_drain_ring(void); 7.122 - 7.123 -#define MAX_URB_LOOP 2048 /* Maximum number of linked URB's */ 7.124 - 7.125 -struct xhci *xhci; 7.126 - 7.127 -enum { USBIF_STATE_CONNECTED = 2, 7.128 - USBIF_STATE_DISCONNECTED = 1, 7.129 - USBIF_STATE_CLOSED =0 7.130 -}; 7.131 - 7.132 -static int awaiting_reset = 0; 7.133 - 7.134 -/** 7.135 - * xhci_construct_isoc - add isochronous information to a request 7.136 - */ 7.137 -int xhci_construct_isoc(usbif_request_t *req, struct urb *urb) 7.138 -{ 7.139 - usbif_iso_t *schedule; 7.140 - int i; 7.141 - struct urb_priv *urb_priv = urb->hcpriv; 7.142 - 7.143 - req->num_iso = urb->number_of_packets; 7.144 - schedule = (usbif_iso_t *)__get_free_page(GFP_KERNEL); 7.145 - 7.146 - if ( schedule == NULL ) 7.147 - return -ENOMEM; 7.148 - 7.149 - for ( i = 0; i < req->num_iso; i++ ) 7.150 - { 7.151 - schedule[i].buffer_offset = urb->iso_frame_desc[i].offset; 7.152 - schedule[i].length = urb->iso_frame_desc[i].length; 7.153 - } 7.154 - 7.155 - urb_priv->schedule = schedule; 7.156 - req->iso_schedule = virt_to_machine(schedule); 7.157 - 7.158 - return 0; 7.159 -} 7.160 - 7.161 -#define USBIF_RING_FULL ((xhci->usbif->req_prod - xhci->usb_resp_cons) == USBIF_RING_SIZE) 7.162 - 7.163 -static void dump_urb(struct urb *urb) 7.164 -{ 7.165 - printk("dumping urb @ %p\n", urb); 7.166 - 7.167 - printk("hcpriv = %p\n", urb->hcpriv); 7.168 - printk("next = %p\n", urb->next); 7.169 - printk("dev = %p\n", urb->dev); 7.170 - printk("pipe = 0x%lx\n", urb->pipe); 7.171 - printk("status = %d\n", urb->status); 7.172 - printk("transfer_flags = 0x%lx\n", urb->transfer_flags); 7.173 - printk("transfer_buffer = %p\n", urb->transfer_buffer); 7.174 - printk("transfer_buffer_length = %d\n", urb->transfer_buffer_length); 7.175 - printk("actual_length = %d\n", urb->actual_length); 7.176 - printk("bandwidth = %d\n", urb->bandwidth); 7.177 - printk("setup_packet = %p\n", urb->setup_packet); 7.178 - if ( urb->setup_packet != NULL ) 7.179 - printk("setup = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", 7.180 - urb->setup_packet[0], urb->setup_packet[1], urb->setup_packet[2], urb->setup_packet[3], 7.181 - urb->setup_packet[4], urb->setup_packet[5], urb->setup_packet[6], urb->setup_packet[7]); 7.182 - printk("complete = %p\n", urb->complete); 7.183 - printk("interval = %d\n", urb->interval); 7.184 - 7.185 -} 7.186 - 7.187 - 7.188 -static int 7.189 -xhci_queue_req(struct urb *urb) 7.190 -{ 7.191 - usbif_request_t *req; 7.192 - usbif_t *usbif = xhci->usbif; 7.193 - 7.194 -#if 0 7.195 - printk("usbif = %p, req_prod = %d (@ 0x%lx), resp_prod = %d, resp_cons = %d\n", 7.196 - usbif, usbif->req_prod, virt_to_machine(&usbif->req_prod), 7.197 - usbif->resp_prod, xhci->usb_resp_cons); 7.198 -#endif 7.199 - 7.200 - 7.201 -/* printk("Usbif_priv %p, want IO at 0x%lx\n", urb->hcpriv, virt_to_machine(urb->transfer_buffer)); */ 7.202 - 7.203 - if ( USBIF_RING_FULL ) 7.204 - { 7.205 - printk("xhci_queue_req(): USB ring full, not queuing request\n"); 7.206 - return -ENOBUFS; 7.207 - } 7.208 - 7.209 - /* Stick something in the shared communications ring. */ 7.210 - req = &usbif->ring[MASK_USBIF_IDX(usbif->req_prod)].req; 7.211 - 7.212 - req->operation = USBIF_OP_IO; 7.213 - req->port = 0; /* We don't care what the port is. */ 7.214 - req->id = (unsigned long) urb->hcpriv; 7.215 - req->transfer_buffer = virt_to_machine(urb->transfer_buffer); 7.216 - req->devnum = usb_pipedevice(urb->pipe); 7.217 - req->direction = usb_pipein(urb->pipe); 7.218 - req->speed = usb_pipeslow(urb->pipe); 7.219 - req->pipe_type = usb_pipetype(urb->pipe); 7.220 - req->length = urb->transfer_buffer_length; 7.221 - req->transfer_flags = urb->transfer_flags; 7.222 - req->endpoint = usb_pipeendpoint(urb->pipe); 7.223 - req->speed = usb_pipeslow(urb->pipe); 7.224 - req->timeout = urb->timeout * (1000 / HZ); 7.225 - 7.226 - if ( usb_pipetype(urb->pipe) == 0 ) /* ISO */ 7.227 - { 7.228 - int ret = xhci_construct_isoc(req, urb); 7.229 - if ( ret != 0 ) 7.230 - return ret; 7.231 - } 7.232 - 7.233 - if(urb->setup_packet != NULL) 7.234 - memcpy(req->setup, urb->setup_packet, 8); 7.235 - else 7.236 - memset(req->setup, 0, 8); 7.237 - 7.238 - wmb(); 7.239 - 7.240 - usbif->req_prod++; 7.241 - 7.242 - notify_via_evtchn(xhci->evtchn); 7.243 - 7.244 - // dump_urb(urb); 7.245 - 7.246 - return -EINPROGRESS; 7.247 -} 7.248 - 7.249 -static inline usbif_request_t * 7.250 -xhci_queue_probe(usbif_vdev_t port) 7.251 -{ 7.252 - usbif_request_t *req; 7.253 - usbif_t *usbif = xhci->usbif; 7.254 - 7.255 -#if 0 7.256 - printk("queuing probe: req_prod = %d (@ 0x%lx), resp_prod = %d, resp_cons = %d\n", 7.257 - usbif->req_prod, virt_to_machine(&usbif->req_prod), 7.258 - usbif->resp_prod, xhci->usb_resp_cons); 7.259 -#endif 7.260 - 7.261 - if ( USBIF_RING_FULL ) 7.262 - { 7.263 - printk("xhci_queue_probe(): USB ring full, not queuing request\n"); 7.264 - return NULL; 7.265 - } 7.266 - 7.267 - /* Stick something in the shared communications ring. */ 7.268 - req = &usbif->ring[MASK_USBIF_IDX(usbif->req_prod)].req; 7.269 - 7.270 - req->operation = USBIF_OP_PROBE; 7.271 - req->port = port; 7.272 - req->id = 0; 7.273 - req->transfer_buffer = 0; 7.274 - req->devnum = 0; 7.275 - req->direction = 0; 7.276 - req->speed = 0; 7.277 - req->pipe_type = 0; 7.278 - req->length = 0; 7.279 - req->transfer_flags = 0; 7.280 - req->endpoint = 0; 7.281 - req->speed = 0; 7.282 - 7.283 - wmb(); 7.284 - 7.285 - usbif->req_prod++; 7.286 - 7.287 - notify_via_evtchn(xhci->evtchn); 7.288 - 7.289 - return req; 7.290 -} 7.291 - 7.292 -static int 7.293 -xhci_port_reset(usbif_vdev_t port) 7.294 -{ 7.295 - usbif_request_t *req; 7.296 - usbif_t *usbif = xhci->usbif; 7.297 - 7.298 - /* We only reset one port at a time, so we only need one variable per 7.299 - * hub. */ 7.300 - awaiting_reset = 1; 7.301 - 7.302 - /* Stick something in the shared communications ring. */ 7.303 - req = &usbif->ring[MASK_USBIF_IDX(usbif->req_prod)].req; 7.304 - 7.305 - req->operation = USBIF_OP_RESET; 7.306 - req->port = port; 7.307 - 7.308 - wmb(); 7.309 - 7.310 - usbif->req_prod++; 7.311 - 7.312 - notify_via_evtchn(xhci->evtchn); 7.313 - 7.314 - while ( awaiting_reset > 0 ) 7.315 - { 7.316 - mdelay(1); 7.317 - xhci_drain_ring(); 7.318 - } 7.319 - 7.320 - return awaiting_reset; 7.321 -} 7.322 - 7.323 -static void xhci_show_resp(usbif_response_t *r) 7.324 -{ 7.325 - printk("id=0x%lx, op=0x%x, data=0x%x, status=0x%x, length=0x%lx\n", 7.326 - r->id, r->operation, r->data, r->status, r->length); 7.327 -} 7.328 - 7.329 - 7.330 -/* 7.331 - * Only the USB core should call xhci_alloc_dev and xhci_free_dev 7.332 - */ 7.333 -static int xhci_alloc_dev(struct usb_device *dev) 7.334 -{ 7.335 - return 0; 7.336 -} 7.337 - 7.338 -static int xhci_free_dev(struct usb_device *dev) 7.339 -{ 7.340 - return 0; 7.341 -} 7.342 - 7.343 -static inline void xhci_add_complete(struct urb *urb) 7.344 -{ 7.345 - struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 7.346 - unsigned long flags; 7.347 - 7.348 - spin_lock_irqsave(&xhci->complete_list_lock, flags); 7.349 - list_add_tail(&urbp->complete_list, &xhci->complete_list); 7.350 - spin_unlock_irqrestore(&xhci->complete_list_lock, flags); 7.351 -} 7.352 - 7.353 -/* When this returns, the owner of the URB may free its 7.354 - * storage. 7.355 - * 7.356 - * We spin and wait for the URB to complete before returning. 7.357 - */ 7.358 -static void xhci_delete_urb(struct urb *urb) 7.359 -{ 7.360 - struct urb_priv *urbp; 7.361 - 7.362 - urbp = urb->hcpriv; 7.363 - 7.364 - /* If there's no urb_priv structure for this URB then it can't have 7.365 - * been submitted at all. */ 7.366 - if ( urbp == NULL ) 7.367 - return; 7.368 - 7.369 - /* For now we just spin until the URB completes. It shouldn't take too 7.370 - * long and we don't expect to have to do this very often. */ 7.371 - while ( urb->status == -EINPROGRESS ) 7.372 - { 7.373 - xhci_drain_ring(); 7.374 - mdelay(1); 7.375 - } 7.376 - 7.377 - /* Now we know that further transfers to the buffer won't 7.378 - * occur, so we can safely return. */ 7.379 -} 7.380 - 7.381 -static struct urb_priv *xhci_alloc_urb_priv(struct urb *urb) 7.382 -{ 7.383 - struct urb_priv *urbp; 7.384 - 7.385 - urbp = kmem_cache_alloc(xhci_up_cachep, SLAB_ATOMIC); 7.386 - if (!urbp) { 7.387 - err("xhci_alloc_urb_priv: couldn't allocate memory for urb_priv\n"); 7.388 - return NULL; 7.389 - } 7.390 - 7.391 - memset((void *)urbp, 0, sizeof(*urbp)); 7.392 - 7.393 - urbp->inserttime = jiffies; 7.394 - urbp->urb = urb; 7.395 - urbp->dev = urb->dev; 7.396 - 7.397 - INIT_LIST_HEAD(&urbp->complete_list); 7.398 - 7.399 - urb->hcpriv = urbp; 7.400 - 7.401 - return urbp; 7.402 -} 7.403 - 7.404 -/* 7.405 - * MUST be called with urb->lock acquired 7.406 - */ 7.407 -/* When is this called? Do we need to stop the transfer (as we 7.408 - * currently do)? */ 7.409 -static void xhci_destroy_urb_priv(struct urb *urb) 7.410 -{ 7.411 - struct urb_priv *urbp; 7.412 - 7.413 - urbp = (struct urb_priv *)urb->hcpriv; 7.414 - if (!urbp) 7.415 - return; 7.416 - 7.417 - if (!list_empty(&urb->urb_list)) 7.418 - warn("xhci_destroy_urb_priv: urb %p still on xhci->urb_list or xhci->remove_list", urb); 7.419 - 7.420 - if (!list_empty(&urbp->complete_list)) 7.421 - warn("xhci_destroy_urb_priv: urb %p still on xhci->complete_list", urb); 7.422 - 7.423 - kmem_cache_free(xhci_up_cachep, urb->hcpriv); 7.424 - 7.425 - urb->hcpriv = NULL; 7.426 -} 7.427 - 7.428 -/** 7.429 - * Try to find URBs in progress on the same pipe to the same device. 7.430 - * 7.431 - * MUST be called with xhci->urb_list_lock acquired 7.432 - */ 7.433 -static struct urb *xhci_find_urb_ep(struct xhci *xhci, struct urb *urb) 7.434 -{ 7.435 - struct list_head *tmp, *head; 7.436 - 7.437 - /* We don't match Isoc transfers since they are special */ 7.438 - if (usb_pipeisoc(urb->pipe)) 7.439 - return NULL; 7.440 - 7.441 - head = &xhci->urb_list; 7.442 - tmp = head->next; 7.443 - while (tmp != head) { 7.444 - struct urb *u = list_entry(tmp, struct urb, urb_list); 7.445 - 7.446 - tmp = tmp->next; 7.447 - 7.448 - if (u->dev == urb->dev && u->pipe == urb->pipe && 7.449 - u->status == -EINPROGRESS) 7.450 - return u; 7.451 - } 7.452 - 7.453 - return NULL; 7.454 -} 7.455 - 7.456 -static int xhci_submit_urb(struct urb *urb) 7.457 -{ 7.458 - int ret = -EINVAL; 7.459 - unsigned long flags; 7.460 - struct urb *eurb; 7.461 - int bustime; 7.462 - 7.463 -#if 0 7.464 - printk("submitting urb @ %p for dev @ %p, devnum = %d path %s\n", 7.465 - urb, urb->dev, urb->dev->devnum, urb->dev->devpath); 7.466 -#endif 7.467 - 7.468 - if (!urb) 7.469 - return -EINVAL; 7.470 - 7.471 - if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv) { 7.472 - warn("xhci_submit_urb: urb %p belongs to disconnected device or bus?", urb); 7.473 - return -ENODEV; 7.474 - } 7.475 - 7.476 - if ( urb->dev->devpath == NULL ) 7.477 - { 7.478 - printk("BARF!\n"); 7.479 - BUG(); 7.480 - } 7.481 - 7.482 - 7.483 - 7.484 - usb_inc_dev_use(urb->dev); 7.485 - 7.486 - spin_lock_irqsave(&xhci->urb_list_lock, flags); 7.487 - spin_lock(&urb->lock); 7.488 - 7.489 - if (urb->status == -EINPROGRESS || urb->status == -ECONNRESET || 7.490 - urb->status == -ECONNABORTED) { 7.491 - dbg("xhci_submit_urb: urb not available to submit (status = %d)", urb->status); 7.492 - /* Since we can have problems on the out path */ 7.493 - spin_unlock(&urb->lock); 7.494 - spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 7.495 - usb_dec_dev_use(urb->dev); 7.496 - 7.497 - return ret; 7.498 - } 7.499 - 7.500 - INIT_LIST_HEAD(&urb->urb_list); 7.501 - if (!xhci_alloc_urb_priv(urb)) { 7.502 - ret = -ENOMEM; 7.503 - 7.504 - goto out; 7.505 - } 7.506 - 7.507 - ( (struct urb_priv *)urb->hcpriv )->in_progress = 1; 7.508 - 7.509 - eurb = xhci_find_urb_ep(xhci, urb); 7.510 - if (eurb && !(urb->transfer_flags & USB_QUEUE_BULK)) { 7.511 - ret = -ENXIO; 7.512 - 7.513 - goto out; 7.514 - } 7.515 - 7.516 - /* Short circuit the virtual root hub */ 7.517 - if (urb->dev == xhci->rh.dev) { 7.518 - ret = rh_submit_urb(urb); 7.519 - 7.520 - goto out; 7.521 - } 7.522 - 7.523 - if ( usb_pipedevice(urb->pipe) == 1 ) 7.524 - printk("dev = %p, dev->path = %s, rh.dev = %p, rh.dev.devnum = %d rh.dev->path = %s!\n", 7.525 - urb->dev, urb->dev->devpath, xhci->rh.dev, xhci->rh.dev->devnum, xhci->rh.dev->devpath); 7.526 - 7.527 - switch (usb_pipetype(urb->pipe)) { 7.528 - case PIPE_CONTROL: 7.529 - ret = xhci_queue_req(urb); 7.530 - break; 7.531 - case PIPE_INTERRUPT: 7.532 - if (urb->bandwidth == 0) { /* not yet checked/allocated */ 7.533 - bustime = usb_check_bandwidth(urb->dev, urb); 7.534 - if (bustime < 0) 7.535 - ret = bustime; 7.536 - else { 7.537 - ret = xhci_queue_req(urb); 7.538 - if (ret == -EINPROGRESS) 7.539 - usb_claim_bandwidth(urb->dev, urb, bustime, 0); 7.540 - } 7.541 - } else /* bandwidth is already set */ 7.542 - ret = xhci_queue_req(urb); 7.543 - break; 7.544 - case PIPE_BULK: 7.545 - ret = xhci_queue_req(urb); 7.546 - break; 7.547 - case PIPE_ISOCHRONOUS: 7.548 - if (urb->bandwidth == 0) { /* not yet checked/allocated */ 7.549 - if (urb->number_of_packets <= 0) { 7.550 - ret = -EINVAL; 7.551 - break; 7.552 - } 7.553 - bustime = usb_check_bandwidth(urb->dev, urb); 7.554 - if (bustime < 0) { 7.555 - ret = bustime; 7.556 - break; 7.557 - } 7.558 - 7.559 - ret = xhci_queue_req(urb); 7.560 - if (ret == -EINPROGRESS) 7.561 - usb_claim_bandwidth(urb->dev, urb, bustime, 1); 7.562 - } else /* bandwidth is already set */ 7.563 - ret = xhci_queue_req(urb); 7.564 - break; 7.565 - } 7.566 - 7.567 -out: 7.568 - urb->status = ret; 7.569 - 7.570 - if (ret == -EINPROGRESS) { 7.571 - /* We use _tail to make find_urb_ep more efficient */ 7.572 - list_add_tail(&urb->urb_list, &xhci->urb_list); 7.573 - 7.574 - spin_unlock(&urb->lock); 7.575 - spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 7.576 - 7.577 - return 0; 7.578 - } 7.579 - 7.580 - xhci_unlink_generic(urb); 7.581 - 7.582 - spin_unlock(&urb->lock); 7.583 - spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 7.584 - 7.585 - /* Only call completion if it was successful */ 7.586 - if (!ret) 7.587 - xhci_call_completion(urb); 7.588 - 7.589 - return ret; 7.590 -} 7.591 - 7.592 -/* 7.593 - * Return the result of a transfer 7.594 - * 7.595 - * MUST be called with urb_list_lock acquired 7.596 - */ 7.597 -static void xhci_transfer_result(struct xhci *xhci, struct urb *urb) 7.598 -{ 7.599 - int ret = 0; 7.600 - unsigned long flags; 7.601 - struct urb_priv *urbp; 7.602 - 7.603 - /* The root hub is special */ 7.604 - if (urb->dev == xhci->rh.dev) 7.605 - return; 7.606 - 7.607 - spin_lock_irqsave(&urb->lock, flags); 7.608 - 7.609 - urbp = (struct urb_priv *)urb->hcpriv; 7.610 - 7.611 - if ( ( (struct urb_priv *)urb->hcpriv )->in_progress ) 7.612 - ret = -EINPROGRESS; 7.613 - 7.614 - if (urb->actual_length < urb->transfer_buffer_length) { 7.615 - if (urb->transfer_flags & USB_DISABLE_SPD) { 7.616 - ret = -EREMOTEIO; 7.617 - } 7.618 - } 7.619 - 7.620 - if (urb->status == -EPIPE) 7.621 - { 7.622 - ret = urb->status; 7.623 - /* endpoint has stalled - mark it halted */ 7.624 - usb_endpoint_halt(urb->dev, usb_pipeendpoint(urb->pipe), 7.625 - usb_pipeout(urb->pipe)); 7.626 - } 7.627 - 7.628 - if ((debug == 1 && ret != 0 && ret != -EPIPE) || 7.629 - (ret != 0 && debug > 1)) { 7.630 - /* Some debugging code */ 7.631 - dbg("xhci_result_interrupt/bulk() failed with status %x", 7.632 - status); 7.633 - } 7.634 - 7.635 - if (ret == -EINPROGRESS) 7.636 - goto out; 7.637 - 7.638 - switch (usb_pipetype(urb->pipe)) { 7.639 - case PIPE_CONTROL: 7.640 - case PIPE_BULK: 7.641 - case PIPE_ISOCHRONOUS: 7.642 - /* Release bandwidth for Interrupt or Isoc. transfers */ 7.643 - /* Spinlock needed ? */ 7.644 - if (urb->bandwidth) 7.645 - usb_release_bandwidth(urb->dev, urb, 1); 7.646 - xhci_unlink_generic(urb); 7.647 - break; 7.648 - case PIPE_INTERRUPT: 7.649 - /* Interrupts are an exception */ 7.650 - if (urb->interval) 7.651 - goto out_complete; 7.652 - 7.653 - /* Release bandwidth for Interrupt or Isoc. transfers */ 7.654 - /* Spinlock needed ? */ 7.655 - if (urb->bandwidth) 7.656 - usb_release_bandwidth(urb->dev, urb, 0); 7.657 - xhci_unlink_generic(urb); 7.658 - break; 7.659 - default: 7.660 - info("xhci_transfer_result: unknown pipe type %d for urb %p\n", 7.661 - usb_pipetype(urb->pipe), urb); 7.662 - } 7.663 - 7.664 - /* Remove it from xhci->urb_list */ 7.665 - list_del_init(&urb->urb_list); 7.666 - 7.667 -out_complete: 7.668 - xhci_add_complete(urb); 7.669 - 7.670 -out: 7.671 - spin_unlock_irqrestore(&urb->lock, flags); 7.672 -} 7.673 - 7.674 -/* 7.675 - * MUST be called with urb->lock acquired 7.676 - */ 7.677 -static void xhci_unlink_generic(struct urb *urb) 7.678 -{ 7.679 - struct urb_priv *urbp = urb->hcpriv; 7.680 - 7.681 - /* We can get called when urbp allocation fails, so check */ 7.682 - if (!urbp) 7.683 - return; 7.684 - 7.685 - /* ??? This function is now so minimal it doesn't do much. Do we really 7.686 - * need it? */ 7.687 - 7.688 - xhci_delete_urb(urb); 7.689 -} 7.690 - 7.691 -static int xhci_unlink_urb(struct urb *urb) 7.692 -{ 7.693 - unsigned long flags; 7.694 - struct urb_priv *urbp = urb->hcpriv; 7.695 - 7.696 - if (!urb) 7.697 - return -EINVAL; 7.698 - 7.699 - if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv) 7.700 - return -ENODEV; 7.701 - 7.702 - spin_lock_irqsave(&xhci->urb_list_lock, flags); 7.703 - spin_lock(&urb->lock); 7.704 - 7.705 - /* Release bandwidth for Interrupt or Isoc. transfers */ 7.706 - /* Spinlock needed ? */ 7.707 - if (urb->bandwidth) { 7.708 - switch (usb_pipetype(urb->pipe)) { 7.709 - case PIPE_INTERRUPT: 7.710 - usb_release_bandwidth(urb->dev, urb, 0); 7.711 - break; 7.712 - case PIPE_ISOCHRONOUS: 7.713 - usb_release_bandwidth(urb->dev, urb, 1); 7.714 - break; 7.715 - default: 7.716 - break; 7.717 - } 7.718 - } 7.719 - 7.720 - if (urb->status != -EINPROGRESS) { 7.721 - spin_unlock(&urb->lock); 7.722 - spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 7.723 - return 0; 7.724 - } 7.725 - 7.726 - list_del_init(&urb->urb_list); 7.727 - 7.728 - xhci_unlink_generic(urb); 7.729 - 7.730 - /* Short circuit the virtual root hub */ 7.731 - if (urb->dev == xhci->rh.dev) { 7.732 - rh_unlink_urb(urb); 7.733 - 7.734 - spin_unlock(&urb->lock); 7.735 - spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 7.736 - 7.737 - xhci_call_completion(urb); 7.738 - } else { 7.739 - if (urb->transfer_flags & USB_ASYNC_UNLINK) { 7.740 - urbp->status = urb->status = -ECONNABORTED; 7.741 - 7.742 - spin_lock(&xhci->urb_remove_list_lock); 7.743 - 7.744 - list_add(&urb->urb_list, &xhci->urb_remove_list); 7.745 - 7.746 - spin_unlock(&xhci->urb_remove_list_lock); 7.747 - 7.748 - spin_unlock(&urb->lock); 7.749 - spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 7.750 - 7.751 - } else { 7.752 - urb->status = -ENOENT; 7.753 - 7.754 - spin_unlock(&urb->lock); 7.755 - spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 7.756 - 7.757 - if (in_interrupt()) { /* wait at least 1 frame */ 7.758 - static int errorcount = 10; 7.759 - 7.760 - if (errorcount--) 7.761 - dbg("xhci_unlink_urb called from interrupt for urb %p", urb); 7.762 - udelay(1000); 7.763 - } else 7.764 - schedule_timeout(1+1*HZ/1000); 7.765 - 7.766 - xhci_call_completion(urb); 7.767 - } 7.768 - } 7.769 - 7.770 - return 0; 7.771 -} 7.772 - 7.773 - 7.774 -struct usb_operations xhci_device_operations = { 7.775 - .allocate = xhci_alloc_dev, 7.776 - .deallocate = xhci_free_dev, 7.777 - /* It doesn't look like any drivers actually care what the frame number 7.778 - * is at the moment! If necessary, we could approximate the current 7.779 - * frame nubmer by passing it from the backend in response messages. */ 7.780 - .get_frame_number = NULL, 7.781 - .submit_urb = xhci_submit_urb, 7.782 - .unlink_urb = xhci_unlink_urb 7.783 -}; 7.784 - 7.785 -/* Virtual Root Hub */ 7.786 - 7.787 -static __u8 root_hub_dev_des[] = 7.788 -{ 7.789 - 0x12, /* __u8 bLength; */ 7.790 - 0x01, /* __u8 bDescriptorType; Device */ 7.791 - 0x00, /* __u16 bcdUSB; v1.0 */ 7.792 - 0x01, 7.793 - 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ 7.794 - 0x00, /* __u8 bDeviceSubClass; */ 7.795 - 0x00, /* __u8 bDeviceProtocol; */ 7.796 - 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */ 7.797 - 0x00, /* __u16 idVendor; */ 7.798 - 0x00, 7.799 - 0x00, /* __u16 idProduct; */ 7.800 - 0x00, 7.801 - 0x00, /* __u16 bcdDevice; */ 7.802 - 0x00, 7.803 - 0x00, /* __u8 iManufacturer; */ 7.804 - 0x02, /* __u8 iProduct; */ 7.805 - 0x01, /* __u8 iSerialNumber; */ 7.806 - 0x01 /* __u8 bNumConfigurations; */ 7.807 -}; 7.808 - 7.809 - 7.810 -/* Configuration descriptor */ 7.811 -static __u8 root_hub_config_des[] = 7.812 -{ 7.813 - 0x09, /* __u8 bLength; */ 7.814 - 0x02, /* __u8 bDescriptorType; Configuration */ 7.815 - 0x19, /* __u16 wTotalLength; */ 7.816 - 0x00, 7.817 - 0x01, /* __u8 bNumInterfaces; */ 7.818 - 0x01, /* __u8 bConfigurationValue; */ 7.819 - 0x00, /* __u8 iConfiguration; */ 7.820 - 0x40, /* __u8 bmAttributes; 7.821 - Bit 7: Bus-powered, 6: Self-powered, 7.822 - Bit 5 Remote-wakeup, 4..0: resvd */ 7.823 - 0x00, /* __u8 MaxPower; */ 7.824 - 7.825 - /* interface */ 7.826 - 0x09, /* __u8 if_bLength; */ 7.827 - 0x04, /* __u8 if_bDescriptorType; Interface */ 7.828 - 0x00, /* __u8 if_bInterfaceNumber; */ 7.829 - 0x00, /* __u8 if_bAlternateSetting; */ 7.830 - 0x01, /* __u8 if_bNumEndpoints; */ 7.831 - 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ 7.832 - 0x00, /* __u8 if_bInterfaceSubClass; */ 7.833 - 0x00, /* __u8 if_bInterfaceProtocol; */ 7.834 - 0x00, /* __u8 if_iInterface; */ 7.835 - 7.836 - /* endpoint */ 7.837 - 0x07, /* __u8 ep_bLength; */ 7.838 - 0x05, /* __u8 ep_bDescriptorType; Endpoint */ 7.839 - 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ 7.840 - 0x03, /* __u8 ep_bmAttributes; Interrupt */ 7.841 - 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */ 7.842 - 0x00, 7.843 - 0xff /* __u8 ep_bInterval; 255 ms */ 7.844 -}; 7.845 - 7.846 -static __u8 root_hub_hub_des[] = 7.847 -{ 7.848 - 0x09, /* __u8 bLength; */ 7.849 - 0x29, /* __u8 bDescriptorType; Hub-descriptor */ 7.850 - 0x02, /* __u8 bNbrPorts; */ 7.851 - 0x00, /* __u16 wHubCharacteristics; */ 7.852 - 0x00, 7.853 - 0x01, /* __u8 bPwrOn2pwrGood; 2ms */ 7.854 - 0x00, /* __u8 bHubContrCurrent; 0 mA */ 7.855 - 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */ 7.856 - 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */ 7.857 -}; 7.858 - 7.859 -/* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */ 7.860 -static int rh_send_irq(struct urb *urb) 7.861 -{ 7.862 - struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 7.863 - xhci_port_t *ports = xhci->rh.ports; 7.864 - unsigned long flags; 7.865 - int i, len = 1; 7.866 - __u16 data = 0; 7.867 - 7.868 - spin_lock_irqsave(&urb->lock, flags); 7.869 - for (i = 0; i < xhci->rh.numports; i++) { 7.870 - /* MAW: No idea what the old code was doing here or why it worked. 7.871 - * This implementation sets a bit if anything at all has changed on the 7.872 - * port, as per USB spec 11.12 */ 7.873 - data |= (ports[i].cs_chg || ports[i].pe_chg ) 7.874 - ? (1 << (i + 1)) 7.875 - : 0; 7.876 - 7.877 - len = (i + 1) / 8 + 1; 7.878 - } 7.879 - 7.880 - *(__u16 *) urb->transfer_buffer = cpu_to_le16(data); 7.881 - urb->actual_length = len; 7.882 - urbp->status = 0; 7.883 - 7.884 - spin_unlock_irqrestore(&urb->lock, flags); 7.885 - 7.886 - if ((data > 0) && (xhci->rh.send != 0)) { 7.887 - dbg("root-hub INT complete: data: %x", data); 7.888 - xhci_call_completion(urb); 7.889 - } 7.890 - 7.891 - return 0; 7.892 -} 7.893 - 7.894 -/* Virtual Root Hub INTs are polled by this timer every "interval" ms */ 7.895 -static int rh_init_int_timer(struct urb *urb); 7.896 - 7.897 -static void rh_int_timer_do(unsigned long ptr) 7.898 -{ 7.899 - struct urb *urb = (struct urb *)ptr; 7.900 - struct list_head list, *tmp, *head; 7.901 - unsigned long flags; 7.902 - int i; 7.903 - 7.904 - for ( i = 0; i < xhci->rh.numports; i++) 7.905 - xhci_queue_probe(i); 7.906 - 7.907 - if (xhci->rh.send) 7.908 - rh_send_irq(urb); 7.909 - 7.910 - INIT_LIST_HEAD(&list); 7.911 - 7.912 - spin_lock_irqsave(&xhci->urb_list_lock, flags); 7.913 - head = &xhci->urb_list; 7.914 - tmp = head->next; 7.915 - while (tmp != head) { 7.916 - struct urb *u = list_entry(tmp, struct urb, urb_list); 7.917 - struct urb_priv *up = (struct urb_priv *)u->hcpriv; 7.918 - 7.919 - tmp = tmp->next; 7.920 - 7.921 - spin_lock(&u->lock); 7.922 - 7.923 - /* Check if the URB timed out */ 7.924 - if (u->timeout && time_after_eq(jiffies, up->inserttime + u->timeout)) { 7.925 - list_del(&u->urb_list); 7.926 - list_add_tail(&u->urb_list, &list); 7.927 - } 7.928 - 7.929 - spin_unlock(&u->lock); 7.930 - } 7.931 - spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 7.932 - 7.933 - head = &list; 7.934 - tmp = head->next; 7.935 - while (tmp != head) { 7.936 - struct urb *u = list_entry(tmp, struct urb, urb_list); 7.937 - 7.938 - tmp = tmp->next; 7.939 - 7.940 - u->transfer_flags |= USB_ASYNC_UNLINK | USB_TIMEOUT_KILLED; 7.941 - xhci_unlink_urb(u); 7.942 - } 7.943 - 7.944 - rh_init_int_timer(urb); 7.945 -} 7.946 - 7.947 -/* Root Hub INTs are polled by this timer */ 7.948 -static int rh_init_int_timer(struct urb *urb) 7.949 -{ 7.950 - xhci->rh.interval = urb->interval; 7.951 - init_timer(&xhci->rh.rh_int_timer); 7.952 - xhci->rh.rh_int_timer.function = rh_int_timer_do; 7.953 - xhci->rh.rh_int_timer.data = (unsigned long)urb; 7.954 - xhci->rh.rh_int_timer.expires = jiffies + (HZ * (urb->interval < 30 ? 30 : urb->interval)) / 1000; 7.955 - add_timer(&xhci->rh.rh_int_timer); 7.956 - 7.957 - return 0; 7.958 -} 7.959 - 7.960 -#define OK(x) len = (x); break 7.961 - 7.962 -/* Root Hub Control Pipe */ 7.963 -static int rh_submit_urb(struct urb *urb) 7.964 -{ 7.965 - unsigned int pipe = urb->pipe; 7.966 - struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *)urb->setup_packet; 7.967 - void *data = urb->transfer_buffer; 7.968 - int leni = urb->transfer_buffer_length; 7.969 - int len = 0; 7.970 - xhci_port_t *status; 7.971 - int stat = 0; 7.972 - int i; 7.973 - int retstatus; 7.974 - unsigned long flags; 7.975 - 7.976 - __u16 cstatus; 7.977 - __u16 bmRType_bReq; 7.978 - __u16 wValue; 7.979 - __u16 wIndex; 7.980 - __u16 wLength; 7.981 - 7.982 - if (usb_pipetype(pipe) == PIPE_INTERRUPT) { 7.983 - xhci->rh.urb = urb; 7.984 - xhci->rh.send = 1; 7.985 - xhci->rh.interval = urb->interval; 7.986 - rh_init_int_timer(urb); 7.987 - 7.988 - return -EINPROGRESS; 7.989 - } 7.990 - 7.991 - bmRType_bReq = cmd->bRequestType | cmd->bRequest << 8; 7.992 - wValue = le16_to_cpu(cmd->wValue); 7.993 - wIndex = le16_to_cpu(cmd->wIndex); 7.994 - wLength = le16_to_cpu(cmd->wLength); 7.995 - 7.996 - for (i = 0; i < 8; i++) 7.997 - xhci->rh.c_p_r[i] = 0; 7.998 - 7.999 - status = &xhci->rh.ports[wIndex - 1]; 7.1000 - 7.1001 - spin_lock_irqsave(&xhci->rh.port_state_lock, flags); 7.1002 - 7.1003 - switch (bmRType_bReq) { 7.1004 - /* Request Destination: 7.1005 - without flags: Device, 7.1006 - RH_INTERFACE: interface, 7.1007 - RH_ENDPOINT: endpoint, 7.1008 - RH_CLASS means HUB here, 7.1009 - RH_OTHER | RH_CLASS almost ever means HUB_PORT here 7.1010 - */ 7.1011 - 7.1012 - case RH_GET_STATUS: 7.1013 - *(__u16 *)data = cpu_to_le16(1); 7.1014 - OK(2); 7.1015 - case RH_GET_STATUS | RH_INTERFACE: 7.1016 - *(__u16 *)data = cpu_to_le16(0); 7.1017 - OK(2); 7.1018 - case RH_GET_STATUS | RH_ENDPOINT: 7.1019 - *(__u16 *)data = cpu_to_le16(0); 7.1020 - OK(2); 7.1021 - case RH_GET_STATUS | RH_CLASS: 7.1022 - *(__u32 *)data = cpu_to_le32(0); 7.1023 - OK(4); /* hub power */ 7.1024 - case RH_GET_STATUS | RH_OTHER | RH_CLASS: 7.1025 - cstatus = (status->cs_chg) | 7.1026 - (status->pe_chg << 1) | 7.1027 - (xhci->rh.c_p_r[wIndex - 1] << 4); 7.1028 - retstatus = (status->ccs) | 7.1029 - (status->pe << 1) | 7.1030 - (status->susp << 2) | 7.1031 - (status->pr << 8) | 7.1032 - (1 << 8) | /* power on */ 7.1033 - (status->lsda << 9); 7.1034 - *(__u16 *)data = cpu_to_le16(retstatus); 7.1035 - *(__u16 *)(data + 2) = cpu_to_le16(cstatus); 7.1036 - OK(4); 7.1037 - case RH_CLEAR_FEATURE | RH_ENDPOINT: 7.1038 - switch (wValue) { 7.1039 - case RH_ENDPOINT_STALL: 7.1040 - OK(0); 7.1041 - } 7.1042 - break; 7.1043 - case RH_CLEAR_FEATURE | RH_CLASS: 7.1044 - switch (wValue) { 7.1045 - case RH_C_HUB_OVER_CURRENT: 7.1046 - OK(0); /* hub power over current */ 7.1047 - } 7.1048 - break; 7.1049 - case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS: 7.1050 - switch (wValue) { 7.1051 - case RH_PORT_ENABLE: 7.1052 - status->pe = 0; 7.1053 - OK(0); 7.1054 - case RH_PORT_SUSPEND: 7.1055 - status->susp = 0; 7.1056 - OK(0); 7.1057 - case RH_PORT_POWER: 7.1058 - OK(0); /* port power */ 7.1059 - case RH_C_PORT_CONNECTION: 7.1060 - status->cs_chg = 0; 7.1061 - OK(0); 7.1062 - case RH_C_PORT_ENABLE: 7.1063 - status->pe_chg = 0; 7.1064 - OK(0); 7.1065 - case RH_C_PORT_SUSPEND: 7.1066 - /*** WR_RH_PORTSTAT(RH_PS_PSSC); */ 7.1067 - OK(0); 7.1068 - case RH_C_PORT_OVER_CURRENT: 7.1069 - OK(0); /* port power over current */ 7.1070 - case RH_C_PORT_RESET: 7.1071 - xhci->rh.c_p_r[wIndex - 1] = 0; 7.1072 - OK(0); 7.1073 - } 7.1074 - break; 7.1075 - case RH_SET_FEATURE | RH_OTHER | RH_CLASS: 7.1076 - switch (wValue) { 7.1077 - case RH_PORT_SUSPEND: 7.1078 - status->susp = 1; 7.1079 - OK(0); 7.1080 - case RH_PORT_RESET: 7.1081 - { 7.1082 - int ret; 7.1083 - xhci->rh.c_p_r[wIndex - 1] = 1; 7.1084 - status->pr = 0; 7.1085 - status->pe = 1; 7.1086 - ret = xhci_port_reset(wIndex - 1); 7.1087 - /* XXX MAW: should probably cancel queued transfers during reset... *\/ */ 7.1088 - if ( ret == 0 ) { OK(0); } 7.1089 - else { return ret; } 7.1090 - } 7.1091 - break; 7.1092 - case RH_PORT_POWER: 7.1093 - OK(0); /* port power ** */ 7.1094 - case RH_PORT_ENABLE: 7.1095 - status->pe = 1; 7.1096 - OK(0); 7.1097 - } 7.1098 - break; 7.1099 - case RH_SET_ADDRESS: 7.1100 - printk("setting root hub device to %d\n", wValue); 7.1101 - xhci->rh.devnum = wValue; 7.1102 - OK(0); 7.1103 - case RH_GET_DESCRIPTOR: 7.1104 - switch ((wValue & 0xff00) >> 8) { 7.1105 - case 0x01: /* device descriptor */ 7.1106 - len = min_t(unsigned int, leni, 7.1107 - min_t(unsigned int, 7.1108 - sizeof(root_hub_dev_des), wLength)); 7.1109 - memcpy(data, root_hub_dev_des, len); 7.1110 - OK(len); 7.1111 - case 0x02: /* configuration descriptor */ 7.1112 - len = min_t(unsigned int, leni, 7.1113 - min_t(unsigned int, 7.1114 - sizeof(root_hub_config_des), wLength)); 7.1115 - memcpy (data, root_hub_config_des, len); 7.1116 - OK(len); 7.1117 - case 0x03: /* string descriptors */ 7.1118 - len = usb_root_hub_string (wValue & 0xff, 7.1119 - 0, "XHCI-alt", 7.1120 - data, wLength); 7.1121 - if (len > 0) { 7.1122 - OK(min_t(int, leni, len)); 7.1123 - } else 7.1124 - stat = -EPIPE; 7.1125 - } 7.1126 - break; 7.1127 - case RH_GET_DESCRIPTOR | RH_CLASS: 7.1128 - root_hub_hub_des[2] = xhci->rh.numports; 7.1129 - len = min_t(unsigned int, leni, 7.1130 - min_t(unsigned int, sizeof(root_hub_hub_des), wLength)); 7.1131 - memcpy(data, root_hub_hub_des, len); 7.1132 - OK(len); 7.1133 - case RH_GET_CONFIGURATION: 7.1134 - *(__u8 *)data = 0x01; 7.1135 - OK(1); 7.1136 - case RH_SET_CONFIGURATION: 7.1137 - OK(0); 7.1138 - case RH_GET_INTERFACE | RH_INTERFACE: 7.1139 - *(__u8 *)data = 0x00; 7.1140 - OK(1); 7.1141 - case RH_SET_INTERFACE | RH_INTERFACE: 7.1142 - OK(0); 7.1143 - default: 7.1144 - stat = -EPIPE; 7.1145 - } 7.1146 - 7.1147 - spin_unlock_irqrestore(&xhci->rh.port_state_lock, flags); 7.1148 - 7.1149 - urb->actual_length = len; 7.1150 - 7.1151 - return stat; 7.1152 -} 7.1153 - 7.1154 -/* 7.1155 - * MUST be called with urb->lock acquired 7.1156 - */ 7.1157 -static int rh_unlink_urb(struct urb *urb) 7.1158 -{ 7.1159 - if (xhci->rh.urb == urb) { 7.1160 - urb->status = -ENOENT; 7.1161 - xhci->rh.send = 0; 7.1162 - xhci->rh.urb = NULL; 7.1163 - del_timer(&xhci->rh.rh_int_timer); 7.1164 - } 7.1165 - return 0; 7.1166 -} 7.1167 - 7.1168 -static void xhci_call_completion(struct urb *urb) 7.1169 -{ 7.1170 - struct urb_priv *urbp; 7.1171 - struct usb_device *dev = urb->dev; 7.1172 - int is_ring = 0, killed, resubmit_interrupt, status; 7.1173 - struct urb *nurb; 7.1174 - unsigned long flags; 7.1175 - 7.1176 - spin_lock_irqsave(&urb->lock, flags); 7.1177 - 7.1178 - urbp = (struct urb_priv *)urb->hcpriv; 7.1179 - if (!urbp || !urb->dev) { 7.1180 - spin_unlock_irqrestore(&urb->lock, flags); 7.1181 - return; 7.1182 - } 7.1183 - 7.1184 - killed = (urb->status == -ENOENT || urb->status == -ECONNABORTED || 7.1185 - urb->status == -ECONNRESET); 7.1186 - resubmit_interrupt = (usb_pipetype(urb->pipe) == PIPE_INTERRUPT && 7.1187 - urb->interval); 7.1188 - 7.1189 - nurb = urb->next; 7.1190 - if (nurb && !killed) { 7.1191 - int count = 0; 7.1192 - 7.1193 - while (nurb && nurb != urb && count < MAX_URB_LOOP) { 7.1194 - if (nurb->status == -ENOENT || 7.1195 - nurb->status == -ECONNABORTED || 7.1196 - nurb->status == -ECONNRESET) { 7.1197 - killed = 1; 7.1198 - break; 7.1199 - } 7.1200 - 7.1201 - nurb = nurb->next; 7.1202 - count++; 7.1203 - } 7.1204 - 7.1205 - if (count == MAX_URB_LOOP) 7.1206 - err("xhci_call_completion: too many linked URB's, loop? (first loop)"); 7.1207 - 7.1208 - /* Check to see if chain is a ring */ 7.1209 - is_ring = (nurb == urb); 7.1210 - } 7.1211 - 7.1212 - status = urbp->status; 7.1213 - if (!resubmit_interrupt || killed) 7.1214 - /* We don't need urb_priv anymore */ 7.1215 - xhci_destroy_urb_priv(urb); 7.1216 - 7.1217 - if (!killed) 7.1218 - urb->status = status; 7.1219 - 7.1220 - spin_unlock_irqrestore(&urb->lock, flags); 7.1221 - 7.1222 - if (urb->complete) 7.1223 - urb->complete(urb); 7.1224 - 7.1225 - if (resubmit_interrupt) 7.1226 - /* Recheck the status. The completion handler may have */ 7.1227 - /* unlinked the resubmitting interrupt URB */ 7.1228 - killed = (urb->status == -ENOENT || 7.1229 - urb->status == -ECONNABORTED || 7.1230 - urb->status == -ECONNRESET); 7.1231 - 7.1232 - if (resubmit_interrupt && !killed) { 7.1233 - if ( urb->dev != xhci->rh.dev ) 7.1234 - xhci_queue_req(urb); /* XXX What if this fails? */ 7.1235 - /* Don't need to resubmit URBs for the virtual root dev. */ 7.1236 - } else { 7.1237 - if (is_ring && !killed) { 7.1238 - urb->dev = dev; 7.1239 - xhci_submit_urb(urb); 7.1240 - } else { 7.1241 - /* We decrement the usage count after we're done */ 7.1242 - /* with everything */ 7.1243 - usb_dec_dev_use(dev); 7.1244 - } 7.1245 - } 7.1246 -} 7.1247 - 7.1248 -static void xhci_finish_completion(void) 7.1249 -{ 7.1250 - struct list_head *tmp, *head; 7.1251 - unsigned long flags; 7.1252 - 7.1253 - spin_lock_irqsave(&xhci->complete_list_lock, flags); 7.1254 - head = &xhci->complete_list; 7.1255 - tmp = head->next; 7.1256 - while (tmp != head) { 7.1257 - struct urb_priv *urbp = list_entry(tmp, struct urb_priv, complete_list); 7.1258 - struct urb *urb = urbp->urb; 7.1259 - 7.1260 - list_del_init(&urbp->complete_list); 7.1261 - spin_unlock_irqrestore(&xhci->complete_list_lock, flags); 7.1262 - 7.1263 - xhci_call_completion(urb); 7.1264 - 7.1265 - spin_lock_irqsave(&xhci->complete_list_lock, flags); 7.1266 - head = &xhci->complete_list; 7.1267 - tmp = head->next; 7.1268 - } 7.1269 - spin_unlock_irqrestore(&xhci->complete_list_lock, flags); 7.1270 -} 7.1271 - 7.1272 -void receive_usb_reset(usbif_response_t *resp) 7.1273 -{ 7.1274 - awaiting_reset = resp->status; 7.1275 - rmb(); 7.1276 - 7.1277 -} 7.1278 - 7.1279 -void receive_usb_probe(usbif_response_t *resp) 7.1280 -{ 7.1281 - spin_lock(&xhci->rh.port_state_lock); 7.1282 - 7.1283 - if ( resp->status > 0 ) 7.1284 - { 7.1285 - if ( resp->status == 1 ) 7.1286 - { 7.1287 -/* printk("hey hey, there's a device on port %d\n", resp->data); */ 7.1288 - 7.1289 - /* If theres a device there and there wasn't one before there must 7.1290 - * have been a connection status change. */ 7.1291 - if( xhci->rh.ports[resp->data].cs == 0 ) 7.1292 - { 7.1293 - xhci->rh.ports[resp->data].cs = 1; 7.1294 - xhci->rh.ports[resp->data].ccs = 1; 7.1295 - xhci->rh.ports[resp->data].cs_chg = 1; 7.1296 -/* printk("Look at device on port %d that wasn't there before\n", resp->data); */ 7.1297 - } 7.1298 - } 7.1299 - else 7.1300 - printk("receive_usb_probe(): unexpected status %d for port %d\n", 7.1301 - resp->status, resp->data); 7.1302 - } 7.1303 - else if ( resp->status < 0) 7.1304 - printk("receive_usb_probe(): got error status %d\n", resp->status); 7.1305 - 7.1306 - spin_unlock(&xhci->rh.port_state_lock); 7.1307 -} 7.1308 - 7.1309 -void receive_usb_io(usbif_response_t *resp) 7.1310 -{ 7.1311 - struct urb_priv *urbp = (struct urb_priv *)resp->id; 7.1312 - struct urb *urb = urbp->urb; 7.1313 - 7.1314 - urb->actual_length = resp->length; 7.1315 - urb->status = resp->status; 7.1316 - urbp->status = resp->status; 7.1317 - urbp->in_progress = 0; 7.1318 - 7.1319 - if( usb_pipetype(urb->pipe) == 0 ) /* ISO */ 7.1320 - { 7.1321 - int i; 7.1322 - 7.1323 - /* Copy ISO schedule results back in. */ 7.1324 - 7.1325 - for ( i = 0; i < urb->number_of_packets; i++ ) 7.1326 - { 7.1327 - urb->iso_frame_desc[i].status 7.1328 - = urbp->schedule[i].status; 7.1329 - urb->iso_frame_desc[i].actual_length 7.1330 - = urbp->schedule[i].length; 7.1331 - } 7.1332 - free_page((unsigned long)urbp->schedule); 7.1333 - } 7.1334 -} 7.1335 - 7.1336 -static void xhci_drain_ring(void) 7.1337 -{ 7.1338 - struct list_head *tmp, *head; 7.1339 - usbif_t *usb_ring = xhci->usbif; 7.1340 - usbif_response_t *resp; 7.1341 - USBIF_RING_IDX i, rp; 7.1342 - 7.1343 - /* Walk the ring here to get responses, updating URBs to show what 7.1344 - * completed. */ 7.1345 - 7.1346 - rp = usb_ring->resp_prod; 7.1347 - rmb(); /* Ensure we see queued requests up to 'rp'. */ 7.1348 - 7.1349 - /* Take items off the comms ring, taking care not to overflow. */ 7.1350 - for ( i = xhci->usb_resp_cons; 7.1351 - (i != rp) && ((i-usb_ring->req_prod) != USBIF_RING_SIZE); 7.1352 - i++ ) 7.1353 - { 7.1354 - resp = &usb_ring->ring[MASK_USBIF_IDX(i)].resp; 7.1355 - 7.1356 - /* May need to deal with batching and with putting a ceiling on 7.1357 - the number dispatched for performance and anti-dos reasons */ 7.1358 - 7.1359 -#if 0 7.1360 - printk("usbfront: Processing response:\n"); 7.1361 - printk(" id = 0x%x\n", resp->id); 7.1362 - printk(" op = %d\n", resp->operation); 7.1363 - printk(" status = %d\n", resp->status); 7.1364 - printk(" length = %d\n", resp->length); 7.1365 -#endif 7.1366 - 7.1367 - switch ( resp->operation ) 7.1368 - { 7.1369 - case USBIF_OP_PROBE: 7.1370 - receive_usb_probe(resp); 7.1371 - break; 7.1372 - 7.1373 - case USBIF_OP_IO: 7.1374 - receive_usb_io(resp); 7.1375 - break; 7.1376 - 7.1377 - case USBIF_OP_RESET: 7.1378 - receive_usb_reset(resp); 7.1379 - break; 7.1380 - 7.1381 - default: 7.1382 - printk("error: unknown USB io operation response [%d]\n", 7.1383 - usb_ring->ring[i].req.operation); 7.1384 - break; 7.1385 - } 7.1386 - } 7.1387 - 7.1388 - xhci->usb_resp_cons = i; 7.1389 - 7.1390 - /* Walk the list of pending URB's to see which ones completed and do 7.1391 - * callbacks, etc. */ 7.1392 - spin_lock(&xhci->urb_list_lock); 7.1393 - head = &xhci->urb_list; 7.1394 - tmp = head->next; 7.1395 - while (tmp != head) { 7.1396 - 7.1397 - struct urb *urb = list_entry(tmp, struct urb, urb_list); 7.1398 - 7.1399 - tmp = tmp->next; 7.1400 - 7.1401 - /* Checks the status and does all of the magic necessary */ 7.1402 - xhci_transfer_result(xhci, urb); 7.1403 - } 7.1404 - spin_unlock(&xhci->urb_list_lock); 7.1405 - 7.1406 - xhci_finish_completion(); 7.1407 -} 7.1408 - 7.1409 - 7.1410 -static void xhci_interrupt(int irq, void *__xhci, struct pt_regs *regs) 7.1411 -{ 7.1412 - xhci_drain_ring(); 7.1413 -} 7.1414 - 7.1415 -static void free_xhci(struct xhci *xhci) 7.1416 -{ 7.1417 - kfree(xhci); 7.1418 -} 7.1419 - 7.1420 -/* /\* */ 7.1421 -/* * De-allocate all resources.. */ 7.1422 -/* *\/ */ 7.1423 -/* static void release_xhci(struct xhci *xhci) */ 7.1424 -/* { */ 7.1425 -/* if (xhci->irq >= 0) { */ 7.1426 -/* free_irq(xhci->irq, xhci); */ 7.1427 -/* xhci->irq = -1; */ 7.1428 -/* } */ 7.1429 - 7.1430 -/* /\* Get the ring back from the backend domain. Then free it. Hmmmm. */ 7.1431 -/* * Lets ignore this for now - not particularly useful. *\/ */ 7.1432 - 7.1433 -/* free_xhci(xhci); */ 7.1434 -/* } */ 7.1435 - 7.1436 -/** 7.1437 - * Initialise a new virtual root hub for a new USB device channel. 7.1438 - */ 7.1439 -static int alloc_xhci(void) 7.1440 -{ 7.1441 - int retval; 7.1442 - struct usb_bus *bus; 7.1443 - 7.1444 - retval = -EBUSY; 7.1445 - 7.1446 - xhci = kmalloc(sizeof(*xhci), GFP_KERNEL); 7.1447 - if (!xhci) { 7.1448 - err("couldn't allocate xhci structure"); 7.1449 - retval = -ENOMEM; 7.1450 - goto err_alloc_xhci; 7.1451 - } 7.1452 - 7.1453 - /* Reset here so we don't get any interrupts from an old setup */ 7.1454 - /* or broken setup */ 7.1455 - // reset_hc(xhci); 7.1456 - 7.1457 - 7.1458 - xhci->state = USBIF_STATE_CLOSED; 7.1459 - xhci->is_suspended = 0; 7.1460 - 7.1461 - spin_lock_init(&xhci->urb_remove_list_lock); 7.1462 - INIT_LIST_HEAD(&xhci->urb_remove_list); 7.1463 - 7.1464 - spin_lock_init(&xhci->urb_list_lock); 7.1465 - INIT_LIST_HEAD(&xhci->urb_list); 7.1466 - 7.1467 - spin_lock_init(&xhci->complete_list_lock); 7.1468 - INIT_LIST_HEAD(&xhci->complete_list); 7.1469 - 7.1470 - spin_lock_init(&xhci->frame_list_lock); 7.1471 - 7.1472 - /* We need exactly one page (per XHCI specs), how convenient */ 7.1473 - /* We assume that one page is atleast 4k (1024 frames * 4 bytes) */ 7.1474 -#if PAGE_SIZE < (4 * 1024) 7.1475 -#error PAGE_SIZE is not atleast 4k 7.1476 -#endif 7.1477 - bus = usb_alloc_bus(&xhci_device_operations); 7.1478 - if (!bus) { 7.1479 - err("unable to allocate bus"); 7.1480 - goto err_alloc_bus; 7.1481 - } 7.1482 - 7.1483 - xhci->bus = bus; 7.1484 - bus->bus_name = "XHCI"; 7.1485 - bus->hcpriv = xhci; 7.1486 - 7.1487 - usb_register_bus(xhci->bus); 7.1488 - 7.1489 - /* Initialize the root hub */ 7.1490 - 7.1491 - xhci->rh.numports = 0; 7.1492 - 7.1493 - xhci->bus->root_hub = xhci->rh.dev = usb_alloc_dev(NULL, xhci->bus); 7.1494 - if (!xhci->rh.dev) { 7.1495 - err("unable to allocate root hub"); 7.1496 - goto err_alloc_root_hub; 7.1497 - } 7.1498 - 7.1499 - xhci->state = 0; 7.1500 - 7.1501 - return 0; 7.1502 - 7.1503 -/* 7.1504 - * error exits: 7.1505 - */ 7.1506 -err_start_root_hub: 7.1507 - free_irq(xhci->irq, xhci); 7.1508 - xhci->irq = -1; 7.1509 - 7.1510 -err_alloc_root_hub: 7.1511 - usb_free_bus(xhci->bus); 7.1512 - xhci->bus = NULL; 7.1513 - 7.1514 -err_alloc_bus: 7.1515 - free_xhci(xhci); 7.1516 - 7.1517 -err_alloc_xhci: 7.1518 - return retval; 7.1519 -} 7.1520 - 7.1521 -static void usbif_status_change(usbif_fe_interface_status_changed_t *status) 7.1522 -{ 7.1523 - ctrl_msg_t cmsg; 7.1524 - usbif_fe_interface_connect_t up; 7.1525 - long rc; 7.1526 - usbif_t *usbif; 7.1527 - 7.1528 - switch ( status->status ) 7.1529 - { 7.1530 - case USBIF_INTERFACE_STATUS_DESTROYED: 7.1531 - printk(KERN_WARNING "Unexpected usbif-DESTROYED message in state %d\n", 7.1532 - xhci->state); 7.1533 - break; 7.1534 - 7.1535 - case USBIF_INTERFACE_STATUS_DISCONNECTED: 7.1536 - if ( xhci->state != USBIF_STATE_CLOSED ) 7.1537 - { 7.1538 - printk(KERN_WARNING "Unexpected usbif-DISCONNECTED message" 7.1539 - " in state %d\n", xhci->state); 7.1540 - break; 7.1541 - /* Not bothering to do recovery here for now. Keep things 7.1542 - * simple. */ 7.1543 - } 7.1544 - 7.1545 - /* Move from CLOSED to DISCONNECTED state. */ 7.1546 - xhci->usbif = usbif = (usbif_t *)__get_free_page(GFP_KERNEL); 7.1547 - usbif->req_prod = usbif->resp_prod = 0; 7.1548 - xhci->state = USBIF_STATE_DISCONNECTED; 7.1549 - 7.1550 - /* Construct an interface-CONNECT message for the domain controller. */ 7.1551 - cmsg.type = CMSG_USBIF_FE; 7.1552 - cmsg.subtype = CMSG_USBIF_FE_INTERFACE_CONNECT; 7.1553 - cmsg.length = sizeof(usbif_fe_interface_connect_t); 7.1554 - up.shmem_frame = virt_to_machine(usbif) >> PAGE_SHIFT; 7.1555 - memcpy(cmsg.msg, &up, sizeof(up)); 7.1556 - 7.1557 - /* Tell the controller to bring up the interface. */ 7.1558 - ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE); 7.1559 - break; 7.1560 - 7.1561 - case USBIF_INTERFACE_STATUS_CONNECTED: 7.1562 - if ( xhci->state == USBIF_STATE_CLOSED ) 7.1563 - { 7.1564 - printk(KERN_WARNING "Unexpected usbif-CONNECTED message" 7.1565 - " in state %d\n", xhci->state); 7.1566 - break; 7.1567 - } 7.1568 - 7.1569 - xhci->evtchn = status->evtchn; 7.1570 - xhci->irq = bind_evtchn_to_irq(xhci->evtchn); 7.1571 - xhci->bandwidth = status->bandwidth; 7.1572 - xhci->rh.numports = status->num_ports; 7.1573 - 7.1574 - xhci->rh.ports = kmalloc (sizeof(xhci_port_t) * xhci->rh.numports, GFP_KERNEL); 7.1575 - memset(xhci->rh.ports, 0, sizeof(xhci_port_t) * xhci->rh.numports); 7.1576 - 7.1577 - printk("rh.dev @ %p\n", xhci->rh.dev); 7.1578 - 7.1579 - usb_connect(xhci->rh.dev); 7.1580 - 7.1581 - if (usb_new_device(xhci->rh.dev) != 0) { 7.1582 - err("unable to start root hub"); 7.1583 - } 7.1584 - 7.1585 - /* Allocate the appropriate USB bandwidth here... Need to 7.1586 - * somehow know what the total available is thought to be so we 7.1587 - * can calculate the reservation correctly. */ 7.1588 - usb_claim_bandwidth(xhci->rh.dev, xhci->rh.urb, 7.1589 - 1000 - xhci->bandwidth, 0); 7.1590 - 7.1591 - if ( (rc = request_irq(xhci->irq, xhci_interrupt, 7.1592 - SA_SAMPLE_RANDOM, "usbif", xhci)) ) 7.1593 - printk(KERN_ALERT"usbfront request_irq failed (%ld)\n",rc); 7.1594 - 7.1595 - printk(KERN_INFO __FILE__ ": USB XHCI: SHM at %p (0x%lx), EVTCHN %d IRQ %d\n", 7.1596 - xhci->usbif, virt_to_machine(xhci->usbif), xhci->evtchn, xhci->irq); 7.1597 - 7.1598 - xhci->state = USBIF_STATE_CONNECTED; 7.1599 - 7.1600 - break; 7.1601 - 7.1602 - default: 7.1603 - printk(KERN_WARNING "Status change to unknown value %d\n", 7.1604 - status->status); 7.1605 - break; 7.1606 - } 7.1607 -} 7.1608 - 7.1609 - 7.1610 -static void usbif_ctrlif_rx(ctrl_msg_t *msg, unsigned long id) 7.1611 -{ 7.1612 - switch ( msg->subtype ) 7.1613 - { 7.1614 - case CMSG_USBIF_FE_INTERFACE_STATUS_CHANGED: 7.1615 - if ( msg->length != sizeof(usbif_fe_interface_status_changed_t) ) 7.1616 - goto parse_error; 7.1617 - usbif_status_change((usbif_fe_interface_status_changed_t *) 7.1618 - &msg->msg[0]); 7.1619 - break; 7.1620 - 7.1621 - /* New interface...? */ 7.1622 - default: 7.1623 - goto parse_error; 7.1624 - } 7.1625 - 7.1626 - ctrl_if_send_response(msg); 7.1627 - return; 7.1628 - 7.1629 - parse_error: 7.1630 - msg->length = 0; 7.1631 - ctrl_if_send_response(msg); 7.1632 -} 7.1633 - 7.1634 - 7.1635 -static int __init xhci_hcd_init(void) 7.1636 -{ 7.1637 - int retval = -ENOMEM, i; 7.1638 - usbif_fe_interface_status_changed_t st; 7.1639 - control_msg_t cmsg; 7.1640 - 7.1641 - if ( (xen_start_info.flags & SIF_INITDOMAIN) 7.1642 - || (xen_start_info.flags & SIF_USB_BE_DOMAIN) ) 7.1643 - return 0; 7.1644 - 7.1645 - info(DRIVER_DESC " " DRIVER_VERSION); 7.1646 - 7.1647 - if (debug) { 7.1648 - errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL); 7.1649 - if (!errbuf) 7.1650 - goto errbuf_failed; 7.1651 - } 7.1652 - 7.1653 - xhci_up_cachep = kmem_cache_create("xhci_urb_priv", 7.1654 - sizeof(struct urb_priv), 0, 0, NULL, NULL); 7.1655 - if (!xhci_up_cachep) 7.1656 - goto up_failed; 7.1657 - 7.1658 - /* Lazily avoid unloading issues for now. ;-)*/ 7.1659 - MOD_INC_USE_COUNT; 7.1660 - 7.1661 - /* Let the domain controller know we're here. For now we wait until 7.1662 - * connection, as for the block and net drivers. This is only strictly 7.1663 - * necessary if we're going to boot off a USB device. */ 7.1664 - printk(KERN_INFO "Initialising Xen virtual USB hub\n"); 7.1665 - 7.1666 - (void)ctrl_if_register_receiver(CMSG_USBIF_FE, usbif_ctrlif_rx, 7.1667 - CALLBACK_IN_BLOCKING_CONTEXT); 7.1668 - 7.1669 - alloc_xhci(); 7.1670 - 7.1671 - /* Send a driver-UP notification to the domain controller. */ 7.1672 - cmsg.type = CMSG_USBIF_FE; 7.1673 - cmsg.subtype = CMSG_USBIF_FE_DRIVER_STATUS_CHANGED; 7.1674 - cmsg.length = sizeof(usbif_fe_driver_status_changed_t); 7.1675 - st.status = USBIF_DRIVER_STATUS_UP; 7.1676 - memcpy(cmsg.msg, &st, sizeof(st)); 7.1677 - ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE); 7.1678 - 7.1679 - /* 7.1680 - * We should read 'nr_interfaces' from response message and wait 7.1681 - * for notifications before proceeding. For now we assume that we 7.1682 - * will be notified of exactly one interface. 7.1683 - */ 7.1684 - for ( i=0; (xhci->state != USBIF_STATE_CONNECTED) && (i < 10*HZ); i++ ) 7.1685 - { 7.1686 - set_current_state(TASK_INTERRUPTIBLE); 7.1687 - schedule_timeout(1); 7.1688 - } 7.1689 - 7.1690 - if (xhci->state != USBIF_STATE_CONNECTED) 7.1691 - printk(KERN_INFO "Timeout connecting USB frontend driver!\n"); 7.1692 - 7.1693 - return 0; 7.1694 - 7.1695 -up_failed: 7.1696 - 7.1697 - if (errbuf) 7.1698 - kfree(errbuf); 7.1699 - 7.1700 -errbuf_failed: 7.1701 - 7.1702 - return retval; 7.1703 -} 7.1704 - 7.1705 -static void __exit xhci_hcd_cleanup(void) 7.1706 -{ 7.1707 - if (kmem_cache_destroy(xhci_up_cachep)) 7.1708 - printk(KERN_INFO "xhci: not all urb_priv's were freed\n"); 7.1709 - 7.1710 -// release_xhci(); do some calls here 7.1711 - 7.1712 - 7.1713 - if (errbuf) 7.1714 - kfree(errbuf); 7.1715 -} 7.1716 - 7.1717 -module_init(xhci_hcd_init); 7.1718 -module_exit(xhci_hcd_cleanup); 7.1719 - 7.1720 -MODULE_AUTHOR(DRIVER_AUTHOR); 7.1721 -MODULE_DESCRIPTION(DRIVER_DESC); 7.1722 -MODULE_LICENSE("GPL"); 7.1723 -
8.1 --- a/linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/frontend/xhci.h Wed Jan 26 10:56:34 2005 +0000 8.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 8.3 @@ -1,210 +0,0 @@ 8.4 -#ifndef __LINUX_XHCI_H 8.5 -#define __LINUX_XHCI_H 8.6 - 8.7 -#include <linux/list.h> 8.8 -#include <linux/usb.h> 8.9 -#include "../usbif.h" 8.10 -#include <linux/spinlock.h> 8.11 - 8.12 -#define XHCI_NUMFRAMES 1024 /* in the frame list [array] */ 8.13 -#define XHCI_MAX_SOF_NUMBER 2047 /* in an SOF packet */ 8.14 -#define CAN_SCHEDULE_FRAMES 1000 /* how far future frames can be scheduled */ 8.15 - 8.16 -/* In the absence of actual hardware state, we maintain the current known state 8.17 - * of the virtual hub ports in this data structure. 8.18 - */ 8.19 -typedef struct 8.20 -{ 8.21 - unsigned int cs :1; /* Connection status. do we really need this /and/ ccs? */ 8.22 - unsigned int cs_chg :1; /* Connection status change. */ 8.23 - unsigned int pe :1; /* Port enable. */ 8.24 - unsigned int pe_chg :1; /* Port enable change. */ 8.25 - unsigned int ccs :1; /* Current connect status. */ 8.26 - unsigned int susp :1; /* Suspended. */ 8.27 - unsigned int lsda :1; /* Low speed device attached. */ 8.28 - unsigned int pr :1; /* Port reset. */ 8.29 - 8.30 - /* Device info? */ 8.31 -} xhci_port_t; 8.32 - 8.33 -struct xhci_frame_list { 8.34 - __u32 frame[XHCI_NUMFRAMES]; 8.35 - 8.36 - void *frame_cpu[XHCI_NUMFRAMES]; 8.37 -}; 8.38 - 8.39 -struct urb_priv; 8.40 - 8.41 -#define xhci_status_bits(ctrl_sts) (ctrl_sts & 0xFE0000) 8.42 -#define xhci_actual_length(ctrl_sts) ((ctrl_sts + 1) & TD_CTRL_ACTLEN_MASK) /* 1-based */ 8.43 - 8.44 -#define xhci_maxlen(token) ((token) >> 21) 8.45 -#define xhci_expected_length(info) (((info >> 21) + 1) & TD_TOKEN_EXPLEN_MASK) /* 1-based */ 8.46 -#define xhci_toggle(token) (((token) >> TD_TOKEN_TOGGLE_SHIFT) & 1) 8.47 -#define xhci_endpoint(token) (((token) >> 15) & 0xf) 8.48 -#define xhci_devaddr(token) (((token) >> 8) & 0x7f) 8.49 -#define xhci_devep(token) (((token) >> 8) & 0x7ff) 8.50 -#define xhci_packetid(token) ((token) & TD_TOKEN_PID_MASK) 8.51 -#define xhci_packetout(token) (xhci_packetid(token) != USB_PID_IN) 8.52 -#define xhci_packetin(token) (xhci_packetid(token) == USB_PID_IN) 8.53 - 8.54 -struct virt_root_hub { 8.55 - struct usb_device *dev; 8.56 - int devnum; /* Address of Root Hub endpoint */ 8.57 - struct urb *urb; 8.58 - void *int_addr; 8.59 - int send; 8.60 - int interval; 8.61 - int numports; 8.62 - int c_p_r[8]; 8.63 - struct timer_list rh_int_timer; 8.64 - spinlock_t port_state_lock; 8.65 - xhci_port_t *ports; /* */ 8.66 -}; 8.67 - 8.68 -/* 8.69 - * This describes the full xhci information. 8.70 - * 8.71 - * Note how the "proper" USB information is just 8.72 - * a subset of what the full implementation needs. 8.73 - */ 8.74 -struct xhci { 8.75 - 8.76 -#ifdef CONFIG_PROC_FS 8.77 - /* procfs */ 8.78 - int num; 8.79 - struct proc_dir_entry *proc_entry; 8.80 -#endif 8.81 - 8.82 - int evtchn; /* Interdom channel to backend */ 8.83 - int irq; /* Bound to evtchn */ 8.84 - int state; /* State of this USB interface */ 8.85 - unsigned long bandwidth; 8.86 - int handle; 8.87 - 8.88 - struct usb_bus *bus; 8.89 - 8.90 - spinlock_t frame_list_lock; 8.91 - struct xhci_frame_list *fl; /* P: xhci->frame_list_lock */ 8.92 - int is_suspended; 8.93 - 8.94 - /* Main list of URB's currently controlled by this HC */ 8.95 - spinlock_t urb_list_lock; 8.96 - struct list_head urb_list; /* P: xhci->urb_list_lock */ 8.97 - 8.98 - /* List of asynchronously unlinked URB's */ 8.99 - spinlock_t urb_remove_list_lock; 8.100 - struct list_head urb_remove_list; /* P: xhci->urb_remove_list_lock */ 8.101 - 8.102 - /* List of URB's awaiting completion callback */ 8.103 - spinlock_t complete_list_lock; 8.104 - struct list_head complete_list; /* P: xhci->complete_list_lock */ 8.105 - 8.106 - struct virt_root_hub rh; /* private data of the virtual root hub */ 8.107 - 8.108 - spinlock_t response_lock; 8.109 - 8.110 - usbif_t *usbif; 8.111 - int usb_resp_cons; 8.112 -}; 8.113 - 8.114 -struct urb_priv { 8.115 - struct urb *urb; 8.116 - usbif_iso_t *schedule; 8.117 - struct usb_device *dev; 8.118 - 8.119 - int in_progress : 1; /* QH was queued (not linked in) */ 8.120 - int short_control_packet : 1; /* If we get a short packet during */ 8.121 - /* a control transfer, retrigger */ 8.122 - /* the status phase */ 8.123 - 8.124 - int status; /* Final status */ 8.125 - 8.126 - unsigned long inserttime; /* In jiffies */ 8.127 - 8.128 - struct list_head queue_list; /* P: xhci->frame_list_lock */ 8.129 - struct list_head complete_list; /* P: xhci->complete_list_lock */ 8.130 -}; 8.131 - 8.132 -/* 8.133 - * Locking in xhci.c 8.134 - * 8.135 - * spinlocks are used extensively to protect the many lists and data 8.136 - * structures we have. It's not that pretty, but it's necessary. We 8.137 - * need to be done with all of the locks (except complete_list_lock) when 8.138 - * we call urb->complete. I've tried to make it simple enough so I don't 8.139 - * have to spend hours racking my brain trying to figure out if the 8.140 - * locking is safe. 8.141 - * 8.142 - * Here's the safe locking order to prevent deadlocks: 8.143 - * 8.144 - * #1 xhci->urb_list_lock 8.145 - * #2 urb->lock 8.146 - * #3 xhci->urb_remove_list_lock, xhci->frame_list_lock, 8.147 - * xhci->qh_remove_list_lock 8.148 - * #4 xhci->complete_list_lock 8.149 - * 8.150 - * If you're going to grab 2 or more locks at once, ALWAYS grab the lock 8.151 - * at the lowest level FIRST and NEVER grab locks at the same level at the 8.152 - * same time. 8.153 - * 8.154 - * So, if you need xhci->urb_list_lock, grab it before you grab urb->lock 8.155 - */ 8.156 - 8.157 -/* ------------------------------------------------------------------------- 8.158 - Virtual Root HUB 8.159 - ------------------------------------------------------------------------- */ 8.160 -/* destination of request */ 8.161 -#define RH_DEVICE 0x00 8.162 -#define RH_INTERFACE 0x01 8.163 -#define RH_ENDPOINT 0x02 8.164 -#define RH_OTHER 0x03 8.165 - 8.166 -#define RH_CLASS 0x20 8.167 -#define RH_VENDOR 0x40 8.168 - 8.169 -/* Requests: bRequest << 8 | bmRequestType */ 8.170 -#define RH_GET_STATUS 0x0080 8.171 -#define RH_CLEAR_FEATURE 0x0100 8.172 -#define RH_SET_FEATURE 0x0300 8.173 -#define RH_SET_ADDRESS 0x0500 8.174 -#define RH_GET_DESCRIPTOR 0x0680 8.175 -#define RH_SET_DESCRIPTOR 0x0700 8.176 -#define RH_GET_CONFIGURATION 0x0880 8.177 -#define RH_SET_CONFIGURATION 0x0900 8.178 -#define RH_GET_STATE 0x0280 8.179 -#define RH_GET_INTERFACE 0x0A80 8.180 -#define RH_SET_INTERFACE 0x0B00 8.181 -#define RH_SYNC_FRAME 0x0C80 8.182 -/* Our Vendor Specific Request */ 8.183 -#define RH_SET_EP 0x2000 8.184 - 8.185 -/* Hub port features */ 8.186 -#define RH_PORT_CONNECTION 0x00 8.187 -#define RH_PORT_ENABLE 0x01 8.188 -#define RH_PORT_SUSPEND 0x02 8.189 -#define RH_PORT_OVER_CURRENT 0x03 8.190 -#define RH_PORT_RESET 0x04 8.191 -#define RH_PORT_POWER 0x08 8.192 -#define RH_PORT_LOW_SPEED 0x09 8.193 -#define RH_C_PORT_CONNECTION 0x10 8.194 -#define RH_C_PORT_ENABLE 0x11 8.195 -#define RH_C_PORT_SUSPEND 0x12 8.196 -#define RH_C_PORT_OVER_CURRENT 0x13 8.197 -#define RH_C_PORT_RESET 0x14 8.198 - 8.199 -/* Hub features */ 8.200 -#define RH_C_HUB_LOCAL_POWER 0x00 8.201 -#define RH_C_HUB_OVER_CURRENT 0x01 8.202 -#define RH_DEVICE_REMOTE_WAKEUP 0x00 8.203 -#define RH_ENDPOINT_STALL 0x01 8.204 - 8.205 -/* Our Vendor Specific feature */ 8.206 -#define RH_REMOVE_EP 0x00 8.207 - 8.208 -#define RH_ACK 0x01 8.209 -#define RH_REQ_ERR -1 8.210 -#define RH_NACK 0x00 8.211 - 8.212 -#endif 8.213 -
9.1 --- a/linux-2.4.29-xen-sparse/arch/xen/drivers/usbif/usbif.h Wed Jan 26 10:56:34 2005 +0000 9.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 9.3 @@ -1,111 +0,0 @@ 9.4 -/****************************************************************************** 9.5 - * usbif.h 9.6 - * 9.7 - * Unified block-device I/O interface for Xen guest OSes. 9.8 - * 9.9 - * Copyright (c) 2003-2004, Keir Fraser 9.10 - */ 9.11 - 9.12 -#ifndef __SHARED_USBIF_H__ 9.13 -#define __SHARED_USBIF_H__ 9.14 - 9.15 -#define usbif_vdev_t u16 9.16 -#define usbif_sector_t u64 9.17 - 9.18 -#define USBIF_OP_IO 0 9.19 -#define USBIF_OP_PROBE 1 /* Is there a device on this port? */ 9.20 -#define USBIF_OP_RESET 2 /* Reset a virtual USB port. */ 9.21 - 9.22 -/* NB. Ring size must be small enough for sizeof(usbif_ring_t) <= PAGE_SIZE. */ 9.23 -#define USBIF_RING_SIZE 64 9.24 - 9.25 -/* XXX this does not want to be here! it really ought to be dynamic but it can 9.26 - * live here for now */ 9.27 -#define NUM_PORTS 1 9.28 - 9.29 -typedef struct { 9.30 - unsigned long id; /* 0: private guest value, echoed in resp */ 9.31 - u8 operation; /* 4: USBIF_OP_??? */ 9.32 - u8 __pad1; 9.33 - usbif_vdev_t port; /* 6 : guest virtual USB port */ 9.34 - unsigned long devnum :7; /* 8 : Device address, as seen by the guest.*/ 9.35 - unsigned long endpoint :4; /* Device endpoint. */ 9.36 - unsigned long direction :1; /* Pipe direction. */ 9.37 - unsigned long speed :1; /* Pipe speed. */ 9.38 - unsigned long pipe_type :2; /* Pipe type (iso, bulk, int, ctrl) */ 9.39 - unsigned long __pad2 :18; 9.40 - unsigned long transfer_buffer; /* 12: Machine address */ 9.41 - unsigned long length; /* 16: Buffer length */ 9.42 - unsigned long transfer_flags; /* 20: For now just pass Linux transfer 9.43 - * flags - this may change. */ 9.44 - unsigned char setup[8]; /* 22 Embed setup packets directly. */ 9.45 - unsigned long iso_schedule; /* 30 Machine address of transfer sched (iso 9.46 - * only) */ 9.47 - unsigned long num_iso; /* 34 : length of iso schedule */ 9.48 - unsigned long timeout; /* 38: timeout in ms */ 9.49 -} PACKED usbif_request_t; /* 42 */ 9.50 -/* Data we need to pass: 9.51 - * - Transparently handle short packets or complain at us? 9.52 - */ 9.53 - 9.54 -typedef struct { 9.55 - unsigned long id; /* 0: copied from request */ 9.56 - u8 operation; /* 4: copied from request */ 9.57 - u8 data; /* 5: Small chunk of in-band data */ 9.58 - s16 status; /* 6: USBIF_RSP_??? */ 9.59 - unsigned long transfer_mutex; /* Used for cancelling requests atomically. */ 9.60 - unsigned long length; /* 8: How much data we really got */ 9.61 -} PACKED usbif_response_t; 9.62 - 9.63 -#define USBIF_RSP_ERROR -1 /* non-specific 'error' */ 9.64 -#define USBIF_RSP_OKAY 0 /* non-specific 'okay' */ 9.65 - 9.66 -/* 9.67 - * We use a special capitalised type name because it is _essential_ that all 9.68 - * arithmetic on indexes is done on an integer type of the correct size. 9.69 - */ 9.70 -typedef u32 USBIF_RING_IDX; 9.71 - 9.72 -/* 9.73 - * Ring indexes are 'free running'. That is, they are not stored modulo the 9.74 - * size of the ring buffer. The following macro converts a free-running counter 9.75 - * into a value that can directly index a ring-buffer array. 9.76 - */ 9.77 -#define MASK_USBIF_IDX(_i) ((_i)&(USBIF_RING_SIZE-1)) 9.78 - 9.79 -typedef struct { 9.80 - USBIF_RING_IDX req_prod; /* 0: Request producer. Updated by front-end. */ 9.81 - USBIF_RING_IDX resp_prod; /* 4: Response producer. Updated by back-end. */ 9.82 - 9.83 - union { /* 8 */ 9.84 - usbif_request_t req; 9.85 - usbif_response_t resp; 9.86 - } PACKED ring[USBIF_RING_SIZE]; 9.87 -} PACKED usbif_t; 9.88 - 9.89 - 9.90 - 9.91 -/* 9.92 - * USBIF_OP_PROBE: 9.93 - * The request format for a probe request is constrained as follows: 9.94 - * @operation == USBIF_OP_PROBE 9.95 - * @nr_segments == size of probe buffer in pages 9.96 - * @device == unused (zero) 9.97 - * @id == any value (echoed in response message) 9.98 - * @sector_num == unused (zero) 9.99 - * @frame_and_sects == list of page-sized buffers. 9.100 - * (i.e., @first_sect == 0, @last_sect == 7). 9.101 - * 9.102 - * The response is a list of vdisk_t elements copied into the out-of-band 9.103 - * probe buffer. On success the response status field contains the number 9.104 - * of vdisk_t elements. 9.105 - */ 9.106 - 9.107 -typedef struct { 9.108 - unsigned long length; /* IN = expected, OUT = actual */ 9.109 - unsigned long buffer_offset; /* IN offset in buffer specified in main 9.110 - packet */ 9.111 - unsigned long status; /* OUT Status for this packet. */ 9.112 -} usbif_iso_t; 9.113 - 9.114 -#endif /* __SHARED_USBIF_H__ */
10.1 --- a/linux-2.4.29-xen-sparse/mkbuildtree Wed Jan 26 10:56:34 2005 +0000 10.2 +++ b/linux-2.4.29-xen-sparse/mkbuildtree Wed Jan 26 13:09:50 2005 +0000 10.3 @@ -282,4 +282,12 @@ ln -sf ../../../../../${LINUX_26}/driver 10.4 cd ${AD}/arch/xen/drivers/blkif/frontend 10.5 ln -sf ../../../../../${LINUX_26}/drivers/xen/blkfront/blkfront.c 10.6 10.7 +cd ${AD}/arch/xen/drivers/usbif/frontend 10.8 +ln -sf ../../../../../${LINUX_26}/drivers/xen/usbfront/usbfront.c main.c 10.9 +ln -sf ../../../../../${LINUX_26}/drivers/xen/usbfront/xhci.h 10.10 10.11 +cd ${AD}/arch/xen/drivers/usbif/backend 10.12 +ln -sf ../../../../../${LINUX_26}/drivers/xen/usbback/common.h 10.13 +ln -sf ../../../../../${LINUX_26}/drivers/xen/usbback/control.c 10.14 +ln -sf ../../../../../${LINUX_26}/drivers/xen/usbback/interface.c 10.15 +ln -sf ../../../../../${LINUX_26}/drivers/xen/usbback/usbback.c main.c
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/linux-2.6.10-xen-sparse/drivers/xen/usbback/common.h Wed Jan 26 13:09:50 2005 +0000 11.3 @@ -0,0 +1,87 @@ 11.4 + 11.5 +#ifndef __USBIF__BACKEND__COMMON_H__ 11.6 +#define __USBIF__BACKEND__COMMON_H__ 11.7 + 11.8 +#include <linux/config.h> 11.9 +#include <linux/version.h> 11.10 +#include <linux/module.h> 11.11 +#include <linux/rbtree.h> 11.12 +#include <linux/interrupt.h> 11.13 +#include <linux/slab.h> 11.14 +#include <linux/blkdev.h> 11.15 +#include <asm/io.h> 11.16 +#include <asm/setup.h> 11.17 +#include <asm/pgalloc.h> 11.18 +#include <asm-xen/ctrl_if.h> 11.19 +#include <asm-xen/hypervisor.h> 11.20 + 11.21 +#include <asm-xen/xen-public/io/usbif.h> 11.22 + 11.23 +#if 0 11.24 +#define ASSERT(_p) \ 11.25 + if ( !(_p) ) { printk("Assertion '%s' failed, line %d, file %s", #_p , \ 11.26 + __LINE__, __FILE__); *(int*)0=0; } 11.27 +#define DPRINTK(_f, _a...) printk(KERN_ALERT "(file=%s, line=%d) " _f, \ 11.28 + __FILE__ , __LINE__ , ## _a ) 11.29 +#else 11.30 +#define ASSERT(_p) ((void)0) 11.31 +#define DPRINTK(_f, _a...) ((void)0) 11.32 +#endif 11.33 + 11.34 +typedef struct usbif_priv_st usbif_priv_t; 11.35 + 11.36 +struct usbif_priv_st { 11.37 + /* Unique identifier for this interface. */ 11.38 + domid_t domid; 11.39 + unsigned int handle; 11.40 + /* Physical parameters of the comms window. */ 11.41 + unsigned long shmem_frame; 11.42 + unsigned int evtchn; 11.43 + int irq; 11.44 + /* Comms information. */ 11.45 + usbif_t *usb_ring_base; /* ioremap()'ed ptr to shmem_frame. */ 11.46 + USBIF_RING_IDX usb_req_cons; /* Request consumer. */ 11.47 + USBIF_RING_IDX usb_resp_prod; /* Private version of resp. producer. */ 11.48 + /* Private fields. */ 11.49 + enum { DISCONNECTED, DISCONNECTING, CONNECTED } status; 11.50 + /* 11.51 + * DISCONNECT response is deferred until pending requests are ack'ed. 11.52 + * We therefore need to store the id from the original request. 11.53 + */ 11.54 + u8 disconnect_rspid; 11.55 + usbif_priv_t *hash_next; 11.56 + struct list_head usbif_list; 11.57 + spinlock_t usb_ring_lock; 11.58 + atomic_t refcnt; 11.59 + atomic_t work_scheduled; 11.60 + 11.61 + struct work_struct work; 11.62 +}; 11.63 + 11.64 +void usbif_create(usbif_be_create_t *create); 11.65 +void usbif_destroy(usbif_be_destroy_t *destroy); 11.66 +void usbif_connect(usbif_be_connect_t *connect); 11.67 +int usbif_disconnect(usbif_be_disconnect_t *disconnect, u8 rsp_id); 11.68 +void usbif_disconnect_complete(usbif_priv_t *up); 11.69 + 11.70 +void usbif_release_port(usbif_be_release_port_t *msg); 11.71 +int usbif_claim_port(usbif_be_claim_port_t *msg); 11.72 +void usbif_release_ports(usbif_priv_t *up); 11.73 + 11.74 +usbif_priv_t *usbif_find(domid_t domid); 11.75 +#define usbif_get(_b) (atomic_inc(&(_b)->refcnt)) 11.76 +#define usbif_put(_b) \ 11.77 + do { \ 11.78 + if ( atomic_dec_and_test(&(_b)->refcnt) ) \ 11.79 + usbif_disconnect_complete(_b); \ 11.80 + } while (0) 11.81 + 11.82 + 11.83 +void usbif_interface_init(void); 11.84 +void usbif_ctrlif_init(void); 11.85 + 11.86 +void usbif_deschedule(usbif_priv_t *usbif); 11.87 + 11.88 +irqreturn_t usbif_be_int(int irq, void *dev_id, struct pt_regs *regs); 11.89 + 11.90 +#endif /* __USBIF__BACKEND__COMMON_H__ */
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/linux-2.6.10-xen-sparse/drivers/xen/usbback/control.c Wed Jan 26 13:09:50 2005 +0000 12.3 @@ -0,0 +1,77 @@ 12.4 +/****************************************************************************** 12.5 + * arch/xen/drivers/usbif/backend/control.c 12.6 + * 12.7 + * Routines for interfacing with the control plane. 12.8 + * 12.9 + * Copyright (c) 2004, Keir Fraser 12.10 + */ 12.11 + 12.12 +#include "common.h" 12.13 + 12.14 +static void usbif_ctrlif_rx(ctrl_msg_t *msg, unsigned long id) 12.15 +{ 12.16 + DPRINTK("Received usbif backend message, subtype=%d\n", msg->subtype); 12.17 + 12.18 + switch ( msg->subtype ) 12.19 + { 12.20 + case CMSG_USBIF_BE_CREATE: 12.21 + if ( msg->length != sizeof(usbif_be_create_t) ) 12.22 + goto parse_error; 12.23 + usbif_create((usbif_be_create_t *)&msg->msg[0]); 12.24 + break; 12.25 + case CMSG_USBIF_BE_DESTROY: 12.26 + if ( msg->length != sizeof(usbif_be_destroy_t) ) 12.27 + goto parse_error; 12.28 + usbif_destroy((usbif_be_destroy_t *)&msg->msg[0]); 12.29 + break; 12.30 + case CMSG_USBIF_BE_CONNECT: 12.31 + if ( msg->length != sizeof(usbif_be_connect_t) ) 12.32 + goto parse_error; 12.33 + usbif_connect((usbif_be_connect_t *)&msg->msg[0]); 12.34 + break; 12.35 + case CMSG_USBIF_BE_DISCONNECT: 12.36 + if ( msg->length != sizeof(usbif_be_disconnect_t) ) 12.37 + goto parse_error; 12.38 + if ( !usbif_disconnect((usbif_be_disconnect_t *)&msg->msg[0],msg->id) ) 12.39 + return; /* Sending the response is deferred until later. */ 12.40 + break; 12.41 + case CMSG_USBIF_BE_CLAIM_PORT: 12.42 + if ( msg->length != sizeof(usbif_be_claim_port_t) ) 12.43 + goto parse_error; 12.44 + usbif_claim_port((usbif_be_claim_port_t *)&msg->msg[0]); 12.45 + break; 12.46 + case CMSG_USBIF_BE_RELEASE_PORT: 12.47 + if ( msg->length != sizeof(usbif_be_release_port_t) ) 12.48 + goto parse_error; 12.49 + usbif_release_port((usbif_be_release_port_t *)&msg->msg[0]); 12.50 + break; 12.51 + default: 12.52 + goto parse_error; 12.53 + } 12.54 + 12.55 + ctrl_if_send_response(msg); 12.56 + return; 12.57 + 12.58 + parse_error: 12.59 + DPRINTK("Parse error while reading message subtype %d, len %d\n", 12.60 + msg->subtype, msg->length); 12.61 + msg->length = 0; 12.62 + ctrl_if_send_response(msg); 12.63 +} 12.64 + 12.65 +void usbif_ctrlif_init(void) 12.66 +{ 12.67 + ctrl_msg_t cmsg; 12.68 + usbif_be_driver_status_changed_t st; 12.69 + 12.70 + (void)ctrl_if_register_receiver(CMSG_USBIF_BE, usbif_ctrlif_rx, 12.71 + CALLBACK_IN_BLOCKING_CONTEXT); 12.72 + 12.73 + /* Send a driver-UP notification to the domain controller. */ 12.74 + cmsg.type = CMSG_USBIF_BE; 12.75 + cmsg.subtype = CMSG_USBIF_BE_DRIVER_STATUS_CHANGED; 12.76 + cmsg.length = sizeof(usbif_be_driver_status_changed_t); 12.77 + st.status = USBIF_DRIVER_STATUS_UP; 12.78 + memcpy(cmsg.msg, &st, sizeof(st)); 12.79 + ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE); 12.80 +}
13.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 13.2 +++ b/linux-2.6.10-xen-sparse/drivers/xen/usbback/interface.c Wed Jan 26 13:09:50 2005 +0000 13.3 @@ -0,0 +1,248 @@ 13.4 +/****************************************************************************** 13.5 + * arch/xen/drivers/usbif/backend/interface.c 13.6 + * 13.7 + * USB device interface management. 13.8 + * 13.9 + * by Mark Williamson, Copyright (c) 2004 13.10 + */ 13.11 + 13.12 + 13.13 +/****************************************************************************** 13.14 + * arch/xen/drivers/blkif/backend/interface.c 13.15 + * 13.16 + * Block-device interface management. 13.17 + * 13.18 + * Copyright (c) 2004, Keir Fraser 13.19 + */ 13.20 + 13.21 +#include "common.h" 13.22 + 13.23 +#define USBIF_HASHSZ 1024 13.24 +#define USBIF_HASH(_d) (((int)(_d))&(USBIF_HASHSZ-1)) 13.25 + 13.26 +static kmem_cache_t *usbif_priv_cachep; 13.27 +static usbif_priv_t *usbif_priv_hash[USBIF_HASHSZ]; 13.28 + 13.29 +usbif_priv_t *usbif_find(domid_t domid) 13.30 +{ 13.31 + usbif_priv_t *up = usbif_priv_hash[USBIF_HASH(domid)]; 13.32 + while ( (up != NULL ) && ( up->domid != domid ) ) 13.33 + up = up->hash_next; 13.34 + return up; 13.35 +} 13.36 + 13.37 +static void __usbif_disconnect_complete(void *arg) 13.38 +{ 13.39 + usbif_priv_t *usbif = (usbif_priv_t *)arg; 13.40 + ctrl_msg_t cmsg; 13.41 + usbif_be_disconnect_t disc; 13.42 + 13.43 + /* 13.44 + * These can't be done in usbif_disconnect() because at that point there 13.45 + * may be outstanding requests at the device whose asynchronous responses 13.46 + * must still be notified to the remote driver. 13.47 + */ 13.48 + unbind_evtchn_from_irq(usbif->evtchn); 13.49 + vfree(usbif->usb_ring_base); 13.50 + 13.51 + /* Construct the deferred response message. */ 13.52 + cmsg.type = CMSG_USBIF_BE; 13.53 + cmsg.subtype = CMSG_USBIF_BE_DISCONNECT; 13.54 + cmsg.id = usbif->disconnect_rspid; 13.55 + cmsg.length = sizeof(usbif_be_disconnect_t); 13.56 + disc.domid = usbif->domid; 13.57 + disc.status = USBIF_BE_STATUS_OKAY; 13.58 + memcpy(cmsg.msg, &disc, sizeof(disc)); 13.59 + 13.60 + /* 13.61 + * Make sure message is constructed /before/ status change, because 13.62 + * after the status change the 'usbif' structure could be deallocated at 13.63 + * any time. Also make sure we send the response /after/ status change, 13.64 + * as otherwise a subsequent CONNECT request could spuriously fail if 13.65 + * another CPU doesn't see the status change yet. 13.66 + */ 13.67 + mb(); 13.68 + if ( usbif->status != DISCONNECTING ) 13.69 + BUG(); 13.70 + usbif->status = DISCONNECTED; 13.71 + mb(); 13.72 + 13.73 + /* Send the successful response. */ 13.74 + ctrl_if_send_response(&cmsg); 13.75 +} 13.76 + 13.77 +void usbif_disconnect_complete(usbif_priv_t *up) 13.78 +{ 13.79 + INIT_WORK(&up->work, __usbif_disconnect_complete, (void *)up); 13.80 + schedule_work(&up->work); 13.81 +} 13.82 + 13.83 +void usbif_create(usbif_be_create_t *create) 13.84 +{ 13.85 + domid_t domid = create->domid; 13.86 + usbif_priv_t **pup, *up; 13.87 + 13.88 + if ( (up = kmem_cache_alloc(usbif_priv_cachep, GFP_KERNEL)) == NULL ) 13.89 + { 13.90 + DPRINTK("Could not create usbif: out of memory\n"); 13.91 + create->status = USBIF_BE_STATUS_OUT_OF_MEMORY; 13.92 + return; 13.93 + } 13.94 + 13.95 + memset(up, 0, sizeof(*up)); 13.96 + up->domid = domid; 13.97 + up->status = DISCONNECTED; 13.98 + spin_lock_init(&up->usb_ring_lock); 13.99 + atomic_set(&up->refcnt, 0); 13.100 + 13.101 + pup = &usbif_priv_hash[USBIF_HASH(domid)]; 13.102 + while ( *pup != NULL ) 13.103 + { 13.104 + if ( (*pup)->domid == domid ) 13.105 + { 13.106 + create->status = USBIF_BE_STATUS_INTERFACE_EXISTS; 13.107 + kmem_cache_free(usbif_priv_cachep, up); 13.108 + return; 13.109 + } 13.110 + pup = &(*pup)->hash_next; 13.111 + } 13.112 + 13.113 + up->hash_next = *pup; 13.114 + *pup = up; 13.115 + 13.116 + create->status = USBIF_BE_STATUS_OKAY; 13.117 +} 13.118 + 13.119 +void usbif_destroy(usbif_be_destroy_t *destroy) 13.120 +{ 13.121 + domid_t domid = destroy->domid; 13.122 + usbif_priv_t **pup, *up; 13.123 + 13.124 + pup = &usbif_priv_hash[USBIF_HASH(domid)]; 13.125 + while ( (up = *pup) != NULL ) 13.126 + { 13.127 + if ( up->domid == domid ) 13.128 + { 13.129 + if ( up->status != DISCONNECTED ) 13.130 + goto still_connected; 13.131 + goto destroy; 13.132 + } 13.133 + pup = &up->hash_next; 13.134 + } 13.135 + 13.136 + destroy->status = USBIF_BE_STATUS_INTERFACE_NOT_FOUND; 13.137 + return; 13.138 + 13.139 + still_connected: 13.140 + destroy->status = USBIF_BE_STATUS_INTERFACE_CONNECTED; 13.141 + return; 13.142 + 13.143 + destroy: 13.144 + *pup = up->hash_next; 13.145 + usbif_release_ports(up); 13.146 + kmem_cache_free(usbif_priv_cachep, up); 13.147 + destroy->status = USBIF_BE_STATUS_OKAY; 13.148 +} 13.149 + 13.150 +void usbif_connect(usbif_be_connect_t *connect) 13.151 +{ 13.152 + domid_t domid = connect->domid; 13.153 + unsigned int evtchn = connect->evtchn; 13.154 + unsigned long shmem_frame = connect->shmem_frame; 13.155 + struct vm_struct *vma; 13.156 + pgprot_t prot; 13.157 + int error; 13.158 + usbif_priv_t *up; 13.159 + 13.160 + up = usbif_find(domid); 13.161 + if ( unlikely(up == NULL) ) 13.162 + { 13.163 + DPRINTK("usbif_connect attempted for non-existent usbif (%u)\n", 13.164 + connect->domid); 13.165 + connect->status = USBIF_BE_STATUS_INTERFACE_NOT_FOUND; 13.166 + return; 13.167 + } 13.168 + 13.169 + if ( (vma = get_vm_area(PAGE_SIZE, VM_IOREMAP)) == NULL ) 13.170 + { 13.171 + connect->status = USBIF_BE_STATUS_OUT_OF_MEMORY; 13.172 + return; 13.173 + } 13.174 + 13.175 + prot = __pgprot(_PAGE_PRESENT | _PAGE_RW | _PAGE_DIRTY | _PAGE_ACCESSED); 13.176 + error = direct_remap_area_pages(&init_mm, VMALLOC_VMADDR(vma->addr), 13.177 + shmem_frame<<PAGE_SHIFT, PAGE_SIZE, 13.178 + prot, domid); 13.179 + if ( error != 0 ) 13.180 + { 13.181 + if ( error == -ENOMEM ) 13.182 + connect->status = USBIF_BE_STATUS_OUT_OF_MEMORY; 13.183 + else if ( error == -EFAULT ) 13.184 + connect->status = USBIF_BE_STATUS_MAPPING_ERROR; 13.185 + else 13.186 + connect->status = USBIF_BE_STATUS_ERROR; 13.187 + vfree(vma->addr); 13.188 + return; 13.189 + } 13.190 + 13.191 + if ( up->status != DISCONNECTED ) 13.192 + { 13.193 + connect->status = USBIF_BE_STATUS_INTERFACE_CONNECTED; 13.194 + vfree(vma->addr); 13.195 + return; 13.196 + } 13.197 + 13.198 + up->evtchn = evtchn; 13.199 + up->irq = bind_evtchn_to_irq(evtchn); 13.200 + up->shmem_frame = shmem_frame; 13.201 + up->usb_ring_base = (usbif_t *)vma->addr; 13.202 + up->status = CONNECTED; 13.203 + usbif_get(up); 13.204 + 13.205 + request_irq(up->irq, usbif_be_int, 0, "usbif-backend", up); 13.206 + 13.207 + connect->status = USBIF_BE_STATUS_OKAY; 13.208 +} 13.209 + 13.210 +/* Remove URBs for this interface before destroying it. */ 13.211 +void usbif_deschedule(usbif_priv_t *up) 13.212 +{ 13.213 + remove_from_usbif_list(up); 13.214 +} 13.215 + 13.216 +int usbif_disconnect(usbif_be_disconnect_t *disconnect, u8 rsp_id) 13.217 +{ 13.218 + domid_t domid = disconnect->domid; 13.219 + usbif_priv_t *up; 13.220 + 13.221 + up = usbif_find(domid); 13.222 + if ( unlikely(up == NULL) ) 13.223 + { 13.224 + DPRINTK("usbif_disconnect attempted for non-existent usbif" 13.225 + " (%u)\n", disconnect->domid); 13.226 + disconnect->status = USBIF_BE_STATUS_INTERFACE_NOT_FOUND; 13.227 + return 1; /* Caller will send response error message. */ 13.228 + } 13.229 + 13.230 + if ( up->status == CONNECTED ) 13.231 + { 13.232 + up->status = DISCONNECTING; 13.233 + up->disconnect_rspid = rsp_id; 13.234 + wmb(); /* Let other CPUs see the status change. */ 13.235 + free_irq(up->irq, up); 13.236 + usbif_deschedule(up); 13.237 + usbif_put(up); 13.238 + return 0; /* Caller should not send response message. */ 13.239 + } 13.240 + 13.241 + disconnect->status = USBIF_BE_STATUS_OKAY; 13.242 + return 1; 13.243 +} 13.244 + 13.245 +void __init usbif_interface_init(void) 13.246 +{ 13.247 + usbif_priv_cachep = kmem_cache_create("usbif_priv_cache", 13.248 + sizeof(usbif_priv_t), 13.249 + 0, 0, NULL, NULL); 13.250 + memset(usbif_priv_hash, 0, sizeof(usbif_priv_hash)); 13.251 +}
14.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 14.2 +++ b/linux-2.6.10-xen-sparse/drivers/xen/usbback/usbback.c Wed Jan 26 13:09:50 2005 +0000 14.3 @@ -0,0 +1,1011 @@ 14.4 +/****************************************************************************** 14.5 + * arch/xen/drivers/usbif/backend/main.c 14.6 + * 14.7 + * Backend for the Xen virtual USB driver - provides an abstraction of a 14.8 + * USB host controller to the corresponding frontend driver. 14.9 + * 14.10 + * by Mark Williamson, Copyright (c) 2004 Intel Research Cambridge 14.11 + * 14.12 + * Based on arch/xen/drivers/blkif/backend/main.c 14.13 + * Copyright (c) 2003-2004, Keir Fraser & Steve Hand 14.14 + */ 14.15 + 14.16 +#include "common.h" 14.17 + 14.18 + 14.19 +#include <linux/list.h> 14.20 +#include <linux/usb.h> 14.21 +#include <linux/spinlock.h> 14.22 +#include <linux/module.h> 14.23 +#include <linux/tqueue.h> 14.24 + 14.25 +/* 14.26 + * This is rather arbitrary. 14.27 + */ 14.28 +#define MAX_PENDING_REQS 4 14.29 +#define BATCH_PER_DOMAIN 1 14.30 + 14.31 +static unsigned long mmap_vstart; 14.32 + 14.33 +/* Needs to be sufficiently large that we can map the (large) buffers 14.34 + * the USB mass storage driver wants. */ 14.35 +#define MMAP_PAGES_PER_REQUEST \ 14.36 + (128) 14.37 +#define MMAP_PAGES \ 14.38 + (MAX_PENDING_REQS * MMAP_PAGES_PER_REQUEST) 14.39 + 14.40 +#define MMAP_VADDR(_req,_seg) \ 14.41 + (mmap_vstart + \ 14.42 + ((_req) * MMAP_PAGES_PER_REQUEST * PAGE_SIZE) + \ 14.43 + ((_seg) * PAGE_SIZE)) 14.44 + 14.45 +#define MIN(x,y) ( ( x < y ) ? x : y ) 14.46 + 14.47 +static spinlock_t owned_ports_lock; 14.48 +LIST_HEAD(owned_ports); 14.49 + 14.50 +/* A list of these structures is used to track ownership of physical USB 14.51 + * ports. */ 14.52 +typedef struct 14.53 +{ 14.54 + usbif_priv_t *usbif_priv; 14.55 + char path[16]; 14.56 + int guest_port; 14.57 + int enabled; 14.58 + struct list_head list; 14.59 + unsigned long guest_address; /* The USB device address that has been 14.60 + * assigned by the guest. */ 14.61 + int dev_present; /* Is there a device present? */ 14.62 + struct usb_device * dev; 14.63 + unsigned long ifaces; /* What interfaces are present on this device? */ 14.64 +} owned_port_t; 14.65 + 14.66 + 14.67 +/* 14.68 + * Each outstanding request that we've passed to the lower device layers has a 14.69 + * 'pending_req' allocated to it. The request is complete, the specified 14.70 + * domain has a response queued for it, with the saved 'id' passed back. 14.71 + */ 14.72 +typedef struct { 14.73 + usbif_priv_t *usbif_priv; 14.74 + usbif_iso_t *iso_sched; 14.75 + unsigned long id; 14.76 + int nr_pages; 14.77 + unsigned short operation; 14.78 + int status; 14.79 +} pending_req_t; 14.80 + 14.81 +/* 14.82 + * We can't allocate pending_req's in order, since they may complete out of 14.83 + * order. We therefore maintain an allocation ring. This ring also indicates 14.84 + * when enough work has been passed down -- at that point the allocation ring 14.85 + * will be empty. 14.86 + */ 14.87 +static pending_req_t pending_reqs[MAX_PENDING_REQS]; 14.88 +static unsigned char pending_ring[MAX_PENDING_REQS]; 14.89 +static spinlock_t pend_prod_lock = SPIN_LOCK_UNLOCKED; 14.90 + 14.91 +/* NB. We use a different index type to differentiate from shared blk rings. */ 14.92 +typedef unsigned int PEND_RING_IDX; 14.93 +#define MASK_PEND_IDX(_i) ((_i)&(MAX_PENDING_REQS-1)) 14.94 +static PEND_RING_IDX pending_prod, pending_cons; 14.95 +#define NR_PENDING_REQS (MAX_PENDING_REQS - pending_prod + pending_cons) 14.96 + 14.97 +static int do_usb_io_op(usbif_priv_t *usbif, int max_to_do); 14.98 +static void make_response(usbif_priv_t *usbif, unsigned long id, 14.99 + unsigned short op, int st, int inband, 14.100 + unsigned long actual_length); 14.101 +static void dispatch_usb_probe(usbif_priv_t *up, unsigned long id, unsigned long port); 14.102 +static void dispatch_usb_io(usbif_priv_t *up, usbif_request_t *req); 14.103 +static void dispatch_usb_reset(usbif_priv_t *up, unsigned long portid); 14.104 +static owned_port_t *usbif_find_port(char *); 14.105 + 14.106 + 14.107 +void dump_port(owned_port_t *p) 14.108 +{ 14.109 + printk("owned_port_t @ %p\n", p); 14.110 + printk(" usbif_priv @ %p\n", p->usbif_priv); 14.111 + printk(" path: %s\n", p->path); 14.112 + printk(" guest_port: %d\n", p->guest_port); 14.113 + printk(" guest_address: %ld\n", p->guest_address); 14.114 + printk(" dev_present: %d\n", p->dev_present); 14.115 + printk(" dev @ %p\n", p->dev); 14.116 + printk(" ifaces: 0x%lx\n", p->ifaces); 14.117 +} 14.118 + 14.119 + 14.120 + 14.121 +static void fast_flush_area(int idx, int nr_pages) 14.122 +{ 14.123 + multicall_entry_t mcl[MMAP_PAGES_PER_REQUEST]; 14.124 + int i; 14.125 + 14.126 + for ( i = 0; i < nr_pages; i++ ) 14.127 + { 14.128 + mcl[i].op = __HYPERVISOR_update_va_mapping; 14.129 + mcl[i].args[0] = MMAP_VADDR(idx, i) >> PAGE_SHIFT; 14.130 + mcl[i].args[1] = 0; 14.131 + mcl[i].args[2] = 0; 14.132 + } 14.133 + 14.134 + mcl[nr_pages-1].args[2] = UVMF_FLUSH_TLB; 14.135 + if ( unlikely(HYPERVISOR_multicall(mcl, nr_pages) != 0) ) 14.136 + BUG(); 14.137 +} 14.138 + 14.139 + 14.140 +/****************************************************************** 14.141 + * USB INTERFACE SCHEDULER LIST MAINTENANCE 14.142 + */ 14.143 + 14.144 +static struct list_head usbio_schedule_list; 14.145 +static spinlock_t usbio_schedule_list_lock; 14.146 + 14.147 +static int __on_usbif_list(usbif_priv_t *up) 14.148 +{ 14.149 + return up->usbif_list.next != NULL; 14.150 +} 14.151 + 14.152 +void remove_from_usbif_list(usbif_priv_t *up) 14.153 +{ 14.154 + unsigned long flags; 14.155 + if ( !__on_usbif_list(up) ) return; 14.156 + spin_lock_irqsave(&usbio_schedule_list_lock, flags); 14.157 + if ( __on_usbif_list(up) ) 14.158 + { 14.159 + list_del(&up->usbif_list); 14.160 + up->usbif_list.next = NULL; 14.161 + usbif_put(up); 14.162 + } 14.163 + spin_unlock_irqrestore(&usbio_schedule_list_lock, flags); 14.164 +} 14.165 + 14.166 +static void add_to_usbif_list_tail(usbif_priv_t *up) 14.167 +{ 14.168 + unsigned long flags; 14.169 + if ( __on_usbif_list(up) ) return; 14.170 + spin_lock_irqsave(&usbio_schedule_list_lock, flags); 14.171 + if ( !__on_usbif_list(up) && (up->status == CONNECTED) ) 14.172 + { 14.173 + list_add_tail(&up->usbif_list, &usbio_schedule_list); 14.174 + usbif_get(up); 14.175 + } 14.176 + spin_unlock_irqrestore(&usbio_schedule_list_lock, flags); 14.177 +} 14.178 + 14.179 + 14.180 +/****************************************************************** 14.181 + * COMPLETION CALLBACK -- Called as urb->complete() 14.182 + */ 14.183 + 14.184 +static void maybe_trigger_usbio_schedule(void); 14.185 + 14.186 +static void __end_usb_io_op(struct urb *purb) 14.187 +{ 14.188 + unsigned long flags; 14.189 + pending_req_t *pending_req; 14.190 + int pending_idx; 14.191 + 14.192 + pending_req = purb->context; 14.193 + 14.194 +/* printk("Completed for id = %p to 0x%lx - 0x%lx\n", pending_req->id, */ 14.195 +/* virt_to_machine(purb->transfer_buffer), */ 14.196 +/* virt_to_machine(purb->transfer_buffer) */ 14.197 +/* + pending_req->nr_pages * PAGE_SIZE); */ 14.198 + 14.199 + pending_idx = pending_req - pending_reqs; 14.200 + 14.201 + ASSERT(purb->actual_length <= purb->transfer_buffer_length); 14.202 + ASSERT(purb->actual_length <= pending_req->nr_pages * PAGE_SIZE); 14.203 + 14.204 + /* An error fails the entire request. */ 14.205 + if ( purb->status ) 14.206 + { 14.207 + printk("URB @ %p failed. Status %d\n", purb, purb->status); 14.208 + } 14.209 + 14.210 + if ( usb_pipetype(purb->pipe) == 0 ) 14.211 + { 14.212 + int i; 14.213 + usbif_iso_t *sched = (usbif_iso_t *)MMAP_VADDR(pending_idx, pending_req->nr_pages - 1); 14.214 + 14.215 + ASSERT(sched == pending_req->sched); 14.216 + 14.217 + // printk("writing back schedule at %p\n", sched); 14.218 + 14.219 + /* If we're dealing with an iso pipe, we need to copy back the schedule. */ 14.220 + for ( i = 0; i < purb->number_of_packets; i++ ) 14.221 + { 14.222 + sched[i].length = purb->iso_frame_desc[i].actual_length; 14.223 + ASSERT(sched[i].buffer_offset == 14.224 + purb->iso_frame_desc[i].offset); 14.225 + sched[i].status = purb->iso_frame_desc[i].status; 14.226 + } 14.227 + } 14.228 + 14.229 + // printk("Flushing %d pages\n", pending_req->nr_pages); 14.230 + fast_flush_area(pending_req - pending_reqs, pending_req->nr_pages); 14.231 + 14.232 + kfree(purb->setup_packet); 14.233 + 14.234 + spin_lock_irqsave(&pending_req->usbif_priv->usb_ring_lock, flags); 14.235 + make_response(pending_req->usbif_priv, pending_req->id, 14.236 + pending_req->operation, pending_req->status, 0, purb->actual_length); 14.237 + spin_unlock_irqrestore(&pending_req->usbif_priv->usb_ring_lock, flags); 14.238 + usbif_put(pending_req->usbif_priv); 14.239 + 14.240 + usb_free_urb(purb); 14.241 + 14.242 + /* Free the pending request. */ 14.243 + spin_lock_irqsave(&pend_prod_lock, flags); 14.244 + pending_ring[MASK_PEND_IDX(pending_prod++)] = pending_idx; 14.245 + spin_unlock_irqrestore(&pend_prod_lock, flags); 14.246 + 14.247 + rmb(); 14.248 + 14.249 + /* Check for anything still waiting in the rings, having freed a request... */ 14.250 + maybe_trigger_usbio_schedule(); 14.251 +} 14.252 + 14.253 +/****************************************************************** 14.254 + * SCHEDULER FUNCTIONS 14.255 + */ 14.256 + 14.257 +static DECLARE_WAIT_QUEUE_HEAD(usbio_schedule_wait); 14.258 + 14.259 +static int usbio_schedule(void *arg) 14.260 +{ 14.261 + DECLARE_WAITQUEUE(wq, current); 14.262 + 14.263 + usbif_priv_t *up; 14.264 + struct list_head *ent; 14.265 + 14.266 + daemonize(); 14.267 + 14.268 + for ( ; ; ) 14.269 + { 14.270 + /* Wait for work to do. */ 14.271 + add_wait_queue(&usbio_schedule_wait, &wq); 14.272 + set_current_state(TASK_INTERRUPTIBLE); 14.273 + if ( (NR_PENDING_REQS == MAX_PENDING_REQS) || 14.274 + list_empty(&usbio_schedule_list) ) 14.275 + schedule(); 14.276 + __set_current_state(TASK_RUNNING); 14.277 + remove_wait_queue(&usbio_schedule_wait, &wq); 14.278 + 14.279 + /* Queue up a batch of requests. */ 14.280 + while ( (NR_PENDING_REQS < MAX_PENDING_REQS) && 14.281 + !list_empty(&usbio_schedule_list) ) 14.282 + { 14.283 + ent = usbio_schedule_list.next; 14.284 + up = list_entry(ent, usbif_priv_t, usbif_list); 14.285 + usbif_get(up); 14.286 + remove_from_usbif_list(up); 14.287 + if ( do_usb_io_op(up, BATCH_PER_DOMAIN) ) 14.288 + add_to_usbif_list_tail(up); 14.289 + usbif_put(up); 14.290 + } 14.291 + } 14.292 +} 14.293 + 14.294 +static void maybe_trigger_usbio_schedule(void) 14.295 +{ 14.296 + /* 14.297 + * Needed so that two processes, who together make the following predicate 14.298 + * true, don't both read stale values and evaluate the predicate 14.299 + * incorrectly. Incredibly unlikely to stall the scheduler on x86, but... 14.300 + */ 14.301 + smp_mb(); 14.302 + 14.303 + if ( !list_empty(&usbio_schedule_list) ) 14.304 + wake_up(&usbio_schedule_wait); 14.305 +} 14.306 + 14.307 + 14.308 +/****************************************************************************** 14.309 + * NOTIFICATION FROM GUEST OS. 14.310 + */ 14.311 + 14.312 +irqreturn_t usbif_be_int(int irq, void *dev_id, struct pt_regs *regs) 14.313 +{ 14.314 + usbif_priv_t *up = dev_id; 14.315 + 14.316 + smp_mb(); 14.317 + 14.318 + add_to_usbif_list_tail(up); 14.319 + 14.320 + /* Will in fact /always/ trigger an io schedule in this case. */ 14.321 + maybe_trigger_usbio_schedule(); 14.322 + 14.323 + return IRQ_HANDLED; 14.324 +} 14.325 + 14.326 + 14.327 + 14.328 +/****************************************************************** 14.329 + * DOWNWARD CALLS -- These interface with the usb-device layer proper. 14.330 + */ 14.331 + 14.332 +static int do_usb_io_op(usbif_priv_t *up, int max_to_do) 14.333 +{ 14.334 + usbif_t *usb_ring = up->usb_ring_base; 14.335 + usbif_request_t *req; 14.336 + USBIF_RING_IDX i, rp; 14.337 + int more_to_do = 0; 14.338 + unsigned long flags; 14.339 + 14.340 + spin_lock_irqsave(&up->usb_ring_lock, flags); 14.341 + 14.342 + rp = usb_ring->req_prod; 14.343 + rmb(); /* Ensure we see queued requests up to 'rp'. */ 14.344 + 14.345 + /* Take items off the comms ring, taking care not to overflow. */ 14.346 + for ( i = up->usb_req_cons; 14.347 + (i != rp) && ((i-up->usb_resp_prod) != USBIF_RING_SIZE); 14.348 + i++ ) 14.349 + { 14.350 + if ( (max_to_do-- == 0) || (NR_PENDING_REQS == MAX_PENDING_REQS) ) 14.351 + { 14.352 + more_to_do = 1; 14.353 + break; 14.354 + } 14.355 + 14.356 + req = &usb_ring->ring[MASK_USBIF_IDX(i)].req; 14.357 + 14.358 + switch ( req->operation ) 14.359 + { 14.360 + case USBIF_OP_PROBE: 14.361 + dispatch_usb_probe(up, req->id, req->port); 14.362 + break; 14.363 + 14.364 + case USBIF_OP_IO: 14.365 + /* Assemble an appropriate URB. */ 14.366 + dispatch_usb_io(up, req); 14.367 + break; 14.368 + 14.369 + case USBIF_OP_RESET: 14.370 + dispatch_usb_reset(up, req->port); 14.371 + break; 14.372 + 14.373 + default: 14.374 + DPRINTK("error: unknown USB io operation [%d]\n", 14.375 + req->operation); 14.376 + make_response(up, req->id, req->operation, -EINVAL, 0, 0); 14.377 + break; 14.378 + } 14.379 + } 14.380 + 14.381 + up->usb_req_cons = i; 14.382 + 14.383 + spin_unlock_irqrestore(&up->usb_ring_lock, flags); 14.384 + 14.385 + return more_to_do; 14.386 +} 14.387 + 14.388 +static owned_port_t *find_guest_port(usbif_priv_t *up, int port) 14.389 +{ 14.390 + unsigned long flags; 14.391 + struct list_head *l; 14.392 + 14.393 + spin_lock_irqsave(&owned_ports_lock, flags); 14.394 + list_for_each(l, &owned_ports) 14.395 + { 14.396 + owned_port_t *p = list_entry(l, owned_port_t, list); 14.397 + if(p->usbif_priv == up && p->guest_port == port) 14.398 + { 14.399 + spin_unlock_irqrestore(&owned_ports_lock, flags); 14.400 + return p; 14.401 + } 14.402 + } 14.403 + spin_unlock_irqrestore(&owned_ports_lock, flags); 14.404 + 14.405 + return NULL; 14.406 +} 14.407 + 14.408 +static void dispatch_usb_reset(usbif_priv_t *up, unsigned long portid) 14.409 +{ 14.410 + owned_port_t *port = find_guest_port(up, portid); 14.411 + int ret = 0; 14.412 + 14.413 + 14.414 + /* Allowing the guest to actually reset the device causes more problems 14.415 + * than it's worth. We just fake it out in software but we will do a real 14.416 + * reset when the interface is destroyed. */ 14.417 + 14.418 +#if 0 14.419 + printk("Reset port %d\n", portid); 14.420 + 14.421 + dump_port(port); 14.422 +#endif 14.423 + 14.424 + port->guest_address = 0; 14.425 + /* If there's an attached device then the port is now enabled. */ 14.426 + if ( port->dev_present ) 14.427 + port->enabled = 1; 14.428 + else 14.429 + port->enabled = 0; 14.430 + 14.431 + make_response(up, 0, USBIF_OP_RESET, ret, 0, 0); 14.432 +} 14.433 + 14.434 +static void dispatch_usb_probe(usbif_priv_t *up, unsigned long id, unsigned long portid) 14.435 +{ 14.436 + owned_port_t *port = find_guest_port(up, portid); 14.437 + int ret; 14.438 + 14.439 + if ( port != NULL ) 14.440 + ret = port->dev_present; 14.441 + else 14.442 + { 14.443 + ret = -EINVAL; 14.444 + printk("dispatch_usb_probe(): invalid port probe request (port %ld)\n", 14.445 + portid); 14.446 + } 14.447 + 14.448 + /* Probe result is sent back in-band. Probes don't have an associated id 14.449 + * right now... */ 14.450 + make_response(up, id, USBIF_OP_PROBE, ret, portid, 0); 14.451 +} 14.452 + 14.453 +owned_port_t *find_port_for_request(usbif_priv_t *up, usbif_request_t *req); 14.454 + 14.455 +static void dump_request(usbif_request_t *req) 14.456 +{ 14.457 + printk("id = 0x%lx\n", req->id); 14.458 + 14.459 + printk("devnum %d\n", req->devnum); 14.460 + printk("endpoint 0x%x\n", req->endpoint); 14.461 + printk("direction %d\n", req->direction); 14.462 + printk("speed %d\n", req->speed); 14.463 + printk("pipe_type 0x%x\n", req->pipe_type); 14.464 + printk("transfer_buffer 0x%lx\n", req->transfer_buffer); 14.465 + printk("length 0x%lx\n", req->length); 14.466 + printk("transfer_flags 0x%lx\n", req->transfer_flags); 14.467 + printk("setup = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", 14.468 + req->setup[0], req->setup[1], req->setup[2], req->setup[3], 14.469 + req->setup[4], req->setup[5], req->setup[6], req->setup[7]); 14.470 + printk("iso_schedule = 0x%lx\n", req->iso_schedule); 14.471 + printk("num_iso %ld\n", req->num_iso); 14.472 +} 14.473 + 14.474 +void dump_urb(struct urb *urb) 14.475 +{ 14.476 + printk("dumping urb @ %p\n", urb); 14.477 + 14.478 +#define DUMP_URB_FIELD(name, format) printk(" " # name " " format "\n", urb-> name) 14.479 + 14.480 + DUMP_URB_FIELD(pipe, "0x%x"); 14.481 + DUMP_URB_FIELD(status, "%d"); 14.482 + DUMP_URB_FIELD(transfer_flags, "0x%x"); 14.483 + DUMP_URB_FIELD(transfer_buffer, "%p"); 14.484 + DUMP_URB_FIELD(transfer_buffer_length, "%d"); 14.485 + DUMP_URB_FIELD(actual_length, "%d"); 14.486 +} 14.487 + 14.488 + 14.489 +static void dispatch_usb_io(usbif_priv_t *up, usbif_request_t *req) 14.490 +{ 14.491 + unsigned long buffer_mach; 14.492 + int i = 0, offset = 0, 14.493 + pending_idx = pending_ring[MASK_PEND_IDX(pending_cons)]; 14.494 + pending_req_t *pending_req; 14.495 + unsigned long remap_prot; 14.496 + multicall_entry_t mcl[MMAP_PAGES_PER_REQUEST]; 14.497 + struct urb *purb = NULL; 14.498 + owned_port_t *port; 14.499 + unsigned char *setup; 14.500 + 14.501 +// dump_request(req); 14.502 + 14.503 + if ( NR_PENDING_REQS == MAX_PENDING_REQS ) 14.504 + { 14.505 + printk("usbback: Max requests already queued. Now giving up!\n"); 14.506 + 14.507 + return; 14.508 + } 14.509 + 14.510 + port = find_port_for_request(up, req); 14.511 + 14.512 + if(port == NULL) 14.513 + { 14.514 + printk("No such device! (%d)\n", req->devnum); 14.515 + dump_request(req); 14.516 + 14.517 + make_response(up, req->id, req->operation, -ENODEV, 0, 0); 14.518 + return; 14.519 + } 14.520 + 14.521 + setup = kmalloc(8, GFP_ATOMIC | GFP_NOIO); 14.522 + 14.523 + if ( setup == NULL ) 14.524 + goto no_mem; 14.525 + 14.526 + /* Copy request out for safety. */ 14.527 + memcpy(setup, req->setup, 8); 14.528 + 14.529 + if( setup[0] == 0x0 && setup[1] == 0x5) 14.530 + { 14.531 + /* To virtualise the USB address space, we need to intercept 14.532 + * set_address messages and emulate. From the USB specification: 14.533 + * bmRequestType = 0x0; 14.534 + * Brequest = SET_ADDRESS (i.e. 0x5) 14.535 + * wValue = device address 14.536 + * wIndex = 0 14.537 + * wLength = 0 14.538 + * data = None 14.539 + */ 14.540 + /* Store into the guest transfer buffer using cpu_to_le16 */ 14.541 + port->guest_address = le16_to_cpu(*(u16 *)(setup + 2)); 14.542 + /* Make a successful response. That was easy! */ 14.543 + 14.544 + make_response(up, req->id, req->operation, 0, 0, 0); 14.545 + 14.546 + kfree(setup); 14.547 + return; 14.548 + } 14.549 + else if ( setup[0] == 0x0 && setup[1] == 0x9 ) 14.550 + { 14.551 + /* The host kernel needs to know what device configuration is in use 14.552 + * because various error checks get confused otherwise. We just do 14.553 + * configuration settings here, under controlled conditions. 14.554 + */ 14.555 + usb_set_configuration(port->dev, setup[2]); 14.556 + 14.557 + make_response(up, req->id, req->operation, 0, 0, 0); 14.558 + 14.559 + kfree(setup); 14.560 + return; 14.561 + } 14.562 + 14.563 + else if ( setup[0] == 0x1 && setup[1] == 0xB ) 14.564 + { 14.565 + /* The host kernel needs to know what device interface is in use 14.566 + * because various error checks get confused otherwise. We just do 14.567 + * configuration settings here, under controlled conditions. 14.568 + */ 14.569 + usb_set_interface(port->dev, (setup[4] | setup[5] << 8), 14.570 + (setup[2] | setup[3] << 8) ); 14.571 + 14.572 + make_response(up, req->id, req->operation, 0, 0, 0); 14.573 + 14.574 + kfree(setup); 14.575 + return; 14.576 + } 14.577 + 14.578 + if ( ( req->transfer_buffer - (req->transfer_buffer & PAGE_MASK) 14.579 + + req->length ) 14.580 + > MMAP_PAGES_PER_REQUEST * PAGE_SIZE ) 14.581 + { 14.582 + printk("usbback: request of %d bytes too large, failing it\n", req->length); 14.583 + make_response(up, req->id, req->operation, -EINVAL, 0, 0); 14.584 + kfree(setup); 14.585 + return; 14.586 + } 14.587 + 14.588 + buffer_mach = req->transfer_buffer; 14.589 + 14.590 + if( buffer_mach == 0 ) 14.591 + goto no_remap; 14.592 + 14.593 + ASSERT((req->length >> PAGE_SHIFT) <= MMAP_PAGES_PER_REQUEST); 14.594 + ASSERT(buffer_mach); 14.595 + 14.596 + /* Always map writeable for now. */ 14.597 + remap_prot = _PAGE_PRESENT|_PAGE_DIRTY|_PAGE_ACCESSED|_PAGE_RW; 14.598 + 14.599 + for ( i = 0, offset = 0; offset < req->length; 14.600 + i++, offset += PAGE_SIZE ) 14.601 + { 14.602 + // printk("length = %d, offset = %d, looping!\n", req->length, offset); 14.603 + 14.604 + mcl[i].op = __HYPERVISOR_update_va_mapping_otherdomain; 14.605 + mcl[i].args[0] = MMAP_VADDR(pending_idx, i) >> PAGE_SHIFT; 14.606 + mcl[i].args[1] = ((buffer_mach & PAGE_MASK) + offset) | remap_prot; 14.607 + mcl[i].args[2] = 0; 14.608 + mcl[i].args[3] = up->domid; 14.609 + 14.610 + phys_to_machine_mapping[__pa(MMAP_VADDR(pending_idx, i))>>PAGE_SHIFT] = 14.611 + FOREIGN_FRAME((buffer_mach + offset) >> PAGE_SHIFT); 14.612 + // printk("i = %d\n", i); 14.613 + 14.614 + ASSERT(virt_to_machine(MMAP_VADDR(pending_idx, i)) 14.615 + == buffer_mach + i << PAGE_SHIFT); 14.616 + } 14.617 + 14.618 + if ( req->pipe_type == 0 && req->num_iso > 0 ) /* Maybe schedule ISO... */ 14.619 + { 14.620 + // printk("for iso, i = %d\n", i); 14.621 + /* Map in ISO schedule, if necessary. */ 14.622 + mcl[i].op = __HYPERVISOR_update_va_mapping_otherdomain; 14.623 + mcl[i].args[0] = MMAP_VADDR(pending_idx, i) >> PAGE_SHIFT; 14.624 + mcl[i].args[1] = (req->iso_schedule & PAGE_MASK) | remap_prot; 14.625 + mcl[i].args[2] = 0; 14.626 + mcl[i].args[3] = up->domid; 14.627 + 14.628 + phys_to_machine_mapping[__pa(MMAP_VADDR(pending_idx, i))>>PAGE_SHIFT] = 14.629 + FOREIGN_FRAME(req->iso_schedule >> PAGE_SHIFT); 14.630 + 14.631 + // printk("Mapped iso at %p\n", MMAP_VADDR(pending_idx, i)); 14.632 + i++; 14.633 + } 14.634 + 14.635 + // printk("Well we got this far!\n"); 14.636 + 14.637 + if ( unlikely(HYPERVISOR_multicall(mcl, i) != 0) ) 14.638 + BUG(); 14.639 + 14.640 + { 14.641 + int j; 14.642 + for ( j = 0; j < i; j++ ) 14.643 + { 14.644 + if ( unlikely(mcl[j].args[5] != 0) ) 14.645 + { 14.646 + printk("invalid buffer %d -- could not remap it\n", j); 14.647 + fast_flush_area(pending_idx, i); 14.648 + printk("sending invalid descriptor\n"); 14.649 + goto bad_descriptor; 14.650 + } 14.651 + } 14.652 + } 14.653 + 14.654 + no_remap: 14.655 + 14.656 + ASSERT(i <= MMAP_PAGES_PER_REQUEST); 14.657 + ASSERT(i * PAGE_SIZE >= req->length); 14.658 + 14.659 + /* We have to do this because some things might complete out of order. */ 14.660 + pending_req = &pending_reqs[pending_idx]; 14.661 + pending_req->usbif_priv= up; 14.662 + pending_req->id = req->id; 14.663 + pending_req->operation = req->operation; 14.664 + pending_req->nr_pages = i; 14.665 + 14.666 + 14.667 + 14.668 + pending_cons++; 14.669 + 14.670 + usbif_get(up); 14.671 + 14.672 + /* Fill out an actual request for the USB layer. */ 14.673 + purb = usb_alloc_urb(req->num_iso); 14.674 + 14.675 + if ( purb == NULL ) 14.676 + goto no_mem; 14.677 + 14.678 + purb->dev = port->dev; 14.679 + purb->context = pending_req; 14.680 + purb->transfer_buffer = (void *)MMAP_VADDR(pending_idx, 0) + (buffer_mach & ~PAGE_MASK); 14.681 + if(buffer_mach == 0) 14.682 + purb->transfer_buffer = NULL; 14.683 + purb->complete = __end_usb_io_op; 14.684 + purb->transfer_buffer_length = req->length; 14.685 + purb->transfer_flags = req->transfer_flags; 14.686 + 14.687 +/* if ( req->transfer_flags != 0 ) */ 14.688 +/* dump_request(req); */ 14.689 + 14.690 + purb->pipe = 0; 14.691 + purb->pipe |= req->direction << 7; 14.692 + purb->pipe |= port->dev->devnum << 8; 14.693 + purb->pipe |= req->speed << 26; 14.694 + purb->pipe |= req->pipe_type << 30; 14.695 + purb->pipe |= req->endpoint << 15; 14.696 + 14.697 + purb->number_of_packets = req->num_iso; 14.698 + 14.699 + /* Make sure there's always some kind of timeout. */ 14.700 + purb->timeout = ( req->timeout > 0 ) ? (req->timeout * HZ) / 1000 14.701 + : 1000; 14.702 + 14.703 + purb->setup_packet = setup; 14.704 + 14.705 + if ( req->pipe_type == 0 ) /* ISO */ 14.706 + { 14.707 + int j; 14.708 + usbif_iso_t *iso_sched = (usbif_iso_t *)MMAP_VADDR(pending_idx, i - 1); 14.709 + 14.710 + // printk("Reading iso sched at %p\n", iso_sched); 14.711 + 14.712 + /* If we're dealing with an iso pipe, we need to copy in a schedule. */ 14.713 + for ( j = 0; j < req->num_iso; j++ ) 14.714 + { 14.715 + purb->iso_frame_desc[j].length = iso_sched[j].length; 14.716 + purb->iso_frame_desc[j].offset = iso_sched[j].buffer_offset; 14.717 + iso_sched[j].status = 0; 14.718 + } 14.719 + pending_req->iso_sched = iso_sched; 14.720 + } 14.721 + 14.722 + { 14.723 + int ret; 14.724 + ret = usb_submit_urb(purb); 14.725 + 14.726 + // dump_urb(purb); 14.727 + 14.728 + if ( ret != 0 ) 14.729 + goto bad_descriptor; /* XXX free pending here! */ 14.730 + } 14.731 + 14.732 + return; 14.733 + 14.734 + bad_descriptor: 14.735 + kfree ( setup ); 14.736 + if ( purb != NULL ) 14.737 + usb_free_urb(purb); 14.738 + make_response(up, req->id, req->operation, -EINVAL, 0, 0); 14.739 + return; 14.740 + 14.741 + no_mem: 14.742 + if ( setup != NULL ) 14.743 + kfree(setup); 14.744 + make_response(up, req->id, req->operation, -ENOMEM, 0, 0); 14.745 + return; 14.746 +} 14.747 + 14.748 + 14.749 + 14.750 +/****************************************************************** 14.751 + * MISCELLANEOUS SETUP / TEARDOWN / DEBUGGING 14.752 + */ 14.753 + 14.754 + 14.755 +static void make_response(usbif_priv_t *up, unsigned long id, 14.756 + unsigned short op, int st, int inband, 14.757 + unsigned long length) 14.758 +{ 14.759 + usbif_response_t *resp; 14.760 + unsigned long flags; 14.761 + 14.762 +#if 0 14.763 + printk("usbback: Sending response:\n"); 14.764 + printk(" id = 0x%x\n", id); 14.765 + printk(" op = %d\n", op); 14.766 + printk(" status = %d\n", st); 14.767 + printk(" data = %d\n", inband); 14.768 + printk(" length = %d\n", length); 14.769 +#endif 14.770 + 14.771 + /* Place on the response ring for the relevant domain. */ 14.772 + spin_lock_irqsave(&up->usb_ring_lock, flags); 14.773 + resp = &up->usb_ring_base-> 14.774 + ring[MASK_USBIF_IDX(up->usb_resp_prod)].resp; 14.775 + resp->id = id; 14.776 + resp->operation = op; 14.777 + resp->status = st; 14.778 + resp->data = inband; 14.779 + resp->length = length; 14.780 + wmb(); /* Ensure other side can see the response fields. */ 14.781 + up->usb_ring_base->resp_prod = ++up->usb_resp_prod; 14.782 + spin_unlock_irqrestore(&up->usb_ring_lock, flags); 14.783 + 14.784 + /* Kick the relevant domain. */ 14.785 + notify_via_evtchn(up->evtchn); 14.786 +} 14.787 + 14.788 +/** 14.789 + * usbif_claim_port - claim devices on a port on behalf of guest 14.790 + * 14.791 + * Once completed, this will ensure that any device attached to that 14.792 + * port is claimed by this driver for use by the guest. 14.793 + */ 14.794 +int usbif_claim_port(usbif_be_claim_port_t *msg) 14.795 +{ 14.796 + owned_port_t *o_p; 14.797 + 14.798 + /* Sanity... */ 14.799 + if ( usbif_find_port(msg->path) != NULL ) 14.800 + { 14.801 + printk("usbback: Attempted to claim USB port " 14.802 + "we already own!\n"); 14.803 + return -EINVAL; 14.804 + } 14.805 + 14.806 + spin_lock_irq(&owned_ports_lock); 14.807 + 14.808 + /* No need for a slab cache - this should be infrequent. */ 14.809 + o_p = kmalloc(sizeof(owned_port_t), GFP_KERNEL); 14.810 + 14.811 + o_p->enabled = 0; 14.812 + o_p->usbif_priv = usbif_find(msg->domid); 14.813 + o_p->guest_port = msg->usbif_port; 14.814 + o_p->dev_present = 0; 14.815 + o_p->guest_address = 0; /* Default address. */ 14.816 + 14.817 + strcpy(o_p->path, msg->path); 14.818 + 14.819 + list_add(&o_p->list, &owned_ports); 14.820 + 14.821 + printk("usbback: Claimed USB port (%s) for %d.%d\n", o_p->path, 14.822 + msg->domid, msg->usbif_port); 14.823 + 14.824 + spin_unlock_irq(&owned_ports_lock); 14.825 + 14.826 + /* Force a reprobe for unclaimed devices. */ 14.827 + usb_scan_devices(); 14.828 + 14.829 + return 0; 14.830 +} 14.831 + 14.832 +owned_port_t *find_port_for_request(usbif_priv_t *up, usbif_request_t *req) 14.833 +{ 14.834 + unsigned long flags; 14.835 + struct list_head *port; 14.836 + 14.837 + /* I'm assuming this is not called from IRQ context - correct? I think 14.838 + * it's probably only called in response to control messages or plug events 14.839 + * in the USB hub kernel thread, so should be OK. */ 14.840 + spin_lock_irqsave(&owned_ports_lock, flags); 14.841 + list_for_each(port, &owned_ports) 14.842 + { 14.843 + owned_port_t *p = list_entry(port, owned_port_t, list); 14.844 + if(p->usbif_priv == up && p->guest_address == req->devnum && p->enabled ) 14.845 + { 14.846 +#if 0 14.847 + printk("Found port for devnum %d\n", req->devnum); 14.848 + 14.849 + dump_port(p); 14.850 +#endif 14.851 + return p; 14.852 + } 14.853 + } 14.854 + spin_unlock_irqrestore(&owned_ports_lock, flags); 14.855 + 14.856 + return NULL; 14.857 +} 14.858 + 14.859 +owned_port_t *usbif_find_port(char *path) 14.860 +{ 14.861 + struct list_head *port; 14.862 + unsigned long flags; 14.863 + 14.864 + spin_lock_irqsave(&owned_ports_lock, flags); 14.865 + list_for_each(port, &owned_ports) 14.866 + { 14.867 + owned_port_t *p = list_entry(port, owned_port_t, list); 14.868 + if(!strcmp(path, p->path)) 14.869 + { 14.870 + spin_unlock_irqrestore(&owned_ports_lock, flags); 14.871 + return p; 14.872 + } 14.873 + } 14.874 + spin_unlock_irqrestore(&owned_ports_lock, flags); 14.875 + 14.876 + return NULL; 14.877 +} 14.878 + 14.879 + 14.880 +static void *probe(struct usb_device *dev, unsigned iface, 14.881 + const struct usb_device_id *id) 14.882 +{ 14.883 + owned_port_t *p; 14.884 + 14.885 + /* We don't care what the device is - if we own the port, we want it. We 14.886 + * don't deal with device-specifics in this driver, so we don't care what 14.887 + * the device actually is ;-) */ 14.888 + if ( ( p = usbif_find_port(dev->devpath) ) != NULL ) 14.889 + { 14.890 + printk("usbback: claimed device attached to owned port\n"); 14.891 + 14.892 + p->dev_present = 1; 14.893 + p->dev = dev; 14.894 + set_bit(iface, &p->ifaces); 14.895 + 14.896 + return p->usbif_priv; 14.897 + } 14.898 + else 14.899 + printk("usbback: hotplug for non-owned port (%s), ignoring\n", dev->devpath); 14.900 + 14.901 + 14.902 + return NULL; 14.903 +} 14.904 + 14.905 +static void disconnect(struct usb_device *dev, void *usbif) 14.906 +{ 14.907 + /* Note the device is removed so we can tell the guest when it probes. */ 14.908 + owned_port_t *port = usbif_find_port(dev->devpath); 14.909 + port->dev_present = 0; 14.910 + port->dev = NULL; 14.911 + port->ifaces = 0; 14.912 +} 14.913 + 14.914 + 14.915 +struct usb_driver driver = 14.916 +{ 14.917 + .owner = THIS_MODULE, 14.918 + .name = "Xen USB Backend", 14.919 + .probe = probe, 14.920 + .disconnect = disconnect, 14.921 + .id_table = NULL, 14.922 +}; 14.923 + 14.924 +/* __usbif_release_port - internal mechanics for releasing a port */ 14.925 +void __usbif_release_port(owned_port_t *p) 14.926 +{ 14.927 + int i; 14.928 + 14.929 + for ( i = 0; p->ifaces != 0; i++) 14.930 + if ( p->ifaces & 1 << i ) 14.931 + { 14.932 + usb_driver_release_interface(&driver, usb_ifnum_to_if(p->dev, i)); 14.933 + clear_bit(i, &p->ifaces); 14.934 + } 14.935 + list_del(&p->list); 14.936 + 14.937 + /* Reset the real device. We don't simulate disconnect / probe for other 14.938 + * drivers in this kernel because we assume the device is completely under 14.939 + * the control of ourselves (i.e. the guest!). This should ensure that the 14.940 + * device is in a sane state for the next customer ;-) */ 14.941 +/* if ( p->dev != NULL) */ 14.942 +/* usb_reset_device(p->dev); */ 14.943 + 14.944 + kfree(p); 14.945 +} 14.946 + 14.947 + 14.948 +/** 14.949 + * usbif_release_port - stop claiming devices on a port on behalf of guest 14.950 + */ 14.951 +void usbif_release_port(usbif_be_release_port_t *msg) 14.952 +{ 14.953 + owned_port_t *p; 14.954 + 14.955 + spin_lock_irq(&owned_ports_lock); 14.956 + p = usbif_find_port(msg->path); 14.957 + __usbif_release_port(p); 14.958 + spin_unlock_irq(&owned_ports_lock); 14.959 +} 14.960 + 14.961 +void usbif_release_ports(usbif_priv_t *up) 14.962 +{ 14.963 + struct list_head *port, *tmp; 14.964 + unsigned long flags; 14.965 + 14.966 + spin_lock_irqsave(&owned_ports_lock, flags); 14.967 + list_for_each_safe(port, tmp, &owned_ports) 14.968 + { 14.969 + owned_port_t *p = list_entry(port, owned_port_t, list); 14.970 + if ( p->usbif_priv == up ) 14.971 + __usbif_release_port(p); 14.972 + } 14.973 + spin_unlock_irqrestore(&owned_ports_lock, flags); 14.974 +} 14.975 + 14.976 +static int __init usbif_init(void) 14.977 +{ 14.978 + int i; 14.979 + 14.980 + if ( !(xen_start_info.flags & SIF_INITDOMAIN) && 14.981 + !(xen_start_info.flags & SIF_USB_BE_DOMAIN) ) 14.982 + return 0; 14.983 + 14.984 + INIT_LIST_HEAD(&owned_ports); 14.985 + 14.986 + usb_register(&driver); 14.987 + 14.988 + usbif_interface_init(); 14.989 + 14.990 + if ( (mmap_vstart = allocate_empty_lowmem_region(MMAP_PAGES)) == 0 ) 14.991 + BUG(); 14.992 + 14.993 + pending_cons = 0; 14.994 + pending_prod = MAX_PENDING_REQS; 14.995 + memset(pending_reqs, 0, sizeof(pending_reqs)); 14.996 + for ( i = 0; i < MAX_PENDING_REQS; i++ ) 14.997 + pending_ring[i] = i; 14.998 + 14.999 + spin_lock_init(&usbio_schedule_list_lock); 14.1000 + INIT_LIST_HEAD(&usbio_schedule_list); 14.1001 + 14.1002 + if ( kernel_thread(usbio_schedule, 0, CLONE_FS | CLONE_FILES) < 0 ) 14.1003 + BUG(); 14.1004 + 14.1005 + usbif_ctrlif_init(); 14.1006 + 14.1007 + spin_lock_init(&owned_ports_lock); 14.1008 + 14.1009 + printk("Xen USB Backend Initialised"); 14.1010 + 14.1011 + return 0; 14.1012 +} 14.1013 + 14.1014 +__initcall(usbif_init);
15.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 15.2 +++ b/linux-2.6.10-xen-sparse/drivers/xen/usbfront/usbfront.c Wed Jan 26 13:09:50 2005 +0000 15.3 @@ -0,0 +1,1720 @@ 15.4 +/* 15.5 + * Xen Virtual USB Frontend Driver 15.6 + * 15.7 + * This file contains the first version of the Xen virtual USB hub 15.8 + * that I've managed not to delete by mistake (3rd time lucky!). 15.9 + * 15.10 + * Based on Linux's uhci.c, original copyright notices are displayed 15.11 + * below. Portions also (c) 2004 Intel Research Cambridge 15.12 + * and (c) 2004 Mark Williamson 15.13 + * 15.14 + * Contact <mark.williamson@cl.cam.ac.uk> or 15.15 + * <xen-devel@lists.sourceforge.net> regarding this code. 15.16 + * 15.17 + * Still to be (maybe) implemented: 15.18 + * - multiple port 15.19 + * - multiple interfaces 15.20 + * - migration / backend restart support? 15.21 + * - unloading support 15.22 + * 15.23 + * Differences to a normal host controller: 15.24 + * - the backend does most of the mucky stuff so we don't have to do various 15.25 + * things that are necessary for a normal host controller (e.g. FSBR). 15.26 + * - we don't have any hardware, so status registers are simulated in software. 15.27 + */ 15.28 + 15.29 +/* 15.30 + * Universal Host Controller Interface driver for USB. 15.31 + * 15.32 + * Maintainer: Johannes Erdfelt <johannes@erdfelt.com> 15.33 + * 15.34 + * (C) Copyright 1999 Linus Torvalds 15.35 + * (C) Copyright 1999-2002 Johannes Erdfelt, johannes@erdfelt.com 15.36 + * (C) Copyright 1999 Randy Dunlap 15.37 + * (C) Copyright 1999 Georg Acher, acher@in.tum.de 15.38 + * (C) Copyright 1999 Deti Fliegl, deti@fliegl.de 15.39 + * (C) Copyright 1999 Thomas Sailer, sailer@ife.ee.ethz.ch 15.40 + * (C) Copyright 1999 Roman Weissgaerber, weissg@vienna.at 15.41 + * (C) Copyright 2000 Yggdrasil Computing, Inc. (port of new PCI interface 15.42 + * support from usb-ohci.c by Adam Richter, adam@yggdrasil.com). 15.43 + * (C) Copyright 1999 Gregory P. Smith (from usb-ohci.c) 15.44 + * 15.45 + * Intel documents this fairly well, and as far as I know there 15.46 + * are no royalties or anything like that, but even so there are 15.47 + * people who decided that they want to do the same thing in a 15.48 + * completely different way. 15.49 + * 15.50 + * WARNING! The USB documentation is downright evil. Most of it 15.51 + * is just crap, written by a committee. You're better off ignoring 15.52 + * most of it, the important stuff is: 15.53 + * - the low-level protocol (fairly simple but lots of small details) 15.54 + * - working around the horridness of the rest 15.55 + */ 15.56 + 15.57 +#include <linux/config.h> 15.58 +#include <linux/module.h> 15.59 +#include <linux/kernel.h> 15.60 +#include <linux/init.h> 15.61 +#include <linux/delay.h> 15.62 +#include <linux/ioport.h> 15.63 +#include <linux/sched.h> 15.64 +#include <linux/slab.h> 15.65 +#include <linux/smp_lock.h> 15.66 +#include <linux/errno.h> 15.67 +#include <linux/unistd.h> 15.68 +#include <linux/interrupt.h> 15.69 +#include <linux/spinlock.h> 15.70 +#ifdef CONFIG_USB_DEBUG 15.71 +#define DEBUG 15.72 +#else 15.73 +#undef DEBUG 15.74 +#endif 15.75 +#include <linux/usb.h> 15.76 + 15.77 +#include <asm/uaccess.h> 15.78 +#include <asm/irq.h> 15.79 +#include <asm/system.h> 15.80 + 15.81 +#include "xhci.h" 15.82 + 15.83 +#include <linux/pm.h> 15.84 + 15.85 +#include "../../../../../drivers/usb/hcd.h" 15.86 + 15.87 +#include <asm-xen/xen-public/io/usbif.h> 15.88 +#include <asm/ctrl_if.h> 15.89 +#include <asm/xen-public/io/domain_controller.h> 15.90 + 15.91 +/* 15.92 + * Version Information 15.93 + */ 15.94 +#define DRIVER_VERSION "v1.0" 15.95 +#define DRIVER_AUTHOR "Linus 'Frodo Rabbit' Torvalds, Johannes Erdfelt, Randy Dunlap, Georg Acher, Deti Fliegl, Thomas Sailer, Roman Weissgaerber, Mark Williamson" 15.96 +#define DRIVER_DESC "Xen Virtual USB Host Controller Interface driver" 15.97 + 15.98 +/* 15.99 + * debug = 0, no debugging messages 15.100 + * debug = 1, dump failed URB's except for stalls 15.101 + * debug = 2, dump all failed URB's (including stalls) 15.102 + */ 15.103 +#ifdef DEBUG 15.104 +static int debug = 1; 15.105 +#else 15.106 +static int debug = 0; 15.107 +#endif 15.108 +MODULE_PARM(debug, "i"); 15.109 +MODULE_PARM_DESC(debug, "Debug level"); 15.110 +static char *errbuf; 15.111 +#define ERRBUF_LEN (PAGE_SIZE * 8) 15.112 + 15.113 +static kmem_cache_t *xhci_up_cachep; /* urb_priv */ 15.114 + 15.115 +static int rh_submit_urb(struct urb *urb); 15.116 +static int rh_unlink_urb(struct urb *urb); 15.117 +//static int xhci_get_current_frame_number(struct usb_device *dev); 15.118 +static int xhci_unlink_urb(struct urb *urb); 15.119 +static void xhci_unlink_generic(struct urb *urb); 15.120 +static void xhci_call_completion(struct urb *urb); 15.121 +static void xhci_drain_ring(void); 15.122 + 15.123 +#define MAX_URB_LOOP 2048 /* Maximum number of linked URB's */ 15.124 + 15.125 +struct xhci *xhci; 15.126 + 15.127 +enum { USBIF_STATE_CONNECTED = 2, 15.128 + USBIF_STATE_DISCONNECTED = 1, 15.129 + USBIF_STATE_CLOSED =0 15.130 +}; 15.131 + 15.132 +static int awaiting_reset = 0; 15.133 + 15.134 +/** 15.135 + * xhci_construct_isoc - add isochronous information to a request 15.136 + */ 15.137 +int xhci_construct_isoc(usbif_request_t *req, struct urb *urb) 15.138 +{ 15.139 + usbif_iso_t *schedule; 15.140 + int i; 15.141 + struct urb_priv *urb_priv = urb->hcpriv; 15.142 + 15.143 + req->num_iso = urb->number_of_packets; 15.144 + schedule = (usbif_iso_t *)__get_free_page(GFP_KERNEL); 15.145 + 15.146 + if ( schedule == NULL ) 15.147 + return -ENOMEM; 15.148 + 15.149 + for ( i = 0; i < req->num_iso; i++ ) 15.150 + { 15.151 + schedule[i].buffer_offset = urb->iso_frame_desc[i].offset; 15.152 + schedule[i].length = urb->iso_frame_desc[i].length; 15.153 + } 15.154 + 15.155 + urb_priv->schedule = schedule; 15.156 + req->iso_schedule = virt_to_machine(schedule); 15.157 + 15.158 + return 0; 15.159 +} 15.160 + 15.161 +#define USBIF_RING_FULL ((xhci->usbif->req_prod - xhci->usb_resp_cons) == USBIF_RING_SIZE) 15.162 + 15.163 +static void dump_urb(struct urb *urb) 15.164 +{ 15.165 + printk("dumping urb @ %p\n", urb); 15.166 + 15.167 + printk("hcpriv = %p\n", urb->hcpriv); 15.168 + printk("next = %p\n", urb->next); 15.169 + printk("dev = %p\n", urb->dev); 15.170 + printk("pipe = 0x%lx\n", urb->pipe); 15.171 + printk("status = %d\n", urb->status); 15.172 + printk("transfer_flags = 0x%lx\n", urb->transfer_flags); 15.173 + printk("transfer_buffer = %p\n", urb->transfer_buffer); 15.174 + printk("transfer_buffer_length = %d\n", urb->transfer_buffer_length); 15.175 + printk("actual_length = %d\n", urb->actual_length); 15.176 + printk("bandwidth = %d\n", urb->bandwidth); 15.177 + printk("setup_packet = %p\n", urb->setup_packet); 15.178 + if ( urb->setup_packet != NULL ) 15.179 + printk("setup = { 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n", 15.180 + urb->setup_packet[0], urb->setup_packet[1], urb->setup_packet[2], urb->setup_packet[3], 15.181 + urb->setup_packet[4], urb->setup_packet[5], urb->setup_packet[6], urb->setup_packet[7]); 15.182 + printk("complete = %p\n", urb->complete); 15.183 + printk("interval = %d\n", urb->interval); 15.184 + 15.185 +} 15.186 + 15.187 + 15.188 +static int 15.189 +xhci_queue_req(struct urb *urb) 15.190 +{ 15.191 + usbif_request_t *req; 15.192 + usbif_t *usbif = xhci->usbif; 15.193 + 15.194 +#if 0 15.195 + printk("usbif = %p, req_prod = %d (@ 0x%lx), resp_prod = %d, resp_cons = %d\n", 15.196 + usbif, usbif->req_prod, virt_to_machine(&usbif->req_prod), 15.197 + usbif->resp_prod, xhci->usb_resp_cons); 15.198 +#endif 15.199 + 15.200 + 15.201 +/* printk("Usbif_priv %p, want IO at 0x%lx\n", urb->hcpriv, virt_to_machine(urb->transfer_buffer)); */ 15.202 + 15.203 + if ( USBIF_RING_FULL ) 15.204 + { 15.205 + printk("xhci_queue_req(): USB ring full, not queuing request\n"); 15.206 + return -ENOBUFS; 15.207 + } 15.208 + 15.209 + /* Stick something in the shared communications ring. */ 15.210 + req = &usbif->ring[MASK_USBIF_IDX(usbif->req_prod)].req; 15.211 + 15.212 + req->operation = USBIF_OP_IO; 15.213 + req->port = 0; /* We don't care what the port is. */ 15.214 + req->id = (unsigned long) urb->hcpriv; 15.215 + req->transfer_buffer = virt_to_machine(urb->transfer_buffer); 15.216 + req->devnum = usb_pipedevice(urb->pipe); 15.217 + req->direction = usb_pipein(urb->pipe); 15.218 + req->speed = usb_pipeslow(urb->pipe); 15.219 + req->pipe_type = usb_pipetype(urb->pipe); 15.220 + req->length = urb->transfer_buffer_length; 15.221 + req->transfer_flags = urb->transfer_flags; 15.222 + req->endpoint = usb_pipeendpoint(urb->pipe); 15.223 + req->speed = usb_pipeslow(urb->pipe); 15.224 + req->timeout = urb->timeout * (1000 / HZ); 15.225 + 15.226 + if ( usb_pipetype(urb->pipe) == 0 ) /* ISO */ 15.227 + { 15.228 + int ret = xhci_construct_isoc(req, urb); 15.229 + if ( ret != 0 ) 15.230 + return ret; 15.231 + } 15.232 + 15.233 + if(urb->setup_packet != NULL) 15.234 + memcpy(req->setup, urb->setup_packet, 8); 15.235 + else 15.236 + memset(req->setup, 0, 8); 15.237 + 15.238 + wmb(); 15.239 + 15.240 + usbif->req_prod++; 15.241 + 15.242 + notify_via_evtchn(xhci->evtchn); 15.243 + 15.244 + // dump_urb(urb); 15.245 + 15.246 + return -EINPROGRESS; 15.247 +} 15.248 + 15.249 +static inline usbif_request_t * 15.250 +xhci_queue_probe(usbif_vdev_t port) 15.251 +{ 15.252 + usbif_request_t *req; 15.253 + usbif_t *usbif = xhci->usbif; 15.254 + 15.255 +#if 0 15.256 + printk("queuing probe: req_prod = %d (@ 0x%lx), resp_prod = %d, resp_cons = %d\n", 15.257 + usbif->req_prod, virt_to_machine(&usbif->req_prod), 15.258 + usbif->resp_prod, xhci->usb_resp_cons); 15.259 +#endif 15.260 + 15.261 + if ( USBIF_RING_FULL ) 15.262 + { 15.263 + printk("xhci_queue_probe(): USB ring full, not queuing request\n"); 15.264 + return NULL; 15.265 + } 15.266 + 15.267 + /* Stick something in the shared communications ring. */ 15.268 + req = &usbif->ring[MASK_USBIF_IDX(usbif->req_prod)].req; 15.269 + 15.270 + req->operation = USBIF_OP_PROBE; 15.271 + req->port = port; 15.272 + req->id = 0; 15.273 + req->transfer_buffer = 0; 15.274 + req->devnum = 0; 15.275 + req->direction = 0; 15.276 + req->speed = 0; 15.277 + req->pipe_type = 0; 15.278 + req->length = 0; 15.279 + req->transfer_flags = 0; 15.280 + req->endpoint = 0; 15.281 + req->speed = 0; 15.282 + 15.283 + wmb(); 15.284 + 15.285 + usbif->req_prod++; 15.286 + 15.287 + notify_via_evtchn(xhci->evtchn); 15.288 + 15.289 + return req; 15.290 +} 15.291 + 15.292 +static int 15.293 +xhci_port_reset(usbif_vdev_t port) 15.294 +{ 15.295 + usbif_request_t *req; 15.296 + usbif_t *usbif = xhci->usbif; 15.297 + 15.298 + /* We only reset one port at a time, so we only need one variable per 15.299 + * hub. */ 15.300 + awaiting_reset = 1; 15.301 + 15.302 + /* Stick something in the shared communications ring. */ 15.303 + req = &usbif->ring[MASK_USBIF_IDX(usbif->req_prod)].req; 15.304 + 15.305 + req->operation = USBIF_OP_RESET; 15.306 + req->port = port; 15.307 + 15.308 + wmb(); 15.309 + 15.310 + usbif->req_prod++; 15.311 + 15.312 + notify_via_evtchn(xhci->evtchn); 15.313 + 15.314 + while ( awaiting_reset > 0 ) 15.315 + { 15.316 + mdelay(1); 15.317 + xhci_drain_ring(); 15.318 + } 15.319 + 15.320 + return awaiting_reset; 15.321 +} 15.322 + 15.323 +static void xhci_show_resp(usbif_response_t *r) 15.324 +{ 15.325 + printk("id=0x%lx, op=0x%x, data=0x%x, status=0x%x, length=0x%lx\n", 15.326 + r->id, r->operation, r->data, r->status, r->length); 15.327 +} 15.328 + 15.329 + 15.330 +/* 15.331 + * Only the USB core should call xhci_alloc_dev and xhci_free_dev 15.332 + */ 15.333 +static int xhci_alloc_dev(struct usb_device *dev) 15.334 +{ 15.335 + return 0; 15.336 +} 15.337 + 15.338 +static int xhci_free_dev(struct usb_device *dev) 15.339 +{ 15.340 + return 0; 15.341 +} 15.342 + 15.343 +static inline void xhci_add_complete(struct urb *urb) 15.344 +{ 15.345 + struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 15.346 + unsigned long flags; 15.347 + 15.348 + spin_lock_irqsave(&xhci->complete_list_lock, flags); 15.349 + list_add_tail(&urbp->complete_list, &xhci->complete_list); 15.350 + spin_unlock_irqrestore(&xhci->complete_list_lock, flags); 15.351 +} 15.352 + 15.353 +/* When this returns, the owner of the URB may free its 15.354 + * storage. 15.355 + * 15.356 + * We spin and wait for the URB to complete before returning. 15.357 + */ 15.358 +static void xhci_delete_urb(struct urb *urb) 15.359 +{ 15.360 + struct urb_priv *urbp; 15.361 + 15.362 + urbp = urb->hcpriv; 15.363 + 15.364 + /* If there's no urb_priv structure for this URB then it can't have 15.365 + * been submitted at all. */ 15.366 + if ( urbp == NULL ) 15.367 + return; 15.368 + 15.369 + /* For now we just spin until the URB completes. It shouldn't take too 15.370 + * long and we don't expect to have to do this very often. */ 15.371 + while ( urb->status == -EINPROGRESS ) 15.372 + { 15.373 + xhci_drain_ring(); 15.374 + mdelay(1); 15.375 + } 15.376 + 15.377 + /* Now we know that further transfers to the buffer won't 15.378 + * occur, so we can safely return. */ 15.379 +} 15.380 + 15.381 +static struct urb_priv *xhci_alloc_urb_priv(struct urb *urb) 15.382 +{ 15.383 + struct urb_priv *urbp; 15.384 + 15.385 + urbp = kmem_cache_alloc(xhci_up_cachep, SLAB_ATOMIC); 15.386 + if (!urbp) { 15.387 + err("xhci_alloc_urb_priv: couldn't allocate memory for urb_priv\n"); 15.388 + return NULL; 15.389 + } 15.390 + 15.391 + memset((void *)urbp, 0, sizeof(*urbp)); 15.392 + 15.393 + urbp->inserttime = jiffies; 15.394 + urbp->urb = urb; 15.395 + urbp->dev = urb->dev; 15.396 + 15.397 + INIT_LIST_HEAD(&urbp->complete_list); 15.398 + 15.399 + urb->hcpriv = urbp; 15.400 + 15.401 + return urbp; 15.402 +} 15.403 + 15.404 +/* 15.405 + * MUST be called with urb->lock acquired 15.406 + */ 15.407 +/* When is this called? Do we need to stop the transfer (as we 15.408 + * currently do)? */ 15.409 +static void xhci_destroy_urb_priv(struct urb *urb) 15.410 +{ 15.411 + struct urb_priv *urbp; 15.412 + 15.413 + urbp = (struct urb_priv *)urb->hcpriv; 15.414 + if (!urbp) 15.415 + return; 15.416 + 15.417 + if (!list_empty(&urb->urb_list)) 15.418 + warn("xhci_destroy_urb_priv: urb %p still on xhci->urb_list or xhci->remove_list", urb); 15.419 + 15.420 + if (!list_empty(&urbp->complete_list)) 15.421 + warn("xhci_destroy_urb_priv: urb %p still on xhci->complete_list", urb); 15.422 + 15.423 + kmem_cache_free(xhci_up_cachep, urb->hcpriv); 15.424 + 15.425 + urb->hcpriv = NULL; 15.426 +} 15.427 + 15.428 +/** 15.429 + * Try to find URBs in progress on the same pipe to the same device. 15.430 + * 15.431 + * MUST be called with xhci->urb_list_lock acquired 15.432 + */ 15.433 +static struct urb *xhci_find_urb_ep(struct xhci *xhci, struct urb *urb) 15.434 +{ 15.435 + struct list_head *tmp, *head; 15.436 + 15.437 + /* We don't match Isoc transfers since they are special */ 15.438 + if (usb_pipeisoc(urb->pipe)) 15.439 + return NULL; 15.440 + 15.441 + head = &xhci->urb_list; 15.442 + tmp = head->next; 15.443 + while (tmp != head) { 15.444 + struct urb *u = list_entry(tmp, struct urb, urb_list); 15.445 + 15.446 + tmp = tmp->next; 15.447 + 15.448 + if (u->dev == urb->dev && u->pipe == urb->pipe && 15.449 + u->status == -EINPROGRESS) 15.450 + return u; 15.451 + } 15.452 + 15.453 + return NULL; 15.454 +} 15.455 + 15.456 +static int xhci_submit_urb(struct urb *urb) 15.457 +{ 15.458 + int ret = -EINVAL; 15.459 + unsigned long flags; 15.460 + struct urb *eurb; 15.461 + int bustime; 15.462 + 15.463 +#if 0 15.464 + printk("submitting urb @ %p for dev @ %p, devnum = %d path %s\n", 15.465 + urb, urb->dev, urb->dev->devnum, urb->dev->devpath); 15.466 +#endif 15.467 + 15.468 + if (!urb) 15.469 + return -EINVAL; 15.470 + 15.471 + if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv) { 15.472 + warn("xhci_submit_urb: urb %p belongs to disconnected device or bus?", urb); 15.473 + return -ENODEV; 15.474 + } 15.475 + 15.476 + if ( urb->dev->devpath == NULL ) 15.477 + { 15.478 + printk("BARF!\n"); 15.479 + BUG(); 15.480 + } 15.481 + 15.482 + 15.483 + 15.484 + usb_inc_dev_use(urb->dev); 15.485 + 15.486 + spin_lock_irqsave(&xhci->urb_list_lock, flags); 15.487 + spin_lock(&urb->lock); 15.488 + 15.489 + if (urb->status == -EINPROGRESS || urb->status == -ECONNRESET || 15.490 + urb->status == -ECONNABORTED) { 15.491 + dbg("xhci_submit_urb: urb not available to submit (status = %d)", urb->status); 15.492 + /* Since we can have problems on the out path */ 15.493 + spin_unlock(&urb->lock); 15.494 + spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 15.495 + usb_dec_dev_use(urb->dev); 15.496 + 15.497 + return ret; 15.498 + } 15.499 + 15.500 + INIT_LIST_HEAD(&urb->urb_list); 15.501 + if (!xhci_alloc_urb_priv(urb)) { 15.502 + ret = -ENOMEM; 15.503 + 15.504 + goto out; 15.505 + } 15.506 + 15.507 + ( (struct urb_priv *)urb->hcpriv )->in_progress = 1; 15.508 + 15.509 + eurb = xhci_find_urb_ep(xhci, urb); 15.510 + if (eurb && !(urb->transfer_flags & USB_QUEUE_BULK)) { 15.511 + ret = -ENXIO; 15.512 + 15.513 + goto out; 15.514 + } 15.515 + 15.516 + /* Short circuit the virtual root hub */ 15.517 + if (urb->dev == xhci->rh.dev) { 15.518 + ret = rh_submit_urb(urb); 15.519 + 15.520 + goto out; 15.521 + } 15.522 + 15.523 + if ( usb_pipedevice(urb->pipe) == 1 ) 15.524 + printk("dev = %p, dev->path = %s, rh.dev = %p, rh.dev.devnum = %d rh.dev->path = %s!\n", 15.525 + urb->dev, urb->dev->devpath, xhci->rh.dev, xhci->rh.dev->devnum, xhci->rh.dev->devpath); 15.526 + 15.527 + switch (usb_pipetype(urb->pipe)) { 15.528 + case PIPE_CONTROL: 15.529 + ret = xhci_queue_req(urb); 15.530 + break; 15.531 + case PIPE_INTERRUPT: 15.532 + if (urb->bandwidth == 0) { /* not yet checked/allocated */ 15.533 + bustime = usb_check_bandwidth(urb->dev, urb); 15.534 + if (bustime < 0) 15.535 + ret = bustime; 15.536 + else { 15.537 + ret = xhci_queue_req(urb); 15.538 + if (ret == -EINPROGRESS) 15.539 + usb_claim_bandwidth(urb->dev, urb, bustime, 0); 15.540 + } 15.541 + } else /* bandwidth is already set */ 15.542 + ret = xhci_queue_req(urb); 15.543 + break; 15.544 + case PIPE_BULK: 15.545 + ret = xhci_queue_req(urb); 15.546 + break; 15.547 + case PIPE_ISOCHRONOUS: 15.548 + if (urb->bandwidth == 0) { /* not yet checked/allocated */ 15.549 + if (urb->number_of_packets <= 0) { 15.550 + ret = -EINVAL; 15.551 + break; 15.552 + } 15.553 + bustime = usb_check_bandwidth(urb->dev, urb); 15.554 + if (bustime < 0) { 15.555 + ret = bustime; 15.556 + break; 15.557 + } 15.558 + 15.559 + ret = xhci_queue_req(urb); 15.560 + if (ret == -EINPROGRESS) 15.561 + usb_claim_bandwidth(urb->dev, urb, bustime, 1); 15.562 + } else /* bandwidth is already set */ 15.563 + ret = xhci_queue_req(urb); 15.564 + break; 15.565 + } 15.566 + 15.567 +out: 15.568 + urb->status = ret; 15.569 + 15.570 + if (ret == -EINPROGRESS) { 15.571 + /* We use _tail to make find_urb_ep more efficient */ 15.572 + list_add_tail(&urb->urb_list, &xhci->urb_list); 15.573 + 15.574 + spin_unlock(&urb->lock); 15.575 + spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 15.576 + 15.577 + return 0; 15.578 + } 15.579 + 15.580 + xhci_unlink_generic(urb); 15.581 + 15.582 + spin_unlock(&urb->lock); 15.583 + spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 15.584 + 15.585 + /* Only call completion if it was successful */ 15.586 + if (!ret) 15.587 + xhci_call_completion(urb); 15.588 + 15.589 + return ret; 15.590 +} 15.591 + 15.592 +/* 15.593 + * Return the result of a transfer 15.594 + * 15.595 + * MUST be called with urb_list_lock acquired 15.596 + */ 15.597 +static void xhci_transfer_result(struct xhci *xhci, struct urb *urb) 15.598 +{ 15.599 + int ret = 0; 15.600 + unsigned long flags; 15.601 + struct urb_priv *urbp; 15.602 + 15.603 + /* The root hub is special */ 15.604 + if (urb->dev == xhci->rh.dev) 15.605 + return; 15.606 + 15.607 + spin_lock_irqsave(&urb->lock, flags); 15.608 + 15.609 + urbp = (struct urb_priv *)urb->hcpriv; 15.610 + 15.611 + if ( ( (struct urb_priv *)urb->hcpriv )->in_progress ) 15.612 + ret = -EINPROGRESS; 15.613 + 15.614 + if (urb->actual_length < urb->transfer_buffer_length) { 15.615 + if (urb->transfer_flags & USB_DISABLE_SPD) { 15.616 + ret = -EREMOTEIO; 15.617 + } 15.618 + } 15.619 + 15.620 + if (urb->status == -EPIPE) 15.621 + { 15.622 + ret = urb->status; 15.623 + /* endpoint has stalled - mark it halted */ 15.624 + usb_endpoint_halt(urb->dev, usb_pipeendpoint(urb->pipe), 15.625 + usb_pipeout(urb->pipe)); 15.626 + } 15.627 + 15.628 + if ((debug == 1 && ret != 0 && ret != -EPIPE) || 15.629 + (ret != 0 && debug > 1)) { 15.630 + /* Some debugging code */ 15.631 + dbg("xhci_result_interrupt/bulk() failed with status %x", 15.632 + status); 15.633 + } 15.634 + 15.635 + if (ret == -EINPROGRESS) 15.636 + goto out; 15.637 + 15.638 + switch (usb_pipetype(urb->pipe)) { 15.639 + case PIPE_CONTROL: 15.640 + case PIPE_BULK: 15.641 + case PIPE_ISOCHRONOUS: 15.642 + /* Release bandwidth for Interrupt or Isoc. transfers */ 15.643 + /* Spinlock needed ? */ 15.644 + if (urb->bandwidth) 15.645 + usb_release_bandwidth(urb->dev, urb, 1); 15.646 + xhci_unlink_generic(urb); 15.647 + break; 15.648 + case PIPE_INTERRUPT: 15.649 + /* Interrupts are an exception */ 15.650 + if (urb->interval) 15.651 + goto out_complete; 15.652 + 15.653 + /* Release bandwidth for Interrupt or Isoc. transfers */ 15.654 + /* Spinlock needed ? */ 15.655 + if (urb->bandwidth) 15.656 + usb_release_bandwidth(urb->dev, urb, 0); 15.657 + xhci_unlink_generic(urb); 15.658 + break; 15.659 + default: 15.660 + info("xhci_transfer_result: unknown pipe type %d for urb %p\n", 15.661 + usb_pipetype(urb->pipe), urb); 15.662 + } 15.663 + 15.664 + /* Remove it from xhci->urb_list */ 15.665 + list_del_init(&urb->urb_list); 15.666 + 15.667 +out_complete: 15.668 + xhci_add_complete(urb); 15.669 + 15.670 +out: 15.671 + spin_unlock_irqrestore(&urb->lock, flags); 15.672 +} 15.673 + 15.674 +/* 15.675 + * MUST be called with urb->lock acquired 15.676 + */ 15.677 +static void xhci_unlink_generic(struct urb *urb) 15.678 +{ 15.679 + struct urb_priv *urbp = urb->hcpriv; 15.680 + 15.681 + /* We can get called when urbp allocation fails, so check */ 15.682 + if (!urbp) 15.683 + return; 15.684 + 15.685 + /* ??? This function is now so minimal it doesn't do much. Do we really 15.686 + * need it? */ 15.687 + 15.688 + xhci_delete_urb(urb); 15.689 +} 15.690 + 15.691 +static int xhci_unlink_urb(struct urb *urb) 15.692 +{ 15.693 + unsigned long flags; 15.694 + struct urb_priv *urbp = urb->hcpriv; 15.695 + 15.696 + if (!urb) 15.697 + return -EINVAL; 15.698 + 15.699 + if (!urb->dev || !urb->dev->bus || !urb->dev->bus->hcpriv) 15.700 + return -ENODEV; 15.701 + 15.702 + spin_lock_irqsave(&xhci->urb_list_lock, flags); 15.703 + spin_lock(&urb->lock); 15.704 + 15.705 + /* Release bandwidth for Interrupt or Isoc. transfers */ 15.706 + /* Spinlock needed ? */ 15.707 + if (urb->bandwidth) { 15.708 + switch (usb_pipetype(urb->pipe)) { 15.709 + case PIPE_INTERRUPT: 15.710 + usb_release_bandwidth(urb->dev, urb, 0); 15.711 + break; 15.712 + case PIPE_ISOCHRONOUS: 15.713 + usb_release_bandwidth(urb->dev, urb, 1); 15.714 + break; 15.715 + default: 15.716 + break; 15.717 + } 15.718 + } 15.719 + 15.720 + if (urb->status != -EINPROGRESS) { 15.721 + spin_unlock(&urb->lock); 15.722 + spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 15.723 + return 0; 15.724 + } 15.725 + 15.726 + list_del_init(&urb->urb_list); 15.727 + 15.728 + xhci_unlink_generic(urb); 15.729 + 15.730 + /* Short circuit the virtual root hub */ 15.731 + if (urb->dev == xhci->rh.dev) { 15.732 + rh_unlink_urb(urb); 15.733 + 15.734 + spin_unlock(&urb->lock); 15.735 + spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 15.736 + 15.737 + xhci_call_completion(urb); 15.738 + } else { 15.739 + if (urb->transfer_flags & USB_ASYNC_UNLINK) { 15.740 + urbp->status = urb->status = -ECONNABORTED; 15.741 + 15.742 + spin_lock(&xhci->urb_remove_list_lock); 15.743 + 15.744 + list_add(&urb->urb_list, &xhci->urb_remove_list); 15.745 + 15.746 + spin_unlock(&xhci->urb_remove_list_lock); 15.747 + 15.748 + spin_unlock(&urb->lock); 15.749 + spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 15.750 + 15.751 + } else { 15.752 + urb->status = -ENOENT; 15.753 + 15.754 + spin_unlock(&urb->lock); 15.755 + spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 15.756 + 15.757 + if (in_interrupt()) { /* wait at least 1 frame */ 15.758 + static int errorcount = 10; 15.759 + 15.760 + if (errorcount--) 15.761 + dbg("xhci_unlink_urb called from interrupt for urb %p", urb); 15.762 + udelay(1000); 15.763 + } else 15.764 + schedule_timeout(1+1*HZ/1000); 15.765 + 15.766 + xhci_call_completion(urb); 15.767 + } 15.768 + } 15.769 + 15.770 + return 0; 15.771 +} 15.772 + 15.773 + 15.774 +struct usb_operations xhci_device_operations = { 15.775 + .allocate = xhci_alloc_dev, 15.776 + .deallocate = xhci_free_dev, 15.777 + /* It doesn't look like any drivers actually care what the frame number 15.778 + * is at the moment! If necessary, we could approximate the current 15.779 + * frame nubmer by passing it from the backend in response messages. */ 15.780 + .get_frame_number = NULL, 15.781 + .submit_urb = xhci_submit_urb, 15.782 + .unlink_urb = xhci_unlink_urb 15.783 +}; 15.784 + 15.785 +/* Virtual Root Hub */ 15.786 + 15.787 +static __u8 root_hub_dev_des[] = 15.788 +{ 15.789 + 0x12, /* __u8 bLength; */ 15.790 + 0x01, /* __u8 bDescriptorType; Device */ 15.791 + 0x00, /* __u16 bcdUSB; v1.0 */ 15.792 + 0x01, 15.793 + 0x09, /* __u8 bDeviceClass; HUB_CLASSCODE */ 15.794 + 0x00, /* __u8 bDeviceSubClass; */ 15.795 + 0x00, /* __u8 bDeviceProtocol; */ 15.796 + 0x08, /* __u8 bMaxPacketSize0; 8 Bytes */ 15.797 + 0x00, /* __u16 idVendor; */ 15.798 + 0x00, 15.799 + 0x00, /* __u16 idProduct; */ 15.800 + 0x00, 15.801 + 0x00, /* __u16 bcdDevice; */ 15.802 + 0x00, 15.803 + 0x00, /* __u8 iManufacturer; */ 15.804 + 0x02, /* __u8 iProduct; */ 15.805 + 0x01, /* __u8 iSerialNumber; */ 15.806 + 0x01 /* __u8 bNumConfigurations; */ 15.807 +}; 15.808 + 15.809 + 15.810 +/* Configuration descriptor */ 15.811 +static __u8 root_hub_config_des[] = 15.812 +{ 15.813 + 0x09, /* __u8 bLength; */ 15.814 + 0x02, /* __u8 bDescriptorType; Configuration */ 15.815 + 0x19, /* __u16 wTotalLength; */ 15.816 + 0x00, 15.817 + 0x01, /* __u8 bNumInterfaces; */ 15.818 + 0x01, /* __u8 bConfigurationValue; */ 15.819 + 0x00, /* __u8 iConfiguration; */ 15.820 + 0x40, /* __u8 bmAttributes; 15.821 + Bit 7: Bus-powered, 6: Self-powered, 15.822 + Bit 5 Remote-wakeup, 4..0: resvd */ 15.823 + 0x00, /* __u8 MaxPower; */ 15.824 + 15.825 + /* interface */ 15.826 + 0x09, /* __u8 if_bLength; */ 15.827 + 0x04, /* __u8 if_bDescriptorType; Interface */ 15.828 + 0x00, /* __u8 if_bInterfaceNumber; */ 15.829 + 0x00, /* __u8 if_bAlternateSetting; */ 15.830 + 0x01, /* __u8 if_bNumEndpoints; */ 15.831 + 0x09, /* __u8 if_bInterfaceClass; HUB_CLASSCODE */ 15.832 + 0x00, /* __u8 if_bInterfaceSubClass; */ 15.833 + 0x00, /* __u8 if_bInterfaceProtocol; */ 15.834 + 0x00, /* __u8 if_iInterface; */ 15.835 + 15.836 + /* endpoint */ 15.837 + 0x07, /* __u8 ep_bLength; */ 15.838 + 0x05, /* __u8 ep_bDescriptorType; Endpoint */ 15.839 + 0x81, /* __u8 ep_bEndpointAddress; IN Endpoint 1 */ 15.840 + 0x03, /* __u8 ep_bmAttributes; Interrupt */ 15.841 + 0x08, /* __u16 ep_wMaxPacketSize; 8 Bytes */ 15.842 + 0x00, 15.843 + 0xff /* __u8 ep_bInterval; 255 ms */ 15.844 +}; 15.845 + 15.846 +static __u8 root_hub_hub_des[] = 15.847 +{ 15.848 + 0x09, /* __u8 bLength; */ 15.849 + 0x29, /* __u8 bDescriptorType; Hub-descriptor */ 15.850 + 0x02, /* __u8 bNbrPorts; */ 15.851 + 0x00, /* __u16 wHubCharacteristics; */ 15.852 + 0x00, 15.853 + 0x01, /* __u8 bPwrOn2pwrGood; 2ms */ 15.854 + 0x00, /* __u8 bHubContrCurrent; 0 mA */ 15.855 + 0x00, /* __u8 DeviceRemovable; *** 7 Ports max *** */ 15.856 + 0xff /* __u8 PortPwrCtrlMask; *** 7 ports max *** */ 15.857 +}; 15.858 + 15.859 +/* prepare Interrupt pipe transaction data; HUB INTERRUPT ENDPOINT */ 15.860 +static int rh_send_irq(struct urb *urb) 15.861 +{ 15.862 + struct urb_priv *urbp = (struct urb_priv *)urb->hcpriv; 15.863 + xhci_port_t *ports = xhci->rh.ports; 15.864 + unsigned long flags; 15.865 + int i, len = 1; 15.866 + __u16 data = 0; 15.867 + 15.868 + spin_lock_irqsave(&urb->lock, flags); 15.869 + for (i = 0; i < xhci->rh.numports; i++) { 15.870 + /* MAW: No idea what the old code was doing here or why it worked. 15.871 + * This implementation sets a bit if anything at all has changed on the 15.872 + * port, as per USB spec 11.12 */ 15.873 + data |= (ports[i].cs_chg || ports[i].pe_chg ) 15.874 + ? (1 << (i + 1)) 15.875 + : 0; 15.876 + 15.877 + len = (i + 1) / 8 + 1; 15.878 + } 15.879 + 15.880 + *(__u16 *) urb->transfer_buffer = cpu_to_le16(data); 15.881 + urb->actual_length = len; 15.882 + urbp->status = 0; 15.883 + 15.884 + spin_unlock_irqrestore(&urb->lock, flags); 15.885 + 15.886 + if ((data > 0) && (xhci->rh.send != 0)) { 15.887 + dbg("root-hub INT complete: data: %x", data); 15.888 + xhci_call_completion(urb); 15.889 + } 15.890 + 15.891 + return 0; 15.892 +} 15.893 + 15.894 +/* Virtual Root Hub INTs are polled by this timer every "interval" ms */ 15.895 +static int rh_init_int_timer(struct urb *urb); 15.896 + 15.897 +static void rh_int_timer_do(unsigned long ptr) 15.898 +{ 15.899 + struct urb *urb = (struct urb *)ptr; 15.900 + struct list_head list, *tmp, *head; 15.901 + unsigned long flags; 15.902 + int i; 15.903 + 15.904 + for ( i = 0; i < xhci->rh.numports; i++) 15.905 + xhci_queue_probe(i); 15.906 + 15.907 + if (xhci->rh.send) 15.908 + rh_send_irq(urb); 15.909 + 15.910 + INIT_LIST_HEAD(&list); 15.911 + 15.912 + spin_lock_irqsave(&xhci->urb_list_lock, flags); 15.913 + head = &xhci->urb_list; 15.914 + tmp = head->next; 15.915 + while (tmp != head) { 15.916 + struct urb *u = list_entry(tmp, struct urb, urb_list); 15.917 + struct urb_priv *up = (struct urb_priv *)u->hcpriv; 15.918 + 15.919 + tmp = tmp->next; 15.920 + 15.921 + spin_lock(&u->lock); 15.922 + 15.923 + /* Check if the URB timed out */ 15.924 + if (u->timeout && time_after_eq(jiffies, up->inserttime + u->timeout)) { 15.925 + list_del(&u->urb_list); 15.926 + list_add_tail(&u->urb_list, &list); 15.927 + } 15.928 + 15.929 + spin_unlock(&u->lock); 15.930 + } 15.931 + spin_unlock_irqrestore(&xhci->urb_list_lock, flags); 15.932 + 15.933 + head = &list; 15.934 + tmp = head->next; 15.935 + while (tmp != head) { 15.936 + struct urb *u = list_entry(tmp, struct urb, urb_list); 15.937 + 15.938 + tmp = tmp->next; 15.939 + 15.940 + u->transfer_flags |= USB_ASYNC_UNLINK | USB_TIMEOUT_KILLED; 15.941 + xhci_unlink_urb(u); 15.942 + } 15.943 + 15.944 + rh_init_int_timer(urb); 15.945 +} 15.946 + 15.947 +/* Root Hub INTs are polled by this timer */ 15.948 +static int rh_init_int_timer(struct urb *urb) 15.949 +{ 15.950 + xhci->rh.interval = urb->interval; 15.951 + init_timer(&xhci->rh.rh_int_timer); 15.952 + xhci->rh.rh_int_timer.function = rh_int_timer_do; 15.953 + xhci->rh.rh_int_timer.data = (unsigned long)urb; 15.954 + xhci->rh.rh_int_timer.expires = jiffies + (HZ * (urb->interval < 30 ? 30 : urb->interval)) / 1000; 15.955 + add_timer(&xhci->rh.rh_int_timer); 15.956 + 15.957 + return 0; 15.958 +} 15.959 + 15.960 +#define OK(x) len = (x); break 15.961 + 15.962 +/* Root Hub Control Pipe */ 15.963 +static int rh_submit_urb(struct urb *urb) 15.964 +{ 15.965 + unsigned int pipe = urb->pipe; 15.966 + struct usb_ctrlrequest *cmd = (struct usb_ctrlrequest *)urb->setup_packet; 15.967 + void *data = urb->transfer_buffer; 15.968 + int leni = urb->transfer_buffer_length; 15.969 + int len = 0; 15.970 + xhci_port_t *status; 15.971 + int stat = 0; 15.972 + int i; 15.973 + int retstatus; 15.974 + unsigned long flags; 15.975 + 15.976 + __u16 cstatus; 15.977 + __u16 bmRType_bReq; 15.978 + __u16 wValue; 15.979 + __u16 wIndex; 15.980 + __u16 wLength; 15.981 + 15.982 + if (usb_pipetype(pipe) == PIPE_INTERRUPT) { 15.983 + xhci->rh.urb = urb; 15.984 + xhci->rh.send = 1; 15.985 + xhci->rh.interval = urb->interval; 15.986 + rh_init_int_timer(urb); 15.987 + 15.988 + return -EINPROGRESS; 15.989 + } 15.990 + 15.991 + bmRType_bReq = cmd->bRequestType | cmd->bRequest << 8; 15.992 + wValue = le16_to_cpu(cmd->wValue); 15.993 + wIndex = le16_to_cpu(cmd->wIndex); 15.994 + wLength = le16_to_cpu(cmd->wLength); 15.995 + 15.996 + for (i = 0; i < 8; i++) 15.997 + xhci->rh.c_p_r[i] = 0; 15.998 + 15.999 + status = &xhci->rh.ports[wIndex - 1]; 15.1000 + 15.1001 + spin_lock_irqsave(&xhci->rh.port_state_lock, flags); 15.1002 + 15.1003 + switch (bmRType_bReq) { 15.1004 + /* Request Destination: 15.1005 + without flags: Device, 15.1006 + RH_INTERFACE: interface, 15.1007 + RH_ENDPOINT: endpoint, 15.1008 + RH_CLASS means HUB here, 15.1009 + RH_OTHER | RH_CLASS almost ever means HUB_PORT here 15.1010 + */ 15.1011 + 15.1012 + case RH_GET_STATUS: 15.1013 + *(__u16 *)data = cpu_to_le16(1); 15.1014 + OK(2); 15.1015 + case RH_GET_STATUS | RH_INTERFACE: 15.1016 + *(__u16 *)data = cpu_to_le16(0); 15.1017 + OK(2); 15.1018 + case RH_GET_STATUS | RH_ENDPOINT: 15.1019 + *(__u16 *)data = cpu_to_le16(0); 15.1020 + OK(2); 15.1021 + case RH_GET_STATUS | RH_CLASS: 15.1022 + *(__u32 *)data = cpu_to_le32(0); 15.1023 + OK(4); /* hub power */ 15.1024 + case RH_GET_STATUS | RH_OTHER | RH_CLASS: 15.1025 + cstatus = (status->cs_chg) | 15.1026 + (status->pe_chg << 1) | 15.1027 + (xhci->rh.c_p_r[wIndex - 1] << 4); 15.1028 + retstatus = (status->ccs) | 15.1029 + (status->pe << 1) | 15.1030 + (status->susp << 2) | 15.1031 + (status->pr << 8) | 15.1032 + (1 << 8) | /* power on */ 15.1033 + (status->lsda << 9); 15.1034 + *(__u16 *)data = cpu_to_le16(retstatus); 15.1035 + *(__u16 *)(data + 2) = cpu_to_le16(cstatus); 15.1036 + OK(4); 15.1037 + case RH_CLEAR_FEATURE | RH_ENDPOINT: 15.1038 + switch (wValue) { 15.1039 + case RH_ENDPOINT_STALL: 15.1040 + OK(0); 15.1041 + } 15.1042 + break; 15.1043 + case RH_CLEAR_FEATURE | RH_CLASS: 15.1044 + switch (wValue) { 15.1045 + case RH_C_HUB_OVER_CURRENT: 15.1046 + OK(0); /* hub power over current */ 15.1047 + } 15.1048 + break; 15.1049 + case RH_CLEAR_FEATURE | RH_OTHER | RH_CLASS: 15.1050 + switch (wValue) { 15.1051 + case RH_PORT_ENABLE: 15.1052 + status->pe = 0; 15.1053 + OK(0); 15.1054 + case RH_PORT_SUSPEND: 15.1055 + status->susp = 0; 15.1056 + OK(0); 15.1057 + case RH_PORT_POWER: 15.1058 + OK(0); /* port power */ 15.1059 + case RH_C_PORT_CONNECTION: 15.1060 + status->cs_chg = 0; 15.1061 + OK(0); 15.1062 + case RH_C_PORT_ENABLE: 15.1063 + status->pe_chg = 0; 15.1064 + OK(0); 15.1065 + case RH_C_PORT_SUSPEND: 15.1066 + /*** WR_RH_PORTSTAT(RH_PS_PSSC); */ 15.1067 + OK(0); 15.1068 + case RH_C_PORT_OVER_CURRENT: 15.1069 + OK(0); /* port power over current */ 15.1070 + case RH_C_PORT_RESET: 15.1071 + xhci->rh.c_p_r[wIndex - 1] = 0; 15.1072 + OK(0); 15.1073 + } 15.1074 + break; 15.1075 + case RH_SET_FEATURE | RH_OTHER | RH_CLASS: 15.1076 + switch (wValue) { 15.1077 + case RH_PORT_SUSPEND: 15.1078 + status->susp = 1; 15.1079 + OK(0); 15.1080 + case RH_PORT_RESET: 15.1081 + { 15.1082 + int ret; 15.1083 + xhci->rh.c_p_r[wIndex - 1] = 1; 15.1084 + status->pr = 0; 15.1085 + status->pe = 1; 15.1086 + ret = xhci_port_reset(wIndex - 1); 15.1087 + /* XXX MAW: should probably cancel queued transfers during reset... *\/ */ 15.1088 + if ( ret == 0 ) { OK(0); } 15.1089 + else { return ret; } 15.1090 + } 15.1091 + break; 15.1092 + case RH_PORT_POWER: 15.1093 + OK(0); /* port power ** */ 15.1094 + case RH_PORT_ENABLE: 15.1095 + status->pe = 1; 15.1096 + OK(0); 15.1097 + } 15.1098 + break; 15.1099 + case RH_SET_ADDRESS: 15.1100 + printk("setting root hub device to %d\n", wValue); 15.1101 + xhci->rh.devnum = wValue; 15.1102 + OK(0); 15.1103 + case RH_GET_DESCRIPTOR: 15.1104 + switch ((wValue & 0xff00) >> 8) { 15.1105 + case 0x01: /* device descriptor */ 15.1106 + len = min_t(unsigned int, leni, 15.1107 + min_t(unsigned int, 15.1108 + sizeof(root_hub_dev_des), wLength)); 15.1109 + memcpy(data, root_hub_dev_des, len); 15.1110 + OK(len); 15.1111 + case 0x02: /* configuration descriptor */ 15.1112 + len = min_t(unsigned int, leni, 15.1113 + min_t(unsigned int, 15.1114 + sizeof(root_hub_config_des), wLength)); 15.1115 + memcpy (data, root_hub_config_des, len); 15.1116 + OK(len); 15.1117 + case 0x03: /* string descriptors */ 15.1118 + len = usb_root_hub_string (wValue & 0xff, 15.1119 + 0, "XHCI-alt", 15.1120 + data, wLength); 15.1121 + if (len > 0) { 15.1122 + OK(min_t(int, leni, len)); 15.1123 + } else 15.1124 + stat = -EPIPE; 15.1125 + } 15.1126 + break; 15.1127 + case RH_GET_DESCRIPTOR | RH_CLASS: 15.1128 + root_hub_hub_des[2] = xhci->rh.numports; 15.1129 + len = min_t(unsigned int, leni, 15.1130 + min_t(unsigned int, sizeof(root_hub_hub_des), wLength)); 15.1131 + memcpy(data, root_hub_hub_des, len); 15.1132 + OK(len); 15.1133 + case RH_GET_CONFIGURATION: 15.1134 + *(__u8 *)data = 0x01; 15.1135 + OK(1); 15.1136 + case RH_SET_CONFIGURATION: 15.1137 + OK(0); 15.1138 + case RH_GET_INTERFACE | RH_INTERFACE: 15.1139 + *(__u8 *)data = 0x00; 15.1140 + OK(1); 15.1141 + case RH_SET_INTERFACE | RH_INTERFACE: 15.1142 + OK(0); 15.1143 + default: 15.1144 + stat = -EPIPE; 15.1145 + } 15.1146 + 15.1147 + spin_unlock_irqrestore(&xhci->rh.port_state_lock, flags); 15.1148 + 15.1149 + urb->actual_length = len; 15.1150 + 15.1151 + return stat; 15.1152 +} 15.1153 + 15.1154 +/* 15.1155 + * MUST be called with urb->lock acquired 15.1156 + */ 15.1157 +static int rh_unlink_urb(struct urb *urb) 15.1158 +{ 15.1159 + if (xhci->rh.urb == urb) { 15.1160 + urb->status = -ENOENT; 15.1161 + xhci->rh.send = 0; 15.1162 + xhci->rh.urb = NULL; 15.1163 + del_timer(&xhci->rh.rh_int_timer); 15.1164 + } 15.1165 + return 0; 15.1166 +} 15.1167 + 15.1168 +static void xhci_call_completion(struct urb *urb) 15.1169 +{ 15.1170 + struct urb_priv *urbp; 15.1171 + struct usb_device *dev = urb->dev; 15.1172 + int is_ring = 0, killed, resubmit_interrupt, status; 15.1173 + struct urb *nurb; 15.1174 + unsigned long flags; 15.1175 + 15.1176 + spin_lock_irqsave(&urb->lock, flags); 15.1177 + 15.1178 + urbp = (struct urb_priv *)urb->hcpriv; 15.1179 + if (!urbp || !urb->dev) { 15.1180 + spin_unlock_irqrestore(&urb->lock, flags); 15.1181 + return; 15.1182 + } 15.1183 + 15.1184 + killed = (urb->status == -ENOENT || urb->status == -ECONNABORTED || 15.1185 + urb->status == -ECONNRESET); 15.1186 + resubmit_interrupt = (usb_pipetype(urb->pipe) == PIPE_INTERRUPT && 15.1187 + urb->interval); 15.1188 + 15.1189 + nurb = urb->next; 15.1190 + if (nurb && !killed) { 15.1191 + int count = 0; 15.1192 + 15.1193 + while (nurb && nurb != urb && count < MAX_URB_LOOP) { 15.1194 + if (nurb->status == -ENOENT || 15.1195 + nurb->status == -ECONNABORTED || 15.1196 + nurb->status == -ECONNRESET) { 15.1197 + killed = 1; 15.1198 + break; 15.1199 + } 15.1200 + 15.1201 + nurb = nurb->next; 15.1202 + count++; 15.1203 + } 15.1204 + 15.1205 + if (count == MAX_URB_LOOP) 15.1206 + err("xhci_call_completion: too many linked URB's, loop? (first loop)"); 15.1207 + 15.1208 + /* Check to see if chain is a ring */ 15.1209 + is_ring = (nurb == urb); 15.1210 + } 15.1211 + 15.1212 + status = urbp->status; 15.1213 + if (!resubmit_interrupt || killed) 15.1214 + /* We don't need urb_priv anymore */ 15.1215 + xhci_destroy_urb_priv(urb); 15.1216 + 15.1217 + if (!killed) 15.1218 + urb->status = status; 15.1219 + 15.1220 + spin_unlock_irqrestore(&urb->lock, flags); 15.1221 + 15.1222 + if (urb->complete) 15.1223 + urb->complete(urb); 15.1224 + 15.1225 + if (resubmit_interrupt) 15.1226 + /* Recheck the status. The completion handler may have */ 15.1227 + /* unlinked the resubmitting interrupt URB */ 15.1228 + killed = (urb->status == -ENOENT || 15.1229 + urb->status == -ECONNABORTED || 15.1230 + urb->status == -ECONNRESET); 15.1231 + 15.1232 + if (resubmit_interrupt && !killed) { 15.1233 + if ( urb->dev != xhci->rh.dev ) 15.1234 + xhci_queue_req(urb); /* XXX What if this fails? */ 15.1235 + /* Don't need to resubmit URBs for the virtual root dev. */ 15.1236 + } else { 15.1237 + if (is_ring && !killed) { 15.1238 + urb->dev = dev; 15.1239 + xhci_submit_urb(urb); 15.1240 + } else { 15.1241 + /* We decrement the usage count after we're done */ 15.1242 + /* with everything */ 15.1243 + usb_dec_dev_use(dev); 15.1244 + } 15.1245 + } 15.1246 +} 15.1247 + 15.1248 +static void xhci_finish_completion(void) 15.1249 +{ 15.1250 + struct list_head *tmp, *head; 15.1251 + unsigned long flags; 15.1252 + 15.1253 + spin_lock_irqsave(&xhci->complete_list_lock, flags); 15.1254 + head = &xhci->complete_list; 15.1255 + tmp = head->next; 15.1256 + while (tmp != head) { 15.1257 + struct urb_priv *urbp = list_entry(tmp, struct urb_priv, complete_list); 15.1258 + struct urb *urb = urbp->urb; 15.1259 + 15.1260 + list_del_init(&urbp->complete_list); 15.1261 + spin_unlock_irqrestore(&xhci->complete_list_lock, flags); 15.1262 + 15.1263 + xhci_call_completion(urb); 15.1264 + 15.1265 + spin_lock_irqsave(&xhci->complete_list_lock, flags); 15.1266 + head = &xhci->complete_list; 15.1267 + tmp = head->next; 15.1268 + } 15.1269 + spin_unlock_irqrestore(&xhci->complete_list_lock, flags); 15.1270 +} 15.1271 + 15.1272 +void receive_usb_reset(usbif_response_t *resp) 15.1273 +{ 15.1274 + awaiting_reset = resp->status; 15.1275 + rmb(); 15.1276 + 15.1277 +} 15.1278 + 15.1279 +void receive_usb_probe(usbif_response_t *resp) 15.1280 +{ 15.1281 + spin_lock(&xhci->rh.port_state_lock); 15.1282 + 15.1283 + if ( resp->status > 0 ) 15.1284 + { 15.1285 + if ( resp->status == 1 ) 15.1286 + { 15.1287 +/* printk("hey hey, there's a device on port %d\n", resp->data); */ 15.1288 + 15.1289 + /* If theres a device there and there wasn't one before there must 15.1290 + * have been a connection status change. */ 15.1291 + if( xhci->rh.ports[resp->data].cs == 0 ) 15.1292 + { 15.1293 + xhci->rh.ports[resp->data].cs = 1; 15.1294 + xhci->rh.ports[resp->data].ccs = 1; 15.1295 + xhci->rh.ports[resp->data].cs_chg = 1; 15.1296 +/* printk("Look at device on port %d that wasn't there before\n", resp->data); */ 15.1297 + } 15.1298 + } 15.1299 + else 15.1300 + printk("receive_usb_probe(): unexpected status %d for port %d\n", 15.1301 + resp->status, resp->data); 15.1302 + } 15.1303 + else if ( resp->status < 0) 15.1304 + printk("receive_usb_probe(): got error status %d\n", resp->status); 15.1305 + 15.1306 + spin_unlock(&xhci->rh.port_state_lock); 15.1307 +} 15.1308 + 15.1309 +void receive_usb_io(usbif_response_t *resp) 15.1310 +{ 15.1311 + struct urb_priv *urbp = (struct urb_priv *)resp->id; 15.1312 + struct urb *urb = urbp->urb; 15.1313 + 15.1314 + urb->actual_length = resp->length; 15.1315 + urb->status = resp->status; 15.1316 + urbp->status = resp->status; 15.1317 + urbp->in_progress = 0; 15.1318 + 15.1319 + if( usb_pipetype(urb->pipe) == 0 ) /* ISO */ 15.1320 + { 15.1321 + int i; 15.1322 + 15.1323 + /* Copy ISO schedule results back in. */ 15.1324 + 15.1325 + for ( i = 0; i < urb->number_of_packets; i++ ) 15.1326 + { 15.1327 + urb->iso_frame_desc[i].status 15.1328 + = urbp->schedule[i].status; 15.1329 + urb->iso_frame_desc[i].actual_length 15.1330 + = urbp->schedule[i].length; 15.1331 + } 15.1332 + free_page((unsigned long)urbp->schedule); 15.1333 + } 15.1334 +} 15.1335 + 15.1336 +static void xhci_drain_ring(void) 15.1337 +{ 15.1338 + struct list_head *tmp, *head; 15.1339 + usbif_t *usb_ring = xhci->usbif; 15.1340 + usbif_response_t *resp; 15.1341 + USBIF_RING_IDX i, rp; 15.1342 + 15.1343 + /* Walk the ring here to get responses, updating URBs to show what 15.1344 + * completed. */ 15.1345 + 15.1346 + rp = usb_ring->resp_prod; 15.1347 + rmb(); /* Ensure we see queued requests up to 'rp'. */ 15.1348 + 15.1349 + /* Take items off the comms ring, taking care not to overflow. */ 15.1350 + for ( i = xhci->usb_resp_cons; 15.1351 + (i != rp) && ((i-usb_ring->req_prod) != USBIF_RING_SIZE); 15.1352 + i++ ) 15.1353 + { 15.1354 + resp = &usb_ring->ring[MASK_USBIF_IDX(i)].resp; 15.1355 + 15.1356 + /* May need to deal with batching and with putting a ceiling on 15.1357 + the number dispatched for performance and anti-dos reasons */ 15.1358 + 15.1359 +#if 0 15.1360 + printk("usbfront: Processing response:\n"); 15.1361 + printk(" id = 0x%x\n", resp->id); 15.1362 + printk(" op = %d\n", resp->operation); 15.1363 + printk(" status = %d\n", resp->status); 15.1364 + printk(" length = %d\n", resp->length); 15.1365 +#endif 15.1366 + 15.1367 + switch ( resp->operation ) 15.1368 + { 15.1369 + case USBIF_OP_PROBE: 15.1370 + receive_usb_probe(resp); 15.1371 + break; 15.1372 + 15.1373 + case USBIF_OP_IO: 15.1374 + receive_usb_io(resp); 15.1375 + break; 15.1376 + 15.1377 + case USBIF_OP_RESET: 15.1378 + receive_usb_reset(resp); 15.1379 + break; 15.1380 + 15.1381 + default: 15.1382 + printk("error: unknown USB io operation response [%d]\n", 15.1383 + usb_ring->ring[i].req.operation); 15.1384 + break; 15.1385 + } 15.1386 + } 15.1387 + 15.1388 + xhci->usb_resp_cons = i; 15.1389 + 15.1390 + /* Walk the list of pending URB's to see which ones completed and do 15.1391 + * callbacks, etc. */ 15.1392 + spin_lock(&xhci->urb_list_lock); 15.1393 + head = &xhci->urb_list; 15.1394 + tmp = head->next; 15.1395 + while (tmp != head) { 15.1396 + 15.1397 + struct urb *urb = list_entry(tmp, struct urb, urb_list); 15.1398 + 15.1399 + tmp = tmp->next; 15.1400 + 15.1401 + /* Checks the status and does all of the magic necessary */ 15.1402 + xhci_transfer_result(xhci, urb); 15.1403 + } 15.1404 + spin_unlock(&xhci->urb_list_lock); 15.1405 + 15.1406 + xhci_finish_completion(); 15.1407 +} 15.1408 + 15.1409 + 15.1410 +static void xhci_interrupt(int irq, void *__xhci, struct pt_regs *regs) 15.1411 +{ 15.1412 + xhci_drain_ring(); 15.1413 +} 15.1414 + 15.1415 +static void free_xhci(struct xhci *xhci) 15.1416 +{ 15.1417 + kfree(xhci); 15.1418 +} 15.1419 + 15.1420 +/* /\* */ 15.1421 +/* * De-allocate all resources.. */ 15.1422 +/* *\/ */ 15.1423 +/* static void release_xhci(struct xhci *xhci) */ 15.1424 +/* { */ 15.1425 +/* if (xhci->irq >= 0) { */ 15.1426 +/* free_irq(xhci->irq, xhci); */ 15.1427 +/* xhci->irq = -1; */ 15.1428 +/* } */ 15.1429 + 15.1430 +/* /\* Get the ring back from the backend domain. Then free it. Hmmmm. */ 15.1431 +/* * Lets ignore this for now - not particularly useful. *\/ */ 15.1432 + 15.1433 +/* free_xhci(xhci); */ 15.1434 +/* } */ 15.1435 + 15.1436 +/** 15.1437 + * Initialise a new virtual root hub for a new USB device channel. 15.1438 + */ 15.1439 +static int alloc_xhci(void) 15.1440 +{ 15.1441 + int retval; 15.1442 + struct usb_bus *bus; 15.1443 + 15.1444 + retval = -EBUSY; 15.1445 + 15.1446 + xhci = kmalloc(sizeof(*xhci), GFP_KERNEL); 15.1447 + if (!xhci) { 15.1448 + err("couldn't allocate xhci structure"); 15.1449 + retval = -ENOMEM; 15.1450 + goto err_alloc_xhci; 15.1451 + } 15.1452 + 15.1453 + /* Reset here so we don't get any interrupts from an old setup */ 15.1454 + /* or broken setup */ 15.1455 + // reset_hc(xhci); 15.1456 + 15.1457 + 15.1458 + xhci->state = USBIF_STATE_CLOSED; 15.1459 + xhci->is_suspended = 0; 15.1460 + 15.1461 + spin_lock_init(&xhci->urb_remove_list_lock); 15.1462 + INIT_LIST_HEAD(&xhci->urb_remove_list); 15.1463 + 15.1464 + spin_lock_init(&xhci->urb_list_lock); 15.1465 + INIT_LIST_HEAD(&xhci->urb_list); 15.1466 + 15.1467 + spin_lock_init(&xhci->complete_list_lock); 15.1468 + INIT_LIST_HEAD(&xhci->complete_list); 15.1469 + 15.1470 + spin_lock_init(&xhci->frame_list_lock); 15.1471 + 15.1472 + /* We need exactly one page (per XHCI specs), how convenient */ 15.1473 + /* We assume that one page is atleast 4k (1024 frames * 4 bytes) */ 15.1474 +#if PAGE_SIZE < (4 * 1024) 15.1475 +#error PAGE_SIZE is not atleast 4k 15.1476 +#endif 15.1477 + bus = usb_alloc_bus(&xhci_device_operations); 15.1478 + if (!bus) { 15.1479 + err("unable to allocate bus"); 15.1480 + goto err_alloc_bus; 15.1481 + } 15.1482 + 15.1483 + xhci->bus = bus; 15.1484 + bus->bus_name = "XHCI"; 15.1485 + bus->hcpriv = xhci; 15.1486 + 15.1487 + usb_register_bus(xhci->bus); 15.1488 + 15.1489 + /* Initialize the root hub */ 15.1490 + 15.1491 + xhci->rh.numports = 0; 15.1492 + 15.1493 + xhci->bus->root_hub = xhci->rh.dev = usb_alloc_dev(NULL, xhci->bus); 15.1494 + if (!xhci->rh.dev) { 15.1495 + err("unable to allocate root hub"); 15.1496 + goto err_alloc_root_hub; 15.1497 + } 15.1498 + 15.1499 + xhci->state = 0; 15.1500 + 15.1501 + return 0; 15.1502 + 15.1503 +/* 15.1504 + * error exits: 15.1505 + */ 15.1506 +err_start_root_hub: 15.1507 + free_irq(xhci->irq, xhci); 15.1508 + xhci->irq = -1; 15.1509 + 15.1510 +err_alloc_root_hub: 15.1511 + usb_free_bus(xhci->bus); 15.1512 + xhci->bus = NULL; 15.1513 + 15.1514 +err_alloc_bus: 15.1515 + free_xhci(xhci); 15.1516 + 15.1517 +err_alloc_xhci: 15.1518 + return retval; 15.1519 +} 15.1520 + 15.1521 +static void usbif_status_change(usbif_fe_interface_status_changed_t *status) 15.1522 +{ 15.1523 + ctrl_msg_t cmsg; 15.1524 + usbif_fe_interface_connect_t up; 15.1525 + long rc; 15.1526 + usbif_t *usbif; 15.1527 + 15.1528 + switch ( status->status ) 15.1529 + { 15.1530 + case USBIF_INTERFACE_STATUS_DESTROYED: 15.1531 + printk(KERN_WARNING "Unexpected usbif-DESTROYED message in state %d\n", 15.1532 + xhci->state); 15.1533 + break; 15.1534 + 15.1535 + case USBIF_INTERFACE_STATUS_DISCONNECTED: 15.1536 + if ( xhci->state != USBIF_STATE_CLOSED ) 15.1537 + { 15.1538 + printk(KERN_WARNING "Unexpected usbif-DISCONNECTED message" 15.1539 + " in state %d\n", xhci->state); 15.1540 + break; 15.1541 + /* Not bothering to do recovery here for now. Keep things 15.1542 + * simple. */ 15.1543 + } 15.1544 + 15.1545 + /* Move from CLOSED to DISCONNECTED state. */ 15.1546 + xhci->usbif = usbif = (usbif_t *)__get_free_page(GFP_KERNEL); 15.1547 + usbif->req_prod = usbif->resp_prod = 0; 15.1548 + xhci->state = USBIF_STATE_DISCONNECTED; 15.1549 + 15.1550 + /* Construct an interface-CONNECT message for the domain controller. */ 15.1551 + cmsg.type = CMSG_USBIF_FE; 15.1552 + cmsg.subtype = CMSG_USBIF_FE_INTERFACE_CONNECT; 15.1553 + cmsg.length = sizeof(usbif_fe_interface_connect_t); 15.1554 + up.shmem_frame = virt_to_machine(usbif) >> PAGE_SHIFT; 15.1555 + memcpy(cmsg.msg, &up, sizeof(up)); 15.1556 + 15.1557 + /* Tell the controller to bring up the interface. */ 15.1558 + ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE); 15.1559 + break; 15.1560 + 15.1561 + case USBIF_INTERFACE_STATUS_CONNECTED: 15.1562 + if ( xhci->state == USBIF_STATE_CLOSED ) 15.1563 + { 15.1564 + printk(KERN_WARNING "Unexpected usbif-CONNECTED message" 15.1565 + " in state %d\n", xhci->state); 15.1566 + break; 15.1567 + } 15.1568 + 15.1569 + xhci->evtchn = status->evtchn; 15.1570 + xhci->irq = bind_evtchn_to_irq(xhci->evtchn); 15.1571 + xhci->bandwidth = status->bandwidth; 15.1572 + xhci->rh.numports = status->num_ports; 15.1573 + 15.1574 + xhci->rh.ports = kmalloc (sizeof(xhci_port_t) * xhci->rh.numports, GFP_KERNEL); 15.1575 + memset(xhci->rh.ports, 0, sizeof(xhci_port_t) * xhci->rh.numports); 15.1576 + 15.1577 + printk("rh.dev @ %p\n", xhci->rh.dev); 15.1578 + 15.1579 + usb_connect(xhci->rh.dev); 15.1580 + 15.1581 + if (usb_new_device(xhci->rh.dev) != 0) { 15.1582 + err("unable to start root hub"); 15.1583 + } 15.1584 + 15.1585 + /* Allocate the appropriate USB bandwidth here... Need to 15.1586 + * somehow know what the total available is thought to be so we 15.1587 + * can calculate the reservation correctly. */ 15.1588 + usb_claim_bandwidth(xhci->rh.dev, xhci->rh.urb, 15.1589 + 1000 - xhci->bandwidth, 0); 15.1590 + 15.1591 + if ( (rc = request_irq(xhci->irq, xhci_interrupt, 15.1592 + SA_SAMPLE_RANDOM, "usbif", xhci)) ) 15.1593 + printk(KERN_ALERT"usbfront request_irq failed (%ld)\n",rc); 15.1594 + 15.1595 + printk(KERN_INFO __FILE__ ": USB XHCI: SHM at %p (0x%lx), EVTCHN %d IRQ %d\n", 15.1596 + xhci->usbif, virt_to_machine(xhci->usbif), xhci->evtchn, xhci->irq); 15.1597 + 15.1598 + xhci->state = USBIF_STATE_CONNECTED; 15.1599 + 15.1600 + break; 15.1601 + 15.1602 + default: 15.1603 + printk(KERN_WARNING "Status change to unknown value %d\n", 15.1604 + status->status); 15.1605 + break; 15.1606 + } 15.1607 +} 15.1608 + 15.1609 + 15.1610 +static void usbif_ctrlif_rx(ctrl_msg_t *msg, unsigned long id) 15.1611 +{ 15.1612 + switch ( msg->subtype ) 15.1613 + { 15.1614 + case CMSG_USBIF_FE_INTERFACE_STATUS_CHANGED: 15.1615 + if ( msg->length != sizeof(usbif_fe_interface_status_changed_t) ) 15.1616 + goto parse_error; 15.1617 + usbif_status_change((usbif_fe_interface_status_changed_t *) 15.1618 + &msg->msg[0]); 15.1619 + break; 15.1620 + 15.1621 + /* New interface...? */ 15.1622 + default: 15.1623 + goto parse_error; 15.1624 + } 15.1625 + 15.1626 + ctrl_if_send_response(msg); 15.1627 + return; 15.1628 + 15.1629 + parse_error: 15.1630 + msg->length = 0; 15.1631 + ctrl_if_send_response(msg); 15.1632 +} 15.1633 + 15.1634 + 15.1635 +static int __init xhci_hcd_init(void) 15.1636 +{ 15.1637 + int retval = -ENOMEM, i; 15.1638 + usbif_fe_interface_status_changed_t st; 15.1639 + control_msg_t cmsg; 15.1640 + 15.1641 + if ( (xen_start_info.flags & SIF_INITDOMAIN) 15.1642 + || (xen_start_info.flags & SIF_USB_BE_DOMAIN) ) 15.1643 + return 0; 15.1644 + 15.1645 + info(DRIVER_DESC " " DRIVER_VERSION); 15.1646 + 15.1647 + if (debug) { 15.1648 + errbuf = kmalloc(ERRBUF_LEN, GFP_KERNEL); 15.1649 + if (!errbuf) 15.1650 + goto errbuf_failed; 15.1651 + } 15.1652 + 15.1653 + xhci_up_cachep = kmem_cache_create("xhci_urb_priv", 15.1654 + sizeof(struct urb_priv), 0, 0, NULL, NULL); 15.1655 + if (!xhci_up_cachep) 15.1656 + goto up_failed; 15.1657 + 15.1658 + /* Lazily avoid unloading issues for now. ;-)*/ 15.1659 + MOD_INC_USE_COUNT; 15.1660 + 15.1661 + /* Let the domain controller know we're here. For now we wait until 15.1662 + * connection, as for the block and net drivers. This is only strictly 15.1663 + * necessary if we're going to boot off a USB device. */ 15.1664 + printk(KERN_INFO "Initialising Xen virtual USB hub\n"); 15.1665 + 15.1666 + (void)ctrl_if_register_receiver(CMSG_USBIF_FE, usbif_ctrlif_rx, 15.1667 + CALLBACK_IN_BLOCKING_CONTEXT); 15.1668 + 15.1669 + alloc_xhci(); 15.1670 + 15.1671 + /* Send a driver-UP notification to the domain controller. */ 15.1672 + cmsg.type = CMSG_USBIF_FE; 15.1673 + cmsg.subtype = CMSG_USBIF_FE_DRIVER_STATUS_CHANGED; 15.1674 + cmsg.length = sizeof(usbif_fe_driver_status_changed_t); 15.1675 + st.status = USBIF_DRIVER_STATUS_UP; 15.1676 + memcpy(cmsg.msg, &st, sizeof(st)); 15.1677 + ctrl_if_send_message_block(&cmsg, NULL, 0, TASK_UNINTERRUPTIBLE); 15.1678 + 15.1679 + /* 15.1680 + * We should read 'nr_interfaces' from response message and wait 15.1681 + * for notifications before proceeding. For now we assume that we 15.1682 + * will be notified of exactly one interface. 15.1683 + */ 15.1684 + for ( i=0; (xhci->state != USBIF_STATE_CONNECTED) && (i < 10*HZ); i++ ) 15.1685 + { 15.1686 + set_current_state(TASK_INTERRUPTIBLE); 15.1687 + schedule_timeout(1); 15.1688 + } 15.1689 + 15.1690 + if (xhci->state != USBIF_STATE_CONNECTED) 15.1691 + printk(KERN_INFO "Timeout connecting USB frontend driver!\n"); 15.1692 + 15.1693 + return 0; 15.1694 + 15.1695 +up_failed: 15.1696 + 15.1697 + if (errbuf) 15.1698 + kfree(errbuf); 15.1699 + 15.1700 +errbuf_failed: 15.1701 + 15.1702 + return retval; 15.1703 +} 15.1704 + 15.1705 +static void __exit xhci_hcd_cleanup(void) 15.1706 +{ 15.1707 + if (kmem_cache_destroy(xhci_up_cachep)) 15.1708 + printk(KERN_INFO "xhci: not all urb_priv's were freed\n"); 15.1709 + 15.1710 +// release_xhci(); do some calls here 15.1711 + 15.1712 + 15.1713 + if (errbuf) 15.1714 + kfree(errbuf); 15.1715 +} 15.1716 + 15.1717 +module_init(xhci_hcd_init); 15.1718 +module_exit(xhci_hcd_cleanup); 15.1719 + 15.1720 +MODULE_AUTHOR(DRIVER_AUTHOR); 15.1721 +MODULE_DESCRIPTION(DRIVER_DESC); 15.1722 +MODULE_LICENSE("GPL"); 15.1723 +
16.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 16.2 +++ b/linux-2.6.10-xen-sparse/drivers/xen/usbfront/xhci.h Wed Jan 26 13:09:50 2005 +0000 16.3 @@ -0,0 +1,210 @@ 16.4 +#ifndef __LINUX_XHCI_H 16.5 +#define __LINUX_XHCI_H 16.6 + 16.7 +#include <linux/list.h> 16.8 +#include <linux/usb.h> 16.9 +#include <asm-xen/xen-public/io/usbif.h> 16.10 +#include <linux/spinlock.h> 16.11 + 16.12 +#define XHCI_NUMFRAMES 1024 /* in the frame list [array] */ 16.13 +#define XHCI_MAX_SOF_NUMBER 2047 /* in an SOF packet */ 16.14 +#define CAN_SCHEDULE_FRAMES 1000 /* how far future frames can be scheduled */ 16.15 + 16.16 +/* In the absence of actual hardware state, we maintain the current known state 16.17 + * of the virtual hub ports in this data structure. 16.18 + */ 16.19 +typedef struct 16.20 +{ 16.21 + unsigned int cs :1; /* Connection status. do we really need this /and/ ccs? */ 16.22 + unsigned int cs_chg :1; /* Connection status change. */ 16.23 + unsigned int pe :1; /* Port enable. */ 16.24 + unsigned int pe_chg :1; /* Port enable change. */ 16.25 + unsigned int ccs :1; /* Current connect status. */ 16.26 + unsigned int susp :1; /* Suspended. */ 16.27 + unsigned int lsda :1; /* Low speed device attached. */ 16.28 + unsigned int pr :1; /* Port reset. */ 16.29 + 16.30 + /* Device info? */ 16.31 +} xhci_port_t; 16.32 + 16.33 +struct xhci_frame_list { 16.34 + __u32 frame[XHCI_NUMFRAMES]; 16.35 + 16.36 + void *frame_cpu[XHCI_NUMFRAMES]; 16.37 +}; 16.38 + 16.39 +struct urb_priv; 16.40 + 16.41 +#define xhci_status_bits(ctrl_sts) (ctrl_sts & 0xFE0000) 16.42 +#define xhci_actual_length(ctrl_sts) ((ctrl_sts + 1) & TD_CTRL_ACTLEN_MASK) /* 1-based */ 16.43 + 16.44 +#define xhci_maxlen(token) ((token) >> 21) 16.45 +#define xhci_expected_length(info) (((info >> 21) + 1) & TD_TOKEN_EXPLEN_MASK) /* 1-based */ 16.46 +#define xhci_toggle(token) (((token) >> TD_TOKEN_TOGGLE_SHIFT) & 1) 16.47 +#define xhci_endpoint(token) (((token) >> 15) & 0xf) 16.48 +#define xhci_devaddr(token) (((token) >> 8) & 0x7f) 16.49 +#define xhci_devep(token) (((token) >> 8) & 0x7ff) 16.50 +#define xhci_packetid(token) ((token) & TD_TOKEN_PID_MASK) 16.51 +#define xhci_packetout(token) (xhci_packetid(token) != USB_PID_IN) 16.52 +#define xhci_packetin(token) (xhci_packetid(token) == USB_PID_IN) 16.53 + 16.54 +struct virt_root_hub { 16.55 + struct usb_device *dev; 16.56 + int devnum; /* Address of Root Hub endpoint */ 16.57 + struct urb *urb; 16.58 + void *int_addr; 16.59 + int send; 16.60 + int interval; 16.61 + int numports; 16.62 + int c_p_r[8]; 16.63 + struct timer_list rh_int_timer; 16.64 + spinlock_t port_state_lock; 16.65 + xhci_port_t *ports; /* */ 16.66 +}; 16.67 + 16.68 +/* 16.69 + * This describes the full xhci information. 16.70 + * 16.71 + * Note how the "proper" USB information is just 16.72 + * a subset of what the full implementation needs. 16.73 + */ 16.74 +struct xhci { 16.75 + 16.76 +#ifdef CONFIG_PROC_FS 16.77 + /* procfs */ 16.78 + int num; 16.79 + struct proc_dir_entry *proc_entry; 16.80 +#endif 16.81 + 16.82 + int evtchn; /* Interdom channel to backend */ 16.83 + int irq; /* Bound to evtchn */ 16.84 + int state; /* State of this USB interface */ 16.85 + unsigned long bandwidth; 16.86 + int handle; 16.87 + 16.88 + struct usb_bus *bus; 16.89 + 16.90 + spinlock_t frame_list_lock; 16.91 + struct xhci_frame_list *fl; /* P: xhci->frame_list_lock */ 16.92 + int is_suspended; 16.93 + 16.94 + /* Main list of URB's currently controlled by this HC */ 16.95 + spinlock_t urb_list_lock; 16.96 + struct list_head urb_list; /* P: xhci->urb_list_lock */ 16.97 + 16.98 + /* List of asynchronously unlinked URB's */ 16.99 + spinlock_t urb_remove_list_lock; 16.100 + struct list_head urb_remove_list; /* P: xhci->urb_remove_list_lock */ 16.101 + 16.102 + /* List of URB's awaiting completion callback */ 16.103 + spinlock_t complete_list_lock; 16.104 + struct list_head complete_list; /* P: xhci->complete_list_lock */ 16.105 + 16.106 + struct virt_root_hub rh; /* private data of the virtual root hub */ 16.107 + 16.108 + spinlock_t response_lock; 16.109 + 16.110 + usbif_t *usbif; 16.111 + int usb_resp_cons; 16.112 +}; 16.113 + 16.114 +struct urb_priv { 16.115 + struct urb *urb; 16.116 + usbif_iso_t *schedule; 16.117 + struct usb_device *dev; 16.118 + 16.119 + int in_progress : 1; /* QH was queued (not linked in) */ 16.120 + int short_control_packet : 1; /* If we get a short packet during */ 16.121 + /* a control transfer, retrigger */ 16.122 + /* the status phase */ 16.123 + 16.124 + int status; /* Final status */ 16.125 + 16.126 + unsigned long inserttime; /* In jiffies */ 16.127 + 16.128 + struct list_head queue_list; /* P: xhci->frame_list_lock */ 16.129 + struct list_head complete_list; /* P: xhci->complete_list_lock */ 16.130 +}; 16.131 + 16.132 +/* 16.133 + * Locking in xhci.c 16.134 + * 16.135 + * spinlocks are used extensively to protect the many lists and data 16.136 + * structures we have. It's not that pretty, but it's necessary. We 16.137 + * need to be done with all of the locks (except complete_list_lock) when 16.138 + * we call urb->complete. I've tried to make it simple enough so I don't 16.139 + * have to spend hours racking my brain trying to figure out if the 16.140 + * locking is safe. 16.141 + * 16.142 + * Here's the safe locking order to prevent deadlocks: 16.143 + * 16.144 + * #1 xhci->urb_list_lock 16.145 + * #2 urb->lock 16.146 + * #3 xhci->urb_remove_list_lock, xhci->frame_list_lock, 16.147 + * xhci->qh_remove_list_lock 16.148 + * #4 xhci->complete_list_lock 16.149 + * 16.150 + * If you're going to grab 2 or more locks at once, ALWAYS grab the lock 16.151 + * at the lowest level FIRST and NEVER grab locks at the same level at the 16.152 + * same time. 16.153 + * 16.154 + * So, if you need xhci->urb_list_lock, grab it before you grab urb->lock 16.155 + */ 16.156 + 16.157 +/* ------------------------------------------------------------------------- 16.158 + Virtual Root HUB 16.159 + ------------------------------------------------------------------------- */ 16.160 +/* destination of request */ 16.161 +#define RH_DEVICE 0x00 16.162 +#define RH_INTERFACE 0x01 16.163 +#define RH_ENDPOINT 0x02 16.164 +#define RH_OTHER 0x03 16.165 + 16.166 +#define RH_CLASS 0x20 16.167 +#define RH_VENDOR 0x40 16.168 + 16.169 +/* Requests: bRequest << 8 | bmRequestType */ 16.170 +#define RH_GET_STATUS 0x0080 16.171 +#define RH_CLEAR_FEATURE 0x0100 16.172 +#define RH_SET_FEATURE 0x0300 16.173 +#define RH_SET_ADDRESS 0x0500 16.174 +#define RH_GET_DESCRIPTOR 0x0680 16.175 +#define RH_SET_DESCRIPTOR 0x0700 16.176 +#define RH_GET_CONFIGURATION 0x0880 16.177 +#define RH_SET_CONFIGURATION 0x0900 16.178 +#define RH_GET_STATE 0x0280 16.179 +#define RH_GET_INTERFACE 0x0A80 16.180 +#define RH_SET_INTERFACE 0x0B00 16.181 +#define RH_SYNC_FRAME 0x0C80 16.182 +/* Our Vendor Specific Request */ 16.183 +#define RH_SET_EP 0x2000 16.184 + 16.185 +/* Hub port features */ 16.186 +#define RH_PORT_CONNECTION 0x00 16.187 +#define RH_PORT_ENABLE 0x01 16.188 +#define RH_PORT_SUSPEND 0x02 16.189 +#define RH_PORT_OVER_CURRENT 0x03 16.190 +#define RH_PORT_RESET 0x04 16.191 +#define RH_PORT_POWER 0x08 16.192 +#define RH_PORT_LOW_SPEED 0x09 16.193 +#define RH_C_PORT_CONNECTION 0x10 16.194 +#define RH_C_PORT_ENABLE 0x11 16.195 +#define RH_C_PORT_SUSPEND 0x12 16.196 +#define RH_C_PORT_OVER_CURRENT 0x13 16.197 +#define RH_C_PORT_RESET 0x14 16.198 + 16.199 +/* Hub features */ 16.200 +#define RH_C_HUB_LOCAL_POWER 0x00 16.201 +#define RH_C_HUB_OVER_CURRENT 0x01 16.202 +#define RH_DEVICE_REMOTE_WAKEUP 0x00 16.203 +#define RH_ENDPOINT_STALL 0x01 16.204 + 16.205 +/* Our Vendor Specific feature */ 16.206 +#define RH_REMOVE_EP 0x00 16.207 + 16.208 +#define RH_ACK 0x01 16.209 +#define RH_REQ_ERR -1 16.210 +#define RH_NACK 0x00 16.211 + 16.212 +#endif 16.213 +
17.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 17.2 +++ b/xen/include/public/io/usbif.h Wed Jan 26 13:09:50 2005 +0000 17.3 @@ -0,0 +1,111 @@ 17.4 +/****************************************************************************** 17.5 + * usbif.h 17.6 + * 17.7 + * Unified block-device I/O interface for Xen guest OSes. 17.8 + * 17.9 + * Copyright (c) 2003-2004, Keir Fraser 17.10 + */ 17.11 + 17.12 +#ifndef __SHARED_USBIF_H__ 17.13 +#define __SHARED_USBIF_H__ 17.14 + 17.15 +#define usbif_vdev_t u16 17.16 +#define usbif_sector_t u64 17.17 + 17.18 +#define USBIF_OP_IO 0 17.19 +#define USBIF_OP_PROBE 1 /* Is there a device on this port? */ 17.20 +#define USBIF_OP_RESET 2 /* Reset a virtual USB port. */ 17.21 + 17.22 +/* NB. Ring size must be small enough for sizeof(usbif_ring_t) <= PAGE_SIZE. */ 17.23 +#define USBIF_RING_SIZE 64 17.24 + 17.25 +/* XXX this does not want to be here! it really ought to be dynamic but it can 17.26 + * live here for now */ 17.27 +#define NUM_PORTS 1 17.28 + 17.29 +typedef struct { 17.30 + unsigned long id; /* 0: private guest value, echoed in resp */ 17.31 + u8 operation; /* 4: USBIF_OP_??? */ 17.32 + u8 __pad1; 17.33 + usbif_vdev_t port; /* 6 : guest virtual USB port */ 17.34 + unsigned long devnum :7; /* 8 : Device address, as seen by the guest.*/ 17.35 + unsigned long endpoint :4; /* Device endpoint. */ 17.36 + unsigned long direction :1; /* Pipe direction. */ 17.37 + unsigned long speed :1; /* Pipe speed. */ 17.38 + unsigned long pipe_type :2; /* Pipe type (iso, bulk, int, ctrl) */ 17.39 + unsigned long __pad2 :18; 17.40 + unsigned long transfer_buffer; /* 12: Machine address */ 17.41 + unsigned long length; /* 16: Buffer length */ 17.42 + unsigned long transfer_flags; /* 20: For now just pass Linux transfer 17.43 + * flags - this may change. */ 17.44 + unsigned char setup[8]; /* 22 Embed setup packets directly. */ 17.45 + unsigned long iso_schedule; /* 30 Machine address of transfer sched (iso 17.46 + * only) */ 17.47 + unsigned long num_iso; /* 34 : length of iso schedule */ 17.48 + unsigned long timeout; /* 38: timeout in ms */ 17.49 +} PACKED usbif_request_t; /* 42 */ 17.50 +/* Data we need to pass: 17.51 + * - Transparently handle short packets or complain at us? 17.52 + */ 17.53 + 17.54 +typedef struct { 17.55 + unsigned long id; /* 0: copied from request */ 17.56 + u8 operation; /* 4: copied from request */ 17.57 + u8 data; /* 5: Small chunk of in-band data */ 17.58 + s16 status; /* 6: USBIF_RSP_??? */ 17.59 + unsigned long transfer_mutex; /* Used for cancelling requests atomically. */ 17.60 + unsigned long length; /* 8: How much data we really got */ 17.61 +} PACKED usbif_response_t; 17.62 + 17.63 +#define USBIF_RSP_ERROR -1 /* non-specific 'error' */ 17.64 +#define USBIF_RSP_OKAY 0 /* non-specific 'okay' */ 17.65 + 17.66 +/* 17.67 + * We use a special capitalised type name because it is _essential_ that all 17.68 + * arithmetic on indexes is done on an integer type of the correct size. 17.69 + */ 17.70 +typedef u32 USBIF_RING_IDX; 17.71 + 17.72 +/* 17.73 + * Ring indexes are 'free running'. That is, they are not stored modulo the 17.74 + * size of the ring buffer. The following macro converts a free-running counter 17.75 + * into a value that can directly index a ring-buffer array. 17.76 + */ 17.77 +#define MASK_USBIF_IDX(_i) ((_i)&(USBIF_RING_SIZE-1)) 17.78 + 17.79 +typedef struct { 17.80 + USBIF_RING_IDX req_prod; /* 0: Request producer. Updated by front-end. */ 17.81 + USBIF_RING_IDX resp_prod; /* 4: Response producer. Updated by back-end. */ 17.82 + 17.83 + union { /* 8 */ 17.84 + usbif_request_t req; 17.85 + usbif_response_t resp; 17.86 + } PACKED ring[USBIF_RING_SIZE]; 17.87 +} PACKED usbif_t; 17.88 + 17.89 + 17.90 + 17.91 +/* 17.92 + * USBIF_OP_PROBE: 17.93 + * The request format for a probe request is constrained as follows: 17.94 + * @operation == USBIF_OP_PROBE 17.95 + * @nr_segments == size of probe buffer in pages 17.96 + * @device == unused (zero) 17.97 + * @id == any value (echoed in response message) 17.98 + * @sector_num == unused (zero) 17.99 + * @frame_and_sects == list of page-sized buffers. 17.100 + * (i.e., @first_sect == 0, @last_sect == 7). 17.101 + * 17.102 + * The response is a list of vdisk_t elements copied into the out-of-band 17.103 + * probe buffer. On success the response status field contains the number 17.104 + * of vdisk_t elements. 17.105 + */ 17.106 + 17.107 +typedef struct { 17.108 + unsigned long length; /* IN = expected, OUT = actual */ 17.109 + unsigned long buffer_offset; /* IN offset in buffer specified in main 17.110 + packet */ 17.111 + unsigned long status; /* OUT Status for this packet. */ 17.112 +} usbif_iso_t; 17.113 + 17.114 +#endif /* __SHARED_USBIF_H__ */