staging: remove version.h includes in rtl8192e
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / rtl8192e / rtllib_crypt.c
1 /*
2  * Host AP crypto routines
3  *
4  * Copyright (c) 2002-2003, Jouni Malinen <jkmaline@cc.hut.fi>
5  * Portions Copyright (C) 2004, Intel Corporation <jketreno@linux.intel.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation. See README and COPYING for
10  * more details.
11  *
12  */
13
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/errno.h>
19
20 #include "rtllib.h"
21
22 struct rtllib_crypto_alg {
23         struct list_head list;
24         struct rtllib_crypto_ops *ops;
25 };
26
27
28 struct rtllib_crypto {
29         struct list_head algs;
30         spinlock_t lock;
31 };
32
33 static struct rtllib_crypto *hcrypt;
34
35 void rtllib_crypt_deinit_entries(struct rtllib_device *ieee,
36                                            int force)
37 {
38         struct list_head *ptr, *n;
39         struct rtllib_crypt_data *entry;
40
41         for (ptr = ieee->crypt_deinit_list.next, n = ptr->next;
42              ptr != &ieee->crypt_deinit_list; ptr = n, n = ptr->next) {
43                 entry = list_entry(ptr, struct rtllib_crypt_data, list);
44
45                 if (atomic_read(&entry->refcnt) != 0 && !force)
46                         continue;
47
48                 list_del(ptr);
49
50                 if (entry->ops)
51                         entry->ops->deinit(entry->priv);
52                 kfree(entry);
53         }
54 }
55 EXPORT_SYMBOL(rtllib_crypt_deinit_entries);
56
57 void rtllib_crypt_deinit_handler(unsigned long data)
58 {
59         struct rtllib_device *ieee = (struct rtllib_device *)data;
60         unsigned long flags;
61
62         spin_lock_irqsave(&ieee->lock, flags);
63         rtllib_crypt_deinit_entries(ieee, 0);
64         if (!list_empty(&ieee->crypt_deinit_list)) {
65                 printk(KERN_DEBUG "%s: entries remaining in delayed crypt "
66                        "deletion list\n", ieee->dev->name);
67                 ieee->crypt_deinit_timer.expires = jiffies + HZ;
68                 add_timer(&ieee->crypt_deinit_timer);
69         }
70         spin_unlock_irqrestore(&ieee->lock, flags);
71
72 }
73 EXPORT_SYMBOL(rtllib_crypt_deinit_handler);
74
75 void rtllib_crypt_delayed_deinit(struct rtllib_device *ieee,
76                                     struct rtllib_crypt_data **crypt)
77 {
78         struct rtllib_crypt_data *tmp;
79         unsigned long flags;
80
81         if (*crypt == NULL)
82                 return;
83
84         tmp = *crypt;
85         *crypt = NULL;
86
87         /* must not run ops->deinit() while there may be pending encrypt or
88          * decrypt operations. Use a list of delayed deinits to avoid needing
89          * locking. */
90
91         spin_lock_irqsave(&ieee->lock, flags);
92         list_add(&tmp->list, &ieee->crypt_deinit_list);
93         if (!timer_pending(&ieee->crypt_deinit_timer)) {
94                 ieee->crypt_deinit_timer.expires = jiffies + HZ;
95                 add_timer(&ieee->crypt_deinit_timer);
96         }
97         spin_unlock_irqrestore(&ieee->lock, flags);
98 }
99 EXPORT_SYMBOL(rtllib_crypt_delayed_deinit);
100
101 int rtllib_register_crypto_ops(struct rtllib_crypto_ops *ops)
102 {
103         unsigned long flags;
104         struct rtllib_crypto_alg *alg;
105
106         if (hcrypt == NULL)
107                 return -1;
108
109         alg = kzalloc(sizeof(*alg), GFP_KERNEL);
110         if (alg == NULL)
111                 return -ENOMEM;
112
113         alg->ops = ops;
114
115         spin_lock_irqsave(&hcrypt->lock, flags);
116         list_add(&alg->list, &hcrypt->algs);
117         spin_unlock_irqrestore(&hcrypt->lock, flags);
118
119         printk(KERN_DEBUG "rtllib_crypt: registered algorithm '%s'\n",
120                ops->name);
121
122         return 0;
123 }
124 EXPORT_SYMBOL(rtllib_register_crypto_ops);
125
126 int rtllib_unregister_crypto_ops(struct rtllib_crypto_ops *ops)
127 {
128         unsigned long flags;
129         struct list_head *ptr;
130         struct rtllib_crypto_alg *del_alg = NULL;
131
132         if (hcrypt == NULL)
133                 return -1;
134
135         spin_lock_irqsave(&hcrypt->lock, flags);
136         for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
137                 struct rtllib_crypto_alg *alg =
138                         (struct rtllib_crypto_alg *) ptr;
139                 if (alg->ops == ops) {
140                         list_del(&alg->list);
141                         del_alg = alg;
142                         break;
143                 }
144         }
145         spin_unlock_irqrestore(&hcrypt->lock, flags);
146
147         if (del_alg) {
148                 printk(KERN_DEBUG "rtllib_crypt: unregistered algorithm "
149                        "'%s'\n", ops->name);
150                 kfree(del_alg);
151         }
152
153         return del_alg ? 0 : -1;
154 }
155 EXPORT_SYMBOL(rtllib_unregister_crypto_ops);
156
157
158 struct rtllib_crypto_ops *rtllib_get_crypto_ops(const char *name)
159 {
160         unsigned long flags;
161         struct list_head *ptr;
162         struct rtllib_crypto_alg *found_alg = NULL;
163
164         if (hcrypt == NULL)
165                 return NULL;
166
167         spin_lock_irqsave(&hcrypt->lock, flags);
168         for (ptr = hcrypt->algs.next; ptr != &hcrypt->algs; ptr = ptr->next) {
169                 struct rtllib_crypto_alg *alg =
170                         (struct rtllib_crypto_alg *) ptr;
171                 if (strcmp(alg->ops->name, name) == 0) {
172                         found_alg = alg;
173                         break;
174                 }
175         }
176         spin_unlock_irqrestore(&hcrypt->lock, flags);
177
178         if (found_alg)
179                 return found_alg->ops;
180         else
181                 return NULL;
182 }
183 EXPORT_SYMBOL(rtllib_get_crypto_ops);
184
185
186 static void * rtllib_crypt_null_init(int keyidx) { return (void *) 1; }
187 static void rtllib_crypt_null_deinit(void *priv) {}
188
189 static struct rtllib_crypto_ops rtllib_crypt_null = {
190         .name                   = "NULL",
191         .init                   = rtllib_crypt_null_init,
192         .deinit                 = rtllib_crypt_null_deinit,
193         .encrypt_mpdu           = NULL,
194         .decrypt_mpdu           = NULL,
195         .encrypt_msdu           = NULL,
196         .decrypt_msdu           = NULL,
197         .set_key                = NULL,
198         .get_key                = NULL,
199         .extra_prefix_len       = 0,
200         .extra_postfix_len      = 0,
201         .owner                  = THIS_MODULE,
202 };
203
204
205 int __init rtllib_crypto_init(void)
206 {
207         int ret = -ENOMEM;
208
209         hcrypt = kzalloc(sizeof(*hcrypt), GFP_KERNEL);
210         if (!hcrypt)
211                 goto out;
212
213         INIT_LIST_HEAD(&hcrypt->algs);
214         spin_lock_init(&hcrypt->lock);
215
216         ret = rtllib_register_crypto_ops(&rtllib_crypt_null);
217         if (ret < 0) {
218                 kfree(hcrypt);
219                 hcrypt = NULL;
220         }
221 out:
222         return ret;
223 }
224
225
226 void __exit rtllib_crypto_deinit(void)
227 {
228         struct list_head *ptr, *n;
229
230         if (hcrypt == NULL)
231                 return;
232
233         for (ptr = hcrypt->algs.next, n = ptr->next; ptr != &hcrypt->algs;
234              ptr = n, n = ptr->next) {
235                 struct rtllib_crypto_alg *alg =
236                         (struct rtllib_crypto_alg *) ptr;
237                 list_del(ptr);
238                 printk(KERN_DEBUG "rtllib_crypt: unregistered algorithm "
239                        "'%s' (deinit)\n", alg->ops->name);
240                 kfree(alg);
241         }
242
243         kfree(hcrypt);
244 }
245
246 module_init(rtllib_crypto_init);
247 module_exit(rtllib_crypto_deinit);
248
249 MODULE_LICENSE("GPL");