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/sched.h>
27 #include <asm/current.h>
28 #include <asm/uaccess.h>
33 #define DPRINTK(stuff...) printk(stuff)
35 #define DPRINTK(stuff...)
38 static struct daisydev {
39 struct daisydev *next;
44 static DEFINE_SPINLOCK(topology_lock);
46 static int numdevs = 0;
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);
54 /* Add a device to the discovered topology. */
55 static void add_dev(int devnum, struct parport *port, int daisy)
57 struct daisydev *newdev, **p;
58 newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
61 newdev->daisy = daisy;
62 newdev->devnum = devnum;
63 spin_lock(&topology_lock);
64 for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
68 spin_unlock(&topology_lock);
72 /* Clone a parport (actually, make an alias). */
73 static struct parport *clone_parport(struct parport *real, int muxport)
75 struct parport *extra = parport_register_port(real->base,
80 extra->portnum = real->portnum;
81 extra->physport = real;
82 extra->muxport = muxport;
83 real->slaves[muxport-1] = extra;
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)
95 static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
101 /* Because this is called before any other devices exist,
102 * we don't have to claim exclusive access. */
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. */
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);
118 if (signal_pending(current))
126 "%s: %d%s port of %d-way multiplexor on %s\n",
127 extra->name, i + 1, th[i + 1], num_ports,
130 /* Analyse that port too. We won't recurse
131 forever because of the 'port->muxport < 0'
133 parport_daisy_init(extra);
137 if (port->muxport >= 0)
140 parport_daisy_deselect_all(port);
141 detected += assign_addrs(port);
143 /* Count the potential legacy device at the end. */
144 add_dev(numdevs++, port, -1);
146 /* Find out the legacy device's IEEE 1284 device ID. */
147 deviceid = kmalloc(1024, GFP_KERNEL);
149 if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
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
159 parport_daisy_fini(port);
160 parport_write_control(port, PARPORT_CONTROL_SELECT);
162 parport_write_control(port,
163 PARPORT_CONTROL_SELECT |
164 PARPORT_CONTROL_INIT);
173 /* Forget about devices on a physical port. */
174 void parport_daisy_fini(struct parport *port)
178 spin_lock(&topology_lock);
181 struct daisydev *dev = *p;
182 if (dev->port != port) {
190 /* Gaps in the numbering could be handled better. How should
191 someone enumerate through all IEEE1284.3 devices in the
193 if (!topology) numdevs = 0;
194 spin_unlock(&topology_lock);
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
204 * @irqf: interrupt handler
205 * @flags: registration flags
206 * @handle: driver data
208 * This function is similar to parport_register_device(), except
209 * that it locates a device by its number rather than by the port
212 * All parameters except for @devnum are the same as for
213 * parport_register_device(). The return value is the same as
214 * for parport_register_device().
217 struct pardevice *parport_open(int devnum, const char *name,
218 int (*pf) (void *), void (*kf) (void *),
219 void (*irqf) (int, void *),
220 int flags, void *handle)
222 struct daisydev *p = topology;
223 struct parport *port;
224 struct pardevice *dev;
227 spin_lock(&topology_lock);
228 while (p && p->devnum != devnum)
232 spin_unlock(&topology_lock);
237 port = parport_get_port(p->port);
238 spin_unlock(&topology_lock);
240 dev = parport_register_device(port, name, pf, kf,
241 irqf, flags, handle);
242 parport_put_port(port);
248 /* Check that there really is a device to select. */
251 parport_claim_or_block(dev);
252 selected = port->daisy;
253 parport_release(dev);
255 if (selected != daisy) {
256 /* No corresponding device. */
257 parport_unregister_device(dev);
266 * parport_close - close a device opened with parport_open()
267 * @dev: device to close
269 * This is to parport_open() as parport_unregister_device() is to
270 * parport_register_device().
273 void parport_close(struct pardevice *dev)
275 parport_unregister_device(dev);
278 /* Send a daisy-chain-style CPP command packet. */
279 static int cpp_daisy(struct parport *port, int cmd)
283 parport_data_forward(port);
284 parport_write_data(port, 0xaa); udelay(2);
285 parport_write_data(port, 0x55); udelay(2);
286 parport_write_data(port, 0x00); udelay(2);
287 parport_write_data(port, 0xff); udelay(2);
288 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
289 | PARPORT_STATUS_PAPEROUT
290 | PARPORT_STATUS_SELECT
291 | PARPORT_STATUS_ERROR);
292 if (s != (PARPORT_STATUS_BUSY
293 | PARPORT_STATUS_PAPEROUT
294 | PARPORT_STATUS_SELECT
295 | PARPORT_STATUS_ERROR)) {
296 DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff(%02x)\n",
301 parport_write_data(port, 0x87); udelay(2);
302 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
303 | PARPORT_STATUS_PAPEROUT
304 | PARPORT_STATUS_SELECT
305 | PARPORT_STATUS_ERROR);
306 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
307 DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff87(%02x)\n",
312 parport_write_data(port, 0x78); udelay(2);
313 parport_write_data(port, cmd); udelay(2);
314 parport_frob_control(port,
315 PARPORT_CONTROL_STROBE,
316 PARPORT_CONTROL_STROBE);
318 s = parport_read_status(port);
319 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
321 parport_write_data(port, 0xff); udelay(2);
326 /* Send a mux-style CPP command packet. */
327 static int cpp_mux(struct parport *port, int cmd)
332 parport_data_forward(port);
333 parport_write_data(port, 0xaa); udelay(2);
334 parport_write_data(port, 0x55); udelay(2);
335 parport_write_data(port, 0xf0); udelay(2);
336 parport_write_data(port, 0x0f); udelay(2);
337 parport_write_data(port, 0x52); udelay(2);
338 parport_write_data(port, 0xad); udelay(2);
339 parport_write_data(port, cmd); udelay(2);
341 s = parport_read_status(port);
342 if (!(s & PARPORT_STATUS_ACK)) {
343 DPRINTK(KERN_DEBUG "%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
348 rc = (((s & PARPORT_STATUS_SELECT ? 1 : 0) << 0) |
349 ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
350 ((s & PARPORT_STATUS_BUSY ? 0 : 1) << 2) |
351 ((s & PARPORT_STATUS_ERROR ? 0 : 1) << 3));
356 void parport_daisy_deselect_all(struct parport *port)
358 cpp_daisy(port, 0x30);
361 int parport_daisy_select(struct parport *port, int daisy, int mode)
365 // For these modes we should switch to EPP mode:
366 case IEEE1284_MODE_EPP:
367 case IEEE1284_MODE_EPPSL:
368 case IEEE1284_MODE_EPPSWE:
369 return !(cpp_daisy(port, 0x20 + daisy) &
370 PARPORT_STATUS_ERROR);
372 // For these modes we should switch to ECP mode:
373 case IEEE1284_MODE_ECP:
374 case IEEE1284_MODE_ECPRLE:
375 case IEEE1284_MODE_ECPSWE:
376 return !(cpp_daisy(port, 0xd0 + daisy) &
377 PARPORT_STATUS_ERROR);
379 // Nothing was told for BECP in Daisy chain specification.
380 // May be it's wise to use ECP?
381 case IEEE1284_MODE_BECP:
382 // Others use compat mode
383 case IEEE1284_MODE_NIBBLE:
384 case IEEE1284_MODE_BYTE:
385 case IEEE1284_MODE_COMPAT:
387 return !(cpp_daisy(port, 0xe0 + daisy) &
388 PARPORT_STATUS_ERROR);
392 static int mux_present(struct parport *port)
394 return cpp_mux(port, 0x51) == 3;
397 static int num_mux_ports(struct parport *port)
399 return cpp_mux(port, 0x58);
402 static int select_port(struct parport *port)
404 int muxport = port->muxport;
405 return cpp_mux(port, 0x60 + muxport) == muxport;
408 static int assign_addrs(struct parport *port)
412 int thisdev = numdevs;
416 parport_data_forward(port);
417 parport_write_data(port, 0xaa); udelay(2);
418 parport_write_data(port, 0x55); udelay(2);
419 parport_write_data(port, 0x00); udelay(2);
420 parport_write_data(port, 0xff); udelay(2);
421 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
422 | PARPORT_STATUS_PAPEROUT
423 | PARPORT_STATUS_SELECT
424 | PARPORT_STATUS_ERROR);
425 if (s != (PARPORT_STATUS_BUSY
426 | PARPORT_STATUS_PAPEROUT
427 | PARPORT_STATUS_SELECT
428 | PARPORT_STATUS_ERROR)) {
429 DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff(%02x)\n",
434 parport_write_data(port, 0x87); udelay(2);
435 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
436 | PARPORT_STATUS_PAPEROUT
437 | PARPORT_STATUS_SELECT
438 | PARPORT_STATUS_ERROR);
439 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
440 DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff87(%02x)\n",
445 parport_write_data(port, 0x78); udelay(2);
446 s = parport_read_status(port);
449 (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
450 == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
453 parport_write_data(port, daisy);
455 parport_frob_control(port,
456 PARPORT_CONTROL_STROBE,
457 PARPORT_CONTROL_STROBE);
459 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
462 add_dev(numdevs++, port, daisy);
464 /* See if this device thought it was the last in the
466 if (!(s & PARPORT_STATUS_BUSY))
469 /* We are seeing pass through status now. We see
470 last_dev from next device or if last_dev does not
471 work status lines from some non-daisy chain
473 s = parport_read_status(port);
476 parport_write_data(port, 0xff); udelay(2);
477 detected = numdevs - thisdev;
478 DPRINTK(KERN_DEBUG "%s: Found %d daisy-chained devices\n", port->name,
481 /* Ask the new devices to introduce themselves. */
482 deviceid = kmalloc(1024, GFP_KERNEL);
483 if (!deviceid) return 0;
485 for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
486 parport_device_id(thisdev, deviceid, 1024);