From 399d6b26539d83dd734746dc2292d53fbc5807b2 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 10 Aug 2008 22:56:15 +0200 Subject: i2c: Fix oops on bus multiplexer driver loading The two I2C bus multiplexer drivers (i2c-amd756-s4882 and i2c-nforce2-s4985) make use of the bus they want to multiplex before checking if it is really present. Swap the instructions to test for presence first. This fixes a oops reported by Ingo Molnar. Signed-off-by: Jean Delvare Cc: Ingo Molnar --- drivers/i2c/busses/i2c-amd756-s4882.c | 9 ++++----- drivers/i2c/busses/i2c-nforce2-s4985.c | 5 +++-- 2 files changed, 7 insertions(+), 7 deletions(-) (limited to 'drivers') diff --git a/drivers/i2c/busses/i2c-amd756-s4882.c b/drivers/i2c/busses/i2c-amd756-s4882.c index 72872d1e63ef..8ba2bcf727d3 100644 --- a/drivers/i2c/busses/i2c-amd756-s4882.c +++ b/drivers/i2c/busses/i2c-amd756-s4882.c @@ -155,6 +155,9 @@ static int __init amd756_s4882_init(void) int i, error; union i2c_smbus_data ioconfig; + if (!amd756_smbus.dev.parent) + return -ENODEV; + /* Configure the PCA9556 multiplexer */ ioconfig.byte = 0x00; /* All I/O to output mode */ error = i2c_smbus_xfer(&amd756_smbus, 0x18, 0, I2C_SMBUS_WRITE, 0x03, @@ -168,11 +171,7 @@ static int __init amd756_s4882_init(void) /* Unregister physical bus */ error = i2c_del_adapter(&amd756_smbus); if (error) { - if (error == -EINVAL) - error = -ENODEV; - else - dev_err(&amd756_smbus.dev, "Physical bus removal " - "failed\n"); + dev_err(&amd756_smbus.dev, "Physical bus removal failed\n"); goto ERROR0; } diff --git a/drivers/i2c/busses/i2c-nforce2-s4985.c b/drivers/i2c/busses/i2c-nforce2-s4985.c index d1a4cbcf2aa4..29015eb9ca46 100644 --- a/drivers/i2c/busses/i2c-nforce2-s4985.c +++ b/drivers/i2c/busses/i2c-nforce2-s4985.c @@ -150,6 +150,9 @@ static int __init nforce2_s4985_init(void) int i, error; union i2c_smbus_data ioconfig; + if (!nforce2_smbus) + return -ENODEV; + /* Configure the PCA9556 multiplexer */ ioconfig.byte = 0x00; /* All I/O to output mode */ error = i2c_smbus_xfer(nforce2_smbus, 0x18, 0, I2C_SMBUS_WRITE, 0x03, @@ -161,8 +164,6 @@ static int __init nforce2_s4985_init(void) } /* Unregister physical bus */ - if (!nforce2_smbus) - return -ENODEV; error = i2c_del_adapter(nforce2_smbus); if (error) { dev_err(&nforce2_smbus->dev, "Physical bus removal failed\n"); -- cgit v1.2.2 From b25b791b13aaa336b56c4f9bd417ff126363f80b Mon Sep 17 00:00:00 2001 From: Hans Verkuil Date: Sun, 10 Aug 2008 22:56:15 +0200 Subject: i2c: Fix NULL pointer dereference in i2c_new_probed_device Fix a NULL pointer dereference that happened when calling i2c_new_probed_device on one of the addresses for which we use byte reads instead of quick write for detection purpose (that is: 0x30-0x37 and 0x50-0x5f). Signed-off-by: Hans Verkuil Signed-off-by: Jean Delvare --- drivers/i2c/i2c-core.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 7bf38c418086..c16dcad94412 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c @@ -1451,9 +1451,11 @@ i2c_new_probed_device(struct i2c_adapter *adap, if ((addr_list[i] & ~0x07) == 0x30 || (addr_list[i] & ~0x0f) == 0x50 || !i2c_check_functionality(adap, I2C_FUNC_SMBUS_QUICK)) { + union i2c_smbus_data data; + if (i2c_smbus_xfer(adap, addr_list[i], 0, I2C_SMBUS_READ, 0, - I2C_SMBUS_BYTE, NULL) >= 0) + I2C_SMBUS_BYTE, &data) >= 0) break; } else { if (i2c_smbus_xfer(adap, addr_list[i], 0, -- cgit v1.2.2 From 8d24f8dcb7ead491704e274883b2c627062f6235 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 10 Aug 2008 22:56:15 +0200 Subject: i2c: Let users select algorithm drivers manually again In kernel 2.6.26, the ability to select I2C algorithm drivers manually was removed, as all in-kernel drivers do that automatically. However there were some complaints that it was a problem for out-of-tree I2C bus drivers. In order to address these complaints, let's allow manual selection of these drivers again, but still hide them by default for better general user experience. This closes bug #11140: http://bugzilla.kernel.org/show_bug.cgi?id=11140 Signed-off-by: Jean Delvare --- drivers/i2c/Kconfig | 14 ++++++++++++++ drivers/i2c/algos/Kconfig | 11 ++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'drivers') diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig index 96867347bcbf..711ca08ab776 100644 --- a/drivers/i2c/Kconfig +++ b/drivers/i2c/Kconfig @@ -38,6 +38,20 @@ config I2C_CHARDEV This support is also available as a module. If so, the module will be called i2c-dev. +config I2C_HELPER_AUTO + bool "Autoselect pertinent helper modules" + default y + help + Some I2C bus drivers require so-called "I2C algorithm" modules + to work. These are basically software-only abstractions of generic + I2C interfaces. This option will autoselect them so that you don't + have to care. + + Unselect this only if you need to enable additional helper + modules, for example for use with external I2C bus drivers. + + In doubt, say Y. + source drivers/i2c/algos/Kconfig source drivers/i2c/busses/Kconfig source drivers/i2c/chips/Kconfig diff --git a/drivers/i2c/algos/Kconfig b/drivers/i2c/algos/Kconfig index 7137a17402fe..b788579b8227 100644 --- a/drivers/i2c/algos/Kconfig +++ b/drivers/i2c/algos/Kconfig @@ -2,15 +2,20 @@ # I2C algorithm drivers configuration # +menu "I2C Algorithms" + depends on !I2C_HELPER_AUTO + config I2C_ALGOBIT - tristate + tristate "I2C bit-banging interfaces" config I2C_ALGOPCF - tristate + tristate "I2C PCF 8584 interfaces" config I2C_ALGOPCA - tristate + tristate "I2C PCA 9564 interfaces" config I2C_ALGO_SGI tristate depends on SGI_IP22 || SGI_IP32 || X86_VISWS + +endmenu -- cgit v1.2.2 From c1159f9e8927f5732c19816a605926bc76c498b2 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 10 Aug 2008 22:56:16 +0200 Subject: i2c: Check for address business before creating clients We check for address business in i2c_probe_address(), i2c_detect_address() and i2c_new_probed_device(), but this isn't sufficient. Drivers can call i2c_attach_client() and i2c_new_device() on any address, so we must check the address there as well. This fixes bug #11239: http://bugzilla.kernel.org/show_bug.cgi?id=11239 Signed-off-by: Jean Delvare --- drivers/i2c/i2c-core.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'drivers') diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index c16dcad94412..550853f79ae8 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c @@ -813,7 +813,12 @@ static int i2c_check_addr(struct i2c_adapter *adapter, int addr) int i2c_attach_client(struct i2c_client *client) { struct i2c_adapter *adapter = client->adapter; - int res = 0; + int res; + + /* Check for address business */ + res = i2c_check_addr(adapter, client->addr); + if (res) + return res; client->dev.parent = &client->adapter->dev; client->dev.bus = &i2c_bus_type; -- cgit v1.2.2 From 2ce5b34fd519275d788338ae692e4b71df6661d4 Mon Sep 17 00:00:00 2001 From: David Brownell Date: Sun, 10 Aug 2008 22:56:16 +0200 Subject: i2c: correct some size_t printk formats Fix various printk format strings where %zd was passed a size_t; those should be %zu instead. (Courtesy of a version of GCC which warns when these details are wrong.) Signed-off-by: David Brownell Signed-off-by: Jean Delvare --- drivers/i2c/chips/at24.c | 8 ++++---- drivers/i2c/i2c-dev.c | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) (limited to 'drivers') diff --git a/drivers/i2c/chips/at24.c b/drivers/i2c/chips/at24.c index e764c94f3e3d..2a4acb269569 100644 --- a/drivers/i2c/chips/at24.c +++ b/drivers/i2c/chips/at24.c @@ -188,7 +188,7 @@ static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf, count = I2C_SMBUS_BLOCK_MAX; status = i2c_smbus_read_i2c_block_data(client, offset, count, buf); - dev_dbg(&client->dev, "smbus read %zd@%d --> %d\n", + dev_dbg(&client->dev, "smbus read %zu@%d --> %d\n", count, offset, status); return (status < 0) ? -EIO : status; } @@ -214,7 +214,7 @@ static ssize_t at24_eeprom_read(struct at24_data *at24, char *buf, msg[1].len = count; status = i2c_transfer(client->adapter, msg, 2); - dev_dbg(&client->dev, "i2c read %zd@%d --> %d\n", + dev_dbg(&client->dev, "i2c read %zu@%d --> %d\n", count, offset, status); if (status == 2) @@ -334,7 +334,7 @@ static ssize_t at24_eeprom_write(struct at24_data *at24, char *buf, if (status == 1) status = count; } - dev_dbg(&client->dev, "write %zd@%d --> %zd (%ld)\n", + dev_dbg(&client->dev, "write %zu@%d --> %zd (%ld)\n", count, offset, status, jiffies); if (status == count) @@ -512,7 +512,7 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) i2c_set_clientdata(client, at24); - dev_info(&client->dev, "%Zd byte %s EEPROM %s\n", + dev_info(&client->dev, "%zu byte %s EEPROM %s\n", at24->bin.size, client->name, writable ? "(writable)" : "(read-only)"); dev_dbg(&client->dev, diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index 9d55c6383b23..af4491fa7e34 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -147,7 +147,7 @@ static ssize_t i2cdev_read (struct file *file, char __user *buf, size_t count, if (tmp==NULL) return -ENOMEM; - pr_debug("i2c-dev: i2c-%d reading %zd bytes.\n", + pr_debug("i2c-dev: i2c-%d reading %zu bytes.\n", iminor(file->f_path.dentry->d_inode), count); ret = i2c_master_recv(client,tmp,count); @@ -175,7 +175,7 @@ static ssize_t i2cdev_write (struct file *file, const char __user *buf, size_t c return -EFAULT; } - pr_debug("i2c-dev: i2c-%d writing %zd bytes.\n", + pr_debug("i2c-dev: i2c-%d writing %zu bytes.\n", iminor(file->f_path.dentry->d_inode), count); ret = i2c_master_send(client,tmp,count); -- cgit v1.2.2 From 8ff69eebf5bf8a123a117b78412d5efb85765d8b Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 10 Aug 2008 22:56:16 +0200 Subject: hwmon: (lm75) Drop legacy i2c driver Drop the legacy lm75 driver, and add a detect callback to the new-style driver to achieve the same functionality. Signed-off-by: Jean Delvare Cc: David Brownell --- drivers/hwmon/lm75.c | 114 ++++++++++----------------------------------------- 1 file changed, 22 insertions(+), 92 deletions(-) (limited to 'drivers') diff --git a/drivers/hwmon/lm75.c b/drivers/hwmon/lm75.c index 7880c273c2c5..8f9595f2fb53 100644 --- a/drivers/hwmon/lm75.c +++ b/drivers/hwmon/lm75.c @@ -54,11 +54,11 @@ enum lm75_type { /* keep sorted in alphabetical order */ tmp75, }; -/* Addresses scanned by legacy style driver binding */ +/* Addresses scanned */ static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, I2C_CLIENT_END }; -/* Insmod parameters (only for legacy style driver binding) */ +/* Insmod parameters */ I2C_CLIENT_INSMOD_1(lm75); @@ -72,7 +72,6 @@ static const u8 LM75_REG_TEMP[3] = { /* Each client has this additional data */ struct lm75_data { - struct i2c_client *client; struct device *hwmon_dev; struct mutex update_lock; u8 orig_conf; @@ -138,7 +137,7 @@ static const struct attribute_group lm75_group = { /*-----------------------------------------------------------------------*/ -/* "New style" I2C driver binding -- following the driver model */ +/* device probe and removal */ static int lm75_probe(struct i2c_client *client, const struct i2c_device_id *id) @@ -157,8 +156,6 @@ lm75_probe(struct i2c_client *client, const struct i2c_device_id *id) return -ENOMEM; i2c_set_clientdata(client, data); - - data->client = client; mutex_init(&data->update_lock); /* Set to LM75 resolution (9 bits, 1/2 degree C) and range. @@ -236,45 +233,16 @@ static const struct i2c_device_id lm75_ids[] = { }; MODULE_DEVICE_TABLE(i2c, lm75_ids); -static struct i2c_driver lm75_driver = { - .driver = { - .name = "lm75", - }, - .probe = lm75_probe, - .remove = lm75_remove, - .id_table = lm75_ids, -}; - -/*-----------------------------------------------------------------------*/ - -/* "Legacy" I2C driver binding */ - -static struct i2c_driver lm75_legacy_driver; - -/* This function is called by i2c_probe */ -static int lm75_detect(struct i2c_adapter *adapter, int address, int kind) +/* Return 0 if detection is successful, -ENODEV otherwise */ +static int lm75_detect(struct i2c_client *new_client, int kind, + struct i2c_board_info *info) { + struct i2c_adapter *adapter = new_client->adapter; int i; - struct i2c_client *new_client; - int err = 0; if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA)) - goto exit; - - /* OK. For now, we presume we have a valid address. We create the - client structure, even though there may be no sensor present. - But it allows us to use i2c_smbus_read_*_data() calls. */ - new_client = kzalloc(sizeof *new_client, GFP_KERNEL); - if (!new_client) { - err = -ENOMEM; - goto exit; - } - - new_client->addr = address; - new_client->adapter = adapter; - new_client->driver = &lm75_legacy_driver; - new_client->flags = 0; + return -ENODEV; /* Now, we do the remaining detection. There is no identification- dedicated register so we have to rely on several tricks: @@ -294,71 +262,44 @@ static int lm75_detect(struct i2c_adapter *adapter, int address, int kind) || i2c_smbus_read_word_data(new_client, 5) != hyst || i2c_smbus_read_word_data(new_client, 6) != hyst || i2c_smbus_read_word_data(new_client, 7) != hyst) - goto exit_free; + return -ENODEV; os = i2c_smbus_read_word_data(new_client, 3); if (i2c_smbus_read_word_data(new_client, 4) != os || i2c_smbus_read_word_data(new_client, 5) != os || i2c_smbus_read_word_data(new_client, 6) != os || i2c_smbus_read_word_data(new_client, 7) != os) - goto exit_free; + return -ENODEV; /* Unused bits */ if (conf & 0xe0) - goto exit_free; + return -ENODEV; /* Addresses cycling */ for (i = 8; i < 0xff; i += 8) if (i2c_smbus_read_byte_data(new_client, i + 1) != conf || i2c_smbus_read_word_data(new_client, i + 2) != hyst || i2c_smbus_read_word_data(new_client, i + 3) != os) - goto exit_free; + return -ENODEV; } /* NOTE: we treat "force=..." and "force_lm75=..." the same. * Only new-style driver binding distinguishes chip types. */ - strlcpy(new_client->name, "lm75", I2C_NAME_SIZE); - - /* Tell the I2C layer a new client has arrived */ - err = i2c_attach_client(new_client); - if (err) - goto exit_free; - - err = lm75_probe(new_client, NULL); - if (err < 0) - goto exit_detach; + strlcpy(info->type, "lm75", I2C_NAME_SIZE); return 0; - -exit_detach: - i2c_detach_client(new_client); -exit_free: - kfree(new_client); -exit: - return err; -} - -static int lm75_attach_adapter(struct i2c_adapter *adapter) -{ - if (!(adapter->class & I2C_CLASS_HWMON)) - return 0; - return i2c_probe(adapter, &addr_data, lm75_detect); } -static int lm75_detach_client(struct i2c_client *client) -{ - lm75_remove(client); - i2c_detach_client(client); - kfree(client); - return 0; -} - -static struct i2c_driver lm75_legacy_driver = { +static struct i2c_driver lm75_driver = { + .class = I2C_CLASS_HWMON, .driver = { - .name = "lm75_legacy", + .name = "lm75", }, - .attach_adapter = lm75_attach_adapter, - .detach_client = lm75_detach_client, + .probe = lm75_probe, + .remove = lm75_remove, + .id_table = lm75_ids, + .detect = lm75_detect, + .address_data = &addr_data, }; /*-----------------------------------------------------------------------*/ @@ -424,22 +365,11 @@ static struct lm75_data *lm75_update_device(struct device *dev) static int __init sensors_lm75_init(void) { - int status; - - status = i2c_add_driver(&lm75_driver); - if (status < 0) - return status; - - status = i2c_add_driver(&lm75_legacy_driver); - if (status < 0) - i2c_del_driver(&lm75_driver); - - return status; + return i2c_add_driver(&lm75_driver); } static void __exit sensors_lm75_exit(void) { - i2c_del_driver(&lm75_legacy_driver); i2c_del_driver(&lm75_driver); } -- cgit v1.2.2