manager.c 9.7 KB

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