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