dmaengine: imx-sdma: Let the core do the device node validation
[linux/fpc-iii.git] / drivers / crypto / atmel-ecc.c
blobba00e4563ca0e0dfc51ddc587b5f957b0aa7d45e
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Microchip / Atmel ECC (I2C) driver.
5 * Copyright (c) 2017, Microchip Technology Inc.
6 * Author: Tudor Ambarus <tudor.ambarus@microchip.com>
7 */
9 #include <linux/bitrev.h>
10 #include <linux/crc16.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/errno.h>
15 #include <linux/i2c.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of_device.h>
20 #include <linux/scatterlist.h>
21 #include <linux/slab.h>
22 #include <linux/workqueue.h>
23 #include <crypto/internal/kpp.h>
24 #include <crypto/ecdh.h>
25 #include <crypto/kpp.h>
26 #include "atmel-ecc.h"
28 /* Used for binding tfm objects to i2c clients. */
29 struct atmel_ecc_driver_data {
30 struct list_head i2c_client_list;
31 spinlock_t i2c_list_lock;
32 } ____cacheline_aligned;
34 static struct atmel_ecc_driver_data driver_data;
36 /**
37 * atmel_ecc_i2c_client_priv - i2c_client private data
38 * @client : pointer to i2c client device
39 * @i2c_client_list_node: part of i2c_client_list
40 * @lock : lock for sending i2c commands
41 * @wake_token : wake token array of zeros
42 * @wake_token_sz : size in bytes of the wake_token
43 * @tfm_count : number of active crypto transformations on i2c client
45 * Reads and writes from/to the i2c client are sequential. The first byte
46 * transmitted to the device is treated as the byte size. Any attempt to send
47 * more than this number of bytes will cause the device to not ACK those bytes.
48 * After the host writes a single command byte to the input buffer, reads are
49 * prohibited until after the device completes command execution. Use a mutex
50 * when sending i2c commands.
52 struct atmel_ecc_i2c_client_priv {
53 struct i2c_client *client;
54 struct list_head i2c_client_list_node;
55 struct mutex lock;
56 u8 wake_token[WAKE_TOKEN_MAX_SIZE];
57 size_t wake_token_sz;
58 atomic_t tfm_count ____cacheline_aligned;
61 /**
62 * atmel_ecdh_ctx - transformation context
63 * @client : pointer to i2c client device
64 * @fallback : used for unsupported curves or when user wants to use its own
65 * private key.
66 * @public_key : generated when calling set_secret(). It's the responsibility
67 * of the user to not call set_secret() while
68 * generate_public_key() or compute_shared_secret() are in flight.
69 * @curve_id : elliptic curve id
70 * @n_sz : size in bytes of the n prime
71 * @do_fallback: true when the device doesn't support the curve or when the user
72 * wants to use its own private key.
74 struct atmel_ecdh_ctx {
75 struct i2c_client *client;
76 struct crypto_kpp *fallback;
77 const u8 *public_key;
78 unsigned int curve_id;
79 size_t n_sz;
80 bool do_fallback;
83 /**
84 * atmel_ecc_work_data - data structure representing the work
85 * @ctx : transformation context.
86 * @cbk : pointer to a callback function to be invoked upon completion of this
87 * request. This has the form:
88 * callback(struct atmel_ecc_work_data *work_data, void *areq, u8 status)
89 * where:
90 * @work_data: data structure representing the work
91 * @areq : optional pointer to an argument passed with the original
92 * request.
93 * @status : status returned from the i2c client device or i2c error.
94 * @areq: optional pointer to a user argument for use at callback time.
95 * @work: describes the task to be executed.
96 * @cmd : structure used for communicating with the device.
98 struct atmel_ecc_work_data {
99 struct atmel_ecdh_ctx *ctx;
100 void (*cbk)(struct atmel_ecc_work_data *work_data, void *areq,
101 int status);
102 void *areq;
103 struct work_struct work;
104 struct atmel_ecc_cmd cmd;
107 static u16 atmel_ecc_crc16(u16 crc, const u8 *buffer, size_t len)
109 return cpu_to_le16(bitrev16(crc16(crc, buffer, len)));
113 * atmel_ecc_checksum() - Generate 16-bit CRC as required by ATMEL ECC.
114 * CRC16 verification of the count, opcode, param1, param2 and data bytes.
115 * The checksum is saved in little-endian format in the least significant
116 * two bytes of the command. CRC polynomial is 0x8005 and the initial register
117 * value should be zero.
119 * @cmd : structure used for communicating with the device.
121 static void atmel_ecc_checksum(struct atmel_ecc_cmd *cmd)
123 u8 *data = &cmd->count;
124 size_t len = cmd->count - CRC_SIZE;
125 u16 *crc16 = (u16 *)(data + len);
127 *crc16 = atmel_ecc_crc16(0, data, len);
130 static void atmel_ecc_init_read_cmd(struct atmel_ecc_cmd *cmd)
132 cmd->word_addr = COMMAND;
133 cmd->opcode = OPCODE_READ;
135 * Read the word from Configuration zone that contains the lock bytes
136 * (UserExtra, Selector, LockValue, LockConfig).
138 cmd->param1 = CONFIG_ZONE;
139 cmd->param2 = DEVICE_LOCK_ADDR;
140 cmd->count = READ_COUNT;
142 atmel_ecc_checksum(cmd);
144 cmd->msecs = MAX_EXEC_TIME_READ;
145 cmd->rxsize = READ_RSP_SIZE;
148 static void atmel_ecc_init_genkey_cmd(struct atmel_ecc_cmd *cmd, u16 keyid)
150 cmd->word_addr = COMMAND;
151 cmd->count = GENKEY_COUNT;
152 cmd->opcode = OPCODE_GENKEY;
153 cmd->param1 = GENKEY_MODE_PRIVATE;
154 /* a random private key will be generated and stored in slot keyID */
155 cmd->param2 = cpu_to_le16(keyid);
157 atmel_ecc_checksum(cmd);
159 cmd->msecs = MAX_EXEC_TIME_GENKEY;
160 cmd->rxsize = GENKEY_RSP_SIZE;
163 static int atmel_ecc_init_ecdh_cmd(struct atmel_ecc_cmd *cmd,
164 struct scatterlist *pubkey)
166 size_t copied;
168 cmd->word_addr = COMMAND;
169 cmd->count = ECDH_COUNT;
170 cmd->opcode = OPCODE_ECDH;
171 cmd->param1 = ECDH_PREFIX_MODE;
172 /* private key slot */
173 cmd->param2 = cpu_to_le16(DATA_SLOT_2);
176 * The device only supports NIST P256 ECC keys. The public key size will
177 * always be the same. Use a macro for the key size to avoid unnecessary
178 * computations.
180 copied = sg_copy_to_buffer(pubkey,
181 sg_nents_for_len(pubkey,
182 ATMEL_ECC_PUBKEY_SIZE),
183 cmd->data, ATMEL_ECC_PUBKEY_SIZE);
184 if (copied != ATMEL_ECC_PUBKEY_SIZE)
185 return -EINVAL;
187 atmel_ecc_checksum(cmd);
189 cmd->msecs = MAX_EXEC_TIME_ECDH;
190 cmd->rxsize = ECDH_RSP_SIZE;
192 return 0;
196 * After wake and after execution of a command, there will be error, status, or
197 * result bytes in the device's output register that can be retrieved by the
198 * system. When the length of that group is four bytes, the codes returned are
199 * detailed in error_list.
201 static int atmel_ecc_status(struct device *dev, u8 *status)
203 size_t err_list_len = ARRAY_SIZE(error_list);
204 int i;
205 u8 err_id = status[1];
207 if (*status != STATUS_SIZE)
208 return 0;
210 if (err_id == STATUS_WAKE_SUCCESSFUL || err_id == STATUS_NOERR)
211 return 0;
213 for (i = 0; i < err_list_len; i++)
214 if (error_list[i].value == err_id)
215 break;
217 /* if err_id is not in the error_list then ignore it */
218 if (i != err_list_len) {
219 dev_err(dev, "%02x: %s:\n", err_id, error_list[i].error_text);
220 return err_id;
223 return 0;
226 static int atmel_ecc_wakeup(struct i2c_client *client)
228 struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
229 u8 status[STATUS_RSP_SIZE];
230 int ret;
233 * The device ignores any levels or transitions on the SCL pin when the
234 * device is idle, asleep or during waking up. Don't check for error
235 * when waking up the device.
237 i2c_master_send(client, i2c_priv->wake_token, i2c_priv->wake_token_sz);
240 * Wait to wake the device. Typical execution times for ecdh and genkey
241 * are around tens of milliseconds. Delta is chosen to 50 microseconds.
243 usleep_range(TWHI_MIN, TWHI_MAX);
245 ret = i2c_master_recv(client, status, STATUS_SIZE);
246 if (ret < 0)
247 return ret;
249 return atmel_ecc_status(&client->dev, status);
252 static int atmel_ecc_sleep(struct i2c_client *client)
254 u8 sleep = SLEEP_TOKEN;
256 return i2c_master_send(client, &sleep, 1);
259 static void atmel_ecdh_done(struct atmel_ecc_work_data *work_data, void *areq,
260 int status)
262 struct kpp_request *req = areq;
263 struct atmel_ecdh_ctx *ctx = work_data->ctx;
264 struct atmel_ecc_cmd *cmd = &work_data->cmd;
265 size_t copied, n_sz;
267 if (status)
268 goto free_work_data;
270 /* might want less than we've got */
271 n_sz = min_t(size_t, ctx->n_sz, req->dst_len);
273 /* copy the shared secret */
274 copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst, n_sz),
275 &cmd->data[RSP_DATA_IDX], n_sz);
276 if (copied != n_sz)
277 status = -EINVAL;
279 /* fall through */
280 free_work_data:
281 kzfree(work_data);
282 kpp_request_complete(req, status);
286 * atmel_ecc_send_receive() - send a command to the device and receive its
287 * response.
288 * @client: i2c client device
289 * @cmd : structure used to communicate with the device
291 * After the device receives a Wake token, a watchdog counter starts within the
292 * device. After the watchdog timer expires, the device enters sleep mode
293 * regardless of whether some I/O transmission or command execution is in
294 * progress. If a command is attempted when insufficient time remains prior to
295 * watchdog timer execution, the device will return the watchdog timeout error
296 * code without attempting to execute the command. There is no way to reset the
297 * counter other than to put the device into sleep or idle mode and then
298 * wake it up again.
300 static int atmel_ecc_send_receive(struct i2c_client *client,
301 struct atmel_ecc_cmd *cmd)
303 struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
304 int ret;
306 mutex_lock(&i2c_priv->lock);
308 ret = atmel_ecc_wakeup(client);
309 if (ret)
310 goto err;
312 /* send the command */
313 ret = i2c_master_send(client, (u8 *)cmd, cmd->count + WORD_ADDR_SIZE);
314 if (ret < 0)
315 goto err;
317 /* delay the appropriate amount of time for command to execute */
318 msleep(cmd->msecs);
320 /* receive the response */
321 ret = i2c_master_recv(client, cmd->data, cmd->rxsize);
322 if (ret < 0)
323 goto err;
325 /* put the device into low-power mode */
326 ret = atmel_ecc_sleep(client);
327 if (ret < 0)
328 goto err;
330 mutex_unlock(&i2c_priv->lock);
331 return atmel_ecc_status(&client->dev, cmd->data);
332 err:
333 mutex_unlock(&i2c_priv->lock);
334 return ret;
337 static void atmel_ecc_work_handler(struct work_struct *work)
339 struct atmel_ecc_work_data *work_data =
340 container_of(work, struct atmel_ecc_work_data, work);
341 struct atmel_ecc_cmd *cmd = &work_data->cmd;
342 struct i2c_client *client = work_data->ctx->client;
343 int status;
345 status = atmel_ecc_send_receive(client, cmd);
346 work_data->cbk(work_data, work_data->areq, status);
349 static void atmel_ecc_enqueue(struct atmel_ecc_work_data *work_data,
350 void (*cbk)(struct atmel_ecc_work_data *work_data,
351 void *areq, int status),
352 void *areq)
354 work_data->cbk = (void *)cbk;
355 work_data->areq = areq;
357 INIT_WORK(&work_data->work, atmel_ecc_work_handler);
358 schedule_work(&work_data->work);
361 static unsigned int atmel_ecdh_supported_curve(unsigned int curve_id)
363 if (curve_id == ECC_CURVE_NIST_P256)
364 return ATMEL_ECC_NIST_P256_N_SIZE;
366 return 0;
370 * A random private key is generated and stored in the device. The device
371 * returns the pair public key.
373 static int atmel_ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
374 unsigned int len)
376 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
377 struct atmel_ecc_cmd *cmd;
378 void *public_key;
379 struct ecdh params;
380 int ret = -ENOMEM;
382 /* free the old public key, if any */
383 kfree(ctx->public_key);
384 /* make sure you don't free the old public key twice */
385 ctx->public_key = NULL;
387 if (crypto_ecdh_decode_key(buf, len, &params) < 0) {
388 dev_err(&ctx->client->dev, "crypto_ecdh_decode_key failed\n");
389 return -EINVAL;
392 ctx->n_sz = atmel_ecdh_supported_curve(params.curve_id);
393 if (!ctx->n_sz || params.key_size) {
394 /* fallback to ecdh software implementation */
395 ctx->do_fallback = true;
396 return crypto_kpp_set_secret(ctx->fallback, buf, len);
399 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
400 if (!cmd)
401 return -ENOMEM;
404 * The device only supports NIST P256 ECC keys. The public key size will
405 * always be the same. Use a macro for the key size to avoid unnecessary
406 * computations.
408 public_key = kmalloc(ATMEL_ECC_PUBKEY_SIZE, GFP_KERNEL);
409 if (!public_key)
410 goto free_cmd;
412 ctx->do_fallback = false;
413 ctx->curve_id = params.curve_id;
415 atmel_ecc_init_genkey_cmd(cmd, DATA_SLOT_2);
417 ret = atmel_ecc_send_receive(ctx->client, cmd);
418 if (ret)
419 goto free_public_key;
421 /* save the public key */
422 memcpy(public_key, &cmd->data[RSP_DATA_IDX], ATMEL_ECC_PUBKEY_SIZE);
423 ctx->public_key = public_key;
425 kfree(cmd);
426 return 0;
428 free_public_key:
429 kfree(public_key);
430 free_cmd:
431 kfree(cmd);
432 return ret;
435 static int atmel_ecdh_generate_public_key(struct kpp_request *req)
437 struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
438 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
439 size_t copied, nbytes;
440 int ret = 0;
442 if (ctx->do_fallback) {
443 kpp_request_set_tfm(req, ctx->fallback);
444 return crypto_kpp_generate_public_key(req);
447 /* might want less than we've got */
448 nbytes = min_t(size_t, ATMEL_ECC_PUBKEY_SIZE, req->dst_len);
450 /* public key was saved at private key generation */
451 copied = sg_copy_from_buffer(req->dst,
452 sg_nents_for_len(req->dst, nbytes),
453 ctx->public_key, nbytes);
454 if (copied != nbytes)
455 ret = -EINVAL;
457 return ret;
460 static int atmel_ecdh_compute_shared_secret(struct kpp_request *req)
462 struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
463 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
464 struct atmel_ecc_work_data *work_data;
465 gfp_t gfp;
466 int ret;
468 if (ctx->do_fallback) {
469 kpp_request_set_tfm(req, ctx->fallback);
470 return crypto_kpp_compute_shared_secret(req);
473 /* must have exactly two points to be on the curve */
474 if (req->src_len != ATMEL_ECC_PUBKEY_SIZE)
475 return -EINVAL;
477 gfp = (req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP) ? GFP_KERNEL :
478 GFP_ATOMIC;
480 work_data = kmalloc(sizeof(*work_data), gfp);
481 if (!work_data)
482 return -ENOMEM;
484 work_data->ctx = ctx;
486 ret = atmel_ecc_init_ecdh_cmd(&work_data->cmd, req->src);
487 if (ret)
488 goto free_work_data;
490 atmel_ecc_enqueue(work_data, atmel_ecdh_done, req);
492 return -EINPROGRESS;
494 free_work_data:
495 kfree(work_data);
496 return ret;
499 static struct i2c_client *atmel_ecc_i2c_client_alloc(void)
501 struct atmel_ecc_i2c_client_priv *i2c_priv, *min_i2c_priv = NULL;
502 struct i2c_client *client = ERR_PTR(-ENODEV);
503 int min_tfm_cnt = INT_MAX;
504 int tfm_cnt;
506 spin_lock(&driver_data.i2c_list_lock);
508 if (list_empty(&driver_data.i2c_client_list)) {
509 spin_unlock(&driver_data.i2c_list_lock);
510 return ERR_PTR(-ENODEV);
513 list_for_each_entry(i2c_priv, &driver_data.i2c_client_list,
514 i2c_client_list_node) {
515 tfm_cnt = atomic_read(&i2c_priv->tfm_count);
516 if (tfm_cnt < min_tfm_cnt) {
517 min_tfm_cnt = tfm_cnt;
518 min_i2c_priv = i2c_priv;
520 if (!min_tfm_cnt)
521 break;
524 if (min_i2c_priv) {
525 atomic_inc(&min_i2c_priv->tfm_count);
526 client = min_i2c_priv->client;
529 spin_unlock(&driver_data.i2c_list_lock);
531 return client;
534 static void atmel_ecc_i2c_client_free(struct i2c_client *client)
536 struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
538 atomic_dec(&i2c_priv->tfm_count);
541 static int atmel_ecdh_init_tfm(struct crypto_kpp *tfm)
543 const char *alg = kpp_alg_name(tfm);
544 struct crypto_kpp *fallback;
545 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
547 ctx->client = atmel_ecc_i2c_client_alloc();
548 if (IS_ERR(ctx->client)) {
549 pr_err("tfm - i2c_client binding failed\n");
550 return PTR_ERR(ctx->client);
553 fallback = crypto_alloc_kpp(alg, 0, CRYPTO_ALG_NEED_FALLBACK);
554 if (IS_ERR(fallback)) {
555 dev_err(&ctx->client->dev, "Failed to allocate transformation for '%s': %ld\n",
556 alg, PTR_ERR(fallback));
557 return PTR_ERR(fallback);
560 crypto_kpp_set_flags(fallback, crypto_kpp_get_flags(tfm));
561 ctx->fallback = fallback;
563 return 0;
566 static void atmel_ecdh_exit_tfm(struct crypto_kpp *tfm)
568 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
570 kfree(ctx->public_key);
571 crypto_free_kpp(ctx->fallback);
572 atmel_ecc_i2c_client_free(ctx->client);
575 static unsigned int atmel_ecdh_max_size(struct crypto_kpp *tfm)
577 struct atmel_ecdh_ctx *ctx = kpp_tfm_ctx(tfm);
579 if (ctx->fallback)
580 return crypto_kpp_maxsize(ctx->fallback);
583 * The device only supports NIST P256 ECC keys. The public key size will
584 * always be the same. Use a macro for the key size to avoid unnecessary
585 * computations.
587 return ATMEL_ECC_PUBKEY_SIZE;
590 static struct kpp_alg atmel_ecdh = {
591 .set_secret = atmel_ecdh_set_secret,
592 .generate_public_key = atmel_ecdh_generate_public_key,
593 .compute_shared_secret = atmel_ecdh_compute_shared_secret,
594 .init = atmel_ecdh_init_tfm,
595 .exit = atmel_ecdh_exit_tfm,
596 .max_size = atmel_ecdh_max_size,
597 .base = {
598 .cra_flags = CRYPTO_ALG_NEED_FALLBACK,
599 .cra_name = "ecdh",
600 .cra_driver_name = "atmel-ecdh",
601 .cra_priority = ATMEL_ECC_PRIORITY,
602 .cra_module = THIS_MODULE,
603 .cra_ctxsize = sizeof(struct atmel_ecdh_ctx),
607 static inline size_t atmel_ecc_wake_token_sz(u32 bus_clk_rate)
609 u32 no_of_bits = DIV_ROUND_UP(TWLO_USEC * bus_clk_rate, USEC_PER_SEC);
611 /* return the size of the wake_token in bytes */
612 return DIV_ROUND_UP(no_of_bits, 8);
615 static int device_sanity_check(struct i2c_client *client)
617 struct atmel_ecc_cmd *cmd;
618 int ret;
620 cmd = kmalloc(sizeof(*cmd), GFP_KERNEL);
621 if (!cmd)
622 return -ENOMEM;
624 atmel_ecc_init_read_cmd(cmd);
626 ret = atmel_ecc_send_receive(client, cmd);
627 if (ret)
628 goto free_cmd;
631 * It is vital that the Configuration, Data and OTP zones be locked
632 * prior to release into the field of the system containing the device.
633 * Failure to lock these zones may permit modification of any secret
634 * keys and may lead to other security problems.
636 if (cmd->data[LOCK_CONFIG_IDX] || cmd->data[LOCK_VALUE_IDX]) {
637 dev_err(&client->dev, "Configuration or Data and OTP zones are unlocked!\n");
638 ret = -ENOTSUPP;
641 /* fall through */
642 free_cmd:
643 kfree(cmd);
644 return ret;
647 static int atmel_ecc_probe(struct i2c_client *client,
648 const struct i2c_device_id *id)
650 struct atmel_ecc_i2c_client_priv *i2c_priv;
651 struct device *dev = &client->dev;
652 int ret;
653 u32 bus_clk_rate;
655 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
656 dev_err(dev, "I2C_FUNC_I2C not supported\n");
657 return -ENODEV;
660 ret = of_property_read_u32(client->adapter->dev.of_node,
661 "clock-frequency", &bus_clk_rate);
662 if (ret) {
663 dev_err(dev, "of: failed to read clock-frequency property\n");
664 return ret;
667 if (bus_clk_rate > 1000000L) {
668 dev_err(dev, "%d exceeds maximum supported clock frequency (1MHz)\n",
669 bus_clk_rate);
670 return -EINVAL;
673 i2c_priv = devm_kmalloc(dev, sizeof(*i2c_priv), GFP_KERNEL);
674 if (!i2c_priv)
675 return -ENOMEM;
677 i2c_priv->client = client;
678 mutex_init(&i2c_priv->lock);
681 * WAKE_TOKEN_MAX_SIZE was calculated for the maximum bus_clk_rate -
682 * 1MHz. The previous bus_clk_rate check ensures us that wake_token_sz
683 * will always be smaller than or equal to WAKE_TOKEN_MAX_SIZE.
685 i2c_priv->wake_token_sz = atmel_ecc_wake_token_sz(bus_clk_rate);
687 memset(i2c_priv->wake_token, 0, sizeof(i2c_priv->wake_token));
689 atomic_set(&i2c_priv->tfm_count, 0);
691 i2c_set_clientdata(client, i2c_priv);
693 ret = device_sanity_check(client);
694 if (ret)
695 return ret;
697 spin_lock(&driver_data.i2c_list_lock);
698 list_add_tail(&i2c_priv->i2c_client_list_node,
699 &driver_data.i2c_client_list);
700 spin_unlock(&driver_data.i2c_list_lock);
702 ret = crypto_register_kpp(&atmel_ecdh);
703 if (ret) {
704 spin_lock(&driver_data.i2c_list_lock);
705 list_del(&i2c_priv->i2c_client_list_node);
706 spin_unlock(&driver_data.i2c_list_lock);
708 dev_err(dev, "%s alg registration failed\n",
709 atmel_ecdh.base.cra_driver_name);
710 } else {
711 dev_info(dev, "atmel ecc algorithms registered in /proc/crypto\n");
714 return ret;
717 static int atmel_ecc_remove(struct i2c_client *client)
719 struct atmel_ecc_i2c_client_priv *i2c_priv = i2c_get_clientdata(client);
721 /* Return EBUSY if i2c client already allocated. */
722 if (atomic_read(&i2c_priv->tfm_count)) {
723 dev_err(&client->dev, "Device is busy\n");
724 return -EBUSY;
727 crypto_unregister_kpp(&atmel_ecdh);
729 spin_lock(&driver_data.i2c_list_lock);
730 list_del(&i2c_priv->i2c_client_list_node);
731 spin_unlock(&driver_data.i2c_list_lock);
733 return 0;
736 #ifdef CONFIG_OF
737 static const struct of_device_id atmel_ecc_dt_ids[] = {
739 .compatible = "atmel,atecc508a",
740 }, {
741 /* sentinel */
744 MODULE_DEVICE_TABLE(of, atmel_ecc_dt_ids);
745 #endif
747 static const struct i2c_device_id atmel_ecc_id[] = {
748 { "atecc508a", 0 },
751 MODULE_DEVICE_TABLE(i2c, atmel_ecc_id);
753 static struct i2c_driver atmel_ecc_driver = {
754 .driver = {
755 .name = "atmel-ecc",
756 .of_match_table = of_match_ptr(atmel_ecc_dt_ids),
758 .probe = atmel_ecc_probe,
759 .remove = atmel_ecc_remove,
760 .id_table = atmel_ecc_id,
763 static int __init atmel_ecc_init(void)
765 spin_lock_init(&driver_data.i2c_list_lock);
766 INIT_LIST_HEAD(&driver_data.i2c_client_list);
767 return i2c_add_driver(&atmel_ecc_driver);
770 static void __exit atmel_ecc_exit(void)
772 flush_scheduled_work();
773 i2c_del_driver(&atmel_ecc_driver);
776 module_init(atmel_ecc_init);
777 module_exit(atmel_ecc_exit);
779 MODULE_AUTHOR("Tudor Ambarus <tudor.ambarus@microchip.com>");
780 MODULE_DESCRIPTION("Microchip / Atmel ECC (I2C) driver");
781 MODULE_LICENSE("GPL v2");