pnp.h 15 KB

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