aboutsummaryrefslogtreecommitdiff
path: root/include/linux/cec-funcs.h
blob: 138bbf721e70cae221544a4dbcd7a3f6175d54cd (plain)
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
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
/*
 * cec - HDMI Consumer Electronics Control message functions
 *
 * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
 *
 * This program is free software; you may redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * Alternatively you can redistribute this file under the terms of the
 * BSD license as stated below:
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. The names of its contributors may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/*
 * Note: this framework is still in staging and it is likely the API
 * will change before it goes out of staging.
 *
 * Once it is moved out of staging this header will move to uapi.
 */
#ifndef _CEC_UAPI_FUNCS_H
#define _CEC_UAPI_FUNCS_H

#include <linux/cec.h>

/* One Touch Play Feature */
static inline void cec_msg_active_source(struct cec_msg *msg, __u16 phys_addr)
{
	msg->len = 4;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_ACTIVE_SOURCE;
	msg->msg[2] = phys_addr >> 8;
	msg->msg[3] = phys_addr & 0xff;
}

static inline void cec_ops_active_source(const struct cec_msg *msg,
					 __u16 *phys_addr)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
}

static inline void cec_msg_image_view_on(struct cec_msg *msg)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_IMAGE_VIEW_ON;
}

static inline void cec_msg_text_view_on(struct cec_msg *msg)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_TEXT_VIEW_ON;
}


/* Routing Control Feature */
static inline void cec_msg_inactive_source(struct cec_msg *msg,
					   __u16 phys_addr)
{
	msg->len = 4;
	msg->msg[1] = CEC_MSG_INACTIVE_SOURCE;
	msg->msg[2] = phys_addr >> 8;
	msg->msg[3] = phys_addr & 0xff;
}

static inline void cec_ops_inactive_source(const struct cec_msg *msg,
					   __u16 *phys_addr)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
}

static inline void cec_msg_request_active_source(struct cec_msg *msg,
						 bool reply)
{
	msg->len = 2;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_REQUEST_ACTIVE_SOURCE;
	msg->reply = reply ? CEC_MSG_ACTIVE_SOURCE : 0;
}

static inline void cec_msg_routing_information(struct cec_msg *msg,
					       __u16 phys_addr)
{
	msg->len = 4;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_ROUTING_INFORMATION;
	msg->msg[2] = phys_addr >> 8;
	msg->msg[3] = phys_addr & 0xff;
}

static inline void cec_ops_routing_information(const struct cec_msg *msg,
					       __u16 *phys_addr)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
}

static inline void cec_msg_routing_change(struct cec_msg *msg,
					  bool reply,
					  __u16 orig_phys_addr,
					  __u16 new_phys_addr)
{
	msg->len = 6;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_ROUTING_CHANGE;
	msg->msg[2] = orig_phys_addr >> 8;
	msg->msg[3] = orig_phys_addr & 0xff;
	msg->msg[4] = new_phys_addr >> 8;
	msg->msg[5] = new_phys_addr & 0xff;
	msg->reply = reply ? CEC_MSG_ROUTING_INFORMATION : 0;
}

static inline void cec_ops_routing_change(const struct cec_msg *msg,
					  __u16 *orig_phys_addr,
					  __u16 *new_phys_addr)
{
	*orig_phys_addr = (msg->msg[2] << 8) | msg->msg[3];
	*new_phys_addr = (msg->msg[4] << 8) | msg->msg[5];
}

static inline void cec_msg_set_stream_path(struct cec_msg *msg, __u16 phys_addr)
{
	msg->len = 4;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_SET_STREAM_PATH;
	msg->msg[2] = phys_addr >> 8;
	msg->msg[3] = phys_addr & 0xff;
}

static inline void cec_ops_set_stream_path(const struct cec_msg *msg,
					   __u16 *phys_addr)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
}


/* Standby Feature */
static inline void cec_msg_standby(struct cec_msg *msg)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_STANDBY;
}


/* One Touch Record Feature */
static inline void cec_msg_record_off(struct cec_msg *msg, bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_RECORD_OFF;
	msg->reply = reply ? CEC_MSG_RECORD_STATUS : 0;
}

struct cec_op_arib_data {
	__u16 transport_id;
	__u16 service_id;
	__u16 orig_network_id;
};

struct cec_op_atsc_data {
	__u16 transport_id;
	__u16 program_number;
};

struct cec_op_dvb_data {
	__u16 transport_id;
	__u16 service_id;
	__u16 orig_network_id;
};

struct cec_op_channel_data {
	__u8 channel_number_fmt;
	__u16 major;
	__u16 minor;
};

struct cec_op_digital_service_id {
	__u8 service_id_method;
	__u8 dig_bcast_system;
	union {
		struct cec_op_arib_data arib;
		struct cec_op_atsc_data atsc;
		struct cec_op_dvb_data dvb;
		struct cec_op_channel_data channel;
	};
};

struct cec_op_record_src {
	__u8 type;
	union {
		struct cec_op_digital_service_id digital;
		struct {
			__u8 ana_bcast_type;
			__u16 ana_freq;
			__u8 bcast_system;
		} analog;
		struct {
			__u8 plug;
		} ext_plug;
		struct {
			__u16 phys_addr;
		} ext_phys_addr;
	};
};

static inline void cec_set_digital_service_id(__u8 *msg,
	      const struct cec_op_digital_service_id *digital)
{
	*msg++ = (digital->service_id_method << 7) | digital->dig_bcast_system;
	if (digital->service_id_method == CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL) {
		*msg++ = (digital->channel.channel_number_fmt << 2) |
			 (digital->channel.major >> 8);
		*msg++ = digital->channel.major & 0xff;
		*msg++ = digital->channel.minor >> 8;
		*msg++ = digital->channel.minor & 0xff;
		*msg++ = 0;
		*msg++ = 0;
		return;
	}
	switch (digital->dig_bcast_system) {
	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_GEN:
	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_CABLE:
	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_SAT:
	case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_T:
		*msg++ = digital->atsc.transport_id >> 8;
		*msg++ = digital->atsc.transport_id & 0xff;
		*msg++ = digital->atsc.program_number >> 8;
		*msg++ = digital->atsc.program_number & 0xff;
		*msg++ = 0;
		*msg++ = 0;
		break;
	default:
		*msg++ = digital->dvb.transport_id >> 8;
		*msg++ = digital->dvb.transport_id & 0xff;
		*msg++ = digital->dvb.service_id >> 8;
		*msg++ = digital->dvb.service_id & 0xff;
		*msg++ = digital->dvb.orig_network_id >> 8;
		*msg++ = digital->dvb.orig_network_id & 0xff;
		break;
	}
}

static inline void cec_get_digital_service_id(const __u8 *msg,
	      struct cec_op_digital_service_id *digital)
{
	digital->service_id_method = msg[0] >> 7;
	digital->dig_bcast_system = msg[0] & 0x7f;
	if (digital->service_id_method == CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL) {
		digital->channel.channel_number_fmt = msg[1] >> 2;
		digital->channel.major = ((msg[1] & 3) << 6) | msg[2];
		digital->channel.minor = (msg[3] << 8) | msg[4];
		return;
	}
	digital->dvb.transport_id = (msg[1] << 8) | msg[2];
	digital->dvb.service_id = (msg[3] << 8) | msg[4];
	digital->dvb.orig_network_id = (msg[5] << 8) | msg[6];
}

static inline void cec_msg_record_on_own(struct cec_msg *msg)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_RECORD_ON;
	msg->msg[2] = CEC_OP_RECORD_SRC_OWN;
}

static inline void cec_msg_record_on_digital(struct cec_msg *msg,
			     const struct cec_op_digital_service_id *digital)
{
	msg->len = 10;
	msg->msg[1] = CEC_MSG_RECORD_ON;
	msg->msg[2] = CEC_OP_RECORD_SRC_DIGITAL;
	cec_set_digital_service_id(msg->msg + 3, digital);
}

