usb-skeleton.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  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. err("%s - error, can't find device for minor %d",
  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. err("%s - nonzero write bulk status received: %d",
  150. __func__, urb->status);
  151. dev->errors = urb->status;
  152. } else {
  153. dev->bulk_in_filled = urb->actual_length;
  154. }
  155. dev->ongoing_read = 0;
  156. spin_unlock(&dev->err_lock);
  157. complete(&dev->bulk_in_completion);
  158. }
  159. static int skel_do_read_io(struct usb_skel *dev, size_t count)
  160. {
  161. int rv;
  162. /* prepare a read */
  163. usb_fill_bulk_urb(dev->bulk_in_urb,
  164. dev->udev,
  165. usb_rcvbulkpipe(dev->udev,
  166. dev->bulk_in_endpointAddr),
  167. dev->bulk_in_buffer,
  168. min(dev->bulk_in_size, count),
  169. skel_read_bulk_callback,
  170. dev);
  171. /* tell everybody to leave the URB alone */
  172. spin_lock_irq(&dev->err_lock);
  173. dev->ongoing_read = 1;
  174. spin_unlock_irq(&dev->err_lock);
  175. /* do it */
  176. rv = usb_submit_urb(dev->bulk_in_urb, GFP_KERNEL);
  177. if (rv < 0) {
  178. err("%s - failed submitting read urb, error %d",
  179. __func__, rv);
  180. dev->bulk_in_filled = 0;
  181. rv = (rv == -ENOMEM) ? rv : -EIO;
  182. spin_lock_irq(&dev->err_lock);
  183. dev->ongoing_read = 0;
  184. spin_unlock_irq(&dev->err_lock);
  185. }
  186. return rv;
  187. }
  188. static ssize_t skel_read(struct file *file, char *buffer, size_t count,
  189. loff_t *ppos)
  190. {
  191. struct usb_skel *dev;
  192. int rv;
  193. bool ongoing_io;
  194. dev = file->private_data;
  195. /* if we cannot read at all, return EOF */
  196. if (!dev->bulk_in_urb || !count)
  197. return 0;
  198. /* no concurrent readers */
  199. rv = mutex_lock_interruptible(&dev->io_mutex);
  200. if (rv < 0)
  201. return rv;
  202. if (!dev->interface) { /* disconnect() was called */
  203. rv = -ENODEV;
  204. goto exit;
  205. }
  206. /* if IO is under way, we must not touch things */
  207. retry:
  208. spin_lock_irq(&dev->err_lock);
  209. ongoing_io = dev->ongoing_read;
  210. spin_unlock_irq(&dev->err_lock);
  211. if (ongoing_io) {
  212. /* nonblocking IO shall not wait */
  213. if (file->f_flags & O_NONBLOCK) {
  214. rv = -EAGAIN;
  215. goto exit;
  216. }
  217. /*
  218. * IO may take forever
  219. * hence wait in an interruptible state
  220. */
  221. rv = wait_for_completion_interruptible(&dev->bulk_in_completion);
  222. if (rv < 0)
  223. goto exit;
  224. /*
  225. * by waiting we also semiprocessed the urb
  226. * we must finish now
  227. */
  228. dev->bulk_in_copied = 0;
  229. dev->processed_urb = 1;
  230. }
  231. if (!dev->processed_urb) {
  232. /*
  233. * the URB hasn't been processed
  234. * do it now
  235. */
  236. wait_for_completion(&dev->bulk_in_completion);
  237. dev->bulk_in_copied = 0;
  238. dev->processed_urb = 1;
  239. }
  240. /* errors must be reported */
  241. rv = dev->errors;
  242. if (rv < 0) {
  243. /* any error is reported once */
  244. dev->errors = 0;
  245. /* to preserve notifications about reset */
  246. rv = (rv == -EPIPE) ? rv : -EIO;
  247. /* no data to deliver */
  248. dev->bulk_in_filled = 0;
  249. /* report it */
  250. goto exit;
  251. }
  252. /*
  253. * if the buffer is filled we may satisfy the read
  254. * else we need to start IO
  255. */
  256. if (dev->bulk_in_filled) {
  257. /* we had read data */
  258. size_t available = dev->bulk_in_filled - dev->bulk_in_copied;
  259. size_t chunk = min(available, count);
  260. if (!available) {
  261. /*
  262. * all data has been used
  263. * actual IO needs to be done
  264. */
  265. rv = skel_do_read_io(dev, count);
  266. if (rv < 0)
  267. goto exit;
  268. else
  269. goto retry;
  270. }
  271. /*
  272. * data is available
  273. * chunk tells us how much shall be copied
  274. */
  275. if (copy_to_user(buffer,
  276. dev->bulk_in_buffer + dev->bulk_in_copied,
  277. chunk))
  278. rv = -EFAULT;
  279. else
  280. rv = chunk;
  281. dev->bulk_in_copied += chunk;
  282. /*
  283. * if we are asked for more than we have,
  284. * we start IO but don't wait
  285. */
  286. if (available < count)
  287. skel_do_read_io(dev, count - chunk);
  288. } else {
  289. /* no data in the buffer */
  290. rv = skel_do_read_io(dev, count);
  291. if (rv < 0)
  292. goto exit;
  293. else if (!(file->f_flags & O_NONBLOCK))
  294. goto retry;
  295. rv = -EAGAIN;
  296. }
  297. exit:
  298. mutex_unlock(&dev->io_mutex);
  299. return rv;
  300. }
  301. static void skel_write_bulk_callback(struct urb *urb)
  302. {
  303. struct usb_skel *dev;
  304. dev = urb->context;
  305. /* sync/async unlink faults aren't errors */
  306. if (urb->status) {
  307. if (!(urb->status == -ENOENT ||
  308. urb->status == -ECONNRESET ||
  309. urb->status == -ESHUTDOWN))
  310. err("%s - nonzero write bulk status received: %d",
  311. __func__, urb->status);
  312. spin_lock(&dev->err_lock);
  313. dev->errors = urb->status;
  314. spin_unlock(&dev->err_lock);
  315. }
  316. /* free up our allocated buffer */
  317. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  318. urb->transfer_buffer, urb->transfer_dma);
  319. up(&dev->limit_sem);
  320. }
  321. static ssize_t skel_write(struct file *file, const char *user_buffer,
  322. size_t count, loff_t *ppos)
  323. {
  324. struct usb_skel *dev;
  325. int retval = 0;
  326. struct urb *urb = NULL;
  327. char *buf = NULL;
  328. size_t writesize = min(count, (size_t)MAX_TRANSFER);
  329. dev = file->private_data;
  330. /* verify that we actually have some data to write */
  331. if (count == 0)
  332. goto exit;
  333. /*
  334. * limit the number of URBs in flight to stop a user from using up all
  335. * RAM
  336. */
  337. if (!(file->f_flags & O_NONBLOCK)) {
  338. if (down_interruptible(&dev->limit_sem)) {
  339. retval = -ERESTARTSYS;
  340. goto exit;
  341. }
  342. } else {
  343. if (down_trylock(&dev->limit_sem)) {
  344. retval = -EAGAIN;
  345. goto exit;
  346. }
  347. }
  348. spin_lock_irq(&dev->err_lock);
  349. retval = dev->errors;
  350. if (retval < 0) {
  351. /* any error is reported once */
  352. dev->errors = 0;
  353. /* to preserve notifications about reset */
  354. retval = (retval == -EPIPE) ? retval : -EIO;
  355. }
  356. spin_unlock_irq(&dev->err_lock);
  357. if (retval < 0)
  358. goto error;
  359. /* create a urb, and a buffer for it, and copy the data to the urb */
  360. urb = usb_alloc_urb(0, GFP_KERNEL);
  361. if (!urb) {
  362. retval = -ENOMEM;
  363. goto error;
  364. }
  365. buf = usb_alloc_coherent(dev->udev, writesize, GFP_KERNEL,
  366. &urb->transfer_dma);
  367. if (!buf) {
  368. retval = -ENOMEM;
  369. goto error;
  370. }
  371. if (copy_from_user(buf, user_buffer, writesize)) {
  372. retval = -EFAULT;
  373. goto error;
  374. }
  375. /* this lock makes sure we don't submit URBs to gone devices */
  376. mutex_lock(&dev->io_mutex);
  377. if (!dev->interface) { /* disconnect() was called */
  378. mutex_unlock(&dev->io_mutex);
  379. retval = -ENODEV;
  380. goto error;
  381. }
  382. /* initialize the urb properly */
  383. usb_fill_bulk_urb(urb, dev->udev,
  384. usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
  385. buf, writesize, skel_write_bulk_callback, dev);
  386. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  387. usb_anchor_urb(urb, &dev->submitted);
  388. /* send the data out the bulk port */
  389. retval = usb_submit_urb(urb, GFP_KERNEL);
  390. mutex_unlock(&dev->io_mutex);
  391. if (retval) {
  392. err("%s - failed submitting write urb, error %d", __func__,
  393. 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. err("Out of memory");
  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. err("Could not allocate bulk_in_buffer");
  468. goto error;
  469. }
  470. dev->bulk_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  471. if (!dev->bulk_in_urb) {
  472. err("Could not allocate bulk_in_urb");
  473. goto error;
  474. }
  475. }
  476. if (!dev->bulk_out_endpointAddr &&
  477. usb_endpoint_is_bulk_out(endpoint)) {
  478. /* we found a bulk out endpoint */
  479. dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
  480. }
  481. }
  482. if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
  483. err("Could not find both bulk-in and bulk-out endpoints");
  484. goto error;
  485. }
  486. /* save our data pointer in this interface device */
  487. usb_set_intfdata(interface, dev);
  488. /* we can register the device now, as it is ready */
  489. retval = usb_register_dev(interface, &skel_class);
  490. if (retval) {
  491. /* something prevented us from registering this driver */
  492. err("Not able to get a minor for this device.");
  493. usb_set_intfdata(interface, NULL);
  494. goto error;
  495. }
  496. /* let the user know what node this device is now attached to */
  497. dev_info(&interface->dev,
  498. "USB Skeleton device now attached to USBSkel-%d",
  499. interface->minor);
  500. return 0;
  501. error:
  502. if (dev)
  503. /* this frees allocated memory */
  504. kref_put(&dev->kref, skel_delete);
  505. return retval;
  506. }
  507. static void skel_disconnect(struct usb_interface *interface)
  508. {
  509. struct usb_skel *dev;
  510. int minor = interface->minor;
  511. dev = usb_get_intfdata(interface);
  512. usb_set_intfdata(interface, NULL);
  513. /* give back our minor */
  514. usb_deregister_dev(interface, &skel_class);
  515. /* prevent more I/O from starting */
  516. mutex_lock(&dev->io_mutex);
  517. dev->interface = NULL;
  518. mutex_unlock(&dev->io_mutex);
  519. usb_kill_anchored_urbs(&dev->submitted);
  520. /* decrement our usage count */
  521. kref_put(&dev->kref, skel_delete);
  522. dev_info(&interface->dev, "USB Skeleton #%d now disconnected", minor);
  523. }
  524. static void skel_draw_down(struct usb_skel *dev)
  525. {
  526. int time;
  527. time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
  528. if (!time)
  529. usb_kill_anchored_urbs(&dev->submitted);
  530. usb_kill_urb(dev->bulk_in_urb);
  531. }
  532. static int skel_suspend(struct usb_interface *intf, pm_message_t message)
  533. {
  534. struct usb_skel *dev = usb_get_intfdata(intf);
  535. if (!dev)
  536. return 0;
  537. skel_draw_down(dev);
  538. return 0;
  539. }
  540. static int skel_resume(struct usb_interface *intf)
  541. {
  542. return 0;
  543. }
  544. static int skel_pre_reset(struct usb_interface *intf)
  545. {
  546. struct usb_skel *dev = usb_get_intfdata(intf);
  547. mutex_lock(&dev->io_mutex);
  548. skel_draw_down(dev);
  549. return 0;
  550. }
  551. static int skel_post_reset(struct usb_interface *intf)
  552. {
  553. struct usb_skel *dev = usb_get_intfdata(intf);
  554. /* we are sure no URBs are active - no locking needed */
  555. dev->errors = -EPIPE;
  556. mutex_unlock(&dev->io_mutex);
  557. return 0;
  558. }
  559. static struct usb_driver skel_driver = {
  560. .name = "skeleton",
  561. .probe = skel_probe,
  562. .disconnect = skel_disconnect,
  563. .suspend = skel_suspend,
  564. .resume = skel_resume,
  565. .pre_reset = skel_pre_reset,
  566. .post_reset = skel_post_reset,
  567. .id_table = skel_table,
  568. .supports_autosuspend = 1,
  569. };
  570. module_usb_driver(skel_driver);
  571. MODULE_LICENSE("GPL");