ALSA: hda: Fix max PCM level to 0 dB for Fujitsu-Siemens laptops using CX20549 (Venice)
[firewire-audio.git] / crypto / algboss.c
blob412241ce4cfae25822a6beccf9360c9f52ccc89f
1 /*
2 * Create default crypto algorithm instances.
4 * Copyright (c) 2006 Herbert Xu <herbert@gondor.apana.org.au>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
13 #include <crypto/internal/aead.h>
14 #include <linux/ctype.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/kthread.h>
18 #include <linux/module.h>
19 #include <linux/notifier.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/sched.h>
22 #include <linux/string.h>
24 #include "internal.h"
26 struct cryptomgr_param {
27 struct rtattr *tb[CRYPTO_MAX_ATTRS + 2];
29 struct {
30 struct rtattr attr;
31 struct crypto_attr_type data;
32 } type;
34 union {
35 struct rtattr attr;
36 struct {
37 struct rtattr attr;
38 struct crypto_attr_alg data;
39 } alg;
40 struct {
41 struct rtattr attr;
42 struct crypto_attr_u32 data;
43 } nu32;
44 } attrs[CRYPTO_MAX_ATTRS];
46 char larval[CRYPTO_MAX_ALG_NAME];
47 char template[CRYPTO_MAX_ALG_NAME];
49 u32 otype;
50 u32 omask;
53 struct crypto_test_param {
54 char driver[CRYPTO_MAX_ALG_NAME];
55 char alg[CRYPTO_MAX_ALG_NAME];
56 u32 type;
59 static int cryptomgr_probe(void *data)
61 struct cryptomgr_param *param = data;
62 struct crypto_template *tmpl;
63 struct crypto_instance *inst;
64 int err;
66 tmpl = crypto_lookup_template(param->template);
67 if (!tmpl)
68 goto err;
70 do {
71 if (tmpl->create) {
72 err = tmpl->create(tmpl, param->tb);
73 continue;
76 inst = tmpl->alloc(param->tb);
77 if (IS_ERR(inst))
78 err = PTR_ERR(inst);
79 else if ((err = crypto_register_instance(tmpl, inst)))
80 tmpl->free(inst);
81 } while (err == -EAGAIN && !signal_pending(current));
83 crypto_tmpl_put(tmpl);
85 if (err)
86 goto err;
88 out:
89 kfree(param);
90 module_put_and_exit(0);
92 err:
93 crypto_larval_error(param->larval, param->otype, param->omask);
94 goto out;
97 static int cryptomgr_schedule_probe(struct crypto_larval *larval)
99 struct task_struct *thread;
100 struct cryptomgr_param *param;
101 const char *name = larval->alg.cra_name;
102 const char *p;
103 unsigned int len;
104 int i;
106 if (!try_module_get(THIS_MODULE))
107 goto err;
109 param = kzalloc(sizeof(*param), GFP_KERNEL);
110 if (!param)
111 goto err_put_module;
113 for (p = name; isalnum(*p) || *p == '-' || *p == '_'; p++)
116 len = p - name;
117 if (!len || *p != '(')
118 goto err_free_param;
120 memcpy(param->template, name, len);
122 i = 0;
123 for (;;) {
124 int notnum = 0;
126 name = ++p;
127 len = 0;
129 for (; isalnum(*p) || *p == '-' || *p == '_'; p++)
130 notnum |= !isdigit(*p);
132 if (*p == '(') {
133 int recursion = 0;
135 for (;;) {
136 if (!*++p)
137 goto err_free_param;
138 if (*p == '(')
139 recursion++;
140 else if (*p == ')' && !recursion--)
141 break;
144 notnum = 1;
145 p++;
148 len = p - name;
149 if (!len)
150 goto err_free_param;
152 if (notnum) {
153 param->attrs[i].alg.attr.rta_len =
154 sizeof(param->attrs[i].alg);
155 param->attrs[i].alg.attr.rta_type = CRYPTOA_ALG;
156 memcpy(param->attrs[i].alg.data.name, name, len);
157 } else {
158 param->attrs[i].nu32.attr.rta_len =
159 sizeof(param->attrs[i].nu32);
160 param->attrs[i].nu32.attr.rta_type = CRYPTOA_U32;
161 param->attrs[i].nu32.data.num =
162 simple_strtol(name, NULL, 0);
165 param->tb[i + 1] = &param->attrs[i].attr;
166 i++;
168 if (i >= CRYPTO_MAX_ATTRS)
169 goto err_free_param;
171 if (*p == ')')
172 break;
174 if (*p != ',')
175 goto err_free_param;
178 if (!i)
179 goto err_free_param;
181 param->tb[i + 1] = NULL;
183 param->type.attr.rta_len = sizeof(param->type);
184 param->type.attr.rta_type = CRYPTOA_TYPE;
185 param->type.data.type = larval->alg.cra_flags & ~CRYPTO_ALG_TESTED;
186 param->type.data.mask = larval->mask & ~CRYPTO_ALG_TESTED;
187 param->tb[0] = &param->type.attr;
189 param->otype = larval->alg.cra_flags;
190 param->omask = larval->mask;
192 memcpy(param->larval, larval->alg.cra_name, CRYPTO_MAX_ALG_NAME);
194 thread = kthread_run(cryptomgr_probe, param, "cryptomgr_probe");
195 if (IS_ERR(thread))
196 goto err_free_param;
198 return NOTIFY_STOP;
200 err_free_param:
201 kfree(param);
202 err_put_module:
203 module_put(THIS_MODULE);
204 err:
205 return NOTIFY_OK;
208 static int cryptomgr_test(void *data)
210 struct crypto_test_param *param = data;
211 u32 type = param->type;
212 int err = 0;
214 if (type & CRYPTO_ALG_TESTED)
215 goto skiptest;
217 err = alg_test(param->driver, param->alg, type, CRYPTO_ALG_TESTED);
219 skiptest:
220 crypto_alg_tested(param->driver, err);
222 kfree(param);
223 module_put_and_exit(0);
226 static int cryptomgr_schedule_test(struct crypto_alg *alg)
228 struct task_struct *thread;
229 struct crypto_test_param *param;
230 u32 type;
232 if (!try_module_get(THIS_MODULE))
233 goto err;
235 param = kzalloc(sizeof(*param), GFP_KERNEL);
236 if (!param)
237 goto err_put_module;
239 memcpy(param->driver, alg->cra_driver_name, sizeof(param->driver));
240 memcpy(param->alg, alg->cra_name, sizeof(param->alg));
241 type = alg->cra_flags;
243 /* This piece of crap needs to disappear into per-type test hooks. */
244 if ((!((type ^ CRYPTO_ALG_TYPE_BLKCIPHER) &
245 CRYPTO_ALG_TYPE_BLKCIPHER_MASK) && !(type & CRYPTO_ALG_GENIV) &&
246 ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
247 CRYPTO_ALG_TYPE_BLKCIPHER ? alg->cra_blkcipher.ivsize :
248 alg->cra_ablkcipher.ivsize)) ||
249 (!((type ^ CRYPTO_ALG_TYPE_AEAD) & CRYPTO_ALG_TYPE_MASK) &&
250 alg->cra_type == &crypto_nivaead_type && alg->cra_aead.ivsize))
251 type |= CRYPTO_ALG_TESTED;
253 param->type = type;
255 thread = kthread_run(cryptomgr_test, param, "cryptomgr_test");
256 if (IS_ERR(thread))
257 goto err_free_param;
259 return NOTIFY_STOP;
261 err_free_param:
262 kfree(param);
263 err_put_module:
264 module_put(THIS_MODULE);
265 err:
266 return NOTIFY_OK;
269 static int cryptomgr_notify(struct notifier_block *this, unsigned long msg,
270 void *data)
272 switch (msg) {
273 case CRYPTO_MSG_ALG_REQUEST:
274 return cryptomgr_schedule_probe(data);
275 case CRYPTO_MSG_ALG_REGISTER:
276 return cryptomgr_schedule_test(data);
279 return NOTIFY_DONE;
282 static struct notifier_block cryptomgr_notifier = {
283 .notifier_call = cryptomgr_notify,
286 static int __init cryptomgr_init(void)
288 return crypto_register_notifier(&cryptomgr_notifier);
291 static void __exit cryptomgr_exit(void)
293 int err = crypto_unregister_notifier(&cryptomgr_notifier);
294 BUG_ON(err);
297 subsys_initcall(cryptomgr_init);
298 module_exit(cryptomgr_exit);
300 MODULE_LICENSE("GPL");
301 MODULE_DESCRIPTION("Crypto Algorithm Manager");