i2c: Floppy controller bus driver
PATCH 1/1 i2c: Floppy controller bus driver
Greetings!
I recently got the idea to (re)use the (at least for me) very often unused floppy connector to attach some hardware to monitor system temperature and control some fans
the basic idea is to connect one of the Motor Control lines with the Disk Changed input to form the SDA line, and the other Motor Control line to control the SCL
this works surprisingly well, as the Motor Control lines are open drain, and the Disk Changed input is CMOS/TTL (depending on the controller)
here is the tested patch for 2.6.27-rc3 with configurable floppy base address, but hardcoded Motor signals
please consider for (mainline) inclusion!
TIA,
Herbert
drivers/i2c/busses/i2c-floppy.c +271 -0
@@ -0,0 +1,271 @@ +/* ------------------------------------------------------------------------ * +* i2c-floppy.c I2C bus over floppy controller * +* ------------------------------------------------------------------------ * +Copyright (C) 2008 Herbert Poetzl <herbert-dBHVzrDq9nF4Lj/[email protected]> + +Somewhat based on i2c-parport-light.c driver +Copyright (C) 2003-2007 Jean Delvare <[email protected]> + +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, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +* ------------------------------------------------------------------------ */ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/init.h> +#include <linux/platform_device.h> +#include <linux/ioport.h> +#include <linux/i2c.h> +#include <linux/i2c-algo-bit.h> +#include <asm/io.h> + + +#ifndef I2C_HW_B_FLOPPY +#define I2C_HW_B_FLOPPY 0x010001/* Floppy controller */ +#endif + +static struct platform_device *pdev; +static unsigned char dor =0 ; + +static u16 base =0 ; +module_param (base, ushort,0 ); +MODULE_PARM_DESC (base,"Base I/O address" ); + + + +#define DEFAULT_BASE 0x3F0 +#define DRVNAME "i2c-floppy" + + +#define FOFF_DOR 0x02 +#define FOFF_DIR 0x07 + +#define FDOR_MOTEA 0x10 +#define FDOR_MOTEB 0x20 + +#define FDIR_DCHNG 0x80 + +#define SCL FDOR_MOTEA +#define SDA FDOR_MOTEB +#define SDA_IN FDIR_DCHNG + +#define LO_INV (SDA|SCL) +#define LI_INV (SDA_IN) + + + + +/* ----- Device list ------------------------------------------------------ */ + +struct i2c_floppy_data { +unsigned char dor; +}; + +struct i2c_floppy { +struct i2c_adapter adapter; +struct i2c_algo_bit_data algo_data; +struct i2c_floppy_data data; +struct i2c_floppy *next; +}; + + +/* ----- Low-level floppy access ------------------------------------------ */ + +static void port_dor_out (unsigned char d) +{ +outb (d ^LO_INV , base +FOFF_DOR ); +} + +static unsigned char port_dir_in (void ) +{ +return inb (base +FOFF_DIR ) ^LI_INV ; +} + + +/* ----- I2C algorithm call-back functions and structures ----------------- */ + +static void floppy_setscl (void *data,int state) +{ +if (state) + dor |=SCL ; +else + dor &= ~SCL ; + +port_dor_out (dor); +} + +static void floppy_setsda (void *data,int state) +{ +if (state) + dor |=SDA ; +else + dor &= ~SDA ; + +port_dor_out (dor); +} + +static int floppy_getsda (void *data) +{ +return port_dir_in () &SDA_IN ; +} + + +/* Encapsulate the functions above in the correct structure +Note that getscl will be set to NULL by the attaching code for adapters +that cannot read SCL back */ +static struct i2c_algo_bit_data floppy_algo_data = { + .setsda = floppy_setsda, + .setscl = floppy_setscl, + .getsda = floppy_getsda, + .getscl =NULL , + .udelay =50 , + .timeout =HZ , +}; + + + +/* ----- Driver registration ---------------------------------------------- */ + +static struct i2c_adapter floppy_adapter = { + .owner =THIS_MODULE , + .class =I2C_CLASS_HWMON , + .id =I2C_HW_B_FLOPPY , + .algo_data = &floppy_algo_data, + .name ="Floppy controller adapter" , +}; + +static int __devinit i2c_floppy_probe (struct platform_device *pdev) +{ +int err; +struct resource *res; + + res =platform_get_resource (pdev,IORESOURCE_IO ,0 ); +if (!request_region (res->start, res->end - res->start +1 ,DRVNAME )) +return -EBUSY ; + +/* Reset hardware to a sane state (SCL and SDA high) */ +floppy_setsda (NULL ,1 ); +floppy_setscl (NULL ,1 ); + + floppy_adapter.dev.parent = &pdev->dev; + err =i2c_bit_add_bus (&floppy_adapter); +if (err) { +dev_err (&pdev->dev,"Unable to register with I2C\n" ); + goto exit_region; + } +return 0 ; + +exit_region: +release_region (res->start, res->end - res->start +1 ); +return err; +} + +static int __devexit i2c_floppy_remove(struct platform_device *pdev) +{ +struct resource *res; + +i2c_del_adapter (&floppy_adapter); + + res =platform_get_resource (pdev,IORESOURCE_IO ,0 ); +release_region (res->start, res->end - res->start +1 ); +return 0 ; +} + +static struct platform_driver i2c_floppy_driver = { + .driver = { + .owner =THIS_MODULE , + .name =DRVNAME , + }, + .probe = i2c_floppy_probe, + .remove =__devexit_p (i2c_floppy_remove), +}; + +static int __init i2c_floppy_device_add (u16 address) +{ +struct resource res = { + .start = address, + .end = address +7 , + .name =DRVNAME , + .flags =IORESOURCE_IO , + }; +int err; + + pdev =platform_device_alloc (DRVNAME ,-1 ); +if (!pdev) { + err = -ENOMEM ; + printk(KERN_ERR DRVNAME ": Device allocation failed\n"); + goto exit; + } + + err =platform_device_add_resources (pdev, &res,1 ); +if (err) { + printk(KERN_ERR DRVNAME ": Device resource addition failed " + "(%d)\n", err); + goto exit_device_put; + } + + err =platform_device_add (pdev); +if (err) { + printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", + err); + goto exit_device_put; + } + +return 0 ; + +exit_device_put: +platform_device_put (pdev); +exit: +return err; +} + +static int __init i2c_floppy_init (void ) +{ +int err; + +if (base ==0 ) { +pr_info (DRVNAME ": using default base 0x%x\n" ,DEFAULT_BASE ); + base =DEFAULT_BASE ; + } + +/* Sets global pdev as a side effect */ + err =i2c_floppy_device_add (base); +if (err) + goto exit; + + err =platform_driver_register (&i2c_floppy_driver); +if (err) + goto exit_device; + +return 0 ; + +exit_device: +platform_device_unregister (pdev); +exit: +return err; +} + +static void __exit i2c_floppy_exit(void) +{ +platform_driver_unregister (&i2c_floppy_driver); +platform_device_unregister (pdev); +} + + +MODULE_AUTHOR ("Herbert Poetzl <herbert-dBHVzrDq9nF4Lj/[email protected]>" ); +MODULE_DESCRIPTION ("I2C bus over floppy controller" ); +MODULE_LICENSE ("GPL" ); + +module_init (i2c_floppy_init); +module_exit (i2c_floppy_exit);
drivers/i2c/busses/Kconfig +11 -0
@@ -564,6 +564,17 @@ config I2C_TINY_USB This driver can also be built as a module. If so, the module will be called i2c-tiny-usb. +config I2C_FLOPPY + tristate "Floppy controller adapter" + select I2C_ALGOBIT + help + This supports floppy controller I2C adapters using the motor + control lines for SDA and SCL, and the drive change for SDA + readback. + + This support is also available as a module. If so, the module + will be called i2c-floppy. + comment "Graphics adapter I2C/DDC channel drivers" depends on PCI
drivers/i2c/busses/Makefile +1 -0
@@ -52,6 +52,7 @@ obj-$(CONFIG_I2C_PARPORT) += i2c-parport obj-$(CONFIG_I2C_PARPORT_LIGHT) += i2c-parport-light.o obj-$(CONFIG_I2C_TAOS_EVM) += i2c-taos-evm.o obj-$(CONFIG_I2C_TINY_USB) += i2c-tiny-usb.o +obj-$(CONFIG_I2C_FLOPPY) += i2c-floppy.o # Graphics adapter I2C/DDC channel drivers obj-$(CONFIG_I2C_VOODOO3) += i2c-voodoo3.o
include/linux/i2c-id.h +1 -0
@@ -91,6 +91,7 @@/* --- Bit algorithm adapters */ #define I2C_HW_B_LP 0x010000/* Parallel port Philips style */ +#define I2C_HW_B_FLOPPY 0x010001/* Floppy controller */ #define I2C_HW_B_BT848 0x010005/* BT848 video boards */ #define I2C_HW_B_VIA 0x010007/* Via vt82c586b */ #define I2C_HW_B_HYDRA 0x010008/* Apple Hydra Mac I/O */
_______________________________________________
i2c mailing list
[email protected]
http://lists.lm-sensors.org/mailman/listinfo/i2c
Hi Herbert,
Quoted 11 lines · Herbert Poetzl
Greetings!
I recently got the idea to (re)use the (at least for me) very often unused floppy connector to attach some hardware to monitor system temperature and control some fans
the basic idea is to connect one of the Motor Control lines with the Disk Changed input to form the SDA line, and the other Motor Control line to control the SCL
Quoted 6 lines
this works surprisingly well, as the Motor Control lines are open drain, and the Disk Changed input is CMOS/TTL (depending on the controller)
here is the tested patch for 2.6.27-rc3 with configurable floppy base address, but hardcoded Motor signals
That's fine with me, as long as the default address is somewhat standard.
Quoted 37 lines
please consider for (mainline) inclusion!
TIA,
Herbertdiff -NurpP --minimal
linux-2.6.27-rc3/drivers/i2c/busses/i2c-floppy.clinux-2.6.27-rc3-fi2c-v0.1/drivers/i2c/busses/i2c-floppy.cdrivers/i2c/busses/i2c-floppy.c +26
-0@@ -0,0 +1,271 @@ +/* ------------------------------------------------------------------------ * +* i2c-floppy.c I2C bus over floppy controller * +* ------------------------------------------------------------------------ * +Copyright (C) 2008 Herbert Poetzl <herbert-dBHVzrDq9nF4Lj/[email protected]> + +Somewhat based on i2c-parport-light.c driver +Copyright (C) 2003-2007 Jean Delvare <[email protected]> + +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, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +* ------------------------------------------------------------------------ */ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/init.h>
drivers/i2c/busses/i2c-floppy.c line 36
Quoted 10 lines
drivers/i2c/busses/i2c-floppy.c +10
-0+#include <linux/platform_device.h> +#include <linux/ioport.h> +#include <linux/i2c.h> +#include <linux/i2c-algo-bit.h> +#include <asm/io.h> + + +#ifndef I2C_HW_B_FLOPPY +#define I2C_HW_B_FLOPPY 0x010001/* Floppy controller */ +#endif
As discussed on IRC, these driver IDs are optional and will be removed soon. So please don't define a new one.
drivers/i2c/busses/i2c-floppy.c line 41
Quoted 5 lines
drivers/i2c/busses/i2c-floppy.c +5
-0+ +static struct platform_device *pdev; +static unsigned char dor =0 ; + +static u16 base =0 ;
Do not initialize static globals to 0 - the compiler does it for you. BTW, please run scripts/checkpatch.pl on your patch and fix the errors and warnings.
drivers/i2c/busses/i2c-floppy.c line 47
Quoted 6 lines
drivers/i2c/busses/i2c-floppy.c +6
-0+module_param (base, ushort,0 ); +MODULE_PARM_DESC (base,"Base I/O address" ); + + + +#define DEFAULT_BASE 0x3F0
Is this address any standard?
Quoted 11 lines
drivers/i2c/busses/i2c-floppy.c +11
-0+#define DRVNAME "i2c-floppy" + + +#define FOFF_DOR 0x02 +#define FOFF_DIR 0x07 + +#define FDOR_MOTEA 0x10 +#define FDOR_MOTEB 0x20 + +#define FDIR_DCHNG 0x80 +
drivers/i2c/busses/i2c-floppy.c line 68
Quoted 10 lines
drivers/i2c/busses/i2c-floppy.c +10
-0+#define SCL FDOR_MOTEA +#define SDA FDOR_MOTEB +#define SDA_IN FDIR_DCHNG + +#define LO_INV (SDA|SCL) +#define LI_INV (SDA_IN) + + + +
Fewer blank lines wouldn't hurt.
drivers/i2c/busses/i2c-floppy.c line 73
Quoted 5 lines
drivers/i2c/busses/i2c-floppy.c +5
-0+/* ----- Device list ------------------------------------------------------ */ + +struct i2c_floppy_data { +unsigned char dor; +};
That's a bit confusing. You define a structure for private data (which is good) but apparently the driver code instead accesses a global variable?
drivers/i2c/busses/i2c-floppy.c line 79
Quoted 6 lines
drivers/i2c/busses/i2c-floppy.c +6
-0+ +struct i2c_floppy { +struct i2c_adapter adapter; +struct i2c_algo_bit_data algo_data; +struct i2c_floppy_data data; +struct i2c_floppy *next;
Your driver doesn't actually support multiple devices, so this last pointer is useless.
Hmm, looking at the rest of the driver code, you don't actually use this structure anywhere? This definitely needs some cleanup.
Quoted 4 lines
drivers/i2c/busses/i2c-floppy.c +4
-0+}; + + +/* ----- Low-level floppy access ------------------------------------------ */
drivers/i2c/busses/i2c-floppy.c line 93
Quoted 10 lines
drivers/i2c/busses/i2c-floppy.c +10
-0+ +static void port_dor_out (unsigned char d) +{ +outb (d ^LO_INV , base +FOFF_DOR ); +} + +static unsigned char port_dir_in (void ) +{ +return inb (base +FOFF_DIR ) ^LI_INV ; +}
These two functions are certainly worth inlining.
Quoted 23 lines
drivers/i2c/busses/i2c-floppy.c +23
-0+ + +/* ----- I2C algorithm call-back functions and structures ----------------- */ + +static void floppy_setscl (void *data,int state) +{ +if (state) + dor |=SCL ; +else + dor &= ~SCL ; + +port_dor_out (dor); +} + +static void floppy_setsda (void *data,int state) +{ +if (state) + dor |=SDA ; +else + dor &= ~SDA ; + +port_dor_out (dor); +}
drivers/i2c/busses/i2c-floppy.c line 126
Quoted 10 lines
drivers/i2c/busses/i2c-floppy.c +10
-0+ +static int floppy_getsda (void *data) +{ +return port_dir_in () &SDA_IN ; +} + + +/* Encapsulate the functions above in the correct structure +Note that getscl will be set to NULL by the attaching code for adapters +that cannot read SCL back */
You must have copied this comment from another driver... but it doesn't apply to yours.
drivers/i2c/busses/i2c-floppy.c line 131
Quoted 5 lines
drivers/i2c/busses/i2c-floppy.c +5
-0+static struct i2c_algo_bit_data floppy_algo_data = { + .setsda = floppy_setsda, + .setscl = floppy_setscl, + .getsda = floppy_getsda, + .getscl =NULL ,
No need to initialize to NULL explicitly, the compiler does it for you.
drivers/i2c/busses/i2c-floppy.c line 143
Quoted 12 lines
drivers/i2c/busses/i2c-floppy.c +12
-0+ .udelay = 50, + .timeout = HZ, +}; + + + +/* ----- Driver registration ---------------------------------------------- */ + +static struct i2c_adapter floppy_adapter = { + .owner =THIS_MODULE , + .class =I2C_CLASS_HWMON , + .id =I2C_HW_B_FLOPPY ,
Drop.
Quoted 25 lines
drivers/i2c/busses/i2c-floppy.c +25
-0+ .algo_data = &floppy_algo_data, + .name = "Floppy controller adapter", +}; + +static int __devinit i2c_floppy_probe (struct platform_device *pdev) +{ +int err; +struct resource *res; + + res =platform_get_resource (pdev,IORESOURCE_IO ,0 ); +if (!request_region (res->start, res->end - res->start +1 ,DRVNAME )) +return -EBUSY ; + +/* Reset hardware to a sane state (SCL and SDA high) */ +floppy_setsda (NULL ,1 ); +floppy_setscl (NULL ,1 ); + + floppy_adapter.dev.parent = &pdev->dev; + err =i2c_bit_add_bus (&floppy_adapter); +if (err) { +dev_err (&pdev->dev,"Unable to register with I2C\n" ); + goto exit_region; + } +return 0 ; +
drivers/i2c/busses/i2c-floppy.c line 178
Quoted 10 lines
drivers/i2c/busses/i2c-floppy.c +10
-0+exit_region: +release_region (res->start, res->end - res->start +1 ); +return err; +} + +static int __devexit i2c_floppy_remove(struct platform_device *pdev) +{ +struct resource *res; + +i2c_del_adapter (&floppy_adapter);
In general, "remove" functions should avoid accessing globals. You should always be able to get access to everything you need based on the device that is passed into parameter. Just use platform_get_drvdata (and use platform_set_drvdata in the "probe" function.)
Quoted 97 lines
drivers/i2c/busses/i2c-floppy.c +93
-0+ + res =platform_get_resource (pdev,IORESOURCE_IO ,0 ); +release_region (res->start, res->end - res->start +1 ); +return 0 ; +} + +static struct platform_driver i2c_floppy_driver = { + .driver = { + .owner =THIS_MODULE , + .name =DRVNAME , + }, + .probe = i2c_floppy_probe, + .remove =__devexit_p (i2c_floppy_remove), +}; + +static int __init i2c_floppy_device_add (u16 address) +{ +struct resource res = { + .start = address, + .end = address +7 , + .name =DRVNAME , + .flags =IORESOURCE_IO , + }; +int err; + + pdev =platform_device_alloc (DRVNAME ,-1 ); +if (!pdev) { + err = -ENOMEM ; + printk(KERN_ERR DRVNAME ": Device allocation failed\n"); + goto exit; + } + + err =platform_device_add_resources (pdev, &res,1 ); +if (err) { + printk(KERN_ERR DRVNAME ": Device resource addition failed " + "(%d)\n", err); + goto exit_device_put; + } + + err =platform_device_add (pdev); +if (err) { + printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", + err); + goto exit_device_put; + } + +return 0 ; + +exit_device_put: +platform_device_put (pdev); +exit: +return err; +} + +static int __init i2c_floppy_init (void ) +{ +int err; + +if (base ==0 ) { +pr_info (DRVNAME ": using default base 0x%x\n" ,DEFAULT_BASE ); + base =DEFAULT_BASE ; + } + +/* Sets global pdev as a side effect */ + err =i2c_floppy_device_add (base); +if (err) + goto exit; + + err =platform_driver_register (&i2c_floppy_driver); +if (err) + goto exit_device; + +return 0 ; + +exit_device: +platform_device_unregister (pdev); +exit: +return err; +} + +static void __exit i2c_floppy_exit(void) +{ +platform_driver_unregister (&i2c_floppy_driver); +platform_device_unregister (pdev); +} + + +MODULE_AUTHOR ("Herbert Poetzl <herbert-dBHVzrDq9nF4Lj/[email protected]>" ); +MODULE_DESCRIPTION ("I2C bus over floppy controller" ); +MODULE_LICENSE ("GPL" ); + +module_init (i2c_floppy_init); +module_exit (i2c_floppy_exit);diff -NurpP --minimal linux-2.6.27-rc3/drivers/i2c/busses/Kconfig linux-2.6.27-rc3-fi2c-v0.1/drivers/i2c/busses/Kconfig
drivers/i2c/busses/Kconfig config I2C_TINY_USB · line 567
Quoted 4 lines
drivers/i2c/busses/Kconfig +1
-0@@ -564,6 +564,17 @@ config I2C_TINY_USB This driver can also be built as a module. If so, the module will be called i2c-tiny-usb. +config I2C_FLOPPY
Right subsection, but please follow the alphabetical order.
drivers/i2c/busses/Kconfig config I2C_TINY_USB · line 569
Quoted 2 lines
drivers/i2c/busses/Kconfig +2
-0+ tristate "Floppy controller adapter" + select I2C_ALGOBIT
You probably want to add "default n", as most users will never need this driver.
drivers/i2c/busses/Kconfig config I2C_TINY_USB · line 576
Quoted 7 lines
drivers/i2c/busses/Kconfig +7
-0+ help + This supports floppy controller I2C adapters using the motor + control lines for SDA and SCL, and the drive change for SDA + readback. + + This support is also available as a module. If so, the module + will be called i2c-floppy.
This lacks a warning that this driver is meant for do-it-yourself hardware and most people can safely say no.
Out of curiosity, what would happen if someone was to run this driver with actual floppy disk drives connected?
drivers/i2c/busses/Makefile obj-$(CONFIG_I2C_PARPORT) += i2c-parport · line 55
Quoted 12 lines
drivers/i2c/busses/Kconfig +1
-0+ comment "Graphics adapter I2C/DDC channel drivers" depends on PCIdiff -NurpP --minimal linux-2.6.27-rc3/drivers/i2c/busses/Makefile linux-2.6.27-rc3-fi2c-v0.1/drivers/i2c/busses/Makefile
drivers/i2c/busses/Makefile +1
-0@@ -52,6 +52,7 @@ obj-$(CONFIG_I2C_PARPORT) += i2c-parport obj-$(CONFIG_I2C_PARPORT_LIGHT) += i2c-parport-light.o obj-$(CONFIG_I2C_TAOS_EVM) += i2c-taos-evm.o obj-$(CONFIG_I2C_TINY_USB) += i2c-tiny-usb.o +obj-$(CONFIG_I2C_FLOPPY) += i2c-floppy.o
Alphabetical order please.
Quoted 7 lines
drivers/i2c/busses/Makefile +0
-0# Graphics adapter I2C/DDC channel drivers obj-$(CONFIG_I2C_VOODOO3) += i2c-voodoo3.odiff -NurpP --minimal
linux-2.6.27-rc3/include/linux/i2c-id.hlinux-2.6.27-rc3-fi2c-v0.1/include/linux/i2c-id.h
include/linux/i2c-id.h line 97
Quoted 7 lines
include/linux/i2c-id.h +1
-0@@ -91,6 +91,7 @@/* --- Bit algorithm adapters */ #define I2C_HW_B_LP 0x010000/* Parallel port Philips style */ +#define I2C_HW_B_FLOPPY 0x010001/* Floppy controller */ #define I2C_HW_B_BT848 0x010005/* BT848 video boards */ #define I2C_HW_B_VIA 0x010007/* Via vt82c586b */ #define I2C_HW_B_HYDRA 0x010008/* Apple Hydra Mac I/O */
Your patch misses Documentation/i2c/busses/i2c-floppy explaining how to build the hardware, things to pay attention to, etc.
Signature · 6 lines
Jean Delvare _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c
Quoted 18 lines · Jean Delvare
Hi Herbert,
Quoted 15 lines · Herbert Poetzl
Greetings!
I recently got the idea to (re)use the (at least for me) very often unused floppy connector to attach some hardware to monitor system temperature and control some fans
the basic idea is to connect one of the Motor Control lines with the Disk Changed input to form the SDA line, and the other Motor Control line to control the SCL
this works surprisingly well, as the Motor Control lines are open drain, and the Disk Changed input is CMOS/TTL (depending on the controller)
Quoted 5 lines
Quoted 2 lines
here is the tested patch for 2.6.27-rc3 with configurable floppy base address, but hardcoded Motor signals
That's fine with me, as long as the default address is somewhat standard.
yep, see below ...
Quoted 50 lines
Quoted 36 lines
please consider for (mainline) inclusion!
TIA,
Herbertdiff -NurpP --minimal
linux-2.6.27-rc3/drivers/i2c/busses/i2c-floppy.clinux-2.6.27-rc3-fi2c-v0.1/drivers/i2c/busses/i2c-floppy.cdrivers/i2c/busses/i2c-floppy.c +26
-0@@ -0,0 +1,271 @@ +/* ------------------------------------------------------------------------ * +* i2c-floppy.c I2C bus over floppy controller * +* ------------------------------------------------------------------------ * +Copyright (C) 2008 Herbert Poetzl <herbert-dBHVzrDq9nF4Lj/[email protected]> + +Somewhat based on i2c-parport-light.c driver +Copyright (C) 2003-2007 Jean Delvare <[email protected]> + +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, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +* ------------------------------------------------------------------------ */ + +#include <linux/kernel.h> +#include <linux/module.h> +#include <linux/init.h> drivers/i2c/busses/i2c-floppy.c line 36
Quoted 10 lines
drivers/i2c/busses/i2c-floppy.c +10
-0+#include <linux/platform_device.h> +#include <linux/ioport.h> +#include <linux/i2c.h> +#include <linux/i2c-algo-bit.h> +#include <asm/io.h> + + +#ifndef I2C_HW_B_FLOPPY +#define I2C_HW_B_FLOPPY 0x010001/* Floppy controller */ +#endif As discussed on IRC, these driver IDs are optional and will be removed soon. So please don't define a new one.
Quoted 9 lines
drivers/i2c/busses/i2c-floppy.c line 41
Quoted 5 lines
drivers/i2c/busses/i2c-floppy.c +5
-0+ +static struct platform_device *pdev; +static unsigned char dor =0 ; + +static u16 base =0 ;Do not initialize static globals to 0 - the compiler does it for you. BTW, please run
scripts/checkpatch.plon your patch and fix the errors and warnings.
okay, will do, thanks for the hint
Quoted 8 lines
drivers/i2c/busses/i2c-floppy.c line 47
Quoted 6 lines
drivers/i2c/busses/i2c-floppy.c +6
-0+module_param (base, ushort,0 ); +MODULE_PARM_DESC (base,"Base I/O address" ); + + + +#define DEFAULT_BASE 0x3F0Is this address any standard?
yep, it is the default address for PC floppy controllers
http://www.pcguide.com/ref/fdd/confUsage-c.html
Quoted 15 lines
Quoted 15 lines
drivers/i2c/busses/i2c-floppy.c +15
-0+#define DRVNAME "i2c-floppy" + + +#define FOFF_DOR 0x02 +#define FOFF_DIR 0x07 + +#define FDOR_MOTEA 0x10 +#define FDOR_MOTEB 0x20 + +#define FDIR_DCHNG 0x80 + +#define SCL FDOR_MOTEA +#define SDA FDOR_MOTEB +#define SDA_IN FDIR_DCHNG +
Quoted 8 lines
drivers/i2c/busses/i2c-floppy.c line 68
Quoted 6 lines
drivers/i2c/busses/i2c-floppy.c +6
-0+#define LO_INV (SDA|SCL) +#define LI_INV (SDA_IN) + + + +Fewer blank lines wouldn't hurt.
k, no problem ...
Quoted 9 lines
drivers/i2c/busses/i2c-floppy.c line 73
Quoted 5 lines
drivers/i2c/busses/i2c-floppy.c +5
-0+/* ----- Device list ------------------------------------------------------ */ + +struct i2c_floppy_data { +unsigned char dor; +};That's a bit confusing. You define a structure for private data (which is good) but apparently the driver code instead accesses a global variable?
yep, sorry, is a leftover from the previous version which was more like the i2c-parport driver than the i2c-parport-light, will remove it for now ...
Quoted 9 lines
drivers/i2c/busses/i2c-floppy.c line 79
Quoted 6 lines
drivers/i2c/busses/i2c-floppy.c +6
-0+ +struct i2c_floppy { +struct i2c_adapter adapter; +struct i2c_algo_bit_data algo_data; +struct i2c_floppy_data data; +struct i2c_floppy *next;Your driver doesn't actually support multiple devices, so this last pointer is useless.
correct, another leftover ...
Quoted 19 lines
Hmm, looking at the rest of the driver code, you don't actually use this structure anywhere? This definitely needs some cleanup.
Quoted 4 lines
drivers/i2c/busses/i2c-floppy.c +4
-0+}; + + +/* ----- Low-level floppy access ------------------------------------------ */ drivers/i2c/busses/i2c-floppy.c line 93
Quoted 10 lines
drivers/i2c/busses/i2c-floppy.c +10
-0+ +static void port_dor_out (unsigned char d) +{ +outb (d ^LO_INV , base +FOFF_DOR ); +} + +static unsigned char port_dir_in (void ) +{ +return inb (base +FOFF_DIR ) ^LI_INV ; +}These two functions are certainly worth inlining.
okay, static inline fine?
Quoted 28 lines
Quoted 28 lines
drivers/i2c/busses/i2c-floppy.c +28
-0+ + +/* ----- I2C algorithm call-back functions and structures ----------------- */ + +static void floppy_setscl (void *data,int state) +{ +if (state) + dor |=SCL ; +else + dor &= ~SCL ; + +port_dor_out (dor); +} + +static void floppy_setsda (void *data,int state) +{ +if (state) + dor |=SDA ; +else + dor &= ~SDA ; + +port_dor_out (dor); +} + +static int floppy_getsda (void *data) +{ +return port_dir_in () &SDA_IN ; +}
Quoted 8 lines
drivers/i2c/busses/i2c-floppy.c line 126
Quoted 5 lines
drivers/i2c/busses/i2c-floppy.c +5
-0+ + +/* Encapsulate the functions above in the correct structure +Note that getscl will be set to NULL by the attaching code for adapters +that cannot read SCL back */ You must have copied this comment from another driver... but it doesn't apply to yours.
yep .. will clean that up
Quoted 7 lines
drivers/i2c/busses/i2c-floppy.c line 131
Quoted 5 lines
drivers/i2c/busses/i2c-floppy.c +5
-0+static struct i2c_algo_bit_data floppy_algo_data = { + .setsda = floppy_setsda, + .setscl = floppy_setscl, + .getsda = floppy_getsda, + .getscl =NULL ,No need to initialize to NULL explicitly, the compiler does it for you.
yes, I know, I just wanted to keep it there to remind me that scl cannot be read back atm
Quoted 6 lines
Quoted 6 lines
drivers/i2c/busses/i2c-floppy.c +6
-0+ .udelay = 50, + .timeout = HZ, +}; + + +
Quoted 8 lines
drivers/i2c/busses/i2c-floppy.c line 143
Quoted 6 lines
drivers/i2c/busses/i2c-floppy.c +6
-0+/* ----- Driver registration ---------------------------------------------- */ + +static struct i2c_adapter floppy_adapter = { + .owner =THIS_MODULE , + .class =I2C_CLASS_HWMON , + .id =I2C_HW_B_FLOPPY ,Drop.
k, will do
Quoted 32 lines
Quoted 32 lines
drivers/i2c/busses/i2c-floppy.c +32
-0+ .algo_data = &floppy_algo_data, + .name = "Floppy controller adapter", +}; + +static int __devinit i2c_floppy_probe (struct platform_device *pdev) +{ +int err; +struct resource *res; + + res =platform_get_resource (pdev,IORESOURCE_IO ,0 ); +if (!request_region (res->start, res->end - res->start +1 ,DRVNAME )) +return -EBUSY ; + +/* Reset hardware to a sane state (SCL and SDA high) */ +floppy_setsda (NULL ,1 ); +floppy_setscl (NULL ,1 ); + + floppy_adapter.dev.parent = &pdev->dev; + err =i2c_bit_add_bus (&floppy_adapter); +if (err) { +dev_err (&pdev->dev,"Unable to register with I2C\n" ); + goto exit_region; + } +return 0 ; + +exit_region: +release_region (res->start, res->end - res->start +1 ); +return err; +} + +static int __devexit i2c_floppy_remove(struct platform_device *pdev) +{
Quoted 8 lines
drivers/i2c/busses/i2c-floppy.c line 178
Quoted 3 lines
drivers/i2c/busses/i2c-floppy.c +3
-0+struct resource *res; + +i2c_del_adapter (&floppy_adapter);In general, "remove" functions should avoid accessing globals. You should always be able to get access to everything you need based on the device that is passed into parameter. Just use
platform_get_drvdata(and useplatform_set_drvdatain the "probe" function.)
hmm, is that something you plan to change in the i2c-parport-light driver too, if so, please point me to the patch, I'll gladly adjust it to match that
Quoted 100 lines
Quoted 100 lines
drivers/i2c/busses/i2c-floppy.c +93
-0+ + res =platform_get_resource (pdev,IORESOURCE_IO ,0 ); +release_region (res->start, res->end - res->start +1 ); +return 0 ; +} + +static struct platform_driver i2c_floppy_driver = { + .driver = { + .owner =THIS_MODULE , + .name =DRVNAME , + }, + .probe = i2c_floppy_probe, + .remove =__devexit_p (i2c_floppy_remove), +}; + +static int __init i2c_floppy_device_add (u16 address) +{ +struct resource res = { + .start = address, + .end = address +7 , + .name =DRVNAME , + .flags =IORESOURCE_IO , + }; +int err; + + pdev =platform_device_alloc (DRVNAME ,-1 ); +if (!pdev) { + err = -ENOMEM ; + printk(KERN_ERR DRVNAME ": Device allocation failed\n"); + goto exit; + } + + err =platform_device_add_resources (pdev, &res,1 ); +if (err) { + printk(KERN_ERR DRVNAME ": Device resource addition failed " + "(%d)\n", err); + goto exit_device_put; + } + + err =platform_device_add (pdev); +if (err) { + printk(KERN_ERR DRVNAME ": Device addition failed (%d)\n", + err); + goto exit_device_put; + } + +return 0 ; + +exit_device_put: +platform_device_put (pdev); +exit: +return err; +} + +static int __init i2c_floppy_init (void ) +{ +int err; + +if (base ==0 ) { +pr_info (DRVNAME ": using default base 0x%x\n" ,DEFAULT_BASE ); + base =DEFAULT_BASE ; + } + +/* Sets global pdev as a side effect */ + err =i2c_floppy_device_add (base); +if (err) + goto exit; + + err =platform_driver_register (&i2c_floppy_driver); +if (err) + goto exit_device; + +return 0 ; + +exit_device: +platform_device_unregister (pdev); +exit: +return err; +} + +static void __exit i2c_floppy_exit(void) +{ +platform_driver_unregister (&i2c_floppy_driver); +platform_device_unregister (pdev); +} + + +MODULE_AUTHOR ("Herbert Poetzl <herbert-dBHVzrDq9nF4Lj/[email protected]>" ); +MODULE_DESCRIPTION ("I2C bus over floppy controller" ); +MODULE_LICENSE ("GPL" ); + +module_init (i2c_floppy_init); +module_exit (i2c_floppy_exit);diff -NurpP --minimal linux-2.6.27-rc3/drivers/i2c/busses/Kconfig linux-2.6.27-rc3-fi2c-v0.1/drivers/i2c/busses/Kconfig
drivers/i2c/busses/Kconfig +0
-0@@ -564,6 +564,17 @@ config I2C_TINY_USB This driver can also be built as a module. If so, the module will be called i2c-tiny-usb.
Quoted 3 lines
drivers/i2c/busses/Kconfig config I2C_TINY_USB · line 567
Quoted 1 line
drivers/i2c/busses/Kconfig +1
-0+config I2C_FLOPPYRight subsection, but please follow the alphabetical order.
okay, will do
Quoted 5 lines
drivers/i2c/busses/Kconfig config I2C_TINY_USB · line 569
Quoted 2 lines
drivers/i2c/busses/Kconfig +2
-0+ tristate "Floppy controller adapter" + select I2C_ALGOBITYou probably want to add "default n", as most users will never need this driver.
yep, that's correct ... for now :)
Quoted 11 lines
drivers/i2c/busses/Kconfig config I2C_TINY_USB · line 576
Quoted 7 lines
drivers/i2c/busses/Kconfig +7
-0+ help + This supports floppy controller I2C adapters using the motor + control lines for SDA and SCL, and the drive change for SDA + readback. + + This support is also available as a module. If so, the module + will be called i2c-floppy.This lacks a warning that this driver is meant for do-it-yourself hardware and most people can safely say no.
Quoted 2 lines
Out of curiosity, what would happen if someone was to run this driver with actual floppy disk drives connected?
I'd say the drive(s) will spin up a little, but that's about all that can happen, the driver doesn't do anything 'bad' with the floppy controller
the other way round is probably more a problem, as the floppy driver will not handle the changing input line that gracefully, but that is a problem users of this kind of hardware should be able to deal with
Quoted 4 lines
Quoted 4 lines
drivers/i2c/busses/Kconfig +1
-0+ comment "Graphics adapter I2C/DDC channel drivers" depends on PCI
Quoted 10 lines
drivers/i2c/busses/Makefile obj-$(CONFIG_I2C_PARPORT) += i2c-parport · line 55
Quoted 8 lines
diff -NurpP --minimal linux-2.6.27-rc3/drivers/i2c/busses/Makefile linux-2.6.27-rc3-fi2c-v0.1/drivers/i2c/busses/Makefile
drivers/i2c/busses/Makefile +1
-0@@ -52,6 +52,7 @@ obj-$(CONFIG_I2C_PARPORT) += i2c-parport obj-$(CONFIG_I2C_PARPORT_LIGHT) += i2c-parport-light.o obj-$(CONFIG_I2C_TAOS_EVM) += i2c-taos-evm.o obj-$(CONFIG_I2C_TINY_USB) += i2c-tiny-usb.o +obj-$(CONFIG_I2C_FLOPPY) += i2c-floppy.oAlphabetical order please.
k, consider it done
Quoted 8 lines
Quoted 8 lines
drivers/i2c/busses/Makefile +0
-0# Graphics adapter I2C/DDC channel drivers obj-$(CONFIG_I2C_VOODOO3) += i2c-voodoo3.odiff -NurpP --minimal
linux-2.6.27-rc3/include/linux/i2c-id.hlinux-2.6.27-rc3-fi2c-v0.1/include/linux/i2c-id.hinclude/linux/i2c-id.h +0
-0@@ -91,6 +91,7 @@
Quoted 9 lines
include/linux/i2c-id.h line 97
Quoted 6 lines
include/linux/i2c-id.h +1
-0/* --- Bit algorithm adapters */ #define I2C_HW_B_LP 0x010000/* Parallel port Philips style */ +#define I2C_HW_B_FLOPPY 0x010001/* Floppy controller */ #define I2C_HW_B_BT848 0x010005/* BT848 video boards */ #define I2C_HW_B_VIA 0x010007/* Via vt82c586b */ #define I2C_HW_B_HYDRA 0x010008/* Apple Hydra Mac I/O */ Your patch misses
Documentation/i2c/busses/i2c-floppyexplaining how to build the hardware, things to pay attention to, etc.
okay, something like the i2c-parport-light is fine? (including the pinout description of course)
thanks for the feedback,
Herbert
PS: expect an updated version soon ...
Quoted 2 lines
Signature · 1 line
Jean Delvare
_______________________________________________
i2c mailing list [email protected]
http://lists.lm-sensors.org/mailman/listinfo/i2c
Quoted 15 lines · Herbert Poetzl
Quoted 13 lines · Jean Delvare
drivers/i2c/busses/i2c-floppy.c line 93
Quoted 10 lines · Herbert Poetzl
(...)
drivers/i2c/busses/i2c-floppy.c +9
-0+static void port_dor_out (unsigned char d) +{ +outb (d ^LO_INV , base +FOFF_DOR ); +} + +static unsigned char port_dir_in (void ) +{ +return inb (base +FOFF_DIR ) ^LI_INV ; +}These two functions are certainly worth inlining.
Quoted 1 line
okay, static inline fine?
Yes.
Quoted 9 lines
Quoted 5 lines
(...)
In general, "remove" functions should avoid accessing globals. You should always be able to get access to everything you need based on the device that is passed into parameter. Just useplatform_get_drvdata(and useplatform_set_drvdatain the "probe" function.)hmm, is that something you plan to change in the i2c-parport-light driver too, if so, please point me to the patch, I'll gladly adjust it to match that
In fact, I was counting on you to fix your driver and then send a patch cleaning up i2c-parport-light in a similar way ;)
Quoted 10 lines
Quoted 6 lines
(...)
This lacks a warning that this driver is meant for do-it-yourself hardware and most people can safely say no.Out of curiosity, what would happen if someone was to run this driver with actual floppy disk drives connected?
I'd say the drive(s) will spin up a little, but that's about all that can happen, the driver doesn't do anything 'bad' with the floppy controller
No risk that the relatively fast motor on/off switching could damage it?
Quoted 4 lines
the other way round is probably more a problem, as the floppy driver will not handle the changing input line that gracefully, but that is a problem users of this kind of hardware should be able to deal with
Correct, but that's still worth a warning in the documentation. Better safe than sorry.
Quoted 6 lines
Quoted 3 lines
(...)
Your patch missesDocumentation/i2c/busses/i2c-floppyexplaining how to build the hardware, things to pay attention to, etc.okay, something like the i2c-parport-light is fine? (including the pinout description of course)
Yes.
Signature · 6 lines
Jean Delvare _______________________________________________ i2c mailing list [email protected] http://lists.lm-sensors.org/mailman/listinfo/i2c