spectrum_cs.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * Driver for 802.11b cards using RAM-loadable Symbol firmware, such as
  3. * Symbol Wireless Networker LA4100, CompactFlash cards by Socket
  4. * Communications and Intel PRO/Wireless 2011B.
  5. *
  6. * The driver implements Symbol firmware download. The rest is handled
  7. * in hermes.c and orinoco.c.
  8. *
  9. * Utilities for downloading the Symbol firmware are available at
  10. * http://sourceforge.net/projects/orinoco/
  11. *
  12. * Copyright (C) 2002-2005 Pavel Roskin <proski@gnu.org>
  13. * Portions based on orinoco_cs.c:
  14. * Copyright (C) David Gibson, Linuxcare Australia
  15. * Portions based on Spectrum24tDnld.c from original spectrum24 driver:
  16. * Copyright (C) Symbol Technologies.
  17. *
  18. * See copyright notice in file orinoco.c.
  19. */
  20. #define DRIVER_NAME "spectrum_cs"
  21. #define PFX DRIVER_NAME ": "
  22. #include <linux/config.h>
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/init.h>
  26. #include <linux/delay.h>
  27. #include <linux/firmware.h>
  28. #include <pcmcia/cs_types.h>
  29. #include <pcmcia/cs.h>
  30. #include <pcmcia/cistpl.h>
  31. #include <pcmcia/cisreg.h>
  32. #include <pcmcia/ds.h>
  33. #include "orinoco.h"
  34. static unsigned char *primsym;
  35. static unsigned char *secsym;
  36. static const char primary_fw_name[] = "symbol_sp24t_prim_fw";
  37. static const char secondary_fw_name[] = "symbol_sp24t_sec_fw";
  38. /********************************************************************/
  39. /* Module stuff */
  40. /********************************************************************/
  41. MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
  42. MODULE_DESCRIPTION("Driver for Symbol Spectrum24 Trilogy cards with firmware downloader");
  43. MODULE_LICENSE("Dual MPL/GPL");
  44. /* Module parameters */
  45. /* Some D-Link cards have buggy CIS. They do work at 5v properly, but
  46. * don't have any CIS entry for it. This workaround it... */
  47. static int ignore_cis_vcc; /* = 0 */
  48. module_param(ignore_cis_vcc, int, 0);
  49. MODULE_PARM_DESC(ignore_cis_vcc, "Allow voltage mismatch between card and socket");
  50. /********************************************************************/
  51. /* Data structures */
  52. /********************************************************************/
  53. /* PCMCIA specific device information (goes in the card field of
  54. * struct orinoco_private */
  55. struct orinoco_pccard {
  56. dev_link_t link;
  57. dev_node_t node;
  58. };
  59. /********************************************************************/
  60. /* Function prototypes */
  61. /********************************************************************/
  62. static void spectrum_cs_config(dev_link_t *link);
  63. static void spectrum_cs_release(dev_link_t *link);
  64. /********************************************************************/
  65. /* Firmware downloader */
  66. /********************************************************************/
  67. /* Position of PDA in the adapter memory */
  68. #define EEPROM_ADDR 0x3000
  69. #define EEPROM_LEN 0x200
  70. #define PDA_OFFSET 0x100
  71. #define PDA_ADDR (EEPROM_ADDR + PDA_OFFSET)
  72. #define PDA_WORDS ((EEPROM_LEN - PDA_OFFSET) / 2)
  73. /* Constants for the CISREG_CCSR register */
  74. #define HCR_RUN 0x07 /* run firmware after reset */
  75. #define HCR_IDLE 0x0E /* don't run firmware after reset */
  76. #define HCR_MEM16 0x10 /* memory width bit, should be preserved */
  77. /*
  78. * AUX port access. To unlock the AUX port write the access keys to the
  79. * PARAM0-2 registers, then write HERMES_AUX_ENABLE to the HERMES_CONTROL
  80. * register. Then read it and make sure it's HERMES_AUX_ENABLED.
  81. */
  82. #define HERMES_AUX_ENABLE 0x8000 /* Enable auxiliary port access */
  83. #define HERMES_AUX_DISABLE 0x4000 /* Disable to auxiliary port access */
  84. #define HERMES_AUX_ENABLED 0xC000 /* Auxiliary port is open */
  85. #define HERMES_AUX_PW0 0xFE01
  86. #define HERMES_AUX_PW1 0xDC23
  87. #define HERMES_AUX_PW2 0xBA45
  88. /* End markers */
  89. #define PDI_END 0x00000000 /* End of PDA */
  90. #define BLOCK_END 0xFFFFFFFF /* Last image block */
  91. #define TEXT_END 0x1A /* End of text header */
  92. /*
  93. * The following structures have little-endian fields denoted by
  94. * the leading underscore. Don't access them directly - use inline
  95. * functions defined below.
  96. */
  97. /*
  98. * The binary image to be downloaded consists of series of data blocks.
  99. * Each block has the following structure.
  100. */
  101. struct dblock {
  102. __le32 _addr; /* adapter address where to write the block */
  103. __le16 _len; /* length of the data only, in bytes */
  104. char data[0]; /* data to be written */
  105. } __attribute__ ((packed));
  106. /*
  107. * Plug Data References are located in in the image after the last data
  108. * block. They refer to areas in the adapter memory where the plug data
  109. * items with matching ID should be written.
  110. */
  111. struct pdr {
  112. __le32 _id; /* record ID */
  113. __le32 _addr; /* adapter address where to write the data */
  114. __le32 _len; /* expected length of the data, in bytes */
  115. char next[0]; /* next PDR starts here */
  116. } __attribute__ ((packed));
  117. /*
  118. * Plug Data Items are located in the EEPROM read from the adapter by
  119. * primary firmware. They refer to the device-specific data that should
  120. * be plugged into the secondary firmware.
  121. */
  122. struct pdi {
  123. __le16 _len; /* length of ID and data, in words */
  124. __le16 _id; /* record ID */
  125. char data[0]; /* plug data */
  126. } __attribute__ ((packed));;
  127. /* Functions for access to little-endian data */
  128. static inline u32
  129. dblock_addr(const struct dblock *blk)
  130. {
  131. return le32_to_cpu(blk->_addr);
  132. }
  133. static inline u32
  134. dblock_len(const struct dblock *blk)
  135. {
  136. return le16_to_cpu(blk->_len);
  137. }
  138. static inline u32
  139. pdr_id(const struct pdr *pdr)
  140. {
  141. return le32_to_cpu(pdr->_id);
  142. }
  143. static inline u32
  144. pdr_addr(const struct pdr *pdr)
  145. {
  146. return le32_to_cpu(pdr->_addr);
  147. }
  148. static inline u32
  149. pdr_len(const struct pdr *pdr)
  150. {
  151. return le32_to_cpu(pdr->_len);
  152. }
  153. static inline u32
  154. pdi_id(const struct pdi *pdi)
  155. {
  156. return le16_to_cpu(pdi->_id);
  157. }
  158. /* Return length of the data only, in bytes */
  159. static inline u32
  160. pdi_len(const struct pdi *pdi)
  161. {
  162. return 2 * (le16_to_cpu(pdi->_len) - 1);
  163. }
  164. /* Set address of the auxiliary port */
  165. static inline void
  166. spectrum_aux_setaddr(hermes_t *hw, u32 addr)
  167. {
  168. hermes_write_reg(hw, HERMES_AUXPAGE, (u16) (addr >> 7));
  169. hermes_write_reg(hw, HERMES_AUXOFFSET, (u16) (addr & 0x7F));
  170. }
  171. /* Open access to the auxiliary port */
  172. static int
  173. spectrum_aux_open(hermes_t *hw)
  174. {
  175. int i;
  176. /* Already open? */
  177. if (hermes_read_reg(hw, HERMES_CONTROL) == HERMES_AUX_ENABLED)
  178. return 0;
  179. hermes_write_reg(hw, HERMES_PARAM0, HERMES_AUX_PW0);
  180. hermes_write_reg(hw, HERMES_PARAM1, HERMES_AUX_PW1);
  181. hermes_write_reg(hw, HERMES_PARAM2, HERMES_AUX_PW2);
  182. hermes_write_reg(hw, HERMES_CONTROL, HERMES_AUX_ENABLE);
  183. for (i = 0; i < 20; i++) {
  184. udelay(10);
  185. if (hermes_read_reg(hw, HERMES_CONTROL) ==
  186. HERMES_AUX_ENABLED)
  187. return 0;
  188. }
  189. return -EBUSY;
  190. }
  191. #define CS_CHECK(fn, ret) \
  192. do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
  193. /*
  194. * Reset the card using configuration registers COR and CCSR.
  195. * If IDLE is 1, stop the firmware, so that it can be safely rewritten.
  196. */
  197. static int
  198. spectrum_reset(dev_link_t *link, int idle)
  199. {
  200. int last_ret, last_fn;
  201. conf_reg_t reg;
  202. u_int save_cor;
  203. /* Doing it if hardware is gone is guaranteed crash */
  204. if (!(link->state & DEV_CONFIG))
  205. return -ENODEV;
  206. /* Save original COR value */
  207. reg.Function = 0;
  208. reg.Action = CS_READ;
  209. reg.Offset = CISREG_COR;
  210. CS_CHECK(AccessConfigurationRegister,
  211. pcmcia_access_configuration_register(link->handle, &reg));
  212. save_cor = reg.Value;
  213. /* Soft-Reset card */
  214. reg.Action = CS_WRITE;
  215. reg.Offset = CISREG_COR;
  216. reg.Value = (save_cor | COR_SOFT_RESET);
  217. CS_CHECK(AccessConfigurationRegister,
  218. pcmcia_access_configuration_register(link->handle, &reg));
  219. udelay(1000);
  220. /* Read CCSR */
  221. reg.Action = CS_READ;
  222. reg.Offset = CISREG_CCSR;
  223. CS_CHECK(AccessConfigurationRegister,
  224. pcmcia_access_configuration_register(link->handle, &reg));
  225. /*
  226. * Start or stop the firmware. Memory width bit should be
  227. * preserved from the value we've just read.
  228. */
  229. reg.Action = CS_WRITE;
  230. reg.Offset = CISREG_CCSR;
  231. reg.Value = (idle ? HCR_IDLE : HCR_RUN) | (reg.Value & HCR_MEM16);
  232. CS_CHECK(AccessConfigurationRegister,
  233. pcmcia_access_configuration_register(link->handle, &reg));
  234. udelay(1000);
  235. /* Restore original COR configuration index */
  236. reg.Action = CS_WRITE;
  237. reg.Offset = CISREG_COR;
  238. reg.Value = (save_cor & ~COR_SOFT_RESET);
  239. CS_CHECK(AccessConfigurationRegister,
  240. pcmcia_access_configuration_register(link->handle, &reg));
  241. udelay(1000);
  242. return 0;
  243. cs_failed:
  244. cs_error(link->handle, last_fn, last_ret);
  245. return -ENODEV;
  246. }
  247. /*
  248. * Scan PDR for the record with the specified RECORD_ID.
  249. * If it's not found, return NULL.
  250. */
  251. static struct pdr *
  252. spectrum_find_pdr(struct pdr *first_pdr, u32 record_id)
  253. {
  254. struct pdr *pdr = first_pdr;
  255. while (pdr_id(pdr) != PDI_END) {
  256. /*
  257. * PDR area is currently not terminated by PDI_END.
  258. * It's followed by CRC records, which have the type
  259. * field where PDR has length. The type can be 0 or 1.
  260. */
  261. if (pdr_len(pdr) < 2)
  262. return NULL;
  263. /* If the record ID matches, we are done */
  264. if (pdr_id(pdr) == record_id)
  265. return pdr;
  266. pdr = (struct pdr *) pdr->next;
  267. }
  268. return NULL;
  269. }
  270. /* Process one Plug Data Item - find corresponding PDR and plug it */
  271. static int
  272. spectrum_plug_pdi(hermes_t *hw, struct pdr *first_pdr, struct pdi *pdi)
  273. {
  274. struct pdr *pdr;
  275. /* Find the PDI corresponding to this PDR */
  276. pdr = spectrum_find_pdr(first_pdr, pdi_id(pdi));
  277. /* No match is found, safe to ignore */
  278. if (!pdr)
  279. return 0;
  280. /* Lengths of the data in PDI and PDR must match */
  281. if (pdi_len(pdi) != pdr_len(pdr))
  282. return -EINVAL;
  283. /* do the actual plugging */
  284. spectrum_aux_setaddr(hw, pdr_addr(pdr));
  285. hermes_write_words(hw, HERMES_AUXDATA, pdi->data,
  286. pdi_len(pdi) / 2);
  287. return 0;
  288. }
  289. /* Read PDA from the adapter */
  290. static int
  291. spectrum_read_pda(hermes_t *hw, __le16 *pda, int pda_len)
  292. {
  293. int ret;
  294. int pda_size;
  295. /* Issue command to read EEPROM */
  296. ret = hermes_docmd_wait(hw, HERMES_CMD_READMIF, 0, NULL);
  297. if (ret)
  298. return ret;
  299. /* Open auxiliary port */
  300. ret = spectrum_aux_open(hw);
  301. if (ret)
  302. return ret;
  303. /* read PDA from EEPROM */
  304. spectrum_aux_setaddr(hw, PDA_ADDR);
  305. hermes_read_words(hw, HERMES_AUXDATA, pda, pda_len / 2);
  306. /* Check PDA length */
  307. pda_size = le16_to_cpu(pda[0]);
  308. if (pda_size > pda_len)
  309. return -EINVAL;
  310. return 0;
  311. }
  312. /* Parse PDA and write the records into the adapter */
  313. static int
  314. spectrum_apply_pda(hermes_t *hw, const struct dblock *first_block,
  315. __le16 *pda)
  316. {
  317. int ret;
  318. struct pdi *pdi;
  319. struct pdr *first_pdr;
  320. const struct dblock *blk = first_block;
  321. /* Skip all blocks to locate Plug Data References */
  322. while (dblock_addr(blk) != BLOCK_END)
  323. blk = (struct dblock *) &blk->data[dblock_len(blk)];
  324. first_pdr = (struct pdr *) blk;
  325. /* Go through every PDI and plug them into the adapter */
  326. pdi = (struct pdi *) (pda + 2);
  327. while (pdi_id(pdi) != PDI_END) {
  328. ret = spectrum_plug_pdi(hw, first_pdr, pdi);
  329. if (ret)
  330. return ret;
  331. /* Increment to the next PDI */
  332. pdi = (struct pdi *) &pdi->data[pdi_len(pdi)];
  333. }
  334. return 0;
  335. }
  336. /* Load firmware blocks into the adapter */
  337. static int
  338. spectrum_load_blocks(hermes_t *hw, const struct dblock *first_block)
  339. {
  340. const struct dblock *blk;
  341. u32 blkaddr;
  342. u32 blklen;
  343. blk = first_block;
  344. blkaddr = dblock_addr(blk);
  345. blklen = dblock_len(blk);
  346. while (dblock_addr(blk) != BLOCK_END) {
  347. spectrum_aux_setaddr(hw, blkaddr);
  348. hermes_write_words(hw, HERMES_AUXDATA, blk->data,
  349. blklen / 2);
  350. blk = (struct dblock *) &blk->data[blklen];
  351. blkaddr = dblock_addr(blk);
  352. blklen = dblock_len(blk);
  353. }
  354. return 0;
  355. }
  356. /*
  357. * Process a firmware image - stop the card, load the firmware, reset
  358. * the card and make sure it responds. For the secondary firmware take
  359. * care of the PDA - read it and then write it on top of the firmware.
  360. */
  361. static int
  362. spectrum_dl_image(hermes_t *hw, dev_link_t *link,
  363. const unsigned char *image)
  364. {
  365. int ret;
  366. const unsigned char *ptr;
  367. const struct dblock *first_block;
  368. /* Plug Data Area (PDA) */
  369. __le16 pda[PDA_WORDS];
  370. /* Binary block begins after the 0x1A marker */
  371. ptr = image;
  372. while (*ptr++ != TEXT_END);
  373. first_block = (const struct dblock *) ptr;
  374. /* Read the PDA */
  375. if (image != primsym) {
  376. ret = spectrum_read_pda(hw, pda, sizeof(pda));
  377. if (ret)
  378. return ret;
  379. }
  380. /* Stop the firmware, so that it can be safely rewritten */
  381. ret = spectrum_reset(link, 1);
  382. if (ret)
  383. return ret;
  384. /* Program the adapter with new firmware */
  385. ret = spectrum_load_blocks(hw, first_block);
  386. if (ret)
  387. return ret;
  388. /* Write the PDA to the adapter */
  389. if (image != primsym) {
  390. ret = spectrum_apply_pda(hw, first_block, pda);
  391. if (ret)
  392. return ret;
  393. }
  394. /* Run the firmware */
  395. ret = spectrum_reset(link, 0);
  396. if (ret)
  397. return ret;
  398. /* Reset hermes chip and make sure it responds */
  399. ret = hermes_init(hw);
  400. /* hermes_reset() should return 0 with the secondary firmware */
  401. if (image != primsym && ret != 0)
  402. return -ENODEV;
  403. /* And this should work with any firmware */
  404. if (!hermes_present(hw))
  405. return -ENODEV;
  406. return 0;
  407. }
  408. /*
  409. * Download the firmware into the card, this also does a PCMCIA soft
  410. * reset on the card, to make sure it's in a sane state.
  411. */
  412. static int
  413. spectrum_dl_firmware(hermes_t *hw, dev_link_t *link)
  414. {
  415. int ret;
  416. client_handle_t handle = link->handle;
  417. const struct firmware *fw_entry;
  418. if (request_firmware(&fw_entry, primary_fw_name,
  419. &handle_to_dev(handle)) == 0) {
  420. primsym = fw_entry->data;
  421. } else {
  422. printk(KERN_ERR PFX "Cannot find firmware: %s\n",
  423. primary_fw_name);
  424. return -ENOENT;
  425. }
  426. if (request_firmware(&fw_entry, secondary_fw_name,
  427. &handle_to_dev(handle)) == 0) {
  428. secsym = fw_entry->data;
  429. } else {
  430. printk(KERN_ERR PFX "Cannot find firmware: %s\n",
  431. secondary_fw_name);
  432. return -ENOENT;
  433. }
  434. /* Load primary firmware */
  435. ret = spectrum_dl_image(hw, link, primsym);
  436. if (ret) {
  437. printk(KERN_ERR PFX "Primary firmware download failed\n");
  438. return ret;
  439. }
  440. /* Load secondary firmware */
  441. ret = spectrum_dl_image(hw, link, secsym);
  442. if (ret) {
  443. printk(KERN_ERR PFX "Secondary firmware download failed\n");
  444. }
  445. return ret;
  446. }
  447. /********************************************************************/
  448. /* Device methods */
  449. /********************************************************************/
  450. static int
  451. spectrum_cs_hard_reset(struct orinoco_private *priv)
  452. {
  453. struct orinoco_pccard *card = priv->card;
  454. dev_link_t *link = &card->link;
  455. int err;
  456. if (!hermes_present(&priv->hw)) {
  457. /* The firmware needs to be reloaded */
  458. if (spectrum_dl_firmware(&priv->hw, &card->link) != 0) {
  459. printk(KERN_ERR PFX "Firmware download failed\n");
  460. err = -ENODEV;
  461. }
  462. } else {
  463. /* Soft reset using COR and HCR */
  464. spectrum_reset(link, 0);
  465. }
  466. return 0;
  467. }
  468. /********************************************************************/
  469. /* PCMCIA stuff */
  470. /********************************************************************/
  471. /*
  472. * This creates an "instance" of the driver, allocating local data
  473. * structures for one device. The device is registered with Card
  474. * Services.
  475. *
  476. * The dev_link structure is initialized, but we don't actually
  477. * configure the card at this point -- we wait until we receive a card
  478. * insertion event. */
  479. static int
  480. spectrum_cs_attach(struct pcmcia_device *p_dev)
  481. {
  482. struct net_device *dev;
  483. struct orinoco_private *priv;
  484. struct orinoco_pccard *card;
  485. dev_link_t *link;
  486. dev = alloc_orinocodev(sizeof(*card), spectrum_cs_hard_reset);
  487. if (! dev)
  488. return -ENOMEM;
  489. priv = netdev_priv(dev);
  490. card = priv->card;
  491. /* Link both structures together */
  492. link = &card->link;
  493. link->priv = dev;
  494. /* Interrupt setup */
  495. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  496. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  497. link->irq.Handler = orinoco_interrupt;
  498. link->irq.Instance = dev;
  499. /* General socket configuration defaults can go here. In this
  500. * client, we assume very little, and rely on the CIS for
  501. * almost everything. In most clients, many details (i.e.,
  502. * number, sizes, and attributes of IO windows) are fixed by
  503. * the nature of the device, and can be hard-wired here. */
  504. link->conf.Attributes = 0;
  505. link->conf.IntType = INT_MEMORY_AND_IO;
  506. link->handle = p_dev;
  507. p_dev->instance = link;
  508. link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
  509. spectrum_cs_config(link);
  510. return 0;
  511. } /* spectrum_cs_attach */
  512. /*
  513. * This deletes a driver "instance". The device is de-registered with
  514. * Card Services. If it has been released, all local data structures
  515. * are freed. Otherwise, the structures will be freed when the device
  516. * is released.
  517. */
  518. static void spectrum_cs_detach(struct pcmcia_device *p_dev)
  519. {
  520. dev_link_t *link = dev_to_instance(p_dev);
  521. struct net_device *dev = link->priv;
  522. if (link->state & DEV_CONFIG)
  523. spectrum_cs_release(link);
  524. DEBUG(0, PFX "detach: link=%p link->dev=%p\n", link, link->dev);
  525. if (link->dev) {
  526. DEBUG(0, PFX "About to unregister net device %p\n",
  527. dev);
  528. unregister_netdev(dev);
  529. }
  530. free_orinocodev(dev);
  531. } /* spectrum_cs_detach */
  532. /*
  533. * spectrum_cs_config() is scheduled to run after a CARD_INSERTION
  534. * event is received, to configure the PCMCIA socket, and to make the
  535. * device available to the system.
  536. */
  537. static void
  538. spectrum_cs_config(dev_link_t *link)
  539. {
  540. struct net_device *dev = link->priv;
  541. client_handle_t handle = link->handle;
  542. struct orinoco_private *priv = netdev_priv(dev);
  543. struct orinoco_pccard *card = priv->card;
  544. hermes_t *hw = &priv->hw;
  545. int last_fn, last_ret;
  546. u_char buf[64];
  547. config_info_t conf;
  548. cisinfo_t info;
  549. tuple_t tuple;
  550. cisparse_t parse;
  551. void __iomem *mem;
  552. CS_CHECK(ValidateCIS, pcmcia_validate_cis(handle, &info));
  553. /*
  554. * This reads the card's CONFIG tuple to find its
  555. * configuration registers.
  556. */
  557. tuple.DesiredTuple = CISTPL_CONFIG;
  558. tuple.Attributes = 0;
  559. tuple.TupleData = buf;
  560. tuple.TupleDataMax = sizeof(buf);
  561. tuple.TupleOffset = 0;
  562. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  563. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
  564. CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
  565. link->conf.ConfigBase = parse.config.base;
  566. link->conf.Present = parse.config.rmask[0];
  567. /* Configure card */
  568. link->state |= DEV_CONFIG;
  569. /* Look up the current Vcc */
  570. CS_CHECK(GetConfigurationInfo,
  571. pcmcia_get_configuration_info(handle, &conf));
  572. link->conf.Vcc = conf.Vcc;
  573. /*
  574. * In this loop, we scan the CIS for configuration table
  575. * entries, each of which describes a valid card
  576. * configuration, including voltage, IO window, memory window,
  577. * and interrupt settings.
  578. *
  579. * We make no assumptions about the card to be configured: we
  580. * use just the information available in the CIS. In an ideal
  581. * world, this would work for any PCMCIA card, but it requires
  582. * a complete and accurate CIS. In practice, a driver usually
  583. * "knows" most of these things without consulting the CIS,
  584. * and most client drivers will only use the CIS to fill in
  585. * implementation-defined details.
  586. */
  587. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  588. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
  589. while (1) {
  590. cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
  591. cistpl_cftable_entry_t dflt = { .index = 0 };
  592. if ( (pcmcia_get_tuple_data(handle, &tuple) != 0)
  593. || (pcmcia_parse_tuple(handle, &tuple, &parse) != 0))
  594. goto next_entry;
  595. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  596. dflt = *cfg;
  597. if (cfg->index == 0)
  598. goto next_entry;
  599. link->conf.ConfigIndex = cfg->index;
  600. /* Does this card need audio output? */
  601. if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
  602. link->conf.Attributes |= CONF_ENABLE_SPKR;
  603. link->conf.Status = CCSR_AUDIO_ENA;
  604. }
  605. /* Use power settings for Vcc and Vpp if present */
  606. /* Note that the CIS values need to be rescaled */
  607. if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
  608. if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
  609. DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
  610. if (!ignore_cis_vcc)
  611. goto next_entry;
  612. }
  613. } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
  614. if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
  615. DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
  616. if(!ignore_cis_vcc)
  617. goto next_entry;
  618. }
  619. }
  620. if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
  621. link->conf.Vpp1 = link->conf.Vpp2 =
  622. cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
  623. else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
  624. link->conf.Vpp1 = link->conf.Vpp2 =
  625. dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
  626. /* Do we need to allocate an interrupt? */
  627. link->conf.Attributes |= CONF_ENABLE_IRQ;
  628. /* IO window settings */
  629. link->io.NumPorts1 = link->io.NumPorts2 = 0;
  630. if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
  631. cistpl_io_t *io =
  632. (cfg->io.nwin) ? &cfg->io : &dflt.io;
  633. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  634. if (!(io->flags & CISTPL_IO_8BIT))
  635. link->io.Attributes1 =
  636. IO_DATA_PATH_WIDTH_16;
  637. if (!(io->flags & CISTPL_IO_16BIT))
  638. link->io.Attributes1 =
  639. IO_DATA_PATH_WIDTH_8;
  640. link->io.IOAddrLines =
  641. io->flags & CISTPL_IO_LINES_MASK;
  642. link->io.BasePort1 = io->win[0].base;
  643. link->io.NumPorts1 = io->win[0].len;
  644. if (io->nwin > 1) {
  645. link->io.Attributes2 =
  646. link->io.Attributes1;
  647. link->io.BasePort2 = io->win[1].base;
  648. link->io.NumPorts2 = io->win[1].len;
  649. }
  650. /* This reserves IO space but doesn't actually enable it */
  651. if (pcmcia_request_io(link->handle, &link->io) != 0)
  652. goto next_entry;
  653. }
  654. /* If we got this far, we're cool! */
  655. break;
  656. next_entry:
  657. if (link->io.NumPorts1)
  658. pcmcia_release_io(link->handle, &link->io);
  659. last_ret = pcmcia_get_next_tuple(handle, &tuple);
  660. if (last_ret == CS_NO_MORE_ITEMS) {
  661. printk(KERN_ERR PFX "GetNextTuple(): No matching "
  662. "CIS configuration. Maybe you need the "
  663. "ignore_cis_vcc=1 parameter.\n");
  664. goto cs_failed;
  665. }
  666. }
  667. /*
  668. * Allocate an interrupt line. Note that this does not assign
  669. * a handler to the interrupt, unless the 'Handler' member of
  670. * the irq structure is initialized.
  671. */
  672. CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
  673. /* We initialize the hermes structure before completing PCMCIA
  674. * configuration just in case the interrupt handler gets
  675. * called. */
  676. mem = ioport_map(link->io.BasePort1, link->io.NumPorts1);
  677. if (!mem)
  678. goto cs_failed;
  679. hermes_struct_init(hw, mem, HERMES_16BIT_REGSPACING);
  680. /*
  681. * This actually configures the PCMCIA socket -- setting up
  682. * the I/O windows and the interrupt mapping, and putting the
  683. * card and host interface into "Memory and IO" mode.
  684. */
  685. CS_CHECK(RequestConfiguration,
  686. pcmcia_request_configuration(link->handle, &link->conf));
  687. /* Ok, we have the configuration, prepare to register the netdev */
  688. dev->base_addr = link->io.BasePort1;
  689. dev->irq = link->irq.AssignedIRQ;
  690. SET_MODULE_OWNER(dev);
  691. card->node.major = card->node.minor = 0;
  692. /* Reset card and download firmware */
  693. if (spectrum_cs_hard_reset(priv) != 0) {
  694. goto failed;
  695. }
  696. SET_NETDEV_DEV(dev, &handle_to_dev(handle));
  697. /* Tell the stack we exist */
  698. if (register_netdev(dev) != 0) {
  699. printk(KERN_ERR PFX "register_netdev() failed\n");
  700. goto failed;
  701. }
  702. /* At this point, the dev_node_t structure(s) needs to be
  703. * initialized and arranged in a linked list at link->dev. */
  704. strcpy(card->node.dev_name, dev->name);
  705. link->dev = &card->node; /* link->dev being non-NULL is also
  706. used to indicate that the
  707. net_device has been registered */
  708. link->state &= ~DEV_CONFIG_PENDING;
  709. /* Finally, report what we've done */
  710. printk(KERN_DEBUG "%s: index 0x%02x: Vcc %d.%d",
  711. dev->name, link->conf.ConfigIndex,
  712. link->conf.Vcc / 10, link->conf.Vcc % 10);
  713. if (link->conf.Vpp1)
  714. printk(", Vpp %d.%d", link->conf.Vpp1 / 10,
  715. link->conf.Vpp1 % 10);
  716. printk(", irq %d", link->irq.AssignedIRQ);
  717. if (link->io.NumPorts1)
  718. printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  719. link->io.BasePort1 + link->io.NumPorts1 - 1);
  720. if (link->io.NumPorts2)
  721. printk(" & 0x%04x-0x%04x", link->io.BasePort2,
  722. link->io.BasePort2 + link->io.NumPorts2 - 1);
  723. printk("\n");
  724. return;
  725. cs_failed:
  726. cs_error(link->handle, last_fn, last_ret);
  727. failed:
  728. spectrum_cs_release(link);
  729. } /* spectrum_cs_config */
  730. /*
  731. * After a card is removed, spectrum_cs_release() will unregister the
  732. * device, and release the PCMCIA configuration. If the device is
  733. * still open, this will be postponed until it is closed.
  734. */
  735. static void
  736. spectrum_cs_release(dev_link_t *link)
  737. {
  738. struct net_device *dev = link->priv;
  739. struct orinoco_private *priv = netdev_priv(dev);
  740. unsigned long flags;
  741. /* We're committed to taking the device away now, so mark the
  742. * hardware as unavailable */
  743. spin_lock_irqsave(&priv->lock, flags);
  744. priv->hw_unavailable++;
  745. spin_unlock_irqrestore(&priv->lock, flags);
  746. /* Don't bother checking to see if these succeed or not */
  747. pcmcia_release_configuration(link->handle);
  748. if (link->io.NumPorts1)
  749. pcmcia_release_io(link->handle, &link->io);
  750. if (link->irq.AssignedIRQ)
  751. pcmcia_release_irq(link->handle, &link->irq);
  752. link->state &= ~DEV_CONFIG;
  753. if (priv->hw.iobase)
  754. ioport_unmap(priv->hw.iobase);
  755. } /* spectrum_cs_release */
  756. static int
  757. spectrum_cs_suspend(struct pcmcia_device *p_dev)
  758. {
  759. dev_link_t *link = dev_to_instance(p_dev);
  760. struct net_device *dev = link->priv;
  761. struct orinoco_private *priv = netdev_priv(dev);
  762. unsigned long flags;
  763. int err = 0;
  764. link->state |= DEV_SUSPEND;
  765. /* Mark the device as stopped, to block IO until later */
  766. if (link->state & DEV_CONFIG) {
  767. spin_lock_irqsave(&priv->lock, flags);
  768. err = __orinoco_down(dev);
  769. if (err)
  770. printk(KERN_WARNING "%s: Error %d downing interface\n",
  771. dev->name, err);
  772. netif_device_detach(dev);
  773. priv->hw_unavailable++;
  774. spin_unlock_irqrestore(&priv->lock, flags);
  775. pcmcia_release_configuration(link->handle);
  776. }
  777. return 0;
  778. }
  779. static int
  780. spectrum_cs_resume(struct pcmcia_device *p_dev)
  781. {
  782. dev_link_t *link = dev_to_instance(p_dev);
  783. struct net_device *dev = link->priv;
  784. struct orinoco_private *priv = netdev_priv(dev);
  785. link->state &= ~DEV_SUSPEND;
  786. if (link->state & DEV_CONFIG) {
  787. /* FIXME: should we double check that this is
  788. * the same card as we had before */
  789. pcmcia_request_configuration(link->handle, &link->conf);
  790. netif_device_attach(dev);
  791. priv->hw_unavailable--;
  792. schedule_work(&priv->reset_work);
  793. }
  794. return 0;
  795. }
  796. /********************************************************************/
  797. /* Module initialization */
  798. /********************************************************************/
  799. /* Can't be declared "const" or the whole __initdata section will
  800. * become const */
  801. static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
  802. " (Pavel Roskin <proski@gnu.org>,"
  803. " David Gibson <hermes@gibson.dropbear.id.au>, et al)";
  804. static struct pcmcia_device_id spectrum_cs_ids[] = {
  805. PCMCIA_DEVICE_MANF_CARD(0x026c, 0x0001), /* Symbol Spectrum24 LA4100 */
  806. PCMCIA_DEVICE_MANF_CARD(0x0104, 0x0001), /* Socket Communications CF */
  807. PCMCIA_DEVICE_PROD_ID12("Intel", "PRO/Wireless LAN PC Card", 0x816cc815, 0x6fbf459a), /* 2011B, not 2011 */
  808. PCMCIA_DEVICE_NULL,
  809. };
  810. MODULE_DEVICE_TABLE(pcmcia, spectrum_cs_ids);
  811. static struct pcmcia_driver orinoco_driver = {
  812. .owner = THIS_MODULE,
  813. .drv = {
  814. .name = DRIVER_NAME,
  815. },
  816. .probe = spectrum_cs_attach,
  817. .remove = spectrum_cs_detach,
  818. .suspend = spectrum_cs_suspend,
  819. .resume = spectrum_cs_resume,
  820. .id_table = spectrum_cs_ids,
  821. };
  822. static int __init
  823. init_spectrum_cs(void)
  824. {
  825. printk(KERN_DEBUG "%s\n", version);
  826. return pcmcia_register_driver(&orinoco_driver);
  827. }
  828. static void __exit
  829. exit_spectrum_cs(void)
  830. {
  831. pcmcia_unregister_driver(&orinoco_driver);
  832. }
  833. module_init(init_spectrum_cs);
  834. module_exit(exit_spectrum_cs);