blob: eb2eb3e12d6a0ed07a48046e0df136a504342470 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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/devfs_fs_kernel.h> /* DevFs support */
60#include <linux/parport.h> /* Our code depend on parport */
61#include <linux/device.h>
62
63/*
64 * TI definitions
65 */
66#include <linux/ticable.h>
67
68/*
69 * Version Information
70 */
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"
75
76#define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
77
78/* ----- global variables --------------------------------------------- */
79
80struct tipar_struct {
81 struct pardevice *dev; /* Parport device entry */
82};
83
84#define PP_NO 3
85static struct tipar_struct table[PP_NO];
86
87static int delay = IO_DELAY; /* inter-bit delay in microseconds */
88static int timeout = TIMAXTIME; /* timeout in tenth of seconds */
89
90static unsigned int tp_count; /* tipar count */
91static unsigned long opened; /* opened devices */
92
gregkh@suse.deca8eca62005-03-23 09:53:09 -080093static struct class *tipar_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95/* --- macros for parport access -------------------------------------- */
96
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)))
101
102/* --- setting states on the D-bus with the right timing: ------------- */
103
104static inline void
105outbyte(int value, int minor)
106{
107 w_dtr(minor, value);
108}
109
110static inline int
111inbyte(int minor)
112{
113 return (r_str(minor));
114}
115
116static inline void
117init_ti_parallel(int minor)
118{
119 outbyte(3, minor);
120}
121
122/* ----- global defines ----------------------------------------------- */
123
124#define START(x) { x = jiffies + (HZ * timeout) / 10; }
125#define WAIT(x) { \
126 if (time_before((x), jiffies)) return -1; \
127 if (need_resched()) schedule(); }
128
129/* ----- D-bus bit-banging functions ---------------------------------- */
130
131/* D-bus protocol (45kbit/s max):
132 1 0 0
133 _______ ______|______ __________|________ __________
134Red : ________ | ____ | ____
135 _ ____________|________ ______|__________ _____
136White: ________ | ______ | _______
137*/
138
139/* Try to transmit a byte on the specified port (-1 if error). */
140static int
141put_ti_parallel(int minor, unsigned char data)
142{
143 unsigned int bit;
144 unsigned long max;
145
146 for (bit = 0; bit < 8; bit++) {
147 if (data & 1) {
148 outbyte(2, minor);
149 START(max);
150 do {
151 WAIT(max);
152 } while (inbyte(minor) & 0x10);
153
154 outbyte(3, minor);
155 START(max);
156 do {
157 WAIT(max);
158 } while (!(inbyte(minor) & 0x10));
159 } else {
160 outbyte(1, minor);
161 START(max);
162 do {
163 WAIT(max);
164 } while (inbyte(minor) & 0x20);
165
166 outbyte(3, minor);
167 START(max);
168 do {
169 WAIT(max);
170 } while (!(inbyte(minor) & 0x20));
171 }
172
173 data >>= 1;
174 udelay(delay);
175
176 if (need_resched())
177 schedule();
178 }
179
180 return 0;
181}
182
183/* Receive a byte on the specified port or -1 if error. */
184static int
185get_ti_parallel(int minor)
186{
187 unsigned int bit;
188 unsigned char v, data = 0;
189 unsigned long max;
190
191 for (bit = 0; bit < 8; bit++) {
192 START(max);
193 do {
194 WAIT(max);
195 } while ((v = inbyte(minor) & 0x30) == 0x30);
196
197 if (v == 0x10) {
198 data = (data >> 1) | 0x80;
199 outbyte(1, minor);
200 START(max);
201 do {
202 WAIT(max);
203 } while (!(inbyte(minor) & 0x20));
204 outbyte(3, minor);
205 } else {
206 data = data >> 1;
207 outbyte(2, minor);
208 START(max);
209 do {
210 WAIT(max);
211 } while (!(inbyte(minor) & 0x10));
212 outbyte(3, minor);
213 }
214
215 udelay(delay);
216 if (need_resched())
217 schedule();
218 }
219
220 return (int) data;
221}
222
223/* Try to detect a parallel link cable on the specified port */
224static int
225probe_ti_parallel(int minor)
226{
227 int i;
228 int seq[] = { 0x00, 0x20, 0x10, 0x30 };
229
230 for (i = 3; i >= 0; i--) {
231 outbyte(3, minor);
232 outbyte(i, minor);
233 udelay(delay);
234 pr_debug("tipar: Probing -> %i: 0x%02x 0x%02x\n", i,
235 data & 0x30, seq[i]);
236 if ((inbyte(minor) & 0x30) != seq[i]) {
237 outbyte(3, minor);
238 return -1;
239 }
240 }
241
242 outbyte(3, minor);
243 return 0;
244}
245
246/* ----- kernel module functions--------------------------------------- */
247
248static int
249tipar_open(struct inode *inode, struct file *file)
250{
251 unsigned int minor = iminor(inode) - TIPAR_MINOR;
252
Andrew Morton1d308832006-02-11 17:56:05 -0800253 if (tp_count == 0 || minor > tp_count - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 return -ENXIO;
255
256 if (test_and_set_bit(minor, &opened))
257 return -EBUSY;
258
Andrew Morton1d308832006-02-11 17:56:05 -0800259 if (!table[minor].dev) {
260 printk(KERN_ERR "%s: NULL device for minor %u\n",
261 __FUNCTION__, minor);
262 return -ENXIO;
263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 parport_claim_or_block(table[minor].dev);
265 init_ti_parallel(minor);
266 parport_release(table[minor].dev);
267
268 return nonseekable_open(inode, file);
269}
270
271static int
272tipar_close(struct inode *inode, struct file *file)
273{
274 unsigned int minor = iminor(inode) - TIPAR_MINOR;
275
276 if (minor > tp_count - 1)
277 return -ENXIO;
278
279 clear_bit(minor, &opened);
280
281 return 0;
282}
283
284static ssize_t
285tipar_write (struct file *file, const char __user *buf, size_t count,
286 loff_t * ppos)
287{
288 unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
289 ssize_t n;
290
291 parport_claim_or_block(table[minor].dev);
292
293 for (n = 0; n < count; n++) {
294 unsigned char b;
295
296 if (get_user(b, buf + n)) {
297 n = -EFAULT;
298 goto out;
299 }
300
301 if (put_ti_parallel(minor, b) == -1) {
302 init_ti_parallel(minor);
303 n = -ETIMEDOUT;
304 goto out;
305 }
306 }
307 out:
308 parport_release(table[minor].dev);
309 return n;
310}
311
312static ssize_t
313tipar_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
314{
315 int b = 0;
316 unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
317 ssize_t retval = 0;
318 ssize_t n = 0;
319
320 if (count == 0)
321 return 0;
322
323 parport_claim_or_block(table[minor].dev);
324
325 while (n < count) {
326 b = get_ti_parallel(minor);
327 if (b == -1) {
328 init_ti_parallel(minor);
329 retval = -ETIMEDOUT;
330 goto out;
331 } else {
332 if (put_user(b, buf + n)) {
333 retval = -EFAULT;
334 break;
335 } else
336 retval = ++n;
337 }
338
339 /* Non-blocking mode : try again ! */
340 if (file->f_flags & O_NONBLOCK) {
341 retval = -EAGAIN;
342 goto out;
343 }
344
345 /* Signal pending, try again ! */
346 if (signal_pending(current)) {
347 retval = -ERESTARTSYS;
348 goto out;
349 }
350
351 if (need_resched())
352 schedule();
353 }
354
355 out:
356 parport_release(table[minor].dev);
357 return retval;
358}
359
360static int
361tipar_ioctl(struct inode *inode, struct file *file,
362 unsigned int cmd, unsigned long arg)
363{
364 int retval = 0;
365
366 switch (cmd) {
367 case IOCTL_TIPAR_DELAY:
368 delay = (int)arg; //get_user(delay, &arg);
369 break;
370 case IOCTL_TIPAR_TIMEOUT:
371 if (arg != 0)
372 timeout = (int)arg;
373 else
374 retval = -EINVAL;
375 break;
376 default:
377 retval = -ENOTTY;
378 break;
379 }
380
381 return retval;
382}
383
384/* ----- kernel module registering ------------------------------------ */
385
386static struct file_operations tipar_fops = {
387 .owner = THIS_MODULE,
388 .llseek = no_llseek,
389 .read = tipar_read,
390 .write = tipar_write,
391 .ioctl = tipar_ioctl,
392 .open = tipar_open,
393 .release = tipar_close,
394};
395
396/* --- initialisation code ------------------------------------- */
397
398#ifndef MODULE
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. */
401static int __init
402tipar_setup(char *str)
403{
Luc Van Oostenryck45ae36c2005-06-28 20:44:44 -0700404 int ints[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 str = get_options(str, ARRAY_SIZE(ints), ints);
407
408 if (ints[0] > 0) {
409 if (ints[1] != 0)
410 timeout = ints[1];
411 else
412 printk(KERN_WARNING "tipar: bad timeout value (0), "
413 "using default value instead");
414 if (ints[0] > 1) {
415 delay = ints[2];
416 }
417 }
418
419 return 1;
420}
421#endif
422
423/*
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)".
428 */
429static int
430tipar_register(int nr, struct parport *port)
431{
432 int err = 0;
433
434 /* Register our module into parport */
435 table[nr].dev = parport_register_device(port, "tipar",
436 NULL, NULL, NULL, 0,
437 (void *) &table[nr]);
438
439 if (table[nr].dev == NULL) {
440 err = 1;
441 goto out;
442 }
443
Greg Kroah-Hartman53f46542005-10-27 22:25:43 -0700444 class_device_create(tipar_class, NULL, MKDEV(TIPAR_MAJOR,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 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);
450 if (err)
451 goto out_class;
452
453 /* Display informations */
454 pr_info("tipar%d: using %s (%s)\n", nr, port->name, (port->irq ==
455 PARPORT_IRQ_NONE) ? "polling" : "interrupt-driven");
456
457 if (probe_ti_parallel(nr) != -1)
458 pr_info("tipar%d: link cable found\n", nr);
459 else
460 pr_info("tipar%d: link cable not found\n", nr);
461
462 err = 0;
463 goto out;
464
465out_class:
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800466 class_device_destroy(tipar_class, MKDEV(TIPAR_MAJOR, TIPAR_MINOR + nr));
467 class_destroy(tipar_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468out:
469 return err;
470}
471
472static void
473tipar_attach(struct parport *port)
474{
475 if (tp_count == PP_NO) {
476 pr_info("tipar: ignoring parallel port (max. %d)\n", PP_NO);
477 return;
478 }
479
480 if (!tipar_register(tp_count, port))
481 tp_count++;
482}
483
484static void
485tipar_detach(struct parport *port)
486{
487 /* Nothing to do */
488}
489
490static struct parport_driver tipar_driver = {
491 .name = "tipar",
492 .attach = tipar_attach,
493 .detach = tipar_detach,
494};
495
496static int __init
497tipar_init_module(void)
498{
499 int err = 0;
500
501 pr_info("tipar: parallel link cable driver, version %s\n",
502 DRIVER_VERSION);
503
504 if (register_chrdev(TIPAR_MAJOR, "tipar", &tipar_fops)) {
505 printk(KERN_ERR "tipar: unable to get major %d\n", TIPAR_MAJOR);
506 err = -EIO;
507 goto out;
508 }
509
510 /* Use devfs with tree: /dev/ticables/par/[0..2] */
511 devfs_mk_dir("ticables/par");
512
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800513 tipar_class = class_create(THIS_MODULE, "ticables");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (IS_ERR(tipar_class)) {
515 err = PTR_ERR(tipar_class);
516 goto out_chrdev;
517 }
Andrew Morton1d308832006-02-11 17:56:05 -0800518 if (parport_register_driver(&tipar_driver) || tp_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 printk(KERN_ERR "tipar: unable to register with parport\n");
520 err = -EIO;
Andrew Morton1d308832006-02-11 17:56:05 -0800521 goto out_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523
524 err = 0;
525 goto out;
526
Andrew Morton1d308832006-02-11 17:56:05 -0800527out_class:
528 class_destroy(tipar_class);
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530out_chrdev:
Andrew Morton1d308832006-02-11 17:56:05 -0800531 devfs_remove("ticables/par");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 unregister_chrdev(TIPAR_MAJOR, "tipar");
533out:
534 return err;
535}
536
537static void __exit
538tipar_cleanup_module(void)
539{
540 unsigned int i;
541
542 /* Unregistering module */
543 parport_unregister_driver(&tipar_driver);
544
545 unregister_chrdev(TIPAR_MAJOR, "tipar");
546
547 for (i = 0; i < PP_NO; i++) {
548 if (table[i].dev == NULL)
549 continue;
550 parport_unregister_device(table[i].dev);
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800551 class_device_destroy(tipar_class, MKDEV(TIPAR_MAJOR, i));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 devfs_remove("ticables/par/%d", i);
553 }
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800554 class_destroy(tipar_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 devfs_remove("ticables/par");
556
557 pr_info("tipar: module unloaded\n");
558}
559
560/* --------------------------------------------------------------------- */
561
562__setup("tipar=", tipar_setup);
563module_init(tipar_init_module);
564module_exit(tipar_cleanup_module);
565
566MODULE_AUTHOR(DRIVER_AUTHOR);
567MODULE_DESCRIPTION(DRIVER_DESC);
568MODULE_LICENSE(DRIVER_LICENSE);
569
570module_param(timeout, int, 0);
571MODULE_PARM_DESC(timeout, "Timeout (default=1.5 seconds)");
572module_param(delay, int, 0);
573MODULE_PARM_DESC(delay, "Inter-bit delay (default=10 microseconds)");