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 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)
94 case IOCTL_GET_DRV_VERSION:
95 sprintf(buf,DRIVER_VERSION);
96 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
108 write_lcd(struct file *file, const char __user *buffer,
109 size_t count, loff_t * ppos)
111 struct lcd_usb_data *lcd = &lcd_instance;
113 unsigned long copy_size;
114 unsigned long bytes_written = 0;
115 unsigned int partial;
120 /* Sanity check to make sure lcd is connected, powered, etc */
123 lcd->lcd_dev == NULL)
127 unsigned long thistime;
128 char *obuf = lcd->obuf;
130 thistime = copy_size =
131 (count >= OBUF_SIZE) ? OBUF_SIZE : count;
132 if (copy_from_user(lcd->obuf, buffer, copy_size))
138 if (signal_pending(current)) {
139 return bytes_written ? bytes_written : -EINTR;
142 result = usb_bulk_msg(lcd->lcd_dev,
143 usb_sndbulkpipe(lcd->lcd_dev, 1),
144 obuf, thistime, &partial, 10 * HZ);
146 dbg("write stats: result:%d thistime:%lu partial:%u",
147 result, thistime, partial);
149 if (result == -ETIMEDOUT) { /* NAK - so hold for a while */
153 interruptible_sleep_on_timeout(&lcd-> wait_q, NAK_TIMEOUT);
155 } else if (!result && partial) {
162 err("Write Whoops - %x", result);
165 bytes_written += copy_size;
170 return bytes_written ? bytes_written : -EIO;
174 read_lcd(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
176 struct lcd_usb_data *lcd = &lcd_instance;
178 unsigned int partial;
182 char *ibuf = lcd->ibuf;
184 /* Sanity check to make sure lcd is connected, powered, etc */
187 lcd->lcd_dev == NULL)
193 if (signal_pending(current)) {
194 return read_count ? read_count : -EINTR;
198 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
200 result = usb_bulk_msg(lcd->lcd_dev,
201 usb_rcvbulkpipe(lcd->lcd_dev, 0),
202 ibuf, this_read, &partial,
205 dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
206 result, this_read, partial);
209 count = this_read = partial;
210 } else if (result == -ETIMEDOUT || result == 15) { /* FIXME: 15 ??? */
212 err("read_lcd: maxretry timeout");
215 interruptible_sleep_on_timeout(&lcd->wait_q,
218 } else if (result != -EREMOTEIO) {
219 err("Read Whoops - result:%u partial:%u this_read:%u",
220 result, partial, this_read);
227 if (copy_to_user(buffer, ibuf, this_read))
230 read_count += this_read;
238 file_operations usb_lcd_fops = {
239 .owner = THIS_MODULE,
244 .release = close_lcd,
247 static struct usb_class_driver usb_lcd_class = {
249 .fops = &usb_lcd_fops,
250 .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
251 .minor_base = USBLCD_MINOR,
254 static int probe_lcd(struct usb_interface *intf, const struct usb_device_id *id)
256 struct usb_device *dev = interface_to_usbdev(intf);
257 struct lcd_usb_data *lcd = &lcd_instance;
261 if (dev->descriptor.idProduct != 0x0001 ) {
262 warn(KERN_INFO "USBLCD model not supported.");
266 if (lcd->present == 1) {
267 warn(KERN_INFO "Multiple USBLCDs are not supported!");
271 i = dev->descriptor.bcdDevice;
273 info("USBLCD Version %1d%1d.%1d%1d found at address %d",
274 (i & 0xF000)>>12,(i & 0xF00)>>8,(i & 0xF0)>>4,(i & 0xF),
282 if (!(lcd->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
283 err("probe_lcd: Not enough memory for the output buffer");
286 dbg("probe_lcd: obuf address:%p", lcd->obuf);
288 if (!(lcd->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
289 err("probe_lcd: Not enough memory for the input buffer");
293 dbg("probe_lcd: ibuf address:%p", lcd->ibuf);
295 retval = usb_register_dev(intf, &usb_lcd_class);
297 err("Not able to get a minor for this device.");
303 usb_set_intfdata (intf, lcd);
307 static void disconnect_lcd(struct usb_interface *intf)
309 struct lcd_usb_data *lcd = usb_get_intfdata (intf);
311 usb_set_intfdata (intf, NULL);
313 usb_deregister_dev(intf, &usb_lcd_class);
317 /* better let it finish - the release will do whats needed */
324 info("USBLCD disconnected.");
330 static struct usb_device_id id_table [] = {
331 { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
335 MODULE_DEVICE_TABLE (usb, id_table);
337 static struct usb_driver lcd_driver = {
338 .owner = THIS_MODULE,
340 .probe = (void *)probe_lcd,
341 .disconnect = disconnect_lcd,
342 .id_table = id_table,
345 static int __init usb_lcd_init(void)
348 retval = usb_register(&lcd_driver);
352 info("%s (C) Adams IT Services http://www.usblcd.de", DRIVER_VERSION);
353 info("USBLCD support registered.");
359 static void __exit usb_lcd_cleanup(void)
361 struct lcd_usb_data *lcd = &lcd_instance;
364 usb_deregister(&lcd_driver);
367 module_init(usb_lcd_init);
368 module_exit(usb_lcd_cleanup);
370 MODULE_AUTHOR("Adams IT Services <info@usblcd.de>");
371 MODULE_DESCRIPTION(DRIVER_VERSION);
372 MODULE_LICENSE("GPL");