manager.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 <linux/mutex.h>
  15. #include "base.h"
  16. DEFINE_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 (idx >= PNP_MAX_PORT) {
  22. dev_err(&dev->dev, "too many I/O port resources\n");
  23. /* pretend we were successful so at least the manager won't try again */
  24. return 1;
  25. }
  26. start = &dev->res.port_resource[idx].start;
  27. end = &dev->res.port_resource[idx].end;
  28. flags = &dev->res.port_resource[idx].flags;
  29. /* check if this resource has been manually set, if so skip */
  30. if (!(dev->res.port_resource[idx].flags & IORESOURCE_AUTO)) {
  31. dev_dbg(&dev->dev, " io %d already set to %#llx-%#llx "
  32. "flags %#lx\n", idx, (unsigned long long) *start,
  33. (unsigned long long) *end, *flags);
  34. return 1;
  35. }
  36. /* set the initial values */
  37. *flags |= rule->flags | IORESOURCE_IO;
  38. *flags &= ~IORESOURCE_UNSET;
  39. if (!rule->size) {
  40. *flags |= IORESOURCE_DISABLED;
  41. dev_dbg(&dev->dev, " io %d disabled\n", idx);
  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. dev_dbg(&dev->dev, " couldn't assign io %d\n", idx);
  52. return 0;
  53. }
  54. }
  55. dev_dbg(&dev->dev, " assign io %d %#llx-%#llx\n", idx,
  56. (unsigned long long) *start, (unsigned long long) *end);
  57. return 1;
  58. }
  59. static int pnp_assign_mem(struct pnp_dev *dev, struct pnp_mem *rule, int idx)
  60. {
  61. resource_size_t *start, *end;
  62. unsigned long *flags;
  63. if (idx >= PNP_MAX_MEM) {
  64. dev_err(&dev->dev, "too many memory resources\n");
  65. /* pretend we were successful so at least the manager won't try again */
  66. return 1;
  67. }
  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. /* check if this resource has been manually set, if so skip */
  72. if (!(dev->res.mem_resource[idx].flags & IORESOURCE_AUTO)) {
  73. dev_dbg(&dev->dev, " mem %d already set to %#llx-%#llx "
  74. "flags %#lx\n", idx, (unsigned long long) *start,
  75. (unsigned long long) *end, *flags);
  76. return 1;
  77. }
  78. /* set the initial values */
  79. *flags |= rule->flags | IORESOURCE_MEM;
  80. *flags &= ~IORESOURCE_UNSET;
  81. /* convert pnp flags to standard Linux flags */
  82. if (!(rule->flags & IORESOURCE_MEM_WRITEABLE))
  83. *flags |= IORESOURCE_READONLY;
  84. if (rule->flags & IORESOURCE_MEM_CACHEABLE)
  85. *flags |= IORESOURCE_CACHEABLE;
  86. if (rule->flags & IORESOURCE_MEM_RANGELENGTH)
  87. *flags |= IORESOURCE_RANGELENGTH;
  88. if (rule->flags & IORESOURCE_MEM_SHADOWABLE)
  89. *flags |= IORESOURCE_SHADOWABLE;
  90. if (!rule->size) {
  91. *flags |= IORESOURCE_DISABLED;
  92. dev_dbg(&dev->dev, " mem %d disabled\n", idx);
  93. return 1; /* skip disabled resource requests */
  94. }
  95. *start = rule->min;
  96. *end = *start + rule->size - 1;
  97. /* run through until pnp_check_mem is happy */
  98. while (!pnp_check_mem(dev, idx)) {
  99. *start += rule->align;
  100. *end = *start + rule->size - 1;
  101. if (*start > rule->max || !rule->align) {
  102. dev_dbg(&dev->dev, " couldn't assign mem %d\n", idx);
  103. return 0;
  104. }
  105. }
  106. dev_dbg(&dev->dev, " assign mem %d %#llx-%#llx\n", idx,
  107. (unsigned long long) *start, (unsigned long long) *end);
  108. return 1;
  109. }
  110. static int pnp_assign_irq(struct pnp_dev *dev, struct pnp_irq *rule, int idx)
  111. {
  112. resource_size_t *start, *end;
  113. unsigned long *flags;
  114. int i;
  115. /* IRQ priority: this table is good for i386 */
  116. static unsigned short xtab[16] = {
  117. 5, 10, 11, 12, 9, 14, 15, 7, 3, 4, 13, 0, 1, 6, 8, 2
  118. };
  119. if (idx >= PNP_MAX_IRQ) {
  120. dev_err(&dev->dev, "too many IRQ resources\n");
  121. /* pretend we were successful so at least the manager won't try again */
  122. return 1;
  123. }
  124. start = &dev->res.irq_resource[idx].start;
  125. end = &dev->res.irq_resource[idx].end;
  126. flags = &dev->res.irq_resource[idx].flags;
  127. /* check if this resource has been manually set, if so skip */
  128. if (!(dev->res.irq_resource[idx].flags & IORESOURCE_AUTO)) {
  129. dev_dbg(&dev->dev, " irq %d already set to %d flags %#lx\n",
  130. idx, (int) *start, *flags);
  131. return 1;
  132. }
  133. /* set the initial values */
  134. *flags |= rule->flags | IORESOURCE_IRQ;
  135. *flags &= ~IORESOURCE_UNSET;
  136. if (bitmap_empty(rule->map, PNP_IRQ_NR)) {
  137. *flags |= IORESOURCE_DISABLED;
  138. dev_dbg(&dev->dev, " irq %d disabled\n", idx);
  139. return 1; /* skip disabled resource requests */
  140. }
  141. /* TBD: need check for >16 IRQ */
  142. *start = find_next_bit(rule->map, PNP_IRQ_NR, 16);
  143. if (*start < PNP_IRQ_NR) {
  144. *end = *start;
  145. dev_dbg(&dev->dev, " assign irq %d %d\n", idx, (int) *start);
  146. return 1;
  147. }
  148. for (i = 0; i < 16; i++) {
  149. if (test_bit(xtab[i], rule->map)) {
  150. *start = *end = xtab[i];
  151. if (pnp_check_irq(dev, idx)) {
  152. dev_dbg(&dev->dev, " assign irq %d %d\n", idx,
  153. (int) *start);
  154. return 1;
  155. }
  156. }
  157. }
  158. dev_dbg(&dev->dev, " couldn't assign irq %d\n", idx);
  159. return 0;
  160. }
  161. static void pnp_assign_dma(struct pnp_dev *dev, struct pnp_dma *rule, int idx)
  162. {
  163. resource_size_t *start, *end;
  164. unsigned long *flags;
  165. int i;
  166. /* DMA priority: this table is good for i386 */
  167. static unsigned short xtab[8] = {
  168. 1, 3, 5, 6, 7, 0, 2, 4
  169. };
  170. if (idx >= PNP_MAX_DMA) {
  171. dev_err(&dev->dev, "too many DMA resources\n");
  172. return;
  173. }
  174. start = &dev->res.dma_resource[idx].start;
  175. end = &dev->res.dma_resource[idx].end;
  176. flags = &dev->res.dma_resource[idx].flags;
  177. /* check if this resource has been manually set, if so skip */
  178. if (!(dev->res.dma_resource[idx].flags & IORESOURCE_AUTO)) {
  179. dev_dbg(&dev->dev, " dma %d already set to %d flags %#lx\n",
  180. idx, (int) *start, *flags);
  181. return;
  182. }
  183. /* set the initial values */
  184. *flags |= rule->flags | IORESOURCE_DMA;
  185. *flags &= ~IORESOURCE_UNSET;
  186. for (i = 0; i < 8; i++) {
  187. if (rule->map & (1 << xtab[i])) {
  188. *start = *end = xtab[i];
  189. if (pnp_check_dma(dev, idx)) {
  190. dev_dbg(&dev->dev, " assign dma %d %d\n", idx,
  191. (int) *start);
  192. return;
  193. }
  194. }
  195. }
  196. #ifdef MAX_DMA_CHANNELS
  197. *start = *end = MAX_DMA_CHANNELS;
  198. #endif
  199. *flags |= IORESOURCE_UNSET | IORESOURCE_DISABLED;
  200. dev_dbg(&dev->dev, " disable dma %d\n", idx);
  201. }
  202. /**
  203. * pnp_init_resources - Resets a resource table to default values.
  204. * @table: pointer to the desired resource table
  205. */
  206. void pnp_init_resources(struct pnp_dev *dev)
  207. {
  208. struct pnp_resource_table *table = &dev->res;
  209. int idx;
  210. for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
  211. table->irq_resource[idx].name = NULL;
  212. table->irq_resource[idx].start = -1;
  213. table->irq_resource[idx].end = -1;
  214. table->irq_resource[idx].flags =
  215. IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
  216. }
  217. for (idx = 0; idx < PNP_MAX_DMA; idx++) {
  218. table->dma_resource[idx].name = NULL;
  219. table->dma_resource[idx].start = -1;
  220. table->dma_resource[idx].end = -1;
  221. table->dma_resource[idx].flags =
  222. IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
  223. }
  224. for (idx = 0; idx < PNP_MAX_PORT; idx++) {
  225. table->port_resource[idx].name = NULL;
  226. table->port_resource[idx].start = 0;
  227. table->port_resource[idx].end = 0;
  228. table->port_resource[idx].flags =
  229. IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
  230. }
  231. for (idx = 0; idx < PNP_MAX_MEM; idx++) {
  232. table->mem_resource[idx].name = NULL;
  233. table->mem_resource[idx].start = 0;
  234. table->mem_resource[idx].end = 0;
  235. table->mem_resource[idx].flags =
  236. IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
  237. }
  238. }
  239. /**
  240. * pnp_clean_resources - clears resources that were not manually set
  241. * @res: the resources to clean
  242. */
  243. static void pnp_clean_resource_table(struct pnp_dev *dev)
  244. {
  245. struct pnp_resource_table *res = &dev->res;
  246. int idx;
  247. for (idx = 0; idx < PNP_MAX_IRQ; idx++) {
  248. if (!(res->irq_resource[idx].flags & IORESOURCE_AUTO))
  249. continue;
  250. res->irq_resource[idx].start = -1;
  251. res->irq_resource[idx].end = -1;
  252. res->irq_resource[idx].flags =
  253. IORESOURCE_IRQ | IORESOURCE_AUTO | IORESOURCE_UNSET;
  254. }
  255. for (idx = 0; idx < PNP_MAX_DMA; idx++) {
  256. if (!(res->dma_resource[idx].flags & IORESOURCE_AUTO))
  257. continue;
  258. res->dma_resource[idx].start = -1;
  259. res->dma_resource[idx].end = -1;
  260. res->dma_resource[idx].flags =
  261. IORESOURCE_DMA | IORESOURCE_AUTO | IORESOURCE_UNSET;
  262. }
  263. for (idx = 0; idx < PNP_MAX_PORT; idx++) {
  264. if (!(res->port_resource[idx].flags & IORESOURCE_AUTO))
  265. continue;
  266. res->port_resource[idx].start = 0;
  267. res->port_resource[idx].end = 0;
  268. res->port_resource[idx].flags =
  269. IORESOURCE_IO | IORESOURCE_AUTO | IORESOURCE_UNSET;
  270. }
  271. for (idx = 0; idx < PNP_MAX_MEM; idx++) {
  272. if (!(res->mem_resource[idx].flags & IORESOURCE_AUTO))
  273. continue;
  274. res->mem_resource[idx].start = 0;
  275. res->mem_resource[idx].end = 0;
  276. res->mem_resource[idx].flags =
  277. IORESOURCE_MEM | IORESOURCE_AUTO | IORESOURCE_UNSET;
  278. }
  279. }
  280. /**
  281. * pnp_assign_resources - assigns resources to the device based on the specified dependent number
  282. * @dev: pointer to the desired device
  283. * @depnum: the dependent function number
  284. *
  285. * Only set depnum to 0 if the device does not have dependent options.
  286. */
  287. static int pnp_assign_resources(struct pnp_dev *dev, int depnum)
  288. {
  289. struct pnp_port *port;
  290. struct pnp_mem *mem;
  291. struct pnp_irq *irq;
  292. struct pnp_dma *dma;
  293. int nport = 0, nmem = 0, nirq = 0, ndma = 0;
  294. if (!pnp_can_configure(dev))
  295. return -ENODEV;
  296. dbg_pnp_show_resources(dev, "before pnp_assign_resources");
  297. mutex_lock(&pnp_res_mutex);
  298. pnp_clean_resource_table(dev);
  299. if (dev->independent) {
  300. dev_dbg(&dev->dev, "assigning independent options\n");
  301. port = dev->independent->port;
  302. mem = dev->independent->mem;
  303. irq = dev->independent->irq;
  304. dma = dev->independent->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. }
  329. if (depnum) {
  330. struct pnp_option *dep;
  331. int i;
  332. dev_dbg(&dev->dev, "assigning dependent option %d\n", depnum);
  333. for (i = 1, dep = dev->dependent; i < depnum;
  334. i++, dep = dep->next)
  335. if (!dep)
  336. goto fail;
  337. port = dep->port;
  338. mem = dep->mem;
  339. irq = dep->irq;
  340. dma = dep->dma;
  341. while (port) {
  342. if (!pnp_assign_port(dev, port, nport))
  343. goto fail;
  344. nport++;
  345. port = port->next;
  346. }
  347. while (mem) {
  348. if (!pnp_assign_mem(dev, mem, nmem))
  349. goto fail;
  350. nmem++;
  351. mem = mem->next;
  352. }
  353. while (irq) {
  354. if (!pnp_assign_irq(dev, irq, nirq))
  355. goto fail;
  356. nirq++;
  357. irq = irq->next;
  358. }
  359. while (dma) {
  360. pnp_assign_dma(dev, dma, ndma);
  361. ndma++;
  362. dma = dma->next;
  363. }
  364. } else if (dev->dependent)
  365. goto fail;
  366. mutex_unlock(&pnp_res_mutex);
  367. dbg_pnp_show_resources(dev, "after pnp_assign_resources");
  368. return 1;
  369. fail:
  370. pnp_clean_resource_table(dev);
  371. mutex_unlock(&pnp_res_mutex);
  372. dbg_pnp_show_resources(dev, "after pnp_assign_resources (failed)");
  373. return 0;
  374. }
  375. /**
  376. * pnp_auto_config_dev - automatically assigns resources to a device
  377. * @dev: pointer to the desired device
  378. */
  379. int pnp_auto_config_dev(struct pnp_dev *dev)
  380. {
  381. struct pnp_option *dep;
  382. int i = 1;
  383. if (!pnp_can_configure(dev)) {
  384. dev_dbg(&dev->dev, "configuration not supported\n");
  385. return -ENODEV;
  386. }
  387. if (!dev->dependent) {
  388. if (pnp_assign_resources(dev, 0))
  389. return 0;
  390. } else {
  391. dep = dev->dependent;
  392. do {
  393. if (pnp_assign_resources(dev, i))
  394. return 0;
  395. dep = dep->next;
  396. i++;
  397. } while (dep);
  398. }
  399. dev_err(&dev->dev, "unable to assign resources\n");
  400. return -EBUSY;
  401. }
  402. /**
  403. * pnp_start_dev - low-level start of the PnP device
  404. * @dev: pointer to the desired device
  405. *
  406. * assumes that resources have already been allocated
  407. */
  408. int pnp_start_dev(struct pnp_dev *dev)
  409. {
  410. if (!pnp_can_write(dev)) {
  411. dev_dbg(&dev->dev, "activation not supported\n");
  412. return -EINVAL;
  413. }
  414. dbg_pnp_show_resources(dev, "pnp_start_dev");
  415. if (dev->protocol->set(dev) < 0) {
  416. dev_err(&dev->dev, "activation failed\n");
  417. return -EIO;
  418. }
  419. dev_info(&dev->dev, "activated\n");
  420. return 0;
  421. }
  422. /**
  423. * pnp_stop_dev - low-level disable of the PnP device
  424. * @dev: pointer to the desired device
  425. *
  426. * does not free resources
  427. */
  428. int pnp_stop_dev(struct pnp_dev *dev)
  429. {
  430. if (!pnp_can_disable(dev)) {
  431. dev_dbg(&dev->dev, "disabling not supported\n");
  432. return -EINVAL;
  433. }
  434. if (dev->protocol->disable(dev) < 0) {
  435. dev_err(&dev->dev, "disable failed\n");
  436. return -EIO;
  437. }
  438. dev_info(&dev->dev, "disabled\n");
  439. return 0;
  440. }
  441. /**
  442. * pnp_activate_dev - activates a PnP device for use
  443. * @dev: pointer to the desired device
  444. *
  445. * does not validate or set resources so be careful.
  446. */
  447. int pnp_activate_dev(struct pnp_dev *dev)
  448. {
  449. int error;
  450. if (dev->active)
  451. return 0;
  452. /* ensure resources are allocated */
  453. if (pnp_auto_config_dev(dev))
  454. return -EBUSY;
  455. error = pnp_start_dev(dev);
  456. if (error)
  457. return error;
  458. dev->active = 1;
  459. return 0;
  460. }
  461. /**
  462. * pnp_disable_dev - disables device
  463. * @dev: pointer to the desired device
  464. *
  465. * inform the correct pnp protocol so that resources can be used by other devices
  466. */
  467. int pnp_disable_dev(struct pnp_dev *dev)
  468. {
  469. int error;
  470. if (!dev->active)
  471. return 0;
  472. error = pnp_stop_dev(dev);
  473. if (error)
  474. return error;
  475. dev->active = 0;
  476. /* release the resources so that other devices can use them */
  477. mutex_lock(&pnp_res_mutex);
  478. pnp_clean_resource_table(dev);
  479. mutex_unlock(&pnp_res_mutex);
  480. return 0;
  481. }
  482. EXPORT_SYMBOL(pnp_start_dev);
  483. EXPORT_SYMBOL(pnp_stop_dev);
  484. EXPORT_SYMBOL(pnp_activate_dev);
  485. EXPORT_SYMBOL(pnp_disable_dev);