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 u16 bcdDevice;
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 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,
93 (bcdDevice & 0xF));
94 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
95 return -EFAULT;
96 break;
97 case IOCTL_GET_DRV_VERSION:
98 sprintf(buf,DRIVER_VERSION);
99 if (copy_to_user((void __user *)arg,buf,strlen(buf))!=0)
100 return -EFAULT;
101 break;
102 default:
103 return -ENOTTY;
104 break;
105 }
106
107 return 0;
108 }
109
110 static ssize_t
111 write_lcd(struct file *file, const char __user *buffer,
112 size_t count, loff_t * ppos)
113 {
114 struct lcd_usb_data *lcd = &lcd_instance;
115
116 unsigned long copy_size;
117 unsigned long bytes_written = 0;
118 unsigned int partial;
119
120 int result = 0;
121 int maxretry;
122
123 /* Sanity check to make sure lcd is connected, powered, etc */
124 if (lcd == NULL ||
125 lcd->present == 0 ||
126 lcd->lcd_dev == NULL)
127 return -1;
128
129 do {
130 unsigned long thistime;
131 char *obuf = lcd->obuf;
132
133 thistime = copy_size =
134 (count >= OBUF_SIZE) ? OBUF_SIZE : count;
135 if (copy_from_user(lcd->obuf, buffer, copy_size))
136 return -EFAULT;
137 maxretry = 5;
138 while (thistime) {
139 if (!lcd->lcd_dev)
140 return -ENODEV;
141 if (signal_pending(current)) {
142 return bytes_written ? bytes_written : -EINTR;
143 }
144
145 result = usb_bulk_msg(lcd->lcd_dev,
146 usb_sndbulkpipe(lcd->lcd_dev, 1),
147 obuf, thistime, &partial, 10 * HZ);
148
149 dbg("write stats: result:%d thistime:%lu partial:%u",
150 result, thistime, partial);
151
152 if (result == -ETIMEDOUT) { /* NAK - so hold for a while */
153 if (!maxretry--) {
154 return -ETIME;
155 }
156 interruptible_sleep_on_timeout(&lcd-> wait_q, NAK_TIMEOUT);
157 continue;
158 } else if (!result && partial) {
159 obuf += partial;
160 thistime -= partial;
161 } else
162 break;
163 };
164 if (result) {
165 err("Write Whoops - %x", result);
166 return -EIO;
167 }
168 bytes_written += copy_size;
169 count -= copy_size;
170 buffer += copy_size;
171 } while (count > 0);
172
173 return bytes_written ? bytes_written : -EIO;
174 }
175
176 static ssize_t
177 read_lcd(struct file *file, char __user *buffer, size_t count, loff_t * ppos)
178 {
179 struct lcd_usb_data *lcd = &lcd_instance;
180 ssize_t read_count;
181 unsigned int partial;
182 int this_read;
183 int result;
184 int maxretry = 10;
185 char *ibuf = lcd->ibuf;
186
187 /* Sanity check to make sure lcd is connected, powered, etc */
188 if (lcd == NULL ||
189 lcd->present == 0 ||
190 lcd->lcd_dev == NULL)
191 return -1;
192
193 read_count = 0;
194
195 while (count > 0) {
196 if (signal_pending(current)) {
197 return read_count ? read_count : -EINTR;
198 }
199 if (!lcd->lcd_dev)
200 return -ENODEV;
201 this_read = (count >= IBUF_SIZE) ? IBUF_SIZE : count;
202
203 result = usb_bulk_msg(lcd->lcd_dev,
204 usb_rcvbulkpipe(lcd->lcd_dev, 0),
205 ibuf, this_read, &partial,
206 (int) (HZ * 8));
207
208 dbg(KERN_DEBUG "read stats: result:%d this_read:%u partial:%u",
209 result, this_read, partial);
210
211 if (partial) {
212 count = this_read = partial;
213 } else if (result == -ETIMEDOUT || result == 15) { /* FIXME: 15 ??? */
214 if (!maxretry--) {
215 err("read_lcd: maxretry timeout");
216 return -ETIME;
217 }
218 interruptible_sleep_on_timeout(&lcd->wait_q,
219 NAK_TIMEOUT);
220 continue;
221 } else if (result != -EREMOTEIO) {
222 err("Read Whoops - result:%u partial:%u this_read:%u",
223 result, partial, this_read);
224 return -EIO;
225 } else {
226 return (0);
227 }
228
229 if (this_read) {
230 if (copy_to_user(buffer, ibuf, this_read))
231 return -EFAULT;
232 count -= this_read;
233 read_count += this_read;
234 buffer += this_read;
235 }
236 }
237 return read_count;
238 }
239
240 static struct
241 file_operations usb_lcd_fops = {
242 .owner = THIS_MODULE,
243 .read = read_lcd,
244 .write = write_lcd,
245 .ioctl = ioctl_lcd,
246 .open = open_lcd,
247 .release = close_lcd,
248 };
249
250 static struct usb_class_driver usb_lcd_class = {
251 .name = "usb/lcd%d",
252 .fops = &usb_lcd_fops,
253 .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
254 .minor_base = USBLCD_MINOR,
255 };
256
257 static int probe_lcd(struct usb_interface *intf, const struct usb_device_id *id)
258 {
259 struct usb_device *dev = interface_to_usbdev(intf);
260 struct lcd_usb_data *lcd = &lcd_instance;
261 int i;
262 int retval;
263
264 if (le16_to_cpu(dev->descriptor.idProduct) != 0x0001) {
265 warn(KERN_INFO "USBLCD model not supported.");
266 return -ENODEV;
267 }
268
269 if (lcd->present == 1) {
270 warn(KERN_INFO "Multiple USBLCDs are not supported!");
271 return -ENODEV;
272 }
273
274 i = le16_to_cpu(dev->descriptor.bcdDevice);
275
276 info("USBLCD Version %1d%1d.%1d%1d found at address %d",
277 (i & 0xF000)>>12,(i & 0xF00)>>8,(i & 0xF0)>>4,(i & 0xF),
278 dev->devnum);
279
280
281
282 lcd->present = 1;
283 lcd->lcd_dev = dev;
284
285 if (!(lcd->obuf = (char *) kmalloc(OBUF_SIZE, GFP_KERNEL))) {
286 err("probe_lcd: Not enough memory for the output buffer");
287 return -ENOMEM;
288 }
289 dbg("probe_lcd: obuf address:%p", lcd->obuf);
290
291 if (!(lcd->ibuf = (char *) kmalloc(IBUF_SIZE, GFP_KERNEL))) {
292 err("probe_lcd: Not enough memory for the input buffer");
293 kfree(lcd->obuf);
294 return -ENOMEM;
295 }
296 dbg("probe_lcd: ibuf address:%p", lcd->ibuf);
297
298 retval = usb_register_dev(intf, &usb_lcd_class);
299 if (retval) {
300 err("Not able to get a minor for this device.");
301 kfree(lcd->obuf);
302 kfree(lcd->ibuf);
303 return -ENOMEM;
304 }
305
306 usb_set_intfdata (intf, lcd);
307 return 0;
308 }
309
310 static void disconnect_lcd(struct usb_interface *intf)
311 {
312 struct lcd_usb_data *lcd = usb_get_intfdata (intf);
313
314 usb_set_intfdata (intf, NULL);
315 if (lcd) {
316 usb_deregister_dev(intf, &usb_lcd_class);
317
318 if (lcd->isopen) {
319 lcd->isopen = 0;
320 /* better let it finish - the release will do whats needed */
321 lcd->lcd_dev = NULL;
322 return;
323 }
324 kfree(lcd->ibuf);
325 kfree(lcd->obuf);
326
327 info("USBLCD disconnected.");
328
329 lcd->present = 0;
330 }
331 }
332
333 static struct usb_device_id id_table [] = {
334 { .idVendor = 0x10D2, .match_flags = USB_DEVICE_ID_MATCH_VENDOR, },
335 {},
336 };
337
338 MODULE_DEVICE_TABLE (usb, id_table);
339
340 static struct usb_driver lcd_driver = {
341 .owner = THIS_MODULE,
342 .name = "usblcd",
343 .probe = (void *)probe_lcd,
344 .disconnect = disconnect_lcd,
345 .id_table = id_table,
346 };
347
348 static int __init usb_lcd_init(void)
349 {
350 int retval;
351 retval = usb_register(&lcd_driver);
352 if (retval)
353 goto out;
354
355 info("%s (C) Adams IT Services http://www.usblcd.de", DRIVER_VERSION);
356 info("USBLCD support registered.");
357 out:
358 return retval;
359 }
360
361
362 static void __exit usb_lcd_cleanup(void)
363 {
364 struct lcd_usb_data *lcd = &lcd_instance;
365
366 lcd->present = 0;
367 usb_deregister(&lcd_driver);
368 }
369
370 module_init(usb_lcd_init);
371 module_exit(usb_lcd_cleanup);
372
373 MODULE_AUTHOR("Adams IT Services <info@usblcd.de>");
374 MODULE_DESCRIPTION(DRIVER_VERSION);
375 MODULE_LICENSE("GPL");
376
|
This page was automatically generated by the
LXR engine.
|