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
);
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
];
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
){
60 /* abort load an panic */
67 /* finish load and get the device back to working
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 */
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 */
98 static struct bin_attribute firmware_attr_data
= {
99 .attr
= {.name
= "data", .mode
= 0644},
101 .read
= firmware_data_read
,
102 .write
= firmware_data_write
,
104 static int fw_setup_class_device(struct class_device
*class_dev
,
106 struct device
*device
)
109 struct firmware_priv
*fw_priv
= kmalloc(sizeof(struct firmware_priv
),
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
);
130 printk(KERN_ERR
"%s: class_device_register failed\n",
132 goto error_free_fw_priv
;
135 retval
= sysfs_create_bin_file(&class_dev
->kobj
, &firmware_attr_data
);
137 printk(KERN_ERR
"%s: sysfs_create_bin_file failed\n",
139 goto error_unreg_class_dev
;
142 retval
= class_device_create_file(class_dev
,
143 &class_device_attr_loading
);
145 printk(KERN_ERR
"%s: class_device_create_file failed\n",
147 goto 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
);
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",
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
);
202 module_init(firmware_sample_init
);
203 module_exit(firmware_sample_exit
);