core.c 27 KB

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