Import 2.3.12pre2
[davej-history.git] / drivers / parport / daisy.c
blob830d9d400d14454c0ffa29f95e1390df40603af2
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/parport.h>
19 #include <linux/delay.h>
20 #include <asm/uaccess.h>
22 #define DEBUG /* undef me for production */
24 #ifdef DEBUG
25 #define DPRINTK(stuff...) printk (stuff)
26 #else
27 #define DPRINTK(stuff...)
28 #endif
30 static struct daisydev {
31 struct daisydev *next;
32 struct parport *port;
33 int daisy;
34 int devnum;
35 } *topology = NULL;
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));
50 if (newdev) {
51 newdev->port = port;
52 newdev->daisy = daisy;
53 newdev->devnum = devnum;
54 newdev->next = topology;
55 if (!topology || topology->devnum >= devnum)
56 topology = newdev;
57 else {
58 struct daisydev *prev = topology;
59 while (prev->next && prev->next->devnum < devnum)
60 prev = prev->next;
61 newdev->next = prev->next;
62 prev->next = newdev;
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,
71 real->irq,
72 real->dma,
73 real->ops);
74 if (extra) {
75 extra->portnum = real->portnum;
76 extra->physport = real;
77 extra->muxport = muxport;
80 return extra;
83 /* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains. */
84 int parport_daisy_init (struct parport *port)
86 char *deviceid;
87 static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
88 int num_ports;
89 int i;
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. */
100 port->muxport = 0;
101 printk (KERN_INFO
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);
107 if (!extra) {
108 if (signal_pending (current))
109 break;
111 schedule ();
112 continue;
115 printk (KERN_INFO
116 "%s: %d%s port of %d-way multiplexor on %s\n",
117 extra->name, i + 1, th[i + 1], num_ports,
118 port->name);
120 /* Analyse that port too. We won't recurse
121 forever because of the 'port->muxport < 0'
122 test above. */
123 parport_announce_port (extra);
127 if (port->muxport >= 0)
128 select_port (port);
130 parport_daisy_deselect_all (port);
131 assign_addrs (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);
138 if (deviceid) {
139 parport_device_id (numdevs - 1, deviceid, 1000);
140 kfree (deviceid);
143 return 0;
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;
153 while (prev) {
154 dev = prev->next;
155 if (dev && dev->port == port)
156 prev->next = dev->next;
158 prev = prev->next;
161 /* Gaps in the numbering could be handled better. How should
162 someone enumerate through all IEEE1284.3 devices in the
163 topology?. */
164 if (!topology) numdevs = 0;
165 return; }
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;
175 int portnum;
176 int muxnum;
177 int daisynum;
179 if (parport_device_coords (devnum, &portnum, &muxnum, &daisynum))
180 return NULL;
182 while (port && ((port->portnum != portnum) ||
183 (port->muxport != muxnum)))
184 port = port->next;
186 if (!port)
187 /* No corresponding parport. */
188 return NULL;
190 dev = parport_register_device (port, name, pf, kf,
191 irqf, flags, handle);
192 if (dev)
193 dev->daisy = daisynum;
195 /* Check that there really is a device to select. */
196 if (daisynum >= 0) {
197 int selected;
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);
205 return NULL;
209 return 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)
225 dev = dev->next;
227 if (!dev)
228 return -ENXIO;
230 return dev->devnum;
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)
239 dev = dev->next;
241 if (!dev)
242 return -ENXIO;
244 if (parport) *parport = dev->port->portnum;
245 if (mux) *mux = dev->port->muxport;
246 if (daisy) *daisy = dev->daisy;
247 return 0;
250 /* Send a daisy-chain-style CPP command packet. */
251 static int cpp_daisy (struct parport *port, int cmd)
253 unsigned char s;
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",
268 port->name, s);
269 return -ENXIO;
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",
279 port->name, s);
280 return -ENXIO;
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);
288 udelay (1);
289 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
290 udelay (1);
291 s = parport_read_status (port);
292 parport_write_data (port, 0xff); udelay (2);
294 return s;
297 /* Send a mux-style CPP command packet. */
298 static int cpp_mux (struct parport *port, int cmd)
300 unsigned char s;
301 int rc;
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",
314 port->name, cmd, s);
315 return -EIO;
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));
323 return rc;
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;
356 unsigned char daisy;
357 int thisdev = numdevs;
358 char *deviceid;
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",
373 port->name, s);
374 return -ENXIO;
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",
384 port->name, s);
385 return -ENXIO;
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);
394 udelay (2);
395 parport_frob_control (port,
396 PARPORT_CONTROL_STROBE,
397 PARPORT_CONTROL_STROBE);
398 udelay (1);
399 parport_frob_control (port, PARPORT_CONTROL_STROBE, 0);
400 udelay (1);
402 if (last_dev)
403 /* No more devices. */
404 break;
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,
414 numdevs - thisdev);
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);
423 kfree (deviceid);
424 return 0;
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)
436 d = d->next;
438 /* Search. */
439 while (d) {
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)))
444 break;
446 d = d->next;
449 if (d)
450 return d->devnum;
452 return -1;
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)
463 d = d->next;
465 /* Search. */
466 while (d && d->port->probe_info[1 + d->daisy].class != cls)
467 d = d->next;
469 if (d)
470 return d->devnum;
472 return -1;