manager.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_RANGELENGTH)
  85. res->flags |= IORESOURCE_RANGELENGTH;
  86. if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
  87. res->flags |= IORESOURCE_SHADOWABLE;
  88. if (!rule->size) {
  89. res->flags |= IORESOURCE_DISABLED;
  90. pnp_dbg(&dev->dev, " mem %d disabled\n", idx);
  91. goto __add;
  92. }
  93. res->start = rule->min;
  94. res->end = res->start + rule->size - 1;
  95. while (!pnp_check_mem(dev, res)) {
  96. res->start += rule->align;
  97. res->end = res->start + rule->size - 1;
  98. if (res->start > rule->max || !rule->align) {
  99. pnp_dbg(&dev->dev, " couldn't assign mem %d "
  100. "(min %#llx max %#llx)\n", idx,
  101. (unsigned long long) rule->min,
  102. (unsigned long long) rule->max);
  103. return -EBUSY;
  104. }
  105. }
  106. __add:
  107. pnp_add_mem_resource(dev, res->start, res->end, res->flags);
  108. return 0;
  109. }
  110. static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
  111. {
  112. struct resource *res, local_res;
  113. int i;
  114. /* IRQ priority: this table is good for i386 */
  115. static unsigned short xtab[16] = {
  116. 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
  117. };
  118. res = pnp_find_resource(dev, rule->flags, IORESOURCE_IRQ, idx);
  119. if (res) {
  120. pnp_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
  121. idx, (int) res->start, res->flags);
  122. return 0;
  123. }
  124. res = &local_res;
  125. res->flags = rule->flags | IORESOURCE_AUTO;
  126. res->start = -1;
  127. res->end = -1;
  128. if (bitmap_empty(rule->map.bits, PNP_IRQ_NR)) {
  129. res->flags |= IORESOURCE_DISABLED;
  130. pnp_dbg(&dev->dev, " irq %d disabled\n", idx);
  131. goto __add;
  132. }
  133. /* TBD: need check for >16 IRQ */
  134. res->start = find_next_bit(rule->map.bits, PNP_IRQ_NR, 16);
  135. if (res->start < PNP_IRQ_NR) {
  136. res->end = res->start;
  137. goto __add;
  138. }
  139. for (i = 0; i < 16; i++) {
  140. if (test_bit(xtab[i], rule->map.bits)) {
  141. res->start = res->end = xtab[i];
  142. if (pnp_check_irq(dev, res))
  143. goto __add;
  144. }
  145. }
  146. if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
  147. res->start = -1;
  148. res->end = -1;
  149. res->flags |= IORESOURCE_DISABLED;
  150. pnp_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
  151. goto __add;
  152. }
  153. pnp_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
  154. return -EBUSY;
  155. __add:
  156. pnp_add_irq_resource(dev, res->start, res->flags);
  157. return 0;
  158. }
  159. #ifdef CONFIG_ISA_DMA_API
  160. static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
  161. {
  162. struct resource *res, local_res;
  163. int i;
  164. /* DMA priority: this table is good for i386 */
  165. static unsigned short xtab[8] = {
  166. 1, 3, 5, 6, 7, 0, 2, 4
  167. };
  168. res = pnp_find_resource(dev, rule->flags, IORESOURCE_DMA, idx);
  169. if (res) {
  170. pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
  171. idx, (int) res->start, res->flags);
  172. return 0;
  173. }
  174. res = &local_res;
  175. res->flags = rule->flags | IORESOURCE_AUTO;
  176. res->start = -1;
  177. res->end = -1;
  178. if (!rule->map) {
  179. res->flags |= IORESOURCE_DISABLED;
  180. pnp_dbg(&dev->dev, " dma %d disabled\n", idx);
  181. goto __add;
  182. }
  183. for (i = 0; i < 8; i++) {
  184. if (rule->map & (1 << xtab[i])) {
  185. res->start = res->end = xtab[i];
  186. if (pnp_check_dma(dev, res))
  187. goto __add;
  188. }
  189. }
  190. pnp_dbg(&dev->dev, " couldn't assign dma %d\n", idx);
  191. return -EBUSY;
  192. __add:
  193. pnp_add_dma_resource(dev, res->start, res->flags);
  194. return 0;
  195. }
  196. #endif /* CONFIG_ISA_DMA_API */
  197. void pnp_init_resources(struct pnp_dev *dev)
  198. {
  199. pnp_free_resources(dev);
  200. }
  201. static void pnp_clean_resource_table(struct pnp_dev *dev)
  202. {
  203. struct pnp_resource *pnp_res, *tmp;
  204. list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
  205. if (pnp_res->res.flags & IORESOURCE_AUTO)
  206. pnp_free_resource(pnp_res);
  207. }
  208. }
  209. /**
  210. * pnp_assign_resources - assigns resources to the device based on the specified dependent number
  211. * @dev: pointer to the desired device
  212. * @set: the dependent function number
  213. */
  214. static int pnp_assign_resources(struct pnp_dev *dev, int set)
  215. {
  216. struct pnp_option *option;
  217. int nport = 0, nmem = 0, nirq = 0;
  218. int ndma __maybe_unused = 0;
  219. int ret = 0;
  220. pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
  221. mutex_lock(&pnp_res_mutex);
  222. pnp_clean_resource_table(dev);
  223. list_for_each_entry(option, &dev->options, list) {
  224. if (pnp_option_is_dependent(option) &&
  225. pnp_option_set(option) != set)
  226. continue;
  227. switch (option->type) {
  228. case IORESOURCE_IO:
  229. ret = pnp_assign_port(dev, &option->u.port, nport++);
  230. break;
  231. case IORESOURCE_MEM:
  232. ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
  233. break;
  234. case IORESOURCE_IRQ:
  235. ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
  236. break;
  237. #ifdef CONFIG_ISA_DMA_API
  238. case IORESOURCE_DMA:
  239. ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
  240. break;
  241. #endif
  242. default:
  243. ret = -EINVAL;
  244. break;
  245. }
  246. if (ret < 0)
  247. break;
  248. }
  249. mutex_unlock(&pnp_res_mutex);
  250. if (ret < 0) {
  251. pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
  252. pnp_clean_resource_table(dev);
  253. } else
  254. dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
  255. return ret;
  256. }
  257. /**
  258. * pnp_auto_config_dev - automatically assigns resources to a device
  259. * @dev: pointer to the desired device
  260. */
  261. int pnp_auto_config_dev(struct pnp_dev *dev)
  262. {
  263. int i, ret;
  264. if (!pnp_can_configure(dev)) {
  265. pnp_dbg(&dev->dev, "configuration not supported\n");
  266. return -ENODEV;
  267. }
  268. ret = pnp_assign_resources(dev, 0);
  269. if (ret == 0)
  270. return 0;
  271. for (i = 1; i < dev->num_dependent_sets; i++) {
  272. ret = pnp_assign_resources(dev, i);
  273. if (ret == 0)
  274. return 0;
  275. }
  276. dev_err(&dev->dev, "unable to assign resources\n");
  277. return ret;
  278. }
  279. /**
  280. * pnp_start_dev - low-level start of the PnP device
  281. * @dev: pointer to the desired device
  282. *
  283. * assumes that resources have already been allocated
  284. */
  285. int pnp_start_dev(struct pnp_dev *dev)
  286. {
  287. if (!pnp_can_write(dev)) {
  288. pnp_dbg(&dev->dev, "activation not supported\n");
  289. return -EINVAL;
  290. }
  291. dbg_pnp_show_resources(dev, "pnp_start_dev");
  292. if (dev->protocol->set(dev) < 0) {
  293. dev_err(&dev->dev, "activation failed\n");
  294. return -EIO;
  295. }
  296. dev_info(&dev->dev, "activated\n");
  297. return 0;
  298. }
  299. /**
  300. * pnp_stop_dev - low-level disable of the PnP device
  301. * @dev: pointer to the desired device
  302. *
  303. * does not free resources
  304. */
  305. int pnp_stop_dev(struct pnp_dev *dev)
  306. {
  307. if (!pnp_can_disable(dev)) {
  308. pnp_dbg(&dev->dev, "disabling not supported\n");
  309. return -EINVAL;
  310. }
  311. if (dev->protocol->disable(dev) < 0) {
  312. dev_err(&dev->dev, "disable failed\n");
  313. return -EIO;
  314. }
  315. dev_info(&dev->dev, "disabled\n");
  316. return 0;
  317. }
  318. /**
  319. * pnp_activate_dev - activates a PnP device for use
  320. * @dev: pointer to the desired device
  321. *
  322. * does not validate or set resources so be careful.
  323. */
  324. int pnp_activate_dev(struct pnp_dev *dev)
  325. {
  326. int error;
  327. if (dev->active)
  328. return 0;
  329. /* ensure resources are allocated */
  330. if (pnp_auto_config_dev(dev))
  331. return -EBUSY;
  332. error = pnp_start_dev(dev);
  333. if (error)
  334. return error;
  335. dev->active = 1;
  336. return 0;
  337. }
  338. /**
  339. * pnp_disable_dev - disables device
  340. * @dev: pointer to the desired device
  341. *
  342. * inform the correct pnp protocol so that resources can be used by other devices
  343. */
  344. int pnp_disable_dev(struct pnp_dev *dev)
  345. {
  346. int error;
  347. if (!dev->active)
  348. return 0;
  349. error = pnp_stop_dev(dev);
  350. if (error)
  351. return error;
  352. dev->active = 0;
  353. /* release the resources so that other devices can use them */
  354. mutex_lock(&pnp_res_mutex);
  355. pnp_clean_resource_table(dev);
  356. mutex_unlock(&pnp_res_mutex);
  357. return 0;
  358. }
  359. EXPORT_SYMBOL(pnp_start_dev);
  360. EXPORT_SYMBOL(pnp_stop_dev);
  361. EXPORT_SYMBOL(pnp_activate_dev);
  362. EXPORT_SYMBOL(pnp_disable_dev);