Advertisement
Guest User

Untitled

a guest
Mar 27th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.40 KB | None | 0 0
  1. #include <linux/init.h>
  2. #include <linux/module.h>
  3. #include <linux/i2c.h>
  4. #include <linux/delay.h>
  5. #include <linux/input-polldev.h>
  6.  
  7. struct input_dev *global_input_dev = NULL;
  8.  
  9. struct nunchuk_dev {
  10.     struct input_polled_dev *polled_input;
  11.     struct i2c_client *i2c_client;
  12. };
  13.  
  14. static int nunchuk_probe(struct i2c_client *client, const struct i2c_device_id *device_id)
  15. {
  16.     int rv;
  17.     struct input_polled_dev *polled_input;
  18.     struct input_dev *input;
  19.     struct nunchuk_dev *nunchuk;
  20.  
  21.     pr_warn("nunchuk: inside nunchuk_probe.\n");
  22.     rv = 0;
  23.    
  24.     nunchuk = devm_kzalloc(&client->dev, sizeof(struct nunchuk_dev), GFP_KERNEL);
  25.     if (!nunchuk) {
  26.         dev_err(&client->dev, "[ERROR] failed to allocate memory for nunchuk_dev\n");
  27.         pr_warn("[ERROR] failed to allocate memory for nunchuk_dev\n");
  28.         rv = -ENOMEM;
  29.         goto final;
  30.     }
  31.  
  32.    
  33.     polled_input = input_allocate_polled_device();
  34.     if (!polled_input) {
  35.         dev_err(&client->dev, "[ERROR] failed to allocate input_polled_dev\n");
  36.         pr_warn("[ERROR] failed to allocate input_polled_dev\n");
  37.         rv = -ENOMEM;
  38.         goto final;
  39.     }
  40.  
  41.     nunchuk->i2c_client = client;
  42.     nunchuk->polled_input = polled_input;
  43.     polled_input->private = nunchuk;
  44.     i2c_set_clientdata(client, nunchuk);
  45.     input = polled_input->input;
  46.     input->dev.parent = &client->dev;
  47.     global_input_dev = input;
  48.  
  49.     pr_warn("nunchuk_probe(): nunchuk = 0x%x, polled_input = 0x%x, input = 0x%x\n", nunchuk, polled_input, input);
  50.  
  51.     rv = input_register_polled_device(polled_input);
  52.     if (rv) {
  53.         dev_err(&client->dev, "[ERROR] failed to register input_polled_dev\n");
  54.         pr_warn("[ERROR] failed to register polled_input\n");
  55.         goto cleanup;
  56.     }
  57.  
  58. cleanup:
  59.     input_free_polled_device(polled_input);
  60. final:
  61.     pr_warn("nunchuk_probe(): just before exiting... input = 0x%x\n", input);
  62.     return rv;
  63. }
  64.  
  65. static int nunchuk_remove(struct i2c_client *client)
  66. {
  67.     struct nunchuk_dev *nunchuk = NULL;
  68.     struct input_polled_dev *polled_input = NULL;
  69.     struct input_dev *input = NULL;
  70.  
  71.     pr_warn("nunchuk: ENTERING remove()\n");
  72.  
  73.     nunchuk = i2c_get_clientdata(client);
  74.  
  75.     input = nunchuk->polled_input->input;
  76.  
  77.     pr_warn("nunchuk_remove(): nunchuk = 0x%x. nunchuk->polled_input = 0x%x. polled_input->input = 0x%x\n", nunchuk, nunchuk->polled_input, input);
  78.     pr_warn("nunchuk_remove(): global_input_dev = 0x%x\n", global_input_dev);
  79.  
  80.     if (!nunchuk) {
  81.         pr_warn("[ERROR] remove(): failed to obtain nunchuk_dev\n");
  82.         return -1;
  83.     }
  84.  
  85.     polled_input = nunchuk->polled_input;
  86.  
  87.     if (!polled_input) {
  88.         pr_warn("[ERROR] nunchuk_remove(): nunchuk->polled_input is null!\n");
  89.         return -1;
  90.     }
  91.  
  92.     pr_warn("*** before input_unregister_polled_device()\n");
  93.     input_unregister_polled_device(polled_input);
  94.     pr_warn("*** before input_free_polled_device()\n");
  95.     input_free_polled_device(polled_input);
  96.     pr_warn("*** about to return\n");
  97.  
  98.     return 0;
  99. }
  100.  
  101. static const struct i2c_device_id nunchuk_id[] = {
  102.     { "nunchuk", 0 },
  103.     { },
  104. };
  105. MODULE_DEVICE_TABLE(i2c, nunchuk_id);
  106.  
  107. #ifdef CONFIG_OF
  108. static const struct of_device_id nunchuk_dt_ids[] = {
  109.     { .compatible = "nintendo,nunchuk", },
  110.     { },
  111. };
  112.  
  113. MODULE_DEVICE_TABLE(of, nunchuk_dt_ids);
  114. #endif
  115.  
  116. static struct i2c_driver nunchuk_driver = {
  117.     .probe = nunchuk_probe,
  118.     .remove = nunchuk_remove,
  119.     .id_table = nunchuk_id,
  120.     .driver = {
  121.         .name = "nunchuk",
  122.         .owner = THIS_MODULE,
  123.         .of_match_table = of_match_ptr(nunchuk_dt_ids),
  124.     },
  125. };
  126.  
  127. module_i2c_driver(nunchuk_driver);
  128.  
  129. MODULE_LICENSE("GPL");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement