blob: ad3edaba4533cf33d5452e5b4bb877455000c05b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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/device.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080016#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include "linux/firmware.h"
19
20#define WE_CAN_NEED_FIRMWARE_BEFORE_USERSPACE_IS_AVAILABLE
21#ifdef WE_CAN_NEED_FIRMWARE_BEFORE_USERSPACE_IS_AVAILABLE
22char __init inkernel_firmware[] = "let's say that this is firmware\n";
23#endif
24
25static struct device ghost_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 .bus_id = "ghost0",
27};
28
29
30static void sample_firmware_load(char *firmware, int size)
31{
32 u8 buf[size+1];
33 memcpy(buf, firmware, size);
34 buf[size] = '\0';
Christophe Lucas20dd0262005-09-08 08:55:53 +020035 printk(KERN_INFO "firmware_sample_driver: firmware: %s\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
38static void sample_probe_default(void)
39{
40 /* uses the default method to get the firmware */
41 const struct firmware *fw_entry;
Christophe Lucas20dd0262005-09-08 08:55:53 +020042 printk(KERN_INFO "firmware_sample_driver: a ghost device got inserted :)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44 if(request_firmware(&fw_entry, "sample_driver_fw", &ghost_device)!=0)
45 {
46 printk(KERN_ERR
47 "firmware_sample_driver: Firmware not available\n");
48 return;
49 }
50
51 sample_firmware_load(fw_entry->data, fw_entry->size);
52
53 release_firmware(fw_entry);
54
55 /* finish setting up the device */
56}
57static void sample_probe_specific(void)
58{
59 /* Uses some specific hotplug support to get the firmware from
60 * userspace directly into the hardware, or via some sysfs file */
61
62 /* NOTE: This currently doesn't work */
63
Christophe Lucas20dd0262005-09-08 08:55:53 +020064 printk(KERN_INFO "firmware_sample_driver: a ghost device got inserted :)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66 if(request_firmware(NULL, "sample_driver_fw", &ghost_device)!=0)
67 {
68 printk(KERN_ERR
69 "firmware_sample_driver: Firmware load failed\n");
70 return;
71 }
72
73 /* request_firmware blocks until userspace finished, so at
74 * this point the firmware should be already in the device */
75
76 /* finish setting up the device */
77}
78static void sample_probe_async_cont(const struct firmware *fw, void *context)
79{
80 if(!fw){
81 printk(KERN_ERR
82 "firmware_sample_driver: firmware load failed\n");
83 return;
84 }
85
Christophe Lucas20dd0262005-09-08 08:55:53 +020086 printk(KERN_INFO "firmware_sample_driver: device pointer \"%s\"\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 (char *)context);
88 sample_firmware_load(fw->data, fw->size);
89}
90static void sample_probe_async(void)
91{
92 /* Let's say that I can't sleep */
93 int error;
Randy Dunlap06842412006-03-25 03:08:21 -080094 error = request_firmware_nowait (THIS_MODULE, FW_ACTION_NOHOTPLUG,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 "sample_driver_fw", &ghost_device,
96 "my device pointer",
97 sample_probe_async_cont);
98 if(error){
99 printk(KERN_ERR
100 "firmware_sample_driver:"
101 " request_firmware_nowait failed\n");
102 }
103}
104
105static int sample_init(void)
106{
107#ifdef WE_CAN_NEED_FIRMWARE_BEFORE_USERSPACE_IS_AVAILABLE
108 register_firmware("sample_driver_fw", inkernel_firmware,
109 sizeof(inkernel_firmware));
110#endif
111 device_initialize(&ghost_device);
112 /* since there is no real hardware insertion I just call the
113 * sample probe functions here */
114 sample_probe_specific();
115 sample_probe_default();
116 sample_probe_async();
117 return 0;
118}
119static void __exit sample_exit(void)
120{
121}
122
123module_init (sample_init);
124module_exit (sample_exit);
125
126MODULE_LICENSE("GPL");