f87cc802167e74cef2960940c6ad171b7b17cc49
[linux-2.6-microblaze.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/slab.h>
26 #include <linux/sched/signal.h>
27
28 #include <asm/current.h>
29 #include <linux/uaccess.h>
30
31 #undef DEBUG
32
33 #ifdef DEBUG
34 #define DPRINTK(stuff...) printk(stuff)
35 #else
36 #define DPRINTK(stuff...)
37 #endif
38
39 static struct daisydev {
40         struct daisydev *next;
41         struct parport *port;
42         int daisy;
43         int devnum;
44 } *topology = NULL;
45 static DEFINE_SPINLOCK(topology_lock);
46
47 static int numdevs;
48 static bool daisy_init_done;
49
50 /* Forward-declaration of lower-level functions. */
51 static int mux_present(struct parport *port);
52 static int num_mux_ports(struct parport *port);
53 static int select_port(struct parport *port);
54 static int assign_addrs(struct parport *port);
55
56 /* Add a device to the discovered topology. */
57 static void add_dev(int devnum, struct parport *port, int daisy)
58 {
59         struct daisydev *newdev, **p;
60         newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
61         if (newdev) {
62                 newdev->port = port;
63                 newdev->daisy = daisy;
64                 newdev->devnum = devnum;
65                 spin_lock(&topology_lock);
66                 for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
67                         ;
68                 newdev->next = *p;
69                 *p = newdev;
70                 spin_unlock(&topology_lock);
71         }
72 }
73
74 /* Clone a parport (actually, make an alias). */
75 static struct parport *clone_parport(struct parport *real, int muxport)
76 {
77         struct parport *extra = parport_register_port(real->base,
78                                                        real->irq,
79                                                        real->dma,
80                                                        real->ops);
81         if (extra) {
82                 extra->portnum = real->portnum;
83                 extra->physport = real;
84                 extra->muxport = muxport;
85                 real->slaves[muxport-1] = extra;
86         }
87
88         return extra;
89 }
90
91 static int daisy_drv_probe(struct pardevice *par_dev)
92 {
93         struct device_driver *drv = par_dev->dev.driver;
94
95         if (strcmp(drv->name, "daisy_drv"))
96                 return -ENODEV;
97         if (strcmp(par_dev->name, daisy_dev_name))
98                 return -ENODEV;
99
100         return 0;
101 }
102
103 static struct parport_driver daisy_driver = {
104         .name = "daisy_drv",
105         .probe = daisy_drv_probe,
106         .devmodel = true,
107 };
108
109 /* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains.
110  * Return value is number of devices actually detected. */
111 int parport_daisy_init(struct parport *port)
112 {
113         int detected = 0;
114         char *deviceid;
115         static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
116         int num_ports;
117         int i;
118         int last_try = 0;
119
120         if (!daisy_init_done) {
121                 /*
122                  * flag should be marked true first as
123                  * parport_register_driver() might try to load the low
124                  * level driver which will lead to announcing new ports
125                  * and which will again come back here at
126                  * parport_daisy_init()
127                  */
128                 daisy_init_done = true;
129                 i = parport_register_driver(&daisy_driver);
130                 if (i) {
131                         pr_err("daisy registration failed\n");
132                         daisy_init_done = false;
133                         return i;
134                 }
135         }
136
137 again:
138         /* Because this is called before any other devices exist,
139          * we don't have to claim exclusive access.  */
140
141         /* If mux present on normal port, need to create new
142          * parports for each extra port. */
143         if (port->muxport < 0 && mux_present(port) &&
144             /* don't be fooled: a mux must have 2 or 4 ports. */
145             ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
146                 /* Leave original as port zero. */
147                 port->muxport = 0;
148                 pr_info("%s: 1st (default) port of %d-way multiplexor\n",
149                         port->name, num_ports);
150                 for (i = 1; i < num_ports; i++) {
151                         /* Clone the port. */
152                         struct parport *extra = clone_parport(port, i);
153                         if (!extra) {
154                                 if (signal_pending(current))
155                                         break;
156
157                                 schedule();
158                                 continue;
159                         }
160
161                         pr_info("%s: %d%s port of %d-way multiplexor on %s\n",
162                                 extra->name, i + 1, th[i + 1], num_ports,
163                                 port->name);
164
165                         /* Analyse that port too.  We won't recurse
166                            forever because of the 'port->muxport < 0'
167                            test above. */
168                         parport_daisy_init(extra);
169                 }
170         }
171
172         if (port->muxport >= 0)
173                 select_port(port);
174
175         parport_daisy_deselect_all(port);
176         detected += assign_addrs(port);
177
178         /* Count the potential legacy device at the end. */
179         add_dev(numdevs++, port, -1);
180
181         /* Find out the legacy device's IEEE 1284 device ID. */
182         deviceid = kmalloc(1024, GFP_KERNEL);
183         if (deviceid) {
184                 if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
185                         detected++;
186
187                 kfree(deviceid);
188         }
189
190         if (!detected && !last_try) {
191                 /* No devices were detected.  Perhaps they are in some
192                    funny state; let's try to reset them and see if
193                    they wake up. */
194                 parport_daisy_fini(port);
195                 parport_write_control(port, PARPORT_CONTROL_SELECT);
196                 udelay(50);
197                 parport_write_control(port,
198                                        PARPORT_CONTROL_SELECT |
199                                        PARPORT_CONTROL_INIT);
200                 udelay(50);
201                 last_try = 1;
202                 goto again;
203         }
204
205         return detected;
206 }
207
208 /* Forget about devices on a physical port. */
209 void parport_daisy_fini(struct parport *port)
210 {
211         struct daisydev **p;
212
213         spin_lock(&topology_lock);
214         p = &topology;
215         while (*p) {
216                 struct daisydev *dev = *p;
217                 if (dev->port != port) {
218                         p = &dev->next;
219                         continue;
220                 }
221                 *p = dev->next;
222                 kfree(dev);
223         }
224
225         /* Gaps in the numbering could be handled better.  How should
226            someone enumerate through all IEEE1284.3 devices in the
227            topology?. */
228         if (!topology) numdevs = 0;
229         spin_unlock(&topology_lock);
230         return;
231 }
232
233 /**
234  *      parport_open - find a device by canonical device number
235  *      @devnum: canonical device number
236  *      @name: name to associate with the device
237  *
238  *      This function is similar to parport_register_device(), except
239  *      that it locates a device by its number rather than by the port
240  *      it is attached to.
241  *
242  *      All parameters except for @devnum are the same as for
243  *      parport_register_device().  The return value is the same as
244  *      for parport_register_device().
245  **/
246
247 struct pardevice *parport_open(int devnum, const char *name)
248 {
249         struct daisydev *p = topology;
250         struct pardev_cb par_cb;
251         struct parport *port;
252         struct pardevice *dev;
253         int daisy;
254
255         memset(&par_cb, 0, sizeof(par_cb));
256         spin_lock(&topology_lock);
257         while (p && p->devnum != devnum)
258                 p = p->next;
259
260         if (!p) {
261                 spin_unlock(&topology_lock);
262                 return NULL;
263         }
264
265         daisy = p->daisy;
266         port = parport_get_port(p->port);
267         spin_unlock(&topology_lock);
268
269         dev = parport_register_dev_model(port, name, &par_cb, devnum);
270         parport_put_port(port);
271         if (!dev)
272                 return NULL;
273
274         dev->daisy = daisy;
275
276         /* Check that there really is a device to select. */
277         if (daisy >= 0) {
278                 int selected;
279                 parport_claim_or_block(dev);
280                 selected = port->daisy;
281                 parport_release(dev);
282
283                 if (selected != daisy) {
284                         /* No corresponding device. */
285                         parport_unregister_device(dev);
286                         return NULL;
287                 }
288         }
289
290         return dev;
291 }
292
293 /**
294  *      parport_close - close a device opened with parport_open()
295  *      @dev: device to close
296  *
297  *      This is to parport_open() as parport_unregister_device() is to
298  *      parport_register_device().
299  **/
300
301 void parport_close(struct pardevice *dev)
302 {
303         parport_unregister_device(dev);
304 }
305
306 /* Send a daisy-chain-style CPP command packet. */
307 static int cpp_daisy(struct parport *port, int cmd)
308 {
309         unsigned char s;
310
311         parport_data_forward(port);
312         parport_write_data(port, 0xaa); udelay(2);
313         parport_write_data(port, 0x55); udelay(2);
314         parport_write_data(port, 0x00); udelay(2);
315         parport_write_data(port, 0xff); udelay(2);
316         s = parport_read_status(port) & (PARPORT_STATUS_BUSY
317                                           | PARPORT_STATUS_PAPEROUT
318                                           | PARPORT_STATUS_SELECT
319                                           | PARPORT_STATUS_ERROR);
320         if (s != (PARPORT_STATUS_BUSY
321                   | PARPORT_STATUS_PAPEROUT
322                   | PARPORT_STATUS_SELECT
323                   | PARPORT_STATUS_ERROR)) {
324                 DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff(%02x)\n",
325                          port->name, s);
326                 return -ENXIO;
327         }
328
329         parport_write_data(port, 0x87); udelay(2);
330         s = parport_read_status(port) & (PARPORT_STATUS_BUSY
331                                           | PARPORT_STATUS_PAPEROUT
332                                           | PARPORT_STATUS_SELECT
333                                           | PARPORT_STATUS_ERROR);
334         if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
335                 DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff87(%02x)\n",
336                          port->name, s);
337                 return -ENXIO;
338         }
339
340         parport_write_data(port, 0x78); udelay(2);
341         parport_write_data(port, cmd); udelay(2);
342         parport_frob_control(port,
343                               PARPORT_CONTROL_STROBE,
344                               PARPORT_CONTROL_STROBE);
345         udelay(1);
346         s = parport_read_status(port);
347         parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
348         udelay(1);
349         parport_write_data(port, 0xff); udelay(2);
350
351         return s;
352 }
353
354 /* Send a mux-style CPP command packet. */
355 static int cpp_mux(struct parport *port, int cmd)
356 {
357         unsigned char s;
358         int rc;
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, 0xf0); udelay(2);
364         parport_write_data(port, 0x0f); udelay(2);
365         parport_write_data(port, 0x52); udelay(2);
366         parport_write_data(port, 0xad); udelay(2);
367         parport_write_data(port, cmd); udelay(2);
368
369         s = parport_read_status(port);
370         if (!(s & PARPORT_STATUS_ACK)) {
371                 DPRINTK(KERN_DEBUG "%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
372                          port->name, cmd, s);
373                 return -EIO;
374         }
375
376         rc = (((s & PARPORT_STATUS_SELECT   ? 1 : 0) << 0) |
377               ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
378               ((s & PARPORT_STATUS_BUSY     ? 0 : 1) << 2) |
379               ((s & PARPORT_STATUS_ERROR    ? 0 : 1) << 3));
380
381         return rc;
382 }
383
384 void parport_daisy_deselect_all(struct parport *port)
385 {
386         cpp_daisy(port, 0x30);
387 }
388
389 int parport_daisy_select(struct parport *port, int daisy, int mode)
390 {
391         switch (mode)
392         {
393                 // For these modes we should switch to EPP mode:
394                 case IEEE1284_MODE_EPP:
395                 case IEEE1284_MODE_EPPSL:
396                 case IEEE1284_MODE_EPPSWE:
397                         return !(cpp_daisy(port, 0x20 + daisy) &
398                                  PARPORT_STATUS_ERROR);
399
400                 // For these modes we should switch to ECP mode:
401                 case IEEE1284_MODE_ECP:
402                 case IEEE1284_MODE_ECPRLE:
403                 case IEEE1284_MODE_ECPSWE: 
404                         return !(cpp_daisy(port, 0xd0 + daisy) &
405                                  PARPORT_STATUS_ERROR);
406
407                 // Nothing was told for BECP in Daisy chain specification.
408                 // May be it's wise to use ECP?
409                 case IEEE1284_MODE_BECP:
410                 // Others use compat mode
411                 case IEEE1284_MODE_NIBBLE:
412                 case IEEE1284_MODE_BYTE:
413                 case IEEE1284_MODE_COMPAT:
414                 default:
415                         return !(cpp_daisy(port, 0xe0 + daisy) &
416                                  PARPORT_STATUS_ERROR);
417         }
418 }
419
420 static int mux_present(struct parport *port)
421 {
422         return cpp_mux(port, 0x51) == 3;
423 }
424
425 static int num_mux_ports(struct parport *port)
426 {
427         return cpp_mux(port, 0x58);
428 }
429
430 static int select_port(struct parport *port)
431 {
432         int muxport = port->muxport;
433         return cpp_mux(port, 0x60 + muxport) == muxport;
434 }
435
436 static int assign_addrs(struct parport *port)
437 {
438         unsigned char s;
439         unsigned char daisy;
440         int thisdev = numdevs;
441         int detected;
442         char *deviceid;
443
444         parport_data_forward(port);
445         parport_write_data(port, 0xaa); udelay(2);
446         parport_write_data(port, 0x55); udelay(2);
447         parport_write_data(port, 0x00); udelay(2);
448         parport_write_data(port, 0xff); udelay(2);
449         s = parport_read_status(port) & (PARPORT_STATUS_BUSY
450                                           | PARPORT_STATUS_PAPEROUT
451                                           | PARPORT_STATUS_SELECT
452                                           | PARPORT_STATUS_ERROR);
453         if (s != (PARPORT_STATUS_BUSY
454                   | PARPORT_STATUS_PAPEROUT
455                   | PARPORT_STATUS_SELECT
456                   | PARPORT_STATUS_ERROR)) {
457                 DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff(%02x)\n",
458                          port->name, s);
459                 return 0;
460         }
461
462         parport_write_data(port, 0x87); udelay(2);
463         s = parport_read_status(port) & (PARPORT_STATUS_BUSY
464                                           | PARPORT_STATUS_PAPEROUT
465                                           | PARPORT_STATUS_SELECT
466                                           | PARPORT_STATUS_ERROR);
467         if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
468                 DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff87(%02x)\n",
469                          port->name, s);
470                 return 0;
471         }
472
473         parport_write_data(port, 0x78); udelay(2);
474         s = parport_read_status(port);
475
476         for (daisy = 0;
477              (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
478                      == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
479                      && daisy < 4;
480              ++daisy) {
481                 parport_write_data(port, daisy);
482                 udelay(2);
483                 parport_frob_control(port,
484                                       PARPORT_CONTROL_STROBE,
485                                       PARPORT_CONTROL_STROBE);
486                 udelay(1);
487                 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
488                 udelay(1);
489
490                 add_dev(numdevs++, port, daisy);
491
492                 /* See if this device thought it was the last in the
493                  * chain. */
494                 if (!(s & PARPORT_STATUS_BUSY))
495                         break;
496
497                 /* We are seeing pass through status now. We see
498                    last_dev from next device or if last_dev does not
499                    work status lines from some non-daisy chain
500                    device. */
501                 s = parport_read_status(port);
502         }
503
504         parport_write_data(port, 0xff); udelay(2);
505         detected = numdevs - thisdev;
506         DPRINTK(KERN_DEBUG "%s: Found %d daisy-chained devices\n", port->name,
507                  detected);
508
509         /* Ask the new devices to introduce themselves. */
510         deviceid = kmalloc(1024, GFP_KERNEL);
511         if (!deviceid) return 0;
512
513         for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
514                 parport_device_id(thisdev, deviceid, 1024);
515
516         kfree(deviceid);
517         return detected;
518 }