drvmain.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include <linux/init.h>
  2. #include <linux/kernel.h> // printk()
  3. #include <linux/kobject.h> // struct kobject
  4. #include <linux/errno.h> // error codes
  5. #include <linux/module.h> // THIS_MODULE
  6. #include <linux/sysfs.h>
  7. #include <linux/syscalls.h>
  8. #include <asm/ioctls.h>
  9. #include <linux/spi/spidev.h>
  10. #include "kfile.h"
  11. #include "kspi.h"
  12. /////////////////////////////////////////////////////////////////////////////
  13. MODULE_LICENSE("Dual BSD/GPL");
  14. MODULE_AUTHOR("GfA");
  15. /////////////////////////////////////////////////////////////////////////////
  16. #define _SPI_DEVICE "/dev/spidev1.0"
  17. static struct file *g_pfSpiDev = NULL;
  18. /////////////////////////////////////////////////////////////////////////////
  19. static struct kobject *pKoGfa = NULL, *pKoTiva = NULL;
  20. /////////////////////////////////////////////////////////////////////////////
  21. static ssize_t firmware_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
  22. {
  23. int hw = 0, sw = 0;
  24. printk(KERN_ALERT "%s, TID: %d, \"%s\"\n", __FUNCTION__, current->pid, current->comm);
  25. CmdGetFirmwareVersion(g_pfSpiDev, &hw, &sw);
  26. return sprintf(buf, "HW: %08X SW: %08X\n", hw, sw);
  27. }
  28. /////////////////////////////////////////////////////////////////////////////
  29. static struct kobj_attribute tivaFirmwareAtt = __ATTR_RO(firmware);
  30. /////////////////////////////////////////////////////////////////////////////
  31. static int drv_init(void)
  32. {
  33. long ret;
  34. uint8_t mode = SPI_MODE_3;
  35. uint8_t bits = 8;
  36. uint32_t speed = 1000000;
  37. /////////////////////////////////////////////////////////////////////////
  38. do
  39. {
  40. if(!(g_pfSpiDev = kf_open(_SPI_DEVICE, O_RDWR, 0)))
  41. {
  42. printk(KERN_ALERT "file_open failed\n");
  43. break;
  44. }
  45. /////////////////////////////////////////////////////////////////////
  46. if(!(pKoGfa = kobject_create_and_add("gfa", /*kernel_kobj*/NULL)))
  47. {
  48. printk(KERN_ALERT "kobject_create_and_add failed\n");
  49. break;
  50. }
  51. if(!(pKoTiva = kobject_create_and_add("tiva", pKoGfa)))
  52. {
  53. printk(KERN_ALERT "kobject_create_and_add failed\n");
  54. break;
  55. }
  56. /////////////////////////////////////////////////////////////////////
  57. if(sysfs_create_file(pKoTiva, &tivaFirmwareAtt.attr))
  58. {
  59. printk(KERN_ALERT "sysfs_create_file failed\n");
  60. break;
  61. }
  62. /////////////////////////////////////////////////////////////////////
  63. ret = kf_ioctl(g_pfSpiDev, SPI_IOC_WR_MODE, (unsigned long)&mode);
  64. if(ret < 0)
  65. {
  66. printk(KERN_ALERT "can't set spi mode\n");
  67. break;
  68. }
  69. mode = 0;
  70. ret = kf_ioctl(g_pfSpiDev, SPI_IOC_RD_MODE, (unsigned long)&mode);
  71. if(ret < 0)
  72. {
  73. printk(KERN_ALERT "can't get spi mode\n");
  74. break;
  75. }
  76. /////////////////////////////////////////////////////////////////////
  77. ret = kf_ioctl(g_pfSpiDev, SPI_IOC_WR_BITS_PER_WORD, (unsigned long)&bits);
  78. if(ret < 0)
  79. {
  80. printk(KERN_ALERT "can't set bits per word\n");
  81. break;
  82. }
  83. bits = 0;
  84. ret = kf_ioctl(g_pfSpiDev, SPI_IOC_RD_BITS_PER_WORD, (unsigned long)&bits);
  85. if(ret < 0)
  86. {
  87. printk(KERN_ALERT "can't get bits per word\n");
  88. break;
  89. }
  90. /////////////////////////////////////////////////////////////////////
  91. ret = kf_ioctl(g_pfSpiDev, SPI_IOC_WR_MAX_SPEED_HZ, (unsigned long)&speed);
  92. if(ret < 0)
  93. {
  94. printk(KERN_ALERT "can't set max speed hz\n");
  95. break;
  96. }
  97. speed = 0;
  98. ret = kf_ioctl(g_pfSpiDev, SPI_IOC_RD_MAX_SPEED_HZ, (unsigned long)&speed);
  99. if(ret < 0)
  100. {
  101. printk(KERN_ALERT "can't get max speed hz\n");
  102. break;
  103. }
  104. /////////////////////////////////////////////////////////////////////
  105. printk(KERN_ALERT "mode: %hhu\n", mode);
  106. printk(KERN_ALERT "bits: %hhu\n", bits);
  107. printk(KERN_ALERT "speed: %u\n", speed);
  108. printk(KERN_ALERT "%s, TID: %d, \"%s\"\n", __FUNCTION__, current->pid, current->comm);
  109. return 0;
  110. }
  111. while(0);
  112. /////////////////////////////////////////////////////////////////////////
  113. if(g_pfSpiDev)
  114. kf_close(g_pfSpiDev);
  115. if(pKoTiva)
  116. {
  117. sysfs_remove_file(pKoTiva, &tivaFirmwareAtt.attr);
  118. kobject_put(pKoTiva);
  119. pKoTiva = NULL;
  120. }
  121. if(pKoGfa)
  122. {
  123. kobject_put(pKoGfa);
  124. pKoGfa = NULL;
  125. }
  126. return -ENOMEM;
  127. }
  128. /////////////////////////////////////////////////////////////////////////////
  129. static void drv_exit(void)
  130. {
  131. if(g_pfSpiDev)
  132. kf_close(g_pfSpiDev);
  133. if(pKoTiva)
  134. {
  135. sysfs_remove_file(pKoTiva, &tivaFirmwareAtt.attr);
  136. kobject_put(pKoTiva);
  137. }
  138. if(pKoGfa)
  139. kobject_put(pKoGfa);
  140. printk(KERN_ALERT "%s, TID: %d, \"%s\"\n", __FUNCTION__, current->pid, current->comm);
  141. }
  142. /////////////////////////////////////////////////////////////////////////////
  143. module_init(drv_init);
  144. module_exit(drv_exit);