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