xref: /openbmc/linux/drivers/parport/share.c (revision 174cd4b1)
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 {
1406fa45a22SSudip Mukherjee 	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 
2331da177e4SLinus Torvalds /**
2341da177e4SLinus Torvalds  *	parport_register_driver - register a parallel port device driver
2351da177e4SLinus Torvalds  *	@drv: structure describing the driver
2366fa45a22SSudip Mukherjee  *	@owner: owner module of drv
2376fa45a22SSudip Mukherjee  *	@mod_name: module name string
2381da177e4SLinus Torvalds  *
2391da177e4SLinus Torvalds  *	This can be called by a parallel port device driver in order
2401da177e4SLinus Torvalds  *	to receive notifications about ports being found in the
2411da177e4SLinus Torvalds  *	system, as well as ports no longer available.
2421da177e4SLinus Torvalds  *
2436fa45a22SSudip Mukherjee  *	If devmodel is true then the new device model is used
2446fa45a22SSudip Mukherjee  *	for registration.
2456fa45a22SSudip Mukherjee  *
2461da177e4SLinus Torvalds  *	The @drv structure is allocated by the caller and must not be
2471da177e4SLinus Torvalds  *	deallocated until after calling parport_unregister_driver().
2481da177e4SLinus Torvalds  *
2496fa45a22SSudip Mukherjee  *	If using the non device model:
2501da177e4SLinus Torvalds  *	The driver's attach() function may block.  The port that
2511da177e4SLinus Torvalds  *	attach() is given will be valid for the duration of the
2521da177e4SLinus Torvalds  *	callback, but if the driver wants to take a copy of the
2531da177e4SLinus Torvalds  *	pointer it must call parport_get_port() to do so.  Calling
2541da177e4SLinus Torvalds  *	parport_register_device() on that port will do this for you.
2551da177e4SLinus Torvalds  *
2561da177e4SLinus Torvalds  *	The driver's detach() function may block.  The port that
2571da177e4SLinus Torvalds  *	detach() is given will be valid for the duration of the
2581da177e4SLinus Torvalds  *	callback, but if the driver wants to take a copy of the
2591da177e4SLinus Torvalds  *	pointer it must call parport_get_port() to do so.
2601da177e4SLinus Torvalds  *
2616fa45a22SSudip Mukherjee  *
2626fa45a22SSudip Mukherjee  *	Returns 0 on success. The non device model will always succeeds.
2636fa45a22SSudip Mukherjee  *	but the new device model can fail and will return the error code.
2641da177e4SLinus Torvalds  **/
2651da177e4SLinus Torvalds 
2666fa45a22SSudip Mukherjee int __parport_register_driver(struct parport_driver *drv, struct module *owner,
2676fa45a22SSudip Mukherjee 			      const char *mod_name)
2681da177e4SLinus Torvalds {
2691da177e4SLinus Torvalds 	if (list_empty(&portlist))
2701da177e4SLinus Torvalds 		get_lowlevel_driver();
2711da177e4SLinus Torvalds 
2726fa45a22SSudip Mukherjee 	if (drv->devmodel) {
2736fa45a22SSudip Mukherjee 		/* using device model */
2746fa45a22SSudip Mukherjee 		int ret;
2756fa45a22SSudip Mukherjee 
2766fa45a22SSudip Mukherjee 		/* initialize common driver fields */
2776fa45a22SSudip Mukherjee 		drv->driver.name = drv->name;
2786fa45a22SSudip Mukherjee 		drv->driver.bus = &parport_bus_type;
2796fa45a22SSudip Mukherjee 		drv->driver.owner = owner;
2806fa45a22SSudip Mukherjee 		drv->driver.mod_name = mod_name;
2816fa45a22SSudip Mukherjee 		ret = driver_register(&drv->driver);
2826fa45a22SSudip Mukherjee 		if (ret)
2836fa45a22SSudip Mukherjee 			return ret;
2846fa45a22SSudip Mukherjee 
2856fa45a22SSudip Mukherjee 		mutex_lock(&registration_lock);
2866fa45a22SSudip Mukherjee 		if (drv->match_port)
2876fa45a22SSudip Mukherjee 			bus_for_each_dev(&parport_bus_type, NULL, drv,
2886fa45a22SSudip Mukherjee 					 port_check);
2896fa45a22SSudip Mukherjee 		mutex_unlock(&registration_lock);
2906fa45a22SSudip Mukherjee 	} else {
2916fa45a22SSudip Mukherjee 		struct parport *port;
2926fa45a22SSudip Mukherjee 
2936fa45a22SSudip Mukherjee 		drv->devmodel = false;
2946fa45a22SSudip Mukherjee 
29514cc3e2bSIngo Molnar 		mutex_lock(&registration_lock);
2961da177e4SLinus Torvalds 		list_for_each_entry(port, &portlist, list)
2971da177e4SLinus Torvalds 			drv->attach(port);
2981da177e4SLinus Torvalds 		list_add(&drv->list, &drivers);
29914cc3e2bSIngo Molnar 		mutex_unlock(&registration_lock);
3006fa45a22SSudip Mukherjee 	}
3016fa45a22SSudip Mukherjee 
3026fa45a22SSudip Mukherjee 	return 0;
3036fa45a22SSudip Mukherjee }
3046fa45a22SSudip Mukherjee EXPORT_SYMBOL(__parport_register_driver);
3056fa45a22SSudip Mukherjee 
3066fa45a22SSudip Mukherjee static int port_detach(struct device *dev, void *_drv)
3076fa45a22SSudip Mukherjee {
3086fa45a22SSudip Mukherjee 	struct parport_driver *drv = _drv;
3096fa45a22SSudip Mukherjee 
3106fa45a22SSudip Mukherjee 	if (is_parport(dev) && drv->detach)
3116fa45a22SSudip Mukherjee 		drv->detach(to_parport_dev(dev));
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	return 0;
3141da177e4SLinus Torvalds }
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds /**
3171da177e4SLinus Torvalds  *	parport_unregister_driver - deregister a parallel port device driver
3181da177e4SLinus Torvalds  *	@drv: structure describing the driver that was given to
3191da177e4SLinus Torvalds  *	      parport_register_driver()
3201da177e4SLinus Torvalds  *
3211da177e4SLinus Torvalds  *	This should be called by a parallel port device driver that
3221da177e4SLinus Torvalds  *	has registered itself using parport_register_driver() when it
3231da177e4SLinus Torvalds  *	is about to be unloaded.
3241da177e4SLinus Torvalds  *
3251da177e4SLinus Torvalds  *	When it returns, the driver's attach() routine will no longer
3261da177e4SLinus Torvalds  *	be called, and for each port that attach() was called for, the
3271da177e4SLinus Torvalds  *	detach() routine will have been called.
3281da177e4SLinus Torvalds  *
3291da177e4SLinus Torvalds  *	All the driver's attach() and detach() calls are guaranteed to have
3301da177e4SLinus Torvalds  *	finished by the time this function returns.
3311da177e4SLinus Torvalds  **/
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds void parport_unregister_driver(struct parport_driver *drv)
3341da177e4SLinus Torvalds {
3351da177e4SLinus Torvalds 	struct parport *port;
3361da177e4SLinus Torvalds 
33714cc3e2bSIngo Molnar 	mutex_lock(&registration_lock);
3386fa45a22SSudip Mukherjee 	if (drv->devmodel) {
3396fa45a22SSudip Mukherjee 		bus_for_each_dev(&parport_bus_type, NULL, drv, port_detach);
3406fa45a22SSudip Mukherjee 		driver_unregister(&drv->driver);
3416fa45a22SSudip Mukherjee 	} else {
3421da177e4SLinus Torvalds 		list_del_init(&drv->list);
3431da177e4SLinus Torvalds 		list_for_each_entry(port, &portlist, list)
3441da177e4SLinus Torvalds 			drv->detach(port);
3456fa45a22SSudip Mukherjee 	}
34614cc3e2bSIngo Molnar 	mutex_unlock(&registration_lock);
3471da177e4SLinus Torvalds }
34827c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_unregister_driver);
3491da177e4SLinus Torvalds 
3506fa45a22SSudip Mukherjee static void free_port(struct device *dev)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	int d;
3536fa45a22SSudip Mukherjee 	struct parport *port = to_parport_dev(dev);
3546fa45a22SSudip Mukherjee 
3551da177e4SLinus Torvalds 	spin_lock(&full_list_lock);
3561da177e4SLinus Torvalds 	list_del(&port->full_list);
3571da177e4SLinus Torvalds 	spin_unlock(&full_list_lock);
3581da177e4SLinus Torvalds 	for (d = 0; d < 5; d++) {
3591da177e4SLinus Torvalds 		kfree(port->probe_info[d].class_name);
3601da177e4SLinus Torvalds 		kfree(port->probe_info[d].mfr);
3611da177e4SLinus Torvalds 		kfree(port->probe_info[d].model);
3621da177e4SLinus Torvalds 		kfree(port->probe_info[d].cmdset);
3631da177e4SLinus Torvalds 		kfree(port->probe_info[d].description);
3641da177e4SLinus Torvalds 	}
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds 	kfree(port->name);
3671da177e4SLinus Torvalds 	kfree(port);
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds /**
3711da177e4SLinus Torvalds  *	parport_get_port - increment a port's reference count
3721da177e4SLinus Torvalds  *	@port: the port
3731da177e4SLinus Torvalds  *
3747d469722SRandy Dunlap  *	This ensures that a struct parport pointer remains valid
3751da177e4SLinus Torvalds  *	until the matching parport_put_port() call.
3761da177e4SLinus Torvalds  **/
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds struct parport *parport_get_port(struct parport *port)
3791da177e4SLinus Torvalds {
3806fa45a22SSudip Mukherjee 	struct device *dev = get_device(&port->bus_dev);
3816fa45a22SSudip Mukherjee 
3826fa45a22SSudip Mukherjee 	return to_parport_dev(dev);
3831da177e4SLinus Torvalds }
38427c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_get_port);
3851da177e4SLinus Torvalds 
3866fa45a22SSudip Mukherjee void parport_del_port(struct parport *port)
3876fa45a22SSudip Mukherjee {
3886fa45a22SSudip Mukherjee 	device_unregister(&port->bus_dev);
3896fa45a22SSudip Mukherjee }
3906fa45a22SSudip Mukherjee EXPORT_SYMBOL(parport_del_port);
3916fa45a22SSudip Mukherjee 
3921da177e4SLinus Torvalds /**
3931da177e4SLinus Torvalds  *	parport_put_port - decrement a port's reference count
3941da177e4SLinus Torvalds  *	@port: the port
3951da177e4SLinus Torvalds  *
3961da177e4SLinus Torvalds  *	This should be called once for each call to parport_get_port(),
3976fa45a22SSudip Mukherjee  *	once the port is no longer needed. When the reference count reaches
3986fa45a22SSudip Mukherjee  *	zero (port is no longer used), free_port is called.
3991da177e4SLinus Torvalds  **/
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds void parport_put_port(struct parport *port)
4021da177e4SLinus Torvalds {
4036fa45a22SSudip Mukherjee 	put_device(&port->bus_dev);
4041da177e4SLinus Torvalds }
40527c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_put_port);
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds /**
4081da177e4SLinus Torvalds  *	parport_register_port - register a parallel port
4091da177e4SLinus Torvalds  *	@base: base I/O address
4101da177e4SLinus Torvalds  *	@irq: IRQ line
4111da177e4SLinus Torvalds  *	@dma: DMA channel
4121da177e4SLinus Torvalds  *	@ops: pointer to the port driver's port operations structure
4131da177e4SLinus Torvalds  *
4141da177e4SLinus Torvalds  *	When a parallel port (lowlevel) driver finds a port that
4151da177e4SLinus Torvalds  *	should be made available to parallel port device drivers, it
4161da177e4SLinus Torvalds  *	should call parport_register_port().  The @base, @irq, and
4171da177e4SLinus Torvalds  *	@dma parameters are for the convenience of port drivers, and
4181da177e4SLinus Torvalds  *	for ports where they aren't meaningful needn't be set to
4191da177e4SLinus Torvalds  *	anything special.  They can be altered afterwards by adjusting
4201da177e4SLinus Torvalds  *	the relevant members of the parport structure that is returned
4211da177e4SLinus Torvalds  *	and represents the port.  They should not be tampered with
4221da177e4SLinus Torvalds  *	after calling parport_announce_port, however.
4231da177e4SLinus Torvalds  *
4241da177e4SLinus Torvalds  *	If there are parallel port device drivers in the system that
4251da177e4SLinus Torvalds  *	have registered themselves using parport_register_driver(),
4261da177e4SLinus Torvalds  *	they are not told about the port at this time; that is done by
4271da177e4SLinus Torvalds  *	parport_announce_port().
4281da177e4SLinus Torvalds  *
4291da177e4SLinus Torvalds  *	The @ops structure is allocated by the caller, and must not be
4301da177e4SLinus Torvalds  *	deallocated before calling parport_remove_port().
4311da177e4SLinus Torvalds  *
4321da177e4SLinus Torvalds  *	If there is no memory to allocate a new parport structure,
4331da177e4SLinus Torvalds  *	this function will return %NULL.
4341da177e4SLinus Torvalds  **/
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds struct parport *parport_register_port(unsigned long base, int irq, int dma,
4371da177e4SLinus Torvalds 				      struct parport_operations *ops)
4381da177e4SLinus Torvalds {
4391da177e4SLinus Torvalds 	struct list_head *l;
4401da177e4SLinus Torvalds 	struct parport *tmp;
4411da177e4SLinus Torvalds 	int num;
4421da177e4SLinus Torvalds 	int device;
4431da177e4SLinus Torvalds 	char *name;
4446fa45a22SSudip Mukherjee 	int ret;
4451da177e4SLinus Torvalds 
446c32df4e1SAlexandru Gheorghiu 	tmp = kzalloc(sizeof(struct parport), GFP_KERNEL);
44747ec57ecSSudip Mukherjee 	if (!tmp)
4481da177e4SLinus Torvalds 		return NULL;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	/* Init our structure */
4511da177e4SLinus Torvalds 	tmp->base = base;
4521da177e4SLinus Torvalds 	tmp->irq = irq;
4531da177e4SLinus Torvalds 	tmp->dma = dma;
4541da177e4SLinus Torvalds 	tmp->muxport = tmp->daisy = tmp->muxsel = -1;
4551da177e4SLinus Torvalds 	tmp->modes = 0;
4561da177e4SLinus Torvalds 	INIT_LIST_HEAD(&tmp->list);
4571da177e4SLinus Torvalds 	tmp->devices = tmp->cad = NULL;
4581da177e4SLinus Torvalds 	tmp->flags = 0;
4591da177e4SLinus Torvalds 	tmp->ops = ops;
4601da177e4SLinus Torvalds 	tmp->physport = tmp;
4611da177e4SLinus Torvalds 	memset(tmp->probe_info, 0, 5 * sizeof(struct parport_device_info));
4621da177e4SLinus Torvalds 	rwlock_init(&tmp->cad_lock);
4631da177e4SLinus Torvalds 	spin_lock_init(&tmp->waitlist_lock);
4641da177e4SLinus Torvalds 	spin_lock_init(&tmp->pardevice_lock);
4651da177e4SLinus Torvalds 	tmp->ieee1284.mode = IEEE1284_MODE_COMPAT;
4661da177e4SLinus Torvalds 	tmp->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
467a529f150SThomas Gleixner 	sema_init(&tmp->ieee1284.irq, 0);
4681da177e4SLinus Torvalds 	tmp->spintime = parport_default_spintime;
4691da177e4SLinus Torvalds 	atomic_set(&tmp->ref_count, 1);
4701da177e4SLinus Torvalds 	INIT_LIST_HEAD(&tmp->full_list);
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	name = kmalloc(15, GFP_KERNEL);
4731da177e4SLinus Torvalds 	if (!name) {
4741da177e4SLinus Torvalds 		kfree(tmp);
4751da177e4SLinus Torvalds 		return NULL;
4761da177e4SLinus Torvalds 	}
4771da177e4SLinus Torvalds 	/* Search for the lowest free parport number. */
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	spin_lock(&full_list_lock);
4801da177e4SLinus Torvalds 	for (l = all_ports.next, num = 0; l != &all_ports; l = l->next, num++) {
4811da177e4SLinus Torvalds 		struct parport *p = list_entry(l, struct parport, full_list);
4821da177e4SLinus Torvalds 		if (p->number != num)
4831da177e4SLinus Torvalds 			break;
4841da177e4SLinus Torvalds 	}
4851da177e4SLinus Torvalds 	tmp->portnum = tmp->number = num;
4861da177e4SLinus Torvalds 	list_add_tail(&tmp->full_list, l);
4871da177e4SLinus Torvalds 	spin_unlock(&full_list_lock);
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	/*
4901da177e4SLinus Torvalds 	 * Now that the portnum is known finish doing the Init.
4911da177e4SLinus Torvalds 	 */
4921da177e4SLinus Torvalds 	sprintf(name, "parport%d", tmp->portnum = tmp->number);
4931da177e4SLinus Torvalds 	tmp->name = name;
4946fa45a22SSudip Mukherjee 	tmp->bus_dev.bus = &parport_bus_type;
4956fa45a22SSudip Mukherjee 	tmp->bus_dev.release = free_port;
4966fa45a22SSudip Mukherjee 	dev_set_name(&tmp->bus_dev, name);
4976fa45a22SSudip Mukherjee 	tmp->bus_dev.type = &parport_device_type;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	for (device = 0; device < 5; device++)
5001da177e4SLinus Torvalds 		/* assume the worst */
5011da177e4SLinus Torvalds 		tmp->probe_info[device].class = PARPORT_CLASS_LEGACY;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	tmp->waithead = tmp->waittail = NULL;
5041da177e4SLinus Torvalds 
5056fa45a22SSudip Mukherjee 	ret = device_register(&tmp->bus_dev);
5066fa45a22SSudip Mukherjee 	if (ret) {
5076fa45a22SSudip Mukherjee 		put_device(&tmp->bus_dev);
5086fa45a22SSudip Mukherjee 		return NULL;
5096fa45a22SSudip Mukherjee 	}
5106fa45a22SSudip Mukherjee 
5111da177e4SLinus Torvalds 	return tmp;
5121da177e4SLinus Torvalds }
51327c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_register_port);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds /**
5161da177e4SLinus Torvalds  *	parport_announce_port - tell device drivers about a parallel port
5171da177e4SLinus Torvalds  *	@port: parallel port to announce
5181da177e4SLinus Torvalds  *
5191da177e4SLinus Torvalds  *	After a port driver has registered a parallel port with
5201da177e4SLinus Torvalds  *	parport_register_port, and performed any necessary
5211da177e4SLinus Torvalds  *	initialisation or adjustments, it should call
5221da177e4SLinus Torvalds  *	parport_announce_port() in order to notify all device drivers
5231da177e4SLinus Torvalds  *	that have called parport_register_driver().  Their attach()
5241da177e4SLinus Torvalds  *	functions will be called, with @port as the parameter.
5251da177e4SLinus Torvalds  **/
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds void parport_announce_port(struct parport *port)
5281da177e4SLinus Torvalds {
5291da177e4SLinus Torvalds 	int i;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
5321da177e4SLinus Torvalds 	/* Analyse the IEEE1284.3 topology of the port. */
5331da177e4SLinus Torvalds 	parport_daisy_init(port);
5341da177e4SLinus Torvalds #endif
5351da177e4SLinus Torvalds 
536c15a3837SDavid Brownell 	if (!port->dev)
537a162188fSSudip Mukherjee 		printk(KERN_WARNING "%s: fix this legacy no-device port driver!\n",
538c15a3837SDavid Brownell 		       port->name);
539c15a3837SDavid Brownell 
5401da177e4SLinus Torvalds 	parport_proc_register(port);
54114cc3e2bSIngo Molnar 	mutex_lock(&registration_lock);
5421da177e4SLinus Torvalds 	spin_lock_irq(&parportlist_lock);
5431da177e4SLinus Torvalds 	list_add_tail(&port->list, &portlist);
5441da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
5451da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
5461da177e4SLinus Torvalds 		if (slave)
5471da177e4SLinus Torvalds 			list_add_tail(&slave->list, &portlist);
5481da177e4SLinus Torvalds 	}
5491da177e4SLinus Torvalds 	spin_unlock_irq(&parportlist_lock);
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	/* Let drivers know that new port(s) has arrived. */
5521da177e4SLinus Torvalds 	attach_driver_chain(port);
5531da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
5541da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
5551da177e4SLinus Torvalds 		if (slave)
5561da177e4SLinus Torvalds 			attach_driver_chain(slave);
5571da177e4SLinus Torvalds 	}
55814cc3e2bSIngo Molnar 	mutex_unlock(&registration_lock);
5591da177e4SLinus Torvalds }
56027c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_announce_port);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds /**
5631da177e4SLinus Torvalds  *	parport_remove_port - deregister a parallel port
5641da177e4SLinus Torvalds  *	@port: parallel port to deregister
5651da177e4SLinus Torvalds  *
5661da177e4SLinus Torvalds  *	When a parallel port driver is forcibly unloaded, or a
5671da177e4SLinus Torvalds  *	parallel port becomes inaccessible, the port driver must call
5681da177e4SLinus Torvalds  *	this function in order to deal with device drivers that still
5691da177e4SLinus Torvalds  *	want to use it.
5701da177e4SLinus Torvalds  *
5711da177e4SLinus Torvalds  *	The parport structure associated with the port has its
5721da177e4SLinus Torvalds  *	operations structure replaced with one containing 'null'
5731da177e4SLinus Torvalds  *	operations that return errors or just don't do anything.
5741da177e4SLinus Torvalds  *
5751da177e4SLinus Torvalds  *	Any drivers that have registered themselves using
5761da177e4SLinus Torvalds  *	parport_register_driver() are notified that the port is no
5771da177e4SLinus Torvalds  *	longer accessible by having their detach() routines called
5781da177e4SLinus Torvalds  *	with @port as the parameter.
5791da177e4SLinus Torvalds  **/
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds void parport_remove_port(struct parport *port)
5821da177e4SLinus Torvalds {
5831da177e4SLinus Torvalds 	int i;
5841da177e4SLinus Torvalds 
58514cc3e2bSIngo Molnar 	mutex_lock(&registration_lock);
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	/* Spread the word. */
5881da177e4SLinus Torvalds 	detach_driver_chain(port);
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
5911da177e4SLinus Torvalds 	/* Forget the IEEE1284.3 topology of the port. */
5921da177e4SLinus Torvalds 	parport_daisy_fini(port);
5931da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
5941da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
5951da177e4SLinus Torvalds 		if (!slave)
5961da177e4SLinus Torvalds 			continue;
5971da177e4SLinus Torvalds 		detach_driver_chain(slave);
5981da177e4SLinus Torvalds 		parport_daisy_fini(slave);
5991da177e4SLinus Torvalds 	}
6001da177e4SLinus Torvalds #endif
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	port->ops = &dead_ops;
6031da177e4SLinus Torvalds 	spin_lock(&parportlist_lock);
6041da177e4SLinus Torvalds 	list_del_init(&port->list);
6051da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
6061da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
6071da177e4SLinus Torvalds 		if (slave)
6081da177e4SLinus Torvalds 			list_del_init(&slave->list);
6091da177e4SLinus Torvalds 	}
6101da177e4SLinus Torvalds 	spin_unlock(&parportlist_lock);
6111da177e4SLinus Torvalds 
61214cc3e2bSIngo Molnar 	mutex_unlock(&registration_lock);
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	parport_proc_unregister(port);
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
6171da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
6181da177e4SLinus Torvalds 		if (slave)
6191da177e4SLinus Torvalds 			parport_put_port(slave);
6201da177e4SLinus Torvalds 	}
6211da177e4SLinus Torvalds }
62227c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_remove_port);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds /**
6251da177e4SLinus Torvalds  *	parport_register_device - register a device on a parallel port
6261da177e4SLinus Torvalds  *	@port: port to which the device is attached
6271da177e4SLinus Torvalds  *	@name: a name to refer to the device
6281da177e4SLinus Torvalds  *	@pf: preemption callback
6291da177e4SLinus Torvalds  *	@kf: kick callback (wake-up)
6301da177e4SLinus Torvalds  *	@irq_func: interrupt handler
6311da177e4SLinus Torvalds  *	@flags: registration flags
6321da177e4SLinus Torvalds  *	@handle: data for callback functions
6331da177e4SLinus Torvalds  *
6341da177e4SLinus Torvalds  *	This function, called by parallel port device drivers,
6351da177e4SLinus Torvalds  *	declares that a device is connected to a port, and tells the
6361da177e4SLinus Torvalds  *	system all it needs to know.
6371da177e4SLinus Torvalds  *
6381da177e4SLinus Torvalds  *	The @name is allocated by the caller and must not be
6391da177e4SLinus Torvalds  *	deallocated until the caller calls @parport_unregister_device
6401da177e4SLinus Torvalds  *	for that device.
6411da177e4SLinus Torvalds  *
6421da177e4SLinus Torvalds  *	The preemption callback function, @pf, is called when this
6431da177e4SLinus Torvalds  *	device driver has claimed access to the port but another
6441da177e4SLinus Torvalds  *	device driver wants to use it.  It is given @handle as its
6451da177e4SLinus Torvalds  *	parameter, and should return zero if it is willing for the
6461da177e4SLinus Torvalds  *	system to release the port to another driver on its behalf.
6471da177e4SLinus Torvalds  *	If it wants to keep control of the port it should return
6481da177e4SLinus Torvalds  *	non-zero, and no action will be taken.  It is good manners for
6491da177e4SLinus Torvalds  *	the driver to try to release the port at the earliest
6501da177e4SLinus Torvalds  *	opportunity after its preemption callback rejects a preemption
6511da177e4SLinus Torvalds  *	attempt.  Note that if a preemption callback is happy for
6521da177e4SLinus Torvalds  *	preemption to go ahead, there is no need to release the port;
6531da177e4SLinus Torvalds  *	it is done automatically.  This function may not block, as it
6541da177e4SLinus Torvalds  *	may be called from interrupt context.  If the device driver
6551da177e4SLinus Torvalds  *	does not support preemption, @pf can be %NULL.
6561da177e4SLinus Torvalds  *
6571da177e4SLinus Torvalds  *	The wake-up ("kick") callback function, @kf, is called when
6581da177e4SLinus Torvalds  *	the port is available to be claimed for exclusive access; that
6591da177e4SLinus Torvalds  *	is, parport_claim() is guaranteed to succeed when called from
6601da177e4SLinus Torvalds  *	inside the wake-up callback function.  If the driver wants to
6611da177e4SLinus Torvalds  *	claim the port it should do so; otherwise, it need not take
6621da177e4SLinus Torvalds  *	any action.  This function may not block, as it may be called
6631da177e4SLinus Torvalds  *	from interrupt context.  If the device driver does not want to
6641da177e4SLinus Torvalds  *	be explicitly invited to claim the port in this way, @kf can
6651da177e4SLinus Torvalds  *	be %NULL.
6661da177e4SLinus Torvalds  *
6671da177e4SLinus Torvalds  *	The interrupt handler, @irq_func, is called when an interrupt
6681da177e4SLinus Torvalds  *	arrives from the parallel port.  Note that if a device driver
6691da177e4SLinus Torvalds  *	wants to use interrupts it should use parport_enable_irq(),
6701da177e4SLinus Torvalds  *	and can also check the irq member of the parport structure
6711da177e4SLinus Torvalds  *	representing the port.
6721da177e4SLinus Torvalds  *
6731da177e4SLinus Torvalds  *	The parallel port (lowlevel) driver is the one that has called
6741da177e4SLinus Torvalds  *	request_irq() and whose interrupt handler is called first.
6751da177e4SLinus Torvalds  *	This handler does whatever needs to be done to the hardware to
6761da177e4SLinus Torvalds  *	acknowledge the interrupt (for PC-style ports there is nothing
6771da177e4SLinus Torvalds  *	special to be done).  It then tells the IEEE 1284 code about
6781da177e4SLinus Torvalds  *	the interrupt, which may involve reacting to an IEEE 1284
6791da177e4SLinus Torvalds  *	event depending on the current IEEE 1284 phase.  After this,
6801da177e4SLinus Torvalds  *	it calls @irq_func.  Needless to say, @irq_func will be called
6811da177e4SLinus Torvalds  *	from interrupt context, and may not block.
6821da177e4SLinus Torvalds  *
6831da177e4SLinus Torvalds  *	The %PARPORT_DEV_EXCL flag is for preventing port sharing, and
6841da177e4SLinus Torvalds  *	so should only be used when sharing the port with other device
6851da177e4SLinus Torvalds  *	drivers is impossible and would lead to incorrect behaviour.
6861da177e4SLinus Torvalds  *	Use it sparingly!  Normally, @flags will be zero.
6871da177e4SLinus Torvalds  *
6881da177e4SLinus Torvalds  *	This function returns a pointer to a structure that represents
6891da177e4SLinus Torvalds  *	the device on the port, or %NULL if there is not enough memory
6901da177e4SLinus Torvalds  *	to allocate space for that structure.
6911da177e4SLinus Torvalds  **/
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds struct pardevice *
6941da177e4SLinus Torvalds parport_register_device(struct parport *port, const char *name,
6951da177e4SLinus Torvalds 			int (*pf)(void *), void (*kf)(void *),
6965712cb3dSJeff Garzik 			void (*irq_func)(void *),
6971da177e4SLinus Torvalds 			int flags, void *handle)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	struct pardevice *tmp;
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds 	if (port->physport->flags & PARPORT_FLAG_EXCL) {
7021da177e4SLinus Torvalds 		/* An exclusive device is registered. */
7031da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: no more devices allowed\n",
7041da177e4SLinus Torvalds 			port->name);
7051da177e4SLinus Torvalds 		return NULL;
7061da177e4SLinus Torvalds 	}
7071da177e4SLinus Torvalds 
7081da177e4SLinus Torvalds 	if (flags & PARPORT_DEV_LURK) {
7091da177e4SLinus Torvalds 		if (!pf || !kf) {
7101da177e4SLinus Torvalds 			printk(KERN_INFO "%s: refused to register lurking device (%s) without callbacks\n", port->name, name);
7111da177e4SLinus Torvalds 			return NULL;
7121da177e4SLinus Torvalds 		}
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 
71550566ac8SSudip Mukherjee 	if (flags & PARPORT_DEV_EXCL) {
71650566ac8SSudip Mukherjee 		if (port->physport->devices) {
71750566ac8SSudip Mukherjee 			/*
71850566ac8SSudip Mukherjee 			 * If a device is already registered and this new
71950566ac8SSudip Mukherjee 			 * device wants exclusive access, then no need to
72050566ac8SSudip Mukherjee 			 * continue as we can not grant exclusive access to
72150566ac8SSudip Mukherjee 			 * this device.
72250566ac8SSudip Mukherjee 			 */
72350566ac8SSudip Mukherjee 			pr_err("%s: cannot grant exclusive access for device %s\n",
72450566ac8SSudip Mukherjee 			       port->name, name);
72550566ac8SSudip Mukherjee 			return NULL;
72650566ac8SSudip Mukherjee 		}
72750566ac8SSudip Mukherjee 	}
72850566ac8SSudip Mukherjee 
7297b7a0a30SSudip Mukherjee 	/*
7307b7a0a30SSudip Mukherjee 	 * We up our own module reference count, and that of the port
7317b7a0a30SSudip Mukherjee 	 * on which a device is to be registered, to ensure that
7327b7a0a30SSudip Mukherjee 	 * neither of us gets unloaded while we sleep in (e.g.)
7337b7a0a30SSudip Mukherjee 	 * kmalloc.
7341da177e4SLinus Torvalds 	 */
73513efa75dSSudip Mukherjee 	if (!try_module_get(port->ops->owner))
7361da177e4SLinus Torvalds 		return NULL;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	parport_get_port(port);
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	tmp = kmalloc(sizeof(struct pardevice), GFP_KERNEL);
741b95b9d6cSSudip Mukherjee 	if (!tmp)
7421da177e4SLinus Torvalds 		goto out;
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	tmp->state = kmalloc(sizeof(struct parport_state), GFP_KERNEL);
745b95b9d6cSSudip Mukherjee 	if (!tmp->state)
7461da177e4SLinus Torvalds 		goto out_free_pardevice;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	tmp->name = name;
7491da177e4SLinus Torvalds 	tmp->port = port;
7501da177e4SLinus Torvalds 	tmp->daisy = -1;
7511da177e4SLinus Torvalds 	tmp->preempt = pf;
7521da177e4SLinus Torvalds 	tmp->wakeup = kf;
7531da177e4SLinus Torvalds 	tmp->private = handle;
7541da177e4SLinus Torvalds 	tmp->flags = flags;
7551da177e4SLinus Torvalds 	tmp->irq_func = irq_func;
7561da177e4SLinus Torvalds 	tmp->waiting = 0;
7571da177e4SLinus Torvalds 	tmp->timeout = 5 * HZ;
7586fa45a22SSudip Mukherjee 	tmp->devmodel = false;
7591da177e4SLinus Torvalds 
7601da177e4SLinus Torvalds 	/* Chain this onto the list */
7611da177e4SLinus Torvalds 	tmp->prev = NULL;
7621da177e4SLinus Torvalds 	/*
7631da177e4SLinus Torvalds 	 * This function must not run from an irq handler so we don' t need
7641da177e4SLinus Torvalds 	 * to clear irq on the local CPU. -arca
7651da177e4SLinus Torvalds 	 */
7661da177e4SLinus Torvalds 	spin_lock(&port->physport->pardevice_lock);
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	if (flags & PARPORT_DEV_EXCL) {
7691da177e4SLinus Torvalds 		if (port->physport->devices) {
7701da177e4SLinus Torvalds 			spin_unlock(&port->physport->pardevice_lock);
7711da177e4SLinus Torvalds 			printk(KERN_DEBUG
772a162188fSSudip Mukherjee 				"%s: cannot grant exclusive access for device %s\n",
773a162188fSSudip Mukherjee 				port->name, name);
7741da177e4SLinus Torvalds 			goto out_free_all;
7751da177e4SLinus Torvalds 		}
7761da177e4SLinus Torvalds 		port->flags |= PARPORT_FLAG_EXCL;
7771da177e4SLinus Torvalds 	}
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	tmp->next = port->physport->devices;
7807b7a0a30SSudip Mukherjee 	wmb(); /*
7817b7a0a30SSudip Mukherjee 		* Make sure that tmp->next is written before it's
7827b7a0a30SSudip Mukherjee 		* added to the list; see comments marked 'no locking
7837b7a0a30SSudip Mukherjee 		* required'
7847b7a0a30SSudip Mukherjee 		*/
7851da177e4SLinus Torvalds 	if (port->physport->devices)
7861da177e4SLinus Torvalds 		port->physport->devices->prev = tmp;
7871da177e4SLinus Torvalds 	port->physport->devices = tmp;
7881da177e4SLinus Torvalds 	spin_unlock(&port->physport->pardevice_lock);
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	init_waitqueue_head(&tmp->wait_q);
7911da177e4SLinus Torvalds 	tmp->timeslice = parport_default_timeslice;
7921da177e4SLinus Torvalds 	tmp->waitnext = tmp->waitprev = NULL;
7931da177e4SLinus Torvalds 
7941da177e4SLinus Torvalds 	/*
7951da177e4SLinus Torvalds 	 * This has to be run as last thing since init_state may need other
7961da177e4SLinus Torvalds 	 * pardevice fields. -arca
7971da177e4SLinus Torvalds 	 */
7981da177e4SLinus Torvalds 	port->ops->init_state(tmp, tmp->state);
79905ad709dSAlan Cox 	if (!test_and_set_bit(PARPORT_DEVPROC_REGISTERED, &port->devflags)) {
80005ad709dSAlan Cox 		port->proc_device = tmp;
8011da177e4SLinus Torvalds 		parport_device_proc_register(tmp);
80205ad709dSAlan Cox 	}
8031da177e4SLinus Torvalds 	return tmp;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds  out_free_all:
8061da177e4SLinus Torvalds 	kfree(tmp->state);
8071da177e4SLinus Torvalds  out_free_pardevice:
8081da177e4SLinus Torvalds 	kfree(tmp);
8091da177e4SLinus Torvalds  out:
8101da177e4SLinus Torvalds 	parport_put_port(port);
8111da177e4SLinus Torvalds 	module_put(port->ops->owner);
8121da177e4SLinus Torvalds 
8131da177e4SLinus Torvalds 	return NULL;
8141da177e4SLinus Torvalds }
81527c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_register_device);
8161da177e4SLinus Torvalds 
8176fa45a22SSudip Mukherjee static void free_pardevice(struct device *dev)
8186fa45a22SSudip Mukherjee {
8196fa45a22SSudip Mukherjee 	struct pardevice *par_dev = to_pardevice(dev);
8206fa45a22SSudip Mukherjee 
8216fa45a22SSudip Mukherjee 	kfree(par_dev->name);
8226fa45a22SSudip Mukherjee 	kfree(par_dev);
8236fa45a22SSudip Mukherjee }
8246fa45a22SSudip Mukherjee 
8256fa45a22SSudip Mukherjee struct pardevice *
8266fa45a22SSudip Mukherjee parport_register_dev_model(struct parport *port, const char *name,
8276fa45a22SSudip Mukherjee 			   const struct pardev_cb *par_dev_cb, int id)
8286fa45a22SSudip Mukherjee {
8296fa45a22SSudip Mukherjee 	struct pardevice *par_dev;
8306fa45a22SSudip Mukherjee 	int ret;
8316fa45a22SSudip Mukherjee 	char *devname;
8326fa45a22SSudip Mukherjee 
8336fa45a22SSudip Mukherjee 	if (port->physport->flags & PARPORT_FLAG_EXCL) {
8346fa45a22SSudip Mukherjee 		/* An exclusive device is registered. */
8356fa45a22SSudip Mukherjee 		pr_err("%s: no more devices allowed\n", port->name);
8366fa45a22SSudip Mukherjee 		return NULL;
8376fa45a22SSudip Mukherjee 	}
8386fa45a22SSudip Mukherjee 
8396fa45a22SSudip Mukherjee 	if (par_dev_cb->flags & PARPORT_DEV_LURK) {
8406fa45a22SSudip Mukherjee 		if (!par_dev_cb->preempt || !par_dev_cb->wakeup) {
8416fa45a22SSudip Mukherjee 			pr_info("%s: refused to register lurking device (%s) without callbacks\n",
8426fa45a22SSudip Mukherjee 				port->name, name);
8436fa45a22SSudip Mukherjee 			return NULL;
8446fa45a22SSudip Mukherjee 		}
8456fa45a22SSudip Mukherjee 	}
8466fa45a22SSudip Mukherjee 
84750566ac8SSudip Mukherjee 	if (par_dev_cb->flags & PARPORT_DEV_EXCL) {
84850566ac8SSudip Mukherjee 		if (port->physport->devices) {
84950566ac8SSudip Mukherjee 			/*
85050566ac8SSudip Mukherjee 			 * If a device is already registered and this new
85150566ac8SSudip Mukherjee 			 * device wants exclusive access, then no need to
85250566ac8SSudip Mukherjee 			 * continue as we can not grant exclusive access to
85350566ac8SSudip Mukherjee 			 * this device.
85450566ac8SSudip Mukherjee 			 */
85550566ac8SSudip Mukherjee 			pr_err("%s: cannot grant exclusive access for device %s\n",
85650566ac8SSudip Mukherjee 			       port->name, name);
85750566ac8SSudip Mukherjee 			return NULL;
85850566ac8SSudip Mukherjee 		}
85950566ac8SSudip Mukherjee 	}
86050566ac8SSudip Mukherjee 
8616fa45a22SSudip Mukherjee 	if (!try_module_get(port->ops->owner))
8626fa45a22SSudip Mukherjee 		return NULL;
8636fa45a22SSudip Mukherjee 
8646fa45a22SSudip Mukherjee 	parport_get_port(port);
8656fa45a22SSudip Mukherjee 
8666fa45a22SSudip Mukherjee 	par_dev = kzalloc(sizeof(*par_dev), GFP_KERNEL);
8676fa45a22SSudip Mukherjee 	if (!par_dev)
8686fa45a22SSudip Mukherjee 		goto err_put_port;
8696fa45a22SSudip Mukherjee 
8706fa45a22SSudip Mukherjee 	par_dev->state = kzalloc(sizeof(*par_dev->state), GFP_KERNEL);
8716fa45a22SSudip Mukherjee 	if (!par_dev->state)
8726fa45a22SSudip Mukherjee 		goto err_put_par_dev;
8736fa45a22SSudip Mukherjee 
8746fa45a22SSudip Mukherjee 	devname = kstrdup(name, GFP_KERNEL);
8756fa45a22SSudip Mukherjee 	if (!devname)
8766fa45a22SSudip Mukherjee 		goto err_free_par_dev;
8776fa45a22SSudip Mukherjee 
8786fa45a22SSudip Mukherjee 	par_dev->name = devname;
8796fa45a22SSudip Mukherjee 	par_dev->port = port;
8806fa45a22SSudip Mukherjee 	par_dev->daisy = -1;
8816fa45a22SSudip Mukherjee 	par_dev->preempt = par_dev_cb->preempt;
8826fa45a22SSudip Mukherjee 	par_dev->wakeup = par_dev_cb->wakeup;
8836fa45a22SSudip Mukherjee 	par_dev->private = par_dev_cb->private;
8846fa45a22SSudip Mukherjee 	par_dev->flags = par_dev_cb->flags;
8856fa45a22SSudip Mukherjee 	par_dev->irq_func = par_dev_cb->irq_func;
8866fa45a22SSudip Mukherjee 	par_dev->waiting = 0;
8876fa45a22SSudip Mukherjee 	par_dev->timeout = 5 * HZ;
8886fa45a22SSudip Mukherjee 
8896fa45a22SSudip Mukherjee 	par_dev->dev.parent = &port->bus_dev;
8906fa45a22SSudip Mukherjee 	par_dev->dev.bus = &parport_bus_type;
8916fa45a22SSudip Mukherjee 	ret = dev_set_name(&par_dev->dev, "%s.%d", devname, id);
8926fa45a22SSudip Mukherjee 	if (ret)
8936fa45a22SSudip Mukherjee 		goto err_free_devname;
8946fa45a22SSudip Mukherjee 	par_dev->dev.release = free_pardevice;
8956fa45a22SSudip Mukherjee 	par_dev->devmodel = true;
8966fa45a22SSudip Mukherjee 	ret = device_register(&par_dev->dev);
89768d35c7bSSudip Mukherjee 	if (ret) {
89868d35c7bSSudip Mukherjee 		put_device(&par_dev->dev);
89968d35c7bSSudip Mukherjee 		goto err_put_port;
90068d35c7bSSudip Mukherjee 	}
9016fa45a22SSudip Mukherjee 
9026fa45a22SSudip Mukherjee 	/* Chain this onto the list */
9036fa45a22SSudip Mukherjee 	par_dev->prev = NULL;
9046fa45a22SSudip Mukherjee 	/*
9056fa45a22SSudip Mukherjee 	 * This function must not run from an irq handler so we don' t need
9066fa45a22SSudip Mukherjee 	 * to clear irq on the local CPU. -arca
9076fa45a22SSudip Mukherjee 	 */
9086fa45a22SSudip Mukherjee 	spin_lock(&port->physport->pardevice_lock);
9096fa45a22SSudip Mukherjee 
9106fa45a22SSudip Mukherjee 	if (par_dev_cb->flags & PARPORT_DEV_EXCL) {
9116fa45a22SSudip Mukherjee 		if (port->physport->devices) {
9126fa45a22SSudip Mukherjee 			spin_unlock(&port->physport->pardevice_lock);
9136fa45a22SSudip Mukherjee 			pr_debug("%s: cannot grant exclusive access for device %s\n",
9146fa45a22SSudip Mukherjee 				 port->name, name);
915cabea695SSudip Mukherjee 			device_unregister(&par_dev->dev);
916cabea695SSudip Mukherjee 			goto err_put_port;
9176fa45a22SSudip Mukherjee 		}
9186fa45a22SSudip Mukherjee 		port->flags |= PARPORT_FLAG_EXCL;
9196fa45a22SSudip Mukherjee 	}
9206fa45a22SSudip Mukherjee 
9216fa45a22SSudip Mukherjee 	par_dev->next = port->physport->devices;
9226fa45a22SSudip Mukherjee 	wmb();	/*
9236fa45a22SSudip Mukherjee 		 * Make sure that tmp->next is written before it's
9246fa45a22SSudip Mukherjee 		 * added to the list; see comments marked 'no locking
9256fa45a22SSudip Mukherjee 		 * required'
9266fa45a22SSudip Mukherjee 		 */
9276fa45a22SSudip Mukherjee 	if (port->physport->devices)
9286fa45a22SSudip Mukherjee 		port->physport->devices->prev = par_dev;
9296fa45a22SSudip Mukherjee 	port->physport->devices = par_dev;
9306fa45a22SSudip Mukherjee 	spin_unlock(&port->physport->pardevice_lock);
9316fa45a22SSudip Mukherjee 
9326fa45a22SSudip Mukherjee 	init_waitqueue_head(&par_dev->wait_q);
9336fa45a22SSudip Mukherjee 	par_dev->timeslice = parport_default_timeslice;
9346fa45a22SSudip Mukherjee 	par_dev->waitnext = NULL;
9356fa45a22SSudip Mukherjee 	par_dev->waitprev = NULL;
9366fa45a22SSudip Mukherjee 
9376fa45a22SSudip Mukherjee 	/*
9386fa45a22SSudip Mukherjee 	 * This has to be run as last thing since init_state may need other
9396fa45a22SSudip Mukherjee 	 * pardevice fields. -arca
9406fa45a22SSudip Mukherjee 	 */
9416fa45a22SSudip Mukherjee 	port->ops->init_state(par_dev, par_dev->state);
9426fa45a22SSudip Mukherjee 	port->proc_device = par_dev;
9436fa45a22SSudip Mukherjee 	parport_device_proc_register(par_dev);
9446fa45a22SSudip Mukherjee 
9456fa45a22SSudip Mukherjee 	return par_dev;
9466fa45a22SSudip Mukherjee 
9476fa45a22SSudip Mukherjee err_free_devname:
9486fa45a22SSudip Mukherjee 	kfree(devname);
9496fa45a22SSudip Mukherjee err_free_par_dev:
9506fa45a22SSudip Mukherjee 	kfree(par_dev->state);
9516fa45a22SSudip Mukherjee err_put_par_dev:
9526fa45a22SSudip Mukherjee 	if (!par_dev->devmodel)
9536fa45a22SSudip Mukherjee 		kfree(par_dev);
9546fa45a22SSudip Mukherjee err_put_port:
9556fa45a22SSudip Mukherjee 	parport_put_port(port);
9566fa45a22SSudip Mukherjee 	module_put(port->ops->owner);
9576fa45a22SSudip Mukherjee 
9586fa45a22SSudip Mukherjee 	return NULL;
9596fa45a22SSudip Mukherjee }
9606fa45a22SSudip Mukherjee EXPORT_SYMBOL(parport_register_dev_model);
9616fa45a22SSudip Mukherjee 
9621da177e4SLinus Torvalds /**
9631da177e4SLinus Torvalds  *	parport_unregister_device - deregister a device on a parallel port
9641da177e4SLinus Torvalds  *	@dev: pointer to structure representing device
9651da177e4SLinus Torvalds  *
9661da177e4SLinus Torvalds  *	This undoes the effect of parport_register_device().
9671da177e4SLinus Torvalds  **/
9681da177e4SLinus Torvalds 
9691da177e4SLinus Torvalds void parport_unregister_device(struct pardevice *dev)
9701da177e4SLinus Torvalds {
9711da177e4SLinus Torvalds 	struct parport *port;
9721da177e4SLinus Torvalds 
9731da177e4SLinus Torvalds #ifdef PARPORT_PARANOID
974b95b9d6cSSudip Mukherjee 	if (!dev) {
9751da177e4SLinus Torvalds 		printk(KERN_ERR "parport_unregister_device: passed NULL\n");
9761da177e4SLinus Torvalds 		return;
9771da177e4SLinus Torvalds 	}
9781da177e4SLinus Torvalds #endif
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds 	port = dev->port->physport;
9811da177e4SLinus Torvalds 
98205ad709dSAlan Cox 	if (port->proc_device == dev) {
98305ad709dSAlan Cox 		port->proc_device = NULL;
98405ad709dSAlan Cox 		clear_bit(PARPORT_DEVPROC_REGISTERED, &port->devflags);
98505ad709dSAlan Cox 		parport_device_proc_unregister(dev);
98605ad709dSAlan Cox 	}
98705ad709dSAlan Cox 
9881da177e4SLinus Torvalds 	if (port->cad == dev) {
9891da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: %s forgot to release port\n",
9901da177e4SLinus Torvalds 		       port->name, dev->name);
9911da177e4SLinus Torvalds 		parport_release(dev);
9921da177e4SLinus Torvalds 	}
9931da177e4SLinus Torvalds 
9941da177e4SLinus Torvalds 	spin_lock(&port->pardevice_lock);
9951da177e4SLinus Torvalds 	if (dev->next)
9961da177e4SLinus Torvalds 		dev->next->prev = dev->prev;
9971da177e4SLinus Torvalds 	if (dev->prev)
9981da177e4SLinus Torvalds 		dev->prev->next = dev->next;
9991da177e4SLinus Torvalds 	else
10001da177e4SLinus Torvalds 		port->devices = dev->next;
10011da177e4SLinus Torvalds 
10021da177e4SLinus Torvalds 	if (dev->flags & PARPORT_DEV_EXCL)
10031da177e4SLinus Torvalds 		port->flags &= ~PARPORT_FLAG_EXCL;
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	spin_unlock(&port->pardevice_lock);
10061da177e4SLinus Torvalds 
10077b7a0a30SSudip Mukherjee 	/*
10087b7a0a30SSudip Mukherjee 	 * Make sure we haven't left any pointers around in the wait
10097b7a0a30SSudip Mukherjee 	 * list.
10107b7a0a30SSudip Mukherjee 	 */
1011cbeb4b7aSAlexander Gordeev 	spin_lock_irq(&port->waitlist_lock);
10121da177e4SLinus Torvalds 	if (dev->waitprev || dev->waitnext || port->waithead == dev) {
10131da177e4SLinus Torvalds 		if (dev->waitprev)
10141da177e4SLinus Torvalds 			dev->waitprev->waitnext = dev->waitnext;
10151da177e4SLinus Torvalds 		else
10161da177e4SLinus Torvalds 			port->waithead = dev->waitnext;
10171da177e4SLinus Torvalds 		if (dev->waitnext)
10181da177e4SLinus Torvalds 			dev->waitnext->waitprev = dev->waitprev;
10191da177e4SLinus Torvalds 		else
10201da177e4SLinus Torvalds 			port->waittail = dev->waitprev;
10211da177e4SLinus Torvalds 	}
1022cbeb4b7aSAlexander Gordeev 	spin_unlock_irq(&port->waitlist_lock);
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	kfree(dev->state);
10256fa45a22SSudip Mukherjee 	if (dev->devmodel)
10266fa45a22SSudip Mukherjee 		device_unregister(&dev->dev);
10276fa45a22SSudip Mukherjee 	else
10281da177e4SLinus Torvalds 		kfree(dev);
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	module_put(port->ops->owner);
10311da177e4SLinus Torvalds 	parport_put_port(port);
10321da177e4SLinus Torvalds }
103327c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_unregister_device);
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds /**
10361da177e4SLinus Torvalds  *	parport_find_number - find a parallel port by number
10371da177e4SLinus Torvalds  *	@number: parallel port number
10381da177e4SLinus Torvalds  *
10391da177e4SLinus Torvalds  *	This returns the parallel port with the specified number, or
10401da177e4SLinus Torvalds  *	%NULL if there is none.
10411da177e4SLinus Torvalds  *
10421da177e4SLinus Torvalds  *	There is an implicit parport_get_port() done already; to throw
10431da177e4SLinus Torvalds  *	away the reference to the port that parport_find_number()
10441da177e4SLinus Torvalds  *	gives you, use parport_put_port().
10451da177e4SLinus Torvalds  */
10461da177e4SLinus Torvalds 
10471da177e4SLinus Torvalds struct parport *parport_find_number(int number)
10481da177e4SLinus Torvalds {
10491da177e4SLinus Torvalds 	struct parport *port, *result = NULL;
10501da177e4SLinus Torvalds 
10511da177e4SLinus Torvalds 	if (list_empty(&portlist))
10521da177e4SLinus Torvalds 		get_lowlevel_driver();
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	spin_lock(&parportlist_lock);
10551da177e4SLinus Torvalds 	list_for_each_entry(port, &portlist, list) {
10561da177e4SLinus Torvalds 		if (port->number == number) {
10571da177e4SLinus Torvalds 			result = parport_get_port(port);
10581da177e4SLinus Torvalds 			break;
10591da177e4SLinus Torvalds 		}
10601da177e4SLinus Torvalds 	}
10611da177e4SLinus Torvalds 	spin_unlock(&parportlist_lock);
10621da177e4SLinus Torvalds 	return result;
10631da177e4SLinus Torvalds }
106427c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_find_number);
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds /**
10671da177e4SLinus Torvalds  *	parport_find_base - find a parallel port by base address
10681da177e4SLinus Torvalds  *	@base: base I/O address
10691da177e4SLinus Torvalds  *
10701da177e4SLinus Torvalds  *	This returns the parallel port with the specified base
10711da177e4SLinus Torvalds  *	address, or %NULL if there is none.
10721da177e4SLinus Torvalds  *
10731da177e4SLinus Torvalds  *	There is an implicit parport_get_port() done already; to throw
10741da177e4SLinus Torvalds  *	away the reference to the port that parport_find_base()
10751da177e4SLinus Torvalds  *	gives you, use parport_put_port().
10761da177e4SLinus Torvalds  */
10771da177e4SLinus Torvalds 
10781da177e4SLinus Torvalds struct parport *parport_find_base(unsigned long base)
10791da177e4SLinus Torvalds {
10801da177e4SLinus Torvalds 	struct parport *port, *result = NULL;
10811da177e4SLinus Torvalds 
10821da177e4SLinus Torvalds 	if (list_empty(&portlist))
10831da177e4SLinus Torvalds 		get_lowlevel_driver();
10841da177e4SLinus Torvalds 
10851da177e4SLinus Torvalds 	spin_lock(&parportlist_lock);
10861da177e4SLinus Torvalds 	list_for_each_entry(port, &portlist, list) {
10871da177e4SLinus Torvalds 		if (port->base == base) {
10881da177e4SLinus Torvalds 			result = parport_get_port(port);
10891da177e4SLinus Torvalds 			break;
10901da177e4SLinus Torvalds 		}
10911da177e4SLinus Torvalds 	}
10921da177e4SLinus Torvalds 	spin_unlock(&parportlist_lock);
10931da177e4SLinus Torvalds 	return result;
10941da177e4SLinus Torvalds }
109527c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_find_base);
10961da177e4SLinus Torvalds 
10971da177e4SLinus Torvalds /**
10981da177e4SLinus Torvalds  *	parport_claim - claim access to a parallel port device
10991da177e4SLinus Torvalds  *	@dev: pointer to structure representing a device on the port
11001da177e4SLinus Torvalds  *
11011da177e4SLinus Torvalds  *	This function will not block and so can be used from interrupt
11021da177e4SLinus Torvalds  *	context.  If parport_claim() succeeds in claiming access to
11031da177e4SLinus Torvalds  *	the port it returns zero and the port is available to use.  It
11041da177e4SLinus Torvalds  *	may fail (returning non-zero) if the port is in use by another
11051da177e4SLinus Torvalds  *	driver and that driver is not willing to relinquish control of
11061da177e4SLinus Torvalds  *	the port.
11071da177e4SLinus Torvalds  **/
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds int parport_claim(struct pardevice *dev)
11101da177e4SLinus Torvalds {
11111da177e4SLinus Torvalds 	struct pardevice *oldcad;
11121da177e4SLinus Torvalds 	struct parport *port = dev->port->physport;
11131da177e4SLinus Torvalds 	unsigned long flags;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	if (port->cad == dev) {
11161da177e4SLinus Torvalds 		printk(KERN_INFO "%s: %s already owner\n",
11171da177e4SLinus Torvalds 		       dev->port->name,dev->name);
11181da177e4SLinus Torvalds 		return 0;
11191da177e4SLinus Torvalds 	}
11201da177e4SLinus Torvalds 
11211da177e4SLinus Torvalds 	/* Preempt any current device */
11221da177e4SLinus Torvalds 	write_lock_irqsave(&port->cad_lock, flags);
1123e0a7f1f0SSudip Mukherjee 	oldcad = port->cad;
1124e0a7f1f0SSudip Mukherjee 	if (oldcad) {
11251da177e4SLinus Torvalds 		if (oldcad->preempt) {
11261da177e4SLinus Torvalds 			if (oldcad->preempt(oldcad->private))
11271da177e4SLinus Torvalds 				goto blocked;
11281da177e4SLinus Torvalds 			port->ops->save_state(port, dev->state);
11291da177e4SLinus Torvalds 		} else
11301da177e4SLinus Torvalds 			goto blocked;
11311da177e4SLinus Torvalds 
11321da177e4SLinus Torvalds 		if (port->cad != oldcad) {
11337b7a0a30SSudip Mukherjee 			/*
11347b7a0a30SSudip Mukherjee 			 * I think we'll actually deadlock rather than
11357b7a0a30SSudip Mukherjee 			 * get here, but just in case..
11367b7a0a30SSudip Mukherjee 			 */
11371da177e4SLinus Torvalds 			printk(KERN_WARNING
11381da177e4SLinus Torvalds 			       "%s: %s released port when preempted!\n",
11391da177e4SLinus Torvalds 			       port->name, oldcad->name);
11401da177e4SLinus Torvalds 			if (port->cad)
11411da177e4SLinus Torvalds 				goto blocked;
11421da177e4SLinus Torvalds 		}
11431da177e4SLinus Torvalds 	}
11441da177e4SLinus Torvalds 
11451da177e4SLinus Torvalds 	/* Can't fail from now on, so mark ourselves as no longer waiting.  */
11461da177e4SLinus Torvalds 	if (dev->waiting & 1) {
11471da177e4SLinus Torvalds 		dev->waiting = 0;
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 		/* Take ourselves out of the wait list again.  */
11501da177e4SLinus Torvalds 		spin_lock_irq(&port->waitlist_lock);
11511da177e4SLinus Torvalds 		if (dev->waitprev)
11521da177e4SLinus Torvalds 			dev->waitprev->waitnext = dev->waitnext;
11531da177e4SLinus Torvalds 		else
11541da177e4SLinus Torvalds 			port->waithead = dev->waitnext;
11551da177e4SLinus Torvalds 		if (dev->waitnext)
11561da177e4SLinus Torvalds 			dev->waitnext->waitprev = dev->waitprev;
11571da177e4SLinus Torvalds 		else
11581da177e4SLinus Torvalds 			port->waittail = dev->waitprev;
11591da177e4SLinus Torvalds 		spin_unlock_irq(&port->waitlist_lock);
11601da177e4SLinus Torvalds 		dev->waitprev = dev->waitnext = NULL;
11611da177e4SLinus Torvalds 	}
11621da177e4SLinus Torvalds 
11631da177e4SLinus Torvalds 	/* Now we do the change of devices */
11641da177e4SLinus Torvalds 	port->cad = dev;
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
11671da177e4SLinus Torvalds 	/* If it's a mux port, select it. */
11681da177e4SLinus Torvalds 	if (dev->port->muxport >= 0) {
11691da177e4SLinus Torvalds 		/* FIXME */
11701da177e4SLinus Torvalds 		port->muxsel = dev->port->muxport;
11711da177e4SLinus Torvalds 	}
11721da177e4SLinus Torvalds 
11731da177e4SLinus Torvalds 	/* If it's a daisy chain device, select it. */
11741da177e4SLinus Torvalds 	if (dev->daisy >= 0) {
11751da177e4SLinus Torvalds 		/* This could be lazier. */
11761da177e4SLinus Torvalds 		if (!parport_daisy_select(port, dev->daisy,
11771da177e4SLinus Torvalds 					   IEEE1284_MODE_COMPAT))
11781da177e4SLinus Torvalds 			port->daisy = dev->daisy;
11791da177e4SLinus Torvalds 	}
11801da177e4SLinus Torvalds #endif /* IEEE1284.3 support */
11811da177e4SLinus Torvalds 
11821da177e4SLinus Torvalds 	/* Restore control registers */
11831da177e4SLinus Torvalds 	port->ops->restore_state(port, dev->state);
11841da177e4SLinus Torvalds 	write_unlock_irqrestore(&port->cad_lock, flags);
11851da177e4SLinus Torvalds 	dev->time = jiffies;
11861da177e4SLinus Torvalds 	return 0;
11871da177e4SLinus Torvalds 
11881da177e4SLinus Torvalds blocked:
11897b7a0a30SSudip Mukherjee 	/*
11907b7a0a30SSudip Mukherjee 	 * If this is the first time we tried to claim the port, register an
11917b7a0a30SSudip Mukherjee 	 * interest.  This is only allowed for devices sleeping in
11927b7a0a30SSudip Mukherjee 	 * parport_claim_or_block(), or those with a wakeup function.
11937b7a0a30SSudip Mukherjee 	 */
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	/* The cad_lock is still held for writing here */
11961da177e4SLinus Torvalds 	if (dev->waiting & 2 || dev->wakeup) {
11971da177e4SLinus Torvalds 		spin_lock(&port->waitlist_lock);
11981da177e4SLinus Torvalds 		if (test_and_set_bit(0, &dev->waiting) == 0) {
11991da177e4SLinus Torvalds 			/* First add ourselves to the end of the wait list. */
12001da177e4SLinus Torvalds 			dev->waitnext = NULL;
12011da177e4SLinus Torvalds 			dev->waitprev = port->waittail;
12021da177e4SLinus Torvalds 			if (port->waittail) {
12031da177e4SLinus Torvalds 				port->waittail->waitnext = dev;
12041da177e4SLinus Torvalds 				port->waittail = dev;
12051da177e4SLinus Torvalds 			} else
12061da177e4SLinus Torvalds 				port->waithead = port->waittail = dev;
12071da177e4SLinus Torvalds 		}
12081da177e4SLinus Torvalds 		spin_unlock(&port->waitlist_lock);
12091da177e4SLinus Torvalds 	}
12101da177e4SLinus Torvalds 	write_unlock_irqrestore(&port->cad_lock, flags);
12111da177e4SLinus Torvalds 	return -EAGAIN;
12121da177e4SLinus Torvalds }
121327c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_claim);
12141da177e4SLinus Torvalds 
12151da177e4SLinus Torvalds /**
12161da177e4SLinus Torvalds  *	parport_claim_or_block - claim access to a parallel port device
12171da177e4SLinus Torvalds  *	@dev: pointer to structure representing a device on the port
12181da177e4SLinus Torvalds  *
12191da177e4SLinus Torvalds  *	This behaves like parport_claim(), but will block if necessary
12201da177e4SLinus Torvalds  *	to wait for the port to be free.  A return value of 1
12211da177e4SLinus Torvalds  *	indicates that it slept; 0 means that it succeeded without
12221da177e4SLinus Torvalds  *	needing to sleep.  A negative error code indicates failure.
12231da177e4SLinus Torvalds  **/
12241da177e4SLinus Torvalds 
12251da177e4SLinus Torvalds int parport_claim_or_block(struct pardevice *dev)
12261da177e4SLinus Torvalds {
12271da177e4SLinus Torvalds 	int r;
12281da177e4SLinus Torvalds 
12297b7a0a30SSudip Mukherjee 	/*
12307b7a0a30SSudip Mukherjee 	 * Signal to parport_claim() that we can wait even without a
12317b7a0a30SSudip Mukherjee 	 * wakeup function.
12327b7a0a30SSudip Mukherjee 	 */
12331da177e4SLinus Torvalds 	dev->waiting = 2;
12341da177e4SLinus Torvalds 
12351da177e4SLinus Torvalds 	/* Try to claim the port.  If this fails, we need to sleep.  */
12361da177e4SLinus Torvalds 	r = parport_claim(dev);
12371da177e4SLinus Torvalds 	if (r == -EAGAIN) {
12381da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING
12391da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: parport_claim() returned -EAGAIN\n", dev->name);
12401da177e4SLinus Torvalds #endif
12411da177e4SLinus Torvalds 		/*
12421da177e4SLinus Torvalds 		 * FIXME!!! Use the proper locking for dev->waiting,
12431da177e4SLinus Torvalds 		 * and make this use the "wait_event_interruptible()"
12441da177e4SLinus Torvalds 		 * interfaces. The cli/sti that used to be here
12451da177e4SLinus Torvalds 		 * did nothing.
12461da177e4SLinus Torvalds 		 *
12471da177e4SLinus Torvalds 		 * See also parport_release()
12481da177e4SLinus Torvalds 		 */
12491da177e4SLinus Torvalds 
12507b7a0a30SSudip Mukherjee 		/*
12517b7a0a30SSudip Mukherjee 		 * If dev->waiting is clear now, an interrupt
12527b7a0a30SSudip Mukherjee 		 * gave us the port and we would deadlock if we slept.
12537b7a0a30SSudip Mukherjee 		 */
12541da177e4SLinus Torvalds 		if (dev->waiting) {
125576ae0536SArnd Bergmann 			wait_event_interruptible(dev->wait_q,
125676ae0536SArnd Bergmann 						 !dev->waiting);
125713efa75dSSudip Mukherjee 			if (signal_pending(current))
12581da177e4SLinus Torvalds 				return -EINTR;
12591da177e4SLinus Torvalds 			r = 1;
12601da177e4SLinus Torvalds 		} else {
12611da177e4SLinus Torvalds 			r = 0;
12621da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING
12631da177e4SLinus Torvalds 			printk(KERN_DEBUG "%s: didn't sleep in parport_claim_or_block()\n",
12641da177e4SLinus Torvalds 			       dev->name);
12651da177e4SLinus Torvalds #endif
12661da177e4SLinus Torvalds 		}
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING
12691da177e4SLinus Torvalds 		if (dev->port->physport->cad != dev)
1270a162188fSSudip Mukherjee 			printk(KERN_DEBUG "%s: exiting parport_claim_or_block but %s owns port!\n",
1271a162188fSSudip Mukherjee 			       dev->name, dev->port->physport->cad ?
12721da177e4SLinus Torvalds 			       dev->port->physport->cad->name:"nobody");
12731da177e4SLinus Torvalds #endif
12741da177e4SLinus Torvalds 	}
12751da177e4SLinus Torvalds 	dev->waiting = 0;
12761da177e4SLinus Torvalds 	return r;
12771da177e4SLinus Torvalds }
127827c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_claim_or_block);
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds /**
12811da177e4SLinus Torvalds  *	parport_release - give up access to a parallel port device
12821da177e4SLinus Torvalds  *	@dev: pointer to structure representing parallel port device
12831da177e4SLinus Torvalds  *
12841da177e4SLinus Torvalds  *	This function cannot fail, but it should not be called without
12851da177e4SLinus Torvalds  *	the port claimed.  Similarly, if the port is already claimed
12861da177e4SLinus Torvalds  *	you should not try claiming it again.
12871da177e4SLinus Torvalds  **/
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds void parport_release(struct pardevice *dev)
12901da177e4SLinus Torvalds {
12911da177e4SLinus Torvalds 	struct parport *port = dev->port->physport;
12921da177e4SLinus Torvalds 	struct pardevice *pd;
12931da177e4SLinus Torvalds 	unsigned long flags;
12941da177e4SLinus Torvalds 
12951da177e4SLinus Torvalds 	/* Make sure that dev is the current device */
12961da177e4SLinus Torvalds 	write_lock_irqsave(&port->cad_lock, flags);
12971da177e4SLinus Torvalds 	if (port->cad != dev) {
12981da177e4SLinus Torvalds 		write_unlock_irqrestore(&port->cad_lock, flags);
1299a162188fSSudip Mukherjee 		printk(KERN_WARNING "%s: %s tried to release parport when not owner\n",
1300a162188fSSudip Mukherjee 		       port->name, dev->name);
13011da177e4SLinus Torvalds 		return;
13021da177e4SLinus Torvalds 	}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
13051da177e4SLinus Torvalds 	/* If this is on a mux port, deselect it. */
13061da177e4SLinus Torvalds 	if (dev->port->muxport >= 0) {
13071da177e4SLinus Torvalds 		/* FIXME */
13081da177e4SLinus Torvalds 		port->muxsel = -1;
13091da177e4SLinus Torvalds 	}
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds 	/* If this is a daisy device, deselect it. */
13121da177e4SLinus Torvalds 	if (dev->daisy >= 0) {
13131da177e4SLinus Torvalds 		parport_daisy_deselect_all(port);
13141da177e4SLinus Torvalds 		port->daisy = -1;
13151da177e4SLinus Torvalds 	}
13161da177e4SLinus Torvalds #endif
13171da177e4SLinus Torvalds 
13181da177e4SLinus Torvalds 	port->cad = NULL;
13191da177e4SLinus Torvalds 	write_unlock_irqrestore(&port->cad_lock, flags);
13201da177e4SLinus Torvalds 
13211da177e4SLinus Torvalds 	/* Save control registers */
13221da177e4SLinus Torvalds 	port->ops->save_state(port, dev->state);
13231da177e4SLinus Torvalds 
13247b7a0a30SSudip Mukherjee 	/*
13257b7a0a30SSudip Mukherjee 	 * If anybody is waiting, find out who's been there longest and
13267b7a0a30SSudip Mukherjee 	 * then wake them up. (Note: no locking required)
13277b7a0a30SSudip Mukherjee 	 */
13281da177e4SLinus Torvalds 	/* !!! LOCKING IS NEEDED HERE */
13291da177e4SLinus Torvalds 	for (pd = port->waithead; pd; pd = pd->waitnext) {
13301da177e4SLinus Torvalds 		if (pd->waiting & 2) { /* sleeping in claim_or_block */
13311da177e4SLinus Torvalds 			parport_claim(pd);
13321da177e4SLinus Torvalds 			if (waitqueue_active(&pd->wait_q))
13331da177e4SLinus Torvalds 				wake_up_interruptible(&pd->wait_q);
13341da177e4SLinus Torvalds 			return;
13351da177e4SLinus Torvalds 		} else if (pd->wakeup) {
13361da177e4SLinus Torvalds 			pd->wakeup(pd->private);
13371da177e4SLinus Torvalds 			if (dev->port->cad) /* racy but no matter */
13381da177e4SLinus Torvalds 				return;
13391da177e4SLinus Torvalds 		} else {
13401da177e4SLinus Torvalds 			printk(KERN_ERR "%s: don't know how to wake %s\n", port->name, pd->name);
13411da177e4SLinus Torvalds 		}
13421da177e4SLinus Torvalds 	}
13431da177e4SLinus Torvalds 
13447b7a0a30SSudip Mukherjee 	/*
13457b7a0a30SSudip Mukherjee 	 * Nobody was waiting, so walk the list to see if anyone is
13467b7a0a30SSudip Mukherjee 	 * interested in being woken up. (Note: no locking required)
13477b7a0a30SSudip Mukherjee 	 */
13481da177e4SLinus Torvalds 	/* !!! LOCKING IS NEEDED HERE */
1349b95b9d6cSSudip Mukherjee 	for (pd = port->devices; !port->cad && pd; pd = pd->next) {
13501da177e4SLinus Torvalds 		if (pd->wakeup && pd != dev)
13511da177e4SLinus Torvalds 			pd->wakeup(pd->private);
13521da177e4SLinus Torvalds 	}
13531da177e4SLinus Torvalds }
135427c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_release);
13551da177e4SLinus Torvalds 
13563f2e40dfSJeff Garzik irqreturn_t parport_irq_handler(int irq, void *dev_id)
13573f2e40dfSJeff Garzik {
13583f2e40dfSJeff Garzik 	struct parport *port = dev_id;
13593f2e40dfSJeff Garzik 
1360f230d101SJeff Garzik 	parport_generic_irq(port);
13613f2e40dfSJeff Garzik 
13623f2e40dfSJeff Garzik 	return IRQ_HANDLED;
13633f2e40dfSJeff Garzik }
13643f2e40dfSJeff Garzik EXPORT_SYMBOL(parport_irq_handler);
13651da177e4SLinus Torvalds 
13661da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1367