1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * RainShadow Tech HDMI CEC driver
5 * Copyright 2016 Hans Verkuil <hverkuil@xs4all.nl
11 * The higher level protocols are currently disabled. This can be added
12 * later, similar to how this is done for the Pulse Eight CEC driver.
14 * Documentation of the protocol is available here:
16 * http://rainshadowtech.com/doc/HDMICECtoUSBandRS232v2.0.pdf
19 #include <linux/completion.h>
20 #include <linux/ctype.h>
21 #include <linux/delay.h>
22 #include <linux/init.h>
23 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/serio.h>
27 #include <linux/slab.h>
28 #include <linux/spinlock.h>
29 #include <linux/time.h>
30 #include <linux/workqueue.h>
32 #include <media/cec.h>
34 MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
35 MODULE_DESCRIPTION("RainShadow Tech HDMI CEC driver");
36 MODULE_LICENSE("GPL");
43 struct cec_adapter
*adap
;
44 struct completion cmd_done
;
45 struct work_struct work
;
47 /* Low-level ringbuffer, collecting incoming characters */
49 unsigned int buf_rd_idx
;
50 unsigned int buf_wr_idx
;
59 /* reply to a command, only used to store the firmware version */
60 char cmd_reply
[DATA_SIZE
];
62 struct mutex write_lock
;
65 static void rain_process_msg(struct rain
*rain
)
67 struct cec_msg msg
= {};
68 const char *cmd
= rain
->cmd
+ 3;
74 if (isxdigit(cmd
[0]) && isxdigit(cmd
[1])) {
75 if (msg
.len
== CEC_MAX_MSG_SIZE
)
77 if (hex2bin(msg
.msg
+ msg
.len
, cmd
, 1))
84 stat
= hex_to_bin(cmd
[0]);
88 if (rain
->cmd
[0] == 'R') {
89 if (stat
== 1 || stat
== 2)
90 cec_received_msg(rain
->adap
, &msg
);
96 cec_transmit_attempt_done(rain
->adap
, CEC_TX_STATUS_OK
);
99 cec_transmit_attempt_done(rain
->adap
, CEC_TX_STATUS_NACK
);
102 cec_transmit_attempt_done(rain
->adap
, CEC_TX_STATUS_LOW_DRIVE
);
107 static void rain_irq_work_handler(struct work_struct
*work
)
110 container_of(work
, struct rain
, work
);
116 spin_lock_irqsave(&rain
->buf_lock
, flags
);
117 if (!rain
->buf_len
) {
118 spin_unlock_irqrestore(&rain
->buf_lock
, flags
);
122 data
= rain
->buf
[rain
->buf_rd_idx
];
124 rain
->buf_rd_idx
= (rain
->buf_rd_idx
+ 1) & 0xff;
126 spin_unlock_irqrestore(&rain
->buf_lock
, flags
);
128 if (!rain
->cmd_started
&& data
!= '?')
133 rain
->cmd
[rain
->cmd_idx
] = '\0';
134 dev_dbg(rain
->dev
, "received: %s\n", rain
->cmd
);
135 if (!memcmp(rain
->cmd
, "REC", 3) ||
136 !memcmp(rain
->cmd
, "STA", 3)) {
137 rain_process_msg(rain
);
139 strscpy(rain
->cmd_reply
, rain
->cmd
,
140 sizeof(rain
->cmd_reply
));
141 complete(&rain
->cmd_done
);
144 rain
->cmd_started
= false;
149 rain
->cmd_started
= false;
154 rain
->cmd_started
= true;
158 if (rain
->cmd_idx
>= DATA_SIZE
- 1) {
160 "throwing away %d bytes of garbage\n", rain
->cmd_idx
);
163 rain
->cmd
[rain
->cmd_idx
++] = data
;
169 static irqreturn_t
rain_interrupt(struct serio
*serio
, unsigned char data
,
172 struct rain
*rain
= serio_get_drvdata(serio
);
174 if (rain
->buf_len
== DATA_SIZE
) {
175 dev_warn_once(rain
->dev
, "buffer overflow\n");
178 spin_lock(&rain
->buf_lock
);
180 rain
->buf
[rain
->buf_wr_idx
] = data
;
181 rain
->buf_wr_idx
= (rain
->buf_wr_idx
+ 1) & 0xff;
182 spin_unlock(&rain
->buf_lock
);
183 schedule_work(&rain
->work
);
187 static void rain_disconnect(struct serio
*serio
)
189 struct rain
*rain
= serio_get_drvdata(serio
);
191 cancel_work_sync(&rain
->work
);
192 cec_unregister_adapter(rain
->adap
);
193 dev_info(&serio
->dev
, "disconnected\n");
195 serio_set_drvdata(serio
, NULL
);
199 static int rain_send(struct rain
*rain
, const char *command
)
201 int err
= serio_write(rain
->serio
, '!');
203 dev_dbg(rain
->dev
, "send: %s\n", command
);
204 while (!err
&& *command
)
205 err
= serio_write(rain
->serio
, *command
++);
207 err
= serio_write(rain
->serio
, '~');
212 static int rain_send_and_wait(struct rain
*rain
,
213 const char *cmd
, const char *reply
)
217 init_completion(&rain
->cmd_done
);
219 mutex_lock(&rain
->write_lock
);
220 err
= rain_send(rain
, cmd
);
224 if (!wait_for_completion_timeout(&rain
->cmd_done
, HZ
)) {
228 if (reply
&& strncmp(rain
->cmd_reply
, reply
, strlen(reply
))) {
230 "transmit of '%s': received '%s' instead of '%s'\n",
231 cmd
, rain
->cmd_reply
, reply
);
235 mutex_unlock(&rain
->write_lock
);
239 static int rain_setup(struct rain
*rain
, struct serio
*serio
,
240 struct cec_log_addrs
*log_addrs
, u16
*pa
)
244 err
= rain_send_and_wait(rain
, "R", "REV");
247 dev_info(rain
->dev
, "Firmware version %s\n", rain
->cmd_reply
+ 4);
249 err
= rain_send_and_wait(rain
, "Q 1", "QTY");
252 err
= rain_send_and_wait(rain
, "c0000", "CFG");
255 return rain_send_and_wait(rain
, "A F 0000", "ADR");
258 static int rain_cec_adap_enable(struct cec_adapter
*adap
, bool enable
)
263 static int rain_cec_adap_log_addr(struct cec_adapter
*adap
, u8 log_addr
)
265 struct rain
*rain
= cec_get_drvdata(adap
);
268 if (log_addr
== CEC_LOG_ADDR_INVALID
)
269 log_addr
= CEC_LOG_ADDR_UNREGISTERED
;
270 snprintf(cmd
, sizeof(cmd
), "A %x", log_addr
);
271 return rain_send_and_wait(rain
, cmd
, "ADR");
274 static int rain_cec_adap_transmit(struct cec_adapter
*adap
, u8 attempts
,
275 u32 signal_free_time
, struct cec_msg
*msg
)
277 struct rain
*rain
= cec_get_drvdata(adap
);
278 char cmd
[2 * CEC_MAX_MSG_SIZE
+ 16];
283 snprintf(cmd
, sizeof(cmd
), "x%x", cec_msg_destination(msg
));
287 snprintf(cmd
, sizeof(cmd
), "x%x %02x ",
288 cec_msg_destination(msg
), msg
->msg
[1]);
289 for (i
= 2; i
< msg
->len
; i
++) {
290 snprintf(hex
, sizeof(hex
), "%02x", msg
->msg
[i
]);
291 strlcat(cmd
, hex
, sizeof(cmd
));
294 mutex_lock(&rain
->write_lock
);
295 err
= rain_send(rain
, cmd
);
296 mutex_unlock(&rain
->write_lock
);
300 static const struct cec_adap_ops rain_cec_adap_ops
= {
301 .adap_enable
= rain_cec_adap_enable
,
302 .adap_log_addr
= rain_cec_adap_log_addr
,
303 .adap_transmit
= rain_cec_adap_transmit
,
306 static int rain_connect(struct serio
*serio
, struct serio_driver
*drv
)
308 u32 caps
= CEC_CAP_DEFAULTS
| CEC_CAP_PHYS_ADDR
| CEC_CAP_MONITOR_ALL
;
311 struct cec_log_addrs log_addrs
= {};
312 u16 pa
= CEC_PHYS_ADDR_INVALID
;
314 rain
= kzalloc(sizeof(*rain
), GFP_KERNEL
);
320 rain
->adap
= cec_allocate_adapter(&rain_cec_adap_ops
, rain
,
321 dev_name(&serio
->dev
), caps
, 1);
322 err
= PTR_ERR_OR_ZERO(rain
->adap
);
326 rain
->dev
= &serio
->dev
;
327 serio_set_drvdata(serio
, rain
);
328 INIT_WORK(&rain
->work
, rain_irq_work_handler
);
329 mutex_init(&rain
->write_lock
);
330 spin_lock_init(&rain
->buf_lock
);
332 err
= serio_open(serio
, drv
);
336 err
= rain_setup(rain
, serio
, &log_addrs
, &pa
);
340 err
= cec_register_adapter(rain
->adap
, &serio
->dev
);
344 rain
->dev
= &rain
->adap
->devnode
.dev
;
350 cec_delete_adapter(rain
->adap
);
351 serio_set_drvdata(serio
, NULL
);
357 static const struct serio_device_id rain_serio_ids
[] = {
360 .proto
= SERIO_RAINSHADOW_CEC
,
367 MODULE_DEVICE_TABLE(serio
, rain_serio_ids
);
369 static struct serio_driver rain_drv
= {
371 .name
= "rainshadow-cec",
373 .description
= "RainShadow Tech HDMI CEC driver",
374 .id_table
= rain_serio_ids
,
375 .interrupt
= rain_interrupt
,
376 .connect
= rain_connect
,
377 .disconnect
= rain_disconnect
,
380 module_serio_driver(rain_drv
);