ttpci-eeprom.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #if 1
  32. #define dprintk(x...) do { printk(x); } while (0)
  33. #else
  34. #define dprintk(x...) do { } while (0)
  35. #endif
  36. static int check_mac_tt(u8 *buf)
  37. {
  38. int i;
  39. u16 tmp = 0xffff;
  40. for (i = 0; i < 8; i++) {
  41. tmp = (tmp << 8) | ((tmp >> 8) ^ buf[i]);
  42. tmp ^= (tmp >> 4) & 0x0f;
  43. tmp ^= (tmp << 12) ^ ((tmp & 0xff) << 5);
  44. }
  45. tmp ^= 0xffff;
  46. return (((tmp >> 8) ^ buf[8]) | ((tmp & 0xff) ^ buf[9]));
  47. }
  48. static int getmac_tt(u8 * decodedMAC, u8 * encodedMAC)
  49. {
  50. u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c,
  51. 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6,
  52. 0x1d, 0x36, 0x64, 0x78};
  53. u8 data[20];
  54. int i;
  55. /* In case there is a sig check failure have the orig contents available */
  56. memcpy(data, encodedMAC, 20);
  57. for (i = 0; i < 20; i++)
  58. data[i] ^= xor[i];
  59. for (i = 0; i < 10; i++)
  60. data[i] = ((data[2 * i + 1] << 8) | data[2 * i])
  61. >> ((data[2 * i + 1] >> 6) & 3);
  62. if (check_mac_tt(data))
  63. return -ENODEV;
  64. decodedMAC[0] = data[2]; decodedMAC[1] = data[1]; decodedMAC[2] = data[0];
  65. decodedMAC[3] = data[6]; decodedMAC[4] = data[5]; decodedMAC[5] = data[4];
  66. return 0;
  67. }
  68. static int ttpci_eeprom_read_encodedMAC(struct i2c_adapter *adapter, u8 * encodedMAC)
  69. {
  70. int ret;
  71. u8 b0[] = { 0xcc };
  72. struct i2c_msg msg[] = {
  73. { .addr = 0x50, .flags = 0, .buf = b0, .len = 1 },
  74. { .addr = 0x50, .flags = I2C_M_RD, .buf = encodedMAC, .len = 20 }
  75. };
  76. /* dprintk("%s\n", __FUNCTION__); */
  77. ret = i2c_transfer(adapter, msg, 2);
  78. if (ret != 2) /* Assume EEPROM isn't there */
  79. return (-ENODEV);
  80. return 0;
  81. }
  82. int ttpci_eeprom_parse_mac(struct i2c_adapter *adapter, u8 *proposed_mac)
  83. {
  84. int ret, i;
  85. u8 encodedMAC[20];
  86. u8 decodedMAC[6];
  87. ret = ttpci_eeprom_read_encodedMAC(adapter, encodedMAC);
  88. if (ret != 0) { /* Will only be -ENODEV */
  89. dprintk("Couldn't read from EEPROM: not there?\n");
  90. memset(proposed_mac, 0, 6);
  91. return ret;
  92. }
  93. ret = getmac_tt(decodedMAC, encodedMAC);
  94. if( ret != 0 ) {
  95. dprintk("adapter failed MAC signature check\n");
  96. dprintk("encoded MAC from EEPROM was " );
  97. for(i=0; i<19; i++) {
  98. dprintk( "%.2x:", encodedMAC[i]);
  99. }
  100. dprintk("%.2x\n", encodedMAC[19]);
  101. memset(proposed_mac, 0, 6);
  102. return ret;
  103. }
  104. memcpy(proposed_mac, decodedMAC, 6);
  105. dprintk("adapter has MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
  106. decodedMAC[0], decodedMAC[1], decodedMAC[2],
  107. decodedMAC[3], decodedMAC[4], decodedMAC[5]);
  108. return 0;
  109. }
  110. EXPORT_SYMBOL(ttpci_eeprom_parse_mac);
  111. MODULE_LICENSE("GPL");
  112. MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others");
  113. MODULE_DESCRIPTION("Decode dvb_net MAC address from EEPROM of PCI DVB cards "
  114. "made by Siemens, Technotrend, Hauppauge");