hdpvr-i2c.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. static struct i2c_board_info hdpvr_i2c_board_info = {
  26. I2C_BOARD_INFO("ir_tx_z8f0811_hdpvr", Z8F0811_IR_TX_I2C_ADDR),
  27. I2C_BOARD_INFO("ir_rx_z8f0811_hdpvr", Z8F0811_IR_RX_I2C_ADDR),
  28. };
  29. int hdpvr_register_i2c_ir(struct hdpvr_device *dev)
  30. {
  31. struct i2c_client *c;
  32. struct IR_i2c_init_data *init_data = &dev->ir_i2c_init_data;
  33. /* Our default information for ir-kbd-i2c.c to use */
  34. init_data->ir_codes = RC_MAP_HAUPPAUGE_NEW;
  35. init_data->internal_get_key_func = IR_KBD_GET_KEY_HAUP_XVR;
  36. init_data->type = RC_TYPE_RC5;
  37. init_data->name = "HD PVR";
  38. hdpvr_i2c_board_info.platform_data = init_data;
  39. c = i2c_new_device(&dev->i2c_adapter, &hdpvr_i2c_board_info);
  40. return (c == NULL) ? -ENODEV : 0;
  41. }
  42. static int hdpvr_i2c_read(struct hdpvr_device *dev, int bus,
  43. unsigned char addr, char *data, int len)
  44. {
  45. int ret;
  46. if (len > sizeof(dev->i2c_buf))
  47. return -EINVAL;
  48. ret = usb_control_msg(dev->udev,
  49. usb_rcvctrlpipe(dev->udev, 0),
  50. REQTYPE_I2C_READ, CTRL_READ_REQUEST,
  51. (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
  52. if (ret == len) {
  53. memcpy(data, &dev->i2c_buf, len);
  54. ret = 0;
  55. } else if (ret >= 0)
  56. ret = -EIO;
  57. return ret;
  58. }
  59. static int hdpvr_i2c_write(struct hdpvr_device *dev, int bus,
  60. unsigned char addr, char *data, int len)
  61. {
  62. int ret;
  63. if (len > sizeof(dev->i2c_buf))
  64. return -EINVAL;
  65. memcpy(&dev->i2c_buf, data, len);
  66. ret = usb_control_msg(dev->udev,
  67. usb_sndctrlpipe(dev->udev, 0),
  68. REQTYPE_I2C_WRITE, CTRL_WRITE_REQUEST,
  69. (bus << 8) | addr, 0, &dev->i2c_buf, len, 1000);
  70. if (ret < 0)
  71. return ret;
  72. ret = usb_control_msg(dev->udev,
  73. usb_rcvctrlpipe(dev->udev, 0),
  74. REQTYPE_I2C_WRITE_STATT, CTRL_READ_REQUEST,
  75. 0, 0, &dev->i2c_buf, 2, 1000);
  76. if ((ret == 2) && (dev->i2c_buf[1] == (len - 1)))
  77. ret = 0;
  78. else if (ret >= 0)
  79. ret = -EIO;
  80. return ret;
  81. }
  82. static int hdpvr_transfer(struct i2c_adapter *i2c_adapter, struct i2c_msg *msgs,
  83. int num)
  84. {
  85. struct hdpvr_device *dev = i2c_get_adapdata(i2c_adapter);
  86. int retval = 0, i, addr;
  87. if (num <= 0)
  88. return 0;
  89. mutex_lock(&dev->i2c_mutex);
  90. for (i = 0; i < num && !retval; i++) {
  91. addr = msgs[i].addr << 1;
  92. if (msgs[i].flags & I2C_M_RD)
  93. retval = hdpvr_i2c_read(dev, 1, addr, msgs[i].buf,
  94. msgs[i].len);
  95. else
  96. retval = hdpvr_i2c_write(dev, 1, addr, msgs[i].buf,
  97. msgs[i].len);
  98. }
  99. mutex_unlock(&dev->i2c_mutex);
  100. return retval ? retval : num;
  101. }
  102. static u32 hdpvr_functionality(struct i2c_adapter *adapter)
  103. {
  104. return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
  105. }
  106. static struct i2c_algorithm hdpvr_algo = {
  107. .master_xfer = hdpvr_transfer,
  108. .functionality = hdpvr_functionality,
  109. };
  110. static struct i2c_adapter hdpvr_i2c_adapter_template = {
  111. .name = "Hauppage HD PVR I2C",
  112. .owner = THIS_MODULE,
  113. .algo = &hdpvr_algo,
  114. };
  115. static int hdpvr_activate_ir(struct hdpvr_device *dev)
  116. {
  117. char buffer[8];
  118. mutex_lock(&dev->i2c_mutex);
  119. hdpvr_i2c_read(dev, 0, 0x54, buffer, 1);
  120. buffer[0] = 0;
  121. buffer[1] = 0x8;
  122. hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
  123. buffer[1] = 0x18;
  124. hdpvr_i2c_write(dev, 1, 0x54, buffer, 2);
  125. mutex_unlock(&dev->i2c_mutex);
  126. return 0;
  127. }
  128. int hdpvr_register_i2c_adapter(struct hdpvr_device *dev)
  129. {
  130. int retval = -ENOMEM;
  131. hdpvr_activate_ir(dev);
  132. memcpy(&dev->i2c_adapter, &hdpvr_i2c_adapter_template,
  133. sizeof(struct i2c_adapter));
  134. dev->i2c_adapter.dev.parent = &dev->udev->dev;
  135. i2c_set_adapdata(&dev->i2c_adapter, dev);
  136. retval = i2c_add_adapter(&dev->i2c_adapter);
  137. return retval;
  138. }
  139. #endif