usb-skeleton.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  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 if (!(file->f_flags & O_NONBLOCK))
  276. goto retry;
  277. rv = -EAGAIN;
  278. }
  279. exit:
  280. mutex_unlock(&dev->io_mutex);
  281. return rv;
  282. }
  283. static void skel_write_bulk_callback(struct urb *urb)
  284. {
  285. struct usb_skel *dev;
  286. dev = urb->context;
  287. /* sync/async unlink faults aren't errors */
  288. if (urb->status) {
  289. if (!(urb->status == -ENOENT ||
  290. urb->status == -ECONNRESET ||
  291. urb->status == -ESHUTDOWN))
  292. dev_err(&dev->interface->dev,
  293. "%s - nonzero write bulk status received: %d\n",
  294. __func__, urb->status);
  295. spin_lock(&dev->err_lock);
  296. dev->errors = urb->status;
  297. spin_unlock(&dev->err_lock);
  298. }
  299. /* free up our allocated buffer */
  300. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  301. urb->transfer_buffer, urb->transfer_dma);
  302. up(&dev->limit_sem);
  303. }
  304. static ssize_t skel_write(struct file *file, const char *user_buffer,
  305. size_t count, loff_t *ppos)
  306. {
  307. struct usb_skel *dev;
  308. int retval = 0;
  309. struct urb *urb = NULL;
  310. char *buf = NULL;
  311. size_t writesize = min(count, (size_t)MAX_TRANSFER);
  312. dev = file->private_data;
  313. /* verify that we actually have some data to write */
  314. if (count == 0)
  315. goto exit;
  316. /*
  317. * limit the number of URBs in flight to stop a user from using up all
  318. * RAM
  319. */
  320. if (!(file->f_flags & O_NONBLOCK)) {
  321. if (down_interruptible(&dev->limit_sem)) {
  322. retval = -ERESTARTSYS;
  323. goto exit;
  324. }
  325. } else {
  326. if (down_trylock(&dev->limit_sem)) {
  327. retval = -EAGAIN;
  328. goto exit;
  329. }
  330. }
  331. spin_lock_irq(&dev->err_lock);
  332. retval = dev->errors;
  333. if (retval < 0) {
  334. /* any error is reported once */
  335. dev->errors = 0;
  336. /* to preserve notifications about reset */
  337. retval = (retval == -EPIPE) ? retval : -EIO;
  338. }
  339. spin_unlock_irq(&dev->err_lock);
  340. if (retval < 0)
  341. goto error;
  342. /* create a urb, and a buffer for it, and copy the data to the urb */
  343. urb = usb_alloc_urb(0, GFP_KERNEL);
  344. if (!urb) {
  345. retval = -ENOMEM;
  346. goto error;
  347. }
  348. buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL,
  349. &urb->transfer_dma);
  350. if (!buf) {
  351. retval = -ENOMEM;
  352. goto error;
  353. }
  354. if (copy_from_user(buf, user_buffer, writesize)) {
  355. retval = -EFAULT;
  356. goto error;
  357. }
  358. /* this lock makes sure we don't submit URBs to gone devices */
  359. mutex_lock(&dev->io_mutex);
  360. if (!dev->interface) { /* disconnect() was called */
  361. mutex_unlock(&dev->io_mutex);
  362. retval = -ENODEV;
  363. goto error;
  364. }
  365. /* initialize the urb properly */
  366. usb_fill_bulk_urb(urb, dev->udev,
  367. usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
  368. buf, writesize, skel_write_bulk_callback, dev);
  369. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  370. usb_anchor_urb(urb, &dev->submitted);
  371. /* send the data out the bulk port */
  372. retval = usb_submit_urb(urb, GFP_KERNEL);
  373. mutex_unlock(&dev->io_mutex);
  374. if (retval) {
  375. dev_err(&dev->interface->dev,
  376. "%s - failed submitting write urb, error %d\n",
  377. __func__, retval);
  378. goto error_unanchor;
  379. }
  380. /*
  381. * release our reference to this urb, the USB core will eventually free
  382. * it entirely
  383. */
  384. usb_free_urb(urb);
  385. return writesize;
  386. error_unanchor:
  387. usb_unanchor_urb(urb);
  388. error:
  389. if (urb) {
  390. usb_free_coherent(dev->udev, writesize, buf, urb->transfer_dma);
  391. usb_free_urb(urb);
  392. }
  393. up(&dev->limit_sem);
  394. exit:
  395. return retval;
  396. }
  397. static const struct file_operations skel_fops = {
  398. .owner = THIS_MODULE,
  399. .read = skel_read,
  400. .write = skel_write,
  401. .open = skel_open,
  402. .release = skel_release,
  403. .flush = skel_flush,
  404. .llseek = noop_llseek,
  405. };
  406. /*
  407. * usb class driver info in order to get a minor number from the usb core,
  408. * and to have the device registered with the driver core
  409. */
  410. static struct usb_class_driver skel_class = {
  411. .name = "skel%d",
  412. .fops = &skel_fops,
  413. .minor_base = USB_SKEL_MINOR_BASE,
  414. };
  415. static int skel_probe(struct usb_interface *interface,
  416. const struct usb_device_id *id)
  417. {
  418. struct usb_skel *dev;
  419. struct usb_host_interface *iface_desc;
  420. struct usb_endpoint_descriptor *endpoint;
  421. size_t buffer_size;
  422. int i;
  423. int retval = -ENOMEM;
  424. /* allocate memory for our device state and initialize it */
  425. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  426. if (!dev) {
  427. dev_err(&interface->dev, "Out of memory\n");
  428. goto error;
  429. }
  430. kref_init(&dev->kref);
  431. sema_init(&dev->limit_sem, WRITES_IN_FLIGHT);
  432. mutex_init(&dev->io_mutex);
  433. spin_lock_init(&dev->err_lock);
  434. init_usb_anchor(&dev->submitted);
  435. init_waitqueue_head(&dev->bulk_in_wait);
  436. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  437. dev->interface = interface;
  438. /* set up the endpoint information */
  439. /* use only the first bulk-in and bulk-out endpoints */
  440. iface_desc = interface->cur_altsetting;
  441. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  442. endpoint = &iface_desc->endpoint[i].desc;
  443. if (!dev->bulk_in_endpointAddr &&
  444. usb_endpoint_is_bulk_in(endpoint)) {
  445. /* we found a bulk in endpoint */
  446. buffer_size = usb_endpoint_maxp(endpoint);
  447. dev->bulk_in_size = buffer_size;
  448. dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
  449. dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
  450. if (!dev->bulk_in_buffer) {
  451. dev_err(&interface->dev,
  452. "Could not allocate bulk_in_buffer\n");
  453. goto error;
  454. }
  455. dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  456. if (!dev->bulk_in_urb) {
  457. dev_err(&interface->dev,
  458. "Could not allocate bulk_in_urb\n");
  459. goto error;
  460. }
  461. }
  462. if (!dev->bulk_out_endpointAddr &&
  463. usb_endpoint_is_bulk_out(endpoint)) {
  464. /* we found a bulk out endpoint */
  465. dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
  466. }
  467. }
  468. if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
  469. dev_err(&interface->dev,
  470. "Could not find both bulk-in and bulk-out endpoints\n");
  471. goto error;
  472. }
  473. /* save our data pointer in this interface device */
  474. usb_set_intfdata(interface, dev);
  475. /* we can register the device now, as it is ready */
  476. retval = usb_register_dev(interface, &skel_class);
  477. if (retval) {
  478. /* something prevented us from registering this driver */
  479. dev_err(&interface->dev,
  480. "Not able to get a minor for this device.\n");
  481. usb_set_intfdata(interface, NULL);
  482. goto error;
  483. }
  484. /* let the user know what node this device is now attached to */
  485. dev_info(&interface->dev,
  486. "USB Skeleton device now attached to USBSkel-%d",
  487. interface->minor);
  488. return 0;
  489. error:
  490. if (dev)
  491. /* this frees allocated memory */
  492. kref_put(&dev->kref, skel_delete);
  493. return retval;
  494. }
  495. static void skel_disconnect(struct usb_interface *interface)
  496. {
  497. struct usb_skel *dev;
  498. int minor = interface->minor;
  499. dev = usb_get_intfdata(interface);
  500. usb_set_intfdata(interface, NULL);
  501. /* give back our minor */
  502. usb_deregister_dev(interface, &skel_class);
  503. /* prevent more I/O from starting */
  504. mutex_lock(&dev->io_mutex);
  505. dev->interface = NULL;
  506. mutex_unlock(&dev->io_mutex);
  507. usb_kill_anchored_urbs(&dev->submitted);
  508. /* decrement our usage count */
  509. kref_put(&dev->kref, skel_delete);
  510. dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor);
  511. }
  512. static void skel_draw_down(struct usb_skel *dev)
  513. {
  514. int time;
  515. time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
  516. if (!time)
  517. usb_kill_anchored_urbs(&dev->submitted);
  518. usb_kill_urb(dev->bulk_in_urb);
  519. }
  520. static int skel_suspend(struct usb_interface *intf, pm_message_t message)
  521. {
  522. struct usb_skel *dev = usb_get_intfdata(intf);
  523. if (!dev)
  524. return 0;
  525. skel_draw_down(dev);
  526. return 0;
  527. }
  528. static int skel_resume(struct usb_interface *intf)
  529. {
  530. return 0;
  531. }
  532. static int skel_pre_reset(struct usb_interface *intf)
  533. {
  534. struct usb_skel *dev = usb_get_intfdata(intf);
  535. mutex_lock(&dev->io_mutex);
  536. skel_draw_down(dev);
  537. return 0;
  538. }
  539. static int skel_post_reset(struct usb_interface *intf)
  540. {
  541. struct usb_skel *dev = usb_get_intfdata(intf);
  542. /* we are sure no URBs are active - no locking needed */
  543. dev->errors = -EPIPE;
  544. mutex_unlock(&dev->io_mutex);
  545. return 0;
  546. }
  547. static struct usb_driver skel_driver = {
  548. .name = "skeleton",
  549. .probe = skel_probe,
  550. .disconnect = skel_disconnect,
  551. .suspend = skel_suspend,
  552. .resume = skel_resume,
  553. .pre_reset = skel_pre_reset,
  554. .post_reset = skel_post_reset,
  555. .id_table = skel_table,
  556. .supports_autosuspend = 1,
  557. };
  558. module_usb_driver(skel_driver);
  559. MODULE_LICENSE("GPL");