static inline void cec_msg_record_on_analog(struct cec_msg *msg,
					    __u8 ana_bcast_type,
					    __u16 ana_freq,
					    __u8 bcast_system)
{
	msg->len = 7;
	msg->msg[1] = CEC_MSG_RECORD_ON;
	msg->msg[2] = CEC_OP_RECORD_SRC_ANALOG;
	msg->msg[3] = ana_bcast_type;
	msg->msg[4] = ana_freq >> 8;
	msg->msg[5] = ana_freq & 0xff;
	msg->msg[6] = bcast_system;
}

static inline void cec_msg_record_on_plug(struct cec_msg *msg,
					  __u8 plug)
{
	msg->len = 4;
	msg->msg[1] = CEC_MSG_RECORD_ON;
	msg->msg[2] = CEC_OP_RECORD_SRC_EXT_PLUG;
	msg->msg[3] = plug;
}

static inline void cec_msg_record_on_phys_addr(struct cec_msg *msg,
					       __u16 phys_addr)
{
	msg->len = 5;
	msg->msg[1] = CEC_MSG_RECORD_ON;
	msg->msg[2] = CEC_OP_RECORD_SRC_EXT_PHYS_ADDR;
	msg->msg[3] = phys_addr >> 8;
	msg->msg[4] = phys_addr & 0xff;
}

static inline void cec_msg_record_on(struct cec_msg *msg,
				     bool reply,
				     const struct cec_op_record_src *rec_src)
{
	switch (rec_src->type) {
	case CEC_OP_RECORD_SRC_OWN:
		cec_msg_record_on_own(msg);
		break;
	case CEC_OP_RECORD_SRC_DIGITAL:
		cec_msg_record_on_digital(msg, &rec_src->digital);
		break;
	case CEC_OP_RECORD_SRC_ANALOG:
		cec_msg_record_on_analog(msg,
					 rec_src->analog.ana_bcast_type,
					 rec_src->analog.ana_freq,
					 rec_src->analog.bcast_system);
		break;
	case CEC_OP_RECORD_SRC_EXT_PLUG:
		cec_msg_record_on_plug(msg, rec_src->ext_plug.plug);
		break;
	case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
		cec_msg_record_on_phys_addr(msg,
					    rec_src->ext_phys_addr.phys_addr);
		break;
	}
	msg->reply = reply ? CEC_MSG_RECORD_STATUS : 0;
}

static inline void cec_ops_record_on(const struct cec_msg *msg,
				     struct cec_op_record_src *rec_src)
{
	rec_src->type = msg->msg[2];
	switch (rec_src->type) {
	case CEC_OP_RECORD_SRC_OWN:
		break;
	case CEC_OP_RECORD_SRC_DIGITAL:
		cec_get_digital_service_id(msg->msg + 3, &rec_src->digital);
		break;
	case CEC_OP_RECORD_SRC_ANALOG:
		rec_src->analog.ana_bcast_type = msg->msg[3];
		rec_src->analog.ana_freq =
			(msg->msg[4] << 8) | msg->msg[5];
		rec_src->analog.bcast_system = msg->msg[6];
		break;
	case CEC_OP_RECORD_SRC_EXT_PLUG:
		rec_src->ext_plug.plug = msg->msg[3];
		break;
	case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
		rec_src->ext_phys_addr.phys_addr =
			(msg->msg[3] << 8) | msg->msg[4];
		break;
	}
}

static inline void cec_msg_record_status(struct cec_msg *msg, __u8 rec_status)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_RECORD_STATUS;
	msg->msg[2] = rec_status;
}

static inline void cec_ops_record_status(const struct cec_msg *msg,
					 __u8 *rec_status)
{
	*rec_status = msg->msg[2];
}

static inline void cec_msg_record_tv_screen(struct cec_msg *msg,
					    bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_RECORD_TV_SCREEN;
	msg->reply = reply ? CEC_MSG_RECORD_ON : 0;
}


/* Timer Programming Feature */
static inline void cec_msg_timer_status(struct cec_msg *msg,
					__u8 timer_overlap_warning,
					__u8 media_info,
					__u8 prog_info,
					__u8 prog_error,
					__u8 duration_hr,
					__u8 duration_min)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_TIMER_STATUS;
	msg->msg[2] = (timer_overlap_warning << 7) |
		(media_info << 5) |
		(prog_info ? 0x10 : 0) |
		(prog_info ? prog_info : prog_error);
	if (prog_info == CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE ||
	    prog_info == CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE ||
	    prog_error == CEC_OP_PROG_ERROR_DUPLICATE) {
		msg->len += 2;
		msg->msg[3] = ((duration_hr / 10) << 4) | (duration_hr % 10);
		msg->msg[4] = ((duration_min / 10) << 4) | (duration_min % 10);
	}
}

static inline void cec_ops_timer_status(const struct cec_msg *msg,
					__u8 *timer_overlap_warning,
					__u8 *media_info,
					__u8 *prog_info,
					__u8 *prog_error,
					__u8 *duration_hr,
					__u8 *duration_min)
{
	*timer_overlap_warning = msg->msg[2] >> 7;
	*media_info = (msg->msg[2] >> 5) & 3;
	if (msg->msg[2] & 0x10) {
		*prog_info = msg->msg[2] & 0xf;
		*prog_error = 0;
	} else {
		*prog_info = 0;
		*prog_error = msg->msg[2] & 0xf;
	}
	if (*prog_info == CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE ||
	    *prog_info == CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE ||
	    *prog_error == CEC_OP_PROG_ERROR_DUPLICATE) {
		*duration_hr = (msg->msg[3] >> 4) * 10 + (msg->msg[3] & 0xf);
		*duration_min = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
	} else {
		*duration_hr = *duration_min = 0;
	}
}

static inline void cec_msg_timer_cleared_status(struct cec_msg *msg,
						__u8 timer_cleared_status)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_TIMER_CLEARED_STATUS;
	msg->msg[2] = timer_cleared_status;
}

static inline void cec_ops_timer_cleared_status(const struct cec_msg *msg,
						__u8 *timer_cleared_status)
{
	*timer_cleared_status = msg->msg[2];
}

static inline void cec_msg_clear_analogue_timer(struct cec_msg *msg,
						bool reply,
						__u8 day,
						__u8 month,
						__u8 start_hr,
						__u8 start_min,
						__u8 duration_hr,
						__u8 duration_min,
						__u8 recording_seq,
						__u8 ana_bcast_type,
						__u16 ana_freq,
						__u8 bcast_system)
{
	msg->len = 13;
	msg->msg[1] = CEC_MSG_CLEAR_ANALOGUE_TIMER;
	msg->msg[2] = day;
	msg->msg[3] = month;
	/* Hours and minutes are in BCD format */
	msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
	msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
	msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
	msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
	msg->msg[8] = recording_seq;
	msg->msg[9] = ana_bcast_type;
	msg->msg[10] = ana_freq >> 8;
	msg->msg[11] = ana_freq & 0xff;
	msg->msg[12] = bcast_system;
	msg->reply = reply ? CEC_MSG_TIMER_CLEARED_STATUS : 0;
}

static inline void cec_ops_clear_analogue_timer(const struct cec_msg *msg,
						__u8 *day,
						__u8 *month,
						__u8 *start_hr,
						__u8 *start_min,
						__u8 *duration_hr,
						__u8 *duration_min,
						__u8 *recording_seq,
						__u8 *ana_bcast_type,
						__u16 *ana_freq,
						__u8 *bcast_system)
{
	*day = msg->msg[2];
	*month = msg->msg[3];
	/* Hours and minutes are in BCD format */
	*start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
	*start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
	*duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
	*duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
	*recording_seq = msg->msg[8];
	*ana_bcast_type = msg->msg[9];
	*ana_freq = (msg->msg[10] << 8) | msg->msg[11];
	*bcast_system = msg->msg[12];
}

