Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/fs.h>
- #include <asm/uaccess.h>
- #include <linux/i2c-dev.h>
- #include <linux/i2c.h>
- #define DEVICE_NAME "imu_dev"
- static dev_t dev_number;
- static struct class *imu_class;
- struct imu_strc {
- struct i2c_client *client;
- unsigned int addr;
- unsigned short current_pointer;
- };
- struct imu_strc * my_dev;
- static const struct i2c_device_id imu_id[] = {
- { DEVICE_NAME , 0 },
- {},
- };
- static int my_dev_open(struct inode *inode, struct file *file)
- {
- //int i, transferred, ret, my_buf[1];
- /* Get the private client data structure for this bank */
- struct imu_strc *my_bank =
- (struct imu_strc *)file->private_data;
- /* Check whether the smbus_read_word() functionality is
- supported */
- my_bank->addr = 0x69;
- if (i2c_check_functionality(my_bank->client->adapter,
- I2C_FUNC_SMBUS_READ_WORD_DATA)) {
- }
- return 0;
- };
- static int my_dev_release(struct inode *inode, struct file *file)
- {
- return 0;
- };
- static int my_dev_write(struct file *file,
- const char __user * buffer,
- size_t length, loff_t * offset)
- {
- return 0;
- };
- static int my_dev_read(struct file *filp,
- char *buffer,
- size_t length,
- loff_t * offset)
- {
- return 0;
- };
- static struct file_operations my_dev_ops = {
- .read = my_dev_read,
- .write = my_dev_write,
- .open = my_dev_open,
- .release = my_dev_release
- };
- static int example_probe(struct i2c_client *client,
- const struct i2c_device_id *id)
- {
- struct imu_strc *state;
- struct device *dev = &client->dev;
- state = kzalloc(sizeof(struct imu_strc), GFP_KERNEL);
- if (state == NULL) {
- dev_err(dev, "failed to create our state\n");
- return -ENOMEM;
- }
- state->client = client;
- i2c_set_clientdata(client, state);
- /* rest of the initialisation goes here. */
- dev_info(dev, "example client created\n");
- return 0;
- }
- static int example_remove(struct i2c_client *client)
- {
- struct imu_strc *state = i2c_get_clientdata(client);
- kfree(state);
- return 0;
- }
- static struct i2c_driver imu_driver =
- {
- .driver = {
- .name = DEVICE_NAME,
- },
- .probe = example_probe,
- .remove = example_remove,
- .id_table = imu_id,
- };
- static int __init l3g4200d_init(void)
- {
- int ret_val;
- printk(KERN_INFO "Welcome to Inertial measurement unit i2c module!\n");
- my_dev = kmalloc(sizeof(struct imu_strc)*1, GFP_KERNEL);
- memset(my_dev, 0, sizeof(struct imu_strc)*1);
- if (alloc_chrdev_region(&dev_number, 0, 1, DEVICE_NAME) < 0) {
- printk(KERN_DEBUG "Can't register device\n");
- return -1;
- }
- ret_val = register_chrdev(MAJOR_NUM, DEVICE_NAME, &my_dev_ops);
- if (ret_val < 0) {
- printk(KERN_ALERT "%s failed with %d\n",
- "Sorry, registering the character device ", ret_val);
- return ret_val;
- }
- device_create(imu_class, NULL,dev_number, NULL, DEVICE_NAME);
- ret_val = i2c_add_driver(&imu_driver);
- if (ret_val) {
- printk("Registering I2C driver failed, errno is %d\n", ret_val);
- return ret_val;
- }
- return 0;
- }
- static void __exit l3g4200d_exit(void)
- {
- #ifdef DEBUG
- pr_info("L3G4200D exit\n");
- #endif
- printk(KERN_INFO "Thank you for using my module. MishaSkt");
- unregister_chrdev(MAJOR_NUM, DEVICE_NAME);
- i2c_del_driver(&imu_driver);
- }
- module_init(l3g4200d_init);
- module_exit(l3g4200d_exit);
- MODULE_DESCRIPTION("driver");
- MODULE_AUTHOR("some author");
- MODULE_LICENSE("some license");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement