# BRCM_VERSION=3
[bcm963xx.git] / kernel / linux / Documentation / firmware_class / firmware_sample_firmware_class.c
1 /*
2  * firmware_sample_firmware_class.c -
3  *
4  * Copyright (c) 2003 Manuel Estrada Sainz <ranty@debian.org>
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 <asm/hardirq.h>
18
19 #include "linux/firmware.h"
20
21 MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>");
22 MODULE_DESCRIPTION("Hackish sample for using firmware class directly");
23 MODULE_LICENSE("GPL");
24
25 static inline struct class_device *to_class_dev(struct kobject *obj)
26 {
27         return container_of(obj,struct class_device,kobj);
28 }
29 static inline
30 struct class_device_attribute *to_class_dev_attr(struct attribute *_attr)
31 {
32         return container_of(_attr,struct class_device_attribute,attr);
33 }
34
35 int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr);
36 int sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr);
37
38 struct firmware_priv {
39         char fw_id[FIRMWARE_NAME_MAX];
40         s32 loading:2;
41         u32 abort:1;
42 };
43
44 extern struct class firmware_class;
45
46 static ssize_t firmware_loading_show(struct class_device *class_dev, char *buf)
47 {
48         struct firmware_priv *fw_priv = class_get_devdata(class_dev);
49         return sprintf(buf, "%d\n", fw_priv->loading);
50 }
51 static ssize_t firmware_loading_store(struct class_device *class_dev,
52                                       const char *buf, size_t count)
53 {
54         struct firmware_priv *fw_priv = class_get_devdata(class_dev);
55         int prev_loading = fw_priv->loading;
56
57         fw_priv->loading = simple_strtol(buf, NULL, 10);
58         
59         switch(fw_priv->loading){
60         case -1:
61                 /* abort load an panic */
62                 break;
63         case 1:
64                 /* setup load */
65                 break;
66         case 0:
67                 if(prev_loading==1){
68                         /* finish load and get the device back to working
69                          * state */
70                 }
71                 break;
72         }
73
74         return count;
75 }
76 static CLASS_DEVICE_ATTR(loading, 0644,
77                          firmware_loading_show, firmware_loading_store);
78
79 static ssize_t firmware_data_read(struct kobject *kobj,
80                                   char *buffer, loff_t offset, size_t count)
81 {
82         struct class_device *class_dev = to_class_dev(kobj);
83         struct firmware_priv *fw_priv = class_get_devdata(class_dev);
84
85         /* read from the devices firmware memory */
86
87         return count;
88 }
89 static ssize_t firmware_data_write(struct kobject *kobj,
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 }
99 static 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 };
105 static int fw_setup_class_device(struct class_device *class_dev,
106                                  const char *fw_name,
107                                  struct device *device)
108 {
109         int retval = 0;
110         struct firmware_priv *fw_priv = kmalloc(sizeof(struct firmware_priv),
111                                                 GFP_KERNEL);
112
113         if(!fw_priv){
114                 retval = -ENOMEM;
115                 goto out;
116         }
117         memset(fw_priv, 0, sizeof(*fw_priv));
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                        __FUNCTION__);
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                        __FUNCTION__);
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                        __FUNCTION__);
148                 goto error_remove_data;
149         }
150
151         goto out;
152         
153 error_remove_data:
154         sysfs_remove_bin_file(&class_dev->kobj, &firmware_attr_data);
155 error_unreg_class_dev:
156         class_device_unregister(class_dev);
157 error_free_fw_priv:
158         kfree(fw_priv);
159 out:
160         return retval;
161 }
162 static 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
171 static struct class_device *class_dev;
172
173 static struct device my_device = {
174         .name      = "Sample Device",
175         .bus_id    = "my_dev0",
176 };
177
178 static int __init firmware_sample_init(void)
179 {
180         int error;
181
182         device_initialize(&my_device);
183         class_dev = kmalloc(sizeof(struct class_device), GFP_KERNEL);
184         if(!class_dev)
185                 return -ENOMEM;
186
187         error = fw_setup_class_device(class_dev, "my_firmware_image",
188                                       &my_device);
189         if(error){
190                 kfree(class_dev);
191                 return error;
192         }
193         return 0;
194
195 }
196 static void __exit firmware_sample_exit(void)
197 {
198         struct firmware_priv *fw_priv = class_get_devdata(class_dev);
199         fw_remove_class_device(class_dev);
200         kfree(fw_priv);
201         kfree(class_dev);
202 }
203 module_init(firmware_sample_init);
204 module_exit(firmware_sample_exit);
205