点击(此处)折叠或打开
- #include <linux/module.h>
 - #include <linux/kernel.h>
 - #include <linux/slab.h>
 - #include <linux/i2c.h>
 - #include <linux/firmware.h>
 - #include <linux/timer.h>
 - #include <linux/workqueue.h>
 - struct driver_data {
 -     struct i2c_client *client;
 -     struct workqueue_struct *wq;
 -     struct work_struct work;
 - };
 - static struct driver_data *ddata;
 - static struct timer_list timer;
 - static void fw_work(void)
 - {
 -     int ret = 0;
 -     struct firmware *fw;
 -     printk("-- %s --\n", __FUNCTION__);
 -     /* 调度一个work来做 */
 -     ret = request_firmware(&fw, "test.bin", &ddata->client->dev);  //可以自设路径, 如/mark/test.bin
 -     if (ret != 0) {
 -         printk("--- error ---\n");
 -         return;
 -     }
 -     printk("fw->size = %d, fw->data = %s\n", fw->size, fw->data);
 -     release_firmware(fw);
 -     return;
 - }
 - static void fw_update(void)
 - {
 -     printk("-- %s --\n", __FUNCTION__);
 -     queue_work(ddata->wq, &ddata->work);
 -     return;
 - }
 - static __devinit int fw_probe(struct i2c_client *client,
 -         const struct i2c_device_id *id)
 - {
 -     printk("%s\n", __FUNCTION__);
 -     ddata = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
 -     ddata->client = client;
 -     INIT_WORK(&ddata->work, fw_work);
 -     ddata->wq = create_singlethread_workqueue("bin");
 -     init_timer(&timer);
 -     timer.data = 0;
 -     timer.expires = jiffies + 20 * HZ;
 -     timer.function = &fw_update;
 -     add_timer(&timer);
 -     return 0;
 - }
 - static const struct i2c_device_id fw_id[] = {
 -     {"bin", 0},
 -     {},
 - };
 - static struct i2c_board_info i2c_info = {
 -     .type = "bin",
 -     .addr = 0x40,
 - };
 - struct i2c_driver fw_driver = {
 -     .driver = {
 -         .name = "bin",
 -         .owner = THIS_MODULE,
 -     },
 -     .probe = fw_probe,
 -     .id_table = fw_id,
 - };
 - static int __init fw_init(void)
 - {
 -     struct i2c_adapter *adapter;
 -     printk("%s\n", __FUNCTION__);
 -     adapter = i2c_get_adapter(0);
 -     if (!adapter) {
 -         printk("error : %d\n", __LINE__);
 -         return -1;
 -     }
 -     i2c_new_device(adapter, &i2c_info);
 -     i2c_put_adapter(adapter);
 -     i2c_add_driver(&fw_driver);
 -     return 0;
 - }
 - static void __exit fw_exit(void)
 - {
 -     return;
 - }
 - module_init(fw_init);
 - module_exit(fw_exit);
 
在目标机中加入文件
点击(此处)折叠或打开
- #vi /lib/firmware/test.bin #输入hello!
 
可以在目标机shell中看到如下信息
点击(此处)折叠或打开
- -- fw_update --
 - -- fw_work --
 - bin 0-0040: firmware: requesting test.bin
 - fw->size = 7, fw->data = hello!