VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / net / wireless / atmel_cs.c
1 /*** -*- linux-c -*- **********************************************************
2
3      Driver for Atmel at76c502 at76c504 and at76c506 wireless cards.
4
5         Copyright 2000-2001 ATMEL Corporation.
6         Copyright 2003 Simon Kelley.
7
8     This code was developed from version 2.1.1 of the Atmel drivers, 
9     released by Atmel corp. under the GPL in December 2002. It also 
10     includes code from the Linux aironet drivers (C) Benjamin Reed, 
11     and the Linux PCMCIA package, (C) David Hinds. 
12
13     For all queries about this code, please contact the current author, 
14     Simon Kelley <simon@thekelleys.org.uk> and not Atmel Corporation.
15
16     This program is free software; you can redistribute it and/or modify
17     it under the terms of the GNU General Public License as published by
18     the Free Software Foundation; either version 2 of the License, or
19     (at your option) any later version.
20
21     This software is distributed in the hope that it will be useful,
22     but WITHOUT ANY WARRANTY; without even the implied warranty of
23     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24     GNU General Public License for more details.
25
26     You should have received a copy of the GNU General Public License
27     along with Atmel wireless lan drivers; if not, write to the Free Software
28     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29
30 ******************************************************************************/
31
32 #include <linux/config.h>
33 #ifdef __IN_PCMCIA_PACKAGE__
34 #include <pcmcia/k_compat.h>
35 #endif
36 #include <linux/init.h>
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/ptrace.h>
40 #include <linux/slab.h>
41 #include <linux/string.h>
42 #include <linux/netdevice.h>
43 #include <linux/moduleparam.h>
44 #include <linux/device.h>
45
46 #include <pcmcia/version.h>
47 #include <pcmcia/cs_types.h>
48 #include <pcmcia/cs.h>
49 #include <pcmcia/cistpl.h>
50 #include <pcmcia/cisreg.h>
51 #include <pcmcia/ds.h>
52 #include <pcmcia/ciscode.h>
53
54 #include <asm/io.h>
55 #include <asm/system.h>
56 #include <linux/wireless.h>
57
58
59 /*
60    All the PCMCIA modules use PCMCIA_DEBUG to control debugging.  If
61    you do not define PCMCIA_DEBUG at all, all the debug code will be
62    left out.  If you compile with PCMCIA_DEBUG=0, the debug code will
63    be present but disabled -- but it can then be enabled for specific
64    modules at load time with a 'pc_debug=#' option to insmod.
65 */
66 #ifdef PCMCIA_DEBUG
67 static int pc_debug = PCMCIA_DEBUG;
68 MODULE_PARM(pc_debug, "i");
69 static char *version = "$Revision: 1.2 $";
70 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args);
71 #else
72 #define DEBUG(n, args...)
73 #endif
74
75 /*====================================================================*/
76
77 /* Parameters that can be set with 'insmod' */
78
79 /* The old way: bit map of interrupts to choose from */
80 /* This means pick from 15, 14, 12, 11, 10, 9, 7, 5, 4, and 3 */
81 static u_int irq_mask = 0xdeb8;
82 /* Newer, simpler way of listing specific interrupts */
83 static int irq_list[4] = { -1 };
84
85 MODULE_AUTHOR("Simon Kelley");
86 MODULE_DESCRIPTION("Support for Atmel at76c50x 802.11 wireless ethernet cards.");
87 MODULE_LICENSE("GPL");
88 MODULE_SUPPORTED_DEVICE("Atmel at76c50x PCMCIA cards");
89 MODULE_PARM(irq_mask, "i");
90 MODULE_PARM(irq_list, "1-4i");
91
92 /*====================================================================*/
93
94 /*
95    The event() function is this driver's Card Services event handler.
96    It will be called by Card Services when an appropriate card status
97    event is received.  The config() and release() entry points are
98    used to configure or release a socket, in response to card
99    insertion and ejection events.  They are invoked from the atmel_cs
100    event handler. 
101 */
102
103 struct net_device *init_atmel_card(int, int, char *, struct device *, 
104                                     int (*present_func)(void *), void * );
105 void stop_atmel_card( struct net_device *, int );
106 int atmel_open( struct net_device * );
107
108 static void atmel_config(dev_link_t *link);
109 static void atmel_release(dev_link_t *link);
110 static int atmel_event(event_t event, int priority,
111                        event_callback_args_t *args);
112
113 /*
114    The attach() and detach() entry points are used to create and destroy
115    "instances" of the driver, where each instance represents everything
116    needed to manage one actual PCMCIA card.
117 */
118
119 static dev_link_t *atmel_attach(void);
120 static void atmel_detach(dev_link_t *);
121
122 /*
123    You'll also need to prototype all the functions that will actually
124    be used to talk to your device.  See 'pcmem_cs' for a good example
125    of a fully self-sufficient driver; the other drivers rely more or
126    less on other parts of the kernel.
127 */
128
129 /*
130    The dev_info variable is the "key" that is used to match up this
131    device driver with appropriate cards, through the card configuration
132    database.
133 */
134
135 static dev_info_t dev_info = "atmel_cs";
136
137 /*
138    A linked list of "instances" of the  atmelnet device.  Each actual
139    PCMCIA card corresponds to one device instance, and is described
140    by one dev_link_t structure (defined in ds.h).
141
142    You may not want to use a linked list for this -- for example, the
143    memory card driver uses an array of dev_link_t pointers, where minor
144    device numbers are used to derive the corresponding array index.
145 */
146
147 static dev_link_t *dev_list = NULL;
148
149 /*
150    A dev_link_t structure has fields for most things that are needed
151    to keep track of a socket, but there will usually be some device
152    specific information that also needs to be kept track of.  The
153    'priv' pointer in a dev_link_t structure can be used to point to
154    a device-specific private data structure, like this.
155
156    A driver needs to provide a dev_node_t structure for each device
157    on a card.  In some cases, there is only one device per card (for
158    example, ethernet cards, modems).  In other cases, there may be
159    many actual or logical devices (SCSI adapters, memory cards with
160    multiple partitions).  The dev_node_t structures need to be kept
161    in a linked list starting at the 'dev' field of a dev_link_t
162    structure.  We allocate them in the card's private data structure,
163    because they generally shouldn't be allocated dynamically.
164
165    In this case, we also provide a flag to indicate if a device is
166    "stopped" due to a power management event, or card ejection.  The
167    device IO routines can use a flag like this to throttle IO to a
168    card that is not ready to accept it.
169 */
170    
171 typedef struct local_info_t {
172         dev_node_t      node;
173         struct net_device *eth_dev;
174 } local_info_t;
175
176 /*======================================================================
177   
178   atmel_attach() creates an "instance" of the driver, allocating
179   local data structures for one device.  The device is registered
180   with Card Services.
181   
182   The dev_link structure is initialized, but we don't actually
183   configure the card at this point -- we wait until we receive a
184   card insertion event.
185   
186   ======================================================================*/
187
188 static dev_link_t *atmel_attach(void)
189 {
190         client_reg_t client_reg;
191         dev_link_t *link;
192         local_info_t *local;
193         int ret, i;
194         
195         DEBUG(0, "atmel_attach()\n");
196
197         /* Initialize the dev_link_t structure */
198         link = kmalloc(sizeof(struct dev_link_t), GFP_KERNEL);
199         if (!link) {
200                 printk(KERN_ERR "atmel_cs: no memory for new device\n");
201                 return NULL;
202         }
203         memset(link, 0, sizeof(struct dev_link_t));
204         
205         /* Interrupt setup */
206         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
207         link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
208         if (irq_list[0] == -1)
209                 link->irq.IRQInfo2 = irq_mask;
210         else
211                 for (i = 0; i < 4; i++)
212                         link->irq.IRQInfo2 |= 1 << irq_list[i];
213         link->irq.Handler = NULL;
214         
215         /*
216           General socket configuration defaults can go here.  In this
217           client, we assume very little, and rely on the CIS for almost
218           everything.  In most clients, many details (i.e., number, sizes,
219           and attributes of IO windows) are fixed by the nature of the
220           device, and can be hard-wired here.
221         */
222         link->conf.Attributes = 0;
223         link->conf.Vcc = 50;
224         link->conf.IntType = INT_MEMORY_AND_IO;
225         
226         /* Allocate space for private device-specific data */
227         local = kmalloc(sizeof(local_info_t), GFP_KERNEL);
228         if (!local) {
229                 printk(KERN_ERR "atmel_cs: no memory for new device\n");
230                 kfree (link);
231                 return NULL;
232         }
233         memset(local, 0, sizeof(local_info_t));
234         link->priv = local;
235         
236         /* Register with Card Services */
237         link->next = dev_list;
238         dev_list = link;
239         client_reg.dev_info = &dev_info;
240         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
241         client_reg.EventMask =
242                 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
243                 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
244                 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
245         client_reg.event_handler = &atmel_event;
246         client_reg.Version = 0x0210;
247         client_reg.event_callback_args.client_data = link;
248         ret = pcmcia_register_client(&link->handle, &client_reg);
249         if (ret != 0) {
250                 cs_error(link->handle, RegisterClient, ret);
251                 atmel_detach(link);
252                 return NULL;
253         }
254         
255         return link;
256 } /* atmel_attach */
257
258 /*======================================================================
259   
260   This deletes a driver "instance".  The device is de-registered
261   with Card Services.  If it has been released, all local data
262   structures are freed.  Otherwise, the structures will be freed
263   when the device is released.
264   
265   ======================================================================*/
266
267 static void atmel_detach(dev_link_t *link)
268 {
269         dev_link_t **linkp;
270         
271         DEBUG(0, "atmel_detach(0x%p)\n", link);
272         
273         /* Locate device structure */
274         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
275                 if (*linkp == link) break;
276         if (*linkp == NULL)
277                 return;
278
279         if (link->state & DEV_CONFIG)
280                 atmel_release(link);
281                 
282         /* Break the link with Card Services */
283         if (link->handle)
284                 pcmcia_deregister_client(link->handle);
285
286         /* Unlink device structure, free pieces */
287         *linkp = link->next;
288         if (link->priv)
289                 kfree(link->priv);
290         kfree(link);
291 }
292
293 /*======================================================================
294   
295   atmel_config() is scheduled to run after a CARD_INSERTION event
296   is received, to configure the PCMCIA socket, and to make the
297   device available to the system.
298   
299   ======================================================================*/
300
301 #define CS_CHECK(fn, ret) \
302 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
303
304 /* Call-back function to interrogate PCMCIA-specific information
305    about the current existance of the card */
306 static int card_present(void *arg)
307
308         dev_link_t *link = (dev_link_t *)arg;
309         if (link->state & DEV_SUSPEND)
310                 return 0;
311         else if (link->state & DEV_PRESENT)
312                 return 1;
313         
314         return 0;
315 }
316
317 /* list of cards we know about and their firmware requirements.
318    Go either by Manfid or version strings.
319    Cards not in this list will need a firmware parameter to the module
320    in all probability. Note that the SMC 2632 V2 and V3 have the same
321    manfids, so we ignore those and use the version1 strings. */
322
323 static struct { 
324         int manf, card;
325         char *ver1;
326         char *firmware;
327         char *name;
328 } card_table[] = {
329         { 0, 0, "WLAN/802.11b PC CARD", "atmel_at76c502d%s.bin", "Actiontec 802CAT1" },  
330         { 0, 0, "ATMEL/AT76C502AR", "atmel_at76c502%s.bin", "NoName-RFMD" }, 
331         { 0, 0, "ATMEL/AT76C502AR_D", "atmel_at76c502d%s.bin", "NoName-revD" }, 
332         { 0, 0, "ATMEL/AT76C502AR_E", "atmel_at76c502e%s.bin", "NoName-revE" },
333         { 0, 0, "ATMEL/AT76C504", "atmel_at76c504%s.bin", "NoName-504" },
334         { 0, 0, "ATMEL/AT76C504A", "atmel_at76c504a_2958%s.bin", "NoName-504a-2958" },
335         { 0, 0, "ATMEL/AT76C504_R", "atmel_at76c504_2958%s.bin", "NoName-504-2958" },
336         { MANFID_3COM, 0x0620, NULL, "atmel_at76c502_3com%s.bin", "3com 3CRWE62092B" }, 
337         { MANFID_3COM, 0x0696, NULL, "atmel_at76c502_3com%s.bin", "3com 3CRSHPW196" }, 
338         { 0, 0, "SMC/2632W-V2", "atmel_at76c502%s.bin", "SMC 2632W-V2" },
339         { 0, 0, "SMC/2632W", "atmel_at76c502d%s.bin", "SMC 2632W-V3" },
340         { 0xd601, 0x0007, NULL, "atmel_at76c502%s.bin", "Sitecom WLAN-011" }, 
341         { 0x01bf, 0x3302, NULL, "atmel_at76c502e%s.bin", "Belkin F5D6020-V2" }, 
342         { 0, 0, "BT/Voyager 1020 Laptop Adapter", "atmel_at76c502%s.bin", "BT Voyager 1020" },
343         { 0, 0, "IEEE 802.11b/Wireless LAN PC Card", "atmel_at76c502%s.bin", "Siemens Gigaset PC Card II" },
344         { 0, 0, "CNet/CNWLC 11Mbps Wireless PC Card V-5", "atmel_at76c502e%s.bin", "CNet CNWLC-811ARL" },
345         { 0, 0, "Wireless/PC_CARD", "atmel_at76c502d%s.bin", "Planet WL-3552" },
346         { 0, 0, "OEM/11Mbps Wireless LAN PC Card V-3", "atmel_at76c502%s.bin", "OEM 11Mbps WLAN PCMCIA Card" },
347         { 0, 0, "11WAVE/11WP611AL-E", "atmel_at76c502e%s.bin", "11WAVE WaveBuddy" } 
348 };
349
350 /* This is strictly temporary, until PCMCIA devices get integrated into the device model. */
351 static struct device *atmel_device(void)
352 {
353         static char *kobj_name = "atmel_cs";
354
355         static struct device dev = {
356                 .bus_id    = "pcmcia",
357         };
358         dev.kobj.k_name = kmalloc(strlen(kobj_name)+1, GFP_KERNEL);
359         strcpy(dev.kobj.k_name, kobj_name);
360         kobject_init(&dev.kobj);
361         
362         return &dev;
363 }
364
365 static void atmel_config(dev_link_t *link)
366 {
367         client_handle_t handle;
368         tuple_t tuple;
369         cisparse_t parse;
370         local_info_t *dev;
371         int last_fn, last_ret;
372         u_char buf[64];
373         int card_index = -1, done = 0;
374         
375         handle = link->handle;
376         dev = link->priv;
377
378         DEBUG(0, "atmel_config(0x%p)\n", link);
379         
380         tuple.Attributes = 0;
381         tuple.TupleData = buf;
382         tuple.TupleDataMax = sizeof(buf);
383         tuple.TupleOffset = 0;
384         
385         tuple.DesiredTuple = CISTPL_MANFID;
386         if (pcmcia_get_first_tuple(handle, &tuple) == 0) {
387                 int i;
388                 cistpl_manfid_t *manfid;
389                 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
390                 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
391                 manfid = &(parse.manfid);
392                 for (i = 0; i < sizeof(card_table)/sizeof(card_table[0]); i++) {
393                         if (!card_table[i].ver1 &&
394                             manfid->manf == card_table[i].manf &&
395                             manfid->card == card_table[i].card) {
396                                 card_index = i;
397                                 done = 1;
398                         }
399                 }
400         }
401
402         tuple.DesiredTuple = CISTPL_VERS_1;
403         if (!done && (pcmcia_get_first_tuple(handle, &tuple) == 0)) {
404                 int i, j, k;
405                 cistpl_vers_1_t *ver1;
406                 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
407                 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
408                 ver1 = &(parse.version_1);
409                 
410                 for (i = 0; i < sizeof(card_table)/sizeof(card_table[0]); i++) {
411                         for (j = 0; j < ver1->ns; j++) {
412                                 char *p = card_table[i].ver1;
413                                 char *q = &ver1->str[ver1->ofs[j]];
414                                 if (!p)
415                                         goto mismatch;
416                                 for (k = 0; k < j; k++) {
417                                         while ((*p != '\0') && (*p != '/')) p++;
418                                         if (*p == '\0') {
419                                                 if (*q != '\0')
420                                                         goto mismatch;
421                                         } else {
422                                                 p++;
423                                         }
424                                 }
425                                 while((*q != '\0') && (*p != '\0') && 
426                                       (*p != '/') && (*p == *q)) p++, q++;
427                                 if (((*p != '\0') && *p != '/') || *q != '\0')
428                                         goto mismatch;
429                         }
430                         card_index = i;
431                         break;  /* done */
432                         
433                 mismatch:
434                         j = 0; /* dummy stmt to shut up compiler */
435                 }
436         }               
437
438         /*
439           This reads the card's CONFIG tuple to find its configuration
440           registers.
441         */
442         tuple.DesiredTuple = CISTPL_CONFIG;
443         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
444         CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
445         CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
446         link->conf.ConfigBase = parse.config.base;
447         link->conf.Present = parse.config.rmask[0];
448         
449         /* Configure card */
450         link->state |= DEV_CONFIG;
451         
452         /*
453           In this loop, we scan the CIS for configuration table entries,
454           each of which describes a valid card configuration, including
455           voltage, IO window, memory window, and interrupt settings.
456           
457           We make no assumptions about the card to be configured: we use
458           just the information available in the CIS.  In an ideal world,
459           this would work for any PCMCIA card, but it requires a complete
460           and accurate CIS.  In practice, a driver usually "knows" most of
461           these things without consulting the CIS, and most client drivers
462           will only use the CIS to fill in implementation-defined details.
463         */
464         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
465         CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
466         while (1) {
467                 cistpl_cftable_entry_t dflt = { 0 };
468                 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
469                 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
470                                 pcmcia_parse_tuple(handle, &tuple, &parse) != 0)
471                         goto next_entry;
472                 
473                 if (cfg->flags & CISTPL_CFTABLE_DEFAULT) dflt = *cfg;
474                 if (cfg->index == 0) goto next_entry;
475                 link->conf.ConfigIndex = cfg->index;
476                 
477                 /* Does this card need audio output? */
478                 if (cfg->flags & CISTPL_CFTABLE_AUDIO) {
479                         link->conf.Attributes |= CONF_ENABLE_SPKR;
480                         link->conf.Status = CCSR_AUDIO_ENA;
481                 }
482                 
483                 /* Use power settings for Vcc and Vpp if present */
484                 /*  Note that the CIS values need to be rescaled */
485                 if (cfg->vcc.present & (1<<CISTPL_POWER_VNOM))
486                         link->conf.Vcc = cfg->vcc.param[CISTPL_POWER_VNOM]/10000;
487                 else if (dflt.vcc.present & (1<<CISTPL_POWER_VNOM))
488                         link->conf.Vcc = dflt.vcc.param[CISTPL_POWER_VNOM]/10000;
489                 
490                 if (cfg->vpp1.present & (1<<CISTPL_POWER_VNOM))
491                         link->conf.Vpp1 = link->conf.Vpp2 =
492                                 cfg->vpp1.param[CISTPL_POWER_VNOM]/10000;
493                 else if (dflt.vpp1.present & (1<<CISTPL_POWER_VNOM))
494                         link->conf.Vpp1 = link->conf.Vpp2 =
495                                 dflt.vpp1.param[CISTPL_POWER_VNOM]/10000;
496                 
497                 /* Do we need to allocate an interrupt? */
498                 if (cfg->irq.IRQInfo1 || dflt.irq.IRQInfo1)
499                         link->conf.Attributes |= CONF_ENABLE_IRQ;
500                 
501                 /* IO window settings */
502                 link->io.NumPorts1 = link->io.NumPorts2 = 0;
503                 if ((cfg->io.nwin > 0) || (dflt.io.nwin > 0)) {
504                         cistpl_io_t *io = (cfg->io.nwin) ? &cfg->io : &dflt.io;
505                         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
506                         if (!(io->flags & CISTPL_IO_8BIT))
507                                 link->io.Attributes1 = IO_DATA_PATH_WIDTH_16;
508                         if (!(io->flags & CISTPL_IO_16BIT))
509                                 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
510                         link->io.BasePort1 = io->win[0].base;
511                         link->io.NumPorts1 = io->win[0].len;
512                         if (io->nwin > 1) {
513                                 link->io.Attributes2 = link->io.Attributes1;
514                                 link->io.BasePort2 = io->win[1].base;
515                                 link->io.NumPorts2 = io->win[1].len;
516                         }
517                 }
518                 
519                 /* This reserves IO space but doesn't actually enable it */
520                 if (pcmcia_request_io(link->handle, &link->io) != 0)
521                         goto next_entry;
522
523                 /* If we got this far, we're cool! */
524                 break;
525                 
526         next_entry:
527                 CS_CHECK(GetNextTuple, pcmcia_get_next_tuple(handle, &tuple));
528         }
529         
530         /*
531           Allocate an interrupt line.  Note that this does not assign a
532           handler to the interrupt, unless the 'Handler' member of the
533           irq structure is initialized.
534         */
535         if (link->conf.Attributes & CONF_ENABLE_IRQ)
536                 CS_CHECK(RequestIRQ, pcmcia_request_irq(link->handle, &link->irq));
537         
538         /*
539           This actually configures the PCMCIA socket -- setting up
540           the I/O windows and the interrupt mapping, and putting the
541           card and host interface into "Memory and IO" mode.
542         */
543         CS_CHECK(RequestConfiguration, pcmcia_request_configuration(link->handle, &link->conf));
544         
545         if (link->irq.AssignedIRQ == 0) {
546                 printk(KERN_ALERT 
547                        "atmel: cannot assign IRQ: check that CONFIG_ISA is set in kernel config.");
548                 goto cs_failed;
549         }
550        
551         ((local_info_t*)link->priv)->eth_dev = 
552                 init_atmel_card(link->irq.AssignedIRQ,
553                                 link->io.BasePort1,
554                                 card_index == -1 ? NULL :  card_table[card_index].firmware,
555                                 atmel_device(),
556                                 card_present, 
557                                 link);
558         if (!((local_info_t*)link->priv)->eth_dev) 
559                 goto cs_failed;
560         
561         /*
562           At this point, the dev_node_t structure(s) need to be
563           initialized and arranged in a linked list at link->dev.
564         */
565         strcpy(dev->node.dev_name, ((local_info_t*)link->priv)->eth_dev->name );
566         dev->node.major = dev->node.minor = 0;
567         link->dev = &dev->node;
568         
569         /* Finally, report what we've done */
570         printk(KERN_INFO "%s: %s%sindex 0x%02x: Vcc %d.%d",
571                dev->node.dev_name,
572                card_index == -1 ? "" :  card_table[card_index].name,
573                card_index == -1 ? "" : " ",
574                link->conf.ConfigIndex,
575                link->conf.Vcc/10, link->conf.Vcc%10);
576         if (link->conf.Vpp1)
577                 printk(", Vpp %d.%d", link->conf.Vpp1/10, link->conf.Vpp1%10);
578         if (link->conf.Attributes & CONF_ENABLE_IRQ)
579                 printk(", irq %d", link->irq.AssignedIRQ);
580         if (link->io.NumPorts1)
581                 printk(", io 0x%04x-0x%04x", link->io.BasePort1,
582                        link->io.BasePort1+link->io.NumPorts1-1);
583         if (link->io.NumPorts2)
584                 printk(" & 0x%04x-0x%04x", link->io.BasePort2,
585                        link->io.BasePort2+link->io.NumPorts2-1);
586         printk("\n");
587         
588         link->state &= ~DEV_CONFIG_PENDING;
589         return;
590         
591  cs_failed:
592         cs_error(link->handle, last_fn, last_ret);
593         atmel_release(link);
594 }
595
596 /*======================================================================
597   
598   After a card is removed, atmel_release() will unregister the
599   device, and release the PCMCIA configuration.  If the device is
600   still open, this will be postponed until it is closed.
601   
602   ======================================================================*/
603
604 static void atmel_release(dev_link_t *link)
605 {
606         struct net_device *dev = ((local_info_t*)link->priv)->eth_dev;
607                 
608         DEBUG(0, "atmel_release(0x%p)\n", link);
609         
610         /* Unlink the device chain */
611         link->dev = NULL;
612         
613         if (dev) 
614                 stop_atmel_card(dev, 0);
615         ((local_info_t*)link->priv)->eth_dev = NULL; 
616         
617         /* Don't bother checking to see if these succeed or not */
618         pcmcia_release_configuration(link->handle);
619         if (link->io.NumPorts1)
620                 pcmcia_release_io(link->handle, &link->io);
621         if (link->irq.AssignedIRQ)
622                 pcmcia_release_irq(link->handle, &link->irq);
623         link->state &= ~DEV_CONFIG;
624 }
625
626 /*======================================================================
627   
628   The card status event handler.  Mostly, this schedules other
629   stuff to run after an event is received.
630
631   When a CARD_REMOVAL event is received, we immediately set a
632   private flag to block future accesses to this device.  All the
633   functions that actually access the device should check this flag
634   to make sure the card is still present.
635   
636   ======================================================================*/
637
638 static int atmel_event(event_t event, int priority,
639                       event_callback_args_t *args)
640 {
641         dev_link_t *link = args->client_data;
642         local_info_t *local = link->priv;
643         
644         DEBUG(1, "atmel_event(0x%06x)\n", event);
645         
646         switch (event) {
647         case CS_EVENT_CARD_REMOVAL:
648                 link->state &= ~DEV_PRESENT;
649                 if (link->state & DEV_CONFIG) {
650                         netif_device_detach(local->eth_dev);
651                         atmel_release(link);
652                 }
653                 break;
654         case CS_EVENT_CARD_INSERTION:
655                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
656                 atmel_config(link);
657                 break;
658         case CS_EVENT_PM_SUSPEND:
659                 link->state |= DEV_SUSPEND;
660                 /* Fall through... */
661         case CS_EVENT_RESET_PHYSICAL:
662                 if (link->state & DEV_CONFIG) {
663                         netif_device_detach(local->eth_dev);
664                         pcmcia_release_configuration(link->handle);
665                 }
666                 break;
667         case CS_EVENT_PM_RESUME:
668                 link->state &= ~DEV_SUSPEND;
669                 /* Fall through... */
670         case CS_EVENT_CARD_RESET:
671                 if (link->state & DEV_CONFIG) {
672                         pcmcia_request_configuration(link->handle, &link->conf);
673                         atmel_open(local->eth_dev);
674                         netif_device_attach(local->eth_dev);
675                 }
676                 break;
677         }
678         return 0;
679 } /* atmel_event */
680
681 /*====================================================================*/
682 static struct pcmcia_driver atmel_driver = {
683         .owner          = THIS_MODULE,
684         .drv            = {
685                 .name   = "atmel_cs",
686         },
687         .attach         = atmel_attach,
688         .detach         = atmel_detach,
689 };
690
691 static int atmel_cs_init(void)
692 {
693         return pcmcia_register_driver(&atmel_driver);
694 }
695
696 static void atmel_cs_cleanup(void)
697 {
698         pcmcia_unregister_driver(&atmel_driver);
699
700         /* XXX: this really needs to move into generic code.. */
701         while (dev_list != NULL) {
702                 if (dev_list->state & DEV_CONFIG)
703                         atmel_release(dev_list);
704                 atmel_detach(dev_list);
705         }
706 }
707
708 /*
709     This program is free software; you can redistribute it and/or
710     modify it under the terms of the GNU General Public License
711     as published by the Free Software Foundation; either version 2
712     of the License, or (at your option) any later version.
713
714     This program is distributed in the hope that it will be useful,
715     but WITHOUT ANY WARRANTY; without even the implied warranty of
716     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
717     GNU General Public License for more details.
718
719     In addition:
720
721     Redistribution and use in source and binary forms, with or without
722     modification, are permitted provided that the following conditions
723     are met:
724
725     1. Redistributions of source code must retain the above copyright
726        notice, this list of conditions and the following disclaimer.
727     2. Redistributions in binary form must reproduce the above copyright
728        notice, this list of conditions and the following disclaimer in the
729        documentation and/or other materials provided with the distribution.
730     3. The name of the author may not be used to endorse or promote
731        products derived from this software without specific prior written
732        permission.
733
734     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
735     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
736     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
737     ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
738     INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
739     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
740     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
741     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
742     STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
743     IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
744     POSSIBILITY OF SUCH DAMAGE.    
745 */
746
747 module_init(atmel_cs_init);
748 module_exit(atmel_cs_cleanup);