1 /* Hey EMACS -*- linux-c -*-
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.
7 * Copyright (C) 2000-2002, Romain Lievin <roms@lpg.ticalc.org>
8 * under the terms of the GNU General Public License.
10 * Various fixes & clean-up from the Linux Kernel Mailing List
11 * (Alan Cox, Richard B. Johnson, Christoph Hellwig).
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
18 * If this driver is built into the kernel, you can configure it using the
19 * kernel command-line. For example:
21 * tipar=timeout,delay (set timeout and delay)
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:
26 * # insmod tipar timeout=15 delay=10
29 /* COMPATIBILITY WITH OLD KERNELS
31 * Usually, parallel cables were bound to ports at
32 * particular I/O addresses, as follows:
39 * This driver, by default, binds tipar devices according to parport and
43 #undef DEBUG /* change to #define to get debugging
44 * output - for pr_debug() */
45 #include <linux/module.h>
46 #include <linux/types.h>
47 #include <linux/errno.h>
48 #include <linux/kernel.h>
49 #include <linux/sched.h>
50 #include <linux/delay.h>
51 #include <linux/fcntl.h>
53 #include <linux/init.h>
54 #include <asm/uaccess.h>
55 #include <linux/ioport.h>
57 #include <linux/bitops.h>
58 #include <linux/parport.h> /* Our code depend on parport */
59 #include <linux/device.h>
64 #include <linux/ticable.h>
69 #define DRIVER_VERSION "1.19"
70 #define DRIVER_AUTHOR "Romain Lievin <roms@lpg.ticalc.org>"
71 #define DRIVER_DESC "Device driver for TI/PC parallel link cables"
72 #define DRIVER_LICENSE "GPL"
74 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
76 /* ----- global variables --------------------------------------------- */
79 struct pardevice *dev; /* Parport device entry */
83 static struct tipar_struct table[PP_NO];
85 static int delay = IO_DELAY; /* inter-bit delay in microseconds */
86 static int timeout = TIMAXTIME; /* timeout in tenth of seconds */
88 static unsigned int tp_count; /* tipar count */
89 static unsigned long opened; /* opened devices */
91 static struct class *tipar_class;
93 /* --- macros for parport access -------------------------------------- */
95 #define r_dtr(x) (parport_read_data(table[(x)].dev->port))
96 #define r_str(x) (parport_read_status(table[(x)].dev->port))
97 #define w_ctr(x,y) (parport_write_control(table[(x)].dev->port, (y)))
98 #define w_dtr(x,y) (parport_write_data(table[(x)].dev->port, (y)))
100 /* --- setting states on the D-bus with the right timing: ------------- */
103 outbyte(int value, int minor)
111 return (r_str(minor));
115 init_ti_parallel(int minor)
120 /* ----- global defines ----------------------------------------------- */
122 #define START(x) { x = jiffies + (HZ * timeout) / 10; }
124 if (time_before((x), jiffies)) return -1; \
125 if (need_resched()) schedule(); }
127 /* ----- D-bus bit-banging functions ---------------------------------- */
129 /* D-bus protocol (45kbit/s max):
131 _______ ______|______ __________|________ __________
132 Red : ________ | ____ | ____
133 _ ____________|________ ______|__________ _____
134 White: ________ | ______ | _______
137 /* Try to transmit a byte on the specified port (-1 if error). */
139 put_ti_parallel(int minor, unsigned char data)
144 for (bit = 0; bit < 8; bit++) {
150 } while (inbyte(minor) & 0x10);
156 } while (!(inbyte(minor) & 0x10));
162 } while (inbyte(minor) & 0x20);
168 } while (!(inbyte(minor) & 0x20));
181 /* Receive a byte on the specified port or -1 if error. */
183 get_ti_parallel(int minor)
186 unsigned char v, data = 0;
189 for (bit = 0; bit < 8; bit++) {
193 } while ((v = inbyte(minor) & 0x30) == 0x30);
196 data = (data >> 1) | 0x80;
201 } while (!(inbyte(minor) & 0x20));
209 } while (!(inbyte(minor) & 0x10));
221 /* Try to detect a parallel link cable on the specified port */
223 probe_ti_parallel(int minor)
226 int seq[] = { 0x00, 0x20, 0x10, 0x30 };
228 for (i = 3; i >= 0; i--) {
232 pr_debug("tipar: Probing -> %i: 0x%02x 0x%02x\n", i,
233 data & 0x30, seq[i]);
234 if ((inbyte(minor) & 0x30) != seq[i]) {
244 /* ----- kernel module functions--------------------------------------- */
247 tipar_open(struct inode *inode, struct file *file)
249 unsigned int minor = iminor(inode) - TIPAR_MINOR;
251 if (tp_count == 0 || minor > tp_count - 1)
254 if (test_and_set_bit(minor, &opened))
257 if (!table[minor].dev) {
258 printk(KERN_ERR "%s: NULL device for minor %u\n",
259 __FUNCTION__, minor);
262 parport_claim_or_block(table[minor].dev);
263 init_ti_parallel(minor);
264 parport_release(table[minor].dev);
266 return nonseekable_open(inode, file);
270 tipar_close(struct inode *inode, struct file *file)
272 unsigned int minor = iminor(inode) - TIPAR_MINOR;
274 if (minor > tp_count - 1)
277 clear_bit(minor, &opened);
283 tipar_write (struct file *file, const char __user *buf, size_t count,
286 unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
289 parport_claim_or_block(table[minor].dev);
291 for (n = 0; n < count; n++) {
294 if (get_user(b, buf + n)) {
299 if (put_ti_parallel(minor, b) == -1) {
300 init_ti_parallel(minor);
306 parport_release(table[minor].dev);
311 tipar_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
314 unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
321 parport_claim_or_block(table[minor].dev);
324 b = get_ti_parallel(minor);
326 init_ti_parallel(minor);
330 if (put_user(b, buf + n)) {
337 /* Non-blocking mode : try again ! */
338 if (file->f_flags & O_NONBLOCK) {
343 /* Signal pending, try again ! */
344 if (signal_pending(current)) {
345 retval = -ERESTARTSYS;
354 parport_release(table[minor].dev);
359 tipar_ioctl(struct inode *inode, struct file *file,
360 unsigned int cmd, unsigned long arg)
365 case IOCTL_TIPAR_DELAY:
366 delay = (int)arg; //get_user(delay, &arg);
368 case IOCTL_TIPAR_TIMEOUT:
382 /* ----- kernel module registering ------------------------------------ */
384 static const struct file_operations tipar_fops = {
385 .owner = THIS_MODULE,
388 .write = tipar_write,
389 .ioctl = tipar_ioctl,
391 .release = tipar_close,
394 /* --- initialisation code ------------------------------------- */
397 /* You must set these - there is no sane way to probe for this cable.
398 * You can use 'tipar=timeout,delay' to set these now. */
400 tipar_setup(char *str)
404 str = get_options(str, ARRAY_SIZE(ints), ints);
410 printk(KERN_WARNING "tipar: bad timeout value (0), "
411 "using default value instead");
422 * Register our module into parport.
423 * Pass also 2 callbacks functions to parport: a pre-emptive function and an
424 * interrupt handler function (unused).
425 * Display a message such "tipar0: using parport0 (polling)".
428 tipar_register(int nr, struct parport *port)
432 /* Register our module into parport */
433 table[nr].dev = parport_register_device(port, "tipar",
435 (void *) &table[nr]);
437 if (table[nr].dev == NULL) {
442 class_device_create(tipar_class, NULL, MKDEV(TIPAR_MAJOR,
443 TIPAR_MINOR + nr), NULL, "par%d", nr);
445 /* Display informations */
446 pr_info("tipar%d: using %s (%s)\n", nr, port->name, (port->irq ==
447 PARPORT_IRQ_NONE) ? "polling" : "interrupt-driven");
449 if (probe_ti_parallel(nr) != -1)
450 pr_info("tipar%d: link cable found\n", nr);
452 pr_info("tipar%d: link cable not found\n", nr);
461 tipar_attach(struct parport *port)
463 if (tp_count == PP_NO) {
464 pr_info("tipar: ignoring parallel port (max. %d)\n", PP_NO);
468 if (!tipar_register(tp_count, port))
473 tipar_detach(struct parport *port)
478 static struct parport_driver tipar_driver = {
480 .attach = tipar_attach,
481 .detach = tipar_detach,
485 tipar_init_module(void)
489 pr_info("tipar: parallel link cable driver, version %s\n",
492 if (register_chrdev(TIPAR_MAJOR, "tipar", &tipar_fops)) {
493 printk(KERN_ERR "tipar: unable to get major %d\n", TIPAR_MAJOR);
498 tipar_class = class_create(THIS_MODULE, "ticables");
499 if (IS_ERR(tipar_class)) {
500 err = PTR_ERR(tipar_class);
503 if (parport_register_driver(&tipar_driver)) {
504 printk(KERN_ERR "tipar: unable to register with parport\n");
513 class_destroy(tipar_class);
516 unregister_chrdev(TIPAR_MAJOR, "tipar");
522 tipar_cleanup_module(void)
526 /* Unregistering module */
527 parport_unregister_driver(&tipar_driver);
529 unregister_chrdev(TIPAR_MAJOR, "tipar");
531 for (i = 0; i < PP_NO; i++) {
532 if (table[i].dev == NULL)
534 parport_unregister_device(table[i].dev);
535 class_device_destroy(tipar_class, MKDEV(TIPAR_MAJOR, i));
537 class_destroy(tipar_class);
539 pr_info("tipar: module unloaded\n");
542 /* --------------------------------------------------------------------- */
544 __setup("tipar=", tipar_setup);
545 module_init(tipar_init_module);
546 module_exit(tipar_cleanup_module);
548 MODULE_AUTHOR(DRIVER_AUTHOR);
549 MODULE_DESCRIPTION(DRIVER_DESC);
550 MODULE_LICENSE(DRIVER_LICENSE);
552 module_param(timeout, int, 0);
553 MODULE_PARM_DESC(timeout, "Timeout (default=1.5 seconds)");
554 module_param(delay, int, 0);
555 MODULE_PARM_DESC(delay, "Inter-bit delay (default=10 microseconds)");