-
Notifications
You must be signed in to change notification settings - Fork 11
/
3001-applesmc-convert-static-structures-to-drvdata.patch
1218 lines (1081 loc) · 36.5 KB
/
3001-applesmc-convert-static-structures-to-drvdata.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From aa8cbca2aa7fa99119fe0e7de616d5b0dcde3a15 Mon Sep 17 00:00:00 2001
From: Paul Pawlowski <[email protected]>
Date: Sun, 17 Nov 2019 23:12:55 +0100
Subject: [PATCH 1/9] applesmc: convert static structures to drvdata
All static data structures have been moved to an applesmc_device struct,
which is then associated with the platform device.
This change is intended to ease the migration to an acpi_device, where
static data would preferably be avoided.
Signed-off-by: Aun-Ali Zaidi <[email protected]>
---
drivers/hwmon/applesmc.c | 540 +++++++++++++++++++++++----------------
1 file changed, 319 insertions(+), 221 deletions(-)
diff --git a/drivers/hwmon/applesmc.c b/drivers/hwmon/applesmc.c
index fc6d6a905..7fb40738d 100644
--- a/drivers/hwmon/applesmc.c
+++ b/drivers/hwmon/applesmc.c
@@ -6,6 +6,7 @@
*
* Copyright (C) 2007 Nicolas Boichat <[email protected]>
* Copyright (C) 2010 Henrik Rydberg <[email protected]>
+ * Copyright (C) 2019 Paul Pawlowski <[email protected]>
*
* Based on hdaps.c driver:
* Copyright (C) 2005 Robert Love <[email protected]>
@@ -119,7 +120,7 @@ struct applesmc_entry {
};
/* Register lookup and registers common to all SMCs */
-static struct applesmc_registers {
+struct applesmc_registers {
struct mutex mutex; /* register read/write mutex */
unsigned int key_count; /* number of SMC registers */
unsigned int fan_count; /* number of fans */
@@ -133,26 +134,32 @@ static struct applesmc_registers {
bool init_complete; /* true when fully initialized */
struct applesmc_entry *cache; /* cached key entries */
const char **index; /* temperature key index */
-} smcreg = {
- .mutex = __MUTEX_INITIALIZER(smcreg.mutex),
};
-static const int debug;
-static struct platform_device *pdev;
-static s16 rest_x;
-static s16 rest_y;
-static u8 backlight_state[2];
+struct applesmc_device {
+ struct platform_device *dev;
+ struct applesmc_registers reg;
-static struct device *hwmon_dev;
-static struct input_dev *applesmc_idev;
+ s16 rest_x;
+ s16 rest_y;
-/*
- * Last index written to key_at_index sysfs file, and value to use for all other
- * key_at_index_* sysfs files.
- */
-static unsigned int key_at_index;
+ u8 backlight_state[2];
+
+ struct device *hwmon_dev;
+ struct input_dev *idev;
+
+ /*
+ * Last index written to key_at_index sysfs file, and value to use for all other
+ * key_at_index_* sysfs files.
+ */
+ unsigned int key_at_index;
+
+ struct workqueue_struct *backlight_wq;
+ struct work_struct backlight_work;
+ struct led_classdev backlight_dev;
+};
-static struct workqueue_struct *applesmc_led_wq;
+static const int debug;
/*
* Wait for specific status bits with a mask on the SMC.
@@ -338,36 +345,37 @@ static int read_register_count(unsigned int *count)
* All functions below are concurrency safe - callers should NOT hold lock.
*/
-static int applesmc_read_entry(const struct applesmc_entry *entry,
- u8 *buf, u8 len)
+static int applesmc_read_entry(struct applesmc_device *smc,
+ const struct applesmc_entry *entry, u8 *buf, u8 len)
{
int ret;
if (entry->len != len)
return -EINVAL;
- mutex_lock(&smcreg.mutex);
+ mutex_lock(&smc->reg.mutex);
ret = read_smc(APPLESMC_READ_CMD, entry->key, buf, len);
- mutex_unlock(&smcreg.mutex);
+ mutex_unlock(&smc->reg.mutex);
return ret;
}
-static int applesmc_write_entry(const struct applesmc_entry *entry,
- const u8 *buf, u8 len)
+static int applesmc_write_entry(struct applesmc_device *smc,
+ const struct applesmc_entry *entry, const u8 *buf, u8 len)
{
int ret;
if (entry->len != len)
return -EINVAL;
- mutex_lock(&smcreg.mutex);
+ mutex_lock(&smc->reg.mutex);
ret = write_smc(APPLESMC_WRITE_CMD, entry->key, buf, len);
- mutex_unlock(&smcreg.mutex);
+ mutex_unlock(&smc->reg.mutex);
return ret;
}
-static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
+static const struct applesmc_entry *applesmc_get_entry_by_index(
+ struct applesmc_device *smc, int index)
{
- struct applesmc_entry *cache = &smcreg.cache[index];
+ struct applesmc_entry *cache = &smc->reg.cache[index];
u8 key[4], info[6];
__be32 be;
int ret = 0;
@@ -375,7 +383,7 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
if (cache->valid)
return cache;
- mutex_lock(&smcreg.mutex);
+ mutex_lock(&smc->reg.mutex);
if (cache->valid)
goto out;
@@ -394,20 +402,21 @@ static const struct applesmc_entry *applesmc_get_entry_by_index(int index)
cache->valid = true;
out:
- mutex_unlock(&smcreg.mutex);
+ mutex_unlock(&smc->reg.mutex);
if (ret)
return ERR_PTR(ret);
return cache;
}
-static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
+static int applesmc_get_lower_bound(struct applesmc_device *smc,
+ unsigned int *lo, const char *key)
{
- int begin = 0, end = smcreg.key_count;
+ int begin = 0, end = smc->reg.key_count;
const struct applesmc_entry *entry;
while (begin != end) {
int middle = begin + (end - begin) / 2;
- entry = applesmc_get_entry_by_index(middle);
+ entry = applesmc_get_entry_by_index(smc, middle);
if (IS_ERR(entry)) {
*lo = 0;
return PTR_ERR(entry);
@@ -422,16 +431,17 @@ static int applesmc_get_lower_bound(unsigned int *lo, const char *key)
return 0;
}
-static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
+static int applesmc_get_upper_bound(struct applesmc_device *smc,
+ unsigned int *hi, const char *key)
{
- int begin = 0, end = smcreg.key_count;
+ int begin = 0, end = smc->reg.key_count;
const struct applesmc_entry *entry;
while (begin != end) {
int middle = begin + (end - begin) / 2;
- entry = applesmc_get_entry_by_index(middle);
+ entry = applesmc_get_entry_by_index(smc, middle);
if (IS_ERR(entry)) {
- *hi = smcreg.key_count;
+ *hi = smc->reg.key_count;
return PTR_ERR(entry);
}
if (strcmp(key, entry->key) < 0)
@@ -444,50 +454,54 @@ static int applesmc_get_upper_bound(unsigned int *hi, const char *key)
return 0;
}
-static const struct applesmc_entry *applesmc_get_entry_by_key(const char *key)
+static const struct applesmc_entry *applesmc_get_entry_by_key(
+ struct applesmc_device *smc, const char *key)
{
int begin, end;
int ret;
- ret = applesmc_get_lower_bound(&begin, key);
+ ret = applesmc_get_lower_bound(smc, &begin, key);
if (ret)
return ERR_PTR(ret);
- ret = applesmc_get_upper_bound(&end, key);
+ ret = applesmc_get_upper_bound(smc, &end, key);
if (ret)
return ERR_PTR(ret);
if (end - begin != 1)
return ERR_PTR(-EINVAL);
- return applesmc_get_entry_by_index(begin);
+ return applesmc_get_entry_by_index(smc, begin);
}
-static int applesmc_read_key(const char *key, u8 *buffer, u8 len)
+static int applesmc_read_key(struct applesmc_device *smc,
+ const char *key, u8 *buffer, u8 len)
{
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_key(key);
+ entry = applesmc_get_entry_by_key(smc, key);
if (IS_ERR(entry))
return PTR_ERR(entry);
- return applesmc_read_entry(entry, buffer, len);
+ return applesmc_read_entry(smc, entry, buffer, len);
}
-static int applesmc_write_key(const char *key, const u8 *buffer, u8 len)
+static int applesmc_write_key(struct applesmc_device *smc,
+ const char *key, const u8 *buffer, u8 len)
{
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_key(key);
+ entry = applesmc_get_entry_by_key(smc, key);
if (IS_ERR(entry))
return PTR_ERR(entry);
- return applesmc_write_entry(entry, buffer, len);
+ return applesmc_write_entry(smc, entry, buffer, len);
}
-static int applesmc_has_key(const char *key, bool *value)
+static int applesmc_has_key(struct applesmc_device *smc,
+ const char *key, bool *value)
{
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_key(key);
+ entry = applesmc_get_entry_by_key(smc, key);
if (IS_ERR(entry) && PTR_ERR(entry) != -EINVAL)
return PTR_ERR(entry);
@@ -498,12 +512,13 @@ static int applesmc_has_key(const char *key, bool *value)
/*
* applesmc_read_s16 - Read 16-bit signed big endian register
*/
-static int applesmc_read_s16(const char *key, s16 *value)
+static int applesmc_read_s16(struct applesmc_device *smc,
+ const char *key, s16 *value)
{
u8 buffer[2];
int ret;
- ret = applesmc_read_key(key, buffer, 2);
+ ret = applesmc_read_key(smc, key, buffer, 2);
if (ret)
return ret;
@@ -514,28 +529,29 @@ static int applesmc_read_s16(const char *key, s16 *value)
/*
* applesmc_device_init - initialize the accelerometer. Can sleep.
*/
-static void applesmc_device_init(void)
+static void applesmc_device_init(struct applesmc_device *smc)
{
int total;
u8 buffer[2];
- if (!smcreg.has_accelerometer)
+ if (!smc->reg.has_accelerometer)
return;
for (total = INIT_TIMEOUT_MSECS; total > 0; total -= INIT_WAIT_MSECS) {
- if (!applesmc_read_key(MOTION_SENSOR_KEY, buffer, 2) &&
+ if (!applesmc_read_key(smc, MOTION_SENSOR_KEY, buffer, 2) &&
(buffer[0] != 0x00 || buffer[1] != 0x00))
return;
buffer[0] = 0xe0;
buffer[1] = 0x00;
- applesmc_write_key(MOTION_SENSOR_KEY, buffer, 2);
+ applesmc_write_key(smc, MOTION_SENSOR_KEY, buffer, 2);
msleep(INIT_WAIT_MSECS);
}
pr_warn("failed to init the device\n");
}
-static int applesmc_init_index(struct applesmc_registers *s)
+static int applesmc_init_index(struct applesmc_device *smc,
+ struct applesmc_registers *s)
{
const struct applesmc_entry *entry;
unsigned int i;
@@ -548,7 +564,7 @@ static int applesmc_init_index(struct applesmc_registers *s)
return -ENOMEM;
for (i = s->temp_begin; i < s->temp_end; i++) {
- entry = applesmc_get_entry_by_index(i);
+ entry = applesmc_get_entry_by_index(smc, i);
if (IS_ERR(entry))
continue;
if (strcmp(entry->type, TEMP_SENSOR_TYPE))
@@ -562,9 +578,9 @@ static int applesmc_init_index(struct applesmc_registers *s)
/*
* applesmc_init_smcreg_try - Try to initialize register cache. Idempotent.
*/
-static int applesmc_init_smcreg_try(void)
+static int applesmc_init_smcreg_try(struct applesmc_device *smc)
{
- struct applesmc_registers *s = &smcreg;
+ struct applesmc_registers *s = &smc->reg;
bool left_light_sensor = false, right_light_sensor = false;
unsigned int count;
u8 tmp[1];
@@ -590,35 +606,35 @@ static int applesmc_init_smcreg_try(void)
if (!s->cache)
return -ENOMEM;
- ret = applesmc_read_key(FANS_COUNT, tmp, 1);
+ ret = applesmc_read_key(smc, FANS_COUNT, tmp, 1);
if (ret)
return ret;
s->fan_count = tmp[0];
if (s->fan_count > 10)
s->fan_count = 10;
- ret = applesmc_get_lower_bound(&s->temp_begin, "T");
+ ret = applesmc_get_lower_bound(smc, &s->temp_begin, "T");
if (ret)
return ret;
- ret = applesmc_get_lower_bound(&s->temp_end, "U");
+ ret = applesmc_get_lower_bound(smc, &s->temp_end, "U");
if (ret)
return ret;
s->temp_count = s->temp_end - s->temp_begin;
- ret = applesmc_init_index(s);
+ ret = applesmc_init_index(smc, s);
if (ret)
return ret;
- ret = applesmc_has_key(LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
+ ret = applesmc_has_key(smc, LIGHT_SENSOR_LEFT_KEY, &left_light_sensor);
if (ret)
return ret;
- ret = applesmc_has_key(LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
+ ret = applesmc_has_key(smc, LIGHT_SENSOR_RIGHT_KEY, &right_light_sensor);
if (ret)
return ret;
- ret = applesmc_has_key(MOTION_SENSOR_KEY, &s->has_accelerometer);
+ ret = applesmc_has_key(smc, MOTION_SENSOR_KEY, &s->has_accelerometer);
if (ret)
return ret;
- ret = applesmc_has_key(BACKLIGHT_KEY, &s->has_key_backlight);
+ ret = applesmc_has_key(smc, BACKLIGHT_KEY, &s->has_key_backlight);
if (ret)
return ret;
@@ -634,13 +650,13 @@ static int applesmc_init_smcreg_try(void)
return 0;
}
-static void applesmc_destroy_smcreg(void)
+static void applesmc_destroy_smcreg(struct applesmc_device *smc)
{
- kfree(smcreg.index);
- smcreg.index = NULL;
- kfree(smcreg.cache);
- smcreg.cache = NULL;
- smcreg.init_complete = false;
+ kfree(smc->reg.index);
+ smc->reg.index = NULL;
+ kfree(smc->reg.cache);
+ smc->reg.cache = NULL;
+ smc->reg.init_complete = false;
}
/*
@@ -649,12 +665,12 @@ static void applesmc_destroy_smcreg(void)
* Retries until initialization is successful, or the operation times out.
*
*/
-static int applesmc_init_smcreg(void)
+static int applesmc_init_smcreg(struct applesmc_device *smc)
{
int ms, ret;
for (ms = 0; ms < INIT_TIMEOUT_MSECS; ms += INIT_WAIT_MSECS) {
- ret = applesmc_init_smcreg_try();
+ ret = applesmc_init_smcreg_try(smc);
if (!ret) {
if (ms)
pr_info("init_smcreg() took %d ms\n", ms);
@@ -663,21 +679,58 @@ static int applesmc_init_smcreg(void)
msleep(INIT_WAIT_MSECS);
}
- applesmc_destroy_smcreg();
+ applesmc_destroy_smcreg(smc);
return ret;
}
/* Device model stuff */
+static int applesmc_create_modules(struct applesmc_device *smc);
+static void applesmc_destroy_modules(struct applesmc_device *smc);
static int applesmc_probe(struct platform_device *dev)
{
+ struct applesmc_device *smc;
int ret;
- ret = applesmc_init_smcreg();
+ smc = kzalloc(sizeof(struct applesmc_device), GFP_KERNEL);
+ if (!smc)
+ return -ENOMEM;
+ smc->dev = dev;
+ mutex_init(&smc->reg.mutex);
+
+ platform_set_drvdata(dev, smc);
+
+ ret = applesmc_init_smcreg(smc);
if (ret)
- return ret;
+ goto out_mem;
+
+ applesmc_device_init(smc);
+
+ ret = applesmc_create_modules(smc);
+ if (ret)
+ goto out_reg;
+
+ return 0;
+
+out_reg:
+ applesmc_destroy_smcreg(smc);
+out_mem:
+ platform_set_drvdata(dev, NULL);
+ mutex_destroy(&smc->reg.mutex);
+ kfree(smc);
- applesmc_device_init();
+ return ret;
+}
+
+static int applesmc_remove(struct platform_device *dev)
+{
+ struct applesmc_device *smc = platform_get_drvdata(dev);
+
+ applesmc_destroy_modules(smc);
+ applesmc_destroy_smcreg(smc);
+
+ mutex_destroy(&smc->reg.mutex);
+ kfree(smc);
return 0;
}
@@ -685,15 +738,21 @@ static int applesmc_probe(struct platform_device *dev)
/* Synchronize device with memorized backlight state */
static int applesmc_pm_resume(struct device *dev)
{
- if (smcreg.has_key_backlight)
- applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+
+ if (smc->reg.has_key_backlight)
+ applesmc_write_key(smc, BACKLIGHT_KEY, smc->backlight_state, 2);
+
return 0;
}
/* Reinitialize device on resume from hibernation */
static int applesmc_pm_restore(struct device *dev)
{
- applesmc_device_init();
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+
+ applesmc_device_init(smc);
+
return applesmc_pm_resume(dev);
}
@@ -704,6 +763,7 @@ static const struct dev_pm_ops applesmc_pm_ops = {
static struct platform_driver applesmc_driver = {
.probe = applesmc_probe,
+ .remove = applesmc_remove,
.driver = {
.name = "applesmc",
.pm = &applesmc_pm_ops,
@@ -714,25 +774,26 @@ static struct platform_driver applesmc_driver = {
* applesmc_calibrate - Set our "resting" values. Callers must
* hold applesmc_lock.
*/
-static void applesmc_calibrate(void)
+static void applesmc_calibrate(struct applesmc_device *smc)
{
- applesmc_read_s16(MOTION_SENSOR_X_KEY, &rest_x);
- applesmc_read_s16(MOTION_SENSOR_Y_KEY, &rest_y);
- rest_x = -rest_x;
+ applesmc_read_s16(smc, MOTION_SENSOR_X_KEY, &smc->rest_x);
+ applesmc_read_s16(smc, MOTION_SENSOR_Y_KEY, &smc->rest_y);
+ smc->rest_x = -smc->rest_x;
}
static void applesmc_idev_poll(struct input_dev *idev)
{
+ struct applesmc_device *smc = dev_get_drvdata(&idev->dev);
s16 x, y;
- if (applesmc_read_s16(MOTION_SENSOR_X_KEY, &x))
+ if (applesmc_read_s16(smc, MOTION_SENSOR_X_KEY, &x))
return;
- if (applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y))
+ if (applesmc_read_s16(smc, MOTION_SENSOR_Y_KEY, &y))
return;
x = -x;
- input_report_abs(idev, ABS_X, x - rest_x);
- input_report_abs(idev, ABS_Y, y - rest_y);
+ input_report_abs(idev, ABS_X, x - smc->rest_x);
+ input_report_abs(idev, ABS_Y, y - smc->rest_y);
input_sync(idev);
}
@@ -747,16 +808,17 @@ static ssize_t applesmc_name_show(struct device *dev,
static ssize_t applesmc_position_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
s16 x, y, z;
- ret = applesmc_read_s16(MOTION_SENSOR_X_KEY, &x);
+ ret = applesmc_read_s16(smc, MOTION_SENSOR_X_KEY, &x);
if (ret)
goto out;
- ret = applesmc_read_s16(MOTION_SENSOR_Y_KEY, &y);
+ ret = applesmc_read_s16(smc, MOTION_SENSOR_Y_KEY, &y);
if (ret)
goto out;
- ret = applesmc_read_s16(MOTION_SENSOR_Z_KEY, &z);
+ ret = applesmc_read_s16(smc, MOTION_SENSOR_Z_KEY, &z);
if (ret)
goto out;
@@ -770,6 +832,7 @@ static ssize_t applesmc_position_show(struct device *dev,
static ssize_t applesmc_light_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
const struct applesmc_entry *entry;
static int data_length;
int ret;
@@ -777,7 +840,7 @@ static ssize_t applesmc_light_show(struct device *dev,
u8 buffer[10];
if (!data_length) {
- entry = applesmc_get_entry_by_key(LIGHT_SENSOR_LEFT_KEY);
+ entry = applesmc_get_entry_by_key(smc, LIGHT_SENSOR_LEFT_KEY);
if (IS_ERR(entry))
return PTR_ERR(entry);
if (entry->len > 10)
@@ -786,7 +849,7 @@ static ssize_t applesmc_light_show(struct device *dev,
pr_info("light sensor data length set to %d\n", data_length);
}
- ret = applesmc_read_key(LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
+ ret = applesmc_read_key(smc, LIGHT_SENSOR_LEFT_KEY, buffer, data_length);
if (ret)
goto out;
/* newer macbooks report a single 10-bit bigendian value */
@@ -796,7 +859,7 @@ static ssize_t applesmc_light_show(struct device *dev,
}
left = buffer[2];
- ret = applesmc_read_key(LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
+ ret = applesmc_read_key(smc, LIGHT_SENSOR_RIGHT_KEY, buffer, data_length);
if (ret)
goto out;
right = buffer[2];
@@ -812,7 +875,8 @@ static ssize_t applesmc_light_show(struct device *dev,
static ssize_t applesmc_show_sensor_label(struct device *dev,
struct device_attribute *devattr, char *sysfsbuf)
{
- const char *key = smcreg.index[to_index(devattr)];
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+ const char *key = smc->reg.index[to_index(devattr)];
return sysfs_emit(sysfsbuf, "%s\n", key);
}
@@ -821,12 +885,13 @@ static ssize_t applesmc_show_sensor_label(struct device *dev,
static ssize_t applesmc_show_temperature(struct device *dev,
struct device_attribute *devattr, char *sysfsbuf)
{
- const char *key = smcreg.index[to_index(devattr)];
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+ const char *key = smc->reg.index[to_index(devattr)];
int ret;
s16 value;
int temp;
- ret = applesmc_read_s16(key, &value);
+ ret = applesmc_read_s16(smc, key, &value);
if (ret)
return ret;
@@ -838,6 +903,7 @@ static ssize_t applesmc_show_temperature(struct device *dev,
static ssize_t applesmc_show_fan_speed(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
unsigned int speed = 0;
char newkey[5];
@@ -846,7 +912,7 @@ static ssize_t applesmc_show_fan_speed(struct device *dev,
scnprintf(newkey, sizeof(newkey), fan_speed_fmt[to_option(attr)],
to_index(attr));
- ret = applesmc_read_key(newkey, buffer, 2);
+ ret = applesmc_read_key(smc, newkey, buffer, 2);
if (ret)
return ret;
@@ -858,6 +924,7 @@ static ssize_t applesmc_store_fan_speed(struct device *dev,
struct device_attribute *attr,
const char *sysfsbuf, size_t count)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
unsigned long speed;
char newkey[5];
@@ -871,7 +938,7 @@ static ssize_t applesmc_store_fan_speed(struct device *dev,
buffer[0] = (speed >> 6) & 0xff;
buffer[1] = (speed << 2) & 0xff;
- ret = applesmc_write_key(newkey, buffer, 2);
+ ret = applesmc_write_key(smc, newkey, buffer, 2);
if (ret)
return ret;
@@ -882,11 +949,12 @@ static ssize_t applesmc_store_fan_speed(struct device *dev,
static ssize_t applesmc_show_fan_manual(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
u16 manual = 0;
u8 buffer[2];
- ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
+ ret = applesmc_read_key(smc, FANS_MANUAL, buffer, 2);
if (ret)
return ret;
@@ -898,6 +966,7 @@ static ssize_t applesmc_store_fan_manual(struct device *dev,
struct device_attribute *attr,
const char *sysfsbuf, size_t count)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
u8 buffer[2];
unsigned long input;
@@ -906,7 +975,7 @@ static ssize_t applesmc_store_fan_manual(struct device *dev,
if (kstrtoul(sysfsbuf, 10, &input) < 0)
return -EINVAL;
- ret = applesmc_read_key(FANS_MANUAL, buffer, 2);
+ ret = applesmc_read_key(smc, FANS_MANUAL, buffer, 2);
if (ret)
goto out;
@@ -920,7 +989,7 @@ static ssize_t applesmc_store_fan_manual(struct device *dev,
buffer[0] = (val >> 8) & 0xFF;
buffer[1] = val & 0xFF;
- ret = applesmc_write_key(FANS_MANUAL, buffer, 2);
+ ret = applesmc_write_key(smc, FANS_MANUAL, buffer, 2);
out:
if (ret)
@@ -932,13 +1001,14 @@ static ssize_t applesmc_store_fan_manual(struct device *dev,
static ssize_t applesmc_show_fan_position(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
char newkey[5];
u8 buffer[17];
scnprintf(newkey, sizeof(newkey), FAN_ID_FMT, to_index(attr));
- ret = applesmc_read_key(newkey, buffer, 16);
+ ret = applesmc_read_key(smc, newkey, buffer, 16);
buffer[16] = 0;
if (ret)
@@ -950,30 +1020,36 @@ static ssize_t applesmc_show_fan_position(struct device *dev,
static ssize_t applesmc_calibrate_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
- return sysfs_emit(sysfsbuf, "(%d,%d)\n", rest_x, rest_y);
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+
+ return sysfs_emit(sysfsbuf, "(%d,%d)\n", smc->rest_x, smc->rest_y);
}
static ssize_t applesmc_calibrate_store(struct device *dev,
struct device_attribute *attr, const char *sysfsbuf, size_t count)
{
- applesmc_calibrate();
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+
+ applesmc_calibrate(smc);
return count;
}
static void applesmc_backlight_set(struct work_struct *work)
{
- applesmc_write_key(BACKLIGHT_KEY, backlight_state, 2);
+ struct applesmc_device *smc = container_of(work, struct applesmc_device, backlight_work);
+
+ applesmc_write_key(smc, BACKLIGHT_KEY, smc->backlight_state, 2);
}
-static DECLARE_WORK(backlight_work, &applesmc_backlight_set);
static void applesmc_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
+ struct applesmc_device *smc = dev_get_drvdata(led_cdev->dev);
int ret;
- backlight_state[0] = value;
- ret = queue_work(applesmc_led_wq, &backlight_work);
+ smc->backlight_state[0] = value;
+ ret = queue_work(smc->backlight_wq, &smc->backlight_work);
if (debug && (!ret))
dev_dbg(led_cdev->dev, "work was already on the queue.\n");
@@ -982,11 +1058,12 @@ static void applesmc_brightness_set(struct led_classdev *led_cdev,
static ssize_t applesmc_key_count_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
int ret;
u8 buffer[4];
u32 count;
- ret = applesmc_read_key(KEY_COUNT_KEY, buffer, 4);
+ ret = applesmc_read_key(smc, KEY_COUNT_KEY, buffer, 4);
if (ret)
return ret;
@@ -998,13 +1075,14 @@ static ssize_t applesmc_key_count_show(struct device *dev,
static ssize_t applesmc_key_at_index_read_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
const struct applesmc_entry *entry;
int ret;
- entry = applesmc_get_entry_by_index(key_at_index);
+ entry = applesmc_get_entry_by_index(smc, smc->key_at_index);
if (IS_ERR(entry))
return PTR_ERR(entry);
- ret = applesmc_read_entry(entry, sysfsbuf, entry->len);
+ ret = applesmc_read_entry(smc, entry, sysfsbuf, entry->len);
if (ret)
return ret;
@@ -1014,9 +1092,10 @@ static ssize_t applesmc_key_at_index_read_show(struct device *dev,
static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_index(key_at_index);
+ entry = applesmc_get_entry_by_index(smc, smc->key_at_index);
if (IS_ERR(entry))
return PTR_ERR(entry);
@@ -1026,9 +1105,10 @@ static ssize_t applesmc_key_at_index_data_length_show(struct device *dev,
static ssize_t applesmc_key_at_index_type_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_index(key_at_index);
+ entry = applesmc_get_entry_by_index(smc, smc->key_at_index);
if (IS_ERR(entry))
return PTR_ERR(entry);
@@ -1038,9 +1118,10 @@ static ssize_t applesmc_key_at_index_type_show(struct device *dev,
static ssize_t applesmc_key_at_index_name_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
const struct applesmc_entry *entry;
- entry = applesmc_get_entry_by_index(key_at_index);
+ entry = applesmc_get_entry_by_index(smc, smc->key_at_index);
if (IS_ERR(entry))
return PTR_ERR(entry);
@@ -1050,28 +1131,25 @@ static ssize_t applesmc_key_at_index_name_show(struct device *dev,
static ssize_t applesmc_key_at_index_show(struct device *dev,
struct device_attribute *attr, char *sysfsbuf)
{
- return sysfs_emit(sysfsbuf, "%d\n", key_at_index);
+ struct applesmc_device *smc = dev_get_drvdata(dev);
+
+ return sysfs_emit(sysfsbuf, "%d\n", smc->key_at_index);
}
static ssize_t applesmc_key_at_index_store(struct device *dev,
struct device_attribute *attr, const char *sysfsbuf, size_t count)
{
+ struct applesmc_device *smc = dev_get_drvdata(dev);
unsigned long newkey;
if (kstrtoul(sysfsbuf, 10, &newkey) < 0
- || newkey >= smcreg.key_count)
+ || newkey >= smc->reg.key_count)
return -EINVAL;
- key_at_index = newkey;
+ smc->key_at_index = newkey;
return count;
}
-static struct led_classdev applesmc_backlight = {
- .name = "smc::kbd_backlight",
- .default_trigger = "nand-disk",
- .brightness_set = applesmc_brightness_set,
-};
-
static struct applesmc_node_group info_group[] = {
{ "name", applesmc_name_show },
{ "key_count", applesmc_key_count_show },
@@ -1116,14 +1194,15 @@ static struct applesmc_node_group temp_group[] = {
/*
* applesmc_destroy_nodes - remove files and free associated memory
*/
-static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
+static void applesmc_destroy_nodes(struct applesmc_device *smc,
+ struct applesmc_node_group *groups)
{
struct applesmc_node_group *grp;
struct applesmc_dev_attr *node;
for (grp = groups; grp->nodes; grp++) {
for (node = grp->nodes; node->sda.dev_attr.attr.name; node++)
- sysfs_remove_file(&pdev->dev.kobj,
+ sysfs_remove_file(&smc->dev->dev.kobj,
&node->sda.dev_attr.attr);
kfree(grp->nodes);
grp->nodes = NULL;
@@ -1133,7 +1212,8 @@ static void applesmc_destroy_nodes(struct applesmc_node_group *groups)
/*
* applesmc_create_nodes - create a two-dimensional group of sysfs files
*/
-static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
+static int applesmc_create_nodes(struct applesmc_device *smc,
+ struct applesmc_node_group *groups, int num)
{
struct applesmc_node_group *grp;
struct applesmc_dev_attr *node;
@@ -1157,7 +1237,7 @@ static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
sysfs_attr_init(attr);
attr->name = node->name;
attr->mode = 0444 | (grp->store ? 0200 : 0);
- ret = sysfs_create_file(&pdev->dev.kobj, attr);
+ ret = sysfs_create_file(&smc->dev->dev.kobj, attr);
if (ret) {
attr->name = NULL;
goto out;
@@ -1167,57 +1247,57 @@ static int applesmc_create_nodes(struct applesmc_node_group *groups, int num)
return 0;
out:
- applesmc_destroy_nodes(groups);
+ applesmc_destroy_nodes(smc, groups);
return ret;
}
/* Create accelerometer resources */
-static int applesmc_create_accelerometer(void)
+static int applesmc_create_accelerometer(struct applesmc_device *smc)
{
int ret;
- if (!smcreg.has_accelerometer)
+ if (!smc->reg.has_accelerometer)
return 0;
- ret = applesmc_create_nodes(accelerometer_group, 1);
+ ret = applesmc_create_nodes(smc, accelerometer_group, 1);
if (ret)
goto out;
- applesmc_idev = input_allocate_device();
- if (!applesmc_idev) {
+ smc->idev = input_allocate_device();
+ if (!smc->idev) {
ret = -ENOMEM;
goto out_sysfs;
}
/* initial calibrate for the input device */
- applesmc_calibrate();
+ applesmc_calibrate(smc);
/* initialize the input device */
- applesmc_idev->name = "applesmc";
- applesmc_idev->id.bustype = BUS_HOST;
- applesmc_idev->dev.parent = &pdev->dev;
- input_set_abs_params(applesmc_idev, ABS_X,
+ smc->idev->name = "applesmc";
+ smc->idev->id.bustype = BUS_HOST;
+ smc->idev->dev.parent = &smc->dev->dev;
+ input_set_abs_params(smc->idev, ABS_X,
-256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
- input_set_abs_params(applesmc_idev, ABS_Y,
+ input_set_abs_params(smc->idev, ABS_Y,
-256, 256, APPLESMC_INPUT_FUZZ, APPLESMC_INPUT_FLAT);
- ret = input_setup_polling(applesmc_idev, applesmc_idev_poll);
+ ret = input_setup_polling(smc->idev, applesmc_idev_poll);
if (ret)
goto out_idev;
- input_set_poll_interval(applesmc_idev, APPLESMC_POLL_INTERVAL);
+ input_set_poll_interval(smc->idev, APPLESMC_POLL_INTERVAL);
- ret = input_register_device(applesmc_idev);
+ ret = input_register_device(smc->idev);
if (ret)
goto out_idev;
return 0;
out_idev:
- input_free_device(applesmc_idev);
+ input_free_device(smc->idev);
out_sysfs:
- applesmc_destroy_nodes(accelerometer_group);
+ applesmc_destroy_nodes(smc, accelerometer_group);
out:
pr_warn("driver init failed (ret=%d)!\n", ret);
@@ -1225,44 +1305,55 @@ static int applesmc_create_accelerometer(void)
}
/* Release all resources used by the accelerometer */
-static void applesmc_release_accelerometer(void)
+static void applesmc_release_accelerometer(struct applesmc_device *smc)
{
- if (!smcreg.has_accelerometer)
+ if (!smc->reg.has_accelerometer)
return;
- input_unregister_device(applesmc_idev);
- applesmc_destroy_nodes(accelerometer_group);
+ input_unregister_device(smc->idev);
+ applesmc_destroy_nodes(smc, accelerometer_group);
}
-static int applesmc_create_light_sensor(void)
+static int applesmc_create_light_sensor(struct applesmc_device *smc)
{
- if (!smcreg.num_light_sensors)
+ if (!smc->reg.num_light_sensors)
return 0;
- return applesmc_create_nodes(light_sensor_group, 1);
+ return applesmc_create_nodes(smc, light_sensor_group, 1);
}
-static void applesmc_release_light_sensor(void)
+static void applesmc_release_light_sensor(struct applesmc_device *smc)
{
- if (!smcreg.num_light_sensors)
+ if (!smc->reg.num_light_sensors)
return;
- applesmc_destroy_nodes(light_sensor_group);