2 * atari_nfeth.c - ARAnyM ethernet card driver for GNU/Linux
4 * Copyright (c) 2005 Milan Jurik, Petr Stehlik of ARAnyM dev team
6 * Based on ARAnyM driver for FreeMiNT written by Standa Opichal
8 * This software may be used and distributed according to the terms of
9 * the GNU General Public License (GPL), incorporated herein by reference.
12 #define DRV_VERSION "0.3"
13 #define DRV_RELDATE "10/12/2005"
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/netdevice.h>
18 #include <linux/etherdevice.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <asm/natfeat.h>
22 #include <asm/virtconvert.h>
25 GET_VERSION
= 0,/* no parameters, return NFAPI_VERSION in d0 */
26 XIF_INTLEVEL
, /* no parameters, return Interrupt Level in d0 */
27 XIF_IRQ
, /* acknowledge interrupt from host */
28 XIF_START
, /* (ethX), called on 'ifup', start receiver thread */
29 XIF_STOP
, /* (ethX), called on 'ifdown', stop the thread */
30 XIF_READLENGTH
, /* (ethX), return size of network data block to read */
31 XIF_READBLOCK
, /* (ethX, buffer, size), read block of network data */
32 XIF_WRITEBLOCK
, /* (ethX, buffer, size), write block of network data */
33 XIF_GET_MAC
, /* (ethX, buffer, size), return MAC HW addr in buffer */
34 XIF_GET_IPHOST
, /* (ethX, buffer, size), return IP address of host */
35 XIF_GET_IPATARI
,/* (ethX, buffer, size), return IP address of atari */
36 XIF_GET_NETMASK
/* (ethX, buffer, size), return IP netmask */
41 /* These identify the driver base version and may not be removed. */
42 static const char version
[] =
43 KERN_INFO KBUILD_MODNAME
".c:v" DRV_VERSION
" " DRV_RELDATE
44 " S.Opichal, M.Jurik, P.Stehlik\n"
45 KERN_INFO
" http://aranym.org/\n";
47 MODULE_AUTHOR("Milan Jurik");
48 MODULE_DESCRIPTION("Atari NFeth driver");
49 MODULE_LICENSE("GPL");
52 static long nfEtherID
;
53 static int nfEtherIRQ
;
55 struct nfeth_private
{
59 static struct net_device
*nfeth_dev
[MAX_UNIT
];
61 static int nfeth_open(struct net_device
*dev
)
63 struct nfeth_private
*priv
= netdev_priv(dev
);
66 res
= nf_call(nfEtherID
+ XIF_START
, priv
->ethX
);
67 netdev_dbg(dev
, "%s: %d\n", __func__
, res
);
70 netif_start_queue(dev
);
75 static int nfeth_stop(struct net_device
*dev
)
77 struct nfeth_private
*priv
= netdev_priv(dev
);
80 netif_stop_queue(dev
);
82 nf_call(nfEtherID
+ XIF_STOP
, priv
->ethX
);
88 * Read a packet out of the adapter and pass it to the upper layers
90 static inline void recv_packet(struct net_device
*dev
)
92 struct nfeth_private
*priv
= netdev_priv(dev
);
93 unsigned short pktlen
;
96 /* read packet length (excluding 32 bit crc) */
97 pktlen
= nf_call(nfEtherID
+ XIF_READLENGTH
, priv
->ethX
);
99 netdev_dbg(dev
, "%s: %u\n", __func__
, pktlen
);
102 netdev_dbg(dev
, "%s: pktlen == 0\n", __func__
);
103 dev
->stats
.rx_errors
++;
107 skb
= dev_alloc_skb(pktlen
+ 2);
109 netdev_dbg(dev
, "%s: out of mem (buf_alloc failed)\n",
111 dev
->stats
.rx_dropped
++;
116 skb_reserve(skb
, 2); /* 16 Byte align */
117 skb_put(skb
, pktlen
); /* make room */
118 nf_call(nfEtherID
+ XIF_READBLOCK
, priv
->ethX
, virt_to_phys(skb
->data
),
121 skb
->protocol
= eth_type_trans(skb
, dev
);
123 dev
->stats
.rx_packets
++;
124 dev
->stats
.rx_bytes
+= pktlen
;
126 /* and enqueue packet */
130 static irqreturn_t
nfeth_interrupt(int irq
, void *dev_id
)
134 mask
= nf_call(nfEtherID
+ XIF_IRQ
, 0);
135 for (i
= 0, m
= 1; i
< MAX_UNIT
; m
<<= 1, i
++) {
136 if (mask
& m
&& nfeth_dev
[i
]) {
137 recv_packet(nfeth_dev
[i
]);
138 nf_call(nfEtherID
+ XIF_IRQ
, m
);
144 static int nfeth_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
147 char *data
, shortpkt
[ETH_ZLEN
];
148 struct nfeth_private
*priv
= netdev_priv(dev
);
152 if (len
< ETH_ZLEN
) {
153 memset(shortpkt
, 0, ETH_ZLEN
);
154 memcpy(shortpkt
, data
, len
);
159 netdev_dbg(dev
, "%s: send %u bytes\n", __func__
, len
);
160 nf_call(nfEtherID
+ XIF_WRITEBLOCK
, priv
->ethX
, virt_to_phys(data
),
163 dev
->stats
.tx_packets
++;
164 dev
->stats
.tx_bytes
+= len
;
170 static void nfeth_tx_timeout(struct net_device
*dev
, unsigned int txqueue
)
172 dev
->stats
.tx_errors
++;
173 netif_wake_queue(dev
);
176 static const struct net_device_ops nfeth_netdev_ops
= {
177 .ndo_open
= nfeth_open
,
178 .ndo_stop
= nfeth_stop
,
179 .ndo_start_xmit
= nfeth_xmit
,
180 .ndo_tx_timeout
= nfeth_tx_timeout
,
181 .ndo_validate_addr
= eth_validate_addr
,
182 .ndo_set_mac_address
= eth_mac_addr
,
185 static struct net_device
* __init
nfeth_probe(int unit
)
187 struct net_device
*dev
;
188 struct nfeth_private
*priv
;
189 char mac
[ETH_ALEN
], host_ip
[32], local_ip
[32];
192 if (!nf_call(nfEtherID
+ XIF_GET_MAC
, unit
, virt_to_phys(mac
),
196 dev
= alloc_etherdev(sizeof(struct nfeth_private
));
200 dev
->irq
= nfEtherIRQ
;
201 dev
->netdev_ops
= &nfeth_netdev_ops
;
203 memcpy(dev
->dev_addr
, mac
, ETH_ALEN
);
205 priv
= netdev_priv(dev
);
208 err
= register_netdev(dev
);
214 nf_call(nfEtherID
+ XIF_GET_IPHOST
, unit
,
215 virt_to_phys(host_ip
), sizeof(host_ip
));
216 nf_call(nfEtherID
+ XIF_GET_IPATARI
, unit
,
217 virt_to_phys(local_ip
), sizeof(local_ip
));
219 netdev_info(dev
, KBUILD_MODNAME
" addr:%s (%s) HWaddr:%pM\n", host_ip
,
225 static int __init
nfeth_init(void)
230 nfEtherID
= nf_get_id("ETHERNET");
234 ver
= nf_call(nfEtherID
+ GET_VERSION
);
235 pr_info("API %lu\n", ver
);
237 nfEtherIRQ
= nf_call(nfEtherID
+ XIF_INTLEVEL
);
238 error
= request_irq(nfEtherIRQ
, nfeth_interrupt
, IRQF_SHARED
,
239 "eth emu", nfeth_interrupt
);
241 pr_err("request for irq %d failed %d", nfEtherIRQ
, error
);
245 for (i
= 0; i
< MAX_UNIT
; i
++)
246 nfeth_dev
[i
] = nfeth_probe(i
);
251 static void __exit
nfeth_cleanup(void)
255 for (i
= 0; i
< MAX_UNIT
; i
++) {
257 unregister_netdev(nfeth_dev
[0]);
258 free_netdev(nfeth_dev
[0]);
261 free_irq(nfEtherIRQ
, nfeth_interrupt
);
264 module_init(nfeth_init
);
265 module_exit(nfeth_cleanup
);