manager.c 14 KB

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