Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 
0003 /*
0004  * Hauppauge HD PVR USB driver
0005  *
0006  * Copyright (C) 2008      Janne Grunau (j@jannau.net)
0007  *
0008  * IR device registration code is
0009  * Copyright (C) 2010   Andy Walls <awalls@md.metrocast.net>
0010  */
0011 
0012 #if IS_ENABLED(CONFIG_I2C)
0013 
0014 #include <linux/i2c.h>
0015 #include <linux/slab.h>
0016 #include <linux/export.h>
0017 
0018 #include "hdpvr.h"
0019 
0020 #define CTRL_READ_REQUEST   0xb8
0021 #define CTRL_WRITE_REQUEST  0x38
0022 
0023 #define REQTYPE_I2C_READ    0xb1
0024 #define REQTYPE_I2C_WRITE   0xb0
0025 #define REQTYPE_I2C_WRITE_STATT 0xd0
0026 
0027 #define Z8F0811_IR_TX_I2C_ADDR  0x70
0028 #define Z8F0811_IR_RX_I2C_ADDR  0x71
0029 
0030 
0031 struct i2c_client *hdpvr_register_ir_i2c(struct hdpvr_device *dev)
0032 {
0033     struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;
0034     struct i2c_board_info info = {
0035         I2C_BOARD_INFO("ir_z8f0811_hdpvr", Z8F0811_IR_RX_I2C_ADDR),
0036     };
0037 
0038     /* Our default information for ir-kbd-i2c.c to use */
0039     init_data->ir_codes = RC_MAP_HAUPPAUGE;
0040     init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
0041     init_data->type = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
0042               RC_PROTO_BIT_RC6_6A_32;
0043     init_data->name = "HD-PVR";
0044     init_data->polling_interval = 405; /* ms, duplicated from Windows */
0045     info.platform_data = init_data;
0046 
0047     return i2c_new_client_device(&dev->i2c_adapter, &info);
0048 }
0049 
0050 static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
0051               unsigned char addr, char *wdata, int wlen,
0052               char *data, int len)
0053 {
0054     int ret;
0055 
0056     if ((len > sizeof(dev->i2c_buf)) || (wlen > sizeof(dev->i2c_buf)))
0057         return -EINVAL;
0058 
0059     if (wlen) {
0060         memcpy(dev->i2c_buf, wdata, wlen);
0061         ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
0062                       REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
0063                       (bus << 8) | addr, 0, dev->i2c_buf,
0064                       wlen, 1000);
0065         if (ret < 0)
0066             return ret;
0067     }
0068 
0069     ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
0070                   REQTYPE_I2C_READ, CTRL_READ_REQUEST,
0071                   (bus << 8) | addr, 0, dev->i2c_buf, len, 1000);
0072 
0073     if (ret == len) {
0074         memcpy(data, dev->i2c_buf, len);
0075         ret = 0;
0076     } else if (ret >= 0)
0077         ret = -EIO;
0078 
0079     return ret;
0080 }
0081 
0082 static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
0083                unsigned char addr, char *data, int len)
0084 {
0085     int ret;
0086 
0087     if (len > sizeof(dev->i2c_buf))
0088         return -EINVAL;
0089 
0090     memcpy(dev->i2c_buf, data, len);
0091     ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
0092                   REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
0093                   (bus << 8) | addr, 0, dev->i2c_buf, len, 1000);
0094 
0095     if (ret < 0)
0096         return ret;
0097 
0098     ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
0099                   REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
0100                   0, 0, dev->i2c_buf, 2, 1000);
0101 
0102     if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
0103         ret = 0;
0104     else if (ret >= 0)
0105         ret = -EIO;
0106 
0107     return ret;
0108 }
0109 
0110 static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
0111               int num)
0112 {
0113     struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
0114     int retval = 0, addr;
0115 
0116     mutex_lock(&dev->i2c_mutex);
0117 
0118     addr = msgs[0].addr << 1;
0119 
0120     if (num == 1) {
0121         if (msgs[0].flags & I2C_M_RD)
0122             retval = hdpvr_i2c_read(dev, 1, addr, NULL, 0,
0123                         msgs[0].buf, msgs[0].len);
0124         else
0125             retval = hdpvr_i2c_write(dev, 1, addr, msgs[0].buf,
0126                          msgs[0].len);
0127     } else if (num == 2) {
0128         if (msgs[0].addr != msgs[1].addr) {
0129             v4l2_warn(&dev->v4l2_dev, "refusing 2-phase i2c xfer with conflicting target addresses\n");
0130             retval = -EINVAL;
0131             goto out;
0132         }
0133 
0134         if ((msgs[0].flags & I2C_M_RD) || !(msgs[1].flags & I2C_M_RD)) {
0135             v4l2_warn(&dev->v4l2_dev, "refusing complex xfer with r0=%d, r1=%d\n",
0136                   msgs[0].flags & I2C_M_RD,
0137                   msgs[1].flags & I2C_M_RD);
0138             retval = -EINVAL;
0139             goto out;
0140         }
0141 
0142         /*
0143          * Write followed by atomic read is the only complex xfer that
0144          * we actually support here.
0145          */
0146         retval = hdpvr_i2c_read(dev, 1, addr, msgs[0].buf, msgs[0].len,
0147                     msgs[1].buf, msgs[1].len);
0148     } else {
0149         v4l2_warn(&dev->v4l2_dev, "refusing %d-phase i2c xfer\n", num);
0150     }
0151 
0152 out:
0153     mutex_unlock(&dev->i2c_mutex);
0154 
0155     return retval ? retval : num;
0156 }
0157 
0158 static u32 hdpvr_functionality(struct i2c_adapter *adapter)
0159 {
0160     return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
0161 }
0162 
0163 static const struct i2c_algorithm hdpvr_algo = {
0164     .master_xfer   = hdpvr_transfer,
0165     .functionality = hdpvr_functionality,
0166 };
0167 
0168 static const struct i2c_adapter hdpvr_i2c_adapter_template = {
0169     .name   = "Hauppauge HD PVR I2C",
0170     .owner  = THIS_MODULE,
0171     .algo   = &hdpvr_algo,
0172 };
0173 
0174 static int hdpvr_activate_ir(struct hdpvr_device *dev)
0175 {
0176     char buffer[2];
0177 
0178     mutex_lock(&dev->i2c_mutex);
0179 
0180     hdpvr_i2c_read(dev, 0, 0x54, NULL, 0, buffer, 1);
0181 
0182     buffer[0] = 0;
0183     buffer[1] = 0x8;
0184     hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
0185 
0186     buffer[1] = 0x18;
0187     hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
0188 
0189     mutex_unlock(&dev->i2c_mutex);
0190 
0191     return 0;
0192 }
0193 
0194 int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
0195 {
0196     hdpvr_activate_ir(dev);
0197 
0198     dev->i2c_adapter = hdpvr_i2c_adapter_template;
0199     dev->i2c_adapter.dev.parent = &dev->udev->dev;
0200 
0201     i2c_set_adapdata(&dev->i2c_adapter, dev);
0202 
0203     return i2c_add_adapter(&dev->i2c_adapter);
0204 }
0205 
0206 #endif