core.c 25 KB

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