daisy.c 12.5 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 * IEEE 1284.3 Parallel port daisy chain and multiplexor code
 * 
 * Copyright (C) 1999, 2000  Tim Waugh <tim@cyberelk.demon.co.uk>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 * ??-12-1998: Initial implementation.
 * 31-01-1999: Make port-cloning transparent.
 * 13-02-1999: Move DeviceID technique from parport_probe.
 * 13-03-1999: Get DeviceID from non-IEEE 1284.3 devices too.
 * 22-02-2000: Count devices that are actually detected.
 *
 * Any part of this program may be used in documents licensed under
 * the GNU Free Documentation License, Version 1.1 or any later version
 * published by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/parport.h>
#include <linux/delay.h>
#include <linux/sched.h>

#include <asm/current.h>
#include <asm/uaccess.h>

#undef DEBUG

#ifdef DEBUG
33
#define DPRINTK(stuff...) printk(stuff)
L
Linus Torvalds 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#else
#define DPRINTK(stuff...)
#endif

static struct daisydev {
	struct daisydev *next;
	struct parport *port;
	int daisy;
	int devnum;
} *topology = NULL;
static DEFINE_SPINLOCK(topology_lock);

static int numdevs = 0;

/* Forward-declaration of lower-level functions. */
49 50 51 52
static int mux_present(struct parport *port);
static int num_mux_ports(struct parport *port);
static int select_port(struct parport *port);
static int assign_addrs(struct parport *port);
L
Linus Torvalds 已提交
53 54

/* Add a device to the discovered topology. */
55
static void add_dev(int devnum, struct parport *port, int daisy)
L
Linus Torvalds 已提交
56 57
{
	struct daisydev *newdev, **p;
58
	newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL);
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72
	if (newdev) {
		newdev->port = port;
		newdev->daisy = daisy;
		newdev->devnum = devnum;
		spin_lock(&topology_lock);
		for (p = &topology; *p && (*p)->devnum<devnum; p = &(*p)->next)
			;
		newdev->next = *p;
		*p = newdev;
		spin_unlock(&topology_lock);
	}
}

/* Clone a parport (actually, make an alias). */
73
static struct parport *clone_parport(struct parport *real, int muxport)
L
Linus Torvalds 已提交
74
{
75
	struct parport *extra = parport_register_port(real->base,
L
Linus Torvalds 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
						       real->irq,
						       real->dma,
						       real->ops);
	if (extra) {
		extra->portnum = real->portnum;
		extra->physport = real;
		extra->muxport = muxport;
		real->slaves[muxport-1] = extra;
	}

	return extra;
}

/* Discover the IEEE1284.3 topology on a port -- muxes and daisy chains.
 * Return value is number of devices actually detected. */
91
int parport_daisy_init(struct parport *port)
L
Linus Torvalds 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105
{
	int detected = 0;
	char *deviceid;
	static const char *th[] = { /*0*/"th", "st", "nd", "rd", "th" };
	int num_ports;
	int i;
	int last_try = 0;

again:
	/* Because this is called before any other devices exist,
	 * we don't have to claim exclusive access.  */

	/* If mux present on normal port, need to create new
	 * parports for each extra port. */
106
	if (port->muxport < 0 && mux_present(port) &&
L
Linus Torvalds 已提交
107
	    /* don't be fooled: a mux must have 2 or 4 ports. */
108
	    ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) {
L
Linus Torvalds 已提交
109 110
		/* Leave original as port zero. */
		port->muxport = 0;
111
		printk(KERN_INFO
L
Linus Torvalds 已提交
112 113 114 115
			"%s: 1st (default) port of %d-way multiplexor\n",
			port->name, num_ports);
		for (i = 1; i < num_ports; i++) {
			/* Clone the port. */
116
			struct parport *extra = clone_parport(port, i);
L
Linus Torvalds 已提交
117
			if (!extra) {
118
				if (signal_pending(current))
L
Linus Torvalds 已提交
119 120
					break;

121
				schedule();
L
Linus Torvalds 已提交
122 123 124
				continue;
			}

125
			printk(KERN_INFO
L
Linus Torvalds 已提交
126 127 128 129 130 131 132 133 134 135 136 137
				"%s: %d%s port of %d-way multiplexor on %s\n",
				extra->name, i + 1, th[i + 1], num_ports,
				port->name);

			/* Analyse that port too.  We won't recurse
			   forever because of the 'port->muxport < 0'
			   test above. */
			parport_daisy_init(extra);
		}
	}

	if (port->muxport >= 0)
138
		select_port(port);
L
Linus Torvalds 已提交
139

140 141
	parport_daisy_deselect_all(port);
	detected += assign_addrs(port);
L
Linus Torvalds 已提交
142 143

	/* Count the potential legacy device at the end. */
144
	add_dev(numdevs++, port, -1);
L
Linus Torvalds 已提交
145 146

	/* Find out the legacy device's IEEE 1284 device ID. */
147
	deviceid = kmalloc(1024, GFP_KERNEL);
L
Linus Torvalds 已提交
148
	if (deviceid) {
149
		if (parport_device_id(numdevs - 1, deviceid, 1024) > 2)
L
Linus Torvalds 已提交
150 151
			detected++;

152
		kfree(deviceid);
L
Linus Torvalds 已提交
153 154 155 156 157 158
	}

	if (!detected && !last_try) {
		/* No devices were detected.  Perhaps they are in some
                   funny state; let's try to reset them and see if
                   they wake up. */
159 160 161 162
		parport_daisy_fini(port);
		parport_write_control(port, PARPORT_CONTROL_SELECT);
		udelay(50);
		parport_write_control(port,
L
Linus Torvalds 已提交
163 164
				       PARPORT_CONTROL_SELECT |
				       PARPORT_CONTROL_INIT);
165
		udelay(50);
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173
		last_try = 1;
		goto again;
	}

	return detected;
}

