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