Received: by 2002:ac0:a581:0:0:0:0:0 with SMTP id m1-v6csp7492850imm; Thu, 28 Jun 2018 04:51:43 -0700 (PDT) X-Google-Smtp-Source: AAOMgpeiiDJPRZIEWqRZD53dgqrncoJraAXkkNXQQYhKgnpy+vwzJ3Rsgjs5GjAMnVi04EpqogNR X-Received: by 2002:a62:569c:: with SMTP id h28-v6mr5680686pfj.201.1530186703225; Thu, 28 Jun 2018 04:51:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1530186703; cv=none; d=google.com; s=arc-20160816; b=pNDcmVg3s6JZ3PSMnZKk2qC7uXMoa0CJGXRkWwypWAMyVLwHFUHNz0+HDuntdwNXU0 9Ng8aucIH01dUqUvlhMd1vUcZokIrhmv+wJppqJTZLotQI12P/63UKesBfax5ZSi0MED H0v5WeCnxNM4F+7xlljEnwyJVl2EYP0dWQrTMEjtYisqTnAnR8PlQj82S4yAlfg12tgZ ugFncL6TbDhj9YLO8hBibhUmupNWXMINAeJH1n2Cja10jXofV/AP0b7mcM0CbKFkx4Xv cKn1fsK2Qjt+saI2E4wUFLS3z+koehYO5zFj1vEi8d+Sy6NjYvFS0ySAQ1OQASWi3/Ya 9x1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature :arc-authentication-results; bh=x5BQIuzmjAQHvGgL7HZMRr3qLlUoy3kJ2Vn0qEyKusg=; b=Am4NxyogSN2JPs9LrUCz+o5Q7gFk6CT+geNvTtxFaMqCeld+MLuPe2M1lvaRl2bi0c Xx7mROfNAjt36ziu4BjMTbzsUgdAsO8dQaMKjNN5YgRzjwkbnots9TTOROwe0Ira5SYE Wjcqb6i1jh+EWr6Wiu3QRd65HS79tjWgOv9yoXb2GlqDyHTjnD9mOKczjGmdjMwm3ENm LxvIIa8TIxy/L15GuTtS573aNS2+sl4lakVVTujm44q57ywmeeRJgpkSKXLmPuh3SWPw Wz4KH22WhGw92/ikYQj0N8zKBvP65X/R4dES6jX7e24TaaImz9Wc8Scpb5xUud/xYGon FTtA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=F6fRpckB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id n5-v6si6572117plk.352.2018.06.28.04.51.26; Thu, 28 Jun 2018 04:51:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=F6fRpckB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752372AbeF1KY3 (ORCPT + 99 others); Thu, 28 Jun 2018 06:24:29 -0400 Received: from mail-ua0-f194.google.com ([209.85.217.194]:38194 "EHLO mail-ua0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751700AbeF1KY2 (ORCPT ); Thu, 28 Jun 2018 06:24:28 -0400 Received: by mail-ua0-f194.google.com with SMTP id 59-v6so3206835uas.5; Thu, 28 Jun 2018 03:24:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=x5BQIuzmjAQHvGgL7HZMRr3qLlUoy3kJ2Vn0qEyKusg=; b=F6fRpckBcgAdJLY90Zatam4DKEosO4iBOlY/OvW4j5AvSmh+HdtqYFizFPEDrcCMgJ Ggv4BDK32MfQpIU/bzdv/RC4wAUbuO1GuoxXUjEJu1REoycOzrHnRa37LReHN+8vxX/v KixSgU/vSMQx6JmSgaNNsHdGxSM8PQ7BuV/+9wGVAgpIOzT4ZXTMayucg6SHF5WpR6qe kukrDeS5hpOQq9KS9cEjhpU5uA00AjDDvibAmrgTcXF/H3LUTsLzgO3+UxjiVDBqjGMT 0x99A4S1NsKP/DSg/92V7ivLP5ZIW7ftS73spZ7LKCvSlfa5jxOgUvO24Qj8YDuE2quQ JENg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=x5BQIuzmjAQHvGgL7HZMRr3qLlUoy3kJ2Vn0qEyKusg=; b=ni+f8hxeJYAB8lb8TeVU4rs4aBwHDNh+26RYR1enxeeqx7fbYT2qiKtM/Ac8VlE7YY dmkcvfbrzpCYV5OM7Xg1wLrTMjk9tL0nsbHjUHXYcAI7XaEyUGKaFDsSSN+QTd4Mf9Gw uDmLroRseCf7wQj06H33SWyvUSs/NrMTtpgn5DwtR9qOJ8Z9ZbMck87jjPUxQspgCwHj q4Zzac4r6fYa4G8qjmVLmVVtkjzqjl3k+QFXli8qZSwP/1VutMoA5cltXQcSsPQAyeVX rYs/OMVru/vpIyhGTb+ovqq5Dk8xVQhWDqZkmtXLLHzKdS/Udmq2cFeNHivwESc4wytj tIEA== X-Gm-Message-State: APt69E2b8/L7GcXNgLi6Om/1x7Lv/S/v0wC5raThim+fUU0Fm0hSqRS9 NjpWesP4fzNRFz0NJ3Sg5TqA9oWKJGbGDidPgM4= X-Received: by 2002:ab0:6037:: with SMTP id n23-v6mr5392012ual.28.1530181467122; Thu, 28 Jun 2018 03:24:27 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a67:8b02:0:0:0:0:0 with HTTP; Thu, 28 Jun 2018 03:24:26 -0700 (PDT) In-Reply-To: References: <03bc9a51ccae38cdc86934746aee9f75ec667cfd.1530162340.git.baolin.wang@linaro.org> From: Andy Shevchenko Date: Thu, 28 Jun 2018 13:24:26 +0300 Message-ID: Subject: Re: [PATCH v2 1/2] leds: core: Introduce generic pattern interface To: Baolin Wang Cc: Jacek Anaszewski , Pavel Machek , Bjorn Andersson , Mark Brown , Linux LED Subsystem , Linux Kernel Mailing List Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jun 28, 2018 at 12:18 PM, Baolin Wang wrote: > On 28 June 2018 at 16:31, Andy Shevchenko wrote: >> On Thu, Jun 28, 2018 at 8:16 AM, Baolin Wang wrote: >>> +What: /sys/class/leds//pattern >>> +Date: June 2018 >>> +KernelVersion: 4.18 >> >> 4.19 ? > > I think this will be merged in 4.18. Is it bug fix? I don't see how it would make v4.18. >>> +static ssize_t pattern_show(struct device *dev, >>> + struct device_attribute *attr, char *buf) >>> +{ >>> + struct led_classdev *led_cdev = dev_get_drvdata(dev); >>> + struct led_pattern *pattern; >>> + size_t offset = 0; >>> + int count, n, i; >> >>> + if (!led_cdev->pattern_get) >>> + return -EOPNOTSUPP; >>> + >> >> Perhaps just hide an attribute completely? > > Driver need implement the pattern_get() interface, otherwise we can > not get any available pattern values to show. It doesn't contradict with what I said. I proposed to just hide an attribute from sysfs completely if there is no such callbacks available. >>> + pattern = led_cdev->pattern_get(led_cdev, &count); >>> + if (IS_ERR_OR_NULL(pattern)) >>> + return PTR_ERR(pattern); >> >> Hmm.. Here you shadow NULL case by returning 0. >> Even if it's correct behaviour IS_ERR_OR_NULL is a beast to hide such >> subtle detail. >> >> It also would be good idea to check for count == 0 and bail out >> immediately. Otherwise you will print an extra blank line. > > We can not check count, since count can be not initialized if failed > to call pattern_get(). So maybe force user to return error pointer, > and we just check like: > if (IS_ERR(pattern)) > return PTR_ERR(pattern); My question is can be counter 0 by some reason? >>> + for (i = 0; i < count; i++) { >>> + n = snprintf(buf + offset, PAGE_SIZE - offset, "%d %d", >>> + pattern[i].brightness, pattern[i].delta_t); >>> + >> >>> + if (offset + n >= PAGE_SIZE) >>> + goto err_nospc; >> >>> + >>> + offset += n; >>> + >> >>> + if (i < count - 1) >>> + buf[offset++] = ' '; >> >> You might add this to the end of above format string and remove this >> conditional completely... > > Hmmm, I do not think we need add one extra ' ' to the end of format string. Why not? You do it anyway for all except last, but... >>> + buf[offset++] = '\n'; >> >> ...and use here something like >> >> buf[offset - 1] = '\n'; > > I don't think so. We need increase the offset value at the same time. Nope, you don't need it. here we replace trailing space by '\n'. >> (we have such patterns in the kernel) ...look at existing patterns in the kernel. >>> + /* Trim trailing newline */ >>> + s[strcspn(s, "\n")] = '\0'; >> >> It's usually done via strstrip(). >> >> sbegin = kstrndup(); >> ... >> >> s = strstrip(sbegin); > > Good idea, will change. Replying to your another message. And who cares about leading spaces? Is it wrong to have them? Why? >>> + /* Parse out the brightness & delta_t touples */ >>> + while ((elem = strsep(&s, " ")) != NULL) { >>> + ret = kstrtoul(elem, 10, &val); >>> + if (ret) >>> + goto out; >>> + >> >>> + if (odd) { >> >> This is effectivelly if (len % 2 == 0) > > It is incorrect, we can not use len to decide the value is brightness > or delta. Here logical is to make sure we must keep delta>, ...... Right, the problem is the format itself I suppose. It's too error prone for one attribute. What about to change it like "brightness delta[, brightness delta[, ...]]" and then you do ...elem = strsep(&s, ",")... sscanf("%d %d") == 2 ? >>> + pattern[len].brightness = val; >>> + } else { >>> + pattern[len].delta_t = val; >>> + len++; >>> + } >>> + >>> + odd = !odd; >>> + } >>> + >>> + /* >>> + * Fail if we didn't find any data points or last data point was partial >>> + */ >>> + if (!len || !odd) { >>> + ret = -EINVAL; >>> + goto out; >>> + } >> >> For partial data can we return different error code? >> Does it make sense? > > Sorry I did not get you here. If user set incorrect pattern values, I > think '-EINVAL' is suitable. OK. -- With Best Regards, Andy Shevchenko