core.c 27 KB

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