hdpvr-i2c.c 5.6 KB

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