resource.c 12 KB

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