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 * 18.9.02 RA Vendor ID change, longer timeouts *
13 *****************************************************************************/
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/init.h>
17 #include <linux/slab.h>
18 #include <linux/errno.h>
19 #include <asm/uaccess.h>
20 #include <linux/usb.h>
22 #define DRIVER_VERSION "USBLCD Driver Version 1.03"
24 #define USBLCD_MINOR 144
26 #define IOCTL_GET_HARD_VERSION 1
27 #define IOCTL_GET_DRV_VERSION 2
29 /* stall/wait timeout for USBLCD */
30 #define NAK_TIMEOUT (10*HZ)
32 #define IBUF_SIZE 0x1000
33 #define OBUF_SIZE 0x10000
36 struct usb_device *lcd_dev; /* init: probe_lcd */
37 unsigned int ifnum; /* Interface number of the USB device */
38 int isopen; /* nz if open */
39 int present; /* Device is present on the bus */
40 char *obuf, *ibuf; /* transfer buffers */
41 char bulk_in_ep, bulk_out_ep; /* Endpoint assignments */
42 wait_queue_head_t wait_q; /* for timeouts */
45 static struct lcd_usb_data lcd_instance;
47 static int open_lcd(struct inode *inode, struct file *file)
49 struct lcd_usb_data *lcd = &lcd_instance;
51 if (lcd->isopen || !lcd->present) {
56 init_waitqueue_head(&lcd->wait_q);
58 info("USBLCD opened.");
63 static int close_lcd(struct inode *inode, struct file *file)
65 struct lcd_usb_data *lcd = &lcd_instance;
69 info("USBLCD closed.");
74 ioctl_lcd(struct inode *inode, struct file *file, unsigned int cmd,
77 struct lcd_usb_data *lcd = &lcd_instance;
81 /* Sanity check to make sure lcd is connected, powered, etc */
88 case IOCTL_GET_HARD_VERSION:
89 i = (lcd->lcd_dev)->descriptor.bcdDevice;
90 sprintf(buf,"%1d%1d.%1d%1d",(i & 0xF000)>>12,(i & 0xF00)>>8,
91 (i & 0xF0)>>4,(i & 0xF));
92 if (copy_to_user((void *)arg,buf,strlen(buf))!=0)
95 case IOCTL_GET_DRV_VERSION:
96 sprintf(buf,DRIVER_VERSION);
97 if (copy_to_user((void *)arg,buf,strlen(buf))!=0)
109 write_lcd(struct file *file, const char *buffer,
110 size_t count, loff_t * ppos)
112 struct lcd_usb_data *lcd = &lcd_instance;
114 unsigned long copy_size;
115 unsigned long bytes_written = 0;
116 unsigned int partial;
121 /* Sanity check to make sure lcd is connected, powered, etc */
124 lcd->lcd_dev == NULL)
128 unsigned long thistime;
129 char *obuf = lcd->obuf;
131 thistime = copy_size =
132 (count >= OBUF_SIZE) ? OBUF_SIZE : count;
133 if (copy_from_user(lcd->obuf, buffer, copy_size))
139 if (signal_pending(current)) {
140 return bytes_written ? bytes_written : -EINTR;
143 result = usb_bulk_msg(lcd->lcd_dev,
144 usb_sndbulkpipe(lcd->lcd_dev, 1),
145 obuf, thistime, &partial, 10 * HZ);
147 dbg("write stats: result:%d thistime:%lu partial:%u",
148 result, thistime, partial);
150 if (result == USB_ST_TIMEOUT) { /* NAK - so hold for a while */
154 interruptible_sleep_on_timeout(&lcd-> wait_q, NAK_TIMEOUT);
156 } else if (!result & partial) {
163 err("Write Whoops - %x", result);
166 bytes_written += copy_size;
171 return bytes_written ? bytes_written : -EIO;
175 read_lcd(struct file *file, char *buffer, size_t count, loff_t * ppos)
177 struct lcd_usb_data *lcd = &lcd_instance;
179 unsigned int partial;
183 char *ibuf = lcd->ibuf;
185 /* Sanity check to make sure lcd is connected, powered, etc */
188 lcd->lcd_dev == NULL)
194 if (signal_pending(current)) {
195 return read_count ? read_count : -EINTR;
199 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
201 result = usb_bulk_msg(lcd->lcd_dev,
202 usb_rcvbulkpipe(lcd->lcd_dev, 0),
203 ibuf, this_read, &partial,
206 dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
207 result, this_read, partial);
210 count = this_read = partial;
211 } else if (result == USB_ST_TIMEOUT || result == 15) { /* FIXME: 15 ??? */
213 err("read_lcd: maxretry timeout");
216 interruptible_sleep_on_timeout(&lcd->wait_q,
219 } else if (result != USB_ST_DATAUNDERRUN) {
220 err("Read Whoops - result:%u partial:%u this_read:%u",
221 result, partial, this_read);
228 if (copy_to_user(buffer, ibuf, this_read))
231 read_count += this_read;
238 static void *probe_lcd(struct usb_device *dev, unsigned int ifnum)
240 struct lcd_usb_data *lcd = &lcd_instance;
243 if (dev->descriptor.idProduct != 0x0001 ) {
244 warn(KERN_INFO "USBLCD model not supported.");
248 if (lcd->present == 1) {
249 warn(KERN_INFO "Multiple USBLCDs are not supported!");
253 i = dev->descriptor.bcdDevice;
255 info("USBLCD Version %1d%1d.%1d%1d found at address %d",
256 (i & 0xF000)>>12,(i & 0xF00)>>8,(i & 0xF0)>>4,(i & 0xF),
262 if (!(lcd->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
263 err("probe_lcd: Not enough memory for the output buffer");
266 dbg("probe_lcd: obuf address:%p", lcd->obuf);
268 if (!(lcd->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
269 err("probe_lcd: Not enough memory for the input buffer");
273 dbg("probe_lcd: ibuf address:%p", lcd->ibuf);
278 static void disconnect_lcd(struct usb_device *dev, void *ptr)
280 struct lcd_usb_data *lcd = (struct lcd_usb_data *) ptr;
284 /* better let it finish - the release will do whats needed */
291 info("USBLCD disconnected.");
296 static struct usb_device_id id_table [] = {
297 { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
301 MODULE_DEVICE_TABLE (usb, id_table);
304 file_operations usb_lcd_fops = {
305 .owner = THIS_MODULE,
310 .release = close_lcd,
314 usb_driver lcd_driver = {
316 .probe = (void *)probe_lcd,
317 .disconnect = disconnect_lcd,
318 .id_table = id_table,
319 .fops = &usb_lcd_fops,
320 .minor = USBLCD_MINOR,
323 int usb_lcd_init(void)
325 if (usb_register(&lcd_driver) < 0)
328 info("%s (C) Adams IT Services http://www.usblcd.de", DRIVER_VERSION);
329 info("USBLCD support registered.");
334 void usb_lcd_cleanup(void)
336 struct lcd_usb_data *lcd = &lcd_instance;
339 usb_deregister(&lcd_driver);
342 module_init(usb_lcd_init);
343 module_exit(usb_lcd_cleanup);
345 MODULE_AUTHOR("Adams IT Services <info@usblcd.de>");
346 MODULE_DESCRIPTION(DRIVER_VERSION);
347 MODULE_LICENSE("GPL");