smc91c92_cs.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:66k
源码类别:

Linux/Unix编程

开发平台:

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