static inline void cec_msg_clear_digital_timer(struct cec_msg *msg,
				bool reply,
				__u8 day,
				__u8 month,
				__u8 start_hr,
				__u8 start_min,
				__u8 duration_hr,
				__u8 duration_min,
				__u8 recording_seq,
				const struct cec_op_digital_service_id *digital)
{
	msg->len = 16;
	msg->reply = reply ? CEC_MSG_TIMER_CLEARED_STATUS : 0;
	msg->msg[1] = CEC_MSG_CLEAR_DIGITAL_TIMER;
	msg->msg[2] = day;
	msg->msg[3] = month;
	/* Hours and minutes are in BCD format */
	msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
	msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
	msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
	msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
	msg->msg[8] = recording_seq;
	cec_set_digital_service_id(msg->msg + 9, digital);
}

static inline void cec_ops_clear_digital_timer(const struct cec_msg *msg,
				__u8 *day,
				__u8 *month,
				__u8 *start_hr,
				__u8 *start_min,
				__u8 *duration_hr,
				__u8 *duration_min,
				__u8 *recording_seq,
				struct cec_op_digital_service_id *digital)
{
	*day = msg->msg[2];
	*month = msg->msg[3];
	/* Hours and minutes are in BCD format */
	*start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
	*start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
	*duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
	*duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
	*recording_seq = msg->msg[8];
	cec_get_digital_service_id(msg->msg + 9, digital);
}

static inline void cec_msg_clear_ext_timer(struct cec_msg *msg,
					   bool reply,
					   __u8 day,
					   __u8 month,
					   __u8 start_hr,
					   __u8 start_min,
					   __u8 duration_hr,
					   __u8 duration_min,
					   __u8 recording_seq,
					   __u8 ext_src_spec,
					   __u8 plug,
					   __u16 phys_addr)
{
	msg->len = 13;
	msg->msg[1] = CEC_MSG_CLEAR_EXT_TIMER;
	msg->msg[2] = day;
	msg->msg[3] = month;
	/* Hours and minutes are in BCD format */
	msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
	msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
	msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
	msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
	msg->msg[8] = recording_seq;
	msg->msg[9] = ext_src_spec;
	msg->msg[10] = plug;
	msg->msg[11] = phys_addr >> 8;
	msg->msg[12] = phys_addr & 0xff;
	msg->reply = reply ? CEC_MSG_TIMER_CLEARED_STATUS : 0;
}

static inline void cec_ops_clear_ext_timer(const struct cec_msg *msg,
					   __u8 *day,
					   __u8 *month,
					   __u8 *start_hr,
					   __u8 *start_min,
					   __u8 *duration_hr,
					   __u8 *duration_min,
					   __u8 *recording_seq,
					   __u8 *ext_src_spec,
					   __u8 *plug,
					   __u16 *phys_addr)
{
	*day = msg->msg[2];
	*month = msg->msg[3];
	/* Hours and minutes are in BCD format */
	*start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
	*start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
	*duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
	*duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
	*recording_seq = msg->msg[8];
	*ext_src_spec = msg->msg[9];
	*plug = msg->msg[10];
	*phys_addr = (msg->msg[11] << 8) | msg->msg[12];
}

static inline void cec_msg_set_analogue_timer(struct cec_msg *msg,
					      bool reply,
					      __u8 day,
					      __u8 month,
					      __u8 start_hr,
					      __u8 start_min,
					      __u8 duration_hr,
					      __u8 duration_min,
					      __u8 recording_seq,
					      __u8 ana_bcast_type,
					      __u16 ana_freq,
					      __u8 bcast_system)
{
	msg->len = 13;
	msg->msg[1] = CEC_MSG_SET_ANALOGUE_TIMER;
	msg->msg[2] = day;
	msg->msg[3] = month;
	/* Hours and minutes are in BCD format */
	msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
	msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
	msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
	msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
	msg->msg[8] = recording_seq;
	msg->msg[9] = ana_bcast_type;
	msg->msg[10] = ana_freq >> 8;
	msg->msg[11] = ana_freq & 0xff;
	msg->msg[12] = bcast_system;
	msg->reply = reply ? CEC_MSG_TIMER_STATUS : 0;
}

static inline void cec_ops_set_analogue_timer(const struct cec_msg *msg,
					      __u8 *day,
					      __u8 *month,
					      __u8 *start_hr,
					      __u8 *start_min,
					      __u8 *duration_hr,
					      __u8 *duration_min,
					      __u8 *recording_seq,
					      __u8 *ana_bcast_type,
					      __u16 *ana_freq,
					      __u8 *bcast_system)
{
	*day = msg->msg[2];
	*month = msg->msg[3];
	/* Hours and minutes are in BCD format */
	*start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
	*start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
	*duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
	*duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
	*recording_seq = msg->msg[8];
	*ana_bcast_type = msg->msg[9];
	*ana_freq = (msg->msg[10] << 8) | msg->msg[11];
	*bcast_system = msg->msg[12];
}

static inline void cec_msg_set_digital_timer(struct cec_msg *msg,
			bool reply,
			__u8 day,
			__u8 month,
			__u8 start_hr,
			__u8 start_min,
			__u8 duration_hr,
			__u8 duration_min,
			__u8 recording_seq,
			const struct cec_op_digital_service_id *digital)
{
	msg->len = 16;
	msg->reply = reply ? CEC_MSG_TIMER_STATUS : 0;
	msg->msg[1] = CEC_MSG_SET_DIGITAL_TIMER;
	msg->msg[2] = day;
	msg->msg[3] = month;
	/* Hours and minutes are in BCD format */
	msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
	msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
	msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
	msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
	msg->msg[8] = recording_seq;
	cec_set_digital_service_id(msg->msg + 9, digital);
}

static inline void cec_ops_set_digital_timer(const struct cec_msg *msg,
			__u8 *day,
			__u8 *month,
			__u8 *start_hr,
			__u8 *start_min,
			__u8 *duration_hr,
			__u8 *duration_min,
			__u8 *recording_seq,
			struct cec_op_digital_service_id *digital)
{
	*day = msg->msg[2];
	*month = msg->msg[3];
	/* Hours and minutes are in BCD format */
	*start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
	*start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
	*duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
	*duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
	*recording_seq = msg->msg[8];
	cec_get_digital_service_id(msg->msg + 9, digital);
}

static inline void cec_msg_set_ext_timer(struct cec_msg *msg,
					 bool reply,
					 __u8 day,
					 __u8 month,
					 __u8 start_hr,
					 __u8 start_min,
					 __u8 duration_hr,
					 __u8 duration_min,
					 __u8 recording_seq,
					 __u8 ext_src_spec,
					 __u8 plug,
					 __u16 phys_addr)
{
	msg->len = 13;
	msg->msg[1] = CEC_MSG_SET_EXT_TIMER;
	msg->msg[2] = day;
	msg->msg[3] = month;
	/* Hours and minutes are in BCD format */
	msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
	msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
	msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
	msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
	msg->msg[8] = recording_seq;
	msg->msg[9] = ext_src_spec;
	msg->msg[10] = plug;
	msg->msg[11] = phys_addr >> 8;
	msg->msg[12] = phys_addr & 0xff;
	msg->reply = reply ? CEC_MSG_TIMER_STATUS : 0;
}

