Chinaunix首页 | 论坛 | 博客
  • 博客访问: 79901
  • 博文数量: 19
  • 博客积分: 325
  • 博客等级: 一等列兵
  • 技术积分: 197
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-29 11:51
文章分类

全部博文(19)

文章存档

2013年(1)

2012年(18)

我的朋友

分类: LINUX

2012-11-27 20:18:02


点击(此处)折叠或打开

  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/slab.h>
  4. #include <linux/i2c.h>
  5. #include <linux/firmware.h>
  6. #include <linux/timer.h>
  7. #include <linux/workqueue.h>

  8. struct driver_data {
  9.     struct i2c_client *client;
  10.     struct workqueue_struct *wq;
  11.     struct work_struct work;
  12. };

  13. static struct driver_data *ddata;
  14. static struct timer_list timer;

  15. static void fw_work(void)
  16. {
  17.     int ret = 0;
  18.     struct firmware *fw;

  19.     printk("-- %s --\n", __FUNCTION__);

  20.     /* 调度一个work来做 */
  21.     ret = request_firmware(&fw, "test.bin", &ddata->client->dev); //可以自设路径,/mark/test.bin

  22.     if (ret != 0) {
  23.         printk("--- error ---\n");
  24.         return;
  25.     }
  26.     printk("fw->size = %d, fw->data = %s\n", fw->size, fw->data);

  27.     release_firmware(fw);

  28.     return;
  29. }

  30. static void fw_update(void)
  31. {
  32.     printk("-- %s --\n", __FUNCTION__);

  33.     queue_work(ddata->wq, &ddata->work);

  34.     return;
  35. }

  36. static __devinit int fw_probe(struct i2c_client *client,
  37.         const struct i2c_device_id *id)
  38. {
  39.     printk("%s\n", __FUNCTION__);

  40.     ddata = kzalloc(sizeof(struct driver_data), GFP_KERNEL);

  41.     ddata->client = client;

  42.     INIT_WORK(&ddata->work, fw_work);
  43.     ddata->wq = create_singlethread_workqueue("bin");

  44.     init_timer(&timer);
  45.     timer.data = 0;
  46.     timer.expires = jiffies + 20 * HZ;
  47.     timer.function = &fw_update;
  48.     add_timer(&timer);

  49.     return 0;
  50. }

  51. static const struct i2c_device_id fw_id[] = {
  52.     {"bin", 0},
  53.     {},
  54. };

  55. static struct i2c_board_info i2c_info = {
  56.     .type = "bin",
  57.     .addr = 0x40,
  58. };

  59. struct i2c_driver fw_driver = {
  60.     .driver = {
  61.         .name = "bin",
  62.         .owner = THIS_MODULE,
  63.     },
  64.     .probe = fw_probe,
  65.     .id_table = fw_id,
  66. };

  67. static int __init fw_init(void)
  68. {
  69.     struct i2c_adapter *adapter;

  70.     printk("%s\n", __FUNCTION__);

  71.     adapter = i2c_get_adapter(0);
  72.     if (!adapter) {
  73.         printk("error : %d\n", __LINE__);
  74.         return -1;
  75.     }

  76.     i2c_new_device(adapter, &i2c_info);

  77.     i2c_put_adapter(adapter);

  78.     i2c_add_driver(&fw_driver);

  79.     return 0;
  80. }

  81. static void __exit fw_exit(void)
  82. {
  83.     return;
  84. }

  85. module_init(fw_init);
  86. module_exit(fw_exit);

在目标机中加入文件

点击(此处)折叠或打开

  1. #vi /lib/firmware/test.bin #输入hello!


可以在目标机shell中看到如下信息

点击(此处)折叠或打开

  1. -- fw_update --
  2. -- fw_work --
  3. bin 0-0040: firmware: requesting test.bin
  4. fw->size = 7, fw->data = hello! 


阅读(892) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~