102 lines
2 KiB
C
102 lines
2 KiB
C
#include <linux/kernel.h>
|
|
#include <linux/module.h>
|
|
#include <linux/stddef.h>
|
|
#include <linux/netdevice.h>
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
struct net_device *dev;
|
|
|
|
static int my_eth_init(struct net_device *dev)
|
|
{
|
|
// Initialize device
|
|
printk(KERN_INFO "My Ethernet device initialized\n");
|
|
netif_start_queue(dev);
|
|
return 0;
|
|
|
|
}
|
|
|
|
static void my_eth_exit(struct net_device *dev)
|
|
{
|
|
// Cleanup device
|
|
pr_info(KERN_INFO "My Ethernet device exited\n");
|
|
netif_stop_queue(dev);
|
|
}
|
|
|
|
static netdev_tx_t my_eth_start_xmit(struct sk_buff *skb,
|
|
struct net_device *dev)
|
|
{
|
|
// Start xmit
|
|
printk(KERN_INFO "My Ethernet device start xmit\n");
|
|
printk("len: %d\n", skb->len);
|
|
for (int i = 0; i < skb->len; i++) {
|
|
printk("data: %c\n", skb->data[i]);
|
|
}
|
|
printk("protocol: %d\n", skb->protocol);
|
|
|
|
netif_receive_skb(skb);
|
|
|
|
return NETDEV_TX_OK;
|
|
}
|
|
|
|
static struct net_device_ops my_netdev_ops = {
|
|
.ndo_init = my_eth_init,
|
|
.ndo_uninit = my_eth_exit,
|
|
.ndo_start_xmit = my_eth_start_xmit
|
|
};
|
|
|
|
void init_testcard(struct net_device *dev)
|
|
{
|
|
dev->netdev_ops = &my_netdev_ops;
|
|
printk("Init testcard\n");
|
|
}
|
|
|
|
struct net_device testcard;
|
|
|
|
int register_device(void) {
|
|
dev = alloc_netdev(0, "myeth%d", NET_NAME_UNKNOWN, init_testcard);
|
|
|
|
if (!dev)
|
|
return -ENOMEM;
|
|
|
|
printk("Registering\n");
|
|
|
|
if (register_netdev(dev)) {
|
|
printk("Failed :(\n");
|
|
free_netdev(dev);
|
|
return -ENODEV;
|
|
}
|
|
|
|
printk("Registered\n");
|
|
|
|
return 0;
|
|
}
|
|
|
|
int __init custom_init(void)
|
|
{
|
|
int err;
|
|
if ((err = register_device())) {
|
|
printk("Error loading postage\n");
|
|
return err;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void __exit custom_exit(void)
|
|
{
|
|
if (!dev) {
|
|
printk(KERN_ERR "Ethernet device not found\n");
|
|
return;
|
|
}
|
|
|
|
unregister_netdev(dev);
|
|
printk(KERN_INFO "Ethernet device unregistered\n");
|
|
|
|
free_netdev(dev);
|
|
printk(KERN_INFO "Ethernet device freed\n");
|
|
}
|
|
|
|
module_init(custom_init);
|
|
module_exit(custom_exit);
|