File: /usr/src/linux/drivers/net/pcmcia/smc91c92_cs.c

1     /*======================================================================
2     
3         A PCMCIA ethernet driver for SMC91c92-based cards.
4     
5         This driver supports Megahertz PCMCIA ethernet cards; and
6         Megahertz, Motorola, Ositech, and Psion Dacom ethernet/modem
7         multifunction cards.
8     
9         Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
10     
11         smc91c92_cs.c 1.106 2001/02/07 00:19:58
12         
13         This driver contains code written by Donald Becker
14         (becker@scyld.com), Rowan Hughes (x-csrdh@jcu.edu.au),
15         David Hinds (dahinds@users.sourceforge.net), and Erik Stahlman
16         (erik@vt.edu).  Donald wrote the SMC 91c92 code using parts of
17         Erik's SMC 91c94 driver.  Rowan wrote a similar driver, and I've
18         incorporated some parts of his driver here.  I (Dave) wrote most
19         of the PCMCIA glue code, and the Ositech support code.  Kelly
20         Stephens (kstephen@holli.com) added support for the Motorola
21         Mariner, with help from Allen Brost. 
22     
23         This software may be used and distributed according to the terms of
24         the GNU General Public License, incorporated herein by reference.
25     
26     ======================================================================*/
27     
28     #include <linux/module.h>
29     #include <linux/kernel.h>
30     #include <linux/init.h>
31     #include <linux/sched.h>
32     #include <linux/slab.h>
33     #include <linux/string.h>
34     #include <linux/timer.h>
35     #include <linux/interrupt.h>
36     #include <linux/delay.h>
37     #include <asm/io.h>
38     #include <asm/system.h>
39     
40     #include <linux/netdevice.h>
41     #include <linux/etherdevice.h>
42     #include <linux/skbuff.h>
43     #include <linux/if_arp.h>
44     #include <linux/ioport.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/ciscode.h>
52     #include <pcmcia/ds.h>
53     
54     /* Ositech Seven of Diamonds firmware */
55     #include "ositech.h"
56     
57     /*====================================================================*/
58     
59     #ifdef PCMCIA_DEBUG
60     static int pc_debug = PCMCIA_DEBUG;
61     MODULE_PARM(pc_debug, "i");
62     static const char *version =
63     "smc91c92_cs.c 0.09 1996/8/4 Donald Becker, becker@scyld.com.\n";
64     #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
65     #else
66     #define DEBUG(n, args...)
67     #endif
68     
69     static char *if_names[] = { "auto", "10baseT", "10base2"};
70     
71     /* Parameters that can be set with 'insmod' */
72     
73     /*
74       Transceiver/media type.
75        0 = auto
76        1 = 10baseT (and autoselect if #define AUTOSELECT),
77        2 = AUI/10base2,
78     */
79     static int if_port;
80     
81     /* Bit map of interrupts to choose from. */
82     static u_int irq_mask = 0xdeb8;
83     static int irq_list[4] = { -1 };
84     
85     MODULE_PARM(if_port, "i");
86     MODULE_PARM(irq_mask, "i");
87     MODULE_PARM(irq_list, "1-4i");
88     
89     /* Operational parameter that usually are not changed. */
90     
91     /* Time in jiffies before concluding Tx hung */
92     #define TX_TIMEOUT		((400*HZ)/1000)
93     
94     /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
95     #define INTR_WORK		4
96     
97     /* Times to check the check the chip before concluding that it doesn't
98        currently have room for another Tx packet. */
99     #define MEMORY_WAIT_TIME       	8
100     
101     static dev_info_t dev_info = "smc91c92_cs";
102     
103     static dev_link_t *dev_list;
104     
105     struct smc_private {
106         dev_link_t			link;
107         struct net_device		dev;
108         u_short			manfid;
109         u_short			cardid;
110         struct net_device_stats	stats;
111         dev_node_t			node;
112         struct sk_buff		*saved_skb;
113         int				packets_waiting;
114         caddr_t			base;
115         u_short			cfg;
116         struct timer_list		media;
117         int				watchdog, tx_err;
118         u_short			media_status;
119         u_short			fast_poll;
120         u_long			last_rx;
121     };
122     
123     /* Special definitions for Megahertz multifunction cards */
124     #define MEGAHERTZ_ISR		0x0380
125     
126     /* Special function registers for Motorola Mariner */
127     #define MOT_LAN			0x0000
128     #define MOT_UART		0x0020
129     #define MOT_EEPROM		0x20
130     
131     #define MOT_NORMAL \
132     (COR_LEVEL_REQ | COR_FUNC_ENA | COR_ADDR_DECODE | COR_IREQ_ENA)
133     
134     /* Special function registers for Ositech cards */
135     #define OSITECH_AUI_CTL		0x0c
136     #define OSITECH_PWRDOWN		0x0d
137     #define OSITECH_RESET		0x0e
138     #define OSITECH_ISR		0x0f
139     #define OSITECH_AUI_PWR		0x0c
140     #define OSITECH_RESET_ISR	0x0e
141     
142     #define OSI_AUI_PWR		0x40
143     #define OSI_LAN_PWRDOWN		0x02
144     #define OSI_MODEM_PWRDOWN	0x01
145     #define OSI_LAN_RESET		0x02
146     #define OSI_MODEM_RESET		0x01
147     
148     /* Symbolic constants for the SMC91c9* series chips, from Erik Stahlman. */
149     #define	BANK_SELECT		14		/* Window select register. */
150     #define SMC_SELECT_BANK(x)  { outw(x, ioaddr + BANK_SELECT); }
151     
152     /* Bank 0 registers. */
153     #define	TCR 		0	/* transmit control register */
154     #define	 TCR_CLEAR	0	/* do NOTHING */
155     #define  TCR_ENABLE	0x0001	/* if this is 1, we can transmit */
156     #define	 TCR_PAD_EN	0x0080	/* pads short packets to 64 bytes */
157     #define  TCR_MONCSN	0x0400  /* Monitor Carrier. */
158     #define  TCR_FDUPLX	0x0800  /* Full duplex mode. */
159     #define	 TCR_NORMAL TCR_ENABLE | TCR_PAD_EN
160     
161     #define EPH		2	/* Ethernet Protocol Handler report. */
162     #define  EPH_TX_SUC	0x0001
163     #define  EPH_SNGLCOL	0x0002
164     #define  EPH_MULCOL	0x0004
165     #define  EPH_LTX_MULT	0x0008
166     #define  EPH_16COL	0x0010
167     #define  EPH_SQET	0x0020
168     #define  EPH_LTX_BRD	0x0040
169     #define  EPH_TX_DEFR	0x0080
170     #define  EPH_LAT_COL	0x0200
171     #define  EPH_LOST_CAR	0x0400
172     #define  EPH_EXC_DEF	0x0800
173     #define  EPH_CTR_ROL	0x1000
174     #define  EPH_RX_OVRN	0x2000
175     #define  EPH_LINK_OK	0x4000
176     #define  EPH_TX_UNRN	0x8000
177     #define MEMINFO		8	/* Memory Information Register */
178     #define MEMCFG		10	/* Memory Configuration Register */
179     
180     /* Bank 1 registers. */
181     #define CONFIG			0
182     #define  CFG_MII_SELECT		0x8000	/* 91C100 only */
183     #define  CFG_NO_WAIT		0x1000
184     #define  CFG_FULL_STEP		0x0400
185     #define  CFG_SET_SQLCH		0x0200
186     #define  CFG_AUI_SELECT	 	0x0100
187     #define  CFG_16BIT		0x0080
188     #define  CFG_DIS_LINK		0x0040
189     #define  CFG_STATIC		0x0030
190     #define  CFG_IRQ_SEL_1		0x0004
191     #define  CFG_IRQ_SEL_0		0x0002
192     #define BASE_ADDR		2
193     #define	ADDR0			4
194     #define	GENERAL			10
195     #define	CONTROL			12
196     #define  CTL_STORE		0x0001
197     #define  CTL_RELOAD		0x0002
198     #define  CTL_EE_SELECT		0x0004
199     #define  CTL_TE_ENABLE		0x0020
200     #define  CTL_CR_ENABLE		0x0040
201     #define  CTL_LE_ENABLE		0x0080
202     #define  CTL_AUTO_RELEASE	0x0800
203     #define	 CTL_POWERDOWN		0x2000
204     
205     /* Bank 2 registers. */
206     #define MMU_CMD		0
207     #define	 MC_ALLOC	0x20  	/* or with number of 256 byte packets */
208     #define	 MC_RESET	0x40
209     #define  MC_RELEASE  	0x80  	/* remove and release the current rx packet */
210     #define  MC_FREEPKT  	0xA0  	/* Release packet in PNR register */
211     #define  MC_ENQUEUE	0xC0 	/* Enqueue the packet for transmit */
212     #define	PNR_ARR		2
213     #define FIFO_PORTS	4
214     #define  FP_RXEMPTY	0x8000
215     #define	POINTER		6
216     #define  PTR_AUTO_INC	0x0040
217     #define  PTR_READ	0x2000
218     #define	 PTR_AUTOINC 	0x4000
219     #define	 PTR_RCV	0x8000
220     #define	DATA_1		8
221     #define	INTERRUPT	12
222     #define  IM_RCV_INT		0x1
223     #define	 IM_TX_INT		0x2
224     #define	 IM_TX_EMPTY_INT	0x4
225     #define	 IM_ALLOC_INT		0x8
226     #define	 IM_RX_OVRN_INT		0x10
227     #define	 IM_EPH_INT		0x20
228     
229     #define	RCR		4
230     enum RxCfg { RxAllMulti = 0x0004, RxPromisc = 0x0002,
231     	     RxEnable = 0x0100, RxStripCRC = 0x0200};
232     #define  RCR_SOFTRESET	0x8000 	/* resets the chip */
233     #define	 RCR_STRIP_CRC	0x200	/* strips CRC */
234     #define  RCR_ENABLE	0x100	/* IFF this is set, we can receive packets */
235     #define  RCR_ALMUL	0x4 	/* receive all multicast packets */
236     #define	 RCR_PROMISC	0x2	/* enable promiscuous mode */
237     
238     /* the normal settings for the RCR register : */
239     #define	 RCR_NORMAL	(RCR_STRIP_CRC | RCR_ENABLE)
240     #define  RCR_CLEAR	0x0		/* set it to a base state */
241     #define	COUNTER		6
242     
243     /* BANK 3 -- not the same values as in smc9194! */
244     #define	MULTICAST0	0
245     #define	MULTICAST2	2
246     #define	MULTICAST4	4
247     #define	MULTICAST6	6
248     #define REVISION	0x0a
249     
250     /* Transmit status bits. */
251     #define TS_SUCCESS 0x0001
252     #define TS_16COL   0x0010
253     #define TS_LATCOL  0x0200
254     #define TS_LOSTCAR 0x0400
255     
256     /* Receive status bits. */
257     #define RS_ALGNERR	0x8000
258     #define RS_BADCRC	0x2000
259     #define RS_ODDFRAME	0x1000
260     #define RS_TOOLONG	0x0800
261     #define RS_TOOSHORT	0x0400
262     #define RS_MULTICAST	0x0001
263     #define RS_ERRORS	(RS_ALGNERR | RS_BADCRC | RS_TOOLONG | RS_TOOSHORT)
264     
265     #define set_bits(v, p) outw(inw(p)|(v), (p))
266     #define mask_bits(v, p) outw(inw(p)&(v), (p))
267     
268     /*====================================================================*/
269     
270     static dev_link_t *smc91c92_attach(void);
271     static void smc91c92_detach(dev_link_t *);
272     static void smc91c92_config(dev_link_t *link);
273     static void smc91c92_release(u_long arg);
274     static int smc91c92_event(event_t event, int priority,
275     			  event_callback_args_t *args);
276     
277     static int smc91c92_open(struct net_device *dev);
278     static int smc91c92_close(struct net_device *dev);
279     static void smc_tx_timeout(struct net_device *dev);
280     static int smc_start_xmit(struct sk_buff *skb, struct net_device *dev);
281     static void smc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
282     static void smc_rx(struct net_device *dev);
283     static struct net_device_stats *smc91c92_get_stats(struct net_device *dev);
284     static void set_rx_mode(struct net_device *dev);
285     static int s9k_config(struct net_device *dev, struct ifmap *map);
286     static void smc_set_xcvr(struct net_device *dev, int if_port);
287     static void smc_reset(struct net_device *dev);
288     static void media_check(u_long arg);
289     
290     /*======================================================================
291     
292         This bit of code is used to avoid unregistering network devices
293         at inappropriate times.  2.2 and later kernels are fairly picky
294         about when this can happen.
295         
296     ======================================================================*/
297     
298     static void flush_stale_links(void)
299     {
300         dev_link_t *link, *next;
301         for (link = dev_list; link; link = next) {
302     	next = link->next;
303     	if (link->state & DEV_STALE_LINK)
304     	    smc91c92_detach(link);
305         }
306     }
307     
308     /*====================================================================*/
309     
310     static void cs_error(client_handle_t handle, int func, int ret)
311     {
312         error_info_t err = { func, ret };
313         CardServices(ReportError, handle, &err);
314     }
315     
316     /*======================================================================
317     
318       smc91c92_attach() creates an "instance" of the driver, allocating
319       local data structures for one device.  The device is registered
320       with Card Services.
321     
322     ======================================================================*/
323     
324     static dev_link_t *smc91c92_attach(void)
325     {
326         client_reg_t client_reg;
327         struct smc_private *smc;
328         dev_link_t *link;
329         struct net_device *dev;
330         int i, ret;
331     
332         DEBUG(0, "smc91c92_attach()\n");
333         flush_stale_links();
334         
335         /* Create new ethernet device */
336         smc = kmalloc(sizeof(struct smc_private), GFP_KERNEL);
337         if (!smc) return NULL;
338         memset(smc, 0, sizeof(struct smc_private));
339         link = &smc->link; dev = &smc->dev;
340     
341         link->release.function = &smc91c92_release;
342         link->release.data = (u_long)link;
343         link->io.NumPorts1 = 16;
344         link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
345         link->io.IOAddrLines = 4;
346         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
347         link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
348         if (irq_list[0] == -1)
349     	link->irq.IRQInfo2 = irq_mask;
350         else
351     	for (i = 0; i < 4; i++)
352     	    link->irq.IRQInfo2 |= 1 << irq_list[i];
353         link->irq.Handler = &smc_interrupt;
354         link->conf.Attributes = CONF_ENABLE_IRQ;
355         link->conf.Vcc = 50;
356         link->conf.IntType = INT_MEMORY_AND_IO;
357     
358         /* The SMC91c92-specific entries in the device structure. */
359         dev->hard_start_xmit = &smc_start_xmit;
360         dev->get_stats = &smc91c92_get_stats;
361         dev->set_config = &s9k_config;
362         dev->set_multicast_list = &set_rx_mode;
363         ether_setup(dev);
364         dev->open = &smc91c92_open;
365         dev->stop = &smc91c92_close;
366     #ifdef HAVE_TX_TIMEOUT
367         dev->tx_timeout = smc_tx_timeout;
368         dev->watchdog_timeo = TX_TIMEOUT;
369     #endif
370         dev->priv = link->priv = link->irq.Instance = smc;
371         
372         /* Register with Card Services */
373         link->next = dev_list;
374         dev_list = link;
375         client_reg.dev_info = &dev_info;
376         client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
377         client_reg.EventMask = CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
378     	CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
379     	CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
380         client_reg.event_handler = &smc91c92_event;
381         client_reg.Version = 0x0210;
382         client_reg.event_callback_args.client_data = link;
383         ret = CardServices(RegisterClient, &link->handle, &client_reg);
384         if (ret != 0) {
385     	cs_error(link->handle, RegisterClient, ret);
386     	smc91c92_detach(link);
387     	return NULL;
388         }
389         
390         return link;
391     } /* smc91c92_attach */
392     
393     /*======================================================================
394     
395         This deletes a driver "instance".  The device is de-registered
396         with Card Services.  If it has been released, all local data
397         structures are freed.  Otherwise, the structures will be freed
398         when the device is released.
399     
400     ======================================================================*/
401     
402     static void smc91c92_detach(dev_link_t *link)
403     {
404         struct smc_private *smc = link->priv;
405         dev_link_t **linkp;
406     
407         DEBUG(0, "smc91c92_detach(0x%p)\n", link);
408         
409         /* Locate device structure */
410         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
411     	if (*linkp == link) break;
412         if (*linkp == NULL)
413     	return;
414         
415         del_timer(&link->release);
416         if (link->state & DEV_CONFIG) {
417     	smc91c92_release((u_long)link);
418     	if (link->state & DEV_STALE_CONFIG) {
419     	    link->state |= DEV_STALE_LINK;
420     	    return;
421     	}
422         }
423         
424         if (link->handle)
425     	CardServices(DeregisterClient, link->handle);
426         
427         /* Unlink device structure, free bits */
428         *linkp = link->next;
429         if (link->dev)
430     	unregister_netdev(&smc->dev);
431         kfree(smc);
432         
433     } /* smc91c92_detach */
434     
435     /*====================================================================*/
436     
437     static int cvt_ascii_address(struct net_device *dev, char *s)
438     {
439         int i, j, da, c;
440     
441         if (strlen(s) != 12)
442     	return -1;
443         for (i = 0; i < 6; i++) {
444     	da = 0;
445     	for (j = 0; j < 2; j++) {
446     	    c = *s++;
447     	    da <<= 4;
448     	    da += ((c >= '0') && (c <= '9')) ?
449     		(c - '0') : ((c & 0x0f) + 9);
450     	}
451     	dev->dev_addr[i] = da;
452         }
453         return 0;
454     }
455     
456     /*====================================================================*/
457     
458     static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple,
459     		     cisparse_t *parse)
460     {
461         int i;
462         i = CardServices(fn, handle, tuple);
463         if (i != CS_SUCCESS) return i;
464         i = CardServices(GetTupleData, handle, tuple);
465         if (i != CS_SUCCESS) return i;
466         return CardServices(ParseTuple, handle, tuple, parse);
467     }
468     
469     #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
470     #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
471     
472     /*======================================================================
473     
474         Configuration stuff for Megahertz cards
475     
476         mhz_3288_power() is used to power up a 3288's ethernet chip.
477         mhz_mfc_config() handles socket setup for multifunction (1144
478         and 3288) cards.  mhz_setup() gets a card's hardware ethernet
479         address.
480         
481     ======================================================================*/
482     
483     static int mhz_3288_power(dev_link_t *link)
484     {
485         struct smc_private *smc = link->priv;
486         u_char tmp;
487         
488         /* Read the ISR twice... */
489         readb(smc->base+MEGAHERTZ_ISR);
490         udelay(5);
491         readb(smc->base+MEGAHERTZ_ISR);
492     
493         /* Pause 200ms... */
494         mdelay(200);
495         
496         /* Now read and write the COR... */
497         tmp = readb(smc->base + link->conf.ConfigBase + CISREG_COR);
498         udelay(5);
499         writeb(tmp, smc->base + link->conf.ConfigBase + CISREG_COR);
500     
501         return 0;
502     }
503     
504     static int mhz_mfc_config(dev_link_t *link)
505     {
506         struct smc_private *smc = link->priv;
507         struct net_device *dev = &smc->dev;
508         tuple_t tuple;
509         cisparse_t parse;
510         u_char buf[255];
511         cistpl_cftable_entry_t *cf = &parse.cftable_entry;
512         win_req_t req;
513         memreq_t mem;
514         int i, k;
515     
516         link->conf.Attributes |= CONF_ENABLE_SPKR;
517         link->conf.Status = CCSR_AUDIO_ENA;
518         link->irq.Attributes =
519     	IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED|IRQ_HANDLE_PRESENT;
520         link->io.IOAddrLines = 16;
521         link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
522         link->io.NumPorts2 = 8;
523     
524         tuple.Attributes = tuple.TupleOffset = 0;
525         tuple.TupleData = (cisdata_t *)buf;
526         tuple.TupleDataMax = sizeof(buf);
527         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
528     
529         i = first_tuple(link->handle, &tuple, &parse);
530         /* The Megahertz combo cards have modem-like CIS entries, so
531            we have to explicitly try a bunch of port combinations. */
532         while (i == CS_SUCCESS) {
533     	link->conf.ConfigIndex = cf->index;
534     	link->io.BasePort2 = cf->io.win[0].base;
535     	for (k = 0; k < 0x400; k += 0x10) {
536     	    if (k & 0x80) continue;
537     	    link->io.BasePort1 = k ^ 0x300;
538     	    i = CardServices(RequestIO, link->handle, &link->io);
539     	    if (i == CS_SUCCESS) break;
540     	}
541     	if (i == CS_SUCCESS) break;
542     	i = next_tuple(link->handle, &tuple, &parse);
543         }
544         if (i != CS_SUCCESS)
545     	return i;
546         dev->base_addr = link->io.BasePort1;
547         
548         /* Allocate a memory window, for accessing the ISR */
549         req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
550         req.Base = req.Size = 0;
551         req.AccessSpeed = 0;
552         link->win = (window_handle_t)link->handle;
553         i = CardServices(RequestWindow, &link->win, &req);
554         if (i != CS_SUCCESS)
555     	return i;
556         smc->base = ioremap(req.Base, req.Size);
557         mem.CardOffset = mem.Page = 0;
558         if (smc->manfid == MANFID_MOTOROLA)
559     	mem.CardOffset = link->conf.ConfigBase;
560         i = CardServices(MapMemPage, link->win, &mem);
561         
562         if ((i == CS_SUCCESS)
563     	&& (smc->manfid == MANFID_MEGAHERTZ)
564     	&& (smc->cardid == PRODID_MEGAHERTZ_EM3288))
565     	mhz_3288_power(link);
566         
567         return i;
568     }
569     
570     static int mhz_setup(dev_link_t *link)
571     {
572         client_handle_t handle = link->handle;
573         struct smc_private *smc = link->priv;
574         struct net_device *dev = &smc->dev;
575         tuple_t tuple;
576         cisparse_t parse;
577         u_char buf[255], *station_addr;
578     
579         tuple.Attributes = tuple.TupleOffset = 0;
580         tuple.TupleData = buf;
581         tuple.TupleDataMax = sizeof(buf);
582     
583         /* Read the station address from the CIS.  It is stored as the last
584            (fourth) string in the Version 1 Version/ID tuple. */
585         tuple.DesiredTuple = CISTPL_VERS_1;
586         if (first_tuple(handle, &tuple, &parse) != CS_SUCCESS)
587     	return -1;
588         /* Ugh -- the EM1144 card has two VERS_1 tuples!?! */
589         if (next_tuple(handle, &tuple, &parse) != CS_SUCCESS)
590     	first_tuple(handle, &tuple, &parse);
591         if (parse.version_1.ns > 3) {
592     	station_addr = parse.version_1.str + parse.version_1.ofs[3];
593     	if (cvt_ascii_address(dev, station_addr) == 0)
594     	    return 0;
595         }
596     
597         /* Another possibility: for the EM3288, in a special tuple */
598         tuple.DesiredTuple = 0x81;
599         if (CardServices(GetFirstTuple, handle, &tuple) != CS_SUCCESS)
600     	return -1;
601         if (CardServices(GetTupleData, handle, &tuple) != CS_SUCCESS)
602     	return -1;
603         buf[12] = '\0';
604         if (cvt_ascii_address(dev, buf) == 0)
605     	return 0;
606         
607         return -1;
608     }
609     
610     /*======================================================================
611     
612         Configuration stuff for the Motorola Mariner
613     
614         mot_config() writes directly to the Mariner configuration
615         registers because the CIS is just bogus.
616         
617     ======================================================================*/
618     
619     static void mot_config(dev_link_t *link)
620     {
621         struct smc_private *smc = link->priv;
622         struct net_device *dev = &smc->dev;
623         ioaddr_t ioaddr = dev->base_addr;
624         ioaddr_t iouart = link->io.BasePort2;
625         
626         /* Set UART base address and force map with COR bit 1 */
627         writeb(iouart & 0xff,        smc->base + MOT_UART + CISREG_IOBASE_0);
628         writeb((iouart >> 8) & 0xff, smc->base + MOT_UART + CISREG_IOBASE_1);
629         writeb(MOT_NORMAL,           smc->base + MOT_UART + CISREG_COR);
630         
631         /* Set SMC base address and force map with COR bit 1 */
632         writeb(ioaddr & 0xff,        smc->base + MOT_LAN + CISREG_IOBASE_0);
633         writeb((ioaddr >> 8) & 0xff, smc->base + MOT_LAN + CISREG_IOBASE_1);
634         writeb(MOT_NORMAL,           smc->base + MOT_LAN + CISREG_COR);
635     
636         /* Wait for things to settle down */
637         mdelay(100);
638     }
639     
640     static int mot_setup(dev_link_t *link)
641     {
642         struct smc_private *smc = link->priv;
643         struct net_device *dev = &smc->dev;
644         ioaddr_t ioaddr = dev->base_addr;
645         int i, wait, loop;
646         u_int addr;
647     
648         /* Read Ethernet address from Serial EEPROM */
649         
650         for (i = 0; i < 3; i++) {
651     	SMC_SELECT_BANK(2);
652     	outw(MOT_EEPROM + i, ioaddr + POINTER);
653     	SMC_SELECT_BANK(1);
654     	outw((CTL_RELOAD | CTL_EE_SELECT), ioaddr + CONTROL);
655     
656     	for (loop = wait = 0; loop < 200; loop++) {
657     	    udelay(10);
658     	    wait = ((CTL_RELOAD | CTL_STORE) & inw(ioaddr + CONTROL));
659     	    if (wait == 0) break;
660     	}
661     	
662     	if (wait)
663     	    return -1;
664     	
665     	addr = inw(ioaddr + GENERAL);
666     	dev->dev_addr[2*i]   = addr & 0xff;
667     	dev->dev_addr[2*i+1] = (addr >> 8) & 0xff;
668         }
669         
670         return 0;
671     }
672     
673     /*====================================================================*/
674     
675     static int smc_config(dev_link_t *link)
676     {
677         struct smc_private *smc = link->priv;
678         struct net_device *dev = &smc->dev;
679         tuple_t tuple;
680         cisparse_t parse;
681         u_char buf[255];
682         cistpl_cftable_entry_t *cf = &parse.cftable_entry;
683         int i;
684     
685         tuple.Attributes = tuple.TupleOffset = 0;
686         tuple.TupleData = (cisdata_t *)buf;
687         tuple.TupleDataMax = sizeof(buf);
688         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
689     
690         link->io.NumPorts1 = 16;
691         i = first_tuple(link->handle, &tuple, &parse);
692         while (i != CS_NO_MORE_ITEMS) {
693     	if (i == CS_SUCCESS) {
694     	    link->conf.ConfigIndex = cf->index;
695     	    link->io.BasePort1 = cf->io.win[0].base;
696     	    link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
697     	    i = CardServices(RequestIO, link->handle, &link->io);
698     	    if (i == CS_SUCCESS) break;
699     	}
700     	i = next_tuple(link->handle, &tuple, &parse);
701         }
702         if (i == CS_SUCCESS)
703     	dev->base_addr = link->io.BasePort1;
704         return i;
705     }
706     
707     static int smc_setup(dev_link_t *link)
708     {
709         client_handle_t handle = link->handle;
710         struct smc_private *smc = link->priv;
711         struct net_device *dev = &smc->dev;
712         tuple_t tuple;
713         cisparse_t parse;
714         cistpl_lan_node_id_t *node_id;
715         u_char buf[255], *station_addr;
716         int i;
717     
718         tuple.Attributes = tuple.TupleOffset = 0;
719         tuple.TupleData = buf;
720         tuple.TupleDataMax = sizeof(buf);
721         
722         /* Check for a LAN function extension tuple */
723         tuple.DesiredTuple = CISTPL_FUNCE;
724         i = first_tuple(handle, &tuple, &parse);
725         while (i == CS_SUCCESS) {
726     	if (parse.funce.type == CISTPL_FUNCE_LAN_NODE_ID)
727     	    break;
728     	i = next_tuple(handle, &tuple, &parse);
729         }
730         if (i == CS_SUCCESS) {
731     	node_id = (cistpl_lan_node_id_t *)parse.funce.data;
732     	if (node_id->nb == 6) {
733     	    for (i = 0; i < 6; i++)
734     		dev->dev_addr[i] = node_id->id[i];
735     	    return 0;
736     	}
737         }
738         
739         /* Try the third string in the Version 1 Version/ID tuple. */
740         tuple.DesiredTuple = CISTPL_VERS_1;
741         if (first_tuple(handle, &tuple, &parse) != CS_SUCCESS)
742     	return -1;
743         station_addr = parse.version_1.str + parse.version_1.ofs[2];
744         if (cvt_ascii_address(dev, station_addr) == 0)
745     	return 0;
746     
747         return -1;
748     }
749     
750     /*====================================================================*/
751     
752     static int osi_config(dev_link_t *link)
753     {
754         struct smc_private *smc = link->priv;
755         struct net_device *dev = &smc->dev;
756         static ioaddr_t com[4] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
757         int i, j;
758         
759         link->conf.Attributes |= CONF_ENABLE_SPKR;
760         link->conf.Status = CCSR_AUDIO_ENA;
761         link->irq.Attributes =
762     	IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED|IRQ_HANDLE_PRESENT;
763         link->io.NumPorts1 = 64;
764         link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
765         link->io.NumPorts2 = 8;
766         link->io.IOAddrLines = 16;
767         
768         /* Enable Hard Decode, LAN, Modem */
769         link->conf.ConfigIndex = 0x23;
770         
771         for (i = j = 0; j < 4; j++) {
772     	link->io.BasePort2 = com[j];
773     	i = CardServices(RequestIO, link->handle, &link->io);
774     	if (i == CS_SUCCESS) break;
775         }
776         if (i != CS_SUCCESS) {
777     	/* Fallback: turn off hard decode */
778     	link->conf.ConfigIndex = 0x03;
779     	link->io.NumPorts2 = 0;
780     	i = CardServices(RequestIO, link->handle, &link->io);
781         }
782         dev->base_addr = link->io.BasePort1 + 0x10;
783         return i;
784     }
785     
786     static int osi_setup(dev_link_t *link, u_short manfid, u_short cardid)
787     {
788         client_handle_t handle = link->handle;
789         struct smc_private *smc = link->priv;
790         struct net_device *dev = &smc->dev;
791         tuple_t tuple;
792         u_char buf[255];
793         int i;
794         
795         tuple.Attributes = TUPLE_RETURN_COMMON;
796         tuple.TupleData = buf;
797         tuple.TupleDataMax = sizeof(buf);
798         tuple.TupleOffset = 0;
799         
800         /* Read the station address from tuple 0x90, subtuple 0x04 */
801         tuple.DesiredTuple = 0x90;
802         i = CardServices(GetFirstTuple, handle, &tuple);
803         while (i == CS_SUCCESS) {
804     	i = CardServices(GetTupleData, handle, &tuple);
805     	if ((i != CS_SUCCESS) || (buf[0] == 0x04))
806     	    break;
807     	i = CardServices(GetNextTuple, handle, &tuple);
808         }
809         if (i != CS_SUCCESS)
810     	return -1;
811         for (i = 0; i < 6; i++)
812     	dev->dev_addr[i] = buf[i+2];
813     
814         if (((manfid == MANFID_OSITECH) &&
815     	 (cardid == PRODID_OSITECH_SEVEN)) ||
816     	((manfid == MANFID_PSION) &&
817     	 (cardid == PRODID_PSION_NET100))) {
818     	/* Download the Seven of Diamonds firmware */
819     	for (i = 0; i < sizeof(__Xilinx7OD); i++) {
820     	    outb(__Xilinx7OD[i], link->io.BasePort1+2);
821     	    udelay(50);
822     	}
823         } else if (manfid == MANFID_OSITECH) {
824     	/* Make sure both functions are powered up */
825     	set_bits(0x300, link->io.BasePort1 + OSITECH_AUI_PWR);
826     	/* Now, turn on the interrupt for both card functions */
827     	set_bits(0x300, link->io.BasePort1 + OSITECH_RESET_ISR);
828     	DEBUG(2, "AUI/PWR: %4.4x RESET/ISR: %4.4x\n",
829     	      inw(link->io.BasePort1 + OSITECH_AUI_PWR),
830     	      inw(link->io.BasePort1 + OSITECH_RESET_ISR));
831         }
832     
833         return 0;
834     }
835     
836     /*======================================================================
837     
838         This verifies that the chip is some SMC91cXX variant, and returns
839         the revision code if successful.  Otherwise, it returns -ENODEV.
840         
841     ======================================================================*/
842     
843     static int check_sig(dev_link_t *link)
844     {
845         struct smc_private *smc = link->priv;
846         struct net_device *dev = &smc->dev;
847         ioaddr_t ioaddr = dev->base_addr;
848         int width;
849         u_short s;
850     
851         SMC_SELECT_BANK(1);
852         if (inw(ioaddr + BANK_SELECT) >> 8 != 0x33) {
853     	/* Try powering up the chip */
854     	outw(0, ioaddr + CONTROL);
855     	mdelay(55);
856         }
857     
858         /* Try setting bus width */
859         width = (link->io.Attributes1 == IO_DATA_PATH_WIDTH_AUTO);
860         s = inb(ioaddr + CONFIG);
861         if (width)
862     	s |= CFG_16BIT;
863         else
864     	s &= ~CFG_16BIT;
865         outb(s, ioaddr + CONFIG);
866         
867         /* Check Base Address Register to make sure bus width is OK */
868         s = inw(ioaddr + BASE_ADDR);
869         if ((inw(ioaddr + BANK_SELECT) >> 8 == 0x33) &&
870     	((s >> 8) != (s & 0xff))) {
871     	SMC_SELECT_BANK(3);
872     	s = inw(ioaddr + REVISION);
873     	return (s & 0xff);
874         }
875     
876         if (width) {
877     	event_callback_args_t args;
878     	printk(KERN_INFO "smc91c92_cs: using 8-bit IO window.\n");
879     	args.client_data = link;
880     	smc91c92_event(CS_EVENT_RESET_PHYSICAL, 0, &args);
881     	CardServices(ReleaseIO, link->handle, &link->io);
882     	link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
883     	CardServices(RequestIO, link->handle, &link->io);
884     	smc91c92_event(CS_EVENT_CARD_RESET, 0, &args);
885     	return check_sig(link);
886         }
887         return -ENODEV;
888     }
889     
890     /*======================================================================
891     
892         smc91c92_config() is scheduled to run after a CARD_INSERTION event
893         is received, to configure the PCMCIA socket, and to make the
894         ethernet device available to the system.
895     
896     ======================================================================*/
897     
898     #define CS_EXIT_TEST(ret, svc, label) \
899     if (ret != CS_SUCCESS) { cs_error(link->handle, svc, ret); goto label; }
900     
901     static void smc91c92_config(dev_link_t *link)
902     {
903         client_handle_t handle = link->handle;
904         struct smc_private *smc = link->priv;
905         struct net_device *dev = &smc->dev;
906         tuple_t tuple;
907         cisparse_t parse;
908         u_short buf[32];
909         char *name;
910         int i, rev;
911     
912         DEBUG(0, "smc91c92_config(0x%p)\n", link);
913         
914         tuple.Attributes = tuple.TupleOffset = 0;
915         tuple.TupleData = (cisdata_t *)buf;
916         tuple.TupleDataMax = sizeof(buf);
917     
918         tuple.DesiredTuple = CISTPL_CONFIG;
919         i = first_tuple(handle, &tuple, &parse);
920         CS_EXIT_TEST(i, ParseTuple, config_failed);
921         link->conf.ConfigBase = parse.config.base;
922         link->conf.Present = parse.config.rmask[0];
923         
924         tuple.DesiredTuple = CISTPL_MANFID;
925         tuple.Attributes = TUPLE_RETURN_COMMON;
926         if (first_tuple(handle, &tuple, &parse) == CS_SUCCESS) {
927     	smc->manfid = parse.manfid.manf;
928     	smc->cardid = parse.manfid.card;
929         }
930         
931         /* Configure card */
932         link->state |= DEV_CONFIG;
933     
934         if ((smc->manfid == MANFID_OSITECH) &&
935     	(smc->cardid != PRODID_OSITECH_SEVEN)) {
936     	i = osi_config(link);
937         } else if ((smc->manfid == MANFID_MOTOROLA) ||
938     	       ((smc->manfid == MANFID_MEGAHERTZ) &&
939     		((smc->cardid == PRODID_MEGAHERTZ_VARIOUS) ||
940     		 (smc->cardid == PRODID_MEGAHERTZ_EM3288)))) {
941     	i = mhz_mfc_config(link);
942         } else {
943     	i = smc_config(link);
944         }
945         CS_EXIT_TEST(i, RequestIO, config_failed);
946         
947         i = CardServices(RequestIRQ, link->handle, &link->irq);
948         CS_EXIT_TEST(i, RequestIRQ, config_failed);
949         i = CardServices(RequestConfiguration, link->handle, &link->conf);
950         CS_EXIT_TEST(i, RequestConfiguration, config_failed);
951         
952         if (smc->manfid == MANFID_MOTOROLA)
953     	mot_config(link);
954     
955         dev->irq = link->irq.AssignedIRQ;
956     
957         if ((if_port >= 0) && (if_port <= 2))
958     	dev->if_port = if_port;
959         else
960     	printk(KERN_NOTICE "smc91c92_cs: invalid if_port requested\n");
961         
962         if (register_netdev(dev) != 0) {
963     	printk(KERN_ERR "smc91c92_cs: register_netdev() failed\n");
964     	goto config_undo;
965         }
966     
967         switch (smc->manfid) {
968         case MANFID_OSITECH:
969         case MANFID_PSION:
970     	i = osi_setup(link, smc->manfid, smc->cardid); break;
971         case MANFID_SMC:
972         case MANFID_NEW_MEDIA:
973     	i = smc_setup(link); break;
974         case 0x128: /* For broken Megahertz cards */
975         case MANFID_MEGAHERTZ:
976     	i = mhz_setup(link); break;
977         case MANFID_MOTOROLA:
978         default: /* get the hw address from EEPROM */
979     	i = mot_setup(link); break;
980         }
981         
982         if (i != 0) {
983     	printk(KERN_NOTICE "smc91c92_cs: Unable to find hardware address.\n");
984     	link->state &= ~DEV_CONFIG_PENDING;
985     	goto config_undo;
986         }
987     
988         strcpy(smc->node.dev_name, dev->name);
989         link->dev = &smc->node;
990         link->state &= ~DEV_CONFIG_PENDING;
991     
992         rev = check_sig(link);
993         name = "???";
994         if (rev > 0)
995     	switch (rev >> 4) {
996     	case 3: name = "92"; break;
997     	case 4: name = ((rev & 15) >= 6) ? "96" : "94"; break;
998     	case 5: name = "95"; break;
999     	case 7: name = "100"; break;
1000     	case 8: name = "100-FD"; break;
1001     	case 9: name = "110"; break;
1002     	}
1003         printk(KERN_INFO "%s: smc91c%s rev %d: io %#3lx, irq %d, "
1004     	   "hw_addr ", dev->name, name, (rev & 0x0f), dev->base_addr,
1005     	   dev->irq);
1006         for (i = 0; i < 6; i++)
1007     	printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
1008         if (rev > 0) {
1009     	u_long mir, mcr;
1010     	ioaddr_t ioaddr = dev->base_addr;
1011     	SMC_SELECT_BANK(0);
1012     	mir = inw(ioaddr + MEMINFO) & 0xff;
1013     	if (mir == 0xff) mir++;
1014     	/* Get scale factor for memory size */
1015     	mcr = ((rev >> 4) > 3) ? inw(ioaddr + MEMCFG) : 0x0200;
1016     	mir *= 128 * (1<<((mcr >> 9) & 7));
1017     	if (mir & 0x3ff)
1018     	    printk(KERN_INFO "  %lu byte", mir);
1019     	else
1020     	    printk(KERN_INFO "  %lu kb", mir>>10);
1021     	SMC_SELECT_BANK(1);
1022     	smc->cfg = inw(ioaddr + CONFIG) & ~CFG_AUI_SELECT;
1023     	smc->cfg |= CFG_NO_WAIT | CFG_16BIT | CFG_STATIC;
1024     	if (smc->manfid == MANFID_OSITECH)
1025     	    smc->cfg |= CFG_IRQ_SEL_1 | CFG_IRQ_SEL_0;
1026     	if ((rev >> 4) >= 7)
1027     	    smc->cfg |= CFG_MII_SELECT;
1028     	printk(" buffer, %s xcvr\n", (smc->cfg & CFG_MII_SELECT) ?
1029     	       "MII" : if_names[dev->if_port]);
1030         }
1031         
1032         return;
1033         
1034     config_undo:
1035         unregister_netdev(dev);
1036     config_failed:			/* CS_EXIT_TEST() calls jump to here... */
1037         smc91c92_release((u_long)link);
1038         
1039     } /* smc91c92_config */
1040     
1041     /*======================================================================
1042     
1043         After a card is removed, smc91c92_release() will unregister the net
1044         device, and release the PCMCIA configuration.  If the device is
1045         still open, this will be postponed until it is closed.
1046     
1047     ======================================================================*/
1048     
1049     static void smc91c92_release(u_long arg)
1050     {
1051         dev_link_t *link = (dev_link_t *)arg;
1052         struct smc_private *smc = link->priv;
1053     
1054         DEBUG(0, "smc91c92_release(0x%p)\n", link);
1055         
1056         if (link->open) {
1057     	DEBUG(1, "smc91c92_cs: release postponed, '%s' still open\n",
1058     	      link->dev->dev_name);
1059     	link->state |= DEV_STALE_CONFIG;
1060     	return;
1061         }
1062         
1063         CardServices(ReleaseConfiguration, link->handle);
1064         CardServices(ReleaseIO, link->handle, &link->io);
1065         CardServices(ReleaseIRQ, link->handle, &link->irq);
1066         if (link->win) {
1067     	iounmap(smc->base);
1068     	CardServices(ReleaseWindow, link->win);
1069         }
1070         
1071         link->state &= ~DEV_CONFIG;
1072     
1073     } /* smc91c92_release */
1074     
1075     /*======================================================================
1076     
1077         The card status event handler.  Mostly, this schedules other
1078         stuff to run after an event is received.  A CARD_REMOVAL event
1079         also sets some flags to discourage the net drivers from trying
1080         to talk to the card any more.
1081     
1082     ======================================================================*/
1083     
1084     static int smc91c92_event(event_t event, int priority,
1085     			  event_callback_args_t *args)
1086     {
1087         dev_link_t *link = args->client_data;
1088         struct smc_private *smc = link->priv;
1089         struct net_device *dev = &smc->dev;
1090         
1091         DEBUG(1, "smc91c92_event(0x%06x)\n", event);
1092     
1093         switch (event) {
1094         case CS_EVENT_CARD_REMOVAL:
1095     	link->state &= ~DEV_PRESENT;
1096     	if (link->state & DEV_CONFIG) {
1097     	    netif_device_detach(dev);
1098     	    mod_timer(&link->release, jiffies + HZ/20);
1099     	}
1100     	break;
1101         case CS_EVENT_CARD_INSERTION:
1102     	link->state |= DEV_PRESENT;
1103     	smc91c92_config(link);
1104     	break;
1105         case CS_EVENT_PM_SUSPEND:
1106     	link->state |= DEV_SUSPEND;
1107     	/* Fall through... */
1108         case CS_EVENT_RESET_PHYSICAL:
1109     	if (link->state & DEV_CONFIG) {
1110     	    if (link->open)
1111     		netif_device_detach(dev);
1112     	    CardServices(ReleaseConfiguration, link->handle);
1113     	}
1114     	break;
1115         case CS_EVENT_PM_RESUME:
1116     	link->state &= ~DEV_SUSPEND;
1117     	/* Fall through... */
1118         case CS_EVENT_CARD_RESET:
1119     	if (link->state & DEV_CONFIG) {
1120     	    if ((smc->manfid == MANFID_MEGAHERTZ) &&
1121     		(smc->cardid == PRODID_MEGAHERTZ_EM3288))
1122     		mhz_3288_power(link);
1123     	    CardServices(RequestConfiguration, link->handle, &link->conf);
1124     	    if (smc->manfid == MANFID_MOTOROLA)
1125     		mot_config(link);
1126     	    if ((smc->manfid == MANFID_OSITECH) &&
1127     		(smc->cardid != PRODID_OSITECH_SEVEN)) {
1128     		/* Power up the card and enable interrupts */
1129     		set_bits(0x0300, dev->base_addr-0x10+OSITECH_AUI_PWR);
1130     		set_bits(0x0300, dev->base_addr-0x10+OSITECH_RESET_ISR);
1131     	    }
1132     	    if (link->open) {
1133     		smc_reset(dev);
1134     		netif_device_attach(dev);
1135     	    }
1136     	}
1137     	break;
1138         }
1139         return 0;
1140     } /* smc91c92_event */
1141     
1142     /*======================================================================
1143       
1144         The driver core code, most of which should be common with a
1145         non-PCMCIA implementation.
1146         
1147     ======================================================================*/
1148     
1149     #ifdef PCMCIA_DEBUG
1150     static void smc_dump(struct net_device *dev)
1151     {
1152         ioaddr_t ioaddr = dev->base_addr;
1153         u_short i, w, save;
1154         save = inw(ioaddr + BANK_SELECT);
1155         for (w = 0; w < 4; w++) {
1156     	SMC_SELECT_BANK(w);
1157     	printk(KERN_DEBUG "bank %d: ", w);
1158     	for (i = 0; i < 14; i += 2)
1159     	    printk(" %04x", inw(ioaddr + i));
1160     	printk("\n");
1161         }
1162         outw(save, ioaddr + BANK_SELECT);
1163     }
1164     #endif
1165     
1166     static int smc91c92_open(struct net_device *dev)
1167     {
1168         struct smc_private *smc = dev->priv;
1169         dev_link_t *link = &smc->link;
1170     
1171     #ifdef PCMCIA_DEBUG
1172         DEBUG(0, "%s: smc91c92_open(%p), ID/Window %4.4x.\n",
1173     	  dev->name, dev, inw(dev->base_addr + BANK_SELECT));
1174         if (pc_debug > 1) smc_dump(dev);
1175     #endif
1176         
1177         /* Check that the PCMCIA card is still here. */
1178         if (!DEV_OK(link))
1179     	return -ENODEV;
1180         /* Physical device present signature. */
1181         if (check_sig(link) < 0) {
1182     	printk("smc91c92_cs: Yikes!  Bad chip signature!\n");
1183     	return -ENODEV;
1184         }
1185         link->open++;
1186         MOD_INC_USE_COUNT;
1187     
1188         netif_start_queue(dev);
1189         smc->saved_skb = 0;
1190         smc->packets_waiting = 0;
1191         
1192         smc_reset(dev);
1193         smc->media.function = &media_check;
1194         smc->media.data = (u_long)smc;
1195         smc->media.expires = jiffies + HZ;
1196         add_timer(&smc->media);
1197         
1198         return 0;
1199     } /* smc91c92_open */
1200     
1201     /*====================================================================*/
1202     
1203     static int smc91c92_close(struct net_device *dev)
1204     {
1205         struct smc_private *smc = dev->priv;
1206         dev_link_t *link = &smc->link;
1207         ioaddr_t ioaddr = dev->base_addr;
1208     
1209         DEBUG(0, "%s: smc91c92_close(), status %4.4x.\n",
1210     	  dev->name, inw(ioaddr + BANK_SELECT));
1211     
1212         netif_stop_queue(dev);
1213     
1214         /* Shut off all interrupts, and turn off the Tx and Rx sections.
1215            Don't bother to check for chip present. */
1216         SMC_SELECT_BANK(2);	/* Nominally paranoia, but do no assume... */
1217         outw(0, ioaddr + INTERRUPT);
1218         SMC_SELECT_BANK(0);
1219         mask_bits(0xff00, ioaddr + RCR);
1220         mask_bits(0xff00, ioaddr + TCR);
1221         
1222         /* Put the chip into power-down mode. */
1223         SMC_SELECT_BANK(1);
1224         outw(CTL_POWERDOWN, ioaddr + CONTROL );
1225         
1226         link->open--;
1227         del_timer(&smc->media);
1228         if (link->state & DEV_STALE_CONFIG)
1229     	mod_timer(&link->release, jiffies + HZ/20);
1230         
1231         MOD_DEC_USE_COUNT;
1232         
1233         return 0;
1234     } /* smc91c92_close */
1235     
1236     /*======================================================================
1237     
1238        Transfer a packet to the hardware and trigger the packet send.
1239        This may be called at either from either the Tx queue code
1240        or the interrupt handler.
1241        
1242     ======================================================================*/
1243     
1244     static void smc_hardware_send_packet(struct net_device * dev)
1245     {
1246         struct smc_private *smc = dev->priv;
1247         struct sk_buff *skb = smc->saved_skb;
1248         ioaddr_t ioaddr = dev->base_addr;
1249         u_char packet_no;
1250         
1251         if (!skb) {
1252     	printk(KERN_ERR "%s: In XMIT with no packet to send.\n", dev->name);
1253     	return;
1254         }
1255         
1256         /* There should be a packet slot waiting. */
1257         packet_no = inw(ioaddr + PNR_ARR) >> 8;
1258         if (packet_no & 0x80) {
1259     	/* If not, there is a hardware problem!  Likely an ejected card. */
1260     	printk(KERN_WARNING "%s: 91c92 hardware Tx buffer allocation"
1261     	       " failed, status %#2.2x.\n", dev->name, packet_no);
1262     	dev_kfree_skb_irq(skb);
1263     	smc->saved_skb = NULL;
1264     	netif_start_queue(dev);
1265     	return;
1266         }
1267     
1268         smc->stats.tx_bytes += skb->len;
1269         /* The card should use the just-allocated buffer. */
1270         outw(packet_no, ioaddr + PNR_ARR);
1271         /* point to the beginning of the packet */
1272         outw(PTR_AUTOINC , ioaddr + POINTER);
1273         
1274         /* Send the packet length (+6 for status, length and ctl byte)
1275            and the status word (set to zeros). */
1276         {
1277     	u_char *buf = skb->data;
1278     	u_int length = skb->len; /* The chip will pad to ethernet min. */
1279     
1280     	DEBUG(2, "%s: Trying to xmit packet of length %d.\n",
1281     	      dev->name, length);
1282     	
1283     	/* send the packet length: +6 for status word, length, and ctl */
1284     	outw(0, ioaddr + DATA_1);
1285     	outw(length + 6, ioaddr + DATA_1);
1286     	outsw(ioaddr + DATA_1, buf, length >> 1);
1287     	
1288     	/* The odd last byte, if there is one, goes in the control word. */
1289     	outw((length & 1) ? 0x2000 | buf[length-1] : 0, ioaddr + DATA_1);
1290         }
1291         
1292         /* Enable the Tx interrupts, both Tx (TxErr) and TxEmpty. */
1293         outw(((IM_TX_INT|IM_TX_EMPTY_INT)<<8) |
1294     	 (inw(ioaddr + INTERRUPT) & 0xff00),
1295     	 ioaddr + INTERRUPT);
1296         
1297         /* The chip does the rest of the work. */
1298         outw(MC_ENQUEUE , ioaddr + MMU_CMD);
1299         
1300         smc->saved_skb = NULL;
1301         dev_kfree_skb_irq(skb);
1302         dev->trans_start = jiffies;
1303         netif_start_queue(dev);
1304         return;
1305     }
1306     
1307     /*====================================================================*/
1308     
1309     static void smc_tx_timeout(struct net_device *dev)
1310     {
1311         struct smc_private *smc = dev->priv;
1312         ioaddr_t ioaddr = dev->base_addr;
1313     
1314         printk(KERN_NOTICE "%s: SMC91c92 transmit timed out, "
1315     	   "Tx_status %2.2x status %4.4x.\n",
1316     	   dev->name, inw(ioaddr)&0xff, inw(ioaddr + 2));
1317         smc->stats.tx_errors++;
1318         smc_reset(dev);
1319         dev->trans_start = jiffies;
1320         smc->saved_skb = NULL;
1321         netif_wake_queue(dev);
1322     }
1323     
1324     static int smc_start_xmit(struct sk_buff *skb, struct net_device *dev)
1325     {
1326         struct smc_private *smc = dev->priv;
1327         ioaddr_t ioaddr = dev->base_addr;
1328         u_short num_pages;
1329         short time_out, ir;
1330     
1331         netif_stop_queue(dev);
1332     
1333         DEBUG(2, "%s: smc91c92_start_xmit(length = %d) called,"
1334     	  " status %4.4x.\n", dev->name, skb->len, inw(ioaddr + 2));
1335         
1336         if (smc->saved_skb) {
1337     	/* THIS SHOULD NEVER HAPPEN. */
1338     	smc->stats.tx_aborted_errors++;
1339     	printk(KERN_DEBUG "%s: Internal error -- sent packet while busy.\n",
1340     	       dev->name);
1341     	return 1;
1342         }
1343         smc->saved_skb = skb;
1344         
1345         num_pages = skb->len >> 8;
1346         
1347         if (num_pages > 7) {
1348     	printk(KERN_ERR "%s: Far too big packet error.\n", dev->name);
1349     	dev_kfree_skb (skb);
1350     	smc->saved_skb = NULL;
1351     	smc->stats.tx_dropped++;
1352     	return 0;		/* Do not re-queue this packet. */
1353         }
1354         /* A packet is now waiting. */
1355         smc->packets_waiting++;
1356         
1357         SMC_SELECT_BANK(2);	/* Paranoia, we should always be in window 2 */
1358         
1359         /* Allocate the memory; send the packet now if we win. */
1360         outw(MC_ALLOC | num_pages, ioaddr + MMU_CMD);
1361         for (time_out = MEMORY_WAIT_TIME; time_out >= 0; time_out--) {
1362     	ir = inw(ioaddr+INTERRUPT);
1363     	if (ir & IM_ALLOC_INT) {
1364     	    /* Acknowledge the interrupt, send the packet. */
1365     	    outw((ir&0xff00) | IM_ALLOC_INT, ioaddr + INTERRUPT);
1366     	    smc_hardware_send_packet(dev);	/* Send the packet now.. */
1367     	    return 0;
1368     	}
1369         }
1370         
1371         /* Otherwise defer until the Tx-space-allocated interrupt. */
1372         DEBUG(2, "%s: memory allocation deferred.\n", dev->name);
1373         outw((IM_ALLOC_INT << 8) | (ir & 0xff00), ioaddr + INTERRUPT);
1374         
1375         return 0;
1376     }
1377     
1378     /*======================================================================
1379     
1380         Handle a Tx anomolous event.  Entered while in Window 2.
1381         
1382     ======================================================================*/
1383     
1384     static void smc_tx_err(struct net_device * dev)
1385     {
1386         struct smc_private *smc = (struct smc_private *)dev->priv;
1387         ioaddr_t ioaddr = dev->base_addr;
1388         int saved_packet = inw(ioaddr + PNR_ARR) & 0xff;
1389         int packet_no = inw(ioaddr + FIFO_PORTS) & 0x7f;
1390         int tx_status;
1391         
1392         /* select this as the packet to read from */
1393         outw(packet_no, ioaddr + PNR_ARR);
1394         
1395         /* read the first word from this packet */
1396         outw(PTR_AUTOINC | PTR_READ | 0, ioaddr + POINTER);
1397         
1398         tx_status = inw(ioaddr + DATA_1);
1399     
1400         smc->stats.tx_errors++;
1401         if (tx_status & TS_LOSTCAR) smc->stats.tx_carrier_errors++;
1402         if (tx_status & TS_LATCOL)  smc->stats.tx_window_errors++;
1403         if (tx_status & TS_16COL) {
1404     	smc->stats.tx_aborted_errors++;
1405     	smc->tx_err++;
1406         }
1407         
1408         if (tx_status & TS_SUCCESS) {
1409     	printk(KERN_NOTICE "%s: Successful packet caused error "
1410     	       "interrupt?\n", dev->name);
1411         }
1412         /* re-enable transmit */
1413         SMC_SELECT_BANK(0);
1414         outw(inw(ioaddr + TCR) | TCR_ENABLE, ioaddr + TCR);
1415         SMC_SELECT_BANK(2);
1416         
1417         outw(MC_FREEPKT, ioaddr + MMU_CMD); 	/* Free the packet memory. */
1418         
1419         /* one less packet waiting for me */
1420         smc->packets_waiting--;
1421         
1422         outw(saved_packet, ioaddr + PNR_ARR);
1423         return;
1424     }
1425     
1426     /*====================================================================*/
1427     
1428     static void smc_eph_irq(struct net_device *dev)
1429     {
1430         struct smc_private *smc = dev->priv;
1431         ioaddr_t ioaddr = dev->base_addr;
1432         u_short card_stats, ephs;
1433         
1434         SMC_SELECT_BANK(0);
1435         ephs = inw(ioaddr + EPH);
1436         DEBUG(2, "%s: Ethernet protocol handler interrupt, status"
1437     	  " %4.4x.\n", dev->name, ephs);
1438         /* Could be a counter roll-over warning: update stats. */
1439         card_stats = inw(ioaddr + COUNTER);
1440         /* single collisions */
1441         smc->stats.collisions += card_stats & 0xF;
1442         card_stats >>= 4;
1443         /* multiple collisions */
1444         smc->stats.collisions += card_stats & 0xF;
1445     #if 0 		/* These are for when linux supports these statistics */
1446         card_stats >>= 4;			/* deferred */
1447         card_stats >>= 4;			/* excess deferred */
1448     #endif
1449         /* If we had a transmit error we must re-enable the transmitter. */
1450         outw(inw(ioaddr + TCR) | TCR_ENABLE, ioaddr + TCR);
1451     
1452         /* Clear a link error interrupt. */
1453         SMC_SELECT_BANK(1);
1454         outw(CTL_AUTO_RELEASE | 0x0000, ioaddr + CONTROL);
1455         outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
1456     	 ioaddr + CONTROL);
1457         SMC_SELECT_BANK(2);
1458     }
1459     
1460     /*====================================================================*/
1461         
1462     static void smc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1463     {
1464         struct smc_private *smc = dev_id;
1465         struct net_device *dev = &smc->dev;
1466         ioaddr_t ioaddr;
1467         u_short saved_bank, saved_pointer, mask, status;
1468         char bogus_cnt = INTR_WORK;		/* Work we are willing to do. */
1469     
1470         if (!netif_device_present(dev))
1471     	return;
1472         ioaddr = dev->base_addr;
1473         
1474         DEBUG(3, "%s: SMC91c92 interrupt %d at %#x.\n", dev->name,
1475     	  irq, ioaddr);
1476         
1477         smc->watchdog = 0;
1478         saved_bank = inw(ioaddr + BANK_SELECT);
1479         if ((saved_bank & 0xff00) != 0x3300) {
1480     	/* The device does not exist -- the card could be off-line, or
1481     	   maybe it has been ejected. */
1482     	DEBUG(1, "%s: SMC91c92 interrupt %d for non-existent"
1483     	      "/ejected device.\n", dev->name, irq);
1484     	goto irq_done;
1485         }
1486         
1487         SMC_SELECT_BANK(2);
1488         saved_pointer = inw(ioaddr + POINTER);
1489         mask = inw(ioaddr + INTERRUPT) >> 8;
1490         /* clear all interrupts */
1491         outw(0, ioaddr + INTERRUPT);
1492         
1493         do { /* read the status flag, and mask it */
1494     	status = inw(ioaddr + INTERRUPT) & 0xff;
1495     	DEBUG(3, "%s: Status is %#2.2x (mask %#2.2x).\n", dev->name,
1496     	      status, mask);
1497     	if ((status & mask) == 0)
1498     	    break;
1499     	
1500     	if (status & IM_RCV_INT) {
1501     	    /* Got a packet(s). */
1502     	    smc_rx(dev);
1503     	    smc->last_rx = jiffies;
1504     	}
1505     	if (status & IM_TX_INT) {
1506     	    smc_tx_err(dev);
1507     	    outw(IM_TX_INT, ioaddr + INTERRUPT);
1508     	}
1509     	status &= mask;
1510     	if (status & IM_TX_EMPTY_INT) {
1511     	    outw(IM_TX_EMPTY_INT, ioaddr + INTERRUPT);
1512     	    mask &= ~IM_TX_EMPTY_INT;
1513     	    smc->stats.tx_packets += smc->packets_waiting;
1514     	    smc->packets_waiting = 0;
1515     	}
1516     	if (status & IM_ALLOC_INT) {
1517     	    /* Clear this interrupt so it doesn't happen again */
1518     	    mask &= ~IM_ALLOC_INT;
1519     	    
1520     	    smc_hardware_send_packet(dev);
1521     	    
1522     	    /* enable xmit interrupts based on this */
1523     	    mask |= (IM_TX_EMPTY_INT | IM_TX_INT);
1524     	    
1525     	    /* and let the card send more packets to me */
1526     	    netif_wake_queue(dev);
1527     	}
1528     	if (status & IM_RX_OVRN_INT) {
1529     	    smc->stats.rx_errors++;
1530     	    smc->stats.rx_fifo_errors++;		
1531     	    outw(IM_RX_OVRN_INT, ioaddr + INTERRUPT);
1532     	}
1533     	if (status & IM_EPH_INT)
1534     	    smc_eph_irq(dev);
1535         } while (--bogus_cnt);
1536     
1537         DEBUG(3, "  Restoring saved registers mask %2.2x bank %4.4x"
1538     	  " pointer %4.4x.\n", mask, saved_bank, saved_pointer);
1539         
1540         /* restore state register */
1541         outw((mask<<8), ioaddr + INTERRUPT);
1542         outw(saved_pointer, ioaddr + POINTER);
1543         SMC_SELECT_BANK(saved_bank);
1544     
1545         DEBUG(3, "%s: Exiting interrupt IRQ%d.\n", dev->name, irq);
1546     
1547     irq_done:
1548         
1549         if ((smc->manfid == MANFID_OSITECH) &&
1550     	(smc->cardid != PRODID_OSITECH_SEVEN)) {
1551     	/* Retrigger interrupt if needed */
1552     	mask_bits(0x00ff, ioaddr-0x10+OSITECH_RESET_ISR);
1553     	set_bits(0x0300, ioaddr-0x10+OSITECH_RESET_ISR);
1554         }
1555         if (smc->manfid == MANFID_MOTOROLA) {
1556     	u_char cor;
1557     	cor = readb(smc->base + MOT_UART + CISREG_COR);
1558     	writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_UART + CISREG_COR);
1559     	writeb(cor, smc->base + MOT_UART + CISREG_COR);
1560     	cor = readb(smc->base + MOT_LAN + CISREG_COR);
1561     	writeb(cor & ~COR_IREQ_ENA, smc->base + MOT_LAN + CISREG_COR);
1562     	writeb(cor, smc->base + MOT_LAN + CISREG_COR);
1563         }
1564     #ifdef DOES_NOT_WORK
1565         if (smc->base != NULL) { /* Megahertz MFC's */
1566     	readb(smc->base+MEGAHERTZ_ISR);
1567     	readb(smc->base+MEGAHERTZ_ISR);
1568         }
1569     #endif
1570     }
1571     
1572     /*====================================================================*/
1573     
1574     static void smc_rx(struct net_device *dev)
1575     {
1576         struct smc_private *smc = (struct smc_private *)dev->priv;
1577         ioaddr_t ioaddr = dev->base_addr;
1578         int rx_status;
1579         int packet_length;	/* Caution: not frame length, rather words
1580     			   to transfer from the chip. */
1581         
1582         /* Assertion: we are in Window 2. */
1583         
1584         if (inw(ioaddr + FIFO_PORTS) & FP_RXEMPTY) {
1585     	printk(KERN_ERR "%s: smc_rx() with nothing on Rx FIFO.\n",
1586     	       dev->name);
1587     	return;
1588         }
1589         
1590         /*  Reset the read pointer, and read the status and packet length. */
1591         outw(PTR_READ | PTR_RCV | PTR_AUTOINC, ioaddr + POINTER);
1592         rx_status = inw(ioaddr + DATA_1);
1593         packet_length = inw(ioaddr + DATA_1) & 0x07ff;
1594     
1595         DEBUG(2, "%s: Receive status %4.4x length %d.\n",
1596     	  dev->name, rx_status, packet_length);
1597         
1598         if (!(rx_status & RS_ERRORS)) {		
1599     	/* do stuff to make a new packet */
1600     	struct sk_buff *skb;
1601     	
1602     	/* Note: packet_length adds 5 or 6 extra bytes here! */
1603     	skb = dev_alloc_skb(packet_length+2);
1604     	
1605     	if (skb == NULL) {
1606     	    DEBUG(1, "%s: Low memory, packet dropped.\n", dev->name);
1607     	    smc->stats.rx_dropped++;
1608     	    outw(MC_RELEASE, ioaddr + MMU_CMD);
1609     	    return;
1610     	}
1611     	
1612     	packet_length -= (rx_status & RS_ODDFRAME ? 5 : 6);
1613     	skb_reserve(skb, 2);
1614     	insw(ioaddr+DATA_1, skb_put(skb, packet_length),
1615     		(packet_length+1)>>1);
1616     	skb->protocol = eth_type_trans(skb, dev);
1617     	
1618     	skb->dev = dev;
1619     	netif_rx(skb);
1620     	dev->last_rx = jiffies;
1621     	smc->stats.rx_packets++;
1622     	smc->stats.rx_bytes += packet_length;
1623     	if (rx_status & RS_MULTICAST)
1624     	    smc->stats.multicast++;
1625         } else {
1626     	/* error ... */
1627     	smc->stats.rx_errors++;
1628     	
1629     	if (rx_status & RS_ALGNERR)  smc->stats.rx_frame_errors++;
1630     	if (rx_status & (RS_TOOSHORT | RS_TOOLONG))
1631     	    smc->stats.rx_length_errors++;
1632     	if (rx_status & RS_BADCRC)	smc->stats.rx_crc_errors++;
1633         }
1634         /* Let the MMU free the memory of this packet. */
1635         outw(MC_RELEASE, ioaddr + MMU_CMD);
1636         
1637         return;
1638     }
1639     
1640     /*====================================================================*/
1641     
1642     static struct net_device_stats *smc91c92_get_stats(struct net_device *dev)
1643     {
1644         struct smc_private *smc = (struct smc_private *)dev->priv;
1645         /* Nothing to update - the 91c92 is a pretty primative chip. */
1646         return &smc->stats;
1647     }
1648     
1649     /*======================================================================
1650     
1651         Compute the AUTODIN polynomial "CRC32" for ethernet packets.
1652     
1653     ======================================================================*/
1654     
1655     static const u_int ethernet_polynomial = 0x04c11db7U;
1656     
1657     static u_int ether_crc(int length, u_char *data)
1658     {
1659         int crc = 0xffffffff;	/* Initial value. */
1660         
1661         while (--length >= 0) {
1662     	u_char current_octet = *data++;
1663     	int bit;
1664     	for (bit = 0; bit < 8; bit++, current_octet >>= 1) {
1665     	    crc = (crc << 1) ^
1666     		((crc < 0) ^ (current_octet & 1) ? ethernet_polynomial : 0);
1667     	}
1668         }
1669         /* The hash index is the either the upper or lower bits of the CRC, so
1670          * we return the entire CRC.
1671          */
1672         return crc;
1673     }
1674     
1675     /*======================================================================
1676       
1677         Calculate values for the hardware multicast filter hash table.
1678         
1679     ======================================================================*/
1680     
1681     static void fill_multicast_tbl(int count, struct dev_mc_list *addrs,
1682     			       u_char *multicast_table)
1683     {
1684         struct dev_mc_list	*mc_addr;
1685         
1686         for (mc_addr = addrs;  mc_addr && --count > 0;  mc_addr = mc_addr->next) {
1687     	u_int position = ether_crc(6, mc_addr->dmi_addr);
1688     #ifndef final_version		/* Verify multicast address. */
1689     	if ((mc_addr->dmi_addr[0] & 1) == 0)
1690     	    continue;
1691     #endif
1692     	multicast_table[position >> 29] |= 1 << ((position >> 26) & 7);
1693         }
1694     }
1695     
1696     /*======================================================================
1697       
1698         Set the receive mode.
1699         
1700         This routine is used by both the protocol level to notify us of
1701         promiscuous/multicast mode changes, and by the open/reset code to
1702         initialize the Rx registers.  We always set the multicast list and
1703         leave the receiver running.
1704         
1705     ======================================================================*/
1706     
1707     static void set_rx_mode(struct net_device *dev)
1708     {
1709         ioaddr_t ioaddr = dev->base_addr;
1710         u_int multicast_table[ 2 ] = { 0, };
1711         long flags;
1712         u_short rx_cfg_setting;
1713         
1714         if (dev->flags & IFF_PROMISC) {
1715     	printk(KERN_NOTICE "%s: setting Rx mode to promiscuous.\n", dev->name);
1716     	rx_cfg_setting = RxStripCRC | RxEnable | RxPromisc | RxAllMulti;
1717         } else if (dev->flags & IFF_ALLMULTI)
1718     	rx_cfg_setting = RxStripCRC | RxEnable | RxAllMulti;
1719         else {
1720     	if (dev->mc_count)  {
1721     	    fill_multicast_tbl(dev->mc_count, dev->mc_list,
1722     			       (u_char *)multicast_table);
1723     	}
1724     	rx_cfg_setting = RxStripCRC | RxEnable;
1725         }
1726         
1727         /* Load MC table and Rx setting into the chip without interrupts. */
1728         save_flags(flags);
1729         cli();
1730         SMC_SELECT_BANK(3);
1731         outl(multicast_table[0], ioaddr + MULTICAST0);
1732         outl(multicast_table[1], ioaddr + MULTICAST4);
1733         SMC_SELECT_BANK(0);
1734         outw(rx_cfg_setting, ioaddr + RCR);
1735         SMC_SELECT_BANK(2);
1736         restore_flags(flags);
1737         
1738         return;
1739     }
1740     
1741     /*======================================================================
1742     
1743         Senses when a card's config changes. Here, it's coax or TP.
1744      
1745     ======================================================================*/
1746     
1747     static int s9k_config(struct net_device *dev, struct ifmap *map)
1748     {
1749         struct smc_private *smc = dev->priv;
1750         if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1751     	if (smc->cfg & CFG_MII_SELECT)
1752     	    return -EOPNOTSUPP;
1753     	else if (map->port > 2)
1754     	    return -EINVAL;
1755     	dev->if_port = map->port;
1756     	printk(KERN_INFO "%s: switched to %s port\n",
1757     	       dev->name, if_names[dev->if_port]);
1758     	smc_reset(dev);
1759         }
1760         return 0;
1761     }
1762     
1763     /*======================================================================
1764     
1765         Reset the chip, reloading every register that might be corrupted.
1766     
1767     ======================================================================*/
1768     
1769     /*
1770       Set transceiver type, perhaps to something other than what the user
1771       specified in dev->if_port.
1772     */
1773     static void smc_set_xcvr(struct net_device *dev, int if_port)
1774     {
1775         struct smc_private *smc = (struct smc_private *)dev->priv;
1776         ioaddr_t ioaddr = dev->base_addr;
1777         u_short saved_bank;
1778     
1779         saved_bank = inw(ioaddr + BANK_SELECT);
1780         SMC_SELECT_BANK(1);
1781         if (if_port == 2) {
1782     	outw(smc->cfg | CFG_AUI_SELECT, ioaddr + CONFIG);
1783     	if ((smc->manfid == MANFID_OSITECH) &&
1784     	    (smc->cardid != PRODID_OSITECH_SEVEN))
1785     	    set_bits(OSI_AUI_PWR, ioaddr - 0x10 + OSITECH_AUI_PWR);
1786     	smc->media_status = ((dev->if_port == 0) ? 0x0001 : 0x0002);
1787         } else {
1788     	outw(smc->cfg, ioaddr + CONFIG);
1789     	if ((smc->manfid == MANFID_OSITECH) &&
1790     	    (smc->cardid != PRODID_OSITECH_SEVEN))
1791     	    mask_bits(~OSI_AUI_PWR, ioaddr - 0x10 + OSITECH_AUI_PWR);
1792     	smc->media_status = ((dev->if_port == 0) ? 0x0012 : 0x4001);
1793         }
1794         SMC_SELECT_BANK(saved_bank);
1795     }
1796     
1797     static void smc_reset(struct net_device *dev)
1798     {
1799         ioaddr_t ioaddr = dev->base_addr;
1800         struct smc_private *smc = dev->priv;
1801         int i;
1802     
1803         DEBUG(0, "%s: smc91c92 reset called.\n", dev->name);
1804         
1805         /* The first interaction must be a write to bring the chip out
1806            of sleep mode. */
1807         SMC_SELECT_BANK(0);
1808         /* Reset the chip. */
1809         outw(RCR_SOFTRESET, ioaddr + RCR);
1810         udelay(10);
1811         
1812         /* Clear the transmit and receive configuration registers. */
1813         outw(RCR_CLEAR, ioaddr + RCR);
1814         outw(TCR_CLEAR, ioaddr + TCR);
1815         
1816         /* Set the Window 1 control, configuration and station addr registers.
1817            No point in writing the I/O base register ;-> */
1818         SMC_SELECT_BANK(1);
1819         /* Automatically release succesfully transmitted packets,
1820            Accept link errors, counter and Tx error interrupts. */
1821         outw(CTL_AUTO_RELEASE | CTL_TE_ENABLE | CTL_CR_ENABLE,
1822     	 ioaddr + CONTROL);
1823         smc_set_xcvr(dev, dev->if_port);
1824         if ((smc->manfid == MANFID_OSITECH) &&
1825     	(smc->cardid != PRODID_OSITECH_SEVEN))
1826     	outw((dev->if_port == 2 ? OSI_AUI_PWR : 0) |
1827     	     (inw(ioaddr-0x10+OSITECH_AUI_PWR) & 0xff00),
1828     	     ioaddr - 0x10 + OSITECH_AUI_PWR);
1829         
1830         /* Fill in the physical address.  The databook is wrong about the order! */
1831         for (i = 0; i < 6; i += 2)
1832     	outw((dev->dev_addr[i+1]<<8)+dev->dev_addr[i],
1833     	     ioaddr + ADDR0 + i);
1834         
1835         /* Reset the MMU */
1836         SMC_SELECT_BANK(2);
1837         outw(MC_RESET, ioaddr + MMU_CMD);
1838         outw(0, ioaddr + INTERRUPT);
1839         
1840         /* Re-enable the chip. */
1841         SMC_SELECT_BANK(0);
1842         outw(((smc->cfg & CFG_MII_SELECT) ? 0 : TCR_MONCSN) |
1843     	 TCR_ENABLE | TCR_PAD_EN, ioaddr + TCR);
1844         set_rx_mode(dev);
1845     
1846         /* Enable interrupts. */
1847         SMC_SELECT_BANK(2);
1848         outw((IM_EPH_INT | IM_RX_OVRN_INT | IM_RCV_INT) << 8,
1849     	 ioaddr + INTERRUPT);
1850     }
1851     
1852     /*======================================================================
1853     
1854         Media selection timer routine
1855         
1856     ======================================================================*/
1857     
1858     static void media_check(u_long arg)
1859     {
1860         struct smc_private *smc = (struct smc_private *)(arg);
1861         struct net_device *dev = &smc->dev;
1862         ioaddr_t ioaddr = dev->base_addr;
1863         u_short i, media, saved_bank;
1864     
1865         if (!netif_device_present(dev))
1866     	goto reschedule;
1867     
1868         saved_bank = inw(ioaddr + BANK_SELECT);
1869         SMC_SELECT_BANK(2);
1870         i = inw(ioaddr + INTERRUPT);
1871         SMC_SELECT_BANK(0);
1872         media = inw(ioaddr + EPH) & EPH_LINK_OK;
1873         SMC_SELECT_BANK(1);
1874         media |= (inw(ioaddr + CONFIG) & CFG_AUI_SELECT) ? 2 : 1;
1875         SMC_SELECT_BANK(saved_bank);
1876         
1877         /* Check for pending interrupt with watchdog flag set: with
1878            this, we can limp along even if the interrupt is blocked */
1879         if (smc->watchdog++ && ((i>>8) & i)) {
1880     	if (!smc->fast_poll)
1881     	    printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
1882     	smc_interrupt(dev->irq, smc, NULL);
1883     	smc->fast_poll = HZ;
1884         }
1885         if (smc->fast_poll) {
1886     	smc->fast_poll--;
1887     	smc->media.expires = jiffies + 1;
1888     	add_timer(&smc->media);
1889     	return;
1890         }
1891     
1892         if (smc->cfg & CFG_MII_SELECT)
1893     	goto reschedule;
1894     
1895         /* Ignore collisions unless we've had no rx's recently */
1896         if (jiffies - smc->last_rx > HZ) {
1897     	if (smc->tx_err || (smc->media_status & EPH_16COL))
1898     	    media |= EPH_16COL;
1899         }
1900         smc->tx_err = 0;
1901     
1902         if (media != smc->media_status) {
1903     	if ((media & smc->media_status & 1) &&
1904     	    ((smc->media_status ^ media) & EPH_LINK_OK))
1905     	    printk(KERN_INFO "%s: %s link beat\n", dev->name,
1906     		   (smc->media_status & EPH_LINK_OK ? "lost" : "found"));
1907     	else if ((media & smc->media_status & 2) &&
1908     		 ((smc->media_status ^ media) & EPH_16COL))
1909     	    printk(KERN_INFO "%s: coax cable %s\n", dev->name,
1910     		   (media & EPH_16COL ? "problem" : "ok"));
1911     	if (dev->if_port == 0) {
1912     	    if (media & 1) {
1913     		if (media & EPH_LINK_OK)
1914     		    printk(KERN_INFO "%s: flipped to 10baseT\n",
1915     			   dev->name);
1916     		else
1917     		    smc_set_xcvr(dev, 2);
1918     	    } else {
1919     		if (media & EPH_16COL)
1920     		    smc_set_xcvr(dev, 1);
1921     		else
1922     		    printk(KERN_INFO "%s: flipped to 10base2\n",
1923     			   dev->name);
1924     	    }
1925     	}
1926     	smc->media_status = media;
1927         }
1928         
1929     reschedule:
1930         smc->media.expires = jiffies + HZ;
1931         add_timer(&smc->media);
1932     }
1933     
1934     /*====================================================================*/
1935     
1936     static int __init init_smc91c92_cs(void)
1937     {
1938         servinfo_t serv;
1939         DEBUG(0, "%s\n", version);
1940         CardServices(GetCardServicesInfo, &serv);
1941         if (serv.Revision != CS_RELEASE_CODE) {
1942     	printk(KERN_ERR
1943     	       "smc91c92_cs: Card Services release does not match!\n");
1944     	return -1;
1945         }
1946         register_pccard_driver(&dev_info, &smc91c92_attach, &smc91c92_detach);
1947         return 0;
1948     }
1949     
1950     static void __exit exit_smc91c92_cs(void)
1951     {
1952         DEBUG(0, "smc91c92_cs: unloading\n");
1953         unregister_pccard_driver(&dev_info);
1954         while (dev_list != NULL)
1955     	smc91c92_detach(dev_list);
1956     }
1957     
1958     module_init(init_smc91c92_cs);
1959     module_exit(exit_smc91c92_cs);
1960