blob: 9392116e47b009e1ef02f69f85ecff247f5b78b8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * firmware_sample_firmware_class.c -
3 *
Markus Rechberger87d37a42007-06-04 18:45:44 +02004 * Copyright (c) 2003 Manuel Estrada Sainz
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080017#include <linux/slab.h>
18#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/firmware.h>
20
21
Markus Rechberger87d37a42007-06-04 18:45:44 +020022MODULE_AUTHOR("Manuel Estrada Sainz");
Linus Torvalds1da177e2005-04-16 15:20:36 -070023MODULE_DESCRIPTION("Hackish sample for using firmware class directly");
24MODULE_LICENSE("GPL");
25
26static inline struct class_device *to_class_dev(struct kobject *obj)
27{
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080028 return container_of(obj, struct class_device, kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029}
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static inline
32struct class_device_attribute *to_class_dev_attr(struct attribute *_attr)
33{
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080034 return container_of(_attr, struct class_device_attribute, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037struct firmware_priv {
38 char fw_id[FIRMWARE_NAME_MAX];
39 s32 loading:2;
40 u32 abort:1;
41};
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043static ssize_t firmware_loading_show(struct class_device *class_dev, char *buf)
44{
45 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
46 return sprintf(buf, "%d\n", fw_priv->loading);
47}
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080048
Linus Torvalds1da177e2005-04-16 15:20:36 -070049static ssize_t firmware_loading_store(struct class_device *class_dev,
50 const char *buf, size_t count)
51{
52 struct firmware_priv *fw_priv = class_get_devdata(class_dev);
53 int prev_loading = fw_priv->loading;
54
55 fw_priv->loading = simple_strtol(buf, NULL, 10);
Randy Dunlapd289bf72008-02-20 13:20:50 -080056
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080057 switch (fw_priv->loading) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 case -1:
59 /* abort load an panic */
60 break;
61 case 1:
62 /* setup load */
63 break;
64 case 0:
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -080065 if (prev_loading == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /* finish load and get the device back to working
67 * state */
68 }
69 break;
70 }
71
72 return count;
73}
74static CLASS_DEVICE_ATTR(loading, 0644,
75 firmware_loading_show, firmware_loading_store);
76
77static ssize_t firmware_data_read(struct kobject *kobj,
Zhang Rui91a69022007-06-09 13:57:22 +080078 struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 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}
88static ssize_t firmware_data_write(struct kobject *kobj,
Zhang Rui91a69022007-06-09 13:57:22 +080089 struct bin_attribute *bin_attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 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}
99static 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};
105static int fw_setup_class_device(struct class_device *class_dev,
106 const char *fw_name,
107 struct device *device)
108{
Mariusz Kozlowskif035ac82007-10-16 23:26:44 -0700109 int retval;
110 struct firmware_priv *fw_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Mariusz Kozlowskif035ac82007-10-16 23:26:44 -0700112 fw_priv = kzalloc(sizeof(struct firmware_priv), GFP_KERNEL);
113 if (!fw_priv) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 retval = -ENOMEM;
115 goto out;
116 }
Mariusz Kozlowskif035ac82007-10-16 23:26:44 -0700117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 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);
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800130 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 printk(KERN_ERR "%s: class_device_register failed\n",
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800132 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto error_free_fw_priv;
134 }
135
136 retval = sysfs_create_bin_file(&class_dev->kobj, &firmware_attr_data);
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800137 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 printk(KERN_ERR "%s: sysfs_create_bin_file failed\n",
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800139 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto error_unreg_class_dev;
141 }
142
143 retval = class_device_create_file(class_dev,
144 &class_device_attr_loading);
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800145 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 printk(KERN_ERR "%s: class_device_create_file failed\n",
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800147 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 goto error_remove_data;
149 }
150
151 goto out;
Randy Dunlapd289bf72008-02-20 13:20:50 -0800152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153error_remove_data:
154 sysfs_remove_bin_file(&class_dev->kobj, &firmware_attr_data);
155error_unreg_class_dev:
156 class_device_unregister(class_dev);
157error_free_fw_priv:
158 kfree(fw_priv);
159out:
160 return retval;
161}
162static 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
171static struct class_device *class_dev;
172
173static struct device my_device = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 .bus_id = "my_dev0",
175};
176
177static 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);
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800183 if (!class_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return -ENOMEM;
185
186 error = fw_setup_class_device(class_dev, "my_firmware_image",
187 &my_device);
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800188 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 kfree(class_dev);
190 return error;
191 }
Greg Kroah-Hartman4b65fc82008-02-20 16:07:07 -0800192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194}
195static 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}
Randy Dunlapd289bf72008-02-20 13:20:50 -0800202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203module_init(firmware_sample_init);
204module_exit(firmware_sample_exit);