PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/kernel-2.6/drivers/usbip/drivers/usb/usbip/stub_dev.c

http://wl500g.googlecode.com/
C | 543 lines | 347 code | 104 blank | 92 comment | 47 complexity | 2c5a5a214ea421d49e6690dcde875453 MD5 | raw file
Possible License(s): GPL-2.0
  1. /*
  2. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  3. *
  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.
  8. *
  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.
  13. *
  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.
  18. */
  19. #include <linux/device.h>
  20. #include <linux/file.h>
  21. #include <linux/kthread.h>
  22. #include <linux/module.h>
  23. #include "usbip_common.h"
  24. #include "stub.h"
  25. /*
  26. * Define device IDs here if you want to explicitly limit exportable devices.
  27. * In most cases, wildcard matching will be okay because driver binding can be
  28. * changed dynamically by a userland program.
  29. */
  30. static struct usb_device_id stub_table[] = {
  31. #if 0
  32. /* just an example */
  33. { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
  34. { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
  35. { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
  36. { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
  37. { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
  38. { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
  39. { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
  40. { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
  41. { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
  42. { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
  43. { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
  44. { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
  45. #endif
  46. /* magic for wild card */
  47. { .driver_info = 1 },
  48. { 0, } /* Terminating entry */
  49. };
  50. MODULE_DEVICE_TABLE(usb, stub_table);
  51. /*
  52. * usbip_status shows the status of usbip-host as long as this driver is bound
  53. * to the target device.
  54. */
  55. static ssize_t show_status(struct device *dev, struct device_attribute *attr,
  56. char *buf)
  57. {
  58. struct stub_device *sdev = dev_get_drvdata(dev);
  59. int status;
  60. if (!sdev) {
  61. dev_err(dev, "sdev is null\n");
  62. return -ENODEV;
  63. }
  64. spin_lock(&sdev->ud.lock);
  65. status = sdev->ud.status;
  66. spin_unlock(&sdev->ud.lock);
  67. return snprintf(buf, PAGE_SIZE, "%d\n", status);
  68. }
  69. static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
  70. /*
  71. * usbip_sockfd gets a socket descriptor of an established TCP connection that
  72. * is used to transfer usbip requests by kernel threads. -1 is a magic number
  73. * by which usbip connection is finished.
  74. */
  75. static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
  76. const char *buf, size_t count)
  77. {
  78. struct stub_device *sdev = dev_get_drvdata(dev);
  79. int sockfd = 0;
  80. struct socket *socket;
  81. if (!sdev) {
  82. dev_err(dev, "sdev is null\n");
  83. return -ENODEV;
  84. }
  85. sscanf(buf, "%d", &sockfd);
  86. if (sockfd != -1) {
  87. dev_info(dev, "stub up\n");
  88. spin_lock(&sdev->ud.lock);
  89. if (sdev->ud.status != SDEV_ST_AVAILABLE) {
  90. dev_err(dev, "not ready\n");
  91. spin_unlock(&sdev->ud.lock);
  92. return -EINVAL;
  93. }
  94. socket = sockfd_to_socket(sockfd);
  95. if (!socket) {
  96. spin_unlock(&sdev->ud.lock);
  97. return -EINVAL;
  98. }
  99. sdev->ud.tcp_socket = socket;
  100. spin_unlock(&sdev->ud.lock);
  101. sdev->ud.tcp_rx = kthread_run(stub_rx_loop, &sdev->ud, "stub_rx");
  102. sdev->ud.tcp_tx = kthread_run(stub_tx_loop, &sdev->ud, "stub_tx");
  103. spin_lock(&sdev->ud.lock);
  104. sdev->ud.status = SDEV_ST_USED;
  105. spin_unlock(&sdev->ud.lock);
  106. } else {
  107. dev_info(dev, "stub down\n");
  108. spin_lock(&sdev->ud.lock);
  109. if (sdev->ud.status != SDEV_ST_USED) {
  110. spin_unlock(&sdev->ud.lock);
  111. return -EINVAL;
  112. }
  113. spin_unlock(&sdev->ud.lock);
  114. usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
  115. }
  116. return count;
  117. }
  118. static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
  119. static int stub_add_files(struct device *dev)
  120. {
  121. int err = 0;
  122. err = device_create_file(dev, &dev_attr_usbip_status);
  123. if (err)
  124. goto err_status;
  125. err = device_create_file(dev, &dev_attr_usbip_sockfd);
  126. if (err)
  127. goto err_sockfd;
  128. err = device_create_file(dev, &dev_attr_usbip_debug);
  129. if (err)
  130. goto err_debug;
  131. return 0;
  132. err_debug:
  133. device_remove_file(dev, &dev_attr_usbip_sockfd);
  134. err_sockfd:
  135. device_remove_file(dev, &dev_attr_usbip_status);
  136. err_status:
  137. return err;
  138. }
  139. static void stub_remove_files(struct device *dev)
  140. {
  141. device_remove_file(dev, &dev_attr_usbip_status);
  142. device_remove_file(dev, &dev_attr_usbip_sockfd);
  143. device_remove_file(dev, &dev_attr_usbip_debug);
  144. }
  145. static void stub_shutdown_connection(struct usbip_device *ud)
  146. {
  147. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  148. /*
  149. * When removing an exported device, kernel panic sometimes occurred
  150. * and then EIP was sk_wait_data of stub_rx thread. Is this because
  151. * sk_wait_data returned though stub_rx thread was already finished by
  152. * step 1?
  153. */
  154. if (ud->tcp_socket) {
  155. dev_dbg(&sdev->udev->dev, "shutdown tcp_socket %p\n",
  156. ud->tcp_socket);
  157. ud->tcp_socket->ops->shutdown(ud->tcp_socket, SEND_SHUTDOWN|RCV_SHUTDOWN);
  158. }
  159. /* 1. stop threads */
  160. if (ud->tcp_rx && !task_is_dead(ud->tcp_rx)) {
  161. force_sig(SIGKILL, ud->tcp_rx);
  162. kthread_stop(ud->tcp_rx);
  163. ud->tcp_rx = NULL;
  164. }
  165. if (ud->tcp_tx && !task_is_dead(ud->tcp_tx)) {
  166. kthread_stop(ud->tcp_tx);
  167. ud->tcp_tx = NULL;
  168. }
  169. /* 2. close the socket */
  170. /*
  171. * tcp_socket is freed after threads are killed.
  172. * So usbip_xmit do not touch NULL socket.
  173. */
  174. if (ud->tcp_socket) {
  175. fput(ud->tcp_socket->file);
  176. ud->tcp_socket = NULL;
  177. }
  178. /* 3. free used data */
  179. stub_device_cleanup_urbs(sdev);
  180. /* 4. free stub_unlink */
  181. {
  182. unsigned long flags;
  183. struct stub_unlink *unlink, *tmp;
  184. spin_lock_irqsave(&sdev->priv_lock, flags);
  185. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
  186. list_del(&unlink->list);
  187. kfree(unlink);
  188. }
  189. list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free,
  190. list) {
  191. list_del(&unlink->list);
  192. kfree(unlink);
  193. }
  194. spin_unlock_irqrestore(&sdev->priv_lock, flags);
  195. }
  196. }
  197. static void stub_device_reset(struct usbip_device *ud)
  198. {
  199. struct stub_device *sdev = container_of(ud, struct stub_device, ud);
  200. struct usb_device *udev = sdev->udev;
  201. int ret;
  202. dev_dbg(&udev->dev, "device reset\n");
  203. ret = usb_lock_device_for_reset(udev, sdev->interface);
  204. if (ret < 0) {
  205. dev_err(&udev->dev, "lock for reset\n");
  206. spin_lock(&ud->lock);
  207. ud->status = SDEV_ST_ERROR;
  208. spin_unlock(&ud->lock);
  209. return;
  210. }
  211. /* try to reset the device */
  212. ret = usb_reset_device(udev);
  213. usb_unlock_device(udev);
  214. spin_lock(&ud->lock);
  215. if (ret) {
  216. dev_err(&udev->dev, "device reset\n");
  217. ud->status = SDEV_ST_ERROR;
  218. } else {
  219. dev_info(&udev->dev, "device reset\n");
  220. ud->status = SDEV_ST_AVAILABLE;
  221. }
  222. spin_unlock(&ud->lock);
  223. }
  224. static void stub_device_unusable(struct usbip_device *ud)
  225. {
  226. spin_lock(&ud->lock);
  227. ud->status = SDEV_ST_ERROR;
  228. spin_unlock(&ud->lock);
  229. }
  230. /**
  231. * stub_device_alloc - allocate a new stub_device struct
  232. * @interface: usb_interface of a new device
  233. *
  234. * Allocates and initializes a new stub_device struct.
  235. */
  236. static struct stub_device *stub_device_alloc(struct usb_device *udev,
  237. struct usb_interface *interface)
  238. {
  239. struct stub_device *sdev;
  240. int busnum = interface_to_busnum(interface);
  241. int devnum = interface_to_devnum(interface);
  242. dev_dbg(&interface->dev, "allocating stub device\n");
  243. /* yes, it's a new device */
  244. sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
  245. if (!sdev) {
  246. dev_err(&interface->dev, "no memory for stub_device\n");
  247. return NULL;
  248. }
  249. sdev->interface = usb_get_intf(interface);
  250. sdev->udev = usb_get_dev(udev);
  251. /*
  252. * devid is defined with devnum when this driver is first allocated.
  253. * devnum may change later if a device is reset. However, devid never
  254. * changes during a usbip connection.
  255. */
  256. sdev->devid = (busnum << 16) | devnum;
  257. sdev->ud.side = USBIP_STUB;
  258. sdev->ud.status = SDEV_ST_AVAILABLE;
  259. spin_lock_init(&sdev->ud.lock);
  260. sdev->ud.tcp_socket = NULL;
  261. INIT_LIST_HEAD(&sdev->priv_init);
  262. INIT_LIST_HEAD(&sdev->priv_tx);
  263. INIT_LIST_HEAD(&sdev->priv_free);
  264. INIT_LIST_HEAD(&sdev->unlink_free);
  265. INIT_LIST_HEAD(&sdev->unlink_tx);
  266. spin_lock_init(&sdev->priv_lock);
  267. init_waitqueue_head(&sdev->tx_waitq);
  268. sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
  269. sdev->ud.eh_ops.reset = stub_device_reset;
  270. sdev->ud.eh_ops.unusable = stub_device_unusable;
  271. usbip_start_eh(&sdev->ud);
  272. dev_dbg(&interface->dev, "register new interface\n");
  273. return sdev;
  274. }
  275. static int stub_device_free(struct stub_device *sdev)
  276. {
  277. if (!sdev)
  278. return -EINVAL;
  279. kfree(sdev);
  280. pr_debug("kfree udev ok\n");
  281. return 0;
  282. }
  283. /*
  284. * If a usb device has multiple active interfaces, this driver is bound to all
  285. * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
  286. * active interface). Currently, a userland program must ensure that it
  287. * looks at the usbip's sysfs entries of only the first active interface.
  288. *
  289. * TODO: use "struct usb_device_driver" to bind a usb device.
  290. * However, it seems it is not fully supported in mainline kernel yet
  291. * (2.6.19.2).
  292. */
  293. static int stub_probe(struct usb_interface *interface,
  294. const struct usb_device_id *id)
  295. {
  296. struct usb_device *udev = interface_to_usbdev(interface);
  297. struct stub_device *sdev = NULL;
  298. const char *udev_busid = dev_name(interface->dev.parent);
  299. int err = 0;
  300. struct bus_id_priv *busid_priv;
  301. dev_dbg(&interface->dev, "Enter\n");
  302. /* check we should claim or not by busid_table */
  303. busid_priv = get_busid_priv(udev_busid);
  304. if (!busid_priv || (busid_priv->status == STUB_BUSID_REMOV) ||
  305. (busid_priv->status == STUB_BUSID_OTHER)) {
  306. dev_info(&interface->dev, "%s is not in match_busid table... "
  307. "skip!\n", udev_busid);
  308. /*
  309. * Return value should be ENODEV or ENOXIO to continue trying
  310. * other matched drivers by the driver core.
  311. * See driver_probe_device() in driver/base/dd.c
  312. */
  313. return -ENODEV;
  314. }
  315. if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
  316. dev_dbg(&udev->dev, "%s is a usb hub device... skip!\n",
  317. udev_busid);
  318. return -ENODEV;
  319. }
  320. if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
  321. dev_dbg(&udev->dev, "%s is attached on vhci_hcd... skip!\n",
  322. udev_busid);
  323. return -ENODEV;
  324. }
  325. if (busid_priv->status == STUB_BUSID_ALLOC) {
  326. sdev = busid_priv->sdev;
  327. if (!sdev)
  328. return -ENODEV;
  329. busid_priv->interf_count++;
  330. dev_info(&interface->dev, "usbip-host: register new interface "
  331. "(bus %u dev %u ifn %u)\n",
  332. udev->bus->busnum, udev->devnum,
  333. interface->cur_altsetting->desc.bInterfaceNumber);
  334. /* set private data to usb_interface */
  335. usb_set_intfdata(interface, sdev);
  336. err = stub_add_files(&interface->dev);
  337. if (err) {
  338. dev_err(&interface->dev, "stub_add_files for %s\n",
  339. udev_busid);
  340. usb_set_intfdata(interface, NULL);
  341. busid_priv->interf_count--;
  342. return err;
  343. }
  344. usb_get_intf(interface);
  345. return 0;
  346. }
  347. /* ok, this is my device */
  348. sdev = stub_device_alloc(udev, interface);
  349. if (!sdev)
  350. return -ENOMEM;
  351. dev_info(&interface->dev, "usbip-host: register new device "
  352. "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
  353. interface->cur_altsetting->desc.bInterfaceNumber);
  354. busid_priv->interf_count = 0;
  355. busid_priv->shutdown_busid = 0;
  356. /* set private data to usb_interface */
  357. usb_set_intfdata(interface, sdev);
  358. busid_priv->interf_count++;
  359. busid_priv->sdev = sdev;
  360. err = stub_add_files(&interface->dev);
  361. if (err) {
  362. dev_err(&interface->dev, "stub_add_files for %s\n", udev_busid);
  363. usb_set_intfdata(interface, NULL);
  364. usb_put_intf(interface);
  365. busid_priv->interf_count = 0;
  366. busid_priv->sdev = NULL;
  367. stub_device_free(sdev);
  368. return err;
  369. }
  370. busid_priv->status = STUB_BUSID_ALLOC;
  371. return 0;
  372. }
  373. static void shutdown_busid(struct bus_id_priv *busid_priv)
  374. {
  375. if (busid_priv->sdev && !busid_priv->shutdown_busid) {
  376. busid_priv->shutdown_busid = 1;
  377. usbip_event_add(&busid_priv->sdev->ud, SDEV_EVENT_REMOVED);
  378. /* 2. wait for the stop of the event handler */
  379. usbip_stop_eh(&busid_priv->sdev->ud);
  380. }
  381. }
  382. /*
  383. * called in usb_disconnect() or usb_deregister()
  384. * but only if actconfig(active configuration) exists
  385. */
  386. static void stub_disconnect(struct usb_interface *interface)
  387. {
  388. struct stub_device *sdev;
  389. const char *udev_busid = dev_name(interface->dev.parent);
  390. struct bus_id_priv *busid_priv;
  391. dev_dbg(&interface->dev, "Enter\n");
  392. busid_priv = get_busid_priv(udev_busid);
  393. if (!busid_priv) {
  394. BUG();
  395. return;
  396. }
  397. sdev = usb_get_intfdata(interface);
  398. /* get stub_device */
  399. if (!sdev) {
  400. dev_err(&interface->dev, "could not get device");
  401. return;
  402. }
  403. usb_set_intfdata(interface, NULL);
  404. /*
  405. * NOTE: rx/tx threads are invoked for each usb_device.
  406. */
  407. stub_remove_files(&interface->dev);
  408. /* If usb reset is called from event handler */
  409. if (busid_priv->sdev->ud.eh == current) {
  410. busid_priv->interf_count--;
  411. return;
  412. }
  413. if (busid_priv->interf_count > 1) {
  414. busid_priv->interf_count--;
  415. shutdown_busid(busid_priv);
  416. usb_put_intf(interface);
  417. return;
  418. }
  419. busid_priv->interf_count = 0;
  420. /* shutdown the current connection */
  421. shutdown_busid(busid_priv);
  422. usb_put_dev(sdev->udev);
  423. usb_put_intf(interface);
  424. /* free sdev */
  425. busid_priv->sdev = NULL;
  426. stub_device_free(sdev);
  427. if (busid_priv->status == STUB_BUSID_ALLOC) {
  428. busid_priv->status = STUB_BUSID_ADDED;
  429. } else {
  430. busid_priv->status = STUB_BUSID_OTHER;
  431. del_match_busid((char *)udev_busid);
  432. }
  433. }
  434. struct usb_driver stub_driver = {
  435. .name = "usbip-host",
  436. .probe = stub_probe,
  437. .disconnect = stub_disconnect,
  438. .id_table = stub_table,
  439. };