123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #include <linux/init.h>
- #include <linux/kernel.h> // printk()
- #include <linux/kobject.h> // struct kobject
- #include <linux/errno.h> // error codes
- #include <linux/module.h> // THIS_MODULE
- #include <linux/sysfs.h>
- #include <linux/syscalls.h>
- #include <asm/ioctls.h>
- #include <linux/spi/spidev.h>
- #include "kfile.h"
- #include "kspi.h"
- /////////////////////////////////////////////////////////////////////////////
- MODULE_LICENSE("Dual BSD/GPL");
- MODULE_AUTHOR("GfA");
- /////////////////////////////////////////////////////////////////////////////
- #define _SPI_DEVICE "/dev/spidev1.0"
- static struct file *g_pfSpiDev = NULL;
- /////////////////////////////////////////////////////////////////////////////
- static struct kobject *pKoGfa = NULL, *pKoTiva = NULL;
- /////////////////////////////////////////////////////////////////////////////
- static ssize_t firmware_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf)
- {
- int hw = 0, sw = 0;
- printk(KERN_ALERT "%s, TID: %d, \"%s\"\n", __FUNCTION__, current->pid, current->comm);
- CmdGetFirmwareVersion(g_pfSpiDev, &hw, &sw);
- return sprintf(buf, "HW: %08X SW: %08X\n", hw, sw);
- }
- /////////////////////////////////////////////////////////////////////////////
- static struct kobj_attribute tivaFirmwareAtt = __ATTR_RO(firmware);
- /////////////////////////////////////////////////////////////////////////////
- static int drv_init(void)
- {
- long ret;
- uint8_t mode = SPI_MODE_3;
- uint8_t bits = 8;
- uint32_t speed = 1000000;
- /////////////////////////////////////////////////////////////////////////
- do
- {
- if(!(g_pfSpiDev = kf_open(_SPI_DEVICE, O_RDWR, 0)))
- {
- printk(KERN_ALERT "file_open failed\n");
- break;
- }
- /////////////////////////////////////////////////////////////////////
- if(!(pKoGfa = kobject_create_and_add("gfa", /*kernel_kobj*/NULL)))
- {
- printk(KERN_ALERT "kobject_create_and_add failed\n");
- break;
- }
- if(!(pKoTiva = kobject_create_and_add("tiva", pKoGfa)))
- {
- printk(KERN_ALERT "kobject_create_and_add failed\n");
- break;
- }
- /////////////////////////////////////////////////////////////////////
- if(sysfs_create_file(pKoTiva, &tivaFirmwareAtt.attr))
- {
- printk(KERN_ALERT "sysfs_create_file failed\n");
- break;
- }
- /////////////////////////////////////////////////////////////////////
- ret = kf_ioctl(g_pfSpiDev, SPI_IOC_WR_MODE, (unsigned long)&mode);
- if(ret < 0)
- {
- printk(KERN_ALERT "can't set spi mode\n");
- break;
- }
- mode = 0;
- ret = kf_ioctl(g_pfSpiDev, SPI_IOC_RD_MODE, (unsigned long)&mode);
- if(ret < 0)
- {
- printk(KERN_ALERT "can't get spi mode\n");
- break;
- }
- /////////////////////////////////////////////////////////////////////
- ret = kf_ioctl(g_pfSpiDev, SPI_IOC_WR_BITS_PER_WORD, (unsigned long)&bits);
- if(ret < 0)
- {
- printk(KERN_ALERT "can't set bits per word\n");
- break;
- }
- bits = 0;
- ret = kf_ioctl(g_pfSpiDev, SPI_IOC_RD_BITS_PER_WORD, (unsigned long)&bits);
- if(ret < 0)
- {
- printk(KERN_ALERT "can't get bits per word\n");
- break;
- }
- /////////////////////////////////////////////////////////////////////
- ret = kf_ioctl(g_pfSpiDev, SPI_IOC_WR_MAX_SPEED_HZ, (unsigned long)&speed);
- if(ret < 0)
- {
- printk(KERN_ALERT "can't set max speed hz\n");
- break;
- }
- speed = 0;
- ret = kf_ioctl(g_pfSpiDev, SPI_IOC_RD_MAX_SPEED_HZ, (unsigned long)&speed);
- if(ret < 0)
- {
- printk(KERN_ALERT "can't get max speed hz\n");
- break;
- }
- /////////////////////////////////////////////////////////////////////
- printk(KERN_ALERT "mode: %hhu\n", mode);
- printk(KERN_ALERT "bits: %hhu\n", bits);
- printk(KERN_ALERT "speed: %u\n", speed);
- printk(KERN_ALERT "%s, TID: %d, \"%s\"\n", __FUNCTION__, current->pid, current->comm);
- return 0;
- }
- while(0);
- /////////////////////////////////////////////////////////////////////////
- if(g_pfSpiDev)
- kf_close(g_pfSpiDev);
- if(pKoTiva)
- {
- sysfs_remove_file(pKoTiva, &tivaFirmwareAtt.attr);
- kobject_put(pKoTiva);
- pKoTiva = NULL;
- }
- if(pKoGfa)
- {
- kobject_put(pKoGfa);
- pKoGfa = NULL;
- }
- return -ENOMEM;
- }
- /////////////////////////////////////////////////////////////////////////////
- static void drv_exit(void)
- {
- if(g_pfSpiDev)
- kf_close(g_pfSpiDev);
- if(pKoTiva)
- {
- sysfs_remove_file(pKoTiva, &tivaFirmwareAtt.attr);
- kobject_put(pKoTiva);
- }
- if(pKoGfa)
- kobject_put(pKoGfa);
- printk(KERN_ALERT "%s, TID: %d, \"%s\"\n", __FUNCTION__, current->pid, current->comm);
- }
- /////////////////////////////////////////////////////////////////////////////
- module_init(drv_init);
- module_exit(drv_exit);
|