pnp.h 14.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Linux Plug and Play Support
 * Copyright by Adam Belay <ambx1@neo.rr.com>
 */

#ifndef _LINUX_PNP_H
#define _LINUX_PNP_H

#ifdef __KERNEL__

#include <linux/device.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/mod_devicetable.h>

#define PNP_NAME_LEN		50

struct pnp_protocol;
struct pnp_dev;
20
struct pnp_resource_table;
L
Linus Torvalds 已提交
21 22 23 24

/*
 * Resource Management
 */
25
struct resource *pnp_get_resource(struct pnp_dev *, unsigned int, unsigned int);
L
Linus Torvalds 已提交
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static inline int pnp_resource_valid(struct resource *res)
{
	if (res && !(res->flags & IORESOURCE_UNSET))
		return 1;
	return 0;
}

static inline resource_size_t pnp_resource_len(struct resource *res)
{
	if (res->start == 0 && res->end == 0)
		return 0;
	return res->end - res->start + 1;
}


static inline resource_size_t pnp_port_start(struct pnp_dev *dev,
					     unsigned int bar)
{
	return pnp_get_resource(dev, IORESOURCE_IO, bar)->start;
}

static inline resource_size_t pnp_port_end(struct pnp_dev *dev,
					   unsigned int bar)
{
	return pnp_get_resource(dev, IORESOURCE_IO, bar)->end;
}

static inline unsigned long pnp_port_flags(struct pnp_dev *dev,
					   unsigned int bar)
{
	return pnp_get_resource(dev, IORESOURCE_IO, bar)->flags;
}

static inline int pnp_port_valid(struct pnp_dev *dev, unsigned int bar)
{
	return pnp_resource_valid(pnp_get_resource(dev, IORESOURCE_IO, bar));
}

static inline resource_size_t pnp_port_len(struct pnp_dev *dev,
					   unsigned int bar)
{
	return pnp_resource_len(pnp_get_resource(dev, IORESOURCE_IO, bar));
}


static inline resource_size_t pnp_mem_start(struct pnp_dev *dev,
					    unsigned int bar)
{
	return pnp_get_resource(dev, IORESOURCE_MEM, bar)->start;
}

static inline resource_size_t pnp_mem_end(struct pnp_dev *dev,
					  unsigned int bar)
{
	return pnp_get_resource(dev, IORESOURCE_MEM, bar)->end;
}

static inline unsigned long pnp_mem_flags(struct pnp_dev *dev, unsigned int bar)
{
	return pnp_get_resource(dev, IORESOURCE_MEM, bar)->flags;
}

static inline int pnp_mem_valid(struct pnp_dev *dev, unsigned int bar)
{
	return pnp_resource_valid(pnp_get_resource(dev, IORESOURCE_MEM, bar));
}

static inline resource_size_t pnp_mem_len(struct pnp_dev *dev,
					  unsigned int bar)
{
	return pnp_resource_len(pnp_get_resource(dev, IORESOURCE_MEM, bar));
}


static inline resource_size_t pnp_irq(struct pnp_dev *dev, unsigned int bar)
{
	return pnp_get_resource(dev, IORESOURCE_IRQ, bar)->start;
}

static inline unsigned long pnp_irq_flags(struct pnp_dev *dev, unsigned int bar)
{
	return pnp_get_resource(dev, IORESOURCE_IRQ, bar)->flags;
}

static inline int pnp_irq_valid(struct pnp_dev *dev, unsigned int bar)
{
	return pnp_resource_valid(pnp_get_resource(dev, IORESOURCE_IRQ, bar));
}


static inline resource_size_t pnp_dma(struct pnp_dev *dev, unsigned int bar)
{
	return pnp_get_resource(dev, IORESOURCE_DMA, bar)->start;
}

static inline unsigned long pnp_dma_flags(struct pnp_dev *dev, unsigned int bar)
{
	return pnp_get_resource(dev, IORESOURCE_DMA, bar)->flags;
}

static inline int pnp_dma_valid(struct pnp_dev *dev, unsigned int bar)
{
	return pnp_resource_valid(pnp_get_resource(dev, IORESOURCE_DMA, bar));
}

L
Linus Torvalds 已提交
132 133 134 135 136

#define PNP_PORT_FLAG_16BITADDR	(1<<0)
#define PNP_PORT_FLAG_FIXED	(1<<1)

struct pnp_port {
B
Bjorn Helgaas 已提交
137 138 139 140 141 142 143
	unsigned short min;	/* min base number */
	unsigned short max;	/* max base number */
	unsigned char align;	/* align boundary */
	unsigned char size;	/* size of range */
	unsigned char flags;	/* port flags */
	unsigned char pad;	/* pad */
	struct pnp_port *next;	/* next port */
L
Linus Torvalds 已提交
144 145 146 147
};

