hdpvr-i2c.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 IS_ENABLED(CONFIG_I2C)
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/export.h>
  18. #include "hdpvr.h"
  19. #define CTRL_READ_REQUEST 0xb8
  20. #define CTRL_WRITE_REQUEST 0x38
  21. #define REQTYPE_I2C_READ 0xb1
  22. #define REQTYPE_I2C_WRITE 0xb0
  23. #define REQTYPE_I2C_WRITE_STATT 0xd0
  24. #define Z8F0811_IR_TX_I2C_ADDR 0x70
  25. #define Z8F0811_IR_RX_I2C_ADDR 0x71
  26. struct i2c_client *hdpvr_register_ir_i2c(struct hdpvr_device *dev)
  27. {
  28. struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;
  29. struct i2c_board_info info = {
  30. I2C_BOARD_INFO("ir_z8f0811_hdpvr", Z8F0811_IR_RX_I2C_ADDR),
  31. };
  32. /* Our default information for ir-kbd-i2c.c to use */
  33. init_data->ir_codes = RC_MAP_HAUPPAUGE;
  34. init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
  35. init_data->type = RC_PROTO_BIT_RC5 | RC_PROTO_BIT_RC6_MCE |
  36. RC_PROTO_BIT_RC6_6A_32;
  37. init_data->name = "HD-PVR";
  38. init_data->polling_interval = 405; /* ms, duplicated from Windows */
  39. info.platform_data = init_data;
  40. return i2c_new_device(&dev->i2c_adapter, &info);
  41. }
  42. static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
  43. unsigned char addr, char *wdata, int wlen,
  44. char *data, int len)
  45. {
  46. int ret;
  47. if ((len > sizeof(dev->i2c_buf)) || (wlen > sizeof(dev->i2c_buf)))
  48. return -EINVAL;
  49. if (wlen) {
  50. memcpy(&dev->i2c_buf, wdata, wlen);
  51. ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  52. REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
  53. (bus << 8) | addr, 0, &dev->i2c_buf,
  54. wlen, 1000);
  55. if (ret < 0)
  56. return ret;
  57. }
  58. ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  59. REQTYPE_I2C_READ, CTRL_READ_REQUEST,
  60. (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
  61. if (ret == len) {
  62. memcpy(data, &dev->i2c_buf, len);
  63. ret = 0;
  64. } else if (ret >= 0)
  65. ret = -EIO;
  66. return ret;
  67. }
  68. static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
  69. unsigned char addr, char *data, int len)
  70. {
  71. int ret;
  72. if (len > sizeof(dev->i2c_buf))
  73. return -EINVAL;
  74. memcpy(&dev->i2c_buf, data, len);
  75. ret = usb_control_msg(dev->udev, usb_sndctrlpipe(dev->udev, 0),
  76. REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
  77. (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
  78. if (ret < 0)
  79. return ret;
  80. ret = usb_control_msg(dev->udev, usb_rcvctrlpipe(dev->udev, 0),
  81. REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
  82. 0, 0, &dev->i2c_buf, 2, 1000);
  83. if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
  84. ret = 0;
  85. else if (ret >= 0)
  86. ret = -EIO;
  87. return ret;
  88. }
  89. static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
  90. int num)
  91. {
  92. struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
  93. int retval = 0, addr;
  94. mutex_lock(&dev->i2c_mutex);
  95. addr = msgs[0].addr << 1;
  96. if (num == 1) {
  97. if (msgs[0].flags & I2C_M_RD)
  98. retval = hdpvr_i2c_read(dev, 1, addr, NULL, 0,
  99. msgs[0].buf, msgs[0].len);
  100. else
  101. retval = hdpvr_i2c_write(dev, 1, addr, msgs[0].buf,
  102. msgs[0].len);
  103. } else if (num == 2) {
  104. if (msgs[0].addr != msgs[1].addr) {
  105. v4l2_warn(&dev->v4l2_dev, "refusing 2-phase i2c xfer with conflicting target addresses\n");
  106. retval = -EINVAL;
  107. goto out;
  108. }
  109. if ((msgs[0].flags & I2C_M_RD) || !(msgs[1].flags & I2C_M_RD)) {
  110. v4l2_warn(&dev->v4l2_dev, "refusing complex xfer with r0=%d, r1=%d\n",
  111. msgs[0].flags & I2C_M_RD,
  112. msgs[1].flags & I2C_M_RD);
  113. retval = -EINVAL;
  114. goto out;
  115. }
  116. /*
  117. * Write followed by atomic read is the only complex xfer that
  118. * we actually support here.
  119. */
  120. retval = hdpvr_i2c_read(dev, 1, addr, msgs[0].buf, msgs[0].len,
  121. msgs[1].buf, msgs[1].len);
  122. } else {
  123. v4l2_warn(&dev->v4l2_dev, "refusing %d-phase i2c xfer\n", num);
  124. }
  125. out:
  126. mutex_unlock(&dev->i2c_mutex);
  127. return retval ? retval : num;
  128. }
  129. static u32 hdpvr_functionality(struct i2c_adapter *adapter)
  130. {
  131. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  132. }
  133. static const struct i2c_algorithm hdpvr_algo = {
  134. .master_xfer = hdpvr_transfer,
  135. .functionality = hdpvr_functionality,
  136. };
  137. static const struct i2c_adapter hdpvr_i2c_adapter_template = {
  138. .name = "Hauppauge HD PVR I2C",
  139. .owner = THIS_MODULE,
  140. .algo = &hdpvr_algo,
  141. };
  142. static int hdpvr_activate_ir(struct hdpvr_device *dev)
  143. {
  144. char buffer[2];
  145. mutex_lock(&dev->i2c_mutex);
  146. hdpvr_i2c_read(dev, 0, 0x54, NULL, 0, buffer, 1);
  147. buffer[0] = 0;
  148. buffer[1] = 0x8;
  149. hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
  150. buffer[1] = 0x18;
  151. hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
  152. mutex_unlock(&dev->i2c_mutex);
  153. return 0;
  154. }
  155. int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
  156. {
  157. int retval = -ENOMEM;
  158. hdpvr_activate_ir(dev);
  159. dev->i2c_adapter = hdpvr_i2c_adapter_template;
  160. dev->i2c_adapter.dev.parent = &dev->udev->dev;
  161. i2c_set_adapdata(&dev->i2c_adapter, dev);
  162. retval = i2c_add_adapter(&dev->i2c_adapter);
  163. return retval;
  164. }
  165. #endif