From patchwork Wed Aug 17 10:56:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artur Rojek X-Patchwork-Id: 12945718 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B0E16C32772 for ; Wed, 17 Aug 2022 10:57:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239064AbiHQK5W (ORCPT ); Wed, 17 Aug 2022 06:57:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239060AbiHQK5V (ORCPT ); Wed, 17 Aug 2022 06:57:21 -0400 Received: from relay12.mail.gandi.net (relay12.mail.gandi.net [IPv6:2001:4b98:dc4:8::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A657786E7; Wed, 17 Aug 2022 03:57:19 -0700 (PDT) Received: (Authenticated sender: contact@artur-rojek.eu) by mail.gandi.net (Postfix) with ESMTPSA id 35646200004; Wed, 17 Aug 2022 10:57:17 +0000 (UTC) From: Artur Rojek To: Paul Cercueil , Jonathan Cameron , Dmitry Torokhov , Chris Morgan Cc: linux-mips@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Artur Rojek Subject: [PATCH 1/4] iio/adc: ingenic: fix channel offsets in buffer Date: Wed, 17 Aug 2022 12:56:40 +0200 Message-Id: <20220817105643.95710-2-contact@artur-rojek.eu> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220817105643.95710-1-contact@artur-rojek.eu> References: <20220817105643.95710-1-contact@artur-rojek.eu> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Consumers expect the buffer to only contain enabled channels. While preparing the buffer, the driver also (incorrectly) inserts empty data for disabled channels, causing the enabled channels to appear at wrong offsets. Fix that. Fixes: b96952f498db ("IIO: Ingenic JZ47xx: Add touchscreen mode.") Tested-by: Paul Cercueil Signed-off-by: Artur Rojek Reviewed-by: Andy Shevchenko --- drivers/iio/adc/ingenic-adc.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/drivers/iio/adc/ingenic-adc.c b/drivers/iio/adc/ingenic-adc.c index a7325dbbb99a..5a932c375a89 100644 --- a/drivers/iio/adc/ingenic-adc.c +++ b/drivers/iio/adc/ingenic-adc.c @@ -804,11 +804,10 @@ static irqreturn_t ingenic_adc_irq(int irq, void *data) unsigned int i; u32 tdat[3]; - for (i = 0; i < ARRAY_SIZE(tdat); mask >>= 2, i++) { + memset(tdat, 0, ARRAY_SIZE(tdat)); + for (i = 0; mask && i < ARRAY_SIZE(tdat); mask >>= 2) { if (mask & 0x3) - tdat[i] = readl(adc->base + JZ_ADC_REG_ADTCH); - else - tdat[i] = 0; + tdat[i++] = readl(adc->base + JZ_ADC_REG_ADTCH); } iio_push_to_buffers(iio_dev, tdat); From patchwork Wed Aug 17 10:56:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artur Rojek X-Patchwork-Id: 12945719 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A7C6EC25B08 for ; Wed, 17 Aug 2022 10:57:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239080AbiHQK5Z (ORCPT ); Wed, 17 Aug 2022 06:57:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239072AbiHQK5X (ORCPT ); Wed, 17 Aug 2022 06:57:23 -0400 Received: from relay12.mail.gandi.net (relay12.mail.gandi.net [IPv6:2001:4b98:dc4:8::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E5CE563F10; Wed, 17 Aug 2022 03:57:21 -0700 (PDT) Received: (Authenticated sender: contact@artur-rojek.eu) by mail.gandi.net (Postfix) with ESMTPSA id E8D9920000C; Wed, 17 Aug 2022 10:57:18 +0000 (UTC) From: Artur Rojek To: Paul Cercueil , Jonathan Cameron , Dmitry Torokhov , Chris Morgan Cc: linux-mips@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Artur Rojek Subject: [PATCH 2/4] iio: add iio_channel_cb_get_iio_buffer helper Date: Wed, 17 Aug 2022 12:56:41 +0200 Message-Id: <20220817105643.95710-3-contact@artur-rojek.eu> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220817105643.95710-1-contact@artur-rojek.eu> References: <20220817105643.95710-1-contact@artur-rojek.eu> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Introduce a helper function to retrieve an iio_buffer from iio_cb_buffer. This is useful for consumers that need to extract metadata about the buffer, e.g. get the channel offsets. Tested-by: Paul Cercueil Signed-off-by: Artur Rojek --- drivers/iio/buffer/industrialio-buffer-cb.c | 7 +++++++ include/linux/iio/consumer.h | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/drivers/iio/buffer/industrialio-buffer-cb.c b/drivers/iio/buffer/industrialio-buffer-cb.c index 4c12b7a94af5..47d6e28b4d36 100644 --- a/drivers/iio/buffer/industrialio-buffer-cb.c +++ b/drivers/iio/buffer/industrialio-buffer-cb.c @@ -151,6 +151,13 @@ struct iio_dev } EXPORT_SYMBOL_GPL(iio_channel_cb_get_iio_dev); +struct iio_buffer +*iio_channel_cb_get_iio_buffer(struct iio_cb_buffer *cb_buffer) +{ + return &cb_buffer->buffer; +} +EXPORT_SYMBOL_GPL(iio_channel_cb_get_iio_buffer); + MODULE_AUTHOR("Jonathan Cameron "); MODULE_DESCRIPTION("Industrial I/O callback buffer"); MODULE_LICENSE("GPL"); diff --git a/include/linux/iio/consumer.h b/include/linux/iio/consumer.h index 6802596b017c..c28925d5b69c 100644 --- a/include/linux/iio/consumer.h +++ b/include/linux/iio/consumer.h @@ -196,6 +196,18 @@ struct iio_channel struct iio_dev *iio_channel_cb_get_iio_dev(const struct iio_cb_buffer *cb_buffer); +/** + * iio_channel_cb_get_iio_buffer() - get access to the underlying buffer. + * @cb_buffer: The callback buffer from whom we want the buffer + * information. + * + * This function allows one to obtain information about the buffer. + * The primary aim is to allow drivers that are consuming a buffer to query + * things like channel offsets in the buffer. + */ +struct iio_buffer +*iio_channel_cb_get_iio_buffer(struct iio_cb_buffer *cb_buffer); + /** * iio_read_channel_raw() - read from a given channel * @chan: The channel being queried. From patchwork Wed Aug 17 10:56:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artur Rojek X-Patchwork-Id: 12945720 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4C269C32789 for ; Wed, 17 Aug 2022 10:57:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239090AbiHQK51 (ORCPT ); Wed, 17 Aug 2022 06:57:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239077AbiHQK5Y (ORCPT ); Wed, 17 Aug 2022 06:57:24 -0400 Received: from relay12.mail.gandi.net (relay12.mail.gandi.net [217.70.178.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 90E1773908; Wed, 17 Aug 2022 03:57:23 -0700 (PDT) Received: (Authenticated sender: contact@artur-rojek.eu) by mail.gandi.net (Postfix) with ESMTPSA id AC28820000E; Wed, 17 Aug 2022 10:57:20 +0000 (UTC) From: Artur Rojek To: Paul Cercueil , Jonathan Cameron , Dmitry Torokhov , Chris Morgan Cc: linux-mips@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Artur Rojek Subject: [PATCH 3/4] iio: add helper function for reading channel offset in buffer Date: Wed, 17 Aug 2022 12:56:42 +0200 Message-Id: <20220817105643.95710-4-contact@artur-rojek.eu> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220817105643.95710-1-contact@artur-rojek.eu> References: <20220817105643.95710-1-contact@artur-rojek.eu> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org This is useful for consumers that wish to parse raw buffer data. Tested-by: Paul Cercueil Signed-off-by: Artur Rojek --- drivers/iio/industrialio-buffer.c | 28 ++++++++++++++++++++++++++++ include/linux/iio/buffer.h | 4 ++++ 2 files changed, 32 insertions(+) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c index 228598b82a2f..cf23736610d9 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -691,6 +691,34 @@ static unsigned int iio_storage_bytes_for_si(struct iio_dev *indio_dev, return bytes; } +int iio_find_channel_offset_in_buffer(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + struct iio_buffer *buffer) +{ + int length, offset = 0; + unsigned int si; + + if (chan->scan_index < 0 || + !test_bit(chan->scan_index, buffer->scan_mask)) { + return -EINVAL; + } + + for (si = 0; si < chan->scan_index; ++si) { + if (!test_bit(si, buffer->scan_mask)) + continue; + + length = iio_storage_bytes_for_si(indio_dev, si); + + /* Account for channel alignment. */ + if (offset % length) + offset += length - (offset % length); + offset += length; + } + + return offset; +} +EXPORT_SYMBOL_GPL(iio_find_channel_offset_in_buffer); + static unsigned int iio_storage_bytes_for_timestamp(struct iio_dev *indio_dev) { struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); diff --git a/include/linux/iio/buffer.h b/include/linux/iio/buffer.h index 418b1307d3f2..b1db74772e77 100644 --- a/include/linux/iio/buffer.h +++ b/include/linux/iio/buffer.h @@ -16,6 +16,10 @@ enum iio_buffer_direction { IIO_BUFFER_DIRECTION_OUT, }; +int iio_find_channel_offset_in_buffer(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + struct iio_buffer *buffer); + int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data); int iio_pop_from_buffer(struct iio_buffer *buffer, void *data); From patchwork Wed Aug 17 10:56:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Artur Rojek X-Patchwork-Id: 12945721 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 71672C32773 for ; Wed, 17 Aug 2022 10:57:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239060AbiHQK5a (ORCPT ); Wed, 17 Aug 2022 06:57:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52928 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239085AbiHQK50 (ORCPT ); Wed, 17 Aug 2022 06:57:26 -0400 Received: from relay12.mail.gandi.net (relay12.mail.gandi.net [217.70.178.232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 56439647E8; Wed, 17 Aug 2022 03:57:25 -0700 (PDT) Received: (Authenticated sender: contact@artur-rojek.eu) by mail.gandi.net (Postfix) with ESMTPSA id 9002B200009; Wed, 17 Aug 2022 10:57:22 +0000 (UTC) From: Artur Rojek To: Paul Cercueil , Jonathan Cameron , Dmitry Torokhov , Chris Morgan Cc: linux-mips@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, linux-input@vger.kernel.org, Artur Rojek Subject: [PATCH 4/4] input: joystick: Fix buffer data parsing Date: Wed, 17 Aug 2022 12:56:43 +0200 Message-Id: <20220817105643.95710-5-contact@artur-rojek.eu> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220817105643.95710-1-contact@artur-rojek.eu> References: <20220817105643.95710-1-contact@artur-rojek.eu> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org Don't try to access buffer data of a channel by its scan index. Instead, use the newly introduced `iio_find_channel_offset_in_buffer` to get the correct data offset. The scan index of a channel does not represent its position in a buffer, as the buffer will contain data for enabled channels only, affecting data offsets and alignment. Fixes: 2c2b364fddd5 ("Input: joystick - add ADC attached joystick driver.") Reported-by: Chris Morgan Tested-by: Paul Cercueil Signed-off-by: Artur Rojek --- drivers/input/joystick/adc-joystick.c | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/drivers/input/joystick/adc-joystick.c b/drivers/input/joystick/adc-joystick.c index c0deff5d4282..aed853ebe1d1 100644 --- a/drivers/input/joystick/adc-joystick.c +++ b/drivers/input/joystick/adc-joystick.c @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -46,36 +47,43 @@ static void adc_joystick_poll(struct input_dev *input) static int adc_joystick_handle(const void *data, void *private) { struct adc_joystick *joy = private; + struct iio_buffer *buffer; enum iio_endian endianness; - int bytes, msb, val, idx, i; - const u16 *data_u16; + int bytes, msb, val, off; + const u8 *chan_data; + unsigned int i; bool sign; bytes = joy->chans[0].channel->scan_type.storagebits >> 3; for (i = 0; i < joy->num_chans; ++i) { - idx = joy->chans[i].channel->scan_index; endianness = joy->chans[i].channel->scan_type.endianness; msb = joy->chans[i].channel->scan_type.realbits - 1; sign = tolower(joy->chans[i].channel->scan_type.sign) == 's'; + buffer = iio_channel_cb_get_iio_buffer(joy->buffer); + off = iio_find_channel_offset_in_buffer(joy->chans[i].indio_dev, + joy->chans[i].channel, + buffer); + if (off < 0) + return off; + + chan_data = (const u8 *)data + off; switch (bytes) { case 1: - val = ((const u8 *)data)[idx]; + val = *chan_data; break; case 2: - data_u16 = (const u16 *)data + idx; - /* * Data is aligned to the sample size by IIO core. * Call `get_unaligned_xe16` to hide type casting. */ if (endianness == IIO_BE) - val = get_unaligned_be16(data_u16); + val = get_unaligned_be16(chan_data); else if (endianness == IIO_LE) - val = get_unaligned_le16(data_u16); + val = get_unaligned_le16(chan_data); else /* IIO_CPU */ - val = *data_u16; + val = *(const u16 *)chan_data; break; default: return -EINVAL;