ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / usb / misc / usblcd.c
1 /*****************************************************************************
2  *                          USBLCD Kernel Driver                             *
3  *        See http://www.usblcd.de for Hardware and Documentation.           *
4  *                            Version 1.03                                   *
5  *             (C) 2002 Adams IT Services <info@usblcd.de>                   *
6  *                                                                           *
7  *     This file is licensed under the GPL. See COPYING in the package.      *
8  * Based on rio500.c by Cesar Miquel (miquel@df.uba.ar) which is based on    *
9  * hp_scanner.c by David E. Nelson (dnelson@jump.net)                        *
10  *                                                                           *
11  * 23.7.02 RA changed minor device number to the official assigned one       *
12  *****************************************************************************/
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/errno.h>
18 #include <asm/uaccess.h>
19 #include <linux/usb.h>
20
21 #define DRIVER_VERSION "USBLCD Driver Version 1.04"
22
23 #define USBLCD_MINOR            144
24
25 #define IOCTL_GET_HARD_VERSION  1
26 #define IOCTL_GET_DRV_VERSION   2
27
28 /* stall/wait timeout for USBLCD */
29 #define NAK_TIMEOUT     (10*HZ)
30
31 #define IBUF_SIZE       0x1000
32 #define OBUF_SIZE       0x10000
33
34 struct lcd_usb_data {
35         struct usb_device *lcd_dev;     /* init: probe_lcd */
36         unsigned int ifnum;             /* Interface number of the USB device */
37         int isopen;                     /* nz if open */
38         int present;                    /* Device is present on the bus */
39         char *obuf, *ibuf;              /* transfer buffers */
40         char bulk_in_ep, bulk_out_ep;   /* Endpoint assignments */
41         wait_queue_head_t wait_q;       /* for timeouts */
42 };
43
44 static struct lcd_usb_data lcd_instance;
45
46 static int open_lcd(struct inode *inode, struct file *file)
47 {
48         struct lcd_usb_data *lcd = &lcd_instance;
49
50         if (lcd->isopen || !lcd->present) {
51                 return -EBUSY;
52         }
53         lcd->isopen = 1;
54
55         init_waitqueue_head(&lcd->wait_q);
56
57         info("USBLCD opened.");
58
59         return 0;
60 }
61
62 static int close_lcd(struct inode *inode, struct file *file)
63 {
64         struct lcd_usb_data *lcd = &lcd_instance;
65
66         lcd->isopen = 0;
67
68         info("USBLCD closed.");
69         return 0;
70 }
71
72 static int
73 ioctl_lcd(struct inode *inode, struct file *file, unsigned int cmd,
74           unsigned long arg)
75 {
76         struct lcd_usb_data *lcd = &lcd_instance;
77         int i;
78         char buf[30];
79
80         /* Sanity check to make sure lcd is connected, powered, etc */
81         if (lcd == NULL ||
82             lcd->present == 0 ||
83             lcd->lcd_dev == NULL)
84                 return -1;
85
86         switch (cmd) {
87         case IOCTL_GET_HARD_VERSION:
88                 i = (lcd->lcd_dev)->descriptor.bcdDevice;
89                 sprintf(buf,"%1d%1d.%1d%1d",(i & 0xF000)>>12,(i & 0xF00)>>8,
90                         (i & 0xF0)>>4,(i & 0xF));
91                 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
92                         return -EFAULT;
93                 break;
94         case IOCTL_GET_DRV_VERSION:
95                 sprintf(buf,DRIVER_VERSION);
96                 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
97                         return -EFAULT;
98                 break;
99         default:
100                 return -ENOTTY;
101                 break;
102         }
103
104         return 0;
105 }
106
107 static ssize_t
108 write_lcd(struct file *file, const char __user *buffer,
109           size_t count, loff_t * ppos)
110 {
111         struct lcd_usb_data *lcd = &lcd_instance;
112
113         unsigned long copy_size;
114         unsigned long bytes_written = 0;
115         unsigned int partial;
116
117         int result = 0;
118         int maxretry;
119
120         /* Sanity check to make sure lcd is connected, powered, etc */
121         if (lcd == NULL ||
122             lcd->present == 0 ||
123             lcd->lcd_dev == NULL)
124                 return -1;
125
126         do {
127                 unsigned long thistime;
128                 char *obuf = lcd->obuf;
129
130                 thistime = copy_size =
131                     (count >= OBUF_SIZE) ? OBUF_SIZE : count;
132                 if (copy_from_user(lcd->obuf, buffer, copy_size))
133                         return -EFAULT;
134                 maxretry = 5;
135                 while (thistime) {
136                         if (!lcd->lcd_dev)
137                                 return -ENODEV;
138                         if (signal_pending(current)) {
139                                 return bytes_written ? bytes_written : -EINTR;
140                         }
141
142                         result = usb_bulk_msg(lcd->lcd_dev,
143                                          usb_sndbulkpipe(lcd->lcd_dev, 1),
144                                          obuf, thistime, &partial, 10 * HZ);
145
146                         dbg("write stats: result:%d thistime:%lu partial:%u",
147                              result, thistime, partial);
148
149                         if (result == -ETIMEDOUT) {     /* NAK - so hold for a while */
150                                 if (!maxretry--) {
151                                         return -ETIME;
152                                 }
153                                 interruptible_sleep_on_timeout(&lcd-> wait_q, NAK_TIMEOUT);
154                                 continue;
155                         } else if (!result && partial) {
156                                 obuf += partial;
157                                 thistime -= partial;
158                         } else
159                                 break;
160                 };
161                 if (result) {
162                         err("Write Whoops - %x", result);
163                         return -EIO;
164                 }
165                 bytes_written += copy_size;
166                 count -= copy_size;
167                 buffer += copy_size;
168         } while (count > 0);
169
170         return bytes_written ? bytes_written : -EIO;
171 }
172
173 static ssize_t
174 read_lcd(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
175 {
176         struct lcd_usb_data *lcd = &lcd_instance;
177         ssize_t read_count;
178         unsigned int partial;
179         int this_read;
180         int result;
181         int maxretry = 10;
182         char *ibuf = lcd->ibuf;
183
184         /* Sanity check to make sure lcd is connected, powered, etc */
185         if (lcd == NULL ||
186             lcd->present == 0 ||
187             lcd->lcd_dev == NULL)
188                 return -1;
189
190         read_count = 0;
191
192         while (count > 0) {
193                 if (signal_pending(current)) {
194                         return read_count ? read_count : -EINTR;
195                 }
196                 if (!lcd->lcd_dev)
197                         return -ENODEV;
198                 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
199
200                 result = usb_bulk_msg(lcd->lcd_dev,
201                                       usb_rcvbulkpipe(lcd->lcd_dev, 0),
202                                       ibuf, this_read, &partial,
203                                       (int) (HZ * 8));
204
205                 dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
206                        result, this_read, partial);
207
208                 if (partial) {
209                         count = this_read = partial;
210                 } else if (result == -ETIMEDOUT || result == 15) {      /* FIXME: 15 ??? */
211                         if (!maxretry--) {
212                                 err("read_lcd: maxretry timeout");
213                                 return -ETIME;
214                         }
215                         interruptible_sleep_on_timeout(&lcd->wait_q,
216                                                        NAK_TIMEOUT);
217                         continue;
218                 } else if (result != -EREMOTEIO) {
219                         err("Read Whoops - result:%u partial:%u this_read:%u",
220                              result, partial, this_read);
221                         return -EIO;
222                 } else {
223                         return (0);
224                 }
225
226                 if (this_read) {
227                         if (copy_to_user(buffer, ibuf, this_read))
228                                 return -EFAULT;
229                         count -= this_read;
230                         read_count += this_read;
231                         buffer += this_read;
232                 }
233         }
234         return read_count;
235 }
236
237 static struct
238 file_operations usb_lcd_fops = {
239         .owner =        THIS_MODULE,
240         .read =         read_lcd,
241         .write =        write_lcd,
242         .ioctl =        ioctl_lcd,
243         .open =         open_lcd,
244         .release =      close_lcd,
245 };
246
247 static struct usb_class_driver usb_lcd_class = {
248         .name =         "usb/lcd%d",
249         .fops =         &usb_lcd_fops,
250         .mode =         S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
251         .minor_base =   USBLCD_MINOR,
252 };
253
254 static int probe_lcd(struct usb_interface *intf, const struct usb_device_id *id)
255 {
256         struct usb_device *dev = interface_to_usbdev(intf);
257         struct lcd_usb_data *lcd = &lcd_instance;
258         int i;
259         int retval;
260
261         if (dev->descriptor.idProduct != 0x0001  ) {
262                 warn(KERN_INFO "USBLCD model not supported.");
263                 return -ENODEV;
264         }
265
266         if (lcd->present == 1) {
267                 warn(KERN_INFO "Multiple USBLCDs are not supported!");
268                 return -ENODEV;
269         }
270
271         i = dev->descriptor.bcdDevice;
272
273         info("USBLCD Version %1d%1d.%1d%1d found at address %d",
274                 (i & 0xF000)>>12,(i & 0xF00)>>8,(i & 0xF0)>>4,(i & 0xF),
275                 dev->devnum);
276
277
278
279         lcd->present = 1;
280         lcd->lcd_dev = dev;
281
282         if (!(lcd->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
283                 err("probe_lcd: Not enough memory for the output buffer");
284                 return -ENOMEM;
285         }
286         dbg("probe_lcd: obuf address:%p", lcd->obuf);
287
288         if (!(lcd->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
289                 err("probe_lcd: Not enough memory for the input buffer");
290                 kfree(lcd->obuf);
291                 return -ENOMEM;
292         }
293         dbg("probe_lcd: ibuf address:%p", lcd->ibuf);
294
295         retval = usb_register_dev(intf, &usb_lcd_class);
296         if (retval) {
297                 err("Not able to get a minor for this device.");
298                 kfree(lcd->obuf);
299                 kfree(lcd->ibuf);
300                 return -ENOMEM;
301         }
302
303         usb_set_intfdata (intf, lcd);
304         return 0;
305 }
306
307 static void disconnect_lcd(struct usb_interface *intf)
308 {
309         struct lcd_usb_data *lcd = usb_get_intfdata (intf);
310
311         usb_set_intfdata (intf, NULL);
312         if (lcd) {
313                 usb_deregister_dev(intf, &usb_lcd_class);
314
315                 if (lcd->isopen) {
316                         lcd->isopen = 0;
317                         /* better let it finish - the release will do whats needed */
318                         lcd->lcd_dev = NULL;
319                         return;
320                 }
321                 kfree(lcd->ibuf);
322                 kfree(lcd->obuf);
323
324                 info("USBLCD disconnected.");
325
326                 lcd->present = 0;
327         }
328 }
329
330 static struct usb_device_id id_table [] = {
331         { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
332         {},
333 };
334
335 MODULE_DEVICE_TABLE (usb, id_table);
336
337 static struct usb_driver lcd_driver = {
338         .owner =        THIS_MODULE,
339         .name =         "usblcd",
340         .probe =        (void *)probe_lcd,
341         .disconnect =   disconnect_lcd,
342         .id_table =     id_table,
343 };
344
345 static int __init usb_lcd_init(void)
346 {
347         int retval;
348         retval = usb_register(&lcd_driver);
349         if (retval)
350                 goto out;
351
352         info("%s (C) Adams IT Services http://www.usblcd.de", DRIVER_VERSION);
353         info("USBLCD support registered.");
354 out:
355         return retval;
356 }
357
358
359 static void __exit usb_lcd_cleanup(void)
360 {
361         struct lcd_usb_data *lcd = &lcd_instance;
362
363         lcd->present = 0;
364         usb_deregister(&lcd_driver);
365 }
366
367 module_init(usb_lcd_init);
368 module_exit(usb_lcd_cleanup);
369
370 MODULE_AUTHOR("Adams IT Services <info@usblcd.de>");
371 MODULE_DESCRIPTION(DRIVER_VERSION);
372 MODULE_LICENSE("GPL");