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/parport.h> /* Our code depend on parport */
60 #include <linux/device.h>
65 #include <linux/ticable.h>
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"
75 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
77 /* ----- global variables --------------------------------------------- */
80 struct pardevice
*dev
; /* Parport device entry */
84 static struct tipar_struct table
[PP_NO
];
86 static int delay
= IO_DELAY
; /* inter-bit delay in microseconds */
87 static int timeout
= TIMAXTIME
; /* timeout in tenth of seconds */
89 static unsigned int tp_count
; /* tipar count */
90 static unsigned long opened
; /* opened devices */
92 static struct class *tipar_class
;
94 /* --- macros for parport access -------------------------------------- */
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)))
101 /* --- setting states on the D-bus with the right timing: ------------- */
104 outbyte(int value
, int minor
)
112 return (r_str(minor
));
116 init_ti_parallel(int minor
)
121 /* ----- global defines ----------------------------------------------- */
123 #define START(x) { x = jiffies + (HZ * timeout) / 10; }
125 if (time_before((x), jiffies)) return -1; \
126 if (need_resched()) schedule(); }
128 /* ----- D-bus bit-banging functions ---------------------------------- */
130 /* D-bus protocol (45kbit/s max):
132 _______ ______|______ __________|________ __________
133 Red : ________ | ____ | ____
134 _ ____________|________ ______|__________ _____
135 White: ________ | ______ | _______
138 /* Try to transmit a byte on the specified port (-1 if error). */
140 put_ti_parallel(int minor
, unsigned char data
)
145 for (bit
= 0; bit
< 8; bit
++) {
151 } while (inbyte(minor
) & 0x10);
157 } while (!(inbyte(minor
) & 0x10));
163 } while (inbyte(minor
) & 0x20);
169 } while (!(inbyte(minor
) & 0x20));
182 /* Receive a byte on the specified port or -1 if error. */
184 get_ti_parallel(int minor
)
187 unsigned char v
, data
= 0;
190 for (bit
= 0; bit
< 8; bit
++) {
194 } while ((v
= inbyte(minor
) & 0x30) == 0x30);
197 data
= (data
>> 1) | 0x80;
202 } while (!(inbyte(minor
) & 0x20));
210 } while (!(inbyte(minor
) & 0x10));
222 /* Try to detect a parallel link cable on the specified port */
224 probe_ti_parallel(int minor
)
227 int seq
[] = { 0x00, 0x20, 0x10, 0x30 };
229 for (i
= 3; i
>= 0; i
--) {
233 pr_debug("tipar: Probing -> %i: 0x%02x 0x%02x\n", i
,
234 data
& 0x30, seq
[i
]);
235 if ((inbyte(minor
) & 0x30) != seq
[i
]) {
245 /* ----- kernel module functions--------------------------------------- */
248 tipar_open(struct inode
*inode
, struct file
*file
)
250 unsigned int minor
= iminor(inode
) - TIPAR_MINOR
;
252 if (tp_count
== 0 || minor
> tp_count
- 1)
255 if (test_and_set_bit(minor
, &opened
))
258 if (!table
[minor
].dev
) {
259 printk(KERN_ERR
"%s: NULL device for minor %u\n",
260 __FUNCTION__
, minor
);
263 parport_claim_or_block(table
[minor
].dev
);
264 init_ti_parallel(minor
);
265 parport_release(table
[minor
].dev
);
267 return nonseekable_open(inode
, file
);
271 tipar_close(struct inode
*inode
, struct file
*file
)
273 unsigned int minor
= iminor(inode
) - TIPAR_MINOR
;
275 if (minor
> tp_count
- 1)
278 clear_bit(minor
, &opened
);
284 tipar_write (struct file
*file
, const char __user
*buf
, size_t count
,
287 unsigned int minor
= iminor(file
->f_dentry
->d_inode
) - TIPAR_MINOR
;
290 parport_claim_or_block(table
[minor
].dev
);
292 for (n
= 0; n
< count
; n
++) {
295 if (get_user(b
, buf
+ n
)) {
300 if (put_ti_parallel(minor
, b
) == -1) {
301 init_ti_parallel(minor
);
307 parport_release(table
[minor
].dev
);
312 tipar_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
* ppos
)
315 unsigned int minor
= iminor(file
->f_dentry
->d_inode
) - TIPAR_MINOR
;
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(KERN_WARNING
"tipar: bad timeout value (0), "
412 "using default value instead");
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)".
429 tipar_register(int nr
, struct parport
*port
)
433 /* Register our module into parport */
434 table
[nr
].dev
= parport_register_device(port
, "tipar",
436 (void *) &table
[nr
]);
438 if (table
[nr
].dev
== NULL
) {
443 class_device_create(tipar_class
, NULL
, MKDEV(TIPAR_MAJOR
,
444 TIPAR_MINOR
+ nr
), NULL
, "par%d", nr
);
446 /* Display informations */
447 pr_info("tipar%d: using %s (%s)\n", nr
, port
->name
, (port
->irq
==
448 PARPORT_IRQ_NONE
) ? "polling" : "interrupt-driven");
450 if (probe_ti_parallel(nr
) != -1)
451 pr_info("tipar%d: link cable found\n", nr
);
453 pr_info("tipar%d: link cable not found\n", nr
);
462 tipar_attach(struct parport
*port
)
464 if (tp_count
== PP_NO
) {
465 pr_info("tipar: ignoring parallel port (max. %d)\n", PP_NO
);
469 if (!tipar_register(tp_count
, port
))
474 tipar_detach(struct parport
*port
)
479 static struct parport_driver tipar_driver
= {
481 .attach
= tipar_attach
,
482 .detach
= tipar_detach
,
486 tipar_init_module(void)
490 pr_info("tipar: parallel link cable driver, version %s\n",
493 if (register_chrdev(TIPAR_MAJOR
, "tipar", &tipar_fops
)) {
494 printk(KERN_ERR
"tipar: unable to get major %d\n", TIPAR_MAJOR
);
499 tipar_class
= class_create(THIS_MODULE
, "ticables");
500 if (IS_ERR(tipar_class
)) {
501 err
= PTR_ERR(tipar_class
);
504 if (parport_register_driver(&tipar_driver
)) {
505 printk(KERN_ERR
"tipar: unable to register with parport\n");
514 class_destroy(tipar_class
);
517 unregister_chrdev(TIPAR_MAJOR
, "tipar");
523 tipar_cleanup_module(void)
527 /* Unregistering module */
528 parport_unregister_driver(&tipar_driver
);
530 unregister_chrdev(TIPAR_MAJOR
, "tipar");
532 for (i
= 0; i
< PP_NO
; i
++) {
533 if (table
[i
].dev
== NULL
)
535 parport_unregister_device(table
[i
].dev
);
536 class_device_destroy(tipar_class
, MKDEV(TIPAR_MAJOR
, i
));
538 class_destroy(tipar_class
);
540 pr_info("tipar: module unloaded\n");
543 /* --------------------------------------------------------------------- */
545 __setup("tipar=", tipar_setup
);
546 module_init(tipar_init_module
);
547 module_exit(tipar_cleanup_module
);
549 MODULE_AUTHOR(DRIVER_AUTHOR
);
550 MODULE_DESCRIPTION(DRIVER_DESC
);
551 MODULE_LICENSE(DRIVER_LICENSE
);
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)");