import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / Documentation / firmware_class / firmware_sample_driver.c
1 /*
2  * firmware_sample_driver.c -
3  *
4  * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org>
5  *
6  * Sample code on how to use request_firmware() from drivers.
7  *
8  * Note that register_firmware() is currently useless.
9  *
10  */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/string.h>
16
17 #include "linux/firmware.h"
18
19 #define WE_CAN_NEED_FIRMWARE_BEFORE_USERSPACE_IS_AVAILABLE
20 #ifdef WE_CAN_NEED_FIRMWARE_BEFORE_USERSPACE_IS_AVAILABLE
21 char __init inkernel_firmware[] = "let's say that this is firmware\n";
22 #endif
23
24 static char ghost_device[] = "ghost0";
25
26 static void sample_firmware_load(char *firmware, int size)
27 {
28         u8 buf[size+1];
29         memcpy(buf, firmware, size);
30         buf[size] = '\0';
31         printk("firmware_sample_driver: firmware: %s\n", buf);
32 }
33
34 static void sample_probe_default(void)
35 {
36         /* uses the default method to get the firmware */
37         const struct firmware *fw_entry;
38         printk("firmware_sample_driver: a ghost device got inserted :)\n");
39
40         if(request_firmware(&fw_entry, "sample_driver_fw", ghost_device)!=0)
41         {
42                 printk(KERN_ERR
43                        "firmware_sample_driver: Firmware not available\n");
44                 return;
45         }
46         
47         sample_firmware_load(fw_entry->data, fw_entry->size);
48
49         release_firmware(fw_entry);
50
51         /* finish setting up the device */
52 }
53 static void sample_probe_specific(void)
54 {
55         /* Uses some specific hotplug support to get the firmware from
56          * userspace  directly into the hardware, or via some sysfs file */
57
58         /* NOTE: This currently doesn't work */
59
60         printk("firmware_sample_driver: a ghost device got inserted :)\n");
61
62         if(request_firmware(NULL, "sample_driver_fw", ghost_device)!=0)
63         {
64                 printk(KERN_ERR
65                        "firmware_sample_driver: Firmware load failed\n");
66                 return;
67         }
68         
69         /* request_firmware blocks until userspace finished, so at
70          * this point the firmware should be already in the device */
71
72         /* finish setting up the device */
73 }
74 static void sample_probe_async_cont(const struct firmware *fw, void *context)
75 {
76         if(!fw){
77                 printk(KERN_ERR
78                        "firmware_sample_driver: firmware load failed\n");
79                 return;
80         }
81
82         printk("firmware_sample_driver: device pointer \"%s\"\n",
83                (char *)context);
84         sample_firmware_load(fw->data, fw->size);
85 }
86 static void sample_probe_async(void)
87 {
88         /* Let's say that I can't sleep */
89         int error;
90         error = request_firmware_nowait (THIS_MODULE,
91                                          "sample_driver_fw", ghost_device,
92                                          "my device pointer",
93                                          sample_probe_async_cont);
94         if(error){
95                 printk(KERN_ERR 
96                        "firmware_sample_driver:"
97                        " request_firmware_nowait failed\n");
98         }
99 }
100
101 static int sample_init(void)
102 {
103 #ifdef WE_CAN_NEED_FIRMWARE_BEFORE_USERSPACE_IS_AVAILABLE
104         register_firmware("sample_driver_fw", inkernel_firmware,
105                           sizeof(inkernel_firmware));
106 #endif
107         /* since there is no real hardware insertion I just call the
108          * sample probe functions here */
109         sample_probe_specific();
110         sample_probe_default();
111         sample_probe_async();
112         return 0;
113 }
114 static void __exit sample_exit(void)
115 {
116 }
117
118 module_init (sample_init);
119 module_exit (sample_exit);
120
121 MODULE_LICENSE("GPL");