core.c 27 KB

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