/* Forget about devices on a physical port. */
174
void parport_daisy_fini(struct parport *port)
L
Linus Torvalds 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
{
	struct daisydev **p;

	spin_lock(&topology_lock);
	p = &topology;
	while (*p) {
		struct daisydev *dev = *p;
		if (dev->port != port) {
			p = &dev->next;
			continue;
		}
		*p = dev->next;
		kfree(dev);
	}

	/* Gaps in the numbering could be handled better.  How should
           someone enumerate through all IEEE1284.3 devices in the
           topology?. */
	if (!topology) numdevs = 0;
	spin_unlock(&topology_lock);
	return;
}

/**
 *	parport_open - find a device by canonical device number
 *	@devnum: canonical device number
 *	@name: name to associate with the device
 *	@pf: preemption callback
 *	@kf: kick callback
 *	@irqf: interrupt handler
 *	@flags: registration flags
 *	@handle: driver data
 *
 *	This function is similar to parport_register_device(), except
 *	that it locates a device by its number rather than by the port
 *	it is attached to.
 *
 *	All parameters except for @devnum are the same as for
 *	parport_register_device().  The return value is the same as
 *	for parport_register_device().
 **/

217
struct pardevice *parport_open(int devnum, const char *name,
L
Linus Torvalds 已提交
218
				int (*pf) (void *), void (*kf) (void *),
219
				void (*irqf) (int, void *),
L
Linus Torvalds 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
				int flags, void *handle)
{
	struct daisydev *p = topology;
	struct parport *port;
	struct pardevice *dev;
	int daisy;

	spin_lock(&topology_lock);
	while (p && p->devnum != devnum)
		p = p->next;

	if (!p) {
		spin_unlock(&topology_lock);
		return NULL;
	}

	daisy = p->daisy;
	port = parport_get_port(p->port);
	spin_unlock(&topology_lock);

240
	dev = parport_register_device(port, name, pf, kf,
L
Linus Torvalds 已提交
241 242 243 244 245 246 247 248 249 250
				       irqf, flags, handle);
	parport_put_port(port);
	if (!dev)
		return NULL;

	dev->daisy = daisy;

	/* Check that there really is a device to select. */
	if (daisy >= 0) {
		int selected;
251
		parport_claim_or_block(dev);
L
Linus Torvalds 已提交
252
		selected = port->daisy;
253
		parport_release(dev);
L
Linus Torvalds 已提交
254

255
		if (selected != daisy) {
L
Linus Torvalds 已提交
256
			/* No corresponding device. */
257
			parport_unregister_device(dev);
L
Linus Torvalds 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
			return NULL;
		}
	}

	return dev;
}

/**
 *	parport_close - close a device opened with parport_open()
 *	@dev: device to close
 *
 *	This is to parport_open() as parport_unregister_device() is to
 *	parport_register_device().
 **/

273
void parport_close(struct pardevice *dev)
L
Linus Torvalds 已提交
274
{
275
	parport_unregister_device(dev);
L
Linus Torvalds 已提交
276 277 278
}

