manager.c 14 KB

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