Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp7310418rwl; Thu, 23 Mar 2023 02:27:05 -0700 (PDT) X-Google-Smtp-Source: AK7set9ZGyeL6u5bTulqg3m8lYAqhq9Cnw3Rltie0JE83HW45n5w8jcLT0ygdsKBLV3+otoVipXV X-Received: by 2002:aa7:d6c6:0:b0:500:4a5d:af3f with SMTP id x6-20020aa7d6c6000000b005004a5daf3fmr8471498edr.34.1679563625071; Thu, 23 Mar 2023 02:27:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679563625; cv=none; d=google.com; s=arc-20160816; b=tchxWS1O3Ahj1jen3elL2T9H8TNwniKV1e+K35gK2/OMd2aj27g2R5u04w8IqnGTpH eClP8ZaRoBHH5lghZJh8kbBgHAeA5CC5FQpkP3PHfDagrr9PubOGenjAUS8gr1dX2ZXa M3dRdK9GATNbtpkhZalCbtqVIED7ydbKuooCNcV4LoGtQXofkNQgnVGFd3Xiq6WxR8A/ OmXOVYmNcJc4WDYV5Orh1TruAeIr4Uuoh8pY2b0Jay9Y2RC4TCLqR07za1sxwtns1dmt MOXRSFl6Je/beiuYpOVg5Ti0yflqxkxoIQqwQg4r6CcdJ4IUIfRy91B6c/jOU3TwhSOL 1Fdw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=7SXBz7/fnvHDj+svrpDNtScZJmIWP5EmHhciSS09B8E=; b=Upq9pCsM0OQNKJ9CG3uB22XGdNP6qrG+i9OGxKRErLhOFavyJUWRPXESSgleFGtC0A zIRi9hunW2SFQn20ZXuSZwzCXipP91uTeTOc/RODPida+RLStQ837Pxig2EAUA3bZLFI lE/flH2flCWkkpQ0yrZXQmxW9o7ztGp3h4lkHJteOx4kcxA0KY/2OyW0XXCYnGXSQrl3 R3emasYfCIabD1IDGLXmoXo4jfhvMiL/gYaPba7XnXqf79y1G5cdZBiGdNBT6vTmmIcw vYt3PtG1ykWcFiyXgqjDO7vgxdgA2hHaoWCO0woQJ0TJzA8jMz0BYoxUXPe5x4I9DxKW 5QXw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l16-20020aa7cad0000000b0050047ebcafesi14654585edt.496.2023.03.23.02.26.41; Thu, 23 Mar 2023 02:27:05 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231684AbjCWJXE convert rfc822-to-8bit (ORCPT + 99 others); Thu, 23 Mar 2023 05:23:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231395AbjCWJWk (ORCPT ); Thu, 23 Mar 2023 05:22:40 -0400 Received: from mail-pj1-f48.google.com (mail-pj1-f48.google.com [209.85.216.48]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6BB481CBC4; Thu, 23 Mar 2023 02:22:35 -0700 (PDT) Received: by mail-pj1-f48.google.com with SMTP id a16so16591671pjs.4; Thu, 23 Mar 2023 02:22:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679563355; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=HpKkhyG3kY0MZoMYHml99x+9t/mfe40fmpPl1syLvyE=; b=hcPQnb55qxobdkcUGjvllNOqdlrn32Fl0+5+1G+dFyQrinMSTvzPhgO8edgvobcZ1e 0e4ksRvf/0mMZczECwpgPNWBX5fNuF1inQQIUpb1kPdSUDQMYWwYzH7bt8XQzjgtSGho pCCj3+2ECLIgIhDvNwmjSRd2zviqM3ggL+eub9ZDEgUXKF421zOtLpcxXDDaIz8e7PRJ BAeSuCrW0GsvvGglHHFiZ7hNsK1RwTzeOOv18vqN7XgxCUJ5sAUVY74SyC/26QAzgsQe piwZkZOFpklyfIAax5d7thRUNlEmMcYMdLR6AjqYqn5K3nUktaG510+WrPWbR15JGmNb kt7Q== X-Gm-Message-State: AAQBX9dY2WhH3WSzWyPPl+yrxGJOlbxoeGdp8CL0lqOj1krJOKVN++kK LBgAjyqGLE9gZRO0Nj4aculsCQIcDslYpAWM89SGrW+ky2j2kw== X-Received: by 2002:a17:903:7c8:b0:19f:39f8:88c4 with SMTP id ko8-20020a17090307c800b0019f39f888c4mr1906789plb.2.1679563354730; Thu, 23 Mar 2023 02:22:34 -0700 (PDT) MIME-Version: 1.0 References: <20230321081152.26510-1-peter_hong@fintek.com.tw> In-Reply-To: From: Vincent MAILHOL Date: Thu, 23 Mar 2023 18:22:23 +0900 Message-ID: Subject: Re: [PATCH V2] can: usb: f81604: add Fintek F81604 support To: Peter Hong Cc: wg@grandegger.com, mkl@pengutronix.de, michal.swiatkowski@linux.intel.com, Steen.Hegelund@microchip.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, frank.jungclaus@esd.eu, linux-kernel@vger.kernel.org, linux-can@vger.kernel.org, netdev@vger.kernel.org, hpeter+linux_kernel@gmail.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=0.5 required=5.0 tests=FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS 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 Thu. 23 Mar 2023 at 14:54, Vincent MAILHOL wrote: > Le jeu. 23 mars 2023 à 14:14, Peter Hong a écrit : > > > > Hi Vincent, > > > > Vincent MAILHOL 於 2023/3/21 下午 11:50 寫道: > > >> +static netdev_tx_t f81604_start_xmit(struct sk_buff *skb, > > >> + struct net_device *netdev) > > >> +{ > > >> + struct can_frame *cf = (struct can_frame *)skb->data; > > >> + struct f81604_port_priv *priv = netdev_priv(netdev); > > >> + struct net_device_stats *stats = &netdev->stats; > > >> + int status; > > >> + u8 *ptr; > > >> + u32 id; > > >> + > > >> + if (can_dropped_invalid_skb(netdev, skb)) > > >> + return NETDEV_TX_OK; > > >> + > > >> + netif_stop_queue(netdev); > > >> + > > >> + ptr = priv->bulk_write_buffer; > > >> + memset(ptr, 0, F81604_DATA_SIZE); > > >> + > > >> + ptr[0] = F81604_CMD_DATA; > > >> + ptr[1] = min_t(u8, cf->can_dlc & 0xf, 8); > > >> + > > >> + if (cf->can_id & CAN_EFF_FLAG) { > > >> + id = (cf->can_id & CAN_ERR_MASK) << 3; > > >> + ptr[1] |= F81604_EFF_BIT; > > >> + ptr[2] = (id >> 24) & 0xff; > > >> + ptr[3] = (id >> 16) & 0xff; > > >> + ptr[4] = (id >> 8) & 0xff; > > >> + ptr[5] = (id >> 0) & 0xff; > > >> + memcpy(&ptr[6], cf->data, ptr[1]); > > > Rather than manipulating an opaque u8 array, please declare a > > > structure with explicit names. > > > > I had try to declare a struct like below and refactoring code : > > > > struct f81604_bulk_data { > > u8 cmd; > > u8 dlc; > > > > union { > > struct { > > u8 id1, id2; > > u8 data[CAN_MAX_DLEN]; > > } sff; > > > > struct { > > u8 id1, id2, id3, id4; > > u8 data[CAN_MAX_DLEN]; > > } eff; > > }; > > } __attribute__((packed)); > > > > This struct can used in TX/RX bulk in/out. Is it ok? > > That's nearly it. It is better to declare the struct sff and eff > separately. Also, do not split the id in bytes. Declare it as a little > endian using the __le16 and __le32 types. > > Something like this (I let you adjust): > > struct f81604_sff { > __le16 id: > u8 data[CAN_MAX_DLEN]; > } __attribute__((packed)); > > struct f81604_eff { > __le32 id; > u8 data[CAN_MAX_DLEN]; > } __attribute__((packed)); > > struct f81604_bulk_data { > u8 cmd; > u8 dlc; > > union { > struct f81604_sff sff; > struct f81604_eff eff; > }; > } __attribute__((packed)); > > The __le16 field should be manipulated using cpu_to_leXX() and > leXX_to_cpu() if the field is aligned, if not, it should be > manipulated using {get|set}_unaligned_leXX() (where XX represents the > size in bits). > > Also, f81604_bulk_data->dlc is not only a DLC but also carries the > F81604_EFF_BIT flag, right? At least, add documentation to the > structure to clarify this point. > > > > +static int f81604_prepare_urbs(struct net_device *netdev) > > > +{ > > > + static const u8 bulk_in_addr[F81604_MAX_DEV] = { 0x82, 0x84 }; > > > + static const u8 bulk_out_addr[F81604_MAX_DEV] = { 0x01, 0x03 }; > > > + static const u8 int_in_addr[F81604_MAX_DEV] = { 0x81, 0x83 }; > > > + struct f81604_port_priv *priv = netdev_priv(netdev); > > > + int id = netdev->dev_id; > > > + int i; > > > + > > > + /* initialize to NULL for error recovery */ > > > + for (i = 0; i < F81604_MAX_RX_URBS; ++i) > > > + priv->read_urb[i] = NULL; > > > priv was allocated with devm_kzalloc() so it should already be zeroed, > > > right? What is the purpose of this loop? > > > > This operation due to following condition: > > f81604_open() -> f81604_close() -> f81604_open() failed. > > > > We had used devm_kzalloc() in f81604_probe(), so first f81604_open() all > > pointers are NULL. But after f81604_close() then f81604_open() second > > times, the URB pointers are not NULLed, it'll makes error on 2nd > > f81604_open() > > with fail. > > Makes sense, thanks for the clarification. > > Then, please replace your loop by a memset(priv->read_urb, 0, > sizeof(priv->read_urb). Actually, your code never accesses the zeroed memory. The next lines are: for (i = 0; i < F81604_MAX_RX_URBS; ++i) { priv->read_urb[i] = usb_alloc_urb(0, GFP_KERNEL); If priv->read_urb[i] is never read before being initialized, no need to zero it. > > >> +/* Called by the usb core when driver is unloaded or device is removed */ > > >> +static void f81604_disconnect(struct usb_interface *intf) > > >> +{ > > >> + struct f81604_priv *priv = usb_get_intfdata(intf); > > >> + int i; > > >> + > > >> + for (i = 0; i < F81604_MAX_DEV; ++i) { > > >> + if (!priv->netdev[i]) > > >> + continue; > > >> + > > >> + unregister_netdev(priv->netdev[i]); > > >> + free_candev(priv->netdev[i]); > > >> + } > > > i> +} > > > > Is typo here? > > Yes, please ignore. > > > Yours sincerely, > Vincent Mailhol