[PATCH] Update MAC handling for various DVB PCI cards
[linux-2.6/history.git] / drivers / media / dvb / ttpci / ttpci-eeprom.c
blob74fcfe04692ed9e0a555f143dcc0718e3f3f1feb
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
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.
11 Robert Schlabbach GMX
12 Michael Glaum KVH Industries
13 Holger Waechtler Convergence
15 Copyright (C) 2002-2003 Ralph Metzler <rjkm@metzlerbros.de>
16 Metzler Brothers Systementwicklung GbR
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.
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.
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.
34 #include <asm/errno.h>
35 #include <linux/init.h>
36 #include <linux/module.h>
37 #include <linux/string.h>
39 #include "dvb_i2c.h"
40 #include "dvb_functions.h"
42 #if 1
43 #define dprintk(x...) do { printk(x); } while (0)
44 #else
45 #define dprintk(x...) do { } while (0)
46 #endif
49 static int check_mac_tt(u8 *buf)
51 int i;
52 u16 tmp = 0xffff;
54 for (i = 0; i < 8; i++) {
55 tmp = (tmp << 8) | ((tmp >> 8) ^ buf[i]);
56 tmp ^= (tmp >> 4) & 0x0f;
57 tmp ^= (tmp << 12) ^ ((tmp & 0xff) << 5);
59 tmp ^= 0xffff;
60 return (((tmp >> 8) ^ buf[8]) | ((tmp & 0xff) ^ buf[9]));
63 static int getmac_tt(u8 * decodedMAC, u8 * encodedMAC)
65 u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c,
66 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6,
67 0x1d, 0x36, 0x64, 0x78};
68 u8 data[20];
69 int i;
71 /* In case there is a sig check failure have the orig contents available */
72 memcpy(data, encodedMAC, 20);
74 for (i = 0; i < 20; i++)
75 data[i] ^= xor[i];
76 for (i = 0; i < 10; i++)
77 data[i] = ((data[2 * i + 1] << 8) | data[2 * i])
78 >> ((data[2 * i + 1] >> 6) & 3);
80 if (check_mac_tt(data))
81 return -ENODEV;
83 decodedMAC[0] = data[2]; decodedMAC[1] = data[1]; decodedMAC[2] = data[0];
84 decodedMAC[3] = data[6]; decodedMAC[4] = data[5]; decodedMAC[5] = data[4];
85 return 0;
88 static int ttpci_eeprom_read_encodedMAC(struct dvb_i2c_bus *i2c, u8 * encodedMAC)
90 int ret;
91 u8 b0[] = { 0xcc };
93 struct i2c_msg msg[] = {
94 {.addr = 0x50,.flags = 0,.buf = b0,.len = 1},
95 { .addr = 0x50, .flags = I2C_M_RD, .buf = encodedMAC, .len = 20 }
98 /* dprintk("%s\n", __FUNCTION__); */
100 ret = i2c->xfer(i2c, msg, 2);
102 if (ret != 2) /* Assume EEPROM isn't there */
103 return (-ENODEV);
105 return 0;
109 int ttpci_eeprom_parse_mac(struct dvb_i2c_bus *i2c)
111 int ret, i;
112 u8 encodedMAC[20];
113 u8 decodedMAC[6];
115 ret = ttpci_eeprom_read_encodedMAC(i2c, encodedMAC);
117 if (ret != 0) { /* Will only be -ENODEV */
118 dprintk("Couldn't read from EEPROM: not there?\n");
119 memset(i2c->adapter->proposed_mac, 0, 6);
120 return ret;
123 ret = getmac_tt(decodedMAC, encodedMAC);
124 if( ret != 0 ) {
125 dprintk("%s adapter %i failed MAC signature check\n",
126 i2c->adapter->name, i2c->adapter->num);
127 dprintk("encoded MAC from EEPROM was " );
128 for(i=0; i<19; i++) {
129 dprintk( "%.2x:", encodedMAC[i]);
131 dprintk("%.2x\n", encodedMAC[19]);
132 memset(i2c->adapter->proposed_mac, 0, 6);
133 return ret;
136 memcpy(i2c->adapter->proposed_mac, decodedMAC, 6);
137 dprintk("%s adapter %i has MAC addr = %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n",
138 i2c->adapter->name, i2c->adapter->num,
139 decodedMAC[0], decodedMAC[1], decodedMAC[2],
140 decodedMAC[3], decodedMAC[4], decodedMAC[5]);
141 return 0;
144 EXPORT_SYMBOL(ttpci_eeprom_parse_mac);