blob: ff9f34453530d991d668fb35737c488a107b1c17 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IEEE 1284.3 Parallel port daisy chain and multiplexor code
3 *
4 * Copyright (C) 1999, 2000 Tim Waugh <tim@cyberelk.demon.co.uk>
5 *
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.
10 *
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.
16 *
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.
20 */
21
22#include <linux/module.h>
23#include <linux/parport.h>
24#include <linux/delay.h>
25#include <linux/sched.h>
26
27#include <asm/current.h>
28#include <asm/uaccess.h>
29
30#undef DEBUG
31
32#ifdef DEBUG
Matthew Martind32ccc42006-10-03 23:25:14 +020033#define DPRINTK(stuff...) printk(stuff)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#else
35#define DPRINTK(stuff...)
36#endif
37
38static struct daisydev {
39 struct daisydev *next;
40 struct parport *port;
41 int daisy;
42 int devnum;
43} *topology = NULL;
44static DEFINE_SPINLOCK(topology_lock);
45
46static int numdevs = 0;
47
48/* Forward-declaration of lower-level functions. */
Matthew Martind32ccc42006-10-03 23:25:14 +020049static int mux_present(struct parport *port);
50static int num_mux_ports(struct parport *port);
51static int select_port(struct parport *port);
52static int assign_addrs(struct parport *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/* Add a device to the discovered topology. */
Matthew Martind32ccc42006-10-03 23:25:14 +020055static void add_dev(int devnum, struct parport *port, int daisy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct daisydev *newdev, **p;
Matthew Martind32ccc42006-10-03 23:25:14 +020058 newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 if (newdev) {
60 newdev->port = port;
61 newdev->daisy = daisy;
62 newdev->devnum = devnum;
63 spin_lock(&topology_lock);
64 for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
65 ;
66 newdev->next = *p;
67 *p = newdev;
68 spin_unlock(&topology_lock);
69 }
70}
71
72/* Clone a parport (actually, make an alias). */
Matthew Martind32ccc42006-10-03 23:25:14 +020073static struct parport *clone_parport(struct parport *real, int muxport)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Matthew Martind32ccc42006-10-03 23:25:14 +020075 struct parport *extra = parport_register_port(real->base,
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 real->irq,
77 real->dma,
78 real->ops);
79 if (extra) {
80 extra->portnum = real->portnum;
81 extra->physport = real;
82 extra->muxport = muxport;
83 real->slaves[muxport-1] = extra;
84 }
85
86 return extra;
87}
88
89/* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains.
90 * Return value is number of devices actually detected. */
Matthew Martind32ccc42006-10-03 23:25:14 +020091int parport_daisy_init(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 int detected = 0;
94 char *deviceid;
95 static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
96 int num_ports;
97 int i;
98 int last_try = 0;
99
100again:
101 /* Because this is called before any other devices exist,
102 * we don't have to claim exclusive access. */
103
104 /* If mux present on normal port, need to create new
105 * parports for each extra port. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200106 if (port->muxport < 0 && mux_present(port) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* don't be fooled: a mux must have 2 or 4 ports. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200108 ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /* Leave original as port zero. */
110 port->muxport = 0;
Matthew Martind32ccc42006-10-03 23:25:14 +0200111 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 "%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. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200116 struct parport *extra = clone_parport(port, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (!extra) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200118 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
120
Matthew Martind32ccc42006-10-03 23:25:14 +0200121 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 continue;
123 }
124
Matthew Martind32ccc42006-10-03 23:25:14 +0200125 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 "%s: %d%s port of %d-way multiplexor on %s\n",
127 extra->name, i + 1, th[i + 1], num_ports,
128 port->name);
129
130 /* Analyse that port too. We won't recurse
131 forever because of the 'port->muxport < 0'
132 test above. */
133 parport_daisy_init(extra);
134 }
135 }
136
137 if (port->muxport >= 0)
Matthew Martind32ccc42006-10-03 23:25:14 +0200138 select_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Matthew Martind32ccc42006-10-03 23:25:14 +0200140 parport_daisy_deselect_all(port);
141 detected += assign_addrs(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /* Count the potential legacy device at the end. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200144 add_dev(numdevs++, port, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /* Find out the legacy device's IEEE 1284 device ID. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200147 deviceid = kmalloc(1024, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (deviceid) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200149 if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 detected++;
151
Matthew Martind32ccc42006-10-03 23:25:14 +0200152 kfree(deviceid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
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
158 they wake up. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200159 parport_daisy_fini(port);
160 parport_write_control(port, PARPORT_CONTROL_SELECT);
161 udelay(50);
162 parport_write_control(port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 PARPORT_CONTROL_SELECT |
164 PARPORT_CONTROL_INIT);
Matthew Martind32ccc42006-10-03 23:25:14 +0200165 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 last_try = 1;
167 goto again;
168 }
169
170 return detected;
171}
172
173/* Forget about devices on a physical port. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200174void parport_daisy_fini(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 struct daisydev **p;
177
178 spin_lock(&topology_lock);
179 p = &topology;
180 while (*p) {
181 struct daisydev *dev = *p;
182 if (dev->port != port) {
183 p = &dev->next;
184 continue;
185 }
186 *p = dev->next;
187 kfree(dev);
188 }
189
190 /* Gaps in the numbering could be handled better. How should
191 someone enumerate through all IEEE1284.3 devices in the
192 topology?. */
193 if (!topology) numdevs = 0;
194 spin_unlock(&topology_lock);
195 return;
196}
197
198/**
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
203 * @kf: kick callback
204 * @irqf: interrupt handler
205 * @flags: registration flags
206 * @handle: driver data
207 *
208 * This function is similar to parport_register_device(), except
209 * that it locates a device by its number rather than by the port
210 * it is attached to.
211 *
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().
215 **/
216
Matthew Martind32ccc42006-10-03 23:25:14 +0200217struct pardevice *parport_open(int devnum, const char *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 int (*pf) (void *), void (*kf) (void *),
David Howells7d12e782006-10-05 14:55:46 +0100219 void (*irqf) (int, void *),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 int flags, void *handle)
221{
222 struct daisydev *p = topology;
223 struct parport *port;
224 struct pardevice *dev;
225 int daisy;
226
227 spin_lock(&topology_lock);
228 while (p && p->devnum != devnum)
229 p = p->next;
230
231 if (!p) {
232 spin_unlock(&topology_lock);
233 return NULL;
234 }
235
236 daisy = p->daisy;
237 port = parport_get_port(p->port);
238 spin_unlock(&topology_lock);
239
Matthew Martind32ccc42006-10-03 23:25:14 +0200240 dev = parport_register_device(port, name, pf, kf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 irqf, flags, handle);
242 parport_put_port(port);
243 if (!dev)
244 return NULL;
245
246 dev->daisy = daisy;
247
248 /* Check that there really is a device to select. */
249 if (daisy >= 0) {
250 int selected;
Matthew Martind32ccc42006-10-03 23:25:14 +0200251 parport_claim_or_block(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 selected = port->daisy;
Matthew Martind32ccc42006-10-03 23:25:14 +0200253 parport_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Marko Kohtalac29a75e2006-01-06 00:19:46 -0800255 if (selected != daisy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 /* No corresponding device. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200257 parport_unregister_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return NULL;
259 }
260 }
261
262 return dev;
263}
264
265/**
266 * parport_close - close a device opened with parport_open()
267 * @dev: device to close
268 *
269 * This is to parport_open() as parport_unregister_device() is to
270 * parport_register_device().
271 **/
272
Matthew Martind32ccc42006-10-03 23:25:14 +0200273void parport_close(struct pardevice *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
Matthew Martind32ccc42006-10-03 23:25:14 +0200275 parport_unregister_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278/**
279 * parport_device_num - convert device coordinates
280 * @parport: parallel port number
281 * @mux: multiplexor port number (-1 for no multiplexor)
282 * @daisy: daisy chain address (-1 for no daisy chain address)
283 *
284 * This tries to locate a device on the given parallel port,
285 * multiplexor port and daisy chain address, and returns its
Randy Dunlap7d469722006-06-25 05:49:29 -0700286 * device number or %-ENXIO if no device with those coordinates
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * exists.
288 **/
289
Matthew Martind32ccc42006-10-03 23:25:14 +0200290int parport_device_num(int parport, int mux, int daisy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 int res = -ENXIO;
293 struct daisydev *dev;
294
295 spin_lock(&topology_lock);
296 dev = topology;
297 while (dev && dev->port->portnum != parport &&
298 dev->port->muxport != mux && dev->daisy != daisy)
299 dev = dev->next;
300 if (dev)
301 res = dev->devnum;
302 spin_unlock(&topology_lock);
303
304 return res;
305}
306
307/* Send a daisy-chain-style CPP command packet. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200308static int cpp_daisy(struct parport *port, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 unsigned char s;
311
Matthew Martind32ccc42006-10-03 23:25:14 +0200312 parport_data_forward(port);
313 parport_write_data(port, 0xaa); udelay(2);
314 parport_write_data(port, 0x55); udelay(2);
315 parport_write_data(port, 0x00); udelay(2);
316 parport_write_data(port, 0xff); udelay(2);
317 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 | PARPORT_STATUS_PAPEROUT
319 | PARPORT_STATUS_SELECT
320 | PARPORT_STATUS_ERROR);
321 if (s != (PARPORT_STATUS_BUSY
322 | PARPORT_STATUS_PAPEROUT
323 | PARPORT_STATUS_SELECT
324 | PARPORT_STATUS_ERROR)) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200325 DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 port->name, s);
327 return -ENXIO;
328 }
329
Matthew Martind32ccc42006-10-03 23:25:14 +0200330 parport_write_data(port, 0x87); udelay(2);
331 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 | PARPORT_STATUS_PAPEROUT
333 | PARPORT_STATUS_SELECT
334 | PARPORT_STATUS_ERROR);
335 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200336 DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff87(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 port->name, s);
338 return -ENXIO;
339 }
340
Matthew Martind32ccc42006-10-03 23:25:14 +0200341 parport_write_data(port, 0x78); udelay(2);
342 parport_write_data(port, cmd); udelay(2);
343 parport_frob_control(port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 PARPORT_CONTROL_STROBE,
345 PARPORT_CONTROL_STROBE);
Matthew Martind32ccc42006-10-03 23:25:14 +0200346 udelay(1);
347 s = parport_read_status(port);
348 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
349 udelay(1);
350 parport_write_data(port, 0xff); udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 return s;
353}
354
355/* Send a mux-style CPP command packet. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200356static int cpp_mux(struct parport *port, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 unsigned char s;
359 int rc;
360
Matthew Martind32ccc42006-10-03 23:25:14 +0200361 parport_data_forward(port);
362 parport_write_data(port, 0xaa); udelay(2);
363 parport_write_data(port, 0x55); udelay(2);
364 parport_write_data(port, 0xf0); udelay(2);
365 parport_write_data(port, 0x0f); udelay(2);
366 parport_write_data(port, 0x52); udelay(2);
367 parport_write_data(port, 0xad); udelay(2);
368 parport_write_data(port, cmd); udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Matthew Martind32ccc42006-10-03 23:25:14 +0200370 s = parport_read_status(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (!(s & PARPORT_STATUS_ACK)) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200372 DPRINTK(KERN_DEBUG "%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 port->name, cmd, s);
374 return -EIO;
375 }
376
377 rc = (((s & PARPORT_STATUS_SELECT ? 1 : 0) << 0) |
378 ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
379 ((s & PARPORT_STATUS_BUSY ? 0 : 1) << 2) |
380 ((s & PARPORT_STATUS_ERROR ? 0 : 1) << 3));
381
382 return rc;
383}
384
Matthew Martind32ccc42006-10-03 23:25:14 +0200385void parport_daisy_deselect_all(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Matthew Martind32ccc42006-10-03 23:25:14 +0200387 cpp_daisy(port, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Matthew Martind32ccc42006-10-03 23:25:14 +0200390int parport_daisy_select(struct parport *port, int daisy, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 switch (mode)
393 {
394 // For these modes we should switch to EPP mode:
395 case IEEE1284_MODE_EPP:
396 case IEEE1284_MODE_EPPSL:
397 case IEEE1284_MODE_EPPSWE:
Matthew Martind32ccc42006-10-03 23:25:14 +0200398 return !(cpp_daisy(port, 0x20 + daisy) &
Marko Kohtala7c9cc3b2006-01-06 00:19:46 -0800399 PARPORT_STATUS_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 // For these modes we should switch to ECP mode:
402 case IEEE1284_MODE_ECP:
403 case IEEE1284_MODE_ECPRLE:
404 case IEEE1284_MODE_ECPSWE:
Matthew Martind32ccc42006-10-03 23:25:14 +0200405 return !(cpp_daisy(port, 0xd0 + daisy) &
Marko Kohtala7c9cc3b2006-01-06 00:19:46 -0800406 PARPORT_STATUS_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 // Nothing was told for BECP in Daisy chain specification.
409 // May be it's wise to use ECP?
410 case IEEE1284_MODE_BECP:
411 // Others use compat mode
412 case IEEE1284_MODE_NIBBLE:
413 case IEEE1284_MODE_BYTE:
414 case IEEE1284_MODE_COMPAT:
415 default:
Matthew Martind32ccc42006-10-03 23:25:14 +0200416 return !(cpp_daisy(port, 0xe0 + daisy) &
Marko Kohtala7c9cc3b2006-01-06 00:19:46 -0800417 PARPORT_STATUS_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419}
420
Matthew Martind32ccc42006-10-03 23:25:14 +0200421static int mux_present(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Matthew Martind32ccc42006-10-03 23:25:14 +0200423 return cpp_mux(port, 0x51) == 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Matthew Martind32ccc42006-10-03 23:25:14 +0200426static int num_mux_ports(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Matthew Martind32ccc42006-10-03 23:25:14 +0200428 return cpp_mux(port, 0x58);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Matthew Martind32ccc42006-10-03 23:25:14 +0200431static int select_port(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
433 int muxport = port->muxport;
Matthew Martind32ccc42006-10-03 23:25:14 +0200434 return cpp_mux(port, 0x60 + muxport) == muxport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
Matthew Martind32ccc42006-10-03 23:25:14 +0200437static int assign_addrs(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Marko Kohtala310c8c32006-01-06 00:19:45 -0800439 unsigned char s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 unsigned char daisy;
441 int thisdev = numdevs;
442 int detected;
443 char *deviceid;
444
Matthew Martind32ccc42006-10-03 23:25:14 +0200445 parport_data_forward(port);
446 parport_write_data(port, 0xaa); udelay(2);
447 parport_write_data(port, 0x55); udelay(2);
448 parport_write_data(port, 0x00); udelay(2);
449 parport_write_data(port, 0xff); udelay(2);
450 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 | PARPORT_STATUS_PAPEROUT
452 | PARPORT_STATUS_SELECT
453 | PARPORT_STATUS_ERROR);
454 if (s != (PARPORT_STATUS_BUSY
455 | PARPORT_STATUS_PAPEROUT
456 | PARPORT_STATUS_SELECT
457 | PARPORT_STATUS_ERROR)) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200458 DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 port->name, s);
460 return 0;
461 }
462
Matthew Martind32ccc42006-10-03 23:25:14 +0200463 parport_write_data(port, 0x87); udelay(2);
464 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 | PARPORT_STATUS_PAPEROUT
466 | PARPORT_STATUS_SELECT
467 | PARPORT_STATUS_ERROR);
468 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200469 DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff87(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 port->name, s);
471 return 0;
472 }
473
Matthew Martind32ccc42006-10-03 23:25:14 +0200474 parport_write_data(port, 0x78); udelay(2);
475 s = parport_read_status(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Marko Kohtala310c8c32006-01-06 00:19:45 -0800477 for (daisy = 0;
478 (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
479 == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
480 && daisy < 4;
481 ++daisy) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200482 parport_write_data(port, daisy);
483 udelay(2);
484 parport_frob_control(port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 PARPORT_CONTROL_STROBE,
486 PARPORT_CONTROL_STROBE);
Matthew Martind32ccc42006-10-03 23:25:14 +0200487 udelay(1);
488 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
489 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Matthew Martind32ccc42006-10-03 23:25:14 +0200491 add_dev(numdevs++, port, daisy);
Marko Kohtala310c8c32006-01-06 00:19:45 -0800492
493 /* See if this device thought it was the last in the
494 * chain. */
495 if (!(s & PARPORT_STATUS_BUSY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 break;
497
Marko Kohtala310c8c32006-01-06 00:19:45 -0800498 /* We are seeing pass through status now. We see
499 last_dev from next device or if last_dev does not
500 work status lines from some non-daisy chain
501 device. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200502 s = parport_read_status(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
504
Matthew Martind32ccc42006-10-03 23:25:14 +0200505 parport_write_data(port, 0xff); udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 detected = numdevs - thisdev;
Matthew Martind32ccc42006-10-03 23:25:14 +0200507 DPRINTK(KERN_DEBUG "%s: Found %d daisy-chained devices\n", port->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 detected);
509
510 /* Ask the new devices to introduce themselves. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200511 deviceid = kmalloc(1024, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (!deviceid) return 0;
513
514 for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
Matthew Martind32ccc42006-10-03 23:25:14 +0200515 parport_device_id(thisdev, deviceid, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Matthew Martind32ccc42006-10-03 23:25:14 +0200517 kfree(deviceid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return detected;
519}