Revert "unicode: Don't special case ignorable code points"
[linux.git] / drivers / crypto / atmel-sha204a.c
blob75bebec2c757bf9b82e67f216df661923e3549c5
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Microchip / Atmel SHA204A (I2C) driver.
5 * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
6 */
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/errno.h>
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/scatterlist.h>
17 #include <linux/slab.h>
18 #include <linux/workqueue.h>
19 #include "atmel-i2c.h"
21 static void atmel_sha204a_rng_done(struct atmel_i2c_work_data *work_data,
22 void *areq, int status)
24 struct atmel_i2c_client_priv *i2c_priv = work_data->ctx;
25 struct hwrng *rng = areq;
27 if (status)
28 dev_warn_ratelimited(&i2c_priv->client->dev,
29 "i2c transaction failed (%d)\n",
30 status);
32 rng->priv = (unsigned long)work_data;
33 atomic_dec(&i2c_priv->tfm_count);
36 static int atmel_sha204a_rng_read_nonblocking(struct hwrng *rng, void *data,
37 size_t max)
39 struct atmel_i2c_client_priv *i2c_priv;
40 struct atmel_i2c_work_data *work_data;
42 i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
44 /* keep maximum 1 asynchronous read in flight at any time */
45 if (!atomic_add_unless(&i2c_priv->tfm_count, 1, 1))
46 return 0;
48 if (rng->priv) {
49 work_data = (struct atmel_i2c_work_data *)rng->priv;
50 max = min(sizeof(work_data->cmd.data), max);
51 memcpy(data, &work_data->cmd.data, max);
52 rng->priv = 0;
53 } else {
54 work_data = kmalloc(sizeof(*work_data), GFP_ATOMIC);
55 if (!work_data)
56 return -ENOMEM;
58 work_data->ctx = i2c_priv;
59 work_data->client = i2c_priv->client;
61 max = 0;
64 atmel_i2c_init_random_cmd(&work_data->cmd);
65 atmel_i2c_enqueue(work_data, atmel_sha204a_rng_done, rng);
67 return max;
70 static int atmel_sha204a_rng_read(struct hwrng *rng, void *data, size_t max,
71 bool wait)
73 struct atmel_i2c_client_priv *i2c_priv;
74 struct atmel_i2c_cmd cmd;
75 int ret;
77 if (!wait)
78 return atmel_sha204a_rng_read_nonblocking(rng, data, max);
80 i2c_priv = container_of(rng, struct atmel_i2c_client_priv, hwrng);
82 atmel_i2c_init_random_cmd(&cmd);
84 ret = atmel_i2c_send_receive(i2c_priv->client, &cmd);
85 if (ret)
86 return ret;
88 max = min(sizeof(cmd.data), max);
89 memcpy(data, cmd.data, max);
91 return max;
94 static int atmel_sha204a_otp_read(struct i2c_client *client, u16 addr, u8 *otp)
96 struct atmel_i2c_cmd cmd;
97 int ret = -1;
99 if (atmel_i2c_init_read_otp_cmd(&cmd, addr) < 0) {
100 dev_err(&client->dev, "failed, invalid otp address %04X\n",
101 addr);
102 return ret;
105 ret = atmel_i2c_send_receive(client, &cmd);
107 if (cmd.data[0] == 0xff) {
108 dev_err(&client->dev, "failed, device not ready\n");
109 return -EINVAL;
112 memcpy(otp, cmd.data+1, 4);
114 return ret;
117 static ssize_t otp_show(struct device *dev,
118 struct device_attribute *attr, char *buf)
120 u16 addr;
121 u8 otp[OTP_ZONE_SIZE];
122 char *str = buf;
123 struct i2c_client *client = to_i2c_client(dev);
124 int i;
126 for (addr = 0; addr < OTP_ZONE_SIZE/4; addr++) {
127 if (atmel_sha204a_otp_read(client, addr, otp + addr * 4) < 0) {
128 dev_err(dev, "failed to read otp zone\n");
129 break;
133 for (i = 0; i < addr*2; i++)
134 str += sprintf(str, "%02X", otp[i]);
135 str += sprintf(str, "\n");
136 return str - buf;
138 static DEVICE_ATTR_RO(otp);
140 static struct attribute *atmel_sha204a_attrs[] = {
141 &dev_attr_otp.attr,
142 NULL
145 static const struct attribute_group atmel_sha204a_groups = {
146 .name = "atsha204a",
147 .attrs = atmel_sha204a_attrs,
150 static int atmel_sha204a_probe(struct i2c_client *client)
152 struct atmel_i2c_client_priv *i2c_priv;
153 int ret;
155 ret = atmel_i2c_probe(client);
156 if (ret)
157 return ret;
159 i2c_priv = i2c_get_clientdata(client);
161 memset(&i2c_priv->hwrng, 0, sizeof(i2c_priv->hwrng));
163 i2c_priv->hwrng.name = dev_name(&client->dev);
164 i2c_priv->hwrng.read = atmel_sha204a_rng_read;
166 ret = devm_hwrng_register(&client->dev, &i2c_priv->hwrng);
167 if (ret)
168 dev_warn(&client->dev, "failed to register RNG (%d)\n", ret);
170 /* otp read out */
171 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
172 return -ENODEV;
174 ret = sysfs_create_group(&client->dev.kobj, &atmel_sha204a_groups);
175 if (ret) {
176 dev_err(&client->dev, "failed to register sysfs entry\n");
177 return ret;
180 return ret;
183 static void atmel_sha204a_remove(struct i2c_client *client)
185 struct atmel_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
187 if (atomic_read(&i2c_priv->tfm_count)) {
188 dev_emerg(&client->dev, "Device is busy, will remove it anyhow\n");
189 return;
192 sysfs_remove_group(&client->dev.kobj, &atmel_sha204a_groups);
194 kfree((void *)i2c_priv->hwrng.priv);
197 static const struct of_device_id atmel_sha204a_dt_ids[] __maybe_unused = {
198 { .compatible = "atmel,atsha204", },
199 { .compatible = "atmel,atsha204a", },
200 { /* sentinel */ }
202 MODULE_DEVICE_TABLE(of, atmel_sha204a_dt_ids);
204 static const struct i2c_device_id atmel_sha204a_id[] = {
205 { "atsha204" },
206 { "atsha204a" },
207 { /* sentinel */ }
209 MODULE_DEVICE_TABLE(i2c, atmel_sha204a_id);
211 static struct i2c_driver atmel_sha204a_driver = {
212 .probe = atmel_sha204a_probe,
213 .remove = atmel_sha204a_remove,
214 .id_table = atmel_sha204a_id,
216 .driver.name = "atmel-sha204a",
217 .driver.of_match_table = of_match_ptr(atmel_sha204a_dt_ids),
220 static int __init atmel_sha204a_init(void)
222 return i2c_add_driver(&atmel_sha204a_driver);
225 static void __exit atmel_sha204a_exit(void)
227 atmel_i2c_flush_queue();
228 i2c_del_driver(&atmel_sha204a_driver);
231 module_init(atmel_sha204a_init);
232 module_exit(atmel_sha204a_exit);
234 MODULE_AUTHOR("Ard Biesheuvel <ard.biesheuvel@linaro.org>");
235 MODULE_DESCRIPTION("Microchip / Atmel SHA204A (I2C) driver");
236 MODULE_LICENSE("GPL v2");