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/parport.h>
19 #include <linux/delay.h>
20 #include <asm/uaccess.h>
22 #define DEBUG /* undef me for production */
25 #define DPRINTK(stuff...) printk (stuff)
27 #define DPRINTK(stuff...)
30 static struct daisydev
{
31 struct daisydev
*next
;
37 static int numdevs
= 0;
39 /* Forward-declaration of lower-level functions. */
40 static int mux_present (struct parport
*port
);
41 static int num_mux_ports (struct parport
*port
);
42 static int select_port (struct parport
*port
);
43 static int assign_addrs (struct parport
*port
);
45 /* Add a device to the discovered topology. */
46 static void add_dev (int devnum
, struct parport
*port
, int daisy
)
48 struct daisydev
*newdev
;
49 newdev
= kmalloc (GFP_KERNEL
, sizeof (struct daisydev
));
52 newdev
->daisy
= daisy
;
53 newdev
->devnum
= devnum
;
54 newdev
->next
= topology
;
55 if (!topology
|| topology
->devnum
>= devnum
)
58 struct daisydev
*prev
= topology
;
59 while (prev
->next
&& prev
->next
->devnum
< devnum
)
61 newdev
->next
= prev
->next
;
67 /* Clone a parport (actually, make an alias). */
68 static struct parport
*clone_parport (struct parport
*real
, int muxport
)
70 struct parport
*extra
= parport_register_port (real
->base
,
75 extra
->portnum
= real
->portnum
;
76 extra
->physport
= real
;
77 extra
->muxport
= muxport
;
83 /* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains. */
84 int parport_daisy_init (struct parport
*port
)
87 static const char *th
[] = { /*0*/"th", "st", "nd", "rd", "th" };
91 /* Because this is called before any other devices exist,
92 * we don't have to claim exclusive access. */
94 /* If mux present on normal port, need to create new
95 * parports for each extra port. */
96 if (port
->muxport
< 0 && mux_present (port
) &&
97 /* don't be fooled: a mux must have 2 or 4 ports. */
98 ((num_ports
= num_mux_ports (port
)) == 2 || num_ports
== 4)) {
99 /* Leave original as port zero. */
102 "%s: 1st (default) port of %d-way multiplexor\n",
103 port
->name
, num_ports
);
104 for (i
= 1; i
< num_ports
; i
++) {
105 /* Clone the port. */
106 struct parport
*extra
= clone_parport (port
, i
);
108 if (signal_pending (current
))
116 "%s: %d%s port of %d-way multiplexor on %s\n",
117 extra
->name
, i
+ 1, th
[i
+ 1], num_ports
,
120 /* Analyse that port too. We won't recurse
121 forever because of the 'port->muxport < 0'
123 parport_announce_port (extra
);
127 if (port
->muxport
>= 0)
130 parport_daisy_deselect_all (port
);
133 /* Count the potential legacy device at the end. */
134 add_dev (numdevs
++, port
, -1);
136 /* Find out the legacy device's IEEE 1284 device ID. */
137 deviceid
= kmalloc (1000, GFP_KERNEL
);
139 parport_device_id (numdevs
- 1, deviceid
, 1000);
146 /* Forget about devices on a physical port. */
147 void parport_daisy_fini (struct parport
*port
)
149 struct daisydev
*dev
, *prev
= topology
;
150 while (prev
&& prev
->port
== port
)
151 prev
= topology
= topology
->next
;
155 if (dev
&& dev
->port
== port
)
156 prev
->next
= dev
->next
;
161 /* Gaps in the numbering could be handled better. How should
162 someone enumerate through all IEEE1284.3 devices in the
164 if (!topology
) numdevs
= 0;
167 /* Find a device by canonical device number. */
168 struct pardevice
*parport_open (int devnum
, const char *name
,
169 int (*pf
) (void *), void (*kf
) (void *),
170 void (*irqf
) (int, void *, struct pt_regs
*),
171 int flags
, void *handle
)
173 struct parport
*port
= parport_enumerate ();
174 struct pardevice
*dev
;
179 if (parport_device_coords (devnum
, &portnum
, &muxnum
, &daisynum
))
182 while (port
&& ((port
->portnum
!= portnum
) ||
183 (port
->muxport
!= muxnum
)))
187 /* No corresponding parport. */
190 dev
= parport_register_device (port
, name
, pf
, kf
,
191 irqf
, flags
, handle
);
193 dev
->daisy
= daisynum
;
195 /* Check that there really is a device to select. */
198 parport_claim_or_block (dev
);
199 selected
= port
->daisy
;
200 parport_release (dev
);
202 if (selected
!= port
->daisy
) {
203 /* No corresponding device. */
204 parport_unregister_device (dev
);
212 /* The converse of parport_open. */
213 void parport_close (struct pardevice
*dev
)
215 parport_unregister_device (dev
);
218 /* Convert device coordinates into a canonical device number. */
219 int parport_device_num (int parport
, int mux
, int daisy
)
221 struct daisydev
*dev
= topology
;
223 while (dev
&& dev
->port
->portnum
!= parport
&&
224 dev
->port
->muxport
!= mux
&& dev
->daisy
!= daisy
)
233 /* Convert a canonical device number into device coordinates. */
234 int parport_device_coords (int devnum
, int *parport
, int *mux
, int *daisy
)
236 struct daisydev
*dev
= topology
;
238 while (dev
&& dev
->devnum
!= devnum
)
244 if (parport
) *parport
= dev
->port
->portnum
;
245 if (mux
) *mux
= dev
->port
->muxport
;
246 if (daisy
) *daisy
= dev
->daisy
;
250 /* Send a daisy-chain-style CPP command packet. */
251 static int cpp_daisy (struct parport
*port
, int cmd
)
255 parport_write_data (port
, 0xaa); udelay (2);
256 parport_write_data (port
, 0x55); udelay (2);
257 parport_write_data (port
, 0x00); udelay (2);
258 parport_write_data (port
, 0xff); udelay (2);
259 s
= parport_read_status (port
) & (PARPORT_STATUS_BUSY
260 | PARPORT_STATUS_PAPEROUT
261 | PARPORT_STATUS_SELECT
262 | PARPORT_STATUS_ERROR
);
263 if (s
!= (PARPORT_STATUS_BUSY
264 | PARPORT_STATUS_PAPEROUT
265 | PARPORT_STATUS_SELECT
266 | PARPORT_STATUS_ERROR
)) {
267 DPRINTK (KERN_DEBUG
"%s: cpp_daisy: aa5500ff(%02x)\n",
272 parport_write_data (port
, 0x87); udelay (2);
273 s
= parport_read_status (port
) & (PARPORT_STATUS_BUSY
274 | PARPORT_STATUS_PAPEROUT
275 | PARPORT_STATUS_SELECT
276 | PARPORT_STATUS_ERROR
);
277 if (s
!= (PARPORT_STATUS_SELECT
| PARPORT_STATUS_ERROR
)) {
278 DPRINTK (KERN_DEBUG
"%s: cpp_daisy: aa5500ff87(%02x)\n",
283 parport_write_data (port
, 0x78); udelay (2);
284 parport_write_data (port
, cmd
); udelay (2);
285 parport_frob_control (port
,
286 PARPORT_CONTROL_STROBE
,
287 PARPORT_CONTROL_STROBE
);
289 parport_frob_control (port
, PARPORT_CONTROL_STROBE
, 0);
291 s
= parport_read_status (port
);
292 parport_write_data (port
, 0xff); udelay (2);
297 /* Send a mux-style CPP command packet. */
298 static int cpp_mux (struct parport
*port
, int cmd
)
303 parport_write_data (port
, 0xaa); udelay (2);
304 parport_write_data (port
, 0x55); udelay (2);
305 parport_write_data (port
, 0xf0); udelay (2);
306 parport_write_data (port
, 0x0f); udelay (2);
307 parport_write_data (port
, 0x52); udelay (2);
308 parport_write_data (port
, 0xad); udelay (2);
309 parport_write_data (port
, cmd
); udelay (2);
311 s
= parport_read_status (port
);
312 if (!(s
& PARPORT_STATUS_ACK
)) {
313 DPRINTK (KERN_DEBUG
"%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
318 rc
= (((s
& PARPORT_STATUS_SELECT
? 1 : 0) << 0) |
319 ((s
& PARPORT_STATUS_PAPEROUT
? 1 : 0) << 1) |
320 ((s
& PARPORT_STATUS_BUSY
? 0 : 1) << 2) |
321 ((s
& PARPORT_STATUS_ERROR
? 0 : 1) << 3));
326 void parport_daisy_deselect_all (struct parport
*port
)
328 cpp_daisy (port
, 0x30);
331 int parport_daisy_select (struct parport
*port
, int daisy
, int mode
)
333 /* mode is currently ignored. FIXME? */
334 return cpp_daisy (port
, 0xe0 + daisy
) & PARPORT_STATUS_ERROR
;
337 static int mux_present (struct parport
*port
)
339 return cpp_mux (port
, 0x51) == 3;
342 static int num_mux_ports (struct parport
*port
)
344 return cpp_mux (port
, 0x58);
347 static int select_port (struct parport
*port
)
349 int muxport
= port
->muxport
;
350 return cpp_mux (port
, 0x60 + muxport
) == muxport
;
353 static int assign_addrs (struct parport
*port
)
355 unsigned char s
, last_dev
;
357 int thisdev
= numdevs
;
360 parport_write_data (port
, 0xaa); udelay (2);
361 parport_write_data (port
, 0x55); udelay (2);
362 parport_write_data (port
, 0x00); udelay (2);
363 parport_write_data (port
, 0xff); udelay (2);
364 s
= parport_read_status (port
) & (PARPORT_STATUS_BUSY
365 | PARPORT_STATUS_PAPEROUT
366 | PARPORT_STATUS_SELECT
367 | PARPORT_STATUS_ERROR
);
368 if (s
!= (PARPORT_STATUS_BUSY
369 | PARPORT_STATUS_PAPEROUT
370 | PARPORT_STATUS_SELECT
371 | PARPORT_STATUS_ERROR
)) {
372 DPRINTK (KERN_DEBUG
"%s: assign_addrs: aa5500ff(%02x)\n",
377 parport_write_data (port
, 0x87); udelay (2);
378 s
= parport_read_status (port
) & (PARPORT_STATUS_BUSY
379 | PARPORT_STATUS_PAPEROUT
380 | PARPORT_STATUS_SELECT
381 | PARPORT_STATUS_ERROR
);
382 if (s
!= (PARPORT_STATUS_SELECT
| PARPORT_STATUS_ERROR
)) {
383 DPRINTK (KERN_DEBUG
"%s: assign_addrs: aa5500ff87(%02x)\n",
388 parport_write_data (port
, 0x78); udelay (2);
389 last_dev
= 0; /* We've just been speaking to a device, so we
390 know there must be at least _one_ out there. */
392 for (daisy
= 0; daisy
< 4; daisy
++) {
393 parport_write_data (port
, daisy
);
395 parport_frob_control (port
,
396 PARPORT_CONTROL_STROBE
,
397 PARPORT_CONTROL_STROBE
);
399 parport_frob_control (port
, PARPORT_CONTROL_STROBE
, 0);
403 /* No more devices. */
406 last_dev
= !(parport_read_status (port
)
407 & PARPORT_STATUS_BUSY
);
409 add_dev (numdevs
++, port
, daisy
);
412 parport_write_data (port
, 0xff); udelay (2);
413 DPRINTK (KERN_DEBUG
"%s: Found %d daisy-chained devices\n", port
->name
,
416 /* Ask the new devices to introduce themselves. */
417 deviceid
= kmalloc (1000, GFP_KERNEL
);
418 if (!deviceid
) return 0;
420 for (daisy
= 0; thisdev
< numdevs
; thisdev
++, daisy
++)
421 parport_device_id (thisdev
, deviceid
, 1000);
427 /* Find a device with a particular manufacturer and model string,
428 starting from a given device number. Like the PCI equivalent,
429 'from' itself is skipped. */
430 int parport_find_device (const char *mfg
, const char *mdl
, int from
)
432 struct daisydev
*d
= topology
; /* sorted by devnum */
434 /* Find where to start. */
435 while (d
&& d
->devnum
<= from
)
440 struct parport_device_info
*info
;
441 info
= &d
->port
->probe_info
[1 + d
->daisy
];
442 if ((!mfg
|| !strcmp (mfg
, info
->mfr
)) &&
443 (!mdl
|| !strcmp (mdl
, info
->model
)))
455 /* Find a device in a particular class. Like the PCI equivalent,
456 'from' itself is skipped. */
457 int parport_find_class (parport_device_class cls
, int from
)
459 struct daisydev
*d
= topology
; /* sorted by devnum */
461 /* Find where to start. */
462 while (d
&& d
->devnum
<= from
)
466 while (d
&& d
->port
->probe_info
[1 + d
->daisy
].class != cls
)