ttpci-eeprom.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Retrieve encoded MAC address from 24C16 serial 2-wire EEPROM,
  3. decode it and store it in the associated adapter struct for
  4. use by dvb_net.c
  5. This card appear to have the 24C16 write protect held to ground,
  6. thus permitting normal read/write operation. Theoretically it
  7. would be possible to write routines to burn a different (encoded)
  8. MAC address into the EEPROM.
  9. Robert Schlabbach GMX
  10. Michael Glaum KVH Industries
  11. Holger Waechtler Convergence
  12. Copyright (C) 2002-2003 Ralph Metzler <rjkm@metzlerbros.de>
  13. Metzler Brothers Systementwicklung GbR
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 2 of the License, or
  17. (at your option) any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. */
  26. #include <asm/errno.h>
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/string.h>
  30. #include <linux/i2c.h>
  31. #include <linux/etherdevice.h>
  32. #include "ttpci-eeprom.h"
  33. #if 1
  34. #define dprintk(x...) do { printk(x); } while (0)
  35. #else
  36. #define dprintk(x...) do { } while (0)
  37. #endif
  38. static int check_mac_tt(u8 *buf)
  39. {
  40. int i;
  41. u16 tmp = 0xffff;
  42. for (i = 0; i < 8; i++) {
  43. tmp = (tmp << 8) | ((tmp >> 8) ^ buf[i]);
  44. tmp ^= (tmp >> 4) & 0x0f;
  45. tmp ^= (tmp << 12) ^ ((tmp & 0xff) << 5);
  46. }
  47. tmp ^= 0xffff;
  48. return (((tmp >> 8) ^ buf[8]) | ((tmp & 0xff) ^ buf[9]));
  49. }
  50. static int getmac_tt(u8 * decodedMAC, u8 * encodedMAC)
  51. {
  52. u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c,
  53. 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6,
  54. 0x1d, 0x36, 0x64, 0x78};
  55. u8 data[20];
  56. int i;
  57. /* In case there is a sig check failure have the orig contents available */
  58. memcpy(data, encodedMAC, 20);
  59. for (i = 0; i < 20; i++)
  60. data[i] ^= xor[i];
  61. for (i = 0; i < 10; i++)
  62. data[i] = ((data[2 * i + 1] << 8) | data[2 * i])
  63. >> ((data[2 * i + 1] >> 6) & 3);
  64. if (check_mac_tt(data))
  65. return -ENODEV;
  66. decodedMAC[0] = data[2]; decodedMAC[1] = data[1]; decodedMAC[2] = data[0];
  67. decodedMAC[3] = data[6]; decodedMAC[4] = data[5]; decodedMAC[5] = data[4];
  68. return 0;
  69. }
  70. int ttpci_eeprom_decode_mac(u8 *decodedMAC, u8 *encodedMAC)
  71. {
  72. u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c,
  73. 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6,
  74. 0x1d, 0x36, 0x64, 0x78};
  75. u8 data[20];
  76. int i;
  77. memcpy(data, encodedMAC, 20);
  78. for (i = 0; i < 20; i++)
  79. data[i] ^= xor[i];
  80. for (i = 0; i < 10; i++)
  81. data[i] = ((data[2 * i + 1] << 8) | data[2 * i])
  82. >> ((data[2 * i + 1] >> 6) & 3);
  83. if (check_mac_tt(data))
  84. return -ENODEV;
  85. decodedMAC[0] = data[2];
  86. decodedMAC[1] = data[1];
  87. decodedMAC[2] = data[0];
  88. decodedMAC[3] = data[6];
  89. decodedMAC[4] = data[5];
  90. decodedMAC[5] = data[4];
  91. return 0;
  92. }
  93. EXPORT_SYMBOL(ttpci_eeprom_decode_mac);
  94. static int ttpci_eeprom_read_encodedMAC(struct i2c_adapter *adapter, u8 * encodedMAC)
  95. {
  96. int ret;
  97. u8 b0[] = { 0xcc };
  98. struct i2c_msg msg[] = {
  99. { .addr = 0x50, .flags = 0, .buf = b0, .len = 1 },
  100. { .addr = 0x50, .flags = I2C_M_RD, .buf = encodedMAC, .len = 20 }
  101. };
  102. /* dprintk("%s\n", __func__); */
  103. ret = i2c_transfer(adapter, msg, 2);
  104. if (ret != 2) /* Assume EEPROM isn't there */
  105. return (-ENODEV);
  106. return 0;
  107. }
  108. int ttpci_eeprom_parse_mac(struct i2c_adapter *adapter, u8 *proposed_mac)
  109. {
  110. int ret, i;
  111. u8 encodedMAC[20];
  112. u8 decodedMAC[6];
  113. ret = ttpci_eeprom_read_encodedMAC(adapter, encodedMAC);
  114. if (ret != 0) { /* Will only be -ENODEV */
  115. dprintk("Couldn't read from EEPROM: not there?\n");
  116. eth_zero_addr(proposed_mac);
  117. return ret;
  118. }
  119. ret = getmac_tt(decodedMAC, encodedMAC);
  120. if( ret != 0 ) {
  121. dprintk("adapter failed MAC signature check\n");
  122. dprintk("encoded MAC from EEPROM was " );
  123. for(i=0; i<19; i++) {
  124. dprintk( "%.2x:", encodedMAC[i]);
  125. }
  126. dprintk("%.2x\n", encodedMAC[19]);
  127. eth_zero_addr(proposed_mac);
  128. return ret;
  129. }
  130. memcpy(proposed_mac, decodedMAC, 6);
  131. dprintk("adapter has MAC addr = %pM\n", decodedMAC);
  132. return 0;
  133. }
  134. EXPORT_SYMBOL(ttpci_eeprom_parse_mac);
  135. MODULE_LICENSE("GPL");
  136. MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others");
  137. MODULE_DESCRIPTION("Decode dvb_net MAC address from EEPROM of PCI DVB cards "
  138. "made by Siemens, Technotrend, Hauppauge");