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/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>
54 #include <linux/init.h>
55 #include <asm/uaccess.h>
56 #include <linux/ioport.h>
58 #include <linux/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))
78 /* ----- global variables --------------------------------------------- */
81 struct pardevice *dev; /* Parport device entry */
85 static struct tipar_struct table[PP_NO];
87 static int delay = IO_DELAY; /* inter-bit delay in microseconds */
88 static int timeout = TIMAXTIME; /* timeout in tenth of seconds */
90 static unsigned int tp_count; /* tipar count */
91 static unsigned long opened; /* opened devices */
93 static struct class *tipar_class;
95 /* --- macros for parport access -------------------------------------- */
97 #define r_dtr(x) (parport_read_data(table[(x)].dev->port))
98 #define r_str(x) (parport_read_status(table[(x)].dev->port))
99 #define w_ctr(x,y) (parport_write_control(table[(x)].dev->port, (y)))
100 #define w_dtr(x,y) (parport_write_data(table[(x)].dev->port, (y)))
102 /* --- setting states on the D-bus with the right timing: ------------- */
105 outbyte(int value, int minor)
113 return (r_str(minor));
117 init_ti_parallel(int minor)
122 /* ----- global defines ----------------------------------------------- */
124 #define START(x) { x = jiffies + (HZ * timeout) / 10; }
126 if (time_before((x), jiffies)) return -1; \
127 if (need_resched()) schedule(); }
129 /* ----- D-bus bit-banging functions ---------------------------------- */
131 /* D-bus protocol (45kbit/s max):
133 _______ ______|______ __________|________ __________
134 Red : ________ | ____ | ____
135 _ ____________|________ ______|__________ _____
136 White: ________ | ______ | _______
139 /* Try to transmit a byte on the specified port (-1 if error). */
141 put_ti_parallel(int minor, unsigned char data)
146 for (bit = 0; bit < 8; bit++) {
152 } while (inbyte(minor) & 0x10);
158 } while (!(inbyte(minor) & 0x10));
164 } while (inbyte(minor) & 0x20);
170 } while (!(inbyte(minor) & 0x20));
183 /* Receive a byte on the specified port or -1 if error. */
185 get_ti_parallel(int minor)
188 unsigned char v, data = 0;
191 for (bit = 0; bit < 8; bit++) {
195 } while ((v = inbyte(minor) & 0x30) == 0x30);
198 data = (data >> 1) | 0x80;
203 } while (!(inbyte(minor) & 0x20));
211 } while (!(inbyte(minor) & 0x10));
223 /* Try to detect a parallel link cable on the specified port */
225 probe_ti_parallel(int minor)
228 int seq[] = { 0x00, 0x20, 0x10, 0x30 };
230 for (i = 3; i >= 0; i--) {
234 pr_debug("tipar: Probing -> %i: 0x%02x 0x%02x\n", i,
235 data & 0x30, seq[i]);
236 if ((inbyte(minor) & 0x30) != seq[i]) {
246 /* ----- kernel module functions--------------------------------------- */
249 tipar_open(struct inode *inode, struct file *file)
251 unsigned int minor = iminor(inode) - TIPAR_MINOR;
253 if (tp_count == 0 || minor > tp_count - 1)
256 if (test_and_set_bit(minor, &opened))
259 if (!table[minor].dev) {
260 printk(KERN_ERR "%s: NULL device for minor %u\n",
261 __FUNCTION__, minor);
264 parport_claim_or_block(table[minor].dev);
265 init_ti_parallel(minor);
266 parport_release(table[minor].dev);
268 return nonseekable_open(inode, file);
272 tipar_close(struct inode *inode, struct file *file)
274 unsigned int minor = iminor(inode) - TIPAR_MINOR;
276 if (minor > tp_count - 1)
279 clear_bit(minor, &opened);
285 tipar_write (struct file *file, const char __user *buf, size_t count,
288 unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
291 parport_claim_or_block(table[minor].dev);
293 for (n = 0; n < count; n++) {
296 if (get_user(b, buf + n)) {
301 if (put_ti_parallel(minor, b) == -1) {
302 init_ti_parallel(minor);
308 parport_release(table[minor].dev);
313 tipar_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
316 unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
323 parport_claim_or_block(table[minor].dev);
326 b = get_ti_parallel(minor);
328 init_ti_parallel(minor);
332 if (put_user(b, buf + n)) {
339 /* Non-blocking mode : try again ! */
340 if (file->f_flags & O_NONBLOCK) {
345 /* Signal pending, try again ! */
346 if (signal_pending(current)) {
347 retval = -ERESTARTSYS;
356 parport_release(table[minor].dev);
361 tipar_ioctl(struct inode *inode, struct file *file,
362 unsigned int cmd, unsigned long arg)
367 case IOCTL_TIPAR_DELAY:
368 delay = (int)arg; //get_user(delay, &arg);
370 case IOCTL_TIPAR_TIMEOUT:
384 /* ----- kernel module registering ------------------------------------ */
386 static struct file_operations tipar_fops = {
387 .owner = THIS_MODULE,
390 .write = tipar_write,
391 .ioctl = tipar_ioctl,
393 .release = tipar_close,
396 /* --- initialisation code ------------------------------------- */
399 /* You must set these - there is no sane way to probe for this cable.
400 * You can use 'tipar=timeout,delay' to set these now. */
402 tipar_setup(char *str)
406 str = get_options(str, ARRAY_SIZE(ints), ints);
412 printk(KERN_WARNING "tipar: bad timeout value (0), "
413 "using default value instead");
424 * Register our module into parport.
425 * Pass also 2 callbacks functions to parport: a pre-emptive function and an
426 * interrupt handler function (unused).
427 * Display a message such "tipar0: using parport0 (polling)".
430 tipar_register(int nr, struct parport *port)
434 /* Register our module into parport */
435 table[nr].dev = parport_register_device(port, "tipar",
437 (void *) &table[nr]);
439 if (table[nr].dev == NULL) {
444 class_device_create(tipar_class, NULL, MKDEV(TIPAR_MAJOR,
445 TIPAR_MINOR + nr), NULL, "par%d", nr);
446 /* Use devfs, tree: /dev/ticables/par/[0..2] */
447 err = devfs_mk_cdev(MKDEV(TIPAR_MAJOR, TIPAR_MINOR + nr),
448 S_IFCHR | S_IRUGO | S_IWUGO,
449 "ticables/par/%d", nr);
453 /* Display informations */
454 pr_info("tipar%d: using %s (%s)\n", nr, port->name, (port->irq ==
455 PARPORT_IRQ_NONE) ? "polling" : "interrupt-driven");
457 if (probe_ti_parallel(nr) != -1)
458 pr_info("tipar%d: link cable found\n", nr);
460 pr_info("tipar%d: link cable not found\n", nr);
466 class_device_destroy(tipar_class, MKDEV(TIPAR_MAJOR, TIPAR_MINOR + nr));
467 class_destroy(tipar_class);
473 tipar_attach(struct parport *port)
475 if (tp_count == PP_NO) {
476 pr_info("tipar: ignoring parallel port (max. %d)\n", PP_NO);
480 if (!tipar_register(tp_count, port))
485 tipar_detach(struct parport *port)
490 static struct parport_driver tipar_driver = {
492 .attach = tipar_attach,
493 .detach = tipar_detach,
497 tipar_init_module(void)
501 pr_info("tipar: parallel link cable driver, version %s\n",
504 if (register_chrdev(TIPAR_MAJOR, "tipar", &tipar_fops)) {
505 printk(KERN_ERR "tipar: unable to get major %d\n", TIPAR_MAJOR);
510 /* Use devfs with tree: /dev/ticables/par/[0..2] */
511 devfs_mk_dir("ticables/par");
513 tipar_class = class_create(THIS_MODULE, "ticables");
514 if (IS_ERR(tipar_class)) {
515 err = PTR_ERR(tipar_class);
518 if (parport_register_driver(&tipar_driver) || tp_count == 0) {
519 printk(KERN_ERR "tipar: unable to register with parport\n");
528 class_destroy(tipar_class);
531 devfs_remove("ticables/par");
532 unregister_chrdev(TIPAR_MAJOR, "tipar");
538 tipar_cleanup_module(void)
542 /* Unregistering module */
543 parport_unregister_driver(&tipar_driver);
545 unregister_chrdev(TIPAR_MAJOR, "tipar");
547 for (i = 0; i < PP_NO; i++) {
548 if (table[i].dev == NULL)
550 parport_unregister_device(table[i].dev);
551 class_device_destroy(tipar_class, MKDEV(TIPAR_MAJOR, i));
552 devfs_remove("ticables/par/%d", i);
554 class_destroy(tipar_class);
555 devfs_remove("ticables/par");
557 pr_info("tipar: module unloaded\n");
560 /* --------------------------------------------------------------------- */
562 __setup("tipar=", tipar_setup);
563 module_init(tipar_init_module);
564 module_exit(tipar_cleanup_module);
566 MODULE_AUTHOR(DRIVER_AUTHOR);
567 MODULE_DESCRIPTION(DRIVER_DESC);
568 MODULE_LICENSE(DRIVER_LICENSE);
570 module_param(timeout, int, 0);
571 MODULE_PARM_DESC(timeout, "Timeout (default=1.5 seconds)");
572 module_param(delay, int, 0);
573 MODULE_PARM_DESC(delay, "Inter-bit delay (default=10 microseconds)");