标签:ini additions ice 功能 abs 结果 done map bit
以module的方式注册设备,并在驱动中调用设备的参数
资料来源于迅为视频学习。
前面一节的设备注册使用的是CONFIG_HELLO_CTL的形式,通过结构体platform_device的调用直接配置,实现注册设备的功能。
本节通过直接调用的注册设备的函数来是其功能。
主要函数:
platform_device_register :drivers/base/platform.c
1 流程: 2 文件drivers/base/platform.c定义了设备的register函数,然后通过platform_device_add. 3 所以我们只需要会使用platform_device_register即可。 4 /** 5 * platform_device_register - add a platform-level device 6 * @pdev: platform device we‘re adding 7 */ 8 int platform_device_register(struct platform_device *pdev) 9 { 10 device_initialize(&pdev->dev); 11 return platform_device_add(pdev); 12 } 13 EXPORT_SYMBOL_GPL(platform_device_register);
platform_device_unregister:同上
1 更详细的以后参考内核代码分析: 2 /** 3 * platform_device_unregister - unregister a platform-level device 4 * @pdev: platform device we‘re unregistering 5 * 6 * Unregistration is done in 2 steps. First we release all resources 7 * and remove it from the subsystem, then we drop reference count by 8 * calling platform_device_put(). 9 */ 10 void platform_device_unregister(struct platform_device *pdev) 11 { 12 platform_device_del(pdev); 13 platform_device_put(pdev); 14 } 15 EXPORT_SYMBOL_GPL(platform_device_unregister);
还有一个结构体:platform_device,作为上面两个函数的使用参数,结构体类型具体如下:
1 /** 2 * struct device - The basic device structure 3 * @parent: The device‘s "parent" device, the device to which it is attached. 4 * In most cases, a parent device is some sort of bus or host 5 * controller. If parent is NULL, the device, is a top-level device, 6 * which is not usually what you want. 7 * @p: Holds the private data of the driver core portions of the device. 8 * See the comment of the struct device_private for detail. 9 * @kobj: A top-level, abstract class from which other classes are derived. 10 * @init_name: Initial name of the device. 11 * @type: The type of device. 12 * This identifies the device type and carries type-specific 13 * information. 14 * @mutex: Mutex to synchronize calls to its driver. 15 * @bus: Type of bus device is on. 16 * @driver: Which driver has allocated this 17 * @platform_data: Platform data specific to the device. 18 * Example: For devices on custom boards, as typical of embedded 19 * and SOC based hardware, Linux often uses platform_data to point 20 * to board-specific structures describing devices and how they 21 * are wired. That can include what ports are available, chip 22 * variants, which GPIO pins act in what additional roles, and so 23 * on. This shrinks the "Board Support Packages" (BSPs) and 24 * minimizes board-specific #ifdefs in drivers. 25 * @power: For device power management. 26 * See Documentation/power/devices.txt for details. 27 * @pwr_domain: Provide callbacks that are executed during system suspend, 28 * hibernation, system resume and during runtime PM transitions 29 * along with subsystem-level and driver-level callbacks. 30 * @numa_node: NUMA node this device is close to. 31 * @dma_mask: Dma mask (if dma‘ble device). 32 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all 33 * hardware supports 64-bit addresses for consistent allocations 34 * such descriptors. 35 * @dma_parms: A low level driver may set these to teach IOMMU code about 36 * segment limitations. 37 * @dma_pools: Dma pools (if dma‘ble device). 38 * @dma_mem: Internal for coherent mem override. 39 * @archdata: For arch-specific additions. 40 * @of_node: Associated device tree node. 41 * @devt: For creating the sysfs "dev". 42 * @devres_lock: Spinlock to protect the resource of the device. 43 * @devres_head: The resources list of the device. 44 * @knode_class: The node used to add the device to the class list. 45 * @class: The class of the device. 46 * @groups: Optional attribute groups. 47 * @release: Callback to free the device after all references have 48 * gone away. This should be set by the allocator of the 49 * device (i.e. the bus driver that discovered the device). 50 * 51 * At the lowest level, every device in a Linux system is represented by an 52 * instance of struct device. The device structure contains the information 53 * that the device model core needs to model the system. Most subsystems, 54 * however, track additional information about the devices they host. As a 55 * result, it is rare for devices to be represented by bare device structures; 56 * instead, that structure, like kobject structures, is usually embedded within 57 * a higher-level representation of the device. 58 */ 59 struct device { 60 struct device *parent; 61 62 struct device_private *p; 63 64 struct kobject kobj; 65 const char *init_name; /* initial name of the device */ 66 const struct device_type *type; 67 68 struct mutex mutex; /* mutex to synchronize calls to 69 * its driver. 70 */ 71 72 struct bus_type *bus; /* type of bus device is on */ 73 struct device_driver *driver; /* which driver has allocated this 74 device */ 75 void *platform_data; /* Platform specific data, device 76 core doesn‘t touch it */ 77 struct dev_pm_info power; 78 struct dev_power_domain *pwr_domain; 79 80 #ifdef CONFIG_NUMA 81 int numa_node; /* NUMA node this device is close to */ 82 #endif 83 u64 *dma_mask; /* dma mask (if dma‘able device) */ 84 u64 coherent_dma_mask;/* Like dma_mask, but for 85 alloc_coherent mappings as 86 not all hardware supports 87 64 bit addresses for consistent 88 allocations such descriptors. */ 89 90 struct device_dma_parameters *dma_parms; 91 92 struct list_head dma_pools; /* dma pools (if dma‘ble) */ 93 94 struct dma_coherent_mem *dma_mem; /* internal for coherent mem 95 override */ 96 /* arch specific additions */ 97 struct dev_archdata archdata; 98 99 struct device_node *of_node; /* associated device tree node */ 100 101 dev_t devt; /* dev_t, creates the sysfs "dev" */ 102 103 spinlock_t devres_lock; 104 struct list_head devres_head; 105 106 struct klist_node knode_class; 107 struct class *class; 108 const struct attribute_group **groups; /* optional groups */ 109 110 void (*release)(struct device *dev); 111 };
下面的是调用具体函数的sample:
文件名:platform_device_test.c
1 #include <linux/init.h> 2 #include <linux/module.h> 3 #include <linux/platform_device.h> 4 5 static void leds_release(struct device *dev) 6 { 7 printk("leds_release\n"); 8 } 9 10 struct platform_device platform_device_hello = { 11 .name = "my_code_led", 12 .id = -1, 13 .dev = { 14 .release = leds_release, 15 16 } 17 }; 18 19 static int hello_init(void) 20 { 21 printk(KERN_EMERG "init \n"); 22 platform_device_register(&platform_device_hello); 23 return 0; 24 } 25 26 static void hello_exit(void) 27 { 28 platform_device_unregister(&platform_device_hello); //unregister会查找release,如果找不到会报错 29 } 30 31 module_init(hello_init); 32 module_exit(hello_exit); 33 34 MODULE_LICENSE("Dual BSD/GPL"); 35 MODULE_AUTHOR("NANZH");
编译:
1 $ make 2 make -C /home/nan/iTOP4412/iTop4412_Kernel_3.0 M=/home/nan/iTOP4412/3 modules 3 make[1]: Entering directory ‘/home/nan/iTOP4412/iTop4412_Kernel_3.0‘ 4 CC [M] /home/nan/iTOP4412/3/platform_device_test.o 5 Building modules, stage 2. 6 MODPOST 1 modules 7 CC /home/nan/iTOP4412/3/platform_device_test.mod.o 8 LD [M] /home/nan/iTOP4412/3/platform_device_test.ko 9 make[1]: Leaving directory ‘/home/nan/iTOP4412/iTop4412_Kernel_3.0‘
拷贝到开发板并查看
1 # insmod platform_device_test.ko 2 [ 544.778744] init 3 # ls /sys/devices/ 4 platform/ system/ virtual/ 5 # ls /sys/devices/platform/ 6 my_code_led ...
至此达到和设备注册一中的mach-itop4412.c中注册方法达到相同的结果。
标签:ini additions ice 功能 abs 结果 done map bit
原文地址:https://www.cnblogs.com/nanzh/p/12441901.html