Added very basic ethernet device.
[cascardo/kernel/samples/netdev/.git] / ndeth.c
1 /*
2  *  Copyright (C) 2010  Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/etherdevice.h>
23
24 MODULE_LICENSE("GPL");
25
26 static int ndeth_open(struct net_device *dev)
27 {
28         printk(KERN_DEBUG "ndeth open\n");
29         return 0;
30 }
31
32 static int ndeth_stop(struct net_device *dev)
33 {
34         printk(KERN_DEBUG "ndeth stop\n");
35         return 0;
36 }
37
38 static int ndeth_tx(struct sk_buff *skb, struct net_device *dev)
39 {
40         dev_kfree_skb(skb);
41         return NETDEV_TX_OK;
42 }
43
44 static const struct net_device_ops ndeth_ops = {
45         .ndo_open = ndeth_open,
46         .ndo_stop = ndeth_stop,
47         .ndo_start_xmit = ndeth_tx,
48 };
49
50 struct net_device *ndeth;
51
52 static __init int ndeth_init(void)
53 {
54         int r = -ENOMEM;
55         ndeth = alloc_etherdev(0);
56         if (!ndeth)
57                 goto out;
58         ndeth->netdev_ops = &ndeth_ops;
59         r = register_netdev(ndeth);
60         if (r)
61                 goto reg_out;
62         return 0;
63 reg_out:
64         free_netdev(ndeth);
65 out:
66         return r;
67 }
68
69 static __exit void ndeth_exit(void)
70 {
71         unregister_netdev(ndeth);
72         free_netdev(ndeth);
73 }
74
75 module_init(ndeth_init);
76 module_exit(ndeth_exit);