usblcd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*****************************************************************************
  3. * USBLCD Kernel Driver *
  4. * Version 1.05 *
  5. * (C) 2005 Georges Toth <[email protected]> *
  6. * *
  7. * This file is licensed under the GPL. See COPYING in the package. *
  8. * Based on usb-skeleton.c 2.0 by Greg Kroah-Hartman ([email protected]) *
  9. * *
  10. * *
  11. * 28.02.05 Complete rewrite of the original usblcd.c driver, *
  12. * based on usb_skeleton.c. *
  13. * This new driver allows more than one USB-LCD to be connected *
  14. * and controlled, at once *
  15. *****************************************************************************/
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include <linux/errno.h>
  20. #include <linux/mutex.h>
  21. #include <linux/rwsem.h>
  22. #include <linux/uaccess.h>
  23. #include <linux/usb.h>
  24. #define DRIVER_VERSION "USBLCD Driver Version 1.05"
  25. #define USBLCD_MINOR 144
  26. #define IOCTL_GET_HARD_VERSION 1
  27. #define IOCTL_GET_DRV_VERSION 2
  28. static const struct usb_device_id id_table[] = {
  29. { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
  30. { },
  31. };
  32. MODULE_DEVICE_TABLE(usb, id_table);
  33. struct usb_lcd {
  34. struct usb_device *udev; /* init: probe_lcd */
  35. struct usb_interface *interface; /* the interface for
  36. this device */
  37. unsigned char *bulk_in_buffer; /* the buffer to receive
  38. data */
  39. size_t bulk_in_size; /* the size of the
  40. receive buffer */
  41. __u8 bulk_in_endpointAddr; /* the address of the
  42. bulk in endpoint */
  43. __u8 bulk_out_endpointAddr; /* the address of the
  44. bulk out endpoint */
  45. struct kref kref;
  46. struct semaphore limit_sem; /* to stop writes at
  47. full throttle from
  48. using up all RAM */
  49. struct usb_anchor submitted; /* URBs to wait for
  50. before suspend */
  51. struct rw_semaphore io_rwsem;
  52. unsigned long disconnected:1;
  53. };
  54. #define to_lcd_dev(d) container_of(d, struct usb_lcd, kref)
  55. #define USB_LCD_CONCURRENT_WRITES 5
  56. static struct usb_driver lcd_driver;
  57. static void lcd_delete(struct kref *kref)
  58. {
  59. struct usb_lcd *dev = to_lcd_dev(kref);
  60. usb_put_dev(dev->udev);
  61. kfree(dev->bulk_in_buffer);
  62. kfree(dev);
  63. }
  64. static int lcd_open(struct inode *inode, struct file *file)
  65. {
  66. struct usb_lcd *dev;
  67. struct usb_interface *interface;
  68. int subminor, r;
  69. subminor = iminor(inode);
  70. interface = usb_find_interface(&lcd_driver, subminor);
  71. if (!interface) {
  72. pr_err("USBLCD: %s - error, can't find device for minor %d\n",
  73. __func__, subminor);
  74. return -ENODEV;
  75. }
  76. dev = usb_get_intfdata(interface);
  77. /* increment our usage count for the device */
  78. kref_get(&dev->kref);
  79. /* grab a power reference */
  80. r = usb_autopm_get_interface(interface);
  81. if (r < 0) {
  82. kref_put(&dev->kref, lcd_delete);
  83. return r;
  84. }
  85. /* save our object in the file's private structure */
  86. file->private_data = dev;
  87. return 0;
  88. }
  89. static int lcd_release(struct inode *inode, struct file *file)
  90. {
  91. struct usb_lcd *dev;
  92. dev = file->private_data;
  93. if (dev == NULL)
  94. return -ENODEV;
  95. /* decrement the count on our device */
  96. usb_autopm_put_interface(dev->interface);
  97. kref_put(&dev->kref, lcd_delete);
  98. return 0;
  99. }
  100. static ssize_t lcd_read(struct file *file, char __user * buffer,
  101. size_t count, loff_t *ppos)
  102. {
  103. struct usb_lcd *dev;
  104. int retval = 0;
  105. int bytes_read;
  106. dev = file->private_data;
  107. down_read(&dev->io_rwsem);
  108. if (dev->disconnected) {
  109. retval = -ENODEV;
  110. goto out_up_io;
  111. }
  112. /* do a blocking bulk read to get data from the device */
  113. retval = usb_bulk_msg(dev->udev,
  114. usb_rcvbulkpipe(dev->udev,
  115. dev->bulk_in_endpointAddr),
  116. dev->bulk_in_buffer,
  117. min(dev->bulk_in_size, count),
  118. &bytes_read, 10000);
  119. /* if the read was successful, copy the data to userspace */
  120. if (!retval) {
  121. if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))
  122. retval = -EFAULT;
  123. else
  124. retval = bytes_read;
  125. }
  126. out_up_io:
  127. up_read(&dev->io_rwsem);
  128. return retval;
  129. }
  130. static long lcd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  131. {
  132. struct usb_lcd *dev;
  133. u16 bcdDevice;
  134. char buf[30];
  135. dev = file->private_data;
  136. if (dev == NULL)
  137. return -ENODEV;
  138. switch (cmd) {
  139. case IOCTL_GET_HARD_VERSION:
  140. bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice);
  141. sprintf(buf, "%1d%1d.%1d%1d",
  142. (bcdDevice & 0xF000)>>12,
  143. (bcdDevice & 0xF00)>>8,
  144. (bcdDevice & 0xF0)>>4,
  145. (bcdDevice & 0xF));
  146. if (copy_to_user((void __user *)arg, buf, strlen(buf)) != 0)
  147. return -EFAULT;
  148. break;
  149. case IOCTL_GET_DRV_VERSION:
  150. sprintf(buf, DRIVER_VERSION);
  151. if (copy_to_user((void __user *)arg, buf, strlen(buf)) != 0)
  152. return -EFAULT;
  153. break;
  154. default:
  155. return -ENOTTY;
  156. }
  157. return 0;
  158. }
  159. static void lcd_write_bulk_callback(struct urb *urb)
  160. {
  161. struct usb_lcd *dev;
  162. int status = urb->status;
  163. dev = urb->context;
  164. /* sync/async unlink faults aren't errors */
  165. if (status &&
  166. !(status == -ENOENT ||
  167. status == -ECONNRESET ||
  168. status == -ESHUTDOWN)) {
  169. dev_dbg(&dev->interface->dev,
  170. "nonzero write bulk status received: %d\n", status);
  171. }
  172. /* free up our allocated buffer */
  173. usb_free_coherent(urb->dev, urb->transfer_buffer_length,
  174. urb->transfer_buffer, urb->transfer_dma);
  175. up(&dev->limit_sem);
  176. }
  177. static ssize_t lcd_write(struct file *file, const char __user * user_buffer,
  178. size_t count, loff_t *ppos)
  179. {
  180. struct usb_lcd *dev;
  181. int retval = 0, r;
  182. struct urb *urb = NULL;
  183. char *buf = NULL;
  184. dev = file->private_data;
  185. /* verify that we actually have some data to write */
  186. if (count == 0)
  187. goto exit;
  188. r = down_interruptible(&dev->limit_sem);
  189. if (r < 0)
  190. return -EINTR;
  191. down_read(&dev->io_rwsem);
  192. if (dev->disconnected) {
  193. retval = -ENODEV;
  194. goto err_up_io;
  195. }
  196. /* create a urb, and a buffer for it, and copy the data to the urb */
  197. urb = usb_alloc_urb(0, GFP_KERNEL);
  198. if (!urb) {
  199. retval = -ENOMEM;
  200. goto err_up_io;
  201. }
  202. buf = usb_alloc_coherent(dev->udev, count, GFP_KERNEL,
  203. &urb->transfer_dma);
  204. if (!buf) {
  205. retval = -ENOMEM;
  206. goto error;
  207. }
  208. if (copy_from_user(buf, user_buffer, count)) {
  209. retval = -EFAULT;
  210. goto error;
  211. }
  212. /* initialize the urb properly */
  213. usb_fill_bulk_urb(urb, dev->udev,
  214. usb_sndbulkpipe(dev->udev,
  215. dev->bulk_out_endpointAddr),
  216. buf, count, lcd_write_bulk_callback, dev);
  217. urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  218. usb_anchor_urb(urb, &dev->submitted);
  219. /* send the data out the bulk port */
  220. retval = usb_submit_urb(urb, GFP_KERNEL);
  221. if (retval) {
  222. dev_err(&dev->udev->dev,
  223. "%s - failed submitting write urb, error %d\n",
  224. __func__, retval);
  225. goto error_unanchor;
  226. }
  227. /* release our reference to this urb,
  228. the USB core will eventually free it entirely */
  229. usb_free_urb(urb);
  230. up_read(&dev->io_rwsem);
  231. exit:
  232. return count;
  233. error_unanchor:
  234. usb_unanchor_urb(urb);
  235. error:
  236. usb_free_coherent(dev->udev, count, buf, urb->transfer_dma);
  237. usb_free_urb(urb);
  238. err_up_io:
  239. up_read(&dev->io_rwsem);
  240. up(&dev->limit_sem);
  241. return retval;
  242. }
  243. static const struct file_operations lcd_fops = {
  244. .owner = THIS_MODULE,
  245. .read = lcd_read,
  246. .write = lcd_write,
  247. .open = lcd_open,
  248. .unlocked_ioctl = lcd_ioctl,
  249. .release = lcd_release,
  250. .llseek = noop_llseek,
  251. };
  252. /*
  253. * usb class driver info in order to get a minor number from the usb core,
  254. * and to have the device registered with the driver core
  255. */
  256. static struct usb_class_driver lcd_class = {
  257. .name = "lcd%d",
  258. .fops = &lcd_fops,
  259. .minor_base = USBLCD_MINOR,
  260. };
  261. static int lcd_probe(struct usb_interface *interface,
  262. const struct usb_device_id *id)
  263. {
  264. struct usb_lcd *dev = NULL;
  265. struct usb_endpoint_descriptor *bulk_in, *bulk_out;
  266. int i;
  267. int retval;
  268. /* allocate memory for our device state and initialize it */
  269. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  270. if (!dev)
  271. return -ENOMEM;
  272. kref_init(&dev->kref);
  273. sema_init(&dev->limit_sem, USB_LCD_CONCURRENT_WRITES);
  274. init_rwsem(&dev->io_rwsem);
  275. init_usb_anchor(&dev->submitted);
  276. dev->udev = usb_get_dev(interface_to_usbdev(interface));
  277. dev->interface = interface;
  278. if (le16_to_cpu(dev->udev->descriptor.idProduct) != 0x0001) {
  279. dev_warn(&interface->dev, "USBLCD model not supported.\n");
  280. retval = -ENODEV;
  281. goto error;
  282. }
  283. /* set up the endpoint information */
  284. /* use only the first bulk-in and bulk-out endpoints */
  285. retval = usb_find_common_endpoints(interface->cur_altsetting,
  286. &bulk_in, &bulk_out, NULL, NULL);
  287. if (retval) {
  288. dev_err(&interface->dev,
  289. "Could not find both bulk-in and bulk-out endpoints\n");
  290. goto error;
  291. }
  292. dev->bulk_in_size = usb_endpoint_maxp(bulk_in);
  293. dev->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
  294. dev->bulk_in_buffer = kmalloc(dev->bulk_in_size, GFP_KERNEL);
  295. if (!dev->bulk_in_buffer) {
  296. retval = -ENOMEM;
  297. goto error;
  298. }
  299. dev->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
  300. /* save our data pointer in this interface device */
  301. usb_set_intfdata(interface, dev);
  302. /* we can register the device now, as it is ready */
  303. retval = usb_register_dev(interface, &lcd_class);
  304. if (retval) {
  305. /* something prevented us from registering this driver */
  306. dev_err(&interface->dev,
  307. "Not able to get a minor for this device.\n");
  308. goto error;
  309. }
  310. i = le16_to_cpu(dev->udev->descriptor.bcdDevice);
  311. dev_info(&interface->dev, "USBLCD Version %1d%1d.%1d%1d found "
  312. "at address %d\n", (i & 0xF000)>>12, (i & 0xF00)>>8,
  313. (i & 0xF0)>>4, (i & 0xF), dev->udev->devnum);
  314. /* let the user know what node this device is now attached to */
  315. dev_info(&interface->dev, "USB LCD device now attached to USBLCD-%d\n",
  316. interface->minor);
  317. return 0;
  318. error:
  319. kref_put(&dev->kref, lcd_delete);
  320. return retval;
  321. }
  322. static void lcd_draw_down(struct usb_lcd *dev)
  323. {
  324. int time;
  325. time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
  326. if (!time)
  327. usb_kill_anchored_urbs(&dev->submitted);
  328. }
  329. static int lcd_suspend(struct usb_interface *intf, pm_message_t message)
  330. {
  331. struct usb_lcd *dev = usb_get_intfdata(intf);
  332. if (!dev)
  333. return 0;
  334. lcd_draw_down(dev);
  335. return 0;
  336. }
  337. static int lcd_resume(struct usb_interface *intf)
  338. {
  339. return 0;
  340. }
  341. static void lcd_disconnect(struct usb_interface *interface)
  342. {
  343. struct usb_lcd *dev = usb_get_intfdata(interface);
  344. int minor = interface->minor;
  345. /* give back our minor */
  346. usb_deregister_dev(interface, &lcd_class);
  347. down_write(&dev->io_rwsem);
  348. dev->disconnected = 1;
  349. up_write(&dev->io_rwsem);
  350. usb_kill_anchored_urbs(&dev->submitted);
  351. /* decrement our usage count */
  352. kref_put(&dev->kref, lcd_delete);
  353. dev_info(&interface->dev, "USB LCD #%d now disconnected\n", minor);
  354. }
  355. static struct usb_driver lcd_driver = {
  356. .name = "usblcd",
  357. .probe = lcd_probe,
  358. .disconnect = lcd_disconnect,
  359. .suspend = lcd_suspend,
  360. .resume = lcd_resume,
  361. .id_table = id_table,
  362. .supports_autosuspend = 1,
  363. };
  364. module_usb_driver(lcd_driver);
  365. MODULE_AUTHOR("Georges Toth <[email protected]>");
  366. MODULE_DESCRIPTION(DRIVER_VERSION);
  367. MODULE_LICENSE("GPL");