Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v6.19 94 lines 2.3 kB view raw
1// SPDX-License-Identifier: GPL-2.0 2/* 3 * CZ.NIC's Turris Omnia MCU TRNG driver 4 * 5 * 2024 by Marek Behún <kabel@kernel.org> 6 */ 7 8#include <linux/completion.h> 9#include <linux/container_of.h> 10#include <linux/errno.h> 11#include <linux/hw_random.h> 12#include <linux/i2c.h> 13#include <linux/interrupt.h> 14#include <linux/minmax.h> 15#include <linux/string.h> 16#include <linux/types.h> 17 18#include <linux/turris-omnia-mcu-interface.h> 19#include "turris-omnia-mcu.h" 20 21#define OMNIA_CMD_TRNG_MAX_ENTROPY_LEN 64 22 23static irqreturn_t omnia_trng_irq_handler(int irq, void *dev_id) 24{ 25 struct omnia_mcu *mcu = dev_id; 26 27 complete(&mcu->trng_entropy_ready); 28 29 return IRQ_HANDLED; 30} 31 32static int omnia_trng_read(struct hwrng *rng, void *data, size_t max, bool wait) 33{ 34 struct omnia_mcu *mcu = container_of(rng, struct omnia_mcu, trng); 35 u8 reply[1 + OMNIA_CMD_TRNG_MAX_ENTROPY_LEN]; 36 int err, bytes; 37 38 if (!wait && !completion_done(&mcu->trng_entropy_ready)) 39 return 0; 40 41 do { 42 if (wait_for_completion_interruptible(&mcu->trng_entropy_ready)) 43 return -ERESTARTSYS; 44 45 err = omnia_cmd_read(mcu->client, 46 OMNIA_CMD_TRNG_COLLECT_ENTROPY, 47 reply, sizeof(reply)); 48 if (err) 49 return err; 50 51 bytes = min3(reply[0], max, OMNIA_CMD_TRNG_MAX_ENTROPY_LEN); 52 } while (wait && !bytes); 53 54 memcpy(data, &reply[1], bytes); 55 56 return bytes; 57} 58 59int omnia_mcu_register_trng(struct omnia_mcu *mcu) 60{ 61 struct device *dev = &mcu->client->dev; 62 u8 dummy; 63 int err; 64 65 if (!(mcu->features & OMNIA_FEAT_TRNG)) 66 return 0; 67 68 /* 69 * If someone else cleared the TRNG interrupt but did not read the 70 * entropy, a new interrupt won't be generated, and entropy collection 71 * will be stuck. Ensure an interrupt will be generated by executing 72 * the collect entropy command (and discarding the result). 73 */ 74 err = omnia_cmd_read(mcu->client, OMNIA_CMD_TRNG_COLLECT_ENTROPY, 75 &dummy, 1); 76 if (err) 77 return err; 78 79 init_completion(&mcu->trng_entropy_ready); 80 81 err = omnia_mcu_request_irq(mcu, OMNIA_INT_TRNG, omnia_trng_irq_handler, 82 "turris-omnia-mcu-trng"); 83 if (err) 84 return dev_err_probe(dev, err, "Cannot request TRNG IRQ\n"); 85 86 mcu->trng.name = "turris-omnia-mcu-trng"; 87 mcu->trng.read = omnia_trng_read; 88 89 err = devm_hwrng_register(dev, &mcu->trng); 90 if (err) 91 return dev_err_probe(dev, err, "Cannot register TRNG\n"); 92 93 return 0; 94}