blob: 15465278c789ed607e154775ccb55a3dcbc20f1e [file] [log] [blame]
Pavel Roskin3a48c4c2005-09-01 20:10:06 -04001/*
2 * Driver for 802.11b cards using RAM-loadable Symbol firmware, such as
Pavel Roskin4ebe2eb2006-04-07 04:10:32 -04003 * Symbol Wireless Networker LA4137, CompactFlash cards by Socket
Pavel Roskin3a48c4c2005-09-01 20:10:06 -04004 * 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
21#define DRIVER_NAME "spectrum_cs"
22#define PFX DRIVER_NAME ": "
23
Pavel Roskin3a48c4c2005-09-01 20:10:06 -040024#include <linux/module.h>
25#include <linux/kernel.h>
26#include <linux/init.h>
Pavel Roskinef846bf2005-09-23 04:18:07 -040027#include <linux/delay.h>
Pavel Roskin65853b12005-09-16 02:15:13 -040028#include <linux/firmware.h>
Pavel Roskin3a48c4c2005-09-01 20:10:06 -040029#include <pcmcia/cs_types.h>
30#include <pcmcia/cs.h>
31#include <pcmcia/cistpl.h>
32#include <pcmcia/cisreg.h>
33#include <pcmcia/ds.h>
34
Pavel Roskin3a48c4c2005-09-01 20:10:06 -040035#include "orinoco.h"
36
Pavel Roskin3a48c4c2005-09-01 20:10:06 -040037static unsigned char *primsym;
38static unsigned char *secsym;
39static const char primary_fw_name[] = "symbol_sp24t_prim_fw";
40static const char secondary_fw_name[] = "symbol_sp24t_sec_fw";
Pavel Roskin3a48c4c2005-09-01 20:10:06 -040041
42/********************************************************************/
43/* Module stuff */
44/********************************************************************/
45
46MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
47MODULE_DESCRIPTION("Driver for Symbol Spectrum24 Trilogy cards with firmware downloader");
48MODULE_LICENSE("Dual MPL/GPL");
49
50/* Module parameters */
51
52/* Some D-Link cards have buggy CIS. They do work at 5v properly, but
53 * don't have any CIS entry for it. This workaround it... */
54static int ignore_cis_vcc; /* = 0 */
55module_param(ignore_cis_vcc, int, 0);
56MODULE_PARM_DESC(ignore_cis_vcc, "Allow voltage mismatch between card and socket");
57
58/********************************************************************/
Pavel Roskin3a48c4c2005-09-01 20:10:06 -040059/* Data structures */
60/********************************************************************/
61
62/* PCMCIA specific device information (goes in the card field of
63 * struct orinoco_private */
64struct orinoco_pccard {
Dominik Brodowskifd238232006-03-05 10:45:09 +010065 struct pcmcia_device *p_dev;
Pavel Roskin3a48c4c2005-09-01 20:10:06 -040066 dev_node_t node;
67};
68
Pavel Roskin3a48c4c2005-09-01 20:10:06 -040069/********************************************************************/
70/* Function prototypes */
71/********************************************************************/
72
Dominik Brodowski15b99ac2006-03-31 17:26:06 +020073static int spectrum_cs_config(struct pcmcia_device *link);
Dominik Brodowskifba395e2006-03-31 17:21:06 +020074static void spectrum_cs_release(struct pcmcia_device *link);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -040075
76/********************************************************************/
77/* Firmware downloader */
78/********************************************************************/
79
80/* Position of PDA in the adapter memory */
81#define EEPROM_ADDR 0x3000
82#define EEPROM_LEN 0x200
83#define PDA_OFFSET 0x100
84
85#define PDA_ADDR (EEPROM_ADDR + PDA_OFFSET)
86#define PDA_WORDS ((EEPROM_LEN - PDA_OFFSET) / 2)
87
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/*
94 * AUX port access. To unlock the AUX port write the access keys to the
95 * PARAM0-2 registers, then write HERMES_AUX_ENABLE to the HERMES_CONTROL
96 * register. Then read it and make sure it's HERMES_AUX_ENABLED.
97 */
98#define HERMES_AUX_ENABLE 0x8000 /* Enable auxiliary port access */
99#define HERMES_AUX_DISABLE 0x4000 /* Disable to auxiliary port access */
100#define HERMES_AUX_ENABLED 0xC000 /* Auxiliary port is open */
101
102#define HERMES_AUX_PW0 0xFE01
103#define HERMES_AUX_PW1 0xDC23
104#define HERMES_AUX_PW2 0xBA45
105
106/* End markers */
107#define PDI_END 0x00000000 /* End of PDA */
108#define BLOCK_END 0xFFFFFFFF /* Last image block */
109#define TEXT_END 0x1A /* End of text header */
110
111/*
112 * The following structures have little-endian fields denoted by
113 * the leading underscore. Don't access them directly - use inline
114 * functions defined below.
115 */
116
117/*
118 * The binary image to be downloaded consists of series of data blocks.
119 * Each block has the following structure.
120 */
121struct dblock {
Pavel Roskin7eeae2f2006-04-07 04:10:24 -0400122 __le32 addr; /* adapter address where to write the block */
123 __le16 len; /* length of the data only, in bytes */
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400124 char data[0]; /* data to be written */
125} __attribute__ ((packed));
126
127/*
128 * Plug Data References are located in in the image after the last data
129 * block. They refer to areas in the adapter memory where the plug data
130 * items with matching ID should be written.
131 */
132struct pdr {
Pavel Roskin7eeae2f2006-04-07 04:10:24 -0400133 __le32 id; /* record ID */
134 __le32 addr; /* adapter address where to write the data */
135 __le32 len; /* expected length of the data, in bytes */
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400136 char next[0]; /* next PDR starts here */
137} __attribute__ ((packed));
138
139
140/*
141 * Plug Data Items are located in the EEPROM read from the adapter by
142 * primary firmware. They refer to the device-specific data that should
143 * be plugged into the secondary firmware.
144 */
145struct pdi {
Pavel Roskin7eeae2f2006-04-07 04:10:24 -0400146 __le16 len; /* length of ID and data, in words */
147 __le16 id; /* record ID */
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400148 char data[0]; /* plug data */
Alexey Dobriyan53b35312006-03-24 03:16:13 -0800149} __attribute__ ((packed));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400150
151
152/* Functions for access to little-endian data */
153static inline u32
154dblock_addr(const struct dblock *blk)
155{
Pavel Roskin7eeae2f2006-04-07 04:10:24 -0400156 return le32_to_cpu(blk->addr);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400157}
158
159static inline u32
160dblock_len(const struct dblock *blk)
161{
Pavel Roskin7eeae2f2006-04-07 04:10:24 -0400162 return le16_to_cpu(blk->len);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400163}
164
165static inline u32
166pdr_id(const struct pdr *pdr)
167{
Pavel Roskin7eeae2f2006-04-07 04:10:24 -0400168 return le32_to_cpu(pdr->id);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400169}
170
171static inline u32
172pdr_addr(const struct pdr *pdr)
173{
Pavel Roskin7eeae2f2006-04-07 04:10:24 -0400174 return le32_to_cpu(pdr->addr);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400175}
176
177static inline u32
178pdr_len(const struct pdr *pdr)
179{
Pavel Roskin7eeae2f2006-04-07 04:10:24 -0400180 return le32_to_cpu(pdr->len);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400181}
182
183static inline u32
184pdi_id(const struct pdi *pdi)
185{
Pavel Roskin7eeae2f2006-04-07 04:10:24 -0400186 return le16_to_cpu(pdi->id);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400187}
188
189/* Return length of the data only, in bytes */
190static inline u32
191pdi_len(const struct pdi *pdi)
192{
Pavel Roskin7eeae2f2006-04-07 04:10:24 -0400193 return 2 * (le16_to_cpu(pdi->len) - 1);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400194}
195
196
197/* Set address of the auxiliary port */
198static inline void
199spectrum_aux_setaddr(hermes_t *hw, u32 addr)
200{
201 hermes_write_reg(hw, HERMES_AUXPAGE, (u16) (addr >> 7));
202 hermes_write_reg(hw, HERMES_AUXOFFSET, (u16) (addr & 0x7F));
203}
204
205
206/* Open access to the auxiliary port */
207static int
208spectrum_aux_open(hermes_t *hw)
209{
210 int i;
211
212 /* Already open? */
213 if (hermes_read_reg(hw, HERMES_CONTROL) == HERMES_AUX_ENABLED)
214 return 0;
215
216 hermes_write_reg(hw, HERMES_PARAM0, HERMES_AUX_PW0);
217 hermes_write_reg(hw, HERMES_PARAM1, HERMES_AUX_PW1);
218 hermes_write_reg(hw, HERMES_PARAM2, HERMES_AUX_PW2);
219 hermes_write_reg(hw, HERMES_CONTROL, HERMES_AUX_ENABLE);
220
221 for (i = 0; i < 20; i++) {
222 udelay(10);
223 if (hermes_read_reg(hw, HERMES_CONTROL) ==
224 HERMES_AUX_ENABLED)
225 return 0;
226 }
227
228 return -EBUSY;
229}
230
231
232#define CS_CHECK(fn, ret) \
233 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
234
235/*
236 * Reset the card using configuration registers COR and CCSR.
237 * If IDLE is 1, stop the firmware, so that it can be safely rewritten.
238 */
239static int
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200240spectrum_reset(struct pcmcia_device *link, int idle)
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400241{
242 int last_ret, last_fn;
243 conf_reg_t reg;
244 u_int save_cor;
245
246 /* Doing it if hardware is gone is guaranteed crash */
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100247 if (pcmcia_dev_present(link))
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400248 return -ENODEV;
249
250 /* Save original COR value */
251 reg.Function = 0;
252 reg.Action = CS_READ;
253 reg.Offset = CISREG_COR;
254 CS_CHECK(AccessConfigurationRegister,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200255 pcmcia_access_configuration_register(link, &reg));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400256 save_cor = reg.Value;
257
258 /* Soft-Reset card */
259 reg.Action = CS_WRITE;
260 reg.Offset = CISREG_COR;
261 reg.Value = (save_cor | COR_SOFT_RESET);
262 CS_CHECK(AccessConfigurationRegister,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200263 pcmcia_access_configuration_register(link, &reg));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400264 udelay(1000);
265
266 /* Read CCSR */
267 reg.Action = CS_READ;
268 reg.Offset = CISREG_CCSR;
269 CS_CHECK(AccessConfigurationRegister,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200270 pcmcia_access_configuration_register(link, &reg));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400271
272 /*
273 * Start or stop the firmware. Memory width bit should be
274 * preserved from the value we've just read.
275 */
276 reg.Action = CS_WRITE;
277 reg.Offset = CISREG_CCSR;
278 reg.Value = (idle ? HCR_IDLE : HCR_RUN) | (reg.Value & HCR_MEM16);
279 CS_CHECK(AccessConfigurationRegister,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200280 pcmcia_access_configuration_register(link, &reg));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400281 udelay(1000);
282
283 /* Restore original COR configuration index */
284 reg.Action = CS_WRITE;
285 reg.Offset = CISREG_COR;
286 reg.Value = (save_cor & ~COR_SOFT_RESET);
287 CS_CHECK(AccessConfigurationRegister,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200288 pcmcia_access_configuration_register(link, &reg));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400289 udelay(1000);
290 return 0;
291
292 cs_failed:
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200293 cs_error(link, last_fn, last_ret);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400294 return -ENODEV;
295}
296
297
298/*
299 * Scan PDR for the record with the specified RECORD_ID.
300 * If it's not found, return NULL.
301 */
302static struct pdr *
303spectrum_find_pdr(struct pdr *first_pdr, u32 record_id)
304{
305 struct pdr *pdr = first_pdr;
306
307 while (pdr_id(pdr) != PDI_END) {
308 /*
309 * PDR area is currently not terminated by PDI_END.
310 * It's followed by CRC records, which have the type
311 * field where PDR has length. The type can be 0 or 1.
312 */
313 if (pdr_len(pdr) < 2)
314 return NULL;
315
316 /* If the record ID matches, we are done */
317 if (pdr_id(pdr) == record_id)
318 return pdr;
319
320 pdr = (struct pdr *) pdr->next;
321 }
322 return NULL;
323}
324
325
326/* Process one Plug Data Item - find corresponding PDR and plug it */
327static int
328spectrum_plug_pdi(hermes_t *hw, struct pdr *first_pdr, struct pdi *pdi)
329{
330 struct pdr *pdr;
331
332 /* Find the PDI corresponding to this PDR */
333 pdr = spectrum_find_pdr(first_pdr, pdi_id(pdi));
334
335 /* No match is found, safe to ignore */
336 if (!pdr)
337 return 0;
338
339 /* Lengths of the data in PDI and PDR must match */
340 if (pdi_len(pdi) != pdr_len(pdr))
341 return -EINVAL;
342
343 /* do the actual plugging */
344 spectrum_aux_setaddr(hw, pdr_addr(pdr));
Pavel Roskin6b616262006-04-07 04:10:39 -0400345 hermes_write_bytes(hw, HERMES_AUXDATA, pdi->data, pdi_len(pdi));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400346
347 return 0;
348}
349
350
351/* Read PDA from the adapter */
352static int
Pavel Roskind133ae42005-09-23 04:18:06 -0400353spectrum_read_pda(hermes_t *hw, __le16 *pda, int pda_len)
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400354{
355 int ret;
356 int pda_size;
357
358 /* Issue command to read EEPROM */
359 ret = hermes_docmd_wait(hw, HERMES_CMD_READMIF, 0, NULL);
360 if (ret)
361 return ret;
362
363 /* Open auxiliary port */
364 ret = spectrum_aux_open(hw);
365 if (ret)
366 return ret;
367
368 /* read PDA from EEPROM */
369 spectrum_aux_setaddr(hw, PDA_ADDR);
370 hermes_read_words(hw, HERMES_AUXDATA, pda, pda_len / 2);
371
372 /* Check PDA length */
373 pda_size = le16_to_cpu(pda[0]);
374 if (pda_size > pda_len)
375 return -EINVAL;
376
377 return 0;
378}
379
380
381/* Parse PDA and write the records into the adapter */
382static int
383spectrum_apply_pda(hermes_t *hw, const struct dblock *first_block,
Pavel Roskind133ae42005-09-23 04:18:06 -0400384 __le16 *pda)
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400385{
386 int ret;
387 struct pdi *pdi;
388 struct pdr *first_pdr;
389 const struct dblock *blk = first_block;
390
391 /* Skip all blocks to locate Plug Data References */
392 while (dblock_addr(blk) != BLOCK_END)
393 blk = (struct dblock *) &blk->data[dblock_len(blk)];
394
395 first_pdr = (struct pdr *) blk;
396
397 /* Go through every PDI and plug them into the adapter */
398 pdi = (struct pdi *) (pda + 2);
399 while (pdi_id(pdi) != PDI_END) {
400 ret = spectrum_plug_pdi(hw, first_pdr, pdi);
401 if (ret)
402 return ret;
403
404 /* Increment to the next PDI */
405 pdi = (struct pdi *) &pdi->data[pdi_len(pdi)];
406 }
407 return 0;
408}
409
410
411/* Load firmware blocks into the adapter */
412static int
413spectrum_load_blocks(hermes_t *hw, const struct dblock *first_block)
414{
415 const struct dblock *blk;
416 u32 blkaddr;
417 u32 blklen;
418
419 blk = first_block;
420 blkaddr = dblock_addr(blk);
421 blklen = dblock_len(blk);
422
423 while (dblock_addr(blk) != BLOCK_END) {
424 spectrum_aux_setaddr(hw, blkaddr);
Pavel Roskin6b616262006-04-07 04:10:39 -0400425 hermes_write_bytes(hw, HERMES_AUXDATA, blk->data,
426 blklen);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400427
428 blk = (struct dblock *) &blk->data[blklen];
429 blkaddr = dblock_addr(blk);
430 blklen = dblock_len(blk);
431 }
432 return 0;
433}
434
435
436/*
437 * Process a firmware image - stop the card, load the firmware, reset
438 * the card and make sure it responds. For the secondary firmware take
439 * care of the PDA - read it and then write it on top of the firmware.
440 */
441static int
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200442spectrum_dl_image(hermes_t *hw, struct pcmcia_device *link,
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400443 const unsigned char *image)
444{
445 int ret;
446 const unsigned char *ptr;
447 const struct dblock *first_block;
448
449 /* Plug Data Area (PDA) */
Pavel Roskind133ae42005-09-23 04:18:06 -0400450 __le16 pda[PDA_WORDS];
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400451
452 /* Binary block begins after the 0x1A marker */
453 ptr = image;
454 while (*ptr++ != TEXT_END);
455 first_block = (const struct dblock *) ptr;
456
457 /* Read the PDA */
458 if (image != primsym) {
459 ret = spectrum_read_pda(hw, pda, sizeof(pda));
460 if (ret)
461 return ret;
462 }
463
464 /* Stop the firmware, so that it can be safely rewritten */
465 ret = spectrum_reset(link, 1);
466 if (ret)
467 return ret;
468
469 /* Program the adapter with new firmware */
470 ret = spectrum_load_blocks(hw, first_block);
471 if (ret)
472 return ret;
473
474 /* Write the PDA to the adapter */
475 if (image != primsym) {
476 ret = spectrum_apply_pda(hw, first_block, pda);
477 if (ret)
478 return ret;
479 }
480
481 /* Run the firmware */
482 ret = spectrum_reset(link, 0);
483 if (ret)
484 return ret;
485
486 /* Reset hermes chip and make sure it responds */
487 ret = hermes_init(hw);
488
489 /* hermes_reset() should return 0 with the secondary firmware */
490 if (image != primsym && ret != 0)
491 return -ENODEV;
492
493 /* And this should work with any firmware */
494 if (!hermes_present(hw))
495 return -ENODEV;
496
497 return 0;
498}
499
500
501/*
502 * Download the firmware into the card, this also does a PCMCIA soft
503 * reset on the card, to make sure it's in a sane state.
504 */
505static int
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200506spectrum_dl_firmware(hermes_t *hw, struct pcmcia_device *link)
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400507{
508 int ret;
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400509 const struct firmware *fw_entry;
510
511 if (request_firmware(&fw_entry, primary_fw_name,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200512 &handle_to_dev(link)) == 0) {
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400513 primsym = fw_entry->data;
514 } else {
515 printk(KERN_ERR PFX "Cannot find firmware: %s\n",
516 primary_fw_name);
517 return -ENOENT;
518 }
519
520 if (request_firmware(&fw_entry, secondary_fw_name,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200521 &handle_to_dev(link)) == 0) {
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400522 secsym = fw_entry->data;
523 } else {
524 printk(KERN_ERR PFX "Cannot find firmware: %s\n",
525 secondary_fw_name);
526 return -ENOENT;
527 }
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400528
529 /* Load primary firmware */
530 ret = spectrum_dl_image(hw, link, primsym);
531 if (ret) {
532 printk(KERN_ERR PFX "Primary firmware download failed\n");
533 return ret;
534 }
535
536 /* Load secondary firmware */
537 ret = spectrum_dl_image(hw, link, secsym);
538
539 if (ret) {
540 printk(KERN_ERR PFX "Secondary firmware download failed\n");
541 }
542
543 return ret;
544}
545
546/********************************************************************/
547/* Device methods */
548/********************************************************************/
549
550static int
551spectrum_cs_hard_reset(struct orinoco_private *priv)
552{
553 struct orinoco_pccard *card = priv->card;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200554 struct pcmcia_device *link = card->p_dev;
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400555 int err;
556
557 if (!hermes_present(&priv->hw)) {
558 /* The firmware needs to be reloaded */
Dominik Brodowskifd238232006-03-05 10:45:09 +0100559 if (spectrum_dl_firmware(&priv->hw, link) != 0) {
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400560 printk(KERN_ERR PFX "Firmware download failed\n");
561 err = -ENODEV;
562 }
563 } else {
564 /* Soft reset using COR and HCR */
565 spectrum_reset(link, 0);
566 }
567
568 return 0;
569}
570
571/********************************************************************/
572/* PCMCIA stuff */
573/********************************************************************/
574
575/*
576 * This creates an "instance" of the driver, allocating local data
577 * structures for one device. The device is registered with Card
578 * Services.
579 *
580 * The dev_link structure is initialized, but we don't actually
581 * configure the card at this point -- we wait until we receive a card
582 * insertion event. */
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100583static int
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200584spectrum_cs_probe(struct pcmcia_device *link)
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400585{
586 struct net_device *dev;
587 struct orinoco_private *priv;
588 struct orinoco_pccard *card;
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400589
590 dev = alloc_orinocodev(sizeof(*card), spectrum_cs_hard_reset);
591 if (! dev)
Dominik Brodowskif8cfa612005-11-14 21:25:51 +0100592 return -ENOMEM;
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400593 priv = netdev_priv(dev);
594 card = priv->card;
595
596 /* Link both structures together */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200597 card->p_dev = link;
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400598 link->priv = dev;
599
600 /* Interrupt setup */
601 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
602 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
603 link->irq.Handler = orinoco_interrupt;
604 link->irq.Instance = dev;
605
606 /* General socket configuration defaults can go here. In this
607 * client, we assume very little, and rely on the CIS for
608 * almost everything. In most clients, many details (i.e.,
609 * number, sizes, and attributes of IO windows) are fixed by
610 * the nature of the device, and can be hard-wired here. */
611 link->conf.Attributes = 0;
612 link->conf.IntType = INT_MEMORY_AND_IO;
613
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200614 return spectrum_cs_config(link);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400615} /* spectrum_cs_attach */
616
617/*
618 * This deletes a driver "instance". The device is de-registered with
619 * Card Services. If it has been released, all local data structures
620 * are freed. Otherwise, the structures will be freed when the device
621 * is released.
622 */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200623static void spectrum_cs_detach(struct pcmcia_device *link)
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400624{
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400625 struct net_device *dev = link->priv;
626
Pavel Roskine4f4f982006-05-01 02:13:24 -0400627 if (link->dev_node)
628 unregister_netdev(dev);
629
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100630 spectrum_cs_release(link);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400631
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400632 free_orinocodev(dev);
633} /* spectrum_cs_detach */
634
635/*
636 * spectrum_cs_config() is scheduled to run after a CARD_INSERTION
637 * event is received, to configure the PCMCIA socket, and to make the
638 * device available to the system.
639 */
640
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200641static int
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200642spectrum_cs_config(struct pcmcia_device *link)
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400643{
644 struct net_device *dev = link->priv;
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400645 struct orinoco_private *priv = netdev_priv(dev);
646 struct orinoco_pccard *card = priv->card;
647 hermes_t *hw = &priv->hw;
648 int last_fn, last_ret;
649 u_char buf[64];
650 config_info_t conf;
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400651 tuple_t tuple;
652 cisparse_t parse;
653 void __iomem *mem;
654
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400655 /*
656 * This reads the card's CONFIG tuple to find its
657 * configuration registers.
658 */
659 tuple.DesiredTuple = CISTPL_CONFIG;
660 tuple.Attributes = 0;
661 tuple.TupleData = buf;
662 tuple.TupleDataMax = sizeof(buf);
663 tuple.TupleOffset = 0;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200664 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
665 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(link, &tuple));
666 CS_CHECK(ParseTuple, pcmcia_parse_tuple(link, &tuple, &parse));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400667 link->conf.ConfigBase = parse.config.base;
668 link->conf.Present = parse.config.rmask[0];
669
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400670 /* Look up the current Vcc */
671 CS_CHECK(GetConfigurationInfo,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200672 pcmcia_get_configuration_info(link, &conf));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400673
674 /*
675 * In this loop, we scan the CIS for configuration table
676 * entries, each of which describes a valid card
677 * configuration, including voltage, IO window, memory window,
678 * and interrupt settings.
679 *
680 * We make no assumptions about the card to be configured: we
681 * use just the information available in the CIS. In an ideal
682 * world, this would work for any PCMCIA card, but it requires
683 * a complete and accurate CIS. In practice, a driver usually
684 * "knows" most of these things without consulting the CIS,
685 * and most client drivers will only use the CIS to fill in
686 * implementation-defined details.
687 */
688 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200689 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(link, &tuple));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400690 while (1) {
691 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
692 cistpl_cftable_entry_t dflt = { .index = 0 };
693
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200694 if ( (pcmcia_get_tuple_data(link, &tuple) != 0)
695 || (pcmcia_parse_tuple(link, &tuple, &parse) != 0))
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400696 goto next_entry;
697
698 if (cfg->flags & CISTPL_CFTABLE_DEFAULT)
699 dflt = *cfg;
700 if (cfg->index == 0)
701 goto next_entry;
702 link->conf.ConfigIndex = cfg->index;
703
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400704 /* Use power settings for Vcc and Vpp if present */
705 /* Note that the CIS values need to be rescaled */
706 if (cfg->vcc.present & (1 << CISTPL_POWER_VNOM)) {
707 if (conf.Vcc != cfg->vcc.param[CISTPL_POWER_VNOM] / 10000) {
708 DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, cfg->vcc.param[CISTPL_POWER_VNOM] / 10000);
709 if (!ignore_cis_vcc)
710 goto next_entry;
711 }
712 } else if (dflt.vcc.present & (1 << CISTPL_POWER_VNOM)) {
713 if (conf.Vcc != dflt.vcc.param[CISTPL_POWER_VNOM] / 10000) {
714 DEBUG(2, "spectrum_cs_config: Vcc mismatch (conf.Vcc = %d, CIS = %d)\n", conf.Vcc, dflt.vcc.param[CISTPL_POWER_VNOM] / 10000);
715 if(!ignore_cis_vcc)
716 goto next_entry;
717 }
718 }
719
720 if (cfg->vpp1.present & (1 << CISTPL_POWER_VNOM))
Dominik Brodowski70294b42006-01-15 12:43:16 +0100721 link->conf.Vpp =
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400722 cfg->vpp1.param[CISTPL_POWER_VNOM] / 10000;
723 else if (dflt.vpp1.present & (1 << CISTPL_POWER_VNOM))
Dominik Brodowski70294b42006-01-15 12:43:16 +0100724 link->conf.Vpp =
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400725 dflt.vpp1.param[CISTPL_POWER_VNOM] / 10000;
726
727 /* Do we need to allocate an interrupt? */
728 link->conf.Attributes |= CONF_ENABLE_IRQ;
729
730 /* IO window settings */
731 link->io.NumPorts1 = link->io.NumPorts2 = 0;
732 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
733 cistpl_io_t *io =
734 (cfg->io.nwin) ? &cfg->io : &dflt.io;
735 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
736 if (!(io->flags & CISTPL_IO_8BIT))
737 link->io.Attributes1 =
738 IO_DATA_PATH_WIDTH_16;
739 if (!(io->flags & CISTPL_IO_16BIT))
740 link->io.Attributes1 =
741 IO_DATA_PATH_WIDTH_8;
742 link->io.IOAddrLines =
743 io->flags & CISTPL_IO_LINES_MASK;
744 link->io.BasePort1 = io->win[0].base;
745 link->io.NumPorts1 = io->win[0].len;
746 if (io->nwin > 1) {
747 link->io.Attributes2 =
748 link->io.Attributes1;
749 link->io.BasePort2 = io->win[1].base;
750 link->io.NumPorts2 = io->win[1].len;
751 }
752
753 /* This reserves IO space but doesn't actually enable it */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200754 if (pcmcia_request_io(link, &link->io) != 0)
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400755 goto next_entry;
756 }
757
758
759 /* If we got this far, we're cool! */
760
761 break;
762
763 next_entry:
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200764 pcmcia_disable_device(link);
765 last_ret = pcmcia_get_next_tuple(link, &tuple);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400766 if (last_ret == CS_NO_MORE_ITEMS) {
767 printk(KERN_ERR PFX "GetNextTuple(): No matching "
768 "CIS configuration. Maybe you need the "
769 "ignore_cis_vcc=1 parameter.\n");
770 goto cs_failed;
771 }
772 }
773
774 /*
775 * Allocate an interrupt line. Note that this does not assign
776 * a handler to the interrupt, unless the 'Handler' member of
777 * the irq structure is initialized.
778 */
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200779 CS_CHECK(RequestIRQ, pcmcia_request_irq(link, &link->irq));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400780
781 /* We initialize the hermes structure before completing PCMCIA
782 * configuration just in case the interrupt handler gets
783 * called. */
784 mem = ioport_map(link->io.BasePort1, link->io.NumPorts1);
785 if (!mem)
786 goto cs_failed;
787
788 hermes_struct_init(hw, mem, HERMES_16BIT_REGSPACING);
789
790 /*
791 * This actually configures the PCMCIA socket -- setting up
792 * the I/O windows and the interrupt mapping, and putting the
793 * card and host interface into "Memory and IO" mode.
794 */
795 CS_CHECK(RequestConfiguration,
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200796 pcmcia_request_configuration(link, &link->conf));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400797
798 /* Ok, we have the configuration, prepare to register the netdev */
799 dev->base_addr = link->io.BasePort1;
800 dev->irq = link->irq.AssignedIRQ;
801 SET_MODULE_OWNER(dev);
802 card->node.major = card->node.minor = 0;
803
804 /* Reset card and download firmware */
805 if (spectrum_cs_hard_reset(priv) != 0) {
806 goto failed;
807 }
808
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200809 SET_NETDEV_DEV(dev, &handle_to_dev(link));
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400810 /* Tell the stack we exist */
811 if (register_netdev(dev) != 0) {
812 printk(KERN_ERR PFX "register_netdev() failed\n");
813 goto failed;
814 }
815
816 /* At this point, the dev_node_t structure(s) needs to be
Dominik Brodowskifd238232006-03-05 10:45:09 +0100817 * initialized and arranged in a linked list at link->dev_node. */
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400818 strcpy(card->node.dev_name, dev->name);
Dominik Brodowskifd238232006-03-05 10:45:09 +0100819 link->dev_node = &card->node; /* link->dev_node being non-NULL is also
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400820 used to indicate that the
821 net_device has been registered */
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400822
823 /* Finally, report what we've done */
Pavel Roskin9a568da2006-05-01 02:13:26 -0400824 printk(KERN_DEBUG "%s: " DRIVER_NAME " at %s, irq %d, io "
825 "0x%04x-0x%04x\n", dev->name, dev->class_dev.dev->bus_id,
826 link->irq.AssignedIRQ, link->io.BasePort1,
827 link->io.BasePort1 + link->io.NumPorts1 - 1);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400828
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200829 return 0;
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400830
831 cs_failed:
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200832 cs_error(link, last_fn, last_ret);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400833
834 failed:
835 spectrum_cs_release(link);
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200836 return -ENODEV;
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400837} /* spectrum_cs_config */
838
839/*
840 * After a card is removed, spectrum_cs_release() will unregister the
841 * device, and release the PCMCIA configuration. If the device is
842 * still open, this will be postponed until it is closed.
843 */
844static void
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200845spectrum_cs_release(struct pcmcia_device *link)
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400846{
847 struct net_device *dev = link->priv;
848 struct orinoco_private *priv = netdev_priv(dev);
849 unsigned long flags;
850
851 /* We're committed to taking the device away now, so mark the
852 * hardware as unavailable */
853 spin_lock_irqsave(&priv->lock, flags);
854 priv->hw_unavailable++;
855 spin_unlock_irqrestore(&priv->lock, flags);
856
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200857 pcmcia_disable_device(link);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400858 if (priv->hw.iobase)
859 ioport_unmap(priv->hw.iobase);
860} /* spectrum_cs_release */
861
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100862
863static int
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200864spectrum_cs_suspend(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100865{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100866 struct net_device *dev = link->priv;
867 struct orinoco_private *priv = netdev_priv(dev);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100868 int err = 0;
869
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100870 /* Mark the device as stopped, to block IO until later */
Pavel Roskin6cbaa332006-05-01 02:13:28 -0400871 spin_lock(&priv->lock);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100872
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100873 err = __orinoco_down(dev);
874 if (err)
875 printk(KERN_WARNING "%s: Error %d downing interface\n",
876 dev->name, err);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100877
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100878 netif_device_detach(dev);
879 priv->hw_unavailable++;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100880
Pavel Roskin6cbaa332006-05-01 02:13:28 -0400881 spin_unlock(&priv->lock);
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100882
Pavel Roskin6cbaa332006-05-01 02:13:28 -0400883 return err;
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100884}
885
886static int
Dominik Brodowskifba395e2006-03-31 17:21:06 +0200887spectrum_cs_resume(struct pcmcia_device *link)
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100888{
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100889 struct net_device *dev = link->priv;
890 struct orinoco_private *priv = netdev_priv(dev);
891
Dominik Brodowskie2d40962006-03-02 00:09:29 +0100892 netif_device_attach(dev);
893 priv->hw_unavailable--;
894 schedule_work(&priv->reset_work);
895
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100896 return 0;
897}
898
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400899
900/********************************************************************/
901/* Module initialization */
902/********************************************************************/
903
904/* Can't be declared "const" or the whole __initdata section will
905 * become const */
906static char version[] __initdata = DRIVER_NAME " " DRIVER_VERSION
907 " (Pavel Roskin <proski@gnu.org>,"
908 " David Gibson <hermes@gibson.dropbear.id.au>, et al)";
909
910static struct pcmcia_device_id spectrum_cs_ids[] = {
Pavel Roskin4ebe2eb2006-04-07 04:10:32 -0400911 PCMCIA_DEVICE_MANF_CARD(0x026c, 0x0001), /* Symbol Spectrum24 LA4137 */
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400912 PCMCIA_DEVICE_MANF_CARD(0x0104, 0x0001), /* Socket Communications CF */
Pavel Roskin9c8a11d2005-09-16 02:18:31 -0400913 PCMCIA_DEVICE_PROD_ID12("Intel", "PRO/Wireless LAN PC Card", 0x816cc815, 0x6fbf459a), /* 2011B, not 2011 */
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400914 PCMCIA_DEVICE_NULL,
915};
916MODULE_DEVICE_TABLE(pcmcia, spectrum_cs_ids);
917
918static struct pcmcia_driver orinoco_driver = {
919 .owner = THIS_MODULE,
920 .drv = {
921 .name = DRIVER_NAME,
922 },
Dominik Brodowski15b99ac2006-03-31 17:26:06 +0200923 .probe = spectrum_cs_probe,
Dominik Brodowskicc3b4862005-11-14 21:23:14 +0100924 .remove = spectrum_cs_detach,
Dominik Brodowski98e4c282005-11-14 21:21:18 +0100925 .suspend = spectrum_cs_suspend,
926 .resume = spectrum_cs_resume,
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400927 .id_table = spectrum_cs_ids,
928};
929
930static int __init
931init_spectrum_cs(void)
932{
933 printk(KERN_DEBUG "%s\n", version);
934
935 return pcmcia_register_driver(&orinoco_driver);
936}
937
938static void __exit
939exit_spectrum_cs(void)
940{
941 pcmcia_unregister_driver(&orinoco_driver);
Pavel Roskin3a48c4c2005-09-01 20:10:06 -0400942}
943
944module_init(init_spectrum_cs);
945module_exit(exit_spectrum_cs);