forked from cryptotronix/atsha204-i2c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atsha204-i2c.c
760 lines (573 loc) · 20.7 KB
/
atsha204-i2c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/* -*- mode: c; c-file-style: "linux" -*- */
/*
* I2C Driver for Atmel ATSHA204 over I2C
*
* Copyright (C) 2014 Josh Datko, Cryptotronix, [email protected]
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/mutex.h>
#include <linux/kfifo.h>
#include <linux/uaccess.h>
#include <linux/crc16.h>
#include <linux/bitrev.h>
#include <linux/delay.h>
#include <linux/atomic.h>
#include <linux/printk.h>
#include "atsha204-i2c.h"
struct atsha204_chip *global_chip = NULL;
static atomic_t atsha204_avail = ATOMIC_INIT(1);
int atsha204_i2c_get_random(u8 *to_fill, const size_t max)
{
int rc;
struct atsha204_buffer recv = {0,0};
int rnd_len;
const u8 rand_cmd[] = {0x03, 0x07, 0x1b, 0x01, 0x00, 0x00, 0x27, 0x47};
rc = atsha204_i2c_transaction(global_chip, rand_cmd, sizeof(rand_cmd),
&recv);
if (sizeof(rand_cmd) == rc){
if (!atsha204_check_rsp_crc16(recv.ptr, recv.len)){
rc = -EBADMSG;
dev_err(global_chip->dev, "%s\n", "Bad CRC on Random");
}
else{
rnd_len = (max > recv.len - 3) ? recv.len - 3 : max;
memcpy(to_fill, &recv.ptr[1], rnd_len);
rc = rnd_len;
dev_info(global_chip->dev, "%s: %d\n",
"Returning randoom bytes", rc);
}
}
return rc;
}
int atsha204_i2c_transaction(struct atsha204_chip *chip,
const u8* to_send, size_t to_send_len,
struct atsha204_buffer *buf)
{
int rc;
u8 status_packet[4];
u8 *recv_buf;
int total_sleep = 60;
int packet_len;
mutex_lock(&chip->transaction_mutex);
dev_dbg(chip->dev, "%s\n", "About to send to device.");
print_hex_dump_bytes("Sending : ", DUMP_PREFIX_OFFSET,
to_send, to_send_len);
/* Begin i2c transactions */
if ((rc = atsha204_i2c_wakeup(chip->client)))
goto out;
if ((rc = i2c_master_send(chip->client, to_send, to_send_len))
!= to_send_len)
goto out;
/* Poll for the response */
while (4 != i2c_master_recv(chip->client, status_packet, 4)
&& total_sleep > 0){
total_sleep = total_sleep - 4;
msleep(4);
}
packet_len = status_packet[0];
/* The device is awake and we don't want to hit the watchdog
timer, so don't allow sleeps here*/
recv_buf = kmalloc(packet_len, GFP_ATOMIC);
memcpy(recv_buf, status_packet, sizeof(status_packet));
rc = i2c_master_recv(chip->client, recv_buf + 4, packet_len - 4);
atsha204_i2c_idle(chip->client);
/* Store the entire packet. Other functions must check the CRC
and strip of the length byte */
buf->ptr = recv_buf;
buf->len = packet_len;
dev_dbg(chip->dev, "%s\n", "Received from device.");
print_hex_dump_bytes("Received: ", DUMP_PREFIX_OFFSET,
recv_buf, packet_len);
rc = to_send_len;
out:
mutex_unlock(&chip->transaction_mutex);
return rc;
}
u16 atsha204_crc16(const u8 *buf, const u8 len)
{
u8 i;
u16 crc16 = 0;
for (i = 0; i < len; i++) {
u8 shift;
for (shift = 0x01; shift > 0x00; shift <<= 1) {
u8 data_bit = (buf[i] & shift) ? 1 : 0;
u8 crc_bit = crc16 >> 15;
crc16 <<= 1;
if ((data_bit ^ crc_bit) != 0)
crc16 ^= 0x8005;
}
}
return cpu_to_le16(crc16);
}
bool atsha204_crc16_matches(const u8 *buf, const u8 len, const u16 crc)
{
u16 crc_calc = atsha204_crc16(buf,len);
return (crc == crc_calc) ? true : false;
}
bool atsha204_check_rsp_crc16(const u8 *buf, const u8 len)
{
u16 *rec_crc = &buf[len - 2];
return atsha204_crc16_matches(buf, len - 2, cpu_to_le16(*rec_crc));
}
int atsha204_i2c_validate_rsp(const struct atsha204_buffer *packet,
struct atsha204_buffer *rsp)
{
int rc;
if (packet->len < 4)
goto out_bad_msg;
else if (atsha204_check_rsp_crc16(packet->ptr, packet->len)){
rsp->ptr = packet->ptr + 1;
rsp->len = packet->len - 3;
rc = 0;
goto out;
}
else
/* CRC failed */
out_bad_msg:
rc = -EBADMSG;
out:
return rc;
}
int atsha204_i2c_wakeup(const struct i2c_client *client)
{
bool is_awake = false;
int retval = -ENODEV;
u8 buf[4] = {0};
unsigned short int try_con = 1;
while (!is_awake){
if (4 == i2c_master_send(client, buf, 4)){
pr_debug("%s\n", "ATSHA204 Device is awake.");
is_awake = true;
if (4 == i2c_master_recv(client, buf, 4)){
pr_debug("%s", "ATSHA204 Received wakeup\n");
}
if (atsha204_check_rsp_crc16(buf,4))
retval = 0;
else
pr_err("%s\n", "ATSHA204 Wakeup CRC failure");
}
else{
/* is_awake is already false */
pr_info("Attempting Wakeup : %u\n",try_con);
if(try_con >= 10){
pr_err("Wakeup Failed. No Device");
return retval;
}
}
++try_con;
}
retval = 0;
return retval;
}
int atsha204_i2c_idle(const struct i2c_client *client)
{
int rc;
u8 idle_cmd[1] = {0x02};
rc = i2c_master_send(client, idle_cmd, 1);
return rc;
}
int atsha204_i2c_sleep(const struct i2c_client *client)
{
int retval;
char to_send[1] = {ATSHA204_SLEEP};
if ((retval = i2c_master_send(client,to_send,1)) == 1)
retval = 0;
else
pr_err("%s: 0x%x\n", "ATSHA204 failed to sleep", client->addr);
return retval;
}
void atsha204_i2c_crc_command(u8 *cmd, int len)
{
/* The command packet is:
[0x03] [Length=1 + command + CRC] [cmd] [crc]
The CRC is calculated over:
CRC([Len] [cmd]) */
int crc_data_len = len - 2 - 1;
u16 crc = atsha204_crc16(&cmd[1], crc_data_len);
cmd[len - 2] = cpu_to_le16(crc) & 0xFF;
cmd[len - 1] = cpu_to_le16(crc) >> 8;
}
int validate_write_size(const size_t count)
{
const int MIN_SIZE = 4;
/* Header and CRC occupy 4 bytes and the length is a one byte
value */
const int MAX_SIZE = 255 - 4;
int rc = -EMSGSIZE;
if (count <= MAX_SIZE && count >= MIN_SIZE)
rc = 0;
return rc;
}
ssize_t atsha204_i2c_write(struct file *filep, const char __user *buf,
size_t count, loff_t *f_pos)
{
struct atsha204_file_priv *priv = filep->private_data;
struct atsha204_chip *chip = priv->chip;
u8 *to_send;
int rc;
/* Add command byte + length + 2 byte crc */
const int SEND_SIZE = count + 4;
const u8 COMMAND_BYTE = 0x03;
if ((rc = validate_write_size(count)))
return rc;
to_send = kmalloc(SEND_SIZE, GFP_KERNEL);
if (!to_send)
return -ENOMEM;
/* Write the header */
to_send[0] = COMMAND_BYTE;
/* Length byte = user size + crc size + length byte */
to_send[1] = count + 2 + 1;
if (copy_from_user(&to_send[2], buf, count)){
rc = -EFAULT;
kfree(to_send);
return rc;
}
atsha204_i2c_crc_command(to_send, SEND_SIZE);
rc = atsha204_i2c_transaction(chip, to_send, SEND_SIZE,
&priv->buf);
/* Return to the user the number of bytes that the
user provided, don't include the extra header / crc
bytes */
if (SEND_SIZE == rc)
rc = count;
/* Reset the f_pos, which indicates the read position in the
buffer. Byte 1 points at the start of the data */
*f_pos = 1;
kfree(to_send);
return rc;
}
ssize_t atsha204_i2c_read(struct file *filep, char __user *buf, size_t count,
loff_t *f_pos)
{
struct atsha204_file_priv *priv = filep->private_data;
struct atsha204_chip *chip = priv->chip;
struct atsha204_buffer *r_buf = &priv->buf;
ssize_t rc = 0;
/* r_buf has 3 extra bytes that should not be returned to the
user. The first byte (length) and the last two (crc).
However, since f_pos is reset to 1 on write, only subtract
2 here.
*/
const int MAX_REC_LEN = r_buf->len - 2;
/* Check the CRC on the rec buffer on the first read */
if (*f_pos == 1 && !atsha204_check_rsp_crc16(r_buf->ptr, r_buf->len)){
rc = -EBADMSG;
dev_err(chip->dev, "%s\n", "CRC on received buffer failed.");
goto out;
}
if (*f_pos >= MAX_REC_LEN)
goto out;
if (*f_pos + count > MAX_REC_LEN)
count = MAX_REC_LEN - *f_pos;
if ((rc = copy_to_user(buf, &r_buf->ptr[*f_pos], count))){
rc = -EFAULT;
}
else{
*f_pos += count;
rc = count;
}
out:
return rc;
}
int atsha204_i2c_open(struct inode *inode, struct file *filep)
{
struct miscdevice *misc = filep->private_data;
struct atsha204_chip *chip = container_of(misc, struct atsha204_chip,
miscdev);
struct atsha204_file_priv *priv;
if (!atomic_dec_and_test(&atsha204_avail)){
atomic_inc(&atsha204_avail);
return -EBUSY;
}
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
if (NULL == priv)
return -ENOMEM;
priv->chip = chip;
filep->private_data = priv;
filep->f_pos = 0;
return 0;
}
int atsha204_i2c_release(struct inode *inode, struct file *filep)
{
atomic_inc(&atsha204_avail);
return 0;
}
struct atsha204_chip *atsha204_i2c_register_hardware(struct device *dev,
struct i2c_client *client)
{
struct atsha204_chip *chip;
if ((chip = kzalloc(sizeof(*chip), GFP_KERNEL)) == NULL)
goto out_null;
chip->dev_num = 0;
scnprintf(chip->devname, sizeof(chip->devname), "%s%d",
"atsha", chip->dev_num);
chip->dev = get_device(dev);
dev_set_drvdata(dev, chip);
chip->client = client;
mutex_init(&chip->transaction_mutex);
if (atsha204_i2c_add_device(chip)){
dev_err(dev, "%s\n", "Failed to add device");
goto put_device;
}
else{
int rc = hwrng_register(&atsha204_i2c_rng);
dev_dbg(dev, "%s%d\n", "HWRNG result: ", rc);
}
return chip;
put_device:
put_device(chip->dev);
kfree(chip);
out_null:
return NULL;
}
int atsha204_i2c_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
int result = -1;
struct device *dev = &client->dev;
struct atsha204_chip *chip;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
return -ENODEV;
if((result = atsha204_i2c_wakeup(client)) == 0){
pr_debug("%s: 0x%x\n", "ATSHA204 Device is awake",
client->addr);
atsha204_i2c_idle(client);
if ((chip = atsha204_i2c_register_hardware(dev, client))
== NULL){
return -ENODEV;
}
else {
global_chip = chip;
}
result = atsha204_sysfs_add_device(chip);
}
else{
pr_err("%s: 0x%x\n", "ATSHA204 device failed to wake",
client->addr);
}
return result;
}
int atsha204_i2c_remove(struct i2c_client *client)
{
struct device *dev = &(client->dev);
struct atsha204_chip *chip = dev_get_drvdata(dev);
if (chip){
misc_deregister(&chip->miscdev);
atsha204_sysfs_del_device(chip);
}
hwrng_unregister(&atsha204_i2c_rng);
kfree(chip);
global_chip = NULL;
/* The device is in an idle state, where it keeps ephemeral
* memory. Wakeup the device and sleep it, which will cause it
* to clear its internal memory */
atsha204_i2c_wakeup(client);
atsha204_i2c_sleep(client);
return 0;
}
MODULE_DEVICE_TABLE(i2c, atsha204_i2c_id);
static struct i2c_driver atsha204_i2c_driver = {
.driver = {
.name = "atsha204-i2c",
.owner = THIS_MODULE,
},
.probe = atsha204_i2c_probe,
.remove = atsha204_i2c_remove,
.id_table = atsha204_i2c_id,
};
static int __init atsha204_i2c_init(void)
{
return i2c_add_driver(&atsha204_i2c_driver);
}
static void __exit atsha204_i2c_driver_cleanup(void)
{
i2c_del_driver(&atsha204_i2c_driver);
}
module_init(atsha204_i2c_init);
module_exit(atsha204_i2c_driver_cleanup);
static const struct file_operations atsha204_i2c_fops = {
.owner = THIS_MODULE,
.llseek = no_llseek,
.open = atsha204_i2c_open,
.read = atsha204_i2c_read,
.write = atsha204_i2c_write,
.release = atsha204_i2c_release,
};
int atsha204_i2c_add_device(struct atsha204_chip *chip)
{
int retval;
chip->miscdev.fops = &atsha204_i2c_fops;
chip->miscdev.minor = MISC_DYNAMIC_MINOR;
chip->miscdev.name = chip->devname;
chip->miscdev.parent = chip->dev;
if ((retval = misc_register(&chip->miscdev)) != 0){
chip->miscdev.name = NULL;
dev_err(chip->dev,
"unable to misc_register %s, minor %d err=%d\n",
chip->miscdev.name,
chip->miscdev.minor,
retval);
}
return retval;
}
int atsha204_i2c_read4(struct atsha204_chip *chip, u8 *read_buf,
const u16 addr, const u8 param1)
{
u8 read_cmd[8] = {0};
u16 crc;
struct atsha204_buffer rsp, msg;
int rc, validate_status;
read_cmd[0] = 0x03; /* Command byte */
read_cmd[1] = 0x07; /* length */
read_cmd[2] = 0x02; /* Read command opcode */
read_cmd[3] = param1;
read_cmd[4] = cpu_to_le16(addr) & 0xFF;
read_cmd[5] = cpu_to_le16(addr) >> 8;
crc = atsha204_crc16(&read_cmd[1], 5);
read_cmd[6] = cpu_to_le16(crc) & 0xFF;
read_cmd[7] = cpu_to_le16(crc) >> 8;
rc = atsha204_i2c_transaction(chip, read_cmd,
sizeof(read_cmd), &rsp);
if (sizeof(read_cmd) == rc){
if ((validate_status = atsha204_i2c_validate_rsp(&rsp, &msg))
== 0){
memcpy(read_buf, msg.ptr, msg.len);
kfree(rsp.ptr);
rc = msg.len;
}
else
rc = validate_status;
}
return rc;
}
static ssize_t configzone_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct atsha204_chip *chip = dev_get_drvdata(dev);
int i;
u16 bytes, word_addr;
bool keep_going = true;
u8 param1 = 0; /* indicates configzone region */
char *str = buf;
u8 configzone[128] = {0};
for (bytes = 0; bytes < sizeof(configzone) && keep_going; bytes += 4){
word_addr = bytes / 4;
if (4 != atsha204_i2c_read4(chip,
&configzone[bytes],
word_addr, param1)){
keep_going = false;
}
}
for (i = 0; i < bytes; i++) {
str += sprintf(str, "%02X ", configzone[i]);
if ((i + 1) % 4 == 0)
str += sprintf(str, "\n");
}
return str - buf;
}
/*static DEVICE_ATTR_RO(configzone);*/
struct device_attribute dev_attr_configzone = __ATTR_RO(configzone);
static ssize_t serialnum_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct atsha204_chip *chip = dev_get_drvdata(dev);
int i;
u16 bytes, word_addr;
bool keep_going = true;
u8 param1 = 0; /* indicates configzone region */
char *str = buf;
u8 serial[12] = {0};
for (bytes = 0; bytes < sizeof(serial) && keep_going; bytes += 4){
word_addr = bytes / 4;
if (4 != atsha204_i2c_read4(chip,
&serial[bytes],
word_addr, param1)){
keep_going = false;
}
}
for (i = 0; i < bytes; i++) {
str += sprintf(str, "%02X", serial[i]);
if ((i + 1) % sizeof(serial) == 0)
str += sprintf(str, "\n");
}
return str - buf;
}
/*static DEVICE_ATTR_RO(configzone);*/
struct device_attribute dev_attr_serialnum = __ATTR_RO(serialnum);
static ssize_t is_locked(struct device *dev,
struct device_attribute *attr,
char *buf,
const int offset)
{
struct atsha204_chip *chip = dev_get_drvdata(dev);
const u16 LOCK_ADDR = 0x15;
u8 param1 = 0; /* indicates configzone region */
char *str = buf;
u8 lock_buf[4];
const u8 UNLOCKED = 0x55;
if (sizeof(lock_buf) == atsha204_i2c_read4(chip,
lock_buf,
LOCK_ADDR, param1)){
if (UNLOCKED == lock_buf[offset])
str += sprintf(str, "0");
else
str += sprintf(str, "1");
str += sprintf(str, "\n");
}
return str - buf;
}
static ssize_t configlocked_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
const int CONFIG_ZONE_LOCK_OFFSET = 3;
return is_locked(dev, attr, buf, CONFIG_ZONE_LOCK_OFFSET);
}
struct device_attribute dev_attr_configlocked = __ATTR_RO(configlocked);
static ssize_t datalocked_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
const int DATA_ZONE_LOCK_OFFSET = 2;
return is_locked(dev, attr, buf, DATA_ZONE_LOCK_OFFSET);
}
struct device_attribute dev_attr_datalocked = __ATTR_RO(datalocked);
static struct attribute *atsha204_dev_attrs[] = {
&dev_attr_configzone.attr,
&dev_attr_serialnum.attr,
&dev_attr_configlocked.attr,
&dev_attr_datalocked.attr,
NULL,
};
static const struct attribute_group atsha204_dev_group = {
.attrs = atsha204_dev_attrs,
};
int atsha204_sysfs_add_device(struct atsha204_chip *chip)
{
int err;
err = sysfs_create_group(&chip->dev->kobj,
&atsha204_dev_group);
if (err)
dev_err(chip->dev,
"failed to create sysfs attributes, %d\n", err);
return err;
}
void atsha204_sysfs_del_device(struct atsha204_chip *chip)
{
sysfs_remove_group(&chip->dev->kobj, &atsha204_dev_group);
}
MODULE_AUTHOR("Josh Datko <[email protected]");
MODULE_DESCRIPTION("Atmel ATSHA204 driver");
MODULE_VERSION(ATSHA204_I2C_VERSION);
MODULE_LICENSE("GPL");