manager.c 14 KB

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