treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / media / usb / pulse8-cec / pulse8-cec.c
blobafda438d4e0ac5c81e66ebc5626960538918d53f
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Pulse Eight HDMI CEC driver
5 * Copyright 2016 Hans Verkuil <hverkuil@xs4all.nl
6 */
8 /*
9 * Notes:
11 * - Devices with firmware version < 2 do not store their configuration in
12 * EEPROM.
14 * - In autonomous mode, only messages from a TV will be acknowledged, even
15 * polling messages. Upon receiving a message from a TV, the dongle will
16 * respond to messages from any logical address.
18 * - In autonomous mode, the dongle will by default reply Feature Abort
19 * [Unrecognized Opcode] when it receives Give Device Vendor ID. It will
20 * however observe vendor ID's reported by other devices and possibly
21 * alter this behavior. When TV's (and TV's only) report that their vendor ID
22 * is LG (0x00e091), the dongle will itself reply that it has the same vendor
23 * ID, and it will respond to at least one vendor specific command.
25 * - In autonomous mode, the dongle is known to attempt wakeup if it receives
26 * <User Control Pressed> ["Power On"], ["Power] or ["Power Toggle"], or if it
27 * receives <Set Stream Path> with its own physical address. It also does this
28 * if it receives <Vendor Specific Command> [0x03 0x00] from an LG TV.
31 #include <linux/completion.h>
32 #include <linux/init.h>
33 #include <linux/interrupt.h>
34 #include <linux/kernel.h>
35 #include <linux/module.h>
36 #include <linux/workqueue.h>
37 #include <linux/serio.h>
38 #include <linux/slab.h>
39 #include <linux/time.h>
40 #include <linux/delay.h>
42 #include <media/cec.h>
44 MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
45 MODULE_DESCRIPTION("Pulse Eight HDMI CEC driver");
46 MODULE_LICENSE("GPL");
48 static int debug;
49 static int persistent_config;
50 module_param(debug, int, 0644);
51 module_param(persistent_config, int, 0644);
52 MODULE_PARM_DESC(debug, "debug level (0-2)");
53 MODULE_PARM_DESC(persistent_config, "read config from persistent memory (0-1)");
55 enum pulse8_msgcodes {
56 MSGCODE_NOTHING = 0,
57 MSGCODE_PING,
58 MSGCODE_TIMEOUT_ERROR,
59 MSGCODE_HIGH_ERROR,
60 MSGCODE_LOW_ERROR,
61 MSGCODE_FRAME_START,
62 MSGCODE_FRAME_DATA,
63 MSGCODE_RECEIVE_FAILED,
64 MSGCODE_COMMAND_ACCEPTED, /* 0x08 */
65 MSGCODE_COMMAND_REJECTED,
66 MSGCODE_SET_ACK_MASK,
67 MSGCODE_TRANSMIT,
68 MSGCODE_TRANSMIT_EOM,
69 MSGCODE_TRANSMIT_IDLETIME,
70 MSGCODE_TRANSMIT_ACK_POLARITY,
71 MSGCODE_TRANSMIT_LINE_TIMEOUT,
72 MSGCODE_TRANSMIT_SUCCEEDED, /* 0x10 */
73 MSGCODE_TRANSMIT_FAILED_LINE,
74 MSGCODE_TRANSMIT_FAILED_ACK,
75 MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA,
76 MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE,
77 MSGCODE_FIRMWARE_VERSION,
78 MSGCODE_START_BOOTLOADER,
79 MSGCODE_GET_BUILDDATE,
80 MSGCODE_SET_CONTROLLED, /* 0x18 */
81 MSGCODE_GET_AUTO_ENABLED,
82 MSGCODE_SET_AUTO_ENABLED,
83 MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS,
84 MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS,
85 MSGCODE_GET_LOGICAL_ADDRESS_MASK,
86 MSGCODE_SET_LOGICAL_ADDRESS_MASK,
87 MSGCODE_GET_PHYSICAL_ADDRESS,
88 MSGCODE_SET_PHYSICAL_ADDRESS, /* 0x20 */
89 MSGCODE_GET_DEVICE_TYPE,
90 MSGCODE_SET_DEVICE_TYPE,
91 MSGCODE_GET_HDMI_VERSION,
92 MSGCODE_SET_HDMI_VERSION,
93 MSGCODE_GET_OSD_NAME,
94 MSGCODE_SET_OSD_NAME,
95 MSGCODE_WRITE_EEPROM,
96 MSGCODE_GET_ADAPTER_TYPE, /* 0x28 */
97 MSGCODE_SET_ACTIVE_SOURCE,
99 MSGCODE_FRAME_EOM = 0x80,
100 MSGCODE_FRAME_ACK = 0x40,
103 static const char * const pulse8_msgnames[] = {
104 "NOTHING",
105 "PING",
106 "TIMEOUT_ERROR",
107 "HIGH_ERROR",
108 "LOW_ERROR",
109 "FRAME_START",
110 "FRAME_DATA",
111 "RECEIVE_FAILED",
112 "COMMAND_ACCEPTED",
113 "COMMAND_REJECTED",
114 "SET_ACK_MASK",
115 "TRANSMIT",
116 "TRANSMIT_EOM",
117 "TRANSMIT_IDLETIME",
118 "TRANSMIT_ACK_POLARITY",
119 "TRANSMIT_LINE_TIMEOUT",
120 "TRANSMIT_SUCCEEDED",
121 "TRANSMIT_FAILED_LINE",
122 "TRANSMIT_FAILED_ACK",
123 "TRANSMIT_FAILED_TIMEOUT_DATA",
124 "TRANSMIT_FAILED_TIMEOUT_LINE",
125 "FIRMWARE_VERSION",
126 "START_BOOTLOADER",
127 "GET_BUILDDATE",
128 "SET_CONTROLLED",
129 "GET_AUTO_ENABLED",
130 "SET_AUTO_ENABLED",
131 "GET_DEFAULT_LOGICAL_ADDRESS",
132 "SET_DEFAULT_LOGICAL_ADDRESS",
133 "GET_LOGICAL_ADDRESS_MASK",
134 "SET_LOGICAL_ADDRESS_MASK",
135 "GET_PHYSICAL_ADDRESS",
136 "SET_PHYSICAL_ADDRESS",
137 "GET_DEVICE_TYPE",
138 "SET_DEVICE_TYPE",
139 "GET_HDMI_VERSION",
140 "SET_HDMI_VERSION",
141 "GET_OSD_NAME",
142 "SET_OSD_NAME",
143 "WRITE_EEPROM",
144 "GET_ADAPTER_TYPE",
145 "SET_ACTIVE_SOURCE",
148 static const char *pulse8_msgname(u8 cmd)
150 static char unknown_msg[5];
152 if ((cmd & 0x3f) < ARRAY_SIZE(pulse8_msgnames))
153 return pulse8_msgnames[cmd & 0x3f];
154 snprintf(unknown_msg, sizeof(unknown_msg), "0x%02x", cmd);
155 return unknown_msg;
158 #define MSGSTART 0xff
159 #define MSGEND 0xfe
160 #define MSGESC 0xfd
161 #define MSGOFFSET 3
163 #define DATA_SIZE 256
165 #define PING_PERIOD (15 * HZ)
167 #define NUM_MSGS 8
169 struct pulse8 {
170 struct device *dev;
171 struct serio *serio;
172 struct cec_adapter *adap;
173 unsigned int vers;
175 struct delayed_work ping_eeprom_work;
177 struct work_struct irq_work;
178 struct cec_msg rx_msg[NUM_MSGS];
179 unsigned int rx_msg_cur_idx, rx_msg_num;
180 /* protect rx_msg_cur_idx and rx_msg_num */
181 spinlock_t msg_lock;
182 u8 new_rx_msg[CEC_MAX_MSG_SIZE];
183 u8 new_rx_msg_len;
185 struct work_struct tx_work;
186 u32 tx_done_status;
187 u32 tx_signal_free_time;
188 struct cec_msg tx_msg;
189 bool tx_msg_is_bcast;
191 struct completion cmd_done;
192 u8 data[DATA_SIZE];
193 unsigned int len;
194 u8 buf[DATA_SIZE];
195 unsigned int idx;
196 bool escape;
197 bool started;
199 /* locks access to the adapter */
200 struct mutex lock;
201 bool config_pending;
202 bool restoring_config;
203 bool autonomous;
206 static int pulse8_send(struct serio *serio, const u8 *command, u8 cmd_len)
208 int err = 0;
210 err = serio_write(serio, MSGSTART);
211 if (err)
212 return err;
213 for (; !err && cmd_len; command++, cmd_len--) {
214 if (*command >= MSGESC) {
215 err = serio_write(serio, MSGESC);
216 if (!err)
217 err = serio_write(serio, *command - MSGOFFSET);
218 } else {
219 err = serio_write(serio, *command);
222 if (!err)
223 err = serio_write(serio, MSGEND);
225 return err;
228 static int pulse8_send_and_wait_once(struct pulse8 *pulse8,
229 const u8 *cmd, u8 cmd_len,
230 u8 response, u8 size)
232 int err;
234 if (debug > 1)
235 dev_info(pulse8->dev, "transmit %s: %*ph\n",
236 pulse8_msgname(cmd[0]), cmd_len, cmd);
237 init_completion(&pulse8->cmd_done);
239 err = pulse8_send(pulse8->serio, cmd, cmd_len);
240 if (err)
241 return err;
243 if (!wait_for_completion_timeout(&pulse8->cmd_done, HZ))
244 return -ETIMEDOUT;
245 if ((pulse8->data[0] & 0x3f) == MSGCODE_COMMAND_REJECTED &&
246 cmd[0] != MSGCODE_SET_CONTROLLED &&
247 cmd[0] != MSGCODE_SET_AUTO_ENABLED &&
248 cmd[0] != MSGCODE_GET_BUILDDATE)
249 return -ENOTTY;
250 if (response &&
251 ((pulse8->data[0] & 0x3f) != response || pulse8->len < size + 1)) {
252 dev_info(pulse8->dev, "transmit %s failed with %s\n",
253 pulse8_msgname(cmd[0]),
254 pulse8_msgname(pulse8->data[0]));
255 return -EIO;
257 return 0;
260 static int pulse8_send_and_wait(struct pulse8 *pulse8,
261 const u8 *cmd, u8 cmd_len, u8 response, u8 size)
263 u8 cmd_sc[2];
264 int err;
266 err = pulse8_send_and_wait_once(pulse8, cmd, cmd_len, response, size);
267 if (err != -ENOTTY)
268 return err;
270 cmd_sc[0] = MSGCODE_SET_CONTROLLED;
271 cmd_sc[1] = 1;
272 err = pulse8_send_and_wait_once(pulse8, cmd_sc, 2,
273 MSGCODE_COMMAND_ACCEPTED, 1);
274 if (!err)
275 err = pulse8_send_and_wait_once(pulse8, cmd, cmd_len,
276 response, size);
277 return err == -ENOTTY ? -EIO : err;
280 static void pulse8_tx_work_handler(struct work_struct *work)
282 struct pulse8 *pulse8 = container_of(work, struct pulse8, tx_work);
283 struct cec_msg *msg = &pulse8->tx_msg;
284 unsigned int i;
285 u8 cmd[2];
286 int err;
288 if (msg->len == 0)
289 return;
291 mutex_lock(&pulse8->lock);
292 cmd[0] = MSGCODE_TRANSMIT_IDLETIME;
293 cmd[1] = pulse8->tx_signal_free_time;
294 err = pulse8_send_and_wait(pulse8, cmd, 2,
295 MSGCODE_COMMAND_ACCEPTED, 1);
296 cmd[0] = MSGCODE_TRANSMIT_ACK_POLARITY;
297 cmd[1] = cec_msg_is_broadcast(msg);
298 pulse8->tx_msg_is_bcast = cec_msg_is_broadcast(msg);
299 if (!err)
300 err = pulse8_send_and_wait(pulse8, cmd, 2,
301 MSGCODE_COMMAND_ACCEPTED, 1);
302 cmd[0] = msg->len == 1 ? MSGCODE_TRANSMIT_EOM : MSGCODE_TRANSMIT;
303 cmd[1] = msg->msg[0];
304 if (!err)
305 err = pulse8_send_and_wait(pulse8, cmd, 2,
306 MSGCODE_COMMAND_ACCEPTED, 1);
307 if (!err && msg->len > 1) {
308 for (i = 1; !err && i < msg->len; i++) {
309 cmd[0] = ((i == msg->len - 1)) ?
310 MSGCODE_TRANSMIT_EOM : MSGCODE_TRANSMIT;
311 cmd[1] = msg->msg[i];
312 err = pulse8_send_and_wait(pulse8, cmd, 2,
313 MSGCODE_COMMAND_ACCEPTED, 1);
316 if (err && debug)
317 dev_info(pulse8->dev, "%s(0x%02x) failed with error %d for msg %*ph\n",
318 pulse8_msgname(cmd[0]), cmd[1],
319 err, msg->len, msg->msg);
320 msg->len = 0;
321 mutex_unlock(&pulse8->lock);
322 if (err)
323 cec_transmit_attempt_done(pulse8->adap, CEC_TX_STATUS_ERROR);
326 static void pulse8_irq_work_handler(struct work_struct *work)
328 struct pulse8 *pulse8 =
329 container_of(work, struct pulse8, irq_work);
330 unsigned long flags;
331 u32 status;
333 spin_lock_irqsave(&pulse8->msg_lock, flags);
334 while (pulse8->rx_msg_num) {
335 spin_unlock_irqrestore(&pulse8->msg_lock, flags);
336 if (debug)
337 dev_info(pulse8->dev, "adap received %*ph\n",
338 pulse8->rx_msg[pulse8->rx_msg_cur_idx].len,
339 pulse8->rx_msg[pulse8->rx_msg_cur_idx].msg);
340 cec_received_msg(pulse8->adap,
341 &pulse8->rx_msg[pulse8->rx_msg_cur_idx]);
342 spin_lock_irqsave(&pulse8->msg_lock, flags);
343 if (pulse8->rx_msg_num)
344 pulse8->rx_msg_num--;
345 pulse8->rx_msg_cur_idx =
346 (pulse8->rx_msg_cur_idx + 1) % NUM_MSGS;
348 spin_unlock_irqrestore(&pulse8->msg_lock, flags);
350 mutex_lock(&pulse8->lock);
351 status = pulse8->tx_done_status;
352 pulse8->tx_done_status = 0;
353 mutex_unlock(&pulse8->lock);
354 if (status)
355 cec_transmit_attempt_done(pulse8->adap, status);
358 static irqreturn_t pulse8_interrupt(struct serio *serio, unsigned char data,
359 unsigned int flags)
361 struct pulse8 *pulse8 = serio_get_drvdata(serio);
362 unsigned long irq_flags;
363 unsigned int idx;
365 if (!pulse8->started && data != MSGSTART)
366 return IRQ_HANDLED;
367 if (data == MSGESC) {
368 pulse8->escape = true;
369 return IRQ_HANDLED;
371 if (pulse8->escape) {
372 data += MSGOFFSET;
373 pulse8->escape = false;
374 } else if (data == MSGEND) {
375 u8 msgcode = pulse8->buf[0];
377 if (debug > 1)
378 dev_info(pulse8->dev, "received %s: %*ph\n",
379 pulse8_msgname(msgcode),
380 pulse8->idx, pulse8->buf);
381 switch (msgcode & 0x3f) {
382 case MSGCODE_FRAME_START:
384 * Test if we are receiving a new msg when a previous
385 * message is still pending.
387 if (!(msgcode & MSGCODE_FRAME_EOM)) {
388 pulse8->new_rx_msg_len = 1;
389 pulse8->new_rx_msg[0] = pulse8->buf[1];
390 break;
392 /* fall through */
393 case MSGCODE_FRAME_DATA:
394 if (pulse8->new_rx_msg_len < CEC_MAX_MSG_SIZE)
395 pulse8->new_rx_msg[pulse8->new_rx_msg_len++] =
396 pulse8->buf[1];
397 if (!(msgcode & MSGCODE_FRAME_EOM))
398 break;
400 spin_lock_irqsave(&pulse8->msg_lock, irq_flags);
401 idx = (pulse8->rx_msg_cur_idx + pulse8->rx_msg_num) %
402 NUM_MSGS;
403 if (pulse8->rx_msg_num == NUM_MSGS) {
404 dev_warn(pulse8->dev,
405 "message queue is full, dropping %*ph\n",
406 pulse8->new_rx_msg_len,
407 pulse8->new_rx_msg);
408 spin_unlock_irqrestore(&pulse8->msg_lock,
409 irq_flags);
410 pulse8->new_rx_msg_len = 0;
411 break;
413 pulse8->rx_msg_num++;
414 memcpy(pulse8->rx_msg[idx].msg, pulse8->new_rx_msg,
415 pulse8->new_rx_msg_len);
416 pulse8->rx_msg[idx].len = pulse8->new_rx_msg_len;
417 spin_unlock_irqrestore(&pulse8->msg_lock, irq_flags);
418 schedule_work(&pulse8->irq_work);
419 pulse8->new_rx_msg_len = 0;
420 break;
421 case MSGCODE_TRANSMIT_SUCCEEDED:
422 WARN_ON(pulse8->tx_done_status);
423 pulse8->tx_done_status = CEC_TX_STATUS_OK;
424 schedule_work(&pulse8->irq_work);
425 break;
426 case MSGCODE_TRANSMIT_FAILED_ACK:
428 * A NACK for a broadcast message makes no sense, these
429 * seem to be spurious messages and are skipped.
431 if (pulse8->tx_msg_is_bcast)
432 break;
433 WARN_ON(pulse8->tx_done_status);
434 pulse8->tx_done_status = CEC_TX_STATUS_NACK;
435 schedule_work(&pulse8->irq_work);
436 break;
437 case MSGCODE_TRANSMIT_FAILED_LINE:
438 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
439 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
440 WARN_ON(pulse8->tx_done_status);
441 pulse8->tx_done_status = CEC_TX_STATUS_ERROR;
442 schedule_work(&pulse8->irq_work);
443 break;
444 case MSGCODE_HIGH_ERROR:
445 case MSGCODE_LOW_ERROR:
446 case MSGCODE_RECEIVE_FAILED:
447 case MSGCODE_TIMEOUT_ERROR:
448 pulse8->new_rx_msg_len = 0;
449 break;
450 case MSGCODE_COMMAND_ACCEPTED:
451 case MSGCODE_COMMAND_REJECTED:
452 default:
453 if (pulse8->idx == 0)
454 break;
455 memcpy(pulse8->data, pulse8->buf, pulse8->idx);
456 pulse8->len = pulse8->idx;
457 complete(&pulse8->cmd_done);
458 break;
460 pulse8->idx = 0;
461 pulse8->started = false;
462 return IRQ_HANDLED;
463 } else if (data == MSGSTART) {
464 pulse8->idx = 0;
465 pulse8->started = true;
466 return IRQ_HANDLED;
469 if (pulse8->idx >= DATA_SIZE) {
470 dev_dbg(pulse8->dev,
471 "throwing away %d bytes of garbage\n", pulse8->idx);
472 pulse8->idx = 0;
474 pulse8->buf[pulse8->idx++] = data;
475 return IRQ_HANDLED;
478 static int pulse8_cec_adap_enable(struct cec_adapter *adap, bool enable)
480 struct pulse8 *pulse8 = cec_get_drvdata(adap);
481 u8 cmd[16];
482 int err;
484 mutex_lock(&pulse8->lock);
485 cmd[0] = MSGCODE_SET_CONTROLLED;
486 cmd[1] = enable;
487 err = pulse8_send_and_wait(pulse8, cmd, 2,
488 MSGCODE_COMMAND_ACCEPTED, 1);
489 if (!enable) {
490 pulse8->rx_msg_num = 0;
491 pulse8->tx_done_status = 0;
493 mutex_unlock(&pulse8->lock);
494 return enable ? err : 0;
497 static int pulse8_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
499 struct pulse8 *pulse8 = cec_get_drvdata(adap);
500 u16 mask = 0;
501 u16 pa = adap->phys_addr;
502 u8 cmd[16];
503 int err = 0;
505 mutex_lock(&pulse8->lock);
506 if (log_addr != CEC_LOG_ADDR_INVALID)
507 mask = 1 << log_addr;
508 cmd[0] = MSGCODE_SET_ACK_MASK;
509 cmd[1] = mask >> 8;
510 cmd[2] = mask & 0xff;
511 err = pulse8_send_and_wait(pulse8, cmd, 3,
512 MSGCODE_COMMAND_ACCEPTED, 0);
513 if ((err && mask != 0) || pulse8->restoring_config)
514 goto unlock;
516 cmd[0] = MSGCODE_SET_AUTO_ENABLED;
517 cmd[1] = log_addr == CEC_LOG_ADDR_INVALID ? 0 : 1;
518 err = pulse8_send_and_wait(pulse8, cmd, 2,
519 MSGCODE_COMMAND_ACCEPTED, 0);
520 if (err)
521 goto unlock;
522 pulse8->autonomous = cmd[1];
523 if (log_addr == CEC_LOG_ADDR_INVALID)
524 goto unlock;
526 cmd[0] = MSGCODE_SET_DEVICE_TYPE;
527 cmd[1] = adap->log_addrs.primary_device_type[0];
528 err = pulse8_send_and_wait(pulse8, cmd, 2,
529 MSGCODE_COMMAND_ACCEPTED, 0);
530 if (err)
531 goto unlock;
533 switch (adap->log_addrs.primary_device_type[0]) {
534 case CEC_OP_PRIM_DEVTYPE_TV:
535 mask = CEC_LOG_ADDR_MASK_TV;
536 break;
537 case CEC_OP_PRIM_DEVTYPE_RECORD:
538 mask = CEC_LOG_ADDR_MASK_RECORD;
539 break;
540 case CEC_OP_PRIM_DEVTYPE_TUNER:
541 mask = CEC_LOG_ADDR_MASK_TUNER;
542 break;
543 case CEC_OP_PRIM_DEVTYPE_PLAYBACK:
544 mask = CEC_LOG_ADDR_MASK_PLAYBACK;
545 break;
546 case CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM:
547 mask = CEC_LOG_ADDR_MASK_AUDIOSYSTEM;
548 break;
549 case CEC_OP_PRIM_DEVTYPE_SWITCH:
550 mask = CEC_LOG_ADDR_MASK_UNREGISTERED;
551 break;
552 case CEC_OP_PRIM_DEVTYPE_PROCESSOR:
553 mask = CEC_LOG_ADDR_MASK_SPECIFIC;
554 break;
555 default:
556 mask = 0;
557 break;
559 cmd[0] = MSGCODE_SET_LOGICAL_ADDRESS_MASK;
560 cmd[1] = mask >> 8;
561 cmd[2] = mask & 0xff;
562 err = pulse8_send_and_wait(pulse8, cmd, 3,
563 MSGCODE_COMMAND_ACCEPTED, 0);
564 if (err)
565 goto unlock;
567 cmd[0] = MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS;
568 cmd[1] = log_addr;
569 err = pulse8_send_and_wait(pulse8, cmd, 2,
570 MSGCODE_COMMAND_ACCEPTED, 0);
571 if (err)
572 goto unlock;
574 cmd[0] = MSGCODE_SET_PHYSICAL_ADDRESS;
575 cmd[1] = pa >> 8;
576 cmd[2] = pa & 0xff;
577 err = pulse8_send_and_wait(pulse8, cmd, 3,
578 MSGCODE_COMMAND_ACCEPTED, 0);
579 if (err)
580 goto unlock;
582 cmd[0] = MSGCODE_SET_HDMI_VERSION;
583 cmd[1] = adap->log_addrs.cec_version;
584 err = pulse8_send_and_wait(pulse8, cmd, 2,
585 MSGCODE_COMMAND_ACCEPTED, 0);
586 if (err)
587 goto unlock;
589 if (adap->log_addrs.osd_name[0]) {
590 size_t osd_len = strlen(adap->log_addrs.osd_name);
591 char *osd_str = cmd + 1;
593 cmd[0] = MSGCODE_SET_OSD_NAME;
594 strscpy(cmd + 1, adap->log_addrs.osd_name, sizeof(cmd) - 1);
595 if (osd_len < 4) {
596 memset(osd_str + osd_len, ' ', 4 - osd_len);
597 osd_len = 4;
598 osd_str[osd_len] = '\0';
599 strscpy(adap->log_addrs.osd_name, osd_str,
600 sizeof(adap->log_addrs.osd_name));
602 err = pulse8_send_and_wait(pulse8, cmd, 1 + osd_len,
603 MSGCODE_COMMAND_ACCEPTED, 0);
604 if (err)
605 goto unlock;
608 unlock:
609 if (pulse8->restoring_config)
610 pulse8->restoring_config = false;
611 else
612 pulse8->config_pending = true;
613 mutex_unlock(&pulse8->lock);
614 return log_addr == CEC_LOG_ADDR_INVALID ? 0 : err;
617 static int pulse8_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
618 u32 signal_free_time, struct cec_msg *msg)
620 struct pulse8 *pulse8 = cec_get_drvdata(adap);
622 pulse8->tx_msg = *msg;
623 if (debug)
624 dev_info(pulse8->dev, "adap transmit %*ph\n",
625 msg->len, msg->msg);
626 pulse8->tx_signal_free_time = signal_free_time;
627 schedule_work(&pulse8->tx_work);
628 return 0;
631 static void pulse8_cec_adap_free(struct cec_adapter *adap)
633 struct pulse8 *pulse8 = cec_get_drvdata(adap);
635 cancel_delayed_work_sync(&pulse8->ping_eeprom_work);
636 cancel_work_sync(&pulse8->irq_work);
637 cancel_work_sync(&pulse8->tx_work);
638 serio_close(pulse8->serio);
639 serio_set_drvdata(pulse8->serio, NULL);
640 kfree(pulse8);
643 static const struct cec_adap_ops pulse8_cec_adap_ops = {
644 .adap_enable = pulse8_cec_adap_enable,
645 .adap_log_addr = pulse8_cec_adap_log_addr,
646 .adap_transmit = pulse8_cec_adap_transmit,
647 .adap_free = pulse8_cec_adap_free,
650 static void pulse8_disconnect(struct serio *serio)
652 struct pulse8 *pulse8 = serio_get_drvdata(serio);
654 cec_unregister_adapter(pulse8->adap);
657 static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio,
658 struct cec_log_addrs *log_addrs, u16 *pa)
660 u8 *data = pulse8->data + 1;
661 u8 cmd[2];
662 int err;
663 struct tm tm;
664 time64_t date;
666 pulse8->vers = 0;
668 cmd[0] = MSGCODE_FIRMWARE_VERSION;
669 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2);
670 if (err)
671 return err;
672 pulse8->vers = (data[0] << 8) | data[1];
673 dev_info(pulse8->dev, "Firmware version %04x\n", pulse8->vers);
674 if (pulse8->vers < 2) {
675 *pa = CEC_PHYS_ADDR_INVALID;
676 return 0;
679 cmd[0] = MSGCODE_GET_BUILDDATE;
680 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 4);
681 if (err)
682 return err;
683 date = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
684 time64_to_tm(date, 0, &tm);
685 dev_info(pulse8->dev, "Firmware build date %04ld.%02d.%02d %02d:%02d:%02d\n",
686 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
687 tm.tm_hour, tm.tm_min, tm.tm_sec);
689 dev_dbg(pulse8->dev, "Persistent config:\n");
690 cmd[0] = MSGCODE_GET_AUTO_ENABLED;
691 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
692 if (err)
693 return err;
694 pulse8->autonomous = data[0];
695 dev_dbg(pulse8->dev, "Autonomous mode: %s",
696 data[0] ? "on" : "off");
698 cmd[0] = MSGCODE_GET_DEVICE_TYPE;
699 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
700 if (err)
701 return err;
702 log_addrs->primary_device_type[0] = data[0];
703 dev_dbg(pulse8->dev, "Primary device type: %d\n", data[0]);
704 switch (log_addrs->primary_device_type[0]) {
705 case CEC_OP_PRIM_DEVTYPE_TV:
706 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TV;
707 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_TV;
708 break;
709 case CEC_OP_PRIM_DEVTYPE_RECORD:
710 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_RECORD;
711 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_RECORD;
712 break;
713 case CEC_OP_PRIM_DEVTYPE_TUNER:
714 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TUNER;
715 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_TUNER;
716 break;
717 case CEC_OP_PRIM_DEVTYPE_PLAYBACK:
718 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK;
719 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_PLAYBACK;
720 break;
721 case CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM:
722 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK;
723 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_AUDIOSYSTEM;
724 break;
725 case CEC_OP_PRIM_DEVTYPE_SWITCH:
726 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
727 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
728 break;
729 case CEC_OP_PRIM_DEVTYPE_PROCESSOR:
730 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_SPECIFIC;
731 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
732 break;
733 default:
734 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
735 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
736 dev_info(pulse8->dev, "Unknown Primary Device Type: %d\n",
737 log_addrs->primary_device_type[0]);
738 break;
741 cmd[0] = MSGCODE_GET_LOGICAL_ADDRESS_MASK;
742 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2);
743 if (err)
744 return err;
745 log_addrs->log_addr_mask = (data[0] << 8) | data[1];
746 dev_dbg(pulse8->dev, "Logical address ACK mask: %x\n",
747 log_addrs->log_addr_mask);
748 if (log_addrs->log_addr_mask)
749 log_addrs->num_log_addrs = 1;
751 cmd[0] = MSGCODE_GET_PHYSICAL_ADDRESS;
752 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
753 if (err)
754 return err;
755 *pa = (data[0] << 8) | data[1];
756 dev_dbg(pulse8->dev, "Physical address: %x.%x.%x.%x\n",
757 cec_phys_addr_exp(*pa));
759 cmd[0] = MSGCODE_GET_HDMI_VERSION;
760 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
761 if (err)
762 return err;
763 log_addrs->cec_version = data[0];
764 dev_dbg(pulse8->dev, "CEC version: %d\n", log_addrs->cec_version);
766 cmd[0] = MSGCODE_GET_OSD_NAME;
767 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 0);
768 if (err)
769 return err;
770 strscpy(log_addrs->osd_name, data, sizeof(log_addrs->osd_name));
771 dev_dbg(pulse8->dev, "OSD name: %s\n", log_addrs->osd_name);
773 return 0;
776 static int pulse8_apply_persistent_config(struct pulse8 *pulse8,
777 struct cec_log_addrs *log_addrs,
778 u16 pa)
780 int err;
782 err = cec_s_log_addrs(pulse8->adap, log_addrs, false);
783 if (err)
784 return err;
786 cec_s_phys_addr(pulse8->adap, pa, false);
788 return 0;
791 static void pulse8_ping_eeprom_work_handler(struct work_struct *work)
793 struct pulse8 *pulse8 =
794 container_of(work, struct pulse8, ping_eeprom_work.work);
795 u8 cmd;
797 mutex_lock(&pulse8->lock);
798 cmd = MSGCODE_PING;
799 pulse8_send_and_wait(pulse8, &cmd, 1,
800 MSGCODE_COMMAND_ACCEPTED, 0);
802 if (pulse8->vers < 2)
803 goto unlock;
805 if (pulse8->config_pending && persistent_config) {
806 dev_dbg(pulse8->dev, "writing pending config to EEPROM\n");
807 cmd = MSGCODE_WRITE_EEPROM;
808 if (pulse8_send_and_wait(pulse8, &cmd, 1,
809 MSGCODE_COMMAND_ACCEPTED, 0))
810 dev_info(pulse8->dev, "failed to write pending config to EEPROM\n");
811 else
812 pulse8->config_pending = false;
814 unlock:
815 schedule_delayed_work(&pulse8->ping_eeprom_work, PING_PERIOD);
816 mutex_unlock(&pulse8->lock);
819 static int pulse8_connect(struct serio *serio, struct serio_driver *drv)
821 u32 caps = CEC_CAP_DEFAULTS | CEC_CAP_PHYS_ADDR | CEC_CAP_MONITOR_ALL;
822 struct pulse8 *pulse8;
823 int err = -ENOMEM;
824 struct cec_log_addrs log_addrs = {};
825 u16 pa = CEC_PHYS_ADDR_INVALID;
827 pulse8 = kzalloc(sizeof(*pulse8), GFP_KERNEL);
829 if (!pulse8)
830 return -ENOMEM;
832 pulse8->serio = serio;
833 pulse8->adap = cec_allocate_adapter(&pulse8_cec_adap_ops, pulse8,
834 dev_name(&serio->dev), caps, 1);
835 err = PTR_ERR_OR_ZERO(pulse8->adap);
836 if (err < 0)
837 goto free_device;
839 pulse8->dev = &serio->dev;
840 serio_set_drvdata(serio, pulse8);
841 INIT_WORK(&pulse8->irq_work, pulse8_irq_work_handler);
842 INIT_WORK(&pulse8->tx_work, pulse8_tx_work_handler);
843 mutex_init(&pulse8->lock);
844 spin_lock_init(&pulse8->msg_lock);
845 pulse8->config_pending = false;
847 err = serio_open(serio, drv);
848 if (err)
849 goto delete_adap;
851 err = pulse8_setup(pulse8, serio, &log_addrs, &pa);
852 if (err)
853 goto close_serio;
855 err = cec_register_adapter(pulse8->adap, &serio->dev);
856 if (err < 0)
857 goto close_serio;
859 pulse8->dev = &pulse8->adap->devnode.dev;
861 if (persistent_config && pulse8->autonomous) {
862 err = pulse8_apply_persistent_config(pulse8, &log_addrs, pa);
863 if (err)
864 goto close_serio;
865 pulse8->restoring_config = true;
868 INIT_DELAYED_WORK(&pulse8->ping_eeprom_work,
869 pulse8_ping_eeprom_work_handler);
870 schedule_delayed_work(&pulse8->ping_eeprom_work, PING_PERIOD);
872 return 0;
874 close_serio:
875 serio_close(serio);
876 delete_adap:
877 cec_delete_adapter(pulse8->adap);
878 serio_set_drvdata(serio, NULL);
879 free_device:
880 kfree(pulse8);
881 return err;
884 static const struct serio_device_id pulse8_serio_ids[] = {
886 .type = SERIO_RS232,
887 .proto = SERIO_PULSE8_CEC,
888 .id = SERIO_ANY,
889 .extra = SERIO_ANY,
891 { 0 }
894 MODULE_DEVICE_TABLE(serio, pulse8_serio_ids);
896 static struct serio_driver pulse8_drv = {
897 .driver = {
898 .name = "pulse8-cec",
900 .description = "Pulse Eight HDMI CEC driver",
901 .id_table = pulse8_serio_ids,
902 .interrupt = pulse8_interrupt,
903 .connect = pulse8_connect,
904 .disconnect = pulse8_disconnect,
907 module_serio_driver(pulse8_drv);