1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Pulse Eight HDMI CEC driver
4  *
5  * Copyright 2016 Hans Verkuil <hverkuil@xs4all.nl
6  */
7 
8 /*
9  * Notes:
10  *
11  * - Devices with firmware version < 2 do not store their configuration in
12  *   EEPROM.
13  *
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.
17  *
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.
24  *
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.
29  */
30 
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>
41 
42 #include <media/cec.h>
43 
44 MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
45 MODULE_DESCRIPTION("Pulse Eight HDMI CEC driver");
46 MODULE_LICENSE("GPL");
47 
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)");
54 
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,
98 
99 	MSGCODE_FRAME_EOM = 0x80,
100 	MSGCODE_FRAME_ACK = 0x40,
101 };
102 
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",
146 };
147 
148 static const char *pulse8_msgname(u8 cmd)
149 {
150 	static char unknown_msg[5];
151 
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;
156 }
157 
158 #define MSGSTART	0xff
159 #define MSGEND		0xfe
160 #define MSGESC		0xfd
161 #define MSGOFFSET	3
162 
163 #define DATA_SIZE 256
164 
165 #define PING_PERIOD	(15 * HZ)
166 
167 #define NUM_MSGS 8
168 
169 struct pulse8 {
170 	struct device *dev;
171 	struct serio *serio;
172 	struct cec_adapter *adap;
173 	unsigned int vers;
174 
175 	struct delayed_work ping_eeprom_work;
176 
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;
184 
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;
190 
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;
198 
199 	/* locks access to the adapter */
200 	struct mutex lock;
201 	bool config_pending;
202 	bool restoring_config;
203 	bool autonomous;
204 };
205 
206 static int pulse8_send(struct serio *serio, const u8 *command, u8 cmd_len)
207 {
208 	int err = 0;
209 
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);
220 		}
221 	}
222 	if (!err)
223 		err = serio_write(serio, MSGEND);
224 
225 	return err;
226 }
227 
228 static int pulse8_send_and_wait_once(struct pulse8 *pulse8,
229 				     const u8 *cmd, u8 cmd_len,
230 				     u8 response, u8 size)
231 {
232 	int err;
233 
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);
238 
239 	err = pulse8_send(pulse8->serio, cmd, cmd_len);
240 	if (err)
241 		return err;
242 
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;
256 	}
257 	return 0;
258 }
259 
260 static int pulse8_send_and_wait(struct pulse8 *pulse8,
261 				const u8 *cmd, u8 cmd_len, u8 response, u8 size)
262 {
263 	u8 cmd_sc[2];
264 	int err;
265 
266 	err = pulse8_send_and_wait_once(pulse8, cmd, cmd_len, response, size);
267 	if (err != -ENOTTY)
268 		return err;
269 
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;
278 }
279 
280 static void pulse8_tx_work_handler(struct work_struct *work)
281 {
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;
287 
288 	if (msg->len == 0)
289 		return;
290 
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);
314 		}
315 	}
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);
324 }
325 
326 static void pulse8_irq_work_handler(struct work_struct *work)
327 {
328 	struct pulse8 *pulse8 =
329 		container_of(work, struct pulse8, irq_work);
330 	unsigned long flags;
331 	u32 status;
332 
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;
347 	}
348 	spin_unlock_irqrestore(&pulse8->msg_lock, flags);
349 
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);
356 }
357 
358 static irqreturn_t pulse8_interrupt(struct serio *serio, unsigned char data,
359 				    unsigned int flags)
360 {
361 	struct pulse8 *pulse8 = serio_get_drvdata(serio);
362 	unsigned long irq_flags;
363 	unsigned int idx;
364 
365 	if (!pulse8->started && data != MSGSTART)
366 		return IRQ_HANDLED;
367 	if (data == MSGESC) {
368 		pulse8->escape = true;
369 		return IRQ_HANDLED;
370 	}
371 	if (pulse8->escape) {
372 		data += MSGOFFSET;
373 		pulse8->escape = false;
374 	} else if (data == MSGEND) {
375 		u8 msgcode = pulse8->buf[0];
376 
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:
383 			/*
384 			 * Test if we are receiving a new msg when a previous
385 			 * message is still pending.
386 			 */
387 			if (!(msgcode & MSGCODE_FRAME_EOM)) {
388 				pulse8->new_rx_msg_len = 1;
389 				pulse8->new_rx_msg[0] = pulse8->buf[1];
390 				break;
391 			}
392 			fallthrough;
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;
399 
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;
412 			}
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:
427 			/*
428 			 * A NACK for a broadcast message makes no sense, these
429 			 * seem to be spurious messages and are skipped.
430 			 */
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;
459 		}
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;
467 	}
468 
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;
473 	}
474 	pulse8->buf[pulse8->idx++] = data;
475 	return IRQ_HANDLED;
476 }
477 
478 static int pulse8_cec_adap_enable(struct cec_adapter *adap, bool enable)
479 {
480 	struct pulse8 *pulse8 = cec_get_drvdata(adap);
481 	u8 cmd[16];
482 	int err;
483 
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;
492 	}
493 	mutex_unlock(&pulse8->lock);
494 	return enable ? err : 0;
495 }
496 
497 static int pulse8_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
498 {
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;
504 
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;
515 
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;
525 
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;
532 
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;
558 	}
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;
566 
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;
573 
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;
581 
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;
588 
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;
592 
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));
601 		}
602 		err = pulse8_send_and_wait(pulse8, cmd, 1 + osd_len,
603 					   MSGCODE_COMMAND_ACCEPTED, 0);
604 		if (err)
605 			goto unlock;
606 	}
607 
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;
615 }
616 
617 static int pulse8_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
618 				    u32 signal_free_time, struct cec_msg *msg)
619 {
620 	struct pulse8 *pulse8 = cec_get_drvdata(adap);
621 
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;
629 }
630 
631 static void pulse8_cec_adap_free(struct cec_adapter *adap)
632 {
633 	struct pulse8 *pulse8 = cec_get_drvdata(adap);
634 
635 	cancel_delayed_work_sync(&pulse8->ping_eeprom_work);
636 	cancel_work_sync(&pulse8->irq_work);
637 	cancel_work_sync(&pulse8->tx_work);
638 	kfree(pulse8);
639 }
640 
641 static const struct cec_adap_ops pulse8_cec_adap_ops = {
642 	.adap_enable = pulse8_cec_adap_enable,
643 	.adap_log_addr = pulse8_cec_adap_log_addr,
644 	.adap_transmit = pulse8_cec_adap_transmit,
645 	.adap_free = pulse8_cec_adap_free,
646 };
647 
648 static void pulse8_disconnect(struct serio *serio)
649 {
650 	struct pulse8 *pulse8 = serio_get_drvdata(serio);
651 
652 	cec_unregister_adapter(pulse8->adap);
653 	pulse8->serio = NULL;
654 	serio_set_drvdata(serio, NULL);
655 	serio_close(serio);
656 }
657 
658 static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio,
659 			struct cec_log_addrs *log_addrs, u16 *pa)
660 {
661 	u8 *data = pulse8->data + 1;
662 	u8 cmd[2];
663 	int err;
664 	time64_t date;
665 
666 	pulse8->vers = 0;
667 
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;
677 	}
678 
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 	dev_info(pulse8->dev, "Firmware build date %ptT\n", &date);
685 
686 	dev_dbg(pulse8->dev, "Persistent config:\n");
687 	cmd[0] = MSGCODE_GET_AUTO_ENABLED;
688 	err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
689 	if (err)
690 		return err;
691 	pulse8->autonomous = data[0];
692 	dev_dbg(pulse8->dev, "Autonomous mode: %s",
693 		data[0] ? "on" : "off");
694 
695 	cmd[0] = MSGCODE_GET_DEVICE_TYPE;
696 	err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
697 	if (err)
698 		return err;
699 	log_addrs->primary_device_type[0] = data[0];
700 	dev_dbg(pulse8->dev, "Primary device type: %d\n", data[0]);
701 	switch (log_addrs->primary_device_type[0]) {
702 	case CEC_OP_PRIM_DEVTYPE_TV:
703 		log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TV;
704 		log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_TV;
705 		break;
706 	case CEC_OP_PRIM_DEVTYPE_RECORD:
707 		log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_RECORD;
708 		log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_RECORD;
709 		break;
710 	case CEC_OP_PRIM_DEVTYPE_TUNER:
711 		log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TUNER;
712 		log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_TUNER;
713 		break;
714 	case CEC_OP_PRIM_DEVTYPE_PLAYBACK:
715 		log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK;
716 		log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_PLAYBACK;
717 		break;
718 	case CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM:
719 		log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK;
720 		log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_AUDIOSYSTEM;
721 		break;
722 	case CEC_OP_PRIM_DEVTYPE_SWITCH:
723 		log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
724 		log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
725 		break;
726 	case CEC_OP_PRIM_DEVTYPE_PROCESSOR:
727 		log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_SPECIFIC;
728 		log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
729 		break;
730 	default:
731 		log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
732 		log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
733 		dev_info(pulse8->dev, "Unknown Primary Device Type: %d\n",
734 			 log_addrs->primary_device_type[0]);
735 		break;
736 	}
737 
738 	cmd[0] = MSGCODE_GET_LOGICAL_ADDRESS_MASK;
739 	err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2);
740 	if (err)
741 		return err;
742 	log_addrs->log_addr_mask = (data[0] << 8) | data[1];
743 	dev_dbg(pulse8->dev, "Logical address ACK mask: %x\n",
744 		log_addrs->log_addr_mask);
745 	if (log_addrs->log_addr_mask)
746 		log_addrs->num_log_addrs = 1;
747 
748 	cmd[0] = MSGCODE_GET_PHYSICAL_ADDRESS;
749 	err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
750 	if (err)
751 		return err;
752 	*pa = (data[0] << 8) | data[1];
753 	dev_dbg(pulse8->dev, "Physical address: %x.%x.%x.%x\n",
754 		cec_phys_addr_exp(*pa));
755 
756 	cmd[0] = MSGCODE_GET_HDMI_VERSION;
757 	err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
758 	if (err)
759 		return err;
760 	log_addrs->cec_version = data[0];
761 	dev_dbg(pulse8->dev, "CEC version: %d\n", log_addrs->cec_version);
762 
763 	cmd[0] = MSGCODE_GET_OSD_NAME;
764 	err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 0);
765 	if (err)
766 		return err;
767 	strscpy(log_addrs->osd_name, data, sizeof(log_addrs->osd_name));
768 	dev_dbg(pulse8->dev, "OSD name: %s\n", log_addrs->osd_name);
769 
770 	return 0;
771 }
772 
773 static int pulse8_apply_persistent_config(struct pulse8 *pulse8,
774 					  struct cec_log_addrs *log_addrs,
775 					  u16 pa)
776 {
777 	int err;
778 
779 	err = cec_s_log_addrs(pulse8->adap, log_addrs, false);
780 	if (err)
781 		return err;
782 
783 	cec_s_phys_addr(pulse8->adap, pa, false);
784 
785 	return 0;
786 }
787 
788 static void pulse8_ping_eeprom_work_handler(struct work_struct *work)
789 {
790 	struct pulse8 *pulse8 =
791 		container_of(work, struct pulse8, ping_eeprom_work.work);
792 	u8 cmd;
793 
794 	mutex_lock(&pulse8->lock);
795 	cmd = MSGCODE_PING;
796 	pulse8_send_and_wait(pulse8, &cmd, 1,
797 			     MSGCODE_COMMAND_ACCEPTED, 0);
798 
799 	if (pulse8->vers < 2)
800 		goto unlock;
801 
802 	if (pulse8->config_pending && persistent_config) {
803 		dev_dbg(pulse8->dev, "writing pending config to EEPROM\n");
804 		cmd = MSGCODE_WRITE_EEPROM;
805 		if (pulse8_send_and_wait(pulse8, &cmd, 1,
806 					 MSGCODE_COMMAND_ACCEPTED, 0))
807 			dev_info(pulse8->dev, "failed to write pending config to EEPROM\n");
808 		else
809 			pulse8->config_pending = false;
810 	}
811 unlock:
812 	schedule_delayed_work(&pulse8->ping_eeprom_work, PING_PERIOD);
813 	mutex_unlock(&pulse8->lock);
814 }
815 
816 static int pulse8_connect(struct serio *serio, struct serio_driver *drv)
817 {
818 	u32 caps = CEC_CAP_DEFAULTS | CEC_CAP_PHYS_ADDR | CEC_CAP_MONITOR_ALL;
819 	struct pulse8 *pulse8;
820 	int err = -ENOMEM;
821 	struct cec_log_addrs log_addrs = {};
822 	u16 pa = CEC_PHYS_ADDR_INVALID;
823 
824 	pulse8 = kzalloc(sizeof(*pulse8), GFP_KERNEL);
825 
826 	if (!pulse8)
827 		return -ENOMEM;
828 
829 	pulse8->serio = serio;
830 	pulse8->adap = cec_allocate_adapter(&pulse8_cec_adap_ops, pulse8,
831 					    dev_name(&serio->dev), caps, 1);
832 	err = PTR_ERR_OR_ZERO(pulse8->adap);
833 	if (err < 0)
834 		goto free_device;
835 
836 	pulse8->dev = &serio->dev;
837 	serio_set_drvdata(serio, pulse8);
838 	INIT_WORK(&pulse8->irq_work, pulse8_irq_work_handler);
839 	INIT_WORK(&pulse8->tx_work, pulse8_tx_work_handler);
840 	INIT_DELAYED_WORK(&pulse8->ping_eeprom_work,
841 			  pulse8_ping_eeprom_work_handler);
842 	mutex_init(&pulse8->lock);
843 	spin_lock_init(&pulse8->msg_lock);
844 	pulse8->config_pending = false;
845 
846 	err = serio_open(serio, drv);
847 	if (err)
848 		goto delete_adap;
849 
850 	err = pulse8_setup(pulse8, serio, &log_addrs, &pa);
851 	if (err)
852 		goto close_serio;
853 
854 	err = cec_register_adapter(pulse8->adap, &serio->dev);
855 	if (err < 0)
856 		goto close_serio;
857 
858 	pulse8->dev = &pulse8->adap->devnode.dev;
859 
860 	if (persistent_config && pulse8->autonomous) {
861 		err = pulse8_apply_persistent_config(pulse8, &log_addrs, pa);
862 		if (err)
863 			goto close_serio;
864 		pulse8->restoring_config = true;
865 	}
866 
867 	schedule_delayed_work(&pulse8->ping_eeprom_work, PING_PERIOD);
868 
869 	return 0;
870 
871 close_serio:
872 	pulse8->serio = NULL;
873 	serio_set_drvdata(serio, NULL);
874 	serio_close(serio);
875 delete_adap:
876 	cec_delete_adapter(pulse8->adap);
877 free_device:
878 	kfree(pulse8);
879 	return err;
880 }
881 
882 static const struct serio_device_id pulse8_serio_ids[] = {
883 	{
884 		.type	= SERIO_RS232,
885 		.proto	= SERIO_PULSE8_CEC,
886 		.id	= SERIO_ANY,
887 		.extra	= SERIO_ANY,
888 	},
889 	{ 0 }
890 };
891 
892 MODULE_DEVICE_TABLE(serio, pulse8_serio_ids);
893 
894 static struct serio_driver pulse8_drv = {
895 	.driver		= {
896 		.name	= "pulse8-cec",
897 	},
898 	.description	= "Pulse Eight HDMI CEC driver",
899 	.id_table	= pulse8_serio_ids,
900 	.interrupt	= pulse8_interrupt,
901 	.connect	= pulse8_connect,
902 	.disconnect	= pulse8_disconnect,
903 };
904 
905 module_serio_driver(pulse8_drv);
906