[PATCH] devfs: Remove the devfs_fs_kernel.h file from the tree
[deliverable/linux.git] / drivers / char / tipar.c
1 /* Hey EMACS -*- linux-c -*-
2 *
3 * tipar - low level driver for handling a parallel link cable designed
4 * for Texas Instruments graphing calculators (http://lpg.ticalc.org).
5 * A part of the TiLP project.
6 *
7 * Copyright (C) 2000-2002, Romain Lievin <roms@lpg.ticalc.org>
8 * under the terms of the GNU General Public License.
9 *
10 * Various fixes & clean-up from the Linux Kernel Mailing List
11 * (Alan Cox, Richard B. Johnson, Christoph Hellwig).
12 */
13
14 /* This driver should, in theory, work with any parallel port that has an
15 * appropriate low-level driver; all I/O is done through the parport
16 * abstraction layer.
17 *
18 * If this driver is built into the kernel, you can configure it using the
19 * kernel command-line. For example:
20 *
21 * tipar=timeout,delay (set timeout and delay)
22 *
23 * If the driver is loaded as a module, similar functionality is available
24 * using module parameters. The equivalent of the above commands would be:
25 *
26 * # insmod tipar timeout=15 delay=10
27 */
28
29 /* COMPATIBILITY WITH OLD KERNELS
30 *
31 * Usually, parallel cables were bound to ports at
32 * particular I/O addresses, as follows:
33 *
34 * tipar0 0x378
35 * tipar1 0x278
36 * tipar2 0x3bc
37 *
38 *
39 * This driver, by default, binds tipar devices according to parport and
40 * the minor number.
41 *
42 */
43 #undef DEBUG /* change to #define to get debugging
44 * output - for pr_debug() */
45 #include <linux/config.h>
46 #include <linux/module.h>
47 #include <linux/types.h>
48 #include <linux/errno.h>
49 #include <linux/kernel.h>
50 #include <linux/sched.h>
51 #include <linux/delay.h>
52 #include <linux/fcntl.h>
53 #include <linux/fs.h>
54 #include <linux/init.h>
55 #include <asm/uaccess.h>
56 #include <linux/ioport.h>
57 #include <asm/io.h>
58 #include <linux/bitops.h>
59 #include <linux/parport.h> /* Our code depend on parport */
60 #include <linux/device.h>
61
62 /*
63 * TI definitions
64 */
65 #include <linux/ticable.h>
66
67 /*
68 * Version Information
69 */
70 #define DRIVER_VERSION "1.19"
71 #define DRIVER_AUTHOR "Romain Lievin <roms@lpg.ticalc.org>"
72 #define DRIVER_DESC "Device driver for TI/PC parallel link cables"
73 #define DRIVER_LICENSE "GPL"
74
75 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
76
77 /* ----- global variables --------------------------------------------- */
78
79 struct tipar_struct {
80 struct pardevice *dev; /* Parport device entry */
81 };
82
83 #define PP_NO 3
84 static struct tipar_struct table[PP_NO];
85
86 static int delay = IO_DELAY; /* inter-bit delay in microseconds */
87 static int timeout = TIMAXTIME; /* timeout in tenth of seconds */
88
89 static unsigned int tp_count; /* tipar count */
90 static unsigned long opened; /* opened devices */
91
92 static struct class *tipar_class;
93
94 /* --- macros for parport access -------------------------------------- */
95
96 #define r_dtr(x) (parport_read_data(table[(x)].dev->port))
97 #define r_str(x) (parport_read_status(table[(x)].dev->port))
98 #define w_ctr(x,y) (parport_write_control(table[(x)].dev->port, (y)))
99 #define w_dtr(x,y) (parport_write_data(table[(x)].dev->port, (y)))
100
101 /* --- setting states on the D-bus with the right timing: ------------- */
102
103 static inline void
104 outbyte(int value, int minor)
105 {
106 w_dtr(minor, value);
107 }
108
109 static inline int
110 inbyte(int minor)
111 {
112 return (r_str(minor));
113 }
114
115 static inline void
116 init_ti_parallel(int minor)
117 {
118 outbyte(3, minor);
119 }
120
121 /* ----- global defines ----------------------------------------------- */
122
123 #define START(x) { x = jiffies + (HZ * timeout) / 10; }
124 #define WAIT(x) { \
125 if (time_before((x), jiffies)) return -1; \
126 if (need_resched()) schedule(); }
127
128 /* ----- D-bus bit-banging functions ---------------------------------- */
129
130 /* D-bus protocol (45kbit/s max):
131 1 0 0
132 _______ ______|______ __________|________ __________
133 Red : ________ | ____ | ____
134 _ ____________|________ ______|__________ _____
135 White: ________ | ______ | _______
136 */
137
138 /* Try to transmit a byte on the specified port (-1 if error). */
139 static int
140 put_ti_parallel(int minor, unsigned char data)
141 {
142 unsigned int bit;
143 unsigned long max;
144
145 for (bit = 0; bit < 8; bit++) {
146 if (data & 1) {
147 outbyte(2, minor);
148 START(max);
149 do {
150 WAIT(max);
151 } while (inbyte(minor) & 0x10);
152
153 outbyte(3, minor);
154 START(max);
155 do {
156 WAIT(max);
157 } while (!(inbyte(minor) & 0x10));
158 } else {
159 outbyte(1, minor);
160 START(max);
161 do {
162 WAIT(max);
163 } while (inbyte(minor) & 0x20);
164
165 outbyte(3, minor);
166 START(max);
167 do {
168 WAIT(max);
169 } while (!(inbyte(minor) & 0x20));
170 }
171
172 data >>= 1;
173 udelay(delay);
174
175 if (need_resched())
176 schedule();
177 }
178
179 return 0;
180 }
181
182 /* Receive a byte on the specified port or -1 if error. */
183 static int
184 get_ti_parallel(int minor)
185 {
186 unsigned int bit;
187 unsigned char v, data = 0;
188 unsigned long max;
189
190 for (bit = 0; bit < 8; bit++) {
191 START(max);
192 do {
193 WAIT(max);
194 } while ((v = inbyte(minor) & 0x30) == 0x30);
195
196 if (v == 0x10) {
197 data = (data >> 1) | 0x80;
198 outbyte(1, minor);
199 START(max);
200 do {
201 WAIT(max);
202 } while (!(inbyte(minor) & 0x20));
203 outbyte(3, minor);
204 } else {
205 data = data >> 1;
206 outbyte(2, minor);
207 START(max);
208 do {
209 WAIT(max);
210 } while (!(inbyte(minor) & 0x10));
211 outbyte(3, minor);
212 }
213
214 udelay(delay);
215 if (need_resched())
216 schedule();
217 }
218
219 return (int) data;
220 }
221
222 /* Try to detect a parallel link cable on the specified port */
223 static int
224 probe_ti_parallel(int minor)
225 {
226 int i;
227 int seq[] = { 0x00, 0x20, 0x10, 0x30 };
228
229 for (i = 3; i >= 0; i--) {
230 outbyte(3, minor);
231 outbyte(i, minor);
232 udelay(delay);
233 pr_debug("tipar: Probing -> %i: 0x%02x 0x%02x\n", i,
234 data & 0x30, seq[i]);
235 if ((inbyte(minor) & 0x30) != seq[i]) {
236 outbyte(3, minor);
237 return -1;
238 }
239 }
240
241 outbyte(3, minor);
242 return 0;
243 }
244
245 /* ----- kernel module functions--------------------------------------- */
246
247 static int
248 tipar_open(struct inode *inode, struct file *file)
249 {
250 unsigned int minor = iminor(inode) - TIPAR_MINOR;
251
252 if (tp_count == 0 || minor > tp_count - 1)
253 return -ENXIO;
254
255 if (test_and_set_bit(minor, &opened))
256 return -EBUSY;
257
258 if (!table[minor].dev) {
259 printk(KERN_ERR "%s: NULL device for minor %u\n",
260 __FUNCTION__, minor);
261 return -ENXIO;
262 }
263 parport_claim_or_block(table[minor].dev);
264 init_ti_parallel(minor);
265 parport_release(table[minor].dev);
266
267 return nonseekable_open(inode, file);
268 }
269
270 static int
271 tipar_close(struct inode *inode, struct file *file)
272 {
273 unsigned int minor = iminor(inode) - TIPAR_MINOR;
274
275 if (minor > tp_count - 1)
276 return -ENXIO;
277
278 clear_bit(minor, &opened);
279
280 return 0;
281 }
282
283 static ssize_t
284 tipar_write (struct file *file, const char __user *buf, size_t count,
285 loff_t * ppos)
286 {
287 unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
288 ssize_t n;
289
290 parport_claim_or_block(table[minor].dev);
291
292 for (n = 0; n < count; n++) {
293 unsigned char b;
294
295 if (get_user(b, buf + n)) {
296 n = -EFAULT;
297 goto out;
298 }
299
300 if (put_ti_parallel(minor, b) == -1) {
301 init_ti_parallel(minor);
302 n = -ETIMEDOUT;
303 goto out;
304 }
305 }
306 out:
307 parport_release(table[minor].dev);
308 return n;
309 }
310
311 static ssize_t
312 tipar_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
313 {
314 int b = 0;
315 unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
316 ssize_t retval = 0;
317 ssize_t n = 0;
318
319 if (count == 0)
320 return 0;
321
322 parport_claim_or_block(table[minor].dev);
323
324 while (n < count) {
325 b = get_ti_parallel(minor);
326 if (b == -1) {
327 init_ti_parallel(minor);
328 retval = -ETIMEDOUT;
329 goto out;
330 } else {
331 if (put_user(b, buf + n)) {
332 retval = -EFAULT;
333 break;
334 } else
335 retval = ++n;
336 }
337
338 /* Non-blocking mode : try again ! */
339 if (file->f_flags & O_NONBLOCK) {
340 retval = -EAGAIN;
341 goto out;
342 }
343
344 /* Signal pending, try again ! */
345 if (signal_pending(current)) {
346 retval = -ERESTARTSYS;
347 goto out;
348 }
349
350 if (need_resched())
351 schedule();
352 }
353
354 out:
355 parport_release(table[minor].dev);
356 return retval;
357 }
358
359 static int
360 tipar_ioctl(struct inode *inode, struct file *file,
361 unsigned int cmd, unsigned long arg)
362 {
363 int retval = 0;
364
365 switch (cmd) {
366 case IOCTL_TIPAR_DELAY:
367 delay = (int)arg; //get_user(delay, &arg);
368 break;
369 case IOCTL_TIPAR_TIMEOUT:
370 if (arg != 0)
371 timeout = (int)arg;
372 else
373 retval = -EINVAL;
374 break;
375 default:
376 retval = -ENOTTY;
377 break;
378 }
379
380 return retval;
381 }
382
383 /* ----- kernel module registering ------------------------------------ */
384
385 static struct file_operations tipar_fops = {
386 .owner = THIS_MODULE,
387 .llseek = no_llseek,
388 .read = tipar_read,
389 .write = tipar_write,
390 .ioctl = tipar_ioctl,
391 .open = tipar_open,
392 .release = tipar_close,
393 };
394
395 /* --- initialisation code ------------------------------------- */
396
397 #ifndef MODULE
398 /* You must set these - there is no sane way to probe for this cable.
399 * You can use 'tipar=timeout,delay' to set these now. */
400 static int __init
401 tipar_setup(char *str)
402 {
403 int ints[3];
404
405 str = get_options(str, ARRAY_SIZE(ints), ints);
406
407 if (ints[0] > 0) {
408 if (ints[1] != 0)
409 timeout = ints[1];
410 else
411 printk(KERN_WARNING "tipar: bad timeout value (0), "
412 "using default value instead");
413 if (ints[0] > 1) {
414 delay = ints[2];
415 }
416 }
417
418 return 1;
419 }
420 #endif
421
422 /*
423 * Register our module into parport.
424 * Pass also 2 callbacks functions to parport: a pre-emptive function and an
425 * interrupt handler function (unused).
426 * Display a message such "tipar0: using parport0 (polling)".
427 */
428 static int
429 tipar_register(int nr, struct parport *port)
430 {
431 int err = 0;
432
433 /* Register our module into parport */
434 table[nr].dev = parport_register_device(port, "tipar",
435 NULL, NULL, NULL, 0,
436 (void *) &table[nr]);
437
438 if (table[nr].dev == NULL) {
439 err = 1;
440 goto out;
441 }
442
443 class_device_create(tipar_class, NULL, MKDEV(TIPAR_MAJOR,
444 TIPAR_MINOR + nr), NULL, "par%d", nr);
445
446 /* Display informations */
447 pr_info("tipar%d: using %s (%s)\n", nr, port->name, (port->irq ==
448 PARPORT_IRQ_NONE) ? "polling" : "interrupt-driven");
449
450 if (probe_ti_parallel(nr) != -1)
451 pr_info("tipar%d: link cable found\n", nr);
452 else
453 pr_info("tipar%d: link cable not found\n", nr);
454
455 err = 0;
456
457 out:
458 return err;
459 }
460
461 static void
462 tipar_attach(struct parport *port)
463 {
464 if (tp_count == PP_NO) {
465 pr_info("tipar: ignoring parallel port (max. %d)\n", PP_NO);
466 return;
467 }
468
469 if (!tipar_register(tp_count, port))
470 tp_count++;
471 }
472
473 static void
474 tipar_detach(struct parport *port)
475 {
476 /* Nothing to do */
477 }
478
479 static struct parport_driver tipar_driver = {
480 .name = "tipar",
481 .attach = tipar_attach,
482 .detach = tipar_detach,
483 };
484
485 static int __init
486 tipar_init_module(void)
487 {
488 int err = 0;
489
490 pr_info("tipar: parallel link cable driver, version %s\n",
491 DRIVER_VERSION);
492
493 if (register_chrdev(TIPAR_MAJOR, "tipar", &tipar_fops)) {
494 printk(KERN_ERR "tipar: unable to get major %d\n", TIPAR_MAJOR);
495 err = -EIO;
496 goto out;
497 }
498
499 tipar_class = class_create(THIS_MODULE, "ticables");
500 if (IS_ERR(tipar_class)) {
501 err = PTR_ERR(tipar_class);
502 goto out_chrdev;
503 }
504 if (parport_register_driver(&tipar_driver)) {
505 printk(KERN_ERR "tipar: unable to register with parport\n");
506 err = -EIO;
507 goto out_class;
508 }
509
510 err = 0;
511 goto out;
512
513 out_class:
514 class_destroy(tipar_class);
515
516 out_chrdev:
517 unregister_chrdev(TIPAR_MAJOR, "tipar");
518 out:
519 return err;
520 }
521
522 static void __exit
523 tipar_cleanup_module(void)
524 {
525 unsigned int i;
526
527 /* Unregistering module */
528 parport_unregister_driver(&tipar_driver);
529
530 unregister_chrdev(TIPAR_MAJOR, "tipar");
531
532 for (i = 0; i < PP_NO; i++) {
533 if (table[i].dev == NULL)
534 continue;
535 parport_unregister_device(table[i].dev);
536 class_device_destroy(tipar_class, MKDEV(TIPAR_MAJOR, i));
537 }
538 class_destroy(tipar_class);
539
540 pr_info("tipar: module unloaded\n");
541 }
542
543 /* --------------------------------------------------------------------- */
544
545 __setup("tipar=", tipar_setup);
546 module_init(tipar_init_module);
547 module_exit(tipar_cleanup_module);
548
549 MODULE_AUTHOR(DRIVER_AUTHOR);
550 MODULE_DESCRIPTION(DRIVER_DESC);
551 MODULE_LICENSE(DRIVER_LICENSE);
552
553 module_param(timeout, int, 0);
554 MODULE_PARM_DESC(timeout, "Timeout (default=1.5 seconds)");
555 module_param(delay, int, 0);
556 MODULE_PARM_DESC(delay, "Inter-bit delay (default=10 microseconds)");
This page took 0.043096 seconds and 6 git commands to generate.