core.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109
  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 (*type == 0xff && *size == 0xffff) /* probably invalid data */
  337. return -1;
  338. return 0;
  339. }
  340. /*
  341. * Skip specified number of bytes from stream.
  342. */
  343. static void __init isapnp_skip_bytes(int count)
  344. {
  345. isapnp_peek(NULL, count);
  346. }
  347. /*
  348. * Parse logical device tag.
  349. */
  350. static struct pnp_dev *__init isapnp_parse_device(struct pnp_card *card,
  351. int size, int number)
  352. {
  353. unsigned char tmp[6];
  354. struct pnp_dev *dev;
  355. u32 eisa_id;
  356. char id[8];
  357. isapnp_peek(tmp, size);
  358. eisa_id = tmp[0] | tmp[1] << 8 | tmp[2] << 16 | tmp[3] << 24;
  359. pnp_eisa_id_to_string(eisa_id, id);
  360. dev = pnp_alloc_dev(&isapnp_protocol, number, id);
  361. if (!dev)
  362. return NULL;
  363. dev->card = card;
  364. dev->capabilities |= PNP_CONFIGURABLE;
  365. dev->capabilities |= PNP_READ;
  366. dev->capabilities |= PNP_WRITE;
  367. dev->capabilities |= PNP_DISABLE;
  368. pnp_init_resources(dev);
  369. return dev;
  370. }
  371. /*
  372. * Add IRQ resource to resources list.
  373. */
  374. static void __init isapnp_parse_irq_resource(struct pnp_dev *dev,
  375. unsigned int option_flags,
  376. int size)
  377. {
  378. unsigned char tmp[3];
  379. unsigned long bits;
  380. pnp_irq_mask_t map;
  381. unsigned char flags = IORESOURCE_IRQ_HIGHEDGE;
  382. isapnp_peek(tmp, size);
  383. bits = (tmp[1] << 8) | tmp[0];
  384. bitmap_zero(map.bits, PNP_IRQ_NR);
  385. bitmap_copy(map.bits, &bits, 16);
  386. if (size > 2)
  387. flags = tmp[2];
  388. pnp_register_irq_resource(dev, option_flags, &map, flags);
  389. }
  390. /*
  391. * Add DMA resource to resources list.
  392. */
  393. static void __init isapnp_parse_dma_resource(struct pnp_dev *dev,
  394. unsigned int option_flags,
  395. int size)
  396. {
  397. unsigned char tmp[2];
  398. isapnp_peek(tmp, size);
  399. pnp_register_dma_resource(dev, option_flags, tmp[0], tmp[1]);
  400. }
  401. /*
  402. * Add port resource to resources list.
  403. */
  404. static void __init isapnp_parse_port_resource(struct pnp_dev *dev,
  405. unsigned int option_flags,
  406. int size)
  407. {
  408. unsigned char tmp[7];
  409. resource_size_t min, max, align, len;
  410. unsigned char flags;
  411. isapnp_peek(tmp, size);
  412. min = (tmp[2] << 8) | tmp[1];
  413. max = (tmp[4] << 8) | tmp[3];
  414. align = tmp[5];
  415. len = tmp[6];
  416. flags = tmp[0] ? IORESOURCE_IO_16BIT_ADDR : 0;
  417. pnp_register_port_resource(dev, option_flags,
  418. min, max, align, len, flags);
  419. }
  420. /*
  421. * Add fixed port resource to resources list.
  422. */
  423. static void __init isapnp_parse_fixed_port_resource(struct pnp_dev *dev,
  424. unsigned int option_flags,
  425. int size)
  426. {
  427. unsigned char tmp[3];
  428. resource_size_t base, len;
  429. isapnp_peek(tmp, size);
  430. base = (tmp[1] << 8) | tmp[0];
  431. len = tmp[2];
  432. pnp_register_port_resource(dev, option_flags, base, base, 0, len,
  433. IORESOURCE_IO_FIXED);
  434. }
  435. /*
  436. * Add memory resource to resources list.
  437. */
  438. static void __init isapnp_parse_mem_resource(struct pnp_dev *dev,
  439. unsigned int option_flags,
  440. int size)
  441. {
  442. unsigned char tmp[9];
  443. resource_size_t min, max, align, len;
  444. unsigned char flags;
  445. isapnp_peek(tmp, size);
  446. min = ((tmp[2] << 8) | tmp[1]) << 8;
  447. max = ((tmp[4] << 8) | tmp[3]) << 8;
  448. align = (tmp[6] << 8) | tmp[5];
  449. len = ((tmp[8] << 8) | tmp[7]) << 8;
  450. flags = tmp[0];
  451. pnp_register_mem_resource(dev, option_flags,
  452. min, max, align, len, flags);
  453. }
  454. /*
  455. * Add 32-bit memory resource to resources list.
  456. */
  457. static void __init isapnp_parse_mem32_resource(struct pnp_dev *dev,
  458. unsigned int option_flags,
  459. int size)
  460. {
  461. unsigned char tmp[17];
  462. resource_size_t min, max, align, len;
  463. unsigned char flags;
  464. isapnp_peek(tmp, size);
  465. min = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
  466. max = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
  467. align = (tmp[12] << 24) | (tmp[11] << 16) | (tmp[10] << 8) | tmp[9];
  468. len = (tmp[16] << 24) | (tmp[15] << 16) | (tmp[14] << 8) | tmp[13];
  469. flags = tmp[0];
  470. pnp_register_mem_resource(dev, option_flags,
  471. min, max, align, len, flags);
  472. }
  473. /*
  474. * Add 32-bit fixed memory resource to resources list.
  475. */
  476. static void __init isapnp_parse_fixed_mem32_resource(struct pnp_dev *dev,
  477. unsigned int option_flags,
  478. int size)
  479. {
  480. unsigned char tmp[9];
  481. resource_size_t base, len;
  482. unsigned char flags;
  483. isapnp_peek(tmp, size);
  484. base = (tmp[4] << 24) | (tmp[3] << 16) | (tmp[2] << 8) | tmp[1];
  485. len = (tmp[8] << 24) | (tmp[7] << 16) | (tmp[6] << 8) | tmp[5];
  486. flags = tmp[0];
  487. pnp_register_mem_resource(dev, option_flags, base, base, 0, len, flags);
  488. }
  489. /*
  490. * Parse card name for ISA PnP device.
  491. */
  492. static void __init
  493. isapnp_parse_name(char *name, unsigned int name_max, unsigned short *size)
  494. {
  495. if (name[0] == '\0') {
  496. unsigned short size1 =
  497. *size >= name_max ? (name_max - 1) : *size;
  498. isapnp_peek(name, size1);
  499. name[size1] = '\0';
  500. *size -= size1;
  501. /* clean whitespace from end of string */
  502. while (size1 > 0 && name[--size1] == ' ')
  503. name[size1] = '\0';
  504. }
  505. }
  506. /*
  507. * Parse resource map for logical device.
  508. */
  509. static int __init isapnp_create_device(struct pnp_card *card,
  510. unsigned short size)
  511. {
  512. int number = 0, skip = 0, priority, compat = 0;
  513. unsigned char type, tmp[17];
  514. unsigned int option_flags;
  515. struct pnp_dev *dev;
  516. u32 eisa_id;
  517. char id[8];
  518. if ((dev = isapnp_parse_device(card, size, number++)) == NULL)
  519. return 1;
  520. option_flags = 0;
  521. pnp_add_card_device(card, dev);
  522. while (1) {
  523. if (isapnp_read_tag(&type, &size) < 0)
  524. return 1;
  525. if (skip && type != _STAG_LOGDEVID && type != _STAG_END)
  526. goto __skip;
  527. switch (type) {
  528. case _STAG_LOGDEVID:
  529. if (size >= 5 && size <= 6) {
  530. if ((dev =
  531. isapnp_parse_device(card, size,
  532. number++)) == NULL)
  533. return 1;
  534. size = 0;
  535. skip = 0;
  536. option_flags = 0;
  537. pnp_add_card_device(card, dev);
  538. } else {
  539. skip = 1;
  540. }
  541. compat = 0;
  542. break;
  543. case _STAG_COMPATDEVID:
  544. if (size == 4 && compat < DEVICE_COUNT_COMPATIBLE) {
  545. isapnp_peek(tmp, 4);
  546. eisa_id = tmp[0] | tmp[1] << 8 |
  547. tmp[2] << 16 | tmp[3] << 24;
  548. pnp_eisa_id_to_string(eisa_id, id);
  549. pnp_add_id(dev, id);
  550. compat++;
  551. size = 0;
  552. }
  553. break;
  554. case _STAG_IRQ:
  555. if (size < 2 || size > 3)
  556. goto __skip;
  557. isapnp_parse_irq_resource(dev, option_flags, size);
  558. size = 0;
  559. break;
  560. case _STAG_DMA:
  561. if (size != 2)
  562. goto __skip;
  563. isapnp_parse_dma_resource(dev, option_flags, size);
  564. size = 0;
  565. break;
  566. case _STAG_STARTDEP:
  567. if (size > 1)
  568. goto __skip;
  569. priority = PNP_RES_PRIORITY_ACCEPTABLE;
  570. if (size > 0) {
  571. isapnp_peek(tmp, size);
  572. priority = tmp[0];
  573. size = 0;
  574. }
  575. option_flags = pnp_new_dependent_set(dev, priority);
  576. break;
  577. case _STAG_ENDDEP:
  578. if (size != 0)
  579. goto __skip;
  580. option_flags = 0;
  581. break;
  582. case _STAG_IOPORT:
  583. if (size != 7)
  584. goto __skip;
  585. isapnp_parse_port_resource(dev, option_flags, size);
  586. size = 0;
  587. break;
  588. case _STAG_FIXEDIO:
  589. if (size != 3)
  590. goto __skip;
  591. isapnp_parse_fixed_port_resource(dev, option_flags,
  592. size);
  593. size = 0;
  594. break;
  595. case _STAG_VENDOR:
  596. break;
  597. case _LTAG_MEMRANGE:
  598. if (size != 9)
  599. goto __skip;
  600. isapnp_parse_mem_resource(dev, option_flags, size);
  601. size = 0;
  602. break;
  603. case _LTAG_ANSISTR:
  604. isapnp_parse_name(dev->name, sizeof(dev->name), &size);
  605. break;
  606. case _LTAG_UNICODESTR:
  607. /* silently ignore */
  608. /* who use unicode for hardware identification? */
  609. break;
  610. case _LTAG_VENDOR:
  611. break;
  612. case _LTAG_MEM32RANGE:
  613. if (size != 17)
  614. goto __skip;
  615. isapnp_parse_mem32_resource(dev, option_flags, size);
  616. size = 0;
  617. break;
  618. case _LTAG_FIXEDMEM32RANGE:
  619. if (size != 9)
  620. goto __skip;
  621. isapnp_parse_fixed_mem32_resource(dev, option_flags,
  622. size);
  623. size = 0;
  624. break;
  625. case _STAG_END:
  626. if (size > 0)
  627. isapnp_skip_bytes(size);
  628. return 1;
  629. default:
  630. dev_err(&dev->dev, "unknown tag %#x (card %i), "
  631. "ignored\n", type, card->number);
  632. }
  633. __skip:
  634. if (size > 0)
  635. isapnp_skip_bytes(size);
  636. }
  637. return 0;
  638. }
  639. /*
  640. * Parse resource map for ISA PnP card.
  641. */
  642. static void __init isapnp_parse_resource_map(struct pnp_card *card)
  643. {
  644. unsigned char type, tmp[17];
  645. unsigned short size;
  646. while (1) {
  647. if (isapnp_read_tag(&type, &size) < 0)
  648. return;
  649. switch (type) {
  650. case _STAG_PNPVERNO:
  651. if (size != 2)
  652. goto __skip;
  653. isapnp_peek(tmp, 2);
  654. card->pnpver = tmp[0];
  655. card->productver = tmp[1];
  656. size = 0;
  657. break;
  658. case _STAG_LOGDEVID:
  659. if (size >= 5 && size <= 6) {
  660. if (isapnp_create_device(card, size) == 1)
  661. return;
  662. size = 0;
  663. }
  664. break;
  665. case _STAG_VENDOR:
  666. break;
  667. case _LTAG_ANSISTR:
  668. isapnp_parse_name(card->name, sizeof(card->name),
  669. &size);
  670. break;
  671. case _LTAG_UNICODESTR:
  672. /* silently ignore */
  673. /* who use unicode for hardware identification? */
  674. break;
  675. case _LTAG_VENDOR:
  676. break;
  677. case _STAG_END:
  678. if (size > 0)
  679. isapnp_skip_bytes(size);
  680. return;
  681. default:
  682. dev_err(&card->dev, "unknown tag %#x, ignored\n",
  683. type);
  684. }
  685. __skip:
  686. if (size > 0)
  687. isapnp_skip_bytes(size);
  688. }
  689. }
  690. /*
  691. * Compute ISA PnP checksum for first eight bytes.
  692. */
  693. static unsigned char __init isapnp_checksum(unsigned char *data)
  694. {
  695. int i, j;
  696. unsigned char checksum = 0x6a, bit, b;
  697. for (i = 0; i < 8; i++) {
  698. b = data[i];
  699. for (j = 0; j < 8; j++) {
  700. bit = 0;
  701. if (b & (1 << j))
  702. bit = 1;
  703. checksum =
  704. ((((checksum ^ (checksum >> 1)) & 0x01) ^ bit) << 7)
  705. | (checksum >> 1);
  706. }
  707. }
  708. return checksum;
  709. }
  710. /*
  711. * Build device list for all present ISA PnP devices.
  712. */
  713. static int __init isapnp_build_device_list(void)
  714. {
  715. int csn;
  716. unsigned char header[9], checksum;
  717. struct pnp_card *card;
  718. u32 eisa_id;
  719. char id[8];
  720. isapnp_wait();
  721. isapnp_key();
  722. for (csn = 1; csn <= isapnp_csn_count; csn++) {
  723. isapnp_wake(csn);
  724. isapnp_peek(header, 9);
  725. checksum = isapnp_checksum(header);
  726. eisa_id = header[0] | header[1] << 8 |
  727. header[2] << 16 | header[3] << 24;
  728. pnp_eisa_id_to_string(eisa_id, id);
  729. card = pnp_alloc_card(&isapnp_protocol, csn, id);
  730. if (!card)
  731. continue;
  732. INIT_LIST_HEAD(&card->devices);
  733. card->serial =
  734. (header[7] << 24) | (header[6] << 16) | (header[5] << 8) |
  735. header[4];
  736. isapnp_checksum_value = 0x00;
  737. isapnp_parse_resource_map(card);
  738. if (isapnp_checksum_value != 0x00)
  739. dev_err(&card->dev, "invalid checksum %#x\n",
  740. isapnp_checksum_value);
  741. card->checksum = isapnp_checksum_value;
  742. pnp_add_card(card);
  743. }
  744. isapnp_wait();
  745. return 0;
  746. }
  747. /*
  748. * Basic configuration routines.
  749. */
  750. int isapnp_present(void)
  751. {
  752. struct pnp_card *card;
  753. pnp_for_each_card(card) {
  754. if (card->protocol == &isapnp_protocol)
  755. return 1;
  756. }
  757. return 0;
  758. }
  759. int isapnp_cfg_begin(int csn, int logdev)
  760. {
  761. if (csn < 1 || csn > isapnp_csn_count || logdev > 10)
  762. return -EINVAL;
  763. mutex_lock(&isapnp_cfg_mutex);
  764. isapnp_wait();
  765. isapnp_key();
  766. isapnp_wake(csn);
  767. #if 0
  768. /* to avoid malfunction when the isapnptools package is used */
  769. /* we must set RDP to our value again */
  770. /* it is possible to set RDP only in the isolation phase */
  771. /* Jens Thoms Toerring <Jens.Toerring@physik.fu-berlin.de> */
  772. isapnp_write_byte(0x02, 0x04); /* clear CSN of card */
  773. mdelay(2); /* is this necessary? */
  774. isapnp_wake(csn); /* bring card into sleep state */
  775. isapnp_wake(0); /* bring card into isolation state */
  776. isapnp_set_rdp(); /* reset the RDP port */
  777. udelay(1000); /* delay 1000us */
  778. isapnp_write_byte(0x06, csn); /* reset CSN to previous value */
  779. udelay(250); /* is this necessary? */
  780. #endif
  781. if (logdev >= 0)
  782. isapnp_device(logdev);
  783. return 0;
  784. }
  785. int isapnp_cfg_end(void)
  786. {
  787. isapnp_wait();
  788. mutex_unlock(&isapnp_cfg_mutex);
  789. return 0;
  790. }
  791. /*
  792. * Initialization.
  793. */
  794. EXPORT_SYMBOL(isapnp_protocol);
  795. EXPORT_SYMBOL(isapnp_present);
  796. EXPORT_SYMBOL(isapnp_cfg_begin);
  797. EXPORT_SYMBOL(isapnp_cfg_end);
  798. EXPORT_SYMBOL(isapnp_write_byte);
  799. static int isapnp_get_resources(struct pnp_dev *dev)
  800. {
  801. int i, ret;
  802. pnp_dbg(&dev->dev, "get resources\n");
  803. pnp_init_resources(dev);
  804. isapnp_cfg_begin(dev->card->number, dev->number);
  805. dev->active = isapnp_read_byte(ISAPNP_CFG_ACTIVATE);
  806. if (!dev->active)
  807. goto __end;
  808. for (i = 0; i < ISAPNP_MAX_PORT; i++) {
  809. ret = isapnp_read_word(ISAPNP_CFG_PORT + (i << 1));
  810. pnp_add_io_resource(dev, ret, ret,
  811. ret == 0 ? IORESOURCE_DISABLED : 0);
  812. }
  813. for (i = 0; i < ISAPNP_MAX_MEM; i++) {
  814. ret = isapnp_read_word(ISAPNP_CFG_MEM + (i << 3)) << 8;
  815. pnp_add_mem_resource(dev, ret, ret,
  816. ret == 0 ? IORESOURCE_DISABLED : 0);
  817. }
  818. for (i = 0; i < ISAPNP_MAX_IRQ; i++) {
  819. ret = isapnp_read_word(ISAPNP_CFG_IRQ + (i << 1)) >> 8;
  820. pnp_add_irq_resource(dev, ret,
  821. ret == 0 ? IORESOURCE_DISABLED : 0);
  822. }
  823. for (i = 0; i < ISAPNP_MAX_DMA; i++) {
  824. ret = isapnp_read_byte(ISAPNP_CFG_DMA + i);
  825. pnp_add_dma_resource(dev, ret,
  826. ret == 4 ? IORESOURCE_DISABLED : 0);
  827. }
  828. __end:
  829. isapnp_cfg_end();
  830. return 0;
  831. }
  832. static int isapnp_set_resources(struct pnp_dev *dev)
  833. {
  834. struct resource *res;
  835. int tmp;
  836. pnp_dbg(&dev->dev, "set resources\n");
  837. isapnp_cfg_begin(dev->card->number, dev->number);
  838. dev->active = 1;
  839. for (tmp = 0; tmp < ISAPNP_MAX_PORT; tmp++) {
  840. res = pnp_get_resource(dev, IORESOURCE_IO, tmp);
  841. if (pnp_resource_enabled(res)) {
  842. pnp_dbg(&dev->dev, " set io %d to %#llx\n",
  843. tmp, (unsigned long long) res->start);
  844. isapnp_write_word(ISAPNP_CFG_PORT + (tmp << 1),
  845. res->start);
  846. }
  847. }
  848. for (tmp = 0; tmp < ISAPNP_MAX_IRQ; tmp++) {
  849. res = pnp_get_resource(dev, IORESOURCE_IRQ, tmp);
  850. if (pnp_resource_enabled(res)) {
  851. int irq = res->start;
  852. if (irq == 2)
  853. irq = 9;
  854. pnp_dbg(&dev->dev, " set irq %d to %d\n", tmp, irq);
  855. isapnp_write_byte(ISAPNP_CFG_IRQ + (tmp << 1), irq);
  856. }
  857. }
  858. for (tmp = 0; tmp < ISAPNP_MAX_DMA; tmp++) {
  859. res = pnp_get_resource(dev, IORESOURCE_DMA, tmp);
  860. if (pnp_resource_enabled(res)) {
  861. pnp_dbg(&dev->dev, " set dma %d to %lld\n",
  862. tmp, (unsigned long long) res->start);
  863. isapnp_write_byte(ISAPNP_CFG_DMA + tmp, res->start);
  864. }
  865. }
  866. for (tmp = 0; tmp < ISAPNP_MAX_MEM; tmp++) {
  867. res = pnp_get_resource(dev, IORESOURCE_MEM, tmp);
  868. if (pnp_resource_enabled(res)) {
  869. pnp_dbg(&dev->dev, " set mem %d to %#llx\n",
  870. tmp, (unsigned long long) res->start);
  871. isapnp_write_word(ISAPNP_CFG_MEM + (tmp << 3),
  872. (res->start >> 8) & 0xffff);
  873. }
  874. }
  875. /* FIXME: We aren't handling 32bit mems properly here */
  876. isapnp_activate(dev->number);
  877. isapnp_cfg_end();
  878. return 0;
  879. }
  880. static int isapnp_disable_resources(struct pnp_dev *dev)
  881. {
  882. if (!dev->active)
  883. return -EINVAL;
  884. isapnp_cfg_begin(dev->card->number, dev->number);
  885. isapnp_deactivate(dev->number);
  886. dev->active = 0;
  887. isapnp_cfg_end();
  888. return 0;
  889. }
  890. struct pnp_protocol isapnp_protocol = {
  891. .name = "ISA Plug and Play",
  892. .get = isapnp_get_resources,
  893. .set = isapnp_set_resources,
  894. .disable = isapnp_disable_resources,
  895. };
  896. static int __init isapnp_init(void)
  897. {
  898. int cards;
  899. struct pnp_card *card;
  900. struct pnp_dev *dev;
  901. if (isapnp_disable) {
  902. printk(KERN_INFO "isapnp: ISA Plug & Play support disabled\n");
  903. return 0;
  904. }
  905. #ifdef CONFIG_PPC
  906. if (check_legacy_ioport(_PIDXR) || check_legacy_ioport(_PNPWRP))
  907. return -EINVAL;
  908. #endif
  909. #ifdef ISAPNP_REGION_OK
  910. if (!request_region(_PIDXR, 1, "isapnp index")) {
  911. printk(KERN_ERR "isapnp: Index Register 0x%x already used\n",
  912. _PIDXR);
  913. return -EBUSY;
  914. }
  915. #endif
  916. if (!request_region(_PNPWRP, 1, "isapnp write")) {
  917. printk(KERN_ERR
  918. "isapnp: Write Data Register 0x%x already used\n",
  919. _PNPWRP);
  920. #ifdef ISAPNP_REGION_OK
  921. release_region(_PIDXR, 1);
  922. #endif
  923. return -EBUSY;
  924. }
  925. if (pnp_register_protocol(&isapnp_protocol) < 0)
  926. return -EBUSY;
  927. /*
  928. * Print a message. The existing ISAPnP code is hanging machines
  929. * so let the user know where.
  930. */
  931. printk(KERN_INFO "isapnp: Scanning for PnP cards...\n");
  932. if (isapnp_rdp >= 0x203 && isapnp_rdp <= 0x3ff) {
  933. isapnp_rdp |= 3;
  934. if (!request_region(isapnp_rdp, 1, "isapnp read")) {
  935. printk(KERN_ERR
  936. "isapnp: Read Data Register 0x%x already used\n",
  937. isapnp_rdp);
  938. #ifdef ISAPNP_REGION_OK
  939. release_region(_PIDXR, 1);
  940. #endif
  941. release_region(_PNPWRP, 1);
  942. return -EBUSY;
  943. }
  944. isapnp_set_rdp();
  945. }
  946. if (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff) {
  947. cards = isapnp_isolate();
  948. if (cards < 0 || (isapnp_rdp < 0x203 || isapnp_rdp > 0x3ff)) {
  949. #ifdef ISAPNP_REGION_OK
  950. release_region(_PIDXR, 1);
  951. #endif
  952. release_region(_PNPWRP, 1);
  953. printk(KERN_INFO
  954. "isapnp: No Plug & Play device found\n");
  955. return 0;
  956. }
  957. request_region(isapnp_rdp, 1, "isapnp read");
  958. }
  959. isapnp_build_device_list();
  960. cards = 0;
  961. protocol_for_each_card(&isapnp_protocol, card) {
  962. cards++;
  963. if (isapnp_verbose) {
  964. dev_info(&card->dev, "card '%s'\n",
  965. card->name[0] ? card->name : "unknown");
  966. if (isapnp_verbose < 2)
  967. continue;
  968. card_for_each_dev(card, dev) {
  969. dev_info(&card->dev, "device '%s'\n",
  970. dev->name[0] ? dev->name : "unknown");
  971. }
  972. }
  973. }
  974. if (cards)
  975. printk(KERN_INFO
  976. "isapnp: %i Plug & Play card%s detected total\n", cards,
  977. cards > 1 ? "s" : "");
  978. else
  979. printk(KERN_INFO "isapnp: No Plug & Play card found\n");
  980. isapnp_proc_init();
  981. return 0;
  982. }
  983. device_initcall(isapnp_init);
  984. /* format is: noisapnp */
  985. static int __init isapnp_setup_disable(char *str)
  986. {
  987. isapnp_disable = 1;
  988. return 1;
  989. }
  990. __setup("noisapnp", isapnp_setup_disable);
  991. /* format is: isapnp=rdp,reset,skip_pci_scan,verbose */
  992. static int __init isapnp_setup_isapnp(char *str)
  993. {
  994. (void)((get_option(&str, &isapnp_rdp) == 2) &&
  995. (get_option(&str, &isapnp_reset) == 2) &&
  996. (get_option(&str, &isapnp_verbose) == 2));
  997. return 1;
  998. }
  999. __setup("isapnp=", isapnp_setup_isapnp);