ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / parport / daisy.c
1 /*
2  * IEEE 1284.3 Parallel port daisy chain and multiplexor code
3  * 
4  * Copyright (C) 1999, 2000  Tim Waugh <tim@cyberelk.demon.co.uk>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version
9  * 2 of the License, or (at your option) any later version.
10  *
11  * ??-12-1998: Initial implementation.
12  * 31-01-1999: Make port-cloning transparent.
13  * 13-02-1999: Move DeviceID technique from parport_probe.
14  * 13-03-1999: Get DeviceID from non-IEEE 1284.3 devices too.
15  * 22-02-2000: Count devices that are actually detected.
16  *
17  * Any part of this program may be used in documents licensed under
18  * the GNU Free Documentation License, Version 1.1 or any later version
19  * published by the Free Software Foundation.
20  */
21
22 #include <linux/module.h>
23 #include <linux/parport.h>
24 #include <linux/delay.h>
25 #include <linux/sched.h>
26
27 #include <asm/current.h>
28 #include <asm/uaccess.h>
29
30 #undef DEBUG
31
32 #ifdef DEBUG
33 #define DPRINTK(stuff...) printk (stuff)
34 #else
35 #define DPRINTK(stuff...)
36 #endif
37
38 static struct daisydev {
39         struct daisydev *next;
40         struct parport *port;
41         int daisy;
42         int devnum;
43 } *topology = NULL;
44 static spinlock_t topology_lock = SPIN_LOCK_UNLOCKED;
45
46 static int numdevs = 0;
47
48 /* Forward-declaration of lower-level functions. */
49 static int mux_present (struct parport *port);
50 static int num_mux_ports (struct parport *port);
51 static int select_port (struct parport *port);
52 static int assign_addrs (struct parport *port);
53
54 /* Add a device to the discovered topology. */
55 static void add_dev (int devnum, struct parport *port, int daisy)
56 {
57         struct daisydev *newdev, **p;
58         newdev = kmalloc (sizeof (struct daisydev), GFP_KERNEL);
59         if (newdev) {
60                 newdev->port = port;
61                 newdev->daisy = daisy;
62                 newdev->devnum = devnum;
63                 spin_lock(&topology_lock);
64                 for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
65                         ;
66                 newdev->next = *p;
67                 *p = newdev;
68                 spin_unlock(&topology_lock);
69         }
70 }
71
72 /* Clone a parport (actually, make an alias). */
73 static struct parport *clone_parport (struct parport *real, int muxport)
74 {
75         struct parport *extra = parport_register_port (real->base,
76                                                        real->irq,
77                                                        real->dma,
78                                                        real->ops);
79         if (extra) {
80                 extra->portnum = real->portnum;
81                 extra->physport = real;
82                 extra->muxport = muxport;
83                 real->slaves[muxport-1] = extra;
84         }
85
86         return extra;
87 }
88
89 /* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains.
90  * Return value is number of devices actually detected. */
91 int parport_daisy_init (struct parport *port)
92 {
93         int detected = 0;
94         char *deviceid;
95         static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
96         int num_ports;
97         int i;
98         int last_try = 0;
99
100 again:
101         /* Because this is called before any other devices exist,
102          * we don't have to claim exclusive access.  */
103
104         /* If mux present on normal port, need to create new
105          * parports for each extra port. */
106         if (port->muxport < 0 && mux_present (port) &&
107             /* don't be fooled: a mux must have 2 or 4 ports. */
108             ((num_ports = num_mux_ports (port)) == 2 || num_ports == 4)) {
109                 /* Leave original as port zero. */
110                 port->muxport = 0;
111                 printk (KERN_INFO
112                         "%s: 1st (default) port of %d-way multiplexor\n",
113                         port->name, num_ports);
114                 for (i = 1; i < num_ports; i++) {
115                         /* Clone the port. */
116                         struct parport *extra = clone_parport (port, i);
117                         if (!extra) {
118                                 if (signal_pending (current))
119                                         break;
120
121                                 schedule ();
122                                 continue;
123                         }
124
125                         printk (KERN_INFO
126                                 "%s: %d%s port of %d-way multiplexor on %s\n",
127                                 extra->name, i + 1, th[i + 1], num_ports,
128                                 port->name);
129
130                         /* Analyse that port too.  We won't recurse
131                            forever because of the 'port->muxport < 0'
132                            test above. */
133                         parport_daisy_init(extra);
134                 }
135         }
136
137         if (port->muxport >= 0)
138                 select_port (port);
139
140         parport_daisy_deselect_all (port);
141         detected += assign_addrs (port);
142
143         /* Count the potential legacy device at the end. */
144         add_dev (numdevs++, port, -1);
145
146         /* Find out the legacy device's IEEE 1284 device ID. */
147         deviceid = kmalloc (1000, GFP_KERNEL);
148         if (deviceid) {
149                 if (parport_device_id (numdevs - 1, deviceid, 1000) > 2)
150                         detected++;
151
152                 kfree (deviceid);
153         }
154
155         if (!detected && !last_try) {
156                 /* No devices were detected.  Perhaps they are in some
157                    funny state; let's try to reset them and see if
158                    they wake up. */
159                 parport_daisy_fini (port);
160                 parport_write_control (port, PARPORT_CONTROL_SELECT);
161                 udelay (50);
162                 parport_write_control (port,
163                                        PARPORT_CONTROL_SELECT |
164                                        PARPORT_CONTROL_INIT);
165                 udelay (50);
166                 last_try = 1;
167                 goto again;
168         }
169
170         return detected;
171 }
172
173 /* Forget about devices on a physical port. */
174 void parport_daisy_fini (struct parport *port)
175 {
176         struct daisydev **p;
177
178         spin_lock(&topology_lock);
179         p = &topology;
180         while (*p) {
181                 struct daisydev *dev = *p;
182                 if (dev->port != port) {
183                         p = &dev->next;
184                         continue;
185                 }
186                 *p = dev->next;
187                 kfree(dev);
188         }
189
190         /* Gaps in the numbering could be handled better.  How should
191            someone enumerate through all IEEE1284.3 devices in the
192            topology?. */
193         if (!topology) numdevs = 0;
194         spin_unlock(&topology_lock);
195         return;
196 }
197
198 /**
199  *      parport_open - find a device by canonical device number
200  *      @devnum: canonical device number
201  *      @name: name to associate with the device
202  *      @pf: preemption callback
203  *      @kf: kick callback
204  *      @irqf: interrupt handler
205  *      @flags: registration flags
206  *      @handle: driver data
207  *
208  *      This function is similar to parport_register_device(), except
209  *      that it locates a device by its number rather than by the port
210  *      it is attached to.  See parport_find_device() and
211  *      parport_find_class().
212  *
213  *      All parameters except for @devnum are the same as for
214  *      parport_register_device().  The return value is the same as
215  *      for parport_register_device().
216  **/
217
218 struct pardevice *parport_open (int devnum, const char *name,
219                                 int (*pf) (void *), void (*kf) (void *),
220                                 void (*irqf) (int, void *, struct pt_regs *),
221                                 int flags, void *handle)
222 {
223         struct daisydev *p = topology;
224         struct parport *port;
225         struct pardevice *dev;
226         int daisy;
227
228         spin_lock(&topology_lock);
229         while (p && p->devnum != devnum)
230                 p = p->next;
231
232         if (!p) {
233                 spin_unlock(&topology_lock);
234                 return NULL;
235         }
236
237         daisy = p->daisy;
238         port = parport_get_port(p->port);
239         spin_unlock(&topology_lock);
240
241         dev = parport_register_device (port, name, pf, kf,
242                                        irqf, flags, handle);
243         parport_put_port(port);
244         if (!dev)
245                 return NULL;
246
247         dev->daisy = daisy;
248
249         /* Check that there really is a device to select. */
250         if (daisy >= 0) {
251                 int selected;
252                 parport_claim_or_block (dev);
253                 selected = port->daisy;
254                 parport_release (dev);
255
256                 if (selected != port->daisy) {
257                         /* No corresponding device. */
258                         parport_unregister_device (dev);
259                         return NULL;
260                 }
261         }
262
263         return dev;
264 }
265
266 /**
267  *      parport_close - close a device opened with parport_open()
268  *      @dev: device to close
269  *
270  *      This is to parport_open() as parport_unregister_device() is to
271  *      parport_register_device().
272  **/
273
274 void parport_close (struct pardevice *dev)
275 {
276         parport_unregister_device (dev);
277 }
278
279 /**
280  *      parport_device_num - convert device coordinates
281  *      @parport: parallel port number
282  *      @mux: multiplexor port number (-1 for no multiplexor)
283  *      @daisy: daisy chain address (-1 for no daisy chain address)
284  *
285  *      This tries to locate a device on the given parallel port,
286  *      multiplexor port and daisy chain address, and returns its
287  *      device number or -NXIO if no device with those coordinates
288  *      exists.
289  **/
290
291 int parport_device_num (int parport, int mux, int daisy)
292 {
293         int res = -ENXIO;
294         struct daisydev *dev;
295
296         spin_lock(&topology_lock);
297         dev = topology;
298         while (dev && dev->port->portnum != parport &&
299                dev->port->muxport != mux && dev->daisy != daisy)
300                 dev = dev->next;
301         if (dev)
302                 res = dev->devnum;
303         spin_unlock(&topology_lock);
304
305         return res;
306 }
307
308 /**
309  *      parport_device_coords - convert canonical device number
310  *      @devnum: device number
311  *      @parport: pointer to storage for parallel port number
312  *      @mux: pointer to storage for multiplexor port number
313  *      @daisy: pointer to storage for daisy chain address
314  *
315  *      This function converts a device number into its coordinates in
316  *      terms of which parallel port in the system it is attached to,
317  *      which multiplexor port it is attached to if there is a
318  *      multiplexor on that port, and which daisy chain address it has
319  *      if it is in a daisy chain.
320  *
321  *      The caller must allocate storage for @parport, @mux, and
322  *      @daisy.
323  *
324  *      If there is no device with the specified device number, -ENXIO
325  *      is returned.  Otherwise, the values pointed to by @parport,
326  *      @mux, and @daisy are set to the coordinates of the device,
327  *      with -1 for coordinates with no value.
328  *
329  *      This function is not actually very useful, but this interface
330  *      was suggested by IEEE 1284.3.
331  **/
332
333 int parport_device_coords (int devnum, int *parport, int *mux, int *daisy)
334 {
335         struct daisydev *dev;
336
337         spin_lock(&topology_lock);
338
339         dev = topology;
340         while (dev && dev->devnum != devnum)
341                 dev = dev->next;
342
343         if (!dev) {
344                 spin_unlock(&topology_lock);
345                 return -ENXIO;
346         }
347
348         if (parport) *parport = dev->port->portnum;
349         if (mux) *mux = dev->port->muxport;
350         if (daisy) *daisy = dev->daisy;
351         spin_unlock(&topology_lock);
352         return 0;
353 }
354
355 /* Send a daisy-chain-style CPP command packet. */
356 static int cpp_daisy (struct parport *port, int cmd)
357 {
358         unsigned char s;
359
360         parport_data_forward (port);
361         parport_write_data (port, 0xaa); udelay (2);
362         parport_write_data (port, 0x55); udelay (2);
363         parport_write_data (port, 0x00); udelay (2);
364         parport_write_data (port, 0xff); udelay (2);
365         s = parport_read_status (port) & (PARPORT_STATUS_BUSY
366                                           | PARPORT_STATUS_PAPEROUT
367                                           | PARPORT_STATUS_SELECT
368                                           | PARPORT_STATUS_ERROR);
369         if (s != (PARPORT_STATUS_BUSY
370                   | PARPORT_STATUS_PAPEROUT
371                   | PARPORT_STATUS_SELECT
372                   | PARPORT_STATUS_ERROR)) {
373                 DPRINTK (KERN_DEBUG "%s: cpp_daisy: aa5500ff(%02x)\n",
374                          port->name, s);
375                 return -ENXIO;
376         }
377
378         parport_write_data (port, 0x87); udelay (2);
379         s = parport_read_status (port) & (PARPORT_STATUS_BUSY
380                                           | PARPORT_STATUS_PAPEROUT
381                                           | PARPORT_STATUS_SELECT
382                                           | PARPORT_STATUS_ERROR);
383         if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
384                 DPRINTK (KERN_DEBUG "%s: cpp_daisy: aa5500ff87(%02x)\n",
385                          port->name, s);
386                 return -ENXIO;
387         }
388
389         parport_write_data (port, 0x78); udelay (2);
390         parport_write_data (port, cmd); udelay (2);
391         parport_frob_control (port,
392                               PARPORT_CONTROL_STROBE,
393                               PARPORT_CONTROL_STROBE);
394         udelay (1);
395         parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
396         udelay (1);
397         s = parport_read_status (port);
398         parport_write_data (port, 0xff); udelay (2);
399
400         return s;
401 }
402
403 /* Send a mux-style CPP command packet. */
404 static int cpp_mux (struct parport *port, int cmd)
405 {
406         unsigned char s;
407         int rc;
408
409         parport_data_forward (port);
410         parport_write_data (port, 0xaa); udelay (2);
411         parport_write_data (port, 0x55); udelay (2);
412         parport_write_data (port, 0xf0); udelay (2);
413         parport_write_data (port, 0x0f); udelay (2);
414         parport_write_data (port, 0x52); udelay (2);
415         parport_write_data (port, 0xad); udelay (2);
416         parport_write_data (port, cmd); udelay (2);
417
418         s = parport_read_status (port);
419         if (!(s & PARPORT_STATUS_ACK)) {
420                 DPRINTK (KERN_DEBUG "%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
421                          port->name, cmd, s);
422                 return -EIO;
423         }
424
425         rc = (((s & PARPORT_STATUS_SELECT   ? 1 : 0) << 0) |
426               ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
427               ((s & PARPORT_STATUS_BUSY     ? 0 : 1) << 2) |
428               ((s & PARPORT_STATUS_ERROR    ? 0 : 1) << 3));
429
430         return rc;
431 }
432
433 void parport_daisy_deselect_all (struct parport *port)
434 {
435         cpp_daisy (port, 0x30);
436 }
437
438 int parport_daisy_select (struct parport *port, int daisy, int mode)
439 {
440         switch (mode)
441         {
442                 // For these modes we should switch to EPP mode:
443                 case IEEE1284_MODE_EPP:
444                 case IEEE1284_MODE_EPPSL:
445                 case IEEE1284_MODE_EPPSWE:
446                         return (cpp_daisy (port, 0x20 + daisy) &
447                                 PARPORT_STATUS_ERROR);
448
449                 // For these modes we should switch to ECP mode:
450                 case IEEE1284_MODE_ECP:
451                 case IEEE1284_MODE_ECPRLE:
452                 case IEEE1284_MODE_ECPSWE: 
453                         return (cpp_daisy (port, 0xd0 + daisy) &
454                                 PARPORT_STATUS_ERROR);
455
456                 // Nothing was told for BECP in Daisy chain specification.
457                 // May be it's wise to use ECP?
458                 case IEEE1284_MODE_BECP:
459                 // Others use compat mode
460                 case IEEE1284_MODE_NIBBLE:
461                 case IEEE1284_MODE_BYTE:
462                 case IEEE1284_MODE_COMPAT:
463                 default:
464                         return (cpp_daisy (port, 0xe0 + daisy) &
465                                 PARPORT_STATUS_ERROR);
466         }
467 }
468
469 static int mux_present (struct parport *port)
470 {
471         return cpp_mux (port, 0x51) == 3;
472 }
473
474 static int num_mux_ports (struct parport *port)
475 {
476         return cpp_mux (port, 0x58);
477 }
478
479 static int select_port (struct parport *port)
480 {
481         int muxport = port->muxport;
482         return cpp_mux (port, 0x60 + muxport) == muxport;
483 }
484
485 static int assign_addrs (struct parport *port)
486 {
487         unsigned char s, last_dev;
488         unsigned char daisy;
489         int thisdev = numdevs;
490         int detected;
491         char *deviceid;
492
493         parport_data_forward (port);
494         parport_write_data (port, 0xaa); udelay (2);
495         parport_write_data (port, 0x55); udelay (2);
496         parport_write_data (port, 0x00); udelay (2);
497         parport_write_data (port, 0xff); udelay (2);
498         s = parport_read_status (port) & (PARPORT_STATUS_BUSY
499                                           | PARPORT_STATUS_PAPEROUT
500                                           | PARPORT_STATUS_SELECT
501                                           | PARPORT_STATUS_ERROR);
502         if (s != (PARPORT_STATUS_BUSY
503                   | PARPORT_STATUS_PAPEROUT
504                   | PARPORT_STATUS_SELECT
505                   | PARPORT_STATUS_ERROR)) {
506                 DPRINTK (KERN_DEBUG "%s: assign_addrs: aa5500ff(%02x)\n",
507                          port->name, s);
508                 return 0;
509         }
510
511         parport_write_data (port, 0x87); udelay (2);
512         s = parport_read_status (port) & (PARPORT_STATUS_BUSY
513                                           | PARPORT_STATUS_PAPEROUT
514                                           | PARPORT_STATUS_SELECT
515                                           | PARPORT_STATUS_ERROR);
516         if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
517                 DPRINTK (KERN_DEBUG "%s: assign_addrs: aa5500ff87(%02x)\n",
518                          port->name, s);
519                 return 0;
520         }
521
522         parport_write_data (port, 0x78); udelay (2);
523         last_dev = 0; /* We've just been speaking to a device, so we
524                          know there must be at least _one_ out there. */
525
526         for (daisy = 0; daisy < 4; daisy++) {
527                 parport_write_data (port, daisy);
528                 udelay (2);
529                 parport_frob_control (port,
530                                       PARPORT_CONTROL_STROBE,
531                                       PARPORT_CONTROL_STROBE);
532                 udelay (1);
533                 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
534                 udelay (1);
535
536                 if (last_dev)
537                         /* No more devices. */
538                         break;
539
540                 last_dev = !(parport_read_status (port)
541                              & PARPORT_STATUS_BUSY);
542
543                 add_dev (numdevs++, port, daisy);
544         }
545
546         parport_write_data (port, 0xff); udelay (2);
547         detected = numdevs - thisdev;
548         DPRINTK (KERN_DEBUG "%s: Found %d daisy-chained devices\n", port->name,
549                  detected);
550
551         /* Ask the new devices to introduce themselves. */
552         deviceid = kmalloc (1000, GFP_KERNEL);
553         if (!deviceid) return 0;
554
555         for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
556                 parport_device_id (thisdev, deviceid, 1000);
557
558         kfree (deviceid);
559         return detected;
560 }
561
562 /* Find a device with a particular manufacturer and model string,
563    starting from a given device number.  Like the PCI equivalent,
564    'from' itself is skipped. */
565
566 /**
567  *      parport_find_device - find a specific device
568  *      @mfg: required manufacturer string
569  *      @mdl: required model string
570  *      @from: previous device number found in search, or %NULL for
571  *             new search
572  *
573  *      This walks through the list of parallel port devices looking
574  *      for a device whose 'MFG' string matches @mfg and whose 'MDL'
575  *      string matches @mdl in their IEEE 1284 Device ID.
576  *
577  *      When a device is found matching those requirements, its device
578  *      number is returned; if there is no matching device, a negative
579  *      value is returned.
580  *
581  *      A new search it initiated by passing %NULL as the @from
582  *      argument.  If @from is not %NULL, the search continues from
583  *      that device.
584  **/
585
586 int parport_find_device (const char *mfg, const char *mdl, int from)
587 {
588         struct daisydev *d;
589         int res = -1;
590
591         /* Find where to start. */
592
593         spin_lock(&topology_lock);
594         d = topology; /* sorted by devnum */
595         while (d && d->devnum <= from)
596                 d = d->next;
597
598         /* Search. */
599         while (d) {
600                 struct parport_device_info *info;
601                 info = &d->port->probe_info[1 + d->daisy];
602                 if ((!mfg || !strcmp (mfg, info->mfr)) &&
603                     (!mdl || !strcmp (mdl, info->model)))
604                         break;
605
606                 d = d->next;
607         }
608
609         if (d)
610                 res = d->devnum;
611
612         spin_unlock(&topology_lock);
613         return res;
614 }
615
616 /**
617  *      parport_find_class - find a device in a specified class
618  *      @cls: required class
619  *      @from: previous device number found in search, or %NULL for
620  *             new search
621  *
622  *      This walks through the list of parallel port devices looking
623  *      for a device whose 'CLS' string matches @cls in their IEEE
624  *      1284 Device ID.
625  *
626  *      When a device is found matching those requirements, its device
627  *      number is returned; if there is no matching device, a negative
628  *      value is returned.
629  *
630  *      A new search it initiated by passing %NULL as the @from
631  *      argument.  If @from is not %NULL, the search continues from
632  *      that device.
633  **/
634
635 int parport_find_class (parport_device_class cls, int from)
636 {
637         struct daisydev *d;
638         int res = -1;
639
640         spin_lock(&topology_lock);
641         d = topology; /* sorted by devnum */
642         /* Find where to start. */
643         while (d && d->devnum <= from)
644                 d = d->next;
645
646         /* Search. */
647         while (d && d->port->probe_info[1 + d->daisy].class != cls)
648                 d = d->next;
649
650         if (d)
651                 res = d->devnum;
652
653         spin_unlock(&topology_lock);
654         return res;
655 }
656
657 EXPORT_SYMBOL(parport_open);
658 EXPORT_SYMBOL(parport_close);
659 EXPORT_SYMBOL(parport_device_num);
660 EXPORT_SYMBOL(parport_device_coords);
661 EXPORT_SYMBOL(parport_daisy_deselect_all);
662 EXPORT_SYMBOL(parport_daisy_select);
663 EXPORT_SYMBOL(parport_daisy_init);
664 EXPORT_SYMBOL(parport_find_device);
665 EXPORT_SYMBOL(parport_find_class);