spectrum_cs.c 28 KB

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