manager.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
  148. {
  149. struct resource *res, local_res;
  150. int i;
  151. /* DMA priority: this table is good for i386 */
  152. static unsigned short xtab[8] = {
  153. 1, 3, 5, 6, 7, 0, 2, 4
  154. };
  155. res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
  156. if (res) {
  157. pnp_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
  158. idx, (int) res->start, res->flags);
  159. return 0;
  160. }
  161. res = &local_res;
  162. res->flags = rule->flags | IORESOURCE_AUTO;
  163. res->start = -1;
  164. res->end = -1;
  165. for (i = 0; i < 8; i++) {
  166. if (rule->map & (1 << xtab[i])) {
  167. res->start = res->end = xtab[i];
  168. if (pnp_check_dma(dev, res))
  169. goto __add;
  170. }
  171. }
  172. #ifdef MAX_DMA_CHANNELS
  173. res->start = res->end = MAX_DMA_CHANNELS;
  174. #endif
  175. res->flags |= IORESOURCE_DISABLED;
  176. pnp_dbg(&dev->dev, " disable dma %d\n", idx);
  177. __add:
  178. pnp_add_dma_resource(dev, res->start, res->flags);
  179. return 0;
  180. }
  181. void pnp_init_resources(struct pnp_dev *dev)
  182. {
  183. pnp_free_resources(dev);
  184. }
  185. static void pnp_clean_resource_table(struct pnp_dev *dev)
  186. {
  187. struct pnp_resource *pnp_res, *tmp;
  188. list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
  189. if (pnp_res->res.flags & IORESOURCE_AUTO)
  190. pnp_free_resource(pnp_res);
  191. }
  192. }
  193. /**
  194. * pnp_assign_resources - assigns resources to the device based on the specified dependent number
  195. * @dev: pointer to the desired device
  196. * @set: the dependent function number
  197. */
  198. static int pnp_assign_resources(struct pnp_dev *dev, int set)
  199. {
  200. struct pnp_option *option;
  201. int nport = 0, nmem = 0, nirq = 0, ndma = 0;
  202. int ret = 0;
  203. pnp_dbg(&dev->dev, "pnp_assign_resources, try dependent set %d\n", set);
  204. mutex_lock(&pnp_res_mutex);
  205. pnp_clean_resource_table(dev);
  206. list_for_each_entry(option, &dev->options, list) {
  207. if (pnp_option_is_dependent(option) &&
  208. pnp_option_set(option) != set)
  209. continue;
  210. switch (option->type) {
  211. case IORESOURCE_IO:
  212. ret = pnp_assign_port(dev, &option->u.port, nport++);
  213. break;
  214. case IORESOURCE_MEM:
  215. ret = pnp_assign_mem(dev, &option->u.mem, nmem++);
  216. break;
  217. case IORESOURCE_IRQ:
  218. ret = pnp_assign_irq(dev, &option->u.irq, nirq++);
  219. break;
  220. case IORESOURCE_DMA:
  221. ret = pnp_assign_dma(dev, &option->u.dma, ndma++);
  222. break;
  223. default:
  224. ret = -EINVAL;
  225. break;
  226. }
  227. if (ret < 0)
  228. break;
  229. }
  230. mutex_unlock(&pnp_res_mutex);
  231. if (ret < 0) {
  232. pnp_dbg(&dev->dev, "pnp_assign_resources failed (%d)\n", ret);
  233. pnp_clean_resource_table(dev);
  234. } else
  235. dbg_pnp_show_resources(dev, "pnp_assign_resources succeeded");
  236. return ret;
  237. }
  238. /**
  239. * pnp_auto_config_dev - automatically assigns resources to a device
  240. * @dev: pointer to the desired device
  241. */
  242. int pnp_auto_config_dev(struct pnp_dev *dev)
  243. {
  244. int i, ret;
  245. if (!pnp_can_configure(dev)) {
  246. pnp_dbg(&dev->dev, "configuration not supported\n");
  247. return -ENODEV;
  248. }
  249. ret = pnp_assign_resources(dev, 0);
  250. if (ret == 0)
  251. return 0;
  252. for (i = 1; i < dev->num_dependent_sets; i++) {
  253. ret = pnp_assign_resources(dev, i);
  254. if (ret == 0)
  255. return 0;
  256. }
  257. dev_err(&dev->dev, "unable to assign resources\n");
  258. return ret;
  259. }
  260. /**
  261. * pnp_start_dev - low-level start of the PnP device
  262. * @dev: pointer to the desired device
  263. *
  264. * assumes that resources have already been allocated
  265. */
  266. int pnp_start_dev(struct pnp_dev *dev)
  267. {
  268. if (!pnp_can_write(dev)) {
  269. pnp_dbg(&dev->dev, "activation not supported\n");
  270. return -EINVAL;
  271. }
  272. dbg_pnp_show_resources(dev, "pnp_start_dev");
  273. if (dev->protocol->set(dev) < 0) {
  274. dev_err(&dev->dev, "activation failed\n");
  275. return -EIO;
  276. }
  277. dev_info(&dev->dev, "activated\n");
  278. return 0;
  279. }
  280. /**
  281. * pnp_stop_dev - low-level disable of the PnP device
  282. * @dev: pointer to the desired device
  283. *
  284. * does not free resources
  285. */
  286. int pnp_stop_dev(struct pnp_dev *dev)
  287. {
  288. if (!pnp_can_disable(dev)) {
  289. pnp_dbg(&dev->dev, "disabling not supported\n");
  290. return -EINVAL;
  291. }
  292. if (dev->protocol->disable(dev) < 0) {
  293. dev_err(&dev->dev, "disable failed\n");
  294. return -EIO;
  295. }
  296. dev_info(&dev->dev, "disabled\n");
  297. return 0;
  298. }
  299. /**
  300. * pnp_activate_dev - activates a PnP device for use
  301. * @dev: pointer to the desired device
  302. *
  303. * does not validate or set resources so be careful.
  304. */
  305. int pnp_activate_dev(struct pnp_dev *dev)
  306. {
  307. int error;
  308. if (dev->active)
  309. return 0;
  310. /* ensure resources are allocated */
  311. if (pnp_auto_config_dev(dev))
  312. return -EBUSY;
  313. error = pnp_start_dev(dev);
  314. if (error)
  315. return error;
  316. dev->active = 1;
  317. return 0;
  318. }
  319. /**
  320. * pnp_disable_dev - disables device
  321. * @dev: pointer to the desired device
  322. *
  323. * inform the correct pnp protocol so that resources can be used by other devices
  324. */
  325. int pnp_disable_dev(struct pnp_dev *dev)
  326. {
  327. int error;
  328. if (!dev->active)
  329. return 0;
  330. error = pnp_stop_dev(dev);
  331. if (error)
  332. return error;
  333. dev->active = 0;
  334. /* release the resources so that other devices can use them */
  335. mutex_lock(&pnp_res_mutex);
  336. pnp_clean_resource_table(dev);
  337. mutex_unlock(&pnp_res_mutex);
  338. return 0;
  339. }
  340. EXPORT_SYMBOL(pnp_start_dev);
  341. EXPORT_SYMBOL(pnp_stop_dev);
  342. EXPORT_SYMBOL(pnp_activate_dev);
  343. EXPORT_SYMBOL(pnp_disable_dev);