2 * firmware_sample_firmware_class.c -
4 * Copyright (c) 2003 Manuel Estrada Sainz
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/slab.h>
18 #include <linux/string.h>
19 #include <linux/firmware.h>
22 MODULE_AUTHOR("Manuel Estrada Sainz");
23 MODULE_DESCRIPTION("Hackish sample for using firmware class directly");
24 MODULE_LICENSE("GPL");
26 static inline struct class_device
*to_class_dev(struct kobject
*obj
)
28 return container_of(obj
, struct class_device
, kobj
);
32 struct class_device_attribute
*to_class_dev_attr(struct attribute
*_attr
)
34 return container_of(_attr
, struct class_device_attribute
, attr
);
37 struct firmware_priv
{
38 char fw_id
[FIRMWARE_NAME_MAX
];
43 static ssize_t
firmware_loading_show(struct class_device
*class_dev
, char *buf
)
45 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
46 return sprintf(buf
, "%d\n", fw_priv
->loading
);
49 static ssize_t
firmware_loading_store(struct class_device
*class_dev
,
50 const char *buf
, size_t count
)
52 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
53 int prev_loading
= fw_priv
->loading
;
55 fw_priv
->loading
= simple_strtol(buf
, NULL
, 10);
57 switch (fw_priv
->loading
) {
59 /* abort load an panic */
65 if (prev_loading
== 1) {
66 /* finish load and get the device back to working
74 static CLASS_DEVICE_ATTR(loading
, 0644,
75 firmware_loading_show
, firmware_loading_store
);
77 static ssize_t
firmware_data_read(struct kobject
*kobj
,
78 struct bin_attribute
*bin_attr
,
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 */
88 static ssize_t
firmware_data_write(struct kobject
*kobj
,
89 struct bin_attribute
*bin_attr
,
90 char *buffer
, loff_t offset
, size_t count
)
92 struct class_device
*class_dev
= to_class_dev(kobj
);
93 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
95 /* write to the devices firmware memory */
99 static struct bin_attribute firmware_attr_data
= {
100 .attr
= {.name
= "data", .mode
= 0644},
102 .read
= firmware_data_read
,
103 .write
= firmware_data_write
,
105 static int fw_setup_class_device(struct class_device
*class_dev
,
107 struct device
*device
)
110 struct firmware_priv
*fw_priv
;
112 fw_priv
= kzalloc(sizeof(struct firmware_priv
), GFP_KERNEL
);
118 memset(class_dev
, 0, sizeof(*class_dev
));
120 strncpy(fw_priv
->fw_id
, fw_name
, FIRMWARE_NAME_MAX
);
121 fw_priv
->fw_id
[FIRMWARE_NAME_MAX
-1] = '\0';
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
;
127 class_dev
->class = &firmware_class
;
128 class_set_devdata(class_dev
, fw_priv
);
129 retval
= class_device_register(class_dev
);
131 printk(KERN_ERR
"%s: class_device_register failed\n",
133 goto error_free_fw_priv
;
136 retval
= sysfs_create_bin_file(&class_dev
->kobj
, &firmware_attr_data
);
138 printk(KERN_ERR
"%s: sysfs_create_bin_file failed\n",
140 goto error_unreg_class_dev
;
143 retval
= class_device_create_file(class_dev
,
144 &class_device_attr_loading
);
146 printk(KERN_ERR
"%s: class_device_create_file failed\n",
148 goto 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
);
162 static void fw_remove_class_device(struct class_device
*class_dev
)
164 struct firmware_priv
*fw_priv
= class_get_devdata(class_dev
);
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
);
171 static struct class_device
*class_dev
;
173 static struct device my_device
= {
177 static int __init
firmware_sample_init(void)
181 device_initialize(&my_device
);
182 class_dev
= kmalloc(sizeof(struct class_device
), GFP_KERNEL
);
186 error
= fw_setup_class_device(class_dev
, "my_firmware_image",
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
);
203 module_init(firmware_sample_init
);
204 module_exit(firmware_sample_exit
);