manager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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@perex.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 void 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("More than 2 dmas is incompatible with pnp "
  147. "specifications.");
  148. return;
  149. }
  150. /* check if this resource has been manually set, if so skip */
  151. if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO))
  152. return;
  153. start = &dev->res.dma_resource[idx].start;
  154. end = &dev->res.dma_resource[idx].end;
  155. flags = &dev->res.dma_resource[idx].flags;
  156. /* set the initial values */
  157. *flags |= rule->flags | IORESOURCE_DMA;
  158. *flags &= ~IORESOURCE_UNSET;
  159. for (i = 0; i < 8; i++) {
  160. if (rule->map & (1 << xtab[i])) {
  161. *start = *end = xtab[i];
  162. if (pnp_check_dma(dev, idx))
  163. return;
  164. }
  165. }
  166. #ifdef MAX_DMA_CHANNELS
  167. *start = *end = MAX_DMA_CHANNELS;
  168. #endif
  169. *flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
  170. }
  171. /**
  172. * pnp_init_resources - Resets a resource table to default values.
  173. * @table: pointer to the desired resource table
  174. */
  175. void pnp_init_resource_table(struct pnp_resource_table *table)
  176. {
  177. int idx;
  178. for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
  179. table->irq_resource[idx].name = NULL;
  180. table->irq_resource[idx].start = -1;
  181. table->irq_resource[idx].end = -1;
  182. table->irq_resource[idx].flags =
  183. IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
  184. }
  185. for (idx = 0; idx < PNP_MAX_DMA; idx++) {
  186. table->dma_resource[idx].name = NULL;
  187. table->dma_resource[idx].start = -1;
  188. table->dma_resource[idx].end = -1;
  189. table->dma_resource[idx].flags =
  190. IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
  191. }
  192. for (idx = 0; idx < PNP_MAX_PORT; idx++) {
  193. table->port_resource[idx].name = NULL;
  194. table->port_resource[idx].start = 0;
  195. table->port_resource[idx].end = 0;
  196. table->port_resource[idx].flags =
  197. 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 =
  204. IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
  205. }
  206. }
  207. /**
  208. * pnp_clean_resources - clears resources that were not manually set
  209. * @res: the resources to clean
  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 =
  220. IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
  221. }
  222. for (idx = 0; idx < PNP_MAX_DMA; idx++) {
  223. if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
  224. continue;
  225. res->dma_resource[idx].start = -1;
  226. res->dma_resource[idx].end = -1;
  227. res->dma_resource[idx].flags =
  228. 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 =
  236. IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
  237. }
  238. for (idx = 0; idx < PNP_MAX_MEM; idx++) {
  239. if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
  240. continue;
  241. res->mem_resource[idx].start = 0;
  242. res->mem_resource[idx].end = 0;
  243. res->mem_resource[idx].flags =
  244. IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
  245. }
  246. }
  247. /**
  248. * pnp_assign_resources - assigns resources to the device based on the specified dependent number
  249. * @dev: pointer to the desired device
  250. * @depnum: the dependent function number
  251. *
  252. * Only set depnum to 0 if the device does not have dependent options.
  253. */
  254. static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
  255. {
  256. struct pnp_port *port;
  257. struct pnp_mem *mem;
  258. struct pnp_irq *irq;
  259. struct pnp_dma *dma;
  260. int nport = 0, nmem = 0, nirq = 0, ndma = 0;
  261. if (!pnp_can_configure(dev))
  262. return -ENODEV;
  263. down(&pnp_res_mutex);
  264. pnp_clean_resource_table(&dev->res); /* start with a fresh slate */
  265. if (dev->independent) {
  266. port = dev->independent->port;
  267. mem = dev->independent->mem;
  268. irq = dev->independent->irq;
  269. dma = dev->independent->dma;
  270. while (port) {
  271. if (!pnp_assign_port(dev, port, nport))
  272. goto fail;
  273. nport++;
  274. port = port->next;
  275. }
  276. while (mem) {
  277. if (!pnp_assign_mem(dev, mem, nmem))
  278. goto fail;
  279. nmem++;
  280. mem = mem->next;
  281. }
  282. while (irq) {
  283. if (!pnp_assign_irq(dev, irq, nirq))
  284. goto fail;
  285. nirq++;
  286. irq = irq->next;
  287. }
  288. while (dma) {
  289. pnp_assign_dma(dev, dma, ndma);
  290. ndma++;
  291. dma = dma->next;
  292. }
  293. }
  294. if (depnum) {
  295. struct pnp_option *dep;
  296. int i;
  297. for (i = 1, dep = dev->dependent; i < depnum;
  298. i++, dep = dep->next)
  299. if (!dep)
  300. goto fail;
  301. port = dep->port;
  302. mem = dep->mem;
  303. irq = dep->irq;
  304. dma = dep->dma;
  305. while (port) {
  306. if (!pnp_assign_port(dev, port, nport))
  307. goto fail;
  308. nport++;
  309. port = port->next;
  310. }
  311. while (mem) {
  312. if (!pnp_assign_mem(dev, mem, nmem))
  313. goto fail;
  314. nmem++;
  315. mem = mem->next;
  316. }
  317. while (irq) {
  318. if (!pnp_assign_irq(dev, irq, nirq))
  319. goto fail;
  320. nirq++;
  321. irq = irq->next;
  322. }
  323. while (dma) {
  324. pnp_assign_dma(dev, dma, ndma);
  325. ndma++;
  326. dma = dma->next;
  327. }
  328. } else if (dev->dependent)
  329. goto fail;
  330. up(&pnp_res_mutex);
  331. return 1;
  332. fail:
  333. pnp_clean_resource_table(&dev->res);
  334. up(&pnp_res_mutex);
  335. return 0;
  336. }
  337. /**
  338. * pnp_manual_config_dev - Disables Auto Config and Manually sets the resource table
  339. * @dev: pointer to the desired device
  340. * @res: pointer to the new resource config
  341. * @mode: 0 or PNP_CONFIG_FORCE
  342. *
  343. * This function can be used by drivers that want to manually set thier resources.
  344. */
  345. int pnp_manual_config_dev(struct pnp_dev *dev, struct pnp_resource_table *res,
  346. int mode)
  347. {
  348. int i;
  349. struct pnp_resource_table *bak;
  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. int pnp_auto_config_dev(struct pnp_dev *dev)
  390. {
  391. struct pnp_option *dep;
  392. int i = 1;
  393. if (!pnp_can_configure(dev)) {
  394. pnp_dbg("Device %s does not support resource configuration.",
  395. 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_start_dev - low-level start of the PnP device
  415. * @dev: pointer to the desired device
  416. *
  417. * assumes that resources have already been allocated
  418. */
  419. int pnp_start_dev(struct pnp_dev *dev)
  420. {
  421. if (!pnp_can_write(dev)) {
  422. pnp_dbg("Device %s does not support activation.",
  423. dev->dev.bus_id);
  424. return -EINVAL;
  425. }
  426. if (dev->protocol->set(dev, &dev->res) < 0) {
  427. pnp_err("Failed to activate device %s.", dev->dev.bus_id);
  428. return -EIO;
  429. }
  430. pnp_info("Device %s activated.", dev->dev.bus_id);
  431. return 0;
  432. }
  433. /**
  434. * pnp_stop_dev - low-level disable of the PnP device
  435. * @dev: pointer to the desired device
  436. *
  437. * does not free resources
  438. */
  439. int pnp_stop_dev(struct pnp_dev *dev)
  440. {
  441. if (!pnp_can_disable(dev)) {
  442. pnp_dbg("Device %s does not support disabling.",
  443. 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->active)
  463. return 0; /* the device is already active */
  464. /* ensure resources are allocated */
  465. if (pnp_auto_config_dev(dev))
  466. return -EBUSY;
  467. error = pnp_start_dev(dev);
  468. if (error)
  469. return error;
  470. dev->active = 1;
  471. return 1;
  472. }
  473. /**
  474. * pnp_disable_dev - disables device
  475. * @dev: pointer to the desired device
  476. *
  477. * inform the correct pnp protocol so that resources can be used by other devices
  478. */
  479. int pnp_disable_dev(struct pnp_dev *dev)
  480. {
  481. int error;
  482. if (!dev->active)
  483. return 0; /* the device is already disabled */
  484. error = pnp_stop_dev(dev);
  485. if (error)
  486. return error;
  487. dev->active = 0;
  488. /* release the resources so that other devices can use them */
  489. down(&pnp_res_mutex);
  490. pnp_clean_resource_table(&dev->res);
  491. up(&pnp_res_mutex);
  492. return 1;
  493. }
  494. /**
  495. * pnp_resource_change - change one resource
  496. * @resource: pointer to resource to be changed
  497. * @start: start of region
  498. * @size: size of region
  499. */
  500. void pnp_resource_change(struct resource *resource, resource_size_t start,
  501. resource_size_t size)
  502. {
  503. resource->flags &= ~(IORESOURCE_AUTO | IORESOURCE_UNSET);
  504. resource->start = start;
  505. resource->end = start + size - 1;
  506. }
  507. EXPORT_SYMBOL(pnp_manual_config_dev);
  508. EXPORT_SYMBOL(pnp_start_dev);
  509. EXPORT_SYMBOL(pnp_stop_dev);
  510. EXPORT_SYMBOL(pnp_activate_dev);
  511. EXPORT_SYMBOL(pnp_disable_dev);
  512. EXPORT_SYMBOL(pnp_resource_change);
  513. EXPORT_SYMBOL(pnp_init_resource_table);