blob: 079ee098b7e37e4e9e2db442e83a5c0873fc6318 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
6 This card appear to have the 24C16 write protect held to ground,
7 thus permitting normal read/write operation. Theoretically it
8 would be possible to write routines to burn a different (encoded)
9 MAC address into the EEPROM.
10
11 Robert Schlabbach GMX
12 Michael Glaum KVH Industries
13 Holger Waechtler Convergence
14
15 Copyright (C) 2002-2003 Ralph Metzler <rjkm@metzlerbros.de>
Michael Krufky50c25ff2006-01-09 15:25:34 -020016 Metzler Brothers Systementwicklung GbR
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 2 of the License, or
21 (at your option) any later version.
22
23 This program is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
27
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31
32*/
33
34#include <asm/errno.h>
35#include <linux/init.h>
36#include <linux/module.h>
37#include <linux/string.h>
38#include <linux/i2c.h>
Vaishali Thakkarfa9163b2015-06-26 01:47:49 -030039#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Adrian Bunk15ac8e62005-12-01 00:51:53 -080041#include "ttpci-eeprom.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#if 1
44#define dprintk(x...) do { printk(x); } while (0)
45#else
46#define dprintk(x...) do { } while (0)
47#endif
48
49
50static int check_mac_tt(u8 *buf)
51{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080052 int i;
53 u16 tmp = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080055 for (i = 0; i < 8; i++) {
56 tmp = (tmp << 8) | ((tmp >> 8) ^ buf[i]);
57 tmp ^= (tmp >> 4) & 0x0f;
58 tmp ^= (tmp << 12) ^ ((tmp & 0xff) << 5);
59 }
60 tmp ^= 0xffff;
61 return (((tmp >> 8) ^ buf[8]) | ((tmp & 0xff) ^ buf[9]));
Linus Torvalds1da177e2005-04-16 15:20:36 -070062}
63
64static int getmac_tt(u8 * decodedMAC, u8 * encodedMAC)
65{
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080066 u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c,
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6,
68 0x1d, 0x36, 0x64, 0x78};
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080069 u8 data[20];
70 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /* In case there is a sig check failure have the orig contents available */
73 memcpy(data, encodedMAC, 20);
74
75 for (i = 0; i < 20; i++)
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080076 data[i] ^= xor[i];
77 for (i = 0; i < 10; i++)
78 data[i] = ((data[2 * i + 1] << 8) | data[2 * i])
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 >> ((data[2 * i + 1] >> 6) & 3);
80
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080081 if (check_mac_tt(data))
82 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 decodedMAC[0] = data[2]; decodedMAC[1] = data[1]; decodedMAC[2] = data[0];
85 decodedMAC[3] = data[6]; decodedMAC[4] = data[5]; decodedMAC[5] = data[4];
Mauro Carvalho Chehab9101e622005-12-12 00:37:24 -080086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Igor M. Liplianin4e2c53f2011-09-23 18:33:50 -030089int ttpci_eeprom_decode_mac(u8 *decodedMAC, u8 *encodedMAC)
90{
91 u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c,
92 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6,
93 0x1d, 0x36, 0x64, 0x78};
94 u8 data[20];
95 int i;
96
97 memcpy(data, encodedMAC, 20);
98
99 for (i = 0; i < 20; i++)
100 data[i] ^= xor[i];
101 for (i = 0; i < 10; i++)
102 data[i] = ((data[2 * i + 1] << 8) | data[2 * i])
103 >> ((data[2 * i + 1] >> 6) & 3);
104
105 if (check_mac_tt(data))
106 return -ENODEV;
107
108 decodedMAC[0] = data[2];
109 decodedMAC[1] = data[1];
110 decodedMAC[2] = data[0];
111 decodedMAC[3] = data[6];
112 decodedMAC[4] = data[5];
113 decodedMAC[5] = data[4];
114 return 0;
115}
116EXPORT_SYMBOL(ttpci_eeprom_decode_mac);
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int ttpci_eeprom_read_encodedMAC(struct i2c_adapter *adapter, u8 * encodedMAC)
119{
120 int ret;
121 u8 b0[] = { 0xcc };
122
123 struct i2c_msg msg[] = {
124 { .addr = 0x50, .flags = 0, .buf = b0, .len = 1 },
125 { .addr = 0x50, .flags = I2C_M_RD, .buf = encodedMAC, .len = 20 }
126 };
127
Harvey Harrison3ca7fc82008-04-08 23:20:00 -0300128 /* dprintk("%s\n", __func__); */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 ret = i2c_transfer(adapter, msg, 2);
131
132 if (ret != 2) /* Assume EEPROM isn't there */
133 return (-ENODEV);
134
135 return 0;
136}
137
138
139int ttpci_eeprom_parse_mac(struct i2c_adapter *adapter, u8 *proposed_mac)
140{
141 int ret, i;
142 u8 encodedMAC[20];
143 u8 decodedMAC[6];
144
145 ret = ttpci_eeprom_read_encodedMAC(adapter, encodedMAC);
146
147 if (ret != 0) { /* Will only be -ENODEV */
148 dprintk("Couldn't read from EEPROM: not there?\n");
Vaishali Thakkarfa9163b2015-06-26 01:47:49 -0300149 eth_zero_addr(proposed_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return ret;
151 }
152
153 ret = getmac_tt(decodedMAC, encodedMAC);
154 if( ret != 0 ) {
155 dprintk("adapter failed MAC signature check\n");
156 dprintk("encoded MAC from EEPROM was " );
157 for(i=0; i<19; i++) {
158 dprintk( "%.2x:", encodedMAC[i]);
159 }
160 dprintk("%.2x\n", encodedMAC[19]);
Vaishali Thakkarfa9163b2015-06-26 01:47:49 -0300161 eth_zero_addr(proposed_mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return ret;
163 }
164
165 memcpy(proposed_mac, decodedMAC, 6);
Joe Perchesdd70b272015-06-14 23:01:45 -0300166 dprintk("adapter has MAC addr = %pM\n", decodedMAC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return 0;
168}
169
170EXPORT_SYMBOL(ttpci_eeprom_parse_mac);
171
172MODULE_LICENSE("GPL");
173MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others");
174MODULE_DESCRIPTION("Decode dvb_net MAC address from EEPROM of PCI DVB cards "
175 "made by Siemens, Technotrend, Hauppauge");