mfd: twl4030 IRQ handling update
[linux-2.6/libata-dev.git] / drivers / staging / usbip / stub_main.c
blobc665d7f1ca9adbe4fbef93a0543fad30809ab54a
1 /*
2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
4 * This is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 * USA.
21 #include "usbip_common.h"
22 #include "stub.h"
24 /* Version Information */
25 #define DRIVER_VERSION "1.0"
26 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
27 #define DRIVER_DESC "Stub Driver for USB/IP"
29 /* stub_priv is allocated from stub_priv_cache */
30 struct kmem_cache *stub_priv_cache;
32 /*-------------------------------------------------------------------------*/
34 /* Define sysfs entries for the usbip driver */
38 * busid_tables defines matching busids that usbip can grab. A user can change
39 * dynamically what device is locally used and what device is exported to a
40 * remote host.
42 #define MAX_BUSID 16
43 static char busid_table[MAX_BUSID][BUS_ID_SIZE];
44 static spinlock_t busid_table_lock;
47 int match_busid(char *busid)
49 int i;
51 spin_lock(&busid_table_lock);
53 for (i = 0; i < MAX_BUSID; i++)
54 if (busid_table[i][0])
55 if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
56 /* already registerd */
57 spin_unlock(&busid_table_lock);
58 return 0;
61 spin_unlock(&busid_table_lock);
63 return 1;
66 static ssize_t show_match_busid(struct device_driver *drv, char *buf)
68 int i;
69 char *out = buf;
71 spin_lock(&busid_table_lock);
73 for (i = 0; i < MAX_BUSID; i++)
74 if (busid_table[i][0])
75 out += sprintf(out, "%s ", busid_table[i]);
77 spin_unlock(&busid_table_lock);
79 out += sprintf(out, "\n");
81 return out - buf;
84 static int add_match_busid(char *busid)
86 int i;
88 if (!match_busid(busid))
89 return 0;
91 spin_lock(&busid_table_lock);
93 for (i = 0; i < MAX_BUSID; i++)
94 if (!busid_table[i][0]) {
95 strncpy(busid_table[i], busid, BUS_ID_SIZE);
96 spin_unlock(&busid_table_lock);
97 return 0;
100 spin_unlock(&busid_table_lock);
102 return -1;
105 static int del_match_busid(char *busid)
107 int i;
109 spin_lock(&busid_table_lock);
111 for (i = 0; i < MAX_BUSID; i++)
112 if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
113 /* found */
114 memset(busid_table[i], 0, BUS_ID_SIZE);
115 spin_unlock(&busid_table_lock);
116 return 0;
119 spin_unlock(&busid_table_lock);
121 return -1;
124 static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
125 size_t count)
127 int len;
128 char busid[BUS_ID_SIZE];
130 if (count < 5)
131 return -EINVAL;
133 /* strnlen() does not include \0 */
134 len = strnlen(buf + 4, BUS_ID_SIZE);
136 /* busid needs to include \0 termination */
137 if (!(len < BUS_ID_SIZE))
138 return -EINVAL;
140 strncpy(busid, buf + 4, BUS_ID_SIZE);
143 if (!strncmp(buf, "add ", 4)) {
144 if (add_match_busid(busid) < 0)
145 return -ENOMEM;
146 else {
147 udbg("add busid %s\n", busid);
148 return count;
150 } else if (!strncmp(buf, "del ", 4)) {
151 if (del_match_busid(busid) < 0)
152 return -ENODEV;
153 else {
154 udbg("del busid %s\n", busid);
155 return count;
157 } else
158 return -EINVAL;
161 static DRIVER_ATTR(match_busid, S_IRUSR|S_IWUSR, show_match_busid,
162 store_match_busid);
166 /*-------------------------------------------------------------------------*/
168 /* Cleanup functions used to free private data */
170 static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
172 struct stub_priv *priv, *tmp;
174 list_for_each_entry_safe(priv, tmp, listhead, list) {
175 list_del(&priv->list);
176 return priv;
179 return NULL;
182 static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
184 unsigned long flags;
185 struct stub_priv *priv;
187 spin_lock_irqsave(&sdev->priv_lock, flags);
189 priv = stub_priv_pop_from_listhead(&sdev->priv_init);
190 if (priv) {
191 spin_unlock_irqrestore(&sdev->priv_lock, flags);
192 return priv;
195 priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
196 if (priv) {
197 spin_unlock_irqrestore(&sdev->priv_lock, flags);
198 return priv;
201 priv = stub_priv_pop_from_listhead(&sdev->priv_free);
202 if (priv) {
203 spin_unlock_irqrestore(&sdev->priv_lock, flags);
204 return priv;
207 spin_unlock_irqrestore(&sdev->priv_lock, flags);
208 return NULL;
211 void stub_device_cleanup_urbs(struct stub_device *sdev)
213 struct stub_priv *priv;
215 udbg("free sdev %p\n", sdev);
217 while ((priv = stub_priv_pop(sdev))) {
218 struct urb *urb = priv->urb;
220 udbg(" free urb %p\n", urb);
221 usb_kill_urb(urb);
223 kmem_cache_free(stub_priv_cache, priv);
225 if (urb->transfer_buffer != NULL)
226 kfree(urb->transfer_buffer);
228 if (urb->setup_packet != NULL)
229 kfree(urb->setup_packet);
231 usb_free_urb(urb);
236 /*-------------------------------------------------------------------------*/
238 static int __init usb_stub_init(void)
240 int ret;
242 stub_priv_cache = kmem_cache_create("stub_priv",
243 sizeof(struct stub_priv), 0,
244 SLAB_HWCACHE_ALIGN, NULL);
246 if (!stub_priv_cache) {
247 printk(KERN_ERR KBUILD_MODNAME
248 ": create stub_priv_cache error\n");
249 return -ENOMEM;
252 ret = usb_register(&stub_driver);
253 if (ret) {
254 printk(KERN_ERR KBUILD_MODNAME ": usb_register failed %d\n",
255 ret);
256 goto error_usb_register;
259 printk(KERN_INFO KBUILD_MODNAME ":"
260 DRIVER_DESC ":" DRIVER_VERSION "\n");
262 memset(busid_table, 0, sizeof(busid_table));
263 spin_lock_init(&busid_table_lock);
265 ret = driver_create_file(&stub_driver.drvwrap.driver,
266 &driver_attr_match_busid);
268 if (ret) {
269 printk(KERN_ERR KBUILD_MODNAME ": create driver sysfs\n");
270 goto error_create_file;
273 return ret;
274 error_create_file:
275 usb_deregister(&stub_driver);
276 error_usb_register:
277 kmem_cache_destroy(stub_priv_cache);
278 return ret;
281 static void __exit usb_stub_exit(void)
283 driver_remove_file(&stub_driver.drvwrap.driver,
284 &driver_attr_match_busid);
287 * deregister() calls stub_disconnect() for all devices. Device
288 * specific data is cleared in stub_disconnect().
290 usb_deregister(&stub_driver);
292 kmem_cache_destroy(stub_priv_cache);
295 module_init(usb_stub_init);
296 module_exit(usb_stub_exit);
298 MODULE_AUTHOR(DRIVER_AUTHOR);
299 MODULE_DESCRIPTION(DRIVER_DESC);
300 MODULE_LICENSE("GPL");