static inline void cec_ops_set_ext_timer(const struct cec_msg *msg,
					 __u8 *day,
					 __u8 *month,
					 __u8 *start_hr,
					 __u8 *start_min,
					 __u8 *duration_hr,
					 __u8 *duration_min,
					 __u8 *recording_seq,
					 __u8 *ext_src_spec,
					 __u8 *plug,
					 __u16 *phys_addr)
{
	*day = msg->msg[2];
	*month = msg->msg[3];
	/* Hours and minutes are in BCD format */
	*start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
	*start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
	*duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
	*duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
	*recording_seq = msg->msg[8];
	*ext_src_spec = msg->msg[9];
	*plug = msg->msg[10];
	*phys_addr = (msg->msg[11] << 8) | msg->msg[12];
}

static inline void cec_msg_set_timer_program_title(struct cec_msg *msg,
						   const char *prog_title)
{
	unsigned int len = strlen(prog_title);

	if (len > 14)
		len = 14;
	msg->len = 2 + len;
	msg->msg[1] = CEC_MSG_SET_TIMER_PROGRAM_TITLE;
	memcpy(msg->msg + 2, prog_title, len);
}

static inline void cec_ops_set_timer_program_title(const struct cec_msg *msg,
						   char *prog_title)
{
	unsigned int len = msg->len > 2 ? msg->len - 2 : 0;

	if (len > 14)
		len = 14;
	memcpy(prog_title, msg->msg + 2, len);
	prog_title[len] = '\0';
}

/* System Information Feature */
static inline void cec_msg_cec_version(struct cec_msg *msg, __u8 cec_version)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_CEC_VERSION;
	msg->msg[2] = cec_version;
}

static inline void cec_ops_cec_version(const struct cec_msg *msg,
				       __u8 *cec_version)
{
	*cec_version = msg->msg[2];
}

static inline void cec_msg_get_cec_version(struct cec_msg *msg,
					   bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_GET_CEC_VERSION;
	msg->reply = reply ? CEC_MSG_CEC_VERSION : 0;
}

static inline void cec_msg_report_physical_addr(struct cec_msg *msg,
					__u16 phys_addr, __u8 prim_devtype)
{
	msg->len = 5;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_REPORT_PHYSICAL_ADDR;
	msg->msg[2] = phys_addr >> 8;
	msg->msg[3] = phys_addr & 0xff;
	msg->msg[4] = prim_devtype;
}

static inline void cec_ops_report_physical_addr(const struct cec_msg *msg,
					__u16 *phys_addr, __u8 *prim_devtype)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
	*prim_devtype = msg->msg[4];
}

static inline void cec_msg_give_physical_addr(struct cec_msg *msg,
					      bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_GIVE_PHYSICAL_ADDR;
	msg->reply = reply ? CEC_MSG_REPORT_PHYSICAL_ADDR : 0;
}

static inline void cec_msg_set_menu_language(struct cec_msg *msg,
					     const char *language)
{
	msg->len = 5;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_SET_MENU_LANGUAGE;
	memcpy(msg->msg + 2, language, 3);
}

static inline void cec_ops_set_menu_language(const struct cec_msg *msg,
					     char *language)
{
	memcpy(language, msg->msg + 2, 3);
	language[3] = '\0';
}

static inline void cec_msg_get_menu_language(struct cec_msg *msg,
					     bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_GET_MENU_LANGUAGE;
	msg->reply = reply ? CEC_MSG_SET_MENU_LANGUAGE : 0;
}

/*
 * Assumes a single RC Profile byte and a single Device Features byte,
 * i.e. no extended features are supported by this helper function.
 *
 * As of CEC 2.0 no extended features are defined, should those be added
 * in the future, then this function needs to be adapted or a new function
 * should be added.
 */
static inline void cec_msg_report_features(struct cec_msg *msg,
				__u8 cec_version, __u8 all_device_types,
				__u8 rc_profile, __u8 dev_features)
{
	msg->len = 6;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_REPORT_FEATURES;
	msg->msg[2] = cec_version;
	msg->msg[3] = all_device_types;
	msg->msg[4] = rc_profile;
	msg->msg[5] = dev_features;
}

static inline void cec_ops_report_features(const struct cec_msg *msg,
			__u8 *cec_version, __u8 *all_device_types,
			const __u8 **rc_profile, const __u8 **dev_features)
{
	const __u8 *p = &msg->msg[4];

	*cec_version = msg->msg[2];
	*all_device_types = msg->msg[3];
	*rc_profile = p;
	while (p < &msg->msg[14] && (*p & CEC_OP_FEAT_EXT))
		p++;
	if (!(*p & CEC_OP_FEAT_EXT)) {
		*dev_features = p + 1;
		while (p < &msg->msg[15] && (*p & CEC_OP_FEAT_EXT))
			p++;
	}
	if (*p & CEC_OP_FEAT_EXT)
		*rc_profile = *dev_features = NULL;
}

static inline void cec_msg_give_features(struct cec_msg *msg,
					 bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_GIVE_FEATURES;
	msg->reply = reply ? CEC_MSG_REPORT_FEATURES : 0;
}

/* Deck Control Feature */
static inline void cec_msg_deck_control(struct cec_msg *msg,
					__u8 deck_control_mode)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_DECK_CONTROL;
	msg->msg[2] = deck_control_mode;
}

static inline void cec_ops_deck_control(const struct cec_msg *msg,
					__u8 *deck_control_mode)
{
	*deck_control_mode = msg->msg[2];
}

static inline void cec_msg_deck_status(struct cec_msg *msg,
				       __u8 deck_info)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_DECK_STATUS;
	msg->msg[2] = deck_info;
}

static inline void cec_ops_deck_status(const struct cec_msg *msg,
				       __u8 *deck_info)
{
	*deck_info = msg->msg[2];
}

static inline void cec_msg_give_deck_status(struct cec_msg *msg,
					    bool reply,
					    __u8 status_req)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_GIVE_DECK_STATUS;
	msg->msg[2] = status_req;
	msg->reply = reply ? CEC_MSG_DECK_STATUS : 0;
}

static inline void cec_ops_give_deck_status(const struct cec_msg *msg,
					    __u8 *status_req)
{
	*status_req = msg->msg[2];
}

static inline void cec_msg_play(struct cec_msg *msg,
				__u8 play_mode)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_PLAY;
	msg->msg[2] = play_mode;
}

static inline void cec_ops_play(const struct cec_msg *msg,
				__u8 *play_mode)
{
	*play_mode = msg->msg[2];
}


/* Tuner Control Feature */
struct cec_op_tuner_device_info {
	__u8 rec_flag;
	__u8 tuner_display_info;
	bool is_analog;
	union {
		struct cec_op_digital_service_id digital;
		struct {
			__u8 ana_bcast_type;
			__u16 ana_freq;
			__u8 bcast_system;
		} analog;
	};
};

static inline void cec_msg_tuner_device_status_analog(struct cec_msg *msg,
						      __u8 rec_flag,
						      __u8 tuner_display_info,
						      __u8 ana_bcast_type,
						      __u16 ana_freq,
						      __u8 bcast_system)
{
	msg->len = 7;
	msg->msg[1] = CEC_MSG_TUNER_DEVICE_STATUS;
	msg->msg[2] = (rec_flag << 7) | tuner_display_info;
	msg->msg[3] = ana_bcast_type;
	msg->msg[4] = ana_freq >> 8;
	msg->msg[5] = ana_freq & 0xff;
	msg->msg[6] = bcast_system;
}

static inline void cec_msg_tuner_device_status_digital(struct cec_msg *msg,
		   __u8 rec_flag, __u8 tuner_display_info,
		   const struct cec_op_digital_service_id *digital)
{
	msg->len = 10;
	msg->msg[1] = CEC_MSG_TUNER_DEVICE_STATUS;
	msg->msg[2] = (rec_flag << 7) | tuner_display_info;
	cec_set_digital_service_id(msg->msg + 3, digital);
}

