GNU Linux-libre 4.14.266-gnu1
[releases.git] / lib / test_firmware.c
1 /*
2  * This module provides an interface to trigger and test firmware loading.
3  *
4  * It is designed to be used for basic evaluation of the firmware loading
5  * subsystem (for example when validating firmware verification). It lacks
6  * any extra dependencies, and will not normally be loaded by the system
7  * unless explicitly requested by name.
8  */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/init.h>
13 #include <linux/module.h>
14 #include <linux/printk.h>
15 #include <linux/completion.h>
16 #include <linux/firmware.h>
17 #include <linux/device.h>
18 #include <linux/fs.h>
19 #include <linux/miscdevice.h>
20 #include <linux/slab.h>
21 #include <linux/uaccess.h>
22 #include <linux/delay.h>
23 #include <linux/kthread.h>
24
25 #define TEST_FIRMWARE_NAME      "test-firmware.bin"
26 #define TEST_FIRMWARE_NUM_REQS  4
27
28 static DEFINE_MUTEX(test_fw_mutex);
29 static const struct firmware *test_firmware;
30
31 struct test_batched_req {
32         u8 idx;
33         int rc;
34         bool sent;
35         const struct firmware *fw;
36         const char *name;
37         struct completion completion;
38         struct task_struct *task;
39         struct device *dev;
40 };
41
42 /**
43  * test_config - represents configuration for the test for different triggers
44  *
45  * @name: the name of the firmware file to look for
46  * @sync_direct: when the sync trigger is used if this is true
47  *      request_firmware_direct() will be used instead.
48  * @send_uevent: whether or not to send a uevent for async requests
49  * @num_requests: number of requests to try per test case. This is trigger
50  *      specific.
51  * @reqs: stores all requests information
52  * @read_fw_idx: index of thread from which we want to read firmware results
53  *      from through the read_fw trigger.
54  * @test_result: a test may use this to collect the result from the call
55  *      of the request_firmware*() calls used in their tests. In order of
56  *      priority we always keep first any setup error. If no setup errors were
57  *      found then we move on to the first error encountered while running the
58  *      API. Note that for async calls this typically will be a successful
59  *      result (0) unless of course you've used bogus parameters, or the system
60  *      is out of memory.  In the async case the callback is expected to do a
61  *      bit more homework to figure out what happened, unfortunately the only
62  *      information passed today on error is the fact that no firmware was
63  *      found so we can only assume -ENOENT on async calls if the firmware is
64  *      NULL.
65  *
66  *      Errors you can expect:
67  *
68  *      API specific:
69  *
70  *      0:              success for sync, for async it means request was sent
71  *      -EINVAL:        invalid parameters or request
72  *      -ENOENT:        files not found
73  *
74  *      System environment:
75  *
76  *      -ENOMEM:        memory pressure on system
77  *      -ENODEV:        out of number of devices to test
78  *      -EINVAL:        an unexpected error has occurred
79  * @req_firmware: if @sync_direct is true this is set to
80  *      request_firmware_direct(), otherwise request_firmware()
81  */
82 struct test_config {
83         char *name;
84         bool sync_direct;
85         bool send_uevent;
86         u8 num_requests;
87         u8 read_fw_idx;
88
89         /*
90          * These below don't belong her but we'll move them once we create
91          * a struct fw_test_device and stuff the misc_dev under there later.
92          */
93         struct test_batched_req *reqs;
94         int test_result;
95         int (*req_firmware)(const struct firmware **fw, const char *name,
96                             struct device *device);
97 };
98
99 struct test_config *test_fw_config;
100
101 static ssize_t test_fw_misc_read(struct file *f, char __user *buf,
102                                  size_t size, loff_t *offset)
103 {
104         ssize_t rc = 0;
105
106         mutex_lock(&test_fw_mutex);
107         if (test_firmware)
108                 rc = simple_read_from_buffer(buf, size, offset,
109                                              test_firmware->data,
110                                              test_firmware->size);
111         mutex_unlock(&test_fw_mutex);
112         return rc;
113 }
114
115 static const struct file_operations test_fw_fops = {
116         .owner          = THIS_MODULE,
117         .read           = test_fw_misc_read,
118 };
119
120 static void __test_release_all_firmware(void)
121 {
122         struct test_batched_req *req;
123         u8 i;
124
125         if (!test_fw_config->reqs)
126                 return;
127
128         for (i = 0; i < test_fw_config->num_requests; i++) {
129                 req = &test_fw_config->reqs[i];
130                 if (req->fw)
131                         release_firmware(req->fw);
132         }
133
134         vfree(test_fw_config->reqs);
135         test_fw_config->reqs = NULL;
136 }
137
138 static void test_release_all_firmware(void)
139 {
140         mutex_lock(&test_fw_mutex);
141         __test_release_all_firmware();
142         mutex_unlock(&test_fw_mutex);
143 }
144
145
146 static void __test_firmware_config_free(void)
147 {
148         __test_release_all_firmware();
149         kfree_const(test_fw_config->name);
150         test_fw_config->name = NULL;
151 }
152
153 /*
154  * XXX: move to kstrncpy() once merged.
155  *
156  * Users should use kfree_const() when freeing these.
157  */
158 static int __kstrncpy(char **dst, const char *name, size_t count, gfp_t gfp)
159 {
160         *dst = kstrndup(name, count, gfp);
161         if (!*dst)
162                 return -ENOSPC;
163         return count;
164 }
165
166 static int __test_firmware_config_init(void)
167 {
168         int ret;
169
170         ret = __kstrncpy(&test_fw_config->name, TEST_FIRMWARE_NAME,
171                          strlen(TEST_FIRMWARE_NAME), GFP_KERNEL);
172         if (ret < 0)
173                 goto out;
174
175         test_fw_config->num_requests = TEST_FIRMWARE_NUM_REQS;
176         test_fw_config->send_uevent = true;
177         test_fw_config->sync_direct = false;
178         test_fw_config->req_firmware = request_firmware;
179         test_fw_config->test_result = 0;
180         test_fw_config->reqs = NULL;
181
182         return 0;
183
184 out:
185         __test_firmware_config_free();
186         return ret;
187 }
188
189 static ssize_t reset_store(struct device *dev,
190                            struct device_attribute *attr,
191                            const char *buf, size_t count)
192 {
193         int ret;
194
195         mutex_lock(&test_fw_mutex);
196
197         __test_firmware_config_free();
198
199         ret = __test_firmware_config_init();
200         if (ret < 0) {
201                 ret = -ENOMEM;
202                 pr_err("could not alloc settings for config trigger: %d\n",
203                        ret);
204                 goto out;
205         }
206
207         pr_info("reset\n");
208         ret = count;
209
210 out:
211         mutex_unlock(&test_fw_mutex);
212
213         return ret;
214 }
215 static DEVICE_ATTR_WO(reset);
216
217 static ssize_t config_show(struct device *dev,
218                            struct device_attribute *attr,
219                            char *buf)
220 {
221         int len = 0;
222
223         mutex_lock(&test_fw_mutex);
224
225         len += scnprintf(buf, PAGE_SIZE - len,
226                         "Custom trigger configuration for: %s\n",
227                         dev_name(dev));
228
229         if (test_fw_config->name)
230                 len += scnprintf(buf+len, PAGE_SIZE - len,
231                                 "name:\t%s\n",
232                                 test_fw_config->name);
233         else
234                 len += scnprintf(buf+len, PAGE_SIZE - len,
235                                 "name:\tEMTPY\n");
236
237         len += scnprintf(buf+len, PAGE_SIZE - len,
238                         "num_requests:\t%u\n", test_fw_config->num_requests);
239
240         len += scnprintf(buf+len, PAGE_SIZE - len,
241                         "send_uevent:\t\t%s\n",
242                         test_fw_config->send_uevent ?
243                         "FW_ACTION_HOTPLUG" :
244                         "FW_ACTION_NOHOTPLUG");
245         len += scnprintf(buf+len, PAGE_SIZE - len,
246                         "sync_direct:\t\t%s\n",
247                         test_fw_config->sync_direct ? "true" : "false");
248         len += scnprintf(buf+len, PAGE_SIZE - len,
249                         "read_fw_idx:\t%u\n", test_fw_config->read_fw_idx);
250
251         mutex_unlock(&test_fw_mutex);
252
253         return len;
254 }
255 static DEVICE_ATTR_RO(config);
256
257 static ssize_t config_name_store(struct device *dev,
258                                  struct device_attribute *attr,
259                                  const char *buf, size_t count)
260 {
261         int ret;
262
263         mutex_lock(&test_fw_mutex);
264         kfree_const(test_fw_config->name);
265         ret = __kstrncpy(&test_fw_config->name, buf, count, GFP_KERNEL);
266         mutex_unlock(&test_fw_mutex);
267
268         return ret;
269 }
270
271 /*
272  * As per sysfs_kf_seq_show() the buf is max PAGE_SIZE.
273  */
274 static ssize_t config_test_show_str(char *dst,
275                                     char *src)
276 {
277         int len;
278
279         mutex_lock(&test_fw_mutex);
280         len = snprintf(dst, PAGE_SIZE, "%s\n", src);
281         mutex_unlock(&test_fw_mutex);
282
283         return len;
284 }
285
286 static int test_dev_config_update_bool(const char *buf, size_t size,
287                                        bool *cfg)
288 {
289         int ret;
290
291         mutex_lock(&test_fw_mutex);
292         if (strtobool(buf, cfg) < 0)
293                 ret = -EINVAL;
294         else
295                 ret = size;
296         mutex_unlock(&test_fw_mutex);
297
298         return ret;
299 }
300
301 static ssize_t
302 test_dev_config_show_bool(char *buf,
303                           bool config)
304 {
305         bool val;
306
307         mutex_lock(&test_fw_mutex);
308         val = config;
309         mutex_unlock(&test_fw_mutex);
310
311         return snprintf(buf, PAGE_SIZE, "%d\n", val);
312 }
313
314 static ssize_t test_dev_config_show_int(char *buf, int cfg)
315 {
316         int val;
317
318         mutex_lock(&test_fw_mutex);
319         val = cfg;
320         mutex_unlock(&test_fw_mutex);
321
322         return snprintf(buf, PAGE_SIZE, "%d\n", val);
323 }
324
325 static int test_dev_config_update_u8(const char *buf, size_t size, u8 *cfg)
326 {
327         int ret;
328         long new;
329
330         ret = kstrtol(buf, 10, &new);
331         if (ret)
332                 return ret;
333
334         if (new > U8_MAX)
335                 return -EINVAL;
336
337         mutex_lock(&test_fw_mutex);
338         *(u8 *)cfg = new;
339         mutex_unlock(&test_fw_mutex);
340
341         /* Always return full write size even if we didn't consume all */
342         return size;
343 }
344
345 static ssize_t test_dev_config_show_u8(char *buf, u8 cfg)
346 {
347         u8 val;
348
349         mutex_lock(&test_fw_mutex);
350         val = cfg;
351         mutex_unlock(&test_fw_mutex);
352
353         return snprintf(buf, PAGE_SIZE, "%u\n", val);
354 }
355
356 static ssize_t config_name_show(struct device *dev,
357                                 struct device_attribute *attr,
358                                 char *buf)
359 {
360         return config_test_show_str(buf, test_fw_config->name);
361 }
362 static DEVICE_ATTR(config_name, 0644, config_name_show, config_name_store);
363
364 static ssize_t config_num_requests_store(struct device *dev,
365                                          struct device_attribute *attr,
366                                          const char *buf, size_t count)
367 {
368         int rc;
369
370         mutex_lock(&test_fw_mutex);
371         if (test_fw_config->reqs) {
372                 pr_err("Must call release_all_firmware prior to changing config\n");
373                 rc = -EINVAL;
374                 mutex_unlock(&test_fw_mutex);
375                 goto out;
376         }
377         mutex_unlock(&test_fw_mutex);
378
379         rc = test_dev_config_update_u8(buf, count,
380                                        &test_fw_config->num_requests);
381
382 out:
383         return rc;
384 }
385
386 static ssize_t config_num_requests_show(struct device *dev,
387                                         struct device_attribute *attr,
388                                         char *buf)
389 {
390         return test_dev_config_show_u8(buf, test_fw_config->num_requests);
391 }
392 static DEVICE_ATTR(config_num_requests, 0644, config_num_requests_show,
393                    config_num_requests_store);
394
395 static ssize_t config_sync_direct_store(struct device *dev,
396                                         struct device_attribute *attr,
397                                         const char *buf, size_t count)
398 {
399         int rc = test_dev_config_update_bool(buf, count,
400                                              &test_fw_config->sync_direct);
401
402         if (rc == count)
403                 test_fw_config->req_firmware = test_fw_config->sync_direct ?
404                                        request_firmware_direct :
405                                        request_firmware;
406         return rc;
407 }
408
409 static ssize_t config_sync_direct_show(struct device *dev,
410                                        struct device_attribute *attr,
411                                        char *buf)
412 {
413         return test_dev_config_show_bool(buf, test_fw_config->sync_direct);
414 }
415 static DEVICE_ATTR(config_sync_direct, 0644, config_sync_direct_show,
416                    config_sync_direct_store);
417
418 static ssize_t config_send_uevent_store(struct device *dev,
419                                         struct device_attribute *attr,
420                                         const char *buf, size_t count)
421 {
422         return test_dev_config_update_bool(buf, count,
423                                            &test_fw_config->send_uevent);
424 }
425
426 static ssize_t config_send_uevent_show(struct device *dev,
427                                        struct device_attribute *attr,
428                                        char *buf)
429 {
430         return test_dev_config_show_bool(buf, test_fw_config->send_uevent);
431 }
432 static DEVICE_ATTR(config_send_uevent, 0644, config_send_uevent_show,
433                    config_send_uevent_store);
434
435 static ssize_t config_read_fw_idx_store(struct device *dev,
436                                         struct device_attribute *attr,
437                                         const char *buf, size_t count)
438 {
439         return test_dev_config_update_u8(buf, count,
440                                          &test_fw_config->read_fw_idx);
441 }
442
443 static ssize_t config_read_fw_idx_show(struct device *dev,
444                                        struct device_attribute *attr,
445                                        char *buf)
446 {
447         return test_dev_config_show_u8(buf, test_fw_config->read_fw_idx);
448 }
449 static DEVICE_ATTR(config_read_fw_idx, 0644, config_read_fw_idx_show,
450                    config_read_fw_idx_store);
451
452
453 static ssize_t trigger_request_store(struct device *dev,
454                                      struct device_attribute *attr,
455                                      const char *buf, size_t count)
456 {
457         int rc;
458         char *name;
459
460         name = kstrndup(buf, count, GFP_KERNEL);
461         if (!name)
462                 return -ENOSPC;
463
464         pr_info("loading '%s'\n", name);
465
466         mutex_lock(&test_fw_mutex);
467         release_firmware(test_firmware);
468         test_firmware = NULL;
469         rc = request_firmware(&test_firmware, name, dev);
470         if (rc) {
471                 pr_info("load of '%s' failed: %d\n", name, rc);
472                 goto out;
473         }
474         pr_info("loaded: %zu\n", test_firmware->size);
475         rc = count;
476
477 out:
478         mutex_unlock(&test_fw_mutex);
479
480         kfree(name);
481
482         return rc;
483 }
484 static DEVICE_ATTR_WO(trigger_request);
485
486 static DECLARE_COMPLETION(async_fw_done);
487
488 static void trigger_async_request_cb(const struct firmware *fw, void *context)
489 {
490         test_firmware = fw;
491         complete(&async_fw_done);
492 }
493
494 static ssize_t trigger_async_request_store(struct device *dev,
495                                            struct device_attribute *attr,
496                                            const char *buf, size_t count)
497 {
498         int rc;
499         char *name;
500
501         name = kstrndup(buf, count, GFP_KERNEL);
502         if (!name)
503                 return -ENOSPC;
504
505         pr_info("loading '%s'\n", name);
506
507         mutex_lock(&test_fw_mutex);
508         release_firmware(test_firmware);
509         test_firmware = NULL;
510         rc = request_firmware_nowait(THIS_MODULE, 1, name, dev, GFP_KERNEL,
511                                      NULL, trigger_async_request_cb);
512         if (rc) {
513                 pr_info("async load of '%s' failed: %d\n", name, rc);
514                 kfree(name);
515                 goto out;
516         }
517         /* Free 'name' ASAP, to test for race conditions */
518         kfree(name);
519
520         wait_for_completion(&async_fw_done);
521
522         if (test_firmware) {
523                 pr_info("loaded: %zu\n", test_firmware->size);
524                 rc = count;
525         } else {
526                 pr_err("failed to async load firmware\n");
527                 rc = -ENODEV;
528         }
529
530 out:
531         mutex_unlock(&test_fw_mutex);
532
533         return rc;
534 }
535 static DEVICE_ATTR_WO(trigger_async_request);
536
537 static ssize_t trigger_custom_fallback_store(struct device *dev,
538                                              struct device_attribute *attr,
539                                              const char *buf, size_t count)
540 {
541         int rc;
542         char *name;
543
544         name = kstrndup(buf, count, GFP_KERNEL);
545         if (!name)
546                 return -ENOSPC;
547
548         pr_info("loading '%s' using custom fallback mechanism\n", name);
549
550         mutex_lock(&test_fw_mutex);
551         release_firmware(test_firmware);
552         test_firmware = NULL;
553         rc = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG, name,
554                                      dev, GFP_KERNEL, NULL,
555                                      trigger_async_request_cb);
556         if (rc) {
557                 pr_info("async load of '%s' failed: %d\n", name, rc);
558                 kfree(name);
559                 goto out;
560         }
561         /* Free 'name' ASAP, to test for race conditions */
562         kfree(name);
563
564         wait_for_completion(&async_fw_done);
565
566         if (test_firmware) {
567                 pr_info("loaded: %zu\n", test_firmware->size);
568                 rc = count;
569         } else {
570                 pr_err("failed to async load firmware\n");
571                 rc = -ENODEV;
572         }
573
574 out:
575         mutex_unlock(&test_fw_mutex);
576
577         return rc;
578 }
579 static DEVICE_ATTR_WO(trigger_custom_fallback);
580
581 static int test_fw_run_batch_request(void *data)
582 {
583         struct test_batched_req *req = data;
584
585         if (!req) {
586                 test_fw_config->test_result = -EINVAL;
587                 return -EINVAL;
588         }
589
590         req->rc = test_fw_config->req_firmware(&req->fw, req->name, req->dev);
591         if (req->rc) {
592                 pr_info("#%u: batched sync load failed: %d\n",
593                         req->idx, req->rc);
594                 if (!test_fw_config->test_result)
595                         test_fw_config->test_result = req->rc;
596         } else if (req->fw) {
597                 req->sent = true;
598                 pr_info("#%u: batched sync loaded %zu\n",
599                         req->idx, req->fw->size);
600         }
601         complete(&req->completion);
602
603         req->task = NULL;
604
605         return 0;
606 }
607
608 /*
609  * We use a kthread as otherwise the kernel serializes all our sync requests
610  * and we would not be able to mimic batched requests on a sync call. Batched
611  * requests on a sync call can for instance happen on a device driver when
612  * multiple cards are used and firmware loading happens outside of probe.
613  */
614 static ssize_t trigger_batched_requests_store(struct device *dev,
615                                               struct device_attribute *attr,
616                                               const char *buf, size_t count)
617 {
618         struct test_batched_req *req;
619         int rc;
620         u8 i;
621
622         mutex_lock(&test_fw_mutex);
623
624         test_fw_config->reqs = vzalloc(sizeof(struct test_batched_req) *
625                                        test_fw_config->num_requests * 2);
626         if (!test_fw_config->reqs) {
627                 rc = -ENOMEM;
628                 goto out_unlock;
629         }
630
631         pr_info("batched sync firmware loading '%s' %u times\n",
632                 test_fw_config->name, test_fw_config->num_requests);
633
634         for (i = 0; i < test_fw_config->num_requests; i++) {
635                 req = &test_fw_config->reqs[i];
636                 if (!req) {
637                         WARN_ON(1);
638                         rc = -ENOMEM;
639                         goto out_bail;
640                 }
641                 req->fw = NULL;
642                 req->idx = i;
643                 req->name = test_fw_config->name;
644                 req->dev = dev;
645                 init_completion(&req->completion);
646                 req->task = kthread_run(test_fw_run_batch_request, req,
647                                              "%s-%u", KBUILD_MODNAME, req->idx);
648                 if (!req->task || IS_ERR(req->task)) {
649                         pr_err("Setting up thread %u failed\n", req->idx);
650                         req->task = NULL;
651                         rc = -ENOMEM;
652                         goto out_bail;
653                 }
654         }
655
656         rc = count;
657
658         /*
659          * We require an explicit release to enable more time and delay of
660          * calling release_firmware() to improve our chances of forcing a
661          * batched request. If we instead called release_firmware() right away
662          * then we might miss on an opportunity of having a successful firmware
663          * request pass on the opportunity to be come a batched request.
664          */
665
666 out_bail:
667         for (i = 0; i < test_fw_config->num_requests; i++) {
668                 req = &test_fw_config->reqs[i];
669                 if (req->task || req->sent)
670                         wait_for_completion(&req->completion);
671         }
672
673         /* Override any worker error if we had a general setup error */
674         if (rc < 0)
675                 test_fw_config->test_result = rc;
676
677 out_unlock:
678         mutex_unlock(&test_fw_mutex);
679
680         return rc;
681 }
682 static DEVICE_ATTR_WO(trigger_batched_requests);
683
684 /*
685  * We wait for each callback to return with the lock held, no need to lock here
686  */
687 static void trigger_batched_cb(const struct firmware *fw, void *context)
688 {
689         struct test_batched_req *req = context;
690
691         if (!req) {
692                 test_fw_config->test_result = -EINVAL;
693                 return;
694         }
695
696         /* forces *some* batched requests to queue up */
697         if (!req->idx)
698                 ssleep(2);
699
700         req->fw = fw;
701
702         /*
703          * Unfortunately the firmware API gives us nothing other than a null FW
704          * if the firmware was not found on async requests.  Best we can do is
705          * just assume -ENOENT. A better API would pass the actual return
706          * value to the callback.
707          */
708         if (!fw && !test_fw_config->test_result)
709                 test_fw_config->test_result = -ENOENT;
710
711         complete(&req->completion);
712 }
713
714 static
715 ssize_t trigger_batched_requests_async_store(struct device *dev,
716                                              struct device_attribute *attr,
717                                              const char *buf, size_t count)
718 {
719         struct test_batched_req *req;
720         bool send_uevent;
721         int rc;
722         u8 i;
723
724         mutex_lock(&test_fw_mutex);
725
726         test_fw_config->reqs = vzalloc(sizeof(struct test_batched_req) *
727                                        test_fw_config->num_requests * 2);
728         if (!test_fw_config->reqs) {
729                 rc = -ENOMEM;
730                 goto out;
731         }
732
733         pr_info("batched loading '%s' custom fallback mechanism %u times\n",
734                 test_fw_config->name, test_fw_config->num_requests);
735
736         send_uevent = test_fw_config->send_uevent ? FW_ACTION_HOTPLUG :
737                 FW_ACTION_NOHOTPLUG;
738
739         for (i = 0; i < test_fw_config->num_requests; i++) {
740                 req = &test_fw_config->reqs[i];
741                 if (!req) {
742                         WARN_ON(1);
743                         goto out_bail;
744                 }
745                 req->name = test_fw_config->name;
746                 req->fw = NULL;
747                 req->idx = i;
748                 init_completion(&req->completion);
749                 rc = request_firmware_nowait(THIS_MODULE, send_uevent,
750                                              req->name,
751                                              dev, GFP_KERNEL, req,
752                                              trigger_batched_cb);
753                 if (rc) {
754                         pr_info("#%u: batched async load failed setup: %d\n",
755                                 i, rc);
756                         req->rc = rc;
757                         goto out_bail;
758                 } else
759                         req->sent = true;
760         }
761
762         rc = count;
763
764 out_bail:
765
766         /*
767          * We require an explicit release to enable more time and delay of
768          * calling release_firmware() to improve our chances of forcing a
769          * batched request. If we instead called release_firmware() right away
770          * then we might miss on an opportunity of having a successful firmware
771          * request pass on the opportunity to be come a batched request.
772          */
773
774         for (i = 0; i < test_fw_config->num_requests; i++) {
775                 req = &test_fw_config->reqs[i];
776                 if (req->sent)
777                         wait_for_completion(&req->completion);
778         }
779
780         /* Override any worker error if we had a general setup error */
781         if (rc < 0)
782                 test_fw_config->test_result = rc;
783
784 out:
785         mutex_unlock(&test_fw_mutex);
786
787         return rc;
788 }
789 static DEVICE_ATTR_WO(trigger_batched_requests_async);
790
791 static ssize_t test_result_show(struct device *dev,
792                                 struct device_attribute *attr,
793                                 char *buf)
794 {
795         return test_dev_config_show_int(buf, test_fw_config->test_result);
796 }
797 static DEVICE_ATTR_RO(test_result);
798
799 static ssize_t release_all_firmware_store(struct device *dev,
800                                           struct device_attribute *attr,
801                                           const char *buf, size_t count)
802 {
803         test_release_all_firmware();
804         return count;
805 }
806 static DEVICE_ATTR_WO(release_all_firmware);
807
808 static ssize_t read_firmware_show(struct device *dev,
809                                   struct device_attribute *attr,
810                                   char *buf)
811 {
812         struct test_batched_req *req;
813         u8 idx;
814         ssize_t rc = 0;
815
816         mutex_lock(&test_fw_mutex);
817
818         idx = test_fw_config->read_fw_idx;
819         if (idx >= test_fw_config->num_requests) {
820                 rc = -ERANGE;
821                 goto out;
822         }
823
824         if (!test_fw_config->reqs) {
825                 rc = -EINVAL;
826                 goto out;
827         }
828
829         req = &test_fw_config->reqs[idx];
830         if (!req->fw) {
831                 pr_err("#%u: failed to async load firmware\n", idx);
832                 rc = -ENOENT;
833                 goto out;
834         }
835
836         pr_info("#%u: loaded %zu\n", idx, req->fw->size);
837
838         if (req->fw->size > PAGE_SIZE) {
839                 pr_err("Testing interface must use PAGE_SIZE firmware for now\n");
840                 rc = -EINVAL;
841                 goto out;
842         }
843         memcpy(buf, req->fw->data, req->fw->size);
844
845         rc = req->fw->size;
846 out:
847         mutex_unlock(&test_fw_mutex);
848
849         return rc;
850 }
851 static DEVICE_ATTR_RO(read_firmware);
852
853 #define TEST_FW_DEV_ATTR(name)          &dev_attr_##name.attr
854
855 static struct attribute *test_dev_attrs[] = {
856         TEST_FW_DEV_ATTR(reset),
857
858         TEST_FW_DEV_ATTR(config),
859         TEST_FW_DEV_ATTR(config_name),
860         TEST_FW_DEV_ATTR(config_num_requests),
861         TEST_FW_DEV_ATTR(config_sync_direct),
862         TEST_FW_DEV_ATTR(config_send_uevent),
863         TEST_FW_DEV_ATTR(config_read_fw_idx),
864
865         /* These don't use the config at all - they could be ported! */
866         TEST_FW_DEV_ATTR(trigger_request),
867         TEST_FW_DEV_ATTR(trigger_async_request),
868         TEST_FW_DEV_ATTR(trigger_custom_fallback),
869
870         /* These use the config and can use the test_result */
871         TEST_FW_DEV_ATTR(trigger_batched_requests),
872         TEST_FW_DEV_ATTR(trigger_batched_requests_async),
873
874         TEST_FW_DEV_ATTR(release_all_firmware),
875         TEST_FW_DEV_ATTR(test_result),
876         TEST_FW_DEV_ATTR(read_firmware),
877         NULL,
878 };
879
880 ATTRIBUTE_GROUPS(test_dev);
881
882 static struct miscdevice test_fw_misc_device = {
883         .minor          = MISC_DYNAMIC_MINOR,
884         .name           = "test_firmware",
885         .fops           = &test_fw_fops,
886         .groups         = test_dev_groups,
887 };
888
889 static int __init test_firmware_init(void)
890 {
891         int rc;
892
893         test_fw_config = kzalloc(sizeof(struct test_config), GFP_KERNEL);
894         if (!test_fw_config)
895                 return -ENOMEM;
896
897         rc = __test_firmware_config_init();
898         if (rc) {
899                 kfree(test_fw_config);
900                 pr_err("could not init firmware test config: %d\n", rc);
901                 return rc;
902         }
903
904         rc = misc_register(&test_fw_misc_device);
905         if (rc) {
906                 kfree(test_fw_config);
907                 pr_err("could not register misc device: %d\n", rc);
908                 return rc;
909         }
910
911         pr_warn("interface ready\n");
912
913         return 0;
914 }
915
916 module_init(test_firmware_init);
917
918 static void __exit test_firmware_exit(void)
919 {
920         mutex_lock(&test_fw_mutex);
921         release_firmware(test_firmware);
922         misc_deregister(&test_fw_misc_device);
923         __test_firmware_config_free();
924         kfree(test_fw_config);
925         mutex_unlock(&test_fw_mutex);
926
927         pr_warn("removed interface\n");
928 }
929
930 module_exit(test_firmware_exit);
931
932 MODULE_AUTHOR("Kees Cook <keescook@chromium.org>");
933 MODULE_LICENSE("GPL");