SPI core -- revisited
RFC 1/1 SPI core -- revisited
Hello guys,
we finally decided to rework the SPI core and now it its ready for your comments.. Here we have several boards equipped with SPI bus, and use this spi core with these boards; Drivers for them are available by request (...and if community approve this patch)
drivers/Kconfig | 2 drivers/Makefile | 1 drivers/spi/Kconfig | 31 ++++ drivers/spi/Makefile | 9 + drivers/spi/helpers.c | 45 ++++++ drivers/spi/spi-core.c | 244 ++++++++++++++++++++++++++++++++++ drivers/spi/spi-dev.c | 337 ++++++++++++++++++++++++++++++++++++++++++++++++ drivers/spi/spi.h | 35 ++++ include/linux/spi/spi.h | 205 +++++++++++++++++++++++++++++ 9 files changed, 909 insertions(+)
drivers/Kconfig +2 -0
@@ -42,6 +42,8 @@ source "drivers/i2c/Kconfig" +source "drivers/spi/Kconfig" + source "drivers/w1/Kconfig" source "drivers/misc/Kconfig"
drivers/Makefile +1 -0
@@ -64,3 +64,4 @@ obj-$(CONFIG_SGI_IOC4) += sn/ obj-y += firmware/ obj-$(CONFIG_CRYPTO) += crypto/ +obj-$(CONFIG_SPI) += spi/
drivers/spi/helpers.c +45 -0
45 lines not on this page · raw message
drivers/spi/Kconfig +31 -0
@@ -0,0 +1,31 @@ +# +# SPI device configuration +# +menu "SPI support" + +config SPI + tristate "SPI support" + default false + help + Say Y if you need to enable SPI support on your kernel + +config SPI_DEBUG + bool "SPI debug output" + depends on SPI + default false + help + Say Y there if you'd like to see debug output from SPI drivers. + If unsure, say N + +config SPI_CHARDEV + tristate "SPI device interface" + depends on SPI + help + Say Y here to use spi-* device files, usually found in the /dev + directory on your system. They make it possible to have user-space + programs use the SPI bus. + This support is also available as a module. If so, the module + will be called spi-dev. + +endmenu +
drivers/spi/Makefile +9 -0
@@ -0,0 +1,9 @@ +# +# Makefile for the kernel spi bus driver. +# + +# Init order: core, chardev, bit adapters, pcf adapters + +obj-$(CONFIG_SPI) += spi-core.o helpers.o +obj-$(CONFIG_SPI_CHARDEV) += spi-dev.o +
drivers/spi/spi-core.c +244 -0
244 lines not on this page · raw message
drivers/spi/spi-dev.c +337 -0
337 lines not on this page · raw message
drivers/spi/spi.h +35 -0
@@ -0,0 +1,35 @@ +/* +* linux/drivers/spi/spi.h +* +* Copyright (C) 2005 MontaVista +* +* Local structures and macros +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License. +* +*/ +#ifndef __SPI_LOCAL_H +#define __SPI_LOCAL_H + +#define SPI_DRV ( n ) container_of( n, struct spi_driver, driver ) +#define SPI_ADAP ( n ) container_of( n, struct spi_adapter, dev ) + +typedef struct { +const void *id; +void *found; +}spi_srch_context_t ; + +#define SPI_STUFF_FOUND -ECANCELED + +/* helpers.c */ +int driver_for_each_dev (struct device_driver *drv,void *data, +int (*callback) (struct device * dev,void *data)); + +// #undef pr_debug +//#define pr_debug printk +#define ENTER () pr_debug( "%s: ENTERed\n", __FUNCTION__ ) +#define LEAVE () pr_debug( "%s: LEFT OUT\n", __FUNCTION__ ) + +#endif /* __SPI_LOCAL_H */
include/linux/spi/spi.h +205 -0
205 lines not on this page · raw message
Quoted 6 lines · dmitry pervushin
Hello guys,
we finally decided to rework the SPI core and now it its ready for your comments.. Here we have several boards equipped with SPI bus, and use this spi core with these boards; Drivers for them are available by request (...and if community approve this patch)
I'm glad to see that work is progressing on SPI core. I've worked on drivers on both ARM linux and Blackfin uclinux that use SPI and would prefer that they not be platform specific.
What I've found in my Blackfin work is that I need asynchronous SPI support. The driver starts an SPI transaction and receives a callback when the SPI transaction has completed. The operations are similar to what you've suggested, except that the message structure includes a pointer to a callback and a void *data pointer.
The driver I'm working on is for the Chipcon CC2420 802.15.4 radio, which uses SPI to access its registers and transmit and receive FIFOs. In my CC2420, it queues SPI requests and initiates the next one when an SPI transaction completes.
Jamey
Quoted 8 lines · Jamey Hicks
Quoted 3 lines · dmitry pervushin
we finally decided to rework the SPI core and now it its ready for your comments.. Here we have several boards equipped with SPI bus, and use this spi core with these boards; Drivers for them are available by request (...and if community approve this patch)
I'm glad to see that work is progressing on SPI core. I've worked on drivers on both ARM linux and Blackfin uclinux that use SPI and would prefer that they not be platform specific.
I worry about SPI at the moment because I can't see how it's being used from just this code.
The worry I have is that it appears to contain an algorithm layer. Would this be better as a library for drivers to use, or something like that?
The reason I bring up this point is that my L3 layer is over-complex for what it does (despite being about 378 lines) because it tried far too hard to look like the I2C layer - soo much so I'm not happy with it for mainline.
(I also have some concerns with the amount of NULL pointer checking in the SPI code...)
Signature · 3 lines
Russell King Linux kernel 2.6 ARM Linux - http://www.arm.linux.org.uk/ maintainer of: 2.6 Serial core
Quoted 13 lines · Russell King
Quoted 9 lines · Jamey Hicks
Quoted 3 lines · dmitry pervushin
we finally decided to rework the SPI core and now it its ready for your comments.. Here we have several boards equipped with SPI bus, and use this spi core with these boards; Drivers for them are available by request (...and if community approve this patch)
I'm glad to see that work is progressing on SPI core. I've worked on drivers on both ARM linux and Blackfin uclinux that use SPI and would prefer that they not be platform specific.
Quoted 8 lines
I worry about SPI at the moment because I can't see how it's being used from just this code.
The worry I have is that it appears to contain an algorithm layer. Would this be better as a library for drivers to use, or something like that?
That's a good point. I think the only algorithm that really gets shared is the bitbanging one, which could be a library, and otherwise it is controller specific and might as well be in the adapter.
Jamey
Hi Dmitry,
Quoted 4 lines
we finally decided to rework the SPI core and now it its ready for your comments.. Here we have several boards equipped with SPI bus, and use this spi core with these boards; Drivers for them are available by request (...and if community approve this patch)
I'm not exactly the best qualified person to comment on your work, but I'd have a couple remarks nevertheless:
Quoted 37 lines
drivers/spi/helpers.c +34
-0@@ -0,0 +1,45 @@ +/* +* drivers/spi/helper.c +* +* Helper functions. +* +* Author: dmitry pervushin <[email protected]> +* +* 2004 (c) MontaVista Software, Inc. This file is licensed under +* the terms of the GNU General Public License version 2. This program +* is licensed "as is" without any warranty of any kind, whether express +* or implied. +*/ + +#include <linux/module.h> +#include <linux/config.h> +#include <linux/kernel.h> +#include <linux/device.h> + +typedef struct { +int (*callback) (struct device * dev,void *data); +struct device_driver *drv; +void *data; +}_find_t ; + +static int dfed_callback (struct device *device,void *data) +{ +_find_t *local_data = (_find_t *) data; + +return (device->driver != local_data->drv) ?0 : + (*local_data->callback) (device, local_data->data); +} + +int driver_for_each_dev (struct device_driver *drv,void *data, +int (*callback) (struct device * dev,void *data))
drivers/spi/helpers.c line 44
Quoted 10 lines
drivers/spi/helpers.c +10
-0+{ +_find_t local_data; + + local_data.drv = drv; + local_data.data = data; + local_data.callback = callback; +return bus_for_each_dev (drv->bus,NULL , &local_data, dfed_callback); +} + +EXPORT_SYMBOL (driver_for_each_dev);
This stuff doesn't seem to be spi-specific at all. It would rather be driver core material, right?
At any rate, I don't think anybody would enjoy a kernel module named "helpers", whether spi-specific or not.
Quoted 2 lines
drivers/spi/helpers.c +1
-0+MODULE_AUTHOR("Jamey Hicks <[email protected]> Frodo Looijaard<[email protected]> and Simon G. Vogl <[email protected]>");
Please leave Frodo and Simon out of this. Crediting them in the headers is fine if this work is derived from theirs, but they definitely are not the authors of these modules.
include/linux/spi/spi.h
Quoted 5 lines
drivers/spi/spi.h +0
-0include/linux/spi/spi.h +0
-0
I don't think it's very smart to have two different files with the same name in the kernel tree. More likely than not it will create confusion at some point in time.
include/linux/spi/spi.h line 72
Quoted 6 lines
include/linux/spi/spi.h +6
-0+#define SELECT 0x01 +#define UNSELECT 0x02 +#define BEFORE_READ 0x03 +#define BEFORE_WRITE 0x04 +#define AFTER_READ 0x05 +#define AFTER_WRITE 0x06
This doesn't seem to belong there. You don't seem to use these values anywhere in the spi core, and if you did these constants would need to be prefixed with SPI_.
Thanks,
Signature · 1 line
Jean Delvare
drivers/spi/helpers.c line 44
Quoted 12 lines · dmitry pervushin
drivers/spi/helpers.c +12
-0+int driver_for_each_dev (struct device_driver *drv,void *data, +int (*callback) (struct device * dev,void *data)) +{ +_find_t local_data; + + local_data.drv = drv; + local_data.data = data; + local_data.callback = callback; +return bus_for_each_dev (drv->bus,NULL , &local_data, dfed_callback); +} + +EXPORT_SYMBOL (driver_for_each_dev);
Use the built-in kernel functions, don't create a new one and go around the _GPL export.
drivers/spi/spi-core.c line 17
Quoted 3 lines
drivers/spi/spi-core.c +3
-0+#ifdef CONFIG_SPI_DEBUG +#define DEBUG +#endif /* CONFIG_SPI_DEBUG */
Have the Makefile define this for you, it's not needed in the .c file
Quoted 1 line
+#include <
linux/spi/spi.h>
Why a separate subdirectory? It should just go in include/linux/ like everything else.
drivers/spi/spi-core.c line 40
Quoted 1 line
drivers/spi/spi-core.c +1
-0+if (NULL == dev ||NULL == driver) {
dev == NULL instead of the other way around (yes, I know why you do this, but the rest of the kernel is the other way, and gcc will warn you if you forget a '=').
drivers/spi/spi-dev.c line 95
Quoted 3 lines
drivers/spi/spi-dev.c +3
-0+SPI_IDS_TABLE_BEGIN + SPI_ID_ANY +SPI_IDS_TABLE_END
Ick, that's a horrible way to define things. Please make it look like real .c code.
drivers/spi/spi-dev.c line 100
Quoted 4 lines
drivers/spi/spi-dev.c +4
-0+static struct spi_driver spidev_driver = { + .owner =THIS_MODULE , + .driver = { + .name ="generic spi" ,
No spaces in driver names please.
drivers/spi/spi-dev.c line 121
Quoted 2 lines
drivers/spi/spi-dev.c +2
-0+ drvdata = (spidev_driver_data_t *)kmalloc (sizeof (spidev_driver_data_t), +GFP_KERNEL );
Cast is unnecessary.
Also, get rid of all typedefs, they are not acceptable.
drivers/spi/spi-dev.c line 135
Quoted 4 lines
drivers/spi/spi-dev.c +4
-0+#ifdef CONFIG_DEVFS_FS +devfs_mk_cdev (MKDEV (SPI_MAJOR , drvdata->minor), +S_IFCHR |S_IRUSR |S_IWUSR ,"spi/%d" , drvdata->minor); +#endif
Don't put #ifdef in code where it is not needed at all.
drivers/spi/spi-dev.c line 148
Quoted 3 lines
drivers/spi/spi-dev.c +3
-0+if (NULL == dev) { +printk (KERN_ERR "%s: removing the NULL device\n" , __FUNCTION__); + }
Extra { } are not needed.
What is this checking for anyway?
Use dev_err() for these things.
drivers/spi/spi-dev.c line 172
Quoted 8 lines
drivers/spi/spi-dev.c +8
-0+static ssize_t spidev_read (struct file *file,char *buf,size_t count, +loff_t * offset) +{ +char *tmp; +int ret =0 ; +#ifdef DEBUG +struct inode *inode = file->f_dentry->d_inode; +#endif
#ifdef not nice to have here, it's not needed.
Quoted 1 line
+#define
SPI_STUFF_FOUND-ECANCELED
Just use the raw error number, don't redefine it.
include/linux/spi/spi.h line 92
Quoted 6 lines
include/linux/spi/spi.h +6
-0+struct spi_adapter { +/* +* This name is used to uniquely identify the adapter. +* It should be the same as the module name. +*/ +char name[32 ];
Why not use the device.bus_id instead?
I too would like to see how this code is used, example drivers please.
thanks,
greg k-h