static inline void cec_msg_tuner_device_status(struct cec_msg *msg,
			const struct cec_op_tuner_device_info *tuner_dev_info)
{
	if (tuner_dev_info->is_analog)
		cec_msg_tuner_device_status_analog(msg,
			tuner_dev_info->rec_flag,
			tuner_dev_info->tuner_display_info,
			tuner_dev_info->analog.ana_bcast_type,
			tuner_dev_info->analog.ana_freq,
			tuner_dev_info->analog.bcast_system);
	else
		cec_msg_tuner_device_status_digital(msg,
			tuner_dev_info->rec_flag,
			tuner_dev_info->tuner_display_info,
			&tuner_dev_info->digital);
}

static inline void cec_ops_tuner_device_status(const struct cec_msg *msg,
				struct cec_op_tuner_device_info *tuner_dev_info)
{
	tuner_dev_info->is_analog = msg->len < 10;
	tuner_dev_info->rec_flag = msg->msg[2] >> 7;
	tuner_dev_info->tuner_display_info = msg->msg[2] & 0x7f;
	if (tuner_dev_info->is_analog) {
		tuner_dev_info->analog.ana_bcast_type = msg->msg[3];
		tuner_dev_info->analog.ana_freq = (msg->msg[4] << 8) | msg->msg[5];
		tuner_dev_info->analog.bcast_system = msg->msg[6];
		return;
	}
	cec_get_digital_service_id(msg->msg + 3, &tuner_dev_info->digital);
}

static inline void cec_msg_give_tuner_device_status(struct cec_msg *msg,
						    bool reply,
						    __u8 status_req)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_GIVE_TUNER_DEVICE_STATUS;
	msg->msg[2] = status_req;
	msg->reply = reply ? CEC_MSG_TUNER_DEVICE_STATUS : 0;
}

static inline void cec_ops_give_tuner_device_status(const struct cec_msg *msg,
						    __u8 *status_req)
{
	*status_req = msg->msg[2];
}

static inline void cec_msg_select_analogue_service(struct cec_msg *msg,
						   __u8 ana_bcast_type,
						   __u16 ana_freq,
						   __u8 bcast_system)
{
	msg->len = 6;
	msg->msg[1] = CEC_MSG_SELECT_ANALOGUE_SERVICE;
	msg->msg[2] = ana_bcast_type;
	msg->msg[3] = ana_freq >> 8;
	msg->msg[4] = ana_freq & 0xff;
	msg->msg[5] = bcast_system;
}

static inline void cec_ops_select_analogue_service(const struct cec_msg *msg,
						   __u8 *ana_bcast_type,
						   __u16 *ana_freq,
						   __u8 *bcast_system)
{
	*ana_bcast_type = msg->msg[2];
	*ana_freq = (msg->msg[3] << 8) | msg->msg[4];
	*bcast_system = msg->msg[5];
}

static inline void cec_msg_select_digital_service(struct cec_msg *msg,
				const struct cec_op_digital_service_id *digital)
{
	msg->len = 9;
	msg->msg[1] = CEC_MSG_SELECT_DIGITAL_SERVICE;
	cec_set_digital_service_id(msg->msg + 2, digital);
}

static inline void cec_ops_select_digital_service(const struct cec_msg *msg,
				struct cec_op_digital_service_id *digital)
{
	cec_get_digital_service_id(msg->msg + 2, digital);
}

static inline void cec_msg_tuner_step_decrement(struct cec_msg *msg)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_TUNER_STEP_DECREMENT;
}

static inline void cec_msg_tuner_step_increment(struct cec_msg *msg)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_TUNER_STEP_INCREMENT;
}


/* Vendor Specific Commands Feature */
static inline void cec_msg_device_vendor_id(struct cec_msg *msg, __u32 vendor_id)
{
	msg->len = 5;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_DEVICE_VENDOR_ID;
	msg->msg[2] = vendor_id >> 16;
	msg->msg[3] = (vendor_id >> 8) & 0xff;
	msg->msg[4] = vendor_id & 0xff;
}

static inline void cec_ops_device_vendor_id(const struct cec_msg *msg,
					    __u32 *vendor_id)
{
	*vendor_id = (msg->msg[2] << 16) | (msg->msg[3] << 8) | msg->msg[4];
}

static inline void cec_msg_give_device_vendor_id(struct cec_msg *msg,
						 bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_GIVE_DEVICE_VENDOR_ID;
	msg->reply = reply ? CEC_MSG_DEVICE_VENDOR_ID : 0;
}

static inline void cec_msg_vendor_command(struct cec_msg *msg,
					  __u8 size, const __u8 *vendor_cmd)
{
	if (size > 14)
		size = 14;
	msg->len = 2 + size;
	msg->msg[1] = CEC_MSG_VENDOR_COMMAND;
	memcpy(msg->msg + 2, vendor_cmd, size);
}

static inline void cec_ops_vendor_command(const struct cec_msg *msg,
					  __u8 *size,
					  const __u8 **vendor_cmd)
{
	*size = msg->len - 2;

	if (*size > 14)
		*size = 14;
	*vendor_cmd = msg->msg + 2;
}

static inline void cec_msg_vendor_command_with_id(struct cec_msg *msg,
						  __u32 vendor_id, __u8 size,
						  const __u8 *vendor_cmd)
{
	if (size > 11)
		size = 11;
	msg->len = 5 + size;
	msg->msg[1] = CEC_MSG_VENDOR_COMMAND_WITH_ID;
	msg->msg[2] = vendor_id >> 16;
	msg->msg[3] = (vendor_id >> 8) & 0xff;
	msg->msg[4] = vendor_id & 0xff;
	memcpy(msg->msg + 5, vendor_cmd, size);
}

static inline void cec_ops_vendor_command_with_id(const struct cec_msg *msg,
						  __u32 *vendor_id,  __u8 *size,
						  const __u8 **vendor_cmd)
{
	*size = msg->len - 5;

	if (*size > 11)
		*size = 11;
	*vendor_id = (msg->msg[2] << 16) | (msg->msg[3] << 8) | msg->msg[4];
	*vendor_cmd = msg->msg + 5;
}

static inline void cec_msg_vendor_remote_button_down(struct cec_msg *msg,
						     __u8 size,
						     const __u8 *rc_code)
{
	if (size > 14)
		size = 14;
	msg->len = 2 + size;
	msg->msg[1] = CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN;
	memcpy(msg->msg + 2, rc_code, size);
}

static inline void cec_ops_vendor_remote_button_down(const struct cec_msg *msg,
						     __u8 *size,
						     const __u8 **rc_code)
{
	*size = msg->len - 2;

	if (*size > 14)
		*size = 14;
	*rc_code = msg->msg + 2;
}

static inline void cec_msg_vendor_remote_button_up(struct cec_msg *msg)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_VENDOR_REMOTE_BUTTON_UP;
}


/* OSD Display Feature */
static inline void cec_msg_set_osd_string(struct cec_msg *msg,
					  __u8 disp_ctl,
					  const char *osd)
{
	unsigned int len = strlen(osd);

	if (len > 13)
		len = 13;
	msg->len = 3 + len;
	msg->msg[1] = CEC_MSG_SET_OSD_STRING;
	msg->msg[2] = disp_ctl;
	memcpy(msg->msg + 3, osd, len);
}

static inline void cec_ops_set_osd_string(const struct cec_msg *msg,
					  __u8 *disp_ctl,
					  char *osd)
{
	unsigned int len = msg->len > 3 ? msg->len - 3 : 0;

	*disp_ctl = msg->msg[2];
	if (len > 13)
		len = 13;
	memcpy(osd, msg->msg + 3, len);
	osd[len] = '\0';
}


