spectrum_cs.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /*
  2. * Driver for 802.11b cards using RAM-loadable Symbol firmware, such as
  3. * Symbol Wireless Networker LA4137, 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. struct pcmcia_device *p_dev;
  57. dev_node_t node;
  58. };
  59. /********************************************************************/
  60. /* Function prototypes */
  61. /********************************************************************/
  62. static int spectrum_cs_config(struct pcmcia_device *link);
  63. static void spectrum_cs_release(struct pcmcia_device *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(struct pcmcia_device *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 (pcmcia_dev_present(link))
  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, &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, &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, &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, &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, &reg));
  241. udelay(1000);
  242. return 0;
  243. cs_failed:
  244. cs_error(link, 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_bytes(hw, HERMES_AUXDATA, pdi->data, pdi_len(pdi));
  286. return 0;
  287. }
  288. /* Read PDA from the adapter */
  289. static int
  290. spectrum_read_pda(hermes_t *hw, __le16 *pda, int pda_len)
  291. {
  292. int ret;
  293. int pda_size;
  294. /* Issue command to read EEPROM */
  295. ret = hermes_docmd_wait(hw, HERMES_CMD_READMIF, 0, NULL);
  296. if (ret)
  297. return ret;
  298. /* Open auxiliary port */
  299. ret = spectrum_aux_open(hw);
  300. if (ret)
  301. return ret;
  302. /* read PDA from EEPROM */
  303. spectrum_aux_setaddr(hw, PDA_ADDR);
  304. hermes_read_words(hw, HERMES_AUXDATA, pda, pda_len / 2);
  305. /* Check PDA length */
  306. pda_size = le16_to_cpu(pda[0]);
  307. if (pda_size > pda_len)
  308. return -EINVAL;
  309. return 0;
  310. }
  311. /* Parse PDA and write the records into the adapter */
  312. static int
  313. spectrum_apply_pda(hermes_t *hw, const struct dblock *first_block,
  314. __le16 *pda)
  315. {
  316. int ret;
  317. struct pdi *pdi;
  318. struct pdr *first_pdr;
  319. const struct dblock *blk = first_block;
  320. /* Skip all blocks to locate Plug Data References */
  321. while (dblock_addr(blk) != BLOCK_END)
  322. blk = (struct dblock *) &blk->data[dblock_len(blk)];
  323. first_pdr = (struct pdr *) blk;
  324. /* Go through every PDI and plug them into the adapter */
  325. pdi = (struct pdi *) (pda + 2);
  326. while (pdi_id(pdi) != PDI_END) {
  327. ret = spectrum_plug_pdi(hw, first_pdr, pdi);
  328. if (ret)
  329. return ret;
  330. /* Increment to the next PDI */
  331. pdi = (struct pdi *) &pdi->data[pdi_len(pdi)];
  332. }
  333. return 0;
  334. }
  335. /* Load firmware blocks into the adapter */
  336. static int
  337. spectrum_load_blocks(hermes_t *hw, const struct dblock *first_block)
  338. {
  339. const struct dblock *blk;
  340. u32 blkaddr;
  341. u32 blklen;
  342. blk = first_block;
  343. blkaddr = dblock_addr(blk);
  344. blklen = dblock_len(blk);
  345. while (dblock_addr(blk) != BLOCK_END) {
  346. spectrum_aux_setaddr(hw, blkaddr);
  347. hermes_write_bytes(hw, HERMES_AUXDATA, blk->data,
  348. blklen);
  349. blk = (struct dblock *) &blk->data[blklen];
  350. blkaddr = dblock_addr(blk);
  351. blklen = dblock_len(blk);
  352. }
  353. return 0;
  354. }
  355. /*
  356. * Process a firmware image - stop the card, load the firmware, reset
  357. * the card and make sure it responds. For the secondary firmware take
  358. * care of the PDA - read it and then write it on top of the firmware.
  359. */
  360. static int
  361. spectrum_dl_image(hermes_t *hw, struct pcmcia_device *link,
  362. const unsigned char *image)
  363. {
  364. int ret;
  365. const unsigned char *ptr;
  366. const struct dblock *first_block;
  367. /* Plug Data Area (PDA) */
  368. __le16 pda[PDA_WORDS];
  369. /* Binary block begins after the 0x1A marker */
  370. ptr = image;
  371. while (*ptr++ != TEXT_END);
  372. first_block = (const struct dblock *) ptr;
  373. /* Read the PDA */
  374. if (image != primsym) {
  375. ret = spectrum_read_pda(hw, pda, sizeof(pda));
  376. if (ret)
  377. return ret;
  378. }
  379. /* Stop the firmware, so that it can be safely rewritten */
  380. ret = spectrum_reset(link, 1);
  381. if (ret)
  382. return ret;
  383. /* Program the adapter with new firmware */
  384. ret = spectrum_load_blocks(hw, first_block);
  385. if (ret)
  386. return ret;
  387. /* Write the PDA to the adapter */
  388. if (image != primsym) {
  389. ret = spectrum_apply_pda(hw, first_block, pda);
  390. if (ret)
  391. return ret;
  392. }
  393. /* Run the firmware */
  394. ret = spectrum_reset(link, 0);
  395. if (ret)
  396. return ret;
  397. /* Reset hermes chip and make sure it responds */
  398. ret = hermes_init(hw);
  399. /* hermes_reset() should return 0 with the secondary firmware */
  400. if (image != primsym && ret != 0)
  401. return -ENODEV;
  402. /* And this should work with any firmware */
  403. if (!hermes_present(hw))
  404. return -ENODEV;
  405. return 0;
  406. }
  407. /*
  408. * Download the firmware into the card, this also does a PCMCIA soft
  409. * reset on the card, to make sure it's in a sane state.
  410. */
  411. static int
  412. spectrum_dl_firmware(hermes_t *hw, struct pcmcia_device *link)
  413. {
  414. int ret;
  415. const struct firmware *fw_entry;
  416. if (request_firmware(&fw_entry, primary_fw_name,
  417. &handle_to_dev(link)) == 0) {
  418. primsym = fw_entry->data;
  419. } else {
  420. printk(KERN_ERR PFX "Cannot find firmware: %s\n",
  421. primary_fw_name);
  422. return -ENOENT;
  423. }
  424. if (request_firmware(&fw_entry, secondary_fw_name,
  425. &handle_to_dev(link)) == 0) {
  426. secsym = fw_entry->data;
  427. } else {
  428. printk(KERN_ERR PFX "Cannot find firmware: %s\n",
  429. secondary_fw_name);
  430. return -ENOENT;
  431. }
  432. /* Load primary firmware */
  433. ret = spectrum_dl_image(hw, link, primsym);
  434. if (ret) {
  435. printk(KERN_ERR PFX "Primary firmware download failed\n");
  436. return ret;
  437. }
  438. /* Load secondary firmware */
  439. ret = spectrum_dl_image(hw, link, secsym);
  440. if (ret) {
  441. printk(KERN_ERR PFX "Secondary firmware download failed\n");
  442. }
  443. return ret;
  444. }
  445. /********************************************************************/
  446. /* Device methods */
  447. /********************************************************************/
  448. static int
  449. spectrum_cs_hard_reset(struct orinoco_private *priv)
  450. {
  451. struct orinoco_pccard *card = priv->card;
  452. struct pcmcia_device *link = card->p_dev;
  453. int err;
  454. if (!hermes_present(&priv->hw)) {
  455. /* The firmware needs to be reloaded */
  456. if (spectrum_dl_firmware(&priv->hw, link) != 0) {
  457. printk(KERN_ERR PFX "Firmware download failed\n");
  458. err = -ENODEV;
  459. }
  460. } else {
  461. /* Soft reset using COR and HCR */
  462. spectrum_reset(link, 0);
  463. }
  464. return 0;
  465. }
  466. /********************************************************************/
  467. /* PCMCIA stuff */
  468. /********************************************************************/
  469. /*
  470. * This creates an "instance" of the driver, allocating local data
  471. * structures for one device. The device is registered with Card
  472. * Services.
  473. *
  474. * The dev_link structure is initialized, but we don't actually
  475. * configure the card at this point -- we wait until we receive a card
  476. * insertion event. */
  477. static int
  478. spectrum_cs_probe(struct pcmcia_device *link)
  479. {
  480. struct net_device *dev;
  481. struct orinoco_private *priv;
  482. struct orinoco_pccard *card;
  483. dev = alloc_orinocodev(sizeof(*card), spectrum_cs_hard_reset);
  484. if (! dev)
  485. return -ENOMEM;
  486. priv = netdev_priv(dev);
  487. card = priv->card;
  488. /* Link both structures together */
  489. card->p_dev = link;
  490. link->priv = dev;
  491. /* Interrupt setup */
  492. link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
  493. link->irq.IRQInfo1 = IRQ_LEVEL_ID;
  494. link->irq.Handler = orinoco_interrupt;
  495. link->irq.Instance = dev;
  496. /* General socket configuration defaults can go here. In this
  497. * client, we assume very little, and rely on the CIS for
  498. * almost everything. In most clients, many details (i.e.,
  499. * number, sizes, and attributes of IO windows) are fixed by
  500. * the nature of the device, and can be hard-wired here. */
  501. link->conf.Attributes = 0;
  502. link->conf.IntType = INT_MEMORY_AND_IO;
  503. return spectrum_cs_config(link);
  504. } /* spectrum_cs_attach */
  505. /*
  506. * This deletes a driver "instance". The device is de-registered with
  507. * Card Services. If it has been released, all local data structures
  508. * are freed. Otherwise, the structures will be freed when the device
  509. * is released.
  510. */
  511. static void spectrum_cs_detach(struct pcmcia_device *link)
  512. {
  513. struct net_device *dev = link->priv;
  514. spectrum_cs_release(link);
  515. DEBUG(0, PFX "detach: link=%p link->dev_node=%p\n", link, link->dev_node);
  516. if (link->dev_node) {
  517. DEBUG(0, PFX "About to unregister net device %p\n",
  518. dev);
  519. unregister_netdev(dev);
  520. }
  521. free_orinocodev(dev);
  522. } /* spectrum_cs_detach */
  523. /*
  524. * spectrum_cs_config() is scheduled to run after a CARD_INSERTION
  525. * event is received, to configure the PCMCIA socket, and to make the
  526. * device available to the system.
  527. */
  528. static int
  529. spectrum_cs_config(struct pcmcia_device *link)
  530. {
  531. struct net_device *dev = link->priv;
  532. struct orinoco_private *priv = netdev_priv(dev);
  533. struct orinoco_pccard *card = priv->card;
  534. hermes_t *hw = &priv->hw;
  535. int last_fn, last_ret;
  536. u_char buf[64];
  537. config_info_t conf;
  538. tuple_t tuple;
  539. cisparse_t parse;
  540. void __iomem *mem;
  541. /*
  542. * This reads the card's CONFIG tuple to find its
  543. * configuration registers.
  544. */
  545. tuple.DesiredTuple = CISTPL_CONFIG;
  546. tuple.Attributes = 0;
  547. tuple.TupleData = buf;
  548. tuple.TupleDataMax = sizeof(buf);
  549. tuple.TupleOffset = 0;
  550. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  551. CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
  552. CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
  553. link->conf.ConfigBase = parse.config.base;
  554. link->conf.Present = parse.config.rmask[0];
  555. /* Look up the current Vcc */
  556. CS_CHECK(GetConfigurationInfo,
  557. pcmcia_get_configuration_info(link, &conf));
  558. /*
  559. * In this loop, we scan the CIS for configuration table
  560. * entries, each of which describes a valid card
  561. * configuration, including voltage, IO window, memory window,
  562. * and interrupt settings.
  563. *
  564. * We make no assumptions about the card to be configured: we
  565. * use just the information available in the CIS. In an ideal
  566. * world, this would work for any PCMCIA card, but it requires
  567. * a complete and accurate CIS. In practice, a driver usually
  568. * "knows" most of these things without consulting the CIS,
  569. * and most client drivers will only use the CIS to fill in
  570. * implementation-defined details.
  571. */
  572. tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
  573. CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
  574. while (1) {
  575. cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
  576. cistpl_cftable_entry_t dflt = { .index = 0 };
  577. if ( (pcmcia_get_tuple_data(link, &tuple) != 0)
  578. || (pcmcia_parse_tuple(link, &tuple, &parse) != 0))
  579. goto next_entry;
  580. if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
  581. dflt = *cfg;
  582. if (cfg->index == 0)
  583. goto next_entry;
  584. link->conf.ConfigIndex = cfg->index;
  585. /* Use power settings for Vcc and Vpp if present */
  586. /* Note that the CIS values need to be rescaled */
  587. if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
  588. if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
  589. DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
  590. if (!ignore_cis_vcc)
  591. goto next_entry;
  592. }
  593. } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
  594. if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
  595. DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
  596. if(!ignore_cis_vcc)
  597. goto next_entry;
  598. }
  599. }
  600. if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
  601. link->conf.Vpp =
  602. cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
  603. else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
  604. link->conf.Vpp =
  605. dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
  606. /* Do we need to allocate an interrupt? */
  607. link->conf.Attributes |= CONF_ENABLE_IRQ;
  608. /* IO window settings */
  609. link->io.NumPorts1 = link->io.NumPorts2 = 0;
  610. if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
  611. cistpl_io_t *io =
  612. (cfg->io.nwin) ? &cfg->io : &dflt.io;
  613. link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
  614. if (!(io->flags & CISTPL_IO_8BIT))
  615. link->io.Attributes1 =
  616. IO_DATA_PATH_WIDTH_16;
  617. if (!(io->flags & CISTPL_IO_16BIT))
  618. link->io.Attributes1 =
  619. IO_DATA_PATH_WIDTH_8;
  620. link->io.IOAddrLines =
  621. io->flags & CISTPL_IO_LINES_MASK;
  622. link->io.BasePort1 = io->win[0].base;
  623. link->io.NumPorts1 = io->win[0].len;
  624. if (io->nwin > 1) {
  625. link->io.Attributes2 =
  626. link->io.Attributes1;
  627. link->io.BasePort2 = io->win[1].base;
  628. link->io.NumPorts2 = io->win[1].len;
  629. }
  630. /* This reserves IO space but doesn't actually enable it */
  631. if (pcmcia_request_io(link, &link->io) != 0)
  632. goto next_entry;
  633. }
  634. /* If we got this far, we're cool! */
  635. break;
  636. next_entry:
  637. pcmcia_disable_device(link);
  638. last_ret = pcmcia_get_next_tuple(link, &tuple);
  639. if (last_ret == CS_NO_MORE_ITEMS) {
  640. printk(KERN_ERR PFX "GetNextTuple(): No matching "
  641. "CIS configuration. Maybe you need the "
  642. "ignore_cis_vcc=1 parameter.\n");
  643. goto cs_failed;
  644. }
  645. }
  646. /*
  647. * Allocate an interrupt line. Note that this does not assign
  648. * a handler to the interrupt, unless the 'Handler' member of
  649. * the irq structure is initialized.
  650. */
  651. CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
  652. /* We initialize the hermes structure before completing PCMCIA
  653. * configuration just in case the interrupt handler gets
  654. * called. */
  655. mem = ioport_map(link->io.BasePort1, link->io.NumPorts1);
  656. if (!mem)
  657. goto cs_failed;
  658. hermes_struct_init(hw, mem, HERMES_16BIT_REGSPACING);
  659. /*
  660. * This actually configures the PCMCIA socket -- setting up
  661. * the I/O windows and the interrupt mapping, and putting the
  662. * card and host interface into "Memory and IO" mode.
  663. */
  664. CS_CHECK(RequestConfiguration,
  665. pcmcia_request_configuration(link, &link->conf));
  666. /* Ok, we have the configuration, prepare to register the netdev */
  667. dev->base_addr = link->io.BasePort1;
  668. dev->irq = link->irq.AssignedIRQ;
  669. SET_MODULE_OWNER(dev);
  670. card->node.major = card->node.minor = 0;
  671. /* Reset card and download firmware */
  672. if (spectrum_cs_hard_reset(priv) != 0) {
  673. goto failed;
  674. }
  675. SET_NETDEV_DEV(dev, &handle_to_dev(link));
  676. /* Tell the stack we exist */
  677. if (register_netdev(dev) != 0) {
  678. printk(KERN_ERR PFX "register_netdev() failed\n");
  679. goto failed;
  680. }
  681. /* At this point, the dev_node_t structure(s) needs to be
  682. * initialized and arranged in a linked list at link->dev_node. */
  683. strcpy(card->node.dev_name, dev->name);
  684. link->dev_node = &card->node; /* link->dev_node being non-NULL is also
  685. used to indicate that the
  686. net_device has been registered */
  687. /* Finally, report what we've done */
  688. printk(KERN_DEBUG "%s: index 0x%02x: ",
  689. dev->name, link->conf.ConfigIndex);
  690. if (link->conf.Vpp)
  691. printk(", Vpp %d.%d", link->conf.Vpp / 10,
  692. link->conf.Vpp % 10);
  693. printk(", irq %d", link->irq.AssignedIRQ);
  694. if (link->io.NumPorts1)
  695. printk(", io 0x%04x-0x%04x", link->io.BasePort1,
  696. link->io.BasePort1 + link->io.NumPorts1 - 1);
  697. if (link->io.NumPorts2)
  698. printk(" & 0x%04x-0x%04x", link->io.BasePort2,
  699. link->io.BasePort2 + link->io.NumPorts2 - 1);
  700. printk("\n");
  701. return 0;
  702. cs_failed:
  703. cs_error(link, last_fn, last_ret);
  704. failed:
  705. spectrum_cs_release(link);
  706. return -ENODEV;
  707. } /* spectrum_cs_config */
  708. /*
  709. * After a card is removed, spectrum_cs_release() will unregister the
  710. * device, and release the PCMCIA configuration. If the device is
  711. * still open, this will be postponed until it is closed.
  712. */
  713. static void
  714. spectrum_cs_release(struct pcmcia_device *link)
  715. {
  716. struct net_device *dev = link->priv;
  717. struct orinoco_private *priv = netdev_priv(dev);
  718. unsigned long flags;
  719. /* We're committed to taking the device away now, so mark the
  720. * hardware as unavailable */
  721. spin_lock_irqsave(&priv->lock, flags);
  722. priv->hw_unavailable++;
  723. spin_unlock_irqrestore(&priv->lock, flags);
  724. pcmcia_disable_device(link);
  725. if (priv->hw.iobase)
  726. ioport_unmap(priv->hw.iobase);
  727. } /* spectrum_cs_release */
  728. static int
  729. spectrum_cs_suspend(struct pcmcia_device *link)
  730. {
  731. struct net_device *dev = link->priv;
  732. struct orinoco_private *priv = netdev_priv(dev);
  733. unsigned long flags;
  734. int err = 0;
  735. /* Mark the device as stopped, to block IO until later */
  736. spin_lock_irqsave(&priv->lock, flags);
  737. err = __orinoco_down(dev);
  738. if (err)
  739. printk(KERN_WARNING "%s: Error %d downing interface\n",
  740. dev->name, err);
  741. netif_device_detach(dev);
  742. priv->hw_unavailable++;
  743. spin_unlock_irqrestore(&priv->lock, flags);
  744. return 0;
  745. }
  746. static int
  747. spectrum_cs_resume(struct pcmcia_device *link)
  748. {
  749. struct net_device *dev = link->priv;
  750. struct orinoco_private *priv = netdev_priv(dev);
  751. netif_device_attach(dev);
  752. priv->hw_unavailable--;
  753. schedule_work(&priv->reset_work);
  754. return 0;
  755. }
  756. /********************************************************************/
  757. /* Module initialization */
  758. /********************************************************************/
  759. /* Can't be declared "const" or the whole __initdata section will
  760. * become const */
  761. static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
  762. " (Pavel Roskin <proski@gnu.org>,"
  763. " David Gibson <hermes@gibson.dropbear.id.au>, et al)";
  764. static struct pcmcia_device_id spectrum_cs_ids[] = {
  765. PCMCIA_DEVICE_MANF_CARD(0x026c, 0x0001), /* Symbol Spectrum24 LA4137 */
  766. PCMCIA_DEVICE_MANF_CARD(0x0104, 0x0001), /* Socket Communications CF */
  767. PCMCIA_DEVICE_PROD_ID12("Intel", "PRO/Wireless LAN PC Card", 0x816cc815, 0x6fbf459a), /* 2011B, not 2011 */
  768. PCMCIA_DEVICE_NULL,
  769. };
  770. MODULE_DEVICE_TABLE(pcmcia, spectrum_cs_ids);
  771. static struct pcmcia_driver orinoco_driver = {
  772. .owner = THIS_MODULE,
  773. .drv = {
  774. .name = DRIVER_NAME,
  775. },
  776. .probe = spectrum_cs_probe,
  777. .remove = spectrum_cs_detach,
  778. .suspend = spectrum_cs_suspend,
  779. .resume = spectrum_cs_resume,
  780. .id_table = spectrum_cs_ids,
  781. };
  782. static int __init
  783. init_spectrum_cs(void)
  784. {
  785. printk(KERN_DEBUG "%s\n", version);
  786. return pcmcia_register_driver(&orinoco_driver);
  787. }
  788. static void __exit
  789. exit_spectrum_cs(void)
  790. {
  791. pcmcia_unregister_driver(&orinoco_driver);
  792. }
  793. module_init(init_spectrum_cs);
  794. module_exit(exit_spectrum_cs);