/*******************************************************************************
* Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
*
*******************************************************************************
*
* DS28E16.c - DS28E16 device module. Requires low level 1-Wire connection.
*/
#define pr_fmt(fmt) "[ds28e16] %s: " fmt, __func__
#include <linux/slab.h> /* kfree() */
#include <linux/module.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/of_gpio.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/string.h>
#include "sha384_software.h"
#include "ds28e16.h"
#include "onewire_gpio.h"
#include <linux/param.h>
#include <linux/jiffies.h>
#include <linux/workqueue.h>
#include <linux/power_supply.h>
#include <linux/uaccess.h>
#include <linux/interrupt.h>
#include <linux/gpio/consumer.h>
#include <linux/regmap.h>
#include <linux/random.h>
#include <linux/sched.h>
#define ds_info pr_info
#define ds_dbg pr_debug
#define ds_err pr_err
#define ds_log pr_debug
struct ds28e16_data {
struct platform_device *pdev;
struct device *dev;
int version;
int cycle_count;
bool batt_verified;
#ifdef CONFIG_FACTORY_BUILD
bool factory_enable;
#endif
struct delayed_work battery_verify_work;
struct power_supply *verify_psy;
struct power_supply_desc verify_psy_d;
};
unsigned int attr_trytimes = 1;
unsigned char session_seed[32] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xAA, 0xAA, 0xAA, 0xAA };
unsigned char S_secret[32] = { 0x0C, 0x99, 0x2B, 0xD3, 0x95, 0xDB, 0xA0, 0xB4,
0xEF, 0x07, 0xB3, 0xD8, 0x75, 0xF3, 0xC7, 0xAE,
0xDA, 0xC4, 0x41, 0x2F, 0x48, 0x93, 0xB5, 0xD9,
0xE1, 0xE5, 0x4B, 0x20, 0x9B, 0xF3, 0x77, 0x39 };
unsigned char challenge[32] = { 0x00 };
int auth_ANON = 1;
int auth_BDCONST = 1;
int pagenumber = 0;
// maxim define
int tm = 1;
unsigned short CRC16;
const short oddparity[16] = { 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 };
unsigned char last_result_byte = RESULT_SUCCESS;
unsigned char MANID[2] = { 0x00 };
// mi add
unsigned char flag_mi_romid = 0;
unsigned char flag_mi_status = 0;
unsigned char flag_mi_page0_data = 0;
unsigned char flag_mi_page1_data = 0;
unsigned char flag_mi_counter = 0;
unsigned char flag_mi_auth_result = 0;
unsigned char mi_romid[8] = { 0x00 };
unsigned char mi_status[8] = { 0x00 };
unsigned char mi_page0_data[16] = { 0x00 };
unsigned char mi_page1_data[16] = { 0x00 };
unsigned char mi_counter[16] = { 0x00 };
int mi_auth_result = 0x00;
static void set_sched_affinity_to_current(void)
{
long ret;
int current_cpu;
preempt_disable();
current_cpu = smp_processor_id();
ret = sched_setaffinity(CURRENT_DS28E16_TASK, cpumask_of(current_cpu));
preempt_enable();
if (ret) {
pr_info("Setting cpu affinity to current cpu failed(%ld) in %s.\n",
ret, __func__);
} else {
pr_info("Setting cpu affinity to current cpu(%d) in %s.\n",
current_cpu, __func__);
}
}
static void set_sched_affinity_to_all(void)
{
long ret;
cpumask_t dstp;
cpumask_setall(&dstp);
ret = sched_setaffinity(CURRENT_DS28E16_TASK, &dstp);
if (ret) {
pr_info("Setting cpu affinity to all valid cpus failed(%ld) in %s.\n",
ret, __func__);
} else {
pr_info("Setting cpu affinity to all valid cpus in %s.\n",
__func__);
}
}
unsigned char crc_low_first(unsigned char *ptr, unsigned char len)
{
unsigned char i;
unsigned char crc = 0x00;
while (len--) {
crc ^= *ptr++;
for (i = 0; i < 8; ++i) {
if (crc & 0x01)
crc = (crc >> 1) ^ 0x8c;
else
crc = (crc >> 1);
}
}
return (crc);
}
short Read_RomID(unsigned char *RomID)
{
unsigned char i;
unsigned char crc = 0x00;
/*if (flag_mi_romid == 2) {
memcpy(RomID, mi_romid, 8);
return DS_TRUE;
}*/
if ((ow_reset()) != 0) {
ds_err("Failed to reset ds28e16!\n");
return ERROR_NO_DEVICE;
}
ds_dbg("Ready to write 0x33 to maxim IC!\n");
write_byte(CMD_READ_ROM);
Delay_us(10);
for (i = 0; i < 8; i++)
RomID[i] = read_byte();
ds_info("RomID = %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n", RomID[0],
RomID[1], RomID[2], RomID[3], RomID[4], RomID[5], RomID[6],
RomID[7]);
crc = crc_low_first(RomID, 7);
ds_dbg("crc_low_first = %02x\n", crc);
if (crc == RomID[7]) {
if (flag_mi_status == 0)
flag_mi_romid = 1;
else
flag_mi_romid = 2;
memcpy(mi_romid, RomID, 8);
return DS_TRUE;
} else {
return DS_FALSE;
}
}
static int ds28el16_Read_RomID_retry(unsigned char *RomID)
{
int i;
set_sched_affinity_to_current();
for (i = 0; i < GET_ROM_ID_RETRY; i++) {
ds_info("read rom id communication start %d...\n", i);
if (Read_RomID(RomID) == DS_TRUE) {
set_sched_affinity_to_all();
return DS_TRUE;
}
}
set_sched_affinity_to_all();
return DS_FALSE;
}
static int ds28el16_get_page_status_retry(unsigned char *data)
{
int i;
set_sched_affinity_to_current();
for (i = 0; i < GET_BLOCK_STATUS_RETRY; i++) {
ds_info("read page status communication start... %d\n", i);
if (DS28E16_cmd_readStatus(data) == DS_TRUE) {
set_sched_affinity_to_all();
return DS_TRUE;
}
}
set_sched_affinity_to_all();
return DS_FALSE;
}
static int ds28el16_get_page_data_retry(int page, unsigned char *data)
{
int i;
if (page >= MAX_PAGENUM)
return DS_FALSE;
set_sched_affinity_to_current();
for (i = 0; i < GET_USER_MEMORY_RETRY; i++) {
ds_dbg("read page data communication start... %d\n", i);
if (DS28E16_cmd_readMemory(page, data) == DS_TRUE) {
set_sched_affinity_to_all();
return DS_TRUE;
}
}
set_sched_affinity_to_all();
return DS_FALSE;
}
static int DS28E16_cmd_computeS_Secret_retry(int anon, int bdconst, int pg,
unsigned char *partial)
{
int i;
if (pg >= MAX_PAGENUM)
return DS_FALSE;
for (i = 0; i < GET_S_SECRET_RETRY; i++) {
if (DS28E16_cmd_computeS_Secret(anon, bdconst, pg, partial) ==
DS_TRUE)
return DS_TRUE;
}
return DS_FALSE;
}
static int DS28E16_cmd_computeReadPageAuthentication_retry(
int anon, int pg, unsigned char *challenge, unsigned char *hmac)
{
int i;
if (pg >= MAX_PAGENUM)
return DS_FALSE;
for (i = 0; i < GET_MAC_RETRY; i++) {
if (DS28E16_cmd_computeReadPageAuthentication(
anon, pg, challenge, hmac) == DS_TRUE)
return DS_TRUE;
}
return DS_FALSE;
}
unsigned short docrc16(unsigned short data)
{
data = (data ^ (CRC16 & 0xff)) & 0xff;
CRC16 >>= 8;
if (oddparity[data & 0xf] ^ oddparity[data >> 4])
CRC16 ^= 0xc001;
data <<= 6;
CRC16 ^= data;
data <<= 1;
CRC16 ^= data;
return CRC16;
}
int DS28E16_standard_cmd_flow(unsigned char *write_buf, int delay_ms,
unsigned char *read_buf, int *read_len,
int write_len)
{
unsigned char buf[128];
int i;
int buf_len = 0;
int expected_read_len = *read_len;
//NEW FLOW
/*'1 Wire
'''''''''''''''''''''''
?<Reset/Presence>
?<ROM level command Sequence>
?TX: Start Command
?TX: Length Byte
?TX: Memory Command
?TX: Parameter, TX: Data
?RX: CRC16
?TX: Release Byte
?<Strong pull-up Delay>
?RX: Dummy Byte
?RX: Length Byte
?RX: Result byte
?RX: CRC16
?< wait for reset>
'''''''''''''''''''''''*/
if ((ow_reset()) != 0) {
ds_err("Failed to reset ds28e16!\n");
goto final_reset;
}
write_byte(CMD_SKIP_ROM);
buf[buf_len++] = CMD_START;
memcpy(&buf[buf_len], write_buf, write_len);
buf_len += write_len;
for (i = 0; i < buf_len; i++)
write_byte(buf[i]);
buf[buf_len++] = read_byte();
buf[buf_len++] = read_byte();
CRC16 = 0;
for (i = 0; i < buf_len; i++)
docrc16(buf[i]);
if (CRC16 != 0xB001)
return DS_FALSE;
// check for strong pull-up
if (delay_ms > 0) {
write_byte(CMD_RELEASE_BYTE);
Delay_us(1000 * delay_ms);
}
read_byte();
*read_len = read_byte();
if (expected_read_len != *read_len)
return DS_FALSE;
buf_len = *read_len + 2;
for (i = 0; i < buf_len; i+
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
maxim.7z (14个子文件)
maxim
ucl_retdefs.h 6KB
Makefile 267B
Kconfig 572B
ucl_sha3.c 12KB
ds28e16.c 48KB
sha384_software.c 3KB
max77932.c 7KB
ds28e16.h 4KB
onewire_gpio.c 14KB
max28200.c 30KB
sha384_software.h 2KB
onewire_gpio.h 213B
ucl_hash.h 2KB
ucl_sha3.h 7KB
共 14 条
- 1
资源评论
hellworl
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功