usblcd.c 12 KB

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