resource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * resource.c - Contains functions for registering and analyzing resource information
  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. #include <linux/module.h>
  8. #include <linux/errno.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/kernel.h>
  11. #include <asm/io.h>
  12. #include <asm/dma.h>
  13. #include <asm/irq.h>
  14. #include <linux/pci.h>
  15. #include <linux/ioport.h>
  16. #include <linux/init.h>
  17. #include <linux/pnp.h>
  18. #include "base.h"
  19. static int pnp_reserve_irq[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
  20. static int pnp_reserve_dma[8] = {[0 ... 7] = -1 }; /* reserve (don't use) some DMA */
  21. static int pnp_reserve_io[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
  22. static int pnp_reserve_mem[16] = {[0 ... 15] = -1 }; /* reserve (don't use) some memory region */
  23. /*
  24. * option registration
  25. */
  26. static struct pnp_option *pnp_build_option(int priority)
  27. {
  28. struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
  29. if (!option)
  30. return NULL;
  31. option->priority = priority & 0xff;
  32. /* make sure the priority is valid */
  33. if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
  34. option->priority = PNP_RES_PRIORITY_INVALID;
  35. return option;
  36. }
  37. struct pnp_option *pnp_register_independent_option(struct pnp_dev *dev)
  38. {
  39. struct pnp_option *option;
  40. if (!dev)
  41. return NULL;
  42. option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
  43. /* this should never happen but if it does we'll try to continue */
  44. if (dev->independent)
  45. pnp_err("independent resource already registered");
  46. dev->independent = option;
  47. return option;
  48. }
  49. struct pnp_option *pnp_register_dependent_option(struct pnp_dev *dev,
  50. int priority)
  51. {
  52. struct pnp_option *option;
  53. if (!dev)
  54. return NULL;
  55. option = pnp_build_option(priority);
  56. if (dev->dependent) {
  57. struct pnp_option *parent = dev->dependent;
  58. while (parent->next)
  59. parent = parent->next;
  60. parent->next = option;
  61. } else
  62. dev->dependent = option;
  63. return option;
  64. }
  65. int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data)
  66. {
  67. struct pnp_irq *ptr;
  68. if (!option)
  69. return -EINVAL;
  70. if (!data)
  71. return -EINVAL;
  72. ptr = option->irq;
  73. while (ptr && ptr->next)
  74. ptr = ptr->next;
  75. if (ptr)
  76. ptr->next = data;
  77. else
  78. option->irq = data;
  79. #ifdef CONFIG_PCI
  80. {
  81. int i;
  82. for (i = 0; i < 16; i++)
  83. if (test_bit(i, data->map))
  84. pcibios_penalize_isa_irq(i, 0);
  85. }
  86. #endif
  87. return 0;
  88. }
  89. int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data)
  90. {
  91. struct pnp_dma *ptr;
  92. if (!option)
  93. return -EINVAL;
  94. if (!data)
  95. return -EINVAL;
  96. ptr = option->dma;
  97. while (ptr && ptr->next)
  98. ptr = ptr->next;
  99. if (ptr)
  100. ptr->next = data;
  101. else
  102. option->dma = data;
  103. return 0;
  104. }
  105. int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data)
  106. {
  107. struct pnp_port *ptr;
  108. if (!option)
  109. return -EINVAL;
  110. if (!data)
  111. return -EINVAL;
  112. ptr = option->port;
  113. while (ptr && ptr->next)
  114. ptr = ptr->next;
  115. if (ptr)
  116. ptr->next = data;
  117. else
  118. option->port = data;
  119. return 0;
  120. }
  121. int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data)
  122. {
  123. struct pnp_mem *ptr;
  124. if (!option)
  125. return -EINVAL;
  126. if (!data)
  127. return -EINVAL;
  128. ptr = option->mem;
  129. while (ptr && ptr->next)
  130. ptr = ptr->next;
  131. if (ptr)
  132. ptr->next = data;
  133. else
  134. option->mem = data;
  135. return 0;
  136. }
  137. static void pnp_free_port(struct pnp_port *port)
  138. {
  139. struct pnp_port *next;
  140. while (port) {
  141. next = port->next;
  142. kfree(port);
  143. port = next;
  144. }
  145. }
  146. static void pnp_free_irq(struct pnp_irq *irq)
  147. {
  148. struct pnp_irq *next;
  149. while (irq) {
  150. next = irq->next;
  151. kfree(irq);
  152. irq = next;
  153. }
  154. }
  155. static void pnp_free_dma(struct pnp_dma *dma)
  156. {
  157. struct pnp_dma *next;
  158. while (dma) {
  159. next = dma->next;
  160. kfree(dma);
  161. dma = next;
  162. }
  163. }
  164. static void pnp_free_mem(struct pnp_mem *mem)
  165. {
  166. struct pnp_mem *next;
  167. while (mem) {
  168. next = mem->next;
  169. kfree(mem);
  170. mem = next;
  171. }
  172. }
  173. void pnp_free_option(struct pnp_option *option)
  174. {
  175. struct pnp_option *next;
  176. while (option) {
  177. next = option->next;
  178. pnp_free_port(option->port);
  179. pnp_free_irq(option->irq);
  180. pnp_free_dma(option->dma);
  181. pnp_free_mem(option->mem);
  182. kfree(option);
  183. option = next;
  184. }
  185. }
  186. /*
  187. * resource validity checking
  188. */
  189. #define length(start, end) (*(end) - *(start) + 1)
  190. /* Two ranges conflict if one doesn't end before the other starts */
  191. #define ranged_conflict(starta, enda, startb, endb) \
  192. !((*(enda) < *(startb)) || (*(endb) < *(starta)))
  193. #define cannot_compare(flags) \
  194. ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
  195. int pnp_check_port(struct pnp_dev *dev, int idx)
  196. {
  197. int tmp;
  198. struct pnp_dev *tdev;
  199. resource_size_t *port, *end, *tport, *tend;
  200. port = &dev->res.port_resource[idx].start;
  201. end = &dev->res.port_resource[idx].end;
  202. /* if the resource doesn't exist, don't complain about it */
  203. if (cannot_compare(dev->res.port_resource[idx].flags))
  204. return 1;
  205. /* check if the resource is already in use, skip if the
  206. * device is active because it itself may be in use */
  207. if (!dev->active) {
  208. if (__check_region(&ioport_resource, *port, length(port, end)))
  209. return 0;
  210. }
  211. /* check if the resource is reserved */
  212. for (tmp = 0; tmp < 8; tmp++) {
  213. int rport = pnp_reserve_io[tmp << 1];
  214. int rend = pnp_reserve_io[(tmp << 1) + 1] + rport - 1;
  215. if (ranged_conflict(port, end, &rport, &rend))
  216. return 0;
  217. }
  218. /* check for internal conflicts */
  219. for (tmp = 0; tmp < PNP_MAX_PORT && tmp != idx; tmp++) {
  220. if (dev->res.port_resource[tmp].flags & IORESOURCE_IO) {
  221. tport = &dev->res.port_resource[tmp].start;
  222. tend = &dev->res.port_resource[tmp].end;
  223. if (ranged_conflict(port, end, tport, tend))
  224. return 0;
  225. }
  226. }
  227. /* check for conflicts with other pnp devices */
  228. pnp_for_each_dev(tdev) {
  229. if (tdev == dev)
  230. continue;
  231. for (tmp = 0; tmp < PNP_MAX_PORT; tmp++) {
  232. if (tdev->res.port_resource[tmp].flags & IORESOURCE_IO) {
  233. if (cannot_compare
  234. (tdev->res.port_resource[tmp].flags))
  235. continue;
  236. tport = &tdev->res.port_resource[tmp].start;
  237. tend = &tdev->res.port_resource[tmp].end;
  238. if (ranged_conflict(port, end, tport, tend))
  239. return 0;
  240. }
  241. }
  242. }
  243. return 1;
  244. }
  245. int pnp_check_mem(struct pnp_dev *dev, int idx)
  246. {
  247. int tmp;
  248. struct pnp_dev *tdev;
  249. resource_size_t *addr, *end, *taddr, *tend;
  250. addr = &dev->res.mem_resource[idx].start;
  251. end = &dev->res.mem_resource[idx].end;
  252. /* if the resource doesn't exist, don't complain about it */
  253. if (cannot_compare(dev->res.mem_resource[idx].flags))
  254. return 1;
  255. /* check if the resource is already in use, skip if the
  256. * device is active because it itself may be in use */
  257. if (!dev->active) {
  258. if (check_mem_region(*addr, length(addr, end)))
  259. return 0;
  260. }
  261. /* check if the resource is reserved */
  262. for (tmp = 0; tmp < 8; tmp++) {
  263. int raddr = pnp_reserve_mem[tmp << 1];
  264. int rend = pnp_reserve_mem[(tmp << 1) + 1] + raddr - 1;
  265. if (ranged_conflict(addr, end, &raddr, &rend))
  266. return 0;
  267. }
  268. /* check for internal conflicts */
  269. for (tmp = 0; tmp < PNP_MAX_MEM && tmp != idx; tmp++) {
  270. if (dev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
  271. taddr = &dev->res.mem_resource[tmp].start;
  272. tend = &dev->res.mem_resource[tmp].end;
  273. if (ranged_conflict(addr, end, taddr, tend))
  274. return 0;
  275. }
  276. }
  277. /* check for conflicts with other pnp devices */
  278. pnp_for_each_dev(tdev) {
  279. if (tdev == dev)
  280. continue;
  281. for (tmp = 0; tmp < PNP_MAX_MEM; tmp++) {
  282. if (tdev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
  283. if (cannot_compare
  284. (tdev->res.mem_resource[tmp].flags))
  285. continue;
  286. taddr = &tdev->res.mem_resource[tmp].start;
  287. tend = &tdev->res.mem_resource[tmp].end;
  288. if (ranged_conflict(addr, end, taddr, tend))
  289. return 0;
  290. }
  291. }
  292. }
  293. return 1;
  294. }
  295. static irqreturn_t pnp_test_handler(int irq, void *dev_id)
  296. {
  297. return IRQ_HANDLED;
  298. }
  299. int pnp_check_irq(struct pnp_dev *dev, int idx)
  300. {
  301. int tmp;
  302. struct pnp_dev *tdev;
  303. resource_size_t *irq = &dev->res.irq_resource[idx].start;
  304. /* if the resource doesn't exist, don't complain about it */
  305. if (cannot_compare(dev->res.irq_resource[idx].flags))
  306. return 1;
  307. /* check if the resource is valid */
  308. if (*irq < 0 || *irq > 15)
  309. return 0;
  310. /* check if the resource is reserved */
  311. for (tmp = 0; tmp < 16; tmp++) {
  312. if (pnp_reserve_irq[tmp] == *irq)
  313. return 0;
  314. }
  315. /* check for internal conflicts */
  316. for (tmp = 0; tmp < PNP_MAX_IRQ && tmp != idx; tmp++) {
  317. if (dev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
  318. if (dev->res.irq_resource[tmp].start == *irq)
  319. return 0;
  320. }
  321. }
  322. #ifdef CONFIG_PCI
  323. /* check if the resource is being used by a pci device */
  324. {
  325. struct pci_dev *pci = NULL;
  326. for_each_pci_dev(pci) {
  327. if (pci->irq == *irq)
  328. return 0;
  329. }
  330. }
  331. #endif
  332. /* check if the resource is already in use, skip if the
  333. * device is active because it itself may be in use */
  334. if (!dev->active) {
  335. if (request_irq(*irq, pnp_test_handler,
  336. IRQF_DISABLED | IRQF_PROBE_SHARED, "pnp", NULL))
  337. return 0;
  338. free_irq(*irq, NULL);
  339. }
  340. /* check for conflicts with other pnp devices */
  341. pnp_for_each_dev(tdev) {
  342. if (tdev == dev)
  343. continue;
  344. for (tmp = 0; tmp < PNP_MAX_IRQ; tmp++) {
  345. if (tdev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
  346. if (cannot_compare
  347. (tdev->res.irq_resource[tmp].flags))
  348. continue;
  349. if ((tdev->res.irq_resource[tmp].start == *irq))
  350. return 0;
  351. }
  352. }
  353. }
  354. return 1;
  355. }
  356. int pnp_check_dma(struct pnp_dev *dev, int idx)
  357. {
  358. #ifndef CONFIG_IA64
  359. int tmp;
  360. struct pnp_dev *tdev;
  361. resource_size_t *dma = &dev->res.dma_resource[idx].start;
  362. /* if the resource doesn't exist, don't complain about it */
  363. if (cannot_compare(dev->res.dma_resource[idx].flags))
  364. return 1;
  365. /* check if the resource is valid */
  366. if (*dma < 0 || *dma == 4 || *dma > 7)
  367. return 0;
  368. /* check if the resource is reserved */
  369. for (tmp = 0; tmp < 8; tmp++) {
  370. if (pnp_reserve_dma[tmp] == *dma)
  371. return 0;
  372. }
  373. /* check for internal conflicts */
  374. for (tmp = 0; tmp < PNP_MAX_DMA && tmp != idx; tmp++) {
  375. if (dev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
  376. if (dev->res.dma_resource[tmp].start == *dma)
  377. return 0;
  378. }
  379. }
  380. /* check if the resource is already in use, skip if the
  381. * device is active because it itself may be in use */
  382. if (!dev->active) {
  383. if (request_dma(*dma, "pnp"))
  384. return 0;
  385. free_dma(*dma);
  386. }
  387. /* check for conflicts with other pnp devices */
  388. pnp_for_each_dev(tdev) {
  389. if (tdev == dev)
  390. continue;
  391. for (tmp = 0; tmp < PNP_MAX_DMA; tmp++) {
  392. if (tdev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
  393. if (cannot_compare
  394. (tdev->res.dma_resource[tmp].flags))
  395. continue;
  396. if ((tdev->res.dma_resource[tmp].start == *dma))
  397. return 0;
  398. }
  399. }
  400. }
  401. return 1;
  402. #else
  403. /* IA64 does not have legacy DMA */
  404. return 0;
  405. #endif
  406. }
  407. /* format is: pnp_reserve_irq=irq1[,irq2] .... */
  408. static int __init pnp_setup_reserve_irq(char *str)
  409. {
  410. int i;
  411. for (i = 0; i < 16; i++)
  412. if (get_option(&str, &pnp_reserve_irq[i]) != 2)
  413. break;
  414. return 1;
  415. }
  416. __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
  417. /* format is: pnp_reserve_dma=dma1[,dma2] .... */
  418. static int __init pnp_setup_reserve_dma(char *str)
  419. {
  420. int i;
  421. for (i = 0; i < 8; i++)
  422. if (get_option(&str, &pnp_reserve_dma[i]) != 2)
  423. break;
  424. return 1;
  425. }
  426. __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
  427. /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
  428. static int __init pnp_setup_reserve_io(char *str)
  429. {
  430. int i;
  431. for (i = 0; i < 16; i++)
  432. if (get_option(&str, &pnp_reserve_io[i]) != 2)
  433. break;
  434. return 1;
  435. }
  436. __setup("pnp_reserve_io=", pnp_setup_reserve_io);
  437. /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
  438. static int __init pnp_setup_reserve_mem(char *str)
  439. {
  440. int i;
  441. for (i = 0; i < 16; i++)
  442. if (get_option(&str, &pnp_reserve_mem[i]) != 2)
  443. break;
  444. return 1;
  445. }
  446. __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);