usblcd.c 12 KB

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