manager.c 9.3 KB

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