usb-skeleton.c 16 KB

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