[Stick] Linux Device Programming

Source: Internet
Author: User

Device mounted to bus

# Include <Linux/module. h>

# Include <Linux/init. h>

# Include <Linux/device. h>

 

Void usb_dev_release (struct device * Dev );

 

Struct bus_type my_usb_bus = {

. Name = "my_usb", // The Name Of The bus. After successful registration, it will be displayed in the/sys/bus/directory.

};

 

Struct device my_usb_device = {

. Bus_id = "usb_device", // device name

. Bus = & my_usb_bus, // this device will be mounted on my_usb_bus

. Release = usb_dev_release,

};

 

# Define count_size 100

 

Char data [count_size] = "Hello mikechen ";

Char data_device [count_size] = "Hello mikechen This Is My device ";

 

Void usb_dev_release (struct device * Dev)

{

Printk ("<kernel> release \ n ");

}

 

/*

Data from kernel to user space

*/

Static ssize_t show_device_data (struct device * bus, struct device_attribute * ATTR, char * BUF)

{

Return snprintf (BUF, count_size, "show bus data: % s \ n", data_device );

}

 

/*

Data from user space to Kernel

*/

Static ssize_t store_device_data (struct device * Dev, struct device_attribute * ATTR, const char * Buf, size_t count)

{

Return snprintf (data_device, count_size, "store bus data: % s \ n", Buf );

}

 

/*

Data from kernel to user space

*/

Static ssize_t show_bus_data (struct bus_type * bus, char * BUF)

{

Return snprintf (BUF, count_size, "show bus data: % s \ n", data );

}

 

/*

Data from user space to Kernel

*/

Static ssize_t store_bus_data (struct bus_type * bus, const char * Buf, size_t count)

{

Return snprintf (data, count_size, "store bus data: % s \ n", Buf );

}

 

/*

Note that this macro returns bus_attr_version.

# Define bus_attr (_ name, _ mode, _ show, _ store )\

Struct bus_attribute bus_attr _ # _ name = _ ATTR (_ name, _ mode, _ show, _ store)

*/

Static bus_attr (version, s_irugo | s_iwugo, show_bus_data, store_bus_data );

 

Static device_attr (verison_device, s_irugo | s_iwugo, show_device_data, store_device_data );

 

Static int _ init my_usb_bus_init (void)

{

Int ret;

 

Ret = bus_register (& my_usb_bus );

If (RET)

{

Printk ("Bus register failed !, RET: % d \ n ", RET );

Return ret;

}

Printk ("USB init comatrix \ n ");

 

Ret = bus_create_file (& my_usb_bus, & bus_attr_version );

If (RET)

{

Printk ("Bus Create File failed ");

Bus_unregister (& my_usb_bus );

Return 0;

}

Printk ("Bus ATTR create commp Lete \ n ");

 

Ret = device_register (& my_usb_device );

If (RET)

{

Printk ("device register failed \ n ");

Bus_remove_file (& my_usb_bus, & bus_attr_version)

Bus_unregister (& my_usb_bus );

Return 0;

}

Printk ("device register commp Lete \ n ");

 

Ret = device_create_file (& my_usb_device, & dev_attr_verison_device );

If (RET)

{

Printk ("device create failed \ n ");

Device_unregister (& my_usb_device );

Bus_remove_file (& my_usb_bus, & bus_attr_version)

Bus_unregister (& my_usb_bus );

Return 0;

}

Printk ("device create commp Lete \ n ");

Return 0;

}

 

Static void _ exit my_usb_bus_exit (void)

{

Device_create_file (& my_usb_bus, & dev_attr_verison_device );

Device_unregister (& my_usb_device );

Bus_remove_file (& my_usb_bus, & bus_attr_version)

Bus_unregister (& my_usb_bus );

Printk ("USB Bus exit \ n ");

}

 

Module_license ("GPL ");

Module_author ("Mike Chen ");

Module_init (my_usb_bus_init );

Module_exit (my_usb_bus_exit );

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.