core.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. /*
  2. * ISA Plug & Play support
  3. * Copyright (c) by Jaroslav Kysela <perex@perex.cz>
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * Changelog:
  21. * 2000-01-01 Added quirks handling for buggy hardware
  22. * Peter Denison <peterd@pnd-pc.demon.co.uk>
  23. * 2000-06-14 Added isapnp_probe_devs() and isapnp_activate_dev()
  24. * Christoph Hellwig <hch@infradead.org>
  25. * 2001-06-03 Added release_region calls to correspond with
  26. * request_region calls when a failure occurs. Also
  27. * added KERN_* constants to printk() calls.
  28. * 2001-11-07 Added isapnp_{,un}register_driver calls along the lines
  29. * of the pci driver interface
  30. * Kai Germaschewski <kai.germaschewski@gmx.de>
  31. * 2002-06-06 Made the use of dma channel 0 configurable
  32. * Gerald Teschl <gerald.teschl@univie.ac.at>
  33. * 2002-10-06 Ported to PnP Layer - Adam Belay <ambx1@neo.rr.com>
  34. * 2003-08-11 Resource Management Updates - Adam Belay <ambx1@neo.rr.com>
  35. */
  36. #include <linux/module.h>
  37. #include <linux/kernel.h>
  38. #include <linux/errno.h>
  39. #include <linux/slab.h>
  40. #include <linux/delay.h>
  41. #include <linux/init.h>
  42. #include <linux/isapnp.h>
  43. #include <linux/mutex.h>
  44. #include <asm/io.h>
  45. #include "../base.h"
  46. #if 0
  47. #define ISAPNP_REGION_OK
  48. #endif
  49. int isapnp_disable; /* Disable ISA PnP */
  50. static int isapnp_rdp; /* Read Data Port */
  51. static int isapnp_reset = 1; /* reset all PnP cards (deactivate) */
  52. static int isapnp_verbose = 1; /* verbose mode */
  53. MODULE_AUTHOR("Jaroslav Kysela <perex@perex.cz>");
  54. MODULE_DESCRIPTION("Generic ISA Plug & Play support");
  55. module_param(isapnp_disable, int, 0);
  56. MODULE_PARM_DESC(isapnp_disable, "ISA Plug & Play disable");
  57. module_param(isapnp_rdp, int, 0);
  58. MODULE_PARM_DESC(isapnp_rdp, "ISA Plug & Play read data port");
  59. module_param(isapnp_reset, int, 0);
  60. MODULE_PARM_DESC(isapnp_reset, "ISA Plug & Play reset all cards");
  61. module_param(isapnp_verbose, int, 0);
  62. MODULE_PARM_DESC(isapnp_verbose, "ISA Plug & Play verbose mode");
  63. MODULE_LICENSE("GPL");
  64. #define _PIDXR 0x279
  65. #define _PNPWRP 0xa79
  66. /* short tags */
  67. #define _STAG_PNPVERNO 0x01
  68. #define _STAG_LOGDEVID 0x02
  69. #define _STAG_COMPATDEVID 0x03
  70. #define _STAG_IRQ 0x04
  71. #define _STAG_DMA 0x05
  72. #define _STAG_STARTDEP 0x06
  73. #define _STAG_ENDDEP 0x07
  74. #define _STAG_IOPORT 0x08
  75. #define _STAG_FIXEDIO 0x09
  76. #define _STAG_VENDOR 0x0e
  77. #define _STAG_END 0x0f
  78. /* long tags */
  79. #define _LTAG_MEMRANGE 0x81
  80. #define _LTAG_ANSISTR 0x82
  81. #define _LTAG_UNICODESTR 0x83
  82. #define _LTAG_VENDOR 0x84
  83. #define _LTAG_MEM32RANGE 0x85
  84. #define _LTAG_FIXEDMEM32RANGE 0x86
  85. /* Logical device control and configuration registers */
  86. #define ISAPNP_CFG_ACTIVATE 0x30 /* byte */
  87. #define ISAPNP_CFG_MEM 0x40 /* 4 * dword */
  88. #define ISAPNP_CFG_PORT 0x60 /* 8 * word */
  89. #define ISAPNP_CFG_IRQ 0x70 /* 2 * word */
  90. #define ISAPNP_CFG_DMA 0x74 /* 2 * byte */
  91. /*
  92. * Sizes of ISAPNP logical device configuration register sets.
  93. * See PNP-ISA-v1.0a.pdf, Appendix A.
  94. */
  95. #define ISAPNP_MAX_MEM 4
  96. #define ISAPNP_MAX_PORT 8
  97. #define ISAPNP_MAX_IRQ 2
  98. #define ISAPNP_MAX_DMA 2
  99. static unsigned char isapnp_checksum_value;
  100. static DEFINE_MUTEX(isapnp_cfg_mutex);
  101. static int isapnp_csn_count;
  102. /* some prototypes */
  103. static inline void write_data(unsigned char x)
  104. {
  105. outb(x, _PNPWRP);
  106. }
  107. static inline void write_address(unsigned char x)
  108. {
  109. outb(x, _PIDXR);
  110. udelay(20);
  111. }
  112. static inline unsigned char read_data(void)
  113. {
  114. unsigned char val = inb(isapnp_rdp);
  115. return val;
  116. }
  117. unsigned char isapnp_read_byte(unsigned char idx)
  118. {
  119. write_address(idx);
  120. return read_data();
  121. }
  122. static unsigned short isapnp_read_word(unsigned char idx)
  123. {
  124. unsigned short val;
  125. val = isapnp_read_byte(idx);
  126. val = (val << 8) + isapnp_read_byte(idx + 1);
  127. return val;
  128. }
  129. void isapnp_write_byte(unsigned char idx, unsigned char val)
  130. {
  131. write_address(idx);
  132. write_data(val);
  133. }
  134. static void isapnp_write_word(unsigned char idx, unsigned short val)
  135. {
  136. isapnp_write_byte(idx, val >> 8);
  137. isapnp_write_byte(idx + 1, val);
  138. }
  139. static void isapnp_key(void)
  140. {
  141. unsigned char code = 0x6a, msb;
  142. int i;
  143. mdelay(1);
  144. write_address(0x00);
  145. write_address(0x00);
  146. write_address(code);
  147. for (i = 1; i < 32; i++) {
  148. msb = ((code & 0x01) ^ ((code & 0x02) >> 1)) << 7;
  149. code = (code >> 1) | msb;
  150. write_address(code);
  151. }
  152. }
  153. /* place all pnp cards in wait-for-key state */
  154. static void isapnp_wait(void)
  155. {
  156. isapnp_write_byte(0x02, 0x02);
  157. }
  158. static void isapnp_wake(unsigned char csn)
  159. {
  160. isapnp_write_byte(0x03, csn);
  161. }
  162. static void isapnp_device(unsigned char logdev)
  163. {
  164. isapnp_write_byte(0x07, logdev);
  165. }
  166. static void isapnp_activate(unsigned char logdev)
  167. {
  168. isapnp_device(logdev);
  169. isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 1);
  170. udelay(250);
  171. }
  172. static void isapnp_deactivate(unsigned char logdev)
  173. {
  174. isapnp_device(logdev);
  175. isapnp_write_byte(ISAPNP_CFG_ACTIVATE, 0);
  176. udelay(500);
  177. }
  178. static void __init isapnp_peek(unsigned char *data, int bytes)
  179. {
  180. int i, j;
  181. unsigned char d = 0;
  182. for (i = 1; i <= bytes; i++) {
  183. for (j = 0; j < 20; j++) {
  184. d = isapnp_read_byte(0x05);
  185. if (d & 1)
  186. break;
  187. udelay(100);
  188. }
  189. if (!(d & 1)) {
  190. if (data != NULL)
  191. *data++ = 0xff;
  192. continue;
  193. }
  194. d = isapnp_read_byte(0x04); /* PRESDI */
  195. isapnp_checksum_value += d;
  196. if (data != NULL)
  197. *data++ = d;
  198. }
  199. }
  200. #define RDP_STEP 32 /* minimum is 4 */
  201. static int isapnp_next_rdp(void)
  202. {
  203. int rdp = isapnp_rdp;
  204. static int old_rdp = 0;
  205. if (old_rdp) {
  206. release_region(old_rdp, 1);
  207. old_rdp = 0;
  208. }
  209. while (rdp <= 0x3ff) {
  210. /*
  211. * We cannot use NE2000 probe spaces for ISAPnP or we
  212. * will lock up machines.
  213. */
  214. if ((rdp < 0x280 || rdp > 0x380)
  215. && request_region(rdp, 1, "ISAPnP")) {
  216. isapnp_rdp = rdp;
  217. old_rdp = rdp;
  218. return 0;
  219. }
  220. rdp += RDP_STEP;
  221. }
  222. return -1;
  223. }
  224. /* Set read port address */
  225. static inline void isapnp_set_rdp(void)
  226. {
  227. isapnp_write_byte(0x00, isapnp_rdp >> 2);
  228. udelay(100);
  229. }
  230. /*
  231. * Perform an isolation. The port selection code now tries to avoid
  232. * "dangerous to read" ports.
  233. */
  234. static int __init isapnp_isolate_rdp_select(void)
  235. {
  236. isapnp_wait();
  237. isapnp_key();
  238. /* Control: reset CSN and conditionally everything else too */
  239. isapnp_write_byte(0x02, isapnp_reset ? 0x05 : 0x04);
  240. mdelay(2);
  241. isapnp_wait();
  242. isapnp_key();
  243. isapnp_wake(0x00);
  244. if (isapnp_next_rdp() < 0) {
  245. isapnp_wait();
  246. return -1;
  247. }
  248. isapnp_set_rdp();
  249. udelay(1000);
  250. write_address(0x01);
  251. udelay(1000);
  252. return 0;
  253. }
  254. /*
  255. * Isolate (assign uniqued CSN) to all ISA PnP devices.
  256. */
  257. static int __init isapnp_isolate(void)
  258. {
  259. unsigned char checksum = 0x6a;
  260. unsigned char chksum = 0x00;
  261. unsigned char bit = 0x00;
  262. int data;
  263. int csn = 0;
  264. int i;
  265. int iteration = 1;
  266. isapnp_rdp = 0x213;
  267. if (isapnp_isolate_rdp_select() < 0)
  268. return -1;
  269. while (1) {
  270. for (i = 1; i <= 64; i++) {
  271. data = read_data() << 8;
  272. udelay(250);
  273. data = data | read_data();
  274. udelay(250);
  275. if (data == 0x55aa)
  276. bit = 0x01;
  277. checksum =
  278. ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7)
  279. | (checksum >> 1);
  280. bit = 0x00;
  281. }
  282. for (i = 65; i <= 72; i++) {
  283. data = read_data() << 8;
  284. udelay(250);
  285. data = data | read_data();
  286. udelay(250);
  287. if (data == 0x55aa)
  288. chksum |= (1 << (i - 65));
  289. }
  290. if (checksum != 0x00 && checksum == chksum) {
  291. csn++;
  292. isapnp_write_byte(0x06, csn);
  293. udelay(250);
  294. iteration++;
  295. isapnp_wake(0x00);
  296. isapnp_set_rdp();
  297. udelay(1000);
  298. write_address(0x01);
  299. udelay(1000);
  300. goto __next;
  301. }
  302. if (iteration == 1) {
  303. isapnp_rdp += RDP_STEP;
  304. if (isapnp_isolate_rdp_select() < 0)
  305. return -1;
  306. } else if (iteration > 1) {
  307. break;
  308. }
  309. __next:
  310. if (csn == 255)
  311. break;
  312. checksum = 0x6a;
  313. chksum = 0x00;
  314. bit = 0x00;
  315. }
  316. isapnp_wait();
  317. isapnp_csn_count = csn;
  318. return csn;
  319. }
  320. /*
  321. * Read one tag from stream.
  322. */
  323. static int __init isapnp_read_tag(unsigned char *type, unsigned short *size)
  324. {
  325. unsigned char tag, tmp[2];
  326. isapnp_peek(&tag, 1);
  327. if (tag == 0) /* invalid tag */
  328. return -1;
  329. if (tag & 0x80) { /* large item */
  330. *type = tag;
  331. isapnp_peek(tmp, 2);
  332. *size = (tmp[1] << 8) | tmp[0];
  333. } else {
  334. *type = (tag >> 3) & 0x0f;
  335. *size = tag & 0x07;
  336. }
  337. #if 0
  338. printk(KERN_DEBUG "tag = 0x%x, type = 0x%x, size = %i\n", tag, *type,
  339. *size);
  340. #endif
  341. if (*type == 0xff && *size == 0xffff) /* probably invalid data */
  342. return -1;
  343. return 0;
  344. }
  345. /*
  346. * Skip specified number of bytes from stream.
  347. */
  348. static void __init isapnp_skip_bytes(int count)
  349. {
  350. isapnp_peek(NULL, count);
  351. }
  352. /*
  353. * Parse logical device tag.
  354. */
  355. static struct pnp_dev *__init isapnp_parse_device(struct pnp_card *card,
  356. int size, int number)
  357. {
  358. unsigned char tmp[6];
  359. struct pnp_dev *dev;
  360. u32 eisa_id;
  361. char id[8];
  362. isapnp_peek(tmp, size);
  363. eisa_id = tmp[0] | tmp[1] << 8 | tmp[2] << 16 | tmp[3] << 24;
  364. pnp_eisa_id_to_string(eisa_id, id);
  365. dev = pnp_alloc_dev(&isapnp_protocol, number, id);
  366. if (!dev)
  367. return NULL;
  368. dev->card = card;
  369. dev->capabilities |= PNP_CONFIGURABLE;
  370. dev->capabilities |= PNP_READ;
  371. dev->capabilities |= PNP_WRITE;
  372. dev->capabilities |= PNP_DISABLE;
  373. pnp_init_resources(dev);
  374. return dev;
  375. }
  376. /*
  377. * Add IRQ resource to resources list.
  378. */
  379. static void __init isapnp_parse_irq_resource(struct pnp_dev *dev,
  380. struct pnp_option *option,
  381. int size)
  382. {
  383. unsigned char tmp[3];
  384. struct pnp_irq *irq;
  385. unsigned long bits;
  386. isapnp_peek(tmp, size);
  387. irq = kzalloc(sizeof(struct pnp_irq), GFP_KERNEL);
  388. if (!irq)
  389. return;
  390. bits = (tmp[1] << 8) | tmp[0];
  391. bitmap_copy(irq->map, &bits, 16);
  392. if (size > 2)
  393. irq->flags = tmp[2];
  394. else
  395. irq->flags = IORESOURCE_IRQ_HIGHEDGE;
  396. pnp_register_irq_resource(dev, option, irq);
  397. }
  398. /*
  399. * Add DMA resource to resources list.
  400. */
  401. static void __init isapnp_parse_dma_resource(struct pnp_dev *dev,
  402. struct pnp_option *option,
  403. int size)
  404. {
  405. unsigned char tmp[2];
  406. struct pnp_dma *dma;
  407. isapnp_peek(tmp, size);
  408. dma = kzalloc(sizeof(struct pnp_dma), GFP_KERNEL);
  409. if (!dma)
  410. return;
  411. dma->map = tmp[0];
  412. dma->flags = tmp[1];
  413. pnp_register_dma_resource(dev, option, dma);
  414. }
  415. /*
  416. * Add port resource to resources list.
  417. */
  418. static void __init isapnp_parse_port_resource(struct pnp_dev *dev,
  419. struct pnp_option *option,
  420. int size)
  421. {
  422. unsigned char tmp[7];
  423. struct pnp_port *port;
  424. isapnp_peek(tmp, size);
  425. port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
  426. if (!port)
  427. return;
  428. port->min = (tmp[2] << 8) | tmp[1];
  429. port->max = (tmp[4] << 8) | tmp[3];
  430. port->align = tmp[5];
  431. port->size = tmp[6];
  432. port->flags = tmp[0] ? PNP_PORT_FLAG_16BITADDR : 0;
  433. pnp_register_port_resource(dev, option, port);
  434. }
  435. /*
  436. * Add fixed port resource to resources list.
  437. */
  438. static void __init isapnp_parse_fixed_port_resource(struct pnp_dev *dev,
  439. struct pnp_option *option,
  440. int size)
  441. {
  442. unsigned char tmp[3];
  443. struct pnp_port *port;
  444. isapnp_peek(tmp, size);
  445. port = kzalloc(sizeof(struct pnp_port), GFP_KERNEL);
  446. if (!port)
  447. return;
  448. port->min = port->max = (tmp[1] << 8) | tmp[0];
  449. port->size = tmp[2];
  450. port->align = 0;
  451. port->flags = PNP_PORT_FLAG_FIXED;
  452. pnp_register_port_resource(dev, option, port);
  453. }
  454. /*
  455. * Add memory resource to resources list.
  456. */
  457. static void __init isapnp_parse_mem_resource(struct pnp_dev *dev,
  458. struct pnp_option *option,
  459. int size)
  460. {
  461. unsigned char tmp[9];
  462. struct pnp_mem *mem;
  463. isapnp_peek(tmp, size);
  464. mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
  465. if (!mem)
  466. return;
  467. mem->min = ((tmp[2] << 8) | tmp[1]) << 8;
  468. mem->max = ((tmp[4] << 8) | tmp[3]) << 8;
  469. mem->align = (tmp[6] << 8) | tmp[5];
  470. mem->size = ((tmp[8] << 8) | tmp[7]) << 8;
  471. mem->flags = tmp[0];
  472. pnp_register_mem_resource(dev, option, mem);
  473. }
  474. /*
  475. * Add 32-bit memory resource to resources list.
  476. */
  477. static void __init isapnp_parse_mem32_resource(struct pnp_dev *dev,
  478. struct pnp_option *option,
  479. int size)
  480. {
  481. unsigned char tmp[17];
  482. struct pnp_mem *mem;
  483. isapnp_peek(tmp, size);
  484. mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
  485. if (!mem)
  486. return;
  487. mem->min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
  488. mem->max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
  489. mem->align =
  490. (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
  491. mem->size =
  492. (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
  493. mem->flags = tmp[0];
  494. pnp_register_mem_resource(dev, option, mem);
  495. }
  496. /*
  497. * Add 32-bit fixed memory resource to resources list.
  498. */
  499. static void __init isapnp_parse_fixed_mem32_resource(struct pnp_dev *dev,
  500. struct pnp_option *option,
  501. int size)
  502. {
  503. unsigned char tmp[9];
  504. struct pnp_mem *mem;
  505. isapnp_peek(tmp, size);
  506. mem = kzalloc(sizeof(struct pnp_mem), GFP_KERNEL);
  507. if (!mem)
  508. return;
  509. mem->min = mem->max =
  510. (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
  511. mem->size = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
  512. mem->align = 0;
  513. mem->flags = tmp[0];
  514. pnp_register_mem_resource(dev, option, mem);
  515. }
  516. /*
  517. * Parse card name for ISA PnP device.
  518. */
  519. static void __init
  520. isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
  521. {
  522. if (name[0] == '\0') {
  523. unsigned short size1 =
  524. *size >= name_max ? (name_max - 1) : *size;
  525. isapnp_peek(name, size1);
  526. name[size1] = '\0';
  527. *size -= size1;
  528. /* clean whitespace from end of string */
  529. while (size1 > 0 && name[--size1] == ' ')
  530. name[size1] = '\0';
  531. }
  532. }
  533. /*
  534. * Parse resource map for logical device.
  535. */
  536. static int __init isapnp_create_device(struct pnp_card *card,
  537. unsigned short size)
  538. {
  539. int number = 0, skip = 0, priority = 0, compat = 0;
  540. unsigned char type, tmp[17];
  541. struct pnp_option *option;
  542. struct pnp_dev *dev;
  543. u32 eisa_id;
  544. char id[8];
  545. if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
  546. return 1;
  547. option = pnp_register_independent_option(dev);
  548. if (!option) {
  549. kfree(dev);
  550. return 1;
  551. }
  552. pnp_add_card_device(card, dev);
  553. while (1) {
  554. if (isapnp_read_tag(&type, &size) < 0)
  555. return 1;
  556. if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
  557. goto __skip;
  558. switch (type) {
  559. case _STAG_LOGDEVID:
  560. if (size >= 5 && size <= 6) {
  561. if ((dev =
  562. isapnp_parse_device(card, size,
  563. number++)) == NULL)
  564. return 1;
  565. size = 0;
  566. skip = 0;
  567. option = pnp_register_independent_option(dev);
  568. if (!option) {
  569. kfree(dev);
  570. return 1;
  571. }
  572. pnp_add_card_device(card, dev);
  573. } else {
  574. skip = 1;
  575. }
  576. priority = 0;
  577. compat = 0;
  578. break;
  579. case _STAG_COMPATDEVID:
  580. if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
  581. isapnp_peek(tmp, 4);
  582. eisa_id = tmp[0] | tmp[1] << 8 |
  583. tmp[2] << 16 | tmp[3] << 24;
  584. pnp_eisa_id_to_string(eisa_id, id);
  585. pnp_add_id(dev, id);
  586. compat++;
  587. size = 0;
  588. }
  589. break;
  590. case _STAG_IRQ:
  591. if (size < 2 || size > 3)
  592. goto __skip;
  593. isapnp_parse_irq_resource(dev, option, size);
  594. size = 0;
  595. break;
  596. case _STAG_DMA:
  597. if (size != 2)
  598. goto __skip;
  599. isapnp_parse_dma_resource(dev, option, size);
  600. size = 0;
  601. break;
  602. case _STAG_STARTDEP:
  603. if (size > 1)
  604. goto __skip;
  605. priority = 0x100 | PNP_RES_PRIORITY_ACCEPTABLE;
  606. if (size > 0) {
  607. isapnp_peek(tmp, size);
  608. priority = 0x100 | tmp[0];
  609. size = 0;
  610. }
  611. option = pnp_register_dependent_option(dev, priority);
  612. if (!option)
  613. return 1;
  614. break;
  615. case _STAG_ENDDEP:
  616. if (size != 0)
  617. goto __skip;
  618. priority = 0;
  619. dev_dbg(&dev->dev, "end dependent options\n");
  620. break;
  621. case _STAG_IOPORT:
  622. if (size != 7)
  623. goto __skip;
  624. isapnp_parse_port_resource(dev, option, size);
  625. size = 0;
  626. break;
  627. case _STAG_FIXEDIO:
  628. if (size != 3)
  629. goto __skip;
  630. isapnp_parse_fixed_port_resource(dev, option, size);
  631. size = 0;
  632. break;
  633. case _STAG_VENDOR:
  634. break;
  635. case _LTAG_MEMRANGE:
  636. if (size != 9)
  637. goto __skip;
  638. isapnp_parse_mem_resource(dev, option, size);
  639. size = 0;
  640. break;
  641. case _LTAG_ANSISTR:
  642. isapnp_parse_name(dev->name, sizeof(dev->name), &size);
  643. break;
  644. case _LTAG_UNICODESTR:
  645. /* silently ignore */
  646. /* who use unicode for hardware identification? */
  647. break;
  648. case _LTAG_VENDOR:
  649. break;
  650. case _LTAG_MEM32RANGE:
  651. if (size != 17)
  652. goto __skip;
  653. isapnp_parse_mem32_resource(dev, option, size);
  654. size = 0;
  655. break;
  656. case _LTAG_FIXEDMEM32RANGE:
  657. if (size != 9)
  658. goto __skip;
  659. isapnp_parse_fixed_mem32_resource(dev, option, size);
  660. size = 0;
  661. break;
  662. case _STAG_END:
  663. if (size > 0)
  664. isapnp_skip_bytes(size);
  665. return 1;
  666. default:
  667. dev_err(&dev->dev, "unknown tag %#x (card %i), "
  668. "ignored\n", type, card->number);
  669. }
  670. __skip:
  671. if (size > 0)
  672. isapnp_skip_bytes(size);
  673. }
  674. return 0;
  675. }
  676. /*
  677. * Parse resource map for ISA PnP card.
  678. */
  679. static void __init isapnp_parse_resource_map(struct pnp_card *card)
  680. {
  681. unsigned char type, tmp[17];
  682. unsigned short size;
  683. while (1) {
  684. if (isapnp_read_tag(&type, &size) < 0)
  685. return;
  686. switch (type) {
  687. case _STAG_PNPVERNO:
  688. if (size != 2)
  689. goto __skip;
  690. isapnp_peek(tmp, 2);
  691. card->pnpver = tmp[0];
  692. card->productver = tmp[1];
  693. size = 0;
  694. break;
  695. case _STAG_LOGDEVID:
  696. if (size >= 5 && size <= 6) {
  697. if (isapnp_create_device(card, size) == 1)
  698. return;
  699. size = 0;
  700. }
  701. break;
  702. case _STAG_VENDOR:
  703. break;
  704. case _LTAG_ANSISTR:
  705. isapnp_parse_name(card->name, sizeof(card->name),
  706. &size);
  707. break;
  708. case _LTAG_UNICODESTR:
  709. /* silently ignore */
  710. /* who use unicode for hardware identification? */
  711. break;
  712. case _LTAG_VENDOR:
  713. break;
  714. case _STAG_END:
  715. if (size > 0)
  716. isapnp_skip_bytes(size);
  717. return;
  718. default:
  719. dev_err(&card->dev, "unknown tag %#x, ignored\n",
  720. type);
  721. }
  722. __skip:
  723. if (size > 0)
  724. isapnp_skip_bytes(size);
  725. }
  726. }
  727. /*
  728. * Compute ISA PnP checksum for first eight bytes.
  729. */
  730. static unsigned char __init isapnp_checksum(unsigned char *data)
  731. {
  732. int i, j;
  733. unsigned char checksum = 0x6a, bit, b;
  734. for (i = 0; i < 8; i++) {
  735. b = data[i];
  736. for (j = 0; j < 8; j++) {
  737. bit = 0;
  738. if (b & (1 << j))
  739. bit = 1;
  740. checksum =
  741. ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7)
  742. | (checksum >> 1);
  743. }
  744. }
  745. return checksum;
  746. }
  747. /*
  748. * Build device list for all present ISA PnP devices.
  749. */
  750. static int __init isapnp_build_device_list(void)
  751. {
  752. int csn;
  753. unsigned char header[9], checksum;
  754. struct pnp_card *card;
  755. u32 eisa_id;
  756. char id[8];
  757. isapnp_wait();
  758. isapnp_key();
  759. for (csn = 1; csn <= isapnp_csn_count; csn++) {
  760. isapnp_wake(csn);
  761. isapnp_peek(header, 9);
  762. checksum = isapnp_checksum(header);
  763. eisa_id = header[0] | header[1] << 8 |
  764. header[2] << 16 | header[3] << 24;
  765. pnp_eisa_id_to_string(eisa_id, id);
  766. card = pnp_alloc_card(&isapnp_protocol, csn, id);
  767. if (!card)
  768. continue;
  769. #if 0
  770. dev_info(&card->dev,
  771. "vendor: %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\n",
  772. header[0], header[1], header[2], header[3], header[4],
  773. header[5], header[6], header[7], header[8]);
  774. dev_info(&card->dev, "checksum = %#x\n", checksum);
  775. #endif
  776. INIT_LIST_HEAD(&card->devices);
  777. card->serial =
  778. (header[7] << 24) | (header[6] << 16) | (header[5] << 8) |
  779. header[4];
  780. isapnp_checksum_value = 0x00;
  781. isapnp_parse_resource_map(card);
  782. if (isapnp_checksum_value != 0x00)
  783. dev_err(&card->dev, "invalid checksum %#x\n",
  784. isapnp_checksum_value);
  785. card->checksum = isapnp_checksum_value;
  786. pnp_add_card(card);
  787. }
  788. isapnp_wait();
  789. return 0;
  790. }
  791. /*
  792. * Basic configuration routines.
  793. */
  794. int isapnp_present(void)
  795. {
  796. struct pnp_card *card;
  797. pnp_for_each_card(card) {
  798. if (card->protocol == &isapnp_protocol)
  799. return 1;
  800. }
  801. return 0;
  802. }
  803. int isapnp_cfg_begin(int csn, int logdev)
  804. {
  805. if (csn < 1 || csn > isapnp_csn_count || logdev > 10)
  806. return -EINVAL;
  807. mutex_lock(&isapnp_cfg_mutex);
  808. isapnp_wait();
  809. isapnp_key();
  810. isapnp_wake(csn);
  811. #if 0
  812. /* to avoid malfunction when the isapnptools package is used */
  813. /* we must set RDP to our value again */
  814. /* it is possible to set RDP only in the isolation phase */
  815. /* Jens Thoms Toerring <Jens.Toerring@physik.fu-berlin.de> */
  816. isapnp_write_byte(0x02, 0x04); /* clear CSN of card */
  817. mdelay(2); /* is this necessary? */
  818. isapnp_wake(csn); /* bring card into sleep state */
  819. isapnp_wake(0); /* bring card into isolation state */
  820. isapnp_set_rdp(); /* reset the RDP port */
  821. udelay(1000); /* delay 1000us */
  822. isapnp_write_byte(0x06, csn); /* reset CSN to previous value */
  823. udelay(250); /* is this necessary? */
  824. #endif
  825. if (logdev >= 0)
  826. isapnp_device(logdev);
  827. return 0;
  828. }
  829. int isapnp_cfg_end(void)
  830. {
  831. isapnp_wait();
  832. mutex_unlock(&isapnp_cfg_mutex);
  833. return 0;
  834. }
  835. /*
  836. * Initialization.
  837. */
  838. EXPORT_SYMBOL(isapnp_protocol);
  839. EXPORT_SYMBOL(isapnp_present);
  840. EXPORT_SYMBOL(isapnp_cfg_begin);
  841. EXPORT_SYMBOL(isapnp_cfg_end);
  842. EXPORT_SYMBOL(isapnp_write_byte);
  843. static int isapnp_get_resources(struct pnp_dev *dev)
  844. {
  845. struct pnp_resource *pnp_res;
  846. int i, ret;
  847. dev_dbg(&dev->dev, "get resources\n");
  848. pnp_init_resources(dev);
  849. isapnp_cfg_begin(dev->card->number, dev->number);
  850. dev->active = isapnp_read_byte(ISAPNP_CFG_ACTIVATE);
  851. if (!dev->active)
  852. goto __end;
  853. for (i = 0; i < ISAPNP_MAX_PORT; i++) {
  854. ret = isapnp_read_word(ISAPNP_CFG_PORT + (i << 1));
  855. if (ret) {
  856. pnp_res = pnp_add_io_resource(dev, ret, ret, 0);
  857. if (pnp_res)
  858. pnp_res->index = i;
  859. }
  860. }
  861. for (i = 0; i < ISAPNP_MAX_MEM; i++) {
  862. ret = isapnp_read_word(ISAPNP_CFG_MEM + (i << 3)) << 8;
  863. if (ret) {
  864. pnp_res = pnp_add_mem_resource(dev, ret, ret, 0);
  865. if (pnp_res)
  866. pnp_res->index = i;
  867. }
  868. }
  869. for (i = 0; i < ISAPNP_MAX_IRQ; i++) {
  870. ret = isapnp_read_word(ISAPNP_CFG_IRQ + (i << 1)) >> 8;
  871. if (ret) {
  872. pnp_res = pnp_add_irq_resource(dev, ret, 0);
  873. if (pnp_res)
  874. pnp_res->index = i;
  875. }
  876. }
  877. for (i = 0; i < ISAPNP_MAX_DMA; i++) {
  878. ret = isapnp_read_byte(ISAPNP_CFG_DMA + i);
  879. if (ret != 4) {
  880. pnp_res = pnp_add_dma_resource(dev, ret, 0);
  881. if (pnp_res)
  882. pnp_res->index = i;
  883. }
  884. }
  885. __end:
  886. isapnp_cfg_end();
  887. return 0;
  888. }
  889. static int isapnp_set_resources(struct pnp_dev *dev)
  890. {
  891. struct pnp_resource *pnp_res;
  892. struct resource *res;
  893. int tmp, index;
  894. dev_dbg(&dev->dev, "set resources\n");
  895. isapnp_cfg_begin(dev->card->number, dev->number);
  896. dev->active = 1;
  897. for (tmp = 0; tmp < ISAPNP_MAX_PORT; tmp++) {
  898. pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IO, tmp);
  899. if (!pnp_res)
  900. continue;
  901. res = &pnp_res->res;
  902. if (pnp_resource_valid(res)) {
  903. index = pnp_res->index;
  904. dev_dbg(&dev->dev, " set io %d to %#llx\n",
  905. index, (unsigned long long) res->start);
  906. isapnp_write_word(ISAPNP_CFG_PORT + (index << 1),
  907. res->start);
  908. }
  909. }
  910. for (tmp = 0; tmp < ISAPNP_MAX_IRQ; tmp++) {
  911. pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_IRQ, tmp);
  912. if (!pnp_res)
  913. continue;
  914. res = &pnp_res->res;
  915. if (pnp_resource_valid(res)) {
  916. int irq = res->start;
  917. if (irq == 2)
  918. irq = 9;
  919. index = pnp_res->index;
  920. dev_dbg(&dev->dev, " set irq %d to %d\n", index, irq);
  921. isapnp_write_byte(ISAPNP_CFG_IRQ + (index << 1), irq);
  922. }
  923. }
  924. for (tmp = 0; tmp < ISAPNP_MAX_DMA; tmp++) {
  925. pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_DMA, tmp);
  926. if (!pnp_res)
  927. continue;
  928. res = &pnp_res->res;
  929. if (pnp_resource_valid(res)) {
  930. index = pnp_res->index;
  931. dev_dbg(&dev->dev, " set dma %d to %lld\n",
  932. index, (unsigned long long) res->start);
  933. isapnp_write_byte(ISAPNP_CFG_DMA + index, res->start);
  934. }
  935. }
  936. for (tmp = 0; tmp < ISAPNP_MAX_MEM; tmp++) {
  937. pnp_res = pnp_get_pnp_resource(dev, IORESOURCE_MEM, tmp);
  938. if (!pnp_res)
  939. continue;
  940. res = &pnp_res->res;
  941. if (pnp_resource_valid(res)) {
  942. index = pnp_res->index;
  943. dev_dbg(&dev->dev, " set mem %d to %#llx\n",
  944. index, (unsigned long long) res->start);
  945. isapnp_write_word(ISAPNP_CFG_MEM + (index << 3),
  946. (res->start >> 8) & 0xffff);
  947. }
  948. }
  949. /* FIXME: We aren't handling 32bit mems properly here */
  950. isapnp_activate(dev->number);
  951. isapnp_cfg_end();
  952. return 0;
  953. }
  954. static int isapnp_disable_resources(struct pnp_dev *dev)
  955. {
  956. if (!dev->active)
  957. return -EINVAL;
  958. isapnp_cfg_begin(dev->card->number, dev->number);
  959. isapnp_deactivate(dev->number);
  960. dev->active = 0;
  961. isapnp_cfg_end();
  962. return 0;
  963. }
  964. struct pnp_protocol isapnp_protocol = {
  965. .name = "ISA Plug and Play",
  966. .get = isapnp_get_resources,
  967. .set = isapnp_set_resources,
  968. .disable = isapnp_disable_resources,
  969. };
  970. static int __init isapnp_init(void)
  971. {
  972. int cards;
  973. struct pnp_card *card;
  974. struct pnp_dev *dev;
  975. if (isapnp_disable) {
  976. printk(KERN_INFO "isapnp: ISA Plug & Play support disabled\n");
  977. return 0;
  978. }
  979. #ifdef CONFIG_PPC_MERGE
  980. if (check_legacy_ioport(_PIDXR) || check_legacy_ioport(_PNPWRP))
  981. return -EINVAL;
  982. #endif
  983. #ifdef ISAPNP_REGION_OK
  984. if (!request_region(_PIDXR, 1, "isapnp index")) {
  985. printk(KERN_ERR "isapnp: Index Register 0x%x already used\n",
  986. _PIDXR);
  987. return -EBUSY;
  988. }
  989. #endif
  990. if (!request_region(_PNPWRP, 1, "isapnp write")) {
  991. printk(KERN_ERR
  992. "isapnp: Write Data Register 0x%x already used\n",
  993. _PNPWRP);
  994. #ifdef ISAPNP_REGION_OK
  995. release_region(_PIDXR, 1);
  996. #endif
  997. return -EBUSY;
  998. }
  999. if (pnp_register_protocol(&isapnp_protocol) < 0)
  1000. return -EBUSY;
  1001. /*
  1002. * Print a message. The existing ISAPnP code is hanging machines
  1003. * so let the user know where.
  1004. */
  1005. printk(KERN_INFO "isapnp: Scanning for PnP cards...\n");
  1006. if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
  1007. isapnp_rdp |= 3;
  1008. if (!request_region(isapnp_rdp, 1, "isapnp read")) {
  1009. printk(KERN_ERR
  1010. "isapnp: Read Data Register 0x%x already used\n",
  1011. isapnp_rdp);
  1012. #ifdef ISAPNP_REGION_OK
  1013. release_region(_PIDXR, 1);
  1014. #endif
  1015. release_region(_PNPWRP, 1);
  1016. return -EBUSY;
  1017. }
  1018. isapnp_set_rdp();
  1019. }
  1020. if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
  1021. cards = isapnp_isolate();
  1022. if (cards < 0 || (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
  1023. #ifdef ISAPNP_REGION_OK
  1024. release_region(_PIDXR, 1);
  1025. #endif
  1026. release_region(_PNPWRP, 1);
  1027. printk(KERN_INFO
  1028. "isapnp: No Plug & Play device found\n");
  1029. return 0;
  1030. }
  1031. request_region(isapnp_rdp, 1, "isapnp read");
  1032. }
  1033. isapnp_build_device_list();
  1034. cards = 0;
  1035. protocol_for_each_card(&isapnp_protocol, card) {
  1036. cards++;
  1037. if (isapnp_verbose) {
  1038. dev_info(&card->dev, "card '%s'\n",
  1039. card->name[0] ? card->name : "unknown");
  1040. if (isapnp_verbose < 2)
  1041. continue;
  1042. card_for_each_dev(card, dev) {
  1043. dev_info(&card->dev, "device '%s'\n",
  1044. dev->name[0] ? dev->name : "unknown");
  1045. }
  1046. }
  1047. }
  1048. if (cards)
  1049. printk(KERN_INFO
  1050. "isapnp: %i Plug & Play card%s detected total\n", cards,
  1051. cards > 1 ? "s" : "");
  1052. else
  1053. printk(KERN_INFO "isapnp: No Plug & Play card found\n");
  1054. isapnp_proc_init();
  1055. return 0;
  1056. }
  1057. device_initcall(isapnp_init);
  1058. /* format is: noisapnp */
  1059. static int __init isapnp_setup_disable(char *str)
  1060. {
  1061. isapnp_disable = 1;
  1062. return 1;
  1063. }
  1064. __setup("noisapnp", isapnp_setup_disable);
  1065. /* format is: isapnp=rdp,reset,skip_pci_scan,verbose */
  1066. static int __init isapnp_setup_isapnp(char *str)
  1067. {
  1068. (void)((get_option(&str, &isapnp_rdp) == 2) &&
  1069. (get_option(&str, &isapnp_reset) == 2) &&
  1070. (get_option(&str, &isapnp_verbose) == 2));
  1071. return 1;
  1072. }
  1073. __setup("isapnp=", isapnp_setup_isapnp);