manager.c 13 KB

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