usb-skeleton.c 17 KB

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