usb-skeleton.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * USB Skeleton driver - 2.2
  3. *
  4. * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation, version 2.
  9. *
  10. * This driver is based on the 2.6.3 version of drivers/usb/usb-skeleton.c
  11. * but has been rewritten to be easier to read and use.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/init.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/kref.h>
  20. #include <linux/uaccess.h>
  21. #include <linux/usb.h>
  22. #include <linux/mutex.h>
  23. /* Define these values to match your devices */
  24. #define USB_SKEL_VENDOR_ID 0xfff0
  25. #define USB_SKEL_PRODUCT_ID 0xfff0
  26. /* table of devices that work with this driver */
  27. static const struct usb_device_id skel_table[] = {
  28. { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },
  29. { } /* Terminating entry */
  30. };
  31. MODULE_DEVICE_TABLE(usb, skel_table);
  32. /* Get a minor range for your devices from the usb maintainer */
  33. #define USB_SKEL_MINOR_BASE 192
  34. /* our private defines. if this grows any larger, use your own .h file */
  35. #define MAX_TRANSFER (PAGE_SIZE - 512)
  36. /* MAX_TRANSFER is chosen so that the VM is not stressed by
  37. allocations > PAGE_SIZE and the number of packets in a page
  38. is an integer 512 is the largest possible packet on EHCI */
  39. #define WRITES_IN_FLIGHT 8
  40. /* arbitrarily chosen */
  41. /* Structure to hold all of our device specific stuff */
  42. struct usb_skel {
  43. struct usb_device *udev; /* the usb device for this device */
  44. struct usb_interface *interface; /* the interface for this device */
  45. struct semaphore limit_sem; /* limiting the number of writes in progress */
  46. struct usb_anchor submitted; /* in case we need to retract our submissions */
  47. struct urb *bulk_in_urb; /* the urb to read data with */
  48. unsigned char *bulk_in_buffer; /* the buffer to receive data */
  49. size_t bulk_in_size; /* the size of the receive buffer */
  50. size_t bulk_in_filled; /* number of bytes in the buffer */
  51. size_t bulk_in_copied; /* already copied to user space */
  52. __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
  53. __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
  54. int errors; /* the last request tanked */
  55. bool ongoing_read; /* a read is going on */
  56. spinlock_t err_lock; /* lock for errors */
  57. struct kref kref;
  58. struct mutex io_mutex; /* synchronize I/O with disconnect */
  59. wait_queue_head_t bulk_in_wait; /* to wait for an ongoing read */
  60. };
  61. #define to_skel_dev(d) container_of(d, struct usb_skel, kref)
  62. static struct usb_driver skel_driver;
  63. static void skel_draw_down(struct usb_skel *dev);
  64. static void skel_delete(struct kref *kref)
  65. {
  66. struct usb_skel *dev = to_skel_dev(kref);
  67. usb_free_urb(dev->bulk_in_urb);
  68. usb_put_dev(dev->udev);
  69. kfree(dev->bulk_in_buffer);
  70. kfree(dev);
  71. }
  72. static int skel_open(struct inode *inode, struct file *file)
  73. {
  74. struct usb_skel *dev;
  75. struct usb_interface *interface;
  76. int subminor;
  77. int retval = 0;
  78. subminor = iminor(inode);
  79. interface = usb_find_interface(&skel_driver, subminor);
  80. if (!interface) {
  81. pr_err("%s - error, can't find device for minor %d\n",
  82. __func__, subminor);
  83. retval = -ENODEV;
  84. goto exit;
  85. }
  86. dev = usb_get_intfdata(interface);
  87. if (!dev) {
  88. retval = -ENODEV;
  89. goto exit;
  90. }
  91. retval = usb_autopm_get_interface(interface);
  92. if (retval)
  93. goto exit;
  94. /* increment our usage count for the device */
  95. kref_get(&dev->kref);
  96. /* save our object in the file's private structure */
  97. file->private_data = dev;
  98. exit:
  99. return retval;
  100. }
  101. static int skel_release(struct inode *inode, struct file *file)
  102. {
  103. struct usb_skel *dev;
  104. dev = file->private_data;
  105. if (dev == NULL)
  106. return -ENODEV;
  107. /* allow the device to be autosuspended */
  108. mutex_lock(&dev->io_mutex);
  109. if (dev->interface)
  110. usb_autopm_put_interface(dev->interface);
  111. mutex_unlock(&dev->io_mutex);
  112. /* decrement the count on our device */
  113. kref_put(&dev->kref, skel_delete);
  114. return 0;
  115. }
  116. static int skel_flush(struct file *file, fl_owner_t id)
  117. {
  118. struct usb_skel *dev;
  119. int res;
  120. dev = file->private_data;
  121. if (dev == NULL)
  122. return -ENODEV;
  123. /* wait for io to stop */
  124. mutex_lock(&dev->io_mutex);
  125. skel_draw_down(dev);
  126. /* read out errors, leave subsequent opens a clean slate */
  127. spin_lock_irq(&dev->err_lock);
  128. res = dev->errors ? (dev->errors == -EPIPE ? -EPIPE : -EIO) : 0;
  129. dev->errors = 0;
  130. spin_unlock_irq(&dev->err_lock);
  131. mutex_unlock(&dev->io_mutex);
  132. return res;
  133. }
  134. static void skel_read_bulk_callback(struct urb *urb)
  135. {
  136. struct usb_skel *dev;
  137. dev = urb->context;
  138. spin_lock(&dev->err_lock);
  139. /* sync/async unlink faults aren't errors */
  140. if (urb->status) {
  141. if (!(urb->status == -ENOENT ||
  142. urb->status == -ECONNRESET ||
  143. urb->status == -ESHUTDOWN))
  144. dev_err(&dev->interface->dev,
  145. "%s - nonzero write bulk status received: %d\n",
  146. __func__, urb->status);
  147. dev->errors = urb->status;
  148. } else {
  149. dev->bulk_in_filled = urb->actual_length;
  150. }
  151. dev->ongoing_read = 0;
  152. spin_unlock(&dev->err_lock);
  153. wake_up_interruptible(&dev->bulk_in_wait);
  154. }
  155. static int skel_do_read_io(struct usb_skel *dev, size_t count)
  156. {
  157. int rv;
  158. /* prepare a read */
  159. usb_fill_bulk_urb(dev->bulk_in_urb,
  160. dev->udev,
  161. usb_rcvbulkpipe(dev->udev,
  162. dev->bulk_in_endpointAddr),
  163. dev->bulk_in_buffer,
  164. min(dev->bulk_in_size, count),
  165. skel_read_bulk_callback,
  166. dev);
  167. /* tell everybody to leave the URB alone */
  168. spin_lock_irq(&dev->err_lock);
  169. dev->ongoing_read = 1;
  170. spin_unlock_irq(&dev->err_lock);
  171. /* submit bulk in urb, which means no data to deliver */
  172. dev->bulk_in_filled = 0;
  173. dev->bulk_in_copied = 0;
  174. /* do it */
  175. rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL);
  176. if (rv < 0) {
  177. dev_err(&dev->interface->dev,
  178. "%s - failed submitting read urb, error %d\n",
  179. __func__, rv);
  180. rv = (rv == -ENOMEM) ? rv : -EIO;
  181. spin_lock_irq(&dev->err_lock);
  182. dev->ongoing_read = 0;
  183. spin_unlock_irq(&dev->err_lock);
  184. }
  185. return rv;
  186. }
  187. static ssize_t skel_read(struct file *file, char *buffer, size_t count,
  188. loff_t *ppos)
  189. {
  190. struct usb_skel *dev;
  191. int rv;
  192. bool ongoing_io;
  193. dev = file->private_data;
  194. /* if we cannot read at all, return EOF */
  195. if (!dev->bulk_in_urb || !count)
  196. return 0;
  197. /* no concurrent readers */
  198. rv = mutex_lock_interruptible(&dev->io_mutex);
  199. if (rv < 0)
  200. return rv;
  201. if (!dev->interface) { /* disconnect() was called */
  202. rv = -ENODEV;
  203. goto exit;
  204. }
  205. /* if IO is under way, we must not touch things */
  206. retry:
  207. spin_lock_irq(&dev->err_lock);
  208. ongoing_io = dev->ongoing_read;
  209. spin_unlock_irq(&dev->err_lock);
  210. if (ongoing_io) {
  211. /* nonblocking IO shall not wait */
  212. if (file->f_flags & O_NONBLOCK) {
  213. rv = -EAGAIN;
  214. goto exit;
  215. }
  216. /*
  217. * IO may take forever
  218. * hence wait in an interruptible state
  219. */
  220. rv = wait_event_interruptible(dev->bulk_in_wait, (!dev->ongoing_read));
  221. if (rv < 0)
  222. goto exit;
  223. }
  224. /* errors must be reported */
  225. rv = dev->errors;
  226. if (rv < 0) {
  227. /* any error is reported once */
  228. dev->errors = 0;
  229. /* to preserve notifications about reset */
  230. rv = (rv == -EPIPE) ? rv : -EIO;
  231. /* report it */
  232. goto exit;
  233. }
  234. /*
  235. * if the buffer is filled we may satisfy the read
  236. * else we need to start IO
  237. */
  238. if (dev->bulk_in_filled) {
  239. /* we had read data */
  240. size_t available = dev->bulk_in_filled - dev->bulk_in_copied;
  241. size_t chunk = min(available, count);
  242. if (!available) {
  243. /*
  244. * all data has been used
  245. * actual IO needs to be done
  246. */
  247. rv = skel_do_read_io(dev, count);
  248. if (rv < 0)
  249. goto exit;
  250. else
  251. goto retry;
  252. }
  253. /*
  254. * data is available
  255. * chunk tells us how much shall be copied
  256. */
  257. if (copy_to_user(buffer,
  258. dev->bulk_in_buffer + dev->bulk_in_copied,
  259. chunk))
  260. rv = -EFAULT;
  261. else
  262. rv = chunk;
  263. dev->bulk_in_copied += chunk;
  264. /*
  265. * if we are asked for more than we have,
  266. * we start IO but don't wait
  267. */
  268. if (available < count)
  269. skel_do_read_io(dev, count - chunk);
  270. } else {
  271. /* no data in the buffer */
  272. rv = skel_do_read_io(dev, count);
  273. if (rv < 0)
  274. goto exit;
  275. else
  276. goto retry;
  277. }
  278. exit:
  279. mutex_unlock(&dev->io_mutex);
  280. return rv;
  281. }
  282. static void skel_write_bulk_callback(struct urb *urb)
  283. {
  284. struct usb_skel *dev;
  285. dev = urb->context;
  286. /* sync/async unlink faults aren't errors */
  287. if (urb->status) {
  288. if (!(urb->status == -ENOENT ||
  289. urb->status == -ECONNRESET ||
  290. urb->status == -ESHUTDOWN))
  291. dev_err(&dev->interface->dev,
  292. "%s - nonzero write bulk status received: %d\n",
  293. __func__, urb->status);
  294. spin_lock(&dev->err_lock);
  295. dev->errors = urb->status;
  296. spin_unlock(&dev->err_lock);
  297. }
  298. /* free up our allocated buffer */
  299. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  300. urb->transfer_buffer, urb->transfer_dma);
  301. up(&dev->limit_sem);
  302. }
  303. static ssize_t skel_write(struct file *file, const char *user_buffer,
  304. size_t count, loff_t *ppos)
  305. {
  306. struct usb_skel *dev;
  307. int retval = 0;
  308. struct urb *urb = NULL;
  309. char *buf = NULL;
  310. size_t writesize = min(count, (size_t)MAX_TRANSFER);
  311. dev = file->private_data;
  312. /* verify that we actually have some data to write */
  313. if (count == 0)
  314. goto exit;
  315. /*
  316. * limit the number of URBs in flight to stop a user from using up all
  317. * RAM
  318. */
  319. if (!(file->f_flags & O_NONBLOCK)) {
  320. if (down_interruptible(&dev->limit_sem)) {
  321. retval = -ERESTARTSYS;
  322. goto exit;
  323. }
  324. } else {
  325. if (down_trylock(&dev->limit_sem)) {
  326. retval = -EAGAIN;
  327. goto exit;
  328. }
  329. }
  330. spin_lock_irq(&dev->err_lock);
  331. retval = dev->errors;
  332. if (retval < 0) {
  333. /* any error is reported once */
  334. dev->errors = 0;
  335. /* to preserve notifications about reset */
  336. retval = (retval == -EPIPE) ? retval : -EIO;
  337. }
  338. spin_unlock_irq(&dev->err_lock);
  339. if (retval < 0)
  340. goto error;
  341. /* create a urb, and a buffer for it, and copy the data to the urb */
  342. urb = usb_alloc_urb(0, GFP_KERNEL);
  343. if (!urb) {
  344. retval = -ENOMEM;
  345. goto error;
  346. }
  347. buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL,
  348. &urb->transfer_dma);
  349. if (!buf) {
  350. retval = -ENOMEM;
  351. goto error;
  352. }
  353. if (copy_from_user(buf, user_buffer, writesize)) {
  354. retval = -EFAULT;
  355. goto error;
  356. }
  357. /* this lock makes sure we don't submit URBs to gone devices */
  358. mutex_lock(&dev->io_mutex);
  359. if (!dev->interface) { /* disconnect() was called */
  360. mutex_unlock(&dev->io_mutex);
  361. retval = -ENODEV;
  362. goto error;
  363. }
  364. /* initialize the urb properly */
  365. usb_fill_bulk_urb(urb, dev->udev,
  366. usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
  367. buf, writesize, skel_write_bulk_callback, dev);
  368. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  369. usb_anchor_urb(urb, &dev->submitted);
  370. /* send the data out the bulk port */
  371. retval = usb_submit_urb(urb, GFP_KERNEL);
  372. mutex_unlock(&dev->io_mutex);
  373. if (retval) {
  374. dev_err(&dev->interface->dev,
  375. "%s - failed submitting write urb, error %d\n",
  376. __func__, retval);
  377. goto error_unanchor;
  378. }
  379. /*
  380. * release our reference to this urb, the USB core will eventually free
  381. * it entirely
  382. */
  383. usb_free_urb(urb);
  384. return writesize;
  385. error_unanchor:
  386. usb_unanchor_urb(urb);
  387. error:
  388. if (urb) {
  389. usb_free_coherent(dev->udev, writesize, buf, urb->transfer_dma);
  390. usb_free_urb(urb);
  391. }
  392. up(&dev->limit_sem);
  393. exit:
  394. return retval;
  395. }
  396. static const struct file_operations skel_fops = {
  397. .owner = THIS_MODULE,
  398. .read = skel_read,
  399. .write = skel_write,
  400. .open = skel_open,
  401. .release = skel_release,
  402. .flush = skel_flush,
  403. .llseek = noop_llseek,
  404. };
  405. /*
  406. * usb class driver info in order to get a minor number from the usb core,
  407. * and to have the device registered with the driver core
  408. */
  409. static struct usb_class_driver skel_class = {
  410. .name = "skel%d",
  411. .fops = &skel_fops,
  412. .minor_base = USB_SKEL_MINOR_BASE,
  413. };
  414. static int skel_probe(struct usb_interface *interface,
  415. const struct usb_device_id *id)
  416. {
  417. struct usb_skel *dev;
  418. struct usb_host_interface *iface_desc;
  419. struct usb_endpoint_descriptor *endpoint;
  420. size_t buffer_size;
  421. int i;
  422. int retval = -ENOMEM;
  423. /* allocate memory for our device state and initialize it */
  424. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  425. if (!dev) {
  426. dev_err(&interface->dev, "Out of memory\n");
  427. goto error;
  428. }
  429. kref_init(&dev->kref);
  430. sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
  431. mutex_init(&dev->io_mutex);
  432. spin_lock_init(&dev->err_lock);
  433. init_usb_anchor(&dev->submitted);
  434. init_waitqueue_head(&dev->bulk_in_wait);
  435. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  436. dev->interface = interface;
  437. /* set up the endpoint information */
  438. /* use only the first bulk-in and bulk-out endpoints */
  439. iface_desc = interface->cur_altsetting;
  440. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  441. endpoint = &iface_desc->endpoint[i].desc;
  442. if (!dev->bulk_in_endpointAddr &&
  443. usb_endpoint_is_bulk_in(endpoint)) {
  444. /* we found a bulk in endpoint */
  445. buffer_size = usb_endpoint_maxp(endpoint);
  446. dev->bulk_in_size = buffer_size;
  447. dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
  448. dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
  449. if (!dev->bulk_in_buffer) {
  450. dev_err(&interface->dev,
  451. "Could not allocate bulk_in_buffer\n");
  452. goto error;
  453. }
  454. dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  455. if (!dev->bulk_in_urb) {
  456. dev_err(&interface->dev,
  457. "Could not allocate bulk_in_urb\n");
  458. goto error;
  459. }
  460. }
  461. if (!dev->bulk_out_endpointAddr &&
  462. usb_endpoint_is_bulk_out(endpoint)) {
  463. /* we found a bulk out endpoint */
  464. dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
  465. }
  466. }
  467. if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
  468. dev_err(&interface->dev,
  469. "Could not find both bulk-in and bulk-out endpoints\n");
  470. goto error;
  471. }
  472. /* save our data pointer in this interface device */
  473. usb_set_intfdata(interface, dev);
  474. /* we can register the device now, as it is ready */
  475. retval = usb_register_dev(interface, &skel_class);
  476. if (retval) {
  477. /* something prevented us from registering this driver */
  478. dev_err(&interface->dev,
  479. "Not able to get a minor for this device.\n");
  480. usb_set_intfdata(interface, NULL);
  481. goto error;
  482. }
  483. /* let the user know what node this device is now attached to */
  484. dev_info(&interface->dev,
  485. "USB Skeleton device now attached to USBSkel-%d",
  486. interface->minor);
  487. return 0;
  488. error:
  489. if (dev)
  490. /* this frees allocated memory */
  491. kref_put(&dev->kref, skel_delete);
  492. return retval;
  493. }
  494. static void skel_disconnect(struct usb_interface *interface)
  495. {
  496. struct usb_skel *dev;
  497. int minor = interface->minor;
  498. dev = usb_get_intfdata(interface);
  499. usb_set_intfdata(interface, NULL);
  500. /* give back our minor */
  501. usb_deregister_dev(interface, &skel_class);
  502. /* prevent more I/O from starting */
  503. mutex_lock(&dev->io_mutex);
  504. dev->interface = NULL;
  505. mutex_unlock(&dev->io_mutex);
  506. usb_kill_anchored_urbs(&dev->submitted);
  507. /* decrement our usage count */
  508. kref_put(&dev->kref, skel_delete);
  509. dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor);
  510. }
  511. static void skel_draw_down(struct usb_skel *dev)
  512. {
  513. int time;
  514. time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
  515. if (!time)
  516. usb_kill_anchored_urbs(&dev->submitted);
  517. usb_kill_urb(dev->bulk_in_urb);
  518. }
  519. static int skel_suspend(struct usb_interface *intf, pm_message_t message)
  520. {
  521. struct usb_skel *dev = usb_get_intfdata(intf);
  522. if (!dev)
  523. return 0;
  524. skel_draw_down(dev);
  525. return 0;
  526. }
  527. static int skel_resume(struct usb_interface *intf)
  528. {
  529. return 0;
  530. }
  531. static int skel_pre_reset(struct usb_interface *intf)
  532. {
  533. struct usb_skel *dev = usb_get_intfdata(intf);
  534. mutex_lock(&dev->io_mutex);
  535. skel_draw_down(dev);
  536. return 0;
  537. }
  538. static int skel_post_reset(struct usb_interface *intf)
  539. {
  540. struct usb_skel *dev = usb_get_intfdata(intf);
  541. /* we are sure no URBs are active - no locking needed */
  542. dev->errors = -EPIPE;
  543. mutex_unlock(&dev->io_mutex);
  544. return 0;
  545. }
  546. static struct usb_driver skel_driver = {
  547. .name = "skeleton",
  548. .probe = skel_probe,
  549. .disconnect = skel_disconnect,
  550. .suspend = skel_suspend,
  551. .resume = skel_resume,
  552. .pre_reset = skel_pre_reset,
  553. .post_reset = skel_post_reset,
  554. .id_table = skel_table,
  555. .supports_autosuspend = 1,
  556. };
  557. module_usb_driver(skel_driver);
  558. MODULE_LICENSE("GPL");