usblcd.c 12 KB

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