blob: 6d78ec3a762fcb36e27e21e75d0488a664d009e3 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070033static struct daisydev {
34 struct daisydev *next;
35 struct parport *port;
36 int daisy;
37 int devnum;
38} *topology = NULL;
39static DEFINE_SPINLOCK(topology_lock);
40
Carlos Palminhadf4c7562017-08-12 22:45:49 +010041static int numdevs;
Sudip Mukherjee60f8a592019-10-16 15:45:40 +010042static bool daisy_init_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/* Forward-declaration of lower-level functions. */
Matthew Martind32ccc42006-10-03 23:25:14 +020045static int mux_present(struct parport *port);
46static int num_mux_ports(struct parport *port);
47static int select_port(struct parport *port);
48static int assign_addrs(struct parport *port);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Add a device to the discovered topology. */
Matthew Martind32ccc42006-10-03 23:25:14 +020051static void add_dev(int devnum, struct parport *port, int daisy)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 struct daisydev *newdev, **p;
Matthew Martind32ccc42006-10-03 23:25:14 +020054 newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 if (newdev) {
56 newdev->port = port;
57 newdev->daisy = daisy;
58 newdev->devnum = devnum;
59 spin_lock(&topology_lock);
60 for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
61 ;
62 newdev->next = *p;
63 *p = newdev;
64 spin_unlock(&topology_lock);
65 }
66}
67
68/* Clone a parport (actually, make an alias). */
Matthew Martind32ccc42006-10-03 23:25:14 +020069static struct parport *clone_parport(struct parport *real, int muxport)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Matthew Martind32ccc42006-10-03 23:25:14 +020071 struct parport *extra = parport_register_port(real->base,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 real->irq,
73 real->dma,
74 real->ops);
75 if (extra) {
76 extra->portnum = real->portnum;
77 extra->physport = real;
78 extra->muxport = muxport;
79 real->slaves[muxport-1] = extra;
80 }
81
82 return extra;
83}
84
Sudip Mukherjee60f8a592019-10-16 15:45:40 +010085static int daisy_drv_probe(struct pardevice *par_dev)
86{
87 struct device_driver *drv = par_dev->dev.driver;
88
89 if (strcmp(drv->name, "daisy_drv"))
90 return -ENODEV;
91 if (strcmp(par_dev->name, daisy_dev_name))
92 return -ENODEV;
93
94 return 0;
95}
96
97static struct parport_driver daisy_driver = {
98 .name = "daisy_drv",
99 .probe = daisy_drv_probe,
100 .devmodel = true,
101};
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains.
104 * Return value is number of devices actually detected. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200105int parport_daisy_init(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 int detected = 0;
108 char *deviceid;
109 static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
110 int num_ports;
111 int i;
112 int last_try = 0;
113
Sudip Mukherjee60f8a592019-10-16 15:45:40 +0100114 if (!daisy_init_done) {
115 /*
116 * flag should be marked true first as
117 * parport_register_driver() might try to load the low
118 * level driver which will lead to announcing new ports
119 * and which will again come back here at
120 * parport_daisy_init()
121 */
122 daisy_init_done = true;
123 i = parport_register_driver(&daisy_driver);
124 if (i) {
125 pr_err("daisy registration failed\n");
126 daisy_init_done = false;
127 return i;
128 }
129 }
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131again:
132 /* Because this is called before any other devices exist,
133 * we don't have to claim exclusive access. */
134
135 /* If mux present on normal port, need to create new
136 * parports for each extra port. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200137 if (port->muxport < 0 && mux_present(port) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /* don't be fooled: a mux must have 2 or 4 ports. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200139 ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 /* Leave original as port zero. */
141 port->muxport = 0;
Joe Perchesdecf26f2020-04-03 14:43:16 +0100142 pr_info("%s: 1st (default) port of %d-way multiplexor\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 port->name, num_ports);
144 for (i = 1; i < num_ports; i++) {
145 /* Clone the port. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200146 struct parport *extra = clone_parport(port, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (!extra) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200148 if (signal_pending(current))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 break;
150
Matthew Martind32ccc42006-10-03 23:25:14 +0200151 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 continue;
153 }
154
Joe Perchesdecf26f2020-04-03 14:43:16 +0100155 pr_info("%s: %d%s port of %d-way multiplexor on %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 extra->name, i + 1, th[i + 1], num_ports,
157 port->name);
158
159 /* Analyse that port too. We won't recurse
160 forever because of the 'port->muxport < 0'
161 test above. */
162 parport_daisy_init(extra);
163 }
164 }
165
166 if (port->muxport >= 0)
Matthew Martind32ccc42006-10-03 23:25:14 +0200167 select_port(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Matthew Martind32ccc42006-10-03 23:25:14 +0200169 parport_daisy_deselect_all(port);
170 detected += assign_addrs(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 /* Count the potential legacy device at the end. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200173 add_dev(numdevs++, port, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 /* Find out the legacy device's IEEE 1284 device ID. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200176 deviceid = kmalloc(1024, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (deviceid) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200178 if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 detected++;
180
Matthew Martind32ccc42006-10-03 23:25:14 +0200181 kfree(deviceid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
183
184 if (!detected && !last_try) {
185 /* No devices were detected. Perhaps they are in some
186 funny state; let's try to reset them and see if
187 they wake up. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200188 parport_daisy_fini(port);
189 parport_write_control(port, PARPORT_CONTROL_SELECT);
190 udelay(50);
191 parport_write_control(port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 PARPORT_CONTROL_SELECT |
193 PARPORT_CONTROL_INIT);
Matthew Martind32ccc42006-10-03 23:25:14 +0200194 udelay(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 last_try = 1;
196 goto again;
197 }
198
199 return detected;
200}
201
202/* Forget about devices on a physical port. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200203void parport_daisy_fini(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
205 struct daisydev **p;
206
207 spin_lock(&topology_lock);
208 p = &topology;
209 while (*p) {
210 struct daisydev *dev = *p;
211 if (dev->port != port) {
212 p = &dev->next;
213 continue;
214 }
215 *p = dev->next;
216 kfree(dev);
217 }
218
219 /* Gaps in the numbering could be handled better. How should
220 someone enumerate through all IEEE1284.3 devices in the
221 topology?. */
222 if (!topology) numdevs = 0;
223 spin_unlock(&topology_lock);
224 return;
225}
226
227/**
228 * parport_open - find a device by canonical device number
229 * @devnum: canonical device number
230 * @name: name to associate with the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 *
232 * This function is similar to parport_register_device(), except
233 * that it locates a device by its number rather than by the port
234 * it is attached to.
235 *
236 * All parameters except for @devnum are the same as for
237 * parport_register_device(). The return value is the same as
238 * for parport_register_device().
239 **/
240
Jeff Garzik5712cb32007-10-19 02:54:26 -0400241struct pardevice *parport_open(int devnum, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct daisydev *p = topology;
Sudip Mukherjee60f8a592019-10-16 15:45:40 +0100244 struct pardev_cb par_cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct parport *port;
246 struct pardevice *dev;
247 int daisy;
248
Sudip Mukherjee60f8a592019-10-16 15:45:40 +0100249 memset(&par_cb, 0, sizeof(par_cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 spin_lock(&topology_lock);
251 while (p && p->devnum != devnum)
252 p = p->next;
253
254 if (!p) {
255 spin_unlock(&topology_lock);
256 return NULL;
257 }
258
259 daisy = p->daisy;
260 port = parport_get_port(p->port);
261 spin_unlock(&topology_lock);
262
Sudip Mukherjee60f8a592019-10-16 15:45:40 +0100263 dev = parport_register_dev_model(port, name, &par_cb, devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 parport_put_port(port);
265 if (!dev)
266 return NULL;
267
268 dev->daisy = daisy;
269
270 /* Check that there really is a device to select. */
271 if (daisy >= 0) {
272 int selected;
Matthew Martind32ccc42006-10-03 23:25:14 +0200273 parport_claim_or_block(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 selected = port->daisy;
Matthew Martind32ccc42006-10-03 23:25:14 +0200275 parport_release(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Marko Kohtalac29a75e2006-01-06 00:19:46 -0800277 if (selected != daisy) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* No corresponding device. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200279 parport_unregister_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return NULL;
281 }
282 }
283
284 return dev;
285}
286
287/**
288 * parport_close - close a device opened with parport_open()
289 * @dev: device to close
290 *
291 * This is to parport_open() as parport_unregister_device() is to
292 * parport_register_device().
293 **/
294
Matthew Martind32ccc42006-10-03 23:25:14 +0200295void parport_close(struct pardevice *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Matthew Martind32ccc42006-10-03 23:25:14 +0200297 parport_unregister_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300/* Send a daisy-chain-style CPP command packet. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200301static int cpp_daisy(struct parport *port, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 unsigned char s;
304
Matthew Martind32ccc42006-10-03 23:25:14 +0200305 parport_data_forward(port);
306 parport_write_data(port, 0xaa); udelay(2);
307 parport_write_data(port, 0x55); udelay(2);
308 parport_write_data(port, 0x00); udelay(2);
309 parport_write_data(port, 0xff); udelay(2);
310 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 | PARPORT_STATUS_PAPEROUT
312 | PARPORT_STATUS_SELECT
313 | PARPORT_STATUS_ERROR);
314 if (s != (PARPORT_STATUS_BUSY
315 | PARPORT_STATUS_PAPEROUT
316 | PARPORT_STATUS_SELECT
317 | PARPORT_STATUS_ERROR)) {
Joe Perches88c5cbd2020-04-03 14:43:18 +0100318 pr_debug("%s: cpp_daisy: aa5500ff(%02x)\n", port->name, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return -ENXIO;
320 }
321
Matthew Martind32ccc42006-10-03 23:25:14 +0200322 parport_write_data(port, 0x87); udelay(2);
323 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 | PARPORT_STATUS_PAPEROUT
325 | PARPORT_STATUS_SELECT
326 | PARPORT_STATUS_ERROR);
327 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
Joe Perches88c5cbd2020-04-03 14:43:18 +0100328 pr_debug("%s: cpp_daisy: aa5500ff87(%02x)\n", port->name, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -ENXIO;
330 }
331
Matthew Martind32ccc42006-10-03 23:25:14 +0200332 parport_write_data(port, 0x78); udelay(2);
333 parport_write_data(port, cmd); udelay(2);
334 parport_frob_control(port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 PARPORT_CONTROL_STROBE,
336 PARPORT_CONTROL_STROBE);
Matthew Martind32ccc42006-10-03 23:25:14 +0200337 udelay(1);
338 s = parport_read_status(port);
339 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
340 udelay(1);
341 parport_write_data(port, 0xff); udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 return s;
344}
345
346/* Send a mux-style CPP command packet. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200347static int cpp_mux(struct parport *port, int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 unsigned char s;
350 int rc;
351
Matthew Martind32ccc42006-10-03 23:25:14 +0200352 parport_data_forward(port);
353 parport_write_data(port, 0xaa); udelay(2);
354 parport_write_data(port, 0x55); udelay(2);
355 parport_write_data(port, 0xf0); udelay(2);
356 parport_write_data(port, 0x0f); udelay(2);
357 parport_write_data(port, 0x52); udelay(2);
358 parport_write_data(port, 0xad); udelay(2);
359 parport_write_data(port, cmd); udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Matthew Martind32ccc42006-10-03 23:25:14 +0200361 s = parport_read_status(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (!(s & PARPORT_STATUS_ACK)) {
Joe Perches88c5cbd2020-04-03 14:43:18 +0100363 pr_debug("%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 port->name, cmd, s);
365 return -EIO;
366 }
367
368 rc = (((s & PARPORT_STATUS_SELECT ? 1 : 0) << 0) |
369 ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
370 ((s & PARPORT_STATUS_BUSY ? 0 : 1) << 2) |
371 ((s & PARPORT_STATUS_ERROR ? 0 : 1) << 3));
372
373 return rc;
374}
375
Matthew Martind32ccc42006-10-03 23:25:14 +0200376void parport_daisy_deselect_all(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Matthew Martind32ccc42006-10-03 23:25:14 +0200378 cpp_daisy(port, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Matthew Martind32ccc42006-10-03 23:25:14 +0200381int parport_daisy_select(struct parport *port, int daisy, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 switch (mode)
384 {
385 // For these modes we should switch to EPP mode:
386 case IEEE1284_MODE_EPP:
387 case IEEE1284_MODE_EPPSL:
388 case IEEE1284_MODE_EPPSWE:
Matthew Martind32ccc42006-10-03 23:25:14 +0200389 return !(cpp_daisy(port, 0x20 + daisy) &
Marko Kohtala7c9cc3b2006-01-06 00:19:46 -0800390 PARPORT_STATUS_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 // For these modes we should switch to ECP mode:
393 case IEEE1284_MODE_ECP:
394 case IEEE1284_MODE_ECPRLE:
395 case IEEE1284_MODE_ECPSWE:
Matthew Martind32ccc42006-10-03 23:25:14 +0200396 return !(cpp_daisy(port, 0xd0 + daisy) &
Marko Kohtala7c9cc3b2006-01-06 00:19:46 -0800397 PARPORT_STATUS_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 // Nothing was told for BECP in Daisy chain specification.
400 // May be it's wise to use ECP?
401 case IEEE1284_MODE_BECP:
402 // Others use compat mode
403 case IEEE1284_MODE_NIBBLE:
404 case IEEE1284_MODE_BYTE:
405 case IEEE1284_MODE_COMPAT:
406 default:
Matthew Martind32ccc42006-10-03 23:25:14 +0200407 return !(cpp_daisy(port, 0xe0 + daisy) &
Marko Kohtala7c9cc3b2006-01-06 00:19:46 -0800408 PARPORT_STATUS_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410}
411
Matthew Martind32ccc42006-10-03 23:25:14 +0200412static int mux_present(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Matthew Martind32ccc42006-10-03 23:25:14 +0200414 return cpp_mux(port, 0x51) == 3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Matthew Martind32ccc42006-10-03 23:25:14 +0200417static int num_mux_ports(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Matthew Martind32ccc42006-10-03 23:25:14 +0200419 return cpp_mux(port, 0x58);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Matthew Martind32ccc42006-10-03 23:25:14 +0200422static int select_port(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 int muxport = port->muxport;
Matthew Martind32ccc42006-10-03 23:25:14 +0200425 return cpp_mux(port, 0x60 + muxport) == muxport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Matthew Martind32ccc42006-10-03 23:25:14 +0200428static int assign_addrs(struct parport *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Marko Kohtala310c8c32006-01-06 00:19:45 -0800430 unsigned char s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned char daisy;
432 int thisdev = numdevs;
433 int detected;
434 char *deviceid;
435
Matthew Martind32ccc42006-10-03 23:25:14 +0200436 parport_data_forward(port);
437 parport_write_data(port, 0xaa); udelay(2);
438 parport_write_data(port, 0x55); udelay(2);
439 parport_write_data(port, 0x00); udelay(2);
440 parport_write_data(port, 0xff); udelay(2);
441 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 | PARPORT_STATUS_PAPEROUT
443 | PARPORT_STATUS_SELECT
444 | PARPORT_STATUS_ERROR);
445 if (s != (PARPORT_STATUS_BUSY
446 | PARPORT_STATUS_PAPEROUT
447 | PARPORT_STATUS_SELECT
448 | PARPORT_STATUS_ERROR)) {
Joe Perches88c5cbd2020-04-03 14:43:18 +0100449 pr_debug("%s: assign_addrs: aa5500ff(%02x)\n", port->name, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 return 0;
451 }
452
Matthew Martind32ccc42006-10-03 23:25:14 +0200453 parport_write_data(port, 0x87); udelay(2);
454 s = parport_read_status(port) & (PARPORT_STATUS_BUSY
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 | PARPORT_STATUS_PAPEROUT
456 | PARPORT_STATUS_SELECT
457 | PARPORT_STATUS_ERROR);
458 if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
Joe Perches88c5cbd2020-04-03 14:43:18 +0100459 pr_debug("%s: assign_addrs: aa5500ff87(%02x)\n", port->name, s);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return 0;
461 }
462
Matthew Martind32ccc42006-10-03 23:25:14 +0200463 parport_write_data(port, 0x78); udelay(2);
464 s = parport_read_status(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Marko Kohtala310c8c32006-01-06 00:19:45 -0800466 for (daisy = 0;
467 (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
468 == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
469 && daisy < 4;
470 ++daisy) {
Matthew Martind32ccc42006-10-03 23:25:14 +0200471 parport_write_data(port, daisy);
472 udelay(2);
473 parport_frob_control(port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 PARPORT_CONTROL_STROBE,
475 PARPORT_CONTROL_STROBE);
Matthew Martind32ccc42006-10-03 23:25:14 +0200476 udelay(1);
477 parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
478 udelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Matthew Martind32ccc42006-10-03 23:25:14 +0200480 add_dev(numdevs++, port, daisy);
Marko Kohtala310c8c32006-01-06 00:19:45 -0800481
482 /* See if this device thought it was the last in the
483 * chain. */
484 if (!(s & PARPORT_STATUS_BUSY))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486
Marko Kohtala310c8c32006-01-06 00:19:45 -0800487 /* We are seeing pass through status now. We see
488 last_dev from next device or if last_dev does not
489 work status lines from some non-daisy chain
490 device. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200491 s = parport_read_status(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Matthew Martind32ccc42006-10-03 23:25:14 +0200494 parport_write_data(port, 0xff); udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 detected = numdevs - thisdev;
Joe Perches88c5cbd2020-04-03 14:43:18 +0100496 pr_debug("%s: Found %d daisy-chained devices\n", port->name, detected);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
498 /* Ask the new devices to introduce themselves. */
Matthew Martind32ccc42006-10-03 23:25:14 +0200499 deviceid = kmalloc(1024, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!deviceid) return 0;
501
502 for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
Matthew Martind32ccc42006-10-03 23:25:14 +0200503 parport_device_id(thisdev, deviceid, 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Matthew Martind32ccc42006-10-03 23:25:14 +0200505 kfree(deviceid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 return detected;
507}