Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 1 | |
| 2 | /* |
Janne Grunau | e86da6f | 2009-03-19 19:00:35 -0300 | [diff] [blame] | 3 | * Hauppauge HD PVR USB driver |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2008 Janne Grunau (j@jannau.net) |
| 6 | * |
Andy Walls | ea6c060 | 2010-12-28 22:46:13 -0300 | [diff] [blame] | 7 | * IR device registration code is |
| 8 | * Copyright (C) 2010 Andy Walls <awalls@md.metrocast.net> |
| 9 | * |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation, version 2. |
| 13 | * |
| 14 | */ |
| 15 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 16 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 17 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 18 | #include <linux/i2c.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 20 | |
| 21 | #include "hdpvr.h" |
| 22 | |
| 23 | #define CTRL_READ_REQUEST 0xb8 |
| 24 | #define CTRL_WRITE_REQUEST 0x38 |
| 25 | |
| 26 | #define REQTYPE_I2C_READ 0xb1 |
| 27 | #define REQTYPE_I2C_WRITE 0xb0 |
| 28 | #define REQTYPE_I2C_WRITE_STATT 0xd0 |
| 29 | |
Andy Walls | ea6c060 | 2010-12-28 22:46:13 -0300 | [diff] [blame] | 30 | #define Z8F0811_IR_TX_I2C_ADDR 0x70 |
| 31 | #define Z8F0811_IR_RX_I2C_ADDR 0x71 |
| 32 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 33 | |
| 34 | static struct i2c_board_info hdpvr_i2c_board_info = { |
| 35 | I2C_BOARD_INFO("ir_tx_z8f0811_hdpvr", Z8F0811_IR_TX_I2C_ADDR), |
| 36 | I2C_BOARD_INFO("ir_rx_z8f0811_hdpvr", Z8F0811_IR_RX_I2C_ADDR), |
Andy Walls | ea6c060 | 2010-12-28 22:46:13 -0300 | [diff] [blame] | 37 | }; |
| 38 | |
Andy Walls | ea6c060 | 2010-12-28 22:46:13 -0300 | [diff] [blame] | 39 | int hdpvr_register_i2c_ir(struct hdpvr_device *dev) |
| 40 | { |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 41 | struct i2c_client *c; |
| 42 | struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data; |
Andy Walls | ea6c060 | 2010-12-28 22:46:13 -0300 | [diff] [blame] | 43 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 44 | /* Our default information for ir-kbd-i2c.c to use */ |
| 45 | init_data->ir_codes = RC_MAP_HAUPPAUGE_NEW; |
| 46 | init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR; |
| 47 | init_data->type = RC_TYPE_RC5; |
| 48 | init_data->name = "HD PVR"; |
| 49 | hdpvr_i2c_board_info.platform_data = init_data; |
Andy Walls | ea6c060 | 2010-12-28 22:46:13 -0300 | [diff] [blame] | 50 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 51 | c = i2c_new_device(&dev->i2c_adapter, &hdpvr_i2c_board_info); |
| 52 | |
| 53 | return (c == NULL) ? -ENODEV : 0; |
Andy Walls | ea6c060 | 2010-12-28 22:46:13 -0300 | [diff] [blame] | 54 | } |
| 55 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 56 | static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus, |
| 57 | unsigned char addr, char *data, int len) |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 58 | { |
| 59 | int ret; |
| 60 | char *buf = kmalloc(len, GFP_KERNEL); |
| 61 | if (!buf) |
| 62 | return -ENOMEM; |
| 63 | |
| 64 | ret = usb_control_msg(dev->udev, |
| 65 | usb_rcvctrlpipe(dev->udev, 0), |
| 66 | REQTYPE_I2C_READ, CTRL_READ_REQUEST, |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 67 | (bus << 8) | addr, 0, buf, len, 1000); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 68 | |
| 69 | if (ret == len) { |
| 70 | memcpy(data, buf, len); |
| 71 | ret = 0; |
| 72 | } else if (ret >= 0) |
| 73 | ret = -EIO; |
| 74 | |
| 75 | kfree(buf); |
| 76 | |
| 77 | return ret; |
| 78 | } |
| 79 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 80 | static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus, |
| 81 | unsigned char addr, char *data, int len) |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 82 | { |
| 83 | int ret; |
| 84 | char *buf = kmalloc(len, GFP_KERNEL); |
| 85 | if (!buf) |
| 86 | return -ENOMEM; |
| 87 | |
| 88 | memcpy(buf, data, len); |
| 89 | ret = usb_control_msg(dev->udev, |
| 90 | usb_sndctrlpipe(dev->udev, 0), |
| 91 | REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST, |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 92 | (bus << 8) | addr, 0, buf, len, 1000); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 93 | |
| 94 | if (ret < 0) |
| 95 | goto error; |
| 96 | |
| 97 | ret = usb_control_msg(dev->udev, |
| 98 | usb_rcvctrlpipe(dev->udev, 0), |
| 99 | REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST, |
| 100 | 0, 0, buf, 2, 1000); |
| 101 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 102 | if ((ret == 2) && (buf[1] == (len - 1))) |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 103 | ret = 0; |
| 104 | else if (ret >= 0) |
| 105 | ret = -EIO; |
| 106 | |
| 107 | error: |
| 108 | kfree(buf); |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs, |
| 113 | int num) |
| 114 | { |
| 115 | struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter); |
| 116 | int retval = 0, i, addr; |
| 117 | |
| 118 | if (num <= 0) |
| 119 | return 0; |
| 120 | |
| 121 | mutex_lock(&dev->i2c_mutex); |
| 122 | |
| 123 | for (i = 0; i < num && !retval; i++) { |
| 124 | addr = msgs[i].addr << 1; |
| 125 | |
| 126 | if (msgs[i].flags & I2C_M_RD) |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 127 | retval = hdpvr_i2c_read(dev, 1, addr, msgs[i].buf, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 128 | msgs[i].len); |
| 129 | else |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 130 | retval = hdpvr_i2c_write(dev, 1, addr, msgs[i].buf, |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 131 | msgs[i].len); |
| 132 | } |
| 133 | |
| 134 | mutex_unlock(&dev->i2c_mutex); |
| 135 | |
| 136 | return retval ? retval : num; |
| 137 | } |
| 138 | |
| 139 | static u32 hdpvr_functionality(struct i2c_adapter *adapter) |
| 140 | { |
| 141 | return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL; |
| 142 | } |
| 143 | |
| 144 | static struct i2c_algorithm hdpvr_algo = { |
| 145 | .master_xfer = hdpvr_transfer, |
| 146 | .functionality = hdpvr_functionality, |
| 147 | }; |
| 148 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 149 | static struct i2c_adapter hdpvr_i2c_adapter_template = { |
| 150 | .name = "Hauppage HD PVR I2C", |
| 151 | .owner = THIS_MODULE, |
| 152 | .algo = &hdpvr_algo, |
| 153 | }; |
| 154 | |
| 155 | static int hdpvr_activate_ir(struct hdpvr_device *dev) |
| 156 | { |
| 157 | char buffer[8]; |
| 158 | |
| 159 | mutex_lock(&dev->i2c_mutex); |
| 160 | |
| 161 | hdpvr_i2c_read(dev, 0, 0x54, buffer, 1); |
| 162 | |
| 163 | buffer[0] = 0; |
| 164 | buffer[1] = 0x8; |
| 165 | hdpvr_i2c_write(dev, 1, 0x54, buffer, 2); |
| 166 | |
| 167 | buffer[1] = 0x18; |
| 168 | hdpvr_i2c_write(dev, 1, 0x54, buffer, 2); |
| 169 | |
| 170 | mutex_unlock(&dev->i2c_mutex); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 175 | int hdpvr_register_i2c_adapter(struct hdpvr_device *dev) |
| 176 | { |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 177 | int retval = -ENOMEM; |
| 178 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 179 | hdpvr_activate_ir(dev); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 180 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 181 | memcpy(&dev->i2c_adapter, &hdpvr_i2c_adapter_template, |
| 182 | sizeof(struct i2c_adapter)); |
| 183 | dev->i2c_adapter.dev.parent = &dev->udev->dev; |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 184 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 185 | i2c_set_adapdata(&dev->i2c_adapter, dev); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 186 | |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 187 | retval = i2c_add_adapter(&dev->i2c_adapter); |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 188 | |
Janne Grunau | 9aba42e | 2009-03-18 18:10:04 -0300 | [diff] [blame] | 189 | return retval; |
| 190 | } |
Jarod Wilson | 324b04b | 2011-01-14 16:25:21 -0300 | [diff] [blame^] | 191 | |
| 192 | #endif |