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