pnp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. /*
  2. * Linux Plug and Play Support
  3. * Copyright by Adam Belay <ambx1@neo.rr.com>
  4. *
  5. */
  6. #ifndef _LINUX_PNP_H
  7. #define _LINUX_PNP_H
  8. #ifdef __KERNEL__
  9. #include <linux/device.h>
  10. #include <linux/list.h>
  11. #include <linux/errno.h>
  12. #include <linux/mod_devicetable.h>
  13. #define PNP_MAX_PORT 8
  14. #define PNP_MAX_MEM 4
  15. #define PNP_MAX_IRQ 2
  16. #define PNP_MAX_DMA 2
  17. #define PNP_NAME_LEN 50
  18. struct pnp_protocol;
  19. struct pnp_dev;
  20. /*
  21. * Resource Management
  22. */
  23. /* Use these instead of directly reading pnp_dev to get resource information */
  24. #define pnp_port_start(dev,bar) ((dev)->res.port_resource[(bar)].start)
  25. #define pnp_port_end(dev,bar) ((dev)->res.port_resource[(bar)].end)
  26. #define pnp_port_flags(dev,bar) ((dev)->res.port_resource[(bar)].flags)
  27. #define pnp_port_valid(dev,bar) \
  28. ((pnp_port_flags((dev),(bar)) & (IORESOURCE_IO | IORESOURCE_UNSET)) \
  29. == IORESOURCE_IO)
  30. #define pnp_port_len(dev,bar) \
  31. ((pnp_port_start((dev),(bar)) == 0 && \
  32. pnp_port_end((dev),(bar)) == \
  33. pnp_port_start((dev),(bar))) ? 0 : \
  34. \
  35. (pnp_port_end((dev),(bar)) - \
  36. pnp_port_start((dev),(bar)) + 1))
  37. #define pnp_mem_start(dev,bar) ((dev)->res.mem_resource[(bar)].start)
  38. #define pnp_mem_end(dev,bar) ((dev)->res.mem_resource[(bar)].end)
  39. #define pnp_mem_flags(dev,bar) ((dev)->res.mem_resource[(bar)].flags)
  40. #define pnp_mem_valid(dev,bar) \
  41. ((pnp_mem_flags((dev),(bar)) & (IORESOURCE_MEM | IORESOURCE_UNSET)) \
  42. == IORESOURCE_MEM)
  43. #define pnp_mem_len(dev,bar) \
  44. ((pnp_mem_start((dev),(bar)) == 0 && \
  45. pnp_mem_end((dev),(bar)) == \
  46. pnp_mem_start((dev),(bar))) ? 0 : \
  47. \
  48. (pnp_mem_end((dev),(bar)) - \
  49. pnp_mem_start((dev),(bar)) + 1))
  50. #define pnp_irq(dev,bar) ((dev)->res.irq_resource[(bar)].start)
  51. #define pnp_irq_flags(dev,bar) ((dev)->res.irq_resource[(bar)].flags)
  52. #define pnp_irq_valid(dev,bar) \
  53. ((pnp_irq_flags((dev),(bar)) & (IORESOURCE_IRQ | IORESOURCE_UNSET)) \
  54. == IORESOURCE_IRQ)
  55. #define pnp_dma(dev,bar) ((dev)->res.dma_resource[(bar)].start)
  56. #define pnp_dma_flags(dev,bar) ((dev)->res.dma_resource[(bar)].flags)
  57. #define pnp_dma_valid(dev,bar) \
  58. ((pnp_dma_flags((dev),(bar)) & (IORESOURCE_DMA | IORESOURCE_UNSET)) \
  59. == IORESOURCE_DMA)
  60. #define PNP_PORT_FLAG_16BITADDR (1<<0)
  61. #define PNP_PORT_FLAG_FIXED (1<<1)
  62. struct pnp_port {
  63. unsigned short min; /* min base number */
  64. unsigned short max; /* max base number */
  65. unsigned char align; /* align boundary */
  66. unsigned char size; /* size of range */
  67. unsigned char flags; /* port flags */
  68. unsigned char pad; /* pad */
  69. struct pnp_port *next; /* next port */
  70. };
  71. #define PNP_IRQ_NR 256
  72. struct pnp_irq {
  73. DECLARE_BITMAP(map, PNP_IRQ_NR); /* bitmaks for IRQ lines */
  74. unsigned char flags; /* IRQ flags */
  75. unsigned char pad; /* pad */
  76. struct pnp_irq *next; /* next IRQ */
  77. };
  78. struct pnp_dma {
  79. unsigned char map; /* bitmask for DMA channels */
  80. unsigned char flags; /* DMA flags */
  81. struct pnp_dma *next; /* next port */
  82. };
  83. struct pnp_mem {
  84. unsigned int min; /* min base number */
  85. unsigned int max; /* max base number */
  86. unsigned int align; /* align boundary */
  87. unsigned int size; /* size of range */
  88. unsigned char flags; /* memory flags */
  89. unsigned char pad; /* pad */
  90. struct pnp_mem *next; /* next memory resource */
  91. };
  92. #define PNP_RES_PRIORITY_PREFERRED 0
  93. #define PNP_RES_PRIORITY_ACCEPTABLE 1
  94. #define PNP_RES_PRIORITY_FUNCTIONAL 2
  95. #define PNP_RES_PRIORITY_INVALID 65535
  96. struct pnp_option {
  97. unsigned short priority; /* priority */
  98. struct pnp_port *port; /* first port */
  99. struct pnp_irq *irq; /* first IRQ */
  100. struct pnp_dma *dma; /* first DMA */
  101. struct pnp_mem *mem; /* first memory resource */
  102. struct pnp_option *next; /* used to chain dependent resources */
  103. };
  104. struct pnp_resource_table {
  105. struct resource port_resource[PNP_MAX_PORT];
  106. struct resource mem_resource[PNP_MAX_MEM];
  107. struct resource dma_resource[PNP_MAX_DMA];
  108. struct resource irq_resource[PNP_MAX_IRQ];
  109. };
  110. /*
  111. * Device Managemnt
  112. */
  113. struct pnp_card {
  114. struct device dev; /* Driver Model device interface */
  115. unsigned char number; /* used as an index, must be unique */
  116. struct list_head global_list; /* node in global list of cards */
  117. struct list_head protocol_list; /* node in protocol's list of cards */
  118. struct list_head devices; /* devices attached to the card */
  119. struct pnp_protocol * protocol;
  120. struct pnp_id * id; /* contains supported EISA IDs*/
  121. char name[PNP_NAME_LEN]; /* contains a human-readable name */
  122. unsigned char pnpver; /* Plug & Play version */
  123. unsigned char productver; /* product version */
  124. unsigned int serial; /* serial number */
  125. unsigned char checksum; /* if zero - checksum passed */
  126. struct proc_dir_entry *procdir; /* directory entry in /proc/bus/isapnp */
  127. };
  128. #define global_to_pnp_card(n) list_entry(n, struct pnp_card, global_list)
  129. #define protocol_to_pnp_card(n) list_entry(n, struct pnp_card, protocol_list)
  130. #define to_pnp_card(n) container_of(n, struct pnp_card, dev)
  131. #define pnp_for_each_card(card) \
  132. for((card) = global_to_pnp_card(pnp_cards.next); \
  133. (card) != global_to_pnp_card(&pnp_cards); \
  134. (card) = global_to_pnp_card((card)->global_list.next))
  135. struct pnp_card_link {
  136. struct pnp_card * card;
  137. struct pnp_card_driver * driver;
  138. void * driver_data;
  139. pm_message_t pm_state;
  140. };
  141. static inline void *pnp_get_card_drvdata (struct pnp_card_link *pcard)
  142. {
  143. return pcard->driver_data;
  144. }
  145. static inline void pnp_set_card_drvdata (struct pnp_card_link *pcard, void *data)
  146. {
  147. pcard->driver_data = data;
  148. }
  149. struct pnp_dev {
  150. struct device dev; /* Driver Model device interface */
  151. unsigned char number; /* used as an index, must be unique */
  152. int status;
  153. struct list_head global_list; /* node in global list of devices */
  154. struct list_head protocol_list; /* node in list of device's protocol */
  155. struct list_head card_list; /* node in card's list of devices */
  156. struct list_head rdev_list; /* node in cards list of requested devices */
  157. struct pnp_protocol * protocol;
  158. struct pnp_card * card; /* card the device is attached to, none if NULL */
  159. struct pnp_driver * driver;
  160. struct pnp_card_link * card_link;
  161. struct pnp_id * id; /* supported EISA IDs*/
  162. int active;
  163. int capabilities;
  164. struct pnp_option * independent;
  165. struct pnp_option * dependent;
  166. struct pnp_resource_table res;
  167. char name[PNP_NAME_LEN]; /* contains a human-readable name */
  168. unsigned short regs; /* ISAPnP: supported registers */
  169. int flags; /* used by protocols */
  170. struct proc_dir_entry *procent; /* device entry in /proc/bus/isapnp */
  171. void *data;
  172. };
  173. #define global_to_pnp_dev(n) list_entry(n, struct pnp_dev, global_list)
  174. #define card_to_pnp_dev(n) list_entry(n, struct pnp_dev, card_list)
  175. #define protocol_to_pnp_dev(n) list_entry(n, struct pnp_dev, protocol_list)
  176. #define to_pnp_dev(n) container_of(n, struct pnp_dev, dev)
  177. #define pnp_for_each_dev(dev) \
  178. for((dev) = global_to_pnp_dev(pnp_global.next); \
  179. (dev) != global_to_pnp_dev(&pnp_global); \
  180. (dev) = global_to_pnp_dev((dev)->global_list.next))
  181. #define card_for_each_dev(card,dev) \
  182. for((dev) = card_to_pnp_dev((card)->devices.next); \
  183. (dev) != card_to_pnp_dev(&(card)->devices); \
  184. (dev) = card_to_pnp_dev((dev)->card_list.next))
  185. #define pnp_dev_name(dev) (dev)->name
  186. static inline void *pnp_get_drvdata (struct pnp_dev *pdev)
  187. {
  188. return dev_get_drvdata(&pdev->dev);
  189. }
  190. static inline void pnp_set_drvdata (struct pnp_dev *pdev, void *data)
  191. {
  192. dev_set_drvdata(&pdev->dev, data);
  193. }
  194. struct pnp_fixup {
  195. char id[7];
  196. void (*quirk_function)(struct pnp_dev *dev); /* fixup function */
  197. };
  198. /* config parameters */
  199. #define PNP_CONFIG_NORMAL 0x0001
  200. #define PNP_CONFIG_FORCE 0x0002 /* disables validity checking */
  201. /* capabilities */
  202. #define PNP_READ 0x0001
  203. #define PNP_WRITE 0x0002
  204. #define PNP_DISABLE 0x0004
  205. #define PNP_CONFIGURABLE 0x0008
  206. #define PNP_REMOVABLE 0x0010
  207. #define pnp_can_read(dev) (((dev)->protocol) && ((dev)->protocol->get) && \
  208. ((dev)->capabilities & PNP_READ))
  209. #define pnp_can_write(dev) (((dev)->protocol) && ((dev)->protocol->set) && \
  210. ((dev)->capabilities & PNP_WRITE))
  211. #define pnp_can_disable(dev) (((dev)->protocol) && ((dev)->protocol->disable) && \
  212. ((dev)->capabilities & PNP_DISABLE))
  213. #define pnp_can_configure(dev) ((!(dev)->active) && \
  214. ((dev)->capabilities & PNP_CONFIGURABLE))
  215. #ifdef CONFIG_ISAPNP
  216. extern struct pnp_protocol isapnp_protocol;
  217. #define pnp_device_is_isapnp(dev) ((dev)->protocol == (&isapnp_protocol))
  218. #else
  219. #define pnp_device_is_isapnp(dev) 0
  220. #endif
  221. #ifdef CONFIG_PNPBIOS
  222. extern struct pnp_protocol pnpbios_protocol;
  223. #define pnp_device_is_pnpbios(dev) ((dev)->protocol == (&pnpbios_protocol))
  224. #else
  225. #define pnp_device_is_pnpbios(dev) 0
  226. #endif
  227. /* status */
  228. #define PNP_READY 0x0000
  229. #define PNP_ATTACHED 0x0001
  230. #define PNP_BUSY 0x0002
  231. #define PNP_FAULTY 0x0004
  232. /* isapnp specific macros */
  233. #define isapnp_card_number(dev) ((dev)->card ? (dev)->card->number : -1)
  234. #define isapnp_csn_number(dev) ((dev)->number)
  235. /*
  236. * Driver Management
  237. */
  238. struct pnp_id {
  239. char id[PNP_ID_LEN];
  240. struct pnp_id * next;
  241. };
  242. struct pnp_driver {
  243. char * name;
  244. const struct pnp_device_id *id_table;
  245. unsigned int flags;
  246. int (*probe) (struct pnp_dev *dev, const struct pnp_device_id *dev_id);
  247. void (*remove) (struct pnp_dev *dev);
  248. int (*suspend) (struct pnp_dev *dev, pm_message_t state);
  249. int (*resume) (struct pnp_dev *dev);
  250. struct device_driver driver;
  251. };
  252. #define to_pnp_driver(drv) container_of(drv, struct pnp_driver, driver)
  253. struct pnp_card_driver {
  254. struct list_head global_list;
  255. char * name;
  256. const struct pnp_card_device_id *id_table;
  257. unsigned int flags;
  258. int (*probe) (struct pnp_card_link *card, const struct pnp_card_device_id *card_id);
  259. void (*remove) (struct pnp_card_link *card);
  260. int (*suspend) (struct pnp_card_link *card, pm_message_t state);
  261. int (*resume) (struct pnp_card_link *card);
  262. struct pnp_driver link;
  263. };
  264. #define to_pnp_card_driver(drv) container_of(drv, struct pnp_card_driver, link)
  265. /* pnp driver flags */
  266. #define PNP_DRIVER_RES_DO_NOT_CHANGE 0x0001 /* do not change the state of the device */
  267. #define PNP_DRIVER_RES_DISABLE 0x0003 /* ensure the device is disabled */
  268. /*
  269. * Protocol Management
  270. */
  271. struct pnp_protocol {
  272. struct list_head protocol_list;
  273. char * name;
  274. /* resource control functions */
  275. int (*get)(struct pnp_dev *dev, struct pnp_resource_table *res);
  276. int (*set)(struct pnp_dev *dev, struct pnp_resource_table *res);
  277. int (*disable)(struct pnp_dev *dev);
  278. /* used by pnp layer only (look but don't touch) */
  279. unsigned char number; /* protocol number*/
  280. struct device dev; /* link to driver model */
  281. struct list_head cards;
  282. struct list_head devices;
  283. };
  284. #define to_pnp_protocol(n) list_entry(n, struct pnp_protocol, protocol_list)
  285. #define protocol_for_each_card(protocol,card) \
  286. for((card) = protocol_to_pnp_card((protocol)->cards.next); \
  287. (card) != protocol_to_pnp_card(&(protocol)->cards); \
  288. (card) = protocol_to_pnp_card((card)->protocol_list.next))
  289. #define protocol_for_each_dev(protocol,dev) \
  290. for((dev) = protocol_to_pnp_dev((protocol)->devices.next); \
  291. (dev) != protocol_to_pnp_dev(&(protocol)->devices); \
  292. (dev) = protocol_to_pnp_dev((dev)->protocol_list.next))
  293. extern struct bus_type pnp_bus_type;
  294. #if defined(CONFIG_PNP)
  295. /* device management */
  296. int pnp_register_protocol(struct pnp_protocol *protocol);
  297. void pnp_unregister_protocol(struct pnp_protocol *protocol);
  298. int pnp_add_device(struct pnp_dev *dev);
  299. int pnp_device_attach(struct pnp_dev *pnp_dev);
  300. void pnp_device_detach(struct pnp_dev *pnp_dev);
  301. extern struct list_head pnp_global;
  302. /* multidevice card support */
  303. int pnp_add_card(struct pnp_card *card);
  304. void pnp_remove_card(struct pnp_card *card);
  305. int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev);
  306. void pnp_remove_card_device(struct pnp_dev *dev);
  307. int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card);
  308. struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from);
  309. void pnp_release_card_device(struct pnp_dev * dev);
  310. int pnp_register_card_driver(struct pnp_card_driver * drv);
  311. void pnp_unregister_card_driver(struct pnp_card_driver * drv);
  312. extern struct list_head pnp_cards;
  313. /* resource management */
  314. struct pnp_option * pnp_register_independent_option(struct pnp_dev *dev);
  315. struct pnp_option * pnp_register_dependent_option(struct pnp_dev *dev, int priority);
  316. int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data);
  317. int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data);
  318. int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data);
  319. int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data);
  320. void pnp_init_resource_table(struct pnp_resource_table *table);
  321. int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res, int mode);
  322. int pnp_auto_config_dev(struct pnp_dev *dev);
  323. int pnp_validate_config(struct pnp_dev *dev);
  324. int pnp_start_dev(struct pnp_dev *dev);
  325. int pnp_stop_dev(struct pnp_dev *dev);
  326. int pnp_activate_dev(struct pnp_dev *dev);
  327. int pnp_disable_dev(struct pnp_dev *dev);
  328. void pnp_resource_change(struct resource *resource, resource_size_t start,
  329. resource_size_t size);
  330. /* protocol helpers */
  331. int pnp_is_active(struct pnp_dev * dev);
  332. int compare_pnp_id(struct pnp_id * pos, const char * id);
  333. int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev);
  334. int pnp_register_driver(struct pnp_driver *drv);
  335. void pnp_unregister_driver(struct pnp_driver *drv);
  336. #else
  337. /* device management */
  338. static inline int pnp_register_protocol(struct pnp_protocol *protocol) { return -ENODEV; }
  339. static inline void pnp_unregister_protocol(struct pnp_protocol *protocol) { }
  340. static inline int pnp_init_device(struct pnp_dev *dev) { return -ENODEV; }
  341. static inline int pnp_add_device(struct pnp_dev *dev) { return -ENODEV; }
  342. static inline int pnp_device_attach(struct pnp_dev *pnp_dev) { return -ENODEV; }
  343. static inline void pnp_device_detach(struct pnp_dev *pnp_dev) { ; }
  344. /* multidevice card support */
  345. static inline int pnp_add_card(struct pnp_card *card) { return -ENODEV; }
  346. static inline void pnp_remove_card(struct pnp_card *card) { ; }
  347. static inline int pnp_add_card_device(struct pnp_card *card, struct pnp_dev *dev) { return -ENODEV; }
  348. static inline void pnp_remove_card_device(struct pnp_dev *dev) { ; }
  349. static inline int pnp_add_card_id(struct pnp_id *id, struct pnp_card *card) { return -ENODEV; }
  350. static inline struct pnp_dev * pnp_request_card_device(struct pnp_card_link *clink, const char * id, struct pnp_dev * from) { return NULL; }
  351. static inline void pnp_release_card_device(struct pnp_dev * dev) { ; }
  352. static inline int pnp_register_card_driver(struct pnp_card_driver * drv) { return -ENODEV; }
  353. static inline void pnp_unregister_card_driver(struct pnp_card_driver * drv) { ; }
  354. /* resource management */
  355. static inline struct pnp_option * pnp_register_independent_option(struct pnp_dev *dev) { return NULL; }
  356. static inline struct pnp_option * pnp_register_dependent_option(struct pnp_dev *dev, int priority) { return NULL; }
  357. static inline int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data) { return -ENODEV; }
  358. static inline int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data) { return -ENODEV; }
  359. static inline int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data) { return -ENODEV; }
  360. static inline int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data) { return -ENODEV; }
  361. static inline void pnp_init_resource_table(struct pnp_resource_table *table) { }
  362. static inline int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res, int mode) { return -ENODEV; }
  363. static inline int pnp_auto_config_dev(struct pnp_dev *dev) { return -ENODEV; }
  364. static inline int pnp_validate_config(struct pnp_dev *dev) { return -ENODEV; }
  365. static inline int pnp_start_dev(struct pnp_dev *dev) { return -ENODEV; }
  366. static inline int pnp_stop_dev(struct pnp_dev *dev) { return -ENODEV; }
  367. static inline int pnp_activate_dev(struct pnp_dev *dev) { return -ENODEV; }
  368. static inline int pnp_disable_dev(struct pnp_dev *dev) { return -ENODEV; }
  369. static inline void pnp_resource_change(struct resource *resource,
  370. resource_size_t start,
  371. resource_size_t size) { }
  372. /* protocol helpers */
  373. static inline int pnp_is_active(struct pnp_dev * dev) { return 0; }
  374. static inline int compare_pnp_id(struct pnp_id * pos, const char * id) { return -ENODEV; }
  375. static inline int pnp_add_id(struct pnp_id *id, struct pnp_dev *dev) { return -ENODEV; }
  376. static inline int pnp_register_driver(struct pnp_driver *drv) { return -ENODEV; }
  377. static inline void pnp_unregister_driver(struct pnp_driver *drv) { ; }
  378. #endif /* CONFIG_PNP */
  379. #define pnp_err(format, arg...) printk(KERN_ERR "pnp: " format "\n" , ## arg)
  380. #define pnp_info(format, arg...) printk(KERN_INFO "pnp: " format "\n" , ## arg)
  381. #define pnp_warn(format, arg...) printk(KERN_WARNING "pnp: " format "\n" , ## arg)
  382. #ifdef CONFIG_PNP_DEBUG
  383. #define pnp_dbg(format, arg...) printk(KERN_DEBUG "pnp: " format "\n" , ## arg)
  384. #else
  385. #define pnp_dbg(format, arg...) do {} while (0)
  386. #endif
  387. #endif /* __KERNEL__ */
  388. #endif /* _LINUX_PNP_H */