core.c 27 KB

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