resource.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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/config.h>
  9. #include <linux/module.h>
  10. #include <linux/errno.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel.h>
  13. #include <asm/io.h>
  14. #include <asm/dma.h>
  15. #include <asm/irq.h>
  16. #include <linux/pci.h>
  17. #include <linux/ioport.h>
  18. #include <linux/init.h>
  19. #include <linux/pnp.h>
  20. #include "base.h"
  21. static int pnp_reserve_irq[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some IRQ */
  22. static int pnp_reserve_dma[8] = { [0 ... 7] = -1 }; /* reserve (don't use) some DMA */
  23. static int pnp_reserve_io[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some I/O region */
  24. static int pnp_reserve_mem[16] = { [0 ... 15] = -1 }; /* reserve (don't use) some memory region */
  25. /*
  26. * option registration
  27. */
  28. static struct pnp_option * pnp_build_option(int priority)
  29. {
  30. struct pnp_option *option = pnp_alloc(sizeof(struct pnp_option));
  31. /* check if pnp_alloc ran out of memory */
  32. if (!option)
  33. return NULL;
  34. option->priority = priority & 0xff;
  35. /* make sure the priority is valid */
  36. if (option->priority > PNP_RES_PRIORITY_FUNCTIONAL)
  37. option->priority = PNP_RES_PRIORITY_INVALID;
  38. return option;
  39. }
  40. struct pnp_option * pnp_register_independent_option(struct pnp_dev *dev)
  41. {
  42. struct pnp_option *option;
  43. if (!dev)
  44. return NULL;
  45. option = pnp_build_option(PNP_RES_PRIORITY_PREFERRED);
  46. /* this should never happen but if it does we'll try to continue */
  47. if (dev->independent)
  48. pnp_err("independent resource already registered");
  49. dev->independent = option;
  50. return option;
  51. }
  52. struct pnp_option * pnp_register_dependent_option(struct pnp_dev *dev, 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. unsigned long *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(tdev->res.port_resource[tmp].flags))
  236. continue;
  237. tport = &tdev->res.port_resource[tmp].start;
  238. tend = &tdev->res.port_resource[tmp].end;
  239. if (ranged_conflict(port,end,tport,tend))
  240. return 0;
  241. }
  242. }
  243. }
  244. return 1;
  245. }
  246. int pnp_check_mem(struct pnp_dev * dev, int idx)
  247. {
  248. int tmp;
  249. struct pnp_dev *tdev;
  250. unsigned long *addr, *end, *taddr, *tend;
  251. addr = &dev->res.mem_resource[idx].start;
  252. end = &dev->res.mem_resource[idx].end;
  253. /* if the resource doesn't exist, don't complain about it */
  254. if (cannot_compare(dev->res.mem_resource[idx].flags))
  255. return 1;
  256. /* check if the resource is already in use, skip if the
  257. * device is active because it itself may be in use */
  258. if(!dev->active) {
  259. if (check_mem_region(*addr, length(addr,end)))
  260. return 0;
  261. }
  262. /* check if the resource is reserved */
  263. for (tmp = 0; tmp < 8; tmp++) {
  264. int raddr = pnp_reserve_mem[tmp << 1];
  265. int rend = pnp_reserve_mem[(tmp << 1) + 1] + raddr - 1;
  266. if (ranged_conflict(addr,end,&raddr,&rend))
  267. return 0;
  268. }
  269. /* check for internal conflicts */
  270. for (tmp = 0; tmp < PNP_MAX_MEM && tmp != idx; tmp++) {
  271. if (dev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
  272. taddr = &dev->res.mem_resource[tmp].start;
  273. tend = &dev->res.mem_resource[tmp].end;
  274. if (ranged_conflict(addr,end,taddr,tend))
  275. return 0;
  276. }
  277. }
  278. /* check for conflicts with other pnp devices */
  279. pnp_for_each_dev(tdev) {
  280. if (tdev == dev)
  281. continue;
  282. for (tmp = 0; tmp < PNP_MAX_MEM; tmp++) {
  283. if (tdev->res.mem_resource[tmp].flags & IORESOURCE_MEM) {
  284. if (cannot_compare(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, struct pt_regs *regs)
  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. unsigned long * 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, SA_INTERRUPT, "pnp", NULL))
  336. return 0;
  337. free_irq(*irq, NULL);
  338. }
  339. /* check for conflicts with other pnp devices */
  340. pnp_for_each_dev(tdev) {
  341. if (tdev == dev)
  342. continue;
  343. for (tmp = 0; tmp < PNP_MAX_IRQ; tmp++) {
  344. if (tdev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
  345. if (cannot_compare(tdev->res.irq_resource[tmp].flags))
  346. continue;
  347. if ((tdev->res.irq_resource[tmp].start == *irq))
  348. return 0;
  349. }
  350. }
  351. }
  352. return 1;
  353. }
  354. int pnp_check_dma(struct pnp_dev * dev, int idx)
  355. {
  356. #ifndef CONFIG_IA64
  357. int tmp;
  358. struct pnp_dev *tdev;
  359. unsigned long * dma = &dev->res.dma_resource[idx].start;
  360. /* if the resource doesn't exist, don't complain about it */
  361. if (cannot_compare(dev->res.dma_resource[idx].flags))
  362. return 1;
  363. /* check if the resource is valid */
  364. if (*dma < 0 || *dma == 4 || *dma > 7)
  365. return 0;
  366. /* check if the resource is reserved */
  367. for (tmp = 0; tmp < 8; tmp++) {
  368. if (pnp_reserve_dma[tmp] == *dma)
  369. return 0;
  370. }
  371. /* check for internal conflicts */
  372. for (tmp = 0; tmp < PNP_MAX_DMA && tmp != idx; tmp++) {
  373. if (dev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
  374. if (dev->res.dma_resource[tmp].start == *dma)
  375. return 0;
  376. }
  377. }
  378. /* check if the resource is already in use, skip if the
  379. * device is active because it itself may be in use */
  380. if(!dev->active) {
  381. if (request_dma(*dma, "pnp"))
  382. return 0;
  383. free_dma(*dma);
  384. }
  385. /* check for conflicts with other pnp devices */
  386. pnp_for_each_dev(tdev) {
  387. if (tdev == dev)
  388. continue;
  389. for (tmp = 0; tmp < PNP_MAX_DMA; tmp++) {
  390. if (tdev->res.dma_resource[tmp].flags & IORESOURCE_DMA) {
  391. if (cannot_compare(tdev->res.dma_resource[tmp].flags))
  392. continue;
  393. if ((tdev->res.dma_resource[tmp].start == *dma))
  394. return 0;
  395. }
  396. }
  397. }
  398. return 1;
  399. #else
  400. /* IA64 hasn't legacy DMA */
  401. return 0;
  402. #endif
  403. }
  404. #if 0
  405. EXPORT_SYMBOL(pnp_register_dependent_option);
  406. EXPORT_SYMBOL(pnp_register_independent_option);
  407. EXPORT_SYMBOL(pnp_register_irq_resource);
  408. EXPORT_SYMBOL(pnp_register_dma_resource);
  409. EXPORT_SYMBOL(pnp_register_port_resource);
  410. EXPORT_SYMBOL(pnp_register_mem_resource);
  411. #endif /* 0 */
  412. /* format is: pnp_reserve_irq=irq1[,irq2] .... */
  413. static int __init pnp_setup_reserve_irq(char *str)
  414. {
  415. int i;
  416. for (i = 0; i < 16; i++)
  417. if (get_option(&str,&pnp_reserve_irq[i]) != 2)
  418. break;
  419. return 1;
  420. }
  421. __setup("pnp_reserve_irq=", pnp_setup_reserve_irq);
  422. /* format is: pnp_reserve_dma=dma1[,dma2] .... */
  423. static int __init pnp_setup_reserve_dma(char *str)
  424. {
  425. int i;
  426. for (i = 0; i < 8; i++)
  427. if (get_option(&str,&pnp_reserve_dma[i]) != 2)
  428. break;
  429. return 1;
  430. }
  431. __setup("pnp_reserve_dma=", pnp_setup_reserve_dma);
  432. /* format is: pnp_reserve_io=io1,size1[,io2,size2] .... */
  433. static int __init pnp_setup_reserve_io(char *str)
  434. {
  435. int i;
  436. for (i = 0; i < 16; i++)
  437. if (get_option(&str,&pnp_reserve_io[i]) != 2)
  438. break;
  439. return 1;
  440. }
  441. __setup("pnp_reserve_io=", pnp_setup_reserve_io);
  442. /* format is: pnp_reserve_mem=mem1,size1[,mem2,size2] .... */
  443. static int __init pnp_setup_reserve_mem(char *str)
  444. {
  445. int i;
  446. for (i = 0; i < 16; i++)
  447. if (get_option(&str,&pnp_reserve_mem[i]) != 2)
  448. break;
  449. return 1;
  450. }
  451. __setup("pnp_reserve_mem=", pnp_setup_reserve_mem);