1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
44MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
45MODULE_DESCRIPTION("Pulse Eight HDMI CEC driver");
46MODULE_LICENSE("GPL");
47
48static int debug;
49static int persistent_config;
50module_param(debug, int, 0644);
51module_param(persistent_config, int, 0644);
52MODULE_PARM_DESC(debug, "debug level (0-2)");
53MODULE_PARM_DESC(persistent_config, "read config from persistent memory (0-1)");
54
55enum 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,
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,
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,
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,
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,
97 MSGCODE_SET_ACTIVE_SOURCE,
98
99 MSGCODE_FRAME_EOM = 0x80,
100 MSGCODE_FRAME_ACK = 0x40,
101};
102
103static 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
148static 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
169struct 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
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
200 struct mutex lock;
201 bool config_pending;
202 bool restoring_config;
203 bool autonomous;
204};
205
206static 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
228static 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
260static 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
280static 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
326static 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
358static 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
385
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
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
429
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
478static 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
497static 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
608unlock:
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
617static 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
631static 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
641static 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
648static 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
658static 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 struct tm tm;
665 time64_t date;
666
667 pulse8->vers = 0;
668
669 cmd[0] = MSGCODE_FIRMWARE_VERSION;
670 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2);
671 if (err)
672 return err;
673 pulse8->vers = (data[0] << 8) | data[1];
674 dev_info(pulse8->dev, "Firmware version %04x\n", pulse8->vers);
675 if (pulse8->vers < 2) {
676 *pa = CEC_PHYS_ADDR_INVALID;
677 return 0;
678 }
679
680 cmd[0] = MSGCODE_GET_BUILDDATE;
681 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 4);
682 if (err)
683 return err;
684 date = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
685 time64_to_tm(date, 0, &tm);
686 dev_info(pulse8->dev, "Firmware build date %04ld.%02d.%02d %02d:%02d:%02d\n",
687 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
688 tm.tm_hour, tm.tm_min, tm.tm_sec);
689
690 dev_dbg(pulse8->dev, "Persistent config:\n");
691 cmd[0] = MSGCODE_GET_AUTO_ENABLED;
692 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
693 if (err)
694 return err;
695 pulse8->autonomous = data[0];
696 dev_dbg(pulse8->dev, "Autonomous mode: %s",
697 data[0] ? "on" : "off");
698
699 cmd[0] = MSGCODE_GET_DEVICE_TYPE;
700 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
701 if (err)
702 return err;
703 log_addrs->primary_device_type[0] = data[0];
704 dev_dbg(pulse8->dev, "Primary device type: %d\n", data[0]);
705 switch (log_addrs->primary_device_type[0]) {
706 case CEC_OP_PRIM_DEVTYPE_TV:
707 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TV;
708 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_TV;
709 break;
710 case CEC_OP_PRIM_DEVTYPE_RECORD:
711 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_RECORD;
712 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_RECORD;
713 break;
714 case CEC_OP_PRIM_DEVTYPE_TUNER:
715 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TUNER;
716 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_TUNER;
717 break;
718 case CEC_OP_PRIM_DEVTYPE_PLAYBACK:
719 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK;
720 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_PLAYBACK;
721 break;
722 case CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM:
723 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK;
724 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_AUDIOSYSTEM;
725 break;
726 case CEC_OP_PRIM_DEVTYPE_SWITCH:
727 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
728 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
729 break;
730 case CEC_OP_PRIM_DEVTYPE_PROCESSOR:
731 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_SPECIFIC;
732 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
733 break;
734 default:
735 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
736 log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
737 dev_info(pulse8->dev, "Unknown Primary Device Type: %d\n",
738 log_addrs->primary_device_type[0]);
739 break;
740 }
741
742 cmd[0] = MSGCODE_GET_LOGICAL_ADDRESS_MASK;
743 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2);
744 if (err)
745 return err;
746 log_addrs->log_addr_mask = (data[0] << 8) | data[1];
747 dev_dbg(pulse8->dev, "Logical address ACK mask: %x\n",
748 log_addrs->log_addr_mask);
749 if (log_addrs->log_addr_mask)
750 log_addrs->num_log_addrs = 1;
751
752 cmd[0] = MSGCODE_GET_PHYSICAL_ADDRESS;
753 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
754 if (err)
755 return err;
756 *pa = (data[0] << 8) | data[1];
757 dev_dbg(pulse8->dev, "Physical address: %x.%x.%x.%x\n",
758 cec_phys_addr_exp(*pa));
759
760 cmd[0] = MSGCODE_GET_HDMI_VERSION;
761 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
762 if (err)
763 return err;
764 log_addrs->cec_version = data[0];
765 dev_dbg(pulse8->dev, "CEC version: %d\n", log_addrs->cec_version);
766
767 cmd[0] = MSGCODE_GET_OSD_NAME;
768 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 0);
769 if (err)
770 return err;
771 strscpy(log_addrs->osd_name, data, sizeof(log_addrs->osd_name));
772 dev_dbg(pulse8->dev, "OSD name: %s\n", log_addrs->osd_name);
773
774 return 0;
775}
776
777static int pulse8_apply_persistent_config(struct pulse8 *pulse8,
778 struct cec_log_addrs *log_addrs,
779 u16 pa)
780{
781 int err;
782
783 err = cec_s_log_addrs(pulse8->adap, log_addrs, false);
784 if (err)
785 return err;
786
787 cec_s_phys_addr(pulse8->adap, pa, false);
788
789 return 0;
790}
791
792static void pulse8_ping_eeprom_work_handler(struct work_struct *work)
793{
794 struct pulse8 *pulse8 =
795 container_of(work, struct pulse8, ping_eeprom_work.work);
796 u8 cmd;
797
798 mutex_lock(&pulse8->lock);
799 cmd = MSGCODE_PING;
800 pulse8_send_and_wait(pulse8, &cmd, 1,
801 MSGCODE_COMMAND_ACCEPTED, 0);
802
803 if (pulse8->vers < 2)
804 goto unlock;
805
806 if (pulse8->config_pending && persistent_config) {
807 dev_dbg(pulse8->dev, "writing pending config to EEPROM\n");
808 cmd = MSGCODE_WRITE_EEPROM;
809 if (pulse8_send_and_wait(pulse8, &cmd, 1,
810 MSGCODE_COMMAND_ACCEPTED, 0))
811 dev_info(pulse8->dev, "failed to write pending config to EEPROM\n");
812 else
813 pulse8->config_pending = false;
814 }
815unlock:
816 schedule_delayed_work(&pulse8->ping_eeprom_work, PING_PERIOD);
817 mutex_unlock(&pulse8->lock);
818}
819
820static int pulse8_connect(struct serio *serio, struct serio_driver *drv)
821{
822 u32 caps = CEC_CAP_DEFAULTS | CEC_CAP_PHYS_ADDR | CEC_CAP_MONITOR_ALL;
823 struct pulse8 *pulse8;
824 int err = -ENOMEM;
825 struct cec_log_addrs log_addrs = {};
826 u16 pa = CEC_PHYS_ADDR_INVALID;
827
828 pulse8 = kzalloc(sizeof(*pulse8), GFP_KERNEL);
829
830 if (!pulse8)
831 return -ENOMEM;
832
833 pulse8->serio = serio;
834 pulse8->adap = cec_allocate_adapter(&pulse8_cec_adap_ops, pulse8,
835 dev_name(&serio->dev), caps, 1);
836 err = PTR_ERR_OR_ZERO(pulse8->adap);
837 if (err < 0)
838 goto free_device;
839
840 pulse8->dev = &serio->dev;
841 serio_set_drvdata(serio, pulse8);
842 INIT_WORK(&pulse8->irq_work, pulse8_irq_work_handler);
843 INIT_WORK(&pulse8->tx_work, pulse8_tx_work_handler);
844 INIT_DELAYED_WORK(&pulse8->ping_eeprom_work,
845 pulse8_ping_eeprom_work_handler);
846 mutex_init(&pulse8->lock);
847 spin_lock_init(&pulse8->msg_lock);
848 pulse8->config_pending = false;
849
850 err = serio_open(serio, drv);
851 if (err)
852 goto delete_adap;
853
854 err = pulse8_setup(pulse8, serio, &log_addrs, &pa);
855 if (err)
856 goto close_serio;
857
858 err = cec_register_adapter(pulse8->adap, &serio->dev);
859 if (err < 0)
860 goto close_serio;
861
862 pulse8->dev = &pulse8->adap->devnode.dev;
863
864 if (persistent_config && pulse8->autonomous) {
865 err = pulse8_apply_persistent_config(pulse8, &log_addrs, pa);
866 if (err)
867 goto close_serio;
868 pulse8->restoring_config = true;
869 }
870
871 schedule_delayed_work(&pulse8->ping_eeprom_work, PING_PERIOD);
872
873 return 0;
874
875close_serio:
876 pulse8->serio = NULL;
877 serio_set_drvdata(serio, NULL);
878 serio_close(serio);
879delete_adap:
880 cec_delete_adapter(pulse8->adap);
881free_device:
882 kfree(pulse8);
883 return err;
884}
885
886static const struct serio_device_id pulse8_serio_ids[] = {
887 {
888 .type = SERIO_RS232,
889 .proto = SERIO_PULSE8_CEC,
890 .id = SERIO_ANY,
891 .extra = SERIO_ANY,
892 },
893 { 0 }
894};
895
896MODULE_DEVICE_TABLE(serio, pulse8_serio_ids);
897
898static struct serio_driver pulse8_drv = {
899 .driver = {
900 .name = "pulse8-cec",
901 },
902 .description = "Pulse Eight HDMI CEC driver",
903 .id_table = pulse8_serio_ids,
904 .interrupt = pulse8_interrupt,
905 .connect = pulse8_connect,
906 .disconnect = pulse8_disconnect,
907};
908
909module_serio_driver(pulse8_drv);
910