core.c 26 KB

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