/* Send a daisy-chain-style CPP command packet. */
279
static int cpp_daisy(struct parport *port, int cmd)
L
Linus Torvalds 已提交
280 281 282
{
	unsigned char s;

283 284 285 286 287 288
	parport_data_forward(port);
	parport_write_data(port, 0xaa); udelay(2);
	parport_write_data(port, 0x55); udelay(2);
	parport_write_data(port, 0x00); udelay(2);
	parport_write_data(port, 0xff); udelay(2);
	s = parport_read_status(port) & (PARPORT_STATUS_BUSY
L
Linus Torvalds 已提交
289 290 291 292 293 294 295
					  | PARPORT_STATUS_PAPEROUT
					  | PARPORT_STATUS_SELECT
					  | PARPORT_STATUS_ERROR);
	if (s != (PARPORT_STATUS_BUSY
		  | PARPORT_STATUS_PAPEROUT
		  | PARPORT_STATUS_SELECT
		  | PARPORT_STATUS_ERROR)) {
296
		DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff(%02x)\n",
L
Linus Torvalds 已提交
297 298 299 300
			 port->name, s);
		return -ENXIO;
	}

301 302
	parport_write_data(port, 0x87); udelay(2);
	s = parport_read_status(port) & (PARPORT_STATUS_BUSY
L
Linus Torvalds 已提交
303 304 305 306
					  | PARPORT_STATUS_PAPEROUT
					  | PARPORT_STATUS_SELECT
					  | PARPORT_STATUS_ERROR);
	if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
307
		DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff87(%02x)\n",
L
Linus Torvalds 已提交
308 309 310 311
			 port->name, s);
		return -ENXIO;
	}

312 313 314
	parport_write_data(port, 0x78); udelay(2);
	parport_write_data(port, cmd); udelay(2);
	parport_frob_control(port,
L
Linus Torvalds 已提交
315 316
			      PARPORT_CONTROL_STROBE,
			      PARPORT_CONTROL_STROBE);
317 318 319 320 321
	udelay(1);
	s = parport_read_status(port);
	parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
	udelay(1);
	parport_write_data(port, 0xff); udelay(2);
L
Linus Torvalds 已提交
322 323 324 325 326

	return s;
}

/* Send a mux-style CPP command packet. */
327
static int cpp_mux(struct parport *port, int cmd)
L
Linus Torvalds 已提交
328 329 330 331
{
	unsigned char s;
	int rc;

332 333 334 335 336 337 338 339
	parport_data_forward(port);
	parport_write_data(port, 0xaa); udelay(2);
	parport_write_data(port, 0x55); udelay(2);
	parport_write_data(port, 0xf0); udelay(2);
	parport_write_data(port, 0x0f); udelay(2);
	parport_write_data(port, 0x52); udelay(2);
	parport_write_data(port, 0xad); udelay(2);
	parport_write_data(port, cmd); udelay(2);
L
Linus Torvalds 已提交
340

341
	s = parport_read_status(port);
L
Linus Torvalds 已提交
342
	if (!(s & PARPORT_STATUS_ACK)) {
343
		DPRINTK(KERN_DEBUG "%s: cpp_mux: aa55f00f52ad%02x(%02x)\n",
L
Linus Torvalds 已提交
344 345 346 347 348 349 350 351 352 353 354 355
			 port->name, cmd, s);
		return -EIO;
	}

	rc = (((s & PARPORT_STATUS_SELECT   ? 1 : 0) << 0) |
	      ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) |
	      ((s & PARPORT_STATUS_BUSY     ? 0 : 1) << 2) |
	      ((s & PARPORT_STATUS_ERROR    ? 0 : 1) << 3));

	return rc;
}

356
void parport_daisy_deselect_all(struct parport *port)
L
Linus Torvalds 已提交
357
{
358
	cpp_daisy(port, 0x30);
L
Linus Torvalds 已提交
359 360
}

361
int parport_daisy_select(struct parport *port, int daisy, int mode)
L
Linus Torvalds 已提交
362 363 364 365 366 367 368
{
	switch (mode)
	{
		// For these modes we should switch to EPP mode:
		case IEEE1284_MODE_EPP:
		case IEEE1284_MODE_EPPSL:
		case IEEE1284_MODE_EPPSWE:
369
			return !(cpp_daisy(port, 0x20 + daisy) &
370
				 PARPORT_STATUS_ERROR);
L
Linus Torvalds 已提交
371 372 373 374 375

		// For these modes we should switch to ECP mode:
		case IEEE1284_MODE_ECP:
		case IEEE1284_MODE_ECPRLE:
		case IEEE1284_MODE_ECPSWE: 
376
			return !(cpp_daisy(port, 0xd0 + daisy) &
377
				 PARPORT_STATUS_ERROR);
L
Linus Torvalds 已提交
378 379 380 381 382 383 384 385 386

		// Nothing was told for BECP in Daisy chain specification.
		// May be it's wise to use ECP?
		case IEEE1284_MODE_BECP:
		// Others use compat mode
		case IEEE1284_MODE_NIBBLE:
		case IEEE1284_MODE_BYTE:
		case IEEE1284_MODE_COMPAT:
		default:
387
			return !(cpp_daisy(port, 0xe0 + daisy) &
388
				 PARPORT_STATUS_ERROR);
L
Linus Torvalds 已提交
389 390 391
	}
}

