rx2.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <linux/fs.h>
  2. #include <linux/init.h>
  3. #include <linux/device.h>
  4. #include <linux/miscdevice.h>
  5. #include <linux/module.h>
  6. #include <linux/gpio.h>
  7. #include <linux/of.h>
  8. #include <linux/of_gpio.h>
  9. #include <asm/uaccess.h>
  10. #include <asm/delay.h>
  11. typedef struct { char *key; u8 value; } t_command;
  12. static t_command commands_table[] = {
  13. { "ENDCODE", 4},
  14. { "FORWARD", 10},
  15. { "FORWARD_TURBO", 16},
  16. { "TURBO", 22},
  17. { "FORWARD_LEFT", 28},
  18. { "FORWARD_RIGHT", 34},
  19. { "BACKWARD", 40},
  20. { "BACKWARD_RIGHT", 46},
  21. { "BACKWARD_LEFT", 52},
  22. { "LEFT", 58},
  23. { "RIGHT", 64}
  24. };
  25. static ssize_t rx2_dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset);
  26. static const struct file_operations rx2_fops = {
  27. .owner = THIS_MODULE,
  28. .write = rx2_dev_write
  29. };
  30. static struct miscdevice rx2_dev = {
  31. MISC_DYNAMIC_MINOR,
  32. "rx2",
  33. &rx2_fops
  34. };
  35. static int gpio = -1;
  36. static void rx2_send_command(u8 command) {
  37. int w = 0;
  38. for(w = 0; w < 4; w++) {
  39. gpio_set_value(gpio, 1);
  40. udelay(1200);
  41. gpio_set_value(gpio, 0);
  42. udelay(400);
  43. }
  44. for(w = 0; w < command; w++) {
  45. gpio_set_value(gpio, 1);
  46. udelay(400);
  47. gpio_set_value(gpio, 0);
  48. udelay(400);
  49. }
  50. }
  51. static ssize_t rx2_dev_write(struct file *filep, const char *buffer, size_t len, loff_t *offset) {
  52. u8 n;
  53. for(n = 0; n < sizeof(commands_table) / sizeof(t_command); n++) {
  54. if(strcmp(commands_table[n].key, buffer) == 0) {
  55. printk(KERN_INFO "%s %i\n", commands_table[n].key, commands_table[n].value);
  56. rx2_send_command(commands_table[n].value);
  57. break;
  58. }
  59. }
  60. return len;
  61. }
  62. static int __init rx2_init(void) {
  63. int ret;
  64. if(!gpio_is_valid(gpio)) {
  65. printk(KERN_ERR "rx2: invalid gpio\n");
  66. return -EINVAL;
  67. }
  68. ret = gpio_request(gpio, "ant");
  69. if(ret) {
  70. if (ret == -EINVAL)
  71. ret = -EPROBE_DEFER;
  72. return ret;
  73. }
  74. printk(KERN_INFO "rx2: ant on gpio: %i\n",gpio);
  75. gpio_direction_output(gpio, 0);
  76. ret = misc_register(&rx2_dev);
  77. if(ret) {
  78. printk(KERN_ERR "Unable to register rx2 device\n");
  79. gpio_free(gpio);
  80. return ret;
  81. }
  82. return ret;
  83. }
  84. static void __exit rx2_exit(void) {
  85. gpio_free(gpio);
  86. misc_deregister(&rx2_dev);
  87. }
  88. module_init(rx2_init);
  89. module_exit(rx2_exit);
  90. module_param(gpio, int, 0444);
  91. MODULE_PARM_DESC(gpio, "Guess what");
  92. MODULE_LICENSE("GPL");
  93. MODULE_AUTHOR("encrypt <encrypt@labr.xyz>");
  94. MODULE_DESCRIPTION("Stupid driver for RX2 ic");
  95. MODULE_VERSION("0.1");