manager.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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;
  20. res = pnp_get_resource(dev, IORESOURCE_IO, idx);
  21. if (!res) {
  22. dev_err(&dev->dev, "too many I/O port resources\n");
  23. /* pretend we were successful so at least the manager won't try again */
  24. return 1;
  25. }
  26. /* check if this resource has been manually set, if so skip */
  27. if (!(res->flags & IORESOURCE_AUTO)) {
  28. dev_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
  29. "flags %#lx\n", idx, (unsigned long long) res->start,
  30. (unsigned long long) res->end, res->flags);
  31. return 1;
  32. }
  33. /* set the initial values */
  34. res->flags |= rule->flags | IORESOURCE_IO;
  35. res->flags &= ~IORESOURCE_UNSET;
  36. if (!rule->size) {
  37. res->flags |= IORESOURCE_DISABLED;
  38. dev_dbg(&dev->dev, " io %d disabled\n", idx);
  39. return 1; /* skip disabled resource requests */
  40. }
  41. res->start = rule->min;
  42. res->end = res->start + rule->size - 1;
  43. /* run through until pnp_check_port is happy */
  44. while (!pnp_check_port(dev, res)) {
  45. res->start += rule->align;
  46. res->end = res->start + rule->size - 1;
  47. if (res->start > rule->max || !rule->align) {
  48. dev_dbg(&dev->dev, " couldn't assign io %d\n", idx);
  49. return 0;
  50. }
  51. }
  52. dev_dbg(&dev->dev, " assign io %d %#llx-%#llx\n", idx,
  53. (unsigned long long) res->start, (unsigned long long) res->end);
  54. return 1;
  55. }
  56. static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
  57. {
  58. struct resource *res;
  59. res = pnp_get_resource(dev, IORESOURCE_MEM, idx);
  60. if (!res) {
  61. dev_err(&dev->dev, "too many memory resources\n");
  62. /* pretend we were successful so at least the manager won't try again */
  63. return 1;
  64. }
  65. /* check if this resource has been manually set, if so skip */
  66. if (!(res->flags & IORESOURCE_AUTO)) {
  67. dev_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
  68. "flags %#lx\n", idx, (unsigned long long) res->start,
  69. (unsigned long long) res->end, res->flags);
  70. return 1;
  71. }
  72. /* set the initial values */
  73. res->flags |= rule->flags | IORESOURCE_MEM;
  74. res->flags &= ~IORESOURCE_UNSET;
  75. /* convert pnp flags to standard Linux flags */
  76. if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
  77. res->flags |= IORESOURCE_READONLY;
  78. if (rule->flags & IORESOURCE_MEM_CACHEABLE)
  79. res->flags |= IORESOURCE_CACHEABLE;
  80. if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
  81. res->flags |= IORESOURCE_RANGELENGTH;
  82. if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
  83. res->flags |= IORESOURCE_SHADOWABLE;
  84. if (!rule->size) {
  85. res->flags |= IORESOURCE_DISABLED;
  86. dev_dbg(&dev->dev, " mem %d disabled\n", idx);
  87. return 1; /* skip disabled resource requests */
  88. }
  89. res->start = rule->min;
  90. res->end = res->start + rule->size - 1;
  91. /* run through until pnp_check_mem is happy */
  92. while (!pnp_check_mem(dev, res)) {
  93. res->start += rule->align;
  94. res->end = res->start + rule->size - 1;
  95. if (res->start > rule->max || !rule->align) {
  96. dev_dbg(&dev->dev, " couldn't assign mem %d\n", idx);
  97. return 0;
  98. }
  99. }
  100. dev_dbg(&dev->dev, " assign mem %d %#llx-%#llx\n", idx,
  101. (unsigned long long) res->start, (unsigned long long) res->end);
  102. return 1;
  103. }
  104. static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
  105. {
  106. struct resource *res;
  107. int i;
  108. /* IRQ priority: this table is good for i386 */
  109. static unsigned short xtab[16] = {
  110. 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
  111. };
  112. res = pnp_get_resource(dev, IORESOURCE_IRQ, idx);
  113. if (!res) {
  114. dev_err(&dev->dev, "too many IRQ resources\n");
  115. /* pretend we were successful so at least the manager won't try again */
  116. return 1;
  117. }
  118. /* check if this resource has been manually set, if so skip */
  119. if (!(res->flags & IORESOURCE_AUTO)) {
  120. dev_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
  121. idx, (int) res->start, res->flags);
  122. return 1;
  123. }
  124. /* set the initial values */
  125. res->flags |= rule->flags | IORESOURCE_IRQ;
  126. res->flags &= ~IORESOURCE_UNSET;
  127. if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
  128. res->flags |= IORESOURCE_DISABLED;
  129. dev_dbg(&dev->dev, " irq %d disabled\n", idx);
  130. return 1; /* skip disabled resource requests */
  131. }
  132. /* TBD: need check for >16 IRQ */
  133. res->start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
  134. if (res->start < PNP_IRQ_NR) {
  135. res->end = res->start;
  136. dev_dbg(&dev->dev, " assign irq %d %d\n", idx,
  137. (int) res->start);
  138. return 1;
  139. }
  140. for (i = 0; i < 16; i++) {
  141. if (test_bit(xtab[i], rule->map)) {
  142. res->start = res->end = xtab[i];
  143. if (pnp_check_irq(dev, res)) {
  144. dev_dbg(&dev->dev, " assign irq %d %d\n", idx,
  145. (int) res->start);
  146. return 1;
  147. }
  148. }
  149. }
  150. dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
  151. return 0;
  152. }
  153. static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
  154. {
  155. struct resource *res;
  156. int i;
  157. /* DMA priority: this table is good for i386 */
  158. static unsigned short xtab[8] = {
  159. 1, 3, 5, 6, 7, 0, 2, 4
  160. };
  161. res = pnp_get_resource(dev, IORESOURCE_DMA, idx);
  162. if (!res) {
  163. dev_err(&dev->dev, "too many DMA resources\n");
  164. return;
  165. }
  166. /* check if this resource has been manually set, if so skip */
  167. if (!(res->flags & IORESOURCE_AUTO)) {
  168. dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
  169. idx, (int) res->start, res->flags);
  170. return;
  171. }
  172. /* set the initial values */
  173. res->flags |= rule->flags | IORESOURCE_DMA;
  174. res->flags &= ~IORESOURCE_UNSET;
  175. for (i = 0; i < 8; i++) {
  176. if (rule->map & (1 << xtab[i])) {
  177. res->start = res->end = xtab[i];
  178. if (pnp_check_dma(dev, res)) {
  179. dev_dbg(&dev->dev, " assign dma %d %d\n", idx,
  180. (int) res->start);
  181. return;
  182. }
  183. }
  184. }
  185. #ifdef MAX_DMA_CHANNELS
  186. res->start = res->end = MAX_DMA_CHANNELS;
  187. #endif
  188. res->flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
  189. dev_dbg(&dev->dev, " disable dma %d\n", idx);
  190. }
  191. void pnp_init_resource(struct resource *res)
  192. {
  193. unsigned long type;
  194. type = res->flags & (IORESOURCE_IO | IORESOURCE_MEM |
  195. IORESOURCE_IRQ | IORESOURCE_DMA);
  196. res->name = NULL;
  197. res->flags = type | IORESOURCE_AUTO | IORESOURCE_UNSET;
  198. if (type == IORESOURCE_IRQ || type == IORESOURCE_DMA) {
  199. res->start = -1;
  200. res->end = -1;
  201. } else {
  202. res->start = 0;
  203. res->end = 0;
  204. }
  205. }
  206. /**
  207. * pnp_init_resources - Resets a resource table to default values.
  208. * @table: pointer to the desired resource table
  209. */
  210. void pnp_init_resources(struct pnp_dev *dev)
  211. {
  212. struct resource *res;
  213. int idx;
  214. for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
  215. res = &dev->res->irq_resource[idx];
  216. res->flags = IORESOURCE_IRQ;
  217. pnp_init_resource(res);
  218. }
  219. for (idx = 0; idx < PNP_MAX_DMA; idx++) {
  220. res = &dev->res->dma_resource[idx];
  221. res->flags = IORESOURCE_DMA;
  222. pnp_init_resource(res);
  223. }
  224. for (idx = 0; idx < PNP_MAX_PORT; idx++) {
  225. res = &dev->res->port_resource[idx];
  226. res->flags = IORESOURCE_IO;
  227. pnp_init_resource(res);
  228. }
  229. for (idx = 0; idx < PNP_MAX_MEM; idx++) {
  230. res = &dev->res->mem_resource[idx];
  231. res->flags = IORESOURCE_MEM;
  232. pnp_init_resource(res);
  233. }
  234. }
  235. /**
  236. * pnp_clean_resources - clears resources that were not manually set
  237. * @res: the resources to clean
  238. */
  239. static void pnp_clean_resource_table(struct pnp_dev *dev)
  240. {
  241. struct resource *res;
  242. int idx;
  243. for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
  244. res = &dev->res->irq_resource[idx];
  245. if (res->flags & IORESOURCE_AUTO) {
  246. res->flags = IORESOURCE_IRQ;
  247. pnp_init_resource(res);
  248. }
  249. }
  250. for (idx = 0; idx < PNP_MAX_DMA; idx++) {
  251. res = &dev->res->dma_resource[idx];
  252. if (res->flags & IORESOURCE_AUTO) {
  253. res->flags = IORESOURCE_DMA;
  254. pnp_init_resource(res);
  255. }
  256. }
  257. for (idx = 0; idx < PNP_MAX_PORT; idx++) {
  258. res = &dev->res->port_resource[idx];
  259. if (res->flags & IORESOURCE_AUTO) {
  260. res->flags = IORESOURCE_IO;
  261. pnp_init_resource(res);
  262. }
  263. }
  264. for (idx = 0; idx < PNP_MAX_MEM; idx++) {
  265. res = &dev->res->mem_resource[idx];
  266. if (res->flags & IORESOURCE_AUTO) {
  267. res->flags = IORESOURCE_MEM;
  268. pnp_init_resource(res);
  269. }
  270. }
  271. }
  272. /**
  273. * pnp_assign_resources - assigns resources to the device based on the specified dependent number
  274. * @dev: pointer to the desired device
  275. * @depnum: the dependent function number
  276. *
  277. * Only set depnum to 0 if the device does not have dependent options.
  278. */
  279. static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
  280. {
  281. struct pnp_port *port;
  282. struct pnp_mem *mem;
  283. struct pnp_irq *irq;
  284. struct pnp_dma *dma;
  285. int nport = 0, nmem = 0, nirq = 0, ndma = 0;
  286. if (!pnp_can_configure(dev))
  287. return -ENODEV;
  288. dbg_pnp_show_resources(dev, "before pnp_assign_resources");
  289. mutex_lock(&pnp_res_mutex);
  290. pnp_clean_resource_table(dev);
  291. if (dev->independent) {
  292. dev_dbg(&dev->dev, "assigning independent options\n");
  293. port = dev->independent->port;
  294. mem = dev->independent->mem;
  295. irq = dev->independent->irq;
  296. dma = dev->independent->dma;
  297. while (port) {
  298. if (!pnp_assign_port(dev, port, nport))
  299. goto fail;
  300. nport++;
  301. port = port->next;
  302. }
  303. while (mem) {
  304. if (!pnp_assign_mem(dev, mem, nmem))
  305. goto fail;
  306. nmem++;
  307. mem = mem->next;
  308. }
  309. while (irq) {
  310. if (!pnp_assign_irq(dev, irq, nirq))
  311. goto fail;
  312. nirq++;
  313. irq = irq->next;
  314. }
  315. while (dma) {
  316. pnp_assign_dma(dev, dma, ndma);
  317. ndma++;
  318. dma = dma->next;
  319. }
  320. }
  321. if (depnum) {
  322. struct pnp_option *dep;
  323. int i;
  324. dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum);
  325. for (i = 1, dep = dev->dependent; i < depnum;
  326. i++, dep = dep->next)
  327. if (!dep)
  328. goto fail;
  329. port = dep->port;
  330. mem = dep->mem;
  331. irq = dep->irq;
  332. dma = dep->dma;
  333. while (port) {
  334. if (!pnp_assign_port(dev, port, nport))
  335. goto fail;
  336. nport++;
  337. port = port->next;
  338. }
  339. while (mem) {
  340. if (!pnp_assign_mem(dev, mem, nmem))
  341. goto fail;
  342. nmem++;
  343. mem = mem->next;
  344. }
  345. while (irq) {
  346. if (!pnp_assign_irq(dev, irq, nirq))
  347. goto fail;
  348. nirq++;
  349. irq = irq->next;
  350. }
  351. while (dma) {
  352. pnp_assign_dma(dev, dma, ndma);
  353. ndma++;
  354. dma = dma->next;
  355. }
  356. } else if (dev->dependent)
  357. goto fail;
  358. mutex_unlock(&pnp_res_mutex);
  359. dbg_pnp_show_resources(dev, "after pnp_assign_resources");
  360. return 1;
  361. fail:
  362. pnp_clean_resource_table(dev);
  363. mutex_unlock(&pnp_res_mutex);
  364. dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)");
  365. return 0;
  366. }
  367. /**
  368. * pnp_auto_config_dev - automatically assigns resources to a device
  369. * @dev: pointer to the desired device
  370. */
  371. int pnp_auto_config_dev(struct pnp_dev *dev)
  372. {
  373. struct pnp_option *dep;
  374. int i = 1;
  375. if (!pnp_can_configure(dev)) {
  376. dev_dbg(&dev->dev, "configuration not supported\n");
  377. return -ENODEV;
  378. }
  379. if (!dev->dependent) {
  380. if (pnp_assign_resources(dev, 0))
  381. return 0;
  382. } else {
  383. dep = dev->dependent;
  384. do {
  385. if (pnp_assign_resources(dev, i))
  386. return 0;
  387. dep = dep->next;
  388. i++;
  389. } while (dep);
  390. }
  391. dev_err(&dev->dev, "unable to assign resources\n");
  392. return -EBUSY;
  393. }
  394. /**
  395. * pnp_start_dev - low-level start of the PnP device
  396. * @dev: pointer to the desired device
  397. *
  398. * assumes that resources have already been allocated
  399. */
  400. int pnp_start_dev(struct pnp_dev *dev)
  401. {
  402. if (!pnp_can_write(dev)) {
  403. dev_dbg(&dev->dev, "activation not supported\n");
  404. return -EINVAL;
  405. }
  406. dbg_pnp_show_resources(dev, "pnp_start_dev");
  407. if (dev->protocol->set(dev) < 0) {
  408. dev_err(&dev->dev, "activation failed\n");
  409. return -EIO;
  410. }
  411. dev_info(&dev->dev, "activated\n");
  412. return 0;
  413. }
  414. /**
  415. * pnp_stop_dev - low-level disable of the PnP device
  416. * @dev: pointer to the desired device
  417. *
  418. * does not free resources
  419. */
  420. int pnp_stop_dev(struct pnp_dev *dev)
  421. {
  422. if (!pnp_can_disable(dev)) {
  423. dev_dbg(&dev->dev, "disabling not supported\n");
  424. return -EINVAL;
  425. }
  426. if (dev->protocol->disable(dev) < 0) {
  427. dev_err(&dev->dev, "disable failed\n");
  428. return -EIO;
  429. }
  430. dev_info(&dev->dev, "disabled\n");
  431. return 0;
  432. }
  433. /**
  434. * pnp_activate_dev - activates a PnP device for use
  435. * @dev: pointer to the desired device
  436. *
  437. * does not validate or set resources so be careful.
  438. */
  439. int pnp_activate_dev(struct pnp_dev *dev)
  440. {
  441. int error;
  442. if (dev->active)
  443. return 0;
  444. /* ensure resources are allocated */
  445. if (pnp_auto_config_dev(dev))
  446. return -EBUSY;
  447. error = pnp_start_dev(dev);
  448. if (error)
  449. return error;
  450. dev->active = 1;
  451. return 0;
  452. }
  453. /**
  454. * pnp_disable_dev - disables device
  455. * @dev: pointer to the desired device
  456. *
  457. * inform the correct pnp protocol so that resources can be used by other devices
  458. */
  459. int pnp_disable_dev(struct pnp_dev *dev)
  460. {
  461. int error;
  462. if (!dev->active)
  463. return 0;
  464. error = pnp_stop_dev(dev);
  465. if (error)
  466. return error;
  467. dev->active = 0;
  468. /* release the resources so that other devices can use them */
  469. mutex_lock(&pnp_res_mutex);
  470. pnp_clean_resource_table(dev);
  471. mutex_unlock(&pnp_res_mutex);
  472. return 0;
  473. }
  474. EXPORT_SYMBOL(pnp_start_dev);
  475. EXPORT_SYMBOL(pnp_stop_dev);
  476. EXPORT_SYMBOL(pnp_activate_dev);
  477. EXPORT_SYMBOL(pnp_disable_dev);