392
static int mux_present(struct parport *port)
L
Linus Torvalds 已提交
393
{
394
	return cpp_mux(port, 0x51) == 3;
L
Linus Torvalds 已提交
395 396
}

397
static int num_mux_ports(struct parport *port)
L
Linus Torvalds 已提交
398
{
399
	return cpp_mux(port, 0x58);
L
Linus Torvalds 已提交
400 401
}

402
static int select_port(struct parport *port)
L
Linus Torvalds 已提交
403 404
{
	int muxport = port->muxport;
405
	return cpp_mux(port, 0x60 + muxport) == muxport;
L
Linus Torvalds 已提交
406 407
}

408
static int assign_addrs(struct parport *port)
L
Linus Torvalds 已提交
409
{
410
	unsigned char s;
L
Linus Torvalds 已提交
411 412 413 414 415
	unsigned char daisy;
	int thisdev = numdevs;
	int detected;
	char *deviceid;

416 417 418 419 420 421
	parport_data_forward(port);
	parport_write_data(port, 0xaa); udelay(2);
	parport_write_data(port, 0x55); udelay(2);
	parport_write_data(port, 0x00); udelay(2);
	parport_write_data(port, 0xff); udelay(2);
	s = parport_read_status(port) & (PARPORT_STATUS_BUSY
L
Linus Torvalds 已提交
422 423 424 425 426 427 428
					  | PARPORT_STATUS_PAPEROUT
					  | PARPORT_STATUS_SELECT
					  | PARPORT_STATUS_ERROR);
	if (s != (PARPORT_STATUS_BUSY
		  | PARPORT_STATUS_PAPEROUT
		  | PARPORT_STATUS_SELECT
		  | PARPORT_STATUS_ERROR)) {
429
		DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff(%02x)\n",
L
Linus Torvalds 已提交
430 431 432 433
			 port->name, s);
		return 0;
	}

434 435
	parport_write_data(port, 0x87); udelay(2);
	s = parport_read_status(port) & (PARPORT_STATUS_BUSY
L
Linus Torvalds 已提交
436 437 438 439
					  | PARPORT_STATUS_PAPEROUT
					  | PARPORT_STATUS_SELECT
					  | PARPORT_STATUS_ERROR);
	if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) {
440
		DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff87(%02x)\n",
L
Linus Torvalds 已提交
441 442 443 444
			 port->name, s);
		return 0;
	}

445 446
	parport_write_data(port, 0x78); udelay(2);
	s = parport_read_status(port);
L
Linus Torvalds 已提交
447

448 449 450 451 452
	for (daisy = 0;
	     (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT))
		     == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)
		     && daisy < 4;
	     ++daisy) {
453 454 455
		parport_write_data(port, daisy);
		udelay(2);
		parport_frob_control(port,
L
Linus Torvalds 已提交
456 457
				      PARPORT_CONTROL_STROBE,
				      PARPORT_CONTROL_STROBE);
458 459 460
		udelay(1);
		parport_frob_control(port, PARPORT_CONTROL_STROBE, 0);
		udelay(1);
L
Linus Torvalds 已提交
461

462
		add_dev(numdevs++, port, daisy);
L
Linus Torvalds 已提交
463

464 465 466 467
		/* See if this device thought it was the last in the
		 * chain. */
		if (!(s & PARPORT_STATUS_BUSY))
			break;
L
Linus Torvalds 已提交
468

469 470 471 472
		/* We are seeing pass through status now. We see
		   last_dev from next device or if last_dev does not
		   work status lines from some non-daisy chain
		   device. */
473
		s = parport_read_status(port);
L
Linus Torvalds 已提交
474 475
	}

476
	parport_write_data(port, 0xff); udelay(2);
L
Linus Torvalds 已提交
477
	detected = numdevs - thisdev;
478
	DPRINTK(KERN_DEBUG "%s: Found %d daisy-chained devices\n", port->name,
L
Linus Torvalds 已提交
479 480 481
		 detected);

	/* Ask the new devices to introduce themselves. */
482
	deviceid = kmalloc(1024, GFP_KERNEL);
L
Linus Torvalds 已提交
483 484 485
	if (!deviceid) return 0;

	for (daisy = 0; thisdev < numdevs; thisdev++, daisy++)
486
		parport_device_id(thisdev, deviceid, 1024);
L
Linus Torvalds 已提交
487

488
	kfree(deviceid);
L
Linus Torvalds 已提交
489 490
	return detected;
}