/* Device OSD Transfer Feature */
static inline void cec_msg_set_osd_name(struct cec_msg *msg, const char *name)
{
	unsigned int len = strlen(name);

	if (len > 14)
		len = 14;
	msg->len = 2 + len;
	msg->msg[1] = CEC_MSG_SET_OSD_NAME;
	memcpy(msg->msg + 2, name, len);
}

static inline void cec_ops_set_osd_name(const struct cec_msg *msg,
					char *name)
{
	unsigned int len = msg->len > 2 ? msg->len - 2 : 0;

	if (len > 14)
		len = 14;
	memcpy(name, msg->msg + 2, len);
	name[len] = '\0';
}

static inline void cec_msg_give_osd_name(struct cec_msg *msg,
					 bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_GIVE_OSD_NAME;
	msg->reply = reply ? CEC_MSG_SET_OSD_NAME : 0;
}


/* Device Menu Control Feature */
static inline void cec_msg_menu_status(struct cec_msg *msg,
				       __u8 menu_state)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_MENU_STATUS;
	msg->msg[2] = menu_state;
}

static inline void cec_ops_menu_status(const struct cec_msg *msg,
				       __u8 *menu_state)
{
	*menu_state = msg->msg[2];
}

static inline void cec_msg_menu_request(struct cec_msg *msg,
					bool reply,
					__u8 menu_req)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_MENU_REQUEST;
	msg->msg[2] = menu_req;
	msg->reply = reply ? CEC_MSG_MENU_STATUS : 0;
}

static inline void cec_ops_menu_request(const struct cec_msg *msg,
					__u8 *menu_req)
{
	*menu_req = msg->msg[2];
}

struct cec_op_ui_command {
	__u8 ui_cmd;
	bool has_opt_arg;
	union {
		struct cec_op_channel_data channel_identifier;
		__u8 ui_broadcast_type;
		__u8 ui_sound_presentation_control;
		__u8 play_mode;
		__u8 ui_function_media;
		__u8 ui_function_select_av_input;
		__u8 ui_function_select_audio_input;
	};
};

static inline void cec_msg_user_control_pressed(struct cec_msg *msg,
					const struct cec_op_ui_command *ui_cmd)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_USER_CONTROL_PRESSED;
	msg->msg[2] = ui_cmd->ui_cmd;
	if (!ui_cmd->has_opt_arg)
		return;
	switch (ui_cmd->ui_cmd) {
	case 0x56:
	case 0x57:
	case 0x60:
	case 0x68:
	case 0x69:
	case 0x6a:
		/* The optional operand is one byte for all these ui commands */
		msg->len++;
		msg->msg[3] = ui_cmd->play_mode;
		break;
	case 0x67:
		msg->len += 4;
		msg->msg[3] = (ui_cmd->channel_identifier.channel_number_fmt << 2) |
			      (ui_cmd->channel_identifier.major >> 8);
		msg->msg[4] = ui_cmd->channel_identifier.major & 0xff;
		msg->msg[5] = ui_cmd->channel_identifier.minor >> 8;
		msg->msg[6] = ui_cmd->channel_identifier.minor & 0xff;
		break;
	}
}

static inline void cec_ops_user_control_pressed(const struct cec_msg *msg,
						struct cec_op_ui_command *ui_cmd)
{
	ui_cmd->ui_cmd = msg->msg[2];
	ui_cmd->has_opt_arg = false;
	if (msg->len == 3)
		return;
	switch (ui_cmd->ui_cmd) {
	case 0x56:
	case 0x57:
	case 0x60:
	case 0x68:
	case 0x69:
	case 0x6a:
		/* The optional operand is one byte for all these ui commands */
		ui_cmd->play_mode = msg->msg[3];
		ui_cmd->has_opt_arg = true;
		break;
	case 0x67:
		if (msg->len < 7)
			break;
		ui_cmd->has_opt_arg = true;
		ui_cmd->channel_identifier.channel_number_fmt = msg->msg[3] >> 2;
		ui_cmd->channel_identifier.major = ((msg->msg[3] & 3) << 6) | msg->msg[4];
		ui_cmd->channel_identifier.minor = (msg->msg[5] << 8) | msg->msg[6];
		break;
	}
}

static inline void cec_msg_user_control_released(struct cec_msg *msg)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_USER_CONTROL_RELEASED;
}

/* Remote Control Passthrough Feature */

/* Power Status Feature */
static inline void cec_msg_report_power_status(struct cec_msg *msg,
					       __u8 pwr_state)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_REPORT_POWER_STATUS;
	msg->msg[2] = pwr_state;
}

static inline void cec_ops_report_power_status(const struct cec_msg *msg,
					       __u8 *pwr_state)
{
	*pwr_state = msg->msg[2];
}

static inline void cec_msg_give_device_power_status(struct cec_msg *msg,
						    bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_GIVE_DEVICE_POWER_STATUS;
	msg->reply = reply ? CEC_MSG_REPORT_POWER_STATUS : 0;
}

/* General Protocol Messages */
static inline void cec_msg_feature_abort(struct cec_msg *msg,
					 __u8 abort_msg, __u8 reason)
{
	msg->len = 4;
	msg->msg[1] = CEC_MSG_FEATURE_ABORT;
	msg->msg[2] = abort_msg;
	msg->msg[3] = reason;
}

static inline void cec_ops_feature_abort(const struct cec_msg *msg,
					 __u8 *abort_msg, __u8 *reason)
{
	*abort_msg = msg->msg[2];
	*reason = msg->msg[3];
}

/* This changes the current message into a feature abort message */
static inline void cec_msg_reply_feature_abort(struct cec_msg *msg, __u8 reason)
{
	cec_msg_set_reply_to(msg, msg);
	msg->len = 4;
	msg->msg[2] = msg->msg[1];
	msg->msg[3] = reason;
	msg->msg[1] = CEC_MSG_FEATURE_ABORT;
}

static inline void cec_msg_abort(struct cec_msg *msg)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_ABORT;
}


/* System Audio Control Feature */
static inline void cec_msg_report_audio_status(struct cec_msg *msg,
					       __u8 aud_mute_status,
					       __u8 aud_vol_status)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_REPORT_AUDIO_STATUS;
	msg->msg[2] = (aud_mute_status << 7) | (aud_vol_status & 0x7f);
}

static inline void cec_ops_report_audio_status(const struct cec_msg *msg,
					       __u8 *aud_mute_status,
					       __u8 *aud_vol_status)
{
	*aud_mute_status = msg->msg[2] >> 7;
	*aud_vol_status = msg->msg[2] & 0x7f;
}

static inline void cec_msg_give_audio_status(struct cec_msg *msg,
					     bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_GIVE_AUDIO_STATUS;
	msg->reply = reply ? CEC_MSG_REPORT_AUDIO_STATUS : 0;
}

static inline void cec_msg_set_system_audio_mode(struct cec_msg *msg,
						 __u8 sys_aud_status)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_SET_SYSTEM_AUDIO_MODE;
	msg->msg[2] = sys_aud_status;
}

static inline void cec_ops_set_system_audio_mode(const struct cec_msg *msg,
						 __u8 *sys_aud_status)
{
	*sys_aud_status = msg->msg[2];
}

static inline void cec_msg_system_audio_mode_request(struct cec_msg *msg,
						     bool reply,
						     __u16 phys_addr)
{
	msg->len = phys_addr == 0xffff ? 2 : 4;
	msg->msg[1] = CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST;
	msg->msg[2] = phys_addr >> 8;
	msg->msg[3] = phys_addr & 0xff;
	msg->reply = reply ? CEC_MSG_SET_SYSTEM_AUDIO_MODE : 0;

}

static inline void cec_ops_system_audio_mode_request(const struct cec_msg *msg,
						     __u16 *phys_addr)
{
	if (msg->len < 4)
		*phys_addr = 0xffff;
	else
		*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
}

