xref: /linux/drivers/parport/daisy.c (revision 55d0969c451159cff86949b38c39171cab962069)
1 /*
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/slab.h>
26 #include <linux/sched/signal.h>
27 
28 #include <asm/current.h>
29 #include <linux/uaccess.h>
30 
31 #undef DEBUG
32 
33 static struct daisydev {
34 	struct daisydev *next;
35 	struct parport *port;
36 	int daisy;
37 	int devnum;
38 } *topology = NULL;
39 static DEFINE_SPINLOCK(topology_lock);
40 
41 static int numdevs;
42 static bool daisy_init_done;
43 
44 /* Forward-declaration of lower-level functions. */
45 static int mux_present(struct parport *port);
46 static int num_mux_ports(struct parport *port);
47 static int select_port(struct parport *port);
48 static int assign_addrs(struct parport *port);
49 
50 /* Add a device to the discovered topology. */
51 static void add_dev(int devnum, struct parport *port, int daisy)
52 {
53 	struct daisydev *newdev, **p;
54 	newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
55 	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). */
69 static struct parport *clone_parport(struct parport *real, int muxport)
70 {
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;
79 		real->slaves[muxport-1] = extra;
80 	}
81 
82 	return extra;
83 }
84 
85 static 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 
97 static struct parport_driver daisy_driver = {
98 	.name = "daisy_drv",
99 	.probe = daisy_drv_probe,
100 };
101 
102 /* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains.
103  * Return value is number of devices actually detected. */
104 int parport_daisy_init(struct parport *port)
105 {
106 	int detected = 0;
107 	char *deviceid;
108 	static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
109 	int num_ports;
110 	int i;
111 	int last_try = 0;
112 
113 	if (!daisy_init_done) {
114 		/*
115 		 * flag should be marked true first as
116 		 * parport_register_driver() might try to load the low
117 		 * level driver which will lead to announcing new ports
118 		 * and which will again come back here at
119 		 * parport_daisy_init()
120 		 */
121 		daisy_init_done = true;
122 		i = parport_register_driver(&daisy_driver);
123 		if (i) {
124 			pr_err("daisy registration failed\n");
125 			daisy_init_done = false;
126 			return i;
127 		}
128 	}
129 
130 again:
131 	/* Because this is called before any other devices exist,
132 	 * we don't have to claim exclusive access.  */
133 
134 	/* If mux present on normal port, need to create new
135 	 * parports for each extra port. */
136 	if (port->muxport < 0 && mux_present(port) &&
137 	    /* don't be fooled: a mux must have 2 or 4 ports. */
138 	    ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
139 		/* Leave original as port zero. */
140 		port->muxport = 0;
141 		pr_info("%s: 1st (default) port of %d-way multiplexor\n",
142 			port->name, num_ports);
143 		for (i = 1; i < num_ports; i++) {
144 			/* Clone the port. */
145 			struct parport *extra = clone_parport(port, i);
146 			if (!extra) {
147 				if (signal_pending(current))
148 					break;
149 
150 				schedule();
151 				continue;
152 			}
153 
154 			pr_info("%s: %d%s port of %d-way multiplexor on %s\n",
155 				extra->name, i + 1, th[i + 1], num_ports,
156 				port->name);
157 
158 			/* Analyse that port too.  We won't recurse
159 			   forever because of the 'port->muxport < 0'
160 			   test above. */
161 			parport_daisy_init(extra);
162 		}
163 	}
164 
165 	if (port->muxport >= 0)
166 		select_port(port);
167 
168 	parport_daisy_deselect_all(port);
169 	detected += assign_addrs(port);
170 
171 	/* Count the potential legacy device at the end. */
172 	add_dev(numdevs++, port, -1);
173 
174 	/* Find out the legacy device's IEEE 1284 device ID. */
175 	deviceid = kmalloc(1024, GFP_KERNEL);
176 	if (deviceid) {
177 		if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
178 			detected++;
179 
180 		kfree(deviceid);
181 	}
182 
183 	if (!detected && !last_try) {
184 		/* No devices were detected.  Perhaps they are in some
185                    funny state; let's try to reset them and see if
186                    they wake up. */
187 		parport_daisy_fini(port);
188 		parport_write_control(port, PARPORT_CONTROL_SELECT);
189 		udelay(50);
190 		parport_write_control(port,
191 				       PARPORT_CONTROL_SELECT |
192 				       PARPORT_CONTROL_INIT);
193 		udelay(50);
194 		last_try = 1;
195 		goto again;
196 	}
197 
198 	return detected;
199 }
200 
201 /* Forget about devices on a physical port. */
202 void parport_daisy_fini(struct parport *port)
203 {
204 	struct daisydev **p;
205 
206 	spin_lock(&topology_lock);
207 	p = &topology;
208 	while (*p) {
209 		struct daisydev *dev = *p;
210 		if (dev->port != port) {
211 			p = &dev->next;
212 			continue;
213 		}
214 		*p = dev->next;
215 		kfree(dev);
216 	}
217 
218 	/* Gaps in the numbering could be handled better.  How should
219            someone enumerate through all IEEE1284.3 devices in the
220            topology?. */
221 	if (!topology) numdevs = 0;
222 	spin_unlock(&topology_lock);
223 	return;
224 }
225 
226 /**
227  *	parport_open - find a device by canonical device number
228  *	@devnum: canonical device number
229  *	@name: name to associate with the device
230  *
231  *	This function is similar to parport_register_device(), except
232  *	that it locates a device by its number rather than by the port
233  *	it is attached to.
234  *
235  *	All parameters except for @devnum are the same as for
236  *	parport_register_device().  The return value is the same as
237  *	for parport_register_device().
238  **/
239 
240 struct pardevice *parport_open(int devnum, const char *name)
241 {
242 	struct daisydev *p = topology;
243 	struct pardev_cb par_cb;
244 	struct parport *port;
245 	struct pardevice *dev;
246 	int daisy;
247 
248 	memset(&par_cb, 0, sizeof(par_cb));
249 	spin_lock(&topology_lock);
250 	while (p && p->devnum != devnum)
251 		p = p->next;
252 
253 	if (!p) {
254 		spin_unlock(&topology_lock);
255 		return NULL;
256 	}
257 
258 	daisy = p->daisy;
259 	port = parport_get_port(p->port);
260 	spin_unlock(&topology_lock);
261 
262 	dev = parport_register_dev_model(port, name, &par_cb, devnum);
263 	parport_put_port(port);
264 	if (!dev)
265 		return NULL;
266 
267 	dev->daisy = daisy;
268 
269 	/* Check that there really is a device to select. */
270 	if (daisy >= 0) {
271 		int selected;
272 		parport_claim_or_block(dev);
273 		selected = port->daisy;
274 		parport_release(dev);
275 
276 		if (selected != daisy) {
277 			/* No corresponding device. */
278 			parport_unregister_device(dev);
279 			return NULL;
280 		}
281 	}
282 
283 	return dev;
284 }
285 
286 /**
287  *	parport_close - close a device opened with parport_open()
288  *	@dev: device to close
289  *
290  *	This is to parport_open() as parport_unregister_device() is to
291  *	parport_register_device().
292  **/
293 
294 void parport_close(struct pardevice *dev)
295 {
296 	parport_unregister_device(dev);
297 }
298 
299 /* Send a daisy-chain-style CPP command packet. */
300 static int cpp_daisy(struct parport *port, int cmd)
301 {
302 	unsigned char s;
303 
304 	parport_data_forward(port);
305 	parport_write_data(port, 0xaa); udelay(2);
306 	parport_write_data(port, 0x55); udelay(2);
307 	parport_write_data(port, 0x00); udelay(2);
308 	parport_write_data(port, 0xff); udelay(2);
309 	s = parport_read_status(port) & (PARPORT_STATUS_BUSY
310 					  | PARPORT_STATUS_PAPEROUT
311 					  | PARPORT_STATUS_SELECT
312 					  | PARPORT_STATUS_ERROR);
313 	if (s != (PARPORT_STATUS_BUSY
314 		  | PARPORT_STATUS_PAPEROUT
315 		  | PARPORT_STATUS_SELECT
316 		  | PARPORT_STATUS_ERROR)) {
317 		pr_debug("%s: cpp_daisy: aa5500ff(%02x)\n", port->name, s);
318 		return -ENXIO;
319 	}
320 
321 	parport_write_data(port, 0x87); udelay(2);
322 	s = parport_read_status(port) & (PARPORT_STATUS_BUSY
323 					  | PARPORT_STATUS_PAPEROUT
324 					  | PARPORT_STATUS_SELECT
325 					  | PARPORT_STATUS_ERROR);
326 	if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
327 		pr_debug("%s: cpp_daisy: aa5500ff87(%02x)\n", port->name, s);
328 		return -ENXIO;
329 	}
330 
331 	parport_write_data(port, 0x78); udelay(2);
332 	parport_write_data(port, cmd); udelay(2);
333 	parport_frob_control(port,
334 			      PARPORT_CONTROL_STROBE,
335 			      PARPORT_CONTROL_STROBE);
336 	udelay(1);
337 	s = parport_read_status(port);
338 	parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
339 	udelay(1);
340 	parport_write_data(port, 0xff); udelay(2);
341 
342 	return s;
343 }
344 
345 /* Send a mux-style CPP command packet. */
346 static int cpp_mux(struct parport *port, int cmd)
347 {
348 	unsigned char s;
349 	int rc;
350 
351 	parport_data_forward(port);
352 	parport_write_data(port, 0xaa); udelay(2);
353 	parport_write_data(port, 0x55); udelay(2);
354 	parport_write_data(port, 0xf0); udelay(2);
355 	parport_write_data(port, 0x0f); udelay(2);
356 	parport_write_data(port, 0x52); udelay(2);
357 	parport_write_data(port, 0xad); udelay(2);
358 	parport_write_data(port, cmd); udelay(2);
359 
360 	s = parport_read_status(port);
361 	if (!(s & PARPORT_STATUS_ACK)) {
362 		pr_debug("%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
363 			 port->name, cmd, s);
364 		return -EIO;
365 	}
366 
367 	rc = (((s & PARPORT_STATUS_SELECT   ? 1 : 0) << 0) |
368 	      ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
369 	      ((s & PARPORT_STATUS_BUSY     ? 0 : 1) << 2) |
370 	      ((s & PARPORT_STATUS_ERROR    ? 0 : 1) << 3));
371 
372 	return rc;
373 }
374 
375 void parport_daisy_deselect_all(struct parport *port)
376 {
377 	cpp_daisy(port, 0x30);
378 }
379 
380 int parport_daisy_select(struct parport *port, int daisy, int mode)
381 {
382 	switch (mode)
383 	{
384 		// For these modes we should switch to EPP mode:
385 		case IEEE1284_MODE_EPP:
386 		case IEEE1284_MODE_EPPSL:
387 		case IEEE1284_MODE_EPPSWE:
388 			return !(cpp_daisy(port, 0x20 + daisy) &
389 				 PARPORT_STATUS_ERROR);
390 
391 		// For these modes we should switch to ECP mode:
392 		case IEEE1284_MODE_ECP:
393 		case IEEE1284_MODE_ECPRLE:
394 		case IEEE1284_MODE_ECPSWE:
395 			return !(cpp_daisy(port, 0xd0 + daisy) &
396 				 PARPORT_STATUS_ERROR);
397 
398 		// Nothing was told for BECP in Daisy chain specification.
399 		// May be it's wise to use ECP?
400 		case IEEE1284_MODE_BECP:
401 		// Others use compat mode
402 		case IEEE1284_MODE_NIBBLE:
403 		case IEEE1284_MODE_BYTE:
404 		case IEEE1284_MODE_COMPAT:
405 		default:
406 			return !(cpp_daisy(port, 0xe0 + daisy) &
407 				 PARPORT_STATUS_ERROR);
408 	}
409 }
410 
411 static int mux_present(struct parport *port)
412 {
413 	return cpp_mux(port, 0x51) == 3;
414 }
415 
416 static int num_mux_ports(struct parport *port)
417 {
418 	return cpp_mux(port, 0x58);
419 }
420 
421 static int select_port(struct parport *port)
422 {
423 	int muxport = port->muxport;
424 	return cpp_mux(port, 0x60 + muxport) == muxport;
425 }
426 
427 static int assign_addrs(struct parport *port)
428 {
429 	unsigned char s;
430 	unsigned char daisy;
431 	int thisdev = numdevs;
432 	int detected;
433 	char *deviceid;
434 
435 	parport_data_forward(port);
436 	parport_write_data(port, 0xaa); udelay(2);
437 	parport_write_data(port, 0x55); udelay(2);
438 	parport_write_data(port, 0x00); udelay(2);
439 	parport_write_data(port, 0xff); udelay(2);
440 	s = parport_read_status(port) & (PARPORT_STATUS_BUSY
441 					  | PARPORT_STATUS_PAPEROUT
442 					  | PARPORT_STATUS_SELECT
443 					  | PARPORT_STATUS_ERROR);
444 	if (s != (PARPORT_STATUS_BUSY
445 		  | PARPORT_STATUS_PAPEROUT
446 		  | PARPORT_STATUS_SELECT
447 		  | PARPORT_STATUS_ERROR)) {
448 		pr_debug("%s: assign_addrs: aa5500ff(%02x)\n", port->name, s);
449 		return 0;
450 	}
451 
452 	parport_write_data(port, 0x87); udelay(2);
453 	s = parport_read_status(port) & (PARPORT_STATUS_BUSY
454 					  | PARPORT_STATUS_PAPEROUT
455 					  | PARPORT_STATUS_SELECT
456 					  | PARPORT_STATUS_ERROR);
457 	if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
458 		pr_debug("%s: assign_addrs: aa5500ff87(%02x)\n", port->name, s);
459 		return 0;
460 	}
461 
462 	parport_write_data(port, 0x78); udelay(2);
463 	s = parport_read_status(port);
464 
465 	for (daisy = 0;
466 	     (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
467 		     == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
468 		     && daisy < 4;
469 	     ++daisy) {
470 		parport_write_data(port, daisy);
471 		udelay(2);
472 		parport_frob_control(port,
473 				      PARPORT_CONTROL_STROBE,
474 				      PARPORT_CONTROL_STROBE);
475 		udelay(1);
476 		parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
477 		udelay(1);
478 
479 		add_dev(numdevs++, port, daisy);
480 
481 		/* See if this device thought it was the last in the
482 		 * chain. */
483 		if (!(s & PARPORT_STATUS_BUSY))
484 			break;
485 
486 		/* We are seeing pass through status now. We see
487 		   last_dev from next device or if last_dev does not
488 		   work status lines from some non-daisy chain
489 		   device. */
490 		s = parport_read_status(port);
491 	}
492 
493 	parport_write_data(port, 0xff); udelay(2);
494 	detected = numdevs - thisdev;
495 	pr_debug("%s: Found %d daisy-chained devices\n", port->name, detected);
496 
497 	/* Ask the new devices to introduce themselves. */
498 	deviceid = kmalloc(1024, GFP_KERNEL);
499 	if (!deviceid) return 0;
500 
501 	for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
502 		parport_device_id(thisdev, deviceid, 1024);
503 
504 	kfree(deviceid);
505 	return detected;
506 }
507