[PATCH] hwmon: hwmon vs i2c, second round (05/11)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / Documentation / firmware_class / firmware_sample_firmware_class.c
blob09eab2f1b3735f0f9ff6feb193bba7b70c72130f
1 /*
2 * firmware_sample_firmware_class.c -
4 * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org>
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.
9 * DON'T USE THIS CODE AS IS
13 #include <linux/device.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/timer.h>
17 #include <linux/firmware.h>
20 MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
21 MODULE_DESCRIPTION("Hackish sample for using firmware class directly");
22 MODULE_LICENSE("GPL");
24 static inline struct class_device *to_class_dev(struct kobject *obj)
26 return container_of(obj,struct class_device,kobj);
28 static inline
29 struct class_device_attribute *to_class_dev_attr(struct attribute *_attr)
31 return container_of(_attr,struct class_device_attribute,attr);
34 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr);
35 int sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr);
37 struct firmware_priv {
38 char fw_id[FIRMWARE_NAME_MAX];
39 s32 loading:2;
40 u32 abort:1;
43 extern struct class firmware_class;
45 static ssize_t firmware_loading_show(struct class_device *class_dev, char *buf)
47 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
48 return sprintf(buf, "%d\n", fw_priv->loading);
50 static ssize_t firmware_loading_store(struct class_device *class_dev,
51 const char *buf, size_t count)
53 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
54 int prev_loading = fw_priv->loading;
56 fw_priv->loading = simple_strtol(buf, NULL, 10);
58 switch(fw_priv->loading){
59 case -1:
60 /* abort load an panic */
61 break;
62 case 1:
63 /* setup load */
64 break;
65 case 0:
66 if(prev_loading==1){
67 /* finish load and get the device back to working
68 * state */
70 break;
73 return count;
75 static CLASS_DEVICE_ATTR(loading, 0644,
76 firmware_loading_show, firmware_loading_store);
78 static ssize_t firmware_data_read(struct kobject *kobj,
79 char *buffer, loff_t offset, size_t count)
81 struct class_device *class_dev = to_class_dev(kobj);
82 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
84 /* read from the devices firmware memory */
86 return count;
88 static ssize_t firmware_data_write(struct kobject *kobj,
89 char *buffer, loff_t offset, size_t count)
91 struct class_device *class_dev = to_class_dev(kobj);
92 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
94 /* write to the devices firmware memory */
96 return count;
98 static struct bin_attribute firmware_attr_data = {
99 .attr = {.name = "data", .mode = 0644},
100 .size = 0,
101 .read = firmware_data_read,
102 .write = firmware_data_write,
104 static int fw_setup_class_device(struct class_device *class_dev,
105 const char *fw_name,
106 struct device *device)
108 int retval = 0;
109 struct firmware_priv *fw_priv = kmalloc(sizeof(struct firmware_priv),
110 GFP_KERNEL);
112 if(!fw_priv){
113 retval = -ENOMEM;
114 goto out;
116 memset(fw_priv, 0, sizeof(*fw_priv));
117 memset(class_dev, 0, sizeof(*class_dev));
119 strncpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX);
120 fw_priv->fw_id[FIRMWARE_NAME_MAX-1] = '\0';
122 strncpy(class_dev->class_id, device->bus_id, BUS_ID_SIZE);
123 class_dev->class_id[BUS_ID_SIZE-1] = '\0';
124 class_dev->dev = device;
126 class_dev->class = &firmware_class,
127 class_set_devdata(class_dev, fw_priv);
128 retval = class_device_register(class_dev);
129 if (retval){
130 printk(KERN_ERR "%s: class_device_register failed\n",
131 __FUNCTION__);
132 goto error_free_fw_priv;
135 retval = sysfs_create_bin_file(&class_dev->kobj, &firmware_attr_data);
136 if (retval){
137 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
138 __FUNCTION__);
139 goto error_unreg_class_dev;
142 retval = class_device_create_file(class_dev,
143 &class_device_attr_loading);
144 if (retval){
145 printk(KERN_ERR "%s: class_device_create_file failed\n",
146 __FUNCTION__);
147 goto error_remove_data;
150 goto out;
152 error_remove_data:
153 sysfs_remove_bin_file(&class_dev->kobj, &firmware_attr_data);
154 error_unreg_class_dev:
155 class_device_unregister(class_dev);
156 error_free_fw_priv:
157 kfree(fw_priv);
158 out:
159 return retval;
161 static void fw_remove_class_device(struct class_device *class_dev)
163 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
165 class_device_remove_file(class_dev, &class_device_attr_loading);
166 sysfs_remove_bin_file(&class_dev->kobj, &firmware_attr_data);
167 class_device_unregister(class_dev);
170 static struct class_device *class_dev;
172 static struct device my_device = {
173 .name = "Sample Device",
174 .bus_id = "my_dev0",
177 static int __init firmware_sample_init(void)
179 int error;
181 device_initialize(&my_device);
182 class_dev = kmalloc(sizeof(struct class_device), GFP_KERNEL);
183 if(!class_dev)
184 return -ENOMEM;
186 error = fw_setup_class_device(class_dev, "my_firmware_image",
187 &my_device);
188 if(error){
189 kfree(class_dev);
190 return error;
192 return 0;
195 static void __exit firmware_sample_exit(void)
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);
202 module_init(firmware_sample_init);
203 module_exit(firmware_sample_exit);