Import 2.3.10pre1
[davej-history.git] / drivers / misc / parport_daisy.c
blob78b63cea26cbcb31442fd35ca0bda3f992a387bd
1 /*
2 * IEEE 1284.3 Parallel port daisy chain and multiplexor code
3 *
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 */
25 #ifdef DEBUG
26 #define DPRINTK(stuff...) printk (stuff)
27 #else
28 #define DPRINTK(stuff...)
29 #endif
31 static struct daisydev {
32 struct daisydev *next;
33 struct parport *port;
34 int daisy;
35 int devnum;
36 } *topology = NULL;
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));
51 if (newdev) {
52 newdev->port = port;
53 newdev->daisy = daisy;
54 newdev->devnum = devnum;
55 newdev->next = topology;
56 if (!topology || topology->devnum >= devnum)
57 topology = newdev;
58 else {
59 struct daisydev *prev = topology;
60 while (prev->next && prev->next->devnum < devnum)
61 prev = prev->next;
62 newdev->next = prev->next;
63 prev->next = newdev;
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,
72 real->irq,
73 real->dma,
74 real->ops);
75 if (extra) {
76 extra->portnum = real->portnum;
77 extra->physport = real;
78 extra->muxport = muxport;
81 return extra;
84 /* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains. */
85 int parport_daisy_init (struct parport *port)
87 char *deviceid;
88 static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
89 int num_ports;
90 int i;
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. */
101 port->muxport = 0;
102 printk (KERN_INFO
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);
108 if (!extra) {
109 if (signal_pending (current))
110 break;
112 schedule ();
113 continue;
116 printk (KERN_INFO
117 "%s: %d%s port of %d-way multiplexor on %s\n",
118 extra->name, i + 1, th[i + 1], num_ports,
119 port->name);
121 /* Analyse that port too. We won't recurse
122 forever because of the 'port->muxport < 0'
123 test above. */
124 parport_announce_port (extra);
128 if (port->muxport >= 0)
129 select_port (port);
131 parport_daisy_deselect_all (port);
132 assign_addrs (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);
139 if (deviceid) {
140 parport_device_id (numdevs - 1, deviceid, 1000);
141 kfree (deviceid);
144 return 0;
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;
154 while (prev) {
155 dev = prev->next;
156 if (dev && dev->port == port)
157 prev->next = dev->next;
159 prev = prev->next;
162 /* Gaps in the numbering could be handled better. How should
163 someone enumerate through all IEEE1284.3 devices in the
164 topology?. */
165 if (!topology) numdevs = 0;
166 return; }
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;
176 int portnum;
177 int muxnum;
178 int daisynum;
180 if (parport_device_coords (devnum, &portnum, &muxnum, &daisynum))
181 return NULL;
183 while (port && ((port->portnum != portnum) ||
184 (port->muxport != muxnum)))
185 port = port->next;
187 if (!port)
188 /* No corresponding parport. */
189 return NULL;
191 dev = parport_register_device (port, name, pf, kf,
192 irqf, flags, handle);
193 if (dev)
194 dev->daisy = daisynum;
196 /* Check that there really is a device to select. */
197 if (daisynum >= 0) {
198 int selected;
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);
206 return NULL;
210 return 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)
226 dev = dev->next;
228 if (!dev)
229 return -ENXIO;
231 return dev->devnum;
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)
240 dev = dev->next;
242 if (!dev)
243 return -ENXIO;
245 if (parport) *parport = dev->port->portnum;
246 if (mux) *mux = dev->port->muxport;
247 if (daisy) *daisy = dev->daisy;
248 return 0;
251 /* Send a daisy-chain-style CPP command packet. */
252 static int cpp_daisy (struct parport *port, int cmd)
254 unsigned char s;
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",
269 port->name, s);
270 return -ENXIO;
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",
280 port->name, s);
281 return -ENXIO;
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);
289 udelay (1);
290 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
291 udelay (1);
292 s = parport_read_status (port);
293 parport_write_data (port, 0xff); udelay (2);
295 return s;
298 /* Send a mux-style CPP command packet. */
299 static int cpp_mux (struct parport *port, int cmd)
301 unsigned char s;
302 int rc;
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",
315 port->name, cmd, s);
316 return -EIO;
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));
324 return rc;
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;
357 unsigned char daisy;
358 int thisdev = numdevs;
359 char *deviceid;
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",
374 port->name, s);
375 return -ENXIO;
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",
385 port->name, s);
386 return -ENXIO;
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);
395 udelay (2);
396 parport_frob_control (port,
397 PARPORT_CONTROL_STROBE,
398 PARPORT_CONTROL_STROBE);
399 udelay (1);
400 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
401 udelay (1);
403 if (last_dev)
404 /* No more devices. */
405 break;
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,
415 numdevs - thisdev);
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);
424 kfree (deviceid);
425 return 0;
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)
437 d = d->next;
439 /* Search. */
440 while (d) {
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)))
445 break;
447 d = d->next;
450 if (d)
451 return d->devnum;
453 return -1;
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)
464 d = d->next;
466 /* Search. */
467 while (d && d->port->probe_info[1 + d->daisy].class != cls)
468 d = d->next;
470 if (d)
471 return d->devnum;
473 return -1;