usblcd.c 12 KB

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