1 /*****************************************************************************
2 * USBLCD Kernel Driver *
3 * See http://www.usblcd.de for Hardware and Documentation. *
5 * (C) 2002 Adams IT Services <info@usblcd.de> *
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) *
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>
21 #define DRIVER_VERSION "USBLCD Driver Version 1.04"
23 #define USBLCD_MINOR 144
25 #define IOCTL_GET_HARD_VERSION 1
26 #define IOCTL_GET_DRV_VERSION 2
28 /* stall/wait timeout for USBLCD */
29 #define NAK_TIMEOUT (10*HZ)
31 #define IBUF_SIZE 0x1000
32 #define OBUF_SIZE 0x10000
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 */
44 static struct lcd_usb_data lcd_instance;
46 static int open_lcd(struct inode *inode, struct file *file)
48 struct lcd_usb_data *lcd = &lcd_instance;
50 if (lcd->isopen || !lcd->present) {
55 init_waitqueue_head(&lcd->wait_q);
57 info("USBLCD opened.");
62 static int close_lcd(struct inode *inode, struct file *file)
64 struct lcd_usb_data *lcd = &lcd_instance;
68 info("USBLCD closed.");
73 ioctl_lcd(struct inode *inode, struct file *file, unsigned int cmd,
76 struct lcd_usb_data *lcd = &lcd_instance;
80 /* Sanity check to make sure lcd is connected, powered, etc */
87 case IOCTL_GET_HARD_VERSION:
88 bcdDevice = le16_to_cpu((lcd->lcd_dev)->descriptor.bcdDevice);
89 sprintf(buf,"%1d%1d.%1d%1d",
90 (bcdDevice & 0xF000)>>12,
91 (bcdDevice & 0xF00)>>8,
92 (bcdDevice & 0xF0)>>4,
94 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
97 case IOCTL_GET_DRV_VERSION:
98 sprintf(buf,DRIVER_VERSION);
99 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
111 write_lcd(struct file *file, const char __user *buffer,
112 size_t count, loff_t * ppos)
114 struct lcd_usb_data *lcd = &lcd_instance;
116 unsigned long copy_size;
117 unsigned long bytes_written = 0;
118 unsigned int partial;
123 /* Sanity check to make sure lcd is connected, powered, etc */
126 lcd->lcd_dev == NULL)
130 unsigned long thistime;
131 char *obuf = lcd->obuf;
133 thistime = copy_size =
134 (count >= OBUF_SIZE) ? OBUF_SIZE : count;
135 if (copy_from_user(lcd->obuf, buffer, copy_size))
141 if (signal_pending(current)) {
142 return bytes_written ? bytes_written : -EINTR;
145 result = usb_bulk_msg(lcd->lcd_dev,
146 usb_sndbulkpipe(lcd->lcd_dev, 1),
147 obuf, thistime, &partial, 10 * HZ);
149 dbg("write stats: result:%d thistime:%lu partial:%u",
150 result, thistime, partial);
152 if (result == -ETIMEDOUT) { /* NAK - so hold for a while */
156 interruptible_sleep_on_timeout(&lcd-> wait_q, NAK_TIMEOUT);
158 } else if (!result && partial) {
165 err("Write Whoops - %x", result);
168 bytes_written += copy_size;
173 return bytes_written ? bytes_written : -EIO;
177 read_lcd(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
179 struct lcd_usb_data *lcd = &lcd_instance;
181 unsigned int partial;
185 char *ibuf = lcd->ibuf;
187 /* Sanity check to make sure lcd is connected, powered, etc */
190 lcd->lcd_dev == NULL)
196 if (signal_pending(current)) {
197 return read_count ? read_count : -EINTR;
201 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
203 result = usb_bulk_msg(lcd->lcd_dev,
204 usb_rcvbulkpipe(lcd->lcd_dev, 0),
205 ibuf, this_read, &partial,
208 dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
209 result, this_read, partial);
212 count = this_read = partial;
213 } else if (result == -ETIMEDOUT || result == 15) { /* FIXME: 15 ??? */
215 err("read_lcd: maxretry timeout");
218 interruptible_sleep_on_timeout(&lcd->wait_q,
221 } else if (result != -EREMOTEIO) {
222 err("Read Whoops - result:%u partial:%u this_read:%u",
223 result, partial, this_read);
230 if (copy_to_user(buffer, ibuf, this_read))
233 read_count += this_read;
241 file_operations usb_lcd_fops = {
242 .owner = THIS_MODULE,
247 .release = close_lcd,
250 static struct usb_class_driver usb_lcd_class = {
252 .fops = &usb_lcd_fops,
253 .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
254 .minor_base = USBLCD_MINOR,
257 static int probe_lcd(struct usb_interface *intf, const struct usb_device_id *id)
259 struct usb_device *dev = interface_to_usbdev(intf);
260 struct lcd_usb_data *lcd = &lcd_instance;
264 if (le16_to_cpu(dev->descriptor.idProduct) != 0x0001) {
265 warn(KERN_INFO "USBLCD model not supported.");
269 if (lcd->present == 1) {
270 warn(KERN_INFO "Multiple USBLCDs are not supported!");
274 i = le16_to_cpu(dev->descriptor.bcdDevice);
276 info("USBLCD Version %1d%1d.%1d%1d found at address %d",
277 (i & 0xF000)>>12,(i & 0xF00)>>8,(i & 0xF0)>>4,(i & 0xF),
285 if (!(lcd->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
286 err("probe_lcd: Not enough memory for the output buffer");
289 dbg("probe_lcd: obuf address:%p", lcd->obuf);
291 if (!(lcd->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
292 err("probe_lcd: Not enough memory for the input buffer");
296 dbg("probe_lcd: ibuf address:%p", lcd->ibuf);
298 retval = usb_register_dev(intf, &usb_lcd_class);
300 err("Not able to get a minor for this device.");
306 usb_set_intfdata (intf, lcd);
310 static void disconnect_lcd(struct usb_interface *intf)
312 struct lcd_usb_data *lcd = usb_get_intfdata (intf);
314 usb_set_intfdata (intf, NULL);
316 usb_deregister_dev(intf, &usb_lcd_class);
320 /* better let it finish - the release will do whats needed */
327 info("USBLCD disconnected.");
333 static struct usb_device_id id_table [] = {
334 { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
338 MODULE_DEVICE_TABLE (usb, id_table);
340 static struct usb_driver lcd_driver = {
341 .owner = THIS_MODULE,
343 .probe = (void *)probe_lcd,
344 .disconnect = disconnect_lcd,
345 .id_table = id_table,
348 static int __init usb_lcd_init(void)
351 retval = usb_register(&lcd_driver);
355 info("%s (C) Adams IT Services http://www.usblcd.de", DRIVER_VERSION);
356 info("USBLCD support registered.");
362 static void __exit usb_lcd_cleanup(void)
364 struct lcd_usb_data *lcd = &lcd_instance;
367 usb_deregister(&lcd_driver);
370 module_init(usb_lcd_init);
371 module_exit(usb_lcd_cleanup);
373 MODULE_AUTHOR("Adams IT Services <info@usblcd.de>");
374 MODULE_DESCRIPTION(DRIVER_VERSION);
375 MODULE_LICENSE("GPL");