1612de10dSAdrian Bunk /* 21da177e4SLinus Torvalds * Parallel-port resource manager code. 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Authors: David Campbell <campbell@tirian.che.curtin.edu.au> 51da177e4SLinus Torvalds * Tim Waugh <tim@cyberelk.demon.co.uk> 61da177e4SLinus Torvalds * Jose Renau <renau@acm.org> 71da177e4SLinus Torvalds * Philip Blundell <philb@gnu.org> 81da177e4SLinus Torvalds * Andrea Arcangeli 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * based on work by Grant Guenther <grant@torque.net> 111da177e4SLinus Torvalds * and Philip Blundell 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Any part of this program may be used in documents licensed under 141da177e4SLinus Torvalds * the GNU Free Documentation License, Version 1.1 or any later version 151da177e4SLinus Torvalds * published by the Free Software Foundation. 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 181da177e4SLinus Torvalds #undef PARPORT_DEBUG_SHARING /* undef for production */ 191da177e4SLinus Torvalds 201da177e4SLinus Torvalds #include <linux/module.h> 211da177e4SLinus Torvalds #include <linux/string.h> 221da177e4SLinus Torvalds #include <linux/threads.h> 231da177e4SLinus Torvalds #include <linux/parport.h> 241da177e4SLinus Torvalds #include <linux/delay.h> 251da177e4SLinus Torvalds #include <linux/errno.h> 261da177e4SLinus Torvalds #include <linux/interrupt.h> 271da177e4SLinus Torvalds #include <linux/ioport.h> 281da177e4SLinus Torvalds #include <linux/kernel.h> 291da177e4SLinus Torvalds #include <linux/slab.h> 301da177e4SLinus Torvalds #include <linux/sched.h> 311da177e4SLinus Torvalds #include <linux/kmod.h> 326fa45a22SSudip Mukherjee #include <linux/device.h> 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds #include <linux/spinlock.h> 3514cc3e2bSIngo Molnar #include <linux/mutex.h> 361da177e4SLinus Torvalds #include <asm/irq.h> 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #undef PARPORT_PARANOID 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #define PARPORT_DEFAULT_TIMESLICE (HZ/5) 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds unsigned long parport_default_timeslice = PARPORT_DEFAULT_TIMESLICE; 431da177e4SLinus Torvalds int parport_default_spintime = DEFAULT_SPIN_TIME; 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds static LIST_HEAD(portlist); 461da177e4SLinus Torvalds static DEFINE_SPINLOCK(parportlist_lock); 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds /* list of all allocated ports, sorted by ->number */ 491da177e4SLinus Torvalds static LIST_HEAD(all_ports); 501da177e4SLinus Torvalds static DEFINE_SPINLOCK(full_list_lock); 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds static LIST_HEAD(drivers); 531da177e4SLinus Torvalds 5414cc3e2bSIngo Molnar static DEFINE_MUTEX(registration_lock); 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds /* What you can do to a port that's gone away.. */ 571da177e4SLinus Torvalds static void dead_write_lines (struct parport *p, unsigned char b){} 581da177e4SLinus Torvalds static unsigned char dead_read_lines (struct parport *p) { return 0; } 591da177e4SLinus Torvalds static unsigned char dead_frob_lines (struct parport *p, unsigned char b, 601da177e4SLinus Torvalds unsigned char c) { return 0; } 611da177e4SLinus Torvalds static void dead_onearg (struct parport *p){} 621da177e4SLinus Torvalds static void dead_initstate (struct pardevice *d, struct parport_state *s) { } 631da177e4SLinus Torvalds static void dead_state (struct parport *p, struct parport_state *s) { } 641da177e4SLinus Torvalds static size_t dead_write (struct parport *p, const void *b, size_t l, int f) 651da177e4SLinus Torvalds { return 0; } 661da177e4SLinus Torvalds static size_t dead_read (struct parport *p, void *b, size_t l, int f) 671da177e4SLinus Torvalds { return 0; } 681da177e4SLinus Torvalds static struct parport_operations dead_ops = { 691da177e4SLinus Torvalds .write_data = dead_write_lines, /* data */ 701da177e4SLinus Torvalds .read_data = dead_read_lines, 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds .write_control = dead_write_lines, /* control */ 731da177e4SLinus Torvalds .read_control = dead_read_lines, 741da177e4SLinus Torvalds .frob_control = dead_frob_lines, 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds .read_status = dead_read_lines, /* status */ 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds .enable_irq = dead_onearg, /* enable_irq */ 791da177e4SLinus Torvalds .disable_irq = dead_onearg, /* disable_irq */ 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds .data_forward = dead_onearg, /* data_forward */ 821da177e4SLinus Torvalds .data_reverse = dead_onearg, /* data_reverse */ 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds .init_state = dead_initstate, /* init_state */ 851da177e4SLinus Torvalds .save_state = dead_state, 861da177e4SLinus Torvalds .restore_state = dead_state, 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds .epp_write_data = dead_write, /* epp */ 891da177e4SLinus Torvalds .epp_read_data = dead_read, 901da177e4SLinus Torvalds .epp_write_addr = dead_write, 911da177e4SLinus Torvalds .epp_read_addr = dead_read, 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds .ecp_write_data = dead_write, /* ecp */ 941da177e4SLinus Torvalds .ecp_read_data = dead_read, 951da177e4SLinus Torvalds .ecp_write_addr = dead_write, 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds .compat_write_data = dead_write, /* compat */ 981da177e4SLinus Torvalds .nibble_read_data = dead_read, /* nibble */ 991da177e4SLinus Torvalds .byte_read_data = dead_read, /* byte */ 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds .owner = NULL, 1021da177e4SLinus Torvalds }; 1031da177e4SLinus Torvalds 1046fa45a22SSudip Mukherjee static struct device_type parport_device_type = { 1056fa45a22SSudip Mukherjee .name = "parport", 1066fa45a22SSudip Mukherjee }; 1076fa45a22SSudip Mukherjee 1086fa45a22SSudip Mukherjee static int is_parport(struct device *dev) 1096fa45a22SSudip Mukherjee { 1106fa45a22SSudip Mukherjee return dev->type == &parport_device_type; 1116fa45a22SSudip Mukherjee } 1126fa45a22SSudip Mukherjee 1136fa45a22SSudip Mukherjee static int parport_probe(struct device *dev) 1146fa45a22SSudip Mukherjee { 1156fa45a22SSudip Mukherjee struct parport_driver *drv; 1166fa45a22SSudip Mukherjee 1176fa45a22SSudip Mukherjee if (is_parport(dev)) 1186fa45a22SSudip Mukherjee return -ENODEV; 1196fa45a22SSudip Mukherjee 1206fa45a22SSudip Mukherjee drv = to_parport_driver(dev->driver); 1216fa45a22SSudip Mukherjee if (!drv->probe) { 1226fa45a22SSudip Mukherjee /* if driver has not defined a custom probe */ 1236fa45a22SSudip Mukherjee struct pardevice *par_dev = to_pardevice(dev); 1246fa45a22SSudip Mukherjee 1256fa45a22SSudip Mukherjee if (strcmp(par_dev->name, drv->name)) 1266fa45a22SSudip Mukherjee return -ENODEV; 1276fa45a22SSudip Mukherjee return 0; 1286fa45a22SSudip Mukherjee } 1296fa45a22SSudip Mukherjee /* if driver defined its own probe */ 1306fa45a22SSudip Mukherjee return drv->probe(to_pardevice(dev)); 1316fa45a22SSudip Mukherjee } 1326fa45a22SSudip Mukherjee 1336fa45a22SSudip Mukherjee static struct bus_type parport_bus_type = { 1346fa45a22SSudip Mukherjee .name = "parport", 1356fa45a22SSudip Mukherjee .probe = parport_probe, 1366fa45a22SSudip Mukherjee }; 1376fa45a22SSudip Mukherjee 1386fa45a22SSudip Mukherjee int parport_bus_init(void) 1396fa45a22SSudip Mukherjee { 1406fa45a22SSudip Mukherjee return bus_register(&parport_bus_type); 1416fa45a22SSudip Mukherjee } 1426fa45a22SSudip Mukherjee 1436fa45a22SSudip Mukherjee void parport_bus_exit(void) 1446fa45a22SSudip Mukherjee { 1456fa45a22SSudip Mukherjee bus_unregister(&parport_bus_type); 1466fa45a22SSudip Mukherjee } 1476fa45a22SSudip Mukherjee 1486fa45a22SSudip Mukherjee /* 1496fa45a22SSudip Mukherjee * iterates through all the drivers registered with the bus and sends the port 1506fa45a22SSudip Mukherjee * details to the match_port callback of the driver, so that the driver can 151e9527365SGeliang Tang * know about the new port that just registered with the bus and decide if it 1526fa45a22SSudip Mukherjee * wants to use this new port. 1536fa45a22SSudip Mukherjee */ 1546fa45a22SSudip Mukherjee static int driver_check(struct device_driver *dev_drv, void *_port) 1556fa45a22SSudip Mukherjee { 1566fa45a22SSudip Mukherjee struct parport *port = _port; 1576fa45a22SSudip Mukherjee struct parport_driver *drv = to_parport_driver(dev_drv); 1586fa45a22SSudip Mukherjee 1596fa45a22SSudip Mukherjee if (drv->match_port) 1606fa45a22SSudip Mukherjee drv->match_port(port); 1616fa45a22SSudip Mukherjee return 0; 1626fa45a22SSudip Mukherjee } 1636fa45a22SSudip Mukherjee 1641da177e4SLinus Torvalds /* Call attach(port) for each registered driver. */ 1651da177e4SLinus Torvalds static void attach_driver_chain(struct parport *port) 1661da177e4SLinus Torvalds { 1671da177e4SLinus Torvalds /* caller has exclusive registration_lock */ 1681da177e4SLinus Torvalds struct parport_driver *drv; 1696fa45a22SSudip Mukherjee 1701da177e4SLinus Torvalds list_for_each_entry(drv, &drivers, list) 1711da177e4SLinus Torvalds drv->attach(port); 1726fa45a22SSudip Mukherjee 1736fa45a22SSudip Mukherjee /* 1746fa45a22SSudip Mukherjee * call the driver_check function of the drivers registered in 1756fa45a22SSudip Mukherjee * new device model 1766fa45a22SSudip Mukherjee */ 1776fa45a22SSudip Mukherjee 1786fa45a22SSudip Mukherjee bus_for_each_drv(&parport_bus_type, NULL, port, driver_check); 1796fa45a22SSudip Mukherjee } 1806fa45a22SSudip Mukherjee 1816fa45a22SSudip Mukherjee static int driver_detach(struct device_driver *_drv, void *_port) 1826fa45a22SSudip Mukherjee { 1836fa45a22SSudip Mukherjee struct parport *port = _port; 1846fa45a22SSudip Mukherjee struct parport_driver *drv = to_parport_driver(_drv); 1856fa45a22SSudip Mukherjee 1866fa45a22SSudip Mukherjee if (drv->detach) 1876fa45a22SSudip Mukherjee drv->detach(port); 1886fa45a22SSudip Mukherjee return 0; 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds /* Call detach(port) for each registered driver. */ 1921da177e4SLinus Torvalds static void detach_driver_chain(struct parport *port) 1931da177e4SLinus Torvalds { 1941da177e4SLinus Torvalds struct parport_driver *drv; 1951da177e4SLinus Torvalds /* caller has exclusive registration_lock */ 1961da177e4SLinus Torvalds list_for_each_entry(drv, &drivers, list) 1971da177e4SLinus Torvalds drv->detach (port); 1986fa45a22SSudip Mukherjee 1996fa45a22SSudip Mukherjee /* 2006fa45a22SSudip Mukherjee * call the detach function of the drivers registered in 2016fa45a22SSudip Mukherjee * new device model 2026fa45a22SSudip Mukherjee */ 2036fa45a22SSudip Mukherjee 2046fa45a22SSudip Mukherjee bus_for_each_drv(&parport_bus_type, NULL, port, driver_detach); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds /* Ask kmod for some lowlevel drivers. */ 2081da177e4SLinus Torvalds static void get_lowlevel_driver (void) 2091da177e4SLinus Torvalds { 2107b7a0a30SSudip Mukherjee /* 2117b7a0a30SSudip Mukherjee * There is no actual module called this: you should set 2127b7a0a30SSudip Mukherjee * up an alias for modutils. 2137b7a0a30SSudip Mukherjee */ 2141da177e4SLinus Torvalds request_module ("parport_lowlevel"); 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 2176fa45a22SSudip Mukherjee /* 2186fa45a22SSudip Mukherjee * iterates through all the devices connected to the bus and sends the device 2196fa45a22SSudip Mukherjee * details to the match_port callback of the driver, so that the driver can 2206fa45a22SSudip Mukherjee * know what are all the ports that are connected to the bus and choose the 2216fa45a22SSudip Mukherjee * port to which it wants to register its device. 2226fa45a22SSudip Mukherjee */ 2236fa45a22SSudip Mukherjee static int port_check(struct device *dev, void *dev_drv) 2246fa45a22SSudip Mukherjee { 2256fa45a22SSudip Mukherjee struct parport_driver *drv = dev_drv; 2266fa45a22SSudip Mukherjee 2276fa45a22SSudip Mukherjee /* only send ports, do not send other devices connected to bus */ 2286fa45a22SSudip Mukherjee if (is_parport(dev)) 2296fa45a22SSudip Mukherjee drv->match_port(to_parport_dev(dev)); 2306fa45a22SSudip Mukherjee return 0; 2316fa45a22SSudip Mukherjee } 2326fa45a22SSudip Mukherjee 2331da177e4SLinus Torvalds /** 2341da177e4SLinus Torvalds * parport_register_driver - register a parallel port device driver 2351da177e4SLinus Torvalds * @drv: structure describing the driver 2366fa45a22SSudip Mukherjee * @owner: owner module of drv 2376fa45a22SSudip Mukherjee * @mod_name: module name string 2381da177e4SLinus Torvalds * 2391da177e4SLinus Torvalds * This can be called by a parallel port device driver in order 2401da177e4SLinus Torvalds * to receive notifications about ports being found in the 2411da177e4SLinus Torvalds * system, as well as ports no longer available. 2421da177e4SLinus Torvalds * 2436fa45a22SSudip Mukherjee * If devmodel is true then the new device model is used 2446fa45a22SSudip Mukherjee * for registration. 2456fa45a22SSudip Mukherjee * 2461da177e4SLinus Torvalds * The @drv structure is allocated by the caller and must not be 2471da177e4SLinus Torvalds * deallocated until after calling parport_unregister_driver(). 2481da177e4SLinus Torvalds * 2496fa45a22SSudip Mukherjee * If using the non device model: 2501da177e4SLinus Torvalds * The driver's attach() function may block. The port that 2511da177e4SLinus Torvalds * attach() is given will be valid for the duration of the 2521da177e4SLinus Torvalds * callback, but if the driver wants to take a copy of the 2531da177e4SLinus Torvalds * pointer it must call parport_get_port() to do so. Calling 2541da177e4SLinus Torvalds * parport_register_device() on that port will do this for you. 2551da177e4SLinus Torvalds * 2561da177e4SLinus Torvalds * The driver's detach() function may block. The port that 2571da177e4SLinus Torvalds * detach() is given will be valid for the duration of the 2581da177e4SLinus Torvalds * callback, but if the driver wants to take a copy of the 2591da177e4SLinus Torvalds * pointer it must call parport_get_port() to do so. 2601da177e4SLinus Torvalds * 2616fa45a22SSudip Mukherjee * 2626fa45a22SSudip Mukherjee * Returns 0 on success. The non device model will always succeeds. 2636fa45a22SSudip Mukherjee * but the new device model can fail and will return the error code. 2641da177e4SLinus Torvalds **/ 2651da177e4SLinus Torvalds 2666fa45a22SSudip Mukherjee int __parport_register_driver(struct parport_driver *drv, struct module *owner, 2676fa45a22SSudip Mukherjee const char *mod_name) 2681da177e4SLinus Torvalds { 2691da177e4SLinus Torvalds if (list_empty(&portlist)) 2701da177e4SLinus Torvalds get_lowlevel_driver (); 2711da177e4SLinus Torvalds 2726fa45a22SSudip Mukherjee if (drv->devmodel) { 2736fa45a22SSudip Mukherjee /* using device model */ 2746fa45a22SSudip Mukherjee int ret; 2756fa45a22SSudip Mukherjee 2766fa45a22SSudip Mukherjee /* initialize common driver fields */ 2776fa45a22SSudip Mukherjee drv->driver.name = drv->name; 2786fa45a22SSudip Mukherjee drv->driver.bus = &parport_bus_type; 2796fa45a22SSudip Mukherjee drv->driver.owner = owner; 2806fa45a22SSudip Mukherjee drv->driver.mod_name = mod_name; 2816fa45a22SSudip Mukherjee ret = driver_register(&drv->driver); 2826fa45a22SSudip Mukherjee if (ret) 2836fa45a22SSudip Mukherjee return ret; 2846fa45a22SSudip Mukherjee 2856fa45a22SSudip Mukherjee mutex_lock(®istration_lock); 2866fa45a22SSudip Mukherjee if (drv->match_port) 2876fa45a22SSudip Mukherjee bus_for_each_dev(&parport_bus_type, NULL, drv, 2886fa45a22SSudip Mukherjee port_check); 2896fa45a22SSudip Mukherjee mutex_unlock(®istration_lock); 2906fa45a22SSudip Mukherjee } else { 2916fa45a22SSudip Mukherjee struct parport *port; 2926fa45a22SSudip Mukherjee 2936fa45a22SSudip Mukherjee drv->devmodel = false; 2946fa45a22SSudip Mukherjee 29514cc3e2bSIngo Molnar mutex_lock(®istration_lock); 2961da177e4SLinus Torvalds list_for_each_entry(port, &portlist, list) 2971da177e4SLinus Torvalds drv->attach(port); 2981da177e4SLinus Torvalds list_add(&drv->list, &drivers); 29914cc3e2bSIngo Molnar mutex_unlock(®istration_lock); 3006fa45a22SSudip Mukherjee } 3016fa45a22SSudip Mukherjee 3026fa45a22SSudip Mukherjee return 0; 3036fa45a22SSudip Mukherjee } 3046fa45a22SSudip Mukherjee EXPORT_SYMBOL(__parport_register_driver); 3056fa45a22SSudip Mukherjee 3066fa45a22SSudip Mukherjee static int port_detach(struct device *dev, void *_drv) 3076fa45a22SSudip Mukherjee { 3086fa45a22SSudip Mukherjee struct parport_driver *drv = _drv; 3096fa45a22SSudip Mukherjee 3106fa45a22SSudip Mukherjee if (is_parport(dev) && drv->detach) 3116fa45a22SSudip Mukherjee drv->detach(to_parport_dev(dev)); 3121da177e4SLinus Torvalds 3131da177e4SLinus Torvalds return 0; 3141da177e4SLinus Torvalds } 3151da177e4SLinus Torvalds 3161da177e4SLinus Torvalds /** 3171da177e4SLinus Torvalds * parport_unregister_driver - deregister a parallel port device driver 3181da177e4SLinus Torvalds * @drv: structure describing the driver that was given to 3191da177e4SLinus Torvalds * parport_register_driver() 3201da177e4SLinus Torvalds * 3211da177e4SLinus Torvalds * This should be called by a parallel port device driver that 3221da177e4SLinus Torvalds * has registered itself using parport_register_driver() when it 3231da177e4SLinus Torvalds * is about to be unloaded. 3241da177e4SLinus Torvalds * 3251da177e4SLinus Torvalds * When it returns, the driver's attach() routine will no longer 3261da177e4SLinus Torvalds * be called, and for each port that attach() was called for, the 3271da177e4SLinus Torvalds * detach() routine will have been called. 3281da177e4SLinus Torvalds * 3291da177e4SLinus Torvalds * All the driver's attach() and detach() calls are guaranteed to have 3301da177e4SLinus Torvalds * finished by the time this function returns. 3311da177e4SLinus Torvalds **/ 3321da177e4SLinus Torvalds 3331da177e4SLinus Torvalds void parport_unregister_driver (struct parport_driver *drv) 3341da177e4SLinus Torvalds { 3351da177e4SLinus Torvalds struct parport *port; 3361da177e4SLinus Torvalds 33714cc3e2bSIngo Molnar mutex_lock(®istration_lock); 3386fa45a22SSudip Mukherjee if (drv->devmodel) { 3396fa45a22SSudip Mukherjee bus_for_each_dev(&parport_bus_type, NULL, drv, port_detach); 3406fa45a22SSudip Mukherjee driver_unregister(&drv->driver); 3416fa45a22SSudip Mukherjee } else { 3421da177e4SLinus Torvalds list_del_init(&drv->list); 3431da177e4SLinus Torvalds list_for_each_entry(port, &portlist, list) 3441da177e4SLinus Torvalds drv->detach(port); 3456fa45a22SSudip Mukherjee } 34614cc3e2bSIngo Molnar mutex_unlock(®istration_lock); 3471da177e4SLinus Torvalds } 34827c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_unregister_driver); 3491da177e4SLinus Torvalds 3506fa45a22SSudip Mukherjee static void free_port(struct device *dev) 3511da177e4SLinus Torvalds { 3521da177e4SLinus Torvalds int d; 3536fa45a22SSudip Mukherjee struct parport *port = to_parport_dev(dev); 3546fa45a22SSudip Mukherjee 3551da177e4SLinus Torvalds spin_lock(&full_list_lock); 3561da177e4SLinus Torvalds list_del(&port->full_list); 3571da177e4SLinus Torvalds spin_unlock(&full_list_lock); 3581da177e4SLinus Torvalds for (d = 0; d < 5; d++) { 3591da177e4SLinus Torvalds kfree(port->probe_info[d].class_name); 3601da177e4SLinus Torvalds kfree(port->probe_info[d].mfr); 3611da177e4SLinus Torvalds kfree(port->probe_info[d].model); 3621da177e4SLinus Torvalds kfree(port->probe_info[d].cmdset); 3631da177e4SLinus Torvalds kfree(port->probe_info[d].description); 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3661da177e4SLinus Torvalds kfree(port->name); 3671da177e4SLinus Torvalds kfree(port); 3681da177e4SLinus Torvalds } 3691da177e4SLinus Torvalds 3701da177e4SLinus Torvalds /** 3711da177e4SLinus Torvalds * parport_get_port - increment a port's reference count 3721da177e4SLinus Torvalds * @port: the port 3731da177e4SLinus Torvalds * 3747d469722SRandy Dunlap * This ensures that a struct parport pointer remains valid 3751da177e4SLinus Torvalds * until the matching parport_put_port() call. 3761da177e4SLinus Torvalds **/ 3771da177e4SLinus Torvalds 3781da177e4SLinus Torvalds struct parport *parport_get_port (struct parport *port) 3791da177e4SLinus Torvalds { 3806fa45a22SSudip Mukherjee struct device *dev = get_device(&port->bus_dev); 3816fa45a22SSudip Mukherjee 3826fa45a22SSudip Mukherjee return to_parport_dev(dev); 3831da177e4SLinus Torvalds } 38427c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_get_port); 3851da177e4SLinus Torvalds 3866fa45a22SSudip Mukherjee void parport_del_port(struct parport *port) 3876fa45a22SSudip Mukherjee { 3886fa45a22SSudip Mukherjee device_unregister(&port->bus_dev); 3896fa45a22SSudip Mukherjee } 3906fa45a22SSudip Mukherjee EXPORT_SYMBOL(parport_del_port); 3916fa45a22SSudip Mukherjee 3921da177e4SLinus Torvalds /** 3931da177e4SLinus Torvalds * parport_put_port - decrement a port's reference count 3941da177e4SLinus Torvalds * @port: the port 3951da177e4SLinus Torvalds * 3961da177e4SLinus Torvalds * This should be called once for each call to parport_get_port(), 3976fa45a22SSudip Mukherjee * once the port is no longer needed. When the reference count reaches 3986fa45a22SSudip Mukherjee * zero (port is no longer used), free_port is called. 3991da177e4SLinus Torvalds **/ 4001da177e4SLinus Torvalds 4011da177e4SLinus Torvalds void parport_put_port (struct parport *port) 4021da177e4SLinus Torvalds { 4036fa45a22SSudip Mukherjee put_device(&port->bus_dev); 4041da177e4SLinus Torvalds } 40527c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_put_port); 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds /** 4081da177e4SLinus Torvalds * parport_register_port - register a parallel port 4091da177e4SLinus Torvalds * @base: base I/O address 4101da177e4SLinus Torvalds * @irq: IRQ line 4111da177e4SLinus Torvalds * @dma: DMA channel 4121da177e4SLinus Torvalds * @ops: pointer to the port driver's port operations structure 4131da177e4SLinus Torvalds * 4141da177e4SLinus Torvalds * When a parallel port (lowlevel) driver finds a port that 4151da177e4SLinus Torvalds * should be made available to parallel port device drivers, it 4161da177e4SLinus Torvalds * should call parport_register_port(). The @base, @irq, and 4171da177e4SLinus Torvalds * @dma parameters are for the convenience of port drivers, and 4181da177e4SLinus Torvalds * for ports where they aren't meaningful needn't be set to 4191da177e4SLinus Torvalds * anything special. They can be altered afterwards by adjusting 4201da177e4SLinus Torvalds * the relevant members of the parport structure that is returned 4211da177e4SLinus Torvalds * and represents the port. They should not be tampered with 4221da177e4SLinus Torvalds * after calling parport_announce_port, however. 4231da177e4SLinus Torvalds * 4241da177e4SLinus Torvalds * If there are parallel port device drivers in the system that 4251da177e4SLinus Torvalds * have registered themselves using parport_register_driver(), 4261da177e4SLinus Torvalds * they are not told about the port at this time; that is done by 4271da177e4SLinus Torvalds * parport_announce_port(). 4281da177e4SLinus Torvalds * 4291da177e4SLinus Torvalds * The @ops structure is allocated by the caller, and must not be 4301da177e4SLinus Torvalds * deallocated before calling parport_remove_port(). 4311da177e4SLinus Torvalds * 4321da177e4SLinus Torvalds * If there is no memory to allocate a new parport structure, 4331da177e4SLinus Torvalds * this function will return %NULL. 4341da177e4SLinus Torvalds **/ 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds struct parport *parport_register_port(unsigned long base, int irq, int dma, 4371da177e4SLinus Torvalds struct parport_operations *ops) 4381da177e4SLinus Torvalds { 4391da177e4SLinus Torvalds struct list_head *l; 4401da177e4SLinus Torvalds struct parport *tmp; 4411da177e4SLinus Torvalds int num; 4421da177e4SLinus Torvalds int device; 4431da177e4SLinus Torvalds char *name; 4446fa45a22SSudip Mukherjee int ret; 4451da177e4SLinus Torvalds 446c32df4e1SAlexandru Gheorghiu tmp = kzalloc(sizeof(struct parport), GFP_KERNEL); 4471da177e4SLinus Torvalds if (!tmp) { 4481da177e4SLinus Torvalds printk(KERN_WARNING "parport: memory squeeze\n"); 4491da177e4SLinus Torvalds return NULL; 4501da177e4SLinus Torvalds } 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds /* Init our structure */ 4531da177e4SLinus Torvalds tmp->base = base; 4541da177e4SLinus Torvalds tmp->irq = irq; 4551da177e4SLinus Torvalds tmp->dma = dma; 4561da177e4SLinus Torvalds tmp->muxport = tmp->daisy = tmp->muxsel = -1; 4571da177e4SLinus Torvalds tmp->modes = 0; 4581da177e4SLinus Torvalds INIT_LIST_HEAD(&tmp->list); 4591da177e4SLinus Torvalds tmp->devices = tmp->cad = NULL; 4601da177e4SLinus Torvalds tmp->flags = 0; 4611da177e4SLinus Torvalds tmp->ops = ops; 4621da177e4SLinus Torvalds tmp->physport = tmp; 4631da177e4SLinus Torvalds memset (tmp->probe_info, 0, 5 * sizeof (struct parport_device_info)); 4641da177e4SLinus Torvalds rwlock_init(&tmp->cad_lock); 4651da177e4SLinus Torvalds spin_lock_init(&tmp->waitlist_lock); 4661da177e4SLinus Torvalds spin_lock_init(&tmp->pardevice_lock); 4671da177e4SLinus Torvalds tmp->ieee1284.mode = IEEE1284_MODE_COMPAT; 4681da177e4SLinus Torvalds tmp->ieee1284.phase = IEEE1284_PH_FWD_IDLE; 469a529f150SThomas Gleixner sema_init(&tmp->ieee1284.irq, 0); 4701da177e4SLinus Torvalds tmp->spintime = parport_default_spintime; 4711da177e4SLinus Torvalds atomic_set (&tmp->ref_count, 1); 4721da177e4SLinus Torvalds INIT_LIST_HEAD(&tmp->full_list); 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds name = kmalloc(15, GFP_KERNEL); 4751da177e4SLinus Torvalds if (!name) { 4761da177e4SLinus Torvalds printk(KERN_ERR "parport: memory squeeze\n"); 4771da177e4SLinus Torvalds kfree(tmp); 4781da177e4SLinus Torvalds return NULL; 4791da177e4SLinus Torvalds } 4801da177e4SLinus Torvalds /* Search for the lowest free parport number. */ 4811da177e4SLinus Torvalds 4821da177e4SLinus Torvalds spin_lock(&full_list_lock); 4831da177e4SLinus Torvalds for (l = all_ports.next, num = 0; l != &all_ports; l = l->next, num++) { 4841da177e4SLinus Torvalds struct parport *p = list_entry(l, struct parport, full_list); 4851da177e4SLinus Torvalds if (p->number != num) 4861da177e4SLinus Torvalds break; 4871da177e4SLinus Torvalds } 4881da177e4SLinus Torvalds tmp->portnum = tmp->number = num; 4891da177e4SLinus Torvalds list_add_tail(&tmp->full_list, l); 4901da177e4SLinus Torvalds spin_unlock(&full_list_lock); 4911da177e4SLinus Torvalds 4921da177e4SLinus Torvalds /* 4931da177e4SLinus Torvalds * Now that the portnum is known finish doing the Init. 4941da177e4SLinus Torvalds */ 4951da177e4SLinus Torvalds sprintf(name, "parport%d", tmp->portnum = tmp->number); 4961da177e4SLinus Torvalds tmp->name = name; 4976fa45a22SSudip Mukherjee tmp->bus_dev.bus = &parport_bus_type; 4986fa45a22SSudip Mukherjee tmp->bus_dev.release = free_port; 4996fa45a22SSudip Mukherjee dev_set_name(&tmp->bus_dev, name); 5006fa45a22SSudip Mukherjee tmp->bus_dev.type = &parport_device_type; 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds for (device = 0; device < 5; device++) 5031da177e4SLinus Torvalds /* assume the worst */ 5041da177e4SLinus Torvalds tmp->probe_info[device].class = PARPORT_CLASS_LEGACY; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds tmp->waithead = tmp->waittail = NULL; 5071da177e4SLinus Torvalds 5086fa45a22SSudip Mukherjee ret = device_register(&tmp->bus_dev); 5096fa45a22SSudip Mukherjee if (ret) { 5106fa45a22SSudip Mukherjee put_device(&tmp->bus_dev); 5116fa45a22SSudip Mukherjee return NULL; 5126fa45a22SSudip Mukherjee } 5136fa45a22SSudip Mukherjee 5141da177e4SLinus Torvalds return tmp; 5151da177e4SLinus Torvalds } 51627c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_register_port); 5171da177e4SLinus Torvalds 5181da177e4SLinus Torvalds /** 5191da177e4SLinus Torvalds * parport_announce_port - tell device drivers about a parallel port 5201da177e4SLinus Torvalds * @port: parallel port to announce 5211da177e4SLinus Torvalds * 5221da177e4SLinus Torvalds * After a port driver has registered a parallel port with 5231da177e4SLinus Torvalds * parport_register_port, and performed any necessary 5241da177e4SLinus Torvalds * initialisation or adjustments, it should call 5251da177e4SLinus Torvalds * parport_announce_port() in order to notify all device drivers 5261da177e4SLinus Torvalds * that have called parport_register_driver(). Their attach() 5271da177e4SLinus Torvalds * functions will be called, with @port as the parameter. 5281da177e4SLinus Torvalds **/ 5291da177e4SLinus Torvalds 5301da177e4SLinus Torvalds void parport_announce_port (struct parport *port) 5311da177e4SLinus Torvalds { 5321da177e4SLinus Torvalds int i; 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284 5351da177e4SLinus Torvalds /* Analyse the IEEE1284.3 topology of the port. */ 5361da177e4SLinus Torvalds parport_daisy_init(port); 5371da177e4SLinus Torvalds #endif 5381da177e4SLinus Torvalds 539c15a3837SDavid Brownell if (!port->dev) 540a162188fSSudip Mukherjee printk(KERN_WARNING "%s: fix this legacy no-device port driver!\n", 541c15a3837SDavid Brownell port->name); 542c15a3837SDavid Brownell 5431da177e4SLinus Torvalds parport_proc_register(port); 54414cc3e2bSIngo Molnar mutex_lock(®istration_lock); 5451da177e4SLinus Torvalds spin_lock_irq(&parportlist_lock); 5461da177e4SLinus Torvalds list_add_tail(&port->list, &portlist); 5471da177e4SLinus Torvalds for (i = 1; i < 3; i++) { 5481da177e4SLinus Torvalds struct parport *slave = port->slaves[i-1]; 5491da177e4SLinus Torvalds if (slave) 5501da177e4SLinus Torvalds list_add_tail(&slave->list, &portlist); 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds spin_unlock_irq(&parportlist_lock); 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds /* Let drivers know that new port(s) has arrived. */ 5551da177e4SLinus Torvalds attach_driver_chain (port); 5561da177e4SLinus Torvalds for (i = 1; i < 3; i++) { 5571da177e4SLinus Torvalds struct parport *slave = port->slaves[i-1]; 5581da177e4SLinus Torvalds if (slave) 5591da177e4SLinus Torvalds attach_driver_chain(slave); 5601da177e4SLinus Torvalds } 56114cc3e2bSIngo Molnar mutex_unlock(®istration_lock); 5621da177e4SLinus Torvalds } 56327c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_announce_port); 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds /** 5661da177e4SLinus Torvalds * parport_remove_port - deregister a parallel port 5671da177e4SLinus Torvalds * @port: parallel port to deregister 5681da177e4SLinus Torvalds * 5691da177e4SLinus Torvalds * When a parallel port driver is forcibly unloaded, or a 5701da177e4SLinus Torvalds * parallel port becomes inaccessible, the port driver must call 5711da177e4SLinus Torvalds * this function in order to deal with device drivers that still 5721da177e4SLinus Torvalds * want to use it. 5731da177e4SLinus Torvalds * 5741da177e4SLinus Torvalds * The parport structure associated with the port has its 5751da177e4SLinus Torvalds * operations structure replaced with one containing 'null' 5761da177e4SLinus Torvalds * operations that return errors or just don't do anything. 5771da177e4SLinus Torvalds * 5781da177e4SLinus Torvalds * Any drivers that have registered themselves using 5791da177e4SLinus Torvalds * parport_register_driver() are notified that the port is no 5801da177e4SLinus Torvalds * longer accessible by having their detach() routines called 5811da177e4SLinus Torvalds * with @port as the parameter. 5821da177e4SLinus Torvalds **/ 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds void parport_remove_port(struct parport *port) 5851da177e4SLinus Torvalds { 5861da177e4SLinus Torvalds int i; 5871da177e4SLinus Torvalds 58814cc3e2bSIngo Molnar mutex_lock(®istration_lock); 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds /* Spread the word. */ 5911da177e4SLinus Torvalds detach_driver_chain (port); 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284 5941da177e4SLinus Torvalds /* Forget the IEEE1284.3 topology of the port. */ 5951da177e4SLinus Torvalds parport_daisy_fini(port); 5961da177e4SLinus Torvalds for (i = 1; i < 3; i++) { 5971da177e4SLinus Torvalds struct parport *slave = port->slaves[i-1]; 5981da177e4SLinus Torvalds if (!slave) 5991da177e4SLinus Torvalds continue; 6001da177e4SLinus Torvalds detach_driver_chain(slave); 6011da177e4SLinus Torvalds parport_daisy_fini(slave); 6021da177e4SLinus Torvalds } 6031da177e4SLinus Torvalds #endif 6041da177e4SLinus Torvalds 6051da177e4SLinus Torvalds port->ops = &dead_ops; 6061da177e4SLinus Torvalds spin_lock(&parportlist_lock); 6071da177e4SLinus Torvalds list_del_init(&port->list); 6081da177e4SLinus Torvalds for (i = 1; i < 3; i++) { 6091da177e4SLinus Torvalds struct parport *slave = port->slaves[i-1]; 6101da177e4SLinus Torvalds if (slave) 6111da177e4SLinus Torvalds list_del_init(&slave->list); 6121da177e4SLinus Torvalds } 6131da177e4SLinus Torvalds spin_unlock(&parportlist_lock); 6141da177e4SLinus Torvalds 61514cc3e2bSIngo Molnar mutex_unlock(®istration_lock); 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds parport_proc_unregister(port); 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds for (i = 1; i < 3; i++) { 6201da177e4SLinus Torvalds struct parport *slave = port->slaves[i-1]; 6211da177e4SLinus Torvalds if (slave) 6221da177e4SLinus Torvalds parport_put_port(slave); 6231da177e4SLinus Torvalds } 6241da177e4SLinus Torvalds } 62527c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_remove_port); 6261da177e4SLinus Torvalds 6271da177e4SLinus Torvalds /** 6281da177e4SLinus Torvalds * parport_register_device - register a device on a parallel port 6291da177e4SLinus Torvalds * @port: port to which the device is attached 6301da177e4SLinus Torvalds * @name: a name to refer to the device 6311da177e4SLinus Torvalds * @pf: preemption callback 6321da177e4SLinus Torvalds * @kf: kick callback (wake-up) 6331da177e4SLinus Torvalds * @irq_func: interrupt handler 6341da177e4SLinus Torvalds * @flags: registration flags 6351da177e4SLinus Torvalds * @handle: data for callback functions 6361da177e4SLinus Torvalds * 6371da177e4SLinus Torvalds * This function, called by parallel port device drivers, 6381da177e4SLinus Torvalds * declares that a device is connected to a port, and tells the 6391da177e4SLinus Torvalds * system all it needs to know. 6401da177e4SLinus Torvalds * 6411da177e4SLinus Torvalds * The @name is allocated by the caller and must not be 6421da177e4SLinus Torvalds * deallocated until the caller calls @parport_unregister_device 6431da177e4SLinus Torvalds * for that device. 6441da177e4SLinus Torvalds * 6451da177e4SLinus Torvalds * The preemption callback function, @pf, is called when this 6461da177e4SLinus Torvalds * device driver has claimed access to the port but another 6471da177e4SLinus Torvalds * device driver wants to use it. It is given @handle as its 6481da177e4SLinus Torvalds * parameter, and should return zero if it is willing for the 6491da177e4SLinus Torvalds * system to release the port to another driver on its behalf. 6501da177e4SLinus Torvalds * If it wants to keep control of the port it should return 6511da177e4SLinus Torvalds * non-zero, and no action will be taken. It is good manners for 6521da177e4SLinus Torvalds * the driver to try to release the port at the earliest 6531da177e4SLinus Torvalds * opportunity after its preemption callback rejects a preemption 6541da177e4SLinus Torvalds * attempt. Note that if a preemption callback is happy for 6551da177e4SLinus Torvalds * preemption to go ahead, there is no need to release the port; 6561da177e4SLinus Torvalds * it is done automatically. This function may not block, as it 6571da177e4SLinus Torvalds * may be called from interrupt context. If the device driver 6581da177e4SLinus Torvalds * does not support preemption, @pf can be %NULL. 6591da177e4SLinus Torvalds * 6601da177e4SLinus Torvalds * The wake-up ("kick") callback function, @kf, is called when 6611da177e4SLinus Torvalds * the port is available to be claimed for exclusive access; that 6621da177e4SLinus Torvalds * is, parport_claim() is guaranteed to succeed when called from 6631da177e4SLinus Torvalds * inside the wake-up callback function. If the driver wants to 6641da177e4SLinus Torvalds * claim the port it should do so; otherwise, it need not take 6651da177e4SLinus Torvalds * any action. This function may not block, as it may be called 6661da177e4SLinus Torvalds * from interrupt context. If the device driver does not want to 6671da177e4SLinus Torvalds * be explicitly invited to claim the port in this way, @kf can 6681da177e4SLinus Torvalds * be %NULL. 6691da177e4SLinus Torvalds * 6701da177e4SLinus Torvalds * The interrupt handler, @irq_func, is called when an interrupt 6711da177e4SLinus Torvalds * arrives from the parallel port. Note that if a device driver 6721da177e4SLinus Torvalds * wants to use interrupts it should use parport_enable_irq(), 6731da177e4SLinus Torvalds * and can also check the irq member of the parport structure 6741da177e4SLinus Torvalds * representing the port. 6751da177e4SLinus Torvalds * 6761da177e4SLinus Torvalds * The parallel port (lowlevel) driver is the one that has called 6771da177e4SLinus Torvalds * request_irq() and whose interrupt handler is called first. 6781da177e4SLinus Torvalds * This handler does whatever needs to be done to the hardware to 6791da177e4SLinus Torvalds * acknowledge the interrupt (for PC-style ports there is nothing 6801da177e4SLinus Torvalds * special to be done). It then tells the IEEE 1284 code about 6811da177e4SLinus Torvalds * the interrupt, which may involve reacting to an IEEE 1284 6821da177e4SLinus Torvalds * event depending on the current IEEE 1284 phase. After this, 6831da177e4SLinus Torvalds * it calls @irq_func. Needless to say, @irq_func will be called 6841da177e4SLinus Torvalds * from interrupt context, and may not block. 6851da177e4SLinus Torvalds * 6861da177e4SLinus Torvalds * The %PARPORT_DEV_EXCL flag is for preventing port sharing, and 6871da177e4SLinus Torvalds * so should only be used when sharing the port with other device 6881da177e4SLinus Torvalds * drivers is impossible and would lead to incorrect behaviour. 6891da177e4SLinus Torvalds * Use it sparingly! Normally, @flags will be zero. 6901da177e4SLinus Torvalds * 6911da177e4SLinus Torvalds * This function returns a pointer to a structure that represents 6921da177e4SLinus Torvalds * the device on the port, or %NULL if there is not enough memory 6931da177e4SLinus Torvalds * to allocate space for that structure. 6941da177e4SLinus Torvalds **/ 6951da177e4SLinus Torvalds 6961da177e4SLinus Torvalds struct pardevice * 6971da177e4SLinus Torvalds parport_register_device(struct parport *port, const char *name, 6981da177e4SLinus Torvalds int (*pf)(void *), void (*kf)(void *), 6995712cb3dSJeff Garzik void (*irq_func)(void *), 7001da177e4SLinus Torvalds int flags, void *handle) 7011da177e4SLinus Torvalds { 7021da177e4SLinus Torvalds struct pardevice *tmp; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds if (port->physport->flags & PARPORT_FLAG_EXCL) { 7051da177e4SLinus Torvalds /* An exclusive device is registered. */ 7061da177e4SLinus Torvalds printk (KERN_DEBUG "%s: no more devices allowed\n", 7071da177e4SLinus Torvalds port->name); 7081da177e4SLinus Torvalds return NULL; 7091da177e4SLinus Torvalds } 7101da177e4SLinus Torvalds 7111da177e4SLinus Torvalds if (flags & PARPORT_DEV_LURK) { 7121da177e4SLinus Torvalds if (!pf || !kf) { 7131da177e4SLinus Torvalds printk(KERN_INFO "%s: refused to register lurking device (%s) without callbacks\n", port->name, name); 7141da177e4SLinus Torvalds return NULL; 7151da177e4SLinus Torvalds } 7161da177e4SLinus Torvalds } 7171da177e4SLinus Torvalds 71850566ac8SSudip Mukherjee if (flags & PARPORT_DEV_EXCL) { 71950566ac8SSudip Mukherjee if (port->physport->devices) { 72050566ac8SSudip Mukherjee /* 72150566ac8SSudip Mukherjee * If a device is already registered and this new 72250566ac8SSudip Mukherjee * device wants exclusive access, then no need to 72350566ac8SSudip Mukherjee * continue as we can not grant exclusive access to 72450566ac8SSudip Mukherjee * this device. 72550566ac8SSudip Mukherjee */ 72650566ac8SSudip Mukherjee pr_err("%s: cannot grant exclusive access for device %s\n", 72750566ac8SSudip Mukherjee port->name, name); 72850566ac8SSudip Mukherjee return NULL; 72950566ac8SSudip Mukherjee } 73050566ac8SSudip Mukherjee } 73150566ac8SSudip Mukherjee 7327b7a0a30SSudip Mukherjee /* 7337b7a0a30SSudip Mukherjee * We up our own module reference count, and that of the port 7347b7a0a30SSudip Mukherjee * on which a device is to be registered, to ensure that 7357b7a0a30SSudip Mukherjee * neither of us gets unloaded while we sleep in (e.g.) 7367b7a0a30SSudip Mukherjee * kmalloc. 7371da177e4SLinus Torvalds */ 7381da177e4SLinus Torvalds if (!try_module_get(port->ops->owner)) { 7391da177e4SLinus Torvalds return NULL; 7401da177e4SLinus Torvalds } 7411da177e4SLinus Torvalds 7421da177e4SLinus Torvalds parport_get_port (port); 7431da177e4SLinus Torvalds 7441da177e4SLinus Torvalds tmp = kmalloc(sizeof(struct pardevice), GFP_KERNEL); 7451da177e4SLinus Torvalds if (tmp == NULL) { 7461da177e4SLinus Torvalds printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name); 7471da177e4SLinus Torvalds goto out; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds tmp->state = kmalloc(sizeof(struct parport_state), GFP_KERNEL); 7511da177e4SLinus Torvalds if (tmp->state == NULL) { 7521da177e4SLinus Torvalds printk(KERN_WARNING "%s: memory squeeze, couldn't register %s.\n", port->name, name); 7531da177e4SLinus Torvalds goto out_free_pardevice; 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds tmp->name = name; 7571da177e4SLinus Torvalds tmp->port = port; 7581da177e4SLinus Torvalds tmp->daisy = -1; 7591da177e4SLinus Torvalds tmp->preempt = pf; 7601da177e4SLinus Torvalds tmp->wakeup = kf; 7611da177e4SLinus Torvalds tmp->private = handle; 7621da177e4SLinus Torvalds tmp->flags = flags; 7631da177e4SLinus Torvalds tmp->irq_func = irq_func; 7641da177e4SLinus Torvalds tmp->waiting = 0; 7651da177e4SLinus Torvalds tmp->timeout = 5 * HZ; 7666fa45a22SSudip Mukherjee tmp->devmodel = false; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds /* Chain this onto the list */ 7691da177e4SLinus Torvalds tmp->prev = NULL; 7701da177e4SLinus Torvalds /* 7711da177e4SLinus Torvalds * This function must not run from an irq handler so we don' t need 7721da177e4SLinus Torvalds * to clear irq on the local CPU. -arca 7731da177e4SLinus Torvalds */ 7741da177e4SLinus Torvalds spin_lock(&port->physport->pardevice_lock); 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds if (flags & PARPORT_DEV_EXCL) { 7771da177e4SLinus Torvalds if (port->physport->devices) { 7781da177e4SLinus Torvalds spin_unlock (&port->physport->pardevice_lock); 7791da177e4SLinus Torvalds printk (KERN_DEBUG 780a162188fSSudip Mukherjee "%s: cannot grant exclusive access for device %s\n", 781a162188fSSudip Mukherjee port->name, name); 7821da177e4SLinus Torvalds goto out_free_all; 7831da177e4SLinus Torvalds } 7841da177e4SLinus Torvalds port->flags |= PARPORT_FLAG_EXCL; 7851da177e4SLinus Torvalds } 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds tmp->next = port->physport->devices; 7887b7a0a30SSudip Mukherjee wmb(); /* 7897b7a0a30SSudip Mukherjee * Make sure that tmp->next is written before it's 7907b7a0a30SSudip Mukherjee * added to the list; see comments marked 'no locking 7917b7a0a30SSudip Mukherjee * required' 7927b7a0a30SSudip Mukherjee */ 7931da177e4SLinus Torvalds if (port->physport->devices) 7941da177e4SLinus Torvalds port->physport->devices->prev = tmp; 7951da177e4SLinus Torvalds port->physport->devices = tmp; 7961da177e4SLinus Torvalds spin_unlock(&port->physport->pardevice_lock); 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds init_waitqueue_head(&tmp->wait_q); 7991da177e4SLinus Torvalds tmp->timeslice = parport_default_timeslice; 8001da177e4SLinus Torvalds tmp->waitnext = tmp->waitprev = NULL; 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds /* 8031da177e4SLinus Torvalds * This has to be run as last thing since init_state may need other 8041da177e4SLinus Torvalds * pardevice fields. -arca 8051da177e4SLinus Torvalds */ 8061da177e4SLinus Torvalds port->ops->init_state(tmp, tmp->state); 80705ad709dSAlan Cox if (!test_and_set_bit(PARPORT_DEVPROC_REGISTERED, &port->devflags)) { 80805ad709dSAlan Cox port->proc_device = tmp; 8091da177e4SLinus Torvalds parport_device_proc_register(tmp); 81005ad709dSAlan Cox } 8111da177e4SLinus Torvalds return tmp; 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds out_free_all: 8141da177e4SLinus Torvalds kfree(tmp->state); 8151da177e4SLinus Torvalds out_free_pardevice: 8161da177e4SLinus Torvalds kfree(tmp); 8171da177e4SLinus Torvalds out: 8181da177e4SLinus Torvalds parport_put_port (port); 8191da177e4SLinus Torvalds module_put(port->ops->owner); 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds return NULL; 8221da177e4SLinus Torvalds } 82327c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_register_device); 8241da177e4SLinus Torvalds 8256fa45a22SSudip Mukherjee static void free_pardevice(struct device *dev) 8266fa45a22SSudip Mukherjee { 8276fa45a22SSudip Mukherjee struct pardevice *par_dev = to_pardevice(dev); 8286fa45a22SSudip Mukherjee 8296fa45a22SSudip Mukherjee kfree(par_dev->name); 8306fa45a22SSudip Mukherjee kfree(par_dev); 8316fa45a22SSudip Mukherjee } 8326fa45a22SSudip Mukherjee 8336fa45a22SSudip Mukherjee struct pardevice * 8346fa45a22SSudip Mukherjee parport_register_dev_model(struct parport *port, const char *name, 8356fa45a22SSudip Mukherjee const struct pardev_cb *par_dev_cb, int id) 8366fa45a22SSudip Mukherjee { 8376fa45a22SSudip Mukherjee struct pardevice *par_dev; 8386fa45a22SSudip Mukherjee int ret; 8396fa45a22SSudip Mukherjee char *devname; 8406fa45a22SSudip Mukherjee 8416fa45a22SSudip Mukherjee if (port->physport->flags & PARPORT_FLAG_EXCL) { 8426fa45a22SSudip Mukherjee /* An exclusive device is registered. */ 8436fa45a22SSudip Mukherjee pr_err("%s: no more devices allowed\n", port->name); 8446fa45a22SSudip Mukherjee return NULL; 8456fa45a22SSudip Mukherjee } 8466fa45a22SSudip Mukherjee 8476fa45a22SSudip Mukherjee if (par_dev_cb->flags & PARPORT_DEV_LURK) { 8486fa45a22SSudip Mukherjee if (!par_dev_cb->preempt || !par_dev_cb->wakeup) { 8496fa45a22SSudip Mukherjee pr_info("%s: refused to register lurking device (%s) without callbacks\n", 8506fa45a22SSudip Mukherjee port->name, name); 8516fa45a22SSudip Mukherjee return NULL; 8526fa45a22SSudip Mukherjee } 8536fa45a22SSudip Mukherjee } 8546fa45a22SSudip Mukherjee 85550566ac8SSudip Mukherjee if (par_dev_cb->flags & PARPORT_DEV_EXCL) { 85650566ac8SSudip Mukherjee if (port->physport->devices) { 85750566ac8SSudip Mukherjee /* 85850566ac8SSudip Mukherjee * If a device is already registered and this new 85950566ac8SSudip Mukherjee * device wants exclusive access, then no need to 86050566ac8SSudip Mukherjee * continue as we can not grant exclusive access to 86150566ac8SSudip Mukherjee * this device. 86250566ac8SSudip Mukherjee */ 86350566ac8SSudip Mukherjee pr_err("%s: cannot grant exclusive access for device %s\n", 86450566ac8SSudip Mukherjee port->name, name); 86550566ac8SSudip Mukherjee return NULL; 86650566ac8SSudip Mukherjee } 86750566ac8SSudip Mukherjee } 86850566ac8SSudip Mukherjee 8696fa45a22SSudip Mukherjee if (!try_module_get(port->ops->owner)) 8706fa45a22SSudip Mukherjee return NULL; 8716fa45a22SSudip Mukherjee 8726fa45a22SSudip Mukherjee parport_get_port(port); 8736fa45a22SSudip Mukherjee 8746fa45a22SSudip Mukherjee par_dev = kzalloc(sizeof(*par_dev), GFP_KERNEL); 8756fa45a22SSudip Mukherjee if (!par_dev) 8766fa45a22SSudip Mukherjee goto err_put_port; 8776fa45a22SSudip Mukherjee 8786fa45a22SSudip Mukherjee par_dev->state = kzalloc(sizeof(*par_dev->state), GFP_KERNEL); 8796fa45a22SSudip Mukherjee if (!par_dev->state) 8806fa45a22SSudip Mukherjee goto err_put_par_dev; 8816fa45a22SSudip Mukherjee 8826fa45a22SSudip Mukherjee devname = kstrdup(name, GFP_KERNEL); 8836fa45a22SSudip Mukherjee if (!devname) 8846fa45a22SSudip Mukherjee goto err_free_par_dev; 8856fa45a22SSudip Mukherjee 8866fa45a22SSudip Mukherjee par_dev->name = devname; 8876fa45a22SSudip Mukherjee par_dev->port = port; 8886fa45a22SSudip Mukherjee par_dev->daisy = -1; 8896fa45a22SSudip Mukherjee par_dev->preempt = par_dev_cb->preempt; 8906fa45a22SSudip Mukherjee par_dev->wakeup = par_dev_cb->wakeup; 8916fa45a22SSudip Mukherjee par_dev->private = par_dev_cb->private; 8926fa45a22SSudip Mukherjee par_dev->flags = par_dev_cb->flags; 8936fa45a22SSudip Mukherjee par_dev->irq_func = par_dev_cb->irq_func; 8946fa45a22SSudip Mukherjee par_dev->waiting = 0; 8956fa45a22SSudip Mukherjee par_dev->timeout = 5 * HZ; 8966fa45a22SSudip Mukherjee 8976fa45a22SSudip Mukherjee par_dev->dev.parent = &port->bus_dev; 8986fa45a22SSudip Mukherjee par_dev->dev.bus = &parport_bus_type; 8996fa45a22SSudip Mukherjee ret = dev_set_name(&par_dev->dev, "%s.%d", devname, id); 9006fa45a22SSudip Mukherjee if (ret) 9016fa45a22SSudip Mukherjee goto err_free_devname; 9026fa45a22SSudip Mukherjee par_dev->dev.release = free_pardevice; 9036fa45a22SSudip Mukherjee par_dev->devmodel = true; 9046fa45a22SSudip Mukherjee ret = device_register(&par_dev->dev); 90568d35c7bSSudip Mukherjee if (ret) { 90668d35c7bSSudip Mukherjee put_device(&par_dev->dev); 90768d35c7bSSudip Mukherjee goto err_put_port; 90868d35c7bSSudip Mukherjee } 9096fa45a22SSudip Mukherjee 9106fa45a22SSudip Mukherjee /* Chain this onto the list */ 9116fa45a22SSudip Mukherjee par_dev->prev = NULL; 9126fa45a22SSudip Mukherjee /* 9136fa45a22SSudip Mukherjee * This function must not run from an irq handler so we don' t need 9146fa45a22SSudip Mukherjee * to clear irq on the local CPU. -arca 9156fa45a22SSudip Mukherjee */ 9166fa45a22SSudip Mukherjee spin_lock(&port->physport->pardevice_lock); 9176fa45a22SSudip Mukherjee 9186fa45a22SSudip Mukherjee if (par_dev_cb->flags & PARPORT_DEV_EXCL) { 9196fa45a22SSudip Mukherjee if (port->physport->devices) { 9206fa45a22SSudip Mukherjee spin_unlock(&port->physport->pardevice_lock); 9216fa45a22SSudip Mukherjee pr_debug("%s: cannot grant exclusive access for device %s\n", 9226fa45a22SSudip Mukherjee port->name, name); 923cabea695SSudip Mukherjee device_unregister(&par_dev->dev); 924cabea695SSudip Mukherjee goto err_put_port; 9256fa45a22SSudip Mukherjee } 9266fa45a22SSudip Mukherjee port->flags |= PARPORT_FLAG_EXCL; 9276fa45a22SSudip Mukherjee } 9286fa45a22SSudip Mukherjee 9296fa45a22SSudip Mukherjee par_dev->next = port->physport->devices; 9306fa45a22SSudip Mukherjee wmb(); /* 9316fa45a22SSudip Mukherjee * Make sure that tmp->next is written before it's 9326fa45a22SSudip Mukherjee * added to the list; see comments marked 'no locking 9336fa45a22SSudip Mukherjee * required' 9346fa45a22SSudip Mukherjee */ 9356fa45a22SSudip Mukherjee if (port->physport->devices) 9366fa45a22SSudip Mukherjee port->physport->devices->prev = par_dev; 9376fa45a22SSudip Mukherjee port->physport->devices = par_dev; 9386fa45a22SSudip Mukherjee spin_unlock(&port->physport->pardevice_lock); 9396fa45a22SSudip Mukherjee 9406fa45a22SSudip Mukherjee init_waitqueue_head(&par_dev->wait_q); 9416fa45a22SSudip Mukherjee par_dev->timeslice = parport_default_timeslice; 9426fa45a22SSudip Mukherjee par_dev->waitnext = NULL; 9436fa45a22SSudip Mukherjee par_dev->waitprev = NULL; 9446fa45a22SSudip Mukherjee 9456fa45a22SSudip Mukherjee /* 9466fa45a22SSudip Mukherjee * This has to be run as last thing since init_state may need other 9476fa45a22SSudip Mukherjee * pardevice fields. -arca 9486fa45a22SSudip Mukherjee */ 9496fa45a22SSudip Mukherjee port->ops->init_state(par_dev, par_dev->state); 9506fa45a22SSudip Mukherjee port->proc_device = par_dev; 9516fa45a22SSudip Mukherjee parport_device_proc_register(par_dev); 9526fa45a22SSudip Mukherjee 9536fa45a22SSudip Mukherjee return par_dev; 9546fa45a22SSudip Mukherjee 9556fa45a22SSudip Mukherjee err_free_devname: 9566fa45a22SSudip Mukherjee kfree(devname); 9576fa45a22SSudip Mukherjee err_free_par_dev: 9586fa45a22SSudip Mukherjee kfree(par_dev->state); 9596fa45a22SSudip Mukherjee err_put_par_dev: 9606fa45a22SSudip Mukherjee if (!par_dev->devmodel) 9616fa45a22SSudip Mukherjee kfree(par_dev); 9626fa45a22SSudip Mukherjee err_put_port: 9636fa45a22SSudip Mukherjee parport_put_port(port); 9646fa45a22SSudip Mukherjee module_put(port->ops->owner); 9656fa45a22SSudip Mukherjee 9666fa45a22SSudip Mukherjee return NULL; 9676fa45a22SSudip Mukherjee } 9686fa45a22SSudip Mukherjee EXPORT_SYMBOL(parport_register_dev_model); 9696fa45a22SSudip Mukherjee 9701da177e4SLinus Torvalds /** 9711da177e4SLinus Torvalds * parport_unregister_device - deregister a device on a parallel port 9721da177e4SLinus Torvalds * @dev: pointer to structure representing device 9731da177e4SLinus Torvalds * 9741da177e4SLinus Torvalds * This undoes the effect of parport_register_device(). 9751da177e4SLinus Torvalds **/ 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds void parport_unregister_device(struct pardevice *dev) 9781da177e4SLinus Torvalds { 9791da177e4SLinus Torvalds struct parport *port; 9801da177e4SLinus Torvalds 9811da177e4SLinus Torvalds #ifdef PARPORT_PARANOID 9821da177e4SLinus Torvalds if (dev == NULL) { 9831da177e4SLinus Torvalds printk(KERN_ERR "parport_unregister_device: passed NULL\n"); 9841da177e4SLinus Torvalds return; 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds #endif 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds port = dev->port->physport; 9891da177e4SLinus Torvalds 99005ad709dSAlan Cox if (port->proc_device == dev) { 99105ad709dSAlan Cox port->proc_device = NULL; 99205ad709dSAlan Cox clear_bit(PARPORT_DEVPROC_REGISTERED, &port->devflags); 99305ad709dSAlan Cox parport_device_proc_unregister(dev); 99405ad709dSAlan Cox } 99505ad709dSAlan Cox 9961da177e4SLinus Torvalds if (port->cad == dev) { 9971da177e4SLinus Torvalds printk(KERN_DEBUG "%s: %s forgot to release port\n", 9981da177e4SLinus Torvalds port->name, dev->name); 9991da177e4SLinus Torvalds parport_release (dev); 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds spin_lock(&port->pardevice_lock); 10031da177e4SLinus Torvalds if (dev->next) 10041da177e4SLinus Torvalds dev->next->prev = dev->prev; 10051da177e4SLinus Torvalds if (dev->prev) 10061da177e4SLinus Torvalds dev->prev->next = dev->next; 10071da177e4SLinus Torvalds else 10081da177e4SLinus Torvalds port->devices = dev->next; 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds if (dev->flags & PARPORT_DEV_EXCL) 10111da177e4SLinus Torvalds port->flags &= ~PARPORT_FLAG_EXCL; 10121da177e4SLinus Torvalds 10131da177e4SLinus Torvalds spin_unlock(&port->pardevice_lock); 10141da177e4SLinus Torvalds 10157b7a0a30SSudip Mukherjee /* 10167b7a0a30SSudip Mukherjee * Make sure we haven't left any pointers around in the wait 10177b7a0a30SSudip Mukherjee * list. 10187b7a0a30SSudip Mukherjee */ 1019cbeb4b7aSAlexander Gordeev spin_lock_irq(&port->waitlist_lock); 10201da177e4SLinus Torvalds if (dev->waitprev || dev->waitnext || port->waithead == dev) { 10211da177e4SLinus Torvalds if (dev->waitprev) 10221da177e4SLinus Torvalds dev->waitprev->waitnext = dev->waitnext; 10231da177e4SLinus Torvalds else 10241da177e4SLinus Torvalds port->waithead = dev->waitnext; 10251da177e4SLinus Torvalds if (dev->waitnext) 10261da177e4SLinus Torvalds dev->waitnext->waitprev = dev->waitprev; 10271da177e4SLinus Torvalds else 10281da177e4SLinus Torvalds port->waittail = dev->waitprev; 10291da177e4SLinus Torvalds } 1030cbeb4b7aSAlexander Gordeev spin_unlock_irq(&port->waitlist_lock); 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds kfree(dev->state); 10336fa45a22SSudip Mukherjee if (dev->devmodel) 10346fa45a22SSudip Mukherjee device_unregister(&dev->dev); 10356fa45a22SSudip Mukherjee else 10361da177e4SLinus Torvalds kfree(dev); 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds module_put(port->ops->owner); 10391da177e4SLinus Torvalds parport_put_port (port); 10401da177e4SLinus Torvalds } 104127c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_unregister_device); 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds /** 10441da177e4SLinus Torvalds * parport_find_number - find a parallel port by number 10451da177e4SLinus Torvalds * @number: parallel port number 10461da177e4SLinus Torvalds * 10471da177e4SLinus Torvalds * This returns the parallel port with the specified number, or 10481da177e4SLinus Torvalds * %NULL if there is none. 10491da177e4SLinus Torvalds * 10501da177e4SLinus Torvalds * There is an implicit parport_get_port() done already; to throw 10511da177e4SLinus Torvalds * away the reference to the port that parport_find_number() 10521da177e4SLinus Torvalds * gives you, use parport_put_port(). 10531da177e4SLinus Torvalds */ 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds struct parport *parport_find_number (int number) 10561da177e4SLinus Torvalds { 10571da177e4SLinus Torvalds struct parport *port, *result = NULL; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds if (list_empty(&portlist)) 10601da177e4SLinus Torvalds get_lowlevel_driver (); 10611da177e4SLinus Torvalds 10621da177e4SLinus Torvalds spin_lock (&parportlist_lock); 10631da177e4SLinus Torvalds list_for_each_entry(port, &portlist, list) { 10641da177e4SLinus Torvalds if (port->number == number) { 10651da177e4SLinus Torvalds result = parport_get_port (port); 10661da177e4SLinus Torvalds break; 10671da177e4SLinus Torvalds } 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds spin_unlock (&parportlist_lock); 10701da177e4SLinus Torvalds return result; 10711da177e4SLinus Torvalds } 107227c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_find_number); 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds /** 10751da177e4SLinus Torvalds * parport_find_base - find a parallel port by base address 10761da177e4SLinus Torvalds * @base: base I/O address 10771da177e4SLinus Torvalds * 10781da177e4SLinus Torvalds * This returns the parallel port with the specified base 10791da177e4SLinus Torvalds * address, or %NULL if there is none. 10801da177e4SLinus Torvalds * 10811da177e4SLinus Torvalds * There is an implicit parport_get_port() done already; to throw 10821da177e4SLinus Torvalds * away the reference to the port that parport_find_base() 10831da177e4SLinus Torvalds * gives you, use parport_put_port(). 10841da177e4SLinus Torvalds */ 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds struct parport *parport_find_base (unsigned long base) 10871da177e4SLinus Torvalds { 10881da177e4SLinus Torvalds struct parport *port, *result = NULL; 10891da177e4SLinus Torvalds 10901da177e4SLinus Torvalds if (list_empty(&portlist)) 10911da177e4SLinus Torvalds get_lowlevel_driver (); 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds spin_lock (&parportlist_lock); 10941da177e4SLinus Torvalds list_for_each_entry(port, &portlist, list) { 10951da177e4SLinus Torvalds if (port->base == base) { 10961da177e4SLinus Torvalds result = parport_get_port (port); 10971da177e4SLinus Torvalds break; 10981da177e4SLinus Torvalds } 10991da177e4SLinus Torvalds } 11001da177e4SLinus Torvalds spin_unlock (&parportlist_lock); 11011da177e4SLinus Torvalds return result; 11021da177e4SLinus Torvalds } 110327c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_find_base); 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds /** 11061da177e4SLinus Torvalds * parport_claim - claim access to a parallel port device 11071da177e4SLinus Torvalds * @dev: pointer to structure representing a device on the port 11081da177e4SLinus Torvalds * 11091da177e4SLinus Torvalds * This function will not block and so can be used from interrupt 11101da177e4SLinus Torvalds * context. If parport_claim() succeeds in claiming access to 11111da177e4SLinus Torvalds * the port it returns zero and the port is available to use. It 11121da177e4SLinus Torvalds * may fail (returning non-zero) if the port is in use by another 11131da177e4SLinus Torvalds * driver and that driver is not willing to relinquish control of 11141da177e4SLinus Torvalds * the port. 11151da177e4SLinus Torvalds **/ 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds int parport_claim(struct pardevice *dev) 11181da177e4SLinus Torvalds { 11191da177e4SLinus Torvalds struct pardevice *oldcad; 11201da177e4SLinus Torvalds struct parport *port = dev->port->physport; 11211da177e4SLinus Torvalds unsigned long flags; 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds if (port->cad == dev) { 11241da177e4SLinus Torvalds printk(KERN_INFO "%s: %s already owner\n", 11251da177e4SLinus Torvalds dev->port->name,dev->name); 11261da177e4SLinus Torvalds return 0; 11271da177e4SLinus Torvalds } 11281da177e4SLinus Torvalds 11291da177e4SLinus Torvalds /* Preempt any current device */ 11301da177e4SLinus Torvalds write_lock_irqsave (&port->cad_lock, flags); 11311da177e4SLinus Torvalds if ((oldcad = port->cad) != NULL) { 11321da177e4SLinus Torvalds if (oldcad->preempt) { 11331da177e4SLinus Torvalds if (oldcad->preempt(oldcad->private)) 11341da177e4SLinus Torvalds goto blocked; 11351da177e4SLinus Torvalds port->ops->save_state(port, dev->state); 11361da177e4SLinus Torvalds } else 11371da177e4SLinus Torvalds goto blocked; 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds if (port->cad != oldcad) { 11407b7a0a30SSudip Mukherjee /* 11417b7a0a30SSudip Mukherjee * I think we'll actually deadlock rather than 11427b7a0a30SSudip Mukherjee * get here, but just in case.. 11437b7a0a30SSudip Mukherjee */ 11441da177e4SLinus Torvalds printk(KERN_WARNING 11451da177e4SLinus Torvalds "%s: %s released port when preempted!\n", 11461da177e4SLinus Torvalds port->name, oldcad->name); 11471da177e4SLinus Torvalds if (port->cad) 11481da177e4SLinus Torvalds goto blocked; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds } 11511da177e4SLinus Torvalds 11521da177e4SLinus Torvalds /* Can't fail from now on, so mark ourselves as no longer waiting. */ 11531da177e4SLinus Torvalds if (dev->waiting & 1) { 11541da177e4SLinus Torvalds dev->waiting = 0; 11551da177e4SLinus Torvalds 11561da177e4SLinus Torvalds /* Take ourselves out of the wait list again. */ 11571da177e4SLinus Torvalds spin_lock_irq (&port->waitlist_lock); 11581da177e4SLinus Torvalds if (dev->waitprev) 11591da177e4SLinus Torvalds dev->waitprev->waitnext = dev->waitnext; 11601da177e4SLinus Torvalds else 11611da177e4SLinus Torvalds port->waithead = dev->waitnext; 11621da177e4SLinus Torvalds if (dev->waitnext) 11631da177e4SLinus Torvalds dev->waitnext->waitprev = dev->waitprev; 11641da177e4SLinus Torvalds else 11651da177e4SLinus Torvalds port->waittail = dev->waitprev; 11661da177e4SLinus Torvalds spin_unlock_irq (&port->waitlist_lock); 11671da177e4SLinus Torvalds dev->waitprev = dev->waitnext = NULL; 11681da177e4SLinus Torvalds } 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds /* Now we do the change of devices */ 11711da177e4SLinus Torvalds port->cad = dev; 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284 11741da177e4SLinus Torvalds /* If it's a mux port, select it. */ 11751da177e4SLinus Torvalds if (dev->port->muxport >= 0) { 11761da177e4SLinus Torvalds /* FIXME */ 11771da177e4SLinus Torvalds port->muxsel = dev->port->muxport; 11781da177e4SLinus Torvalds } 11791da177e4SLinus Torvalds 11801da177e4SLinus Torvalds /* If it's a daisy chain device, select it. */ 11811da177e4SLinus Torvalds if (dev->daisy >= 0) { 11821da177e4SLinus Torvalds /* This could be lazier. */ 11831da177e4SLinus Torvalds if (!parport_daisy_select (port, dev->daisy, 11841da177e4SLinus Torvalds IEEE1284_MODE_COMPAT)) 11851da177e4SLinus Torvalds port->daisy = dev->daisy; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds #endif /* IEEE1284.3 support */ 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds /* Restore control registers */ 11901da177e4SLinus Torvalds port->ops->restore_state(port, dev->state); 11911da177e4SLinus Torvalds write_unlock_irqrestore(&port->cad_lock, flags); 11921da177e4SLinus Torvalds dev->time = jiffies; 11931da177e4SLinus Torvalds return 0; 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds blocked: 11967b7a0a30SSudip Mukherjee /* 11977b7a0a30SSudip Mukherjee * If this is the first time we tried to claim the port, register an 11987b7a0a30SSudip Mukherjee * interest. This is only allowed for devices sleeping in 11997b7a0a30SSudip Mukherjee * parport_claim_or_block(), or those with a wakeup function. 12007b7a0a30SSudip Mukherjee */ 12011da177e4SLinus Torvalds 12021da177e4SLinus Torvalds /* The cad_lock is still held for writing here */ 12031da177e4SLinus Torvalds if (dev->waiting & 2 || dev->wakeup) { 12041da177e4SLinus Torvalds spin_lock (&port->waitlist_lock); 12051da177e4SLinus Torvalds if (test_and_set_bit(0, &dev->waiting) == 0) { 12061da177e4SLinus Torvalds /* First add ourselves to the end of the wait list. */ 12071da177e4SLinus Torvalds dev->waitnext = NULL; 12081da177e4SLinus Torvalds dev->waitprev = port->waittail; 12091da177e4SLinus Torvalds if (port->waittail) { 12101da177e4SLinus Torvalds port->waittail->waitnext = dev; 12111da177e4SLinus Torvalds port->waittail = dev; 12121da177e4SLinus Torvalds } else 12131da177e4SLinus Torvalds port->waithead = port->waittail = dev; 12141da177e4SLinus Torvalds } 12151da177e4SLinus Torvalds spin_unlock (&port->waitlist_lock); 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds write_unlock_irqrestore (&port->cad_lock, flags); 12181da177e4SLinus Torvalds return -EAGAIN; 12191da177e4SLinus Torvalds } 122027c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_claim); 12211da177e4SLinus Torvalds 12221da177e4SLinus Torvalds /** 12231da177e4SLinus Torvalds * parport_claim_or_block - claim access to a parallel port device 12241da177e4SLinus Torvalds * @dev: pointer to structure representing a device on the port 12251da177e4SLinus Torvalds * 12261da177e4SLinus Torvalds * This behaves like parport_claim(), but will block if necessary 12271da177e4SLinus Torvalds * to wait for the port to be free. A return value of 1 12281da177e4SLinus Torvalds * indicates that it slept; 0 means that it succeeded without 12291da177e4SLinus Torvalds * needing to sleep. A negative error code indicates failure. 12301da177e4SLinus Torvalds **/ 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds int parport_claim_or_block(struct pardevice *dev) 12331da177e4SLinus Torvalds { 12341da177e4SLinus Torvalds int r; 12351da177e4SLinus Torvalds 12367b7a0a30SSudip Mukherjee /* 12377b7a0a30SSudip Mukherjee * Signal to parport_claim() that we can wait even without a 12387b7a0a30SSudip Mukherjee * wakeup function. 12397b7a0a30SSudip Mukherjee */ 12401da177e4SLinus Torvalds dev->waiting = 2; 12411da177e4SLinus Torvalds 12421da177e4SLinus Torvalds /* Try to claim the port. If this fails, we need to sleep. */ 12431da177e4SLinus Torvalds r = parport_claim(dev); 12441da177e4SLinus Torvalds if (r == -EAGAIN) { 12451da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING 12461da177e4SLinus Torvalds printk(KERN_DEBUG "%s: parport_claim() returned -EAGAIN\n", dev->name); 12471da177e4SLinus Torvalds #endif 12481da177e4SLinus Torvalds /* 12491da177e4SLinus Torvalds * FIXME!!! Use the proper locking for dev->waiting, 12501da177e4SLinus Torvalds * and make this use the "wait_event_interruptible()" 12511da177e4SLinus Torvalds * interfaces. The cli/sti that used to be here 12521da177e4SLinus Torvalds * did nothing. 12531da177e4SLinus Torvalds * 12541da177e4SLinus Torvalds * See also parport_release() 12551da177e4SLinus Torvalds */ 12561da177e4SLinus Torvalds 12577b7a0a30SSudip Mukherjee /* 12587b7a0a30SSudip Mukherjee * If dev->waiting is clear now, an interrupt 12597b7a0a30SSudip Mukherjee * gave us the port and we would deadlock if we slept. 12607b7a0a30SSudip Mukherjee */ 12611da177e4SLinus Torvalds if (dev->waiting) { 126276ae0536SArnd Bergmann wait_event_interruptible(dev->wait_q, 126376ae0536SArnd Bergmann !dev->waiting); 12641da177e4SLinus Torvalds if (signal_pending (current)) { 12651da177e4SLinus Torvalds return -EINTR; 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds r = 1; 12681da177e4SLinus Torvalds } else { 12691da177e4SLinus Torvalds r = 0; 12701da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING 12711da177e4SLinus Torvalds printk(KERN_DEBUG "%s: didn't sleep in parport_claim_or_block()\n", 12721da177e4SLinus Torvalds dev->name); 12731da177e4SLinus Torvalds #endif 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds #ifdef PARPORT_DEBUG_SHARING 12771da177e4SLinus Torvalds if (dev->port->physport->cad != dev) 1278a162188fSSudip Mukherjee printk(KERN_DEBUG "%s: exiting parport_claim_or_block but %s owns port!\n", 1279a162188fSSudip Mukherjee dev->name, dev->port->physport->cad ? 12801da177e4SLinus Torvalds dev->port->physport->cad->name:"nobody"); 12811da177e4SLinus Torvalds #endif 12821da177e4SLinus Torvalds } 12831da177e4SLinus Torvalds dev->waiting = 0; 12841da177e4SLinus Torvalds return r; 12851da177e4SLinus Torvalds } 128627c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_claim_or_block); 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds /** 12891da177e4SLinus Torvalds * parport_release - give up access to a parallel port device 12901da177e4SLinus Torvalds * @dev: pointer to structure representing parallel port device 12911da177e4SLinus Torvalds * 12921da177e4SLinus Torvalds * This function cannot fail, but it should not be called without 12931da177e4SLinus Torvalds * the port claimed. Similarly, if the port is already claimed 12941da177e4SLinus Torvalds * you should not try claiming it again. 12951da177e4SLinus Torvalds **/ 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds void parport_release(struct pardevice *dev) 12981da177e4SLinus Torvalds { 12991da177e4SLinus Torvalds struct parport *port = dev->port->physport; 13001da177e4SLinus Torvalds struct pardevice *pd; 13011da177e4SLinus Torvalds unsigned long flags; 13021da177e4SLinus Torvalds 13031da177e4SLinus Torvalds /* Make sure that dev is the current device */ 13041da177e4SLinus Torvalds write_lock_irqsave(&port->cad_lock, flags); 13051da177e4SLinus Torvalds if (port->cad != dev) { 13061da177e4SLinus Torvalds write_unlock_irqrestore (&port->cad_lock, flags); 1307a162188fSSudip Mukherjee printk(KERN_WARNING "%s: %s tried to release parport when not owner\n", 1308a162188fSSudip Mukherjee port->name, dev->name); 13091da177e4SLinus Torvalds return; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds #ifdef CONFIG_PARPORT_1284 13131da177e4SLinus Torvalds /* If this is on a mux port, deselect it. */ 13141da177e4SLinus Torvalds if (dev->port->muxport >= 0) { 13151da177e4SLinus Torvalds /* FIXME */ 13161da177e4SLinus Torvalds port->muxsel = -1; 13171da177e4SLinus Torvalds } 13181da177e4SLinus Torvalds 13191da177e4SLinus Torvalds /* If this is a daisy device, deselect it. */ 13201da177e4SLinus Torvalds if (dev->daisy >= 0) { 13211da177e4SLinus Torvalds parport_daisy_deselect_all (port); 13221da177e4SLinus Torvalds port->daisy = -1; 13231da177e4SLinus Torvalds } 13241da177e4SLinus Torvalds #endif 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds port->cad = NULL; 13271da177e4SLinus Torvalds write_unlock_irqrestore(&port->cad_lock, flags); 13281da177e4SLinus Torvalds 13291da177e4SLinus Torvalds /* Save control registers */ 13301da177e4SLinus Torvalds port->ops->save_state(port, dev->state); 13311da177e4SLinus Torvalds 13327b7a0a30SSudip Mukherjee /* 13337b7a0a30SSudip Mukherjee * If anybody is waiting, find out who's been there longest and 13347b7a0a30SSudip Mukherjee * then wake them up. (Note: no locking required) 13357b7a0a30SSudip Mukherjee */ 13361da177e4SLinus Torvalds /* !!! LOCKING IS NEEDED HERE */ 13371da177e4SLinus Torvalds for (pd = port->waithead; pd; pd = pd->waitnext) { 13381da177e4SLinus Torvalds if (pd->waiting & 2) { /* sleeping in claim_or_block */ 13391da177e4SLinus Torvalds parport_claim(pd); 13401da177e4SLinus Torvalds if (waitqueue_active(&pd->wait_q)) 13411da177e4SLinus Torvalds wake_up_interruptible(&pd->wait_q); 13421da177e4SLinus Torvalds return; 13431da177e4SLinus Torvalds } else if (pd->wakeup) { 13441da177e4SLinus Torvalds pd->wakeup(pd->private); 13451da177e4SLinus Torvalds if (dev->port->cad) /* racy but no matter */ 13461da177e4SLinus Torvalds return; 13471da177e4SLinus Torvalds } else { 13481da177e4SLinus Torvalds printk(KERN_ERR "%s: don't know how to wake %s\n", port->name, pd->name); 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 13527b7a0a30SSudip Mukherjee /* 13537b7a0a30SSudip Mukherjee * Nobody was waiting, so walk the list to see if anyone is 13547b7a0a30SSudip Mukherjee * interested in being woken up. (Note: no locking required) 13557b7a0a30SSudip Mukherjee */ 13561da177e4SLinus Torvalds /* !!! LOCKING IS NEEDED HERE */ 13571da177e4SLinus Torvalds for (pd = port->devices; (port->cad == NULL) && pd; pd = pd->next) { 13581da177e4SLinus Torvalds if (pd->wakeup && pd != dev) 13591da177e4SLinus Torvalds pd->wakeup(pd->private); 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds } 136227c6db26SSudip Mukherjee EXPORT_SYMBOL(parport_release); 13631da177e4SLinus Torvalds 13643f2e40dfSJeff Garzik irqreturn_t parport_irq_handler(int irq, void *dev_id) 13653f2e40dfSJeff Garzik { 13663f2e40dfSJeff Garzik struct parport *port = dev_id; 13673f2e40dfSJeff Garzik 1368f230d101SJeff Garzik parport_generic_irq(port); 13693f2e40dfSJeff Garzik 13703f2e40dfSJeff Garzik return IRQ_HANDLED; 13713f2e40dfSJeff Garzik } 13723f2e40dfSJeff Garzik EXPORT_SYMBOL(parport_irq_handler); 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1375