linux/drivers/media/usb/pulse8-cec/pulse8-cec.c
<<
>>
Prefs
   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
  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-1)");
  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,       /* 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#define MSGSTART        0xff
 104#define MSGEND          0xfe
 105#define MSGESC          0xfd
 106#define MSGOFFSET       3
 107
 108#define DATA_SIZE 256
 109
 110#define PING_PERIOD     (15 * HZ)
 111
 112struct pulse8 {
 113        struct device *dev;
 114        struct serio *serio;
 115        struct cec_adapter *adap;
 116        unsigned int vers;
 117        struct completion cmd_done;
 118        struct work_struct work;
 119        struct delayed_work ping_eeprom_work;
 120        struct cec_msg rx_msg;
 121        u8 data[DATA_SIZE];
 122        unsigned int len;
 123        u8 buf[DATA_SIZE];
 124        unsigned int idx;
 125        bool escape;
 126        bool started;
 127        struct mutex config_lock;
 128        struct mutex write_lock;
 129        bool config_pending;
 130        bool restoring_config;
 131        bool autonomous;
 132};
 133
 134static void pulse8_ping_eeprom_work_handler(struct work_struct *work);
 135
 136static void pulse8_irq_work_handler(struct work_struct *work)
 137{
 138        struct pulse8 *pulse8 =
 139                container_of(work, struct pulse8, work);
 140
 141        switch (pulse8->data[0] & 0x3f) {
 142        case MSGCODE_FRAME_DATA:
 143                cec_received_msg(pulse8->adap, &pulse8->rx_msg);
 144                break;
 145        case MSGCODE_TRANSMIT_SUCCEEDED:
 146                cec_transmit_attempt_done(pulse8->adap, CEC_TX_STATUS_OK);
 147                break;
 148        case MSGCODE_TRANSMIT_FAILED_ACK:
 149                cec_transmit_attempt_done(pulse8->adap, CEC_TX_STATUS_NACK);
 150                break;
 151        case MSGCODE_TRANSMIT_FAILED_LINE:
 152        case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
 153        case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
 154                cec_transmit_attempt_done(pulse8->adap, CEC_TX_STATUS_ERROR);
 155                break;
 156        }
 157}
 158
 159static irqreturn_t pulse8_interrupt(struct serio *serio, unsigned char data,
 160                                    unsigned int flags)
 161{
 162        struct pulse8 *pulse8 = serio_get_drvdata(serio);
 163
 164        if (!pulse8->started && data != MSGSTART)
 165                return IRQ_HANDLED;
 166        if (data == MSGESC) {
 167                pulse8->escape = true;
 168                return IRQ_HANDLED;
 169        }
 170        if (pulse8->escape) {
 171                data += MSGOFFSET;
 172                pulse8->escape = false;
 173        } else if (data == MSGEND) {
 174                struct cec_msg *msg = &pulse8->rx_msg;
 175
 176                if (debug)
 177                        dev_info(pulse8->dev, "received: %*ph\n",
 178                                 pulse8->idx, pulse8->buf);
 179                pulse8->data[0] = pulse8->buf[0];
 180                switch (pulse8->buf[0] & 0x3f) {
 181                case MSGCODE_FRAME_START:
 182                        msg->len = 1;
 183                        msg->msg[0] = pulse8->buf[1];
 184                        break;
 185                case MSGCODE_FRAME_DATA:
 186                        if (msg->len == CEC_MAX_MSG_SIZE)
 187                                break;
 188                        msg->msg[msg->len++] = pulse8->buf[1];
 189                        if (pulse8->buf[0] & MSGCODE_FRAME_EOM)
 190                                schedule_work(&pulse8->work);
 191                        break;
 192                case MSGCODE_TRANSMIT_SUCCEEDED:
 193                case MSGCODE_TRANSMIT_FAILED_LINE:
 194                case MSGCODE_TRANSMIT_FAILED_ACK:
 195                case MSGCODE_TRANSMIT_FAILED_TIMEOUT_DATA:
 196                case MSGCODE_TRANSMIT_FAILED_TIMEOUT_LINE:
 197                        schedule_work(&pulse8->work);
 198                        break;
 199                case MSGCODE_HIGH_ERROR:
 200                case MSGCODE_LOW_ERROR:
 201                case MSGCODE_RECEIVE_FAILED:
 202                case MSGCODE_TIMEOUT_ERROR:
 203                        break;
 204                case MSGCODE_COMMAND_ACCEPTED:
 205                case MSGCODE_COMMAND_REJECTED:
 206                default:
 207                        if (pulse8->idx == 0)
 208                                break;
 209                        memcpy(pulse8->data, pulse8->buf, pulse8->idx);
 210                        pulse8->len = pulse8->idx;
 211                        complete(&pulse8->cmd_done);
 212                        break;
 213                }
 214                pulse8->idx = 0;
 215                pulse8->started = false;
 216                return IRQ_HANDLED;
 217        } else if (data == MSGSTART) {
 218                pulse8->idx = 0;
 219                pulse8->started = true;
 220                return IRQ_HANDLED;
 221        }
 222
 223        if (pulse8->idx >= DATA_SIZE) {
 224                dev_dbg(pulse8->dev,
 225                        "throwing away %d bytes of garbage\n", pulse8->idx);
 226                pulse8->idx = 0;
 227        }
 228        pulse8->buf[pulse8->idx++] = data;
 229        return IRQ_HANDLED;
 230}
 231
 232static void pulse8_disconnect(struct serio *serio)
 233{
 234        struct pulse8 *pulse8 = serio_get_drvdata(serio);
 235
 236        cec_unregister_adapter(pulse8->adap);
 237        cancel_delayed_work_sync(&pulse8->ping_eeprom_work);
 238        dev_info(&serio->dev, "disconnected\n");
 239        serio_close(serio);
 240        serio_set_drvdata(serio, NULL);
 241        kfree(pulse8);
 242}
 243
 244static int pulse8_send(struct serio *serio, const u8 *command, u8 cmd_len)
 245{
 246        int err = 0;
 247
 248        err = serio_write(serio, MSGSTART);
 249        if (err)
 250                return err;
 251        for (; !err && cmd_len; command++, cmd_len--) {
 252                if (*command >= MSGESC) {
 253                        err = serio_write(serio, MSGESC);
 254                        if (!err)
 255                                err = serio_write(serio, *command - MSGOFFSET);
 256                } else {
 257                        err = serio_write(serio, *command);
 258                }
 259        }
 260        if (!err)
 261                err = serio_write(serio, MSGEND);
 262
 263        return err;
 264}
 265
 266static int pulse8_send_and_wait_once(struct pulse8 *pulse8,
 267                                     const u8 *cmd, u8 cmd_len,
 268                                     u8 response, u8 size)
 269{
 270        int err;
 271
 272        /*dev_info(pulse8->dev, "transmit: %*ph\n", cmd_len, cmd);*/
 273        init_completion(&pulse8->cmd_done);
 274
 275        err = pulse8_send(pulse8->serio, cmd, cmd_len);
 276        if (err)
 277                return err;
 278
 279        if (!wait_for_completion_timeout(&pulse8->cmd_done, HZ))
 280                return -ETIMEDOUT;
 281        if ((pulse8->data[0] & 0x3f) == MSGCODE_COMMAND_REJECTED &&
 282            cmd[0] != MSGCODE_SET_CONTROLLED &&
 283            cmd[0] != MSGCODE_SET_AUTO_ENABLED &&
 284            cmd[0] != MSGCODE_GET_BUILDDATE)
 285                return -ENOTTY;
 286        if (response &&
 287            ((pulse8->data[0] & 0x3f) != response || pulse8->len < size + 1)) {
 288                dev_info(pulse8->dev, "transmit: failed %02x\n",
 289                         pulse8->data[0] & 0x3f);
 290                return -EIO;
 291        }
 292        return 0;
 293}
 294
 295static int pulse8_send_and_wait(struct pulse8 *pulse8,
 296                                const u8 *cmd, u8 cmd_len, u8 response, u8 size)
 297{
 298        u8 cmd_sc[2];
 299        int err;
 300
 301        mutex_lock(&pulse8->write_lock);
 302        err = pulse8_send_and_wait_once(pulse8, cmd, cmd_len, response, size);
 303
 304        if (err == -ENOTTY) {
 305                cmd_sc[0] = MSGCODE_SET_CONTROLLED;
 306                cmd_sc[1] = 1;
 307                err = pulse8_send_and_wait_once(pulse8, cmd_sc, 2,
 308                                                MSGCODE_COMMAND_ACCEPTED, 1);
 309                if (err)
 310                        goto unlock;
 311                err = pulse8_send_and_wait_once(pulse8, cmd, cmd_len,
 312                                                response, size);
 313        }
 314
 315unlock:
 316        mutex_unlock(&pulse8->write_lock);
 317        return err == -ENOTTY ? -EIO : err;
 318}
 319
 320static int pulse8_setup(struct pulse8 *pulse8, struct serio *serio,
 321                        struct cec_log_addrs *log_addrs, u16 *pa)
 322{
 323        u8 *data = pulse8->data + 1;
 324        u8 cmd[2];
 325        int err;
 326        struct tm tm;
 327        time64_t date;
 328
 329        pulse8->vers = 0;
 330
 331        cmd[0] = MSGCODE_FIRMWARE_VERSION;
 332        err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 2);
 333        if (err)
 334                return err;
 335        pulse8->vers = (data[0] << 8) | data[1];
 336        dev_info(pulse8->dev, "Firmware version %04x\n", pulse8->vers);
 337        if (pulse8->vers < 2) {
 338                *pa = CEC_PHYS_ADDR_INVALID;
 339                return 0;
 340        }
 341
 342        cmd[0] = MSGCODE_GET_BUILDDATE;
 343        err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 4);
 344        if (err)
 345                return err;
 346        date = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
 347        time64_to_tm(date, 0, &tm);
 348        dev_info(pulse8->dev, "Firmware build date %04ld.%02d.%02d %02d:%02d:%02d\n",
 349                 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
 350                 tm.tm_hour, tm.tm_min, tm.tm_sec);
 351
 352        dev_dbg(pulse8->dev, "Persistent config:\n");
 353        cmd[0] = MSGCODE_GET_AUTO_ENABLED;
 354        err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
 355        if (err)
 356                return err;
 357        pulse8->autonomous = data[0];
 358        dev_dbg(pulse8->dev, "Autonomous mode: %s",
 359                data[0] ? "on" : "off");
 360
 361        cmd[0] = MSGCODE_GET_DEVICE_TYPE;
 362        err = pulse8_send_and_wait(pulse8, cmd, 1, cmd[0], 1);
 363        if (err)
 364                return err;
 365        log_addrs->primary_device_type[0] = data[0];
 366        dev_dbg(pulse8->dev, "Primary device type: %d\n", data[0]);
 367        switch (log_addrs->primary_device_type[0]) {
 368        case CEC_OP_PRIM_DEVTYPE_TV:
 369                log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TV;
 370                log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_TV;
 371                break;
 372        case CEC_OP_PRIM_DEVTYPE_RECORD:
 373                log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_RECORD;
 374                log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_RECORD;
 375                break;
 376        case CEC_OP_PRIM_DEVTYPE_TUNER:
 377                log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_TUNER;
 378                log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_TUNER;
 379                break;
 380        case CEC_OP_PRIM_DEVTYPE_PLAYBACK:
 381                log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK;
 382                log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_PLAYBACK;
 383                break;
 384        case CEC_OP_PRIM_DEVTYPE_AUDIOSYSTEM:
 385                log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_PLAYBACK;
 386                log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_AUDIOSYSTEM;
 387                break;
 388        case CEC_OP_PRIM_DEVTYPE_SWITCH:
 389                log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
 390                log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
 391                break;
 392        case CEC_OP_PRIM_DEVTYPE_PROCESSOR:
 393                log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_SPECIFIC;
 394                log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
 395                break;
 396        default:
 397                log_addrs->log_addr_type[0] = CEC_LOG_ADDR_TYPE_UNREGISTERED;
 398                log_addrs->all_device_types[0] = CEC_OP_ALL_DEVTYPE_SWITCH;
 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        strscpy(log_addrs->osd_name, data, sizeof(log_addrs->osd_name));
 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 = cec_get_drvdata(adap);
 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 = cec_get_drvdata(adap);
 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                strscpy(cmd + 1, adap->log_addrs.osd_name, sizeof(cmd) - 1);
 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                        strscpy(adap->log_addrs.osd_name, osd_str,
 570                                sizeof(adap->log_addrs.osd_name));
 571                }
 572                err = pulse8_send_and_wait(pulse8, cmd, 1 + osd_len,
 573                                           MSGCODE_COMMAND_ACCEPTED, 0);
 574                if (err)
 575                        goto unlock;
 576        }
 577
 578unlock:
 579        if (pulse8->restoring_config)
 580                pulse8->restoring_config = false;
 581        else
 582                pulse8->config_pending = true;
 583        mutex_unlock(&pulse8->config_lock);
 584        return log_addr == CEC_LOG_ADDR_INVALID ? 0 : err;
 585}
 586
 587static int pulse8_cec_adap_transmit(struct cec_adapter *adap, u8 attempts,
 588                                    u32 signal_free_time, struct cec_msg *msg)
 589{
 590        struct pulse8 *pulse8 = cec_get_drvdata(adap);
 591        u8 cmd[2];
 592        unsigned int i;
 593        int err;
 594
 595        cmd[0] = MSGCODE_TRANSMIT_IDLETIME;
 596        cmd[1] = signal_free_time;
 597        err = pulse8_send_and_wait(pulse8, cmd, 2,
 598                                   MSGCODE_COMMAND_ACCEPTED, 1);
 599        cmd[0] = MSGCODE_TRANSMIT_ACK_POLARITY;
 600        cmd[1] = cec_msg_is_broadcast(msg);
 601        if (!err)
 602                err = pulse8_send_and_wait(pulse8, cmd, 2,
 603                                           MSGCODE_COMMAND_ACCEPTED, 1);
 604        cmd[0] = msg->len == 1 ? MSGCODE_TRANSMIT_EOM : MSGCODE_TRANSMIT;
 605        cmd[1] = msg->msg[0];
 606        if (!err)
 607                err = pulse8_send_and_wait(pulse8, cmd, 2,
 608                                           MSGCODE_COMMAND_ACCEPTED, 1);
 609        if (!err && msg->len > 1) {
 610                cmd[0] = msg->len == 2 ? MSGCODE_TRANSMIT_EOM :
 611                                         MSGCODE_TRANSMIT;
 612                cmd[1] = msg->msg[1];
 613                err = pulse8_send_and_wait(pulse8, cmd, 2,
 614                                           MSGCODE_COMMAND_ACCEPTED, 1);
 615                for (i = 0; !err && i + 2 < msg->len; i++) {
 616                        cmd[0] = (i + 2 == msg->len - 1) ?
 617                                MSGCODE_TRANSMIT_EOM : MSGCODE_TRANSMIT;
 618                        cmd[1] = msg->msg[i + 2];
 619                        err = pulse8_send_and_wait(pulse8, cmd, 2,
 620                                                   MSGCODE_COMMAND_ACCEPTED, 1);
 621                }
 622        }
 623
 624        return err;
 625}
 626
 627static int pulse8_received(struct cec_adapter *adap, struct cec_msg *msg)
 628{
 629        return -ENOMSG;
 630}
 631
 632static const struct cec_adap_ops pulse8_cec_adap_ops = {
 633        .adap_enable = pulse8_cec_adap_enable,
 634        .adap_log_addr = pulse8_cec_adap_log_addr,
 635        .adap_transmit = pulse8_cec_adap_transmit,
 636        .received = pulse8_received,
 637};
 638
 639static int pulse8_connect(struct serio *serio, struct serio_driver *drv)
 640{
 641        u32 caps = CEC_CAP_DEFAULTS | CEC_CAP_PHYS_ADDR | 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                                            dev_name(&serio->dev), caps, 1);
 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, &serio->dev);
 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 const 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