blob: 3b00e2c8e2e9a3ebc328196a0294577648c5bad4 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010026#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/current.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#undef DEBUG
32
33#ifdef DEBUG
Matthew Martind32ccc42006-10-03 23:25:14 +020034#define DPRINTK(stuff...) printk(stuff)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#else
36#define DPRINTK(stuff...)
37#endif
38
39static struct daisydev {
40 struct daisydev *next;
41 struct parport *port;
42 int daisy;
43 int devnum;
44} *topology = NULL;
45static DEFINE_SPINLOCK(topology_lock);
46
Carlos Palminhadf4c7562017-08-12 22:45:49 +010047static int numdevs;
Sudip Mukherjee60f8a592019-10-16 15:45:40 +010048static bool daisy_init_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Forward-declaration of lower-level functions. */
Matthew Martind32ccc42006-10-03 23:25:14 +020051static int mux_present(struct parport *port);
52static int num_mux_ports(struct parport *port);
53static int select_port(struct parport *port);
54static int assign_addrs(struct parport *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/* Add a device to the discovered topology. */
Matthew Martind32ccc42006-10-03 23:25:14 +020057static void add_dev(int devnum, struct parport *port, int daisy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
59 struct daisydev *newdev, **p;
Matthew Martind32ccc42006-10-03 23:25:14 +020060 newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 if (newdev) {
62 newdev->port = port;
63 newdev->daisy = daisy;
64 newdev->devnum = devnum;
65 spin_lock(&topology_lock);
66 for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
67 ;
68 newdev->next = *p;
69 *p = newdev;
70 spin_unlock(&topology_lock);
71 }
72}
73
74/* Clone a parport (actually, make an alias). */
Matthew Martind32ccc42006-10-03 23:25:14 +020075static struct parport *clone_parport(struct parport *real, int muxport)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076{
Matthew Martind32ccc42006-10-03 23:25:14 +020077 struct parport *extra = parport_register_port(real->base,
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 real->irq,
79 real->dma,
80 real->ops);
81 if (extra) {
82 extra->portnum = real->portnum;
83 extra->physport = real;
84 extra->muxport = muxport;
85 real->slaves[muxport-1] = extra;
86 }
87
88 return extra;
89}
90
Sudip Mukherjee60f8a592019-10-16 15:45:40 +010091static int daisy_drv_probe(struct pardevice *par_dev)
92{
93 struct device_driver *drv = par_dev->dev.driver;
94
95 if (strcmp(drv->name, "daisy_drv"))
96 return -ENODEV;
97 if (strcmp(par_dev->name, daisy_dev_name))
98 return -ENODEV;
99
100 return 0;
101}
102
103static struct parport_driver daisy_driver = {
104 .name = "daisy_drv",
105 .probe = daisy_drv_probe,
106 .devmodel = true,
107};
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109/* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains.
110 * Return value is number of devices actually detected. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200111int parport_daisy_init(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 int detected = 0;
114 char *deviceid;
115 static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
116 int num_ports;
117 int i;
118 int last_try = 0;
119
Sudip Mukherjee60f8a592019-10-16 15:45:40 +0100120 if (!daisy_init_done) {
121 /*
122 * flag should be marked true first as
123 * parport_register_driver() might try to load the low
124 * level driver which will lead to announcing new ports
125 * and which will again come back here at
126 * parport_daisy_init()
127 */
128 daisy_init_done = true;
129 i = parport_register_driver(&daisy_driver);
130 if (i) {
131 pr_err("daisy registration failed\n");
132 daisy_init_done = false;
133 return i;
134 }
135 }
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137again:
138 /* Because this is called before any other devices exist,
139 * we don't have to claim exclusive access. */
140
141 /* If mux present on normal port, need to create new
142 * parports for each extra port. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200143 if (port->muxport < 0 && mux_present(port) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 /* don't be fooled: a mux must have 2 or 4 ports. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200145 ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 /* Leave original as port zero. */
147 port->muxport = 0;
Matthew Martind32ccc42006-10-03 23:25:14 +0200148 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 "%s: 1st (default) port of %d-way multiplexor\n",
150 port->name, num_ports);
151 for (i = 1; i < num_ports; i++) {
152 /* Clone the port. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200153 struct parport *extra = clone_parport(port, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!extra) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200155 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 break;
157
Matthew Martind32ccc42006-10-03 23:25:14 +0200158 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 continue;
160 }
161
Matthew Martind32ccc42006-10-03 23:25:14 +0200162 printk(KERN_INFO
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 "%s: %d%s port of %d-way multiplexor on %s\n",
164 extra->name, i + 1, th[i + 1], num_ports,
165 port->name);
166
167 /* Analyse that port too. We won't recurse
168 forever because of the 'port->muxport < 0'
169 test above. */
170 parport_daisy_init(extra);
171 }
172 }
173
174 if (port->muxport >= 0)
Matthew Martind32ccc42006-10-03 23:25:14 +0200175 select_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Matthew Martind32ccc42006-10-03 23:25:14 +0200177 parport_daisy_deselect_all(port);
178 detected += assign_addrs(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 /* Count the potential legacy device at the end. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200181 add_dev(numdevs++, port, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 /* Find out the legacy device's IEEE 1284 device ID. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200184 deviceid = kmalloc(1024, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (deviceid) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200186 if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 detected++;
188
Matthew Martind32ccc42006-10-03 23:25:14 +0200189 kfree(deviceid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
192 if (!detected && !last_try) {
193 /* No devices were detected. Perhaps they are in some
194 funny state; let's try to reset them and see if
195 they wake up. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200196 parport_daisy_fini(port);
197 parport_write_control(port, PARPORT_CONTROL_SELECT);
198 udelay(50);
199 parport_write_control(port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 PARPORT_CONTROL_SELECT |
201 PARPORT_CONTROL_INIT);
Matthew Martind32ccc42006-10-03 23:25:14 +0200202 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 last_try = 1;
204 goto again;
205 }
206
207 return detected;
208}
209
210/* Forget about devices on a physical port. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200211void parport_daisy_fini(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct daisydev **p;
214
215 spin_lock(&topology_lock);
216 p = &topology;
217 while (*p) {
218 struct daisydev *dev = *p;
219 if (dev->port != port) {
220 p = &dev->next;
221 continue;
222 }
223 *p = dev->next;
224 kfree(dev);
225 }
226
227 /* Gaps in the numbering could be handled better. How should
228 someone enumerate through all IEEE1284.3 devices in the
229 topology?. */
230 if (!topology) numdevs = 0;
231 spin_unlock(&topology_lock);
232 return;
233}
234
235/**
236 * parport_open - find a device by canonical device number
237 * @devnum: canonical device number
238 * @name: name to associate with the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 *
240 * This function is similar to parport_register_device(), except
241 * that it locates a device by its number rather than by the port
242 * it is attached to.
243 *
244 * All parameters except for @devnum are the same as for
245 * parport_register_device(). The return value is the same as
246 * for parport_register_device().
247 **/
248
Jeff Garzik5712cb32007-10-19 02:54:26 -0400249struct pardevice *parport_open(int devnum, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct daisydev *p = topology;
Sudip Mukherjee60f8a592019-10-16 15:45:40 +0100252 struct pardev_cb par_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct parport *port;
254 struct pardevice *dev;
255 int daisy;
256
Sudip Mukherjee60f8a592019-10-16 15:45:40 +0100257 memset(&par_cb, 0, sizeof(par_cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 spin_lock(&topology_lock);
259 while (p && p->devnum != devnum)
260 p = p->next;
261
262 if (!p) {
263 spin_unlock(&topology_lock);
264 return NULL;
265 }
266
267 daisy = p->daisy;
268 port = parport_get_port(p->port);
269 spin_unlock(&topology_lock);
270
Sudip Mukherjee60f8a592019-10-16 15:45:40 +0100271 dev = parport_register_dev_model(port, name, &par_cb, devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 parport_put_port(port);
273 if (!dev)
274 return NULL;
275
276 dev->daisy = daisy;
277
278 /* Check that there really is a device to select. */
279 if (daisy >= 0) {
280 int selected;
Matthew Martind32ccc42006-10-03 23:25:14 +0200281 parport_claim_or_block(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 selected = port->daisy;
Matthew Martind32ccc42006-10-03 23:25:14 +0200283 parport_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Marko Kohtalac29a75e2006-01-06 00:19:46 -0800285 if (selected != daisy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 /* No corresponding device. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200287 parport_unregister_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return NULL;
289 }
290 }
291
292 return dev;
293}
294
295/**
296 * parport_close - close a device opened with parport_open()
297 * @dev: device to close
298 *
299 * This is to parport_open() as parport_unregister_device() is to
300 * parport_register_device().
301 **/
302
Matthew Martind32ccc42006-10-03 23:25:14 +0200303void parport_close(struct pardevice *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Matthew Martind32ccc42006-10-03 23:25:14 +0200305 parport_unregister_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308/* Send a daisy-chain-style CPP command packet. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200309static int cpp_daisy(struct parport *port, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 unsigned char s;
312
Matthew Martind32ccc42006-10-03 23:25:14 +0200313 parport_data_forward(port);
314 parport_write_data(port, 0xaa); udelay(2);
315 parport_write_data(port, 0x55); udelay(2);
316 parport_write_data(port, 0x00); udelay(2);
317 parport_write_data(port, 0xff); udelay(2);
318 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 | PARPORT_STATUS_PAPEROUT
320 | PARPORT_STATUS_SELECT
321 | PARPORT_STATUS_ERROR);
322 if (s != (PARPORT_STATUS_BUSY
323 | PARPORT_STATUS_PAPEROUT
324 | PARPORT_STATUS_SELECT
325 | PARPORT_STATUS_ERROR)) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200326 DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 port->name, s);
328 return -ENXIO;
329 }
330
Matthew Martind32ccc42006-10-03 23:25:14 +0200331 parport_write_data(port, 0x87); udelay(2);
332 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 | PARPORT_STATUS_PAPEROUT
334 | PARPORT_STATUS_SELECT
335 | PARPORT_STATUS_ERROR);
336 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200337 DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff87(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 port->name, s);
339 return -ENXIO;
340 }
341
Matthew Martind32ccc42006-10-03 23:25:14 +0200342 parport_write_data(port, 0x78); udelay(2);
343 parport_write_data(port, cmd); udelay(2);
344 parport_frob_control(port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 PARPORT_CONTROL_STROBE,
346 PARPORT_CONTROL_STROBE);
Matthew Martind32ccc42006-10-03 23:25:14 +0200347 udelay(1);
348 s = parport_read_status(port);
349 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
350 udelay(1);
351 parport_write_data(port, 0xff); udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 return s;
354}
355
356/* Send a mux-style CPP command packet. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200357static int cpp_mux(struct parport *port, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 unsigned char s;
360 int rc;
361
Matthew Martind32ccc42006-10-03 23:25:14 +0200362 parport_data_forward(port);
363 parport_write_data(port, 0xaa); udelay(2);
364 parport_write_data(port, 0x55); udelay(2);
365 parport_write_data(port, 0xf0); udelay(2);
366 parport_write_data(port, 0x0f); udelay(2);
367 parport_write_data(port, 0x52); udelay(2);
368 parport_write_data(port, 0xad); udelay(2);
369 parport_write_data(port, cmd); udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Matthew Martind32ccc42006-10-03 23:25:14 +0200371 s = parport_read_status(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (!(s & PARPORT_STATUS_ACK)) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200373 DPRINTK(KERN_DEBUG "%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 port->name, cmd, s);
375 return -EIO;
376 }
377
378 rc = (((s & PARPORT_STATUS_SELECT ? 1 : 0) << 0) |
379 ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
380 ((s & PARPORT_STATUS_BUSY ? 0 : 1) << 2) |
381 ((s & PARPORT_STATUS_ERROR ? 0 : 1) << 3));
382
383 return rc;
384}
385
Matthew Martind32ccc42006-10-03 23:25:14 +0200386void parport_daisy_deselect_all(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
Matthew Martind32ccc42006-10-03 23:25:14 +0200388 cpp_daisy(port, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Matthew Martind32ccc42006-10-03 23:25:14 +0200391int parport_daisy_select(struct parport *port, int daisy, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 switch (mode)
394 {
395 // For these modes we should switch to EPP mode:
396 case IEEE1284_MODE_EPP:
397 case IEEE1284_MODE_EPPSL:
398 case IEEE1284_MODE_EPPSWE:
Matthew Martind32ccc42006-10-03 23:25:14 +0200399 return !(cpp_daisy(port, 0x20 + daisy) &
Marko Kohtala7c9cc3b2006-01-06 00:19:46 -0800400 PARPORT_STATUS_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 // For these modes we should switch to ECP mode:
403 case IEEE1284_MODE_ECP:
404 case IEEE1284_MODE_ECPRLE:
405 case IEEE1284_MODE_ECPSWE:
Matthew Martind32ccc42006-10-03 23:25:14 +0200406 return !(cpp_daisy(port, 0xd0 + daisy) &
Marko Kohtala7c9cc3b2006-01-06 00:19:46 -0800407 PARPORT_STATUS_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 // Nothing was told for BECP in Daisy chain specification.
410 // May be it's wise to use ECP?
411 case IEEE1284_MODE_BECP:
412 // Others use compat mode
413 case IEEE1284_MODE_NIBBLE:
414 case IEEE1284_MODE_BYTE:
415 case IEEE1284_MODE_COMPAT:
416 default:
Matthew Martind32ccc42006-10-03 23:25:14 +0200417 return !(cpp_daisy(port, 0xe0 + daisy) &
Marko Kohtala7c9cc3b2006-01-06 00:19:46 -0800418 PARPORT_STATUS_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420}
421
Matthew Martind32ccc42006-10-03 23:25:14 +0200422static int mux_present(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Matthew Martind32ccc42006-10-03 23:25:14 +0200424 return cpp_mux(port, 0x51) == 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Matthew Martind32ccc42006-10-03 23:25:14 +0200427static int num_mux_ports(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Matthew Martind32ccc42006-10-03 23:25:14 +0200429 return cpp_mux(port, 0x58);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Matthew Martind32ccc42006-10-03 23:25:14 +0200432static int select_port(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 int muxport = port->muxport;
Matthew Martind32ccc42006-10-03 23:25:14 +0200435 return cpp_mux(port, 0x60 + muxport) == muxport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Matthew Martind32ccc42006-10-03 23:25:14 +0200438static int assign_addrs(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Marko Kohtala310c8c32006-01-06 00:19:45 -0800440 unsigned char s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned char daisy;
442 int thisdev = numdevs;
443 int detected;
444 char *deviceid;
445
Matthew Martind32ccc42006-10-03 23:25:14 +0200446 parport_data_forward(port);
447 parport_write_data(port, 0xaa); udelay(2);
448 parport_write_data(port, 0x55); udelay(2);
449 parport_write_data(port, 0x00); udelay(2);
450 parport_write_data(port, 0xff); udelay(2);
451 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 | PARPORT_STATUS_PAPEROUT
453 | PARPORT_STATUS_SELECT
454 | PARPORT_STATUS_ERROR);
455 if (s != (PARPORT_STATUS_BUSY
456 | PARPORT_STATUS_PAPEROUT
457 | PARPORT_STATUS_SELECT
458 | PARPORT_STATUS_ERROR)) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200459 DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 port->name, s);
461 return 0;
462 }
463
Matthew Martind32ccc42006-10-03 23:25:14 +0200464 parport_write_data(port, 0x87); udelay(2);
465 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 | PARPORT_STATUS_PAPEROUT
467 | PARPORT_STATUS_SELECT
468 | PARPORT_STATUS_ERROR);
469 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200470 DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff87(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 port->name, s);
472 return 0;
473 }
474
Matthew Martind32ccc42006-10-03 23:25:14 +0200475 parport_write_data(port, 0x78); udelay(2);
476 s = parport_read_status(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Marko Kohtala310c8c32006-01-06 00:19:45 -0800478 for (daisy = 0;
479 (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
480 == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
481 && daisy < 4;
482 ++daisy) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200483 parport_write_data(port, daisy);
484 udelay(2);
485 parport_frob_control(port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 PARPORT_CONTROL_STROBE,
487 PARPORT_CONTROL_STROBE);
Matthew Martind32ccc42006-10-03 23:25:14 +0200488 udelay(1);
489 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
490 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Matthew Martind32ccc42006-10-03 23:25:14 +0200492 add_dev(numdevs++, port, daisy);
Marko Kohtala310c8c32006-01-06 00:19:45 -0800493
494 /* See if this device thought it was the last in the
495 * chain. */
496 if (!(s & PARPORT_STATUS_BUSY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
498
Marko Kohtala310c8c32006-01-06 00:19:45 -0800499 /* We are seeing pass through status now. We see
500 last_dev from next device or if last_dev does not
501 work status lines from some non-daisy chain
502 device. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200503 s = parport_read_status(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
505
Matthew Martind32ccc42006-10-03 23:25:14 +0200506 parport_write_data(port, 0xff); udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 detected = numdevs - thisdev;
Matthew Martind32ccc42006-10-03 23:25:14 +0200508 DPRINTK(KERN_DEBUG "%s: Found %d daisy-chained devices\n", port->name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 detected);
510
511 /* Ask the new devices to introduce themselves. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200512 deviceid = kmalloc(1024, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!deviceid) return 0;
514
515 for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
Matthew Martind32ccc42006-10-03 23:25:14 +0200516 parport_device_id(thisdev, deviceid, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Matthew Martind32ccc42006-10-03 23:25:14 +0200518 kfree(deviceid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return detected;
520}