#define PNP_IRQ_NR 256
struct pnp_irq {
B
Bjorn Helgaas 已提交
148
	DECLARE_BITMAP(map, PNP_IRQ_NR);	/* bitmask for IRQ lines */
B
Bjorn Helgaas 已提交
149 150 151
	unsigned char flags;	/* IRQ flags */
	unsigned char pad;	/* pad */
	struct pnp_irq *next;	/* next IRQ */
L
Linus Torvalds 已提交
152 153 154
};

struct pnp_dma {
B
Bjorn Helgaas 已提交
155 156 157
	unsigned char map;	/* bitmask for DMA channels */
	unsigned char flags;	/* DMA flags */
	struct pnp_dma *next;	/* next port */
L
Linus Torvalds 已提交
158 159 160
};

struct pnp_mem {
B
Bjorn Helgaas 已提交
161 162 163 164 165 166 167
	unsigned int min;	/* min base number */
	unsigned int max;	/* max base number */
	unsigned int align;	/* align boundary */
	unsigned int size;	/* size of range */
	unsigned char flags;	/* memory flags */
	unsigned char pad;	/* pad */
	struct pnp_mem *next;	/* next memory resource */
L
Linus Torvalds 已提交
168 169 170 171 172 173 174 175 176
};

#define PNP_RES_PRIORITY_PREFERRED	0
#define PNP_RES_PRIORITY_ACCEPTABLE	1
#define PNP_RES_PRIORITY_FUNCTIONAL	2
#define PNP_RES_PRIORITY_INVALID	65535

struct pnp_option {
	unsigned short priority;	/* priority */
B
Bjorn Helgaas 已提交
177 178 179 180
	struct pnp_port *port;		/* first port */
	struct pnp_irq *irq;		/* first IRQ */
	struct pnp_dma *dma;		/* first DMA */
	struct pnp_mem *mem;		/* first memory resource */
L
Linus Torvalds 已提交
181 182 183 184
	struct pnp_option *next;	/* used to chain dependent resources */
};

/*
J
Joe Perches 已提交
185
 * Device Management
L
Linus Torvalds 已提交
186 187 188
 */

struct pnp_card {
B
Bjorn Helgaas 已提交
189 190
	struct device dev;		/* Driver Model device interface */
	unsigned char number;		/* used as an index, must be unique */
L
Linus Torvalds 已提交
191 192 193 194
	struct list_head global_list;	/* node in global list of cards */
	struct list_head protocol_list;	/* node in protocol's list of cards */
	struct list_head devices;	/* devices attached to the card */

B
Bjorn Helgaas 已提交
195
	struct pnp_protocol *protocol;
B
Bjorn Helgaas 已提交
196
	struct pnp_id *id;		/* contains supported EISA IDs */
L
Linus Torvalds 已提交
197 198

	char name[PNP_NAME_LEN];	/* contains a human-readable name */
B
Bjorn Helgaas 已提交
199
	unsigned char pnpver;		/* Plug & Play version */
B
Bjorn Helgaas 已提交
200
	unsigned char productver;	/* product version */
B
Bjorn Helgaas 已提交
201 202
	unsigned int serial;		/* serial number */
	unsigned char checksum;		/* if zero - checksum passed */
L
Linus Torvalds 已提交
203 204 205 206 207 208 209 210 211 212 213 214
	struct proc_dir_entry *procdir;	/* directory entry in /proc/bus/isapnp */
};

#define global_to_pnp_card(n) list_entry(n, struct pnp_card, global_list)
#define protocol_to_pnp_card(n) list_entry(n, struct pnp_card, protocol_list)
#define to_pnp_card(n) container_of(n, struct pnp_card, dev)
#define pnp_for_each_card(card) \
	for((card) = global_to_pnp_card(pnp_cards.next); \
	(card) != global_to_pnp_card(&pnp_cards); \
	(card) = global_to_pnp_card((card)->global_list.next))

struct pnp_card_link {
B
Bjorn Helgaas 已提交
215 216 217
	struct pnp_card *card;
	struct pnp_card_driver *driver;
	void *driver_data;
218
	pm_message_t pm_state;
L
Linus Torvalds 已提交
219 220
};

B
Bjorn Helgaas 已提交
221
static inline void *pnp_get_card_drvdata(struct pnp_card_link *pcard)
L
Linus Torvalds 已提交
222 223 224 225
{
	return pcard->driver_data;
}

