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