spectrum_cs.c 29 KB

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