2 * RainShadow Tech HDMI CEC driver
4 * Copyright 2016 Hans Verkuil <hverkuil@xs4all.nl
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version of 2 of the License, or (at your
9 * option) any later version. See the file COPYING in the main directory of
10 * this archive for more details.
16 * The higher level protocols are currently disabled. This can be added
17 * later, similar to how this is done for the Pulse Eight CEC driver.
19 * Documentation of the protocol is available here:
21 * http://rainshadowtech.com/doc/HDMICECtoUSBandRS232v2.0.pdf
24 #include <linux/completion.h>
25 #include <linux/ctype.h>
26 #include <linux/delay.h>
27 #include <linux/init.h>
28 #include <linux/interrupt.h>
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/serio.h>
32 #include <linux/slab.h>
33 #include <linux/spinlock.h>
34 #include <linux/time.h>
35 #include <linux/workqueue.h>
37 #include <media/cec.h>
39 MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
40 MODULE_DESCRIPTION("RainShadow Tech HDMI CEC driver");
41 MODULE_LICENSE("GPL");
48 struct cec_adapter
*adap
;
49 struct completion cmd_done
;
50 struct work_struct work
;
52 /* Low-level ringbuffer, collecting incoming characters */
54 unsigned int buf_rd_idx
;
55 unsigned int buf_wr_idx
;
64 /* reply to a command, only used to store the firmware version */
65 char cmd_reply
[DATA_SIZE
];
67 struct mutex write_lock
;
70 static void rain_process_msg(struct rain
*rain
)
72 struct cec_msg msg
= {};
73 const char *cmd
= rain
->cmd
+ 3;
79 if (isxdigit(cmd
[0]) && isxdigit(cmd
[1])) {
80 if (msg
.len
== CEC_MAX_MSG_SIZE
)
82 if (hex2bin(msg
.msg
+ msg
.len
, cmd
, 1))
89 stat
= hex_to_bin(cmd
[0]);
93 if (rain
->cmd
[0] == 'R') {
94 if (stat
== 1 || stat
== 2)
95 cec_received_msg(rain
->adap
, &msg
);
101 cec_transmit_attempt_done(rain
->adap
, CEC_TX_STATUS_OK
);
104 cec_transmit_attempt_done(rain
->adap
, CEC_TX_STATUS_NACK
);
107 cec_transmit_attempt_done(rain
->adap
, CEC_TX_STATUS_LOW_DRIVE
);
112 static void rain_irq_work_handler(struct work_struct
*work
)
115 container_of(work
, struct rain
, work
);
119 bool exit_loop
= false;
122 spin_lock_irqsave(&rain
->buf_lock
, flags
);
124 data
= rain
->buf
[rain
->buf_rd_idx
];
126 rain
->buf_rd_idx
= (rain
->buf_rd_idx
+ 1) & 0xff;
130 spin_unlock_irqrestore(&rain
->buf_lock
, flags
);
135 if (!rain
->cmd_started
&& data
!= '?')
140 rain
->cmd
[rain
->cmd_idx
] = '\0';
141 dev_dbg(rain
->dev
, "received: %s\n", rain
->cmd
);
142 if (!memcmp(rain
->cmd
, "REC", 3) ||
143 !memcmp(rain
->cmd
, "STA", 3)) {
144 rain_process_msg(rain
);
146 strcpy(rain
->cmd_reply
, rain
->cmd
);
147 complete(&rain
->cmd_done
);
150 rain
->cmd_started
= false;
155 rain
->cmd_started
= false;
160 rain
->cmd_started
= true;
164 if (rain
->cmd_idx
>= DATA_SIZE
- 1) {
166 "throwing away %d bytes of garbage\n", rain
->cmd_idx
);
169 rain
->cmd
[rain
->cmd_idx
++] = data
;
175 static irqreturn_t
rain_interrupt(struct serio
*serio
, unsigned char data
,
178 struct rain
*rain
= serio_get_drvdata(serio
);
180 if (rain
->buf_len
== DATA_SIZE
) {
181 dev_warn_once(rain
->dev
, "buffer overflow\n");
184 spin_lock(&rain
->buf_lock
);
186 rain
->buf
[rain
->buf_wr_idx
] = data
;
187 rain
->buf_wr_idx
= (rain
->buf_wr_idx
+ 1) & 0xff;
188 spin_unlock(&rain
->buf_lock
);
189 schedule_work(&rain
->work
);
193 static void rain_disconnect(struct serio
*serio
)
195 struct rain
*rain
= serio_get_drvdata(serio
);
197 cancel_work_sync(&rain
->work
);
198 cec_unregister_adapter(rain
->adap
);
199 dev_info(&serio
->dev
, "disconnected\n");
201 serio_set_drvdata(serio
, NULL
);
205 static int rain_send(struct rain
*rain
, const char *command
)
207 int err
= serio_write(rain
->serio
, '!');
209 dev_dbg(rain
->dev
, "send: %s\n", command
);
210 while (!err
&& *command
)
211 err
= serio_write(rain
->serio
, *command
++);
213 err
= serio_write(rain
->serio
, '~');
218 static int rain_send_and_wait(struct rain
*rain
,
219 const char *cmd
, const char *reply
)
223 init_completion(&rain
->cmd_done
);
225 mutex_lock(&rain
->write_lock
);
226 err
= rain_send(rain
, cmd
);
230 if (!wait_for_completion_timeout(&rain
->cmd_done
, HZ
)) {
234 if (reply
&& strncmp(rain
->cmd_reply
, reply
, strlen(reply
))) {
236 "transmit of '%s': received '%s' instead of '%s'\n",
237 cmd
, rain
->cmd_reply
, reply
);
241 mutex_unlock(&rain
->write_lock
);
245 static int rain_setup(struct rain
*rain
, struct serio
*serio
,
246 struct cec_log_addrs
*log_addrs
, u16
*pa
)
250 err
= rain_send_and_wait(rain
, "R", "REV");
253 dev_info(rain
->dev
, "Firmware version %s\n", rain
->cmd_reply
+ 4);
255 err
= rain_send_and_wait(rain
, "Q 1", "QTY");
258 err
= rain_send_and_wait(rain
, "c0000", "CFG");
261 return rain_send_and_wait(rain
, "A F 0000", "ADR");
264 static int rain_cec_adap_enable(struct cec_adapter
*adap
, bool enable
)
269 static int rain_cec_adap_log_addr(struct cec_adapter
*adap
, u8 log_addr
)
271 struct rain
*rain
= cec_get_drvdata(adap
);
274 if (log_addr
== CEC_LOG_ADDR_INVALID
)
275 log_addr
= CEC_LOG_ADDR_UNREGISTERED
;
276 snprintf(cmd
, sizeof(cmd
), "A %x", log_addr
);
277 return rain_send_and_wait(rain
, cmd
, "ADR");
280 static int rain_cec_adap_transmit(struct cec_adapter
*adap
, u8 attempts
,
281 u32 signal_free_time
, struct cec_msg
*msg
)
283 struct rain
*rain
= cec_get_drvdata(adap
);
284 char cmd
[2 * CEC_MAX_MSG_SIZE
+ 16];
289 snprintf(cmd
, sizeof(cmd
), "x%x", cec_msg_destination(msg
));
293 snprintf(cmd
, sizeof(cmd
), "x%x %02x ",
294 cec_msg_destination(msg
), msg
->msg
[1]);
295 for (i
= 2; i
< msg
->len
; i
++) {
296 snprintf(hex
, sizeof(hex
), "%02x", msg
->msg
[i
]);
297 strlcat(cmd
, hex
, sizeof(cmd
));
300 mutex_lock(&rain
->write_lock
);
301 err
= rain_send(rain
, cmd
);
302 mutex_unlock(&rain
->write_lock
);
306 static const struct cec_adap_ops rain_cec_adap_ops
= {
307 .adap_enable
= rain_cec_adap_enable
,
308 .adap_log_addr
= rain_cec_adap_log_addr
,
309 .adap_transmit
= rain_cec_adap_transmit
,
312 static int rain_connect(struct serio
*serio
, struct serio_driver
*drv
)
314 u32 caps
= CEC_CAP_TRANSMIT
| CEC_CAP_LOG_ADDRS
| CEC_CAP_PHYS_ADDR
|
315 CEC_CAP_PASSTHROUGH
| CEC_CAP_RC
| CEC_CAP_MONITOR_ALL
;
318 struct cec_log_addrs log_addrs
= {};
319 u16 pa
= CEC_PHYS_ADDR_INVALID
;
321 rain
= kzalloc(sizeof(*rain
), GFP_KERNEL
);
327 rain
->adap
= cec_allocate_adapter(&rain_cec_adap_ops
, rain
,
328 "HDMI CEC", caps
, 1);
329 err
= PTR_ERR_OR_ZERO(rain
->adap
);
333 rain
->dev
= &serio
->dev
;
334 serio_set_drvdata(serio
, rain
);
335 INIT_WORK(&rain
->work
, rain_irq_work_handler
);
336 mutex_init(&rain
->write_lock
);
337 spin_lock_init(&rain
->buf_lock
);
339 err
= serio_open(serio
, drv
);
343 err
= rain_setup(rain
, serio
, &log_addrs
, &pa
);
347 err
= cec_register_adapter(rain
->adap
, &serio
->dev
);
351 rain
->dev
= &rain
->adap
->devnode
.dev
;
357 cec_delete_adapter(rain
->adap
);
358 serio_set_drvdata(serio
, NULL
);
364 static struct serio_device_id rain_serio_ids
[] = {
367 .proto
= SERIO_RAINSHADOW_CEC
,
374 MODULE_DEVICE_TABLE(serio
, rain_serio_ids
);
376 static struct serio_driver rain_drv
= {
378 .name
= "rainshadow-cec",
380 .description
= "RainShadow Tech HDMI CEC driver",
381 .id_table
= rain_serio_ids
,
382 .interrupt
= rain_interrupt
,
383 .connect
= rain_connect
,
384 .disconnect
= rain_disconnect
,
387 module_serio_driver(rain_drv
);