linux/include/linux/parport.h
<<
>>
Prefs
   1/*
   2 * Any part of this program may be used in documents licensed under
   3 * the GNU Free Documentation License, Version 1.1 or any later version
   4 * published by the Free Software Foundation.
   5 */
   6#ifndef _PARPORT_H_
   7#define _PARPORT_H_
   8
   9
  10#include <linux/jiffies.h>
  11#include <linux/proc_fs.h>
  12#include <linux/spinlock.h>
  13#include <linux/wait.h>
  14#include <linux/irqreturn.h>
  15#include <linux/semaphore.h>
  16#include <linux/device.h>
  17#include <asm/ptrace.h>
  18#include <uapi/linux/parport.h>
  19
  20/* Define this later. */
  21struct parport;
  22struct pardevice;
  23
  24struct pc_parport_state {
  25        unsigned int ctr;
  26        unsigned int ecr;
  27};
  28
  29struct ax_parport_state {
  30        unsigned int ctr;
  31        unsigned int ecr;
  32        unsigned int dcsr;
  33};
  34
  35/* used by both parport_amiga and parport_mfc3 */
  36struct amiga_parport_state {
  37       unsigned char data;     /* ciaa.prb */
  38       unsigned char datadir;  /* ciaa.ddrb */
  39       unsigned char status;   /* ciab.pra & 7 */
  40       unsigned char statusdir;/* ciab.ddrb & 7 */
  41};
  42
  43struct ax88796_parport_state {
  44        unsigned char cpr;
  45};
  46
  47struct ip32_parport_state {
  48        unsigned int dcr;
  49        unsigned int ecr;
  50};
  51
  52struct parport_state {
  53        union {
  54                struct pc_parport_state pc;
  55                /* ARC has no state. */
  56                struct ax_parport_state ax;
  57                struct amiga_parport_state amiga;
  58                struct ax88796_parport_state ax88796;
  59                /* Atari has not state. */
  60                struct ip32_parport_state ip32;
  61                void *misc; 
  62        } u;
  63};
  64
  65struct parport_operations {
  66        /* IBM PC-style virtual registers. */
  67        void (*write_data)(struct parport *, unsigned char);
  68        unsigned char (*read_data)(struct parport *);
  69
  70        void (*write_control)(struct parport *, unsigned char);
  71        unsigned char (*read_control)(struct parport *);
  72        unsigned char (*frob_control)(struct parport *, unsigned char mask,
  73                                      unsigned char val);
  74
  75        unsigned char (*read_status)(struct parport *);
  76
  77        /* IRQs. */
  78        void (*enable_irq)(struct parport *);
  79        void (*disable_irq)(struct parport *);
  80
  81        /* Data direction. */
  82        void (*data_forward) (struct parport *);
  83        void (*data_reverse) (struct parport *);
  84
  85        /* For core parport code. */
  86        void (*init_state)(struct pardevice *, struct parport_state *);
  87        void (*save_state)(struct parport *, struct parport_state *);
  88        void (*restore_state)(struct parport *, struct parport_state *);
  89
  90        /* Block read/write */
  91        size_t (*epp_write_data) (struct parport *port, const void *buf,
  92                                  size_t len, int flags);
  93        size_t (*epp_read_data) (struct parport *port, void *buf, size_t len,
  94                                 int flags);
  95        size_t (*epp_write_addr) (struct parport *port, const void *buf,
  96                                  size_t len, int flags);
  97        size_t (*epp_read_addr) (struct parport *port, void *buf, size_t len,
  98                                 int flags);
  99
 100        size_t (*ecp_write_data) (struct parport *port, const void *buf,
 101                                  size_t len, int flags);
 102        size_t (*ecp_read_data) (struct parport *port, void *buf, size_t len,
 103                                 int flags);
 104        size_t (*ecp_write_addr) (struct parport *port, const void *buf,
 105                                  size_t len, int flags);
 106
 107        size_t (*compat_write_data) (struct parport *port, const void *buf,
 108                                     size_t len, int flags);
 109        size_t (*nibble_read_data) (struct parport *port, void *buf,
 110                                    size_t len, int flags);
 111        size_t (*byte_read_data) (struct parport *port, void *buf,
 112                                  size_t len, int flags);
 113        struct module *owner;
 114};
 115
 116struct parport_device_info {
 117        parport_device_class class;
 118        const char *class_name;
 119        const char *mfr;
 120        const char *model;
 121        const char *cmdset;
 122        const char *description;
 123};
 124
 125/* Each device can have two callback functions:
 126 *  1) a preemption function, called by the resource manager to request
 127 *     that the driver relinquish control of the port.  The driver should
 128 *     return zero if it agrees to release the port, and nonzero if it 
 129 *     refuses.  Do not call parport_release() - the kernel will do this
 130 *     implicitly.
 131 *
 132 *  2) a wake-up function, called by the resource manager to tell drivers
 133 *     that the port is available to be claimed.  If a driver wants to use
 134 *     the port, it should call parport_claim() here.
 135 */
 136
 137/* A parallel port device */
 138struct pardevice {
 139        const char *name;
 140        struct parport *port;
 141        int daisy;
 142        int (*preempt)(void *);
 143        void (*wakeup)(void *);
 144        void *private;
 145        void (*irq_func)(void *);
 146        unsigned int flags;
 147        struct pardevice *next;
 148        struct pardevice *prev;
 149        struct device dev;
 150        bool devmodel;
 151        struct parport_state *state;     /* saved status over preemption */
 152        wait_queue_head_t wait_q;
 153        unsigned long int time;
 154        unsigned long int timeslice;
 155        volatile long int timeout;
 156        unsigned long waiting;           /* long req'd for set_bit --RR */
 157        struct pardevice *waitprev;
 158        struct pardevice *waitnext;
 159        void * sysctl_table;
 160};
 161
 162#define to_pardevice(n) container_of(n, struct pardevice, dev)
 163
 164/* IEEE1284 information */
 165
 166/* IEEE1284 phases. These are exposed to userland through ppdev IOCTL
 167 * PP[GS]ETPHASE, so do not change existing values. */
 168enum ieee1284_phase {
 169        IEEE1284_PH_FWD_DATA,
 170        IEEE1284_PH_FWD_IDLE,
 171        IEEE1284_PH_TERMINATE,
 172        IEEE1284_PH_NEGOTIATION,
 173        IEEE1284_PH_HBUSY_DNA,
 174        IEEE1284_PH_REV_IDLE,
 175        IEEE1284_PH_HBUSY_DAVAIL,
 176        IEEE1284_PH_REV_DATA,
 177        IEEE1284_PH_ECP_SETUP,
 178        IEEE1284_PH_ECP_FWD_TO_REV,
 179        IEEE1284_PH_ECP_REV_TO_FWD,
 180        IEEE1284_PH_ECP_DIR_UNKNOWN,
 181};
 182struct ieee1284_info {
 183        int mode;
 184        volatile enum ieee1284_phase phase;
 185        struct semaphore irq;
 186};
 187
 188/* A parallel port */
 189struct parport {
 190        unsigned long base;     /* base address */
 191        unsigned long base_hi;  /* base address (hi - ECR) */
 192        unsigned int size;      /* IO extent */
 193        const char *name;
 194        unsigned int modes;
 195        int irq;                /* interrupt (or -1 for none) */
 196        int dma;
 197        int muxport;            /* which muxport (if any) this is */
 198        int portnum;            /* which physical parallel port (not mux) */
 199        struct device *dev;     /* Physical device associated with IO/DMA.
 200                                 * This may unfortulately be null if the
 201                                 * port has a legacy driver.
 202                                 */
 203        struct device bus_dev;  /* to link with the bus */
 204        struct parport *physport;
 205                                /* If this is a non-default mux
 206                                   parport, i.e. we're a clone of a real
 207                                   physical port, this is a pointer to that
 208                                   port. The locking is only done in the
 209                                   real port.  For a clone port, the
 210                                   following structure members are
 211                                   meaningless: devices, cad, muxsel,
 212                                   waithead, waittail, flags, pdir,
 213                                   dev, ieee1284, *_lock.
 214
 215                                   It this is a default mux parport, or
 216                                   there is no mux involved, this points to
 217                                   ourself. */
 218
 219        struct pardevice *devices;
 220        struct pardevice *cad;  /* port owner */
 221        int daisy;              /* currently selected daisy addr */
 222        int muxsel;             /* currently selected mux port */
 223
 224        struct pardevice *waithead;
 225        struct pardevice *waittail;
 226
 227        struct list_head list;
 228        struct timer_list timer;
 229        unsigned int flags;
 230
 231        void *sysctl_table;
 232        struct parport_device_info probe_info[5]; /* 0-3 + non-IEEE1284.3 */
 233        struct ieee1284_info ieee1284;
 234
 235        struct parport_operations *ops;
 236        void *private_data;     /* for lowlevel driver */
 237
 238        int number;             /* port index - the `n' in `parportn' */
 239        spinlock_t pardevice_lock;
 240        spinlock_t waitlist_lock;
 241        rwlock_t cad_lock;
 242
 243        int spintime;
 244        atomic_t ref_count;
 245
 246        unsigned long devflags;
 247#define PARPORT_DEVPROC_REGISTERED      0
 248        struct pardevice *proc_device;  /* Currently register proc device */
 249
 250        struct list_head full_list;
 251        struct parport *slaves[3];
 252};
 253
 254#define to_parport_dev(n) container_of(n, struct parport, bus_dev)
 255
 256#define DEFAULT_SPIN_TIME 500 /* us */
 257
 258struct parport_driver {
 259        const char *name;
 260        void (*attach) (struct parport *);
 261        void (*detach) (struct parport *);
 262        void (*match_port)(struct parport *);
 263        int (*probe)(struct pardevice *);
 264        struct device_driver driver;
 265        bool devmodel;
 266        struct list_head list;
 267};
 268
 269#define to_parport_driver(n) container_of(n, struct parport_driver, driver)
 270
 271int parport_bus_init(void);
 272void parport_bus_exit(void);
 273
 274/* parport_register_port registers a new parallel port at the given
 275   address (if one does not already exist) and returns a pointer to it.
 276   This entails claiming the I/O region, IRQ and DMA.  NULL is returned
 277   if initialisation fails. */
 278struct parport *parport_register_port(unsigned long base, int irq, int dma,
 279                                      struct parport_operations *ops);
 280
 281/* Once a registered port is ready for high-level drivers to use, the
 282   low-level driver that registered it should announce it.  This will
 283   call the high-level drivers' attach() functions (after things like
 284   determining the IEEE 1284.3 topology of the port and collecting
 285   DeviceIDs). */
 286void parport_announce_port (struct parport *port);
 287
 288/* Unregister a port. */
 289extern void parport_remove_port(struct parport *port);
 290
 291/* Register a new high-level driver. */
 292
 293int __must_check __parport_register_driver(struct parport_driver *,
 294                                           struct module *,
 295                                           const char *mod_name);
 296/*
 297 * parport_register_driver must be a macro so that KBUILD_MODNAME can
 298 * be expanded
 299 */
 300#define parport_register_driver(driver)             \
 301        __parport_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)
 302
 303/* Unregister a high-level driver. */
 304extern void parport_unregister_driver (struct parport_driver *);
 305void parport_unregister_driver(struct parport_driver *);
 306
 307/* If parport_register_driver doesn't fit your needs, perhaps
 308 * parport_find_xxx does. */
 309extern struct parport *parport_find_number (int);
 310extern struct parport *parport_find_base (unsigned long);
 311
 312/* generic irq handler, if it suits your needs */
 313extern irqreturn_t parport_irq_handler(int irq, void *dev_id);
 314
 315/* Reference counting for ports. */
 316extern struct parport *parport_get_port (struct parport *);
 317extern void parport_put_port (struct parport *);
 318void parport_del_port(struct parport *);
 319
 320struct pardev_cb {
 321        int (*preempt)(void *);
 322        void (*wakeup)(void *);
 323        void *private;
 324        void (*irq_func)(void *);
 325        unsigned int flags;
 326};
 327
 328/*
 329 * parport_register_dev_model declares that a device is connected to a
 330 * port, and tells the kernel all it needs to know.
 331 */
 332struct pardevice *
 333parport_register_dev_model(struct parport *port, const char *name,
 334                           const struct pardev_cb *par_dev_cb, int cnt);
 335
 336/* parport_unregister unlinks a device from the chain. */
 337extern void parport_unregister_device(struct pardevice *dev);
 338
 339/* parport_claim tries to gain ownership of the port for a particular
 340   driver.  This may fail (return non-zero) if another driver is busy.
 341   If this driver has registered an interrupt handler, it will be
 342   enabled.  */
 343extern int parport_claim(struct pardevice *dev);
 344
 345/* parport_claim_or_block is the same, but sleeps if the port cannot
 346   be claimed.  Return value is 1 if it slept, 0 normally and -errno
 347   on error.  */
 348extern int parport_claim_or_block(struct pardevice *dev);
 349
 350/* parport_release reverses a previous parport_claim.  This can never
 351   fail, though the effects are undefined (except that they are bad)
 352   if you didn't previously own the port.  Once you have released the
 353   port you should make sure that neither your code nor the hardware
 354   on the port tries to initiate any communication without first
 355   re-claiming the port.  If you mess with the port state (enabling
 356   ECP for example) you should clean up before releasing the port. */
 357
 358extern void parport_release(struct pardevice *dev);
 359
 360/**
 361 * parport_yield - relinquish a parallel port temporarily
 362 * @dev: a device on the parallel port
 363 *
 364 * This function relinquishes the port if it would be helpful to other
 365 * drivers to do so.  Afterwards it tries to reclaim the port using
 366 * parport_claim(), and the return value is the same as for
 367 * parport_claim().  If it fails, the port is left unclaimed and it is
 368 * the driver's responsibility to reclaim the port.
 369 *
 370 * The parport_yield() and parport_yield_blocking() functions are for
 371 * marking points in the driver at which other drivers may claim the
 372 * port and use their devices.  Yielding the port is similar to
 373 * releasing it and reclaiming it, but is more efficient because no
 374 * action is taken if there are no other devices needing the port.  In
 375 * fact, nothing is done even if there are other devices waiting but
 376 * the current device is still within its "timeslice".  The default
 377 * timeslice is half a second, but it can be adjusted via the /proc
 378 * interface.
 379 **/
 380static __inline__ int parport_yield(struct pardevice *dev)
 381{
 382        unsigned long int timeslip = (jiffies - dev->time);
 383        if ((dev->port->waithead == NULL) || (timeslip < dev->timeslice))
 384                return 0;
 385        parport_release(dev);
 386        return parport_claim(dev);
 387}
 388
 389/**
 390 * parport_yield_blocking - relinquish a parallel port temporarily
 391 * @dev: a device on the parallel port
 392 *
 393 * This function relinquishes the port if it would be helpful to other
 394 * drivers to do so.  Afterwards it tries to reclaim the port using
 395 * parport_claim_or_block(), and the return value is the same as for
 396 * parport_claim_or_block().
 397 **/
 398static __inline__ int parport_yield_blocking(struct pardevice *dev)
 399{
 400        unsigned long int timeslip = (jiffies - dev->time);
 401        if ((dev->port->waithead == NULL) || (timeslip < dev->timeslice))
 402                return 0;
 403        parport_release(dev);
 404        return parport_claim_or_block(dev);
 405}
 406
 407/* Flags used to identify what a device does. */
 408#define PARPORT_DEV_TRAN                0       /* WARNING !! DEPRECATED !! */
 409#define PARPORT_DEV_LURK                (1<<0)  /* WARNING !! DEPRECATED !! */
 410#define PARPORT_DEV_EXCL                (1<<1)  /* Need exclusive access. */
 411
 412#define PARPORT_FLAG_EXCL               (1<<1)  /* EXCL driver registered. */
 413
 414/* IEEE1284 functions */
 415extern void parport_ieee1284_interrupt (void *);
 416extern int parport_negotiate (struct parport *, int mode);
 417extern ssize_t parport_write (struct parport *, const void *buf, size_t len);
 418extern ssize_t parport_read (struct parport *, void *buf, size_t len);
 419
 420#define PARPORT_INACTIVITY_O_NONBLOCK 1
 421extern long parport_set_timeout (struct pardevice *, long inactivity);
 422
 423extern int parport_wait_event (struct parport *, long timeout);
 424extern int parport_wait_peripheral (struct parport *port,
 425                                    unsigned char mask,
 426                                    unsigned char val);
 427extern int parport_poll_peripheral (struct parport *port,
 428                                    unsigned char mask,
 429                                    unsigned char val,
 430                                    int usec);
 431
 432/* For architectural drivers */
 433extern size_t parport_ieee1284_write_compat (struct parport *,
 434                                             const void *, size_t, int);
 435extern size_t parport_ieee1284_read_nibble (struct parport *,
 436                                            void *, size_t, int);
 437extern size_t parport_ieee1284_read_byte (struct parport *,
 438                                          void *, size_t, int);
 439extern size_t parport_ieee1284_ecp_read_data (struct parport *,
 440                                              void *, size_t, int);
 441extern size_t parport_ieee1284_ecp_write_data (struct parport *,
 442                                               const void *, size_t, int);
 443extern size_t parport_ieee1284_ecp_write_addr (struct parport *,
 444                                               const void *, size_t, int);
 445extern size_t parport_ieee1284_epp_write_data (struct parport *,
 446                                               const void *, size_t, int);
 447extern size_t parport_ieee1284_epp_read_data (struct parport *,
 448                                              void *, size_t, int);
 449extern size_t parport_ieee1284_epp_write_addr (struct parport *,
 450                                               const void *, size_t, int);
 451extern size_t parport_ieee1284_epp_read_addr (struct parport *,
 452                                              void *, size_t, int);
 453
 454/* IEEE1284.3 functions */
 455#define daisy_dev_name "Device ID probe"
 456extern int parport_daisy_init (struct parport *port);
 457extern void parport_daisy_fini (struct parport *port);
 458extern struct pardevice *parport_open (int devnum, const char *name);
 459extern void parport_close (struct pardevice *dev);
 460extern ssize_t parport_device_id (int devnum, char *buffer, size_t len);
 461extern void parport_daisy_deselect_all (struct parport *port);
 462extern int parport_daisy_select (struct parport *port, int daisy, int mode);
 463
 464/* Lowlevel drivers _can_ call this support function to handle irqs.  */
 465static inline void parport_generic_irq(struct parport *port)
 466{
 467        parport_ieee1284_interrupt (port);
 468        read_lock(&port->cad_lock);
 469        if (port->cad && port->cad->irq_func)
 470                port->cad->irq_func(port->cad->private);
 471        read_unlock(&port->cad_lock);
 472}
 473
 474/* Prototypes from parport_procfs */
 475extern int parport_proc_register(struct parport *pp);
 476extern int parport_proc_unregister(struct parport *pp);
 477extern int parport_device_proc_register(struct pardevice *device);
 478extern int parport_device_proc_unregister(struct pardevice *device);
 479
 480/* If PC hardware is the only type supported, we can optimise a bit.  */
 481#if !defined(CONFIG_PARPORT_NOT_PC)
 482
 483#include <linux/parport_pc.h>
 484#define parport_write_data(p,x)            parport_pc_write_data(p,x)
 485#define parport_read_data(p)               parport_pc_read_data(p)
 486#define parport_write_control(p,x)         parport_pc_write_control(p,x)
 487#define parport_read_control(p)            parport_pc_read_control(p)
 488#define parport_frob_control(p,m,v)        parport_pc_frob_control(p,m,v)
 489#define parport_read_status(p)             parport_pc_read_status(p)
 490#define parport_enable_irq(p)              parport_pc_enable_irq(p)
 491#define parport_disable_irq(p)             parport_pc_disable_irq(p)
 492#define parport_data_forward(p)            parport_pc_data_forward(p)
 493#define parport_data_reverse(p)            parport_pc_data_reverse(p)
 494
 495#else  /*  !CONFIG_PARPORT_NOT_PC  */
 496
 497/* Generic operations vector through the dispatch table. */
 498#define parport_write_data(p,x)            (p)->ops->write_data(p,x)
 499#define parport_read_data(p)               (p)->ops->read_data(p)
 500#define parport_write_control(p,x)         (p)->ops->write_control(p,x)
 501#define parport_read_control(p)            (p)->ops->read_control(p)
 502#define parport_frob_control(p,m,v)        (p)->ops->frob_control(p,m,v)
 503#define parport_read_status(p)             (p)->ops->read_status(p)
 504#define parport_enable_irq(p)              (p)->ops->enable_irq(p)
 505#define parport_disable_irq(p)             (p)->ops->disable_irq(p)
 506#define parport_data_forward(p)            (p)->ops->data_forward(p)
 507#define parport_data_reverse(p)            (p)->ops->data_reverse(p)
 508
 509#endif /*  !CONFIG_PARPORT_NOT_PC  */
 510
 511extern unsigned long parport_default_timeslice;
 512extern int parport_default_spintime;
 513
 514#endif /* _PARPORT_H_ */
 515