core.c 27 KB

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