manager.c 14 KB

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