ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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
62 /*
63  * TI definitions
64  */
65 #include <linux/ticable.h>
66
67 /*
68  * Version Information
69  */
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"
74
75 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
76 #if LINUX_VERSION_CODE < VERSION(2,5,0)
77 # define need_resched() (current->need_resched)
78 #endif
79
80 /* ----- global variables --------------------------------------------- */
81
82 struct tipar_struct {
83         struct pardevice *dev;  /* Parport device entry */
84 };
85
86 #define PP_NO 3
87 static struct tipar_struct table[PP_NO];
88
89 static int delay = IO_DELAY;    /* inter-bit delay in microseconds */
90 static int timeout = TIMAXTIME; /* timeout in tenth of seconds     */
91
92 static unsigned int tp_count;   /* tipar count */
93 static unsigned long opened;    /* opened devices */
94
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
104 static inline void
105 outbyte(int value, int minor)
106 {
107         w_dtr(minor, value);
108 }
109
110 static inline int
111 inbyte(int minor)
112 {
113         return (r_str(minor));
114 }
115
116 static inline void
117 init_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        _______        ______|______    __________|________    __________
134 Red  :        ________      |      ____          |        ____
135        _        ____________|________      ______|__________       _____
136 White:  ________            |        ______      |          _______
137 */
138
139 /* Try to transmit a byte on the specified port (-1 if error). */
140 static int
141 put_ti_parallel(int minor, unsigned char data)
142 {
143         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. */
184 static int
185 get_ti_parallel(int minor)
186 {
187         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 */
224 static int
225 probe_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                 /*printk(KERN_DEBUG "Probing -> %i: 0x%02x 0x%02x\n", i, data & 0x30, seq[i]); */
235                 if ((inbyte(minor) & 0x30) != seq[i]) {
236                         outbyte(3, minor);
237                         return -1;
238                 }
239         }
240
241         outbyte(3, minor);
242         return 0;
243 }
244
245 /* ----- kernel module functions--------------------------------------- */
246
247 static int
248 tipar_open(struct inode *inode, struct file *file)
249 {
250         unsigned int minor = iminor(inode) - TIPAR_MINOR;
251
252         if (minor > tp_count - 1)
253                 return -ENXIO;
254
255         if (test_and_set_bit(minor, &opened))
256                 return -EBUSY;
257
258         parport_claim_or_block(table[minor].dev);
259         init_ti_parallel(minor);
260         parport_release(table[minor].dev);
261
262         return 0;
263 }
264
265 static int
266 tipar_close(struct inode *inode, struct file *file)
267 {
268         unsigned int minor = iminor(inode) - TIPAR_MINOR;
269
270         if (minor > tp_count - 1)
271                 return -ENXIO;
272
273         clear_bit(minor, &opened);
274
275         return 0;
276 }
277
278 static ssize_t
279 tipar_write(struct file *file, const char *buf, size_t count, loff_t * ppos)
280 {
281         unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
282         ssize_t n;
283
284         parport_claim_or_block(table[minor].dev);
285
286         for (n = 0; n < count; n++) {
287                 unsigned char b;
288
289                 if (get_user(b, buf + n)) {
290                         n = -EFAULT;
291                         goto out;
292                 }
293
294                 if (put_ti_parallel(minor, b) == -1) {
295                         init_ti_parallel(minor);
296                         n = -ETIMEDOUT;
297                         goto out;
298                 }
299         }
300       out:
301         parport_release(table[minor].dev);
302         return n;
303 }
304
305 static ssize_t
306 tipar_read(struct file *file, char *buf, size_t count, loff_t * ppos)
307 {
308         int b = 0;
309         unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
310         ssize_t retval = 0;
311         ssize_t n = 0;
312
313         if (count == 0)
314                 return 0;
315
316         if (ppos != &file->f_pos)
317                 return -ESPIPE;
318
319         parport_claim_or_block(table[minor].dev);
320
321         while (n < count) {
322                 b = get_ti_parallel(minor);
323                 if (b == -1) {
324                         init_ti_parallel(minor);
325                         retval = -ETIMEDOUT;
326                         goto out;
327                 } else {
328                         if (put_user(b, ((unsigned char *) buf) + n)) {
329                                 retval = -EFAULT;
330                                 break;
331                         } else
332                                 retval = ++n;
333                 }
334
335                 /* Non-blocking mode : try again ! */
336                 if (file->f_flags & O_NONBLOCK) {
337                         retval = -EAGAIN;
338                         goto out;
339                 }
340
341                 /* Signal pending, try again ! */
342                 if (signal_pending(current)) {
343                         retval = -ERESTARTSYS;
344                         goto out;
345                 }
346
347                 if (need_resched())
348                         schedule();
349         }
350
351       out:
352         parport_release(table[minor].dev);
353         return retval;
354 }
355
356 static int
357 tipar_ioctl(struct inode *inode, struct file *file,
358             unsigned int cmd, unsigned long arg)
359 {
360         int retval = 0;
361
362         switch (cmd) {
363         case IOCTL_TIPAR_DELAY:
364                 delay = (int)arg;    //get_user(delay, &arg);
365                 break;
366         case IOCTL_TIPAR_TIMEOUT:
367                 if (arg != 0)
368                         timeout = (int)arg;
369                 else
370                         retval = -EINVAL;
371           break;
372         default:
373                 retval = -ENOTTY;
374                 break;
375         }
376
377         return retval;
378 }
379
380 /* ----- kernel module registering ------------------------------------ */
381
382 static struct file_operations tipar_fops = {
383         .owner = THIS_MODULE,
384         .llseek = no_llseek,
385         .read = tipar_read,
386         .write = tipar_write,
387         .ioctl = tipar_ioctl,
388         .open = tipar_open,
389         .release = tipar_close,
390 };
391
392 /* --- initialisation code ------------------------------------- */
393
394 #ifndef MODULE
395 /*      You must set these - there is no sane way to probe for this cable.
396  *      You can use 'tipar=timeout,delay' to set these now. */
397 static int __init
398 tipar_setup(char *str)
399 {
400         int ints[2];
401
402         str = get_options(str, ARRAY_SIZE(ints), ints);
403
404         if (ints[0] > 0) {
405                 if (ints[1] != 0)
406                         timeout = ints[1];
407                 else
408                         printk("tipar: wrong timeout value (0), using default value instead.");
409                 if (ints[0] > 1) {
410                         delay = ints[2];
411                 }
412         }
413
414         return 1;
415 }
416 #endif
417
418 /*
419  * Register our module into parport.
420  * Pass also 2 callbacks functions to parport: a pre-emptive function and an
421  * interrupt handler function (unused).
422  * Display a message such "tipar0: using parport0 (polling)".
423  */
424 static int
425 tipar_register(int nr, struct parport *port)
426 {
427         /* Register our module into parport */
428         table[nr].dev = parport_register_device(port, "tipar",
429                                                 NULL, NULL, NULL, 0,
430                                                 (void *) &table[nr]);
431
432         if (table[nr].dev == NULL)
433                 return 1;
434
435         /* Use devfs, tree: /dev/ticables/par/[0..2] */
436         devfs_mk_cdev(MKDEV(TIPAR_MAJOR, TIPAR_MINOR + nr),
437                         S_IFCHR | S_IRUGO | S_IWUGO,
438                         "ticables/par/%d", nr);
439
440         /* Display informations */
441         printk(KERN_INFO "tipar%d: using %s (%s).\n", nr, port->name,
442                (port->irq ==
443                 PARPORT_IRQ_NONE) ? "polling" : "interrupt-driven");
444
445         if (probe_ti_parallel(nr) != -1)
446                 printk("tipar%d: link cable found !\n", nr);
447         else
448                 printk("tipar%d: link cable not found.\n", nr);
449
450         return 0;
451 }
452
453 static void
454 tipar_attach(struct parport *port)
455 {
456         if (tp_count == PP_NO) {
457                 printk("tipar: ignoring parallel port (max. %d)\n", PP_NO);
458                 return;
459         }
460
461         if (!tipar_register(tp_count, port))
462                 tp_count++;
463 }
464
465 static void
466 tipar_detach(struct parport *port)
467 {
468         /* Nothing to do */
469 }
470
471 static struct parport_driver tipar_driver = {
472         .name = "tipar",
473         .attach = tipar_attach,
474         .detach = tipar_detach,
475 };
476
477 int __init
478 tipar_init_module(void)
479 {
480         printk("tipar: parallel link cable driver, version %s\n",
481                DRIVER_VERSION);
482
483         if (register_chrdev(TIPAR_MAJOR, "tipar", &tipar_fops)) {
484                 printk("tipar: unable to get major %d\n", TIPAR_MAJOR);
485                 return -EIO;
486         }
487
488         /* Use devfs with tree: /dev/ticables/par/[0..2] */
489         devfs_mk_dir("ticables/par");
490
491         if (parport_register_driver(&tipar_driver)) {
492                 printk("tipar: unable to register with parport\n");
493                 return -EIO;
494         }
495
496         return 0;
497 }
498
499 void __exit
500 tipar_cleanup_module(void)
501 {
502         unsigned int i;
503
504         /* Unregistering module */
505         parport_unregister_driver(&tipar_driver);
506
507         unregister_chrdev(TIPAR_MAJOR, "tipar");
508
509         for (i = 0; i < PP_NO; i++) {
510                 if (table[i].dev == NULL)
511                         continue;
512                 parport_unregister_device(table[i].dev);
513                 devfs_remove("ticables/par/%d", i);
514         }
515         devfs_remove("ticables/par");
516
517         printk("tipar: module unloaded !\n");
518 }
519
520 /* --------------------------------------------------------------------- */
521
522 __setup("tipar=", tipar_setup);
523 module_init(tipar_init_module);
524 module_exit(tipar_cleanup_module);
525
526 MODULE_AUTHOR(DRIVER_AUTHOR);
527 MODULE_DESCRIPTION(DRIVER_DESC);
528 MODULE_LICENSE(DRIVER_LICENSE);
529
530 MODULE_PARM(timeout, "i");
531 MODULE_PARM_DESC(timeout, "Timeout (default=1.5 seconds)");
532 MODULE_PARM(delay, "i");
533 MODULE_PARM_DESC(delay, "Inter-bit delay (default=10 microseconds)");