GNU Linux-libre 4.9.332-gnu1
[releases.git] / include / linux / cec-funcs.h
1 /*
2  * cec - HDMI Consumer Electronics Control message functions
3  *
4  * Copyright 2016 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5  *
6  * This program is free software; you may redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * Alternatively you can redistribute this file under the terms of the
11  * BSD license as stated below:
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in
20  *    the documentation and/or other materials provided with the
21  *    distribution.
22  * 3. The names of its contributors may not be used to endorse or promote
23  *    products derived from this software without specific prior written
24  *    permission.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  */
35
36 /*
37  * Note: this framework is still in staging and it is likely the API
38  * will change before it goes out of staging.
39  *
40  * Once it is moved out of staging this header will move to uapi.
41  */
42 #ifndef _CEC_UAPI_FUNCS_H
43 #define _CEC_UAPI_FUNCS_H
44
45 #include <linux/cec.h>
46
47 /* One Touch Play Feature */
48 static inline void cec_msg_active_source(struct cec_msg *msg, __u16 phys_addr)
49 {
50         msg->len = 4;
51         msg->msg[0] |= 0xf; /* broadcast */
52         msg->msg[1] = CEC_MSG_ACTIVE_SOURCE;
53         msg->msg[2] = phys_addr >> 8;
54         msg->msg[3] = phys_addr & 0xff;
55 }
56
57 static inline void cec_ops_active_source(const struct cec_msg *msg,
58                                          __u16 *phys_addr)
59 {
60         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
61 }
62
63 static inline void cec_msg_image_view_on(struct cec_msg *msg)
64 {
65         msg->len = 2;
66         msg->msg[1] = CEC_MSG_IMAGE_VIEW_ON;
67 }
68
69 static inline void cec_msg_text_view_on(struct cec_msg *msg)
70 {
71         msg->len = 2;
72         msg->msg[1] = CEC_MSG_TEXT_VIEW_ON;
73 }
74
75
76 /* Routing Control Feature */
77 static inline void cec_msg_inactive_source(struct cec_msg *msg,
78                                            __u16 phys_addr)
79 {
80         msg->len = 4;
81         msg->msg[1] = CEC_MSG_INACTIVE_SOURCE;
82         msg->msg[2] = phys_addr >> 8;
83         msg->msg[3] = phys_addr & 0xff;
84 }
85
86 static inline void cec_ops_inactive_source(const struct cec_msg *msg,
87                                            __u16 *phys_addr)
88 {
89         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
90 }
91
92 static inline void cec_msg_request_active_source(struct cec_msg *msg,
93                                                  bool reply)
94 {
95         msg->len = 2;
96         msg->msg[0] |= 0xf; /* broadcast */
97         msg->msg[1] = CEC_MSG_REQUEST_ACTIVE_SOURCE;
98         msg->reply = reply ? CEC_MSG_ACTIVE_SOURCE : 0;
99 }
100
101 static inline void cec_msg_routing_information(struct cec_msg *msg,
102                                                __u16 phys_addr)
103 {
104         msg->len = 4;
105         msg->msg[0] |= 0xf; /* broadcast */
106         msg->msg[1] = CEC_MSG_ROUTING_INFORMATION;
107         msg->msg[2] = phys_addr >> 8;
108         msg->msg[3] = phys_addr & 0xff;
109 }
110
111 static inline void cec_ops_routing_information(const struct cec_msg *msg,
112                                                __u16 *phys_addr)
113 {
114         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
115 }
116
117 static inline void cec_msg_routing_change(struct cec_msg *msg,
118                                           bool reply,
119                                           __u16 orig_phys_addr,
120                                           __u16 new_phys_addr)
121 {
122         msg->len = 6;
123         msg->msg[0] |= 0xf; /* broadcast */
124         msg->msg[1] = CEC_MSG_ROUTING_CHANGE;
125         msg->msg[2] = orig_phys_addr >> 8;
126         msg->msg[3] = orig_phys_addr & 0xff;
127         msg->msg[4] = new_phys_addr >> 8;
128         msg->msg[5] = new_phys_addr & 0xff;
129         msg->reply = reply ? CEC_MSG_ROUTING_INFORMATION : 0;
130 }
131
132 static inline void cec_ops_routing_change(const struct cec_msg *msg,
133                                           __u16 *orig_phys_addr,
134                                           __u16 *new_phys_addr)
135 {
136         *orig_phys_addr = (msg->msg[2] << 8) | msg->msg[3];
137         *new_phys_addr = (msg->msg[4] << 8) | msg->msg[5];
138 }
139
140 static inline void cec_msg_set_stream_path(struct cec_msg *msg, __u16 phys_addr)
141 {
142         msg->len = 4;
143         msg->msg[0] |= 0xf; /* broadcast */
144         msg->msg[1] = CEC_MSG_SET_STREAM_PATH;
145         msg->msg[2] = phys_addr >> 8;
146         msg->msg[3] = phys_addr & 0xff;
147 }
148
149 static inline void cec_ops_set_stream_path(const struct cec_msg *msg,
150                                            __u16 *phys_addr)
151 {
152         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
153 }
154
155
156 /* Standby Feature */
157 static inline void cec_msg_standby(struct cec_msg *msg)
158 {
159         msg->len = 2;
160         msg->msg[1] = CEC_MSG_STANDBY;
161 }
162
163
164 /* One Touch Record Feature */
165 static inline void cec_msg_record_off(struct cec_msg *msg, bool reply)
166 {
167         msg->len = 2;
168         msg->msg[1] = CEC_MSG_RECORD_OFF;
169         msg->reply = reply ? CEC_MSG_RECORD_STATUS : 0;
170 }
171
172 struct cec_op_arib_data {
173         __u16 transport_id;
174         __u16 service_id;
175         __u16 orig_network_id;
176 };
177
178 struct cec_op_atsc_data {
179         __u16 transport_id;
180         __u16 program_number;
181 };
182
183 struct cec_op_dvb_data {
184         __u16 transport_id;
185         __u16 service_id;
186         __u16 orig_network_id;
187 };
188
189 struct cec_op_channel_data {
190         __u8 channel_number_fmt;
191         __u16 major;
192         __u16 minor;
193 };
194
195 struct cec_op_digital_service_id {
196         __u8 service_id_method;
197         __u8 dig_bcast_system;
198         union {
199                 struct cec_op_arib_data arib;
200                 struct cec_op_atsc_data atsc;
201                 struct cec_op_dvb_data dvb;
202                 struct cec_op_channel_data channel;
203         };
204 };
205
206 struct cec_op_record_src {
207         __u8 type;
208         union {
209                 struct cec_op_digital_service_id digital;
210                 struct {
211                         __u8 ana_bcast_type;
212                         __u16 ana_freq;
213                         __u8 bcast_system;
214                 } analog;
215                 struct {
216                         __u8 plug;
217                 } ext_plug;
218                 struct {
219                         __u16 phys_addr;
220                 } ext_phys_addr;
221         };
222 };
223
224 static inline void cec_set_digital_service_id(__u8 *msg,
225               const struct cec_op_digital_service_id *digital)
226 {
227         *msg++ = (digital->service_id_method << 7) | digital->dig_bcast_system;
228         if (digital->service_id_method == CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL) {
229                 *msg++ = (digital->channel.channel_number_fmt << 2) |
230                          (digital->channel.major >> 8);
231                 *msg++ = digital->channel.major & 0xff;
232                 *msg++ = digital->channel.minor >> 8;
233                 *msg++ = digital->channel.minor & 0xff;
234                 *msg++ = 0;
235                 *msg++ = 0;
236                 return;
237         }
238         switch (digital->dig_bcast_system) {
239         case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_GEN:
240         case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_CABLE:
241         case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_SAT:
242         case CEC_OP_DIG_SERVICE_BCAST_SYSTEM_ATSC_T:
243                 *msg++ = digital->atsc.transport_id >> 8;
244                 *msg++ = digital->atsc.transport_id & 0xff;
245                 *msg++ = digital->atsc.program_number >> 8;
246                 *msg++ = digital->atsc.program_number & 0xff;
247                 *msg++ = 0;
248                 *msg++ = 0;
249                 break;
250         default:
251                 *msg++ = digital->dvb.transport_id >> 8;
252                 *msg++ = digital->dvb.transport_id & 0xff;
253                 *msg++ = digital->dvb.service_id >> 8;
254                 *msg++ = digital->dvb.service_id & 0xff;
255                 *msg++ = digital->dvb.orig_network_id >> 8;
256                 *msg++ = digital->dvb.orig_network_id & 0xff;
257                 break;
258         }
259 }
260
261 static inline void cec_get_digital_service_id(const __u8 *msg,
262               struct cec_op_digital_service_id *digital)
263 {
264         digital->service_id_method = msg[0] >> 7;
265         digital->dig_bcast_system = msg[0] & 0x7f;
266         if (digital->service_id_method == CEC_OP_SERVICE_ID_METHOD_BY_CHANNEL) {
267                 digital->channel.channel_number_fmt = msg[1] >> 2;
268                 digital->channel.major = ((msg[1] & 3) << 6) | msg[2];
269                 digital->channel.minor = (msg[3] << 8) | msg[4];
270                 return;
271         }
272         digital->dvb.transport_id = (msg[1] << 8) | msg[2];
273         digital->dvb.service_id = (msg[3] << 8) | msg[4];
274         digital->dvb.orig_network_id = (msg[5] << 8) | msg[6];
275 }
276
277 static inline void cec_msg_record_on_own(struct cec_msg *msg)
278 {
279         msg->len = 3;
280         msg->msg[1] = CEC_MSG_RECORD_ON;
281         msg->msg[2] = CEC_OP_RECORD_SRC_OWN;
282 }
283
284 static inline void cec_msg_record_on_digital(struct cec_msg *msg,
285                              const struct cec_op_digital_service_id *digital)
286 {
287         msg->len = 10;
288         msg->msg[1] = CEC_MSG_RECORD_ON;
289         msg->msg[2] = CEC_OP_RECORD_SRC_DIGITAL;
290         cec_set_digital_service_id(msg->msg + 3, digital);
291 }
292
293 static inline void cec_msg_record_on_analog(struct cec_msg *msg,
294                                             __u8 ana_bcast_type,
295                                             __u16 ana_freq,
296                                             __u8 bcast_system)
297 {
298         msg->len = 7;
299         msg->msg[1] = CEC_MSG_RECORD_ON;
300         msg->msg[2] = CEC_OP_RECORD_SRC_ANALOG;
301         msg->msg[3] = ana_bcast_type;
302         msg->msg[4] = ana_freq >> 8;
303         msg->msg[5] = ana_freq & 0xff;
304         msg->msg[6] = bcast_system;
305 }
306
307 static inline void cec_msg_record_on_plug(struct cec_msg *msg,
308                                           __u8 plug)
309 {
310         msg->len = 4;
311         msg->msg[1] = CEC_MSG_RECORD_ON;
312         msg->msg[2] = CEC_OP_RECORD_SRC_EXT_PLUG;
313         msg->msg[3] = plug;
314 }
315
316 static inline void cec_msg_record_on_phys_addr(struct cec_msg *msg,
317                                                __u16 phys_addr)
318 {
319         msg->len = 5;
320         msg->msg[1] = CEC_MSG_RECORD_ON;
321         msg->msg[2] = CEC_OP_RECORD_SRC_EXT_PHYS_ADDR;
322         msg->msg[3] = phys_addr >> 8;
323         msg->msg[4] = phys_addr & 0xff;
324 }
325
326 static inline void cec_msg_record_on(struct cec_msg *msg,
327                                      bool reply,
328                                      const struct cec_op_record_src *rec_src)
329 {
330         switch (rec_src->type) {
331         case CEC_OP_RECORD_SRC_OWN:
332                 cec_msg_record_on_own(msg);
333                 break;
334         case CEC_OP_RECORD_SRC_DIGITAL:
335                 cec_msg_record_on_digital(msg, &rec_src->digital);
336                 break;
337         case CEC_OP_RECORD_SRC_ANALOG:
338                 cec_msg_record_on_analog(msg,
339                                          rec_src->analog.ana_bcast_type,
340                                          rec_src->analog.ana_freq,
341                                          rec_src->analog.bcast_system);
342                 break;
343         case CEC_OP_RECORD_SRC_EXT_PLUG:
344                 cec_msg_record_on_plug(msg, rec_src->ext_plug.plug);
345                 break;
346         case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
347                 cec_msg_record_on_phys_addr(msg,
348                                             rec_src->ext_phys_addr.phys_addr);
349                 break;
350         }
351         msg->reply = reply ? CEC_MSG_RECORD_STATUS : 0;
352 }
353
354 static inline void cec_ops_record_on(const struct cec_msg *msg,
355                                      struct cec_op_record_src *rec_src)
356 {
357         rec_src->type = msg->msg[2];
358         switch (rec_src->type) {
359         case CEC_OP_RECORD_SRC_OWN:
360                 break;
361         case CEC_OP_RECORD_SRC_DIGITAL:
362                 cec_get_digital_service_id(msg->msg + 3, &rec_src->digital);
363                 break;
364         case CEC_OP_RECORD_SRC_ANALOG:
365                 rec_src->analog.ana_bcast_type = msg->msg[3];
366                 rec_src->analog.ana_freq =
367                         (msg->msg[4] << 8) | msg->msg[5];
368                 rec_src->analog.bcast_system = msg->msg[6];
369                 break;
370         case CEC_OP_RECORD_SRC_EXT_PLUG:
371                 rec_src->ext_plug.plug = msg->msg[3];
372                 break;
373         case CEC_OP_RECORD_SRC_EXT_PHYS_ADDR:
374                 rec_src->ext_phys_addr.phys_addr =
375                         (msg->msg[3] << 8) | msg->msg[4];
376                 break;
377         }
378 }
379
380 static inline void cec_msg_record_status(struct cec_msg *msg, __u8 rec_status)
381 {
382         msg->len = 3;
383         msg->msg[1] = CEC_MSG_RECORD_STATUS;
384         msg->msg[2] = rec_status;
385 }
386
387 static inline void cec_ops_record_status(const struct cec_msg *msg,
388                                          __u8 *rec_status)
389 {
390         *rec_status = msg->msg[2];
391 }
392
393 static inline void cec_msg_record_tv_screen(struct cec_msg *msg,
394                                             bool reply)
395 {
396         msg->len = 2;
397         msg->msg[1] = CEC_MSG_RECORD_TV_SCREEN;
398         msg->reply = reply ? CEC_MSG_RECORD_ON : 0;
399 }
400
401
402 /* Timer Programming Feature */
403 static inline void cec_msg_timer_status(struct cec_msg *msg,
404                                         __u8 timer_overlap_warning,
405                                         __u8 media_info,
406                                         __u8 prog_info,
407                                         __u8 prog_error,
408                                         __u8 duration_hr,
409                                         __u8 duration_min)
410 {
411         msg->len = 3;
412         msg->msg[1] = CEC_MSG_TIMER_STATUS;
413         msg->msg[2] = (timer_overlap_warning << 7) |
414                 (media_info << 5) |
415                 (prog_info ? 0x10 : 0) |
416                 (prog_info ? prog_info : prog_error);
417         if (prog_info == CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE ||
418             prog_info == CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE ||
419             prog_error == CEC_OP_PROG_ERROR_DUPLICATE) {
420                 msg->len += 2;
421                 msg->msg[3] = ((duration_hr / 10) << 4) | (duration_hr % 10);
422                 msg->msg[4] = ((duration_min / 10) << 4) | (duration_min % 10);
423         }
424 }
425
426 static inline void cec_ops_timer_status(const struct cec_msg *msg,
427                                         __u8 *timer_overlap_warning,
428                                         __u8 *media_info,
429                                         __u8 *prog_info,
430                                         __u8 *prog_error,
431                                         __u8 *duration_hr,
432                                         __u8 *duration_min)
433 {
434         *timer_overlap_warning = msg->msg[2] >> 7;
435         *media_info = (msg->msg[2] >> 5) & 3;
436         if (msg->msg[2] & 0x10) {
437                 *prog_info = msg->msg[2] & 0xf;
438                 *prog_error = 0;
439         } else {
440                 *prog_info = 0;
441                 *prog_error = msg->msg[2] & 0xf;
442         }
443         if (*prog_info == CEC_OP_PROG_INFO_NOT_ENOUGH_SPACE ||
444             *prog_info == CEC_OP_PROG_INFO_MIGHT_NOT_BE_ENOUGH_SPACE ||
445             *prog_error == CEC_OP_PROG_ERROR_DUPLICATE) {
446                 *duration_hr = (msg->msg[3] >> 4) * 10 + (msg->msg[3] & 0xf);
447                 *duration_min = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
448         } else {
449                 *duration_hr = *duration_min = 0;
450         }
451 }
452
453 static inline void cec_msg_timer_cleared_status(struct cec_msg *msg,
454                                                 __u8 timer_cleared_status)
455 {
456         msg->len = 3;
457         msg->msg[1] = CEC_MSG_TIMER_CLEARED_STATUS;
458         msg->msg[2] = timer_cleared_status;
459 }
460
461 static inline void cec_ops_timer_cleared_status(const struct cec_msg *msg,
462                                                 __u8 *timer_cleared_status)
463 {
464         *timer_cleared_status = msg->msg[2];
465 }
466
467 static inline void cec_msg_clear_analogue_timer(struct cec_msg *msg,
468                                                 bool reply,
469                                                 __u8 day,
470                                                 __u8 month,
471                                                 __u8 start_hr,
472                                                 __u8 start_min,
473                                                 __u8 duration_hr,
474                                                 __u8 duration_min,
475                                                 __u8 recording_seq,
476                                                 __u8 ana_bcast_type,
477                                                 __u16 ana_freq,
478                                                 __u8 bcast_system)
479 {
480         msg->len = 13;
481         msg->msg[1] = CEC_MSG_CLEAR_ANALOGUE_TIMER;
482         msg->msg[2] = day;
483         msg->msg[3] = month;
484         /* Hours and minutes are in BCD format */
485         msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
486         msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
487         msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
488         msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
489         msg->msg[8] = recording_seq;
490         msg->msg[9] = ana_bcast_type;
491         msg->msg[10] = ana_freq >> 8;
492         msg->msg[11] = ana_freq & 0xff;
493         msg->msg[12] = bcast_system;
494         msg->reply = reply ? CEC_MSG_TIMER_CLEARED_STATUS : 0;
495 }
496
497 static inline void cec_ops_clear_analogue_timer(const struct cec_msg *msg,
498                                                 __u8 *day,
499                                                 __u8 *month,
500                                                 __u8 *start_hr,
501                                                 __u8 *start_min,
502                                                 __u8 *duration_hr,
503                                                 __u8 *duration_min,
504                                                 __u8 *recording_seq,
505                                                 __u8 *ana_bcast_type,
506                                                 __u16 *ana_freq,
507                                                 __u8 *bcast_system)
508 {
509         *day = msg->msg[2];
510         *month = msg->msg[3];
511         /* Hours and minutes are in BCD format */
512         *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
513         *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
514         *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
515         *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
516         *recording_seq = msg->msg[8];
517         *ana_bcast_type = msg->msg[9];
518         *ana_freq = (msg->msg[10] << 8) | msg->msg[11];
519         *bcast_system = msg->msg[12];
520 }
521
522 static inline void cec_msg_clear_digital_timer(struct cec_msg *msg,
523                                 bool reply,
524                                 __u8 day,
525                                 __u8 month,
526                                 __u8 start_hr,
527                                 __u8 start_min,
528                                 __u8 duration_hr,
529                                 __u8 duration_min,
530                                 __u8 recording_seq,
531                                 const struct cec_op_digital_service_id *digital)
532 {
533         msg->len = 16;
534         msg->reply = reply ? CEC_MSG_TIMER_CLEARED_STATUS : 0;
535         msg->msg[1] = CEC_MSG_CLEAR_DIGITAL_TIMER;
536         msg->msg[2] = day;
537         msg->msg[3] = month;
538         /* Hours and minutes are in BCD format */
539         msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
540         msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
541         msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
542         msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
543         msg->msg[8] = recording_seq;
544         cec_set_digital_service_id(msg->msg + 9, digital);
545 }
546
547 static inline void cec_ops_clear_digital_timer(const struct cec_msg *msg,
548                                 __u8 *day,
549                                 __u8 *month,
550                                 __u8 *start_hr,
551                                 __u8 *start_min,
552                                 __u8 *duration_hr,
553                                 __u8 *duration_min,
554                                 __u8 *recording_seq,
555                                 struct cec_op_digital_service_id *digital)
556 {
557         *day = msg->msg[2];
558         *month = msg->msg[3];
559         /* Hours and minutes are in BCD format */
560         *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
561         *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
562         *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
563         *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
564         *recording_seq = msg->msg[8];
565         cec_get_digital_service_id(msg->msg + 9, digital);
566 }
567
568 static inline void cec_msg_clear_ext_timer(struct cec_msg *msg,
569                                            bool reply,
570                                            __u8 day,
571                                            __u8 month,
572                                            __u8 start_hr,
573                                            __u8 start_min,
574                                            __u8 duration_hr,
575                                            __u8 duration_min,
576                                            __u8 recording_seq,
577                                            __u8 ext_src_spec,
578                                            __u8 plug,
579                                            __u16 phys_addr)
580 {
581         msg->len = 13;
582         msg->msg[1] = CEC_MSG_CLEAR_EXT_TIMER;
583         msg->msg[2] = day;
584         msg->msg[3] = month;
585         /* Hours and minutes are in BCD format */
586         msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
587         msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
588         msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
589         msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
590         msg->msg[8] = recording_seq;
591         msg->msg[9] = ext_src_spec;
592         msg->msg[10] = plug;
593         msg->msg[11] = phys_addr >> 8;
594         msg->msg[12] = phys_addr & 0xff;
595         msg->reply = reply ? CEC_MSG_TIMER_CLEARED_STATUS : 0;
596 }
597
598 static inline void cec_ops_clear_ext_timer(const struct cec_msg *msg,
599                                            __u8 *day,
600                                            __u8 *month,
601                                            __u8 *start_hr,
602                                            __u8 *start_min,
603                                            __u8 *duration_hr,
604                                            __u8 *duration_min,
605                                            __u8 *recording_seq,
606                                            __u8 *ext_src_spec,
607                                            __u8 *plug,
608                                            __u16 *phys_addr)
609 {
610         *day = msg->msg[2];
611         *month = msg->msg[3];
612         /* Hours and minutes are in BCD format */
613         *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
614         *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
615         *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
616         *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
617         *recording_seq = msg->msg[8];
618         *ext_src_spec = msg->msg[9];
619         *plug = msg->msg[10];
620         *phys_addr = (msg->msg[11] << 8) | msg->msg[12];
621 }
622
623 static inline void cec_msg_set_analogue_timer(struct cec_msg *msg,
624                                               bool reply,
625                                               __u8 day,
626                                               __u8 month,
627                                               __u8 start_hr,
628                                               __u8 start_min,
629                                               __u8 duration_hr,
630                                               __u8 duration_min,
631                                               __u8 recording_seq,
632                                               __u8 ana_bcast_type,
633                                               __u16 ana_freq,
634                                               __u8 bcast_system)
635 {
636         msg->len = 13;
637         msg->msg[1] = CEC_MSG_SET_ANALOGUE_TIMER;
638         msg->msg[2] = day;
639         msg->msg[3] = month;
640         /* Hours and minutes are in BCD format */
641         msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
642         msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
643         msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
644         msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
645         msg->msg[8] = recording_seq;
646         msg->msg[9] = ana_bcast_type;
647         msg->msg[10] = ana_freq >> 8;
648         msg->msg[11] = ana_freq & 0xff;
649         msg->msg[12] = bcast_system;
650         msg->reply = reply ? CEC_MSG_TIMER_STATUS : 0;
651 }
652
653 static inline void cec_ops_set_analogue_timer(const struct cec_msg *msg,
654                                               __u8 *day,
655                                               __u8 *month,
656                                               __u8 *start_hr,
657                                               __u8 *start_min,
658                                               __u8 *duration_hr,
659                                               __u8 *duration_min,
660                                               __u8 *recording_seq,
661                                               __u8 *ana_bcast_type,
662                                               __u16 *ana_freq,
663                                               __u8 *bcast_system)
664 {
665         *day = msg->msg[2];
666         *month = msg->msg[3];
667         /* Hours and minutes are in BCD format */
668         *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
669         *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
670         *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
671         *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
672         *recording_seq = msg->msg[8];
673         *ana_bcast_type = msg->msg[9];
674         *ana_freq = (msg->msg[10] << 8) | msg->msg[11];
675         *bcast_system = msg->msg[12];
676 }
677
678 static inline void cec_msg_set_digital_timer(struct cec_msg *msg,
679                         bool reply,
680                         __u8 day,
681                         __u8 month,
682                         __u8 start_hr,
683                         __u8 start_min,
684                         __u8 duration_hr,
685                         __u8 duration_min,
686                         __u8 recording_seq,
687                         const struct cec_op_digital_service_id *digital)
688 {
689         msg->len = 16;
690         msg->reply = reply ? CEC_MSG_TIMER_STATUS : 0;
691         msg->msg[1] = CEC_MSG_SET_DIGITAL_TIMER;
692         msg->msg[2] = day;
693         msg->msg[3] = month;
694         /* Hours and minutes are in BCD format */
695         msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
696         msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
697         msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
698         msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
699         msg->msg[8] = recording_seq;
700         cec_set_digital_service_id(msg->msg + 9, digital);
701 }
702
703 static inline void cec_ops_set_digital_timer(const struct cec_msg *msg,
704                         __u8 *day,
705                         __u8 *month,
706                         __u8 *start_hr,
707                         __u8 *start_min,
708                         __u8 *duration_hr,
709                         __u8 *duration_min,
710                         __u8 *recording_seq,
711                         struct cec_op_digital_service_id *digital)
712 {
713         *day = msg->msg[2];
714         *month = msg->msg[3];
715         /* Hours and minutes are in BCD format */
716         *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
717         *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
718         *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
719         *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
720         *recording_seq = msg->msg[8];
721         cec_get_digital_service_id(msg->msg + 9, digital);
722 }
723
724 static inline void cec_msg_set_ext_timer(struct cec_msg *msg,
725                                          bool reply,
726                                          __u8 day,
727                                          __u8 month,
728                                          __u8 start_hr,
729                                          __u8 start_min,
730                                          __u8 duration_hr,
731                                          __u8 duration_min,
732                                          __u8 recording_seq,
733                                          __u8 ext_src_spec,
734                                          __u8 plug,
735                                          __u16 phys_addr)
736 {
737         msg->len = 13;
738         msg->msg[1] = CEC_MSG_SET_EXT_TIMER;
739         msg->msg[2] = day;
740         msg->msg[3] = month;
741         /* Hours and minutes are in BCD format */
742         msg->msg[4] = ((start_hr / 10) << 4) | (start_hr % 10);
743         msg->msg[5] = ((start_min / 10) << 4) | (start_min % 10);
744         msg->msg[6] = ((duration_hr / 10) << 4) | (duration_hr % 10);
745         msg->msg[7] = ((duration_min / 10) << 4) | (duration_min % 10);
746         msg->msg[8] = recording_seq;
747         msg->msg[9] = ext_src_spec;
748         msg->msg[10] = plug;
749         msg->msg[11] = phys_addr >> 8;
750         msg->msg[12] = phys_addr & 0xff;
751         msg->reply = reply ? CEC_MSG_TIMER_STATUS : 0;
752 }
753
754 static inline void cec_ops_set_ext_timer(const struct cec_msg *msg,
755                                          __u8 *day,
756                                          __u8 *month,
757                                          __u8 *start_hr,
758                                          __u8 *start_min,
759                                          __u8 *duration_hr,
760                                          __u8 *duration_min,
761                                          __u8 *recording_seq,
762                                          __u8 *ext_src_spec,
763                                          __u8 *plug,
764                                          __u16 *phys_addr)
765 {
766         *day = msg->msg[2];
767         *month = msg->msg[3];
768         /* Hours and minutes are in BCD format */
769         *start_hr = (msg->msg[4] >> 4) * 10 + (msg->msg[4] & 0xf);
770         *start_min = (msg->msg[5] >> 4) * 10 + (msg->msg[5] & 0xf);
771         *duration_hr = (msg->msg[6] >> 4) * 10 + (msg->msg[6] & 0xf);
772         *duration_min = (msg->msg[7] >> 4) * 10 + (msg->msg[7] & 0xf);
773         *recording_seq = msg->msg[8];
774         *ext_src_spec = msg->msg[9];
775         *plug = msg->msg[10];
776         *phys_addr = (msg->msg[11] << 8) | msg->msg[12];
777 }
778
779 static inline void cec_msg_set_timer_program_title(struct cec_msg *msg,
780                                                    const char *prog_title)
781 {
782         unsigned int len = strlen(prog_title);
783
784         if (len > 14)
785                 len = 14;
786         msg->len = 2 + len;
787         msg->msg[1] = CEC_MSG_SET_TIMER_PROGRAM_TITLE;
788         memcpy(msg->msg + 2, prog_title, len);
789 }
790
791 static inline void cec_ops_set_timer_program_title(const struct cec_msg *msg,
792                                                    char *prog_title)
793 {
794         unsigned int len = msg->len > 2 ? msg->len - 2 : 0;
795
796         if (len > 14)
797                 len = 14;
798         memcpy(prog_title, msg->msg + 2, len);
799         prog_title[len] = '\0';
800 }
801
802 /* System Information Feature */
803 static inline void cec_msg_cec_version(struct cec_msg *msg, __u8 cec_version)
804 {
805         msg->len = 3;
806         msg->msg[1] = CEC_MSG_CEC_VERSION;
807         msg->msg[2] = cec_version;
808 }
809
810 static inline void cec_ops_cec_version(const struct cec_msg *msg,
811                                        __u8 *cec_version)
812 {
813         *cec_version = msg->msg[2];
814 }
815
816 static inline void cec_msg_get_cec_version(struct cec_msg *msg,
817                                            bool reply)
818 {
819         msg->len = 2;
820         msg->msg[1] = CEC_MSG_GET_CEC_VERSION;
821         msg->reply = reply ? CEC_MSG_CEC_VERSION : 0;
822 }
823
824 static inline void cec_msg_report_physical_addr(struct cec_msg *msg,
825                                         __u16 phys_addr, __u8 prim_devtype)
826 {
827         msg->len = 5;
828         msg->msg[0] |= 0xf; /* broadcast */
829         msg->msg[1] = CEC_MSG_REPORT_PHYSICAL_ADDR;
830         msg->msg[2] = phys_addr >> 8;
831         msg->msg[3] = phys_addr & 0xff;
832         msg->msg[4] = prim_devtype;
833 }
834
835 static inline void cec_ops_report_physical_addr(const struct cec_msg *msg,
836                                         __u16 *phys_addr, __u8 *prim_devtype)
837 {
838         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
839         *prim_devtype = msg->msg[4];
840 }
841
842 static inline void cec_msg_give_physical_addr(struct cec_msg *msg,
843                                               bool reply)
844 {
845         msg->len = 2;
846         msg->msg[1] = CEC_MSG_GIVE_PHYSICAL_ADDR;
847         msg->reply = reply ? CEC_MSG_REPORT_PHYSICAL_ADDR : 0;
848 }
849
850 static inline void cec_msg_set_menu_language(struct cec_msg *msg,
851                                              const char *language)
852 {
853         msg->len = 5;
854         msg->msg[0] |= 0xf; /* broadcast */
855         msg->msg[1] = CEC_MSG_SET_MENU_LANGUAGE;
856         memcpy(msg->msg + 2, language, 3);
857 }
858
859 static inline void cec_ops_set_menu_language(const struct cec_msg *msg,
860                                              char *language)
861 {
862         memcpy(language, msg->msg + 2, 3);
863         language[3] = '\0';
864 }
865
866 static inline void cec_msg_get_menu_language(struct cec_msg *msg,
867                                              bool reply)
868 {
869         msg->len = 2;
870         msg->msg[1] = CEC_MSG_GET_MENU_LANGUAGE;
871         msg->reply = reply ? CEC_MSG_SET_MENU_LANGUAGE : 0;
872 }
873
874 /*
875  * Assumes a single RC Profile byte and a single Device Features byte,
876  * i.e. no extended features are supported by this helper function.
877  *
878  * As of CEC 2.0 no extended features are defined, should those be added
879  * in the future, then this function needs to be adapted or a new function
880  * should be added.
881  */
882 static inline void cec_msg_report_features(struct cec_msg *msg,
883                                 __u8 cec_version, __u8 all_device_types,
884                                 __u8 rc_profile, __u8 dev_features)
885 {
886         msg->len = 6;
887         msg->msg[0] |= 0xf; /* broadcast */
888         msg->msg[1] = CEC_MSG_REPORT_FEATURES;
889         msg->msg[2] = cec_version;
890         msg->msg[3] = all_device_types;
891         msg->msg[4] = rc_profile;
892         msg->msg[5] = dev_features;
893 }
894
895 static inline void cec_ops_report_features(const struct cec_msg *msg,
896                         __u8 *cec_version, __u8 *all_device_types,
897                         const __u8 **rc_profile, const __u8 **dev_features)
898 {
899         const __u8 *p = &msg->msg[4];
900
901         *cec_version = msg->msg[2];
902         *all_device_types = msg->msg[3];
903         *rc_profile = p;
904         while (p < &msg->msg[14] && (*p & CEC_OP_FEAT_EXT))
905                 p++;
906         if (!(*p & CEC_OP_FEAT_EXT)) {
907                 *dev_features = p + 1;
908                 while (p < &msg->msg[15] && (*p & CEC_OP_FEAT_EXT))
909                         p++;
910         }
911         if (*p & CEC_OP_FEAT_EXT)
912                 *rc_profile = *dev_features = NULL;
913 }
914
915 static inline void cec_msg_give_features(struct cec_msg *msg,
916                                          bool reply)
917 {
918         msg->len = 2;
919         msg->msg[1] = CEC_MSG_GIVE_FEATURES;
920         msg->reply = reply ? CEC_MSG_REPORT_FEATURES : 0;
921 }
922
923 /* Deck Control Feature */
924 static inline void cec_msg_deck_control(struct cec_msg *msg,
925                                         __u8 deck_control_mode)
926 {
927         msg->len = 3;
928         msg->msg[1] = CEC_MSG_DECK_CONTROL;
929         msg->msg[2] = deck_control_mode;
930 }
931
932 static inline void cec_ops_deck_control(const struct cec_msg *msg,
933                                         __u8 *deck_control_mode)
934 {
935         *deck_control_mode = msg->msg[2];
936 }
937
938 static inline void cec_msg_deck_status(struct cec_msg *msg,
939                                        __u8 deck_info)
940 {
941         msg->len = 3;
942         msg->msg[1] = CEC_MSG_DECK_STATUS;
943         msg->msg[2] = deck_info;
944 }
945
946 static inline void cec_ops_deck_status(const struct cec_msg *msg,
947                                        __u8 *deck_info)
948 {
949         *deck_info = msg->msg[2];
950 }
951
952 static inline void cec_msg_give_deck_status(struct cec_msg *msg,
953                                             bool reply,
954                                             __u8 status_req)
955 {
956         msg->len = 3;
957         msg->msg[1] = CEC_MSG_GIVE_DECK_STATUS;
958         msg->msg[2] = status_req;
959         msg->reply = (reply && status_req != CEC_OP_STATUS_REQ_OFF) ?
960                                 CEC_MSG_DECK_STATUS : 0;
961 }
962
963 static inline void cec_ops_give_deck_status(const struct cec_msg *msg,
964                                             __u8 *status_req)
965 {
966         *status_req = msg->msg[2];
967 }
968
969 static inline void cec_msg_play(struct cec_msg *msg,
970                                 __u8 play_mode)
971 {
972         msg->len = 3;
973         msg->msg[1] = CEC_MSG_PLAY;
974         msg->msg[2] = play_mode;
975 }
976
977 static inline void cec_ops_play(const struct cec_msg *msg,
978                                 __u8 *play_mode)
979 {
980         *play_mode = msg->msg[2];
981 }
982
983
984 /* Tuner Control Feature */
985 struct cec_op_tuner_device_info {
986         __u8 rec_flag;
987         __u8 tuner_display_info;
988         bool is_analog;
989         union {
990                 struct cec_op_digital_service_id digital;
991                 struct {
992                         __u8 ana_bcast_type;
993                         __u16 ana_freq;
994                         __u8 bcast_system;
995                 } analog;
996         };
997 };
998
999 static inline void cec_msg_tuner_device_status_analog(struct cec_msg *msg,
1000                                                       __u8 rec_flag,
1001                                                       __u8 tuner_display_info,
1002                                                       __u8 ana_bcast_type,
1003                                                       __u16 ana_freq,
1004                                                       __u8 bcast_system)
1005 {
1006         msg->len = 7;
1007         msg->msg[1] = CEC_MSG_TUNER_DEVICE_STATUS;
1008         msg->msg[2] = (rec_flag << 7) | tuner_display_info;
1009         msg->msg[3] = ana_bcast_type;
1010         msg->msg[4] = ana_freq >> 8;
1011         msg->msg[5] = ana_freq & 0xff;
1012         msg->msg[6] = bcast_system;
1013 }
1014
1015 static inline void cec_msg_tuner_device_status_digital(struct cec_msg *msg,
1016                    __u8 rec_flag, __u8 tuner_display_info,
1017                    const struct cec_op_digital_service_id *digital)
1018 {
1019         msg->len = 10;
1020         msg->msg[1] = CEC_MSG_TUNER_DEVICE_STATUS;
1021         msg->msg[2] = (rec_flag << 7) | tuner_display_info;
1022         cec_set_digital_service_id(msg->msg + 3, digital);
1023 }
1024
1025 static inline void cec_msg_tuner_device_status(struct cec_msg *msg,
1026                         const struct cec_op_tuner_device_info *tuner_dev_info)
1027 {
1028         if (tuner_dev_info->is_analog)
1029                 cec_msg_tuner_device_status_analog(msg,
1030                         tuner_dev_info->rec_flag,
1031                         tuner_dev_info->tuner_display_info,
1032                         tuner_dev_info->analog.ana_bcast_type,
1033                         tuner_dev_info->analog.ana_freq,
1034                         tuner_dev_info->analog.bcast_system);
1035         else
1036                 cec_msg_tuner_device_status_digital(msg,
1037                         tuner_dev_info->rec_flag,
1038                         tuner_dev_info->tuner_display_info,
1039                         &tuner_dev_info->digital);
1040 }
1041
1042 static inline void cec_ops_tuner_device_status(const struct cec_msg *msg,
1043                                 struct cec_op_tuner_device_info *tuner_dev_info)
1044 {
1045         tuner_dev_info->is_analog = msg->len < 10;
1046         tuner_dev_info->rec_flag = msg->msg[2] >> 7;
1047         tuner_dev_info->tuner_display_info = msg->msg[2] & 0x7f;
1048         if (tuner_dev_info->is_analog) {
1049                 tuner_dev_info->analog.ana_bcast_type = msg->msg[3];
1050                 tuner_dev_info->analog.ana_freq = (msg->msg[4] << 8) | msg->msg[5];
1051                 tuner_dev_info->analog.bcast_system = msg->msg[6];
1052                 return;
1053         }
1054         cec_get_digital_service_id(msg->msg + 3, &tuner_dev_info->digital);
1055 }
1056
1057 static inline void cec_msg_give_tuner_device_status(struct cec_msg *msg,
1058                                                     bool reply,
1059                                                     __u8 status_req)
1060 {
1061         msg->len = 3;
1062         msg->msg[1] = CEC_MSG_GIVE_TUNER_DEVICE_STATUS;
1063         msg->msg[2] = status_req;
1064         msg->reply = (reply && status_req != CEC_OP_STATUS_REQ_OFF) ?
1065                                 CEC_MSG_TUNER_DEVICE_STATUS : 0;
1066 }
1067
1068 static inline void cec_ops_give_tuner_device_status(const struct cec_msg *msg,
1069                                                     __u8 *status_req)
1070 {
1071         *status_req = msg->msg[2];
1072 }
1073
1074 static inline void cec_msg_select_analogue_service(struct cec_msg *msg,
1075                                                    __u8 ana_bcast_type,
1076                                                    __u16 ana_freq,
1077                                                    __u8 bcast_system)
1078 {
1079         msg->len = 6;
1080         msg->msg[1] = CEC_MSG_SELECT_ANALOGUE_SERVICE;
1081         msg->msg[2] = ana_bcast_type;
1082         msg->msg[3] = ana_freq >> 8;
1083         msg->msg[4] = ana_freq & 0xff;
1084         msg->msg[5] = bcast_system;
1085 }
1086
1087 static inline void cec_ops_select_analogue_service(const struct cec_msg *msg,
1088                                                    __u8 *ana_bcast_type,
1089                                                    __u16 *ana_freq,
1090                                                    __u8 *bcast_system)
1091 {
1092         *ana_bcast_type = msg->msg[2];
1093         *ana_freq = (msg->msg[3] << 8) | msg->msg[4];
1094         *bcast_system = msg->msg[5];
1095 }
1096
1097 static inline void cec_msg_select_digital_service(struct cec_msg *msg,
1098                                 const struct cec_op_digital_service_id *digital)
1099 {
1100         msg->len = 9;
1101         msg->msg[1] = CEC_MSG_SELECT_DIGITAL_SERVICE;
1102         cec_set_digital_service_id(msg->msg + 2, digital);
1103 }
1104
1105 static inline void cec_ops_select_digital_service(const struct cec_msg *msg,
1106                                 struct cec_op_digital_service_id *digital)
1107 {
1108         cec_get_digital_service_id(msg->msg + 2, digital);
1109 }
1110
1111 static inline void cec_msg_tuner_step_decrement(struct cec_msg *msg)
1112 {
1113         msg->len = 2;
1114         msg->msg[1] = CEC_MSG_TUNER_STEP_DECREMENT;
1115 }
1116
1117 static inline void cec_msg_tuner_step_increment(struct cec_msg *msg)
1118 {
1119         msg->len = 2;
1120         msg->msg[1] = CEC_MSG_TUNER_STEP_INCREMENT;
1121 }
1122
1123
1124 /* Vendor Specific Commands Feature */
1125 static inline void cec_msg_device_vendor_id(struct cec_msg *msg, __u32 vendor_id)
1126 {
1127         msg->len = 5;
1128         msg->msg[0] |= 0xf; /* broadcast */
1129         msg->msg[1] = CEC_MSG_DEVICE_VENDOR_ID;
1130         msg->msg[2] = vendor_id >> 16;
1131         msg->msg[3] = (vendor_id >> 8) & 0xff;
1132         msg->msg[4] = vendor_id & 0xff;
1133 }
1134
1135 static inline void cec_ops_device_vendor_id(const struct cec_msg *msg,
1136                                             __u32 *vendor_id)
1137 {
1138         *vendor_id = (msg->msg[2] << 16) | (msg->msg[3] << 8) | msg->msg[4];
1139 }
1140
1141 static inline void cec_msg_give_device_vendor_id(struct cec_msg *msg,
1142                                                  bool reply)
1143 {
1144         msg->len = 2;
1145         msg->msg[1] = CEC_MSG_GIVE_DEVICE_VENDOR_ID;
1146         msg->reply = reply ? CEC_MSG_DEVICE_VENDOR_ID : 0;
1147 }
1148
1149 static inline void cec_msg_vendor_command(struct cec_msg *msg,
1150                                           __u8 size, const __u8 *vendor_cmd)
1151 {
1152         if (size > 14)
1153                 size = 14;
1154         msg->len = 2 + size;
1155         msg->msg[1] = CEC_MSG_VENDOR_COMMAND;
1156         memcpy(msg->msg + 2, vendor_cmd, size);
1157 }
1158
1159 static inline void cec_ops_vendor_command(const struct cec_msg *msg,
1160                                           __u8 *size,
1161                                           const __u8 **vendor_cmd)
1162 {
1163         *size = msg->len - 2;
1164
1165         if (*size > 14)
1166                 *size = 14;
1167         *vendor_cmd = msg->msg + 2;
1168 }
1169
1170 static inline void cec_msg_vendor_command_with_id(struct cec_msg *msg,
1171                                                   __u32 vendor_id, __u8 size,
1172                                                   const __u8 *vendor_cmd)
1173 {
1174         if (size > 11)
1175                 size = 11;
1176         msg->len = 5 + size;
1177         msg->msg[1] = CEC_MSG_VENDOR_COMMAND_WITH_ID;
1178         msg->msg[2] = vendor_id >> 16;
1179         msg->msg[3] = (vendor_id >> 8) & 0xff;
1180         msg->msg[4] = vendor_id & 0xff;
1181         memcpy(msg->msg + 5, vendor_cmd, size);
1182 }
1183
1184 static inline void cec_ops_vendor_command_with_id(const struct cec_msg *msg,
1185                                                   __u32 *vendor_id,  __u8 *size,
1186                                                   const __u8 **vendor_cmd)
1187 {
1188         *size = msg->len - 5;
1189
1190         if (*size > 11)
1191                 *size = 11;
1192         *vendor_id = (msg->msg[2] << 16) | (msg->msg[3] << 8) | msg->msg[4];
1193         *vendor_cmd = msg->msg + 5;
1194 }
1195
1196 static inline void cec_msg_vendor_remote_button_down(struct cec_msg *msg,
1197                                                      __u8 size,
1198                                                      const __u8 *rc_code)
1199 {
1200         if (size > 14)
1201                 size = 14;
1202         msg->len = 2 + size;
1203         msg->msg[1] = CEC_MSG_VENDOR_REMOTE_BUTTON_DOWN;
1204         memcpy(msg->msg + 2, rc_code, size);
1205 }
1206
1207 static inline void cec_ops_vendor_remote_button_down(const struct cec_msg *msg,
1208                                                      __u8 *size,
1209                                                      const __u8 **rc_code)
1210 {
1211         *size = msg->len - 2;
1212
1213         if (*size > 14)
1214                 *size = 14;
1215         *rc_code = msg->msg + 2;
1216 }
1217
1218 static inline void cec_msg_vendor_remote_button_up(struct cec_msg *msg)
1219 {
1220         msg->len = 2;
1221         msg->msg[1] = CEC_MSG_VENDOR_REMOTE_BUTTON_UP;
1222 }
1223
1224
1225 /* OSD Display Feature */
1226 static inline void cec_msg_set_osd_string(struct cec_msg *msg,
1227                                           __u8 disp_ctl,
1228                                           const char *osd)
1229 {
1230         unsigned int len = strlen(osd);
1231
1232         if (len > 13)
1233                 len = 13;
1234         msg->len = 3 + len;
1235         msg->msg[1] = CEC_MSG_SET_OSD_STRING;
1236         msg->msg[2] = disp_ctl;
1237         memcpy(msg->msg + 3, osd, len);
1238 }
1239
1240 static inline void cec_ops_set_osd_string(const struct cec_msg *msg,
1241                                           __u8 *disp_ctl,
1242                                           char *osd)
1243 {
1244         unsigned int len = msg->len > 3 ? msg->len - 3 : 0;
1245
1246         *disp_ctl = msg->msg[2];
1247         if (len > 13)
1248                 len = 13;
1249         memcpy(osd, msg->msg + 3, len);
1250         osd[len] = '\0';
1251 }
1252
1253
1254 /* Device OSD Transfer Feature */
1255 static inline void cec_msg_set_osd_name(struct cec_msg *msg, const char *name)
1256 {
1257         unsigned int len = strlen(name);
1258
1259         if (len > 14)
1260                 len = 14;
1261         msg->len = 2 + len;
1262         msg->msg[1] = CEC_MSG_SET_OSD_NAME;
1263         memcpy(msg->msg + 2, name, len);
1264 }
1265
1266 static inline void cec_ops_set_osd_name(const struct cec_msg *msg,
1267                                         char *name)
1268 {
1269         unsigned int len = msg->len > 2 ? msg->len - 2 : 0;
1270
1271         if (len > 14)
1272                 len = 14;
1273         memcpy(name, msg->msg + 2, len);
1274         name[len] = '\0';
1275 }
1276
1277 static inline void cec_msg_give_osd_name(struct cec_msg *msg,
1278                                          bool reply)
1279 {
1280         msg->len = 2;
1281         msg->msg[1] = CEC_MSG_GIVE_OSD_NAME;
1282         msg->reply = reply ? CEC_MSG_SET_OSD_NAME : 0;
1283 }
1284
1285
1286 /* Device Menu Control Feature */
1287 static inline void cec_msg_menu_status(struct cec_msg *msg,
1288                                        __u8 menu_state)
1289 {
1290         msg->len = 3;
1291         msg->msg[1] = CEC_MSG_MENU_STATUS;
1292         msg->msg[2] = menu_state;
1293 }
1294
1295 static inline void cec_ops_menu_status(const struct cec_msg *msg,
1296                                        __u8 *menu_state)
1297 {
1298         *menu_state = msg->msg[2];
1299 }
1300
1301 static inline void cec_msg_menu_request(struct cec_msg *msg,
1302                                         bool reply,
1303                                         __u8 menu_req)
1304 {
1305         msg->len = 3;
1306         msg->msg[1] = CEC_MSG_MENU_REQUEST;
1307         msg->msg[2] = menu_req;
1308         msg->reply = reply ? CEC_MSG_MENU_STATUS : 0;
1309 }
1310
1311 static inline void cec_ops_menu_request(const struct cec_msg *msg,
1312                                         __u8 *menu_req)
1313 {
1314         *menu_req = msg->msg[2];
1315 }
1316
1317 struct cec_op_ui_command {
1318         __u8 ui_cmd;
1319         bool has_opt_arg;
1320         union {
1321                 struct cec_op_channel_data channel_identifier;
1322                 __u8 ui_broadcast_type;
1323                 __u8 ui_sound_presentation_control;
1324                 __u8 play_mode;
1325                 __u8 ui_function_media;
1326                 __u8 ui_function_select_av_input;
1327                 __u8 ui_function_select_audio_input;
1328         };
1329 };
1330
1331 static inline void cec_msg_user_control_pressed(struct cec_msg *msg,
1332                                         const struct cec_op_ui_command *ui_cmd)
1333 {
1334         msg->len = 3;
1335         msg->msg[1] = CEC_MSG_USER_CONTROL_PRESSED;
1336         msg->msg[2] = ui_cmd->ui_cmd;
1337         if (!ui_cmd->has_opt_arg)
1338                 return;
1339         switch (ui_cmd->ui_cmd) {
1340         case 0x56:
1341         case 0x57:
1342         case 0x60:
1343         case 0x68:
1344         case 0x69:
1345         case 0x6a:
1346                 /* The optional operand is one byte for all these ui commands */
1347                 msg->len++;
1348                 msg->msg[3] = ui_cmd->play_mode;
1349                 break;
1350         case 0x67:
1351                 msg->len += 4;
1352                 msg->msg[3] = (ui_cmd->channel_identifier.channel_number_fmt << 2) |
1353                               (ui_cmd->channel_identifier.major >> 8);
1354                 msg->msg[4] = ui_cmd->channel_identifier.major & 0xff;
1355                 msg->msg[5] = ui_cmd->channel_identifier.minor >> 8;
1356                 msg->msg[6] = ui_cmd->channel_identifier.minor & 0xff;
1357                 break;
1358         }
1359 }
1360
1361 static inline void cec_ops_user_control_pressed(const struct cec_msg *msg,
1362                                                 struct cec_op_ui_command *ui_cmd)
1363 {
1364         ui_cmd->ui_cmd = msg->msg[2];
1365         ui_cmd->has_opt_arg = false;
1366         if (msg->len == 3)
1367                 return;
1368         switch (ui_cmd->ui_cmd) {
1369         case 0x56:
1370         case 0x57:
1371         case 0x60:
1372         case 0x68:
1373         case 0x69:
1374         case 0x6a:
1375                 /* The optional operand is one byte for all these ui commands */
1376                 ui_cmd->play_mode = msg->msg[3];
1377                 ui_cmd->has_opt_arg = true;
1378                 break;
1379         case 0x67:
1380                 if (msg->len < 7)
1381                         break;
1382                 ui_cmd->has_opt_arg = true;
1383                 ui_cmd->channel_identifier.channel_number_fmt = msg->msg[3] >> 2;
1384                 ui_cmd->channel_identifier.major = ((msg->msg[3] & 3) << 6) | msg->msg[4];
1385                 ui_cmd->channel_identifier.minor = (msg->msg[5] << 8) | msg->msg[6];
1386                 break;
1387         }
1388 }
1389
1390 static inline void cec_msg_user_control_released(struct cec_msg *msg)
1391 {
1392         msg->len = 2;
1393         msg->msg[1] = CEC_MSG_USER_CONTROL_RELEASED;
1394 }
1395
1396 /* Remote Control Passthrough Feature */
1397
1398 /* Power Status Feature */
1399 static inline void cec_msg_report_power_status(struct cec_msg *msg,
1400                                                __u8 pwr_state)
1401 {
1402         msg->len = 3;
1403         msg->msg[1] = CEC_MSG_REPORT_POWER_STATUS;
1404         msg->msg[2] = pwr_state;
1405 }
1406
1407 static inline void cec_ops_report_power_status(const struct cec_msg *msg,
1408                                                __u8 *pwr_state)
1409 {
1410         *pwr_state = msg->msg[2];
1411 }
1412
1413 static inline void cec_msg_give_device_power_status(struct cec_msg *msg,
1414                                                     bool reply)
1415 {
1416         msg->len = 2;
1417         msg->msg[1] = CEC_MSG_GIVE_DEVICE_POWER_STATUS;
1418         msg->reply = reply ? CEC_MSG_REPORT_POWER_STATUS : 0;
1419 }
1420
1421 /* General Protocol Messages */
1422 static inline void cec_msg_feature_abort(struct cec_msg *msg,
1423                                          __u8 abort_msg, __u8 reason)
1424 {
1425         msg->len = 4;
1426         msg->msg[1] = CEC_MSG_FEATURE_ABORT;
1427         msg->msg[2] = abort_msg;
1428         msg->msg[3] = reason;
1429 }
1430
1431 static inline void cec_ops_feature_abort(const struct cec_msg *msg,
1432                                          __u8 *abort_msg, __u8 *reason)
1433 {
1434         *abort_msg = msg->msg[2];
1435         *reason = msg->msg[3];
1436 }
1437
1438 /* This changes the current message into a feature abort message */
1439 static inline void cec_msg_reply_feature_abort(struct cec_msg *msg, __u8 reason)
1440 {
1441         cec_msg_set_reply_to(msg, msg);
1442         msg->len = 4;
1443         msg->msg[2] = msg->msg[1];
1444         msg->msg[3] = reason;
1445         msg->msg[1] = CEC_MSG_FEATURE_ABORT;
1446 }
1447
1448 static inline void cec_msg_abort(struct cec_msg *msg)
1449 {
1450         msg->len = 2;
1451         msg->msg[1] = CEC_MSG_ABORT;
1452 }
1453
1454
1455 /* System Audio Control Feature */
1456 static inline void cec_msg_report_audio_status(struct cec_msg *msg,
1457                                                __u8 aud_mute_status,
1458                                                __u8 aud_vol_status)
1459 {
1460         msg->len = 3;
1461         msg->msg[1] = CEC_MSG_REPORT_AUDIO_STATUS;
1462         msg->msg[2] = (aud_mute_status << 7) | (aud_vol_status & 0x7f);
1463 }
1464
1465 static inline void cec_ops_report_audio_status(const struct cec_msg *msg,
1466                                                __u8 *aud_mute_status,
1467                                                __u8 *aud_vol_status)
1468 {
1469         *aud_mute_status = msg->msg[2] >> 7;
1470         *aud_vol_status = msg->msg[2] & 0x7f;
1471 }
1472
1473 static inline void cec_msg_give_audio_status(struct cec_msg *msg,
1474                                              bool reply)
1475 {
1476         msg->len = 2;
1477         msg->msg[1] = CEC_MSG_GIVE_AUDIO_STATUS;
1478         msg->reply = reply ? CEC_MSG_REPORT_AUDIO_STATUS : 0;
1479 }
1480
1481 static inline void cec_msg_set_system_audio_mode(struct cec_msg *msg,
1482                                                  __u8 sys_aud_status)
1483 {
1484         msg->len = 3;
1485         msg->msg[1] = CEC_MSG_SET_SYSTEM_AUDIO_MODE;
1486         msg->msg[2] = sys_aud_status;
1487 }
1488
1489 static inline void cec_ops_set_system_audio_mode(const struct cec_msg *msg,
1490                                                  __u8 *sys_aud_status)
1491 {
1492         *sys_aud_status = msg->msg[2];
1493 }
1494
1495 static inline void cec_msg_system_audio_mode_request(struct cec_msg *msg,
1496                                                      bool reply,
1497                                                      __u16 phys_addr)
1498 {
1499         msg->len = phys_addr == 0xffff ? 2 : 4;
1500         msg->msg[1] = CEC_MSG_SYSTEM_AUDIO_MODE_REQUEST;
1501         msg->msg[2] = phys_addr >> 8;
1502         msg->msg[3] = phys_addr & 0xff;
1503         msg->reply = reply ? CEC_MSG_SET_SYSTEM_AUDIO_MODE : 0;
1504
1505 }
1506
1507 static inline void cec_ops_system_audio_mode_request(const struct cec_msg *msg,
1508                                                      __u16 *phys_addr)
1509 {
1510         if (msg->len < 4)
1511                 *phys_addr = 0xffff;
1512         else
1513                 *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1514 }
1515
1516 static inline void cec_msg_system_audio_mode_status(struct cec_msg *msg,
1517                                                     __u8 sys_aud_status)
1518 {
1519         msg->len = 3;
1520         msg->msg[1] = CEC_MSG_SYSTEM_AUDIO_MODE_STATUS;
1521         msg->msg[2] = sys_aud_status;
1522 }
1523
1524 static inline void cec_ops_system_audio_mode_status(const struct cec_msg *msg,
1525                                                     __u8 *sys_aud_status)
1526 {
1527         *sys_aud_status = msg->msg[2];
1528 }
1529
1530 static inline void cec_msg_give_system_audio_mode_status(struct cec_msg *msg,
1531                                                          bool reply)
1532 {
1533         msg->len = 2;
1534         msg->msg[1] = CEC_MSG_GIVE_SYSTEM_AUDIO_MODE_STATUS;
1535         msg->reply = reply ? CEC_MSG_SYSTEM_AUDIO_MODE_STATUS : 0;
1536 }
1537
1538 static inline void cec_msg_report_short_audio_descriptor(struct cec_msg *msg,
1539                                         __u8 num_descriptors,
1540                                         const __u32 *descriptors)
1541 {
1542         unsigned int i;
1543
1544         if (num_descriptors > 4)
1545                 num_descriptors = 4;
1546         msg->len = 2 + num_descriptors * 3;
1547         msg->msg[1] = CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR;
1548         for (i = 0; i < num_descriptors; i++) {
1549                 msg->msg[2 + i * 3] = (descriptors[i] >> 16) & 0xff;
1550                 msg->msg[3 + i * 3] = (descriptors[i] >> 8) & 0xff;
1551                 msg->msg[4 + i * 3] = descriptors[i] & 0xff;
1552         }
1553 }
1554
1555 static inline void cec_ops_report_short_audio_descriptor(const struct cec_msg *msg,
1556                                                          __u8 *num_descriptors,
1557                                                          __u32 *descriptors)
1558 {
1559         unsigned int i;
1560
1561         *num_descriptors = (msg->len - 2) / 3;
1562         if (*num_descriptors > 4)
1563                 *num_descriptors = 4;
1564         for (i = 0; i < *num_descriptors; i++)
1565                 descriptors[i] = (msg->msg[2 + i * 3] << 16) |
1566                         (msg->msg[3 + i * 3] << 8) |
1567                         msg->msg[4 + i * 3];
1568 }
1569
1570 static inline void cec_msg_request_short_audio_descriptor(struct cec_msg *msg,
1571                                         bool reply,
1572                                         __u8 num_descriptors,
1573                                         const __u8 *audio_format_id,
1574                                         const __u8 *audio_format_code)
1575 {
1576         unsigned int i;
1577
1578         if (num_descriptors > 4)
1579                 num_descriptors = 4;
1580         msg->len = 2 + num_descriptors;
1581         msg->msg[1] = CEC_MSG_REQUEST_SHORT_AUDIO_DESCRIPTOR;
1582         msg->reply = reply ? CEC_MSG_REPORT_SHORT_AUDIO_DESCRIPTOR : 0;
1583         for (i = 0; i < num_descriptors; i++)
1584                 msg->msg[2 + i] = (audio_format_id[i] << 6) |
1585                                   (audio_format_code[i] & 0x3f);
1586 }
1587
1588 static inline void cec_ops_request_short_audio_descriptor(const struct cec_msg *msg,
1589                                         __u8 *num_descriptors,
1590                                         __u8 *audio_format_id,
1591                                         __u8 *audio_format_code)
1592 {
1593         unsigned int i;
1594
1595         *num_descriptors = msg->len - 2;
1596         if (*num_descriptors > 4)
1597                 *num_descriptors = 4;
1598         for (i = 0; i < *num_descriptors; i++) {
1599                 audio_format_id[i] = msg->msg[2 + i] >> 6;
1600                 audio_format_code[i] = msg->msg[2 + i] & 0x3f;
1601         }
1602 }
1603
1604
1605 /* Audio Rate Control Feature */
1606 static inline void cec_msg_set_audio_rate(struct cec_msg *msg,
1607                                           __u8 audio_rate)
1608 {
1609         msg->len = 3;
1610         msg->msg[1] = CEC_MSG_SET_AUDIO_RATE;
1611         msg->msg[2] = audio_rate;
1612 }
1613
1614 static inline void cec_ops_set_audio_rate(const struct cec_msg *msg,
1615                                           __u8 *audio_rate)
1616 {
1617         *audio_rate = msg->msg[2];
1618 }
1619
1620
1621 /* Audio Return Channel Control Feature */
1622 static inline void cec_msg_report_arc_initiated(struct cec_msg *msg)
1623 {
1624         msg->len = 2;
1625         msg->msg[1] = CEC_MSG_REPORT_ARC_INITIATED;
1626 }
1627
1628 static inline void cec_msg_initiate_arc(struct cec_msg *msg,
1629                                         bool reply)
1630 {
1631         msg->len = 2;
1632         msg->msg[1] = CEC_MSG_INITIATE_ARC;
1633         msg->reply = reply ? CEC_MSG_REPORT_ARC_INITIATED : 0;
1634 }
1635
1636 static inline void cec_msg_request_arc_initiation(struct cec_msg *msg,
1637                                                   bool reply)
1638 {
1639         msg->len = 2;
1640         msg->msg[1] = CEC_MSG_REQUEST_ARC_INITIATION;
1641         msg->reply = reply ? CEC_MSG_INITIATE_ARC : 0;
1642 }
1643
1644 static inline void cec_msg_report_arc_terminated(struct cec_msg *msg)
1645 {
1646         msg->len = 2;
1647         msg->msg[1] = CEC_MSG_REPORT_ARC_TERMINATED;
1648 }
1649
1650 static inline void cec_msg_terminate_arc(struct cec_msg *msg,
1651                                          bool reply)
1652 {
1653         msg->len = 2;
1654         msg->msg[1] = CEC_MSG_TERMINATE_ARC;
1655         msg->reply = reply ? CEC_MSG_REPORT_ARC_TERMINATED : 0;
1656 }
1657
1658 static inline void cec_msg_request_arc_termination(struct cec_msg *msg,
1659                                                    bool reply)
1660 {
1661         msg->len = 2;
1662         msg->msg[1] = CEC_MSG_REQUEST_ARC_TERMINATION;
1663         msg->reply = reply ? CEC_MSG_TERMINATE_ARC : 0;
1664 }
1665
1666
1667 /* Dynamic Audio Lipsync Feature */
1668 /* Only for CEC 2.0 and up */
1669 static inline void cec_msg_report_current_latency(struct cec_msg *msg,
1670                                                   __u16 phys_addr,
1671                                                   __u8 video_latency,
1672                                                   __u8 low_latency_mode,
1673                                                   __u8 audio_out_compensated,
1674                                                   __u8 audio_out_delay)
1675 {
1676         msg->len = 7;
1677         msg->msg[0] |= 0xf; /* broadcast */
1678         msg->msg[1] = CEC_MSG_REPORT_CURRENT_LATENCY;
1679         msg->msg[2] = phys_addr >> 8;
1680         msg->msg[3] = phys_addr & 0xff;
1681         msg->msg[4] = video_latency;
1682         msg->msg[5] = (low_latency_mode << 2) | audio_out_compensated;
1683         msg->msg[6] = audio_out_delay;
1684 }
1685
1686 static inline void cec_ops_report_current_latency(const struct cec_msg *msg,
1687                                                   __u16 *phys_addr,
1688                                                   __u8 *video_latency,
1689                                                   __u8 *low_latency_mode,
1690                                                   __u8 *audio_out_compensated,
1691                                                   __u8 *audio_out_delay)
1692 {
1693         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1694         *video_latency = msg->msg[4];
1695         *low_latency_mode = (msg->msg[5] >> 2) & 1;
1696         *audio_out_compensated = msg->msg[5] & 3;
1697         *audio_out_delay = msg->msg[6];
1698 }
1699
1700 static inline void cec_msg_request_current_latency(struct cec_msg *msg,
1701                                                    bool reply,
1702                                                    __u16 phys_addr)
1703 {
1704         msg->len = 4;
1705         msg->msg[0] |= 0xf; /* broadcast */
1706         msg->msg[1] = CEC_MSG_REQUEST_CURRENT_LATENCY;
1707         msg->msg[2] = phys_addr >> 8;
1708         msg->msg[3] = phys_addr & 0xff;
1709         msg->reply = reply ? CEC_MSG_REPORT_CURRENT_LATENCY : 0;
1710 }
1711
1712 static inline void cec_ops_request_current_latency(const struct cec_msg *msg,
1713                                                    __u16 *phys_addr)
1714 {
1715         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1716 }
1717
1718
1719 /* Capability Discovery and Control Feature */
1720 static inline void cec_msg_cdc_hec_inquire_state(struct cec_msg *msg,
1721                                                  __u16 phys_addr1,
1722                                                  __u16 phys_addr2)
1723 {
1724         msg->len = 9;
1725         msg->msg[0] |= 0xf; /* broadcast */
1726         msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1727         /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1728         msg->msg[4] = CEC_MSG_CDC_HEC_INQUIRE_STATE;
1729         msg->msg[5] = phys_addr1 >> 8;
1730         msg->msg[6] = phys_addr1 & 0xff;
1731         msg->msg[7] = phys_addr2 >> 8;
1732         msg->msg[8] = phys_addr2 & 0xff;
1733 }
1734
1735 static inline void cec_ops_cdc_hec_inquire_state(const struct cec_msg *msg,
1736                                                  __u16 *phys_addr,
1737                                                  __u16 *phys_addr1,
1738                                                  __u16 *phys_addr2)
1739 {
1740         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1741         *phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
1742         *phys_addr2 = (msg->msg[7] << 8) | msg->msg[8];
1743 }
1744
1745 static inline void cec_msg_cdc_hec_report_state(struct cec_msg *msg,
1746                                                 __u16 target_phys_addr,
1747                                                 __u8 hec_func_state,
1748                                                 __u8 host_func_state,
1749                                                 __u8 enc_func_state,
1750                                                 __u8 cdc_errcode,
1751                                                 __u8 has_field,
1752                                                 __u16 hec_field)
1753 {
1754         msg->len = has_field ? 10 : 8;
1755         msg->msg[0] |= 0xf; /* broadcast */
1756         msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1757         /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1758         msg->msg[4] = CEC_MSG_CDC_HEC_REPORT_STATE;
1759         msg->msg[5] = target_phys_addr >> 8;
1760         msg->msg[6] = target_phys_addr & 0xff;
1761         msg->msg[7] = (hec_func_state << 6) |
1762                       (host_func_state << 4) |
1763                       (enc_func_state << 2) |
1764                       cdc_errcode;
1765         if (has_field) {
1766                 msg->msg[8] = hec_field >> 8;
1767                 msg->msg[9] = hec_field & 0xff;
1768         }
1769 }
1770
1771 static inline void cec_ops_cdc_hec_report_state(const struct cec_msg *msg,
1772                                                 __u16 *phys_addr,
1773                                                 __u16 *target_phys_addr,
1774                                                 __u8 *hec_func_state,
1775                                                 __u8 *host_func_state,
1776                                                 __u8 *enc_func_state,
1777                                                 __u8 *cdc_errcode,
1778                                                 __u8 *has_field,
1779                                                 __u16 *hec_field)
1780 {
1781         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1782         *target_phys_addr = (msg->msg[5] << 8) | msg->msg[6];
1783         *hec_func_state = msg->msg[7] >> 6;
1784         *host_func_state = (msg->msg[7] >> 4) & 3;
1785         *enc_func_state = (msg->msg[7] >> 4) & 3;
1786         *cdc_errcode = msg->msg[7] & 3;
1787         *has_field = msg->len >= 10;
1788         *hec_field = *has_field ? ((msg->msg[8] << 8) | msg->msg[9]) : 0;
1789 }
1790
1791 static inline void cec_msg_cdc_hec_set_state(struct cec_msg *msg,
1792                                              __u16 phys_addr1,
1793                                              __u16 phys_addr2,
1794                                              __u8 hec_set_state,
1795                                              __u16 phys_addr3,
1796                                              __u16 phys_addr4,
1797                                              __u16 phys_addr5)
1798 {
1799         msg->len = 10;
1800         msg->msg[0] |= 0xf; /* broadcast */
1801         msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1802         /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1803         msg->msg[4] = CEC_MSG_CDC_HEC_INQUIRE_STATE;
1804         msg->msg[5] = phys_addr1 >> 8;
1805         msg->msg[6] = phys_addr1 & 0xff;
1806         msg->msg[7] = phys_addr2 >> 8;
1807         msg->msg[8] = phys_addr2 & 0xff;
1808         msg->msg[9] = hec_set_state;
1809         if (phys_addr3 != CEC_PHYS_ADDR_INVALID) {
1810                 msg->msg[msg->len++] = phys_addr3 >> 8;
1811                 msg->msg[msg->len++] = phys_addr3 & 0xff;
1812                 if (phys_addr4 != CEC_PHYS_ADDR_INVALID) {
1813                         msg->msg[msg->len++] = phys_addr4 >> 8;
1814                         msg->msg[msg->len++] = phys_addr4 & 0xff;
1815                         if (phys_addr5 != CEC_PHYS_ADDR_INVALID) {
1816                                 msg->msg[msg->len++] = phys_addr5 >> 8;
1817                                 msg->msg[msg->len++] = phys_addr5 & 0xff;
1818                         }
1819                 }
1820         }
1821 }
1822
1823 static inline void cec_ops_cdc_hec_set_state(const struct cec_msg *msg,
1824                                              __u16 *phys_addr,
1825                                              __u16 *phys_addr1,
1826                                              __u16 *phys_addr2,
1827                                              __u8 *hec_set_state,
1828                                              __u16 *phys_addr3,
1829                                              __u16 *phys_addr4,
1830                                              __u16 *phys_addr5)
1831 {
1832         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1833         *phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
1834         *phys_addr2 = (msg->msg[7] << 8) | msg->msg[8];
1835         *hec_set_state = msg->msg[9];
1836         *phys_addr3 = *phys_addr4 = *phys_addr5 = CEC_PHYS_ADDR_INVALID;
1837         if (msg->len >= 12)
1838                 *phys_addr3 = (msg->msg[10] << 8) | msg->msg[11];
1839         if (msg->len >= 14)
1840                 *phys_addr4 = (msg->msg[12] << 8) | msg->msg[13];
1841         if (msg->len >= 16)
1842                 *phys_addr5 = (msg->msg[14] << 8) | msg->msg[15];
1843 }
1844
1845 static inline void cec_msg_cdc_hec_set_state_adjacent(struct cec_msg *msg,
1846                                                       __u16 phys_addr1,
1847                                                       __u8 hec_set_state)
1848 {
1849         msg->len = 8;
1850         msg->msg[0] |= 0xf; /* broadcast */
1851         msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1852         /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1853         msg->msg[4] = CEC_MSG_CDC_HEC_SET_STATE_ADJACENT;
1854         msg->msg[5] = phys_addr1 >> 8;
1855         msg->msg[6] = phys_addr1 & 0xff;
1856         msg->msg[7] = hec_set_state;
1857 }
1858
1859 static inline void cec_ops_cdc_hec_set_state_adjacent(const struct cec_msg *msg,
1860                                                       __u16 *phys_addr,
1861                                                       __u16 *phys_addr1,
1862                                                       __u8 *hec_set_state)
1863 {
1864         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1865         *phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
1866         *hec_set_state = msg->msg[7];
1867 }
1868
1869 static inline void cec_msg_cdc_hec_request_deactivation(struct cec_msg *msg,
1870                                                         __u16 phys_addr1,
1871                                                         __u16 phys_addr2,
1872                                                         __u16 phys_addr3)
1873 {
1874         msg->len = 11;
1875         msg->msg[0] |= 0xf; /* broadcast */
1876         msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1877         /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1878         msg->msg[4] = CEC_MSG_CDC_HEC_REQUEST_DEACTIVATION;
1879         msg->msg[5] = phys_addr1 >> 8;
1880         msg->msg[6] = phys_addr1 & 0xff;
1881         msg->msg[7] = phys_addr2 >> 8;
1882         msg->msg[8] = phys_addr2 & 0xff;
1883         msg->msg[9] = phys_addr3 >> 8;
1884         msg->msg[10] = phys_addr3 & 0xff;
1885 }
1886
1887 static inline void cec_ops_cdc_hec_request_deactivation(const struct cec_msg *msg,
1888                                                         __u16 *phys_addr,
1889                                                         __u16 *phys_addr1,
1890                                                         __u16 *phys_addr2,
1891                                                         __u16 *phys_addr3)
1892 {
1893         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1894         *phys_addr1 = (msg->msg[5] << 8) | msg->msg[6];
1895         *phys_addr2 = (msg->msg[7] << 8) | msg->msg[8];
1896         *phys_addr3 = (msg->msg[9] << 8) | msg->msg[10];
1897 }
1898
1899 static inline void cec_msg_cdc_hec_notify_alive(struct cec_msg *msg)
1900 {
1901         msg->len = 5;
1902         msg->msg[0] |= 0xf; /* broadcast */
1903         msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1904         /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1905         msg->msg[4] = CEC_MSG_CDC_HEC_NOTIFY_ALIVE;
1906 }
1907
1908 static inline void cec_ops_cdc_hec_notify_alive(const struct cec_msg *msg,
1909                                                 __u16 *phys_addr)
1910 {
1911         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1912 }
1913
1914 static inline void cec_msg_cdc_hec_discover(struct cec_msg *msg)
1915 {
1916         msg->len = 5;
1917         msg->msg[0] |= 0xf; /* broadcast */
1918         msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1919         /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1920         msg->msg[4] = CEC_MSG_CDC_HEC_DISCOVER;
1921 }
1922
1923 static inline void cec_ops_cdc_hec_discover(const struct cec_msg *msg,
1924                                             __u16 *phys_addr)
1925 {
1926         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1927 }
1928
1929 static inline void cec_msg_cdc_hpd_set_state(struct cec_msg *msg,
1930                                              __u8 input_port,
1931                                              __u8 hpd_state)
1932 {
1933         msg->len = 6;
1934         msg->msg[0] |= 0xf; /* broadcast */
1935         msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1936         /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1937         msg->msg[4] = CEC_MSG_CDC_HPD_SET_STATE;
1938         msg->msg[5] = (input_port << 4) | hpd_state;
1939 }
1940
1941 static inline void cec_ops_cdc_hpd_set_state(const struct cec_msg *msg,
1942                                             __u16 *phys_addr,
1943                                             __u8 *input_port,
1944                                             __u8 *hpd_state)
1945 {
1946         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1947         *input_port = msg->msg[5] >> 4;
1948         *hpd_state = msg->msg[5] & 0xf;
1949 }
1950
1951 static inline void cec_msg_cdc_hpd_report_state(struct cec_msg *msg,
1952                                                 __u8 hpd_state,
1953                                                 __u8 hpd_error)
1954 {
1955         msg->len = 6;
1956         msg->msg[0] |= 0xf; /* broadcast */
1957         msg->msg[1] = CEC_MSG_CDC_MESSAGE;
1958         /* msg[2] and msg[3] (phys_addr) are filled in by the CEC framework */
1959         msg->msg[4] = CEC_MSG_CDC_HPD_REPORT_STATE;
1960         msg->msg[5] = (hpd_state << 4) | hpd_error;
1961 }
1962
1963 static inline void cec_ops_cdc_hpd_report_state(const struct cec_msg *msg,
1964                                                 __u16 *phys_addr,
1965                                                 __u8 *hpd_state,
1966                                                 __u8 *hpd_error)
1967 {
1968         *phys_addr = (msg->msg[2] << 8) | msg->msg[3];
1969         *hpd_state = msg->msg[5] >> 4;
1970         *hpd_error = msg->msg[5] & 0xf;
1971 }
1972
1973 #endif