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
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
|
/*
* MHL3 HID Tunneling implementation
*
* Copyright (c) 2013-2014 Lee Mulcahy <william.mulcahy@siliconimage.com>
* Copyright (c) 2013-2014 Silicon Image, Inc
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation version 2.
* This program is distributed AS-IS WITHOUT ANY WARRANTY of any
* kind, whether express or implied; INCLUDING without the implied warranty
* of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE or NON-INFRINGEMENT.
* See the GNU General Public License for more details at
* http://www.gnu.org/licenses/gpl-2.0.html.
*
* This code is inspired by the "HID over I2C protocol implementation"
*
* Copyright (c) 2012 Benjamin Tissoires <benjamin.tissoires@gmail.com>
* Copyright (c) 2012 Ecole Nationale de l'Aviation Civile, France
* Copyright (c) 2012 Red Hat, Inc
*
* and the "USB HID support for Linux"
*
* Copyright (c) 1999 Andreas Gal
* Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
* Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
* Copyright (c) 2007-2008 Oliver Neukum
* Copyright (c) 2006-2010 Jiri Kosina
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING in the main directory of this archive for
* more details.
*/
#include <linux/kernel.h>
#include <linux/semaphore.h>
#include <linux/cdev.h>
#include <linux/gpio.h>
#include <linux/device.h>
#include <linux/delay.h>
#include <linux/hrtimer.h>
#include <linux/export.h>
#include <linux/module.h>
#include <linux/hid.h>
#include <linux/workqueue.h>
#include "si_fw_macros.h"
#include "si_app_devcap.h"
#include "si_infoframe.h"
#include "si_edid.h"
#include "si_mhl_defs.h"
#include "si_mhl2_edid_3d_api.h"
#include "si_8620_internal_api.h"
#include "si_mhl_tx_hw_drv_api.h"
#ifdef MEDIA_DATA_TUNNEL_SUPPORT
#include "si_mdt_inputdev.h"
#endif
#include "si_mhl_callback_api.h"
#include "si_8620_drv.h"
#include "mhl_linux_tx.h"
#include "si_8620_regs.h"
#include "mhl_supp.h"
#include "platform.h"
#include "si_emsc_hid.h"
#define EMSC_RCV_MSG_START 0 /* First fragment of message. */
#define EMSC_RCV_MSG_NEXT 1 /* Subsequent fragment of message. */
void dump_array(int level, char *ptitle, uint8_t *pdata, int count)
{
int i, buf_offset;
int bufsize = 128;
char *buf;
if (level > debug_level)
return;
if (count > 1) {
bufsize += count * 3;
bufsize += ((count / 16) + 1) * 8;
}
buf = kmalloc(bufsize, GFP_KERNEL);
if (!buf)
return;
buf_offset = 0;
if (ptitle)
buf_offset = scnprintf(&buf[0], bufsize,
"%s (%d bytes):", ptitle, count);
for (i = 0; i < count; i++) {
if ((i & 0x0F) == 0)
buf_offset += scnprintf(&buf[buf_offset],
bufsize - buf_offset, "\n%04X: ", i);
buf_offset += scnprintf(&buf[buf_offset],
bufsize - buf_offset, "%02X ", pdata[i]);
}
buf_offset += scnprintf(&buf[buf_offset], bufsize - buf_offset, "\n");
print_formatted_debug_msg(NULL, NULL, -1, buf);
kfree(buf);
}
/*
* Send Host role request or relinquish it. This should be called with
* MHL_RHID_REQUEST_HOST for a source during MHL connection and when the
* want_host flag is set (after the sink has relinquished the Host role that
* we let them have by sending MHL_RHID_RELIQUISH_HOST).
*/
void mhl_tx_hid_host_role_request(struct mhl_dev_context *context, int request)
{
MHL3_HID_DBG_INFO("RHID: Sending HID Host role %s message\n",
(request == MHL_RHID_REQUEST_HOST) ?
"REQUEST" : "RELINQUISH");
/* During a request, we are in limbo */
context->mhl_ghid.is_host = false;
context->mhl_ghid.is_device = false;
context->mhl_ghid.want_host = false;
si_mhl_tx_send_msc_msg(context, MHL_MSC_MSG_RHID, request);
}
/*
* Handle host-device negotiation request messages received from peer.
*
* A sink requesting Host role is typically just trying to determine
* if the source has already requested the Host role, which is required
* of the sink before starting the Device role (14.3.1.1).
* As a source, we should have sent a host role request to the sink
* before this (dev_context->mhl_ghid.is_host == true), so host
* requests should be refused.
*
* When the sink relinquishes the Host role (assuming we let the sink have
* it for some reason), we immediately want it back.
*/
void mhl_tx_hid_host_negotiation(struct mhl_dev_context *mdev)
{
uint8_t rhidk_status = MHL_RHID_NO_ERR;
if (mdev->msc_msg_sub_command == MHL_MSC_MSG_RHID) {
if (mdev->msc_msg_data == MHL_RHID_REQUEST_HOST) {
if (mdev->mhl_ghid.is_host)
rhidk_status = MHL_RHID_DENY;
} else if (mdev->msc_msg_data == MHL_RHID_RELINQUISH_HOST) {
mdev->mhl_ghid.want_host = true;
} else {
rhidk_status = MHL_RHID_INVALID;
}
MHL3_HID_DBG_INFO(
"RHID: Received HID Host role %s result: %d\n",
(mdev->msc_msg_data == MHL_RHID_REQUEST_HOST) ?
"REQUEST" : "RELINQUISH", rhidk_status);
/* Always RHIDK to the peer */
si_mhl_tx_send_msc_msg(mdev, MHL_MSC_MSG_RHIDK, rhidk_status);
} else if (mdev->msc_msg_sub_command == MHL_MSC_MSG_RHIDK) {
if (mdev->msc_msg_data == MHL_RHID_NO_ERR) {
if (mdev->msc_msg_last_data == MHL_RHID_REQUEST_HOST) {
mdev->mhl_ghid.is_host = true;
mdev->mhl_ghid.is_device = false;
MHL3_HID_DBG_INFO("HID HOST role granted\n");
} else {
mdev->mhl_ghid.is_host = false;
mdev->mhl_ghid.is_device = true;
MHL3_HID_DBG_INFO("HID DEVICE role granted\n");
}
} else if (mdev->msc_msg_last_data == MHL_RHID_REQUEST_HOST) {
mdev->mhl_ghid.is_host = false;
mdev->mhl_ghid.is_device = true;
MHL3_HID_DBG_INFO("HID HOST role DENIED\n");
}
}
}
/*
* Add a HID message to the eMSC output queue using one or more
* eMSC BLOCK commands.
*/
static int si_mhl_tx_emsc_add_hid_message(struct mhl_dev_context *mdev,
uint8_t hb0, uint8_t hb1, uint8_t *msg, int msg_len)
{
int i, cmd_size, fragment_count, msg_index, index;
uint8_t *payload;
uint8_t payload_size;
uint16_t accum;
uint16_t *pchksum;
bool first_fragment;
MHL3_HID_DBG_INFO("called\n");
/* If message exceeds one (empty) BLOCK command buffer, we must
* break it into fragments. Since the first fragment will be
* the full command buffer size, the current request (if any) will be
* sent before starting to add this one.
*/
i = msg_len + HID_MSG_HEADER_LEN + HID_MSG_CHKSUM_LEN;
fragment_count = i / HID_FRAG_LEN_MAX;
if ((fragment_count * HID_FRAG_LEN_MAX) != i)
fragment_count++;
/* This time don't include the standard header in the message length. */
cmd_size =
HID_BURST_ID_LEN +
HID_FRAG_HEADER_LEN +
HID_MSG_HEADER_LEN +
HID_MSG_CHKSUM_LEN +
msg_len;
first_fragment = true;
msg_index = 0;
index = 0;
accum = 0;
/*
* TODO: Need to make sure there will be enough buffers
* for the fragments.
*/
index = 0;
while (fragment_count > 0) {
payload_size = (cmd_size > EMSC_BLK_CMD_MAX_LEN) ?
EMSC_BLK_CMD_MAX_LEN : cmd_size;
/* TODO: Need up to 17 buffers (do we have them?) */
payload = (uint8_t *)si_mhl_tx_get_sub_payload_buffer(
mdev, payload_size);
if (payload == NULL) {
MHL3_HID_DBG_ERR(
"%ssi_mhl_tx_get_sub_payload_buffer failed%s\n",
ANSI_ESC_RED_TEXT, ANSI_ESC_RESET_TEXT);
/*
* TODO: Should be handled with an error code and
* exit, but need to clean up any buffers that may
* have been successfully allocated.
*/
} else {
payload[index++] = (uint8_t)(burst_id_HID_PAYLOAD >> 8);
payload[index++] = (uint8_t)(burst_id_HID_PAYLOAD);
payload[index++] = payload_size - HID_BURST_ID_LEN;
payload[index++] = fragment_count - 1;
payload_size -=
(HID_BURST_ID_LEN + HID_FRAG_HEADER_LEN);
if (fragment_count == 1)
payload_size -= HID_MSG_CHKSUM_LEN;
if (first_fragment) {
first_fragment = false;
payload[index++] = hb0;
payload[index++] = hb1;
payload_size -= HID_MSG_HEADER_LEN;
accum = ((((uint16_t)hb1) << 8) | hb0);
pchksum = (uint16_t *)&msg[0];
for (i = 0; i < (msg_len / 2); i++)
accum += pchksum[i];
if (msg_len & 0x01)
accum += ((uint16_t)msg[msg_len - 1]);
accum += (msg_len + HID_MSG_HEADER_LEN);
}
memcpy(&payload[index], &msg[msg_index], payload_size);
msg_index += payload_size;
index += payload_size;
if (fragment_count == 1) {
payload[index++] = (uint8_t)accum;
payload[index++] = (uint8_t)(accum >> 8);
}
}
fragment_count--;
}
return 0;
}
/*
* Build a HID tunneling message and send it.
* Returns non-zero if an error occurred, such as the device was
* disconnected.
* Always use the CTRL channel to send messages from the HOST.
*
* This function should be called wrapped in an isr_lock semaphore pair UNLESS
* it is being called from the Titan interrupt handler.
*/
static int send_hid_msg(struct mhl3_hid_data *mhid, int outlen, bool want_ack)
{
struct mhl_dev_context *mdev = mhid->context;
uint8_t hb0, hb1;
int ret;
hb0 = mhid->id << 4;
if (want_ack) {
hb1 = mhid->msg_count[0] | EMSC_HID_HB1_ACK;
mhid->msg_count[0] =
(mhid->msg_count[0] + 1) & EMSC_HID_HB1_MSG_CNT_FLD;
}
hb1 = mhid->msg_count[0] | (want_ack ? EMSC_HID_HB1_ACK : 0x00);
mhid->msg_count[0] =
(mhid->msg_count[0] + 1) & EMSC_HID_HB1_MSG_CNT_FLD;
ret = si_mhl_tx_emsc_add_hid_message(mdev, hb0, hb1,
mhid->out_data, outlen);
if (ret == 0)
si_mhl_tx_push_block_transactions(mdev);
return ret;
}
/*
* Send an MHL3 HID Command and wait for a response.
* called only from a WORK QUEUE function; Do NOT call from
* the Titan interrupt context.
* Returns negative if an error occurred, such as the device was
* disconnected, otherwise returns the number of bytes
* received.
*/
static int send_hid_wait(struct mhl3_hid_data *mhid,
int outlen, uint8_t *pin, int inlen, bool want_ack)
{
struct mhl_dev_context *context = mhid->context;
int count, ret;
/* If work is not active, we're being canceled. */
if (!mhid->hid_work_active)
return -ENODEV;
/*
* Take a hold of the wait lock. It will be released
* when the response is received. This down call will be
* released by an up call in the hid_message_processor()
* function when the response message has been received. Not
* conventional, but until I think of a better way,
* this is it.
*/
if (down_timeout(&mhid->data_wait_lock, 1*HZ)) {
MHL3_HID_DBG_ERR("Could not acquire data_wait lock !!!\n");
return -ENODEV;
}
MHL3_HID_DBG_INFO("Acquired data_wait_lock\n");
/*
* Send the message when the Titan ISR is not active so
* that we don't deadlock with eMsc block buffer allocation.
*/
if (down_interruptible(&context->isr_lock)) {
MHL3_HID_DBG_ERR(
"Could not acquire isr_lock for HID work queue\n");
ret = -ERESTARTSYS;
goto done_release;
}
ret = send_hid_msg(mhid, outlen, want_ack);
up(&context->isr_lock);
if (ret == 0) {
/* If work is not active, we're being canceled. */
if (!mhid->hid_work_active) {
ret = -ENODEV;
goto done_release;
}
/* Wait until a message is ready (when the driver unblocks). */
/* TODO: Reduce this timeout value */
if (down_timeout(&mhid->data_wait_lock, 15*HZ)) {
MHL3_HID_DBG_ERR(
"Timed out waiting for data_wait semaphore!\n");
ret = -EBUSY;
/*
* As odd as it may seem, we must release the semaphore
* that we were waiting for because something
* apparently has gone wrong with the communications
* protocol and we need to start over.
*/
goto done_release;
}
/* Intercept HID_ACK{NO_DEV} messages. */
if ((mhid->in_data[0] == MHL3_HID_ACK) &&
(mhid->in_data[1] == HID_ACK_NODEV)) {
ret = -ENODEV;
goto done_release;
}
/* Message has been placed in our input buffer. */
count = (mhid->in_data_length > inlen) ?
inlen : mhid->in_data_length;
memcpy(pin, mhid->in_data, count);
ret = count;
}
done_release:
up(&mhid->data_wait_lock);
return ret;
}
/*
* Message protocol level ACK packet, as opposed to the HID_ACK message.
*/
static int send_ack_packet(struct mhl_dev_context *mdev,
uint8_t hb0, uint8_t hb1)
{
uint8_t *payload;
payload = (uint8_t *)si_mhl_tx_get_sub_payload_buffer(
mdev, HID_BURST_ID_LEN + HID_ACK_PACKET_LEN);
if (payload == NULL) {
MHL3_HID_DBG_ERR(
"%ssi_mhl_tx_get_sub_payload_buffer failed%s\n",
ANSI_ESC_RED_TEXT, ANSI_ESC_RESET_TEXT);
/*
* TODO: Should be handled with an error code and
* exit, but need to clean up any buffers that may
* have been successfully allocated.
*/
} else {
payload[0] = (uint8_t)(burst_id_HID_PAYLOAD >> 8);
payload[1] = (uint8_t)(burst_id_HID_PAYLOAD);
payload[2] = HID_ACK_PACKET_LEN;
payload[3] = hb1 | 0x80;
payload[4] = hb0;
}
return 0;
}
/*
* Send a HID_ACK message with the passed error code from the interrupt
* context without using the mhid device structure (in case it's not there).
*/
static int mhl3_int_send_ack(struct mhl_dev_context *mdev,
int ret, uint8_t hb0)
{
uint8_t out_data[2];
MHL3_HID_DBG_WARN("%s - HID_ACK reason code: %02X\n", __func__, ret);
out_data[0] = MHL3_HID_ACK;
out_data[1] = (ret < 0) ? HID_ACK_NODEV : ret;
ret = si_mhl_tx_emsc_add_hid_message(mdev, hb0, 0, out_data, 2);
if (ret < 0)
MHL3_HID_DBG_ERR("MHID: Failed to send HID_ACK to device.\n");
return ret;
}
/*
* Send a HID_ACK message with the passed error code.
*/
static int mhl3_send_ack(struct mhl3_hid_data *mhid, int ret)
{
struct mhl_dev_context *context = mhid->context;
MHL3_HID_DBG_WARN("%s - HID_ACK reason code: %02X\n", __func__, ret);
mhid->out_data[0] = MHL3_HID_ACK;
mhid->out_data[1] = (ret < 0) ? HID_ACK_NODEV : ret;
/*
* Send the message when the Titan ISR is not active so
* that we don't deadlock with eMsc block buffer allocation.
*/
if (down_interruptible(&context->isr_lock)) {
MHL3_HID_DBG_ERR("Could not acquire isr_lock for HID work\n");
return -ERESTARTSYS;
}
ret = send_hid_msg(mhid, 2, false);
up(&context->isr_lock);
if (ret < 0)
MHL3_HID_DBG_ERR("Failed to send a HID_ACK to device.\n");
return ret;
}
/*
* Called from mhl3_hid_get_raw_report() and mhl3_hid_init_report()
* to get report data from the device.
*
* It CANNOT be called from the Titan driver interrupt context, because
* it blocks until it gets a response FROM the Titan interrupt.
*/
static int mhl3_hid_get_report(struct mhl3_hid_data *mhid, u8 report_type,
u8 report_id, unsigned char *pin, int inlen)
{
int ret = 0;
MHL3_HID_DBG_INFO("%s\n", __func__);
/* Build MHL3_GET_REPORT message and add it to the out queue. */
mhid->out_data[0] = MHL3_GET_REPORT;
mhid->out_data[1] = report_type;
mhid->out_data[2] = report_id;
ret = send_hid_wait(mhid, 3, pin, inlen, false);
if (ret < 0)
MHL3_HID_DBG_ERR("Failed to retrieve report from device.\n");
return ret;
}
/*
* Called from mhl3_hid_output_raw_report() to send report data to
* the device.
*/
static int mhl3_hid_set_report(struct mhl3_hid_data *mhid, u8 report_type,
u8 report_id, unsigned char *pout, size_t outlen)
{
struct mhl_dev_context *context = mhid->context;
int ret = 0;
MHL3_HID_DBG_INFO("%s\n", __func__);
mhid->out_data[0] = MHL3_SET_REPORT;
mhid->out_data[1] = report_type;
mhid->out_data[2] = report_id;
memcpy(&mhid->out_data[3], pout, outlen);
/* TODO: Need to make sure that this function is never called from
* the Titan ISR (through linkage from a HID function called
* from a HID report response or some such). Just needs some
* research....
*/
/*
* Send the message when the Titan ISR is not active so
* that we don't deadlock with eMsc block buffer allocation.
*/
if (down_interruptible(&context->isr_lock)) {
MHL3_HID_DBG_ERR("Could not acquire isr_lock for HID work\n");
return -ERESTARTSYS;
}
ret = send_hid_msg(mhid, outlen + 3, false);
up(&context->isr_lock);
if (ret) {
MHL3_HID_DBG_ERR("Failed to set a report to device.\n");
return ret;
}
return outlen + 3;
}
/*
* TODO: Doesn't do anything yet
*/
static int mhl3_hid_set_power(struct mhl_dev_context *context, int power_state)
{
/* struct mhl3_hid_data *mhid = context->mhl_hid; */
int ret = 0;
MHL3_HID_DBG_INFO("%s\n", __func__);
return ret;
}
static void mhl3_hid_free_buffers(struct mhl3_hid_data *mhid)
{
kfree(mhid->hdesc);
mhid->hdesc = NULL;
kfree(mhid->in_report_buf);
mhid->in_report_buf = NULL;
}
static int mhl3_hid_alloc_buffers(struct mhl3_hid_data *mhid,
size_t report_size)
{
mhid->in_report_buf = kzalloc(report_size, GFP_KERNEL);
if (mhid->in_report_buf == NULL) {
mhl3_hid_free_buffers(mhid);
return -ENOMEM;
}
mhid->bufsize = report_size;
return 0;
}
/*
* Called from the HID driver to obtain raw report data from the
* device.
*
* It is not called from an interrupt context, so it is OK to block
* until a reply is given.
*/
static int mhl3_hid_get_raw_report(struct hid_device *hid,
unsigned char report_number, __u8 *buf,
size_t count, unsigned char report_type)
{
struct mhl3_hid_data *mhid = hid->driver_data;
int ret;
MHL3_HID_DBG_INFO("%s\n", __func__);
if (report_type == HID_OUTPUT_REPORT)
return -EINVAL;
ret = mhl3_hid_get_report(mhid,
report_type == HID_FEATURE_REPORT ? 0x03 : 0x01,
report_number, buf, count);
return ret;
}
/*
* Called from the HID driver to send report data to the device.
*/
static int mhl3_hid_output_raw_report(struct hid_device *hid, __u8 *buf,
size_t count, unsigned char report_type)
{
struct mhl3_hid_data *mhid = hid->driver_data;
int report_id = buf[0];
int ret;
MHL3_HID_DBG_INFO("%s\n", __func__);
if (report_type == HID_INPUT_REPORT)
return -EINVAL;
if (report_id) {
buf++;
count--;
}
ret = mhl3_hid_set_report(mhid,
report_type == HID_FEATURE_REPORT ? 0x03 : 0x02,
report_id, buf, count);
if (report_id && ret >= 0)
ret++; /* add report_id to the number of transfered bytes */
return ret;
}
static int mhl3_hid_get_report_length(struct hid_report *report)
{
return ((report->size - 1) >> 3) + 1 +
report->device->report_enum[report->type].numbered + 2;
}
/*
* Traverse the supplied list of reports and find the longest
*/
static void mhl3_hid_find_max_report(struct hid_device *hid, unsigned int type,
unsigned int *max)
{
struct hid_report *report;
unsigned int size;
/*
* We should not rely on wMaxInputLength, as some devices may set
* it to a wrong length.
*/
list_for_each_entry(report, &hid->report_enum[type].report_list, list) {
size = mhl3_hid_get_report_length(report);
if (*max < size)
*max = size;
}
}
/*
*
*/
#if 0
static void mhl3_hid_init_report(struct hid_report *report, u8 *buffer,
size_t bufsize)
{
struct hid_device *hid = report->device;
struct mhl3_hid_data *mhid = hid->driver_data;
unsigned int size, ret_size;
size = mhl3_hid_get_report_length(report);
if (mhl3_hid_get_report(mhid,
report->type == HID_FEATURE_REPORT ? 0x03 : 0x01,
report->id, buffer, size))
return;
ret_size = buffer[0] | (buffer[1] << 8);
if (ret_size != size) {
MHL3_HID_DBG_ERR("Error in %s size:%d / ret_size:%d\n",
__func__, size, ret_size);
return;
}
/* hid->driver_lock is held as we are in probe function,
* we just need to setup the input fields, so using
* hid_report_raw_event is safe. */
hid_report_raw_event(hid, report->type, buffer + 2, size - 2, 1);
}
#endif
#if 0
/*
* Initialize all reports. This gets the current value of all
* input/feature reports for the device so that the HID-core can keep
* them in internal structures. The structure is updated as further
* device reports occur.
*/
static void mhl3_hid_init_reports(struct hid_device *hid)
{
struct mhl3_hid_data *mhid = hid->driver_data;
struct hid_report *rep;
MHL3_HID_DBG_INFO("%s\n", __func__);
list_for_each_entry(report,
&hid->report_enum[HID_INPUT_REPORT].report_list, list)
mhl3_hid_init_report(rep, mhid->in_report_buf, mhid->bufsize);
list_for_each_entry(report,
&hid->report_enum[HID_FEATURE_REPORT].report_list, list)
mhl3_hid_init_report(rep, mhid->in_report_buf, mhid->bufsize);
}
#endif
/*
* TODO: Doesn't do anything yet except manage the open count
*/
static int mhl3_hid_open(struct hid_device *hid)
{
struct mhl_dev_context *context = 0;
struct mhl3_hid_data *mhid = 0;
int ret = 0;
MHL3_HID_DBG_INFO("%s\n", __func__);
if (hid)
mhid = hid->driver_data;
if (mhid == 0)
return 0;
context = mhid->context;
mutex_lock(&mhl3_hid_open_mutex);
if (!hid->open++) {
ret = mhl3_hid_set_power(context, MHL3_HID_PWR_ON);
if (ret) {
hid->open--;
goto done;
}
set_bit(MHL3_HID_STARTED, &mhid->flags);
}
done:
mutex_unlock(&mhl3_hid_open_mutex);
return ret;
}
/*
* TODO: Doesn't do anything yet except manage the open count
*/
static void mhl3_hid_close(struct hid_device *hid)
{
struct mhl_dev_context *context = 0;
struct mhl3_hid_data *mhid = 0;
MHL3_HID_DBG_INFO("%s\n", __func__);
if (hid)
mhid = hid->driver_data;
if (mhid == 0)
return;
context = mhid->context;
/*
* Protecting hid->open to make sure we don't restart
* data acquisition due to a resumption we no longer
* care about
*/
mutex_lock(&mhl3_hid_open_mutex);
if (!--hid->open) {
clear_bit(MHL3_HID_STARTED, &mhid->flags);
mhl3_hid_set_power(context, MHL3_HID_PWR_SLEEP);
}
mutex_unlock(&mhl3_hid_open_mutex);
}
/*
* TODO: Doesn't do anything yet
*/
static int mhl3_hid_power(struct hid_device *hid, int lvl)
{
int ret = 0;
MHL3_HID_DBG_INFO("%s level: %d\n", __func__, lvl);
switch (lvl) {
case PM_HINT_FULLON:
break;
case PM_HINT_NORMAL:
break;
}
return ret;
}
static struct hid_ll_driver mhl3_hid_ll_driver = {
.open = mhl3_hid_open,
.close = mhl3_hid_close,
.power = mhl3_hid_power,
};
/*
* Use the MHL3 GET_MHID_DSCRPT message to request the HID Device
* Descriptor from the device on the control channel. The device
* should reply with an MHL3_MHID_DSCRPT message
*
* This function MUST be called from a work queue function.
*
*/
static int mhl3_hid_fetch_hid_descriptor(struct mhl3_hid_data *mhid)
{
struct mhl3_hid_desc *hdesc;
uint8_t *pdesc_raw;
int ret = -ENODEV;
int desc_len, raw_offset;
/* The actual length of the data will likely not be this much. */
desc_len = sizeof(struct mhl3_hid_desc) +
sizeof(mhid->desc_product_name) +
sizeof(mhid->desc_mfg_name) +
sizeof(mhid->desc_serial_number);
pdesc_raw = kmalloc(desc_len, GFP_KERNEL);
if (!pdesc_raw) {
MHL3_HID_DBG_ERR("Couldn't allocate raw descriptor memory\n");
return -ENOMEM;
}
MHL3_HID_DBG_INFO("Fetching the HID descriptor\n");
/*
* Build GET_MHID_DSCRPT message and add it to the out queue.
* By setting the LANG_ID field bytes to 0, we allow the
* device to send any language it chooses.
*/
mhid->out_data[0] = MHL3_GET_MHID_DSCRPT;
mhid->out_data[1] = 0;
mhid->out_data[2] = 0;
mhid->opState = OP_STATE_WAIT_MHID_DSCRPT;
ret = send_hid_wait(mhid, 3, pdesc_raw, desc_len, false);
if (ret < 0)
goto done;
hdesc = kmalloc(desc_len, GFP_KERNEL);
if (!hdesc) {
MHL3_HID_DBG_ERR("Couldn't allocate hdesc descriptor memory\n");
return -ENOMEM;
}
/* dump_array(DBG_MSG_LEVEL_INFO, "HID descriptor", pdesc_raw, ret); */
/* Get the fixed length part and verify. */
memcpy(hdesc, pdesc_raw, sizeof(struct mhl3_hid_desc));
raw_offset = sizeof(struct mhl3_hid_desc);
/* Do some simple checks. */
if (hdesc->bMHL3HIDmessageID != 0x05) {
MHL3_HID_DBG_ERR("Invalid MHID_DSCRPT data\n");
ret = -EINVAL;
goto done_err;
}
if (ret < (sizeof(struct mhl3_hid_desc) +
hdesc->bProductNameSize +
hdesc->bManufacturerNameSize +
hdesc->bSerialNumberSize)) {
memcpy(mhid->desc_product_name,
&pdesc_raw[raw_offset], hdesc->bProductNameSize);
raw_offset += hdesc->bProductNameSize;
memcpy(mhid->desc_mfg_name,
&pdesc_raw[raw_offset], hdesc->bManufacturerNameSize);
raw_offset += hdesc->bManufacturerNameSize;
memcpy(mhid->desc_serial_number,
&pdesc_raw[raw_offset], hdesc->bSerialNumberSize);
}
/*
* If this was an existing mhid being updated, free the previous
* descriptor and reassign with the new one.
*/
kfree(mhid->hdesc);
mhid->hdesc = hdesc;
ret = 0;
goto done;
done_err:
kfree(hdesc);
done:
kfree(pdesc_raw);
return ret;
}
/* TODO: Eliminated ACPI stuff; i2c_hid_acpi_pdata. etc. */
/*
* The users of the hid_device structure don't always check that
* the hid_device structure has a valid hid_driver before trying to access
* its members. Creating an empty structure at least avoids a
* kernel panic.
*/
static struct hid_driver mhl3_hid_driver = {
.name = "mhl3_hid",
};
static struct hid_driver mhl3_mt3_hid_driver = {
.name = "mhl3_hid-mt",
.event = mhl3_mt_event,
.input_mapping = mhl3_mt_input_mapping,
.input_mapped = mhl3_mt_input_mapped,
.feature_mapping = mt_feature_mapping,
};
/*
* Get report descriptors from the device and parse them.
* The Report Descriptor describes the report ID and the type(s)
* of data it contains. During operation, when the device returns
* a report with a specific ReportID, the HID core will know how
* to parse it.
*/
int mhl3_hid_report_desc_parse(struct mhl3_hid_data *mhid)
{
unsigned int report_len;
int ret;
uint8_t *report_desc_raw = NULL;
struct hid_device *hdev;
hdev = mhid->hid;
report_desc_raw = kmalloc(HID_MAX_DESCRIPTOR_SIZE, GFP_KERNEL);
if (!report_desc_raw) {
MHL3_HID_DBG_ERR("Allocate raw report descriptor mem failed\n");
ret = -ENOMEM;
goto err;
}
mhid->out_data[0] = MHL3_GET_REPORT_DSCRPT;
mhid->opState = OP_STATE_WAIT_REPORT_DSCRPT;
ret = send_hid_wait(mhid, 1, report_desc_raw,
HID_MAX_DESCRIPTOR_SIZE, false);
if (ret < 0) {
MHL3_HID_DBG_ERR(
"Failed to get descriptor from device: %d.\n", ret);
goto err;
}
if (report_desc_raw[0] != MHL3_REPORT_DSCRPT) {
MHL3_HID_DBG_ERR(
"Invalid response to MHL3_GET_REPORT_DSCRPT\n");
ret = -EINVAL;
goto err;
}
report_len = report_desc_raw[2];
report_len <<= 8;
report_len |= report_desc_raw[1];
report_len = (report_len < (ret-3)) ? report_len : (ret-3);
ret = hid_parse_report(hdev, &report_desc_raw[3], report_len);
if (ret) {
MHL3_HID_DBG_ERR("hid_parse_report failed\n");
goto err;
}
kfree(report_desc_raw);
return 0;
err:
MHL3_HID_DBG_ERR("WORK QUEUE mhl3_hid_report_desc_parse() FAIL\n");
kfree(report_desc_raw);
return ret;
}
/*
* The second part of the mhl3_hid_add() function, implemented as
* a work queue. It is entered at opState == OP_STATE_IDLE
* A failure here deletes the mhid device.
*/
static void mhl3_hid_add_work(struct work_struct *workdata)
{
struct mhl3_hid_data *mhid =
((struct hid_add_work_struct *)workdata)->mhid;
struct mhl_dev_context *context = mhid->context;
struct hid_device *hdev;
struct hid_device_id id = {0};
unsigned int bufsize = HID_MIN_BUFFER_SIZE;
int ret;
MHL3_HID_DBG_INFO("%s WORK QUEUE function executing\n", __func__);
ret = mhl3_hid_fetch_hid_descriptor(mhid);
if (ret < 0)
goto err;
/* Get a HID device if this is not an update of the existing HID. */
if (mhid->hid == NULL) {
hdev = hid_allocate_device();
if (IS_ERR(hdev)) {
ret = PTR_ERR(hdev);
goto err;
}
mhid->hid = hdev;
hdev->driver = &mhl3_mt3_hid_driver;
hdev->driver_data = mhid;
id.vendor = mhid->hdesc->wHIDVendorID;
id.product = mhid->hdesc->wHIDProductID;
}
hdev = mhid->hid;
hdev->ll_driver = &mhl3_hid_ll_driver;
hdev->hid_get_raw_report = mhl3_hid_get_raw_report;
hdev->hid_output_raw_report = mhl3_hid_output_raw_report;
hdev->dev.parent = NULL;
hdev->bus = BUS_VIRTUAL;
hdev->version = mhid->hdesc->wBcdHID;
hdev->vendor = mhid->hdesc->wHIDVendorID;
hdev->product = mhid->hdesc->wHIDProductID;
snprintf(hdev->name, sizeof(hdev->name), "MHL3 HID %04hX:%04hX",
hdev->vendor, hdev->product);
/* Check for multitouch device first. */
ret = mhl3_mt_add(mhid, &id);
if (ret) {
MHL3_HID_DBG_INFO("NOT Multitouch, trying generic HID\n");
hdev->driver = &mhl3_hid_driver;
ret = mhl3_hid_report_desc_parse(mhid);
}
if (ret < 0)
goto err;
/* TODO: Don't know why this didn't work: if (hid->claimed == 0) { */
if (!test_bit(MHL3_HID_CONNECTED, &mhid->flags)) {
/* TODO: What if this call fails? */
if (!hidinput_connect(hdev, 0)) {
MHL3_HID_DBG_INFO(
"%s hidinput_connect succeeded\n", __func__);
hdev->claimed |= HID_CLAIMED_INPUT;
set_bit(MHL3_HID_CONNECTED, &mhid->flags);
} else {
MHL3_HID_DBG_ERR(
"%s hidinput_connect FAILED\n", __func__);
goto err;
}
}
/*
* Allocate some report buffers and read the initial state of
* the INPUT and FEATURE reports.
*/
mhl3_hid_find_max_report(hdev, HID_INPUT_REPORT, &bufsize);
mhl3_hid_find_max_report(hdev, HID_OUTPUT_REPORT, &bufsize);
mhl3_hid_find_max_report(hdev, HID_FEATURE_REPORT, &bufsize);
if (bufsize > mhid->bufsize) {
kfree(mhid->in_report_buf);
mhid->in_report_buf = NULL;
ret = mhl3_hid_alloc_buffers(mhid, bufsize);
if (ret)
goto err;
}
/* if (!(hdev->quirks & HID_QUIRK_NO_INIT_REPORTS))
mhl3_hid_init_reports(hdev);
*/
mhid->opState = OP_STATE_CONNECTED;
MHL3_HID_DBG_INFO("WORK QUEUE function SUCCESS\n");
mhid->hid_work_active = false;
return;
err:
MHL3_HID_DBG_ERR("WORK QUEUE function FAIL\n");
if (mhid->hid) {
if (mhid->hid->claimed & HID_CLAIMED_INPUT)
hidinput_disconnect(mhid->hid);
hid_destroy_device(mhid->hid);
}
mhl3_send_ack(mhid, HID_ACK_NODEV);
/*
* Don't destroy the mhid while an interrupt is in progress on the
* off chance that the message we were waiting for came in after the
* timeout.
*/
MHL3_HID_DBG_INFO("WAITING FOR ISR_LOCK\n");
if (down_interruptible(&context->isr_lock)) {
MHL3_HID_DBG_ERR("Could not acquire isr_lock\n");
return;
}
MHL3_HID_DBG_INFO("ACQUIRED ISR_LOCK\n");
context->mhl_hid[mhid->id] = 0;
mhid->hid_work_active = false;
mhl3_hid_free_buffers(mhid);
kfree(mhid);
up(&context->isr_lock);
MHL3_HID_DBG_INFO("FREED ISR_LOCK\n");
return;
}
/*
* Create and initialize the device control structures and get added to
* the system HID device list. This is the equivalent to the probe
* function of normal HID-type drivers, but it is called when the
* MHL transport receives the eMSC Block transfer HID Tunneling request
* message MHL3_DSCRPT_UPDATE
*
* Called from a Titan interrupt handler. All events in the MHL driver
* are handled in interrupt handlers, so the real work here is performed on
* a work queue so the function can wait for responses from the driver.
*/
static int mhl3_hid_add(struct mhl_dev_context *context, int device_id)
{
struct mhl3_hid_data *mhid;
MHL3_HID_DBG_INFO("%s Adding device %d\n", __func__, device_id);
if (device_id >= MAX_HID_MESSAGE_CHANNELS)
return -EINVAL;
if (context->mhl_hid[device_id] == 0) {
mhid = kzalloc(sizeof(struct mhl3_hid_data), GFP_KERNEL);
if (!mhid)
return -ENOMEM;
context->mhl_hid[device_id] = mhid;
sema_init(&mhid->data_wait_lock, 1);
mhid->mhl3_work.mhid = mhid;
mhid->id = device_id;
mhid->context = context;
} else {
mhid = context->mhl_hid[device_id];
}
mhid->hid_work_active = true;
INIT_WORK((struct work_struct *)&mhid->mhl3_work, mhl3_hid_add_work);
schedule_work((struct work_struct *)&mhid->mhl3_work);
return 0;
}
/*
* This is the reverse of the mhl3_hid_add() function.
* The device_id parameter is for when we support multiple HID devices.
*/
int mhl3_hid_remove(struct mhl_dev_context *context, int device_id)
{
struct mhl3_hid_data *mhid;
struct hid_device *hid;
int dummy;
MHL3_HID_DBG_INFO("%s\n", __func__);
if (device_id >= MAX_HID_MESSAGE_CHANNELS)
return -EINVAL;
mhid = context->mhl_hid[device_id];
if (mhid == NULL)
return 0;
hid = mhid->hid;
mhid->hid_work_active = false;
/* Release the data wait to let the work queue finish. */
dummy = down_trylock(&mhid->data_wait_lock);
up(&mhid->data_wait_lock);
MHL3_HID_DBG_INFO("%s - cancel_work_sync() returned: %d\n", __func__,
cancel_work_sync((struct work_struct *)&mhid->mhl3_work));
if (hid) {
MHL3_HID_DBG_INFO("%s Disconnecting HID device %d\n",
__func__, device_id);
hidinput_disconnect(hid);
hid_destroy_device(hid);
}
/* TODO: Do we need to do an isr_lock here? */
if (mhid->bufsize)
mhl3_hid_free_buffers(mhid);
context->mhl_hid[device_id] = 0;
kfree(mhid);
return 0;
}
/*
* For when we support multiple HID devices.
*/
void mhl3_hid_remove_all(struct mhl_dev_context *context)
{
mhl3_hid_remove(context, 0);
}
/*
* We have received a completed MHL3 HID Tunneling message. Parse the
* header to decide what to do with it.
* Called indirectly from the Titan interrupt handler.
*/
static void mhl3_hid_message_processor(struct mhl_dev_context *mdev,
uint8_t hb0, uint8_t hb1, uint8_t *pmsg, int length)
{
struct mhl3_hid_data *mhid;
uint8_t msg_id;
bool want_ack;
bool matched_expected_message;
int opState, mhl3_device_id, hid_msg_count, header_len, ret;
int msg_channel;
mhl3_device_id = (hb0 >> 4) & 0x0F;
msg_channel = (hb0 & 0x01);
want_ack = ((hb1 & 0x80) != 0);
hid_msg_count = hb1 & 0x7F;
msg_id = pmsg[0];
matched_expected_message = false;
MHL3_HID_DBG_INFO("Received message: msg_id: %02X\n", msg_id);
mhid = mdev->mhl_hid[mhl3_device_id];
opState = OP_STATE_IDLE;
if (mhid == 0) {
MHL3_HID_DBG_INFO("Message received with mhid == 0\n");
if (msg_id != MHL3_DSCRPT_UPDATE) {
mhl3_int_send_ack(mdev, HID_ACK_TIMEOUT, hb0);
return;
}
} else {
if (want_ack) {
if (hid_msg_count !=
(mhid->msg_count[msg_channel] + 1)) {
send_ack_packet(
mdev, hb0,
mhid->msg_count[msg_channel]);
return;
} else {
mhid->msg_count[msg_channel] += 1;
}
}
opState = mhid->opState;
}
if (want_ack)
send_ack_packet(mdev, hb0, hb1);
switch (opState) {
case OP_STATE_IDLE:
if (msg_id == MHL3_DSCRPT_UPDATE) {
ret = mhl3_hid_add(mdev, mhl3_device_id);
if (ret < 0)
mhl3_int_send_ack(mdev, ret, hb0);
} else {
mhl3_int_send_ack(mdev, HID_ACK_PROTV, hb0);
}
return;
case OP_STATE_WAIT_MHID_DSCRPT:
MHL3_HID_DBG_INFO("OP_STATE_WAIT_MHID_DSCRPT\n");
if ((msg_id == MHL3_MHID_DSCRPT) ||
(msg_id == MHL3_DSCRPT_UPDATE)) {
matched_expected_message = true;
}
break;
case OP_STATE_WAIT_REPORT_DSCRPT:
MHL3_HID_DBG_INFO("OP_STATE_WAIT_REPORT_DSCRPT\n");
if ((msg_id == MHL3_REPORT_DSCRPT) ||
(msg_id == MHL3_DSCRPT_UPDATE))
matched_expected_message = true;
break;
case OP_STATE_WAIT_REPORT:
MHL3_HID_DBG_INFO("OP_STATE_WAIT_REPORT\n");
if ((msg_id == MHL3_REPORT) || (msg_id == MHL3_DSCRPT_UPDATE))
matched_expected_message = true;
break;
case OP_STATE_CONNECTED:
MHL3_HID_DBG_INFO("OP_STATE_CONNECTED\n");
/*
* If sent by the interrupt channel, the message is NOT from a
* MHL3_GET_REPORT request by the host, and we send it
* directly to the HID core. Otherwise, we return it via the
* WORK QUEUE.
*/
switch (msg_id) {
case MHL3_REPORT:
if (msg_channel == HID_ACHID_INT) {
MHL3_HID_DBG_INFO(
"Sending %s report to HID-core\n",
mhid->hid->
report_enum[HID_INPUT_REPORT].numbered ?
"numbered" : "");
/* Send the report directly to the HID core,
* minus the MSG_ID, REPORT_TYPE, REPORT_ID
* (if present), and LENGTH (2 bytes). */
header_len =
(mhid->hid->
report_enum[HID_INPUT_REPORT].numbered) ?
5 : 4;
ret = hid_input_report(
mhid->hid, HID_INPUT_REPORT,
pmsg + header_len,
length - header_len, 1);
return;
}
matched_expected_message = true;
break;
case MHL3_DSCRPT_UPDATE:
ret = mhl3_hid_add(mdev, mhl3_device_id);
if (ret < 0)
mhl3_int_send_ack(mdev, ret, hb0);
return;
break;
default:
if (msg_id != MHL3_HID_ACK)
matched_expected_message = true;
break;
}
break;
}
if (!matched_expected_message && (msg_id != MHL3_HID_ACK)) {
mhl3_int_send_ack(mdev, HID_ACK_PROTV, hb0);
return;
}
/* If someone was waiting for this data, let them know it's here. */
if (down_trylock(&mhid->data_wait_lock)) {
memcpy(mhid->in_data, pmsg, length);
mhid->in_data_length = length;
} else {
/* TODO: If not on a WORK QUEUE, where does it go? */
MHL3_HID_DBG_WARN("Sending received msg into the ether!!!\n");
}
/* Either the try was successful, in which case no one was waiting
* for the message, or it wasn't, meaning someone WAS waiting. In
* either case we need to release the semaphore. */
up(&mhid->data_wait_lock);
}
static void validate_ack(struct mhl_dev_context *mdev,
uint8_t *pmsg, int length)
{
/* uint8_t ack_msg, channel; */
int dev_id;
struct mhl3_hid_data *mhid;
if (length != HID_ACK_PACKET_LEN)
return;
dev_id = (pmsg[1] & HID_HB0_DEV_ID_MSK) >> 4;
mhid = mdev->mhl_hid[dev_id];
if (mhid == 0)
return;
/*
ack_msg = pmsg[0] & HID_FRAG_HB0_CNT_MSK;
channel = pmsg[1] & HID_HB0_ACHID_MSK;
if (ack_msg == mhid->msg_count[channel])
resend_last_message();
*/
}
/*
* Accumulate fragments of a HID message until the entire message has
* been received, then dispatch it properly.
* Called from the Titan interrupt
*/
void build_received_hid_message(struct mhl_dev_context *mdev,
uint8_t *pmsg, int length)
{
int i, fragment_count = 0;
uint16_t *pchksum;
uint16_t accum, msg_chksum;
struct mhl3_hid_global_data *emsc = &mdev->mhl_ghid;
if (length == 0)
return;
switch (emsc->hid_receive_state) {
case EMSC_RCV_MSG_START:
if ((pmsg[0] & HID_FRAG_HB0_TYPE) == HID_FRAG_HB0_TYPE_ACK) {
validate_ack(mdev, pmsg, length);
return;
}
fragment_count = pmsg[0];
if (length >= (HID_FRAG_HEADER_LEN +
HID_MSG_HEADER_LEN +
HID_MSG_CHKSUM_LEN + 1)) {
emsc->hb0 = pmsg[1];
emsc->hb1 = pmsg[2];
length -= 3;
memcpy(&emsc->in_buf[0], &pmsg[3], length);
}
emsc->msg_length = length;
emsc->hid_receive_state = EMSC_RCV_MSG_NEXT;
break;
case EMSC_RCV_MSG_NEXT:
fragment_count = pmsg[0];
if (length >= (HID_FRAG_HEADER_LEN + 1)) {
length--;
memcpy(&emsc->in_buf[emsc->msg_length],
&pmsg[1], length);
emsc->msg_length += length;
}
break;
default:
/* This is an error, so don't dispatch anything. */
fragment_count = 1;
break;
}
/* If the end of the message, dispatch it. */
if (fragment_count == 0) {
emsc->hid_receive_state = EMSC_RCV_MSG_START;
accum = 0;
pchksum = (uint16_t *)&emsc->in_buf[0];
emsc->msg_length -= HID_MSG_CHKSUM_LEN;
for (i = 0; i < (emsc->msg_length / 2); i++)
accum += pchksum[i];
accum += ((uint16_t)emsc->hb0 | (((uint16_t)emsc->hb1) << 8));
if (emsc->msg_length & 0x01)
accum += ((uint16_t)emsc->in_buf[emsc->msg_length - 1]);
/* Add in length of message including HB0/HB1. */
accum += emsc->msg_length + HID_MSG_HEADER_LEN;
msg_chksum = *((uint16_t *)&emsc->in_buf[emsc->msg_length]);
if (accum != msg_chksum) {
MHL3_HID_DBG_ERR(
"HID MSG CKSM fail, ignoring message: "
"Act: %04X Exp: %04X\n",
accum, msg_chksum);
/* Request a re-try. */
if (emsc->hb1 & EMSC_HID_HB1_ACK) {
send_ack_packet(
mdev, emsc->hb0,
(emsc->hb1 & EMSC_HID_HB1_MSG_CNT_FLD)
- 1);
}
return;
}
mhl3_hid_message_processor(mdev,
emsc->hb0, emsc->hb1, emsc->in_buf,
emsc->msg_length);
}
}
#ifdef SI_CONFIG_PM_SLEEP /* this was originally CONFIG_PM_SLEEP,
* but we don't want the compiler warnings
*/
/*
* TODO: This is used during system suspend and hibernation as well
* as normal runtime PM. Needs work.
*/
static int mhl3_hid_suspend(struct device *dev)
{
/* struct i2c_client *client = to_i2c_client(dev);
if (device_may_wakeup(&client->dev))
enable_irq_wake(client->irq);
mhl3_hid_set_power(client, I2C_HID_PWR_SLEEP);
*/
return 0;
}
/*
* TODO: This is used during system suspend and hibernation as well
* as normal runtime PM. Needs work.
*/
static int mhl3_hid_resume(struct device *dev)
{
/* int ret;
struct i2c_client *client = to_i2c_client(dev);
ret = i2c_hid_hwreset(client);
if (ret)
return ret;
if (device_may_wakeup(&client->dev))
disable_irq_wake(client->irq);
*/
return 0;
}
#endif
|