manager.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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 0;
  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 -EBUSY;
  47. }
  48. }
  49. __add:
  50. pnp_add_io_resource(dev, res->start, res->end, res->flags);
  51. return 0;
  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 0;
  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 -EBUSY;
  91. }
  92. }
  93. __add:
  94. pnp_add_mem_resource(dev, res->start, res->end, res->flags);
  95. return 0;
  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 0;
  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. if (rule->flags & IORESOURCE_IRQ_OPTIONAL) {
  134. res->start = -1;
  135. res->end = -1;
  136. res->flags |= IORESOURCE_DISABLED;
  137. dev_dbg(&dev->dev, " irq %d disabled (optional)\n", idx);
  138. goto __add;
  139. }
  140. dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
  141. return -EBUSY;
  142. __add:
  143. pnp_add_irq_resource(dev, res->start, res->flags);
  144. return 0;
  145. }
  146. static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
  147. {
  148. struct resource *res, local_res;
  149. int i;
  150. /* DMA priority: this table is good for i386 */
  151. static unsigned short xtab[8] = {
  152. 1, 3, 5, 6, 7, 0, 2, 4
  153. };
  154. res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
  155. if (res) {
  156. dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
  157. idx, (int) res->start, res->flags);
  158. return 0;
  159. }
  160. res = &local_res;
  161. res->flags = rule->flags | IORESOURCE_AUTO;
  162. res->start = -1;
  163. res->end = -1;
  164. for (i = 0; i < 8; i++) {
  165. if (rule->map & (1 << xtab[i])) {
  166. res->start = res->end = xtab[i];
  167. if (pnp_check_dma(dev, res))
  168. goto __add;
  169. }
  170. }
  171. #ifdef MAX_DMA_CHANNELS
  172. res->start = res->end = MAX_DMA_CHANNELS;
  173. #endif
  174. res->flags |= IORESOURCE_DISABLED;
  175. dev_dbg(&dev->dev, " disable dma %d\n", idx);
  176. __add:
  177. pnp_add_dma_resource(dev, res->start, res->flags);
  178. return 0;
  179. }
  180. void pnp_init_resources(struct pnp_dev *dev)
  181. {
  182. pnp_free_resources(dev);
  183. }
  184. static void pnp_clean_resource_table(struct pnp_dev *dev)
  185. {
  186. struct pnp_resource *pnp_res, *tmp;
  187. list_for_each_entry_safe(pnp_res, tmp, &dev->resources, list) {
  188. if (pnp_res->res.flags & IORESOURCE_AUTO)
  189. pnp_free_resource(pnp_res);
  190. }
  191. }
  192. /**
  193. * pnp_assign_resources - assigns resources to the device based on the specified dependent number
  194. * @dev: pointer to the desired device
  195. * @depnum: the dependent function number
  196. *
  197. * Only set depnum to 0 if the device does not have dependent options.
  198. */
  199. static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
  200. {
  201. struct pnp_port *port;
  202. struct pnp_mem *mem;
  203. struct pnp_irq *irq;
  204. struct pnp_dma *dma;
  205. int nport = 0, nmem = 0, nirq = 0, ndma = 0;
  206. dbg_pnp_show_resources(dev, "before pnp_assign_resources");
  207. mutex_lock(&pnp_res_mutex);
  208. pnp_clean_resource_table(dev);
  209. if (dev->independent) {
  210. dev_dbg(&dev->dev, "assigning independent options\n");
  211. port = dev->independent->port;
  212. mem = dev->independent->mem;
  213. irq = dev->independent->irq;
  214. dma = dev->independent->dma;
  215. while (port) {
  216. if (pnp_assign_port(dev, port, nport) < 0)
  217. goto fail;
  218. nport++;
  219. port = port->next;
  220. }
  221. while (mem) {
  222. if (pnp_assign_mem(dev, mem, nmem) < 0)
  223. goto fail;
  224. nmem++;
  225. mem = mem->next;
  226. }
  227. while (irq) {
  228. if (pnp_assign_irq(dev, irq, nirq) < 0)
  229. goto fail;
  230. nirq++;
  231. irq = irq->next;
  232. }
  233. while (dma) {
  234. if (pnp_assign_dma(dev, dma, ndma) < 0)
  235. goto fail;
  236. ndma++;
  237. dma = dma->next;
  238. }
  239. }
  240. if (depnum) {
  241. struct pnp_option *dep;
  242. int i;
  243. dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum);
  244. for (i = 1, dep = dev->dependent; i < depnum;
  245. i++, dep = dep->next)
  246. if (!dep)
  247. goto fail;
  248. port = dep->port;
  249. mem = dep->mem;
  250. irq = dep->irq;
  251. dma = dep->dma;
  252. while (port) {
  253. if (pnp_assign_port(dev, port, nport) < 0)
  254. goto fail;
  255. nport++;
  256. port = port->next;
  257. }
  258. while (mem) {
  259. if (pnp_assign_mem(dev, mem, nmem) < 0)
  260. goto fail;
  261. nmem++;
  262. mem = mem->next;
  263. }
  264. while (irq) {
  265. if (pnp_assign_irq(dev, irq, nirq) < 0)
  266. goto fail;
  267. nirq++;
  268. irq = irq->next;
  269. }
  270. while (dma) {
  271. if (pnp_assign_dma(dev, dma, ndma) < 0)
  272. goto fail;
  273. ndma++;
  274. dma = dma->next;
  275. }
  276. } else if (dev->dependent)
  277. goto fail;
  278. mutex_unlock(&pnp_res_mutex);
  279. dbg_pnp_show_resources(dev, "after pnp_assign_resources");
  280. return 1;
  281. fail:
  282. pnp_clean_resource_table(dev);
  283. mutex_unlock(&pnp_res_mutex);
  284. dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)");
  285. return 0;
  286. }
  287. /**
  288. * pnp_auto_config_dev - automatically assigns resources to a device
  289. * @dev: pointer to the desired device
  290. */
  291. int pnp_auto_config_dev(struct pnp_dev *dev)
  292. {
  293. struct pnp_option *dep;
  294. int i = 1;
  295. if (!pnp_can_configure(dev)) {
  296. dev_dbg(&dev->dev, "configuration not supported\n");
  297. return -ENODEV;
  298. }
  299. if (!dev->dependent) {
  300. if (pnp_assign_resources(dev, 0))
  301. return 0;
  302. } else {
  303. dep = dev->dependent;
  304. do {
  305. if (pnp_assign_resources(dev, i))
  306. return 0;
  307. dep = dep->next;
  308. i++;
  309. } while (dep);
  310. }
  311. dev_err(&dev->dev, "unable to assign resources\n");
  312. return -EBUSY;
  313. }
  314. /**
  315. * pnp_start_dev - low-level start of the PnP device
  316. * @dev: pointer to the desired device
  317. *
  318. * assumes that resources have already been allocated
  319. */
  320. int pnp_start_dev(struct pnp_dev *dev)
  321. {
  322. if (!pnp_can_write(dev)) {
  323. dev_dbg(&dev->dev, "activation not supported\n");
  324. return -EINVAL;
  325. }
  326. dbg_pnp_show_resources(dev, "pnp_start_dev");
  327. if (dev->protocol->set(dev) < 0) {
  328. dev_err(&dev->dev, "activation failed\n");
  329. return -EIO;
  330. }
  331. dev_info(&dev->dev, "activated\n");
  332. return 0;
  333. }
  334. /**
  335. * pnp_stop_dev - low-level disable of the PnP device
  336. * @dev: pointer to the desired device
  337. *
  338. * does not free resources
  339. */
  340. int pnp_stop_dev(struct pnp_dev *dev)
  341. {
  342. if (!pnp_can_disable(dev)) {
  343. dev_dbg(&dev->dev, "disabling not supported\n");
  344. return -EINVAL;
  345. }
  346. if (dev->protocol->disable(dev) < 0) {
  347. dev_err(&dev->dev, "disable failed\n");
  348. return -EIO;
  349. }
  350. dev_info(&dev->dev, "disabled\n");
  351. return 0;
  352. }
  353. /**
  354. * pnp_activate_dev - activates a PnP device for use
  355. * @dev: pointer to the desired device
  356. *
  357. * does not validate or set resources so be careful.
  358. */
  359. int pnp_activate_dev(struct pnp_dev *dev)
  360. {
  361. int error;
  362. if (dev->active)
  363. return 0;
  364. /* ensure resources are allocated */
  365. if (pnp_auto_config_dev(dev))
  366. return -EBUSY;
  367. error = pnp_start_dev(dev);
  368. if (error)
  369. return error;
  370. dev->active = 1;
  371. return 0;
  372. }
  373. /**
  374. * pnp_disable_dev - disables device
  375. * @dev: pointer to the desired device
  376. *
  377. * inform the correct pnp protocol so that resources can be used by other devices
  378. */
  379. int pnp_disable_dev(struct pnp_dev *dev)
  380. {
  381. int error;
  382. if (!dev->active)
  383. return 0;
  384. error = pnp_stop_dev(dev);
  385. if (error)
  386. return error;
  387. dev->active = 0;
  388. /* release the resources so that other devices can use them */
  389. mutex_lock(&pnp_res_mutex);
  390. pnp_clean_resource_table(dev);
  391. mutex_unlock(&pnp_res_mutex);
  392. return 0;
  393. }
  394. EXPORT_SYMBOL(pnp_start_dev);
  395. EXPORT_SYMBOL(pnp_stop_dev);
  396. EXPORT_SYMBOL(pnp_activate_dev);
  397. EXPORT_SYMBOL(pnp_disable_dev);