aboutsummaryrefslogtreecommitdiffstats
path: root/samples
diff options
context:
space:
mode:
Diffstat (limited to 'samples')
-rw-r--r--samples/firmware_class/firmware_sample_driver.c121
-rw-r--r--samples/firmware_class/firmware_sample_firmware_class.c204
2 files changed, 325 insertions, 0 deletions
diff --git a/samples/firmware_class/firmware_sample_driver.c b/samples/firmware_class/firmware_sample_driver.c
new file mode 100644
index 000000000000..11114f389c49
--- /dev/null
+++ b/samples/firmware_class/firmware_sample_driver.c
@@ -0,0 +1,121 @@
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#include <linux/firmware.h>
16
17static struct device ghost_device = {
18 .bus_id = "ghost0",
19};
20
21
22static void sample_firmware_load(char *firmware, int size)
23{
24 u8 buf[size+1];
25 memcpy(buf, firmware, size);
26 buf[size] = '\0';
27 printk(KERN_INFO "firmware_sample_driver: firmware: %s\n", buf);
28}
29
30static void sample_probe_default(void)
31{
32 /* uses the default method to get the firmware */
33 const struct firmware *fw_entry;
34 int retval;
35
36 printk(KERN_INFO "firmware_sample_driver: "
37 "a ghost device got inserted :)\n");
38
39 retval = request_firmware(&fw_entry, "sample_driver_fw", &ghost_device);
40 if (retval) {
41 printk(KERN_ERR
42 "firmware_sample_driver: Firmware not available\n");
43 return;
44 }
45
46 sample_firmware_load(fw_entry->data, fw_entry->size);
47
48 release_firmware(fw_entry);
49
50 /* finish setting up the device */
51}
52
53static void sample_probe_specific(void)
54{
55 int retval;
56 /* Uses some specific hotplug support to get the firmware from
57 * userspace directly into the hardware, or via some sysfs file */
58
59 /* NOTE: This currently doesn't work */
60
61 printk(KERN_INFO "firmware_sample_driver: "
62 "a ghost device got inserted :)\n");
63
64 retval = request_firmware(NULL, "sample_driver_fw", &ghost_device);
65 if (retval) {
66 printk(KERN_ERR
67 "firmware_sample_driver: Firmware load failed\n");
68 return;
69 }
70
71 /* request_firmware blocks until userspace finished, so at
72 * this point the firmware should be already in the device */
73
74 /* finish setting up the device */
75}
76
77static void sample_probe_async_cont(const struct firmware *fw, void *context)
78{
79 if (!fw) {
80 printk(KERN_ERR
81 "firmware_sample_driver: firmware load failed\n");
82 return;
83 }
84
85 printk(KERN_INFO "firmware_sample_driver: device pointer \"%s\"\n",
86 (char *)context);
87 sample_firmware_load(fw->data, fw->size);
88}
89
90static void sample_probe_async(void)
91{
92 /* Let's say that I can't sleep */
93 int error;
94 error = request_firmware_nowait(THIS_MODULE, FW_ACTION_NOHOTPLUG,
95 "sample_driver_fw", &ghost_device,
96 "my device pointer",
97 sample_probe_async_cont);
98 if (error)
99 printk(KERN_ERR "firmware_sample_driver:"
100 " request_firmware_nowait failed\n");
101}
102
103static int sample_init(void)
104{
105 device_initialize(&ghost_device);
106 /* since there is no real hardware insertion I just call the
107 * sample probe functions here */
108 sample_probe_specific();
109 sample_probe_default();
110 sample_probe_async();
111 return 0;
112}
113
114static void __exit sample_exit(void)
115{
116}
117
118module_init(sample_init);
119module_exit(sample_exit);
120
121MODULE_LICENSE("GPL");
diff --git a/samples/firmware_class/firmware_sample_firmware_class.c b/samples/firmware_class/firmware_sample_firmware_class.c
new file mode 100644
index 000000000000..9392116e47b0
--- /dev/null
+++ b/samples/firmware_class/firmware_sample_firmware_class.c
@@ -0,0 +1,204 @@
1/*
2 * firmware_sample_firmware_class.c -
3 *
4 * Copyright (c) 2003 Manuel Estrada Sainz
5 *
6 * NOTE: This is just a probe of concept, if you think that your driver would
7 * be well served by this mechanism please contact me first.
8 *
9 * DON'T USE THIS CODE AS IS
10 *
11 */
12
13#include <linux/device.h>
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/timer.h>
17#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/firmware.h>
20
21
22MODULE_AUTHOR("Manuel Estrada Sainz");
23MODULE_DESCRIPTION("Hackish sample for using firmware class directly");
24MODULE_LICENSE("GPL");
25
26static inline struct class_device *to_class_dev(struct kobject *obj)
27{
28 return container_of(obj, struct class_device, kobj);
29}
30
31static inline
32struct class_device_attribute *to_class_dev_attr(struct attribute *_attr)
33{
34 return container_of(_attr, struct class_device_attribute, attr);
35}
36
37struct firmware_priv {
38 char fw_id[FIRMWARE_NAME_MAX];
39 s32 loading:2;
40 u32 abort:1;
41};
42
43static ssize_t firmware_loading_show(struct class_device *class_dev, char *buf)
44{
45 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
46 return sprintf(buf, "%d\n", fw_priv->loading);
47}
48
49static ssize_t firmware_loading_store(struct class_device *class_dev,
50 const char *buf, size_t count)
51{
52 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
53 int prev_loading = fw_priv->loading;
54
55 fw_priv->loading = simple_strtol(buf, NULL, 10);
56
57 switch (fw_priv->loading) {
58 case -1:
59 /* abort load an panic */
60 break;
61 case 1:
62 /* setup load */
63 break;
64 case 0:
65 if (prev_loading == 1) {
66 /* finish load and get the device back to working
67 * state */
68 }
69 break;
70 }
71
72 return count;
73}
74static CLASS_DEVICE_ATTR(loading, 0644,
75 firmware_loading_show, firmware_loading_store);
76
77static ssize_t firmware_data_read(struct kobject *kobj,
78 struct bin_attribute *bin_attr,
79 char *buffer, loff_t offset, size_t count)
80{
81 struct class_device *class_dev = to_class_dev(kobj);
82 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
83
84 /* read from the devices firmware memory */
85
86 return count;
87}
88static ssize_t firmware_data_write(struct kobject *kobj,
89 struct bin_attribute *bin_attr,
90 char *buffer, loff_t offset, size_t count)
91{
92 struct class_device *class_dev = to_class_dev(kobj);
93 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
94
95 /* write to the devices firmware memory */
96
97 return count;
98}
99static struct bin_attribute firmware_attr_data = {
100 .attr = {.name = "data", .mode = 0644},
101 .size = 0,
102 .read = firmware_data_read,
103 .write = firmware_data_write,
104};
105static int fw_setup_class_device(struct class_device *class_dev,
106 const char *fw_name,
107 struct device *device)
108{
109 int retval;
110 struct firmware_priv *fw_priv;
111
112 fw_priv = kzalloc(sizeof(struct firmware_priv), GFP_KERNEL);
113 if (!fw_priv) {
114 retval = -ENOMEM;
115 goto out;
116 }
117
118 memset(class_dev, 0, sizeof(*class_dev));
119
120 strncpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
121 fw_priv->fw_id[FIRMWARE_NAME_MAX-1] = '\0';
122
123 strncpy(class_dev->class_id, device->bus_id, BUS_ID_SIZE);
124 class_dev->class_id[BUS_ID_SIZE-1] = '\0';
125 class_dev->dev = device;
126
127 class_dev->class = &firmware_class,
128 class_set_devdata(class_dev, fw_priv);
129 retval = class_device_register(class_dev);
130 if (retval) {
131 printk(KERN_ERR "%s: class_device_register failed\n",
132 __func__);
133 goto error_free_fw_priv;
134 }
135
136 retval = sysfs_create_bin_file(&class_dev->kobj, &firmware_attr_data);
137 if (retval) {
138 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
139 __func__);
140 goto error_unreg_class_dev;
141 }
142
143 retval = class_device_create_file(class_dev,
144 &class_device_attr_loading);
145 if (retval) {
146 printk(KERN_ERR "%s: class_device_create_file failed\n",
147 __func__);
148 goto error_remove_data;
149 }
150
151 goto out;
152
153error_remove_data:
154 sysfs_remove_bin_file(&class_dev->kobj, &firmware_attr_data);
155error_unreg_class_dev:
156 class_device_unregister(class_dev);
157error_free_fw_priv:
158 kfree(fw_priv);
159out:
160 return retval;
161}
162static void fw_remove_class_device(struct class_device *class_dev)
163{
164 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
165
166 class_device_remove_file(class_dev, &class_device_attr_loading);
167 sysfs_remove_bin_file(&class_dev->kobj, &firmware_attr_data);
168 class_device_unregister(class_dev);
169}
170
171static struct class_device *class_dev;
172
173static struct device my_device = {
174 .bus_id = "my_dev0",
175};
176
177static int __init firmware_sample_init(void)
178{
179 int error;
180
181 device_initialize(&my_device);
182 class_dev = kmalloc(sizeof(struct class_device), GFP_KERNEL);
183 if (!class_dev)
184 return -ENOMEM;
185
186 error = fw_setup_class_device(class_dev, "my_firmware_image",
187 &my_device);
188 if (error) {
189 kfree(class_dev);
190 return error;
191 }
192 return 0;
193
194}
195static void __exit firmware_sample_exit(void)
196{
197 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
198 fw_remove_class_device(class_dev);
199 kfree(fw_priv);
200 kfree(class_dev);
201}
202
203module_init(firmware_sample_init);
204module_exit(firmware_sample_exit);