usb-skeleton.c 16 KB

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