2 * IEEE 1284.3 Parallel port daisy chain and multiplexor code
4 * Copyright (C) 1999 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.
18 #include <linux/config.h>
19 #include <linux/parport.h>
20 #include <linux/delay.h>
21 #include <asm/uaccess.h>
23 #define DEBUG /* undef me for production */
26 #define DPRINTK(stuff...) printk (stuff)
28 #define DPRINTK(stuff...)
31 static struct daisydev
{
32 struct daisydev
*next
;
38 static int numdevs
= 0;
40 /* Forward-declaration of lower-level functions. */
41 static int mux_present (struct parport
*port
);
42 static int num_mux_ports (struct parport
*port
);
43 static int select_port (struct parport
*port
);
44 static int assign_addrs (struct parport
*port
);
46 /* Add a device to the discovered topology. */
47 static void add_dev (int devnum
, struct parport
*port
, int daisy
)
49 struct daisydev
*newdev
;
50 newdev
= kmalloc (GFP_KERNEL
, sizeof (struct daisydev
));
53 newdev
->daisy
= daisy
;
54 newdev
->devnum
= devnum
;
55 newdev
->next
= topology
;
56 if (!topology
|| topology
->devnum
>= devnum
)
59 struct daisydev
*prev
= topology
;
60 while (prev
->next
&& prev
->next
->devnum
< devnum
)
62 newdev
->next
= prev
->next
;
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
;
84 /* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains. */
85 int parport_daisy_init (struct parport
*port
)
88 static const char *th
[] = { /*0*/"th", "st", "nd", "rd", "th" };
92 /* Because this is called before any other devices exist,
93 * we don't have to claim exclusive access. */
95 /* If mux present on normal port, need to create new
96 * parports for each extra port. */
97 if (port
->muxport
< 0 && mux_present (port
) &&
98 /* don't be fooled: a mux must have 2 or 4 ports. */
99 ((num_ports
= num_mux_ports (port
)) == 2 || num_ports
== 4)) {
100 /* Leave original as port zero. */
103 "%s: 1st (default) port of %d-way multiplexor\n",
104 port
->name
, num_ports
);
105 for (i
= 1; i
< num_ports
; i
++) {
106 /* Clone the port. */
107 struct parport
*extra
= clone_parport (port
, i
);
109 if (signal_pending (current
))
117 "%s: %d%s port of %d-way multiplexor on %s\n",
118 extra
->name
, i
+ 1, th
[i
+ 1], num_ports
,
121 /* Analyse that port too. We won't recurse
122 forever because of the 'port->muxport < 0'
124 parport_announce_port (extra
);
128 if (port
->muxport
>= 0)
131 parport_daisy_deselect_all (port
);
134 /* Count the potential legacy device at the end. */
135 add_dev (numdevs
++, port
, -1);
137 /* Find out the legacy device's IEEE 1284 device ID. */
138 deviceid
= kmalloc (1000, GFP_KERNEL
);
140 parport_device_id (numdevs
- 1, deviceid
, 1000);
147 /* Forget about devices on a physical port. */
148 void parport_daisy_fini (struct parport
*port
)
150 struct daisydev
*dev
, *prev
= topology
;
151 while (prev
&& prev
->port
== port
)
152 prev
= topology
= topology
->next
;
156 if (dev
&& dev
->port
== port
)
157 prev
->next
= dev
->next
;
162 /* Gaps in the numbering could be handled better. How should
163 someone enumerate through all IEEE1284.3 devices in the
165 if (!topology
) numdevs
= 0;
168 /* Find a device by canonical device number. */
169 struct pardevice
*parport_open (int devnum
, const char *name
,
170 int (*pf
) (void *), void (*kf
) (void *),
171 void (*irqf
) (int, void *, struct pt_regs
*),
172 int flags
, void *handle
)
174 struct parport
*port
= parport_enumerate ();
175 struct pardevice
*dev
;
180 if (parport_device_coords (devnum
, &portnum
, &muxnum
, &daisynum
))
183 while (port
&& ((port
->portnum
!= portnum
) ||
184 (port
->muxport
!= muxnum
)))
188 /* No corresponding parport. */
191 dev
= parport_register_device (port
, name
, pf
, kf
,
192 irqf
, flags
, handle
);
194 dev
->daisy
= daisynum
;
196 /* Check that there really is a device to select. */
199 parport_claim_or_block (dev
);
200 selected
= port
->daisy
;
201 parport_release (dev
);
203 if (selected
!= port
->daisy
) {
204 /* No corresponding device. */
205 parport_unregister_device (dev
);
213 /* The converse of parport_open. */
214 void parport_close (struct pardevice
*dev
)
216 parport_unregister_device (dev
);
219 /* Convert device coordinates into a canonical device number. */
220 int parport_device_num (int parport
, int mux
, int daisy
)
222 struct daisydev
*dev
= topology
;
224 while (dev
&& dev
->port
->portnum
!= parport
&&
225 dev
->port
->muxport
!= mux
&& dev
->daisy
!= daisy
)
234 /* Convert a canonical device number into device coordinates. */
235 int parport_device_coords (int devnum
, int *parport
, int *mux
, int *daisy
)
237 struct daisydev
*dev
= topology
;
239 while (dev
&& dev
->devnum
!= devnum
)
245 if (parport
) *parport
= dev
->port
->portnum
;
246 if (mux
) *mux
= dev
->port
->muxport
;
247 if (daisy
) *daisy
= dev
->daisy
;
251 /* Send a daisy-chain-style CPP command packet. */
252 static int cpp_daisy (struct parport
*port
, int cmd
)
256 parport_write_data (port
, 0xaa); udelay (2);
257 parport_write_data (port
, 0x55); udelay (2);
258 parport_write_data (port
, 0x00); udelay (2);
259 parport_write_data (port
, 0xff); udelay (2);
260 s
= parport_read_status (port
) & (PARPORT_STATUS_BUSY
261 | PARPORT_STATUS_PAPEROUT
262 | PARPORT_STATUS_SELECT
263 | PARPORT_STATUS_ERROR
);
264 if (s
!= (PARPORT_STATUS_BUSY
265 | PARPORT_STATUS_PAPEROUT
266 | PARPORT_STATUS_SELECT
267 | PARPORT_STATUS_ERROR
)) {
268 DPRINTK (KERN_DEBUG
"%s: cpp_daisy: aa5500ff(%02x)\n",
273 parport_write_data (port
, 0x87); udelay (2);
274 s
= parport_read_status (port
) & (PARPORT_STATUS_BUSY
275 | PARPORT_STATUS_PAPEROUT
276 | PARPORT_STATUS_SELECT
277 | PARPORT_STATUS_ERROR
);
278 if (s
!= (PARPORT_STATUS_SELECT
| PARPORT_STATUS_ERROR
)) {
279 DPRINTK (KERN_DEBUG
"%s: cpp_daisy: aa5500ff87(%02x)\n",
284 parport_write_data (port
, 0x78); udelay (2);
285 parport_write_data (port
, cmd
); udelay (2);
286 parport_frob_control (port
,
287 PARPORT_CONTROL_STROBE
,
288 PARPORT_CONTROL_STROBE
);
290 parport_frob_control (port
, PARPORT_CONTROL_STROBE
, 0);
292 s
= parport_read_status (port
);
293 parport_write_data (port
, 0xff); udelay (2);
298 /* Send a mux-style CPP command packet. */
299 static int cpp_mux (struct parport
*port
, int cmd
)
304 parport_write_data (port
, 0xaa); udelay (2);
305 parport_write_data (port
, 0x55); udelay (2);
306 parport_write_data (port
, 0xf0); udelay (2);
307 parport_write_data (port
, 0x0f); udelay (2);
308 parport_write_data (port
, 0x52); udelay (2);
309 parport_write_data (port
, 0xad); udelay (2);
310 parport_write_data (port
, cmd
); udelay (2);
312 s
= parport_read_status (port
);
313 if (!(s
& PARPORT_STATUS_ACK
)) {
314 DPRINTK (KERN_DEBUG
"%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
319 rc
= (((s
& PARPORT_STATUS_SELECT
? 1 : 0) << 0) |
320 ((s
& PARPORT_STATUS_PAPEROUT
? 1 : 0) << 1) |
321 ((s
& PARPORT_STATUS_BUSY
? 0 : 1) << 2) |
322 ((s
& PARPORT_STATUS_ERROR
? 0 : 1) << 3));
327 void parport_daisy_deselect_all (struct parport
*port
)
329 cpp_daisy (port
, 0x30);
332 int parport_daisy_select (struct parport
*port
, int daisy
, int mode
)
334 /* mode is currently ignored. FIXME? */
335 return cpp_daisy (port
, 0xe0 + daisy
) & PARPORT_STATUS_ERROR
;
338 static int mux_present (struct parport
*port
)
340 return cpp_mux (port
, 0x51) == 3;
343 static int num_mux_ports (struct parport
*port
)
345 return cpp_mux (port
, 0x58);
348 static int select_port (struct parport
*port
)
350 int muxport
= port
->muxport
;
351 return cpp_mux (port
, 0x60 + muxport
) == muxport
;
354 static int assign_addrs (struct parport
*port
)
356 unsigned char s
, last_dev
;
358 int thisdev
= numdevs
;
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: assign_addrs: aa5500ff(%02x)\n",
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: assign_addrs: aa5500ff87(%02x)\n",
389 parport_write_data (port
, 0x78); udelay (2);
390 last_dev
= 0; /* We've just been speaking to a device, so we
391 know there must be at least _one_ out there. */
393 for (daisy
= 0; daisy
< 4; daisy
++) {
394 parport_write_data (port
, daisy
);
396 parport_frob_control (port
,
397 PARPORT_CONTROL_STROBE
,
398 PARPORT_CONTROL_STROBE
);
400 parport_frob_control (port
, PARPORT_CONTROL_STROBE
, 0);
404 /* No more devices. */
407 last_dev
= !(parport_read_status (port
)
408 & PARPORT_STATUS_BUSY
);
410 add_dev (numdevs
++, port
, daisy
);
413 parport_write_data (port
, 0xff); udelay (2);
414 DPRINTK (KERN_DEBUG
"%s: Found %d daisy-chained devices\n", port
->name
,
417 /* Ask the new devices to introduce themselves. */
418 deviceid
= kmalloc (1000, GFP_KERNEL
);
419 if (!deviceid
) return 0;
421 for (daisy
= 0; thisdev
< numdevs
; thisdev
++, daisy
++)
422 parport_device_id (thisdev
, deviceid
, 1000);
428 /* Find a device with a particular manufacturer and model string,
429 starting from a given device number. Like the PCI equivalent,
430 'from' itself is skipped. */
431 int parport_find_device (const char *mfg
, const char *mdl
, int from
)
433 struct daisydev
*d
= topology
; /* sorted by devnum */
435 /* Find where to start. */
436 while (d
&& d
->devnum
<= from
)
441 struct parport_device_info
*info
;
442 info
= &d
->port
->probe_info
[1 + d
->daisy
];
443 if ((!mfg
|| !strcmp (mfg
, info
->mfr
)) &&
444 (!mdl
|| !strcmp (mdl
, info
->model
)))
456 /* Find a device in a particular class. Like the PCI equivalent,
457 'from' itself is skipped. */
458 int parport_find_class (parport_device_class cls
, int from
)
460 struct daisydev
*d
= topology
; /* sorted by devnum */
462 /* Find where to start. */
463 while (d
&& d
->devnum
<= from
)
467 while (d
&& d
->port
->probe_info
[1 + d
->daisy
].class != cls
)