/[alx-svn]/alx-src/tags/kernel26-2.6.12-alx-r9/Documentation/firmware_class/firmware_sample_firmware_class.c |
Contents of /alx-src/tags/kernel26-2.6.12-alx-r9/Documentation/firmware_class/firmware_sample_firmware_class.c
Parent Directory | Revision Log
Revision 630 -
(show annotations)
(download)
Wed Mar 4 11:03:09 2009 UTC (15 years, 6 months ago) by niro
File MIME type: text/plain
File size: 5188 byte(s)
Wed Mar 4 11:03:09 2009 UTC (15 years, 6 months ago) by niro
File MIME type: text/plain
File size: 5188 byte(s)
Tag kernel26-2.6.12-alx-r9
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 <linux/firmware.h> |
18 | |
19 | |
20 | MODULE_AUTHOR("Manuel Estrada Sainz <ranty@debian.org>"); |
21 | MODULE_DESCRIPTION("Hackish sample for using firmware class directly"); |
22 | MODULE_LICENSE("GPL"); |
23 | |
24 | static inline struct class_device *to_class_dev(struct kobject *obj) |
25 | { |
26 | return container_of(obj,struct class_device,kobj); |
27 | } |
28 | static inline |
29 | struct class_device_attribute *to_class_dev_attr(struct attribute *_attr) |
30 | { |
31 | return container_of(_attr,struct class_device_attribute,attr); |
32 | } |
33 | |
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); |
36 | |
37 | struct firmware_priv { |
38 | char fw_id[FIRMWARE_NAME_MAX]; |
39 | s32 loading:2; |
40 | u32 abort:1; |
41 | }; |
42 | |
43 | extern struct class firmware_class; |
44 | |
45 | static ssize_t firmware_loading_show(struct class_device *class_dev, char *buf) |
46 | { |
47 | struct firmware_priv *fw_priv = class_get_devdata(class_dev); |
48 | return sprintf(buf, "%d\n", fw_priv->loading); |
49 | } |
50 | static ssize_t firmware_loading_store(struct class_device *class_dev, |
51 | const char *buf, size_t count) |
52 | { |
53 | struct firmware_priv *fw_priv = class_get_devdata(class_dev); |
54 | int prev_loading = fw_priv->loading; |
55 | |
56 | fw_priv->loading = simple_strtol(buf, NULL, 10); |
57 | |
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 */ |
69 | } |
70 | break; |
71 | } |
72 | |
73 | return count; |
74 | } |
75 | static CLASS_DEVICE_ATTR(loading, 0644, |
76 | firmware_loading_show, firmware_loading_store); |
77 | |
78 | static ssize_t firmware_data_read(struct kobject *kobj, |
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 | } |
88 | static ssize_t firmware_data_write(struct kobject *kobj, |
89 | char *buffer, loff_t offset, size_t count) |
90 | { |
91 | struct class_device *class_dev = to_class_dev(kobj); |
92 | struct firmware_priv *fw_priv = class_get_devdata(class_dev); |
93 | |
94 | /* write to the devices firmware memory */ |
95 | |
96 | return count; |
97 | } |
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, |
103 | }; |
104 | static int fw_setup_class_device(struct class_device *class_dev, |
105 | const char *fw_name, |
106 | struct device *device) |
107 | { |
108 | int retval = 0; |
109 | struct firmware_priv *fw_priv = kmalloc(sizeof(struct firmware_priv), |
110 | GFP_KERNEL); |
111 | |
112 | if(!fw_priv){ |
113 | retval = -ENOMEM; |
114 | goto out; |
115 | } |
116 | memset(fw_priv, 0, sizeof(*fw_priv)); |
117 | memset(class_dev, 0, sizeof(*class_dev)); |
118 | |
119 | strncpy(fw_priv->fw_id, fw_name, FIRMWARE_NAME_MAX); |
120 | fw_priv->fw_id[FIRMWARE_NAME_MAX-1] = '\0'; |
121 | |
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; |
125 | |
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; |
133 | } |
134 | |
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; |
140 | } |
141 | |
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; |
148 | } |
149 | |
150 | goto out; |
151 | |
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; |
160 | } |
161 | static void fw_remove_class_device(struct class_device *class_dev) |
162 | { |
163 | struct firmware_priv *fw_priv = class_get_devdata(class_dev); |
164 | |
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); |
168 | } |
169 | |
170 | static struct class_device *class_dev; |
171 | |
172 | static struct device my_device = { |
173 | .name = "Sample Device", |
174 | .bus_id = "my_dev0", |
175 | }; |
176 | |
177 | static 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 | } |
195 | static 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 | module_init(firmware_sample_init); |
203 | module_exit(firmware_sample_exit); |
204 |