mfd: twl4030 IRQ handling update
[linux-2.6/libata-dev.git] / drivers / staging / usbip / stub_dev.c
blobee455a087eaf496152990623f8529a2a4458f2dd
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 "usbip_common.h"
21 #include "stub.h"
25 static int stub_probe(struct usb_interface *interface,
26 const struct usb_device_id *id);
27 static void stub_disconnect(struct usb_interface *interface);
31 * Define device IDs here if you want to explicitly limit exportable devices.
32 * In the most cases, wild card matching will be ok because driver binding can
33 * be changed dynamically by a userland program.
35 static struct usb_device_id stub_table[] = {
36 #if 0
37 /* just an example */
38 { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
39 { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
40 { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
41 { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
42 { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
43 { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
44 { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
45 { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
46 { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
47 { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
48 { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
49 { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
50 #endif
51 /* magic for wild card */
52 { .driver_info = 1 },
53 { 0, } /* Terminating entry */
55 MODULE_DEVICE_TABLE(usb, stub_table);
57 struct usb_driver stub_driver = {
58 .name = "usbip",
59 .probe = stub_probe,
60 .disconnect = stub_disconnect,
61 .id_table = stub_table,
65 /*-------------------------------------------------------------------------*/
67 /* Define sysfs entries for a usbip-bound device */
71 * usbip_status shows status of usbip as long as this driver is bound to the
72 * target device.
74 static ssize_t show_status(struct device *dev, struct device_attribute *attr,
75 char *buf)
77 struct stub_device *sdev = dev_get_drvdata(dev);
78 int status;
80 if (!sdev) {
81 dev_err(dev, "sdev is null\n");
82 return -ENODEV;
85 spin_lock(&sdev->ud.lock);
86 status = sdev->ud.status;
87 spin_unlock(&sdev->ud.lock);
89 return snprintf(buf, PAGE_SIZE, "%d\n", status);
91 static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
94 * usbip_sockfd gets a socket descriptor of an established TCP connection that
95 * is used to transfer usbip requests by kernel threads. -1 is a magic number
96 * by which usbip connection is finished.
98 static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
99 const char *buf, size_t count)
101 struct stub_device *sdev = dev_get_drvdata(dev);
102 int sockfd = 0;
103 struct socket *socket;
105 if (!sdev) {
106 dev_err(dev, "sdev is null\n");
107 return -ENODEV;
110 sscanf(buf, "%d", &sockfd);
112 if (sockfd != -1) {
113 dev_info(dev, "stub up\n");
115 spin_lock(&sdev->ud.lock);
117 if (sdev->ud.status != SDEV_ST_AVAILABLE) {
118 dev_err(dev, "not ready\n");
119 spin_unlock(&sdev->ud.lock);
120 return -EINVAL;
123 socket = sockfd_to_socket(sockfd);
124 if (!socket) {
125 spin_unlock(&sdev->ud.lock);
126 return -EINVAL;
129 #if 0
130 setnodelay(socket);
131 setkeepalive(socket);
132 setreuse(socket);
133 #endif
135 sdev->ud.tcp_socket = socket;
137 spin_unlock(&sdev->ud.lock);
139 usbip_start_threads(&sdev->ud);
141 spin_lock(&sdev->ud.lock);
142 sdev->ud.status = SDEV_ST_USED;
143 spin_unlock(&sdev->ud.lock);
145 } else {
146 dev_info(dev, "stub down\n");
148 spin_lock(&sdev->ud.lock);
149 if (sdev->ud.status != SDEV_ST_USED) {
150 spin_unlock(&sdev->ud.lock);
151 return -EINVAL;
153 spin_unlock(&sdev->ud.lock);
155 usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
158 return count;
160 static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
162 static int stub_add_files(struct device *dev)
164 int err = 0;
166 err = device_create_file(dev, &dev_attr_usbip_status);
167 if (err)
168 goto err_status;
170 err = device_create_file(dev, &dev_attr_usbip_sockfd);
171 if (err)
172 goto err_sockfd;
174 err = device_create_file(dev, &dev_attr_usbip_debug);
175 if (err)
176 goto err_debug;
178 return 0;
180 err_debug:
181 device_remove_file(dev, &dev_attr_usbip_sockfd);
183 err_sockfd:
184 device_remove_file(dev, &dev_attr_usbip_status);
186 err_status:
187 return err;
190 static void stub_remove_files(struct device *dev)
192 device_remove_file(dev, &dev_attr_usbip_status);
193 device_remove_file(dev, &dev_attr_usbip_sockfd);
194 device_remove_file(dev, &dev_attr_usbip_debug);
199 /*-------------------------------------------------------------------------*/
201 /* Event handler functions called by an event handler thread */
203 static void stub_shutdown_connection(struct usbip_device *ud)
205 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
208 * When removing an exported device, kernel panic sometimes occurred
209 * and then EIP was sk_wait_data of stub_rx thread. Is this because
210 * sk_wait_data returned though stub_rx thread was already finished by
211 * step 1?
213 if (ud->tcp_socket) {
214 udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
215 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
218 /* 1. stop threads */
219 usbip_stop_threads(ud);
221 /* 2. close the socket */
223 * tcp_socket is freed after threads are killed.
224 * So usbip_xmit do not touch NULL socket.
226 if (ud->tcp_socket) {
227 sock_release(ud->tcp_socket);
228 ud->tcp_socket = NULL;
231 /* 3. free used data */
232 stub_device_cleanup_urbs(sdev);
234 /* 4. free stub_unlink */
236 unsigned long flags;
237 struct stub_unlink *unlink, *tmp;
239 spin_lock_irqsave(&sdev->priv_lock, flags);
241 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
242 list_del(&unlink->list);
243 kfree(unlink);
246 list_for_each_entry_safe(unlink, tmp,
247 &sdev->unlink_free, list) {
248 list_del(&unlink->list);
249 kfree(unlink);
252 spin_unlock_irqrestore(&sdev->priv_lock, flags);
256 static void stub_device_reset(struct usbip_device *ud)
258 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
259 struct usb_device *udev = interface_to_usbdev(sdev->interface);
260 int ret;
262 udbg("device reset");
263 ret = usb_lock_device_for_reset(udev, sdev->interface);
264 if (ret < 0) {
265 dev_err(&udev->dev, "lock for reset\n");
267 spin_lock(&ud->lock);
268 ud->status = SDEV_ST_ERROR;
269 spin_unlock(&ud->lock);
271 return;
274 /* try to reset the device */
275 ret = usb_reset_device(udev);
277 usb_unlock_device(udev);
279 spin_lock(&ud->lock);
280 if (ret) {
281 dev_err(&udev->dev, "device reset\n");
282 ud->status = SDEV_ST_ERROR;
284 } else {
285 dev_info(&udev->dev, "device reset\n");
286 ud->status = SDEV_ST_AVAILABLE;
289 spin_unlock(&ud->lock);
291 return;
294 static void stub_device_unusable(struct usbip_device *ud)
296 spin_lock(&ud->lock);
297 ud->status = SDEV_ST_ERROR;
298 spin_unlock(&ud->lock);
302 /*-------------------------------------------------------------------------*/
305 * stub_device_alloc - allocate a new stub_device struct
306 * @interface: usb_interface of a new device
308 * Allocates and initializes a new stub_device struct.
310 static struct stub_device *stub_device_alloc(struct usb_interface *interface)
312 struct stub_device *sdev;
313 int busnum = interface_to_busnum(interface);
314 int devnum = interface_to_devnum(interface);
316 dev_dbg(&interface->dev, "allocating stub device");
318 /* yes, it's a new device */
319 sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
320 if (!sdev) {
321 dev_err(&interface->dev, "no memory for stub_device\n");
322 return NULL;
325 sdev->interface = interface;
328 * devid is defined with devnum when this driver is first allocated.
329 * devnum may change later if a device is reset. However, devid never
330 * changes during a usbip connection.
332 sdev->devid = (busnum << 16) | devnum;
334 usbip_task_init(&sdev->ud.tcp_rx, "stub_rx", stub_rx_loop);
335 usbip_task_init(&sdev->ud.tcp_tx, "stub_tx", stub_tx_loop);
337 sdev->ud.side = USBIP_STUB;
338 sdev->ud.status = SDEV_ST_AVAILABLE;
339 /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
340 spin_lock_init(&sdev->ud.lock);
341 sdev->ud.tcp_socket = NULL;
343 INIT_LIST_HEAD(&sdev->priv_init);
344 INIT_LIST_HEAD(&sdev->priv_tx);
345 INIT_LIST_HEAD(&sdev->priv_free);
346 INIT_LIST_HEAD(&sdev->unlink_free);
347 INIT_LIST_HEAD(&sdev->unlink_tx);
348 /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
349 spin_lock_init(&sdev->priv_lock);
351 init_waitqueue_head(&sdev->tx_waitq);
353 sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
354 sdev->ud.eh_ops.reset = stub_device_reset;
355 sdev->ud.eh_ops.unusable = stub_device_unusable;
357 usbip_start_eh(&sdev->ud);
359 udbg("register new interface\n");
360 return sdev;
363 static int stub_device_free(struct stub_device *sdev)
365 if (!sdev)
366 return -EINVAL;
368 kfree(sdev);
369 udbg("kfree udev ok\n");
371 return 0;
375 /*-------------------------------------------------------------------------*/
378 * If a usb device has multiple active interfaces, this driver is bound to all
379 * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
380 * active interface). Currently, a userland program must ensure that it
381 * looks at the usbip's sysfs entries of only the first active interface.
383 * TODO: use "struct usb_device_driver" to bind a usb device.
384 * However, it seems it is not fully supported in mainline kernel yet
385 * (2.6.19.2).
387 static int stub_probe(struct usb_interface *interface,
388 const struct usb_device_id *id)
390 struct usb_device *udev = interface_to_usbdev(interface);
391 struct stub_device *sdev = NULL;
392 char *udev_busid = interface->dev.parent->bus_id;
393 int err = 0;
395 dev_dbg(&interface->dev, "Enter\n");
397 /* check we should claim or not by busid_table */
398 if (match_busid(udev_busid)) {
399 dev_info(&interface->dev,
400 "this device %s is not in match_busid table. skip!\n",
401 udev_busid);
404 * Return value should be ENODEV or ENOXIO to continue trying
405 * other matched drivers by the driver core.
406 * See driver_probe_device() in driver/base/dd.c
408 return -ENODEV;
411 if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
412 udbg("this device %s is a usb hub device. skip!\n",
413 udev_busid);
414 return -ENODEV;
417 if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
418 udbg("this device %s is attached on vhci_hcd. skip!\n",
419 udev_busid);
420 return -ENODEV;
423 /* ok. this is my device. */
424 sdev = stub_device_alloc(interface);
425 if (!sdev)
426 return -ENOMEM;
428 dev_info(&interface->dev, "USB/IP Stub: register a new interface "
429 "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
430 interface->cur_altsetting->desc.bInterfaceNumber);
432 /* set private data to usb_interface */
433 usb_set_intfdata(interface, sdev);
435 err = stub_add_files(&interface->dev);
436 if (err) {
437 dev_err(&interface->dev, "create sysfs files for %s\n",
438 udev_busid);
439 return err;
442 return 0;
447 * called in usb_disconnect() or usb_deregister()
448 * but only if actconfig(active configuration) exists
450 static void stub_disconnect(struct usb_interface *interface)
452 struct stub_device *sdev = usb_get_intfdata(interface);
454 udbg("Enter\n");
456 /* get stub_device */
457 if (!sdev) {
458 err(" could not get device from inteface data");
459 /* BUG(); */
460 return;
463 usb_set_intfdata(interface, NULL);
467 * NOTE:
468 * rx/tx threads are invoked for each usb_device.
470 stub_remove_files(&interface->dev);
472 /* 1. shutdown the current connection */
473 usbip_event_add(&sdev->ud, SDEV_EVENT_REMOVED);
475 /* 2. wait for the stop of the event handler */
476 usbip_stop_eh(&sdev->ud);
478 /* 3. free sdev */
479 stub_device_free(sdev);
482 udbg("bye\n");