manager.c 14 KB

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