From patchwork Thu Jul 10 10:45:11 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jean Delvare X-Patchwork-Id: 368565 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 838081400A0 for ; Thu, 10 Jul 2014 20:45:22 +1000 (EST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752622AbaGJKpU (ORCPT ); Thu, 10 Jul 2014 06:45:20 -0400 Received: from cantor2.suse.de ([195.135.220.15]:33191 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752469AbaGJKpR (ORCPT ); Thu, 10 Jul 2014 06:45:17 -0400 Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id C4918ABA2; Thu, 10 Jul 2014 10:45:15 +0000 (UTC) Date: Thu, 10 Jul 2014 12:45:11 +0200 From: Jean Delvare To: Linux I2C Cc: Guenter Roeck Subject: [PATCH 1/2] i2c-stub: Remember the number of emulated chips Message-ID: <20140710124511.05108894@endymion.delvare> Organization: SUSE Linux X-Mailer: Claws Mail 3.9.3 (GTK+ 2.24.22; x86_64-suse-linux-gnu) MIME-Version: 1.0 Sender: linux-i2c-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-i2c@vger.kernel.org This makes initialization, cleanup and look-up easier. Signed-off-by: Jean Delvare Cc: Guenter Roeck Reviewed-by: Guenter Roeck --- This applies on top of Guenter's patch "i2c: stub: Add support for SMBus block commands", v2. drivers/i2c/i2c-stub.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) --- linux-3.16-rc3.orig/drivers/i2c/i2c-stub.c 2014-07-09 12:57:46.383772594 +0200 +++ linux-3.16-rc3/drivers/i2c/i2c-stub.c 2014-07-09 13:09:42.982241933 +0200 @@ -62,6 +62,7 @@ struct stub_chip { }; static struct stub_chip *stub_chips; +static int stub_chips_nr; static struct smbus_block_data *stub_find_block(struct device *dev, struct stub_chip *chip, @@ -95,7 +96,7 @@ static s32 stub_xfer(struct i2c_adapter struct smbus_block_data *b; /* Search for the right chip */ - for (i = 0; i < MAX_CHIPS && chip_addr[i]; i++) { + for (i = 0; i < stub_chips_nr; i++) { if (addr == chip_addr[i]) { chip = stub_chips + i; break; @@ -281,12 +282,14 @@ static int __init i2c_stub_init(void) } /* Allocate memory for all chips at once */ - stub_chips = kzalloc(i * sizeof(struct stub_chip), GFP_KERNEL); + stub_chips_nr = i; + stub_chips = kcalloc(stub_chips_nr, sizeof(struct stub_chip), + GFP_KERNEL); if (!stub_chips) { pr_err("i2c-stub: Out of memory\n"); return -ENOMEM; } - for (i--; i >= 0; i--) + for (i = 0; i < stub_chips_nr; i++) INIT_LIST_HEAD(&stub_chips[i].smbus_blocks); ret = i2c_add_adapter(&stub_adapter);