From mboxrd@z Thu Jan 1 00:00:00 1970 From: mindentropy@gmail.com (mind entropy) Date: Sun, 10 Aug 2014 13:27:37 +0530 Subject: On platform device template. Message-ID: To: kernelnewbies@lists.kernelnewbies.org List-Id: kernelnewbies.lists.kernelnewbies.org Hi, I am testing out the platform driver and would like to know the template for platform device register/unregister. The following code causes a crash whenever the device gets unregistered. --------------------------------------------------- #include #include #include #include #include #include struct test_platform_data { int x; int y; }; static struct test_platform_data test_pd = { .x = 2, .y = 3, }; static struct platform_device test_platform_device = { .name = "test-plat", .id = -1, .resource = NULL, .num_resources = 0, .dev = { .platform_data = &test_pd, } }; static int __init platform_driver_test_init(void) { int retval = 0; printk(KERN_ALERT "Platform driver init\n"); retval = platform_device_register(&test_platform_device); if(retval != 0) { printk(KERN_ALERT "Could not register platform device\n"); return retval; } else { printk(KERN_ALERT "Registered platform device\n"); printk(KERN_ALERT "Unregistering\n"); platform_device_unregister(&test_platform_device); } return 0; } static void __exit platform_driver_test_exit(void) { printk(KERN_ALERT "Platform driver exit\n"); } module_init(platform_driver_test_init); module_exit(platform_driver_test_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("TEST"); MODULE_DESCRIPTION("Platform driver test"); --------------------------------------------------- Should the platform device be created using platform_device_alloc(..) and added using platform_device_add(..) like the below code? ---------------------------------------------------- #include #include #include #include #include #include struct test_platform_data { int x; int y; }; static struct test_platform_data test_pd = { .x = 2, .y = 3, }; static struct platform_device *test_platform_device; static int __init platform_driver_test_init(void) { int retval = 0; printk(KERN_ALERT "Platform driver init\n"); test_platform_device = platform_device_alloc("test-plat",-1); if(test_platform_device == NULL) { printk(KERN_ALERT "Could not allocate platform device\n"); return 0; } platform_device_add_data(test_platform_device,&test_pd,sizeof(test_pd)); retval = platform_device_add(test_platform_device); if(retval != 0) { printk(KERN_ALERT "Could not register platform device\n"); return retval; } else { printk(KERN_ALERT "Registered platform device\n"); printk(KERN_ALERT "Unregistering\n"); platform_device_unregister(test_platform_device); } return 0; } static void __exit platform_driver_test_exit(void) { printk(KERN_ALERT "Platform driver exit\n"); } module_init(platform_driver_test_init); module_exit(platform_driver_test_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("TEST"); MODULE_DESCRIPTION("Platform driver test"); ---------------------------------------------------- Thanks, Gautam.