staging: usbip: fixup MODULE_ macros
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / usbip / stub_main.c
blobc139f0e25d8520bc4771cc466ed0049b6eb9f9ae
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.
20 #include <linux/slab.h>
22 #include "usbip_common.h"
23 #include "stub.h"
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;
33 * busid_tables defines matching busids that usbip can grab. A user can change
34 * dynamically what device is locally used and what device is exported to a
35 * remote host.
37 #define MAX_BUSID 16
38 static struct bus_id_priv busid_table[MAX_BUSID];
39 static spinlock_t busid_table_lock;
41 int match_busid(const char *busid)
43 int i;
45 spin_lock(&busid_table_lock);
47 for (i = 0; i < MAX_BUSID; i++)
48 if (busid_table[i].name[0])
49 if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
50 /* already registerd */
51 spin_unlock(&busid_table_lock);
52 return 0;
55 spin_unlock(&busid_table_lock);
57 return 1;
60 struct bus_id_priv *get_busid_priv(const char *busid)
62 int i;
64 spin_lock(&busid_table_lock);
66 for (i = 0; i < MAX_BUSID; i++)
67 if (busid_table[i].name[0])
68 if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
69 /* already registerd */
70 spin_unlock(&busid_table_lock);
71 return &(busid_table[i]);
74 spin_unlock(&busid_table_lock);
76 return NULL;
79 static ssize_t show_match_busid(struct device_driver *drv, char *buf)
81 int i;
82 char *out = buf;
84 spin_lock(&busid_table_lock);
86 for (i = 0; i < MAX_BUSID; i++)
87 if (busid_table[i].name[0])
88 out += sprintf(out, "%s ", busid_table[i].name);
90 spin_unlock(&busid_table_lock);
92 out += sprintf(out, "\n");
94 return out - buf;
97 static int add_match_busid(char *busid)
99 int i;
101 if (!match_busid(busid))
102 return 0;
104 spin_lock(&busid_table_lock);
106 for (i = 0; i < MAX_BUSID; i++)
107 if (!busid_table[i].name[0]) {
108 strncpy(busid_table[i].name, busid, BUSID_SIZE);
109 if ((busid_table[i].status != STUB_BUSID_ALLOC) &&
110 (busid_table[i].status != STUB_BUSID_REMOV))
111 busid_table[i].status = STUB_BUSID_ADDED;
112 spin_unlock(&busid_table_lock);
113 return 0;
116 spin_unlock(&busid_table_lock);
118 return -1;
121 int del_match_busid(char *busid)
123 int i;
125 spin_lock(&busid_table_lock);
127 for (i = 0; i < MAX_BUSID; i++)
128 if (!strncmp(busid_table[i].name, busid, BUSID_SIZE)) {
129 /* found */
130 if (busid_table[i].status == STUB_BUSID_OTHER)
131 memset(busid_table[i].name, 0, BUSID_SIZE);
132 if ((busid_table[i].status != STUB_BUSID_OTHER) &&
133 (busid_table[i].status != STUB_BUSID_ADDED)) {
134 busid_table[i].status = STUB_BUSID_REMOV;
136 spin_unlock(&busid_table_lock);
137 return 0;
140 spin_unlock(&busid_table_lock);
142 return -1;
145 static void init_busid_table(void)
147 int i;
149 for (i = 0; i < MAX_BUSID; i++) {
150 memset(busid_table[i].name, 0, BUSID_SIZE);
151 busid_table[i].status = STUB_BUSID_OTHER;
152 busid_table[i].interf_count = 0;
153 busid_table[i].sdev = NULL;
154 busid_table[i].shutdown_busid = 0;
157 spin_lock_init(&busid_table_lock);
160 static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
161 size_t count)
163 int len;
164 char busid[BUSID_SIZE];
166 if (count < 5)
167 return -EINVAL;
169 /* strnlen() does not include \0 */
170 len = strnlen(buf + 4, BUSID_SIZE);
172 /* busid needs to include \0 termination */
173 if (!(len < BUSID_SIZE))
174 return -EINVAL;
176 strncpy(busid, buf + 4, BUSID_SIZE);
178 if (!strncmp(buf, "add ", 4)) {
179 if (add_match_busid(busid) < 0)
180 return -ENOMEM;
181 else {
182 usbip_udbg("add busid %s\n", busid);
183 return count;
185 } else if (!strncmp(buf, "del ", 4)) {
186 if (del_match_busid(busid) < 0)
187 return -ENODEV;
188 else {
189 usbip_udbg("del busid %s\n", busid);
190 return count;
192 } else
193 return -EINVAL;
195 static DRIVER_ATTR(match_busid, S_IRUSR|S_IWUSR, show_match_busid,
196 store_match_busid);
198 static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
200 struct stub_priv *priv, *tmp;
202 list_for_each_entry_safe(priv, tmp, listhead, list) {
203 list_del(&priv->list);
204 return priv;
207 return NULL;
210 static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
212 unsigned long flags;
213 struct stub_priv *priv;
215 spin_lock_irqsave(&sdev->priv_lock, flags);
217 priv = stub_priv_pop_from_listhead(&sdev->priv_init);
218 if (priv) {
219 spin_unlock_irqrestore(&sdev->priv_lock, flags);
220 return priv;
223 priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
224 if (priv) {
225 spin_unlock_irqrestore(&sdev->priv_lock, flags);
226 return priv;
229 priv = stub_priv_pop_from_listhead(&sdev->priv_free);
230 if (priv) {
231 spin_unlock_irqrestore(&sdev->priv_lock, flags);
232 return priv;
235 spin_unlock_irqrestore(&sdev->priv_lock, flags);
236 return NULL;
239 void stub_device_cleanup_urbs(struct stub_device *sdev)
241 struct stub_priv *priv;
243 usbip_udbg("free sdev %p\n", sdev);
245 while ((priv = stub_priv_pop(sdev))) {
246 struct urb *urb = priv->urb;
248 usbip_udbg(" free urb %p\n", urb);
249 usb_kill_urb(urb);
251 kmem_cache_free(stub_priv_cache, priv);
253 kfree(urb->transfer_buffer);
254 kfree(urb->setup_packet);
256 usb_free_urb(urb);
260 static int __init usb_stub_init(void)
262 int ret;
264 stub_priv_cache = kmem_cache_create("stub_priv",
265 sizeof(struct stub_priv), 0,
266 SLAB_HWCACHE_ALIGN, NULL);
268 if (!stub_priv_cache) {
269 printk(KERN_ERR KBUILD_MODNAME
270 ": create stub_priv_cache error\n");
271 return -ENOMEM;
274 ret = usb_register(&stub_driver);
275 if (ret) {
276 printk(KERN_ERR KBUILD_MODNAME ": usb_register failed %d\n",
277 ret);
278 goto error_usb_register;
281 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC " " DRIVER_VERSION
282 "\n");
284 init_busid_table();
286 ret = driver_create_file(&stub_driver.drvwrap.driver,
287 &driver_attr_match_busid);
289 if (ret) {
290 printk(KERN_ERR KBUILD_MODNAME ": create driver sysfs\n");
291 goto error_create_file;
294 return ret;
295 error_create_file:
296 usb_deregister(&stub_driver);
297 error_usb_register:
298 kmem_cache_destroy(stub_priv_cache);
299 return ret;
302 static void __exit usb_stub_exit(void)
304 driver_remove_file(&stub_driver.drvwrap.driver,
305 &driver_attr_match_busid);
308 * deregister() calls stub_disconnect() for all devices. Device
309 * specific data is cleared in stub_disconnect().
311 usb_deregister(&stub_driver);
313 kmem_cache_destroy(stub_priv_cache);
316 module_init(usb_stub_init);
317 module_exit(usb_stub_exit);
319 MODULE_AUTHOR(DRIVER_AUTHOR);
320 MODULE_DESCRIPTION(DRIVER_DESC);
321 MODULE_LICENSE("GPL");
322 MODULE_VERSION(DRIVER_VERSION);