flexcop-eeprom.c 3.2 KB

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