| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/sched.h> | 
 | 26 |  | 
 | 27 | #include <asm/current.h> | 
 | 28 | #include <asm/uaccess.h> | 
 | 29 |  | 
 | 30 | #undef DEBUG | 
 | 31 |  | 
 | 32 | #ifdef DEBUG | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 33 | #define DPRINTK(stuff...) printk(stuff) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #else | 
 | 35 | #define DPRINTK(stuff...) | 
 | 36 | #endif | 
 | 37 |  | 
 | 38 | static struct daisydev { | 
 | 39 | 	struct daisydev *next; | 
 | 40 | 	struct parport *port; | 
 | 41 | 	int daisy; | 
 | 42 | 	int devnum; | 
 | 43 | } *topology = NULL; | 
 | 44 | static DEFINE_SPINLOCK(topology_lock); | 
 | 45 |  | 
 | 46 | static int numdevs = 0; | 
 | 47 |  | 
 | 48 | /* Forward-declaration of lower-level functions. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 49 | static int mux_present(struct parport *port); | 
 | 50 | static int num_mux_ports(struct parport *port); | 
 | 51 | static int select_port(struct parport *port); | 
 | 52 | static int assign_addrs(struct parport *port); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 |  | 
 | 54 | /* Add a device to the discovered topology. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 55 | static void add_dev(int devnum, struct parport *port, int daisy) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | { | 
 | 57 | 	struct daisydev *newdev, **p; | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 58 | 	newdev = kmalloc(sizeof(struct daisydev), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | 	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 Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 73 | static struct parport *clone_parport(struct parport *real, int muxport) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 75 | 	struct parport *extra = parport_register_port(real->base, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | 						       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 Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 91 | int parport_daisy_init(struct parport *port) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | { | 
 | 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 |  | 
 | 100 | again: | 
 | 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 Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 106 | 	if (port->muxport < 0 && mux_present(port) && | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | 	    /* don't be fooled: a mux must have 2 or 4 ports. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 108 | 	    ((num_ports = num_mux_ports(port)) == 2 || num_ports == 4)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | 		/* Leave original as port zero. */ | 
 | 110 | 		port->muxport = 0; | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 111 | 		printk(KERN_INFO | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | 			"%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 Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 116 | 			struct parport *extra = clone_parport(port, i); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | 			if (!extra) { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 118 | 				if (signal_pending(current)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | 					break; | 
 | 120 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 121 | 				schedule(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | 				continue; | 
 | 123 | 			} | 
 | 124 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 125 | 			printk(KERN_INFO | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | 				"%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 Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 138 | 		select_port(port); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 140 | 	parport_daisy_deselect_all(port); | 
 | 141 | 	detected += assign_addrs(port); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 |  | 
 | 143 | 	/* Count the potential legacy device at the end. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 144 | 	add_dev(numdevs++, port, -1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 |  | 
 | 146 | 	/* Find out the legacy device's IEEE 1284 device ID. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 147 | 	deviceid = kmalloc(1024, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | 	if (deviceid) { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 149 | 		if (parport_device_id(numdevs - 1, deviceid, 1024) > 2) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | 			detected++; | 
 | 151 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 152 | 		kfree(deviceid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | 	} | 
 | 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 Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 159 | 		parport_daisy_fini(port); | 
 | 160 | 		parport_write_control(port, PARPORT_CONTROL_SELECT); | 
 | 161 | 		udelay(50); | 
 | 162 | 		parport_write_control(port, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | 				       PARPORT_CONTROL_SELECT | | 
 | 164 | 				       PARPORT_CONTROL_INIT); | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 165 | 		udelay(50); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | 		last_try = 1; | 
 | 167 | 		goto again; | 
 | 168 | 	} | 
 | 169 |  | 
 | 170 | 	return detected; | 
 | 171 | } | 
 | 172 |  | 
 | 173 | /* Forget about devices on a physical port. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 174 | void parport_daisy_fini(struct parport *port) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | { | 
 | 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 | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 |  * | 
 | 203 |  *	This function is similar to parport_register_device(), except | 
 | 204 |  *	that it locates a device by its number rather than by the port | 
 | 205 |  *	it is attached to. | 
 | 206 |  * | 
 | 207 |  *	All parameters except for @devnum are the same as for | 
 | 208 |  *	parport_register_device().  The return value is the same as | 
 | 209 |  *	for parport_register_device(). | 
 | 210 |  **/ | 
 | 211 |  | 
| Jeff Garzik | 5712cb3 | 2007-10-19 02:54:26 -0400 | [diff] [blame] | 212 | struct pardevice *parport_open(int devnum, const char *name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { | 
 | 214 | 	struct daisydev *p = topology; | 
 | 215 | 	struct parport *port; | 
 | 216 | 	struct pardevice *dev; | 
 | 217 | 	int daisy; | 
 | 218 |  | 
 | 219 | 	spin_lock(&topology_lock); | 
 | 220 | 	while (p && p->devnum != devnum) | 
 | 221 | 		p = p->next; | 
 | 222 |  | 
 | 223 | 	if (!p) { | 
 | 224 | 		spin_unlock(&topology_lock); | 
 | 225 | 		return NULL; | 
 | 226 | 	} | 
 | 227 |  | 
 | 228 | 	daisy = p->daisy; | 
 | 229 | 	port = parport_get_port(p->port); | 
 | 230 | 	spin_unlock(&topology_lock); | 
 | 231 |  | 
| Jeff Garzik | 5712cb3 | 2007-10-19 02:54:26 -0400 | [diff] [blame] | 232 | 	dev = parport_register_device(port, name, NULL, NULL, NULL, 0, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | 	parport_put_port(port); | 
 | 234 | 	if (!dev) | 
 | 235 | 		return NULL; | 
 | 236 |  | 
 | 237 | 	dev->daisy = daisy; | 
 | 238 |  | 
 | 239 | 	/* Check that there really is a device to select. */ | 
 | 240 | 	if (daisy >= 0) { | 
 | 241 | 		int selected; | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 242 | 		parport_claim_or_block(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | 		selected = port->daisy; | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 244 | 		parport_release(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 |  | 
| Marko Kohtala | c29a75e | 2006-01-06 00:19:46 -0800 | [diff] [blame] | 246 | 		if (selected != daisy) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | 			/* No corresponding device. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 248 | 			parport_unregister_device(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | 			return NULL; | 
 | 250 | 		} | 
 | 251 | 	} | 
 | 252 |  | 
 | 253 | 	return dev; | 
 | 254 | } | 
 | 255 |  | 
 | 256 | /** | 
 | 257 |  *	parport_close - close a device opened with parport_open() | 
 | 258 |  *	@dev: device to close | 
 | 259 |  * | 
 | 260 |  *	This is to parport_open() as parport_unregister_device() is to | 
 | 261 |  *	parport_register_device(). | 
 | 262 |  **/ | 
 | 263 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 264 | void parport_close(struct pardevice *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 266 | 	parport_unregister_device(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } | 
 | 268 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | /* Send a daisy-chain-style CPP command packet. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 270 | static int cpp_daisy(struct parport *port, int cmd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { | 
 | 272 | 	unsigned char s; | 
 | 273 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 274 | 	parport_data_forward(port); | 
 | 275 | 	parport_write_data(port, 0xaa); udelay(2); | 
 | 276 | 	parport_write_data(port, 0x55); udelay(2); | 
 | 277 | 	parport_write_data(port, 0x00); udelay(2); | 
 | 278 | 	parport_write_data(port, 0xff); udelay(2); | 
 | 279 | 	s = parport_read_status(port) & (PARPORT_STATUS_BUSY | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | 					  | PARPORT_STATUS_PAPEROUT | 
 | 281 | 					  | PARPORT_STATUS_SELECT | 
 | 282 | 					  | PARPORT_STATUS_ERROR); | 
 | 283 | 	if (s != (PARPORT_STATUS_BUSY | 
 | 284 | 		  | PARPORT_STATUS_PAPEROUT | 
 | 285 | 		  | PARPORT_STATUS_SELECT | 
 | 286 | 		  | PARPORT_STATUS_ERROR)) { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 287 | 		DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff(%02x)\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | 			 port->name, s); | 
 | 289 | 		return -ENXIO; | 
 | 290 | 	} | 
 | 291 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 292 | 	parport_write_data(port, 0x87); udelay(2); | 
 | 293 | 	s = parport_read_status(port) & (PARPORT_STATUS_BUSY | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | 					  | PARPORT_STATUS_PAPEROUT | 
 | 295 | 					  | PARPORT_STATUS_SELECT | 
 | 296 | 					  | PARPORT_STATUS_ERROR); | 
 | 297 | 	if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 298 | 		DPRINTK(KERN_DEBUG "%s: cpp_daisy: aa5500ff87(%02x)\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | 			 port->name, s); | 
 | 300 | 		return -ENXIO; | 
 | 301 | 	} | 
 | 302 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 303 | 	parport_write_data(port, 0x78); udelay(2); | 
 | 304 | 	parport_write_data(port, cmd); udelay(2); | 
 | 305 | 	parport_frob_control(port, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | 			      PARPORT_CONTROL_STROBE, | 
 | 307 | 			      PARPORT_CONTROL_STROBE); | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 308 | 	udelay(1); | 
 | 309 | 	s = parport_read_status(port); | 
 | 310 | 	parport_frob_control(port, PARPORT_CONTROL_STROBE, 0); | 
 | 311 | 	udelay(1); | 
 | 312 | 	parport_write_data(port, 0xff); udelay(2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 |  | 
 | 314 | 	return s; | 
 | 315 | } | 
 | 316 |  | 
 | 317 | /* Send a mux-style CPP command packet. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 318 | static int cpp_mux(struct parport *port, int cmd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | { | 
 | 320 | 	unsigned char s; | 
 | 321 | 	int rc; | 
 | 322 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 323 | 	parport_data_forward(port); | 
 | 324 | 	parport_write_data(port, 0xaa); udelay(2); | 
 | 325 | 	parport_write_data(port, 0x55); udelay(2); | 
 | 326 | 	parport_write_data(port, 0xf0); udelay(2); | 
 | 327 | 	parport_write_data(port, 0x0f); udelay(2); | 
 | 328 | 	parport_write_data(port, 0x52); udelay(2); | 
 | 329 | 	parport_write_data(port, 0xad); udelay(2); | 
 | 330 | 	parport_write_data(port, cmd); udelay(2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 332 | 	s = parport_read_status(port); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | 	if (!(s & PARPORT_STATUS_ACK)) { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 334 | 		DPRINTK(KERN_DEBUG "%s: cpp_mux: aa55f00f52ad%02x(%02x)\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | 			 port->name, cmd, s); | 
 | 336 | 		return -EIO; | 
 | 337 | 	} | 
 | 338 |  | 
 | 339 | 	rc = (((s & PARPORT_STATUS_SELECT   ? 1 : 0) << 0) | | 
 | 340 | 	      ((s & PARPORT_STATUS_PAPEROUT ? 1 : 0) << 1) | | 
 | 341 | 	      ((s & PARPORT_STATUS_BUSY     ? 0 : 1) << 2) | | 
 | 342 | 	      ((s & PARPORT_STATUS_ERROR    ? 0 : 1) << 3)); | 
 | 343 |  | 
 | 344 | 	return rc; | 
 | 345 | } | 
 | 346 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 347 | void parport_daisy_deselect_all(struct parport *port) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 349 | 	cpp_daisy(port, 0x30); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } | 
 | 351 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 352 | int parport_daisy_select(struct parport *port, int daisy, int mode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { | 
 | 354 | 	switch (mode) | 
 | 355 | 	{ | 
 | 356 | 		// For these modes we should switch to EPP mode: | 
 | 357 | 		case IEEE1284_MODE_EPP: | 
 | 358 | 		case IEEE1284_MODE_EPPSL: | 
 | 359 | 		case IEEE1284_MODE_EPPSWE: | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 360 | 			return !(cpp_daisy(port, 0x20 + daisy) & | 
| Marko Kohtala | 7c9cc3b | 2006-01-06 00:19:46 -0800 | [diff] [blame] | 361 | 				 PARPORT_STATUS_ERROR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 |  | 
 | 363 | 		// For these modes we should switch to ECP mode: | 
 | 364 | 		case IEEE1284_MODE_ECP: | 
 | 365 | 		case IEEE1284_MODE_ECPRLE: | 
 | 366 | 		case IEEE1284_MODE_ECPSWE:  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 367 | 			return !(cpp_daisy(port, 0xd0 + daisy) & | 
| Marko Kohtala | 7c9cc3b | 2006-01-06 00:19:46 -0800 | [diff] [blame] | 368 | 				 PARPORT_STATUS_ERROR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 |  | 
 | 370 | 		// Nothing was told for BECP in Daisy chain specification. | 
 | 371 | 		// May be it's wise to use ECP? | 
 | 372 | 		case IEEE1284_MODE_BECP: | 
 | 373 | 		// Others use compat mode | 
 | 374 | 		case IEEE1284_MODE_NIBBLE: | 
 | 375 | 		case IEEE1284_MODE_BYTE: | 
 | 376 | 		case IEEE1284_MODE_COMPAT: | 
 | 377 | 		default: | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 378 | 			return !(cpp_daisy(port, 0xe0 + daisy) & | 
| Marko Kohtala | 7c9cc3b | 2006-01-06 00:19:46 -0800 | [diff] [blame] | 379 | 				 PARPORT_STATUS_ERROR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | 	} | 
 | 381 | } | 
 | 382 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 383 | static int mux_present(struct parport *port) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 385 | 	return cpp_mux(port, 0x51) == 3; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | } | 
 | 387 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 388 | static int num_mux_ports(struct parport *port) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 390 | 	return cpp_mux(port, 0x58); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } | 
 | 392 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 393 | static int select_port(struct parport *port) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { | 
 | 395 | 	int muxport = port->muxport; | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 396 | 	return cpp_mux(port, 0x60 + muxport) == muxport; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } | 
 | 398 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 399 | static int assign_addrs(struct parport *port) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | { | 
| Marko Kohtala | 310c8c3 | 2006-01-06 00:19:45 -0800 | [diff] [blame] | 401 | 	unsigned char s; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | 	unsigned char daisy; | 
 | 403 | 	int thisdev = numdevs; | 
 | 404 | 	int detected; | 
 | 405 | 	char *deviceid; | 
 | 406 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 407 | 	parport_data_forward(port); | 
 | 408 | 	parport_write_data(port, 0xaa); udelay(2); | 
 | 409 | 	parport_write_data(port, 0x55); udelay(2); | 
 | 410 | 	parport_write_data(port, 0x00); udelay(2); | 
 | 411 | 	parport_write_data(port, 0xff); udelay(2); | 
 | 412 | 	s = parport_read_status(port) & (PARPORT_STATUS_BUSY | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | 					  | PARPORT_STATUS_PAPEROUT | 
 | 414 | 					  | PARPORT_STATUS_SELECT | 
 | 415 | 					  | PARPORT_STATUS_ERROR); | 
 | 416 | 	if (s != (PARPORT_STATUS_BUSY | 
 | 417 | 		  | PARPORT_STATUS_PAPEROUT | 
 | 418 | 		  | PARPORT_STATUS_SELECT | 
 | 419 | 		  | PARPORT_STATUS_ERROR)) { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 420 | 		DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff(%02x)\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | 			 port->name, s); | 
 | 422 | 		return 0; | 
 | 423 | 	} | 
 | 424 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 425 | 	parport_write_data(port, 0x87); udelay(2); | 
 | 426 | 	s = parport_read_status(port) & (PARPORT_STATUS_BUSY | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | 					  | PARPORT_STATUS_PAPEROUT | 
 | 428 | 					  | PARPORT_STATUS_SELECT | 
 | 429 | 					  | PARPORT_STATUS_ERROR); | 
 | 430 | 	if (s != (PARPORT_STATUS_SELECT | PARPORT_STATUS_ERROR)) { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 431 | 		DPRINTK(KERN_DEBUG "%s: assign_addrs: aa5500ff87(%02x)\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | 			 port->name, s); | 
 | 433 | 		return 0; | 
 | 434 | 	} | 
 | 435 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 436 | 	parport_write_data(port, 0x78); udelay(2); | 
 | 437 | 	s = parport_read_status(port); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 |  | 
| Marko Kohtala | 310c8c3 | 2006-01-06 00:19:45 -0800 | [diff] [blame] | 439 | 	for (daisy = 0; | 
 | 440 | 	     (s & (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT)) | 
 | 441 | 		     == (PARPORT_STATUS_PAPEROUT|PARPORT_STATUS_SELECT) | 
 | 442 | 		     && daisy < 4; | 
 | 443 | 	     ++daisy) { | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 444 | 		parport_write_data(port, daisy); | 
 | 445 | 		udelay(2); | 
 | 446 | 		parport_frob_control(port, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | 				      PARPORT_CONTROL_STROBE, | 
 | 448 | 				      PARPORT_CONTROL_STROBE); | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 449 | 		udelay(1); | 
 | 450 | 		parport_frob_control(port, PARPORT_CONTROL_STROBE, 0); | 
 | 451 | 		udelay(1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 453 | 		add_dev(numdevs++, port, daisy); | 
| Marko Kohtala | 310c8c3 | 2006-01-06 00:19:45 -0800 | [diff] [blame] | 454 |  | 
 | 455 | 		/* See if this device thought it was the last in the | 
 | 456 | 		 * chain. */ | 
 | 457 | 		if (!(s & PARPORT_STATUS_BUSY)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | 			break; | 
 | 459 |  | 
| Marko Kohtala | 310c8c3 | 2006-01-06 00:19:45 -0800 | [diff] [blame] | 460 | 		/* We are seeing pass through status now. We see | 
 | 461 | 		   last_dev from next device or if last_dev does not | 
 | 462 | 		   work status lines from some non-daisy chain | 
 | 463 | 		   device. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 464 | 		s = parport_read_status(port); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | 	} | 
 | 466 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 467 | 	parport_write_data(port, 0xff); udelay(2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | 	detected = numdevs - thisdev; | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 469 | 	DPRINTK(KERN_DEBUG "%s: Found %d daisy-chained devices\n", port->name, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | 		 detected); | 
 | 471 |  | 
 | 472 | 	/* Ask the new devices to introduce themselves. */ | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 473 | 	deviceid = kmalloc(1024, GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | 	if (!deviceid) return 0; | 
 | 475 |  | 
 | 476 | 	for (daisy = 0; thisdev < numdevs; thisdev++, daisy++) | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 477 | 		parport_device_id(thisdev, deviceid, 1024); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 |  | 
| Matthew Martin | d32ccc4 | 2006-10-03 23:25:14 +0200 | [diff] [blame] | 479 | 	kfree(deviceid); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | 	return detected; | 
 | 481 | } |