B
Bjorn Helgaas 已提交
226
static inline void pnp_set_card_drvdata(struct pnp_card_link *pcard, void *data)
L
Linus Torvalds 已提交
227 228 229 230 231
{
	pcard->driver_data = data;
}

struct pnp_dev {
B
Bjorn Helgaas 已提交
232
	struct device dev;		/* Driver Model device interface */
D
David Brownell 已提交
233
	u64 dma_mask;
234
	unsigned int number;		/* used as an index, must be unique */
L
Linus Torvalds 已提交
235 236 237 238 239 240 241
	int status;

	struct list_head global_list;	/* node in global list of devices */
	struct list_head protocol_list;	/* node in list of device's protocol */
	struct list_head card_list;	/* node in card's list of devices */
	struct list_head rdev_list;	/* node in cards list of requested devices */

B
Bjorn Helgaas 已提交
242 243 244 245
	struct pnp_protocol *protocol;
	struct pnp_card *card;	/* card the device is attached to, none if NULL */
	struct pnp_driver *driver;
	struct pnp_card_link *card_link;
L
Linus Torvalds 已提交
246

B
Bjorn Helgaas 已提交
247
	struct pnp_id *id;		/* supported EISA IDs */
L
Linus Torvalds 已提交
248 249 250

	int active;
	int capabilities;
B
Bjorn Helgaas 已提交
251 252
	struct pnp_option *independent;
	struct pnp_option *dependent;
253
	struct pnp_resource_table *res;
L
Linus Torvalds 已提交
254 255

	char name[PNP_NAME_LEN];	/* contains a human-readable name */
B
Bjorn Helgaas 已提交
256
	int flags;			/* used by protocols */
L
Linus Torvalds 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	struct proc_dir_entry *procent;	/* device entry in /proc/bus/isapnp */
	void *data;
};

#define global_to_pnp_dev(n) list_entry(n, struct pnp_dev, global_list)
#define card_to_pnp_dev(n) list_entry(n, struct pnp_dev, card_list)
#define protocol_to_pnp_dev(n) list_entry(n, struct pnp_dev, protocol_list)
#define	to_pnp_dev(n) container_of(n, struct pnp_dev, dev)
#define pnp_for_each_dev(dev) \
	for((dev) = global_to_pnp_dev(pnp_global.next); \
	(dev) != global_to_pnp_dev(&pnp_global); \
	(dev) = global_to_pnp_dev((dev)->global_list.next))
#define card_for_each_dev(card,dev) \
	for((dev) = card_to_pnp_dev((card)->devices.next); \
	(dev) != card_to_pnp_dev(&(card)->devices); \
	(dev) = card_to_pnp_dev((dev)->card_list.next))
#define pnp_dev_name(dev) (dev)->name

B
Bjorn Helgaas 已提交
275
static inline void *pnp_get_drvdata(struct pnp_dev *pdev)
L
Linus Torvalds 已提交
276 277 278 279
{
	return dev_get_drvdata(&pdev->dev);
}

B
Bjorn Helgaas 已提交
280
static inline void pnp_set_drvdata(struct pnp_dev *pdev, void *data)
L
Linus Torvalds 已提交
281 282 283 284 285 286
{
	dev_set_drvdata(&pdev->dev, data);
}

struct pnp_fixup {
	char id[7];
B
Bjorn Helgaas 已提交
287
	void (*quirk_function) (struct pnp_dev * dev);	/* fixup function */
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300
};

/* config parameters */
#define PNP_CONFIG_NORMAL	0x0001
#define PNP_CONFIG_FORCE	0x0002	/* disables validity checking */

/* capabilities */
#define PNP_READ		0x0001
#define PNP_WRITE		0x0002
#define PNP_DISABLE		0x0004
#define PNP_CONFIGURABLE	0x0008
#define PNP_REMOVABLE		0x0010

B
Bjorn Helgaas 已提交
301
#define pnp_can_read(dev)	(((dev)->protocol->get) && \
L
Linus Torvalds 已提交
302
				 ((dev)->capabilities & PNP_READ))
B
Bjorn Helgaas 已提交
303
#define pnp_can_write(dev)	(((dev)->protocol->set) && \
L
Linus Torvalds 已提交
304
				 ((dev)->capabilities & PNP_WRITE))
B
Bjorn Helgaas 已提交
305
#define pnp_can_disable(dev)	(((dev)->protocol->disable) && \
L
Linus Torvalds 已提交
306 307 308 309 310 311 312 313 314 315
				 ((dev)->capabilities & PNP_DISABLE))
#define pnp_can_configure(dev)	((!(dev)->active) && \
				 ((dev)->capabilities & PNP_CONFIGURABLE))

