manager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. */
  8. #include <linux/config.h>
  9. #include <linux/errno.h>
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/pnp.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. unsigned long *start, *end, *flags;
  19. if (!dev || !rule)
  20. return -EINVAL;
  21. if (idx >= PNP_MAX_PORT) {
  22. pnp_err("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. unsigned long *start, *end, *flags;
  53. if (!dev || !rule)
  54. return -EINVAL;
  55. if (idx >= PNP_MAX_MEM) {
  56. pnp_err("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. unsigned long *start, *end, *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 (!dev || !rule)
  102. return -EINVAL;
  103. if (idx >= PNP_MAX_IRQ) {
  104. pnp_err("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. unsigned long *start, *end, *flags;
  139. int i;
  140. /* DMA priority: this table is good for i386 */
  141. static unsigned short xtab[8] = {
  142. 1, 3, 5, 6, 7, 0, 2, 4
  143. };
  144. if (!dev || !rule)
  145. return -EINVAL;
  146. if (idx >= PNP_MAX_DMA) {
  147. pnp_err("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. */
  178. void pnp_init_resource_table(struct pnp_resource_table *table)
  179. {
  180. int idx;
  181. for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
  182. table->irq_resource[idx].name = NULL;
  183. table->irq_resource[idx].start = -1;
  184. table->irq_resource[idx].end = -1;
  185. table->irq_resource[idx].flags = 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 = IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
  192. }
  193. for (idx = 0; idx < PNP_MAX_PORT; idx++) {
  194. table->port_resource[idx].name = NULL;
  195. table->port_resource[idx].start = 0;
  196. table->port_resource[idx].end = 0;
  197. table->port_resource[idx].flags = IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
  198. }
  199. for (idx = 0; idx < PNP_MAX_MEM; idx++) {
  200. table->mem_resource[idx].name = NULL;
  201. table->mem_resource[idx].start = 0;
  202. table->mem_resource[idx].end = 0;
  203. table->mem_resource[idx].flags = IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
  204. }
  205. }
  206. /**
  207. * pnp_clean_resources - clears resources that were not manually set
  208. * @res: the resources to clean
  209. *
  210. */
  211. static void pnp_clean_resource_table(struct pnp_resource_table * res)
  212. {
  213. int idx;
  214. for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
  215. if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
  216. continue;
  217. res->irq_resource[idx].start = -1;
  218. res->irq_resource[idx].end = -1;
  219. res->irq_resource[idx].flags = IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
  220. }
  221. for (idx = 0; idx < PNP_MAX_DMA; idx++) {
  222. if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
  223. continue;
  224. res->dma_resource[idx].start = -1;
  225. res->dma_resource[idx].end = -1;
  226. res->dma_resource[idx].flags = IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
  227. }
  228. for (idx = 0; idx < PNP_MAX_PORT; idx++) {
  229. if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
  230. continue;
  231. res->port_resource[idx].start = 0;
  232. res->port_resource[idx].end = 0;
  233. res->port_resource[idx].flags = 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 = IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
  241. }
  242. }
  243. /**
  244. * pnp_assign_resources - assigns resources to the device based on the specified dependent number
  245. * @dev: pointer to the desired device
  246. * @depnum: the dependent function number
  247. *
  248. * Only set depnum to 0 if the device does not have dependent options.
  249. */
  250. static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
  251. {
  252. struct pnp_port *port;
  253. struct pnp_mem *mem;
  254. struct pnp_irq *irq;
  255. struct pnp_dma *dma;
  256. int nport = 0, nmem = 0, nirq = 0, ndma = 0;
  257. if (!pnp_can_configure(dev))
  258. return -ENODEV;
  259. down(&pnp_res_mutex);
  260. pnp_clean_resource_table(&dev->res); /* start with a fresh slate */
  261. if (dev->independent) {
  262. port = dev->independent->port;
  263. mem = dev->independent->mem;
  264. irq = dev->independent->irq;
  265. dma = dev->independent->dma;
  266. while (port) {
  267. if (!pnp_assign_port(dev, port, nport))
  268. goto fail;
  269. nport++;
  270. port = port->next;
  271. }
  272. while (mem) {
  273. if (!pnp_assign_mem(dev, mem, nmem))
  274. goto fail;
  275. nmem++;
  276. mem = mem->next;
  277. }
  278. while (irq) {
  279. if (!pnp_assign_irq(dev, irq, nirq))
  280. goto fail;
  281. nirq++;
  282. irq = irq->next;
  283. }
  284. while (dma) {
  285. if (!pnp_assign_dma(dev, dma, ndma))
  286. goto fail;
  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; i++, dep=dep->next)
  295. if(!dep)
  296. goto fail;
  297. port =dep->port;
  298. mem = dep->mem;
  299. irq = dep->irq;
  300. dma = dep->dma;
  301. while (port) {
  302. if (!pnp_assign_port(dev, port, nport))
  303. goto fail;
  304. nport++;
  305. port = port->next;
  306. }
  307. while (mem) {
  308. if (!pnp_assign_mem(dev, mem, nmem))
  309. goto fail;
  310. nmem++;
  311. mem = mem->next;
  312. }
  313. while (irq) {
  314. if (!pnp_assign_irq(dev, irq, nirq))
  315. goto fail;
  316. nirq++;
  317. irq = irq->next;
  318. }
  319. while (dma) {
  320. if (!pnp_assign_dma(dev, dma, ndma))
  321. goto fail;
  322. ndma++;
  323. dma = dma->next;
  324. }
  325. } else if (dev->dependent)
  326. goto fail;
  327. up(&pnp_res_mutex);
  328. return 1;
  329. fail:
  330. pnp_clean_resource_table(&dev->res);
  331. up(&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, int mode)
  343. {
  344. int i;
  345. struct pnp_resource_table * bak;
  346. if (!dev || !res)
  347. return -EINVAL;
  348. if (!pnp_can_configure(dev))
  349. return -ENODEV;
  350. bak = pnp_alloc(sizeof(struct pnp_resource_table));
  351. if (!bak)
  352. return -ENOMEM;
  353. *bak = dev->res;
  354. down(&pnp_res_mutex);
  355. dev->res = *res;
  356. if (!(mode & PNP_CONFIG_FORCE)) {
  357. for (i = 0; i < PNP_MAX_PORT; i++) {
  358. if(!pnp_check_port(dev,i))
  359. goto fail;
  360. }
  361. for (i = 0; i < PNP_MAX_MEM; i++) {
  362. if(!pnp_check_mem(dev,i))
  363. goto fail;
  364. }
  365. for (i = 0; i < PNP_MAX_IRQ; i++) {
  366. if(!pnp_check_irq(dev,i))
  367. goto fail;
  368. }
  369. for (i = 0; i < PNP_MAX_DMA; i++) {
  370. if(!pnp_check_dma(dev,i))
  371. goto fail;
  372. }
  373. }
  374. up(&pnp_res_mutex);
  375. kfree(bak);
  376. return 0;
  377. fail:
  378. dev->res = *bak;
  379. up(&pnp_res_mutex);
  380. kfree(bak);
  381. return -EINVAL;
  382. }
  383. /**
  384. * pnp_auto_config_dev - automatically assigns resources to a device
  385. * @dev: pointer to the desired device
  386. *
  387. */
  388. int pnp_auto_config_dev(struct pnp_dev *dev)
  389. {
  390. struct pnp_option *dep;
  391. int i = 1;
  392. if(!dev)
  393. return -EINVAL;
  394. if(!pnp_can_configure(dev)) {
  395. pnp_info("Device %s does not support resource configuration.", dev->dev.bus_id);
  396. return -ENODEV;
  397. }
  398. if (!dev->dependent) {
  399. if (pnp_assign_resources(dev, 0))
  400. return 0;
  401. } else {
  402. dep = dev->dependent;
  403. do {
  404. if (pnp_assign_resources(dev, i))
  405. return 0;
  406. dep = dep->next;
  407. i++;
  408. } while (dep);
  409. }
  410. pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
  411. return -EBUSY;
  412. }
  413. /**
  414. * pnp_activate_dev - activates a PnP device for use
  415. * @dev: pointer to the desired device
  416. *
  417. * does not validate or set resources so be careful.
  418. */
  419. int pnp_activate_dev(struct pnp_dev *dev)
  420. {
  421. if (!dev)
  422. return -EINVAL;
  423. if (dev->active) {
  424. return 0; /* the device is already active */
  425. }
  426. /* ensure resources are allocated */
  427. if (pnp_auto_config_dev(dev))
  428. return -EBUSY;
  429. if (!pnp_can_write(dev)) {
  430. pnp_info("Device %s does not supported activation.", dev->dev.bus_id);
  431. return -EINVAL;
  432. }
  433. if (dev->protocol->set(dev, &dev->res)<0) {
  434. pnp_err("Failed to activate device %s.", dev->dev.bus_id);
  435. return -EIO;
  436. }
  437. dev->active = 1;
  438. pnp_info("Device %s activated.", dev->dev.bus_id);
  439. return 1;
  440. }
  441. /**
  442. * pnp_disable_dev - disables device
  443. * @dev: pointer to the desired device
  444. *
  445. * inform the correct pnp protocol so that resources can be used by other devices
  446. */
  447. int pnp_disable_dev(struct pnp_dev *dev)
  448. {
  449. if (!dev)
  450. return -EINVAL;
  451. if (!dev->active) {
  452. return 0; /* the device is already disabled */
  453. }
  454. if (!pnp_can_disable(dev)) {
  455. pnp_info("Device %s does not supported disabling.", dev->dev.bus_id);
  456. return -EINVAL;
  457. }
  458. if (dev->protocol->disable(dev)<0) {
  459. pnp_err("Failed to disable device %s.", dev->dev.bus_id);
  460. return -EIO;
  461. }
  462. dev->active = 0;
  463. pnp_info("Device %s disabled.", dev->dev.bus_id);
  464. /* release the resources so that other devices can use them */
  465. down(&pnp_res_mutex);
  466. pnp_clean_resource_table(&dev->res);
  467. up(&pnp_res_mutex);
  468. return 1;
  469. }
  470. /**
  471. * pnp_resource_change - change one resource
  472. * @resource: pointer to resource to be changed
  473. * @start: start of region
  474. * @size: size of region
  475. *
  476. */
  477. void pnp_resource_change(struct resource *resource, unsigned long start, unsigned long size)
  478. {
  479. if (resource == NULL)
  480. return;
  481. resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
  482. resource->start = start;
  483. resource->end = start + size - 1;
  484. }
  485. EXPORT_SYMBOL(pnp_manual_config_dev);
  486. EXPORT_SYMBOL(pnp_auto_config_dev);
  487. EXPORT_SYMBOL(pnp_activate_dev);
  488. EXPORT_SYMBOL(pnp_disable_dev);
  489. EXPORT_SYMBOL(pnp_resource_change);
  490. EXPORT_SYMBOL(pnp_init_resource_table);