Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp183670iob; Mon, 2 May 2022 16:32:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy/RGcKa02TUouRkL3cMmbUb0YjDbD3BcrAnNuF8ZC2qYx1cqezxmUaU6NvpM/0+QFWZS2u X-Received: by 2002:a17:90a:ca89:b0:1d9:7d1a:c337 with SMTP id y9-20020a17090aca8900b001d97d1ac337mr1698504pjt.88.1651534348332; Mon, 02 May 2022 16:32:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651534348; cv=none; d=google.com; s=arc-20160816; b=Ly8NDMThYDcCwgVDyucGJpIIw/bZo5IFh0qEU4VomubFRBMj80ytuzryD7K347a3Qv T6tsnSYKrViWdGeV+v9K9vC0gr1qsqGiwjAqP9ccwdR1FNhHrI9fxrgpmz1wm7JcpYSp Hp38nE77bRImTpN5+JEpHBH+l6p4FnGEIP6024frxEHq5PCGXIOEMX9TNTo4976hzy8i P9USOeVdZiRb4TQJgBJO15r8hXE2NBimSVFiR0vUz49GA+00yQQ7i5v/MwHkbzDIZpCL L8Xiw8DjlTWS2pfKlDxVVhYkFFbighN+/Mbr9iR4OJEbY6TL+mOfl64cekUEGHXDMWBr HUvA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=Iymi+5khW50GeB4u9utBY2pY3ATPV9dMvcwkgjmV68k=; b=p9O2aoItaBO5KSNEfunDqhvRngdJex9YbQ6FjRbRfK0Xj5VN0g+rbXA1xaigFh1NCI y8T3x3cJ24CDGCiTtPgMGbOoTpoG4sTDJ5CinDxINfjDsa5EgRqDr21Cn8RHViLD2EiC ee20lTUhbIhEG4aUR4ahDS6aP3jrTzY9I8L7KNuj5xMRopwFCUiZJVENZWiwGdL6O9FT ZjhIpWnUgxS9So2iIc/nm3zQgukqnG35K6FfuU7t7Nrz87hBI2hF1KIdsGa9nPW+26yP su7u/LNerfMuuMWrMWNHJqp+yRoG3dpuTqPgdb5mZwl5/t3VXhgi4pAg0EnVLVwRJPAF C1Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="bxa2u/xq"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id bi8-20020a170902bf0800b00153b2d16475si14218794plb.125.2022.05.02.16.32.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 May 2022 16:32:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="bxa2u/xq"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E8F57DE8; Mon, 2 May 2022 16:32:22 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1384547AbiEBKQV (ORCPT + 99 others); Mon, 2 May 2022 06:16:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46772 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1384558AbiEBKQK (ORCPT ); Mon, 2 May 2022 06:16:10 -0400 Received: from mail-ed1-x52c.google.com (mail-ed1-x52c.google.com [IPv6:2a00:1450:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CA5D2DC1; Mon, 2 May 2022 03:12:23 -0700 (PDT) Received: by mail-ed1-x52c.google.com with SMTP id be20so16047942edb.12; Mon, 02 May 2022 03:12:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=Iymi+5khW50GeB4u9utBY2pY3ATPV9dMvcwkgjmV68k=; b=bxa2u/xq1Bm6JNNYuARJHWzKFrM/ZrKd3v8JVZsa4hFKdGL15tYQ5/xuxOq9VacfKG au4LAiMQ3Ld8gnFZ5b3eUAfT7CaeDU6lQVn47+gRUWBB18M5U5rcnZafFMNe4nhaaKIL VMbk019KcEEkx9s+l+BNn/CPEw2s+TPY6TP+0BhE+Louai0pP41QZfonS4nfcNqfsQ3F VRcmC0xXPUDUXKkwNbm4kw8b7UHq/k56vZLrVqzEQ+8WQMEYdzfkjHR1LZi43xgLfkeh R61hCUBL3VRfVj/KqYMmVLG6Pcv/+aUcbXFr8zQNxClhPLFCBUqQnS63YvvIWnZaRg4l EByA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=Iymi+5khW50GeB4u9utBY2pY3ATPV9dMvcwkgjmV68k=; b=OZ3LyzfUFQdrV9sjVv0z8PxPPhhPM8np0o3Kw/bLFe8qtgOqdsVUIFezg1vPkmOGS3 dYrcpSO+PUAFPpK7hwQo0vX+KhFDZCvtePI42fdGsCxJv8myWzq6z5REAqQDQn/1o1/j 5ihIKmkz/EfVIFtq4mLdsedoJu4uLY5NvDNxXkHdDYS+X8DjOf5qNcFitYoS/Qwit34k ti+iruzsjKAtuNDq5O5XQogMIW0u5hOWHlzwaVeZkNgqhUmndCHBysNnq7PEWlvWqzvr WYDQxFskSvFpvGn9ZdOPa6y13p9dGTZbX830VP5dverye688782obA79IrCnxEfd881W 1Miw== X-Gm-Message-State: AOAM530ErzWbb5Lpjj+wnyHQC7VluJyVy1KoclGDY+jMX45vVTqYXkda XxLWucU+C5xcg4EotKPiQAsgDDP1abllT/U3iuk= X-Received: by 2002:a50:e696:0:b0:419:998d:5feb with SMTP id z22-20020a50e696000000b00419998d5febmr12901013edm.122.1651486341862; Mon, 02 May 2022 03:12:21 -0700 (PDT) MIME-Version: 1.0 References: <20220426131102.23966-1-andrea.merello@gmail.com> <20220426131102.23966-9-andrea.merello@gmail.com> In-Reply-To: From: Andy Shevchenko Date: Mon, 2 May 2022 12:11:45 +0200 Message-ID: Subject: Re: [v5 08/14] iio: imu: add Bosch Sensortec BNO055 core driver To: Andrea Merello Cc: Jonathan Cameron , Mauro Carvalho Chehab , linux-iio , Linux Kernel Mailing List , devicetree , Lars-Peter Clausen , Rob Herring , Matt Ranostay , Alexandru Ardelean , jmondi , Andrea Merello Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, May 2, 2022 at 11:50 AM Andrea Merello wrote: > Il giorno mer 27 apr 2022 alle ore 15:23 Andy Shevchenko > ha scritto: ... > > > +#define BNO055_ATTR_VALS(...) \ > > > + .vals = (int[]){ __VA_ARGS__}, \ > > > + .len = ARRAY_SIZE(((int[]){__VA_ARGS__})) > > > > Not sure this adds any readability to the code. Can we simply have an > > array of int for each case with the explicit ARRAY_SIZE() calls? > > Do you mean moving the vals array out of the structs? Something like: > > static int bno055_gyr_scale_vals[] = {125, 1877467, 250, 1877467, > 500, 1877467, 1000, 1877467, 2000, 1877467}; With the better style, but yes ...[] = { 1, 2, 3, 4, 5 6, 7, 8, 9, 10, }; > static struct bno055_sysfs_attr_aux_data bno055_gyr_scale_aux = { > .fusion_vals = (int[]){1, 900}, > .hw_xlate = (int[]){4, 3, 2, 1, 0}, > .type = IIO_VAL_FRACTIONAL (with last comma to be kept, but yes) > ? > But then I'd make also something like: > > #define bno055_sysfs_attr_avail(priv, attr, vals, len) \ > _bno055_sysfs_attr_avail(priv, attr##_vals, > ARRAY_SIZE(attr##_vals), attr##_aux, vals, len) > > And the same for all other users of those structs. > > My point is not about readability, And my point about readability. The reader, and even the author after some time, may have no clue in this forest of the macros and castings what's going on. > but about avoiding as much as > possible bugs caused by mismatched attr_vals, attr_aux and > ARRAY_SIZE() arg. e.g: > bno055_sysfs_attr_avail(priv, bno_foo_vals, ARRAY_SIZE(bno_bar_vals), > bno_foobar_aux, vals, len) > > I used to make quite a lot of mess until I grouped all the stuff in > one struct :/ If something you want to prevent at compile time, consider to utilize static_assert() and / or BUILD_BUG_ON() depending on the place in the code (the former is preferred). ... > > > + msleep(20); > > > > Perhaps a comment why so long sleep is needed. > > DS says that switching mode can last from 7mS up to 19mS depending on > the case, but I don't know _why_ it takes so long. I may add a comment > that just states that it's a sensor requirement. Yes, please add the comment that this time has been chosen to follow data sheet recommendations. ... > > > + for (i = 0; i < bno055_acc_range.len; i++) > > > + len += sysfs_emit_at(buf, len, "%d%c", bno055_acc_range.vals[i], > > > + (i == bno055_acc_range.len - 1) ? '\n' : ' '); > > > > You may move the condition out of the loop. > > May you elaborate, please? Do you mean something like: loop one time > less, and then call sysfs_emit_at() once more outside the loop, > getting rid of the conditional ternary operator at all? Regular loop with a space as a delimiter and after it the condition to check i and replace last space by \n. I.o.w. the ternary is an invariant to the loop and no need to call it for each iteration. ... > > > + if (indio_dev->active_scan_mask && > > > + !bitmap_empty(indio_dev->active_scan_mask, _BNO055_SCAN_MAX)) > > > + return -EBUSY; > > > + > > > + if (sysfs_streq(buf, "0")) { > > > + ret = bno055_operation_mode_set(priv, BNO055_OPR_MODE_AMG); > > > > return bno055_operation_mode_set(...); > > Why? bno055_operation_mode_set() returns an error code, while here we > need to return the len, or propagate the error code only when it's the > case Ah good point. > > > + } else { > > > > ...and drop this with the following decreasing indentation. > > if you want to drop this, then I can just duplicate if(ret) return > ret; i.e. add it after bno055_operation_mode_set(priv, > BNO055_OPR_MODE_AMG); and get rid of the else branch (see above) Yes, but now, reading the original code again, I'm wondering why you are not using kstrtobool(). ... > > Can be removed to group all related checks together. > > I'm not sure what you mean here, but see below > > > > + if (ret) > > > + dev_notice(dev, "Calibration file load failed. See instruction in kernel Documentation/iio/bno055.rst"); > > > + > > > + if (caldata) { > > > + caldata_data = caldata->data; > > > + caldata_size = caldata->size; > > > + } > > > + ret = bno055_init(priv, caldata_data, caldata_size); > > > > > + if (caldata) > > > + release_firmware(caldata); > > > > > + if (ret) > > > + return ret; > > > > Can be rewritten in a form of > > > > if (caldata) { > > ret = bno055_init(); > > release_firmware(...); > > } else { > > ret = bno055_init(); > > } > > if (ret) > > return ret; > > > > ? > > Indeed I'd say it could be rewritten as: > > if (ret) > ret = request_firmware(&caldata, BNO055_FW_GENERIC_NAME, dev); > if (ret) { > dev_notice(dev, "Calibration file load failed. See > instruction in kernel Documentation/iio/bno055.rst"); Missed \n. > ret = bno055_init(priv, NULL, 0); > } else { > ret = bno055_init(priv, caldata->data, caldata->size); > release_firmware(caldata); > } > if (ret) > return ret; Yes, something like this. Experiment with it and choose which one is read better. -- With Best Regards, Andy Shevchenko