core.c 27 KB

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