- Update Xen patches to 3.3-rc5 and c/s 1157.
[linux-flexiantxendom0-3.2.10.git] / drivers / xen / usbback / usbback.h
1 /*
2  * usbback.h
3  *
4  * This file is part of Xen USB backend driver.
5  *
6  * Copyright (C) 2009, FUJITSU LABORATORIES LTD.
7  * Author: Noboru Iwamatsu <n_iwamatsu@jp.fujitsu.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, see <http://www.gnu.org/licenses/>.
21  *
22  * or, by your choice,
23  *
24  * When distributed separately from the Linux kernel or incorporated into
25  * other software packages, subject to the following license:
26  *
27  * Permission is hereby granted, free of charge, to any person obtaining a copy
28  * of this software and associated documentation files (the "Software"), to
29  * deal in the Software without restriction, including without limitation the
30  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
31  * sell copies of the Software, and to permit persons to whom the Software is
32  * furnished to do so, subject to the following conditions:
33  *
34  * The above copyright notice and this permission notice shall be included in
35  * all copies or substantial portions of the Software.
36  *
37  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
42  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
43  * DEALINGS IN THE SOFTWARE.
44  */
45
46 #ifndef __XEN_USBBACK_H__
47 #define __XEN_USBBACK_H__
48
49 #include <linux/module.h>
50 #include <linux/interrupt.h>
51 #include <linux/slab.h>
52 #include <linux/usb.h>
53 #include <linux/kthread.h>
54 #include <linux/wait.h>
55 #include <linux/list.h>
56 #include <linux/kref.h>
57 #include <xen/xenbus.h>
58 #include <xen/interface/event_channel.h>
59 #include <xen/interface/io/usbif.h>
60
61 struct usbstub;
62
63 #ifndef BUS_ID_SIZE
64 #define USBBACK_BUS_ID_SIZE 20
65 #else
66 #define USBBACK_BUS_ID_SIZE BUS_ID_SIZE
67 #endif
68
69 #define USB_DEV_ADDR_SIZE 128
70
71 typedef struct usbif_st {
72         domid_t domid;
73         unsigned int handle;
74         int num_ports;
75         enum usb_spec_version usb_ver;
76
77         struct xenbus_device *xbdev;
78         struct list_head usbif_list;
79
80         unsigned int      irq;
81
82         usbif_urb_back_ring_t urb_ring;
83         usbif_conn_back_ring_t conn_ring;
84         struct vm_struct *urb_ring_area;
85         struct vm_struct *conn_ring_area;
86
87         spinlock_t urb_ring_lock;
88         spinlock_t conn_ring_lock;
89         atomic_t refcnt;
90
91         struct xenbus_watch backend_watch;
92
93         /* device address lookup table */
94         struct usbstub *addr_table[USB_DEV_ADDR_SIZE];
95         spinlock_t addr_lock;
96
97         /* connected device list */
98         struct list_head stub_list;
99         spinlock_t stub_lock;
100
101         /* request schedule */
102         struct task_struct *xenusbd;
103         unsigned int waiting_reqs;
104         wait_queue_head_t waiting_to_free;
105         wait_queue_head_t wq;
106 } usbif_t;
107
108 struct vusb_port_id {
109         struct list_head id_list;
110
111         char phys_bus[USBBACK_BUS_ID_SIZE];
112         domid_t domid;
113         unsigned int handle;
114         int portnum;
115         unsigned is_connected:1;
116 };
117
118 struct usbstub {
119         struct kref kref;
120         struct list_head dev_list;
121
122         struct vusb_port_id *portid;
123         struct usb_device *udev;
124         usbif_t *usbif;
125         int addr;
126
127         struct list_head submitting_list;
128         spinlock_t submitting_lock;
129 };
130
131 usbif_t *usbif_alloc(domid_t domid, unsigned int handle);
132 void usbif_disconnect(usbif_t *usbif);
133 void usbif_free(usbif_t *usbif);
134 int usbif_map(usbif_t *usbif, grant_ref_t urb_ring_ref,
135               grant_ref_t conn_ring_ref, evtchn_port_t);
136
137 #define usbif_get(_b) (atomic_inc(&(_b)->refcnt))
138 #define usbif_put(_b) \
139         do { \
140                 if (atomic_dec_and_test(&(_b)->refcnt)) \
141                         wake_up(&(_b)->waiting_to_free); \
142         } while (0)
143
144 usbif_t *find_usbif(domid_t domid, unsigned int handle);
145 int usbback_xenbus_init(void);
146 void usbback_xenbus_exit(void);
147 struct vusb_port_id *find_portid_by_busid(const char *busid);
148 struct vusb_port_id *find_portid(const domid_t domid,
149                                                 const unsigned int handle,
150                                                 const int portnum);
151 int portid_add(const char *busid,
152                                         const domid_t domid,
153                                         const unsigned int handle,
154                                         const int portnum);
155 int portid_remove(const domid_t domid,
156                                         const unsigned int handle,
157                                         const int portnum);
158 irqreturn_t usbbk_be_int(int irq, void *dev_id);
159 int usbbk_schedule(void *arg);
160 struct usbstub *find_attached_device(usbif_t *usbif, int port);
161 void usbbk_attach_device(usbif_t *usbif, struct usbstub *stub);
162 void usbbk_detach_device(usbif_t *usbif, struct usbstub *stub);
163 void usbbk_hotplug_notify(usbif_t *usbif, int portnum, int speed);
164 void detach_device_without_lock(usbif_t *usbif, struct usbstub *stub);
165 void usbbk_unlink_urbs(struct usbstub *stub);
166
167 int usbstub_init(void);
168 void usbstub_exit(void);
169
170 #endif /* __XEN_USBBACK_H__ */