static inline void cec_msg_system_audio_mode_status(struct cec_msg *msg,
						    __u8 sys_aud_status)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_SYSTEM_AUDIO_MODE_STATUS;
	msg->msg[2] = sys_aud_status;
}

static inline void cec_ops_system_audio_mode_status(const struct cec_msg *msg,
						    __u8 *sys_aud_status)
{
	*sys_aud_status = msg->msg[2];
}

static inline void cec_msg_give_system_audio_mode_status(struct cec_msg *msg,
							 bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS;
	msg->reply = reply ? CEC_MSG_SYSTEM_AUDIO_MODE_STATUS : 0;
}

static inline void cec_msg_report_short_audio_descriptor(struct cec_msg *msg,
					__u8 num_descriptors,
					const __u32 *descriptors)
{
	unsigned int i;

	if (num_descriptors > 4)
		num_descriptors = 4;
	msg->len = 2 + num_descriptors * 3;
	msg->msg[1] = CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR;
	for (i = 0; i < num_descriptors; i++) {
		msg->msg[2 + i * 3] = (descriptors[i] >> 16) & 0xff;
		msg->msg[3 + i * 3] = (descriptors[i] >> 8) & 0xff;
		msg->msg[4 + i * 3] = descriptors[i] & 0xff;
	}
}

static inline void cec_ops_report_short_audio_descriptor(const struct cec_msg *msg,
							 __u8 *num_descriptors,
							 __u32 *descriptors)
{
	unsigned int i;

	*num_descriptors = (msg->len - 2) / 3;
	if (*num_descriptors > 4)
		*num_descriptors = 4;
	for (i = 0; i < *num_descriptors; i++)
		descriptors[i] = (msg->msg[2 + i * 3] << 16) |
			(msg->msg[3 + i * 3] << 8) |
			msg->msg[4 + i * 3];
}

static inline void cec_msg_request_short_audio_descriptor(struct cec_msg *msg,
					bool reply,
					__u8 num_descriptors,
					const __u8 *audio_format_id,
					const __u8 *audio_format_code)
{
	unsigned int i;

	if (num_descriptors > 4)
		num_descriptors = 4;
	msg->len = 2 + num_descriptors;
	msg->msg[1] = CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR;
	msg->reply = reply ? CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR : 0;
	for (i = 0; i < num_descriptors; i++)
		msg->msg[2 + i] = (audio_format_id[i] << 6) |
				  (audio_format_code[i] & 0x3f);
}

static inline void cec_ops_request_short_audio_descriptor(const struct cec_msg *msg,
					__u8 *num_descriptors,
					__u8 *audio_format_id,
					__u8 *audio_format_code)
{
	unsigned int i;

	*num_descriptors = msg->len - 2;
	if (*num_descriptors > 4)
		*num_descriptors = 4;
	for (i = 0; i < *num_descriptors; i++) {
		audio_format_id[i] = msg->msg[2 + i] >> 6;
		audio_format_code[i] = msg->msg[2 + i] & 0x3f;
	}
}


/* Audio Rate Control Feature */
static inline void cec_msg_set_audio_rate(struct cec_msg *msg,
					  __u8 audio_rate)
{
	msg->len = 3;
	msg->msg[1] = CEC_MSG_SET_AUDIO_RATE;
	msg->msg[2] = audio_rate;
}

static inline void cec_ops_set_audio_rate(const struct cec_msg *msg,
					  __u8 *audio_rate)
{
	*audio_rate = msg->msg[2];
}


/* Audio Return Channel Control Feature */
static inline void cec_msg_report_arc_initiated(struct cec_msg *msg)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_REPORT_ARC_INITIATED;
}

static inline void cec_msg_initiate_arc(struct cec_msg *msg,
					bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_INITIATE_ARC;
	msg->reply = reply ? CEC_MSG_REPORT_ARC_INITIATED : 0;
}

static inline void cec_msg_request_arc_initiation(struct cec_msg *msg,
						  bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_REQUEST_ARC_INITIATION;
	msg->reply = reply ? CEC_MSG_INITIATE_ARC : 0;
}

static inline void cec_msg_report_arc_terminated(struct cec_msg *msg)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_REPORT_ARC_TERMINATED;
}

static inline void cec_msg_terminate_arc(struct cec_msg *msg,
					 bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_TERMINATE_ARC;
	msg->reply = reply ? CEC_MSG_REPORT_ARC_TERMINATED : 0;
}

static inline void cec_msg_request_arc_termination(struct cec_msg *msg,
						   bool reply)
{
	msg->len = 2;
	msg->msg[1] = CEC_MSG_REQUEST_ARC_TERMINATION;
	msg->reply = reply ? CEC_MSG_TERMINATE_ARC : 0;
}


/* Dynamic Audio Lipsync Feature */
/* Only for CEC 2.0 and up */
static inline void cec_msg_report_current_latency(struct cec_msg *msg,
						  __u16 phys_addr,
						  __u8 video_latency,
						  __u8 low_latency_mode,
						  __u8 audio_out_compensated,
						  __u8 audio_out_delay)
{
	msg->len = 7;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_REPORT_CURRENT_LATENCY;
	msg->msg[2] = phys_addr >> 8;
	msg->msg[3] = phys_addr & 0xff;
	msg->msg[4] = video_latency;
	msg->msg[5] = (low_latency_mode << 2) | audio_out_compensated;
	msg->msg[6] = audio_out_delay;
}

static inline void cec_ops_report_current_latency(const struct cec_msg *msg,
						  __u16 *phys_addr,
						  __u8 *video_latency,
						  __u8 *low_latency_mode,
						  __u8 *audio_out_compensated,
						  __u8 *audio_out_delay)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
	*video_latency = msg->msg[4];
	*low_latency_mode = (msg->msg[5] >> 2) & 1;
	*audio_out_compensated = msg->msg[5] & 3;
	*audio_out_delay = msg->msg[6];
}

static inline void cec_msg_request_current_latency(struct cec_msg *msg,
						   bool reply,
						   __u16 phys_addr)
{
	msg->len = 4;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_REQUEST_CURRENT_LATENCY;
	msg->msg[2] = phys_addr >> 8;
	msg->msg[3] = phys_addr & 0xff;
	msg->reply = reply ? CEC_MSG_REPORT_CURRENT_LATENCY : 0;
}

static inline void cec_ops_request_current_latency(const struct cec_msg *msg,
						   __u16 *phys_addr)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
}


