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
44 #include <linux/config.h>
45 #include <linux/module.h>
46 #include <linux/version.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>
54 #include <linux/init.h>
55 #include <asm/uaccess.h>
56 #include <linux/ioport.h>
58 #include <asm/bitops.h>
59 #include <linux/devfs_fs_kernel.h> /* DevFs support */
60 #include <linux/parport.h> /* Our code depend on parport */
61 #include <linux/device.h>
66 #include <linux/ticable.h>
71 #define DRIVER_VERSION "1.19"
72 #define DRIVER_AUTHOR "Romain Lievin <roms@lpg.ticalc.org>"
73 #define DRIVER_DESC "Device driver for TI/PC parallel link cables"
74 #define DRIVER_LICENSE "GPL"
76 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
77 #if LINUX_VERSION_CODE < VERSION(2,5,0)
78 # define need_resched() (current->need_resched)
81 /* ----- global variables --------------------------------------------- */
84 struct pardevice *dev; /* Parport device entry */
88 static struct tipar_struct table[PP_NO];
90 static int delay = IO_DELAY; /* inter-bit delay in microseconds */
91 static int timeout = TIMAXTIME; /* timeout in tenth of seconds */
93 static unsigned int tp_count; /* tipar count */
94 static unsigned long opened; /* opened devices */
96 static struct class_simple *tipar_class;
98 /* --- macros for parport access -------------------------------------- */
100 #define r_dtr(x) (parport_read_data(table[(x)].dev->port))
101 #define r_str(x) (parport_read_status(table[(x)].dev->port))
102 #define w_ctr(x,y) (parport_write_control(table[(x)].dev->port, (y)))
103 #define w_dtr(x,y) (parport_write_data(table[(x)].dev->port, (y)))
105 /* --- setting states on the D-bus with the right timing: ------------- */
108 outbyte(int value, int minor)
116 return (r_str(minor));
120 init_ti_parallel(int minor)
125 /* ----- global defines ----------------------------------------------- */
127 #define START(x) { x = jiffies + (HZ * timeout) / 10; }
129 if (time_before((x), jiffies)) return -1; \
130 if (need_resched()) schedule(); }
132 /* ----- D-bus bit-banging functions ---------------------------------- */
134 /* D-bus protocol (45kbit/s max):
136 _______ ______|______ __________|________ __________
137 Red : ________ | ____ | ____
138 _ ____________|________ ______|__________ _____
139 White: ________ | ______ | _______
142 /* Try to transmit a byte on the specified port (-1 if error). */
144 put_ti_parallel(int minor, unsigned char data)
149 for (bit = 0; bit < 8; bit++) {
155 } while (inbyte(minor) & 0x10);
161 } while (!(inbyte(minor) & 0x10));
167 } while (inbyte(minor) & 0x20);
173 } while (!(inbyte(minor) & 0x20));
186 /* Receive a byte on the specified port or -1 if error. */
188 get_ti_parallel(int minor)
191 unsigned char v, data = 0;
194 for (bit = 0; bit < 8; bit++) {
198 } while ((v = inbyte(minor) & 0x30) == 0x30);
201 data = (data >> 1) | 0x80;
206 } while (!(inbyte(minor) & 0x20));
214 } while (!(inbyte(minor) & 0x10));
226 /* Try to detect a parallel link cable on the specified port */
228 probe_ti_parallel(int minor)
231 int seq[] = { 0x00, 0x20, 0x10, 0x30 };
233 for (i = 3; i >= 0; i--) {
237 /*printk(KERN_DEBUG "Probing -> %i: 0x%02x 0x%02x\n", i, data & 0x30, seq[i]); */
238 if ((inbyte(minor) & 0x30) != seq[i]) {
248 /* ----- kernel module functions--------------------------------------- */
251 tipar_open(struct inode *inode, struct file *file)
253 unsigned int minor = iminor(inode) - TIPAR_MINOR;
255 if (minor > tp_count - 1)
258 if (test_and_set_bit(minor, &opened))
261 parport_claim_or_block(table[minor].dev);
262 init_ti_parallel(minor);
263 parport_release(table[minor].dev);
269 tipar_close(struct inode *inode, struct file *file)
271 unsigned int minor = iminor(inode) - TIPAR_MINOR;
273 if (minor > tp_count - 1)
276 clear_bit(minor, &opened);
282 tipar_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
284 unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
287 parport_claim_or_block(table[minor].dev);
289 for (n = 0; n < count; n++) {
292 if (get_user(b, buf + n)) {
297 if (put_ti_parallel(minor, b) == -1) {
298 init_ti_parallel(minor);
304 parport_release(table[minor].dev);
309 tipar_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
312 unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
319 if (ppos != &file->f_pos)
322 parport_claim_or_block(table[minor].dev);
325 b = get_ti_parallel(minor);
327 init_ti_parallel(minor);
331 if (put_user(b, buf + n)) {
338 /* Non-blocking mode : try again ! */
339 if (file->f_flags & O_NONBLOCK) {
344 /* Signal pending, try again ! */
345 if (signal_pending(current)) {
346 retval = -ERESTARTSYS;
355 parport_release(table[minor].dev);
360 tipar_ioctl(struct inode *inode, struct file *file,
361 unsigned int cmd, unsigned long arg)
366 case IOCTL_TIPAR_DELAY:
367 delay = (int)arg; //get_user(delay, &arg);
369 case IOCTL_TIPAR_TIMEOUT:
383 /* ----- kernel module registering ------------------------------------ */
385 static struct file_operations tipar_fops = {
386 .owner = THIS_MODULE,
389 .write = tipar_write,
390 .ioctl = tipar_ioctl,
392 .release = tipar_close,
395 /* --- initialisation code ------------------------------------- */
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. */
401 tipar_setup(char *str)
405 str = get_options(str, ARRAY_SIZE(ints), ints);
411 printk("tipar: wrong timeout value (0), 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_simple_device_add(tipar_class, MKDEV(TIPAR_MAJOR, TIPAR_MINOR + nr),
444 /* Use devfs, tree: /dev/ticables/par/[0..2] */
445 err = devfs_mk_cdev(MKDEV(TIPAR_MAJOR, TIPAR_MINOR + nr),
446 S_IFCHR | S_IRUGO | S_IWUGO,
447 "ticables/par/%d", nr);
451 /* Display informations */
452 printk(KERN_INFO "tipar%d: using %s (%s).\n", nr, port->name,
454 PARPORT_IRQ_NONE) ? "polling" : "interrupt-driven");
456 if (probe_ti_parallel(nr) != -1)
457 printk("tipar%d: link cable found !\n", nr);
459 printk("tipar%d: link cable not found.\n", nr);
465 class_simple_device_remove(MKDEV(TIPAR_MAJOR, TIPAR_MINOR + nr));
466 class_simple_destroy(tipar_class);
472 tipar_attach(struct parport *port)
474 if (tp_count == PP_NO) {
475 printk("tipar: ignoring parallel port (max. %d)\n", PP_NO);
479 if (!tipar_register(tp_count, port))
484 tipar_detach(struct parport *port)
489 static struct parport_driver tipar_driver = {
491 .attach = tipar_attach,
492 .detach = tipar_detach,
496 tipar_init_module(void)
500 printk("tipar: parallel link cable driver, version %s\n",
503 if (register_chrdev(TIPAR_MAJOR, "tipar", &tipar_fops)) {
504 printk("tipar: unable to get major %d\n", TIPAR_MAJOR);
509 /* Use devfs with tree: /dev/ticables/par/[0..2] */
510 devfs_mk_dir("ticables/par");
512 tipar_class = class_simple_create(THIS_MODULE, "ticables");
513 if (IS_ERR(tipar_class)) {
514 err = PTR_ERR(tipar_class);
517 if (parport_register_driver(&tipar_driver)) {
518 printk("tipar: unable to register with parport\n");
527 unregister_chrdev(TIPAR_MAJOR, "tipar");
533 tipar_cleanup_module(void)
537 /* Unregistering module */
538 parport_unregister_driver(&tipar_driver);
540 unregister_chrdev(TIPAR_MAJOR, "tipar");
542 for (i = 0; i < PP_NO; i++) {
543 if (table[i].dev == NULL)
545 parport_unregister_device(table[i].dev);
546 class_simple_device_remove(MKDEV(TIPAR_MAJOR, i));
547 devfs_remove("ticables/par/%d", i);
549 class_simple_destroy(tipar_class);
550 devfs_remove("ticables/par");
552 printk("tipar: module unloaded !\n");
555 /* --------------------------------------------------------------------- */
557 __setup("tipar=", tipar_setup);
558 module_init(tipar_init_module);
559 module_exit(tipar_cleanup_module);
561 MODULE_AUTHOR(DRIVER_AUTHOR);
562 MODULE_DESCRIPTION(DRIVER_DESC);
563 MODULE_LICENSE(DRIVER_LICENSE);
565 MODULE_PARM(timeout, "i");
566 MODULE_PARM_DESC(timeout, "Timeout (default=1.5 seconds)");
567 MODULE_PARM(delay, "i");
568 MODULE_PARM_DESC(delay, "Inter-bit delay (default=10 microseconds)");