pnp.h 15 KB

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