core.c 24 KB

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