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