Thomas Gleixner | 74ba920 | 2019-05-20 09:19:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | Retrieve encoded MAC address from 24C16 serial 2-wire EEPROM, |
| 4 | decode it and store it in the associated adapter struct for |
| 5 | use by dvb_net.c |
| 6 | |
| 7 | This card appear to have the 24C16 write protect held to ground, |
| 8 | thus permitting normal read/write operation. Theoretically it |
| 9 | would be possible to write routines to burn a different (encoded) |
| 10 | MAC address into the EEPROM. |
| 11 | |
| 12 | Robert Schlabbach GMX |
| 13 | Michael Glaum KVH Industries |
| 14 | Holger Waechtler Convergence |
| 15 | |
| 16 | Copyright (C) 2002-2003 Ralph Metzler <rjkm@metzlerbros.de> |
Michael Krufky | 50c25ff | 2006-01-09 15:25:34 -0200 | [diff] [blame] | 17 | Metzler Brothers Systementwicklung GbR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | */ |
| 21 | |
| 22 | #include <asm/errno.h> |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/i2c.h> |
Vaishali Thakkar | fa9163b | 2015-06-26 01:47:49 -0300 | [diff] [blame] | 27 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Adrian Bunk | 15ac8e6 | 2005-12-01 00:51:53 -0800 | [diff] [blame] | 29 | #include "ttpci-eeprom.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #if 1 |
| 32 | #define dprintk(x...) do { printk(x); } while (0) |
| 33 | #else |
| 34 | #define dprintk(x...) do { } while (0) |
| 35 | #endif |
| 36 | |
| 37 | |
| 38 | static int check_mac_tt(u8 *buf) |
| 39 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 40 | int i; |
| 41 | u16 tmp = 0xffff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 43 | for (i = 0; i < 8; i++) { |
| 44 | tmp = (tmp << 8) | ((tmp >> 8) ^ buf[i]); |
| 45 | tmp ^= (tmp >> 4) & 0x0f; |
| 46 | tmp ^= (tmp << 12) ^ ((tmp & 0xff) << 5); |
| 47 | } |
| 48 | tmp ^= 0xffff; |
| 49 | return (((tmp >> 8) ^ buf[8]) | ((tmp & 0xff) ^ buf[9])); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static int getmac_tt(u8 * decodedMAC, u8 * encodedMAC) |
| 53 | { |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 54 | u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6, |
| 56 | 0x1d, 0x36, 0x64, 0x78}; |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 57 | u8 data[20]; |
| 58 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | /* In case there is a sig check failure have the orig contents available */ |
| 61 | memcpy(data, encodedMAC, 20); |
| 62 | |
| 63 | for (i = 0; i < 20; i++) |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 64 | data[i] ^= xor[i]; |
| 65 | for (i = 0; i < 10; i++) |
| 66 | data[i] = ((data[2 * i + 1] << 8) | data[2 * i]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | >> ((data[2 * i + 1] >> 6) & 3); |
| 68 | |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 69 | if (check_mac_tt(data)) |
| 70 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | decodedMAC[0] = data[2]; decodedMAC[1] = data[1]; decodedMAC[2] = data[0]; |
| 73 | decodedMAC[3] = data[6]; decodedMAC[4] = data[5]; decodedMAC[5] = data[4]; |
Mauro Carvalho Chehab | 9101e62 | 2005-12-12 00:37:24 -0800 | [diff] [blame] | 74 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Igor M. Liplianin | 4e2c53f | 2011-09-23 18:33:50 -0300 | [diff] [blame] | 77 | int ttpci_eeprom_decode_mac(u8 *decodedMAC, u8 *encodedMAC) |
| 78 | { |
| 79 | u8 xor[20] = { 0x72, 0x23, 0x68, 0x19, 0x5c, 0xa8, 0x71, 0x2c, |
| 80 | 0x54, 0xd3, 0x7b, 0xf1, 0x9E, 0x23, 0x16, 0xf6, |
| 81 | 0x1d, 0x36, 0x64, 0x78}; |
| 82 | u8 data[20]; |
| 83 | int i; |
| 84 | |
| 85 | memcpy(data, encodedMAC, 20); |
| 86 | |
| 87 | for (i = 0; i < 20; i++) |
| 88 | data[i] ^= xor[i]; |
| 89 | for (i = 0; i < 10; i++) |
| 90 | data[i] = ((data[2 * i + 1] << 8) | data[2 * i]) |
| 91 | >> ((data[2 * i + 1] >> 6) & 3); |
| 92 | |
| 93 | if (check_mac_tt(data)) |
| 94 | return -ENODEV; |
| 95 | |
| 96 | decodedMAC[0] = data[2]; |
| 97 | decodedMAC[1] = data[1]; |
| 98 | decodedMAC[2] = data[0]; |
| 99 | decodedMAC[3] = data[6]; |
| 100 | decodedMAC[4] = data[5]; |
| 101 | decodedMAC[5] = data[4]; |
| 102 | return 0; |
| 103 | } |
| 104 | EXPORT_SYMBOL(ttpci_eeprom_decode_mac); |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | static int ttpci_eeprom_read_encodedMAC(struct i2c_adapter *adapter, u8 * encodedMAC) |
| 107 | { |
| 108 | int ret; |
| 109 | u8 b0[] = { 0xcc }; |
| 110 | |
| 111 | struct i2c_msg msg[] = { |
| 112 | { .addr = 0x50, .flags = 0, .buf = b0, .len = 1 }, |
| 113 | { .addr = 0x50, .flags = I2C_M_RD, .buf = encodedMAC, .len = 20 } |
| 114 | }; |
| 115 | |
Harvey Harrison | 3ca7fc8 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 116 | /* dprintk("%s\n", __func__); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | ret = i2c_transfer(adapter, msg, 2); |
| 119 | |
| 120 | if (ret != 2) /* Assume EEPROM isn't there */ |
| 121 | return (-ENODEV); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | int ttpci_eeprom_parse_mac(struct i2c_adapter *adapter, u8 *proposed_mac) |
| 128 | { |
Daniel Scheller | 2c000f4 | 2018-03-10 07:24:43 -0500 | [diff] [blame] | 129 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | u8 encodedMAC[20]; |
| 131 | u8 decodedMAC[6]; |
| 132 | |
| 133 | ret = ttpci_eeprom_read_encodedMAC(adapter, encodedMAC); |
| 134 | |
| 135 | if (ret != 0) { /* Will only be -ENODEV */ |
| 136 | dprintk("Couldn't read from EEPROM: not there?\n"); |
Vaishali Thakkar | fa9163b | 2015-06-26 01:47:49 -0300 | [diff] [blame] | 137 | eth_zero_addr(proposed_mac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | ret = getmac_tt(decodedMAC, encodedMAC); |
| 142 | if( ret != 0 ) { |
| 143 | dprintk("adapter failed MAC signature check\n"); |
Daniel Scheller | 2c000f4 | 2018-03-10 07:24:43 -0500 | [diff] [blame] | 144 | dprintk("encoded MAC from EEPROM was %*phC", |
| 145 | (int)sizeof(encodedMAC), &encodedMAC); |
Vaishali Thakkar | fa9163b | 2015-06-26 01:47:49 -0300 | [diff] [blame] | 146 | eth_zero_addr(proposed_mac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | return ret; |
| 148 | } |
| 149 | |
| 150 | memcpy(proposed_mac, decodedMAC, 6); |
Joe Perches | dd70b27 | 2015-06-14 23:01:45 -0300 | [diff] [blame] | 151 | dprintk("adapter has MAC addr = %pM\n", decodedMAC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | EXPORT_SYMBOL(ttpci_eeprom_parse_mac); |
| 156 | |
| 157 | MODULE_LICENSE("GPL"); |
| 158 | MODULE_AUTHOR("Ralph Metzler, Marcus Metzler, others"); |
Mauro Carvalho Chehab | 008e6ff | 2016-10-18 17:44:07 -0200 | [diff] [blame] | 159 | MODULE_DESCRIPTION("Decode dvb_net MAC address from EEPROM of PCI DVB cards made by Siemens, Technotrend, Hauppauge"); |