xref: /openbmc/linux/drivers/parport/share.c (revision 5712cb3d)
11da177e4SLinus Torvalds /* $Id: parport_share.c,v 1.15 1998/01/11 12:06:17 philip Exp $
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>
301da177e4SLinus Torvalds #include <linux/sched.h>
311da177e4SLinus Torvalds #include <linux/kmod.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #include <linux/spinlock.h>
3414cc3e2bSIngo Molnar #include <linux/mutex.h>
351da177e4SLinus Torvalds #include <asm/irq.h>
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds #undef PARPORT_PARANOID
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define PARPORT_DEFAULT_TIMESLICE	(HZ/5)
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds unsigned long parport_default_timeslice = PARPORT_DEFAULT_TIMESLICE;
421da177e4SLinus Torvalds int parport_default_spintime =  DEFAULT_SPIN_TIME;
431da177e4SLinus Torvalds 
441da177e4SLinus Torvalds static LIST_HEAD(portlist);
451da177e4SLinus Torvalds static DEFINE_SPINLOCK(parportlist_lock);
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds /* list of all allocated ports, sorted by ->number */
481da177e4SLinus Torvalds static LIST_HEAD(all_ports);
491da177e4SLinus Torvalds static DEFINE_SPINLOCK(full_list_lock);
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds static LIST_HEAD(drivers);
521da177e4SLinus Torvalds 
5314cc3e2bSIngo Molnar static DEFINE_MUTEX(registration_lock);
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /* What you can do to a port that's gone away.. */
561da177e4SLinus Torvalds static void dead_write_lines (struct parport *p, unsigned char b){}
571da177e4SLinus Torvalds static unsigned char dead_read_lines (struct parport *p) { return 0; }
581da177e4SLinus Torvalds static unsigned char dead_frob_lines (struct parport *p, unsigned char b,
591da177e4SLinus Torvalds 			     unsigned char c) { return 0; }
601da177e4SLinus Torvalds static void dead_onearg (struct parport *p){}
611da177e4SLinus Torvalds static void dead_initstate (struct pardevice *d, struct parport_state *s) { }
621da177e4SLinus Torvalds static void dead_state (struct parport *p, struct parport_state *s) { }
631da177e4SLinus Torvalds static size_t dead_write (struct parport *p, const void *b, size_t l, int f)
641da177e4SLinus Torvalds { return 0; }
651da177e4SLinus Torvalds static size_t dead_read (struct parport *p, void *b, size_t l, int f)
661da177e4SLinus Torvalds { return 0; }
671da177e4SLinus Torvalds static struct parport_operations dead_ops = {
681da177e4SLinus Torvalds 	.write_data	= dead_write_lines,	/* data */
691da177e4SLinus Torvalds 	.read_data	= dead_read_lines,
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	.write_control	= dead_write_lines,	/* control */
721da177e4SLinus Torvalds 	.read_control	= dead_read_lines,
731da177e4SLinus Torvalds 	.frob_control	= dead_frob_lines,
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds 	.read_status	= dead_read_lines,	/* status */
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 	.enable_irq	= dead_onearg,		/* enable_irq */
781da177e4SLinus Torvalds 	.disable_irq	= dead_onearg,		/* disable_irq */
791da177e4SLinus Torvalds 
801da177e4SLinus Torvalds 	.data_forward	= dead_onearg,		/* data_forward */
811da177e4SLinus Torvalds 	.data_reverse	= dead_onearg,		/* data_reverse */
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	.init_state	= dead_initstate,	/* init_state */
841da177e4SLinus Torvalds 	.save_state	= dead_state,
851da177e4SLinus Torvalds 	.restore_state	= dead_state,
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	.epp_write_data	= dead_write,		/* epp */
881da177e4SLinus Torvalds 	.epp_read_data	= dead_read,
891da177e4SLinus Torvalds 	.epp_write_addr	= dead_write,
901da177e4SLinus Torvalds 	.epp_read_addr	= dead_read,
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	.ecp_write_data	= dead_write,		/* ecp */
931da177e4SLinus Torvalds 	.ecp_read_data	= dead_read,
941da177e4SLinus Torvalds 	.ecp_write_addr	= dead_write,
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	.compat_write_data	= dead_write,	/* compat */
971da177e4SLinus Torvalds 	.nibble_read_data	= dead_read,	/* nibble */
981da177e4SLinus Torvalds 	.byte_read_data		= dead_read,	/* byte */
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	.owner		= NULL,
1011da177e4SLinus Torvalds };
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds /* Call attach(port) for each registered driver. */
1041da177e4SLinus Torvalds static void attach_driver_chain(struct parport *port)
1051da177e4SLinus Torvalds {
1061da177e4SLinus Torvalds 	/* caller has exclusive registration_lock */
1071da177e4SLinus Torvalds 	struct parport_driver *drv;
1081da177e4SLinus Torvalds 	list_for_each_entry(drv, &drivers, list)
1091da177e4SLinus Torvalds 		drv->attach(port);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds /* Call detach(port) for each registered driver. */
1131da177e4SLinus Torvalds static void detach_driver_chain(struct parport *port)
1141da177e4SLinus Torvalds {
1151da177e4SLinus Torvalds 	struct parport_driver *drv;
1161da177e4SLinus Torvalds 	/* caller has exclusive registration_lock */
1171da177e4SLinus Torvalds 	list_for_each_entry(drv, &drivers, list)
1181da177e4SLinus Torvalds 		drv->detach (port);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds /* Ask kmod for some lowlevel drivers. */
1221da177e4SLinus Torvalds static void get_lowlevel_driver (void)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	/* There is no actual module called this: you should set
1251da177e4SLinus Torvalds 	 * up an alias for modutils. */
1261da177e4SLinus Torvalds 	request_module ("parport_lowlevel");
1271da177e4SLinus Torvalds }
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /**
1301da177e4SLinus Torvalds  *	parport_register_driver - register a parallel port device driver
1311da177e4SLinus Torvalds  *	@drv: structure describing the driver
1321da177e4SLinus Torvalds  *
1331da177e4SLinus Torvalds  *	This can be called by a parallel port device driver in order
1341da177e4SLinus Torvalds  *	to receive notifications about ports being found in the
1351da177e4SLinus Torvalds  *	system, as well as ports no longer available.
1361da177e4SLinus Torvalds  *
1371da177e4SLinus Torvalds  *	The @drv structure is allocated by the caller and must not be
1381da177e4SLinus Torvalds  *	deallocated until after calling parport_unregister_driver().
1391da177e4SLinus Torvalds  *
1401da177e4SLinus Torvalds  *	The driver's attach() function may block.  The port that
1411da177e4SLinus Torvalds  *	attach() is given will be valid for the duration of the
1421da177e4SLinus Torvalds  *	callback, but if the driver wants to take a copy of the
1431da177e4SLinus Torvalds  *	pointer it must call parport_get_port() to do so.  Calling
1441da177e4SLinus Torvalds  *	parport_register_device() on that port will do this for you.
1451da177e4SLinus Torvalds  *
1461da177e4SLinus Torvalds  *	The driver's detach() function may block.  The port that
1471da177e4SLinus Torvalds  *	detach() is given will be valid for the duration of the
1481da177e4SLinus Torvalds  *	callback, but if the driver wants to take a copy of the
1491da177e4SLinus Torvalds  *	pointer it must call parport_get_port() to do so.
1501da177e4SLinus Torvalds  *
1511da177e4SLinus Torvalds  *	Returns 0 on success.  Currently it always succeeds.
1521da177e4SLinus Torvalds  **/
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds int parport_register_driver (struct parport_driver *drv)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	struct parport *port;
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	if (list_empty(&portlist))
1591da177e4SLinus Torvalds 		get_lowlevel_driver ();
1601da177e4SLinus Torvalds 
16114cc3e2bSIngo Molnar 	mutex_lock(&registration_lock);
1621da177e4SLinus Torvalds 	list_for_each_entry(port, &portlist, list)
1631da177e4SLinus Torvalds 		drv->attach(port);
1641da177e4SLinus Torvalds 	list_add(&drv->list, &drivers);
16514cc3e2bSIngo Molnar 	mutex_unlock(&registration_lock);
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds 	return 0;
1681da177e4SLinus Torvalds }
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds /**
1711da177e4SLinus Torvalds  *	parport_unregister_driver - deregister a parallel port device driver
1721da177e4SLinus Torvalds  *	@drv: structure describing the driver that was given to
1731da177e4SLinus Torvalds  *	      parport_register_driver()
1741da177e4SLinus Torvalds  *
1751da177e4SLinus Torvalds  *	This should be called by a parallel port device driver that
1761da177e4SLinus Torvalds  *	has registered itself using parport_register_driver() when it
1771da177e4SLinus Torvalds  *	is about to be unloaded.
1781da177e4SLinus Torvalds  *
1791da177e4SLinus Torvalds  *	When it returns, the driver's attach() routine will no longer
1801da177e4SLinus Torvalds  *	be called, and for each port that attach() was called for, the
1811da177e4SLinus Torvalds  *	detach() routine will have been called.
1821da177e4SLinus Torvalds  *
1831da177e4SLinus Torvalds  *	All the driver's attach() and detach() calls are guaranteed to have
1841da177e4SLinus Torvalds  *	finished by the time this function returns.
1851da177e4SLinus Torvalds  **/
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds void parport_unregister_driver (struct parport_driver *drv)
1881da177e4SLinus Torvalds {
1891da177e4SLinus Torvalds 	struct parport *port;
1901da177e4SLinus Torvalds 
19114cc3e2bSIngo Molnar 	mutex_lock(&registration_lock);
1921da177e4SLinus Torvalds 	list_del_init(&drv->list);
1931da177e4SLinus Torvalds 	list_for_each_entry(port, &portlist, list)
1941da177e4SLinus Torvalds 		drv->detach(port);
19514cc3e2bSIngo Molnar 	mutex_unlock(&registration_lock);
1961da177e4SLinus Torvalds }
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds static void free_port (struct parport *port)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	int d;
2011da177e4SLinus Torvalds 	spin_lock(&full_list_lock);
2021da177e4SLinus Torvalds 	list_del(&port->full_list);
2031da177e4SLinus Torvalds 	spin_unlock(&full_list_lock);
2041da177e4SLinus Torvalds 	for (d = 0; d < 5; d++) {
2051da177e4SLinus Torvalds 		kfree(port->probe_info[d].class_name);
2061da177e4SLinus Torvalds 		kfree(port->probe_info[d].mfr);
2071da177e4SLinus Torvalds 		kfree(port->probe_info[d].model);
2081da177e4SLinus Torvalds 		kfree(port->probe_info[d].cmdset);
2091da177e4SLinus Torvalds 		kfree(port->probe_info[d].description);
2101da177e4SLinus Torvalds 	}
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds 	kfree(port->name);
2131da177e4SLinus Torvalds 	kfree(port);
2141da177e4SLinus Torvalds }
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds /**
2171da177e4SLinus Torvalds  *	parport_get_port - increment a port's reference count
2181da177e4SLinus Torvalds  *	@port: the port
2191da177e4SLinus Torvalds  *
2207d469722SRandy Dunlap  *	This ensures that a struct parport pointer remains valid
2211da177e4SLinus Torvalds  *	until the matching parport_put_port() call.
2221da177e4SLinus Torvalds  **/
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds struct parport *parport_get_port (struct parport *port)
2251da177e4SLinus Torvalds {
2261da177e4SLinus Torvalds 	atomic_inc (&port->ref_count);
2271da177e4SLinus Torvalds 	return port;
2281da177e4SLinus Torvalds }
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds /**
2311da177e4SLinus Torvalds  *	parport_put_port - decrement a port's reference count
2321da177e4SLinus Torvalds  *	@port: the port
2331da177e4SLinus Torvalds  *
2341da177e4SLinus Torvalds  *	This should be called once for each call to parport_get_port(),
2351da177e4SLinus Torvalds  *	once the port is no longer needed.
2361da177e4SLinus Torvalds  **/
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds void parport_put_port (struct parport *port)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	if (atomic_dec_and_test (&port->ref_count))
2411da177e4SLinus Torvalds 		/* Can destroy it now. */
2421da177e4SLinus Torvalds 		free_port (port);
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds 	return;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds /**
2481da177e4SLinus Torvalds  *	parport_register_port - register a parallel port
2491da177e4SLinus Torvalds  *	@base: base I/O address
2501da177e4SLinus Torvalds  *	@irq: IRQ line
2511da177e4SLinus Torvalds  *	@dma: DMA channel
2521da177e4SLinus Torvalds  *	@ops: pointer to the port driver's port operations structure
2531da177e4SLinus Torvalds  *
2541da177e4SLinus Torvalds  *	When a parallel port (lowlevel) driver finds a port that
2551da177e4SLinus Torvalds  *	should be made available to parallel port device drivers, it
2561da177e4SLinus Torvalds  *	should call parport_register_port().  The @base, @irq, and
2571da177e4SLinus Torvalds  *	@dma parameters are for the convenience of port drivers, and
2581da177e4SLinus Torvalds  *	for ports where they aren't meaningful needn't be set to
2591da177e4SLinus Torvalds  *	anything special.  They can be altered afterwards by adjusting
2601da177e4SLinus Torvalds  *	the relevant members of the parport structure that is returned
2611da177e4SLinus Torvalds  *	and represents the port.  They should not be tampered with
2621da177e4SLinus Torvalds  *	after calling parport_announce_port, however.
2631da177e4SLinus Torvalds  *
2641da177e4SLinus Torvalds  *	If there are parallel port device drivers in the system that
2651da177e4SLinus Torvalds  *	have registered themselves using parport_register_driver(),
2661da177e4SLinus Torvalds  *	they are not told about the port at this time; that is done by
2671da177e4SLinus Torvalds  *	parport_announce_port().
2681da177e4SLinus Torvalds  *
2691da177e4SLinus Torvalds  *	The @ops structure is allocated by the caller, and must not be
2701da177e4SLinus Torvalds  *	deallocated before calling parport_remove_port().
2711da177e4SLinus Torvalds  *
2721da177e4SLinus Torvalds  *	If there is no memory to allocate a new parport structure,
2731da177e4SLinus Torvalds  *	this function will return %NULL.
2741da177e4SLinus Torvalds  **/
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds struct parport *parport_register_port(unsigned long base, int irq, int dma,
2771da177e4SLinus Torvalds 				      struct parport_operations *ops)
2781da177e4SLinus Torvalds {
2791da177e4SLinus Torvalds 	struct list_head *l;
2801da177e4SLinus Torvalds 	struct parport *tmp;
2811da177e4SLinus Torvalds 	int num;
2821da177e4SLinus Torvalds 	int device;
2831da177e4SLinus Torvalds 	char *name;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	tmp = kmalloc(sizeof(struct parport), GFP_KERNEL);
2861da177e4SLinus Torvalds 	if (!tmp) {
2871da177e4SLinus Torvalds 		printk(KERN_WARNING "parport: memory squeeze\n");
2881da177e4SLinus Torvalds 		return NULL;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	/* Init our structure */
2921da177e4SLinus Torvalds  	memset(tmp, 0, sizeof(struct parport));
2931da177e4SLinus Torvalds 	tmp->base = base;
2941da177e4SLinus Torvalds 	tmp->irq = irq;
2951da177e4SLinus Torvalds 	tmp->dma = dma;
2961da177e4SLinus Torvalds 	tmp->muxport = tmp->daisy = tmp->muxsel = -1;
2971da177e4SLinus Torvalds 	tmp->modes = 0;
2981da177e4SLinus Torvalds  	INIT_LIST_HEAD(&tmp->list);
2991da177e4SLinus Torvalds 	tmp->devices = tmp->cad = NULL;
3001da177e4SLinus Torvalds 	tmp->flags = 0;
3011da177e4SLinus Torvalds 	tmp->ops = ops;
3021da177e4SLinus Torvalds 	tmp->physport = tmp;
3031da177e4SLinus Torvalds 	memset (tmp->probe_info, 0, 5 * sizeof (struct parport_device_info));
3041da177e4SLinus Torvalds 	rwlock_init(&tmp->cad_lock);
3051da177e4SLinus Torvalds 	spin_lock_init(&tmp->waitlist_lock);
3061da177e4SLinus Torvalds 	spin_lock_init(&tmp->pardevice_lock);
3071da177e4SLinus Torvalds 	tmp->ieee1284.mode = IEEE1284_MODE_COMPAT;
3081da177e4SLinus Torvalds 	tmp->ieee1284.phase = IEEE1284_PH_FWD_IDLE;
3091da177e4SLinus Torvalds 	init_MUTEX_LOCKED (&tmp->ieee1284.irq); /* actually a semaphore at 0 */
3101da177e4SLinus Torvalds 	tmp->spintime = parport_default_spintime;
3111da177e4SLinus Torvalds 	atomic_set (&tmp->ref_count, 1);
3121da177e4SLinus Torvalds 	INIT_LIST_HEAD(&tmp->full_list);
3131da177e4SLinus Torvalds 
3141da177e4SLinus Torvalds 	name = kmalloc(15, GFP_KERNEL);
3151da177e4SLinus Torvalds 	if (!name) {
3161da177e4SLinus Torvalds 		printk(KERN_ERR "parport: memory squeeze\n");
3171da177e4SLinus Torvalds 		kfree(tmp);
3181da177e4SLinus Torvalds 		return NULL;
3191da177e4SLinus Torvalds 	}
3201da177e4SLinus Torvalds 	/* Search for the lowest free parport number. */
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	spin_lock(&full_list_lock);
3231da177e4SLinus Torvalds 	for (l = all_ports.next, num = 0; l != &all_ports; l = l->next, num++) {
3241da177e4SLinus Torvalds 		struct parport *p = list_entry(l, struct parport, full_list);
3251da177e4SLinus Torvalds 		if (p->number != num)
3261da177e4SLinus Torvalds 			break;
3271da177e4SLinus Torvalds 	}
3281da177e4SLinus Torvalds 	tmp->portnum = tmp->number = num;
3291da177e4SLinus Torvalds 	list_add_tail(&tmp->full_list, l);
3301da177e4SLinus Torvalds 	spin_unlock(&full_list_lock);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	/*
3331da177e4SLinus Torvalds 	 * Now that the portnum is known finish doing the Init.
3341da177e4SLinus Torvalds 	 */
3351da177e4SLinus Torvalds 	sprintf(name, "parport%d", tmp->portnum = tmp->number);
3361da177e4SLinus Torvalds 	tmp->name = name;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	for (device = 0; device < 5; device++)
3391da177e4SLinus Torvalds 		/* assume the worst */
3401da177e4SLinus Torvalds 		tmp->probe_info[device].class = PARPORT_CLASS_LEGACY;
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	tmp->waithead = tmp->waittail = NULL;
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	return tmp;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /**
3481da177e4SLinus Torvalds  *	parport_announce_port - tell device drivers about a parallel port
3491da177e4SLinus Torvalds  *	@port: parallel port to announce
3501da177e4SLinus Torvalds  *
3511da177e4SLinus Torvalds  *	After a port driver has registered a parallel port with
3521da177e4SLinus Torvalds  *	parport_register_port, and performed any necessary
3531da177e4SLinus Torvalds  *	initialisation or adjustments, it should call
3541da177e4SLinus Torvalds  *	parport_announce_port() in order to notify all device drivers
3551da177e4SLinus Torvalds  *	that have called parport_register_driver().  Their attach()
3561da177e4SLinus Torvalds  *	functions will be called, with @port as the parameter.
3571da177e4SLinus Torvalds  **/
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds void parport_announce_port (struct parport *port)
3601da177e4SLinus Torvalds {
3611da177e4SLinus Torvalds 	int i;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
3641da177e4SLinus Torvalds 	/* Analyse the IEEE1284.3 topology of the port. */
3651da177e4SLinus Torvalds 	parport_daisy_init(port);
3661da177e4SLinus Torvalds #endif
3671da177e4SLinus Torvalds 
368c15a3837SDavid Brownell 	if (!port->dev)
369c15a3837SDavid Brownell 		printk(KERN_WARNING "%s: fix this legacy "
370c15a3837SDavid Brownell 				"no-device port driver!\n",
371c15a3837SDavid Brownell 				port->name);
372c15a3837SDavid Brownell 
3731da177e4SLinus Torvalds 	parport_proc_register(port);
37414cc3e2bSIngo Molnar 	mutex_lock(&registration_lock);
3751da177e4SLinus Torvalds 	spin_lock_irq(&parportlist_lock);
3761da177e4SLinus Torvalds 	list_add_tail(&port->list, &portlist);
3771da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
3781da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
3791da177e4SLinus Torvalds 		if (slave)
3801da177e4SLinus Torvalds 			list_add_tail(&slave->list, &portlist);
3811da177e4SLinus Torvalds 	}
3821da177e4SLinus Torvalds 	spin_unlock_irq(&parportlist_lock);
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds 	/* Let drivers know that new port(s) has arrived. */
3851da177e4SLinus Torvalds 	attach_driver_chain (port);
3861da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
3871da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
3881da177e4SLinus Torvalds 		if (slave)
3891da177e4SLinus Torvalds 			attach_driver_chain(slave);
3901da177e4SLinus Torvalds 	}
39114cc3e2bSIngo Molnar 	mutex_unlock(&registration_lock);
3921da177e4SLinus Torvalds }
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds /**
3951da177e4SLinus Torvalds  *	parport_remove_port - deregister a parallel port
3961da177e4SLinus Torvalds  *	@port: parallel port to deregister
3971da177e4SLinus Torvalds  *
3981da177e4SLinus Torvalds  *	When a parallel port driver is forcibly unloaded, or a
3991da177e4SLinus Torvalds  *	parallel port becomes inaccessible, the port driver must call
4001da177e4SLinus Torvalds  *	this function in order to deal with device drivers that still
4011da177e4SLinus Torvalds  *	want to use it.
4021da177e4SLinus Torvalds  *
4031da177e4SLinus Torvalds  *	The parport structure associated with the port has its
4041da177e4SLinus Torvalds  *	operations structure replaced with one containing 'null'
4051da177e4SLinus Torvalds  *	operations that return errors or just don't do anything.
4061da177e4SLinus Torvalds  *
4071da177e4SLinus Torvalds  *	Any drivers that have registered themselves using
4081da177e4SLinus Torvalds  *	parport_register_driver() are notified that the port is no
4091da177e4SLinus Torvalds  *	longer accessible by having their detach() routines called
4101da177e4SLinus Torvalds  *	with @port as the parameter.
4111da177e4SLinus Torvalds  **/
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds void parport_remove_port(struct parport *port)
4141da177e4SLinus Torvalds {
4151da177e4SLinus Torvalds 	int i;
4161da177e4SLinus Torvalds 
41714cc3e2bSIngo Molnar 	mutex_lock(&registration_lock);
4181da177e4SLinus Torvalds 
4191da177e4SLinus Torvalds 	/* Spread the word. */
4201da177e4SLinus Torvalds 	detach_driver_chain (port);
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
4231da177e4SLinus Torvalds 	/* Forget the IEEE1284.3 topology of the port. */
4241da177e4SLinus Torvalds 	parport_daisy_fini(port);
4251da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
4261da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
4271da177e4SLinus Torvalds 		if (!slave)
4281da177e4SLinus Torvalds 			continue;
4291da177e4SLinus Torvalds 		detach_driver_chain(slave);
4301da177e4SLinus Torvalds 		parport_daisy_fini(slave);
4311da177e4SLinus Torvalds 	}
4321da177e4SLinus Torvalds #endif
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds 	port->ops = &dead_ops;
4351da177e4SLinus Torvalds 	spin_lock(&parportlist_lock);
4361da177e4SLinus Torvalds 	list_del_init(&port->list);
4371da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
4381da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
4391da177e4SLinus Torvalds 		if (slave)
4401da177e4SLinus Torvalds 			list_del_init(&slave->list);
4411da177e4SLinus Torvalds 	}
4421da177e4SLinus Torvalds 	spin_unlock(&parportlist_lock);
4431da177e4SLinus Torvalds 
44414cc3e2bSIngo Molnar 	mutex_unlock(&registration_lock);
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 	parport_proc_unregister(port);
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	for (i = 1; i < 3; i++) {
4491da177e4SLinus Torvalds 		struct parport *slave = port->slaves[i-1];
4501da177e4SLinus Torvalds 		if (slave)
4511da177e4SLinus Torvalds 			parport_put_port(slave);
4521da177e4SLinus Torvalds 	}
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4551da177e4SLinus Torvalds /**
4561da177e4SLinus Torvalds  *	parport_register_device - register a device on a parallel port
4571da177e4SLinus Torvalds  *	@port: port to which the device is attached
4581da177e4SLinus Torvalds  *	@name: a name to refer to the device
4591da177e4SLinus Torvalds  *	@pf: preemption callback
4601da177e4SLinus Torvalds  *	@kf: kick callback (wake-up)
4611da177e4SLinus Torvalds  *	@irq_func: interrupt handler
4621da177e4SLinus Torvalds  *	@flags: registration flags
4631da177e4SLinus Torvalds  *	@handle: data for callback functions
4641da177e4SLinus Torvalds  *
4651da177e4SLinus Torvalds  *	This function, called by parallel port device drivers,
4661da177e4SLinus Torvalds  *	declares that a device is connected to a port, and tells the
4671da177e4SLinus Torvalds  *	system all it needs to know.
4681da177e4SLinus Torvalds  *
4691da177e4SLinus Torvalds  *	The @name is allocated by the caller and must not be
4701da177e4SLinus Torvalds  *	deallocated until the caller calls @parport_unregister_device
4711da177e4SLinus Torvalds  *	for that device.
4721da177e4SLinus Torvalds  *
4731da177e4SLinus Torvalds  *	The preemption callback function, @pf, is called when this
4741da177e4SLinus Torvalds  *	device driver has claimed access to the port but another
4751da177e4SLinus Torvalds  *	device driver wants to use it.  It is given @handle as its
4761da177e4SLinus Torvalds  *	parameter, and should return zero if it is willing for the
4771da177e4SLinus Torvalds  *	system to release the port to another driver on its behalf.
4781da177e4SLinus Torvalds  *	If it wants to keep control of the port it should return
4791da177e4SLinus Torvalds  *	non-zero, and no action will be taken.  It is good manners for
4801da177e4SLinus Torvalds  *	the driver to try to release the port at the earliest
4811da177e4SLinus Torvalds  *	opportunity after its preemption callback rejects a preemption
4821da177e4SLinus Torvalds  *	attempt.  Note that if a preemption callback is happy for
4831da177e4SLinus Torvalds  *	preemption to go ahead, there is no need to release the port;
4841da177e4SLinus Torvalds  *	it is done automatically.  This function may not block, as it
4851da177e4SLinus Torvalds  *	may be called from interrupt context.  If the device driver
4861da177e4SLinus Torvalds  *	does not support preemption, @pf can be %NULL.
4871da177e4SLinus Torvalds  *
4881da177e4SLinus Torvalds  *	The wake-up ("kick") callback function, @kf, is called when
4891da177e4SLinus Torvalds  *	the port is available to be claimed for exclusive access; that
4901da177e4SLinus Torvalds  *	is, parport_claim() is guaranteed to succeed when called from
4911da177e4SLinus Torvalds  *	inside the wake-up callback function.  If the driver wants to
4921da177e4SLinus Torvalds  *	claim the port it should do so; otherwise, it need not take
4931da177e4SLinus Torvalds  *	any action.  This function may not block, as it may be called
4941da177e4SLinus Torvalds  *	from interrupt context.  If the device driver does not want to
4951da177e4SLinus Torvalds  *	be explicitly invited to claim the port in this way, @kf can
4961da177e4SLinus Torvalds  *	be %NULL.
4971da177e4SLinus Torvalds  *
4981da177e4SLinus Torvalds  *	The interrupt handler, @irq_func, is called when an interrupt
4991da177e4SLinus Torvalds  *	arrives from the parallel port.  Note that if a device driver
5001da177e4SLinus Torvalds  *	wants to use interrupts it should use parport_enable_irq(),
5011da177e4SLinus Torvalds  *	and can also check the irq member of the parport structure
5021da177e4SLinus Torvalds  *	representing the port.
5031da177e4SLinus Torvalds  *
5041da177e4SLinus Torvalds  *	The parallel port (lowlevel) driver is the one that has called
5051da177e4SLinus Torvalds  *	request_irq() and whose interrupt handler is called first.
5061da177e4SLinus Torvalds  *	This handler does whatever needs to be done to the hardware to
5071da177e4SLinus Torvalds  *	acknowledge the interrupt (for PC-style ports there is nothing
5081da177e4SLinus Torvalds  *	special to be done).  It then tells the IEEE 1284 code about
5091da177e4SLinus Torvalds  *	the interrupt, which may involve reacting to an IEEE 1284
5101da177e4SLinus Torvalds  *	event depending on the current IEEE 1284 phase.  After this,
5111da177e4SLinus Torvalds  *	it calls @irq_func.  Needless to say, @irq_func will be called
5121da177e4SLinus Torvalds  *	from interrupt context, and may not block.
5131da177e4SLinus Torvalds  *
5141da177e4SLinus Torvalds  *	The %PARPORT_DEV_EXCL flag is for preventing port sharing, and
5151da177e4SLinus Torvalds  *	so should only be used when sharing the port with other device
5161da177e4SLinus Torvalds  *	drivers is impossible and would lead to incorrect behaviour.
5171da177e4SLinus Torvalds  *	Use it sparingly!  Normally, @flags will be zero.
5181da177e4SLinus Torvalds  *
5191da177e4SLinus Torvalds  *	This function returns a pointer to a structure that represents
5201da177e4SLinus Torvalds  *	the device on the port, or %NULL if there is not enough memory
5211da177e4SLinus Torvalds  *	to allocate space for that structure.
5221da177e4SLinus Torvalds  **/
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds struct pardevice *
5251da177e4SLinus Torvalds parport_register_device(struct parport *port, const char *name,
5261da177e4SLinus Torvalds 			int (*pf)(void *), void (*kf)(void *),
5275712cb3dSJeff Garzik 			void (*irq_func)(void *),
5281da177e4SLinus Torvalds 			int flags, void *handle)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds 	struct pardevice *tmp;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	if (port->physport->flags & PARPORT_FLAG_EXCL) {
5331da177e4SLinus Torvalds 		/* An exclusive device is registered. */
5341da177e4SLinus Torvalds 		printk (KERN_DEBUG "%s: no more devices allowed\n",
5351da177e4SLinus Torvalds 			port->name);
5361da177e4SLinus Torvalds 		return NULL;
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	if (flags & PARPORT_DEV_LURK) {
5401da177e4SLinus Torvalds 		if (!pf || !kf) {
5411da177e4SLinus Torvalds 			printk(KERN_INFO "%s: refused to register lurking device (%s) without callbacks\n", port->name, name);
5421da177e4SLinus Torvalds 			return NULL;
5431da177e4SLinus Torvalds 		}
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds 	/* We up our own module reference count, and that of the port
5471da177e4SLinus Torvalds            on which a device is to be registered, to ensure that
5481da177e4SLinus Torvalds            neither of us gets unloaded while we sleep in (e.g.)
5491da177e4SLinus Torvalds            kmalloc.
5501da177e4SLinus Torvalds          */
5511da177e4SLinus Torvalds 	if (!try_module_get(port->ops->owner)) {
5521da177e4SLinus Torvalds 		return NULL;
5531da177e4SLinus Torvalds 	}
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	parport_get_port (port);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	tmp = kmalloc(sizeof(struct pardevice), GFP_KERNEL);
5581da177e4SLinus Torvalds 	if (tmp == NULL) {
5591da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name);
5601da177e4SLinus Torvalds 		goto out;
5611da177e4SLinus Torvalds 	}
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	tmp->state = kmalloc(sizeof(struct parport_state), GFP_KERNEL);
5641da177e4SLinus Torvalds 	if (tmp->state == NULL) {
5651da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name);
5661da177e4SLinus Torvalds 		goto out_free_pardevice;
5671da177e4SLinus Torvalds 	}
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds 	tmp->name = name;
5701da177e4SLinus Torvalds 	tmp->port = port;
5711da177e4SLinus Torvalds 	tmp->daisy = -1;
5721da177e4SLinus Torvalds 	tmp->preempt = pf;
5731da177e4SLinus Torvalds 	tmp->wakeup = kf;
5741da177e4SLinus Torvalds 	tmp->private = handle;
5751da177e4SLinus Torvalds 	tmp->flags = flags;
5761da177e4SLinus Torvalds 	tmp->irq_func = irq_func;
5771da177e4SLinus Torvalds 	tmp->waiting = 0;
5781da177e4SLinus Torvalds 	tmp->timeout = 5 * HZ;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	/* Chain this onto the list */
5811da177e4SLinus Torvalds 	tmp->prev = NULL;
5821da177e4SLinus Torvalds 	/*
5831da177e4SLinus Torvalds 	 * This function must not run from an irq handler so we don' t need
5841da177e4SLinus Torvalds 	 * to clear irq on the local CPU. -arca
5851da177e4SLinus Torvalds 	 */
5861da177e4SLinus Torvalds 	spin_lock(&port->physport->pardevice_lock);
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	if (flags & PARPORT_DEV_EXCL) {
5891da177e4SLinus Torvalds 		if (port->physport->devices) {
5901da177e4SLinus Torvalds 			spin_unlock (&port->physport->pardevice_lock);
5911da177e4SLinus Torvalds 			printk (KERN_DEBUG
5921da177e4SLinus Torvalds 				"%s: cannot grant exclusive access for "
5931da177e4SLinus Torvalds 				"device %s\n", port->name, name);
5941da177e4SLinus Torvalds 			goto out_free_all;
5951da177e4SLinus Torvalds 		}
5961da177e4SLinus Torvalds 		port->flags |= PARPORT_FLAG_EXCL;
5971da177e4SLinus Torvalds 	}
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 	tmp->next = port->physport->devices;
6001da177e4SLinus Torvalds 	wmb(); /* Make sure that tmp->next is written before it's
6011da177e4SLinus Torvalds                   added to the list; see comments marked 'no locking
6021da177e4SLinus Torvalds                   required' */
6031da177e4SLinus Torvalds 	if (port->physport->devices)
6041da177e4SLinus Torvalds 		port->physport->devices->prev = tmp;
6051da177e4SLinus Torvalds 	port->physport->devices = tmp;
6061da177e4SLinus Torvalds 	spin_unlock(&port->physport->pardevice_lock);
6071da177e4SLinus Torvalds 
6081da177e4SLinus Torvalds 	init_waitqueue_head(&tmp->wait_q);
6091da177e4SLinus Torvalds 	tmp->timeslice = parport_default_timeslice;
6101da177e4SLinus Torvalds 	tmp->waitnext = tmp->waitprev = NULL;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	/*
6131da177e4SLinus Torvalds 	 * This has to be run as last thing since init_state may need other
6141da177e4SLinus Torvalds 	 * pardevice fields. -arca
6151da177e4SLinus Torvalds 	 */
6161da177e4SLinus Torvalds 	port->ops->init_state(tmp, tmp->state);
6171da177e4SLinus Torvalds 	parport_device_proc_register(tmp);
6181da177e4SLinus Torvalds 	return tmp;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds  out_free_all:
6211da177e4SLinus Torvalds 	kfree(tmp->state);
6221da177e4SLinus Torvalds  out_free_pardevice:
6231da177e4SLinus Torvalds 	kfree(tmp);
6241da177e4SLinus Torvalds  out:
6251da177e4SLinus Torvalds 	parport_put_port (port);
6261da177e4SLinus Torvalds 	module_put(port->ops->owner);
6271da177e4SLinus Torvalds 
6281da177e4SLinus Torvalds 	return NULL;
6291da177e4SLinus Torvalds }
6301da177e4SLinus Torvalds 
6311da177e4SLinus Torvalds /**
6321da177e4SLinus Torvalds  *	parport_unregister_device - deregister a device on a parallel port
6331da177e4SLinus Torvalds  *	@dev: pointer to structure representing device
6341da177e4SLinus Torvalds  *
6351da177e4SLinus Torvalds  *	This undoes the effect of parport_register_device().
6361da177e4SLinus Torvalds  **/
6371da177e4SLinus Torvalds 
6381da177e4SLinus Torvalds void parport_unregister_device(struct pardevice *dev)
6391da177e4SLinus Torvalds {
6401da177e4SLinus Torvalds 	struct parport *port;
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds #ifdef PARPORT_PARANOID
6431da177e4SLinus Torvalds 	if (dev == NULL) {
6441da177e4SLinus Torvalds 		printk(KERN_ERR "parport_unregister_device: passed NULL\n");
6451da177e4SLinus Torvalds 		return;
6461da177e4SLinus Torvalds 	}
6471da177e4SLinus Torvalds #endif
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 	parport_device_proc_unregister(dev);
6501da177e4SLinus Torvalds 
6511da177e4SLinus Torvalds 	port = dev->port->physport;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds 	if (port->cad == dev) {
6541da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: %s forgot to release port\n",
6551da177e4SLinus Torvalds 		       port->name, dev->name);
6561da177e4SLinus Torvalds 		parport_release (dev);
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 
6591da177e4SLinus Torvalds 	spin_lock(&port->pardevice_lock);
6601da177e4SLinus Torvalds 	if (dev->next)
6611da177e4SLinus Torvalds 		dev->next->prev = dev->prev;
6621da177e4SLinus Torvalds 	if (dev->prev)
6631da177e4SLinus Torvalds 		dev->prev->next = dev->next;
6641da177e4SLinus Torvalds 	else
6651da177e4SLinus Torvalds 		port->devices = dev->next;
6661da177e4SLinus Torvalds 
6671da177e4SLinus Torvalds 	if (dev->flags & PARPORT_DEV_EXCL)
6681da177e4SLinus Torvalds 		port->flags &= ~PARPORT_FLAG_EXCL;
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds 	spin_unlock(&port->pardevice_lock);
6711da177e4SLinus Torvalds 
6721da177e4SLinus Torvalds 	/* Make sure we haven't left any pointers around in the wait
6731da177e4SLinus Torvalds 	 * list. */
6741da177e4SLinus Torvalds 	spin_lock (&port->waitlist_lock);
6751da177e4SLinus Torvalds 	if (dev->waitprev || dev->waitnext || port->waithead == dev) {
6761da177e4SLinus Torvalds 		if (dev->waitprev)
6771da177e4SLinus Torvalds 			dev->waitprev->waitnext = dev->waitnext;
6781da177e4SLinus Torvalds 		else
6791da177e4SLinus Torvalds 			port->waithead = dev->waitnext;
6801da177e4SLinus Torvalds 		if (dev->waitnext)
6811da177e4SLinus Torvalds 			dev->waitnext->waitprev = dev->waitprev;
6821da177e4SLinus Torvalds 		else
6831da177e4SLinus Torvalds 			port->waittail = dev->waitprev;
6841da177e4SLinus Torvalds 	}
6851da177e4SLinus Torvalds 	spin_unlock (&port->waitlist_lock);
6861da177e4SLinus Torvalds 
6871da177e4SLinus Torvalds 	kfree(dev->state);
6881da177e4SLinus Torvalds 	kfree(dev);
6891da177e4SLinus Torvalds 
6901da177e4SLinus Torvalds 	module_put(port->ops->owner);
6911da177e4SLinus Torvalds 	parport_put_port (port);
6921da177e4SLinus Torvalds }
6931da177e4SLinus Torvalds 
6941da177e4SLinus Torvalds /**
6951da177e4SLinus Torvalds  *	parport_find_number - find a parallel port by number
6961da177e4SLinus Torvalds  *	@number: parallel port number
6971da177e4SLinus Torvalds  *
6981da177e4SLinus Torvalds  *	This returns the parallel port with the specified number, or
6991da177e4SLinus Torvalds  *	%NULL if there is none.
7001da177e4SLinus Torvalds  *
7011da177e4SLinus Torvalds  *	There is an implicit parport_get_port() done already; to throw
7021da177e4SLinus Torvalds  *	away the reference to the port that parport_find_number()
7031da177e4SLinus Torvalds  *	gives you, use parport_put_port().
7041da177e4SLinus Torvalds  */
7051da177e4SLinus Torvalds 
7061da177e4SLinus Torvalds struct parport *parport_find_number (int number)
7071da177e4SLinus Torvalds {
7081da177e4SLinus Torvalds 	struct parport *port, *result = NULL;
7091da177e4SLinus Torvalds 
7101da177e4SLinus Torvalds 	if (list_empty(&portlist))
7111da177e4SLinus Torvalds 		get_lowlevel_driver ();
7121da177e4SLinus Torvalds 
7131da177e4SLinus Torvalds 	spin_lock (&parportlist_lock);
7141da177e4SLinus Torvalds 	list_for_each_entry(port, &portlist, list) {
7151da177e4SLinus Torvalds 		if (port->number == number) {
7161da177e4SLinus Torvalds 			result = parport_get_port (port);
7171da177e4SLinus Torvalds 			break;
7181da177e4SLinus Torvalds 		}
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds 	spin_unlock (&parportlist_lock);
7211da177e4SLinus Torvalds 	return result;
7221da177e4SLinus Torvalds }
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds /**
7251da177e4SLinus Torvalds  *	parport_find_base - find a parallel port by base address
7261da177e4SLinus Torvalds  *	@base: base I/O address
7271da177e4SLinus Torvalds  *
7281da177e4SLinus Torvalds  *	This returns the parallel port with the specified base
7291da177e4SLinus Torvalds  *	address, or %NULL if there is none.
7301da177e4SLinus Torvalds  *
7311da177e4SLinus Torvalds  *	There is an implicit parport_get_port() done already; to throw
7321da177e4SLinus Torvalds  *	away the reference to the port that parport_find_base()
7331da177e4SLinus Torvalds  *	gives you, use parport_put_port().
7341da177e4SLinus Torvalds  */
7351da177e4SLinus Torvalds 
7361da177e4SLinus Torvalds struct parport *parport_find_base (unsigned long base)
7371da177e4SLinus Torvalds {
7381da177e4SLinus Torvalds 	struct parport *port, *result = NULL;
7391da177e4SLinus Torvalds 
7401da177e4SLinus Torvalds 	if (list_empty(&portlist))
7411da177e4SLinus Torvalds 		get_lowlevel_driver ();
7421da177e4SLinus Torvalds 
7431da177e4SLinus Torvalds 	spin_lock (&parportlist_lock);
7441da177e4SLinus Torvalds 	list_for_each_entry(port, &portlist, list) {
7451da177e4SLinus Torvalds 		if (port->base == base) {
7461da177e4SLinus Torvalds 			result = parport_get_port (port);
7471da177e4SLinus Torvalds 			break;
7481da177e4SLinus Torvalds 		}
7491da177e4SLinus Torvalds 	}
7501da177e4SLinus Torvalds 	spin_unlock (&parportlist_lock);
7511da177e4SLinus Torvalds 	return result;
7521da177e4SLinus Torvalds }
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds /**
7551da177e4SLinus Torvalds  *	parport_claim - claim access to a parallel port device
7561da177e4SLinus Torvalds  *	@dev: pointer to structure representing a device on the port
7571da177e4SLinus Torvalds  *
7581da177e4SLinus Torvalds  *	This function will not block and so can be used from interrupt
7591da177e4SLinus Torvalds  *	context.  If parport_claim() succeeds in claiming access to
7601da177e4SLinus Torvalds  *	the port it returns zero and the port is available to use.  It
7611da177e4SLinus Torvalds  *	may fail (returning non-zero) if the port is in use by another
7621da177e4SLinus Torvalds  *	driver and that driver is not willing to relinquish control of
7631da177e4SLinus Torvalds  *	the port.
7641da177e4SLinus Torvalds  **/
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds int parport_claim(struct pardevice *dev)
7671da177e4SLinus Torvalds {
7681da177e4SLinus Torvalds 	struct pardevice *oldcad;
7691da177e4SLinus Torvalds 	struct parport *port = dev->port->physport;
7701da177e4SLinus Torvalds 	unsigned long flags;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	if (port->cad == dev) {
7731da177e4SLinus Torvalds 		printk(KERN_INFO "%s: %s already owner\n",
7741da177e4SLinus Torvalds 		       dev->port->name,dev->name);
7751da177e4SLinus Torvalds 		return 0;
7761da177e4SLinus Torvalds 	}
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds 	/* Preempt any current device */
7791da177e4SLinus Torvalds 	write_lock_irqsave (&port->cad_lock, flags);
7801da177e4SLinus Torvalds 	if ((oldcad = port->cad) != NULL) {
7811da177e4SLinus Torvalds 		if (oldcad->preempt) {
7821da177e4SLinus Torvalds 			if (oldcad->preempt(oldcad->private))
7831da177e4SLinus Torvalds 				goto blocked;
7841da177e4SLinus Torvalds 			port->ops->save_state(port, dev->state);
7851da177e4SLinus Torvalds 		} else
7861da177e4SLinus Torvalds 			goto blocked;
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 		if (port->cad != oldcad) {
7891da177e4SLinus Torvalds 			/* I think we'll actually deadlock rather than
7901da177e4SLinus Torvalds                            get here, but just in case.. */
7911da177e4SLinus Torvalds 			printk(KERN_WARNING
7921da177e4SLinus Torvalds 			       "%s: %s released port when preempted!\n",
7931da177e4SLinus Torvalds 			       port->name, oldcad->name);
7941da177e4SLinus Torvalds 			if (port->cad)
7951da177e4SLinus Torvalds 				goto blocked;
7961da177e4SLinus Torvalds 		}
7971da177e4SLinus Torvalds 	}
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	/* Can't fail from now on, so mark ourselves as no longer waiting.  */
8001da177e4SLinus Torvalds 	if (dev->waiting & 1) {
8011da177e4SLinus Torvalds 		dev->waiting = 0;
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 		/* Take ourselves out of the wait list again.  */
8041da177e4SLinus Torvalds 		spin_lock_irq (&port->waitlist_lock);
8051da177e4SLinus Torvalds 		if (dev->waitprev)
8061da177e4SLinus Torvalds 			dev->waitprev->waitnext = dev->waitnext;
8071da177e4SLinus Torvalds 		else
8081da177e4SLinus Torvalds 			port->waithead = dev->waitnext;
8091da177e4SLinus Torvalds 		if (dev->waitnext)
8101da177e4SLinus Torvalds 			dev->waitnext->waitprev = dev->waitprev;
8111da177e4SLinus Torvalds 		else
8121da177e4SLinus Torvalds 			port->waittail = dev->waitprev;
8131da177e4SLinus Torvalds 		spin_unlock_irq (&port->waitlist_lock);
8141da177e4SLinus Torvalds 		dev->waitprev = dev->waitnext = NULL;
8151da177e4SLinus Torvalds 	}
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	/* Now we do the change of devices */
8181da177e4SLinus Torvalds 	port->cad = dev;
8191da177e4SLinus Torvalds 
8201da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
8211da177e4SLinus Torvalds 	/* If it's a mux port, select it. */
8221da177e4SLinus Torvalds 	if (dev->port->muxport >= 0) {
8231da177e4SLinus Torvalds 		/* FIXME */
8241da177e4SLinus Torvalds 		port->muxsel = dev->port->muxport;
8251da177e4SLinus Torvalds 	}
8261da177e4SLinus Torvalds 
8271da177e4SLinus Torvalds 	/* If it's a daisy chain device, select it. */
8281da177e4SLinus Torvalds 	if (dev->daisy >= 0) {
8291da177e4SLinus Torvalds 		/* This could be lazier. */
8301da177e4SLinus Torvalds 		if (!parport_daisy_select (port, dev->daisy,
8311da177e4SLinus Torvalds 					   IEEE1284_MODE_COMPAT))
8321da177e4SLinus Torvalds 			port->daisy = dev->daisy;
8331da177e4SLinus Torvalds 	}
8341da177e4SLinus Torvalds #endif /* IEEE1284.3 support */
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	/* Restore control registers */
8371da177e4SLinus Torvalds 	port->ops->restore_state(port, dev->state);
8381da177e4SLinus Torvalds 	write_unlock_irqrestore(&port->cad_lock, flags);
8391da177e4SLinus Torvalds 	dev->time = jiffies;
8401da177e4SLinus Torvalds 	return 0;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds blocked:
8431da177e4SLinus Torvalds 	/* If this is the first time we tried to claim the port, register an
8441da177e4SLinus Torvalds 	   interest.  This is only allowed for devices sleeping in
8451da177e4SLinus Torvalds 	   parport_claim_or_block(), or those with a wakeup function.  */
8461da177e4SLinus Torvalds 
8471da177e4SLinus Torvalds 	/* The cad_lock is still held for writing here */
8481da177e4SLinus Torvalds 	if (dev->waiting & 2 || dev->wakeup) {
8491da177e4SLinus Torvalds 		spin_lock (&port->waitlist_lock);
8501da177e4SLinus Torvalds 		if (test_and_set_bit(0, &dev->waiting) == 0) {
8511da177e4SLinus Torvalds 			/* First add ourselves to the end of the wait list. */
8521da177e4SLinus Torvalds 			dev->waitnext = NULL;
8531da177e4SLinus Torvalds 			dev->waitprev = port->waittail;
8541da177e4SLinus Torvalds 			if (port->waittail) {
8551da177e4SLinus Torvalds 				port->waittail->waitnext = dev;
8561da177e4SLinus Torvalds 				port->waittail = dev;
8571da177e4SLinus Torvalds 			} else
8581da177e4SLinus Torvalds 				port->waithead = port->waittail = dev;
8591da177e4SLinus Torvalds 		}
8601da177e4SLinus Torvalds 		spin_unlock (&port->waitlist_lock);
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 	write_unlock_irqrestore (&port->cad_lock, flags);
8631da177e4SLinus Torvalds 	return -EAGAIN;
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
8661da177e4SLinus Torvalds /**
8671da177e4SLinus Torvalds  *	parport_claim_or_block - claim access to a parallel port device
8681da177e4SLinus Torvalds  *	@dev: pointer to structure representing a device on the port
8691da177e4SLinus Torvalds  *
8701da177e4SLinus Torvalds  *	This behaves like parport_claim(), but will block if necessary
8711da177e4SLinus Torvalds  *	to wait for the port to be free.  A return value of 1
8721da177e4SLinus Torvalds  *	indicates that it slept; 0 means that it succeeded without
8731da177e4SLinus Torvalds  *	needing to sleep.  A negative error code indicates failure.
8741da177e4SLinus Torvalds  **/
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds int parport_claim_or_block(struct pardevice *dev)
8771da177e4SLinus Torvalds {
8781da177e4SLinus Torvalds 	int r;
8791da177e4SLinus Torvalds 
8801da177e4SLinus Torvalds 	/* Signal to parport_claim() that we can wait even without a
8811da177e4SLinus Torvalds 	   wakeup function.  */
8821da177e4SLinus Torvalds 	dev->waiting = 2;
8831da177e4SLinus Torvalds 
8841da177e4SLinus Torvalds 	/* Try to claim the port.  If this fails, we need to sleep.  */
8851da177e4SLinus Torvalds 	r = parport_claim(dev);
8861da177e4SLinus Torvalds 	if (r == -EAGAIN) {
8871da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING
8881da177e4SLinus Torvalds 		printk(KERN_DEBUG "%s: parport_claim() returned -EAGAIN\n", dev->name);
8891da177e4SLinus Torvalds #endif
8901da177e4SLinus Torvalds 		/*
8911da177e4SLinus Torvalds 		 * FIXME!!! Use the proper locking for dev->waiting,
8921da177e4SLinus Torvalds 		 * and make this use the "wait_event_interruptible()"
8931da177e4SLinus Torvalds 		 * interfaces. The cli/sti that used to be here
8941da177e4SLinus Torvalds 		 * did nothing.
8951da177e4SLinus Torvalds 		 *
8961da177e4SLinus Torvalds 		 * See also parport_release()
8971da177e4SLinus Torvalds 		 */
8981da177e4SLinus Torvalds 
8991da177e4SLinus Torvalds 		/* If dev->waiting is clear now, an interrupt
9001da177e4SLinus Torvalds 		   gave us the port and we would deadlock if we slept.  */
9011da177e4SLinus Torvalds 		if (dev->waiting) {
9021da177e4SLinus Torvalds 			interruptible_sleep_on (&dev->wait_q);
9031da177e4SLinus Torvalds 			if (signal_pending (current)) {
9041da177e4SLinus Torvalds 				return -EINTR;
9051da177e4SLinus Torvalds 			}
9061da177e4SLinus Torvalds 			r = 1;
9071da177e4SLinus Torvalds 		} else {
9081da177e4SLinus Torvalds 			r = 0;
9091da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING
9101da177e4SLinus Torvalds 			printk(KERN_DEBUG "%s: didn't sleep in parport_claim_or_block()\n",
9111da177e4SLinus Torvalds 			       dev->name);
9121da177e4SLinus Torvalds #endif
9131da177e4SLinus Torvalds 		}
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING
9161da177e4SLinus Torvalds 		if (dev->port->physport->cad != dev)
9171da177e4SLinus Torvalds 			printk(KERN_DEBUG "%s: exiting parport_claim_or_block "
9181da177e4SLinus Torvalds 			       "but %s owns port!\n", dev->name,
9191da177e4SLinus Torvalds 			       dev->port->physport->cad ?
9201da177e4SLinus Torvalds 			       dev->port->physport->cad->name:"nobody");
9211da177e4SLinus Torvalds #endif
9221da177e4SLinus Torvalds 	}
9231da177e4SLinus Torvalds 	dev->waiting = 0;
9241da177e4SLinus Torvalds 	return r;
9251da177e4SLinus Torvalds }
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds /**
9281da177e4SLinus Torvalds  *	parport_release - give up access to a parallel port device
9291da177e4SLinus Torvalds  *	@dev: pointer to structure representing parallel port device
9301da177e4SLinus Torvalds  *
9311da177e4SLinus Torvalds  *	This function cannot fail, but it should not be called without
9321da177e4SLinus Torvalds  *	the port claimed.  Similarly, if the port is already claimed
9331da177e4SLinus Torvalds  *	you should not try claiming it again.
9341da177e4SLinus Torvalds  **/
9351da177e4SLinus Torvalds 
9361da177e4SLinus Torvalds void parport_release(struct pardevice *dev)
9371da177e4SLinus Torvalds {
9381da177e4SLinus Torvalds 	struct parport *port = dev->port->physport;
9391da177e4SLinus Torvalds 	struct pardevice *pd;
9401da177e4SLinus Torvalds 	unsigned long flags;
9411da177e4SLinus Torvalds 
9421da177e4SLinus Torvalds 	/* Make sure that dev is the current device */
9431da177e4SLinus Torvalds 	write_lock_irqsave(&port->cad_lock, flags);
9441da177e4SLinus Torvalds 	if (port->cad != dev) {
9451da177e4SLinus Torvalds 		write_unlock_irqrestore (&port->cad_lock, flags);
9461da177e4SLinus Torvalds 		printk(KERN_WARNING "%s: %s tried to release parport "
9471da177e4SLinus Torvalds 		       "when not owner\n", port->name, dev->name);
9481da177e4SLinus Torvalds 		return;
9491da177e4SLinus Torvalds 	}
9501da177e4SLinus Torvalds 
9511da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284
9521da177e4SLinus Torvalds 	/* If this is on a mux port, deselect it. */
9531da177e4SLinus Torvalds 	if (dev->port->muxport >= 0) {
9541da177e4SLinus Torvalds 		/* FIXME */
9551da177e4SLinus Torvalds 		port->muxsel = -1;
9561da177e4SLinus Torvalds 	}
9571da177e4SLinus Torvalds 
9581da177e4SLinus Torvalds 	/* If this is a daisy device, deselect it. */
9591da177e4SLinus Torvalds 	if (dev->daisy >= 0) {
9601da177e4SLinus Torvalds 		parport_daisy_deselect_all (port);
9611da177e4SLinus Torvalds 		port->daisy = -1;
9621da177e4SLinus Torvalds 	}
9631da177e4SLinus Torvalds #endif
9641da177e4SLinus Torvalds 
9651da177e4SLinus Torvalds 	port->cad = NULL;
9661da177e4SLinus Torvalds 	write_unlock_irqrestore(&port->cad_lock, flags);
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	/* Save control registers */
9691da177e4SLinus Torvalds 	port->ops->save_state(port, dev->state);
9701da177e4SLinus Torvalds 
9711da177e4SLinus Torvalds 	/* If anybody is waiting, find out who's been there longest and
9721da177e4SLinus Torvalds 	   then wake them up. (Note: no locking required) */
9731da177e4SLinus Torvalds 	/* !!! LOCKING IS NEEDED HERE */
9741da177e4SLinus Torvalds 	for (pd = port->waithead; pd; pd = pd->waitnext) {
9751da177e4SLinus Torvalds 		if (pd->waiting & 2) { /* sleeping in claim_or_block */
9761da177e4SLinus Torvalds 			parport_claim(pd);
9771da177e4SLinus Torvalds 			if (waitqueue_active(&pd->wait_q))
9781da177e4SLinus Torvalds 				wake_up_interruptible(&pd->wait_q);
9791da177e4SLinus Torvalds 			return;
9801da177e4SLinus Torvalds 		} else if (pd->wakeup) {
9811da177e4SLinus Torvalds 			pd->wakeup(pd->private);
9821da177e4SLinus Torvalds 			if (dev->port->cad) /* racy but no matter */
9831da177e4SLinus Torvalds 				return;
9841da177e4SLinus Torvalds 		} else {
9851da177e4SLinus Torvalds 			printk(KERN_ERR "%s: don't know how to wake %s\n", port->name, pd->name);
9861da177e4SLinus Torvalds 		}
9871da177e4SLinus Torvalds 	}
9881da177e4SLinus Torvalds 
9891da177e4SLinus Torvalds 	/* Nobody was waiting, so walk the list to see if anyone is
9901da177e4SLinus Torvalds 	   interested in being woken up. (Note: no locking required) */
9911da177e4SLinus Torvalds 	/* !!! LOCKING IS NEEDED HERE */
9921da177e4SLinus Torvalds 	for (pd = port->devices; (port->cad == NULL) && pd; pd = pd->next) {
9931da177e4SLinus Torvalds 		if (pd->wakeup && pd != dev)
9941da177e4SLinus Torvalds 			pd->wakeup(pd->private);
9951da177e4SLinus Torvalds 	}
9961da177e4SLinus Torvalds }
9971da177e4SLinus Torvalds 
9983f2e40dfSJeff Garzik irqreturn_t parport_irq_handler(int irq, void *dev_id)
9993f2e40dfSJeff Garzik {
10003f2e40dfSJeff Garzik 	struct parport *port = dev_id;
10013f2e40dfSJeff Garzik 
1002f230d101SJeff Garzik 	parport_generic_irq(port);
10033f2e40dfSJeff Garzik 
10043f2e40dfSJeff Garzik 	return IRQ_HANDLED;
10053f2e40dfSJeff Garzik }
10063f2e40dfSJeff Garzik 
10071da177e4SLinus Torvalds /* Exported symbols for modules. */
10081da177e4SLinus Torvalds 
10091da177e4SLinus Torvalds EXPORT_SYMBOL(parport_claim);
10101da177e4SLinus Torvalds EXPORT_SYMBOL(parport_claim_or_block);
10111da177e4SLinus Torvalds EXPORT_SYMBOL(parport_release);
10121da177e4SLinus Torvalds EXPORT_SYMBOL(parport_register_port);
10131da177e4SLinus Torvalds EXPORT_SYMBOL(parport_announce_port);
10141da177e4SLinus Torvalds EXPORT_SYMBOL(parport_remove_port);
10151da177e4SLinus Torvalds EXPORT_SYMBOL(parport_register_driver);
10161da177e4SLinus Torvalds EXPORT_SYMBOL(parport_unregister_driver);
10171da177e4SLinus Torvalds EXPORT_SYMBOL(parport_register_device);
10181da177e4SLinus Torvalds EXPORT_SYMBOL(parport_unregister_device);
101994b82095SMarko Kohtala EXPORT_SYMBOL(parport_get_port);
10201da177e4SLinus Torvalds EXPORT_SYMBOL(parport_put_port);
10211da177e4SLinus Torvalds EXPORT_SYMBOL(parport_find_number);
10221da177e4SLinus Torvalds EXPORT_SYMBOL(parport_find_base);
10233f2e40dfSJeff Garzik EXPORT_SYMBOL(parport_irq_handler);
10241da177e4SLinus Torvalds 
10251da177e4SLinus Torvalds MODULE_LICENSE("GPL");
1026