Advertisement
Guest User

linux i2c deivce driver (don't work)

a guest
Jun 1st, 2014
2,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include <linux/kernel.h>  
  2. #include <linux/module.h>  
  3. #include <linux/fs.h>
  4. #include <asm/uaccess.h>   
  5. #include <linux/i2c-dev.h>
  6. #include <linux/i2c.h>
  7.  
  8. #define DEVICE_NAME "imu_dev"
  9.  
  10.  
  11. static dev_t dev_number;
  12. static struct class *imu_class;
  13.  
  14.  
  15. struct imu_strc {
  16. struct i2c_client *client;
  17. unsigned int addr;
  18. unsigned short current_pointer;
  19. };
  20.  
  21. struct imu_strc * my_dev;
  22.  
  23. static const struct i2c_device_id imu_id[] = {
  24.     { DEVICE_NAME , 0 },
  25.     {},
  26. };
  27.  
  28.  
  29. static int my_dev_open(struct inode *inode, struct file *file)
  30. {
  31.  
  32.     //int i, transferred, ret, my_buf[1];
  33.     /* Get the private client data structure for this bank */
  34.     struct imu_strc *my_bank =
  35.     (struct imu_strc *)file->private_data;
  36.     /* Check whether the smbus_read_word() functionality is
  37.     supported */
  38.     my_bank->addr = 0x69;  
  39.     if (i2c_check_functionality(my_bank->client->adapter,
  40.     I2C_FUNC_SMBUS_READ_WORD_DATA)) {
  41.     }  
  42.  
  43. return 0;
  44. };
  45.  
  46. static int my_dev_release(struct inode *inode, struct file *file)
  47. {
  48.  
  49. return 0;
  50. };
  51.  
  52.  
  53. static int my_dev_write(struct file *file,
  54.                 const char __user * buffer,
  55.                 size_t length, loff_t * offset)
  56. {
  57.  
  58. return 0;
  59. };
  60.  
  61. static int my_dev_read(struct file *filp,
  62.             char *buffer,
  63.             size_t length,
  64.             loff_t * offset)
  65. {
  66.  
  67.  
  68. return 0;
  69. };
  70.  
  71.  
  72. static struct file_operations my_dev_ops = {
  73.     .read = my_dev_read,
  74.     .write = my_dev_write,
  75.     .open = my_dev_open,
  76.     .release = my_dev_release
  77.  
  78. };
  79.  
  80.  
  81. static int example_probe(struct i2c_client *client,
  82.                      const struct i2c_device_id *id)
  83.     {
  84.         struct imu_strc *state;
  85.         struct device *dev = &client->dev;
  86.    
  87.         state = kzalloc(sizeof(struct imu_strc), GFP_KERNEL);
  88.         if (state == NULL) {
  89.             dev_err(dev, "failed to create our state\n");
  90.             return -ENOMEM;
  91.         }
  92.    
  93.         state->client = client;
  94.         i2c_set_clientdata(client, state);
  95.    
  96.         /* rest of the initialisation goes here. */
  97.    
  98.         dev_info(dev, "example client created\n");
  99.    
  100.         return 0;
  101.     }
  102.  
  103. static int example_remove(struct i2c_client *client)
  104.     {
  105.         struct imu_strc *state = i2c_get_clientdata(client);
  106.    
  107.         kfree(state);
  108.         return 0;
  109.     }
  110.  
  111. static struct i2c_driver imu_driver =
  112.     {
  113.         .driver = {
  114.         .name   = DEVICE_NAME,
  115.         },
  116.         .probe = example_probe,
  117.         .remove = example_remove,
  118.         .id_table = imu_id,
  119.     };
  120.  
  121.  
  122. static int __init l3g4200d_init(void)
  123. {
  124. int ret_val;
  125.  
  126.     printk(KERN_INFO "Welcome to Inertial measurement unit i2c module!\n");
  127.    
  128.     my_dev = kmalloc(sizeof(struct imu_strc)*1, GFP_KERNEL);   
  129.     memset(my_dev, 0, sizeof(struct imu_strc)*1);
  130.  
  131.     if (alloc_chrdev_region(&dev_number, 0, 1, DEVICE_NAME) < 0) {
  132.     printk(KERN_DEBUG "Can't register device\n");
  133.     return -1;
  134.     }
  135.     ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &my_dev_ops);
  136.  
  137.     if (ret_val < 0) {
  138.     printk(KERN_ALERT "%s failed with %d\n",
  139.     "Sorry, registering the character device ", ret_val);
  140.     return ret_val;
  141.     }
  142.  
  143.     device_create(imu_class, NULL,dev_number, NULL, DEVICE_NAME);
  144.  
  145.     ret_val = i2c_add_driver(&imu_driver);
  146.     if (ret_val) {
  147.     printk("Registering I2C driver failed, errno is %d\n", ret_val);
  148.     return ret_val;
  149.     }
  150.  
  151. return 0;
  152.  
  153. }
  154.  
  155. static void __exit l3g4200d_exit(void)
  156. {
  157. #ifdef DEBUG
  158.     pr_info("L3G4200D exit\n");
  159. #endif
  160.  
  161.     printk(KERN_INFO "Thank you for using my module. MishaSkt");
  162.  
  163.     unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
  164.  
  165.     i2c_del_driver(&imu_driver);
  166.  
  167. }
  168. module_init(l3g4200d_init);
  169. module_exit(l3g4200d_exit);
  170.  
  171. MODULE_DESCRIPTION("driver");
  172. MODULE_AUTHOR("some author");
  173. MODULE_LICENSE("some license");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement