Merge git://git.kernel.org/pub/scm/linux/kernel/git/sam/kbuild
[pandora-kernel.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 #undef DEBUG                            /* change to #define to get debugging
44                                          * output - for pr_debug() */
45 #include <linux/module.h>
46 #include <linux/types.h>
47 #include <linux/errno.h>
48 #include <linux/kernel.h>
49 #include <linux/sched.h>
50 #include <linux/delay.h>
51 #include <linux/fcntl.h>
52 #include <linux/fs.h>
53 #include <linux/init.h>
54 #include <asm/uaccess.h>
55 #include <linux/ioport.h>
56 #include <asm/io.h>
57 #include <linux/bitops.h>
58 #include <linux/parport.h>              /* Our code depend on parport */
59 #include <linux/device.h>
60
61 /*
62  * TI definitions
63  */
64 #include <linux/ticable.h>
65
66 /*
67  * Version Information
68  */
69 #define DRIVER_VERSION "1.19"
70 #define DRIVER_AUTHOR  "Romain Lievin <roms@lpg.ticalc.org>"
71 #define DRIVER_DESC    "Device driver for TI/PC parallel link cables"
72 #define DRIVER_LICENSE "GPL"
73
74 #define VERSION(ver,rel,seq) (((ver)<<16) | ((rel)<<8) | (seq))
75
76 /* ----- global variables --------------------------------------------- */
77
78 struct tipar_struct {
79         struct pardevice *dev;  /* Parport device entry */
80 };
81
82 #define PP_NO 3
83 static struct tipar_struct table[PP_NO];
84
85 static int delay = IO_DELAY;    /* inter-bit delay in microseconds */
86 static int timeout = TIMAXTIME; /* timeout in tenth of seconds     */
87
88 static unsigned int tp_count;   /* tipar count */
89 static unsigned long opened;    /* opened devices */
90
91 static struct class *tipar_class;
92
93 /* --- macros for parport access -------------------------------------- */
94
95 #define r_dtr(x)        (parport_read_data(table[(x)].dev->port))
96 #define r_str(x)        (parport_read_status(table[(x)].dev->port))
97 #define w_ctr(x,y)      (parport_write_control(table[(x)].dev->port, (y)))
98 #define w_dtr(x,y)      (parport_write_data(table[(x)].dev->port, (y)))
99
100 /* --- setting states on the D-bus with the right timing: ------------- */
101
102 static inline void
103 outbyte(int value, int minor)
104 {
105         w_dtr(minor, value);
106 }
107
108 static inline int
109 inbyte(int minor)
110 {
111         return (r_str(minor));
112 }
113
114 static inline void
115 init_ti_parallel(int minor)
116 {
117         outbyte(3, minor);
118 }
119
120 /* ----- global defines ----------------------------------------------- */
121
122 #define START(x) { x = jiffies + (HZ * timeout) / 10; }
123 #define WAIT(x)  { \
124   if (time_before((x), jiffies)) return -1; \
125   if (need_resched()) schedule(); }
126
127 /* ----- D-bus bit-banging functions ---------------------------------- */
128
129 /* D-bus protocol (45kbit/s max):
130                     1                 0                      0
131        _______        ______|______    __________|________    __________
132 Red  :        ________      |      ____          |        ____
133        _        ____________|________      ______|__________       _____
134 White:  ________            |        ______      |          _______
135 */
136
137 /* Try to transmit a byte on the specified port (-1 if error). */
138 static int
139 put_ti_parallel(int minor, unsigned char data)
140 {
141         unsigned int bit;
142         unsigned long max;
143
144         for (bit = 0; bit < 8; bit++) {
145                 if (data & 1) {
146                         outbyte(2, minor);
147                         START(max);
148                         do {
149                                 WAIT(max);
150                         } while (inbyte(minor) & 0x10);
151
152                         outbyte(3, minor);
153                         START(max);
154                         do {
155                                 WAIT(max);
156                         } while (!(inbyte(minor) & 0x10));
157                 } else {
158                         outbyte(1, minor);
159                         START(max);
160                         do {
161                                 WAIT(max);
162                         } while (inbyte(minor) & 0x20);
163
164                         outbyte(3, minor);
165                         START(max);
166                         do {
167                                 WAIT(max);
168                         } while (!(inbyte(minor) & 0x20));
169                 }
170
171                 data >>= 1;
172                 udelay(delay);
173
174                 if (need_resched())
175                         schedule();
176         }
177
178         return 0;
179 }
180
181 /* Receive a byte on the specified port or -1 if error. */
182 static int
183 get_ti_parallel(int minor)
184 {
185         unsigned int bit;
186         unsigned char v, data = 0;
187         unsigned long max;
188
189         for (bit = 0; bit < 8; bit++) {
190                 START(max);
191                 do {
192                         WAIT(max);
193                 } while ((v = inbyte(minor) & 0x30) == 0x30);
194
195                 if (v == 0x10) {
196                         data = (data >> 1) | 0x80;
197                         outbyte(1, minor);
198                         START(max);
199                         do {
200                                 WAIT(max);
201                         } while (!(inbyte(minor) & 0x20));
202                         outbyte(3, minor);
203                 } else {
204                         data = data >> 1;
205                         outbyte(2, minor);
206                         START(max);
207                         do {
208                                 WAIT(max);
209                         } while (!(inbyte(minor) & 0x10));
210                         outbyte(3, minor);
211                 }
212
213                 udelay(delay);
214                 if (need_resched())
215                         schedule();
216         }
217
218         return (int) data;
219 }
220
221 /* Try to detect a parallel link cable on the specified port */
222 static int
223 probe_ti_parallel(int minor)
224 {
225         int i;
226         int seq[] = { 0x00, 0x20, 0x10, 0x30 };
227
228         for (i = 3; i >= 0; i--) {
229                 outbyte(3, minor);
230                 outbyte(i, minor);
231                 udelay(delay);
232                 pr_debug("tipar: Probing -> %i: 0x%02x 0x%02x\n", i,
233                         data & 0x30, seq[i]);
234                 if ((inbyte(minor) & 0x30) != seq[i]) {
235                         outbyte(3, minor);
236                         return -1;
237                 }
238         }
239
240         outbyte(3, minor);
241         return 0;
242 }
243
244 /* ----- kernel module functions--------------------------------------- */
245
246 static int
247 tipar_open(struct inode *inode, struct file *file)
248 {
249         unsigned int minor = iminor(inode) - TIPAR_MINOR;
250
251         if (tp_count == 0 || minor > tp_count - 1)
252                 return -ENXIO;
253
254         if (test_and_set_bit(minor, &opened))
255                 return -EBUSY;
256
257         if (!table[minor].dev) {
258                 printk(KERN_ERR "%s: NULL device for minor %u\n",
259                                 __FUNCTION__, minor);
260                 return -ENXIO;
261         }
262         parport_claim_or_block(table[minor].dev);
263         init_ti_parallel(minor);
264         parport_release(table[minor].dev);
265
266         return nonseekable_open(inode, file);
267 }
268
269 static int
270 tipar_close(struct inode *inode, struct file *file)
271 {
272         unsigned int minor = iminor(inode) - TIPAR_MINOR;
273
274         if (minor > tp_count - 1)
275                 return -ENXIO;
276
277         clear_bit(minor, &opened);
278
279         return 0;
280 }
281
282 static ssize_t
283 tipar_write (struct file *file, const char __user *buf, size_t count,
284                 loff_t * ppos)
285 {
286         unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
287         ssize_t n;
288
289         parport_claim_or_block(table[minor].dev);
290
291         for (n = 0; n < count; n++) {
292                 unsigned char b;
293
294                 if (get_user(b, buf + n)) {
295                         n = -EFAULT;
296                         goto out;
297                 }
298
299                 if (put_ti_parallel(minor, b) == -1) {
300                         init_ti_parallel(minor);
301                         n = -ETIMEDOUT;
302                         goto out;
303                 }
304         }
305       out:
306         parport_release(table[minor].dev);
307         return n;
308 }
309
310 static ssize_t
311 tipar_read(struct file *file, char __user *buf, size_t count, loff_t * ppos)
312 {
313         int b = 0;
314         unsigned int minor = iminor(file->f_dentry->d_inode) - TIPAR_MINOR;
315         ssize_t retval = 0;
316         ssize_t n = 0;
317
318         if (count == 0)
319                 return 0;
320
321         parport_claim_or_block(table[minor].dev);
322
323         while (n < count) {
324                 b = get_ti_parallel(minor);
325                 if (b == -1) {
326                         init_ti_parallel(minor);
327                         retval = -ETIMEDOUT;
328                         goto out;
329                 } else {
330                         if (put_user(b, buf + n)) {
331                                 retval = -EFAULT;
332                                 break;
333                         } else
334                                 retval = ++n;
335                 }
336
337                 /* Non-blocking mode : try again ! */
338                 if (file->f_flags & O_NONBLOCK) {
339                         retval = -EAGAIN;
340                         goto out;
341                 }
342
343                 /* Signal pending, try again ! */
344                 if (signal_pending(current)) {
345                         retval = -ERESTARTSYS;
346                         goto out;
347                 }
348
349                 if (need_resched())
350                         schedule();
351         }
352
353       out:
354         parport_release(table[minor].dev);
355         return retval;
356 }
357
358 static int
359 tipar_ioctl(struct inode *inode, struct file *file,
360             unsigned int cmd, unsigned long arg)
361 {
362         int retval = 0;
363
364         switch (cmd) {
365         case IOCTL_TIPAR_DELAY:
366                 delay = (int)arg;    //get_user(delay, &arg);
367                 break;
368         case IOCTL_TIPAR_TIMEOUT:
369                 if (arg != 0)
370                         timeout = (int)arg;
371                 else
372                         retval = -EINVAL;
373           break;
374         default:
375                 retval = -ENOTTY;
376                 break;
377         }
378
379         return retval;
380 }
381
382 /* ----- kernel module registering ------------------------------------ */
383
384 static const struct file_operations tipar_fops = {
385         .owner = THIS_MODULE,
386         .llseek = no_llseek,
387         .read = tipar_read,
388         .write = tipar_write,
389         .ioctl = tipar_ioctl,
390         .open = tipar_open,
391         .release = tipar_close,
392 };
393
394 /* --- initialisation code ------------------------------------- */
395
396 #ifndef MODULE
397 /*      You must set these - there is no sane way to probe for this cable.
398  *      You can use 'tipar=timeout,delay' to set these now. */
399 static int __init
400 tipar_setup(char *str)
401 {
402         int ints[3];
403
404         str = get_options(str, ARRAY_SIZE(ints), ints);
405
406         if (ints[0] > 0) {
407                 if (ints[1] != 0)
408                         timeout = ints[1];
409                 else
410                         printk(KERN_WARNING "tipar: bad timeout value (0), "
411                                 "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_device_create(tipar_class, NULL, MKDEV(TIPAR_MAJOR,
443                         TIPAR_MINOR + nr), NULL, "par%d", nr);
444
445         /* Display informations */
446         pr_info("tipar%d: using %s (%s)\n", nr, port->name, (port->irq ==
447                 PARPORT_IRQ_NONE) ? "polling" : "interrupt-driven");
448
449         if (probe_ti_parallel(nr) != -1)
450                 pr_info("tipar%d: link cable found\n", nr);
451         else
452                 pr_info("tipar%d: link cable not found\n", nr);
453
454         err = 0;
455
456 out:
457         return err;
458 }
459
460 static void
461 tipar_attach(struct parport *port)
462 {
463         if (tp_count == PP_NO) {
464                 pr_info("tipar: ignoring parallel port (max. %d)\n", PP_NO);
465                 return;
466         }
467
468         if (!tipar_register(tp_count, port))
469                 tp_count++;
470 }
471
472 static void
473 tipar_detach(struct parport *port)
474 {
475         /* Nothing to do */
476 }
477
478 static struct parport_driver tipar_driver = {
479         .name = "tipar",
480         .attach = tipar_attach,
481         .detach = tipar_detach,
482 };
483
484 static int __init
485 tipar_init_module(void)
486 {
487         int err = 0;
488
489         pr_info("tipar: parallel link cable driver, version %s\n",
490                 DRIVER_VERSION);
491
492         if (register_chrdev(TIPAR_MAJOR, "tipar", &tipar_fops)) {
493                 printk(KERN_ERR "tipar: unable to get major %d\n", TIPAR_MAJOR);
494                 err = -EIO;
495                 goto out;
496         }
497
498         tipar_class = class_create(THIS_MODULE, "ticables");
499         if (IS_ERR(tipar_class)) {
500                 err = PTR_ERR(tipar_class);
501                 goto out_chrdev;
502         }
503         if (parport_register_driver(&tipar_driver)) {
504                 printk(KERN_ERR "tipar: unable to register with parport\n");
505                 err = -EIO;
506                 goto out_class;
507         }
508
509         err = 0;
510         goto out;
511
512 out_class:
513         class_destroy(tipar_class);
514
515 out_chrdev:
516         unregister_chrdev(TIPAR_MAJOR, "tipar");
517 out:
518         return err;     
519 }
520
521 static void __exit
522 tipar_cleanup_module(void)
523 {
524         unsigned int i;
525
526         /* Unregistering module */
527         parport_unregister_driver(&tipar_driver);
528
529         unregister_chrdev(TIPAR_MAJOR, "tipar");
530
531         for (i = 0; i < PP_NO; i++) {
532                 if (table[i].dev == NULL)
533                         continue;
534                 parport_unregister_device(table[i].dev);
535                 class_device_destroy(tipar_class, MKDEV(TIPAR_MAJOR, i));
536         }
537         class_destroy(tipar_class);
538
539         pr_info("tipar: module unloaded\n");
540 }
541
542 /* --------------------------------------------------------------------- */
543
544 __setup("tipar=", tipar_setup);
545 module_init(tipar_init_module);
546 module_exit(tipar_cleanup_module);
547
548 MODULE_AUTHOR(DRIVER_AUTHOR);
549 MODULE_DESCRIPTION(DRIVER_DESC);
550 MODULE_LICENSE(DRIVER_LICENSE);
551
552 module_param(timeout, int, 0);
553 MODULE_PARM_DESC(timeout, "Timeout (default=1.5 seconds)");
554 module_param(delay, int, 0);
555 MODULE_PARM_DESC(delay, "Inter-bit delay (default=10 microseconds)");