]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/subr_firmware.c
Keep firmware images on the list until they have been unregistered
[FreeBSD/FreeBSD.git] / sys / kern / subr_firmware.c
1 /*-
2  * Copyright (c) 2005, Sam Leffler <sam@errno.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/queue.h>
34 #include <sys/taskqueue.h>
35 #include <sys/systm.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/errno.h>
39 #include <sys/linker.h>
40 #include <sys/firmware.h>
41 #include <sys/proc.h>
42 #include <sys/module.h>
43
44 #define FIRMWARE_MAX    30
45 static struct firmware firmware_table[FIRMWARE_MAX];
46 struct task firmware_task;
47 struct mtx firmware_mtx;
48 MTX_SYSINIT(firmware, &firmware_mtx, "firmware table", MTX_DEF);
49
50 /*
51  * Register a firmware image with the specified name.  The
52  * image name must not already be registered.  If this is a
53  * subimage then parent refers to a previously registered
54  * image that this should be associated with.
55  */
56 struct firmware *
57 firmware_register(const char *imagename, const void *data, size_t datasize,
58     unsigned int version, struct firmware *parent)
59 {
60         struct firmware *frp = NULL;
61         int i;
62
63         mtx_lock(&firmware_mtx);
64         for (i = 0; i < FIRMWARE_MAX; i++) {
65                 struct firmware *fp = &firmware_table[i];
66
67                 if (fp->name == NULL) {
68                         if (frp == NULL)
69                                 frp = fp;
70                         continue;
71                 }
72                 if (strcasecmp(imagename, fp->name) == 0) {
73                         mtx_unlock(&firmware_mtx);
74                         printf("%s: image %s already registered!\n",
75                                 __func__, imagename);
76                         return NULL;
77                 }
78         }
79         if (frp == NULL) {
80                 mtx_unlock(&firmware_mtx);
81                 printf("%s: cannot register image %s, firmware table full!\n",
82                     __func__, imagename);
83                 return NULL;
84         }
85         frp->name = imagename;
86         frp->data = data;
87         frp->datasize = datasize;
88         frp->version = version;
89         frp->refcnt = 0;
90         frp->flags = 0;
91         if (parent != NULL)
92                 parent->refcnt++;
93         frp->parent = parent;
94         frp->file = NULL;
95         mtx_unlock(&firmware_mtx);
96         return frp;
97 }
98
99 static void
100 clearentry(struct firmware *fp)
101 {
102         KASSERT(fp->refcnt == 0, ("image %s refcnt %u", fp->name, fp->refcnt));
103         fp->name = NULL;
104         fp->file = NULL;
105         fp->data = NULL;
106         fp->datasize = 0;
107         fp->version = 0;
108         fp->flags = 0;
109         if (fp->parent != NULL) {       /* release parent reference */
110                 fp->parent->refcnt--;
111                 fp->parent = NULL;
112         }
113 }
114
115 static struct firmware *
116 lookup(const char *name)
117 {
118         int i;
119
120         for (i = 0; i < FIRMWARE_MAX; i++) {
121                 struct firmware * fp = &firmware_table[i];
122                 if (fp->name != NULL && strcasecmp(name, fp->name) == 0)
123                         return fp;
124         }
125         return NULL;
126 }
127
128 /*
129  * Unregister/remove a firmware image.  If there are outstanding
130  * references an error is returned and the image is not removed
131  * from the registry.
132  */
133 int
134 firmware_unregister(const char *imagename)
135 {
136         struct firmware *fp;
137         int refcnt = 0;
138
139         mtx_lock(&firmware_mtx);
140         /*
141          * NB: it is ok for the lookup to fail; this can happen
142          * when a module is unloaded on last reference and the
143          * module unload handler unregister's each of it's
144          * firmware images.
145          */
146         fp = lookup(imagename);
147         if (fp != NULL) {
148                 refcnt = fp->refcnt;
149                 if (refcnt == 0)
150                         clearentry(fp);
151         }
152         mtx_unlock(&firmware_mtx);
153         return (refcnt != 0 ? EBUSY : 0);
154 }
155
156 /*
157  * Lookup and potentially load the specified firmware image.
158  * If the firmware is not found in the registry attempt to
159  * load a kernel module with the image name.  If the firmware
160  * is located a reference is returned.  The caller must release
161  * this reference for the image to be eligible for removal/unload.
162  */
163 struct firmware *
164 firmware_get(const char *imagename)
165 {
166         struct thread *td;
167         struct firmware *fp;
168         linker_file_t result;
169         int requested_load = 0;
170
171 again:
172         mtx_lock(&firmware_mtx);
173         fp = lookup(imagename);
174         if (fp != NULL) {
175                 if (requested_load)
176                         fp->file = result;
177                 fp->refcnt++;
178                 mtx_unlock(&firmware_mtx);
179                 return fp;
180         }
181         /*
182          * Image not present, try to load the module holding it
183          * or if we already tried give up.
184          */
185         mtx_unlock(&firmware_mtx);
186         if (requested_load) {
187                 printf("%s: failed to load firmware image %s\n",
188                     __func__, imagename);
189                 return NULL;
190         }
191         td = curthread;
192         if (suser(td) != 0 || securelevel_gt(td->td_ucred, 0) != 0) {
193                 printf("%s: insufficient privileges to "
194                     "load firmware image %s\n", __func__, imagename);
195                 return NULL;
196         }
197         mtx_lock(&Giant);               /* XXX */
198         (void) linker_reference_module(imagename, NULL, &result);
199         mtx_unlock(&Giant);             /* XXX */
200         requested_load = 1;
201         goto again;             /* sort of an Algol-style for loop */
202 }
203
204 static void
205 unloadentry(void *unused1, int unused2)
206 {
207         struct firmware *fp;
208         linker_file_t file;
209         int i;
210
211         mtx_lock(&firmware_mtx);
212         for (;;) {
213                 /* Look for an unwanted entry that we explicitly loaded. */
214                 for (i = 0; i < FIRMWARE_MAX; i++) {
215                         fp = &firmware_table[i];
216                         if (fp->name != NULL && fp->file != NULL &&
217                             fp->refcnt == 0 &&
218                             (fp->flags & FIRMWAREFLAG_KEEPKLDREF) == 0)
219                                 break;
220                         fp = NULL;
221                 }
222                 if (fp == NULL)
223                         break;
224                 file = fp->file;
225                 /* No longer explicitly loaded. */
226                 fp->file = NULL;
227                 mtx_unlock(&firmware_mtx);
228
229                 linker_file_unload(file, LINKER_UNLOAD_NORMAL);
230
231                 mtx_lock(&firmware_mtx);
232         }
233         mtx_unlock(&firmware_mtx);
234 }
235
236 /*
237  * Release a reference to a firmware image returned by
238  * firmware_get.  The reference is released and if this is
239  * the last reference to the firmware image the associated
240  * module may be released/unloaded.
241  */
242 void
243 firmware_put(struct firmware *fp, int flags)
244 {
245         mtx_lock(&firmware_mtx);
246         fp->refcnt--;
247         if (fp->refcnt == 0) {
248                 if ((flags & FIRMWARE_UNLOAD) == 0)
249                         fp->flags |= FIRMWAREFLAG_KEEPKLDREF;
250         }
251         if (fp->file)
252                 taskqueue_enqueue(taskqueue_thread, &firmware_task);
253         mtx_unlock(&firmware_mtx);
254 }
255
256 /*
257  * Module glue.
258  */
259 static int
260 firmware_modevent(module_t mod, int type, void *unused)
261 {
262         int i;
263
264         switch (type) {
265         case MOD_LOAD:
266                 TASK_INIT(&firmware_task, 0, unloadentry, NULL);
267                 return 0;
268         case MOD_UNLOAD:
269                 for (i = 0; i < FIRMWARE_MAX; i++) {
270                         struct firmware *fp = &firmware_table[i];
271                         fp->flags &= ~FIRMWAREFLAG_KEEPKLDREF;
272                 }
273                 taskqueue_enqueue(taskqueue_thread, &firmware_task);
274                 taskqueue_drain(taskqueue_thread, &firmware_task);
275                 return 0;
276         }
277         return EINVAL;
278 }
279
280 static moduledata_t firmware_mod = {
281         "firmware",
282         firmware_modevent,
283         0
284 };
285 DECLARE_MODULE(firmware, firmware_mod, SI_SUB_DRIVERS, SI_ORDER_FIRST);
286 MODULE_VERSION(firmware, 1);