usb-skeleton.c 16 KB

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