From patchwork Fri Jan 9 17:21:32 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wolfram Sang X-Patchwork-Id: 5602111 Return-Path: X-Original-To: patchwork-linux-arm@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 279E9C058D for ; Fri, 9 Jan 2015 17:27:22 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 595E02060C for ; Fri, 9 Jan 2015 17:27:21 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.9]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7D7DC20608 for ; Fri, 9 Jan 2015 17:27:20 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1Y9dIl-0002A4-NK; Fri, 09 Jan 2015 17:24:47 +0000 Received: from sauhun.de ([89.238.76.85] helo=pokefinder.org) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1Y9dGl-0000jA-4U for linux-arm-kernel@lists.infradead.org; Fri, 09 Jan 2015 17:22:48 +0000 Received: from p4fe257be.dip0.t-ipconnect.de ([79.226.87.190]:48232 helo=localhost) by pokefinder.org with esmtpsa (TLS1.2:RSA_AES_128_CBC_SHA1:128) (Exim 4.80) (envelope-from ) id 1Y9dGJ-0000M1-9A; Fri, 09 Jan 2015 18:22:15 +0100 From: Wolfram Sang To: linux-i2c@vger.kernel.org Subject: [RFC 02/11] i2c: add quirk checks to core Date: Fri, 9 Jan 2015 18:21:32 +0100 Message-Id: <1420824103-24169-3-git-send-email-wsa@the-dreams.de> X-Mailer: git-send-email 2.1.3 In-Reply-To: <1420824103-24169-1-git-send-email-wsa@the-dreams.de> References: <1420824103-24169-1-git-send-email-wsa@the-dreams.de> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20150109_092243_467965_0853271D X-CRM114-Status: GOOD ( 13.19 ) X-Spam-Score: -0.0 (/) Cc: linux-mips@linux-mips.org, Wolfram Sang , Benjamin Herrenschmidt , linux-kernel@vger.kernel.org, Ludovic Desroches , Yingjoe Chen , linuxppc-dev@lists.ozlabs.org, linux-arm-kernel@lists.infradead.org X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.18-1 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_MED, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Let the core do the checks if HW quirks prevent a transfer. Saves code from drivers and adds consistency. Signed-off-by: Wolfram Sang --- drivers/i2c/i2c-core.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index 39d25a8cb1ad..7b10a19abf5b 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c @@ -2063,6 +2063,56 @@ module_exit(i2c_exit); * ---------------------------------------------------- */ +/* Check if val is exceeding the quirk IFF quirk is non 0 */ +#define i2c_quirk_exceeded(val, quirk) ((quirk) && ((val) > (quirk))) + +static int i2c_quirk_error(struct i2c_adapter *adap, struct i2c_msg *msg, char *err_msg) +{ + dev_err(&adap->dev, "quirk: %s (addr 0x%04x, size %u)\n", err_msg, msg->addr, msg->len); + return -EOPNOTSUPP; +} + +static int i2c_check_for_quirks(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) +{ + struct i2c_adapter_quirks *q = adap->quirks; + u16 max_read = q->max_read_len, max_write = q->max_write_len; + int max_num = q->max_num_msgs, i; + + if (q->flags & I2C_ADAPTER_QUIRK_COMB_WRITE_THEN_READ) + max_num = 2; + + if (i2c_quirk_exceeded(num, max_num)) + return i2c_quirk_error(adap, &msgs[0], "too many messages"); + + if (num == 2 && q->flags & I2C_ADAPTER_QUIRK_COMB_WRITE_FIRST) { + if (msgs[0].flags & I2C_M_RD) + return i2c_quirk_error(adap, &msgs[0], "invalid first write msg"); + + max_write = q->max_comb_write_len; + } + + if (num == 2 && q->flags & I2C_ADAPTER_QUIRK_COMB_READ_SECOND) { + if (!(msgs[1].flags & I2C_M_RD) || msgs[0].addr != msgs[1].addr) + return i2c_quirk_error(adap, &msgs[1], "invalid second read msg"); + + max_read = q->max_comb_read_len; + } + + for (i = 0; i < num; i++) { + u16 len = msgs[i].len; + + if (msgs[i].flags & I2C_M_RD) { + if (i2c_quirk_exceeded(len, max_read)) + return i2c_quirk_error(adap, &msgs[i], "msg too long"); + } else { + if (i2c_quirk_exceeded(len, max_write)) + return i2c_quirk_error(adap, &msgs[i], "msg too long"); + } + } + + return 0; +} + /** * __i2c_transfer - unlocked flavor of i2c_transfer * @adap: Handle to I2C bus @@ -2080,6 +2130,9 @@ int __i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num) unsigned long orig_jiffies; int ret, try; + if (adap->quirks && i2c_check_for_quirks(adap, msgs, num)) + return -EOPNOTSUPP; + /* i2c_trace_msg gets enabled when tracepoint i2c_transfer gets * enabled. This is an efficient way of keeping the for-loop from * being executed when not needed.