forked from borg42/w5x00
-
Notifications
You must be signed in to change notification settings - Fork 1
/
netdrv.c
276 lines (224 loc) · 5.78 KB
/
netdrv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/****************************************************************************
*
* driver/netdrv.c
*/
/*********************************************
* network interface driver support
*********************************************/
#include <linux/spi/spi.h>
#include "w5x00.h"
#include "queue.h"
extern struct spi_device *spi_device;
extern struct workqueue_struct *w5x00_wq;
//#define DEBUG_PKT
#if 0
#define DPRINTK(format,args...) printk(format,##args)
#else
#define DPRINTK(format,args...)
#endif
/*
* Open/initialize the board. This is called (in the current kernel)
* sometime after booting when the 'ifconfig' program is run.
*
* This routine should set everything up anew at each open, even
* registers that "should" only need to be set once at boot, so that
* there is non-reboot way to recover if something goes wrong.
*/
static int
wiznet_open(struct net_device *dev)
{
struct wiz_private *wp = netdev_priv(dev);
int r;
/* create socket */
r = iinchip_netif_create(dev);
if (r < 0) {
printk("%s: can't create socket\n", dev->name);
return -EFAULT;
}
/* open socket */
r = iinchip_open(wp->s, WSOCK_MACL_RAWM, 0);
if (r < 0) {
printk("%s: can't open socket\n", dev->name);
return -EFAULT;
}
/*
* start driver.
*/
netif_start_queue(dev);
return 0;
}
/*
* The inverse routine to net_open().
*/
static int
wiznet_close(struct net_device *dev)
{
struct wiz_private *wp = netdev_priv(dev);
/*
* stop driver.
*/
netif_stop_queue(dev);
// close socket
iinchip_close(wp->s);
// delete interface
iinchip_netif_delete(dev);
return 0;
}
/* This will only be invoked if the driver is _not_ in XOFF state.
* What this means is that we need not check it, and that this
* invariant will hold if we make sure that the netif_*_queue()
* calls are done at the proper times.
*/
static int
wiznet_start_xmit(struct sk_buff *skb, struct net_device *dev)
{
struct wiz_private *wp = netdev_priv(dev);
SKB_NODE *p;
p = alloc_skb_node();
if(p == NULL) {
printk("skb alloc failed\n");
return 0;
}
p->skb = skb;
push_skb(p);
queue_work(w5x00_wq, &(wp->wiz->tx_work));
return 0;
}
/*
* Get the current statistics.
* This may be called with the card open or closed.
*/
static struct net_device_stats *
wiznet_get_stats(struct net_device *dev)
{
struct wiz_private *wp = netdev_priv(dev);
return (&wp->stats);
}
/*
* set MAC address of the interface. called from the core after a
* SIOCSIFADDR ioctl, and from the bootup above.
*/
static int
wiznet_set_address(struct net_device *dev, void *p)
{
struct sockaddr *addr = p;
/* update */
iinchip_socket_sethwaddr(addr->sa_data);
/* remember it */
memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
return 0;
}
/*
* Set or clear the multicast filter for this adaptor.
* num_addrs == -1 Promiscuous mode, receive all packets
* num_addrs == 0 Normal mode, clear multicast list
* num_addrs > 0 Multicast mode, receive normal and MC packets,
* and do best-effort filtering.
*/
static void
wiznet_set_multicast(struct net_device *dev)
{
if (dev->flags & IFF_PROMISC) {
/* promiscuous mode */
} else if (dev->flags & IFF_ALLMULTI) {
/* MC mode, receive normal and MC packets */
} else {
/* Normal, clear the mc list */
}
}
static void
wiznet_tx_timeout(struct net_device *dev)
{
struct wiz_private *wp = netdev_priv(dev);
wp->stats.tx_errors++;
}
/* ioctl service */
static int
wiznet_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
{
struct s_wiznet_ioctl req;
int err = 0;
if (!capable (CAP_NET_ADMIN))
return -EPERM;
if (dev == NULL)
return -ENODEV;
if (cmd != WIZNETIOCTL)
return -ENOTTY;
err = copy_from_user(&req, ifr->ifr_data, sizeof(struct s_wiznet_ioctl));
if (err)
return -EFAULT;
switch (req.cmd) {
case WZIOC_SETOID:
DPRINTK("ioctl: Set REG 0x%x: data %p, len %d\n", req.oid, req.data, req.len);
if (!access_ok(VERIFY_READ, req.data, req.len))
return -EFAULT;
iinchip_copy_to(req.oid, req.data, req.len);
if (copy_to_user(ifr->ifr_data, &req, sizeof(struct s_wiznet_ioctl)))
err = -EFAULT;
break;
case WZIOC_GETOID:
DPRINTK("ioctl: Get REG 0x%x, len %d\n", req.oid, req.len);
if (!access_ok(VERIFY_WRITE, req.data, req.len))
return -EFAULT;
iinchip_copy_from(req.oid, req.data, req.len);
if (copy_to_user(ifr->ifr_data, &req, sizeof(struct s_wiznet_ioctl)))
err = -EFAULT;
break;
default:
err = -EOPNOTSUPP;
}
return err;
}
/* create network device driver */
static const struct net_device_ops netdev_ops = {
.ndo_open = wiznet_open,
.ndo_stop = wiznet_close,
.ndo_get_stats = wiznet_get_stats,
.ndo_start_xmit = wiznet_start_xmit,
.ndo_tx_timeout = wiznet_tx_timeout,
.ndo_set_mac_address = wiznet_set_address,
.ndo_do_ioctl = wiznet_ioctl,
.ndo_set_rx_mode = wiznet_set_multicast,
.ndo_change_mtu = eth_change_mtu,
.ndo_validate_addr = eth_validate_addr,
};
struct net_device *
wiznet_drv_create(wiz_t *wz)
{
struct net_device *dev;
struct wiz_private *wp;
/* create ethernet device */
dev = alloc_etherdev(sizeof(struct wiz_private));
if (dev == NULL) {
printk("%s: can't create device\n", DRV_NAME);
return NULL;
}
/* initialize private device structure */
/* initialize network device structure */
wp = netdev_priv(dev);
dev_set_drvdata(&spi_device->dev, wp);
SET_NETDEV_DEV(dev, &spi_device->dev);
wp->s = 0;
wp->wiz = wz;
memcpy(dev->dev_addr, wz->macaddr, 6);
dev->base_addr = wz->base;
dev->irq = wz->irq;
dev->netdev_ops = &netdev_ops;
dev->watchdog_timeo = 2 * HZ;
#ifndef CONFIG_RPI_EXT
/* override Name */
strcpy(dev->name, "wiz%d");
#endif
/* register driver */
if (register_netdev(dev)) {
printk("%s: register_netdev() failed\n", DRV_NAME);
kfree(dev);
return NULL;
}
return dev;
}
void wiznet_drv_delete(struct net_device *dev)
{
/* unregister driver */
unregister_netdev(dev);
}