1 // SPDX-License-Identifier: GPL-2.0
2 /*****************************************************************************
3 * USBLCD Kernel Driver *
5 * (C) 2005 Georges Toth <g.toth@e-biz.lu> *
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) *
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>
25 #define DRIVER_VERSION "USBLCD Driver Version 1.05"
27 #define USBLCD_MINOR 144
29 #define IOCTL_GET_HARD_VERSION 1
30 #define IOCTL_GET_DRV_VERSION 2
33 static DEFINE_MUTEX(lcd_mutex);
34 static const struct usb_device_id id_table[] = {
35 { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
38 MODULE_DEVICE_TABLE(usb, id_table);
40 static DEFINE_MUTEX(open_disc_mutex);
44 struct usb_device *udev; /* init: probe_lcd */
45 struct usb_interface *interface; /* the interface for
47 unsigned char *bulk_in_buffer; /* the buffer to receive
49 size_t bulk_in_size; /* the size of the
51 __u8 bulk_in_endpointAddr; /* the address of the
53 __u8 bulk_out_endpointAddr; /* the address of the
56 struct semaphore limit_sem; /* to stop writes at
59 struct usb_anchor submitted; /* URBs to wait for
61 struct rw_semaphore io_rwsem;
62 unsigned long disconnected:1;
64 #define to_lcd_dev(d) container_of(d, struct usb_lcd, kref)
66 #define USB_LCD_CONCURRENT_WRITES 5
68 static struct usb_driver lcd_driver;
71 static void lcd_delete(struct kref *kref)
73 struct usb_lcd *dev = to_lcd_dev(kref);
75 usb_put_dev(dev->udev);
76 kfree(dev->bulk_in_buffer);
81 static int lcd_open(struct inode *inode, struct file *file)
84 struct usb_interface *interface;
87 mutex_lock(&lcd_mutex);
88 subminor = iminor(inode);
90 interface = usb_find_interface(&lcd_driver, subminor);
92 mutex_unlock(&lcd_mutex);
93 printk(KERN_ERR "USBLCD: %s - error, can't find device for minor %d\n",
98 mutex_lock(&open_disc_mutex);
99 dev = usb_get_intfdata(interface);
101 mutex_unlock(&open_disc_mutex);
102 mutex_unlock(&lcd_mutex);
106 /* increment our usage count for the device */
107 kref_get(&dev->kref);
108 mutex_unlock(&open_disc_mutex);
110 /* grab a power reference */
111 r = usb_autopm_get_interface(interface);
113 kref_put(&dev->kref, lcd_delete);
114 mutex_unlock(&lcd_mutex);
118 /* save our object in the file's private structure */
119 file->private_data = dev;
120 mutex_unlock(&lcd_mutex);
125 static int lcd_release(struct inode *inode, struct file *file)
129 dev = file->private_data;
133 /* decrement the count on our device */
134 usb_autopm_put_interface(dev->interface);
135 kref_put(&dev->kref, lcd_delete);
139 static ssize_t lcd_read(struct file *file, char __user * buffer,
140 size_t count, loff_t *ppos)
146 dev = file->private_data;
148 down_read(&dev->io_rwsem);
150 if (dev->disconnected) {
155 /* do a blocking bulk read to get data from the device */
156 retval = usb_bulk_msg(dev->udev,
157 usb_rcvbulkpipe(dev->udev,
158 dev->bulk_in_endpointAddr),
160 min(dev->bulk_in_size, count),
163 /* if the read was successful, copy the data to userspace */
165 if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))
172 up_read(&dev->io_rwsem);
177 static long lcd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
183 dev = file->private_data;
188 case IOCTL_GET_HARD_VERSION:
189 mutex_lock(&lcd_mutex);
190 bcdDevice = le16_to_cpu((dev->udev)->descriptor.bcdDevice);
191 sprintf(buf, "%1d%1d.%1d%1d",
192 (bcdDevice & 0xF000)>>12,
193 (bcdDevice & 0xF00)>>8,
194 (bcdDevice & 0xF0)>>4,
196 mutex_unlock(&lcd_mutex);
197 if (copy_to_user((void __user *)arg, buf, strlen(buf)) != 0)
200 case IOCTL_GET_DRV_VERSION:
201 sprintf(buf, DRIVER_VERSION);
202 if (copy_to_user((void __user *)arg, buf, strlen(buf)) != 0)
213 static void lcd_write_bulk_callback(struct urb *urb)
216 int status = urb->status;
220 /* sync/async unlink faults aren't errors */
222 !(status == -ENOENT ||
223 status == -ECONNRESET ||
224 status == -ESHUTDOWN)) {
225 dev_dbg(&dev->interface->dev,
226 "nonzero write bulk status received: %d\n", status);
229 /* free up our allocated buffer */
230 usb_free_coherent(urb->dev, urb->transfer_buffer_length,
231 urb->transfer_buffer, urb->transfer_dma);
235 static ssize_t lcd_write(struct file *file, const char __user * user_buffer,
236 size_t count, loff_t *ppos)
240 struct urb *urb = NULL;
243 dev = file->private_data;
245 /* verify that we actually have some data to write */
249 r = down_interruptible(&dev->limit_sem);
253 down_read(&dev->io_rwsem);
255 if (dev->disconnected) {
260 /* create a urb, and a buffer for it, and copy the data to the urb */
261 urb = usb_alloc_urb(0, GFP_KERNEL);
267 buf = usb_alloc_coherent(dev->udev, count, GFP_KERNEL,
274 if (copy_from_user(buf, user_buffer, count)) {
279 /* initialize the urb properly */
280 usb_fill_bulk_urb(urb, dev->udev,
281 usb_sndbulkpipe(dev->udev,
282 dev->bulk_out_endpointAddr),
283 buf, count, lcd_write_bulk_callback, dev);
284 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
286 usb_anchor_urb(urb, &dev->submitted);
288 /* send the data out the bulk port */
289 retval = usb_submit_urb(urb, GFP_KERNEL);
291 dev_err(&dev->udev->dev,
292 "%s - failed submitting write urb, error %d\n",
297 /* release our reference to this urb,
298 the USB core will eventually free it entirely */
301 up_read(&dev->io_rwsem);
305 usb_unanchor_urb(urb);
307 usb_free_coherent(dev->udev, count, buf, urb->transfer_dma);
310 up_read(&dev->io_rwsem);
315 static const struct file_operations lcd_fops = {
316 .owner = THIS_MODULE,
320 .unlocked_ioctl = lcd_ioctl,
321 .release = lcd_release,
322 .llseek = noop_llseek,
326 * usb class driver info in order to get a minor number from the usb core,
327 * and to have the device registered with the driver core
329 static struct usb_class_driver lcd_class = {
332 .minor_base = USBLCD_MINOR,
335 static int lcd_probe(struct usb_interface *interface,
336 const struct usb_device_id *id)
338 struct usb_lcd *dev = NULL;
339 struct usb_endpoint_descriptor *bulk_in, *bulk_out;
343 /* allocate memory for our device state and initialize it */
344 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
348 kref_init(&dev->kref);
349 sema_init(&dev->limit_sem, USB_LCD_CONCURRENT_WRITES);
350 init_rwsem(&dev->io_rwsem);
351 init_usb_anchor(&dev->submitted);
353 dev->udev = usb_get_dev(interface_to_usbdev(interface));
354 dev->interface = interface;
356 if (le16_to_cpu(dev->udev->descriptor.idProduct) != 0x0001) {
357 dev_warn(&interface->dev, "USBLCD model not supported.\n");
362 /* set up the endpoint information */
363 /* use only the first bulk-in and bulk-out endpoints */
364 retval = usb_find_common_endpoints(interface->cur_altsetting,
365 &bulk_in, &bulk_out, NULL, NULL);
367 dev_err(&interface->dev,
368 "Could not find both bulk-in and bulk-out endpoints\n");
372 dev->bulk_in_size = usb_endpoint_maxp(bulk_in);
373 dev->bulk_in_endpointAddr = bulk_in->bEndpointAddress;
374 dev->bulk_in_buffer = kmalloc(dev->bulk_in_size, GFP_KERNEL);
375 if (!dev->bulk_in_buffer) {
380 dev->bulk_out_endpointAddr = bulk_out->bEndpointAddress;
382 /* save our data pointer in this interface device */
383 usb_set_intfdata(interface, dev);
385 /* we can register the device now, as it is ready */
386 retval = usb_register_dev(interface, &lcd_class);
388 /* something prevented us from registering this driver */
389 dev_err(&interface->dev,
390 "Not able to get a minor for this device.\n");
391 usb_set_intfdata(interface, NULL);
395 i = le16_to_cpu(dev->udev->descriptor.bcdDevice);
397 dev_info(&interface->dev, "USBLCD Version %1d%1d.%1d%1d found "
398 "at address %d\n", (i & 0xF000)>>12, (i & 0xF00)>>8,
399 (i & 0xF0)>>4, (i & 0xF), dev->udev->devnum);
401 /* let the user know what node this device is now attached to */
402 dev_info(&interface->dev, "USB LCD device now attached to USBLCD-%d\n",
407 kref_put(&dev->kref, lcd_delete);
411 static void lcd_draw_down(struct usb_lcd *dev)
415 time = usb_wait_anchor_empty_timeout(&dev->submitted, 1000);
417 usb_kill_anchored_urbs(&dev->submitted);
420 static int lcd_suspend(struct usb_interface *intf, pm_message_t message)
422 struct usb_lcd *dev = usb_get_intfdata(intf);
430 static int lcd_resume(struct usb_interface *intf)
435 static void lcd_disconnect(struct usb_interface *interface)
438 int minor = interface->minor;
440 mutex_lock(&open_disc_mutex);
441 dev = usb_get_intfdata(interface);
442 usb_set_intfdata(interface, NULL);
443 mutex_unlock(&open_disc_mutex);
445 /* give back our minor */
446 usb_deregister_dev(interface, &lcd_class);
448 down_write(&dev->io_rwsem);
449 dev->disconnected = 1;
450 up_write(&dev->io_rwsem);
452 usb_kill_anchored_urbs(&dev->submitted);
454 /* decrement our usage count */
455 kref_put(&dev->kref, lcd_delete);
457 dev_info(&interface->dev, "USB LCD #%d now disconnected\n", minor);
460 static struct usb_driver lcd_driver = {
463 .disconnect = lcd_disconnect,
464 .suspend = lcd_suspend,
465 .resume = lcd_resume,
466 .id_table = id_table,
467 .supports_autosuspend = 1,
470 module_usb_driver(lcd_driver);
472 MODULE_AUTHOR("Georges Toth <g.toth@e-biz.lu>");
473 MODULE_DESCRIPTION(DRIVER_VERSION);
474 MODULE_LICENSE("GPL");