blob: fba943aacf93e2a8b040996bf26c408ee737e1a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_sample_firmware_class.c -
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080017#include <linux/slab.h>
18#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/firmware.h>
20
21
Markus Rechberger87d37a42007-06-04 18:45:44 +020022MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070023MODULE_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}
30static inline
31struct class_device_attribute *to_class_dev_attr(struct attribute *_attr)
32{
33 return container_of(_attr,struct class_device_attribute,attr);
34}
35
36int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr);
37int sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr);
38
39struct firmware_priv {
40 char fw_id[FIRMWARE_NAME_MAX];
41 s32 loading:2;
42 u32 abort:1;
43};
44
45extern struct class firmware_class;
46
47static ssize_t firmware_loading_show(struct class_device *class_dev, char *buf)
48{
49 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
50 return sprintf(buf, "%d\n", fw_priv->loading);
51}
52static ssize_t firmware_loading_store(struct class_device *class_dev,
53 const char *buf, size_t count)
54{
55 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
56 int prev_loading = fw_priv->loading;
57
58 fw_priv->loading = simple_strtol(buf, NULL, 10);
59
60 switch(fw_priv->loading){
61 case -1:
62 /* abort load an panic */
63 break;
64 case 1:
65 /* setup load */
66 break;
67 case 0:
68 if(prev_loading==1){
69 /* finish load and get the device back to working
70 * state */
71 }
72 break;
73 }
74
75 return count;
76}
77static CLASS_DEVICE_ATTR(loading, 0644,
78 firmware_loading_show, firmware_loading_store);
79
80static ssize_t firmware_data_read(struct kobject *kobj,
Zhang Rui91a69022007-06-09 13:57:22 +080081 struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 char *buffer, loff_t offset, size_t count)
83{
84 struct class_device *class_dev = to_class_dev(kobj);
85 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
86
87 /* read from the devices firmware memory */
88
89 return count;
90}
91static ssize_t firmware_data_write(struct kobject *kobj,
Zhang Rui91a69022007-06-09 13:57:22 +080092 struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 char *buffer, loff_t offset, size_t count)
94{
95 struct class_device *class_dev = to_class_dev(kobj);
96 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
97
98 /* write to the devices firmware memory */
99
100 return count;
101}
102static struct bin_attribute firmware_attr_data = {
103 .attr = {.name = "data", .mode = 0644},
104 .size = 0,
105 .read = firmware_data_read,
106 .write = firmware_data_write,
107};
108static int fw_setup_class_device(struct class_device *class_dev,
109 const char *fw_name,
110 struct device *device)
111{
112 int retval = 0;
113 struct firmware_priv *fw_priv = kmalloc(sizeof(struct firmware_priv),
114 GFP_KERNEL);
115
116 if(!fw_priv){
117 retval = -ENOMEM;
118 goto out;
119 }
120 memset(fw_priv, 0, sizeof(*fw_priv));
121 memset(class_dev, 0, sizeof(*class_dev));
122
123 strncpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
124 fw_priv->fw_id[FIRMWARE_NAME_MAX-1] = '\0';
125
126 strncpy(class_dev->class_id, device->bus_id, BUS_ID_SIZE);
127 class_dev->class_id[BUS_ID_SIZE-1] = '\0';
128 class_dev->dev = device;
129
130 class_dev->class = &firmware_class,
131 class_set_devdata(class_dev, fw_priv);
132 retval = class_device_register(class_dev);
133 if (retval){
134 printk(KERN_ERR "%s: class_device_register failed\n",
135 __FUNCTION__);
136 goto error_free_fw_priv;
137 }
138
139 retval = sysfs_create_bin_file(&class_dev->kobj, &firmware_attr_data);
140 if (retval){
141 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
142 __FUNCTION__);
143 goto error_unreg_class_dev;
144 }
145
146 retval = class_device_create_file(class_dev,
147 &class_device_attr_loading);
148 if (retval){
149 printk(KERN_ERR "%s: class_device_create_file failed\n",
150 __FUNCTION__);
151 goto error_remove_data;
152 }
153
154 goto out;
155
156error_remove_data:
157 sysfs_remove_bin_file(&class_dev->kobj, &firmware_attr_data);
158error_unreg_class_dev:
159 class_device_unregister(class_dev);
160error_free_fw_priv:
161 kfree(fw_priv);
162out:
163 return retval;
164}
165static void fw_remove_class_device(struct class_device *class_dev)
166{
167 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
168
169 class_device_remove_file(class_dev, &class_device_attr_loading);
170 sysfs_remove_bin_file(&class_dev->kobj, &firmware_attr_data);
171 class_device_unregister(class_dev);
172}
173
174static struct class_device *class_dev;
175
176static struct device my_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 .bus_id = "my_dev0",
178};
179
180static int __init firmware_sample_init(void)
181{
182 int error;
183
184 device_initialize(&my_device);
185 class_dev = kmalloc(sizeof(struct class_device), GFP_KERNEL);
186 if(!class_dev)
187 return -ENOMEM;
188
189 error = fw_setup_class_device(class_dev, "my_firmware_image",
190 &my_device);
191 if(error){
192 kfree(class_dev);
193 return error;
194 }
195 return 0;
196
197}
198static void __exit firmware_sample_exit(void)
199{
200 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
201 fw_remove_class_device(class_dev);
202 kfree(fw_priv);
203 kfree(class_dev);
204}
205module_init(firmware_sample_init);
206module_exit(firmware_sample_exit);
207