2019-05-27 14:55:06 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* i2c-amd756-s4882.c - i2c-amd756 extras for the Tyan S4882 motherboard
|
|
|
|
*
|
2014-01-30 03:40:08 +08:00
|
|
|
* Copyright (C) 2004, 2008 Jean Delvare <jdelvare@suse.de>
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We select the channels by sending commands to the Philips
|
|
|
|
* PCA9556 chip at I2C address 0x18. The main adapter is used for
|
|
|
|
* the non-multiplexed part of the bus, and 4 virtual adapters
|
|
|
|
* are defined for the multiplexed addresses: 0x50-0x53 (memory
|
|
|
|
* module EEPROM) located on channels 1-4, and 0x4c (LM63)
|
|
|
|
* located on multiplexed channels 0 and 5-7. We define one
|
|
|
|
* virtual adapter per CPU, which corresponds to two multiplexed
|
|
|
|
* channels:
|
|
|
|
* CPU0: virtual adapter 1, channels 1 and 0
|
|
|
|
* CPU1: virtual adapter 2, channels 2 and 5
|
|
|
|
* CPU2: virtual adapter 3, channels 3 and 6
|
|
|
|
* CPU3: virtual adapter 4, channels 4 and 7
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/i2c.h>
|
2006-01-19 06:16:04 +08:00
|
|
|
#include <linux/mutex.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
extern struct i2c_adapter amd756_smbus;
|
|
|
|
|
|
|
|
static struct i2c_adapter *s4882_adapter;
|
|
|
|
static struct i2c_algorithm *s4882_algo;
|
|
|
|
|
|
|
|
/* Wrapper access functions for multiplexed SMBus */
|
2006-02-28 06:09:22 +08:00
|
|
|
static DEFINE_MUTEX(amd756_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
static s32 amd756_access_virt0(struct i2c_adapter * adap, u16 addr,
|
|
|
|
unsigned short flags, char read_write,
|
|
|
|
u8 command, int size,
|
|
|
|
union i2c_smbus_data * data)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* We exclude the multiplexed addresses */
|
|
|
|
if (addr == 0x4c || (addr & 0xfc) == 0x50 || (addr & 0xfc) == 0x30
|
|
|
|
|| addr == 0x18)
|
2008-07-15 04:38:25 +08:00
|
|
|
return -ENXIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-19 06:16:04 +08:00
|
|
|
mutex_lock(&amd756_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
|
|
|
|
command, size, data);
|
|
|
|
|
2006-01-19 06:16:04 +08:00
|
|
|
mutex_unlock(&amd756_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We remember the last used channels combination so as to only switch
|
|
|
|
channels when it is really needed. This greatly reduces the SMBus
|
|
|
|
overhead, but also assumes that nobody will be writing to the PCA9556
|
|
|
|
in our back. */
|
|
|
|
static u8 last_channels;
|
|
|
|
|
|
|
|
static inline s32 amd756_access_channel(struct i2c_adapter * adap, u16 addr,
|
|
|
|
unsigned short flags, char read_write,
|
|
|
|
u8 command, int size,
|
|
|
|
union i2c_smbus_data * data,
|
|
|
|
u8 channels)
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
/* We exclude the non-multiplexed addresses */
|
|
|
|
if (addr != 0x4c && (addr & 0xfc) != 0x50 && (addr & 0xfc) != 0x30)
|
2008-07-15 04:38:25 +08:00
|
|
|
return -ENXIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-01-19 06:16:04 +08:00
|
|
|
mutex_lock(&amd756_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (last_channels != channels) {
|
|
|
|
union i2c_smbus_data mplxdata;
|
|
|
|
mplxdata.byte = channels;
|
|
|
|
|
|
|
|
error = amd756_smbus.algo->smbus_xfer(adap, 0x18, 0,
|
|
|
|
I2C_SMBUS_WRITE, 0x01,
|
|
|
|
I2C_SMBUS_BYTE_DATA,
|
|
|
|
&mplxdata);
|
|
|
|
if (error)
|
|
|
|
goto UNLOCK;
|
|
|
|
last_channels = channels;
|
|
|
|
}
|
|
|
|
error = amd756_smbus.algo->smbus_xfer(adap, addr, flags, read_write,
|
|
|
|
command, size, data);
|
|
|
|
|
|
|
|
UNLOCK:
|
2006-01-19 06:16:04 +08:00
|
|
|
mutex_unlock(&amd756_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static s32 amd756_access_virt1(struct i2c_adapter * adap, u16 addr,
|
|
|
|
unsigned short flags, char read_write,
|
|
|
|
u8 command, int size,
|
|
|
|
union i2c_smbus_data * data)
|
|
|
|
{
|
|
|
|
/* CPU0: channels 1 and 0 enabled */
|
|
|
|
return amd756_access_channel(adap, addr, flags, read_write, command,
|
|
|
|
size, data, 0x03);
|
|
|
|
}
|
|
|
|
|
|
|
|
static s32 amd756_access_virt2(struct i2c_adapter * adap, u16 addr,
|
|
|
|
unsigned short flags, char read_write,
|
|
|
|
u8 command, int size,
|
|
|
|
union i2c_smbus_data * data)
|
|
|
|
{
|
|
|
|
/* CPU1: channels 2 and 5 enabled */
|
|
|
|
return amd756_access_channel(adap, addr, flags, read_write, command,
|
|
|
|
size, data, 0x24);
|
|
|
|
}
|
|
|
|
|
|
|
|
static s32 amd756_access_virt3(struct i2c_adapter * adap, u16 addr,
|
|
|
|
unsigned short flags, char read_write,
|
|
|
|
u8 command, int size,
|
|
|
|
union i2c_smbus_data * data)
|
|
|
|
{
|
|
|
|
/* CPU2: channels 3 and 6 enabled */
|
|
|
|
return amd756_access_channel(adap, addr, flags, read_write, command,
|
|
|
|
size, data, 0x48);
|
|
|
|
}
|
|
|
|
|
|
|
|
static s32 amd756_access_virt4(struct i2c_adapter * adap, u16 addr,
|
|
|
|
unsigned short flags, char read_write,
|
|
|
|
u8 command, int size,
|
|
|
|
union i2c_smbus_data * data)
|
|
|
|
{
|
|
|
|
/* CPU3: channels 4 and 7 enabled */
|
|
|
|
return amd756_access_channel(adap, addr, flags, read_write, command,
|
|
|
|
size, data, 0x90);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init amd756_s4882_init(void)
|
|
|
|
{
|
|
|
|
int i, error;
|
|
|
|
union i2c_smbus_data ioconfig;
|
|
|
|
|
2008-08-11 04:56:15 +08:00
|
|
|
if (!amd756_smbus.dev.parent)
|
|
|
|
return -ENODEV;
|
|
|
|
|
2008-07-17 01:30:05 +08:00
|
|
|
/* 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,
|
|
|
|
I2C_SMBUS_BYTE_DATA, &ioconfig);
|
|
|
|
if (error) {
|
|
|
|
dev_err(&amd756_smbus.dev, "PCA9556 configuration failed\n");
|
|
|
|
error = -EIO;
|
|
|
|
goto ERROR0;
|
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Unregister physical bus */
|
2013-03-09 16:16:46 +08:00
|
|
|
i2c_del_adapter(&amd756_smbus);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
printk(KERN_INFO "Enabling SMBus multiplexing for Tyan S4882\n");
|
|
|
|
/* Define the 5 virtual adapters and algorithms structures */
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
if (!(s4882_adapter = kcalloc(5, sizeof(struct i2c_adapter),
|
2005-04-17 06:20:36 +08:00
|
|
|
GFP_KERNEL))) {
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto ERROR1;
|
|
|
|
}
|
treewide: kzalloc() -> kcalloc()
The kzalloc() function has a 2-factor argument form, kcalloc(). This
patch replaces cases of:
kzalloc(a * b, gfp)
with:
kcalloc(a * b, gfp)
as well as handling cases of:
kzalloc(a * b * c, gfp)
with:
kzalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kzalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kzalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kzalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kzalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kzalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kzalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kzalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kzalloc
+ kcalloc
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kzalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kzalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kzalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kzalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kzalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kzalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kzalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kzalloc(sizeof(THING) * C2, ...)
|
kzalloc(sizeof(TYPE) * C2, ...)
|
kzalloc(C1 * C2 * C3, ...)
|
kzalloc(C1 * C2, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * E2
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kzalloc
+ kcalloc
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-13 05:03:40 +08:00
|
|
|
if (!(s4882_algo = kcalloc(5, sizeof(struct i2c_algorithm),
|
2005-04-17 06:20:36 +08:00
|
|
|
GFP_KERNEL))) {
|
|
|
|
error = -ENOMEM;
|
|
|
|
goto ERROR2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in the new structures */
|
|
|
|
s4882_algo[0] = *(amd756_smbus.algo);
|
|
|
|
s4882_algo[0].smbus_xfer = amd756_access_virt0;
|
|
|
|
s4882_adapter[0] = amd756_smbus;
|
|
|
|
s4882_adapter[0].algo = s4882_algo;
|
2007-02-14 05:09:03 +08:00
|
|
|
s4882_adapter[0].dev.parent = amd756_smbus.dev.parent;
|
2005-04-17 06:20:36 +08:00
|
|
|
for (i = 1; i < 5; i++) {
|
|
|
|
s4882_algo[i] = *(amd756_smbus.algo);
|
|
|
|
s4882_adapter[i] = amd756_smbus;
|
2009-01-07 21:29:18 +08:00
|
|
|
snprintf(s4882_adapter[i].name, sizeof(s4882_adapter[i].name),
|
|
|
|
"SMBus 8111 adapter (CPU%d)", i-1);
|
2005-04-17 06:20:36 +08:00
|
|
|
s4882_adapter[i].algo = s4882_algo+i;
|
2007-02-14 05:09:03 +08:00
|
|
|
s4882_adapter[i].dev.parent = amd756_smbus.dev.parent;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
s4882_algo[1].smbus_xfer = amd756_access_virt1;
|
|
|
|
s4882_algo[2].smbus_xfer = amd756_access_virt2;
|
|
|
|
s4882_algo[3].smbus_xfer = amd756_access_virt3;
|
|
|
|
s4882_algo[4].smbus_xfer = amd756_access_virt4;
|
|
|
|
|
|
|
|
/* Register virtual adapters */
|
|
|
|
for (i = 0; i < 5; i++) {
|
|
|
|
error = i2c_add_adapter(s4882_adapter+i);
|
|
|
|
if (error) {
|
2008-07-17 01:30:05 +08:00
|
|
|
printk(KERN_ERR "i2c-amd756-s4882: "
|
2005-04-17 06:20:36 +08:00
|
|
|
"Virtual adapter %d registration "
|
|
|
|
"failed, module not inserted\n", i);
|
|
|
|
for (i--; i >= 0; i--)
|
|
|
|
i2c_del_adapter(s4882_adapter+i);
|
|
|
|
goto ERROR3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ERROR3:
|
|
|
|
kfree(s4882_algo);
|
|
|
|
s4882_algo = NULL;
|
|
|
|
ERROR2:
|
|
|
|
kfree(s4882_adapter);
|
|
|
|
s4882_adapter = NULL;
|
|
|
|
ERROR1:
|
2008-04-30 05:11:38 +08:00
|
|
|
/* Restore physical bus */
|
|
|
|
i2c_add_adapter(&amd756_smbus);
|
2005-04-17 06:20:36 +08:00
|
|
|
ERROR0:
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit amd756_s4882_exit(void)
|
|
|
|
{
|
|
|
|
if (s4882_adapter) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
i2c_del_adapter(s4882_adapter+i);
|
|
|
|
kfree(s4882_adapter);
|
|
|
|
s4882_adapter = NULL;
|
|
|
|
}
|
2005-11-07 17:01:32 +08:00
|
|
|
kfree(s4882_algo);
|
|
|
|
s4882_algo = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Restore physical bus */
|
|
|
|
if (i2c_add_adapter(&amd756_smbus))
|
2008-07-17 01:30:05 +08:00
|
|
|
printk(KERN_ERR "i2c-amd756-s4882: "
|
|
|
|
"Physical bus restoration failed\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2014-01-30 03:40:08 +08:00
|
|
|
MODULE_AUTHOR("Jean Delvare <jdelvare@suse.de>");
|
2005-04-17 06:20:36 +08:00
|
|
|
MODULE_DESCRIPTION("S4882 SMBus multiplexing");
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
|
|
|
|
module_init(amd756_s4882_init);
|
|
|
|
module_exit(amd756_s4882_exit);
|