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
32
33
34
35
36#include <linux/completion.h>
37#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <linux/kernel.h>
40#include <linux/module.h>
41#include <linux/workqueue.h>
42#include <linux/serio.h>
43#include <linux/slab.h>
44#include <linux/time.h>
45#include <linux/delay.h>
46
47#include <media/cec.h>
48
49MODULE_AUTHOR("Hans Verkuil <hverkuil@xs4all.nl>");
50MODULE_DESCRIPTION("Pulse Eight HDMI CEC driver");
51MODULE_LICENSE("GPL");
52
53static int debug;
54static int persistent_config = 1;
55module_param(debug, int, 0644);
56module_param(persistent_config, int, 0644);
57MODULE_PARM_DESC(debug, "debug level (0-1)");
58MODULE_PARM_DESC(persistent_config, "read config from persistent memory (0-1)");
59
60enum pulse8_msgcodes {
61 MSGCODE_NOTHING = 0,
62 MSGCODE_PING,
63 MSGCODE_TIMEOUT_ERROR,
64 MSGCODE_HIGH_ERROR,
65 MSGCODE_LOW_ERROR,
66 MSGCODE_FRAME_START,
67 MSGCODE_FRAME_DATA,
68 MSGCODE_RECEIVE_FAILED,
69 MSGCODE_COMMAND_ACCEPTED,
70 MSGCODE_COMMAND_REJECTED,
71 MSGCODE_SET_ACK_MASK,
72 MSGCODE_TRANSMIT,
73 MSGCODE_TRANSMIT_EOM,
74 MSGCODE_TRANSMIT_IDLETIME,
75 MSGCODE_TRANSMIT_ACK_POLARITY,
76 MSGCODE_TRANSMIT_LINE_TIMEOUT,
77 MSGCODE_TRANSMIT_SUCCEEDED,
78 MSGCODE_TRANSMIT_FAILED_LINE,
79 MSGCODE_TRANSMIT_FAILED_ACK,
80 MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA,
81 MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE,
82 MSGCODE_FIRMWARE_VERSION,
83 MSGCODE_START_BOOTLOADER,
84 MSGCODE_GET_BUILDDATE,
85 MSGCODE_SET_CONTROLLED,
86 MSGCODE_GET_AUTO_ENABLED,
87 MSGCODE_SET_AUTO_ENABLED,
88 MSGCODE_GET_DEFAULT_LOGICAL_ADDRESS,
89 MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS,
90 MSGCODE_GET_LOGICAL_ADDRESS_MASK,
91 MSGCODE_SET_LOGICAL_ADDRESS_MASK,
92 MSGCODE_GET_PHYSICAL_ADDRESS,
93 MSGCODE_SET_PHYSICAL_ADDRESS,
94 MSGCODE_GET_DEVICE_TYPE,
95 MSGCODE_SET_DEVICE_TYPE,
96 MSGCODE_GET_HDMI_VERSION,
97 MSGCODE_SET_HDMI_VERSION,
98 MSGCODE_GET_OSD_NAME,
99 MSGCODE_SET_OSD_NAME,
100 MSGCODE_WRITE_EEPROM,
101 MSGCODE_GET_ADAPTER_TYPE,
102 MSGCODE_SET_ACTIVE_SOURCE,
103
104 MSGCODE_FRAME_EOM = 0x80,
105 MSGCODE_FRAME_ACK = 0x40,
106};
107
108#define MSGSTART 0xff
109#define MSGEND 0xfe
110#define MSGESC 0xfd
111#define MSGOFFSET 3
112
113#define DATA_SIZE 256
114
115#define PING_PERIOD (15 * HZ)
116
117struct pulse8 {
118 struct device *dev;
119 struct serio *serio;
120 struct cec_adapter *adap;
121 unsigned int vers;
122 struct completion cmd_done;
123 struct work_struct work;
124 struct delayed_work ping_eeprom_work;
125 struct cec_msg rx_msg;
126 u8 data[DATA_SIZE];
127 unsigned int len;
128 u8 buf[DATA_SIZE];
129 unsigned int idx;
130 bool escape;
131 bool started;
132 struct mutex config_lock;
133 struct mutex write_lock;
134 bool config_pending;
135 bool restoring_config;
136 bool autonomous;
137};
138
139static void pulse8_ping_eeprom_work_handler(struct work_struct *work);
140
141static void pulse8_irq_work_handler(struct work_struct *work)
142{
143 struct pulse8 *pulse8 =
144 container_of(work, struct pulse8, work);
145
146 switch (pulse8->data[0] & 0x3f) {
147 case MSGCODE_FRAME_DATA:
148 cec_received_msg(pulse8->adap, &pulse8->rx_msg);
149 break;
150 case MSGCODE_TRANSMIT_SUCCEEDED:
151 cec_transmit_done(pulse8->adap, CEC_TX_STATUS_OK,
152 0, 0, 0, 0);
153 break;
154 case MSGCODE_TRANSMIT_FAILED_ACK:
155 cec_transmit_done(pulse8->adap, CEC_TX_STATUS_NACK,
156 0, 1, 0, 0);
157 break;
158 case MSGCODE_TRANSMIT_FAILED_LINE:
159 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
160 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
161 cec_transmit_done(pulse8->adap, CEC_TX_STATUS_ERROR,
162 0, 0, 0, 1);
163 break;
164 }
165}
166
167static irqreturn_t pulse8_interrupt(struct serio *serio, unsigned char data,
168 unsigned int flags)
169{
170 struct pulse8 *pulse8 = serio_get_drvdata(serio);
171
172 if (!pulse8->started && data != MSGSTART)
173 return IRQ_HANDLED;
174 if (data == MSGESC) {
175 pulse8->escape = true;
176 return IRQ_HANDLED;
177 }
178 if (pulse8->escape) {
179 data += MSGOFFSET;
180 pulse8->escape = false;
181 } else if (data == MSGEND) {
182 struct cec_msg *msg = &pulse8->rx_msg;
183
184 if (debug)
185 dev_info(pulse8->dev, "received: %*ph\n",
186 pulse8->idx, pulse8->buf);
187 pulse8->data[0] = pulse8->buf[0];
188 switch (pulse8->buf[0] & 0x3f) {
189 case MSGCODE_FRAME_START:
190 msg->len = 1;
191 msg->msg[0] = pulse8->buf[1];
192 break;
193 case MSGCODE_FRAME_DATA:
194 if (msg->len == CEC_MAX_MSG_SIZE)
195 break;
196 msg->msg[msg->len++] = pulse8->buf[1];
197 if (pulse8->buf[0] & MSGCODE_FRAME_EOM)
198 schedule_work(&pulse8->work);
199 break;
200 case MSGCODE_TRANSMIT_SUCCEEDED:
201 case MSGCODE_TRANSMIT_FAILED_LINE:
202 case MSGCODE_TRANSMIT_FAILED_ACK:
203 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
204 case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
205 schedule_work(&pulse8->work);
206 break;
207 case MSGCODE_HIGH_ERROR:
208 case MSGCODE_LOW_ERROR:
209 case MSGCODE_RECEIVE_FAILED:
210 case MSGCODE_TIMEOUT_ERROR:
211 break;
212 case MSGCODE_COMMAND_ACCEPTED:
213 case MSGCODE_COMMAND_REJECTED:
214 default:
215 if (pulse8->idx == 0)
216 break;
217 memcpy(pulse8->data, pulse8->buf, pulse8->idx);
218 pulse8->len = pulse8->idx;
219 complete(&pulse8->cmd_done);
220 break;
221 }
222 pulse8->idx = 0;
223 pulse8->started = false;
224 return IRQ_HANDLED;
225 } else if (data == MSGSTART) {
226 pulse8->idx = 0;
227 pulse8->started = true;
228 return IRQ_HANDLED;
229 }
230
231 if (pulse8->idx >= DATA_SIZE) {
232 dev_dbg(pulse8->dev,
233 "throwing away %d bytes of garbage\n", pulse8->idx);
234 pulse8->idx = 0;
235 }
236 pulse8->buf[pulse8->idx++] = data;
237 return IRQ_HANDLED;
238}
239
240static void pulse8_disconnect(struct serio *serio)
241{
242 struct pulse8 *pulse8 = serio_get_drvdata(serio);
243
244 cec_unregister_adapter(pulse8->adap);
245 cancel_delayed_work_sync(&pulse8->ping_eeprom_work);
246 dev_info(&serio->dev, "disconnected\n");
247 serio_close(serio);
248 serio_set_drvdata(serio, NULL);
249 kfree(pulse8);
250}
251
252static int pulse8_send(struct serio *serio, const u8 *command, u8 cmd_len)
253{
254 int err = 0;
255
256 err = serio_write(serio, MSGSTART);
257 if (err)
258 return err;
259 for (; !err && cmd_len; command++, cmd_len--) {
260 if (*command >= MSGESC) {
261 err = serio_write(serio, MSGESC);
262 if (!err)
263 err = serio_write(serio, *command - MSGOFFSET);
264 } else {
265 err = serio_write(serio, *command);
266 }
267 }
268 if (!err)
269 err = serio_write(serio, MSGEND);
270
271 return err;
272}
273
274static int pulse8_send_and_wait_once(struct pulse8 *pulse8,
275 const u8 *cmd, u8 cmd_len,
276 u8 response, u8 size)
277{
278 int err;
279
280
281 init_completion(&pulse8->cmd_done);
282
283 err = pulse8_send(pulse8->serio, cmd, cmd_len);
284 if (err)
285 return err;
286
287 if (!wait_for_completion_timeout(&pulse8->cmd_done, HZ))
288 return -ETIMEDOUT;
289 if ((pulse8->data[0] & 0x3f) == MSGCODE_COMMAND_REJECTED &&
290 cmd[0] != MSGCODE_SET_CONTROLLED &&
291 cmd[0] != MSGCODE_SET_AUTO_ENABLED &&
292 cmd[0] != MSGCODE_GET_BUILDDATE)
293 return -ENOTTY;
294 if (response &&
295 ((pulse8->data[0] & 0x3f) != response || pulse8->len < size + 1)) {
296 dev_info(pulse8->dev, "transmit: failed %02x\n",
297 pulse8->data[0] & 0x3f);
298 return -EIO;
299 }
300 return 0;
301}
302
303static int pulse8_send_and_wait(struct pulse8 *pulse8,
304 const u8 *cmd, u8 cmd_len, u8 response, u8 size)
305{
306 u8 cmd_sc[2];
307 int err;
308
309 mutex_lock(&pulse8->write_lock);
310 err = pulse8_send_and_wait_once(pulse8, cmd, cmd_len, response, size);
311
312 if (err == -ENOTTY) {
313 cmd_sc[0] = MSGCODE_SET_CONTROLLED;
314 cmd_sc[1] = 1;
315 err = pulse8_send_and_wait_once(pulse8, cmd_sc, 2,
316 MSGCODE_COMMAND_ACCEPTED, 1);
317 if (err)
318 goto unlock;
319 err = pulse8_send_and_wait_once(pulse8, cmd, cmd_len,
320 response, size);
321 }
322
323unlock:
324 mutex_unlock(&pulse8->write_lock);
325 return err == -ENOTTY ? -EIO : err;
326}
327
328static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio,
329 struct cec_log_addrs *log_addrs, u16 *pa)
330{
331 u8 *data = pulse8->data + 1;
332 u8 cmd[2];
333 int err;
334 struct tm tm;
335 time_t date;
336
337 pulse8->vers = 0;
338
339 cmd[0] = MSGCODE_FIRMWARE_VERSION;
340 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2);
341 if (err)
342 return err;
343 pulse8->vers = (data[0] << 8) | data[1];
344 dev_info(pulse8->dev, "Firmware version %04x\n", pulse8->vers);
345 if (pulse8->vers < 2) {
346 *pa = CEC_PHYS_ADDR_INVALID;
347 return 0;
348 }
349
350 cmd[0] = MSGCODE_GET_BUILDDATE;
351 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 4);
352 if (err)
353 return err;
354 date = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
355 time_to_tm(date, 0, &tm);
356 dev_info(pulse8->dev, "Firmware build date %04ld.%02d.%02d %02d:%02d:%02d\n",
357 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
358 tm.tm_hour, tm.tm_min, tm.tm_sec);
359
360 dev_dbg(pulse8->dev, "Persistent config:\n");
361 cmd[0] = MSGCODE_GET_AUTO_ENABLED;
362 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
363 if (err)
364 return err;
365 pulse8->autonomous = data[0];
366 dev_dbg(pulse8->dev, "Autonomous mode: %s",
367 data[0] ? "on" : "off");
368
369 cmd[0] = MSGCODE_GET_DEVICE_TYPE;
370 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
371 if (err)
372 return err;
373 log_addrs->primary_device_type[0] = data[0];
374 dev_dbg(pulse8->dev, "Primary device type: %d\n", data[0]);
375 switch (log_addrs->primary_device_type[0]) {
376 case CEC_OP_PRIM_DEVTYPE_TV:
377 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TV;
378 break;
379 case CEC_OP_PRIM_DEVTYPE_RECORD:
380 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_RECORD;
381 break;
382 case CEC_OP_PRIM_DEVTYPE_TUNER:
383 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TUNER;
384 break;
385 case CEC_OP_PRIM_DEVTYPE_PLAYBACK:
386 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK;
387 break;
388 case CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM:
389 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK;
390 break;
391 case CEC_OP_PRIM_DEVTYPE_SWITCH:
392 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
393 break;
394 case CEC_OP_PRIM_DEVTYPE_PROCESSOR:
395 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_SPECIFIC;
396 break;
397 default:
398 log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
399 dev_info(pulse8->dev, "Unknown Primary Device Type: %d\n",
400 log_addrs->primary_device_type[0]);
401 break;
402 }
403
404 cmd[0] = MSGCODE_GET_LOGICAL_ADDRESS_MASK;
405 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2);
406 if (err)
407 return err;
408 log_addrs->log_addr_mask = (data[0] << 8) | data[1];
409 dev_dbg(pulse8->dev, "Logical address ACK mask: %x\n",
410 log_addrs->log_addr_mask);
411 if (log_addrs->log_addr_mask)
412 log_addrs->num_log_addrs = 1;
413
414 cmd[0] = MSGCODE_GET_PHYSICAL_ADDRESS;
415 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
416 if (err)
417 return err;
418 *pa = (data[0] << 8) | data[1];
419 dev_dbg(pulse8->dev, "Physical address: %x.%x.%x.%x\n",
420 cec_phys_addr_exp(*pa));
421
422 cmd[0] = MSGCODE_GET_HDMI_VERSION;
423 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
424 if (err)
425 return err;
426 log_addrs->cec_version = data[0];
427 dev_dbg(pulse8->dev, "CEC version: %d\n", log_addrs->cec_version);
428
429 cmd[0] = MSGCODE_GET_OSD_NAME;
430 err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 0);
431 if (err)
432 return err;
433 strncpy(log_addrs->osd_name, data, 13);
434 dev_dbg(pulse8->dev, "OSD name: %s\n", log_addrs->osd_name);
435
436 return 0;
437}
438
439static int pulse8_apply_persistent_config(struct pulse8 *pulse8,
440 struct cec_log_addrs *log_addrs,
441 u16 pa)
442{
443 int err;
444
445 err = cec_s_log_addrs(pulse8->adap, log_addrs, false);
446 if (err)
447 return err;
448
449 cec_s_phys_addr(pulse8->adap, pa, false);
450
451 return 0;
452}
453
454static int pulse8_cec_adap_enable(struct cec_adapter *adap, bool enable)
455{
456 struct pulse8 *pulse8 = adap->priv;
457 u8 cmd[16];
458 int err;
459
460 cmd[0] = MSGCODE_SET_CONTROLLED;
461 cmd[1] = enable;
462 err = pulse8_send_and_wait(pulse8, cmd, 2,
463 MSGCODE_COMMAND_ACCEPTED, 1);
464 return enable ? err : 0;
465}
466
467static int pulse8_cec_adap_log_addr(struct cec_adapter *adap, u8 log_addr)
468{
469 struct pulse8 *pulse8 = adap->priv;
470 u16 mask = 0;
471 u16 pa = adap->phys_addr;
472 u8 cmd[16];
473 int err = 0;
474
475 mutex_lock(&pulse8->config_lock);
476 if (log_addr != CEC_LOG_ADDR_INVALID)
477 mask = 1 << log_addr;
478 cmd[0] = MSGCODE_SET_ACK_MASK;
479 cmd[1] = mask >> 8;
480 cmd[2] = mask & 0xff;
481 err = pulse8_send_and_wait(pulse8, cmd, 3,
482 MSGCODE_COMMAND_ACCEPTED, 0);
483 if ((err && mask != 0) || pulse8->restoring_config)
484 goto unlock;
485
486 cmd[0] = MSGCODE_SET_AUTO_ENABLED;
487 cmd[1] = log_addr == CEC_LOG_ADDR_INVALID ? 0 : 1;
488 err = pulse8_send_and_wait(pulse8, cmd, 2,
489 MSGCODE_COMMAND_ACCEPTED, 0);
490 if (err)
491 goto unlock;
492 pulse8->autonomous = cmd[1];
493 if (log_addr == CEC_LOG_ADDR_INVALID)
494 goto unlock;
495
496 cmd[0] = MSGCODE_SET_DEVICE_TYPE;
497 cmd[1] = adap->log_addrs.primary_device_type[0];
498 err = pulse8_send_and_wait(pulse8, cmd, 2,
499 MSGCODE_COMMAND_ACCEPTED, 0);
500 if (err)
501 goto unlock;
502
503 switch (adap->log_addrs.primary_device_type[0]) {
504 case CEC_OP_PRIM_DEVTYPE_TV:
505 mask = CEC_LOG_ADDR_MASK_TV;
506 break;
507 case CEC_OP_PRIM_DEVTYPE_RECORD:
508 mask = CEC_LOG_ADDR_MASK_RECORD;
509 break;
510 case CEC_OP_PRIM_DEVTYPE_TUNER:
511 mask = CEC_LOG_ADDR_MASK_TUNER;
512 break;
513 case CEC_OP_PRIM_DEVTYPE_PLAYBACK:
514 mask = CEC_LOG_ADDR_MASK_PLAYBACK;
515 break;
516 case CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM:
517 mask = CEC_LOG_ADDR_MASK_AUDIOSYSTEM;
518 break;
519 case CEC_OP_PRIM_DEVTYPE_SWITCH:
520 mask = CEC_LOG_ADDR_MASK_UNREGISTERED;
521 break;
522 case CEC_OP_PRIM_DEVTYPE_PROCESSOR:
523 mask = CEC_LOG_ADDR_MASK_SPECIFIC;
524 break;
525 default:
526 mask = 0;
527 break;
528 }
529 cmd[0] = MSGCODE_SET_LOGICAL_ADDRESS_MASK;
530 cmd[1] = mask >> 8;
531 cmd[2] = mask & 0xff;
532 err = pulse8_send_and_wait(pulse8, cmd, 3,
533 MSGCODE_COMMAND_ACCEPTED, 0);
534 if (err)
535 goto unlock;
536
537 cmd[0] = MSGCODE_SET_DEFAULT_LOGICAL_ADDRESS;
538 cmd[1] = log_addr;
539 err = pulse8_send_and_wait(pulse8, cmd, 2,
540 MSGCODE_COMMAND_ACCEPTED, 0);
541 if (err)
542 goto unlock;
543
544 cmd[0] = MSGCODE_SET_PHYSICAL_ADDRESS;
545 cmd[1] = pa >> 8;
546 cmd[2] = pa & 0xff;
547 err = pulse8_send_and_wait(pulse8, cmd, 3,
548 MSGCODE_COMMAND_ACCEPTED, 0);
549 if (err)
550 goto unlock;
551
552 cmd[0] = MSGCODE_SET_HDMI_VERSION;
553 cmd[1] = adap->log_addrs.cec_version;
554 err = pulse8_send_and_wait(pulse8, cmd, 2,
555 MSGCODE_COMMAND_ACCEPTED, 0);
556 if (err)
557 goto unlock;
558
559 if (adap->log_addrs.osd_name[0]) {
560 size_t osd_len = strlen(adap->log_addrs.osd_name);
561 char *osd_str = cmd + 1;
562
563 cmd[0] = MSGCODE_SET_OSD_NAME;
564 strncpy(cmd + 1, adap->log_addrs.osd_name, 13);
565 if (osd_len < 4) {
566 memset(osd_str + osd_len, ' ', 4 - osd_len);
567 osd_len = 4;
568 osd_str[osd_len] = '\0';
569 strcpy(adap->log_addrs.osd_name, osd_str);
570 }
571 err = pulse8_send_and_wait(pulse8, cmd, 1 + osd_len,
572 MSGCODE_COMMAND_ACCEPTED, 0);
573 if (err)
574 goto unlock;
575 }
576
577unlock:
578 if (pulse8->restoring_config)
579 pulse8->restoring_config = false;
580 else
581 pulse8->config_pending = true;
582 mutex_unlock(&pulse8->config_lock);
583 return err;
584}
585
586static int pulse8_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
587 u32 signal_free_time, struct cec_msg *msg)
588{
589 struct pulse8 *pulse8 = adap->priv;
590 u8 cmd[2];
591 unsigned int i;
592 int err;
593
594 cmd[0] = MSGCODE_TRANSMIT_IDLETIME;
595 cmd[1] = signal_free_time;
596 err = pulse8_send_and_wait(pulse8, cmd, 2,
597 MSGCODE_COMMAND_ACCEPTED, 1);
598 cmd[0] = MSGCODE_TRANSMIT_ACK_POLARITY;
599 cmd[1] = cec_msg_is_broadcast(msg);
600 if (!err)
601 err = pulse8_send_and_wait(pulse8, cmd, 2,
602 MSGCODE_COMMAND_ACCEPTED, 1);
603 cmd[0] = msg->len == 1 ? MSGCODE_TRANSMIT_EOM : MSGCODE_TRANSMIT;
604 cmd[1] = msg->msg[0];
605 if (!err)
606 err = pulse8_send_and_wait(pulse8, cmd, 2,
607 MSGCODE_COMMAND_ACCEPTED, 1);
608 if (!err && msg->len > 1) {
609 cmd[0] = msg->len == 2 ? MSGCODE_TRANSMIT_EOM :
610 MSGCODE_TRANSMIT;
611 cmd[1] = msg->msg[1];
612 err = pulse8_send_and_wait(pulse8, cmd, 2,
613 MSGCODE_COMMAND_ACCEPTED, 1);
614 for (i = 0; !err && i + 2 < msg->len; i++) {
615 cmd[0] = (i + 2 == msg->len - 1) ?
616 MSGCODE_TRANSMIT_EOM : MSGCODE_TRANSMIT;
617 cmd[1] = msg->msg[i + 2];
618 err = pulse8_send_and_wait(pulse8, cmd, 2,
619 MSGCODE_COMMAND_ACCEPTED, 1);
620 }
621 }
622
623 return err;
624}
625
626static int pulse8_received(struct cec_adapter *adap, struct cec_msg *msg)
627{
628 return -ENOMSG;
629}
630
631static const struct cec_adap_ops pulse8_cec_adap_ops = {
632 .adap_enable = pulse8_cec_adap_enable,
633 .adap_log_addr = pulse8_cec_adap_log_addr,
634 .adap_transmit = pulse8_cec_adap_transmit,
635 .received = pulse8_received,
636};
637
638static int pulse8_connect(struct serio *serio, struct serio_driver *drv)
639{
640 u32 caps = CEC_CAP_TRANSMIT | CEC_CAP_LOG_ADDRS | CEC_CAP_PHYS_ADDR |
641 CEC_CAP_PASSTHROUGH | CEC_CAP_RC | CEC_CAP_MONITOR_ALL;
642 struct pulse8 *pulse8;
643 int err = -ENOMEM;
644 struct cec_log_addrs log_addrs = {};
645 u16 pa = CEC_PHYS_ADDR_INVALID;
646
647 pulse8 = kzalloc(sizeof(*pulse8), GFP_KERNEL);
648
649 if (!pulse8)
650 return -ENOMEM;
651
652 pulse8->serio = serio;
653 pulse8->adap = cec_allocate_adapter(&pulse8_cec_adap_ops, pulse8,
654 "HDMI CEC", caps, 1, &serio->dev);
655 err = PTR_ERR_OR_ZERO(pulse8->adap);
656 if (err < 0)
657 goto free_device;
658
659 pulse8->dev = &serio->dev;
660 serio_set_drvdata(serio, pulse8);
661 INIT_WORK(&pulse8->work, pulse8_irq_work_handler);
662 mutex_init(&pulse8->write_lock);
663 mutex_init(&pulse8->config_lock);
664 pulse8->config_pending = false;
665
666 err = serio_open(serio, drv);
667 if (err)
668 goto delete_adap;
669
670 err = pulse8_setup(pulse8, serio, &log_addrs, &pa);
671 if (err)
672 goto close_serio;
673
674 err = cec_register_adapter(pulse8->adap);
675 if (err < 0)
676 goto close_serio;
677
678 pulse8->dev = &pulse8->adap->devnode.dev;
679
680 if (persistent_config && pulse8->autonomous) {
681 err = pulse8_apply_persistent_config(pulse8, &log_addrs, pa);
682 if (err)
683 goto close_serio;
684 pulse8->restoring_config = true;
685 }
686
687 INIT_DELAYED_WORK(&pulse8->ping_eeprom_work,
688 pulse8_ping_eeprom_work_handler);
689 schedule_delayed_work(&pulse8->ping_eeprom_work, PING_PERIOD);
690
691 return 0;
692
693close_serio:
694 serio_close(serio);
695delete_adap:
696 cec_delete_adapter(pulse8->adap);
697 serio_set_drvdata(serio, NULL);
698free_device:
699 kfree(pulse8);
700 return err;
701}
702
703static void pulse8_ping_eeprom_work_handler(struct work_struct *work)
704{
705 struct pulse8 *pulse8 =
706 container_of(work, struct pulse8, ping_eeprom_work.work);
707 u8 cmd;
708
709 schedule_delayed_work(&pulse8->ping_eeprom_work, PING_PERIOD);
710 cmd = MSGCODE_PING;
711 pulse8_send_and_wait(pulse8, &cmd, 1,
712 MSGCODE_COMMAND_ACCEPTED, 0);
713
714 if (pulse8->vers < 2)
715 return;
716
717 mutex_lock(&pulse8->config_lock);
718 if (pulse8->config_pending && persistent_config) {
719 dev_dbg(pulse8->dev, "writing pending config to EEPROM\n");
720 cmd = MSGCODE_WRITE_EEPROM;
721 if (pulse8_send_and_wait(pulse8, &cmd, 1,
722 MSGCODE_COMMAND_ACCEPTED, 0))
723 dev_info(pulse8->dev, "failed to write pending config to EEPROM\n");
724 else
725 pulse8->config_pending = false;
726 }
727 mutex_unlock(&pulse8->config_lock);
728}
729
730static struct serio_device_id pulse8_serio_ids[] = {
731 {
732 .type = SERIO_RS232,
733 .proto = SERIO_PULSE8_CEC,
734 .id = SERIO_ANY,
735 .extra = SERIO_ANY,
736 },
737 { 0 }
738};
739
740MODULE_DEVICE_TABLE(serio, pulse8_serio_ids);
741
742static struct serio_driver pulse8_drv = {
743 .driver = {
744 .name = "pulse8-cec",
745 },
746 .description = "Pulse Eight HDMI CEC driver",
747 .id_table = pulse8_serio_ids,
748 .interrupt = pulse8_interrupt,
749 .connect = pulse8_connect,
750 .disconnect = pulse8_disconnect,
751};
752
753module_serio_driver(pulse8_drv);
754