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