/* Capability Discovery and Control Feature */
static inline void cec_msg_cdc_hec_inquire_state(struct cec_msg *msg,
						 __u16 phys_addr1,
						 __u16 phys_addr2)
{
	msg->len = 9;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_CDC_MESSAGE;
	/* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
	msg->msg[4] = CEC_MSG_CDC_HEC_INQUIRE_STATE;
	msg->msg[5] = phys_addr1 >> 8;
	msg->msg[6] = phys_addr1 & 0xff;
	msg->msg[7] = phys_addr2 >> 8;
	msg->msg[8] = phys_addr2 & 0xff;
}

static inline void cec_ops_cdc_hec_inquire_state(const struct cec_msg *msg,
						 __u16 *phys_addr,
						 __u16 *phys_addr1,
						 __u16 *phys_addr2)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
	*phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
	*phys_addr2 = (msg->msg[7] << 8) | msg->msg[8];
}

static inline void cec_msg_cdc_hec_report_state(struct cec_msg *msg,
						__u16 target_phys_addr,
						__u8 hec_func_state,
						__u8 host_func_state,
						__u8 enc_func_state,
						__u8 cdc_errcode,
						__u8 has_field,
						__u16 hec_field)
{
	msg->len = has_field ? 10 : 8;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_CDC_MESSAGE;
	/* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
	msg->msg[4] = CEC_MSG_CDC_HEC_REPORT_STATE;
	msg->msg[5] = target_phys_addr >> 8;
	msg->msg[6] = target_phys_addr & 0xff;
	msg->msg[7] = (hec_func_state << 6) |
		      (host_func_state << 4) |
		      (enc_func_state << 2) |
		      cdc_errcode;
	if (has_field) {
		msg->msg[8] = hec_field >> 8;
		msg->msg[9] = hec_field & 0xff;
	}
}

static inline void cec_ops_cdc_hec_report_state(const struct cec_msg *msg,
						__u16 *phys_addr,
						__u16 *target_phys_addr,
						__u8 *hec_func_state,
						__u8 *host_func_state,
						__u8 *enc_func_state,
						__u8 *cdc_errcode,
						__u8 *has_field,
						__u16 *hec_field)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
	*target_phys_addr = (msg->msg[5] << 8) | msg->msg[6];
	*hec_func_state = msg->msg[7] >> 6;
	*host_func_state = (msg->msg[7] >> 4) & 3;
	*enc_func_state = (msg->msg[7] >> 4) & 3;
	*cdc_errcode = msg->msg[7] & 3;
	*has_field = msg->len >= 10;
	*hec_field = *has_field ? ((msg->msg[8] << 8) | msg->msg[9]) : 0;
}

static inline void cec_msg_cdc_hec_set_state(struct cec_msg *msg,
					     __u16 phys_addr1,
					     __u16 phys_addr2,
					     __u8 hec_set_state,
					     __u16 phys_addr3,
					     __u16 phys_addr4,
					     __u16 phys_addr5)
{
	msg->len = 10;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_CDC_MESSAGE;
	/* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
	msg->msg[4] = CEC_MSG_CDC_HEC_INQUIRE_STATE;
	msg->msg[5] = phys_addr1 >> 8;
	msg->msg[6] = phys_addr1 & 0xff;
	msg->msg[7] = phys_addr2 >> 8;
	msg->msg[8] = phys_addr2 & 0xff;
	msg->msg[9] = hec_set_state;
	if (phys_addr3 != CEC_PHYS_ADDR_INVALID) {
		msg->msg[msg->len++] = phys_addr3 >> 8;
		msg->msg[msg->len++] = phys_addr3 & 0xff;
		if (phys_addr4 != CEC_PHYS_ADDR_INVALID) {
			msg->msg[msg->len++] = phys_addr4 >> 8;
			msg->msg[msg->len++] = phys_addr4 & 0xff;
			if (phys_addr5 != CEC_PHYS_ADDR_INVALID) {
				msg->msg[msg->len++] = phys_addr5 >> 8;
				msg->msg[msg->len++] = phys_addr5 & 0xff;
			}
		}
	}
}

static inline void cec_ops_cdc_hec_set_state(const struct cec_msg *msg,
					     __u16 *phys_addr,
					     __u16 *phys_addr1,
					     __u16 *phys_addr2,
					     __u8 *hec_set_state,
					     __u16 *phys_addr3,
					     __u16 *phys_addr4,
					     __u16 *phys_addr5)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
	*phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
	*phys_addr2 = (msg->msg[7] << 8) | msg->msg[8];
	*hec_set_state = msg->msg[9];
	*phys_addr3 = *phys_addr4 = *phys_addr5 = CEC_PHYS_ADDR_INVALID;
	if (msg->len >= 12)
		*phys_addr3 = (msg->msg[10] << 8) | msg->msg[11];
	if (msg->len >= 14)
		*phys_addr4 = (msg->msg[12] << 8) | msg->msg[13];
	if (msg->len >= 16)
		*phys_addr5 = (msg->msg[14] << 8) | msg->msg[15];
}

static inline void cec_msg_cdc_hec_set_state_adjacent(struct cec_msg *msg,
						      __u16 phys_addr1,
						      __u8 hec_set_state)
{
	msg->len = 8;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_CDC_MESSAGE;
	/* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
	msg->msg[4] = CEC_MSG_CDC_HEC_SET_STATE_ADJACENT;
	msg->msg[5] = phys_addr1 >> 8;
	msg->msg[6] = phys_addr1 & 0xff;
	msg->msg[7] = hec_set_state;
}

static inline void cec_ops_cdc_hec_set_state_adjacent(const struct cec_msg *msg,
						      __u16 *phys_addr,
						      __u16 *phys_addr1,
						      __u8 *hec_set_state)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
	*phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
	*hec_set_state = msg->msg[7];
}

static inline void cec_msg_cdc_hec_request_deactivation(struct cec_msg *msg,
							__u16 phys_addr1,
							__u16 phys_addr2,
							__u16 phys_addr3)
{
	msg->len = 11;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_CDC_MESSAGE;
	/* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
	msg->msg[4] = CEC_MSG_CDC_HEC_REQUEST_DEACTIVATION;
	msg->msg[5] = phys_addr1 >> 8;
	msg->msg[6] = phys_addr1 & 0xff;
	msg->msg[7] = phys_addr2 >> 8;
	msg->msg[8] = phys_addr2 & 0xff;
	msg->msg[9] = phys_addr3 >> 8;
	msg->msg[10] = phys_addr3 & 0xff;
}

static inline void cec_ops_cdc_hec_request_deactivation(const struct cec_msg *msg,
							__u16 *phys_addr,
							__u16 *phys_addr1,
							__u16 *phys_addr2,
							__u16 *phys_addr3)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
	*phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
	*phys_addr2 = (msg->msg[7] << 8) | msg->msg[8];
	*phys_addr3 = (msg->msg[9] << 8) | msg->msg[10];
}

static inline void cec_msg_cdc_hec_notify_alive(struct cec_msg *msg)
{
	msg->len = 5;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_CDC_MESSAGE;
	/* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
	msg->msg[4] = CEC_MSG_CDC_HEC_NOTIFY_ALIVE;
}

static inline void cec_ops_cdc_hec_notify_alive(const struct cec_msg *msg,
						__u16 *phys_addr)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
}

static inline void cec_msg_cdc_hec_discover(struct cec_msg *msg)
{
	msg->len = 5;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_CDC_MESSAGE;
	/* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
	msg->msg[4] = CEC_MSG_CDC_HEC_DISCOVER;
}

static inline void cec_ops_cdc_hec_discover(const struct cec_msg *msg,
					    __u16 *phys_addr)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
}

static inline void cec_msg_cdc_hpd_set_state(struct cec_msg *msg,
					     __u8 input_port,
					     __u8 hpd_state)
{
	msg->len = 6;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_CDC_MESSAGE;
	/* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
	msg->msg[4] = CEC_MSG_CDC_HPD_SET_STATE;
	msg->msg[5] = (input_port << 4) | hpd_state;
}

static inline void cec_ops_cdc_hpd_set_state(const struct cec_msg *msg,
					    __u16 *phys_addr,
					    __u8 *input_port,
					    __u8 *hpd_state)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
	*input_port = msg->msg[5] >> 4;
	*hpd_state = msg->msg[5] & 0xf;
}

static inline void cec_msg_cdc_hpd_report_state(struct cec_msg *msg,
						__u8 hpd_state,
						__u8 hpd_error)
{
	msg->len = 6;
	msg->msg[0] |= 0xf; /* broadcast */
	msg->msg[1] = CEC_MSG_CDC_MESSAGE;
	/* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
	msg->msg[4] = CEC_MSG_CDC_HPD_REPORT_STATE;
	msg->msg[5] = (hpd_state << 4) | hpd_error;
}

static inline void cec_ops_cdc_hpd_report_state(const struct cec_msg *msg,
						__u16 *phys_addr,
						__u8 *hpd_state,
						__u8 *hpd_error)
{
	*phys_addr = (msg->msg[2] << 8) | msg->msg[3];
	*hpd_state = msg->msg[5] >> 4;
	*hpd_error = msg->msg[5] & 0xf;
}

#endif