spi设备驱动模型

#include <linux/init.h>
#include <linux/module.h>
#include <linux/ioctl.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/err.h>
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/compat.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/spi/spi.h>
#include <linux/spi/spidev.h>
#include <linux/uaccess.h>


#define SPIDEV_MAJOR 153 /* assigned */
#define N_SPI_MINORS 32 /* ... up to 256 */


static DECLARE_BITMAP(minors, N_SPI_MINORS);


struct spidev_data {
dev_t devt;
spinlock_t spi_lock;
struct spi_device *spi;
struct list_head device_entry;
struct mutex buf_lock;
unsigned users;
u8 *tx_buffer;
u8 *rx_buffer;
u32 speed_hz;
};


static LIST_HEAD(device_list);
static DEFINE_MUTEX(device_list_lock);


static ssize_t
spidev_sync(struct spidev_data *spidev, struct spi_message *message)
{
return 0;
}


static inline ssize_t
spidev_sync_write(struct spidev_data *spidev, size_t len)
{
return 0;
}


static inline ssize_t
spidev_sync_read(struct spidev_data *spidev, size_t len)
{
return 0;
}


/* Read-only message with current device setup */
static ssize_t
spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
return 0;
}


/* Write-only message with current device setup */
static ssize_t
spidev_write(struct file *filp, const char __user *buf,
size_t count, loff_t *f_pos)
{
return 0;
}


static int spidev_message(struct spidev_data *spidev,
struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
{
return 0;
}


static struct spi_ioc_transfer *
spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc,
unsigned *n_ioc)
{
return 0;
}


static long
spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
return 0;
}


#ifdef CONFIG_COMPAT
static long
spidev_compat_ioc_message(struct file *filp, unsigned int cmd,
unsigned long arg)
{
return 0;
}


static long
spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
return 0;
}
#else
#define spidev_compat_ioctl NULL
#endif /* CONFIG_COMPAT */


static int spidev_open(struct inode *inode, struct file *filp)
{
return 0;
}


static int spidev_release(struct inode *inode, struct file *filp)
{
return 0;
}


static const struct file_operations spidev_fops = {
.owner = THIS_MODULE,
.write = spidev_write,
.read = spidev_read,
.unlocked_ioctl = spidev_ioctl,
.compat_ioctl = spidev_compat_ioctl,
.open = spidev_open,
.release = spidev_release,
};


-----------------------------------------------------------




static struct class *spidev_class;


#ifdef CONFIG_OF
static const struct of_device_id spidev_dt_ids[] = {
{ .compatible = "rohm,dh2228fv" },
{ .compatible = "lineartechnology,ltc2488" },
{},
};
MODULE_DEVICE_TABLE(of, spidev_dt_ids);
#endif






static int spidev_probe(struct spi_device *spi)
{
spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
dev = device_create(spidev_class, &spi->dev, spidev->devt,
   spidev, "spidev%d.%d",
   spi->master->bus_num, spi->chip_select);
return status;
}


static int spidev_remove(struct spi_device *spi)
{
device_destroy(spidev_class, spidev->devt);
return 0;
}


static struct spi_driver spidev_spi_driver = {
.driver = {
.name = "spidev",
.of_match_table = of_match_ptr(spidev_dt_ids),
},
.probe = spidev_probe,
.remove = spidev_remove,
};




static int __init spidev_init(void)
{
int status;
status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
spidev_class = class_create(THIS_MODULE, "spidev");
status = spi_register_driver(&spidev_spi_driver);
return status;
}
module_init(spidev_init);


static void __exit spidev_exit(void)
{
spi_unregister_driver(&spidev_spi_driver);
class_destroy(spidev_class);
unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
}
module_exit(spidev_exit);


MODULE_LICENSE("GPL");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Linux老A

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值