2 * IEEE 1284.3 Parallel port daisy chain and multiplexor code
4 * Copyright (C) 1999, 2000 Tim Waugh <tim@cyberelk.demon.co.uk>
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.
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.
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.
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>
28 #include <asm/current.h>
29 #include <linux/uaccess.h>
34 #define DPRINTK(stuff...) printk(stuff)
36 #define DPRINTK(stuff...)
39 static struct daisydev
{
40 struct daisydev
*next
;
45 static DEFINE_SPINLOCK(topology_lock
);
48 static bool daisy_init_done
;
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
);
56 /* Add a device to the discovered topology. */
57 static void add_dev(int devnum
, struct parport
*port
, int daisy
)
59 struct daisydev
*newdev
, **p
;
60 newdev
= kmalloc(sizeof(struct daisydev
), GFP_KERNEL
);
63 newdev
->daisy
= daisy
;
64 newdev
->devnum
= devnum
;
65 spin_lock(&topology_lock
);
66 for (p
= &topology
; *p
&& (*p
)->devnum
<devnum
; p
= &(*p
)->next
)
70 spin_unlock(&topology_lock
);
74 /* Clone a parport (actually, make an alias). */
75 static struct parport
*clone_parport(struct parport
*real
, int muxport
)
77 struct parport
*extra
= parport_register_port(real
->base
,
82 extra
->portnum
= real
->portnum
;
83 extra
->physport
= real
;
84 extra
->muxport
= muxport
;
85 real
->slaves
[muxport
-1] = extra
;
91 static int daisy_drv_probe(struct pardevice
*par_dev
)
93 struct device_driver
*drv
= par_dev
->dev
.driver
;
95 if (strcmp(drv
->name
, "daisy_drv"))
97 if (strcmp(par_dev
->name
, daisy_dev_name
))
103 static struct parport_driver daisy_driver
= {
105 .probe
= daisy_drv_probe
,
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
)
115 static const char *th
[] = { /*0*/"th", "st", "nd", "rd", "th" };
120 if (!daisy_init_done
) {
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()
128 daisy_init_done
= true;
129 i
= parport_register_driver(&daisy_driver
);
131 pr_err("daisy registration failed\n");
132 daisy_init_done
= false;
138 /* Because this is called before any other devices exist,
139 * we don't have to claim exclusive access. */
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. */
149 "%s: 1st (default) port of %d-way multiplexor\n",
150 port
->name
, num_ports
);
151 for (i
= 1; i
< num_ports
; i
++) {
152 /* Clone the port. */
153 struct parport
*extra
= clone_parport(port
, i
);
155 if (signal_pending(current
))
163 "%s: %d%s port of %d-way multiplexor on %s\n",
164 extra
->name
, i
+ 1, th
[i
+ 1], num_ports
,
167 /* Analyse that port too. We won't recurse
168 forever because of the 'port->muxport < 0'
170 parport_daisy_init(extra
);
174 if (port
->muxport
>= 0)
177 parport_daisy_deselect_all(port
);
178 detected
+= assign_addrs(port
);
180 /* Count the potential legacy device at the end. */
181 add_dev(numdevs
++, port
, -1);
183 /* Find out the legacy device's IEEE 1284 device ID. */
184 deviceid
= kmalloc(1024, GFP_KERNEL
);
186 if (parport_device_id(numdevs
- 1, deviceid
, 1024) > 2)
192 if (!detected
&& !last_try
) {
193 /* No devices were detected. Perhaps they are in some
194 funny state; let's try to reset them and see if
196 parport_daisy_fini(port
);
197 parport_write_control(port
, PARPORT_CONTROL_SELECT
);
199 parport_write_control(port
,
200 PARPORT_CONTROL_SELECT
|
201 PARPORT_CONTROL_INIT
);
210 /* Forget about devices on a physical port. */
211 void parport_daisy_fini(struct parport
*port
)
215 spin_lock(&topology_lock
);
218 struct daisydev
*dev
= *p
;
219 if (dev
->port
!= port
) {
227 /* Gaps in the numbering could be handled better. How should
228 someone enumerate through all IEEE1284.3 devices in the
230 if (!topology
) numdevs
= 0;
231 spin_unlock(&topology_lock
);
236 * parport_open - find a device by canonical device number
237 * @devnum: canonical device number
238 * @name: name to associate with the device
240 * This function is similar to parport_register_device(), except
241 * that it locates a device by its number rather than by the port
244 * All parameters except for @devnum are the same as for
245 * parport_register_device(). The return value is the same as
246 * for parport_register_device().
249 struct pardevice
*parport_open(int devnum
, const char *name
)
251 struct daisydev
*p
= topology
;
252 struct pardev_cb par_cb
;
253 struct parport
*port
;
254 struct pardevice
*dev
;
257 memset(&par_cb
, 0, sizeof(par_cb
));
258 spin_lock(&topology_lock
);
259 while (p
&& p
->devnum
!= devnum
)
263 spin_unlock(&topology_lock
);
268 port
= parport_get_port(p
->port
);
269 spin_unlock(&topology_lock
);
271 dev
= parport_register_dev_model(port
, name
, &par_cb
, devnum
);
272 parport_put_port(port
);
278 /* Check that there really is a device to select. */
281 parport_claim_or_block(dev
);
282 selected
= port
->daisy
;
283 parport_release(dev
);
285 if (selected
!= daisy
) {
286 /* No corresponding device. */
287 parport_unregister_device(dev
);
296 * parport_close - close a device opened with parport_open()
297 * @dev: device to close
299 * This is to parport_open() as parport_unregister_device() is to
300 * parport_register_device().
303 void parport_close(struct pardevice
*dev
)
305 parport_unregister_device(dev
);
308 /* Send a daisy-chain-style CPP command packet. */
309 static int cpp_daisy(struct parport
*port
, int cmd
)
313 parport_data_forward(port
);
314 parport_write_data(port
, 0xaa); udelay(2);
315 parport_write_data(port
, 0x55); udelay(2);
316 parport_write_data(port
, 0x00); udelay(2);
317 parport_write_data(port
, 0xff); udelay(2);
318 s
= parport_read_status(port
) & (PARPORT_STATUS_BUSY
319 | PARPORT_STATUS_PAPEROUT
320 | PARPORT_STATUS_SELECT
321 | PARPORT_STATUS_ERROR
);
322 if (s
!= (PARPORT_STATUS_BUSY
323 | PARPORT_STATUS_PAPEROUT
324 | PARPORT_STATUS_SELECT
325 | PARPORT_STATUS_ERROR
)) {
326 DPRINTK(KERN_DEBUG
"%s: cpp_daisy: aa5500ff(%02x)\n",
331 parport_write_data(port
, 0x87); udelay(2);
332 s
= parport_read_status(port
) & (PARPORT_STATUS_BUSY
333 | PARPORT_STATUS_PAPEROUT
334 | PARPORT_STATUS_SELECT
335 | PARPORT_STATUS_ERROR
);
336 if (s
!= (PARPORT_STATUS_SELECT
| PARPORT_STATUS_ERROR
)) {
337 DPRINTK(KERN_DEBUG
"%s: cpp_daisy: aa5500ff87(%02x)\n",
342 parport_write_data(port
, 0x78); udelay(2);
343 parport_write_data(port
, cmd
); udelay(2);
344 parport_frob_control(port
,
345 PARPORT_CONTROL_STROBE
,
346 PARPORT_CONTROL_STROBE
);
348 s
= parport_read_status(port
);
349 parport_frob_control(port
, PARPORT_CONTROL_STROBE
, 0);
351 parport_write_data(port
, 0xff); udelay(2);
356 /* Send a mux-style CPP command packet. */
357 static int cpp_mux(struct parport
*port
, int cmd
)
362 parport_data_forward(port
);
363 parport_write_data(port
, 0xaa); udelay(2);
364 parport_write_data(port
, 0x55); udelay(2);
365 parport_write_data(port
, 0xf0); udelay(2);
366 parport_write_data(port
, 0x0f); udelay(2);
367 parport_write_data(port
, 0x52); udelay(2);
368 parport_write_data(port
, 0xad); udelay(2);
369 parport_write_data(port
, cmd
); udelay(2);
371 s
= parport_read_status(port
);
372 if (!(s
& PARPORT_STATUS_ACK
)) {
373 DPRINTK(KERN_DEBUG
"%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
378 rc
= (((s
& PARPORT_STATUS_SELECT
? 1 : 0) << 0) |
379 ((s
& PARPORT_STATUS_PAPEROUT
? 1 : 0) << 1) |
380 ((s
& PARPORT_STATUS_BUSY
? 0 : 1) << 2) |
381 ((s
& PARPORT_STATUS_ERROR
? 0 : 1) << 3));
386 void parport_daisy_deselect_all(struct parport
*port
)
388 cpp_daisy(port
, 0x30);
391 int parport_daisy_select(struct parport
*port
, int daisy
, int mode
)
395 // For these modes we should switch to EPP mode:
396 case IEEE1284_MODE_EPP
:
397 case IEEE1284_MODE_EPPSL
:
398 case IEEE1284_MODE_EPPSWE
:
399 return !(cpp_daisy(port
, 0x20 + daisy
) &
400 PARPORT_STATUS_ERROR
);
402 // For these modes we should switch to ECP mode:
403 case IEEE1284_MODE_ECP
:
404 case IEEE1284_MODE_ECPRLE
:
405 case IEEE1284_MODE_ECPSWE
:
406 return !(cpp_daisy(port
, 0xd0 + daisy
) &
407 PARPORT_STATUS_ERROR
);
409 // Nothing was told for BECP in Daisy chain specification.
410 // May be it's wise to use ECP?
411 case IEEE1284_MODE_BECP
:
412 // Others use compat mode
413 case IEEE1284_MODE_NIBBLE
:
414 case IEEE1284_MODE_BYTE
:
415 case IEEE1284_MODE_COMPAT
:
417 return !(cpp_daisy(port
, 0xe0 + daisy
) &
418 PARPORT_STATUS_ERROR
);
422 static int mux_present(struct parport
*port
)
424 return cpp_mux(port
, 0x51) == 3;
427 static int num_mux_ports(struct parport
*port
)
429 return cpp_mux(port
, 0x58);
432 static int select_port(struct parport
*port
)
434 int muxport
= port
->muxport
;
435 return cpp_mux(port
, 0x60 + muxport
) == muxport
;
438 static int assign_addrs(struct parport
*port
)
442 int thisdev
= numdevs
;
446 parport_data_forward(port
);
447 parport_write_data(port
, 0xaa); udelay(2);
448 parport_write_data(port
, 0x55); udelay(2);
449 parport_write_data(port
, 0x00); udelay(2);
450 parport_write_data(port
, 0xff); udelay(2);
451 s
= parport_read_status(port
) & (PARPORT_STATUS_BUSY
452 | PARPORT_STATUS_PAPEROUT
453 | PARPORT_STATUS_SELECT
454 | PARPORT_STATUS_ERROR
);
455 if (s
!= (PARPORT_STATUS_BUSY
456 | PARPORT_STATUS_PAPEROUT
457 | PARPORT_STATUS_SELECT
458 | PARPORT_STATUS_ERROR
)) {
459 DPRINTK(KERN_DEBUG
"%s: assign_addrs: aa5500ff(%02x)\n",
464 parport_write_data(port
, 0x87); udelay(2);
465 s
= parport_read_status(port
) & (PARPORT_STATUS_BUSY
466 | PARPORT_STATUS_PAPEROUT
467 | PARPORT_STATUS_SELECT
468 | PARPORT_STATUS_ERROR
);
469 if (s
!= (PARPORT_STATUS_SELECT
| PARPORT_STATUS_ERROR
)) {
470 DPRINTK(KERN_DEBUG
"%s: assign_addrs: aa5500ff87(%02x)\n",
475 parport_write_data(port
, 0x78); udelay(2);
476 s
= parport_read_status(port
);
479 (s
& (PARPORT_STATUS_PAPEROUT
|PARPORT_STATUS_SELECT
))
480 == (PARPORT_STATUS_PAPEROUT
|PARPORT_STATUS_SELECT
)
483 parport_write_data(port
, daisy
);
485 parport_frob_control(port
,
486 PARPORT_CONTROL_STROBE
,
487 PARPORT_CONTROL_STROBE
);
489 parport_frob_control(port
, PARPORT_CONTROL_STROBE
, 0);
492 add_dev(numdevs
++, port
, daisy
);
494 /* See if this device thought it was the last in the
496 if (!(s
& PARPORT_STATUS_BUSY
))
499 /* We are seeing pass through status now. We see
500 last_dev from next device or if last_dev does not
501 work status lines from some non-daisy chain
503 s
= parport_read_status(port
);
506 parport_write_data(port
, 0xff); udelay(2);
507 detected
= numdevs
- thisdev
;
508 DPRINTK(KERN_DEBUG
"%s: Found %d daisy-chained devices\n", port
->name
,
511 /* Ask the new devices to introduce themselves. */
512 deviceid
= kmalloc(1024, GFP_KERNEL
);
513 if (!deviceid
) return 0;
515 for (daisy
= 0; thisdev
< numdevs
; thisdev
++, daisy
++)
516 parport_device_id(thisdev
, deviceid
, 1024);