#include #include // printk() #include // struct kobject #include // error codes #include // THIS_MODULE #include #include #include #include #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);