ttpci-eeprom.c 4.0 KB

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