manager.c 13 KB

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