manager.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * manager.c - Resource Management, Conflict Resolution, Activation and Disabling of Devices
  3. *
  4. * based on isapnp.c resource management (c) Jaroslav Kysela <perex@perex.cz>
  5. * Copyright 2003 Adam Belay <ambx1@neo.rr.com>
  6. * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
  7. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/pnp.h>
  14. #include <linux/bitmap.h>
  15. #include <linux/mutex.h>
  16. #include "base.h"
  17. DEFINE_MUTEX(pnp_res_mutex);
  18. static struct resource *pnp_find_resource(struct pnp_dev *dev,
  19. unsigned char rule,
  20. unsigned long type,
  21. unsigned int bar)
  22. {
  23. struct resource *res = pnp_get_resource(dev, type, bar);
  24. /* when the resource already exists, set its resource bits from rule */
  25. if (res) {
  26. res->flags &= ~IORESOURCE_BITS;
  27. res->flags |= rule & IORESOURCE_BITS;
  28. }
  29. return res;
  30. }
  31. static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
  32. {
  33. struct resource *res, local_res;
  34. res = pnp_find_resource(dev, rule->flags, IORESOURCE_IO, idx);
  35. if (res) {
  36. pnp_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
  37. "flags %#lx\n", idx, (unsigned long long) res->start,
  38. (unsigned long long) res->end, res->flags);
  39. return 0;
  40. }
  41. res = &local_res;
  42. res->flags = rule->flags | IORESOURCE_AUTO;
  43. res->start = 0;
  44. res->end = 0;
  45. if (!rule->size) {
  46. res->flags |= IORESOURCE_DISABLED;
  47. pnp_dbg(&dev->dev, " io %d disabled\n", idx);
  48. goto __add;
  49. }
  50. res->start = rule->min;
  51. res->end = res->start + rule->size - 1;
  52. while (!pnp_check_port(dev, res)) {
  53. res->start += rule->align;
  54. res->end = res->start + rule->size - 1;
  55. if (res->start > rule->max || !rule->align) {
  56. pnp_dbg(&dev->dev, " couldn't assign io %d "
  57. "(min %#llx max %#llx)\n", idx,
  58. (unsigned long long) rule->min,
  59. (unsigned long long) rule->max);
  60. return -EBUSY;
  61. }
  62. }
  63. __add:
  64. pnp_add_io_resource(dev, res->start, res->end, res->flags);
  65. return 0;
  66. }
  67. static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
  68. {
  69. struct resource *res, local_res;
  70. res = pnp_find_resource(dev, rule->flags, IORESOURCE_MEM, idx);
  71. if (res) {
  72. pnp_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
  73. "flags %#lx\n", idx, (unsigned long long) res->start,
  74. (unsigned long long) res->end, res->flags);
  75. return 0;
  76. }
  77. res = &local_res;
  78. res->flags = rule->flags | IORESOURCE_AUTO;
  79. res->start = 0;
  80. res->end = 0;
  81. /* ??? rule->flags restricted to 8 bits, all tests bogus ??? */
  82. if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
  83. res->flags |= IORESOURCE_READONLY;
  84. if (rule->flags & IORESOURCE_MEM_CACHEABLE)
  85. res->flags |= IORESOURCE_CACHEABLE;
  86. if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
  87. res->flags |= IORESOURCE_RANGELENGTH;
  88. if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
  89. res->flags |= IORESOURCE_SHADOWABLE;
  90. if (!rule->size) {
  91. res->flags |= IORESOURCE_DISABLED;
  92. pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
  93. goto __add;
  94. }
  95. res->start = rule->min;
  96. res->end = res->start + rule->size - 1;
  97. while (!pnp_check_mem(dev, res)) {
  98. res->start += rule->align;
  99. res->end = res->start + rule->size - 1;
  100. if (res->start > rule->max || !rule->align) {
  101. pnp_dbg(&dev->dev, " couldn't assign mem %d "
  102. "(min %#llx max %#llx)\n", idx,
  103. (unsigned long long) rule->min,
  104. (unsigned long long) rule->max);
  105. return -EBUSY;
  106. }
  107. }
  108. __add:
  109. pnp_add_mem_resource(dev, res->start, res->end, res->flags);
  110. return 0;
  111. }
  112. static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
  113. {
  114. struct resource *res, local_res;
  115. int i;
  116. /* IRQ priority: this table is good for i386 */
  117. static unsigned short xtab[16] = {
  118. 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
  119. };
  120. res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx);
  121. if (res) {
  122. pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
  123. idx, (int) res->start, res->flags);
  124. return 0;
  125. }
  126. res = &local_res;
  127. res->flags = rule->flags | IORESOURCE_AUTO;
  128. res->start = -1;
  129. res->end = -1;
  130. if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
  131. res->flags |= IORESOURCE_DISABLED;
  132. pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
  133. goto __add;
  134. }
  135. /* TBD: need check for >16 IRQ */
  136. res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
  137. if (res->start < PNP_IRQ_NR) {
  138. res->end = res->start;
  139. goto __add;
  140. }
  141. for (i = 0; i < 16; i++) {
  142. if (test_bit(xtab[i], rule->map.bits)) {
  143. res->start = res->end = xtab[i];
  144. if (pnp_check_irq(dev, res))
  145. goto __add;
  146. }
  147. }
  148. if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
  149. res->start = -1;
  150. res->end = -1;
  151. res->flags |= IORESOURCE_DISABLED;
  152. pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
  153. goto __add;
  154. }
  155. pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
  156. return -EBUSY;
  157. __add:
  158. pnp_add_irq_resource(dev, res->start, res->flags);
  159. return 0;
  160. }
  161. #ifdef CONFIG_ISA_DMA_API
  162. static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
  163. {
  164. struct resource *res, local_res;
  165. int i;
  166. /* DMA priority: this table is good for i386 */
  167. static unsigned short xtab[8] = {
  168. 1, 3, 5, 6, 7, 0, 2, 4
  169. };
  170. res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx);
  171. if (res) {
  172. pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
  173. idx, (int) res->start, res->flags);
  174. return 0;
  175. }
  176. res = &local_res;
  177. res->flags = rule->flags | IORESOURCE_AUTO;
  178. res->start = -1;
  179. res->end = -1;
  180. if (!rule->map) {
  181. res->flags |= IORESOURCE_DISABLED;
  182. pnp_dbg(&dev->dev, " dma %d disabled\n", idx);
  183. goto __add;
  184. }
  185. for (i = 0; i < 8; i++) {
  186. if (rule->map & (1 << xtab[i])) {
  187. res->start = res->end = xtab[i];
  188. if (pnp_check_dma(dev, res))
  189. goto __add;
  190. }
  191. }
  192. pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx);
  193. return -EBUSY;
  194. __add:
  195. pnp_add_dma_resource(dev, res->start, res->flags);
  196. return 0;
  197. }
  198. #endif /* CONFIG_ISA_DMA_API */
  199. void pnp_init_resources(struct pnp_dev *dev)
  200. {
  201. pnp_free_resources(dev);
  202. }
  203. static void pnp_clean_resource_table(struct pnp_dev *dev)
  204. {
  205. struct pnp_resource *pnp_res, *tmp;
  206. list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
  207. if (pnp_res->res.flags & IORESOURCE_AUTO)
  208. pnp_free_resource(pnp_res);
  209. }
  210. }
  211. /**
  212. * pnp_assign_resources - assigns resources to the device based on the specified dependent number
  213. * @dev: pointer to the desired device
  214. * @set: the dependent function number
  215. */
  216. static int pnp_assign_resources(struct pnp_dev *dev, int set)
  217. {
  218. struct pnp_option *option;
  219. int nport = 0, nmem = 0, nirq = 0;
  220. int ndma __maybe_unused = 0;
  221. int ret = 0;
  222. pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
  223. mutex_lock(&pnp_res_mutex);
  224. pnp_clean_resource_table(dev);
  225. list_for_each_entry(option, &dev->options, list) {
  226. if (pnp_option_is_dependent(option) &&
  227. pnp_option_set(option) != set)
  228. continue;
  229. switch (option->type) {
  230. case IORESOURCE_IO:
  231. ret = pnp_assign_port(dev, &option->u.port, nport++);
  232. break;
  233. case IORESOURCE_MEM:
  234. ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
  235. break;
  236. case IORESOURCE_IRQ:
  237. ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
  238. break;
  239. #ifdef CONFIG_ISA_DMA_API
  240. case IORESOURCE_DMA:
  241. ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
  242. break;
  243. #endif
  244. default:
  245. ret = -EINVAL;
  246. break;
  247. }
  248. if (ret < 0)
  249. break;
  250. }
  251. mutex_unlock(&pnp_res_mutex);
  252. if (ret < 0) {
  253. pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
  254. pnp_clean_resource_table(dev);
  255. } else
  256. dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
  257. return ret;
  258. }
  259. /**
  260. * pnp_auto_config_dev - automatically assigns resources to a device
  261. * @dev: pointer to the desired device
  262. */
  263. int pnp_auto_config_dev(struct pnp_dev *dev)
  264. {
  265. int i, ret;
  266. if (!pnp_can_configure(dev)) {
  267. pnp_dbg(&dev->dev, "configuration not supported\n");
  268. return -ENODEV;
  269. }
  270. ret = pnp_assign_resources(dev, 0);
  271. if (ret == 0)
  272. return 0;
  273. for (i = 1; i < dev->num_dependent_sets; i++) {
  274. ret = pnp_assign_resources(dev, i);
  275. if (ret == 0)
  276. return 0;
  277. }
  278. dev_err(&dev->dev, "unable to assign resources\n");
  279. return ret;
  280. }
  281. /**
  282. * pnp_start_dev - low-level start of the PnP device
  283. * @dev: pointer to the desired device
  284. *
  285. * assumes that resources have already been allocated
  286. */
  287. int pnp_start_dev(struct pnp_dev *dev)
  288. {
  289. if (!pnp_can_write(dev)) {
  290. pnp_dbg(&dev->dev, "activation not supported\n");
  291. return -EINVAL;
  292. }
  293. dbg_pnp_show_resources(dev, "pnp_start_dev");
  294. if (dev->protocol->set(dev) < 0) {
  295. dev_err(&dev->dev, "activation failed\n");
  296. return -EIO;
  297. }
  298. dev_info(&dev->dev, "activated\n");
  299. return 0;
  300. }
  301. /**
  302. * pnp_stop_dev - low-level disable of the PnP device
  303. * @dev: pointer to the desired device
  304. *
  305. * does not free resources
  306. */
  307. int pnp_stop_dev(struct pnp_dev *dev)
  308. {
  309. if (!pnp_can_disable(dev)) {
  310. pnp_dbg(&dev->dev, "disabling not supported\n");
  311. return -EINVAL;
  312. }
  313. if (dev->protocol->disable(dev) < 0) {
  314. dev_err(&dev->dev, "disable failed\n");
  315. return -EIO;
  316. }
  317. dev_info(&dev->dev, "disabled\n");
  318. return 0;
  319. }
  320. /**
  321. * pnp_activate_dev - activates a PnP device for use
  322. * @dev: pointer to the desired device
  323. *
  324. * does not validate or set resources so be careful.
  325. */
  326. int pnp_activate_dev(struct pnp_dev *dev)
  327. {
  328. int error;
  329. if (dev->active)
  330. return 0;
  331. /* ensure resources are allocated */
  332. if (pnp_auto_config_dev(dev))
  333. return -EBUSY;
  334. error = pnp_start_dev(dev);
  335. if (error)
  336. return error;
  337. dev->active = 1;
  338. return 0;
  339. }
  340. /**
  341. * pnp_disable_dev - disables device
  342. * @dev: pointer to the desired device
  343. *
  344. * inform the correct pnp protocol so that resources can be used by other devices
  345. */
  346. int pnp_disable_dev(struct pnp_dev *dev)
  347. {
  348. int error;
  349. if (!dev->active)
  350. return 0;
  351. error = pnp_stop_dev(dev);
  352. if (error)
  353. return error;
  354. dev->active = 0;
  355. /* release the resources so that other devices can use them */
  356. mutex_lock(&pnp_res_mutex);
  357. pnp_clean_resource_table(dev);
  358. mutex_unlock(&pnp_res_mutex);
  359. return 0;
  360. }
  361. EXPORT_SYMBOL(pnp_start_dev);
  362. EXPORT_SYMBOL(pnp_stop_dev);
  363. EXPORT_SYMBOL(pnp_activate_dev);
  364. EXPORT_SYMBOL(pnp_disable_dev);