xref: /openbmc/linux/drivers/parport/share.c (revision 6824f0ce)
1612de10dSAdrian Bunk /*
21da177e4SLinus Torvalds  * Parallel-port resource manager code.
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Authors: David Campbell <campbell@tirian.che.curtin.edu.au>
51da177e4SLinus Torvalds  *          Tim Waugh <tim@cyberelk.demon.co.uk>
61da177e4SLinus Torvalds  *          Jose Renau <renau@acm.org>
71da177e4SLinus Torvalds  *          Philip Blundell <philb@gnu.org>
81da177e4SLinus Torvalds  *	    Andrea Arcangeli
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * based on work by Grant Guenther <grant@torque.net>
111da177e4SLinus Torvalds  *          and Philip Blundell
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * Any part of this program may be used in documents licensed under
141da177e4SLinus Torvalds  * the GNU Free Documentation License, Version 1.1 or any later version
151da177e4SLinus Torvalds  * published by the Free Software Foundation.
161da177e4SLinus Torvalds  */
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #undef PARPORT_DEBUG_SHARING		/* undef for production */
191da177e4SLinus Torvalds 
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/string.h>
221da177e4SLinus Torvalds #include <linux/threads.h>
231da177e4SLinus Torvalds #include <linux/parport.h>
241da177e4SLinus Torvalds #include <linux/delay.h>
251da177e4SLinus Torvalds #include <linux/errno.h>
261da177e4SLinus Torvalds #include <linux/interrupt.h>
271da177e4SLinus Torvalds #include <linux/ioport.h>
281da177e4SLinus Torvalds #include <linux/kernel.h>
291da177e4SLinus Torvalds #include <linux/slab.h>
30174cd4b1SIngo Molnar #include <linux/sched/signal.h>
311da177e4SLinus Torvalds #include <linux/kmod.h>
326fa45a22SSudip Mukherjee #include <linux/device.h>
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #include <linux/spinlock.h>
3514cc3e2bSIngo Molnar #include <linux/mutex.h>
361da177e4SLinus Torvalds #include <asm/irq.h>
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #undef PARPORT_PARANOID
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #define PARPORT_DEFAULT_TIMESLICE	(HZ/5)
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds unsigned long parport_default_timeslice = PARPORT_DEFAULT_TIMESLICE;
431da177e4SLinus Torvalds int parport_default_spintime =  DEFAULT_SPIN_TIME;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds static LIST_HEAD(portlist);
461da177e4SLinus Torvalds static DEFINE_SPINLOCK(parportlist_lock);
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /* list of all allocated ports, sorted by ->number */
491da177e4SLinus Torvalds static LIST_HEAD(all_ports);
501da177e4SLinus Torvalds static DEFINE_SPINLOCK(full_list_lock);
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static LIST_HEAD(drivers);
531da177e4SLinus Torvalds 
5414cc3e2bSIngo Molnar static DEFINE_MUTEX(registration_lock);
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /* What you can do to a port that's gone away.. */
571da177e4SLinus Torvalds static void dead_write_lines(struct parport *p, unsigned char b){}
581da177e4SLinus Torvalds static unsigned char dead_read_lines(struct parport *p) { return 0; }
591da177e4SLinus Torvalds static unsigned char dead_frob_lines(struct parport *p, unsigned char b,
601da177e4SLinus Torvalds 			     unsigned char c) { return 0; }
611da177e4SLinus Torvalds static void dead_onearg(struct parport *p){}
621da177e4SLinus Torvalds static void dead_initstate(struct pardevice *d, struct parport_state *s) { }
631da177e4SLinus Torvalds static void dead_state(struct parport *p, struct parport_state *s) { }
641da177e4SLinus Torvalds static size_t dead_write(struct parport *p, const void *b, size_t l, int f)
651da177e4SLinus Torvalds { return 0; }
661da177e4SLinus Torvalds static size_t dead_read(struct parport *p, void *b, size_t l, int f)
671da177e4SLinus Torvalds { return 0; }
681da177e4SLinus Torvalds static struct parport_operations dead_ops = {
691da177e4SLinus Torvalds 	.write_data	= dead_write_lines,	/* data */
701da177e4SLinus Torvalds 	.read_data	= dead_read_lines,
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	.write_control	= dead_write_lines,	/* control */
731da177e4SLinus Torvalds 	.read_control	= dead_read_lines,
741da177e4SLinus Torvalds 	.frob_control	= dead_frob_lines,
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	.read_status	= dead_read_lines,	/* status */
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	.enable_irq	= dead_onearg,		/* enable_irq */
791da177e4SLinus Torvalds 	.disable_irq	= dead_onearg,		/* disable_irq */
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds 	.data_forward	= dead_onearg,		/* data_forward */
821da177e4SLinus Torvalds 	.data_reverse	= dead_onearg,		/* data_reverse */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	.init_state	= dead_initstate,	/* init_state */
851da177e4SLinus Torvalds 	.save_state	= dead_state,
861da177e4SLinus Torvalds 	.restore_state	= dead_state,
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds 	.epp_write_data	= dead_write,		/* epp */
891da177e4SLinus Torvalds 	.epp_read_data	= dead_read,
901da177e4SLinus Torvalds 	.epp_write_addr	= dead_write,
911da177e4SLinus Torvalds 	.epp_read_addr	= dead_read,
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	.ecp_write_data	= dead_write,		/* ecp */
941da177e4SLinus Torvalds 	.ecp_read_data	= dead_read,
951da177e4SLinus Torvalds 	.ecp_write_addr	= dead_write,
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	.compat_write_data	= dead_write,	/* compat */
981da177e4SLinus Torvalds 	.nibble_read_data	= dead_read,	/* nibble */
991da177e4SLinus Torvalds 	.byte_read_data		= dead_read,	/* byte */
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	.owner		= NULL,
1021da177e4SLinus Torvalds };
1031da177e4SLinus Torvalds 
1046fa45a22SSudip Mukherjee static struct device_type parport_device_type = {
1056fa45a22SSudip Mukherjee 	.name = "parport",
1066fa45a22SSudip Mukherjee };
1076fa45a22SSudip Mukherjee 
1086fa45a22SSudip Mukherjee static int is_parport(struct device *dev)
1096fa45a22SSudip Mukherjee {
1106fa45a22SSudip Mukherjee 	return dev->type == &parport_device_type;
1116fa45a22SSudip Mukherjee }
1126fa45a22SSudip Mukherjee 
1136fa45a22SSudip Mukherjee static int parport_probe(struct device *dev)
1146fa45a22SSudip Mukherjee {
1156fa45a22SSudip Mukherjee 	struct parport_driver *drv;
1166fa45a22SSudip Mukherjee 
1176fa45a22SSudip Mukherjee 	if (is_parport(dev))
1186fa45a22SSudip Mukherjee 		return -ENODEV;
1196fa45a22SSudip Mukherjee 
1206fa45a22SSudip Mukherjee 	drv = to_parport_driver(dev->driver);
1216fa45a22SSudip Mukherjee 	if (!drv->probe) {
1226fa45a22SSudip Mukherjee 		/* if driver has not defined a custom probe */
1236fa45a22SSudip Mukherjee 		struct pardevice *par_dev = to_pardevice(dev);
1246fa45a22SSudip Mukherjee 
1256fa45a22SSudip Mukherjee 		if (strcmp(par_dev->name, drv->name))
1266fa45a22SSudip Mukherjee 			return -ENODEV;
1276fa45a22SSudip Mukherjee 		return 0;
1286fa45a22SSudip Mukherjee 	}
1296fa45a22SSudip Mukherjee 	/* if driver defined its own probe */
1306fa45a22SSudip Mukherjee 	return drv->probe(to_pardevice(dev));
1316fa45a22SSudip Mukherjee }
1326fa45a22SSudip Mukherjee 
1336fa45a22SSudip Mukherjee static struct bus_type parport_bus_type = {
1346fa45a22SSudip Mukherjee 	.name = "parport",
1356fa45a22SSudip Mukherjee 	.probe = parport_probe,
1366fa45a22SSudip Mukherjee };
1376fa45a22SSudip Mukherjee 
1386fa45a22SSudip Mukherjee int parport_bus_init(void)
1396fa45a22SSudip Mukherjee {
140a3ac7917SLinus Torvalds 	return bus_register(&parport_bus_type);
1416fa45a22SSudip Mukherjee }
1426fa45a22SSudip Mukherjee 
1436fa45a22SSudip Mukherjee void parport_bus_exit(void)
1446fa45a22SSudip Mukherjee {
1456fa45a22SSudip Mukherjee 	bus_unregister(&parport_bus_type);
1466fa45a22SSudip Mukherjee }
1476fa45a22SSudip Mukherjee 
1486fa45a22SSudip Mukherjee /*
1496fa45a22SSudip Mukherjee  * iterates through all the drivers registered with the bus and sends the port
1506fa45a22SSudip Mukherjee  * details to the match_port callback of the driver, so that the driver can
151e9527365SGeliang Tang  * know about the new port that just registered with the bus and decide if it
1526fa45a22SSudip Mukherjee  * wants to use this new port.
1536fa45a22SSudip Mukherjee  */
1546fa45a22SSudip Mukherjee static int driver_check(struct device_driver *dev_drv, void *_port)
1556fa45a22SSudip Mukherjee {
1566fa45a22SSudip Mukherjee 	struct parport *port = _port;
1576fa45a22SSudip Mukherjee 	struct parport_driver *drv = to_parport_driver(dev_drv);
1586fa45a22SSudip Mukherjee 
1596fa45a22SSudip Mukherjee 	if (drv->match_port)
1606fa45a22SSudip Mukherjee 		drv->match_port(port);
1616fa45a22SSudip Mukherjee 	return 0;
1626fa45a22SSudip Mukherjee }
1636fa45a22SSudip Mukherjee 
1641da177e4SLinus Torvalds /* Call attach(port) for each registered driver. */
1651da177e4SLinus Torvalds static void attach_driver_chain(struct parport *port)
1661da177e4SLinus Torvalds {
1671da177e4SLinus Torvalds 	/* caller has exclusive registration_lock */
1681da177e4SLinus Torvalds 	struct parport_driver *drv;
1696fa45a22SSudip Mukherjee 
1701da177e4SLinus Torvalds 	list_for_each_entry(drv, &drivers, list)
1711da177e4SLinus Torvalds 		drv->attach(port);
1726fa45a22SSudip Mukherjee 
1736fa45a22SSudip Mukherjee 	/*
1746fa45a22SSudip Mukherjee 	 * call the driver_check function of the drivers registered in
1756fa45a22SSudip Mukherjee 	 * new device model
1766fa45a22SSudip Mukherjee 	 */
1776fa45a22SSudip Mukherjee 
1786fa45a22SSudip Mukherjee 	bus_for_each_drv(&parport_bus_type, NULL, port, driver_check);
1796fa45a22SSudip Mukherjee }
1806fa45a22SSudip Mukherjee 
1816fa45a22SSudip Mukherjee static int driver_detach(struct device_driver *_drv, void *_port)
1826fa45a22SSudip Mukherjee {
1836fa45a22SSudip Mukherjee 	struct parport *port = _port;
1846fa45a22SSudip Mukherjee 	struct parport_driver *drv = to_parport_driver(_drv);
1856fa45a22SSudip Mukherjee 
1866fa45a22SSudip Mukherjee 	if (drv->detach)
1876fa45a22SSudip Mukherjee 		drv->detach(port);
1886fa45a22SSudip Mukherjee 	return 0;
1891da177e4SLinus Torvalds }
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds /* Call detach(port) for each registered driver. */
1921da177e4SLinus Torvalds static void detach_driver_chain(struct parport *port)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	struct parport_driver *drv;
1951da177e4SLinus Torvalds 	/* caller has exclusive registration_lock */
1961da177e4SLinus Torvalds 	list_for_each_entry(drv, &drivers, list)
1971da177e4SLinus Torvalds 		drv->detach(port);
1986fa45a22SSudip Mukherjee 
1996fa45a22SSudip Mukherjee 	/*
2006fa45a22SSudip Mukherjee 	 * call the detach function of the drivers registered in
2016fa45a22SSudip Mukherjee 	 * new device model
2026fa45a22SSudip Mukherjee 	 */
2036fa45a22SSudip Mukherjee 
2046fa45a22SSudip Mukherjee 	bus_for_each_drv(&parport_bus_type, NULL, port, driver_detach);
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /* Ask kmod for some lowlevel drivers. */
2081da177e4SLinus Torvalds static void get_lowlevel_driver(void)
2091da177e4SLinus Torvalds {
2107b7a0a30SSudip Mukherjee 	/*
2117b7a0a30SSudip Mukherjee 	 * There is no actual module called this: you should set
2127b7a0a30SSudip Mukherjee 	 * up an alias for modutils.
2137b7a0a30SSudip Mukherjee 	 */
2141da177e4SLinus Torvalds 	request_module("parport_lowlevel");
2151da177e4SLinus Torvalds }
2161da177e4SLinus Torvalds 
2176fa45a22SSudip Mukherjee /*
2186fa45a22SSudip Mukherjee  * iterates through all the devices connected to the bus and sends the device
2196fa45a22SSudip Mukherjee  * details to the match_port callback of the driver, so that the driver can
2206fa45a22SSudip Mukherjee  * know what are all the ports that are connected to the bus and choose the
2216fa45a22SSudip Mukherjee  * port to which it wants to register its device.
2226fa45a22SSudip Mukherjee  */
2236fa45a22SSudip Mukherjee static int port_check(struct device *dev, void *dev_drv)
2246fa45a22SSudip Mukherjee {
2256fa45a22SSudip Mukherjee 	struct parport_driver *drv = dev_drv;
2266fa45a22SSudip Mukherjee 
2276fa45a22SSudip Mukherjee 	/* only send ports, do not send other devices connected to bus */
2286fa45a22SSudip Mukherjee 	if (is_parport(dev))
2296fa45a22SSudip Mukherjee 		drv->match_port(to_parport_dev(dev));
2306fa45a22SSudip Mukherjee 	return 0;
2316fa45a22SSudip Mukherjee }
2326fa45a22SSudip Mukherjee 
233231ec2f2SSudip Mukherjee /*
234231ec2f2SSudip Mukherjee  * Iterates through all the devices connected to the bus and return 1
235231ec2f2SSudip Mukherjee  * if the device is a parallel port.
236231ec2f2SSudip Mukherjee  */
237231ec2f2SSudip Mukherjee 
238231ec2f2SSudip Mukherjee static int port_detect(struct device *dev, void *dev_drv)
239231ec2f2SSudip Mukherjee {
240231ec2f2SSudip Mukherjee 	if (is_parport(dev))
241231ec2f2SSudip Mukherjee 		return 1;
242231ec2f2SSudip Mukherjee 	return 0;
243231ec2f2SSudip Mukherjee }
244231ec2f2SSudip Mukherjee 
2451da177e4SLinus Torvalds /**
2461da177e4SLinus Torvalds  *	parport_register_driver - register a parallel port device driver
2471da177e4SLinus Torvalds  *	@drv: structure describing the driver
2486fa45a22SSudip Mukherjee  *	@owner: owner module of drv
2496fa45a22SSudip Mukherjee  *	@mod_name: module name string
2501da177e4SLinus Torvalds  *
2511da177e4SLinus Torvalds  *	This can be called by a parallel port device driver in order
2521da177e4SLinus Torvalds  *	to receive notifications about ports being found in the
2531da177e4SLinus Torvalds  *	system, as well as ports no longer available.
2541da177e4SLinus Torvalds  *
2556fa45a22SSudip Mukherjee  *	If devmodel is true then the new device model is used
2566fa45a22SSudip Mukherjee  *	for registration.
2576fa45a22SSudip Mukherjee  *
2581da177e4SLinus Torvalds  *	The @drv structure is allocated by the caller and must not be
2591da177e4SLinus Torvalds  *	deallocated until after calling parport_unregister_driver().
2601da177e4SLinus Torvalds  *
2616fa45a22SSudip Mukherjee  *	If using the non device model:
2621da177e4SLinus Torvalds  *	The driver's attach() function may block.  The port that
2631da177e4SLinus Torvalds  *	attach() is given will be valid for the duration of the
2641da177e4SLinus Torvalds  *	callback, but if the driver wants to take a copy of the
2651da177e4SLinus Torvalds  *	pointer it must call parport_get_port() to do so.  Calling
2661da177e4SLinus Torvalds  *	parport_register_device() on that port will do this for you.
2671da177e4SLinus Torvalds  *
2681da177e4SLinus Torvalds  *	The driver's detach() function may block.  The port that
2691da177e4SLinus Torvalds  *	detach() is given will be valid for the duration of the
2701da177e4SLinus Torvalds  *	callback, but if the driver wants to take a copy of the
2711da177e4SLinus Torvalds  *	pointer it must call parport_get_port() to do so.
2721da177e4SLinus Torvalds  *
2736fa45a22SSudip Mukherjee  *
2746fa45a22SSudip Mukherjee  *	Returns 0 on success. The non device model will always succeeds.
2756fa45a22SSudip Mukherjee  *	but the new device model can fail and will return the error code.
2761da177e4SLinus Torvalds  **/
2771da177e4SLinus Torvalds 
2786fa45a22SSudip Mukherjee int __parport_register_driver(struct parport_driver *drv, struct module *owner,
2796fa45a22SSudip Mukherjee 			      const char *mod_name)
2801da177e4SLinus Torvalds {
2816fa45a22SSudip Mukherjee 	if (drv->devmodel) {
2826fa45a22SSudip Mukherjee 		/* using device model */
2836fa45a22SSudip Mukherjee 		int ret;
2846fa45a22SSudip Mukherjee 
2856fa45a22SSudip Mukherjee 		/* initialize common driver fields */
2866fa45a22SSudip Mukherjee 		drv->driver.name = drv->name;
2876fa45a22SSudip Mukherjee 		drv->driver.bus = &parport_bus_type;
2886fa45a22SSudip Mukherjee 		drv->driver.owner = owner;
2896fa45a22SSudip Mukherjee 		drv->driver.mod_name = mod_name;
2906fa45a22SSudip Mukherjee 		ret = driver_register(&drv->driver);
2916fa45a22SSudip Mukherjee 		if (ret)
2926fa45a22SSudip Mukherjee 			return ret;
2936fa45a22SSudip Mukherjee 
294231ec2f2SSudip Mukherjee 		/*
295231ec2f2SSudip Mukherjee 		 * check if bus has any parallel port registered, if
296231ec2f2SSudip Mukherjee 		 * none is found then load the lowlevel driver.
297231ec2f2SSudip Mukherjee 		 */
298231ec2f2SSudip Mukherjee 		ret = bus_for_each_dev(&parport_bus_type, NULL, NULL,
299231ec2f2SSudip Mukherjee 				       port_detect);
300231ec2f2SSudip Mukherjee 		if (!ret)
301231ec2f2SSudip Mukherjee 			get_lowlevel_driver();
302231ec2f2SSudip Mukherjee 
3036fa45a22SSudip Mukherjee 		mutex_lock(&registration_lock);
3046fa45a22SSudip Mukherjee 		if (drv->match_port)
3056fa45a22SSudip Mukherjee 			bus_for_each_dev(&parport_bus_type, NULL, drv,
3066fa45a22SSudip Mukherjee 					 port_check);
3076fa45a22SSudip Mukherjee 		mutex_unlock(&registration_lock);
3086fa45a22SSudip Mukherjee 	} else {
3096fa45a22SSudip Mukherjee 		struct parport *port;
3106fa45a22SSudip Mukherjee 
3116fa45a22SSudip Mukherjee 		drv->devmodel = false;
3126fa45a22SSudip Mukherjee 
313e962cd9cSSudip Mukherjee 		if (list_empty(&portlist))
314e962cd9cSSudip Mukherjee 			get_lowlevel_driver();
31514cc3e2bSIngo Molnar 		mutex_lock(&registration_lock);
3161da177e4SLinus Torvalds 		list_for_each_entry(port, &portlist, list)
3171da177e4SLinus Torvalds 			drv->attach(port);
3181da177e4SLinus Torvalds 		list_add(&drv->list, &drivers);
31914cc3e2bSIngo Molnar 		mutex_unlock(&registration_lock);
3206fa45a22SSudip Mukherjee 	}
3216fa45a22SSudip Mukherjee 
3226fa45a22SSudip Mukherjee 	return 0;
3236fa45a22SSudip Mukherjee }
3246fa45a22SSudip Mukherjee EXPORT_SYMBOL(__parport_register_driver);
3256fa45a22SSudip Mukherjee 
3266fa45a22SSudip Mukherjee static int port_detach(struct device *dev, void *_drv)
3276fa45a22SSudip Mukherjee {
3286fa45a22SSudip Mukherjee 	struct parport_driver *drv = _drv;
3296fa45a22SSudip Mukherjee 
3306fa45a22SSudip Mukherjee 	if (is_parport(dev) && drv->detach)
3316fa45a22SSudip Mukherjee 		drv->detach(to_parport_dev(dev));
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds 	return 0;
3341da177e4SLinus Torvalds }
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds /**
3371da177e4SLinus Torvalds  *	parport_unregister_driver - deregister a parallel port device driver
3381da177e4SLinus Torvalds  *	@drv: structure describing the driver that was given to
3391da177e4SLinus Torvalds  *	      parport_register_driver()
3401da177e4SLinus Torvalds  *
3411da177e4SLinus Torvalds  *	This should be called by a parallel port device driver that
3421da177e4SLinus Torvalds  *	has registered itself using parport_register_driver() when it
3431da177e4SLinus Torvalds  *	is about to be unloaded.
3441da177e4SLinus Torvalds  *
3451da177e4SLinus Torvalds  *	When it returns, the driver's attach() routine will no longer
3461da177e4SLinus Torvalds  *	be called, and for each port that attach() was called for, the
3471da177e4SLinus Torvalds  *	detach() routine will have been called.
3481da177e4SLinus Torvalds  *
3491da177e4SLinus Torvalds  *	All the driver's attach() and detach() calls are guaranteed to have
3501da177e4SLinus Torvalds  *	finished by the time this function returns.
3511da177e4SLinus Torvalds  **/
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds void parport_unregister_driver(struct parport_driver *drv)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	struct parport *port;
3561da177e4SLinus Torvalds 
35714cc3e2bSIngo Molnar 	mutex_lock(&registration_lock);
3586fa45a22SSudip Mukherjee 	if (drv->devmodel) {
3596fa45a22SSudip Mukherjee 		bus_for_each_dev(&parport_bus_type, NULL, drv, port_detach);
3606fa45a22SSudip Mukherjee 		driver_unregister(&drv->driver);
3616fa45a22SSudip Mukherjee 	} else {
3621da177e4SLinus Torvalds 		list_del_init(&drv->list);
3631da177e4SLinus Torvalds 		list_for_each_entry(port, &portlist, list)
3641da177e4SLinus Torvalds 			drv->detach(port);
3656fa45a22SSudip Mukherjee 	}
36614cc3e2bSIngo Molnar 	mutex_unlock(&registration_lock);
3671da177e4SLinus Torvalds }
36827c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_unregister_driver);
3691da177e4SLinus Torvalds 
3706fa45a22SSudip Mukherjee static void free_port(struct device *dev)
3711da177e4SLinus Torvalds {
3721da177e4SLinus Torvalds 	int d;
3736fa45a22SSudip Mukherjee 	struct parport *port = to_parport_dev(dev);
3746fa45a22SSudip Mukherjee 
3751da177e4SLinus Torvalds 	spin_lock(&full_list_lock);
3761da177e4SLinus Torvalds 	list_del(&port->full_list);
3771da177e4SLinus Torvalds 	spin_unlock(&full_list_lock);
3781da177e4SLinus Torvalds 	for (d = 0; d < 5; d++) {
3791da177e4SLinus Torvalds 		kfree(port->probe_info[d].class_name);
3801da177e4SLinus Torvalds 		kfree(port->probe_info[d].mfr);
3811da177e4SLinus Torvalds 		kfree(port->probe_info[d].model);
3821da177e4SLinus Torvalds 		kfree(port->probe_info[d].cmdset);
3831da177e4SLinus Torvalds 		kfree(port->probe_info[d].description);
3841da177e4SLinus Torvalds 	}
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds 	kfree(port->name);
3871da177e4SLinus Torvalds 	kfree(port);
3881da177e4SLinus Torvalds }
3891da177e4SLinus Torvalds 
3901da177e4SLinus Torvalds /**
3911da177e4SLinus Torvalds  *	parport_get_port - increment a port's reference count
3921da177e4SLinus Torvalds  *	@port: the port
3931da177e4SLinus Torvalds  *
3947d469722SRandy Dunlap  *	This ensures that a struct parport pointer remains valid
3951da177e4SLinus Torvalds  *	until the matching parport_put_port() call.
3961da177e4SLinus Torvalds  **/
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds struct parport *parport_get_port(struct parport *port)
3991da177e4SLinus Torvalds {
4006fa45a22SSudip Mukherjee 	struct device *dev = get_device(&port->bus_dev);
4016fa45a22SSudip Mukherjee 
4026fa45a22SSudip Mukherjee 	return to_parport_dev(dev);
4031da177e4SLinus Torvalds }
40427c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_get_port);
4051da177e4SLinus Torvalds 
4066fa45a22SSudip Mukherjee void parport_del_port(struct parport *port)
4076fa45a22SSudip Mukherjee {
4086fa45a22SSudip Mukherjee 	device_unregister(&port->bus_dev);
4096fa45a22SSudip Mukherjee }
4106fa45a22SSudip Mukherjee EXPORT_SYMBOL(parport_del_port);
4116fa45a22SSudip Mukherjee 
4121da177e4SLinus Torvalds /**
4131da177e4SLinus Torvalds  *	parport_put_port - decrement a port's reference count
4141da177e4SLinus Torvalds  *	@port: the port
4151da177e4SLinus Torvalds  *
4161da177e4SLinus Torvalds  *	This should be called once for each call to parport_get_port(),
4176fa45a22SSudip Mukherjee  *	once the port is no longer needed. When the reference count reaches
4186fa45a22SSudip Mukherjee  *	zero (port is no longer used), free_port is called.
4191da177e4SLinus Torvalds  **/
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds void parport_put_port(struct parport *port)
4221da177e4SLinus Torvalds {
4236fa45a22SSudip Mukherjee 	put_device(&port->bus_dev);
4241da177e4SLinus Torvalds }
42527c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_put_port);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds /**
4281da177e4SLinus Torvalds  *	parport_register_port - register a parallel port
4291da177e4SLinus Torvalds  *	@base: base I/O address
4301da177e4SLinus Torvalds  *	@irq: IRQ line
4311da177e4SLinus Torvalds  *	@dma: DMA channel
4321da177e4SLinus Torvalds  *	@ops: pointer to the port driver's port operations structure
4331da177e4SLinus Torvalds  *
4341da177e4SLinus Torvalds  *	When a parallel port (lowlevel) driver finds a port that
4351da177e4SLinus Torvalds  *	should be made available to parallel port device drivers, it
4361da177e4SLinus Torvalds  *	should call parport_register_port().  The @base, @irq, and
4371da177e4SLinus Torvalds  *	@dma parameters are for the convenience of port drivers, and
4381da177e4SLinus Torvalds  *	for ports where they aren't meaningful needn't be set to
4391da177e4SLinus Torvalds  *	anything special.  They can be altered afterwards by adjusting
4401da177e4SLinus Torvalds  *	the relevant members of the parport structure that is returned
4411da177e4SLinus Torvalds  *	and represents the port.  They should not be tampered with
4421da177e4SLinus Torvalds  *	after calling parport_announce_port, however.
4431da177e4SLinus Torvalds  *
4441da177e4SLinus Torvalds  *	If there are parallel port device drivers in the system that
4451da177e4SLinus Torvalds  *	have registered themselves using parport_register_driver(),
4461da177e4SLinus Torvalds  *	they are not told about the port at this time; that is done by
4471da177e4SLinus Torvalds  *	parport_announce_port().
4481da177e4SLinus Torvalds  *
4491da177e4SLinus Torvalds  *	The @ops structure is allocated by the caller, and must not be
4501da177e4SLinus Torvalds  *	deallocated before calling parport_remove_port().
4511da177e4SLinus Torvalds  *
4521da177e4SLinus Torvalds  *	If there is no memory to allocate a new parport structure,
4531da177e4SLinus Torvalds  *	this function will return %NULL.
4541da177e4SLinus Torvalds  **/
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds struct parport *parport_register_port(unsigned long base, int irq, int dma,
4571da177e4SLinus Torvalds 				      struct parport_operations *ops)
4581da177e4SLinus Torvalds {
4591da177e4SLinus Torvalds 	struct list_head *l;
4601da177e4SLinus Torvalds 	struct parport *tmp;
4611da177e4SLinus Torvalds 	int num;
4621da177e4SLinus Torvalds 	int device;
4631da177e4SLinus Torvalds 	char *name;
4646fa45a22SSudip Mukherjee 	int ret;
4651da177e4SLinus Torvalds 
466c32df4e1SAlexandru Gheorghiu 	tmp = kzalloc(sizeof(struct parport), GFP_KERNEL);
46747ec57ecSSudip Mukherjee 	if (!tmp)
4681da177e4SLinus Torvalds 		return NULL;
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	/* Init our structure */
4711da177e4SLinus Torvalds 	tmp->base = base;
4721da177e4SLinus Torvalds 	tmp->irq = irq;
4731da177e4SLinus Torvalds 	tmp->dma = dma;
4741da177e4SLinus Torvalds 	tmp->muxport = tmp->daisy = tmp->muxsel = -1;
4751da177e4SLinus Torvalds 	tmp->modes = 0;
4761da177e4SLinus Torvalds 	INIT_LIST_HEAD(&tmp->list);
4771da177e4SLinus Torvalds 	tmp->devices = tmp->cad = NULL;
4781da177e4SLinus Torvalds 	tmp->flags = 0;
4791da177e4SLinus Torvalds 	tmp->ops = ops;
4801da177e4SLinus Torvalds 	tmp->physport = tmp;
4811da177e4SLinus Torvalds 	memset(tmp->probe_info, 0, 5 * sizeof(struct parport_device_info));
4821da177e4SLinus Torvalds 	rwlock_init(&tmp->cad_lock);
4831da177e4SLinus Torvalds 	spin_lock_init(&tmp->waitlist_lock);
4841da177e4SLinus Torvalds 	spin_lock_init(&tmp->pardevice_lock);
4851da177e4SLinus Torvalds 	tmp->ieee1284.mode = IEEE1284_MODE_COMPAT;
4861da177e4SLinus Torvalds 	tmp->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
487a529f150SThomas Gleixner 	sema_init(&tmp->ieee1284.irq, 0);
4881da177e4SLinus Torvalds 	tmp->spintime = parport_default_spintime;
4891da177e4SLinus Torvalds 	atomic_set(&tmp->ref_count, 1);
4901da177e4SLinus Torvalds 	INIT_LIST_HEAD(&tmp->full_list);
4911da177e4SLinus Torvalds 
4921da177e4SLinus Torvalds 	name = kmalloc(15, GFP_KERNEL);
4931da177e4SLinus Torvalds 	if (!name) {
4941da177e4SLinus Torvalds 		kfree(tmp);
4951da177e4SLinus Torvalds 		return NULL;
4961da177e4SLinus Torvalds 	}
4971da177e4SLinus Torvalds 	/* Search for the lowest free parport number. */
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	spin_lock(&full_list_lock);
5001da177e4SLinus Torvalds 	for (l = all_ports.next, num = 0; l != &all_ports; l = l->next, num++) {
5011da177e4SLinus Torvalds 		struct parport *p = list_entry(l, struct parport, full_list);
5021da177e4SLinus Torvalds 		if (p->number != num)
5031da177e4SLinus Torvalds 			break;
5041da177e4SLinus Torvalds 	}
5051da177e4SLinus Torvalds 	tmp->portnum = tmp->number = num;
5061da177e4SLinus Torvalds 	list_add_tail(&tmp->full_list, l);
5071da177e4SLinus Torvalds 	spin_unlock(&full_list_lock);
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	/*
5101da177e4SLinus Torvalds 	 * Now that the portnum is known finish doing the Init.
5111da177e4SLinus Torvalds 	 */
5121da177e4SLinus Torvalds 	sprintf(name, "parport%d", tmp->portnum = tmp->number);
5131da177e4SLinus Torvalds 	tmp->name = name;
5146fa45a22SSudip Mukherjee 	tmp->bus_dev.bus = &parport_bus_type;
5156fa45a22SSudip Mukherjee 	tmp->bus_dev.release = free_port;
5166fa45a22SSudip Mukherjee 	dev_set_name(&tmp->bus_dev, name);
5176fa45a22SSudip Mukherjee 	tmp->bus_dev.type = &parport_device_type;
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 	for (device = 0; device < 5; device++)
5201da177e4SLinus Torvalds 		/* assume the worst */
5211da177e4SLinus Torvalds 		tmp->probe_info[device].class = PARPORT_CLASS_LEGACY;
5221da177e4SLinus Torvalds 
5231da177e4SLinus Torvalds 	tmp->waithead = tmp->waittail = NULL;
5241da177e4SLinus Torvalds 
5256fa45a22SSudip Mukherjee 	ret = device_register(&tmp->bus_dev);
5266fa45a22SSudip Mukherjee 	if (ret) {
5276fa45a22SSudip Mukherjee 		put_device(&tmp->bus_dev);
5286fa45a22SSudip Mukherjee 		return NULL;
5296fa45a22SSudip Mukherjee 	}
5306fa45a22SSudip Mukherjee 
5311da177e4SLinus Torvalds 	return tmp;
5321da177e4SLinus Torvalds }
53327c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_register_port);
5341da177e4SLinus Torvalds 
5351da177e4SLinus Torvalds /**
5361da177e4SLinus Torvalds  *	parport_announce_port - tell device drivers about a parallel port
5371da177e4SLinus Torvalds  *	@port: parallel port to announce
5381da177e4SLinus Torvalds  *
5391da177e4SLinus Torvalds  *	After a port driver has registered a parallel port with
5401da177e4SLinus Torvalds  *	parport_register_port, and performed any necessary
5411da177e4SLinus Torvalds  *	initialisation or adjustments, it should call
5421da177e4SLinus Torvalds  *	parport_announce_port() in order to notify all device drivers
5431da177e4SLinus Torvalds  *	that have called parport_register_driver().  Their attach()
5441da177e4SLinus Torvalds  *	functions will be called, with @port as the parameter.
5451da177e4SLinus Torvalds  **/
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds void parport_announce_port(struct parport *port)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	int i;
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
5521da177e4SLinus Torvalds 	/* Analyse the IEEE1284.3 topology of the port. */
5531da177e4SLinus Torvalds 	parport_daisy_init(port);
5541da177e4SLinus Torvalds #endif
5551da177e4SLinus Torvalds 
556c15a3837SDavid Brownell 	if (!port->dev)
557decf26f6SJoe Perches 		pr_warn("%s: fix this legacy no-device port driver!\n",
558c15a3837SDavid Brownell 			port->name);
559c15a3837SDavid Brownell 
5601da177e4SLinus Torvalds 	parport_proc_register(port);
56114cc3e2bSIngo Molnar 	mutex_lock(&registration_lock);
5621da177e4SLinus Torvalds 	spin_lock_irq(&parportlist_lock);
5631da177e4SLinus Torvalds 	list_add_tail(&port->list, &portlist);
5641da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
5651da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
5661da177e4SLinus Torvalds 		if (slave)
5671da177e4SLinus Torvalds 			list_add_tail(&slave->list, &portlist);
5681da177e4SLinus Torvalds 	}
5691da177e4SLinus Torvalds 	spin_unlock_irq(&parportlist_lock);
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	/* Let drivers know that new port(s) has arrived. */
5721da177e4SLinus Torvalds 	attach_driver_chain(port);
5731da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
5741da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
5751da177e4SLinus Torvalds 		if (slave)
5761da177e4SLinus Torvalds 			attach_driver_chain(slave);
5771da177e4SLinus Torvalds 	}
57814cc3e2bSIngo Molnar 	mutex_unlock(&registration_lock);
5791da177e4SLinus Torvalds }
58027c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_announce_port);
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds /**
5831da177e4SLinus Torvalds  *	parport_remove_port - deregister a parallel port
5841da177e4SLinus Torvalds  *	@port: parallel port to deregister
5851da177e4SLinus Torvalds  *
5861da177e4SLinus Torvalds  *	When a parallel port driver is forcibly unloaded, or a
5871da177e4SLinus Torvalds  *	parallel port becomes inaccessible, the port driver must call
5881da177e4SLinus Torvalds  *	this function in order to deal with device drivers that still
5891da177e4SLinus Torvalds  *	want to use it.
5901da177e4SLinus Torvalds  *
5911da177e4SLinus Torvalds  *	The parport structure associated with the port has its
5921da177e4SLinus Torvalds  *	operations structure replaced with one containing 'null'
5931da177e4SLinus Torvalds  *	operations that return errors or just don't do anything.
5941da177e4SLinus Torvalds  *
5951da177e4SLinus Torvalds  *	Any drivers that have registered themselves using
5961da177e4SLinus Torvalds  *	parport_register_driver() are notified that the port is no
5971da177e4SLinus Torvalds  *	longer accessible by having their detach() routines called
5981da177e4SLinus Torvalds  *	with @port as the parameter.
5991da177e4SLinus Torvalds  **/
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds void parport_remove_port(struct parport *port)
6021da177e4SLinus Torvalds {
6031da177e4SLinus Torvalds 	int i;
6041da177e4SLinus Torvalds 
60514cc3e2bSIngo Molnar 	mutex_lock(&registration_lock);
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds 	/* Spread the word. */
6081da177e4SLinus Torvalds 	detach_driver_chain(port);
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
6111da177e4SLinus Torvalds 	/* Forget the IEEE1284.3 topology of the port. */
6121da177e4SLinus Torvalds 	parport_daisy_fini(port);
6131da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
6141da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
6151da177e4SLinus Torvalds 		if (!slave)
6161da177e4SLinus Torvalds 			continue;
6171da177e4SLinus Torvalds 		detach_driver_chain(slave);
6181da177e4SLinus Torvalds 		parport_daisy_fini(slave);
6191da177e4SLinus Torvalds 	}
6201da177e4SLinus Torvalds #endif
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds 	port->ops = &dead_ops;
6231da177e4SLinus Torvalds 	spin_lock(&parportlist_lock);
6241da177e4SLinus Torvalds 	list_del_init(&port->list);
6251da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
6261da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
6271da177e4SLinus Torvalds 		if (slave)
6281da177e4SLinus Torvalds 			list_del_init(&slave->list);
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds 	spin_unlock(&parportlist_lock);
6311da177e4SLinus Torvalds 
63214cc3e2bSIngo Molnar 	mutex_unlock(&registration_lock);
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	parport_proc_unregister(port);
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
6371da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
6381da177e4SLinus Torvalds 		if (slave)
6391da177e4SLinus Torvalds 			parport_put_port(slave);
6401da177e4SLinus Torvalds 	}
6411da177e4SLinus Torvalds }
64227c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_remove_port);
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds /**
6451da177e4SLinus Torvalds  *	parport_register_device - register a device on a parallel port
6461da177e4SLinus Torvalds  *	@port: port to which the device is attached
6471da177e4SLinus Torvalds  *	@name: a name to refer to the device
6481da177e4SLinus Torvalds  *	@pf: preemption callback
6491da177e4SLinus Torvalds  *	@kf: kick callback (wake-up)
6501da177e4SLinus Torvalds  *	@irq_func: interrupt handler
6511da177e4SLinus Torvalds  *	@flags: registration flags
6521da177e4SLinus Torvalds  *	@handle: data for callback functions
6531da177e4SLinus Torvalds  *
6541da177e4SLinus Torvalds  *	This function, called by parallel port device drivers,
6551da177e4SLinus Torvalds  *	declares that a device is connected to a port, and tells the
6561da177e4SLinus Torvalds  *	system all it needs to know.
6571da177e4SLinus Torvalds  *
6581da177e4SLinus Torvalds  *	The @name is allocated by the caller and must not be
6591da177e4SLinus Torvalds  *	deallocated until the caller calls @parport_unregister_device
6601da177e4SLinus Torvalds  *	for that device.
6611da177e4SLinus Torvalds  *
6621da177e4SLinus Torvalds  *	The preemption callback function, @pf, is called when this
6631da177e4SLinus Torvalds  *	device driver has claimed access to the port but another
6641da177e4SLinus Torvalds  *	device driver wants to use it.  It is given @handle as its
6651da177e4SLinus Torvalds  *	parameter, and should return zero if it is willing for the
6661da177e4SLinus Torvalds  *	system to release the port to another driver on its behalf.
6671da177e4SLinus Torvalds  *	If it wants to keep control of the port it should return
6681da177e4SLinus Torvalds  *	non-zero, and no action will be taken.  It is good manners for
6691da177e4SLinus Torvalds  *	the driver to try to release the port at the earliest
6701da177e4SLinus Torvalds  *	opportunity after its preemption callback rejects a preemption
6711da177e4SLinus Torvalds  *	attempt.  Note that if a preemption callback is happy for
6721da177e4SLinus Torvalds  *	preemption to go ahead, there is no need to release the port;
6731da177e4SLinus Torvalds  *	it is done automatically.  This function may not block, as it
6741da177e4SLinus Torvalds  *	may be called from interrupt context.  If the device driver
6751da177e4SLinus Torvalds  *	does not support preemption, @pf can be %NULL.
6761da177e4SLinus Torvalds  *
6771da177e4SLinus Torvalds  *	The wake-up ("kick") callback function, @kf, is called when
6781da177e4SLinus Torvalds  *	the port is available to be claimed for exclusive access; that
6791da177e4SLinus Torvalds  *	is, parport_claim() is guaranteed to succeed when called from
6801da177e4SLinus Torvalds  *	inside the wake-up callback function.  If the driver wants to
6811da177e4SLinus Torvalds  *	claim the port it should do so; otherwise, it need not take
6821da177e4SLinus Torvalds  *	any action.  This function may not block, as it may be called
6831da177e4SLinus Torvalds  *	from interrupt context.  If the device driver does not want to
6841da177e4SLinus Torvalds  *	be explicitly invited to claim the port in this way, @kf can
6851da177e4SLinus Torvalds  *	be %NULL.
6861da177e4SLinus Torvalds  *
6871da177e4SLinus Torvalds  *	The interrupt handler, @irq_func, is called when an interrupt
6881da177e4SLinus Torvalds  *	arrives from the parallel port.  Note that if a device driver
6891da177e4SLinus Torvalds  *	wants to use interrupts it should use parport_enable_irq(),
6901da177e4SLinus Torvalds  *	and can also check the irq member of the parport structure
6911da177e4SLinus Torvalds  *	representing the port.
6921da177e4SLinus Torvalds  *
6931da177e4SLinus Torvalds  *	The parallel port (lowlevel) driver is the one that has called
6941da177e4SLinus Torvalds  *	request_irq() and whose interrupt handler is called first.
6951da177e4SLinus Torvalds  *	This handler does whatever needs to be done to the hardware to
6961da177e4SLinus Torvalds  *	acknowledge the interrupt (for PC-style ports there is nothing
6971da177e4SLinus Torvalds  *	special to be done).  It then tells the IEEE 1284 code about
6981da177e4SLinus Torvalds  *	the interrupt, which may involve reacting to an IEEE 1284
6991da177e4SLinus Torvalds  *	event depending on the current IEEE 1284 phase.  After this,
7001da177e4SLinus Torvalds  *	it calls @irq_func.  Needless to say, @irq_func will be called
7011da177e4SLinus Torvalds  *	from interrupt context, and may not block.
7021da177e4SLinus Torvalds  *
7031da177e4SLinus Torvalds  *	The %PARPORT_DEV_EXCL flag is for preventing port sharing, and
7041da177e4SLinus Torvalds  *	so should only be used when sharing the port with other device
7051da177e4SLinus Torvalds  *	drivers is impossible and would lead to incorrect behaviour.
7061da177e4SLinus Torvalds  *	Use it sparingly!  Normally, @flags will be zero.
7071da177e4SLinus Torvalds  *
7081da177e4SLinus Torvalds  *	This function returns a pointer to a structure that represents
7091da177e4SLinus Torvalds  *	the device on the port, or %NULL if there is not enough memory
7101da177e4SLinus Torvalds  *	to allocate space for that structure.
7111da177e4SLinus Torvalds  **/
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds struct pardevice *
7141da177e4SLinus Torvalds parport_register_device(struct parport *port, const char *name,
7151da177e4SLinus Torvalds 			int (*pf)(void *), void (*kf)(void *),
7165712cb3dSJeff Garzik 			void (*irq_func)(void *),
7171da177e4SLinus Torvalds 			int flags, void *handle)
7181da177e4SLinus Torvalds {
7191da177e4SLinus Torvalds 	struct pardevice *tmp;
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	if (port->physport->flags & PARPORT_FLAG_EXCL) {
7221da177e4SLinus Torvalds 		/* An exclusive device is registered. */
723aa3d6e7cSJoe Perches 		printk(KERN_DEBUG "%s: no more devices allowed\n", port->name);
7241da177e4SLinus Torvalds 		return NULL;
7251da177e4SLinus Torvalds 	}
7261da177e4SLinus Torvalds 
7271da177e4SLinus Torvalds 	if (flags & PARPORT_DEV_LURK) {
7281da177e4SLinus Torvalds 		if (!pf || !kf) {
729decf26f6SJoe Perches 			pr_info("%s: refused to register lurking device (%s) without callbacks\n",
730decf26f6SJoe Perches 				port->name, name);
7311da177e4SLinus Torvalds 			return NULL;
7321da177e4SLinus Torvalds 		}
7331da177e4SLinus Torvalds 	}
7341da177e4SLinus Torvalds 
73550566ac8SSudip Mukherjee 	if (flags & PARPORT_DEV_EXCL) {
73650566ac8SSudip Mukherjee 		if (port->physport->devices) {
73750566ac8SSudip Mukherjee 			/*
73850566ac8SSudip Mukherjee 			 * If a device is already registered and this new
73950566ac8SSudip Mukherjee 			 * device wants exclusive access, then no need to
74050566ac8SSudip Mukherjee 			 * continue as we can not grant exclusive access to
74150566ac8SSudip Mukherjee 			 * this device.
74250566ac8SSudip Mukherjee 			 */
74350566ac8SSudip Mukherjee 			pr_err("%s: cannot grant exclusive access for device %s\n",
74450566ac8SSudip Mukherjee 			       port->name, name);
74550566ac8SSudip Mukherjee 			return NULL;
74650566ac8SSudip Mukherjee 		}
74750566ac8SSudip Mukherjee 	}
74850566ac8SSudip Mukherjee 
7497b7a0a30SSudip Mukherjee 	/*
7507b7a0a30SSudip Mukherjee 	 * We up our own module reference count, and that of the port
7517b7a0a30SSudip Mukherjee 	 * on which a device is to be registered, to ensure that
7527b7a0a30SSudip Mukherjee 	 * neither of us gets unloaded while we sleep in (e.g.)
7537b7a0a30SSudip Mukherjee 	 * kmalloc.
7541da177e4SLinus Torvalds 	 */
75513efa75dSSudip Mukherjee 	if (!try_module_get(port->ops->owner))
7561da177e4SLinus Torvalds 		return NULL;
7571da177e4SLinus Torvalds 
7581da177e4SLinus Torvalds 	parport_get_port(port);
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	tmp = kmalloc(sizeof(struct pardevice), GFP_KERNEL);
761b95b9d6cSSudip Mukherjee 	if (!tmp)
7621da177e4SLinus Torvalds 		goto out;
7631da177e4SLinus Torvalds 
7641da177e4SLinus Torvalds 	tmp->state = kmalloc(sizeof(struct parport_state), GFP_KERNEL);
765b95b9d6cSSudip Mukherjee 	if (!tmp->state)
7661da177e4SLinus Torvalds 		goto out_free_pardevice;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	tmp->name = name;
7691da177e4SLinus Torvalds 	tmp->port = port;
7701da177e4SLinus Torvalds 	tmp->daisy = -1;
7711da177e4SLinus Torvalds 	tmp->preempt = pf;
7721da177e4SLinus Torvalds 	tmp->wakeup = kf;
7731da177e4SLinus Torvalds 	tmp->private = handle;
7741da177e4SLinus Torvalds 	tmp->flags = flags;
7751da177e4SLinus Torvalds 	tmp->irq_func = irq_func;
7761da177e4SLinus Torvalds 	tmp->waiting = 0;
7771da177e4SLinus Torvalds 	tmp->timeout = 5 * HZ;
7786fa45a22SSudip Mukherjee 	tmp->devmodel = false;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	/* Chain this onto the list */
7811da177e4SLinus Torvalds 	tmp->prev = NULL;
7821da177e4SLinus Torvalds 	/*
7831da177e4SLinus Torvalds 	 * This function must not run from an irq handler so we don' t need
7841da177e4SLinus Torvalds 	 * to clear irq on the local CPU. -arca
7851da177e4SLinus Torvalds 	 */
7861da177e4SLinus Torvalds 	spin_lock(&port->physport->pardevice_lock);
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	if (flags & PARPORT_DEV_EXCL) {
7891da177e4SLinus Torvalds 		if (port->physport->devices) {
7901da177e4SLinus Torvalds 			spin_unlock(&port->physport->pardevice_lock);
791aa3d6e7cSJoe Perches 			printk(KERN_DEBUG "%s: cannot grant exclusive access for device %s\n",
792a162188fSSudip Mukherjee 			       port->name, name);
7931da177e4SLinus Torvalds 			goto out_free_all;
7941da177e4SLinus Torvalds 		}
7951da177e4SLinus Torvalds 		port->flags |= PARPORT_FLAG_EXCL;
7961da177e4SLinus Torvalds 	}
7971da177e4SLinus Torvalds 
7981da177e4SLinus Torvalds 	tmp->next = port->physport->devices;
7997b7a0a30SSudip Mukherjee 	wmb(); /*
8007b7a0a30SSudip Mukherjee 		* Make sure that tmp->next is written before it's
8017b7a0a30SSudip Mukherjee 		* added to the list; see comments marked 'no locking
8027b7a0a30SSudip Mukherjee 		* required'
8037b7a0a30SSudip Mukherjee 		*/
8041da177e4SLinus Torvalds 	if (port->physport->devices)
8051da177e4SLinus Torvalds 		port->physport->devices->prev = tmp;
8061da177e4SLinus Torvalds 	port->physport->devices = tmp;
8071da177e4SLinus Torvalds 	spin_unlock(&port->physport->pardevice_lock);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	init_waitqueue_head(&tmp->wait_q);
8101da177e4SLinus Torvalds 	tmp->timeslice = parport_default_timeslice;
8111da177e4SLinus Torvalds 	tmp->waitnext = tmp->waitprev = NULL;
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	/*
8141da177e4SLinus Torvalds 	 * This has to be run as last thing since init_state may need other
8151da177e4SLinus Torvalds 	 * pardevice fields. -arca
8161da177e4SLinus Torvalds 	 */
8171da177e4SLinus Torvalds 	port->ops->init_state(tmp, tmp->state);
81805ad709dSAlan Cox 	if (!test_and_set_bit(PARPORT_DEVPROC_REGISTERED, &port->devflags)) {
81905ad709dSAlan Cox 		port->proc_device = tmp;
8201da177e4SLinus Torvalds 		parport_device_proc_register(tmp);
82105ad709dSAlan Cox 	}
8221da177e4SLinus Torvalds 	return tmp;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds  out_free_all:
8251da177e4SLinus Torvalds 	kfree(tmp->state);
8261da177e4SLinus Torvalds  out_free_pardevice:
8271da177e4SLinus Torvalds 	kfree(tmp);
8281da177e4SLinus Torvalds  out:
8291da177e4SLinus Torvalds 	parport_put_port(port);
8301da177e4SLinus Torvalds 	module_put(port->ops->owner);
8311da177e4SLinus Torvalds 
8321da177e4SLinus Torvalds 	return NULL;
8331da177e4SLinus Torvalds }
83427c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_register_device);
8351da177e4SLinus Torvalds 
8366fa45a22SSudip Mukherjee static void free_pardevice(struct device *dev)
8376fa45a22SSudip Mukherjee {
8386fa45a22SSudip Mukherjee 	struct pardevice *par_dev = to_pardevice(dev);
8396fa45a22SSudip Mukherjee 
8406fa45a22SSudip Mukherjee 	kfree(par_dev->name);
8416fa45a22SSudip Mukherjee 	kfree(par_dev);
8426fa45a22SSudip Mukherjee }
8436fa45a22SSudip Mukherjee 
8446824f0ceSSudip Mukherjee /**
8456824f0ceSSudip Mukherjee  *	parport_register_dev_model - register a device on a parallel port
8466824f0ceSSudip Mukherjee  *	@port: port to which the device is attached
8476824f0ceSSudip Mukherjee  *	@name: a name to refer to the device
8486824f0ceSSudip Mukherjee  *	@par_dev_cb: struct containing callbacks
8496824f0ceSSudip Mukherjee  *	@id: device number to be given to the device
8506824f0ceSSudip Mukherjee  *
8516824f0ceSSudip Mukherjee  *	This function, called by parallel port device drivers,
8526824f0ceSSudip Mukherjee  *	declares that a device is connected to a port, and tells the
8536824f0ceSSudip Mukherjee  *	system all it needs to know.
8546824f0ceSSudip Mukherjee  *
8556824f0ceSSudip Mukherjee  *	The struct pardev_cb contains pointer to callbacks. preemption
8566824f0ceSSudip Mukherjee  *	callback function, @preempt, is called when this device driver
8576824f0ceSSudip Mukherjee  *	has claimed access to the port but another device driver wants
8586824f0ceSSudip Mukherjee  *	to use it.  It is given, @private, as its parameter, and should
8596824f0ceSSudip Mukherjee  *	return zero if it is willing for the system to release the port
8606824f0ceSSudip Mukherjee  *	to another driver on its behalf. If it wants to keep control of
8616824f0ceSSudip Mukherjee  *	the port it should return non-zero, and no action will be taken.
8626824f0ceSSudip Mukherjee  *	It is good manners for the driver to try to release the port at
8636824f0ceSSudip Mukherjee  *	the earliest opportunity after its preemption callback rejects a
8646824f0ceSSudip Mukherjee  *	preemption attempt. Note that if a preemption callback is happy
8656824f0ceSSudip Mukherjee  *	for preemption to go ahead, there is no need to release the
8666824f0ceSSudip Mukherjee  *	port; it is done automatically. This function may not block, as
8676824f0ceSSudip Mukherjee  *	it may be called from interrupt context. If the device driver
8686824f0ceSSudip Mukherjee  *	does not support preemption, @preempt can be %NULL.
8696824f0ceSSudip Mukherjee  *
8706824f0ceSSudip Mukherjee  *	The wake-up ("kick") callback function, @wakeup, is called when
8716824f0ceSSudip Mukherjee  *	the port is available to be claimed for exclusive access; that
8726824f0ceSSudip Mukherjee  *	is, parport_claim() is guaranteed to succeed when called from
8736824f0ceSSudip Mukherjee  *	inside the wake-up callback function.  If the driver wants to
8746824f0ceSSudip Mukherjee  *	claim the port it should do so; otherwise, it need not take
8756824f0ceSSudip Mukherjee  *	any action.  This function may not block, as it may be called
8766824f0ceSSudip Mukherjee  *	from interrupt context.  If the device driver does not want to
8776824f0ceSSudip Mukherjee  *	be explicitly invited to claim the port in this way, @wakeup can
8786824f0ceSSudip Mukherjee  *	be %NULL.
8796824f0ceSSudip Mukherjee  *
8806824f0ceSSudip Mukherjee  *	The interrupt handler, @irq_func, is called when an interrupt
8816824f0ceSSudip Mukherjee  *	arrives from the parallel port.  Note that if a device driver
8826824f0ceSSudip Mukherjee  *	wants to use interrupts it should use parport_enable_irq(),
8836824f0ceSSudip Mukherjee  *	and can also check the irq member of the parport structure
8846824f0ceSSudip Mukherjee  *	representing the port.
8856824f0ceSSudip Mukherjee  *
8866824f0ceSSudip Mukherjee  *	The parallel port (lowlevel) driver is the one that has called
8876824f0ceSSudip Mukherjee  *	request_irq() and whose interrupt handler is called first.
8886824f0ceSSudip Mukherjee  *	This handler does whatever needs to be done to the hardware to
8896824f0ceSSudip Mukherjee  *	acknowledge the interrupt (for PC-style ports there is nothing
8906824f0ceSSudip Mukherjee  *	special to be done).  It then tells the IEEE 1284 code about
8916824f0ceSSudip Mukherjee  *	the interrupt, which may involve reacting to an IEEE 1284
8926824f0ceSSudip Mukherjee  *	event depending on the current IEEE 1284 phase.  After this,
8936824f0ceSSudip Mukherjee  *	it calls @irq_func.  Needless to say, @irq_func will be called
8946824f0ceSSudip Mukherjee  *	from interrupt context, and may not block.
8956824f0ceSSudip Mukherjee  *
8966824f0ceSSudip Mukherjee  *	The %PARPORT_DEV_EXCL flag is for preventing port sharing, and
8976824f0ceSSudip Mukherjee  *	so should only be used when sharing the port with other device
8986824f0ceSSudip Mukherjee  *	drivers is impossible and would lead to incorrect behaviour.
8996824f0ceSSudip Mukherjee  *	Use it sparingly!  Normally, @flags will be zero.
9006824f0ceSSudip Mukherjee  *
9016824f0ceSSudip Mukherjee  *	This function returns a pointer to a structure that represents
9026824f0ceSSudip Mukherjee  *	the device on the port, or %NULL if there is not enough memory
9036824f0ceSSudip Mukherjee  *	to allocate space for that structure.
9046824f0ceSSudip Mukherjee  **/
9056824f0ceSSudip Mukherjee 
9066fa45a22SSudip Mukherjee struct pardevice *
9076fa45a22SSudip Mukherjee parport_register_dev_model(struct parport *port, const char *name,
9086fa45a22SSudip Mukherjee 			   const struct pardev_cb *par_dev_cb, int id)
9096fa45a22SSudip Mukherjee {
9106fa45a22SSudip Mukherjee 	struct pardevice *par_dev;
9116fa45a22SSudip Mukherjee 	int ret;
9126fa45a22SSudip Mukherjee 	char *devname;
9136fa45a22SSudip Mukherjee 
9146fa45a22SSudip Mukherjee 	if (port->physport->flags & PARPORT_FLAG_EXCL) {
9156fa45a22SSudip Mukherjee 		/* An exclusive device is registered. */
9166fa45a22SSudip Mukherjee 		pr_err("%s: no more devices allowed\n", port->name);
9176fa45a22SSudip Mukherjee 		return NULL;
9186fa45a22SSudip Mukherjee 	}
9196fa45a22SSudip Mukherjee 
9206fa45a22SSudip Mukherjee 	if (par_dev_cb->flags & PARPORT_DEV_LURK) {
9216fa45a22SSudip Mukherjee 		if (!par_dev_cb->preempt || !par_dev_cb->wakeup) {
9226fa45a22SSudip Mukherjee 			pr_info("%s: refused to register lurking device (%s) without callbacks\n",
9236fa45a22SSudip Mukherjee 				port->name, name);
9246fa45a22SSudip Mukherjee 			return NULL;
9256fa45a22SSudip Mukherjee 		}
9266fa45a22SSudip Mukherjee 	}
9276fa45a22SSudip Mukherjee 
92850566ac8SSudip Mukherjee 	if (par_dev_cb->flags & PARPORT_DEV_EXCL) {
92950566ac8SSudip Mukherjee 		if (port->physport->devices) {
93050566ac8SSudip Mukherjee 			/*
93150566ac8SSudip Mukherjee 			 * If a device is already registered and this new
93250566ac8SSudip Mukherjee 			 * device wants exclusive access, then no need to
93350566ac8SSudip Mukherjee 			 * continue as we can not grant exclusive access to
93450566ac8SSudip Mukherjee 			 * this device.
93550566ac8SSudip Mukherjee 			 */
93650566ac8SSudip Mukherjee 			pr_err("%s: cannot grant exclusive access for device %s\n",
93750566ac8SSudip Mukherjee 			       port->name, name);
93850566ac8SSudip Mukherjee 			return NULL;
93950566ac8SSudip Mukherjee 		}
94050566ac8SSudip Mukherjee 	}
94150566ac8SSudip Mukherjee 
9426fa45a22SSudip Mukherjee 	if (!try_module_get(port->ops->owner))
9436fa45a22SSudip Mukherjee 		return NULL;
9446fa45a22SSudip Mukherjee 
9456fa45a22SSudip Mukherjee 	parport_get_port(port);
9466fa45a22SSudip Mukherjee 
9476fa45a22SSudip Mukherjee 	par_dev = kzalloc(sizeof(*par_dev), GFP_KERNEL);
9486fa45a22SSudip Mukherjee 	if (!par_dev)
9496fa45a22SSudip Mukherjee 		goto err_put_port;
9506fa45a22SSudip Mukherjee 
9516fa45a22SSudip Mukherjee 	par_dev->state = kzalloc(sizeof(*par_dev->state), GFP_KERNEL);
9526fa45a22SSudip Mukherjee 	if (!par_dev->state)
9536fa45a22SSudip Mukherjee 		goto err_put_par_dev;
9546fa45a22SSudip Mukherjee 
9556fa45a22SSudip Mukherjee 	devname = kstrdup(name, GFP_KERNEL);
9566fa45a22SSudip Mukherjee 	if (!devname)
9576fa45a22SSudip Mukherjee 		goto err_free_par_dev;
9586fa45a22SSudip Mukherjee 
9596fa45a22SSudip Mukherjee 	par_dev->name = devname;
9606fa45a22SSudip Mukherjee 	par_dev->port = port;
9616fa45a22SSudip Mukherjee 	par_dev->daisy = -1;
9626fa45a22SSudip Mukherjee 	par_dev->preempt = par_dev_cb->preempt;
9636fa45a22SSudip Mukherjee 	par_dev->wakeup = par_dev_cb->wakeup;
9646fa45a22SSudip Mukherjee 	par_dev->private = par_dev_cb->private;
9656fa45a22SSudip Mukherjee 	par_dev->flags = par_dev_cb->flags;
9666fa45a22SSudip Mukherjee 	par_dev->irq_func = par_dev_cb->irq_func;
9676fa45a22SSudip Mukherjee 	par_dev->waiting = 0;
9686fa45a22SSudip Mukherjee 	par_dev->timeout = 5 * HZ;
9696fa45a22SSudip Mukherjee 
9706fa45a22SSudip Mukherjee 	par_dev->dev.parent = &port->bus_dev;
9716fa45a22SSudip Mukherjee 	par_dev->dev.bus = &parport_bus_type;
9726fa45a22SSudip Mukherjee 	ret = dev_set_name(&par_dev->dev, "%s.%d", devname, id);
9736fa45a22SSudip Mukherjee 	if (ret)
9746fa45a22SSudip Mukherjee 		goto err_free_devname;
9756fa45a22SSudip Mukherjee 	par_dev->dev.release = free_pardevice;
9766fa45a22SSudip Mukherjee 	par_dev->devmodel = true;
9776fa45a22SSudip Mukherjee 	ret = device_register(&par_dev->dev);
97868d35c7bSSudip Mukherjee 	if (ret) {
9791c7ebeabSYueHaibing 		kfree(par_dev->state);
98068d35c7bSSudip Mukherjee 		put_device(&par_dev->dev);
98168d35c7bSSudip Mukherjee 		goto err_put_port;
98268d35c7bSSudip Mukherjee 	}
9836fa45a22SSudip Mukherjee 
9846fa45a22SSudip Mukherjee 	/* Chain this onto the list */
9856fa45a22SSudip Mukherjee 	par_dev->prev = NULL;
9866fa45a22SSudip Mukherjee 	/*
9876fa45a22SSudip Mukherjee 	 * This function must not run from an irq handler so we don' t need
9886fa45a22SSudip Mukherjee 	 * to clear irq on the local CPU. -arca
9896fa45a22SSudip Mukherjee 	 */
9906fa45a22SSudip Mukherjee 	spin_lock(&port->physport->pardevice_lock);
9916fa45a22SSudip Mukherjee 
9926fa45a22SSudip Mukherjee 	if (par_dev_cb->flags & PARPORT_DEV_EXCL) {
9936fa45a22SSudip Mukherjee 		if (port->physport->devices) {
9946fa45a22SSudip Mukherjee 			spin_unlock(&port->physport->pardevice_lock);
9956fa45a22SSudip Mukherjee 			pr_debug("%s: cannot grant exclusive access for device %s\n",
9966fa45a22SSudip Mukherjee 				 port->name, name);
9971c7ebeabSYueHaibing 			kfree(par_dev->state);
998cabea695SSudip Mukherjee 			device_unregister(&par_dev->dev);
999cabea695SSudip Mukherjee 			goto err_put_port;
10006fa45a22SSudip Mukherjee 		}
10016fa45a22SSudip Mukherjee 		port->flags |= PARPORT_FLAG_EXCL;
10026fa45a22SSudip Mukherjee 	}
10036fa45a22SSudip Mukherjee 
10046fa45a22SSudip Mukherjee 	par_dev->next = port->physport->devices;
10056fa45a22SSudip Mukherjee 	wmb();	/*
10066fa45a22SSudip Mukherjee 		 * Make sure that tmp->next is written before it's
10076fa45a22SSudip Mukherjee 		 * added to the list; see comments marked 'no locking
10086fa45a22SSudip Mukherjee 		 * required'
10096fa45a22SSudip Mukherjee 		 */
10106fa45a22SSudip Mukherjee 	if (port->physport->devices)
10116fa45a22SSudip Mukherjee 		port->physport->devices->prev = par_dev;
10126fa45a22SSudip Mukherjee 	port->physport->devices = par_dev;
10136fa45a22SSudip Mukherjee 	spin_unlock(&port->physport->pardevice_lock);
10146fa45a22SSudip Mukherjee 
10156fa45a22SSudip Mukherjee 	init_waitqueue_head(&par_dev->wait_q);
10166fa45a22SSudip Mukherjee 	par_dev->timeslice = parport_default_timeslice;
10176fa45a22SSudip Mukherjee 	par_dev->waitnext = NULL;
10186fa45a22SSudip Mukherjee 	par_dev->waitprev = NULL;
10196fa45a22SSudip Mukherjee 
10206fa45a22SSudip Mukherjee 	/*
10216fa45a22SSudip Mukherjee 	 * This has to be run as last thing since init_state may need other
10226fa45a22SSudip Mukherjee 	 * pardevice fields. -arca
10236fa45a22SSudip Mukherjee 	 */
10246fa45a22SSudip Mukherjee 	port->ops->init_state(par_dev, par_dev->state);
102503270c6aSSudip Mukherjee 	if (!test_and_set_bit(PARPORT_DEVPROC_REGISTERED, &port->devflags)) {
10266fa45a22SSudip Mukherjee 		port->proc_device = par_dev;
10276fa45a22SSudip Mukherjee 		parport_device_proc_register(par_dev);
102803270c6aSSudip Mukherjee 	}
10296fa45a22SSudip Mukherjee 
10306fa45a22SSudip Mukherjee 	return par_dev;
10316fa45a22SSudip Mukherjee 
10326fa45a22SSudip Mukherjee err_free_devname:
10336fa45a22SSudip Mukherjee 	kfree(devname);
10346fa45a22SSudip Mukherjee err_free_par_dev:
10356fa45a22SSudip Mukherjee 	kfree(par_dev->state);
10366fa45a22SSudip Mukherjee err_put_par_dev:
10376fa45a22SSudip Mukherjee 	if (!par_dev->devmodel)
10386fa45a22SSudip Mukherjee 		kfree(par_dev);
10396fa45a22SSudip Mukherjee err_put_port:
10406fa45a22SSudip Mukherjee 	parport_put_port(port);
10416fa45a22SSudip Mukherjee 	module_put(port->ops->owner);
10426fa45a22SSudip Mukherjee 
10436fa45a22SSudip Mukherjee 	return NULL;
10446fa45a22SSudip Mukherjee }
10456fa45a22SSudip Mukherjee EXPORT_SYMBOL(parport_register_dev_model);
10466fa45a22SSudip Mukherjee 
10471da177e4SLinus Torvalds /**
10481da177e4SLinus Torvalds  *	parport_unregister_device - deregister a device on a parallel port
10491da177e4SLinus Torvalds  *	@dev: pointer to structure representing device
10501da177e4SLinus Torvalds  *
10511da177e4SLinus Torvalds  *	This undoes the effect of parport_register_device().
10521da177e4SLinus Torvalds  **/
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds void parport_unregister_device(struct pardevice *dev)
10551da177e4SLinus Torvalds {
10561da177e4SLinus Torvalds 	struct parport *port;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds #ifdef PARPORT_PARANOID
1059b95b9d6cSSudip Mukherjee 	if (!dev) {
1060decf26f6SJoe Perches 		pr_err("%s: passed NULL\n", __func__);
10611da177e4SLinus Torvalds 		return;
10621da177e4SLinus Torvalds 	}
10631da177e4SLinus Torvalds #endif
10641da177e4SLinus Torvalds 
10651da177e4SLinus Torvalds 	port = dev->port->physport;
10661da177e4SLinus Torvalds 
106705ad709dSAlan Cox 	if (port->proc_device == dev) {
106805ad709dSAlan Cox 		port->proc_device = NULL;
106905ad709dSAlan Cox 		clear_bit(PARPORT_DEVPROC_REGISTERED, &port->devflags);
107005ad709dSAlan Cox 		parport_device_proc_unregister(dev);
107105ad709dSAlan Cox 	}
107205ad709dSAlan Cox 
10731da177e4SLinus Torvalds 	if (port->cad == dev) {
10741da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: %s forgot to release port\n",
10751da177e4SLinus Torvalds 		       port->name, dev->name);
10761da177e4SLinus Torvalds 		parport_release(dev);
10771da177e4SLinus Torvalds 	}
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	spin_lock(&port->pardevice_lock);
10801da177e4SLinus Torvalds 	if (dev->next)
10811da177e4SLinus Torvalds 		dev->next->prev = dev->prev;
10821da177e4SLinus Torvalds 	if (dev->prev)
10831da177e4SLinus Torvalds 		dev->prev->next = dev->next;
10841da177e4SLinus Torvalds 	else
10851da177e4SLinus Torvalds 		port->devices = dev->next;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	if (dev->flags & PARPORT_DEV_EXCL)
10881da177e4SLinus Torvalds 		port->flags &= ~PARPORT_FLAG_EXCL;
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 	spin_unlock(&port->pardevice_lock);
10911da177e4SLinus Torvalds 
10927b7a0a30SSudip Mukherjee 	/*
10937b7a0a30SSudip Mukherjee 	 * Make sure we haven't left any pointers around in the wait
10947b7a0a30SSudip Mukherjee 	 * list.
10957b7a0a30SSudip Mukherjee 	 */
1096cbeb4b7aSAlexander Gordeev 	spin_lock_irq(&port->waitlist_lock);
10971da177e4SLinus Torvalds 	if (dev->waitprev || dev->waitnext || port->waithead == dev) {
10981da177e4SLinus Torvalds 		if (dev->waitprev)
10991da177e4SLinus Torvalds 			dev->waitprev->waitnext = dev->waitnext;
11001da177e4SLinus Torvalds 		else
11011da177e4SLinus Torvalds 			port->waithead = dev->waitnext;
11021da177e4SLinus Torvalds 		if (dev->waitnext)
11031da177e4SLinus Torvalds 			dev->waitnext->waitprev = dev->waitprev;
11041da177e4SLinus Torvalds 		else
11051da177e4SLinus Torvalds 			port->waittail = dev->waitprev;
11061da177e4SLinus Torvalds 	}
1107cbeb4b7aSAlexander Gordeev 	spin_unlock_irq(&port->waitlist_lock);
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	kfree(dev->state);
11106fa45a22SSudip Mukherjee 	if (dev->devmodel)
11116fa45a22SSudip Mukherjee 		device_unregister(&dev->dev);
11126fa45a22SSudip Mukherjee 	else
11131da177e4SLinus Torvalds 		kfree(dev);
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	module_put(port->ops->owner);
11161da177e4SLinus Torvalds 	parport_put_port(port);
11171da177e4SLinus Torvalds }
111827c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_unregister_device);
11191da177e4SLinus Torvalds 
11201da177e4SLinus Torvalds /**
11211da177e4SLinus Torvalds  *	parport_find_number - find a parallel port by number
11221da177e4SLinus Torvalds  *	@number: parallel port number
11231da177e4SLinus Torvalds  *
11241da177e4SLinus Torvalds  *	This returns the parallel port with the specified number, or
11251da177e4SLinus Torvalds  *	%NULL if there is none.
11261da177e4SLinus Torvalds  *
11271da177e4SLinus Torvalds  *	There is an implicit parport_get_port() done already; to throw
11281da177e4SLinus Torvalds  *	away the reference to the port that parport_find_number()
11291da177e4SLinus Torvalds  *	gives you, use parport_put_port().
11301da177e4SLinus Torvalds  */
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds struct parport *parport_find_number(int number)
11331da177e4SLinus Torvalds {
11341da177e4SLinus Torvalds 	struct parport *port, *result = NULL;
11351da177e4SLinus Torvalds 
11361da177e4SLinus Torvalds 	if (list_empty(&portlist))
11371da177e4SLinus Torvalds 		get_lowlevel_driver();
11381da177e4SLinus Torvalds 
11391da177e4SLinus Torvalds 	spin_lock(&parportlist_lock);
11401da177e4SLinus Torvalds 	list_for_each_entry(port, &portlist, list) {
11411da177e4SLinus Torvalds 		if (port->number == number) {
11421da177e4SLinus Torvalds 			result = parport_get_port(port);
11431da177e4SLinus Torvalds 			break;
11441da177e4SLinus Torvalds 		}
11451da177e4SLinus Torvalds 	}
11461da177e4SLinus Torvalds 	spin_unlock(&parportlist_lock);
11471da177e4SLinus Torvalds 	return result;
11481da177e4SLinus Torvalds }
114927c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_find_number);
11501da177e4SLinus Torvalds 
11511da177e4SLinus Torvalds /**
11521da177e4SLinus Torvalds  *	parport_find_base - find a parallel port by base address
11531da177e4SLinus Torvalds  *	@base: base I/O address
11541da177e4SLinus Torvalds  *
11551da177e4SLinus Torvalds  *	This returns the parallel port with the specified base
11561da177e4SLinus Torvalds  *	address, or %NULL if there is none.
11571da177e4SLinus Torvalds  *
11581da177e4SLinus Torvalds  *	There is an implicit parport_get_port() done already; to throw
11591da177e4SLinus Torvalds  *	away the reference to the port that parport_find_base()
11601da177e4SLinus Torvalds  *	gives you, use parport_put_port().
11611da177e4SLinus Torvalds  */
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds struct parport *parport_find_base(unsigned long base)
11641da177e4SLinus Torvalds {
11651da177e4SLinus Torvalds 	struct parport *port, *result = NULL;
11661da177e4SLinus Torvalds 
11671da177e4SLinus Torvalds 	if (list_empty(&portlist))
11681da177e4SLinus Torvalds 		get_lowlevel_driver();
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 	spin_lock(&parportlist_lock);
11711da177e4SLinus Torvalds 	list_for_each_entry(port, &portlist, list) {
11721da177e4SLinus Torvalds 		if (port->base == base) {
11731da177e4SLinus Torvalds 			result = parport_get_port(port);
11741da177e4SLinus Torvalds 			break;
11751da177e4SLinus Torvalds 		}
11761da177e4SLinus Torvalds 	}
11771da177e4SLinus Torvalds 	spin_unlock(&parportlist_lock);
11781da177e4SLinus Torvalds 	return result;
11791da177e4SLinus Torvalds }
118027c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_find_base);
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds /**
11831da177e4SLinus Torvalds  *	parport_claim - claim access to a parallel port device
11841da177e4SLinus Torvalds  *	@dev: pointer to structure representing a device on the port
11851da177e4SLinus Torvalds  *
11861da177e4SLinus Torvalds  *	This function will not block and so can be used from interrupt
11871da177e4SLinus Torvalds  *	context.  If parport_claim() succeeds in claiming access to
11881da177e4SLinus Torvalds  *	the port it returns zero and the port is available to use.  It
11891da177e4SLinus Torvalds  *	may fail (returning non-zero) if the port is in use by another
11901da177e4SLinus Torvalds  *	driver and that driver is not willing to relinquish control of
11911da177e4SLinus Torvalds  *	the port.
11921da177e4SLinus Torvalds  **/
11931da177e4SLinus Torvalds 
11941da177e4SLinus Torvalds int parport_claim(struct pardevice *dev)
11951da177e4SLinus Torvalds {
11961da177e4SLinus Torvalds 	struct pardevice *oldcad;
11971da177e4SLinus Torvalds 	struct parport *port = dev->port->physport;
11981da177e4SLinus Torvalds 	unsigned long flags;
11991da177e4SLinus Torvalds 
12001da177e4SLinus Torvalds 	if (port->cad == dev) {
1201decf26f6SJoe Perches 		pr_info("%s: %s already owner\n", dev->port->name, dev->name);
12021da177e4SLinus Torvalds 		return 0;
12031da177e4SLinus Torvalds 	}
12041da177e4SLinus Torvalds 
12051da177e4SLinus Torvalds 	/* Preempt any current device */
12061da177e4SLinus Torvalds 	write_lock_irqsave(&port->cad_lock, flags);
1207e0a7f1f0SSudip Mukherjee 	oldcad = port->cad;
1208e0a7f1f0SSudip Mukherjee 	if (oldcad) {
12091da177e4SLinus Torvalds 		if (oldcad->preempt) {
12101da177e4SLinus Torvalds 			if (oldcad->preempt(oldcad->private))
12111da177e4SLinus Torvalds 				goto blocked;
12121da177e4SLinus Torvalds 			port->ops->save_state(port, dev->state);
12131da177e4SLinus Torvalds 		} else
12141da177e4SLinus Torvalds 			goto blocked;
12151da177e4SLinus Torvalds 
12161da177e4SLinus Torvalds 		if (port->cad != oldcad) {
12177b7a0a30SSudip Mukherjee 			/*
12187b7a0a30SSudip Mukherjee 			 * I think we'll actually deadlock rather than
12197b7a0a30SSudip Mukherjee 			 * get here, but just in case..
12207b7a0a30SSudip Mukherjee 			 */
1221decf26f6SJoe Perches 			pr_warn("%s: %s released port when preempted!\n",
12221da177e4SLinus Torvalds 				port->name, oldcad->name);
12231da177e4SLinus Torvalds 			if (port->cad)
12241da177e4SLinus Torvalds 				goto blocked;
12251da177e4SLinus Torvalds 		}
12261da177e4SLinus Torvalds 	}
12271da177e4SLinus Torvalds 
12281da177e4SLinus Torvalds 	/* Can't fail from now on, so mark ourselves as no longer waiting.  */
12291da177e4SLinus Torvalds 	if (dev->waiting & 1) {
12301da177e4SLinus Torvalds 		dev->waiting = 0;
12311da177e4SLinus Torvalds 
12321da177e4SLinus Torvalds 		/* Take ourselves out of the wait list again.  */
12331da177e4SLinus Torvalds 		spin_lock_irq(&port->waitlist_lock);
12341da177e4SLinus Torvalds 		if (dev->waitprev)
12351da177e4SLinus Torvalds 			dev->waitprev->waitnext = dev->waitnext;
12361da177e4SLinus Torvalds 		else
12371da177e4SLinus Torvalds 			port->waithead = dev->waitnext;
12381da177e4SLinus Torvalds 		if (dev->waitnext)
12391da177e4SLinus Torvalds 			dev->waitnext->waitprev = dev->waitprev;
12401da177e4SLinus Torvalds 		else
12411da177e4SLinus Torvalds 			port->waittail = dev->waitprev;
12421da177e4SLinus Torvalds 		spin_unlock_irq(&port->waitlist_lock);
12431da177e4SLinus Torvalds 		dev->waitprev = dev->waitnext = NULL;
12441da177e4SLinus Torvalds 	}
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 	/* Now we do the change of devices */
12471da177e4SLinus Torvalds 	port->cad = dev;
12481da177e4SLinus Torvalds 
12491da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
12501da177e4SLinus Torvalds 	/* If it's a mux port, select it. */
12511da177e4SLinus Torvalds 	if (dev->port->muxport >= 0) {
12521da177e4SLinus Torvalds 		/* FIXME */
12531da177e4SLinus Torvalds 		port->muxsel = dev->port->muxport;
12541da177e4SLinus Torvalds 	}
12551da177e4SLinus Torvalds 
12561da177e4SLinus Torvalds 	/* If it's a daisy chain device, select it. */
12571da177e4SLinus Torvalds 	if (dev->daisy >= 0) {
12581da177e4SLinus Torvalds 		/* This could be lazier. */
12591da177e4SLinus Torvalds 		if (!parport_daisy_select(port, dev->daisy,
12601da177e4SLinus Torvalds 					   IEEE1284_MODE_COMPAT))
12611da177e4SLinus Torvalds 			port->daisy = dev->daisy;
12621da177e4SLinus Torvalds 	}
12631da177e4SLinus Torvalds #endif /* IEEE1284.3 support */
12641da177e4SLinus Torvalds 
12651da177e4SLinus Torvalds 	/* Restore control registers */
12661da177e4SLinus Torvalds 	port->ops->restore_state(port, dev->state);
12671da177e4SLinus Torvalds 	write_unlock_irqrestore(&port->cad_lock, flags);
12681da177e4SLinus Torvalds 	dev->time = jiffies;
12691da177e4SLinus Torvalds 	return 0;
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds blocked:
12727b7a0a30SSudip Mukherjee 	/*
12737b7a0a30SSudip Mukherjee 	 * If this is the first time we tried to claim the port, register an
12747b7a0a30SSudip Mukherjee 	 * interest.  This is only allowed for devices sleeping in
12757b7a0a30SSudip Mukherjee 	 * parport_claim_or_block(), or those with a wakeup function.
12767b7a0a30SSudip Mukherjee 	 */
12771da177e4SLinus Torvalds 
12781da177e4SLinus Torvalds 	/* The cad_lock is still held for writing here */
12791da177e4SLinus Torvalds 	if (dev->waiting & 2 || dev->wakeup) {
12801da177e4SLinus Torvalds 		spin_lock(&port->waitlist_lock);
12811da177e4SLinus Torvalds 		if (test_and_set_bit(0, &dev->waiting) == 0) {
12821da177e4SLinus Torvalds 			/* First add ourselves to the end of the wait list. */
12831da177e4SLinus Torvalds 			dev->waitnext = NULL;
12841da177e4SLinus Torvalds 			dev->waitprev = port->waittail;
12851da177e4SLinus Torvalds 			if (port->waittail) {
12861da177e4SLinus Torvalds 				port->waittail->waitnext = dev;
12871da177e4SLinus Torvalds 				port->waittail = dev;
12881da177e4SLinus Torvalds 			} else
12891da177e4SLinus Torvalds 				port->waithead = port->waittail = dev;
12901da177e4SLinus Torvalds 		}
12911da177e4SLinus Torvalds 		spin_unlock(&port->waitlist_lock);
12921da177e4SLinus Torvalds 	}
12931da177e4SLinus Torvalds 	write_unlock_irqrestore(&port->cad_lock, flags);
12941da177e4SLinus Torvalds 	return -EAGAIN;
12951da177e4SLinus Torvalds }
129627c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_claim);
12971da177e4SLinus Torvalds 
12981da177e4SLinus Torvalds /**
12991da177e4SLinus Torvalds  *	parport_claim_or_block - claim access to a parallel port device
13001da177e4SLinus Torvalds  *	@dev: pointer to structure representing a device on the port
13011da177e4SLinus Torvalds  *
13021da177e4SLinus Torvalds  *	This behaves like parport_claim(), but will block if necessary
13031da177e4SLinus Torvalds  *	to wait for the port to be free.  A return value of 1
13041da177e4SLinus Torvalds  *	indicates that it slept; 0 means that it succeeded without
13051da177e4SLinus Torvalds  *	needing to sleep.  A negative error code indicates failure.
13061da177e4SLinus Torvalds  **/
13071da177e4SLinus Torvalds 
13081da177e4SLinus Torvalds int parport_claim_or_block(struct pardevice *dev)
13091da177e4SLinus Torvalds {
13101da177e4SLinus Torvalds 	int r;
13111da177e4SLinus Torvalds 
13127b7a0a30SSudip Mukherjee 	/*
13137b7a0a30SSudip Mukherjee 	 * Signal to parport_claim() that we can wait even without a
13147b7a0a30SSudip Mukherjee 	 * wakeup function.
13157b7a0a30SSudip Mukherjee 	 */
13161da177e4SLinus Torvalds 	dev->waiting = 2;
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	/* Try to claim the port.  If this fails, we need to sleep.  */
13191da177e4SLinus Torvalds 	r = parport_claim(dev);
13201da177e4SLinus Torvalds 	if (r == -EAGAIN) {
13211da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING
1322aa3d6e7cSJoe Perches 		printk(KERN_DEBUG "%s: parport_claim() returned -EAGAIN\n",
1323aa3d6e7cSJoe Perches 		       dev->name);
13241da177e4SLinus Torvalds #endif
13251da177e4SLinus Torvalds 		/*
13261da177e4SLinus Torvalds 		 * FIXME!!! Use the proper locking for dev->waiting,
13271da177e4SLinus Torvalds 		 * and make this use the "wait_event_interruptible()"
13281da177e4SLinus Torvalds 		 * interfaces. The cli/sti that used to be here
13291da177e4SLinus Torvalds 		 * did nothing.
13301da177e4SLinus Torvalds 		 *
13311da177e4SLinus Torvalds 		 * See also parport_release()
13321da177e4SLinus Torvalds 		 */
13331da177e4SLinus Torvalds 
13347b7a0a30SSudip Mukherjee 		/*
13357b7a0a30SSudip Mukherjee 		 * If dev->waiting is clear now, an interrupt
13367b7a0a30SSudip Mukherjee 		 * gave us the port and we would deadlock if we slept.
13377b7a0a30SSudip Mukherjee 		 */
13381da177e4SLinus Torvalds 		if (dev->waiting) {
133976ae0536SArnd Bergmann 			wait_event_interruptible(dev->wait_q,
134076ae0536SArnd Bergmann 						 !dev->waiting);
134113efa75dSSudip Mukherjee 			if (signal_pending(current))
13421da177e4SLinus Torvalds 				return -EINTR;
13431da177e4SLinus Torvalds 			r = 1;
13441da177e4SLinus Torvalds 		} else {
13451da177e4SLinus Torvalds 			r = 0;
13461da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING
13471da177e4SLinus Torvalds 			printk(KERN_DEBUG "%s: didn't sleep in parport_claim_or_block()\n",
13481da177e4SLinus Torvalds 			       dev->name);
13491da177e4SLinus Torvalds #endif
13501da177e4SLinus Torvalds 		}
13511da177e4SLinus Torvalds 
13521da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING
13531da177e4SLinus Torvalds 		if (dev->port->physport->cad != dev)
1354a162188fSSudip Mukherjee 			printk(KERN_DEBUG "%s: exiting parport_claim_or_block but %s owns port!\n",
1355a162188fSSudip Mukherjee 			       dev->name, dev->port->physport->cad ?
13561da177e4SLinus Torvalds 			       dev->port->physport->cad->name : "nobody");
13571da177e4SLinus Torvalds #endif
13581da177e4SLinus Torvalds 	}
13591da177e4SLinus Torvalds 	dev->waiting = 0;
13601da177e4SLinus Torvalds 	return r;
13611da177e4SLinus Torvalds }
136227c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_claim_or_block);
13631da177e4SLinus Torvalds 
13641da177e4SLinus Torvalds /**
13651da177e4SLinus Torvalds  *	parport_release - give up access to a parallel port device
13661da177e4SLinus Torvalds  *	@dev: pointer to structure representing parallel port device
13671da177e4SLinus Torvalds  *
13681da177e4SLinus Torvalds  *	This function cannot fail, but it should not be called without
13691da177e4SLinus Torvalds  *	the port claimed.  Similarly, if the port is already claimed
13701da177e4SLinus Torvalds  *	you should not try claiming it again.
13711da177e4SLinus Torvalds  **/
13721da177e4SLinus Torvalds 
13731da177e4SLinus Torvalds void parport_release(struct pardevice *dev)
13741da177e4SLinus Torvalds {
13751da177e4SLinus Torvalds 	struct parport *port = dev->port->physport;
13761da177e4SLinus Torvalds 	struct pardevice *pd;
13771da177e4SLinus Torvalds 	unsigned long flags;
13781da177e4SLinus Torvalds 
13791da177e4SLinus Torvalds 	/* Make sure that dev is the current device */
13801da177e4SLinus Torvalds 	write_lock_irqsave(&port->cad_lock, flags);
13811da177e4SLinus Torvalds 	if (port->cad != dev) {
13821da177e4SLinus Torvalds 		write_unlock_irqrestore(&port->cad_lock, flags);
1383decf26f6SJoe Perches 		pr_warn("%s: %s tried to release parport when not owner\n",
1384a162188fSSudip Mukherjee 			port->name, dev->name);
13851da177e4SLinus Torvalds 		return;
13861da177e4SLinus Torvalds 	}
13871da177e4SLinus Torvalds 
13881da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
13891da177e4SLinus Torvalds 	/* If this is on a mux port, deselect it. */
13901da177e4SLinus Torvalds 	if (dev->port->muxport >= 0) {
13911da177e4SLinus Torvalds 		/* FIXME */
13921da177e4SLinus Torvalds 		port->muxsel = -1;
13931da177e4SLinus Torvalds 	}
13941da177e4SLinus Torvalds 
13951da177e4SLinus Torvalds 	/* If this is a daisy device, deselect it. */
13961da177e4SLinus Torvalds 	if (dev->daisy >= 0) {
13971da177e4SLinus Torvalds 		parport_daisy_deselect_all(port);
13981da177e4SLinus Torvalds 		port->daisy = -1;
13991da177e4SLinus Torvalds 	}
14001da177e4SLinus Torvalds #endif
14011da177e4SLinus Torvalds 
14021da177e4SLinus Torvalds 	port->cad = NULL;
14031da177e4SLinus Torvalds 	write_unlock_irqrestore(&port->cad_lock, flags);
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	/* Save control registers */
14061da177e4SLinus Torvalds 	port->ops->save_state(port, dev->state);
14071da177e4SLinus Torvalds 
14087b7a0a30SSudip Mukherjee 	/*
14097b7a0a30SSudip Mukherjee 	 * If anybody is waiting, find out who's been there longest and
14107b7a0a30SSudip Mukherjee 	 * then wake them up. (Note: no locking required)
14117b7a0a30SSudip Mukherjee 	 */
14121da177e4SLinus Torvalds 	/* !!! LOCKING IS NEEDED HERE */
14131da177e4SLinus Torvalds 	for (pd = port->waithead; pd; pd = pd->waitnext) {
14141da177e4SLinus Torvalds 		if (pd->waiting & 2) { /* sleeping in claim_or_block */
14151da177e4SLinus Torvalds 			parport_claim(pd);
14161da177e4SLinus Torvalds 			if (waitqueue_active(&pd->wait_q))
14171da177e4SLinus Torvalds 				wake_up_interruptible(&pd->wait_q);
14181da177e4SLinus Torvalds 			return;
14191da177e4SLinus Torvalds 		} else if (pd->wakeup) {
14201da177e4SLinus Torvalds 			pd->wakeup(pd->private);
14211da177e4SLinus Torvalds 			if (dev->port->cad) /* racy but no matter */
14221da177e4SLinus Torvalds 				return;
14231da177e4SLinus Torvalds 		} else {
1424decf26f6SJoe Perches 			pr_err("%s: don't know how to wake %s\n",
1425decf26f6SJoe Perches 			       port->name, pd->name);
14261da177e4SLinus Torvalds 		}
14271da177e4SLinus Torvalds 	}
14281da177e4SLinus Torvalds 
14297b7a0a30SSudip Mukherjee 	/*
14307b7a0a30SSudip Mukherjee 	 * Nobody was waiting, so walk the list to see if anyone is
14317b7a0a30SSudip Mukherjee 	 * interested in being woken up. (Note: no locking required)
14327b7a0a30SSudip Mukherjee 	 */
14331da177e4SLinus Torvalds 	/* !!! LOCKING IS NEEDED HERE */
1434b95b9d6cSSudip Mukherjee 	for (pd = port->devices; !port->cad && pd; pd = pd->next) {
14351da177e4SLinus Torvalds 		if (pd->wakeup && pd != dev)
14361da177e4SLinus Torvalds 			pd->wakeup(pd->private);
14371da177e4SLinus Torvalds 	}
14381da177e4SLinus Torvalds }
143927c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_release);
14401da177e4SLinus Torvalds 
14413f2e40dfSJeff Garzik irqreturn_t parport_irq_handler(int irq, void *dev_id)
14423f2e40dfSJeff Garzik {
14433f2e40dfSJeff Garzik 	struct parport *port = dev_id;
14443f2e40dfSJeff Garzik 
1445f230d101SJeff Garzik 	parport_generic_irq(port);
14463f2e40dfSJeff Garzik 
14473f2e40dfSJeff Garzik 	return IRQ_HANDLED;
14483f2e40dfSJeff Garzik }
14493f2e40dfSJeff Garzik EXPORT_SYMBOL(parport_irq_handler);
14501da177e4SLinus Torvalds 
14511da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1452