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/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, int priority)
  52. {
  53. struct pnp_option *option;
  54. if (!dev)
  55. return NULL;
  56. option = pnp_build_option(priority);
  57. if (dev->dependent) {
  58. struct pnp_option *parent = dev->dependent;
  59. while (parent->next)
  60. parent = parent->next;
  61. parent->next = option;
  62. } else
  63. dev->dependent = option;
  64. return option;
  65. }
  66. int pnp_register_irq_resource(struct pnp_option *option, struct pnp_irq *data)
  67. {
  68. struct pnp_irq *ptr;
  69. if (!option)
  70. return -EINVAL;
  71. if (!data)
  72. return -EINVAL;
  73. ptr = option->irq;
  74. while (ptr && ptr->next)
  75. ptr = ptr->next;
  76. if (ptr)
  77. ptr->next = data;
  78. else
  79. option->irq = data;
  80. #ifdef CONFIG_PCI
  81. {
  82. int i;
  83. for (i = 0; i < 16; i++)
  84. if (test_bit(i, data->map))
  85. pcibios_penalize_isa_irq(i, 0);
  86. }
  87. #endif
  88. return 0;
  89. }
  90. int pnp_register_dma_resource(struct pnp_option *option, struct pnp_dma *data)
  91. {
  92. struct pnp_dma *ptr;
  93. if (!option)
  94. return -EINVAL;
  95. if (!data)
  96. return -EINVAL;
  97. ptr = option->dma;
  98. while (ptr && ptr->next)
  99. ptr = ptr->next;
  100. if (ptr)
  101. ptr->next = data;
  102. else
  103. option->dma = data;
  104. return 0;
  105. }
  106. int pnp_register_port_resource(struct pnp_option *option, struct pnp_port *data)
  107. {
  108. struct pnp_port *ptr;
  109. if (!option)
  110. return -EINVAL;
  111. if (!data)
  112. return -EINVAL;
  113. ptr = option->port;
  114. while (ptr && ptr->next)
  115. ptr = ptr->next;
  116. if (ptr)
  117. ptr->next = data;
  118. else
  119. option->port = data;
  120. return 0;
  121. }
  122. int pnp_register_mem_resource(struct pnp_option *option, struct pnp_mem *data)
  123. {
  124. struct pnp_mem *ptr;
  125. if (!option)
  126. return -EINVAL;
  127. if (!data)
  128. return -EINVAL;
  129. ptr = option->mem;
  130. while (ptr && ptr->next)
  131. ptr = ptr->next;
  132. if (ptr)
  133. ptr->next = data;
  134. else
  135. option->mem = data;
  136. return 0;
  137. }
  138. static void pnp_free_port(struct pnp_port *port)
  139. {
  140. struct pnp_port *next;
  141. while (port) {
  142. next = port->next;
  143. kfree(port);
  144. port = next;
  145. }
  146. }
  147. static void pnp_free_irq(struct pnp_irq *irq)
  148. {
  149. struct pnp_irq *next;
  150. while (irq) {
  151. next = irq->next;
  152. kfree(irq);
  153. irq = next;
  154. }
  155. }
  156. static void pnp_free_dma(struct pnp_dma *dma)
  157. {
  158. struct pnp_dma *next;
  159. while (dma) {
  160. next = dma->next;
  161. kfree(dma);
  162. dma = next;
  163. }
  164. }
  165. static void pnp_free_mem(struct pnp_mem *mem)
  166. {
  167. struct pnp_mem *next;
  168. while (mem) {
  169. next = mem->next;
  170. kfree(mem);
  171. mem = next;
  172. }
  173. }
  174. void pnp_free_option(struct pnp_option *option)
  175. {
  176. struct pnp_option *next;
  177. while (option) {
  178. next = option->next;
  179. pnp_free_port(option->port);
  180. pnp_free_irq(option->irq);
  181. pnp_free_dma(option->dma);
  182. pnp_free_mem(option->mem);
  183. kfree(option);
  184. option = next;
  185. }
  186. }
  187. /*
  188. * resource validity checking
  189. */
  190. #define length(start, end) (*(end) - *(start) + 1)
  191. /* Two ranges conflict if one doesn't end before the other starts */
  192. #define ranged_conflict(starta, enda, startb, endb) \
  193. !((*(enda) < *(startb)) || (*(endb) < *(starta)))
  194. #define cannot_compare(flags) \
  195. ((flags) & (IORESOURCE_UNSET | IORESOURCE_DISABLED))
  196. int pnp_check_port(struct pnp_dev * dev, int idx)
  197. {
  198. int tmp;
  199. struct pnp_dev *tdev;
  200. resource_size_t *port, *end, *tport, *tend;
  201. port = &dev->res.port_resource[idx].start;
  202. end = &dev->res.port_resource[idx].end;
  203. /* if the resource doesn't exist, don't complain about it */
  204. if (cannot_compare(dev->res.port_resource[idx].flags))
  205. return 1;
  206. /* check if the resource is already in use, skip if the
  207. * device is active because it itself may be in use */
  208. if(!dev->active) {
  209. if (__check_region(&ioport_resource, *port, length(port,end)))
  210. return 0;
  211. }
  212. /* check if the resource is reserved */
  213. for (tmp = 0; tmp < 8; tmp++) {
  214. int rport = pnp_reserve_io[tmp << 1];
  215. int rend = pnp_reserve_io[(tmp << 1) + 1] + rport - 1;
  216. if (ranged_conflict(port,end,&rport,&rend))
  217. return 0;
  218. }
  219. /* check for internal conflicts */
  220. for (tmp = 0; tmp < PNP_MAX_PORT && tmp != idx; tmp++) {
  221. if (dev->res.port_resource[tmp].flags & IORESOURCE_IO) {
  222. tport = &dev->res.port_resource[tmp].start;
  223. tend = &dev->res.port_resource[tmp].end;
  224. if (ranged_conflict(port,end,tport,tend))
  225. return 0;
  226. }
  227. }
  228. /* check for conflicts with other pnp devices */
  229. pnp_for_each_dev(tdev) {
  230. if (tdev == dev)
  231. continue;
  232. for (tmp = 0; tmp < PNP_MAX_PORT; tmp++) {
  233. if (tdev->res.port_resource[tmp].flags & IORESOURCE_IO) {
  234. if (cannot_compare(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(tdev->res.mem_resource[tmp].flags))
  284. continue;
  285. taddr = &tdev->res.mem_resource[tmp].start;
  286. tend = &tdev->res.mem_resource[tmp].end;
  287. if (ranged_conflict(addr,end,taddr,tend))
  288. return 0;
  289. }
  290. }
  291. }
  292. return 1;
  293. }
  294. static irqreturn_t pnp_test_handler(int irq, void *dev_id)
  295. {
  296. return IRQ_HANDLED;
  297. }
  298. int pnp_check_irq(struct pnp_dev * dev, int idx)
  299. {
  300. int tmp;
  301. struct pnp_dev *tdev;
  302. resource_size_t * irq = &dev->res.irq_resource[idx].start;
  303. /* if the resource doesn't exist, don't complain about it */
  304. if (cannot_compare(dev->res.irq_resource[idx].flags))
  305. return 1;
  306. /* check if the resource is valid */
  307. if (*irq < 0 || *irq > 15)
  308. return 0;
  309. /* check if the resource is reserved */
  310. for (tmp = 0; tmp < 16; tmp++) {
  311. if (pnp_reserve_irq[tmp] == *irq)
  312. return 0;
  313. }
  314. /* check for internal conflicts */
  315. for (tmp = 0; tmp < PNP_MAX_IRQ && tmp != idx; tmp++) {
  316. if (dev->res.irq_resource[tmp].flags & IORESOURCE_IRQ) {
  317. if (dev->res.irq_resource[tmp].start == *irq)
  318. return 0;
  319. }
  320. }
  321. #ifdef CONFIG_PCI
  322. /* check if the resource is being used by a pci device */
  323. {
  324. struct pci_dev *pci = NULL;
  325. for_each_pci_dev(pci) {
  326. if (pci->irq == *irq)
  327. return 0;
  328. }
  329. }
  330. #endif
  331. /* check if the resource is already in use, skip if the
  332. * device is active because it itself may be in use */
  333. if(!dev->active) {
  334. if (request_irq(*irq, pnp_test_handler,
  335. IRQF_DISABLED|IRQF_PROBE_SHARED, "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. resource_size_t * 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);