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>
33 static struct daisydev {
34 struct daisydev *next;
39 static DEFINE_SPINLOCK(topology_lock);
42 static bool daisy_init_done;
44 /* Forward-declaration of lower-level functions. */
45 static int mux_present(struct parport *port);
46 static int num_mux_ports(struct parport *port);
47 static int select_port(struct parport *port);
48 static int assign_addrs(struct parport *port);
50 /* Add a device to the discovered topology. */
51 static void add_dev(int devnum, struct parport *port, int daisy)
53 struct daisydev *newdev, **p;
54 newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
57 newdev->daisy = daisy;
58 newdev->devnum = devnum;
59 spin_lock(&topology_lock);
60 for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
64 spin_unlock(&topology_lock);
68 /* Clone a parport (actually, make an alias). */
69 static struct parport *clone_parport(struct parport *real, int muxport)
71 struct parport *extra = parport_register_port(real->base,
76 extra->portnum = real->portnum;
77 extra->physport = real;
78 extra->muxport = muxport;
79 real->slaves[muxport-1] = extra;
85 static int daisy_drv_probe(struct pardevice *par_dev)
87 struct device_driver *drv = par_dev->dev.driver;
89 if (strcmp(drv->name, "daisy_drv"))
91 if (strcmp(par_dev->name, daisy_dev_name))
97 static struct parport_driver daisy_driver = {
99 .probe = daisy_drv_probe,
103 /* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains.
104 * Return value is number of devices actually detected. */
105 int parport_daisy_init(struct parport *port)
109 static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
114 if (!daisy_init_done) {
116 * flag should be marked true first as
117 * parport_register_driver() might try to load the low
118 * level driver which will lead to announcing new ports
119 * and which will again come back here at
120 * parport_daisy_init()
122 daisy_init_done = true;
123 i = parport_register_driver(&daisy_driver);
125 pr_err("daisy registration failed\n");
126 daisy_init_done = false;
132 /* Because this is called before any other devices exist,
133 * we don't have to claim exclusive access. */
135 /* If mux present on normal port, need to create new
136 * parports for each extra port. */
137 if (port->muxport < 0 && mux_present(port) &&
138 /* don't be fooled: a mux must have 2 or 4 ports. */
139 ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
140 /* Leave original as port zero. */
142 pr_info("%s: 1st (default) port of %d-way multiplexor\n",
143 port->name, num_ports);
144 for (i = 1; i < num_ports; i++) {
145 /* Clone the port. */
146 struct parport *extra = clone_parport(port, i);
148 if (signal_pending(current))
155 pr_info("%s: %d%s port of %d-way multiplexor on %s\n",
156 extra->name, i + 1, th[i + 1], num_ports,
159 /* Analyse that port too. We won't recurse
160 forever because of the 'port->muxport < 0'
162 parport_daisy_init(extra);
166 if (port->muxport >= 0)
169 parport_daisy_deselect_all(port);
170 detected += assign_addrs(port);
172 /* Count the potential legacy device at the end. */
173 add_dev(numdevs++, port, -1);
175 /* Find out the legacy device's IEEE 1284 device ID. */
176 deviceid = kmalloc(1024, GFP_KERNEL);
178 if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
184 if (!detected && !last_try) {
185 /* No devices were detected. Perhaps they are in some
186 funny state; let's try to reset them and see if
188 parport_daisy_fini(port);
189 parport_write_control(port, PARPORT_CONTROL_SELECT);
191 parport_write_control(port,
192 PARPORT_CONTROL_SELECT |
193 PARPORT_CONTROL_INIT);
202 /* Forget about devices on a physical port. */
203 void parport_daisy_fini(struct parport *port)
207 spin_lock(&topology_lock);
210 struct daisydev *dev = *p;
211 if (dev->port != port) {
219 /* Gaps in the numbering could be handled better. How should
220 someone enumerate through all IEEE1284.3 devices in the
222 if (!topology) numdevs = 0;
223 spin_unlock(&topology_lock);
228 * parport_open - find a device by canonical device number
229 * @devnum: canonical device number
230 * @name: name to associate with the device
232 * This function is similar to parport_register_device(), except
233 * that it locates a device by its number rather than by the port
236 * All parameters except for @devnum are the same as for
237 * parport_register_device(). The return value is the same as
238 * for parport_register_device().
241 struct pardevice *parport_open(int devnum, const char *name)
243 struct daisydev *p = topology;
244 struct pardev_cb par_cb;
245 struct parport *port;
246 struct pardevice *dev;
249 memset(&par_cb, 0, sizeof(par_cb));
250 spin_lock(&topology_lock);
251 while (p && p->devnum != devnum)
255 spin_unlock(&topology_lock);
260 port = parport_get_port(p->port);
261 spin_unlock(&topology_lock);
263 dev = parport_register_dev_model(port, name, &par_cb, devnum);
264 parport_put_port(port);
270 /* Check that there really is a device to select. */
273 parport_claim_or_block(dev);
274 selected = port->daisy;
275 parport_release(dev);
277 if (selected != daisy) {
278 /* No corresponding device. */
279 parport_unregister_device(dev);
288 * parport_close - close a device opened with parport_open()
289 * @dev: device to close
291 * This is to parport_open() as parport_unregister_device() is to
292 * parport_register_device().
295 void parport_close(struct pardevice *dev)
297 parport_unregister_device(dev);
300 /* Send a daisy-chain-style CPP command packet. */
301 static int cpp_daisy(struct parport *port, int cmd)
305 parport_data_forward(port);
306 parport_write_data(port, 0xaa); udelay(2);
307 parport_write_data(port, 0x55); udelay(2);
308 parport_write_data(port, 0x00); udelay(2);
309 parport_write_data(port, 0xff); udelay(2);
310 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
311 | PARPORT_STATUS_PAPEROUT
312 | PARPORT_STATUS_SELECT
313 | PARPORT_STATUS_ERROR);
314 if (s != (PARPORT_STATUS_BUSY
315 | PARPORT_STATUS_PAPEROUT
316 | PARPORT_STATUS_SELECT
317 | PARPORT_STATUS_ERROR)) {
318 pr_debug("%s: cpp_daisy: aa5500ff(%02x)\n", port->name, s);
322 parport_write_data(port, 0x87); udelay(2);
323 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
324 | PARPORT_STATUS_PAPEROUT
325 | PARPORT_STATUS_SELECT
326 | PARPORT_STATUS_ERROR);
327 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
328 pr_debug("%s: cpp_daisy: aa5500ff87(%02x)\n", port->name, s);
332 parport_write_data(port, 0x78); udelay(2);
333 parport_write_data(port, cmd); udelay(2);
334 parport_frob_control(port,
335 PARPORT_CONTROL_STROBE,
336 PARPORT_CONTROL_STROBE);
338 s = parport_read_status(port);
339 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
341 parport_write_data(port, 0xff); udelay(2);
346 /* Send a mux-style CPP command packet. */
347 static int cpp_mux(struct parport *port, int cmd)
352 parport_data_forward(port);
353 parport_write_data(port, 0xaa); udelay(2);
354 parport_write_data(port, 0x55); udelay(2);
355 parport_write_data(port, 0xf0); udelay(2);
356 parport_write_data(port, 0x0f); udelay(2);
357 parport_write_data(port, 0x52); udelay(2);
358 parport_write_data(port, 0xad); udelay(2);
359 parport_write_data(port, cmd); udelay(2);
361 s = parport_read_status(port);
362 if (!(s & PARPORT_STATUS_ACK)) {
363 pr_debug("%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
368 rc = (((s & PARPORT_STATUS_SELECT ? 1 : 0) << 0) |
369 ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
370 ((s & PARPORT_STATUS_BUSY ? 0 : 1) << 2) |
371 ((s & PARPORT_STATUS_ERROR ? 0 : 1) << 3));
376 void parport_daisy_deselect_all(struct parport *port)
378 cpp_daisy(port, 0x30);
381 int parport_daisy_select(struct parport *port, int daisy, int mode)
385 // For these modes we should switch to EPP mode:
386 case IEEE1284_MODE_EPP:
387 case IEEE1284_MODE_EPPSL:
388 case IEEE1284_MODE_EPPSWE:
389 return !(cpp_daisy(port, 0x20 + daisy) &
390 PARPORT_STATUS_ERROR);
392 // For these modes we should switch to ECP mode:
393 case IEEE1284_MODE_ECP:
394 case IEEE1284_MODE_ECPRLE:
395 case IEEE1284_MODE_ECPSWE:
396 return !(cpp_daisy(port, 0xd0 + daisy) &
397 PARPORT_STATUS_ERROR);
399 // Nothing was told for BECP in Daisy chain specification.
400 // May be it's wise to use ECP?
401 case IEEE1284_MODE_BECP:
402 // Others use compat mode
403 case IEEE1284_MODE_NIBBLE:
404 case IEEE1284_MODE_BYTE:
405 case IEEE1284_MODE_COMPAT:
407 return !(cpp_daisy(port, 0xe0 + daisy) &
408 PARPORT_STATUS_ERROR);
412 static int mux_present(struct parport *port)
414 return cpp_mux(port, 0x51) == 3;
417 static int num_mux_ports(struct parport *port)
419 return cpp_mux(port, 0x58);
422 static int select_port(struct parport *port)
424 int muxport = port->muxport;
425 return cpp_mux(port, 0x60 + muxport) == muxport;
428 static int assign_addrs(struct parport *port)
432 int thisdev = numdevs;
436 parport_data_forward(port);
437 parport_write_data(port, 0xaa); udelay(2);
438 parport_write_data(port, 0x55); udelay(2);
439 parport_write_data(port, 0x00); udelay(2);
440 parport_write_data(port, 0xff); udelay(2);
441 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
442 | PARPORT_STATUS_PAPEROUT
443 | PARPORT_STATUS_SELECT
444 | PARPORT_STATUS_ERROR);
445 if (s != (PARPORT_STATUS_BUSY
446 | PARPORT_STATUS_PAPEROUT
447 | PARPORT_STATUS_SELECT
448 | PARPORT_STATUS_ERROR)) {
449 pr_debug("%s: assign_addrs: aa5500ff(%02x)\n", port->name, s);
453 parport_write_data(port, 0x87); udelay(2);
454 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
455 | PARPORT_STATUS_PAPEROUT
456 | PARPORT_STATUS_SELECT
457 | PARPORT_STATUS_ERROR);
458 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
459 pr_debug("%s: assign_addrs: aa5500ff87(%02x)\n", port->name, s);
463 parport_write_data(port, 0x78); udelay(2);
464 s = parport_read_status(port);
467 (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
468 == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
471 parport_write_data(port, daisy);
473 parport_frob_control(port,
474 PARPORT_CONTROL_STROBE,
475 PARPORT_CONTROL_STROBE);
477 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
480 add_dev(numdevs++, port, daisy);
482 /* See if this device thought it was the last in the
484 if (!(s & PARPORT_STATUS_BUSY))
487 /* We are seeing pass through status now. We see
488 last_dev from next device or if last_dev does not
489 work status lines from some non-daisy chain
491 s = parport_read_status(port);
494 parport_write_data(port, 0xff); udelay(2);
495 detected = numdevs - thisdev;
496 pr_debug("%s: Found %d daisy-chained devices\n", port->name, detected);
498 /* Ask the new devices to introduce themselves. */
499 deviceid = kmalloc(1024, GFP_KERNEL);
500 if (!deviceid) return 0;
502 for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
503 parport_device_id(thisdev, deviceid, 1024);