Linux Network Device Driver Programming

Source: Internet
Author: User
Tags call back
Article Title: Linux Network Device Driver Programming. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

This article only discusses the general Writing of network device drivers. The Code related to the hardware is omitted because of the different hardware specifications. Are there any mistakes or supplements? You are welcome to raise them.

1. Loading and uninstalling the driver module

If the network device (including wireless) is PCI compliant, first register the PCI device (pci_register_driver) with the kernel ), then, the probe function pointer in the pci_driver data structure points to the detection function to initialize the PCI device and register and initialize the network device at the same time.

If the network device (including wireless) is in the PCMCIA specification, first register the PCMCIA device (register_pccard_driver) with the kernel ), then, the attach function pointer in the driver_info_t data structure points to the detection function to initialize the PCMCIA device and register and initialize the network device at the same time.

Static int _ init tg3_init (void)

{

// Register as a PCI device and initialize it. If it is another ESIA or PCMCIA, use other functions.

Return pci_module_init (& tg3_driver );

}

Static void _ exit tg3_cleanup (void)

{

Pci_unregister_driver (& tg3_driver); // cancel the PCI device

}

Module_init (tg3_init); // load the driver module

Module_exit (tg3_cleanup); // uninstall the driver module

Stated as a PCI device:

Static struct pci_driver tg3_driver = {

. Name = DRV_MODULE_NAME,

. Id_table = tg3_pci_tbl, // The NIC Series Supported by this driver, vendor_id, device_id

. Probe = tg3_init_one, // initialize the network device callback function

. Remove = _ devexit_p (tg3_remove_one), // call back the network device

. Suspend = tg3_suspend, // device suspension Function

. Resume = tg3_resume // device recovery function

};

2. The probe function of the PCI device is used to initialize the network device.

Static int _ devinit tg3_init_one (struct pci_dev * pdev, const struct pci_device_id * ent)

{

// Initialize the device to make I/O and memory available and wake up the device

Pci_enable_device (pdev );

// Apply for memory space and configure Nic I/O and memory resources

Pci_request_regions (pdev, DRV_MODULE_NAME );

Pci_set_master (pdev );

// Set the DMA attribute

Pci_set_dma_mask (pdev, (u64) 0 xffffffffffffffff );

// Nic I/O, starting address of memory resource

Tg3reg_base = pci_resource_start (pdev, 0 );

// Nic I/O, memory resource size

Tg3reg_len = pci_resource_len (pdev, 0 );

// Allocate and SET network devices

Dev = alloc_etherdev (sizeof (* tp ));

// Declare as the kernel device module

SET_MODULE_OWNER (dev );

// Initialize the Member values in the private structure

Tp = dev-> priv;

Tp-> pdev = pdev;

Tp-> dev = dev;

......

// Lock Initialization

Spin_lock_init (& tp-> lock );

// Map I/O, memory address to register structure in private domain

Tp-> regs = (unsigned long) ioremap (tg3reg_base, tg3reg_len );

Dev-> irq = pdev-> irq;

// Network device callback function value assignment

Dev-> open = tg3_open;

Dev-> stop = tg3_close;

Dev-> get_stats = tg3_get_stats;

Dev-> set_multicast_list = tg3_set_rx_mode;

Dev-> set_mac_address = tg3_set_mac_addr;

Dev-> do_ioctl = tg3_ioctl;

Dev-> tx_timeout = tg3_tx_timeout;

Dev-> hard_start_xmit = tg3_start_xmit;

// Assign the MAC address of the NIC to dev-> addr.

Tg3_get_device_address (tp );

// Register a network device

Register_netdev (dev );

// Put the network device pointer address in the device pointer of the PCI device

Pci_set_drvdata (pdev, dev );

}

[1] [2] [3] [4] Next page

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.