manager.c 9.9 KB

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