5
5
#include <linux/acpi.h>
6
6
#include <linux/bits.h>
7
7
#include <linux/init.h>
8
- #include <linux/kdev_t.h>
9
8
#include <linux/mutex.h>
10
9
#include <linux/platform_profile.h>
11
10
#include <linux/sysfs.h>
12
11
12
+ #define to_pprof_handler (d ) (container_of(d, struct platform_profile_handler, class_dev))
13
+
13
14
static DEFINE_MUTEX (profile_lock );
14
15
15
16
static const char * const profile_names [] = {
@@ -60,7 +61,7 @@ static int _store_class_profile(struct device *dev, void *data)
60
61
int * bit = (int * )data ;
61
62
62
63
lockdep_assert_held (& profile_lock );
63
- handler = dev_get_drvdata (dev );
64
+ handler = to_pprof_handler (dev );
64
65
if (!test_bit (* bit , handler -> choices ))
65
66
return - EOPNOTSUPP ;
66
67
@@ -76,11 +77,11 @@ static int _store_class_profile(struct device *dev, void *data)
76
77
*/
77
78
static int _notify_class_profile (struct device * dev , void * data )
78
79
{
79
- struct platform_profile_handler * handler = dev_get_drvdata (dev );
80
+ struct platform_profile_handler * handler = to_pprof_handler (dev );
80
81
81
82
lockdep_assert_held (& profile_lock );
82
- sysfs_notify (& handler -> class_dev -> kobj , NULL , "profile" );
83
- kobject_uevent (& handler -> class_dev -> kobj , KOBJ_CHANGE );
83
+ sysfs_notify (& handler -> class_dev . kobj , NULL , "profile" );
84
+ kobject_uevent (& handler -> class_dev . kobj , KOBJ_CHANGE );
84
85
85
86
return 0 ;
86
87
}
@@ -100,7 +101,7 @@ static int get_class_profile(struct device *dev,
100
101
int err ;
101
102
102
103
lockdep_assert_held (& profile_lock );
103
- handler = dev_get_drvdata (dev );
104
+ handler = to_pprof_handler (dev );
104
105
err = handler -> profile_get (handler , & val );
105
106
if (err ) {
106
107
pr_err ("Failed to get profile for handler %s\n" , handler -> name );
@@ -124,7 +125,7 @@ static int get_class_profile(struct device *dev,
124
125
*/
125
126
static ssize_t name_show (struct device * dev , struct device_attribute * attr , char * buf )
126
127
{
127
- struct platform_profile_handler * handler = dev_get_drvdata (dev );
128
+ struct platform_profile_handler * handler = to_pprof_handler (dev );
128
129
129
130
return sysfs_emit (buf , "%s\n" , handler -> name );
130
131
}
@@ -142,7 +143,7 @@ static ssize_t choices_show(struct device *dev,
142
143
struct device_attribute * attr ,
143
144
char * buf )
144
145
{
145
- struct platform_profile_handler * handler = dev_get_drvdata (dev );
146
+ struct platform_profile_handler * handler = to_pprof_handler (dev );
146
147
147
148
return _commmon_choices_show (handler -> choices , buf );
148
149
}
@@ -229,7 +230,7 @@ static int _aggregate_choices(struct device *dev, void *data)
229
230
unsigned long * aggregate = data ;
230
231
231
232
lockdep_assert_held (& profile_lock );
232
- handler = dev_get_drvdata (dev );
233
+ handler = to_pprof_handler (dev );
233
234
if (test_bit (PLATFORM_PROFILE_LAST , aggregate ))
234
235
bitmap_copy (aggregate , handler -> choices , PLATFORM_PROFILE_LAST );
235
236
else
@@ -410,7 +411,7 @@ static const struct attribute_group platform_profile_group = {
410
411
void platform_profile_notify (struct platform_profile_handler * pprof )
411
412
{
412
413
scoped_cond_guard (mutex_intr , return , & profile_lock ) {
413
- _notify_class_profile (pprof -> class_dev , NULL );
414
+ _notify_class_profile (& pprof -> class_dev , NULL );
414
415
}
415
416
sysfs_notify (acpi_kobj , NULL , "platform_profile" );
416
417
}
@@ -476,11 +477,13 @@ int platform_profile_register(struct platform_profile_handler *pprof)
476
477
pprof -> minor = ida_alloc (& platform_profile_ida , GFP_KERNEL );
477
478
if (pprof -> minor < 0 )
478
479
return pprof -> minor ;
479
- pprof -> class_dev = device_create (& platform_profile_class , pprof -> dev ,
480
- MKDEV (0 , 0 ), pprof , "platform-profile-%d" ,
481
- pprof -> minor );
482
- if (IS_ERR (pprof -> class_dev )) {
483
- err = PTR_ERR (pprof -> class_dev );
480
+
481
+ pprof -> class_dev .class = & platform_profile_class ;
482
+ pprof -> class_dev .parent = pprof -> dev ;
483
+ dev_set_name (& pprof -> class_dev , "platform-profile-%d" , pprof -> minor );
484
+ err = device_register (& pprof -> class_dev );
485
+ if (err ) {
486
+ put_device (& pprof -> class_dev );
484
487
goto cleanup_ida ;
485
488
}
486
489
@@ -493,7 +496,7 @@ int platform_profile_register(struct platform_profile_handler *pprof)
493
496
return 0 ;
494
497
495
498
cleanup_cur :
496
- device_unregister (pprof -> class_dev );
499
+ device_unregister (& pprof -> class_dev );
497
500
498
501
cleanup_ida :
499
502
ida_free (& platform_profile_ida , pprof -> minor );
@@ -508,7 +511,7 @@ int platform_profile_remove(struct platform_profile_handler *pprof)
508
511
guard (mutex )(& profile_lock );
509
512
510
513
id = pprof -> minor ;
511
- device_unregister (pprof -> class_dev );
514
+ device_unregister (& pprof -> class_dev );
512
515
ida_free (& platform_profile_ida , id );
513
516
514
517
sysfs_notify (acpi_kobj , NULL , "platform_profile" );
0 commit comments