blob: 12bad8a205a75cc9fc8d8e7f2b4e55b3d092ebc7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*****************************************************************************
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/init.h>
18#include <linux/slab.h>
19#include <linux/errno.h>
20#include <asm/uaccess.h>
21#include <linux/usb.h>
22
23#define DRIVER_VERSION "USBLCD Driver Version 1.05"
24
25#define USBLCD_MINOR 144
26
27#define IOCTL_GET_HARD_VERSION 1
28#define IOCTL_GET_DRV_VERSION 2
29
30
31static struct usb_device_id id_table [] = {
32 { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
33 { },
34};
35MODULE_DEVICE_TABLE (usb, id_table);
36
37
38struct usb_lcd {
39 struct usb_device * udev; /* init: probe_lcd */
40 struct usb_interface * interface; /* the interface for this device */
41 unsigned char * bulk_in_buffer; /* the buffer to receive data */
42 size_t bulk_in_size; /* the size of the receive buffer */
43 __u8 bulk_in_endpointAddr; /* the address of the bulk in endpoint */
44 __u8 bulk_out_endpointAddr; /* the address of the bulk out endpoint */
Oliver Neukum5afeb102007-06-11 15:36:02 +020045 struct kref kref;
46 struct semaphore limit_sem; /* to stop writes at full throttle from
47 * using up all RAM */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49#define to_lcd_dev(d) container_of(d, struct usb_lcd, kref)
50
Oliver Neukum5afeb102007-06-11 15:36:02 +020051#define USB_LCD_CONCURRENT_WRITES 5
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static struct usb_driver lcd_driver;
Oliver Neukum2e85c912007-03-05 15:11:14 +010054static DEFINE_MUTEX(usb_lcd_open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56
57static void lcd_delete(struct kref *kref)
58{
59 struct usb_lcd *dev = to_lcd_dev(kref);
60
61 usb_put_dev(dev->udev);
62 kfree (dev->bulk_in_buffer);
63 kfree (dev);
64}
65
66
67static int lcd_open(struct inode *inode, struct file *file)
68{
69 struct usb_lcd *dev;
70 struct usb_interface *interface;
71 int subminor;
72 int retval = 0;
73
74 subminor = iminor(inode);
75
Oliver Neukum2e85c912007-03-05 15:11:14 +010076 mutex_lock(&usb_lcd_open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 interface = usb_find_interface(&lcd_driver, subminor);
78 if (!interface) {
79 err ("USBLCD: %s - error, can't find device for minor %d",
80 __FUNCTION__, subminor);
81 retval = -ENODEV;
82 goto exit;
83 }
84
85 dev = usb_get_intfdata(interface);
86 if (!dev) {
87 retval = -ENODEV;
88 goto exit;
89 }
90
91 /* increment our usage count for the device */
92 kref_get(&dev->kref);
93
94 /* save our object in the file's private structure */
95 file->private_data = dev;
96
97exit:
Oliver Neukum2e85c912007-03-05 15:11:14 +010098 mutex_unlock(&usb_lcd_open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 return retval;
100}
101
102static int lcd_release(struct inode *inode, struct file *file)
103{
104 struct usb_lcd *dev;
105
106 dev = (struct usb_lcd *)file->private_data;
107 if (dev == NULL)
108 return -ENODEV;
109
110 /* decrement the count on our device */
111 kref_put(&dev->kref, lcd_delete);
112 return 0;
113}
114
115static ssize_t lcd_read(struct file *file, char __user * buffer, size_t count, loff_t *ppos)
116{
117 struct usb_lcd *dev;
118 int retval = 0;
119 int bytes_read;
120
121 dev = (struct usb_lcd *)file->private_data;
122
123 /* do a blocking bulk read to get data from the device */
124 retval = usb_bulk_msg(dev->udev,
125 usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
126 dev->bulk_in_buffer,
127 min(dev->bulk_in_size, count),
128 &bytes_read, 10000);
129
130 /* if the read was successful, copy the data to userspace */
131 if (!retval) {
132 if (copy_to_user(buffer, dev->bulk_in_buffer, bytes_read))
133 retval = -EFAULT;
134 else
135 retval = bytes_read;
136 }
137
138 return retval;
139}
140
141static int lcd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
142{
143 struct usb_lcd *dev;
144 u16 bcdDevice;
145 char buf[30];
146
147 dev = (struct usb_lcd *)file->private_data;
148 if (dev == NULL)
149 return -ENODEV;
150
151 switch (cmd) {
152 case IOCTL_GET_HARD_VERSION:
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 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
160 return -EFAULT;
161 break;
162 case IOCTL_GET_DRV_VERSION:
163 sprintf(buf,DRIVER_VERSION);
164 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
165 return -EFAULT;
166 break;
167 default:
168 return -ENOTTY;
169 break;
170 }
171
172 return 0;
173}
174
David Howells7d12e782006-10-05 14:55:46 +0100175static void lcd_write_bulk_callback(struct urb *urb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct usb_lcd *dev;
178
179 dev = (struct usb_lcd *)urb->context;
180
181 /* sync/async unlink faults aren't errors */
182 if (urb->status &&
183 !(urb->status == -ENOENT ||
184 urb->status == -ECONNRESET ||
185 urb->status == -ESHUTDOWN)) {
186 dbg("USBLCD: %s - nonzero write bulk status received: %d",
187 __FUNCTION__, urb->status);
188 }
189
190 /* free up our allocated buffer */
191 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
192 urb->transfer_buffer, urb->transfer_dma);
Oliver Neukum5afeb102007-06-11 15:36:02 +0200193 up(&dev->limit_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196static ssize_t lcd_write(struct file *file, const char __user * user_buffer, size_t count, loff_t *ppos)
197{
198 struct usb_lcd *dev;
Oliver Neukum5afeb102007-06-11 15:36:02 +0200199 int retval = 0, r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct urb *urb = NULL;
201 char *buf = NULL;
202
203 dev = (struct usb_lcd *)file->private_data;
204
205 /* verify that we actually have some data to write */
206 if (count == 0)
207 goto exit;
208
Oliver Neukum5afeb102007-06-11 15:36:02 +0200209 r = down_interruptible(&dev->limit_sem);
210 if (r < 0)
211 return -EINTR;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* create a urb, and a buffer for it, and copy the data to the urb */
214 urb = usb_alloc_urb(0, GFP_KERNEL);
Oliver Neukum5afeb102007-06-11 15:36:02 +0200215 if (!urb) {
216 retval = -ENOMEM;
217 goto err_no_buf;
218 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 buf = usb_buffer_alloc(dev->udev, count, GFP_KERNEL, &urb->transfer_dma);
221 if (!buf) {
222 retval = -ENOMEM;
223 goto error;
224 }
225
226 if (copy_from_user(buf, user_buffer, count)) {
227 retval = -EFAULT;
228 goto error;
229 }
230
231 /* initialize the urb properly */
232 usb_fill_bulk_urb(urb, dev->udev,
233 usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
234 buf, count, lcd_write_bulk_callback, dev);
235 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
236
237 /* send the data out the bulk port */
238 retval = usb_submit_urb(urb, GFP_KERNEL);
239 if (retval) {
240 err("USBLCD: %s - failed submitting write urb, error %d", __FUNCTION__, retval);
241 goto error;
242 }
243
244 /* release our reference to this urb, the USB core will eventually free it entirely */
245 usb_free_urb(urb);
246
247exit:
248 return count;
249
250error:
251 usb_buffer_free(dev->udev, count, buf, urb->transfer_dma);
252 usb_free_urb(urb);
Oliver Neukum5afeb102007-06-11 15:36:02 +0200253err_no_buf:
254 up(&dev->limit_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return retval;
256}
257
Luiz Fernando N. Capitulino066202d2006-08-05 20:37:11 -0300258static const struct file_operations lcd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .owner = THIS_MODULE,
260 .read = lcd_read,
261 .write = lcd_write,
262 .open = lcd_open,
263 .ioctl = lcd_ioctl,
264 .release = lcd_release,
265};
266
267/*
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700268 * 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static struct usb_class_driver lcd_class = {
Greg Kroah-Hartmand6e5bcf2005-06-20 21:15:16 -0700272 .name = "lcd%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 .fops = &lcd_fops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 .minor_base = USBLCD_MINOR,
275};
276
277static int lcd_probe(struct usb_interface *interface, const struct usb_device_id *id)
278{
279 struct usb_lcd *dev = NULL;
280 struct usb_host_interface *iface_desc;
281 struct usb_endpoint_descriptor *endpoint;
282 size_t buffer_size;
283 int i;
284 int retval = -ENOMEM;
285
286 /* allocate memory for our device state and initialize it */
Eric Sesterhenn80b6ca42006-02-27 21:29:43 +0100287 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (dev == NULL) {
289 err("Out of memory");
290 goto error;
291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 kref_init(&dev->kref);
Oliver Neukum5afeb102007-06-11 15:36:02 +0200293 sema_init(&dev->limit_sem, USB_LCD_CONCURRENT_WRITES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 dev->udev = usb_get_dev(interface_to_usbdev(interface));
296 dev->interface = interface;
297
298 if (le16_to_cpu(dev->udev->descriptor.idProduct) != 0x0001) {
299 warn(KERN_INFO "USBLCD model not supported.");
300 return -ENODEV;
301 }
302
303 /* set up the endpoint information */
304 /* use only the first bulk-in and bulk-out endpoints */
305 iface_desc = interface->cur_altsetting;
306 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
307 endpoint = &iface_desc->endpoint[i].desc;
308
309 if (!dev->bulk_in_endpointAddr &&
Luiz Fernando N. Capitulinob0b660b2006-09-27 11:58:54 -0700310 usb_endpoint_is_bulk_in(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* we found a bulk in endpoint */
312 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
313 dev->bulk_in_size = buffer_size;
314 dev->bulk_in_endpointAddr = endpoint->bEndpointAddress;
315 dev->bulk_in_buffer = kmalloc(buffer_size, GFP_KERNEL);
316 if (!dev->bulk_in_buffer) {
317 err("Could not allocate bulk_in_buffer");
318 goto error;
319 }
320 }
321
322 if (!dev->bulk_out_endpointAddr &&
Luiz Fernando N. Capitulinob0b660b2006-09-27 11:58:54 -0700323 usb_endpoint_is_bulk_out(endpoint)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* we found a bulk out endpoint */
325 dev->bulk_out_endpointAddr = endpoint->bEndpointAddress;
326 }
327 }
328 if (!(dev->bulk_in_endpointAddr && dev->bulk_out_endpointAddr)) {
329 err("Could not find both bulk-in and bulk-out endpoints");
330 goto error;
331 }
332
333 /* save our data pointer in this interface device */
334 usb_set_intfdata(interface, dev);
335
336 /* we can register the device now, as it is ready */
337 retval = usb_register_dev(interface, &lcd_class);
338 if (retval) {
339 /* something prevented us from registering this driver */
340 err("Not able to get a minor for this device.");
341 usb_set_intfdata(interface, NULL);
342 goto error;
343 }
344
345 i = le16_to_cpu(dev->udev->descriptor.bcdDevice);
346
347 info("USBLCD Version %1d%1d.%1d%1d found at address %d",
348 (i & 0xF000)>>12,(i & 0xF00)>>8,(i & 0xF0)>>4,(i & 0xF),
349 dev->udev->devnum);
350
351 /* let the user know what node this device is now attached to */
352 info("USB LCD device now attached to USBLCD-%d", interface->minor);
353 return 0;
354
355error:
356 if (dev)
357 kref_put(&dev->kref, lcd_delete);
358 return retval;
359}
360
361static void lcd_disconnect(struct usb_interface *interface)
362{
363 struct usb_lcd *dev;
364 int minor = interface->minor;
365
366 /* prevent skel_open() from racing skel_disconnect() */
Oliver Neukum2e85c912007-03-05 15:11:14 +0100367 mutex_lock(&usb_lcd_open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 dev = usb_get_intfdata(interface);
370 usb_set_intfdata(interface, NULL);
371
372 /* give back our minor */
373 usb_deregister_dev(interface, &lcd_class);
374
Oliver Neukum2e85c912007-03-05 15:11:14 +0100375 mutex_unlock(&usb_lcd_open_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
377 /* decrement our usage count */
378 kref_put(&dev->kref, lcd_delete);
379
380 info("USB LCD #%d now disconnected", minor);
381}
382
383static struct usb_driver lcd_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 .name = "usblcd",
385 .probe = lcd_probe,
386 .disconnect = lcd_disconnect,
387 .id_table = id_table,
388};
389
390static int __init usb_lcd_init(void)
391{
392 int result;
393
394 result = usb_register(&lcd_driver);
395 if (result)
396 err("usb_register failed. Error number %d", result);
397
398 return result;
399}
400
401
402static void __exit usb_lcd_exit(void)
403{
404 usb_deregister(&lcd_driver);
405}
406
407module_init(usb_lcd_init);
408module_exit(usb_lcd_exit);
409
410MODULE_AUTHOR("Georges Toth <g.toth@e-biz.lu>");
411MODULE_DESCRIPTION(DRIVER_VERSION);
412MODULE_LICENSE("GPL");