#ifdef CONFIG_ISAPNP
extern struct pnp_protocol isapnp_protocol;
#define pnp_device_is_isapnp(dev) ((dev)->protocol == (&isapnp_protocol))
#else
#define pnp_device_is_isapnp(dev) 0
#endif
D
Daniel Walker 已提交
316
extern struct mutex pnp_res_mutex;
L
Linus Torvalds 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

#ifdef CONFIG_PNPBIOS
extern struct pnp_protocol pnpbios_protocol;
#define pnp_device_is_pnpbios(dev) ((dev)->protocol == (&pnpbios_protocol))
#else
#define pnp_device_is_pnpbios(dev) 0
#endif

/* status */
#define PNP_READY		0x0000
#define PNP_ATTACHED		0x0001
#define PNP_BUSY		0x0002
#define PNP_FAULTY		0x0004

/* isapnp specific macros */

#define isapnp_card_number(dev)	((dev)->card ? (dev)->card->number : -1)
#define isapnp_csn_number(dev)  ((dev)->number)

/*
 * Driver Management
 */

struct pnp_id {
	char id[PNP_ID_LEN];
B
Bjorn Helgaas 已提交
342
	struct pnp_id *next;
L
Linus Torvalds 已提交
343 344 345
};

struct pnp_driver {
B
Bjorn Helgaas 已提交
346
	char *name;
L
Linus Torvalds 已提交
347 348
	const struct pnp_device_id *id_table;
	unsigned int flags;
B
Bjorn Helgaas 已提交
349 350 351 352
	int (*probe) (struct pnp_dev *dev, const struct pnp_device_id *dev_id);
	void (*remove) (struct pnp_dev *dev);
	int (*suspend) (struct pnp_dev *dev, pm_message_t state);
	int (*resume) (struct pnp_dev *dev);
L
Linus Torvalds 已提交
353 354 355 356 357 358 359
	struct device_driver driver;
};

#define	to_pnp_driver(drv) container_of(drv, struct pnp_driver, driver)

struct pnp_card_driver {
	struct list_head global_list;
B
Bjorn Helgaas 已提交
360
	char *name;
L
Linus Torvalds 已提交
361 362
	const struct pnp_card_device_id *id_table;
	unsigned int flags;
B
Bjorn Helgaas 已提交
363 364 365 366 367
	int (*probe) (struct pnp_card_link *card,
		      const struct pnp_card_device_id *card_id);
	void (*remove) (struct pnp_card_link *card);
	int (*suspend) (struct pnp_card_link *card, pm_message_t state);
	int (*resume) (struct pnp_card_link *card);
L
Linus Torvalds 已提交
368 369 370 371 372 373 374 375 376 377 378 379 380 381
	struct pnp_driver link;
};

#define	to_pnp_card_driver(drv) container_of(drv, struct pnp_card_driver, link)

/* pnp driver flags */
#define PNP_DRIVER_RES_DO_NOT_CHANGE	0x0001	/* do not change the state of the device */
#define PNP_DRIVER_RES_DISABLE		0x0003	/* ensure the device is disabled */

/*
 * Protocol Management
 */

struct pnp_protocol {
B
Bjorn Helgaas 已提交
382 383
	struct list_head protocol_list;
	char *name;
L
Linus Torvalds 已提交
384 385

	/* resource control functions */
386 387
	int (*get) (struct pnp_dev *dev);
	int (*set) (struct pnp_dev *dev);
B
Bjorn Helgaas 已提交
388
	int (*disable) (struct pnp_dev *dev);
L
Linus Torvalds 已提交
389

390
	/* protocol specific suspend/resume */
B
Bjorn Helgaas 已提交
391 392
	int (*suspend) (struct pnp_dev * dev, pm_message_t state);
	int (*resume) (struct pnp_dev * dev);
393

L
Linus Torvalds 已提交
394
	/* used by pnp layer only (look but don't touch) */
B
Bjorn Helgaas 已提交
395 396 397 398
	unsigned char number;	/* protocol number */
	struct device dev;	/* link to driver model */
	struct list_head cards;
	struct list_head devices;
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408 409 410
};

#define to_pnp_protocol(n) list_entry(n, struct pnp_protocol, protocol_list)
#define protocol_for_each_card(protocol,card) \
	for((card) = protocol_to_pnp_card((protocol)->cards.next); \
	(card) != protocol_to_pnp_card(&(protocol)->cards); \
	(card) = protocol_to_pnp_card((card)->protocol_list.next))
#define protocol_for_each_dev(protocol,dev) \
	for((dev) = protocol_to_pnp_dev((protocol)->devices.next); \
	(dev) != protocol_to_pnp_dev(&(protocol)->devices); \
	(dev) = protocol_to_pnp_dev((dev)->protocol_list.next))

