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