0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022 #include <asm/errno.h>
0023 #include <linux/init.h>
0024 #include <linux/module.h>
0025 #include <linux/string.h>
0026 #include <linux/i2c.h>
0027 #include <linux/etherdevice.h>
0028
0029 #include "ttpci-eeprom.h"
0030
0031 #if 1
0032 #define dprintk(x...) do { printk(x); } while (0)
0033 #else
0034 #define dprintk(x...) do { } while (0)
0035 #endif
0036
0037
0038 static int check_mac_tt(u8 *buf)
0039 {
0040 int i;
0041 u16 tmp = 0xffff;
0042
0043 for (i = 0; i < 8; i++) {
0044 tmp = (tmp << 8) | ((tmp >> 8) ^ buf[i]);
0045 tmp ^= (tmp >> 4) & 0x0f;
0046 tmp ^= (tmp << 12) ^ ((tmp & 0xff) << 5);
0047 }
0048 tmp ^= 0xffff;
0049 return (((tmp >> 8) ^ buf[8]) | ((tmp & 0xff) ^ buf[9]));
0050 }
0051
0052 static int getmac_tt(u8 * decodedMAC, u8 * encodedMAC)
0053 {
0054 u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c,
0055 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6,
0056 0x1d, 0x36, 0x64, 0x78};
0057 u8 data[20];
0058 int i;
0059
0060
0061 memcpy(data, encodedMAC, 20);
0062
0063 for (i = 0; i < 20; i++)
0064 data[i] ^= xor[i];
0065 for (i = 0; i < 10; i++)
0066 data[i] = ((data[2 * i + 1] << 8) | data[2 * i])
0067 >> ((data[2 * i + 1] >> 6) & 3);
0068
0069 if (check_mac_tt(data))
0070 return -ENODEV;
0071
0072 decodedMAC[0] = data[2]; decodedMAC[1] = data[1]; decodedMAC[2] = data[0];
0073 decodedMAC[3] = data[6]; decodedMAC[4] = data[5]; decodedMAC[5] = data[4];
0074 return 0;
0075 }
0076
0077 int ttpci_eeprom_decode_mac(u8 *decodedMAC, u8 *encodedMAC)
0078 {
0079 u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c,
0080 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6,
0081 0x1d, 0x36, 0x64, 0x78};
0082 u8 data[20];
0083 int i;
0084
0085 memcpy(data, encodedMAC, 20);
0086
0087 for (i = 0; i < 20; i++)
0088 data[i] ^= xor[i];
0089 for (i = 0; i < 10; i++)
0090 data[i] = ((data[2 * i + 1] << 8) | data[2 * i])
0091 >> ((data[2 * i + 1] >> 6) & 3);
0092
0093 if (check_mac_tt(data))
0094 return -ENODEV;
0095
0096 decodedMAC[0] = data[2];
0097 decodedMAC[1] = data[1];
0098 decodedMAC[2] = data[0];
0099 decodedMAC[3] = data[6];
0100 decodedMAC[4] = data[5];
0101 decodedMAC[5] = data[4];
0102 return 0;
0103 }
0104 EXPORT_SYMBOL(ttpci_eeprom_decode_mac);
0105
0106 static int ttpci_eeprom_read_encodedMAC(struct i2c_adapter *adapter, u8 * encodedMAC)
0107 {
0108 int ret;
0109 u8 b0[] = { 0xcc };
0110
0111 struct i2c_msg msg[] = {
0112 { .addr = 0x50, .flags = 0, .buf = b0, .len = 1 },
0113 { .addr = 0x50, .flags = I2C_M_RD, .buf = encodedMAC, .len = 20 }
0114 };
0115
0116
0117
0118 ret = i2c_transfer(adapter, msg, 2);
0119
0120 if (ret != 2)
0121 return (-ENODEV);
0122
0123 return 0;
0124 }
0125
0126
0127 int ttpci_eeprom_parse_mac(struct i2c_adapter *adapter, u8 *proposed_mac)
0128 {
0129 int ret;
0130 u8 encodedMAC[20];
0131 u8 decodedMAC[6];
0132
0133 ret = ttpci_eeprom_read_encodedMAC(adapter, encodedMAC);
0134
0135 if (ret != 0) {
0136 dprintk("Couldn't read from EEPROM: not there?\n");
0137 eth_zero_addr(proposed_mac);
0138 return ret;
0139 }
0140
0141 ret = getmac_tt(decodedMAC, encodedMAC);
0142 if( ret != 0 ) {
0143 dprintk("adapter failed MAC signature check\n");
0144 dprintk("encoded MAC from EEPROM was %*phC",
0145 (int)sizeof(encodedMAC), &encodedMAC);
0146 eth_zero_addr(proposed_mac);
0147 return ret;
0148 }
0149
0150 memcpy(proposed_mac, decodedMAC, 6);
0151 dprintk("adapter has MAC addr = %pM\n", decodedMAC);
0152 return 0;
0153 }
0154
0155 EXPORT_SYMBOL(ttpci_eeprom_parse_mac);
0156
0157 MODULE_LICENSE("GPL");
0158 MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others");
0159 MODULE_DESCRIPTION("Decode dvb_net MAC address from EEPROM of PCI DVB cards made by Siemens, Technotrend, Hauppauge");