411 412
extern struct bus_type pnp_bus_type;

L
Linus Torvalds 已提交
413 414 415 416 417 418
#if defined(CONFIG_PNP)

/* device management */
int pnp_device_attach(struct pnp_dev *pnp_dev);
void pnp_device_detach(struct pnp_dev *pnp_dev);
extern struct list_head pnp_global;
419
extern int pnp_platform_devices;
L
Linus Torvalds 已提交
420 421

/* multidevice card support */
B
Bjorn Helgaas 已提交
422 423 424 425 426
struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink,
					const char *id, struct pnp_dev *from);
void pnp_release_card_device(struct pnp_dev *dev);
int pnp_register_card_driver(struct pnp_card_driver *drv);
void pnp_unregister_card_driver(struct pnp_card_driver *drv);
L
Linus Torvalds 已提交
427 428 429 430
extern struct list_head pnp_cards;

/* resource management */
int pnp_auto_config_dev(struct pnp_dev *dev);
431 432
int pnp_start_dev(struct pnp_dev *dev);
int pnp_stop_dev(struct pnp_dev *dev);
L
Linus Torvalds 已提交
433 434 435 436
int pnp_activate_dev(struct pnp_dev *dev);
int pnp_disable_dev(struct pnp_dev *dev);

/* protocol helpers */
B
Bjorn Helgaas 已提交
437 438
int pnp_is_active(struct pnp_dev *dev);
int compare_pnp_id(struct pnp_id *pos, const char *id);
L
Linus Torvalds 已提交
439 440 441 442 443 444
int pnp_register_driver(struct pnp_driver *drv);
void pnp_unregister_driver(struct pnp_driver *drv);

#else

/* device management */
B
Bjorn Helgaas 已提交
445 446
static inline int pnp_device_attach(struct pnp_dev *pnp_dev) { return -ENODEV; }
static inline void pnp_device_detach(struct pnp_dev *pnp_dev) { }
B
Bjorn Helgaas 已提交
447

448
#define pnp_platform_devices 0
L
Linus Torvalds 已提交
449 450

/* multidevice card support */
B
Bjorn Helgaas 已提交
451 452 453 454
static inline struct pnp_dev *pnp_request_card_device(struct pnp_card_link *clink, const char *id, struct pnp_dev *from) { return NULL; }
static inline void pnp_release_card_device(struct pnp_dev *dev) { }
static inline int pnp_register_card_driver(struct pnp_card_driver *drv) { return -ENODEV; }
static inline void pnp_unregister_card_driver(struct pnp_card_driver *drv) { }
L
Linus Torvalds 已提交
455 456

/* resource management */
B
Bjorn Helgaas 已提交
457 458 459 460 461
static inline int pnp_auto_config_dev(struct pnp_dev *dev) { return -ENODEV; }
static inline int pnp_start_dev(struct pnp_dev *dev) { return -ENODEV; }
static inline int pnp_stop_dev(struct pnp_dev *dev) { return -ENODEV; }
static inline int pnp_activate_dev(struct pnp_dev *dev) { return -ENODEV; }
static inline int pnp_disable_dev(struct pnp_dev *dev) { return -ENODEV; }
L
Linus Torvalds 已提交
462 463

/* protocol helpers */
B
Bjorn Helgaas 已提交
464 465 466 467
static inline int pnp_is_active(struct pnp_dev *dev) { return 0; }
static inline int compare_pnp_id(struct pnp_id *pos, const char *id) { return -ENODEV; }
static inline int pnp_register_driver(struct pnp_driver *drv) { return -ENODEV; }
static inline void pnp_unregister_driver(struct pnp_driver *drv) { }
L
Linus Torvalds 已提交
468 469 470 471 472 473 474

#endif /* CONFIG_PNP */

#define pnp_err(format, arg...) printk(KERN_ERR "pnp: " format "\n" , ## arg)
#define pnp_info(format, arg...) printk(KERN_INFO "pnp: " format "\n" , ## arg)
#define pnp_warn(format, arg...) printk(KERN_WARNING "pnp: " format "\n" , ## arg)

475
#ifdef CONFIG_PNP_DEBUG
L
Linus Torvalds 已提交
476 477 478 479 480 481 482 483
#define pnp_dbg(format, arg...) printk(KERN_DEBUG "pnp: " format "\n" , ## arg)
#else
#define pnp_dbg(format, arg...) do {} while (0)
#endif

#endif /* __KERNEL__ */

#endif /* _LINUX_PNP_H */