manager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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/errno.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/pnp.h>
  13. #include <linux/slab.h>
  14. #include <linux/bitmap.h>
  15. #include "base.h"
  16. DECLARE_MUTEX(pnp_res_mutex);
  17. static int pnp_assign_port(struct pnp_dev *dev, struct pnp_port *rule, int idx)
  18. {
  19. resource_size_t *start, *end;
  20. unsigned long *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. 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("More than 8 mems is incompatible with pnp specifications.");
  60. /* pretend we were successful so at least the manager won't try again */
  61. return 1;
  62. }
  63. /* check if this resource has been manually set, if so skip */
  64. if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO))
  65. return 1;
  66. start = &dev->res.mem_resource[idx].start;
  67. end = &dev->res.mem_resource[idx].end;
  68. flags = &dev->res.mem_resource[idx].flags;
  69. /* set the initial values */
  70. *flags |= rule->flags | IORESOURCE_MEM;
  71. *flags &= ~IORESOURCE_UNSET;
  72. /* convert pnp flags to standard Linux flags */
  73. if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
  74. *flags |= IORESOURCE_READONLY;
  75. if (rule->flags & IORESOURCE_MEM_CACHEABLE)
  76. *flags |= IORESOURCE_CACHEABLE;
  77. if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
  78. *flags |= IORESOURCE_RANGELENGTH;
  79. if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
  80. *flags |= IORESOURCE_SHADOWABLE;
  81. if (!rule->size) {
  82. *flags |= IORESOURCE_DISABLED;
  83. return 1; /* skip disabled resource requests */
  84. }
  85. *start = rule->min;
  86. *end = *start + rule->size -1;
  87. /* run through until pnp_check_mem is happy */
  88. while (!pnp_check_mem(dev, idx)) {
  89. *start += rule->align;
  90. *end = *start + rule->size - 1;
  91. if (*start > rule->max || !rule->align)
  92. return 0;
  93. }
  94. return 1;
  95. }
  96. static int pnp_assign_irq(struct pnp_dev * dev, struct pnp_irq *rule, int idx)
  97. {
  98. resource_size_t *start, *end;
  99. unsigned long *flags;
  100. int i;
  101. /* IRQ priority: this table is good for i386 */
  102. static unsigned short xtab[16] = {
  103. 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
  104. };
  105. if (!dev || !rule)
  106. return -EINVAL;
  107. if (idx >= PNP_MAX_IRQ) {
  108. pnp_err("More than 2 irqs is incompatible with pnp specifications.");
  109. /* pretend we were successful so at least the manager won't try again */
  110. return 1;
  111. }
  112. /* check if this resource has been manually set, if so skip */
  113. if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO))
  114. return 1;
  115. start = &dev->res.irq_resource[idx].start;
  116. end = &dev->res.irq_resource[idx].end;
  117. flags = &dev->res.irq_resource[idx].flags;
  118. /* set the initial values */
  119. *flags |= rule->flags | IORESOURCE_IRQ;
  120. *flags &= ~IORESOURCE_UNSET;
  121. if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
  122. *flags |= IORESOURCE_DISABLED;
  123. return 1; /* skip disabled resource requests */
  124. }
  125. /* TBD: need check for >16 IRQ */
  126. *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
  127. if (*start < PNP_IRQ_NR) {
  128. *end = *start;
  129. return 1;
  130. }
  131. for (i = 0; i < 16; i++) {
  132. if(test_bit(xtab[i], rule->map)) {
  133. *start = *end = xtab[i];
  134. if(pnp_check_irq(dev, idx))
  135. return 1;
  136. }
  137. }
  138. return 0;
  139. }
  140. static int pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
  141. {
  142. resource_size_t *start, *end;
  143. unsigned long *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. * @mode: 0 or PNP_CONFIG_FORCE
  344. *
  345. * This function can be used by drivers that want to manually set thier resources.
  346. */
  347. int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table * res, int mode)
  348. {
  349. int i;
  350. struct pnp_resource_table * bak;
  351. if (!dev || !res)
  352. return -EINVAL;
  353. if (!pnp_can_configure(dev))
  354. return -ENODEV;
  355. bak = pnp_alloc(sizeof(struct pnp_resource_table));
  356. if (!bak)
  357. return -ENOMEM;
  358. *bak = dev->res;
  359. down(&pnp_res_mutex);
  360. dev->res = *res;
  361. if (!(mode & PNP_CONFIG_FORCE)) {
  362. for (i = 0; i < PNP_MAX_PORT; i++) {
  363. if(!pnp_check_port(dev,i))
  364. goto fail;
  365. }
  366. for (i = 0; i < PNP_MAX_MEM; i++) {
  367. if(!pnp_check_mem(dev,i))
  368. goto fail;
  369. }
  370. for (i = 0; i < PNP_MAX_IRQ; i++) {
  371. if(!pnp_check_irq(dev,i))
  372. goto fail;
  373. }
  374. for (i = 0; i < PNP_MAX_DMA; i++) {
  375. if(!pnp_check_dma(dev,i))
  376. goto fail;
  377. }
  378. }
  379. up(&pnp_res_mutex);
  380. kfree(bak);
  381. return 0;
  382. fail:
  383. dev->res = *bak;
  384. up(&pnp_res_mutex);
  385. kfree(bak);
  386. return -EINVAL;
  387. }
  388. /**
  389. * pnp_auto_config_dev - automatically assigns resources to a device
  390. * @dev: pointer to the desired device
  391. *
  392. */
  393. int pnp_auto_config_dev(struct pnp_dev *dev)
  394. {
  395. struct pnp_option *dep;
  396. int i = 1;
  397. if(!dev)
  398. return -EINVAL;
  399. if(!pnp_can_configure(dev)) {
  400. pnp_info("Device %s does not support resource configuration.", dev->dev.bus_id);
  401. return -ENODEV;
  402. }
  403. if (!dev->dependent) {
  404. if (pnp_assign_resources(dev, 0))
  405. return 0;
  406. } else {
  407. dep = dev->dependent;
  408. do {
  409. if (pnp_assign_resources(dev, i))
  410. return 0;
  411. dep = dep->next;
  412. i++;
  413. } while (dep);
  414. }
  415. pnp_err("Unable to assign resources to device %s.", dev->dev.bus_id);
  416. return -EBUSY;
  417. }
  418. /**
  419. * pnp_start_dev - low-level start of the PnP device
  420. * @dev: pointer to the desired device
  421. *
  422. * assumes that resources have alread been allocated
  423. */
  424. int pnp_start_dev(struct pnp_dev *dev)
  425. {
  426. if (!pnp_can_write(dev)) {
  427. pnp_info("Device %s does not support activation.", 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_info("Device %s does not support disabling.", dev->dev.bus_id);
  447. return -EINVAL;
  448. }
  449. if (dev->protocol->disable(dev)<0) {
  450. pnp_err("Failed to disable device %s.", dev->dev.bus_id);
  451. return -EIO;
  452. }
  453. pnp_info("Device %s disabled.", dev->dev.bus_id);
  454. return 0;
  455. }
  456. /**
  457. * pnp_activate_dev - activates a PnP device for use
  458. * @dev: pointer to the desired device
  459. *
  460. * does not validate or set resources so be careful.
  461. */
  462. int pnp_activate_dev(struct pnp_dev *dev)
  463. {
  464. int error;
  465. if (!dev)
  466. return -EINVAL;
  467. if (dev->active) {
  468. return 0; /* the device is already active */
  469. }
  470. /* ensure resources are allocated */
  471. if (pnp_auto_config_dev(dev))
  472. return -EBUSY;
  473. error = pnp_start_dev(dev);
  474. if (error)
  475. return error;
  476. dev->active = 1;
  477. return 1;
  478. }
  479. /**
  480. * pnp_disable_dev - disables device
  481. * @dev: pointer to the desired device
  482. *
  483. * inform the correct pnp protocol so that resources can be used by other devices
  484. */
  485. int pnp_disable_dev(struct pnp_dev *dev)
  486. {
  487. int error;
  488. if (!dev)
  489. return -EINVAL;
  490. if (!dev->active) {
  491. return 0; /* the device is already disabled */
  492. }
  493. error = pnp_stop_dev(dev);
  494. if (error)
  495. return error;
  496. dev->active = 0;
  497. /* release the resources so that other devices can use them */
  498. down(&pnp_res_mutex);
  499. pnp_clean_resource_table(&dev->res);
  500. up(&pnp_res_mutex);
  501. return 1;
  502. }
  503. /**
  504. * pnp_resource_change - change one resource
  505. * @resource: pointer to resource to be changed
  506. * @start: start of region
  507. * @size: size of region
  508. *
  509. */
  510. void pnp_resource_change(struct resource *resource, resource_size_t start,
  511. resource_size_t size)
  512. {
  513. if (resource == NULL)
  514. return;
  515. resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
  516. resource->start = start;
  517. resource->end = start + size - 1;
  518. }
  519. EXPORT_SYMBOL(pnp_manual_config_dev);
  520. #if 0
  521. EXPORT_SYMBOL(pnp_auto_config_dev);
  522. #endif
  523. EXPORT_SYMBOL(pnp_start_dev);
  524. EXPORT_SYMBOL(pnp_stop_dev);
  525. EXPORT_SYMBOL(pnp_activate_dev);
  526. EXPORT_SYMBOL(pnp_disable_dev);
  527. EXPORT_SYMBOL(pnp_resource_change);
  528. EXPORT_SYMBOL(pnp_init_resource_table);