flexcop-eeprom.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Linux driver for digital TV devices equipped with B2C2 FlexcopII(b)/III
  3. * flexcop-eeprom.c - eeprom access methods (currently only MAC address reading)
  4. * see flexcop.c for copyright information
  5. */
  6. #include "flexcop.h"
  7. #if 0
  8. /*EEPROM (Skystar2 has one "24LC08B" chip on board) */
  9. static int eeprom_write(struct adapter *adapter, u16 addr, u8 *buf, u16 len)
  10. {
  11. return flex_i2c_write(adapter, 0x20000000, 0x50, addr, buf, len);
  12. }
  13. static int eeprom_lrc_write(struct adapter *adapter, u32 addr,
  14. u32 len, u8 *wbuf, u8 *rbuf, int retries)
  15. {
  16. int i;
  17. for (i = 0; i < retries; i++) {
  18. if (eeprom_write(adapter, addr, wbuf, len) == len) {
  19. if (eeprom_lrc_read(adapter, addr, len, rbuf, retries) == 1)
  20. return 1;
  21. }
  22. }
  23. return 0;
  24. }
  25. /* These functions could be used to unlock SkyStar2 cards. */
  26. static int eeprom_writeKey(struct adapter *adapter, u8 *key, u32 len)
  27. {
  28. u8 rbuf[20];
  29. u8 wbuf[20];
  30. if (len != 16)
  31. return 0;
  32. memcpy(wbuf, key, len);
  33. wbuf[16] = 0;
  34. wbuf[17] = 0;
  35. wbuf[18] = 0;
  36. wbuf[19] = calc_lrc(wbuf, 19);
  37. return eeprom_lrc_write(adapter, 0x3e4, 20, wbuf, rbuf, 4);
  38. }
  39. static int eeprom_readKey(struct adapter *adapter, u8 *key, u32 len)
  40. {
  41. u8 buf[20];
  42. if (len != 16)
  43. return 0;
  44. if (eeprom_lrc_read(adapter, 0x3e4, 20, buf, 4) == 0)
  45. return 0;
  46. memcpy(key, buf, len);
  47. return 1;
  48. }
  49. static char eeprom_set_mac_addr(struct adapter *adapter, char type, u8 *mac)
  50. {
  51. u8 tmp[8];
  52. if (type != 0) {
  53. tmp[0] = mac[0];
  54. tmp[1] = mac[1];
  55. tmp[2] = mac[2];
  56. tmp[3] = mac[5];
  57. tmp[4] = mac[6];
  58. tmp[5] = mac[7];
  59. } else {
  60. tmp[0] = mac[0];
  61. tmp[1] = mac[1];
  62. tmp[2] = mac[2];
  63. tmp[3] = mac[3];
  64. tmp[4] = mac[4];
  65. tmp[5] = mac[5];
  66. }
  67. tmp[6] = 0;
  68. tmp[7] = calc_lrc(tmp, 7);
  69. if (eeprom_write(adapter, 0x3f8, tmp, 8) == 8)
  70. return 1;
  71. return 0;
  72. }
  73. static int flexcop_eeprom_read(struct flexcop_device *fc,
  74. u16 addr, u8 *buf, u16 len)
  75. {
  76. return fc->i2c_request(fc,FC_READ,FC_I2C_PORT_EEPROM,0x50,addr,buf,len);
  77. }
  78. #endif
  79. static u8 calc_lrc(u8 *buf, int len)
  80. {
  81. int i;
  82. u8 sum = 0;
  83. for (i = 0; i < len; i++)
  84. sum = sum ^ buf[i];
  85. return sum;
  86. }
  87. static int flexcop_eeprom_request(struct flexcop_device *fc,
  88. flexcop_access_op_t op, u16 addr, u8 *buf, u16 len, int retries)
  89. {
  90. int i,ret = 0;
  91. u8 chipaddr = 0x50 | ((addr >> 8) & 3);
  92. for (i = 0; i < retries; i++) {
  93. ret = fc->i2c_request(&fc->fc_i2c_adap[1], op, chipaddr,
  94. addr & 0xff, buf, len);
  95. if (ret == 0)
  96. break;
  97. }
  98. return ret;
  99. }
  100. static int flexcop_eeprom_lrc_read(struct flexcop_device *fc, u16 addr,
  101. u8 *buf, u16 len, int retries)
  102. {
  103. int ret = flexcop_eeprom_request(fc, FC_READ, addr, buf, len, retries);
  104. if (ret == 0)
  105. if (calc_lrc(buf, len - 1) != buf[len - 1])
  106. ret = -EINVAL;
  107. return ret;
  108. }
  109. /* JJ's comment about extended == 1: it is not presently used anywhere but was
  110. * added to the low-level functions for possible support of EUI64 */
  111. int flexcop_eeprom_check_mac_addr(struct flexcop_device *fc, int extended)
  112. {
  113. u8 buf[8];
  114. int ret = 0;
  115. if ((ret = flexcop_eeprom_lrc_read(fc,0x3f8,buf,8,4)) == 0) {
  116. if (extended != 0) {
  117. err("TODO: extended (EUI64) MAC addresses aren't "
  118. "completely supported yet");
  119. ret = -EINVAL;
  120. } else
  121. memcpy(fc->dvb_adapter.proposed_mac,buf,6);
  122. }
  123. return ret;
  124. }
  125. EXPORT_SYMBOL(flexcop_eeprom_check_mac_addr);