smc91c92_cs.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:62k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

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