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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
|
// SPDX-License-Identifier: GPL-2.0+
/*
* Surface Book (gen. 2 and later) detachment system (DTX) driver.
*
* Provides a user-space interface to properly handle clipboard/tablet
* (containing screen and processor) detachment from the base of the device
* (containing the keyboard and optionally a discrete GPU). Allows to
* acknowledge (to speed things up), abort (e.g. in case the dGPU is still in
* use), or request detachment via user-space.
*
* Copyright (C) 2019-2021 Maximilian Luz <luzmaximilian@gmail.com>
*/
#include <linux/fs.h>
#include <linux/input.h>
#include <linux/ioctl.h>
#include <linux/kernel.h>
#include <linux/kfifo.h>
#include <linux/kref.h>
#include <linux/miscdevice.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/rwsem.h>
#include <linux/slab.h>
#include <linux/workqueue.h>
#include <linux/surface_aggregator/controller.h>
#include <linux/surface_aggregator/device.h>
#include <linux/surface_aggregator/dtx.h>
/* -- SSAM interface. ------------------------------------------------------- */
enum sam_event_cid_bas {
SAM_EVENT_CID_DTX_CONNECTION = 0x0c,
SAM_EVENT_CID_DTX_REQUEST = 0x0e,
SAM_EVENT_CID_DTX_CANCEL = 0x0f,
SAM_EVENT_CID_DTX_LATCH_STATUS = 0x11,
};
enum ssam_bas_base_state {
SSAM_BAS_BASE_STATE_DETACH_SUCCESS = 0x00,
SSAM_BAS_BASE_STATE_ATTACHED = 0x01,
SSAM_BAS_BASE_STATE_NOT_FEASIBLE = 0x02,
};
enum ssam_bas_latch_status {
SSAM_BAS_LATCH_STATUS_CLOSED = 0x00,
SSAM_BAS_LATCH_STATUS_OPENED = 0x01,
SSAM_BAS_LATCH_STATUS_FAILED_TO_OPEN = 0x02,
SSAM_BAS_LATCH_STATUS_FAILED_TO_REMAIN_OPEN = 0x03,
SSAM_BAS_LATCH_STATUS_FAILED_TO_CLOSE = 0x04,
};
enum ssam_bas_cancel_reason {
SSAM_BAS_CANCEL_REASON_NOT_FEASIBLE = 0x00, /* Low battery. */
SSAM_BAS_CANCEL_REASON_TIMEOUT = 0x02,
SSAM_BAS_CANCEL_REASON_FAILED_TO_OPEN = 0x03,
SSAM_BAS_CANCEL_REASON_FAILED_TO_REMAIN_OPEN = 0x04,
SSAM_BAS_CANCEL_REASON_FAILED_TO_CLOSE = 0x05,
};
struct ssam_bas_base_info {
u8 state;
u8 base_id;
} __packed;
static_assert(sizeof(struct ssam_bas_base_info) == 2);
SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_lock, {
.target_category = SSAM_SSH_TC_BAS,
.target_id = 0x01,
.command_id = 0x06,
.instance_id = 0x00,
});
SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_unlock, {
.target_category = SSAM_SSH_TC_BAS,
.target_id = 0x01,
.command_id = 0x07,
.instance_id = 0x00,
});
SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_request, {
.target_category = SSAM_SSH_TC_BAS,
.target_id = 0x01,
.command_id = 0x08,
.instance_id = 0x00,
});
SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_confirm, {
.target_category = SSAM_SSH_TC_BAS,
.target_id = 0x01,
.command_id = 0x09,
.instance_id = 0x00,
});
SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_heartbeat, {
.target_category = SSAM_SSH_TC_BAS,
.target_id = 0x01,
.command_id = 0x0a,
.instance_id = 0x00,
});
SSAM_DEFINE_SYNC_REQUEST_N(ssam_bas_latch_cancel, {
.target_category = SSAM_SSH_TC_BAS,
.target_id = 0x01,
.command_id = 0x0b,
.instance_id = 0x00,
});
SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_base, struct ssam_bas_base_info, {
.target_category = SSAM_SSH_TC_BAS,
.target_id = 0x01,
.command_id = 0x0c,
.instance_id = 0x00,
});
SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_device_mode, u8, {
.target_category = SSAM_SSH_TC_BAS,
.target_id = 0x01,
.command_id = 0x0d,
.instance_id = 0x00,
});
SSAM_DEFINE_SYNC_REQUEST_R(ssam_bas_get_latch_status, u8, {
.target_category = SSAM_SSH_TC_BAS,
.target_id = 0x01,
.command_id = 0x11,
.instance_id = 0x00,
});
/* -- Main structures. ------------------------------------------------------ */
enum sdtx_device_state {
SDTX_DEVICE_SHUTDOWN_BIT = BIT(0),
SDTX_DEVICE_DIRTY_BASE_BIT = BIT(1),
SDTX_DEVICE_DIRTY_MODE_BIT = BIT(2),
SDTX_DEVICE_DIRTY_LATCH_BIT = BIT(3),
};
struct sdtx_device {
struct kref kref;
struct rw_semaphore lock; /* Guards device and controller reference. */
struct device *dev;
struct ssam_controller *ctrl;
unsigned long flags;
struct miscdevice mdev;
wait_queue_head_t waitq;
struct mutex write_lock; /* Guards order of events/notifications. */
struct rw_semaphore client_lock; /* Guards client list. */
struct list_head client_list;
struct delayed_work state_work;
struct {
struct ssam_bas_base_info base;
u8 device_mode;
u8 latch_status;
} state;
struct delayed_work mode_work;
struct input_dev *mode_switch;
struct ssam_event_notifier notif;
};
enum sdtx_client_state {
SDTX_CLIENT_EVENTS_ENABLED_BIT = BIT(0),
};
struct sdtx_client {
struct sdtx_device *ddev;
struct list_head node;
unsigned long flags;
struct fasync_struct *fasync;
struct mutex read_lock; /* Guards FIFO buffer read access. */
DECLARE_KFIFO(buffer, u8, 512);
};
static void __sdtx_device_release(struct kref *kref)
{
struct sdtx_device *ddev = container_of(kref, struct sdtx_device, kref);
mutex_destroy(&ddev->write_lock);
kfree(ddev);
}
static struct sdtx_device *sdtx_device_get(struct sdtx_device *ddev)
{
if (ddev)
kref_get(&ddev->kref);
return ddev;
}
static void sdtx_device_put(struct sdtx_device *ddev)
{
if (ddev)
kref_put(&ddev->kref, __sdtx_device_release);
}
/* -- Firmware value translations. ------------------------------------------ */
static u16 sdtx_translate_base_state(struct sdtx_device *ddev, u8 state)
{
switch (state) {
case SSAM_BAS_BASE_STATE_ATTACHED:
return SDTX_BASE_ATTACHED;
case SSAM_BAS_BASE_STATE_DETACH_SUCCESS:
return SDTX_BASE_DETACHED;
case SSAM_BAS_BASE_STATE_NOT_FEASIBLE:
return SDTX_DETACH_NOT_FEASIBLE;
default:
dev_err(ddev->dev, "unknown base state: %#04x\n", state);
return SDTX_UNKNOWN(state);
}
}
static u16 sdtx_translate_latch_status(struct sdtx_device *ddev, u8 status)
{
switch (status) {
case SSAM_BAS_LATCH_STATUS_CLOSED:
return SDTX_LATCH_CLOSED;
case SSAM_BAS_LATCH_STATUS_OPENED:
return SDTX_LATCH_OPENED;
case SSAM_BAS_LATCH_STATUS_FAILED_TO_OPEN:
return SDTX_ERR_FAILED_TO_OPEN;
case SSAM_BAS_LATCH_STATUS_FAILED_TO_REMAIN_OPEN:
return SDTX_ERR_FAILED_TO_REMAIN_OPEN;
case SSAM_BAS_LATCH_STATUS_FAILED_TO_CLOSE:
return SDTX_ERR_FAILED_TO_CLOSE;
default:
dev_err(ddev->dev, "unknown latch status: %#04x\n", status);
return SDTX_UNKNOWN(status);
}
}
static u16 sdtx_translate_cancel_reason(struct sdtx_device *ddev, u8 reason)
{
switch (reason) {
case SSAM_BAS_CANCEL_REASON_NOT_FEASIBLE:
return SDTX_DETACH_NOT_FEASIBLE;
case SSAM_BAS_CANCEL_REASON_TIMEOUT:
return SDTX_DETACH_TIMEDOUT;
case SSAM_BAS_CANCEL_REASON_FAILED_TO_OPEN:
return SDTX_ERR_FAILED_TO_OPEN;
case SSAM_BAS_CANCEL_REASON_FAILED_TO_REMAIN_OPEN:
return SDTX_ERR_FAILED_TO_REMAIN_OPEN;
case SSAM_BAS_CANCEL_REASON_FAILED_TO_CLOSE:
return SDTX_ERR_FAILED_TO_CLOSE;
default:
dev_err(ddev->dev, "unknown cancel reason: %#04x\n", reason);
return SDTX_UNKNOWN(reason);
}
}
/* -- IOCTLs. --------------------------------------------------------------- */
static int sdtx_ioctl_get_base_info(struct sdtx_device *ddev,
struct sdtx_base_info __user *buf)
{
struct ssam_bas_base_info raw;
struct sdtx_base_info info;
int status;
lockdep_assert_held_read(&ddev->lock);
status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &raw);
if (status < 0)
return status;
info.state = sdtx_translate_base_state(ddev, raw.state);
info.base_id = SDTX_BASE_TYPE_SSH(raw.base_id);
if (copy_to_user(buf, &info, sizeof(info)))
return -EFAULT;
return 0;
}
static int sdtx_ioctl_get_device_mode(struct sdtx_device *ddev, u16 __user *buf)
{
u8 mode;
int status;
lockdep_assert_held_read(&ddev->lock);
status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
if (status < 0)
return status;
return put_user(mode, buf);
}
static int sdtx_ioctl_get_latch_status(struct sdtx_device *ddev, u16 __user *buf)
{
u8 latch;
int status;
lockdep_assert_held_read(&ddev->lock);
status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &latch);
if (status < 0)
return status;
return put_user(sdtx_translate_latch_status(ddev, latch), buf);
}
static long __surface_dtx_ioctl(struct sdtx_client *client, unsigned int cmd, unsigned long arg)
{
struct sdtx_device *ddev = client->ddev;
lockdep_assert_held_read(&ddev->lock);
switch (cmd) {
case SDTX_IOCTL_EVENTS_ENABLE:
set_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags);
return 0;
case SDTX_IOCTL_EVENTS_DISABLE:
clear_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags);
return 0;
case SDTX_IOCTL_LATCH_LOCK:
return ssam_retry(ssam_bas_latch_lock, ddev->ctrl);
case SDTX_IOCTL_LATCH_UNLOCK:
return ssam_retry(ssam_bas_latch_unlock, ddev->ctrl);
case SDTX_IOCTL_LATCH_REQUEST:
return ssam_retry(ssam_bas_latch_request, ddev->ctrl);
case SDTX_IOCTL_LATCH_CONFIRM:
return ssam_retry(ssam_bas_latch_confirm, ddev->ctrl);
case SDTX_IOCTL_LATCH_HEARTBEAT:
return ssam_retry(ssam_bas_latch_heartbeat, ddev->ctrl);
case SDTX_IOCTL_LATCH_CANCEL:
return ssam_retry(ssam_bas_latch_cancel, ddev->ctrl);
case SDTX_IOCTL_GET_BASE_INFO:
return sdtx_ioctl_get_base_info(ddev, (struct sdtx_base_info __user *)arg);
case SDTX_IOCTL_GET_DEVICE_MODE:
return sdtx_ioctl_get_device_mode(ddev, (u16 __user *)arg);
case SDTX_IOCTL_GET_LATCH_STATUS:
return sdtx_ioctl_get_latch_status(ddev, (u16 __user *)arg);
default:
return -EINVAL;
}
}
static long surface_dtx_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
struct sdtx_client *client = file->private_data;
long status;
if (down_read_killable(&client->ddev->lock))
return -ERESTARTSYS;
if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &client->ddev->flags)) {
up_read(&client->ddev->lock);
return -ENODEV;
}
status = __surface_dtx_ioctl(client, cmd, arg);
up_read(&client->ddev->lock);
return status;
}
/* -- File operations. ------------------------------------------------------ */
static int surface_dtx_open(struct inode *inode, struct file *file)
{
struct sdtx_device *ddev = container_of(file->private_data, struct sdtx_device, mdev);
struct sdtx_client *client;
/* Initialize client. */
client = kzalloc(sizeof(*client), GFP_KERNEL);
if (!client)
return -ENOMEM;
client->ddev = sdtx_device_get(ddev);
INIT_LIST_HEAD(&client->node);
mutex_init(&client->read_lock);
INIT_KFIFO(client->buffer);
file->private_data = client;
/* Attach client. */
down_write(&ddev->client_lock);
/*
* Do not add a new client if the device has been shut down. Note that
* it's enough to hold the client_lock here as, during shutdown, we
* only acquire that lock and remove clients after marking the device
* as shut down.
*/
if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
up_write(&ddev->client_lock);
sdtx_device_put(client->ddev);
kfree(client);
return -ENODEV;
}
list_add_tail(&client->node, &ddev->client_list);
up_write(&ddev->client_lock);
stream_open(inode, file);
return 0;
}
static int surface_dtx_release(struct inode *inode, struct file *file)
{
struct sdtx_client *client = file->private_data;
/* Detach client. */
down_write(&client->ddev->client_lock);
list_del(&client->node);
up_write(&client->ddev->client_lock);
/* Free client. */
sdtx_device_put(client->ddev);
mutex_destroy(&client->read_lock);
kfree(client);
return 0;
}
static ssize_t surface_dtx_read(struct file *file, char __user *buf, size_t count, loff_t *offs)
{
struct sdtx_client *client = file->private_data;
struct sdtx_device *ddev = client->ddev;
unsigned int copied;
int status = 0;
if (down_read_killable(&ddev->lock))
return -ERESTARTSYS;
/* Make sure we're not shut down. */
if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
up_read(&ddev->lock);
return -ENODEV;
}
do {
/* Check availability, wait if necessary. */
if (kfifo_is_empty(&client->buffer)) {
up_read(&ddev->lock);
if (file->f_flags & O_NONBLOCK)
return -EAGAIN;
status = wait_event_interruptible(ddev->waitq,
!kfifo_is_empty(&client->buffer) ||
test_bit(SDTX_DEVICE_SHUTDOWN_BIT,
&ddev->flags));
if (status < 0)
return status;
if (down_read_killable(&ddev->lock))
return -ERESTARTSYS;
/* Need to check that we're not shut down again. */
if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags)) {
up_read(&ddev->lock);
return -ENODEV;
}
}
/* Try to read from FIFO. */
if (mutex_lock_interruptible(&client->read_lock)) {
up_read(&ddev->lock);
return -ERESTARTSYS;
}
status = kfifo_to_user(&client->buffer, buf, count, &copied);
mutex_unlock(&client->read_lock);
if (status < 0) {
up_read(&ddev->lock);
return status;
}
/* We might not have gotten anything, check this here. */
if (copied == 0 && (file->f_flags & O_NONBLOCK)) {
up_read(&ddev->lock);
return -EAGAIN;
}
} while (copied == 0);
up_read(&ddev->lock);
return copied;
}
static __poll_t surface_dtx_poll(struct file *file, struct poll_table_struct *pt)
{
struct sdtx_client *client = file->private_data;
__poll_t events = 0;
if (test_bit(SDTX_DEVICE_SHUTDOWN_BIT, &client->ddev->flags))
return EPOLLHUP | EPOLLERR;
poll_wait(file, &client->ddev->waitq, pt);
if (!kfifo_is_empty(&client->buffer))
events |= EPOLLIN | EPOLLRDNORM;
return events;
}
static int surface_dtx_fasync(int fd, struct file *file, int on)
{
struct sdtx_client *client = file->private_data;
return fasync_helper(fd, file, on, &client->fasync);
}
static const struct file_operations surface_dtx_fops = {
.owner = THIS_MODULE,
.open = surface_dtx_open,
.release = surface_dtx_release,
.read = surface_dtx_read,
.poll = surface_dtx_poll,
.fasync = surface_dtx_fasync,
.unlocked_ioctl = surface_dtx_ioctl,
.compat_ioctl = surface_dtx_ioctl,
.llseek = no_llseek,
};
/* -- Event handling/forwarding. -------------------------------------------- */
/*
* The device operation mode is not immediately updated on the EC when the
* base has been connected, i.e. querying the device mode inside the
* connection event callback yields an outdated value. Thus, we can only
* determine the new tablet-mode switch and device mode values after some
* time.
*
* These delays have been chosen by experimenting. We first delay on connect
* events, then check and validate the device mode against the base state and
* if invalid delay again by the "recheck" delay.
*/
#define SDTX_DEVICE_MODE_DELAY_CONNECT msecs_to_jiffies(100)
#define SDTX_DEVICE_MODE_DELAY_RECHECK msecs_to_jiffies(100)
struct sdtx_status_event {
struct sdtx_event e;
__u16 v;
} __packed;
struct sdtx_base_info_event {
struct sdtx_event e;
struct sdtx_base_info v;
} __packed;
union sdtx_generic_event {
struct sdtx_event common;
struct sdtx_status_event status;
struct sdtx_base_info_event base;
};
static void sdtx_update_device_mode(struct sdtx_device *ddev, unsigned long delay);
/* Must be executed with ddev->write_lock held. */
static void sdtx_push_event(struct sdtx_device *ddev, struct sdtx_event *evt)
{
const size_t len = sizeof(struct sdtx_event) + evt->length;
struct sdtx_client *client;
lockdep_assert_held(&ddev->write_lock);
down_read(&ddev->client_lock);
list_for_each_entry(client, &ddev->client_list, node) {
if (!test_bit(SDTX_CLIENT_EVENTS_ENABLED_BIT, &client->flags))
continue;
if (likely(kfifo_avail(&client->buffer) >= len))
kfifo_in(&client->buffer, (const u8 *)evt, len);
else
dev_warn(ddev->dev, "event buffer overrun\n");
kill_fasync(&client->fasync, SIGIO, POLL_IN);
}
up_read(&ddev->client_lock);
wake_up_interruptible(&ddev->waitq);
}
static u32 sdtx_notifier(struct ssam_event_notifier *nf, const struct ssam_event *in)
{
struct sdtx_device *ddev = container_of(nf, struct sdtx_device, notif);
union sdtx_generic_event event;
size_t len;
/* Validate event payload length. */
switch (in->command_id) {
case SAM_EVENT_CID_DTX_CONNECTION:
len = 2 * sizeof(u8);
break;
case SAM_EVENT_CID_DTX_REQUEST:
len = 0;
break;
case SAM_EVENT_CID_DTX_CANCEL:
len = sizeof(u8);
break;
case SAM_EVENT_CID_DTX_LATCH_STATUS:
len = sizeof(u8);
break;
default:
return 0;
}
if (in->length != len) {
dev_err(ddev->dev,
"unexpected payload size for event %#04x: got %u, expected %zu\n",
in->command_id, in->length, len);
return 0;
}
mutex_lock(&ddev->write_lock);
/* Translate event. */
switch (in->command_id) {
case SAM_EVENT_CID_DTX_CONNECTION:
clear_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags);
/* If state has not changed: do not send new event. */
if (ddev->state.base.state == in->data[0] &&
ddev->state.base.base_id == in->data[1])
goto out;
ddev->state.base.state = in->data[0];
ddev->state.base.base_id = in->data[1];
event.base.e.length = sizeof(struct sdtx_base_info);
event.base.e.code = SDTX_EVENT_BASE_CONNECTION;
event.base.v.state = sdtx_translate_base_state(ddev, in->data[0]);
event.base.v.base_id = SDTX_BASE_TYPE_SSH(in->data[1]);
break;
case SAM_EVENT_CID_DTX_REQUEST:
event.common.code = SDTX_EVENT_REQUEST;
event.common.length = 0;
break;
case SAM_EVENT_CID_DTX_CANCEL:
event.status.e.length = sizeof(u16);
event.status.e.code = SDTX_EVENT_CANCEL;
event.status.v = sdtx_translate_cancel_reason(ddev, in->data[0]);
break;
case SAM_EVENT_CID_DTX_LATCH_STATUS:
clear_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags);
/* If state has not changed: do not send new event. */
if (ddev->state.latch_status == in->data[0])
goto out;
ddev->state.latch_status = in->data[0];
event.status.e.length = sizeof(u16);
event.status.e.code = SDTX_EVENT_LATCH_STATUS;
event.status.v = sdtx_translate_latch_status(ddev, in->data[0]);
break;
}
sdtx_push_event(ddev, &event.common);
/* Update device mode on base connection change. */
if (in->command_id == SAM_EVENT_CID_DTX_CONNECTION) {
unsigned long delay;
delay = in->data[0] ? SDTX_DEVICE_MODE_DELAY_CONNECT : 0;
sdtx_update_device_mode(ddev, delay);
}
out:
mutex_unlock(&ddev->write_lock);
return SSAM_NOTIF_HANDLED;
}
/* -- State update functions. ----------------------------------------------- */
static bool sdtx_device_mode_invalid(u8 mode, u8 base_state)
{
return ((base_state == SSAM_BAS_BASE_STATE_ATTACHED) &&
(mode == SDTX_DEVICE_MODE_TABLET)) ||
((base_state == SSAM_BAS_BASE_STATE_DETACH_SUCCESS) &&
(mode != SDTX_DEVICE_MODE_TABLET));
}
static void sdtx_device_mode_workfn(struct work_struct *work)
{
struct sdtx_device *ddev = container_of(work, struct sdtx_device, mode_work.work);
struct sdtx_status_event event;
struct ssam_bas_base_info base;
int status, tablet;
u8 mode;
/* Get operation mode. */
status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
if (status) {
dev_err(ddev->dev, "failed to get device mode: %d\n", status);
return;
}
/* Get base info. */
status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &base);
if (status) {
dev_err(ddev->dev, "failed to get base info: %d\n", status);
return;
}
/*
* In some cases (specifically when attaching the base), the device
* mode isn't updated right away. Thus we check if the device mode
* makes sense for the given base state and try again later if it
* doesn't.
*/
if (sdtx_device_mode_invalid(mode, base.state)) {
dev_dbg(ddev->dev, "device mode is invalid, trying again\n");
sdtx_update_device_mode(ddev, SDTX_DEVICE_MODE_DELAY_RECHECK);
return;
}
mutex_lock(&ddev->write_lock);
clear_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags);
/* Avoid sending duplicate device-mode events. */
if (ddev->state.device_mode == mode) {
mutex_unlock(&ddev->write_lock);
return;
}
ddev->state.device_mode = mode;
event.e.length = sizeof(u16);
event.e.code = SDTX_EVENT_DEVICE_MODE;
event.v = mode;
sdtx_push_event(ddev, &event.e);
/* Send SW_TABLET_MODE event. */
tablet = mode != SDTX_DEVICE_MODE_LAPTOP;
input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet);
input_sync(ddev->mode_switch);
mutex_unlock(&ddev->write_lock);
}
static void sdtx_update_device_mode(struct sdtx_device *ddev, unsigned long delay)
{
schedule_delayed_work(&ddev->mode_work, delay);
}
/* Must be executed with ddev->write_lock held. */
static void __sdtx_device_state_update_base(struct sdtx_device *ddev,
struct ssam_bas_base_info info)
{
struct sdtx_base_info_event event;
lockdep_assert_held(&ddev->write_lock);
/* Prevent duplicate events. */
if (ddev->state.base.state == info.state &&
ddev->state.base.base_id == info.base_id)
return;
ddev->state.base = info;
event.e.length = sizeof(struct sdtx_base_info);
event.e.code = SDTX_EVENT_BASE_CONNECTION;
event.v.state = sdtx_translate_base_state(ddev, info.state);
event.v.base_id = SDTX_BASE_TYPE_SSH(info.base_id);
sdtx_push_event(ddev, &event.e);
}
/* Must be executed with ddev->write_lock held. */
static void __sdtx_device_state_update_mode(struct sdtx_device *ddev, u8 mode)
{
struct sdtx_status_event event;
int tablet;
/*
* Note: This function must be called after updating the base state
* via __sdtx_device_state_update_base(), as we rely on the updated
* base state value in the validity check below.
*/
lockdep_assert_held(&ddev->write_lock);
if (sdtx_device_mode_invalid(mode, ddev->state.base.state)) {
dev_dbg(ddev->dev, "device mode is invalid, trying again\n");
sdtx_update_device_mode(ddev, SDTX_DEVICE_MODE_DELAY_RECHECK);
return;
}
/* Prevent duplicate events. */
if (ddev->state.device_mode == mode)
return;
ddev->state.device_mode = mode;
/* Send event. */
event.e.length = sizeof(u16);
event.e.code = SDTX_EVENT_DEVICE_MODE;
event.v = mode;
sdtx_push_event(ddev, &event.e);
/* Send SW_TABLET_MODE event. */
tablet = mode != SDTX_DEVICE_MODE_LAPTOP;
input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet);
input_sync(ddev->mode_switch);
}
/* Must be executed with ddev->write_lock held. */
static void __sdtx_device_state_update_latch(struct sdtx_device *ddev, u8 status)
{
struct sdtx_status_event event;
lockdep_assert_held(&ddev->write_lock);
/* Prevent duplicate events. */
if (ddev->state.latch_status == status)
return;
ddev->state.latch_status = status;
event.e.length = sizeof(struct sdtx_base_info);
event.e.code = SDTX_EVENT_BASE_CONNECTION;
event.v = sdtx_translate_latch_status(ddev, status);
sdtx_push_event(ddev, &event.e);
}
static void sdtx_device_state_workfn(struct work_struct *work)
{
struct sdtx_device *ddev = container_of(work, struct sdtx_device, state_work.work);
struct ssam_bas_base_info base;
u8 mode, latch;
int status;
/* Mark everything as dirty. */
set_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags);
set_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags);
set_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags);
/*
* Ensure that the state gets marked as dirty before continuing to
* query it. Necessary to ensure that clear_bit() calls in
* sdtx_notifier() and sdtx_device_mode_workfn() actually clear these
* bits if an event is received while updating the state here.
*/
smp_mb__after_atomic();
status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &base);
if (status) {
dev_err(ddev->dev, "failed to get base state: %d\n", status);
return;
}
status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &mode);
if (status) {
dev_err(ddev->dev, "failed to get device mode: %d\n", status);
return;
}
status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &latch);
if (status) {
dev_err(ddev->dev, "failed to get latch status: %d\n", status);
return;
}
mutex_lock(&ddev->write_lock);
/*
* If the respective dirty-bit has been cleared, an event has been
* received, updating this state. The queried state may thus be out of
* date. At this point, we can safely assume that the state provided
* by the event is either up to date, or we're about to receive
* another event updating it.
*/
if (test_and_clear_bit(SDTX_DEVICE_DIRTY_BASE_BIT, &ddev->flags))
__sdtx_device_state_update_base(ddev, base);
if (test_and_clear_bit(SDTX_DEVICE_DIRTY_MODE_BIT, &ddev->flags))
__sdtx_device_state_update_mode(ddev, mode);
if (test_and_clear_bit(SDTX_DEVICE_DIRTY_LATCH_BIT, &ddev->flags))
__sdtx_device_state_update_latch(ddev, latch);
mutex_unlock(&ddev->write_lock);
}
static void sdtx_update_device_state(struct sdtx_device *ddev, unsigned long delay)
{
schedule_delayed_work(&ddev->state_work, delay);
}
/* -- Common device initialization. ----------------------------------------- */
static int sdtx_device_init(struct sdtx_device *ddev, struct device *dev,
struct ssam_controller *ctrl)
{
int status, tablet_mode;
/* Basic initialization. */
kref_init(&ddev->kref);
init_rwsem(&ddev->lock);
ddev->dev = dev;
ddev->ctrl = ctrl;
ddev->mdev.minor = MISC_DYNAMIC_MINOR;
ddev->mdev.name = "surface_dtx";
ddev->mdev.nodename = "surface/dtx";
ddev->mdev.fops = &surface_dtx_fops;
ddev->notif.base.priority = 1;
ddev->notif.base.fn = sdtx_notifier;
ddev->notif.event.reg = SSAM_EVENT_REGISTRY_SAM;
ddev->notif.event.id.target_category = SSAM_SSH_TC_BAS;
ddev->notif.event.id.instance = 0;
ddev->notif.event.mask = SSAM_EVENT_MASK_NONE;
ddev->notif.event.flags = SSAM_EVENT_SEQUENCED;
init_waitqueue_head(&ddev->waitq);
mutex_init(&ddev->write_lock);
init_rwsem(&ddev->client_lock);
INIT_LIST_HEAD(&ddev->client_list);
INIT_DELAYED_WORK(&ddev->mode_work, sdtx_device_mode_workfn);
INIT_DELAYED_WORK(&ddev->state_work, sdtx_device_state_workfn);
/*
* Get current device state. We want to guarantee that events are only
* sent when state actually changes. Thus we cannot use special
* "uninitialized" values, as that would cause problems when manually
* querying the state in surface_dtx_pm_complete(). I.e. we would not
* be able to detect state changes there if no change event has been
* received between driver initialization and first device suspension.
*
* Note that we also need to do this before registering the event
* notifier, as that may access the state values.
*/
status = ssam_retry(ssam_bas_get_base, ddev->ctrl, &ddev->state.base);
if (status)
return status;
status = ssam_retry(ssam_bas_get_device_mode, ddev->ctrl, &ddev->state.device_mode);
if (status)
return status;
status = ssam_retry(ssam_bas_get_latch_status, ddev->ctrl, &ddev->state.latch_status);
if (status)
return status;
/* Set up tablet mode switch. */
ddev->mode_switch = input_allocate_device();
if (!ddev->mode_switch)
return -ENOMEM;
ddev->mode_switch->name = "Microsoft Surface DTX Device Mode Switch";
ddev->mode_switch->phys = "ssam/01:11:01:00:00/input0";
ddev->mode_switch->id.bustype = BUS_HOST;
ddev->mode_switch->dev.parent = ddev->dev;
tablet_mode = (ddev->state.device_mode != SDTX_DEVICE_MODE_LAPTOP);
input_set_capability(ddev->mode_switch, EV_SW, SW_TABLET_MODE);
input_report_switch(ddev->mode_switch, SW_TABLET_MODE, tablet_mode);
status = input_register_device(ddev->mode_switch);
if (status) {
input_free_device(ddev->mode_switch);
return status;
}
/* Set up event notifier. */
status = ssam_notifier_register(ddev->ctrl, &ddev->notif);
if (status)
goto err_notif;
/* Register miscdevice. */
status = misc_register(&ddev->mdev);
if (status)
goto err_mdev;
/*
* Update device state in case it has changed between getting the
* initial mode and registering the event notifier.
*/
sdtx_update_device_state(ddev, 0);
return 0;
err_notif:
ssam_notifier_unregister(ddev->ctrl, &ddev->notif);
cancel_delayed_work_sync(&ddev->mode_work);
err_mdev:
input_unregister_device(ddev->mode_switch);
return status;
}
static struct sdtx_device *sdtx_device_create(struct device *dev, struct ssam_controller *ctrl)
{
struct sdtx_device *ddev;
int status;
ddev = kzalloc(sizeof(*ddev), GFP_KERNEL);
if (!ddev)
return ERR_PTR(-ENOMEM);
status = sdtx_device_init(ddev, dev, ctrl);
if (status) {
sdtx_device_put(ddev);
return ERR_PTR(status);
}
return ddev;
}
static void sdtx_device_destroy(struct sdtx_device *ddev)
{
struct sdtx_client *client;
/*
* Mark device as shut-down. Prevent new clients from being added and
* new operations from being executed.
*/
set_bit(SDTX_DEVICE_SHUTDOWN_BIT, &ddev->flags);
/* Disable notifiers, prevent new events from arriving. */
ssam_notifier_unregister(ddev->ctrl, &ddev->notif);
/* Stop mode_work, prevent access to mode_switch. */
cancel_delayed_work_sync(&ddev->mode_work);
/* Stop state_work. */
cancel_delayed_work_sync(&ddev->state_work);
/* With mode_work canceled, we can unregister the mode_switch. */
input_unregister_device(ddev->mode_switch);
/* Wake up async clients. */
down_write(&ddev->client_lock);
list_for_each_entry(client, &ddev->client_list, node) {
kill_fasync(&client->fasync, SIGIO, POLL_HUP);
}
up_write(&ddev->client_lock);
/* Wake up blocking clients. */
wake_up_interruptible(&ddev->waitq);
/*
* Wait for clients to finish their current operation. After this, the
* controller and device references are guaranteed to be no longer in
* use.
*/
down_write(&ddev->lock);
ddev->dev = NULL;
ddev->ctrl = NULL;
up_write(&ddev->lock);
/* Finally remove the misc-device. */
misc_deregister(&ddev->mdev);
/*
* We're now guaranteed that sdtx_device_open() won't be called any
* more, so we can now drop out reference.
*/
sdtx_device_put(ddev);
}
/* -- PM ops. --------------------------------------------------------------- */
#ifdef CONFIG_PM_SLEEP
static void surface_dtx_pm_complete(struct device *dev)
{
struct sdtx_device *ddev = dev_get_drvdata(dev);
/*
* Normally, the EC will store events while suspended (i.e. in
* display-off state) and release them when resumed (i.e. transitioned
* to display-on state). During hibernation, however, the EC will be
* shut down and does not store events. Furthermore, events might be
* dropped during prolonged suspension (it is currently unknown how
* big this event buffer is and how it behaves on overruns).
*
* To prevent any problems, we update the device state here. We do
* this delayed to ensure that any events sent by the EC directly
* after resuming will be handled first. The delay below has been
* chosen (experimentally), so that there should be ample time for
* these events to be handled, before we check and, if necessary,
* update the state.
*/
sdtx_update_device_state(ddev, msecs_to_jiffies(1000));
}
static const struct dev_pm_ops surface_dtx_pm_ops = {
.complete = surface_dtx_pm_complete,
};
#else /* CONFIG_PM_SLEEP */
static const struct dev_pm_ops surface_dtx_pm_ops = {};
#endif /* CONFIG_PM_SLEEP */
/* -- Platform driver. ------------------------------------------------------ */
static int surface_dtx_platform_probe(struct platform_device *pdev)
{
struct ssam_controller *ctrl;
struct sdtx_device *ddev;
/* Link to EC. */
ctrl = ssam_client_bind(&pdev->dev);
if (IS_ERR(ctrl))
return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
ddev = sdtx_device_create(&pdev->dev, ctrl);
if (IS_ERR(ddev))
return PTR_ERR(ddev);
platform_set_drvdata(pdev, ddev);
return 0;
}
static int surface_dtx_platform_remove(struct platform_device *pdev)
{
sdtx_device_destroy(platform_get_drvdata(pdev));
return 0;
}
static const struct acpi_device_id surface_dtx_acpi_match[] = {
{ "MSHW0133", 0 },
{ },
};
MODULE_DEVICE_TABLE(acpi, surface_dtx_acpi_match);
static struct platform_driver surface_dtx_platform_driver = {
.probe = surface_dtx_platform_probe,
.remove = surface_dtx_platform_remove,
.driver = {
.name = "surface_dtx_pltf",
.acpi_match_table = surface_dtx_acpi_match,
.pm = &surface_dtx_pm_ops,
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
},
};
/* -- SSAM device driver. --------------------------------------------------- */
#ifdef CONFIG_SURFACE_AGGREGATOR_BUS
static int surface_dtx_ssam_probe(struct ssam_device *sdev)
{
struct sdtx_device *ddev;
ddev = sdtx_device_create(&sdev->dev, sdev->ctrl);
if (IS_ERR(ddev))
return PTR_ERR(ddev);
ssam_device_set_drvdata(sdev, ddev);
return 0;
}
static void surface_dtx_ssam_remove(struct ssam_device *sdev)
{
sdtx_device_destroy(ssam_device_get_drvdata(sdev));
}
static const struct ssam_device_id surface_dtx_ssam_match[] = {
{ SSAM_SDEV(BAS, 0x01, 0x00, 0x00) },
{ },
};
MODULE_DEVICE_TABLE(ssam, surface_dtx_ssam_match);
static struct ssam_device_driver surface_dtx_ssam_driver = {
.probe = surface_dtx_ssam_probe,
.remove = surface_dtx_ssam_remove,
.match_table = surface_dtx_ssam_match,
.driver = {
.name = "surface_dtx",
.pm = &surface_dtx_pm_ops,
.probe_type = PROBE_PREFER_ASYNCHRONOUS,
},
};
static int ssam_dtx_driver_register(void)
{
return ssam_device_driver_register(&surface_dtx_ssam_driver);
}
static void ssam_dtx_driver_unregister(void)
{
ssam_device_driver_unregister(&surface_dtx_ssam_driver);
}
#else /* CONFIG_SURFACE_AGGREGATOR_BUS */
static int ssam_dtx_driver_register(void)
{
return 0;
}
static void ssam_dtx_driver_unregister(void)
{
}
#endif /* CONFIG_SURFACE_AGGREGATOR_BUS */
/* -- Module setup. --------------------------------------------------------- */
static int __init surface_dtx_init(void)
{
int status;
status = ssam_dtx_driver_register();
if (status)
return status;
status = platform_driver_register(&surface_dtx_platform_driver);
if (status)
ssam_dtx_driver_unregister();
return status;
}
module_init(surface_dtx_init);
static void __exit surface_dtx_exit(void)
{
platform_driver_unregister(&surface_dtx_platform_driver);
ssam_dtx_driver_unregister();
}
module_exit(surface_dtx_exit);
MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
MODULE_DESCRIPTION("Detachment-system driver for Surface System Aggregator Module");
MODULE_LICENSE("GPL");
|