manager.c 9.3 KB

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