core.c 26 KB

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