usb-skeleton.c 16 KB

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