From nobody Tue Feb 10 17:30:38 2026 Received: from m16.mail.163.com (m16.mail.163.com [220.197.31.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A82F226E6F2; Mon, 26 Jan 2026 06:22:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=220.197.31.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769408581; cv=none; b=VTG2bIGYVGWCSAnLQJTWMZQ/CUiO+dLQhKEtwj3JvFHhVcgejRAbCxbZZhaGD2OhV5O179ou8DmIWhX7cs/4K7trdNV5qAoU81wEp1P1TDFgmranhCAO7+Gex3+j7DE0g66TPEKWfxK49Xa70c+qp5odME9duV6N+PzYSosoERo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769408581; c=relaxed/simple; bh=IT+9ec6tYUIaqWEnT7zUZrABK+pEwqjPKReyebksm34=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=vGvJa8hI8VAVgFjsR2mbCjSJPF3rz1iP/xmMNLWF0Ns/m4BiZTu140Y+Hg9oi8M8d+oxLhvI9coMbIdcRGR0njAJgI0ttglogpSOvNIm6ENXWu7iYZvss+ip/7NG8s1CyoKTVN2QNlMk6EpPGq5KG8/+EWOSz0arq9DWPSloB+c= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=RA7kyreO; arc=none smtp.client-ip=220.197.31.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="RA7kyreO" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-Id:MIME-Version; bh=/s StrZiGTZYmClhQCpesOCkV2ki2nCzgYp0H3gRJmVk=; b=RA7kyreOTrYChBZu+G EfaGWT7zYjG+3na04CdeK0VJhgX8MvTzXnmlVWtiGgr91U89iywzTw5f7U3D5soL pid+1/Bv2ryxI8mBzV/6Sh+Y8LcH3CLfF3Z/LPHMc4BIyZMbFFdah7kta8lXgJo1 oyolviqL8hqPCzBvm9XypoTgs= Received: from localhost.localdomain (unknown []) by gzga-smtp-mtada-g1-3 (Coremail) with SMTP id _____wCHzzkJCHdpL7kRIg--.5216S5; Mon, 26 Jan 2026 14:22:11 +0800 (CST) From: Slark Xiao To: loic.poulain@oss.qualcomm.com, ryazanov.s.a@gmail.com, johannes@sipsolutions.net, andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com, mani@kernel.org Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, slark_xiao@163.com Subject: [net-next v8 3/8] net: wwan: core: split port creation and registration Date: Mon, 26 Jan 2026 14:21:53 +0800 Message-Id: <20260126062158.308598-4-slark_xiao@163.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20260126062158.308598-1-slark_xiao@163.com> References: <20260126062158.308598-1-slark_xiao@163.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: _____wCHzzkJCHdpL7kRIg--.5216S5 X-Coremail-Antispam: 1Uf129KBjvJXoWxJFWkAw45CFW3JFWDuryfZwb_yoWrZw18p3 W0gas3tFW8Jrnrur43AF47ZF4rua1Ik34SyrW8W34Skrn0qryFvF9Y9FyqvFWrGFy7GFy3 Xrs8ta1093WUWr7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zReOJnUUUUU= X-CM-SenderInfo: xvod2y5b0lt0i6rwjhhfrp/xtbC5xM+oml3CBPoqgAA3v Content-Type: text/plain; charset="utf-8" From: Sergey Ryazanov Upcoming GNSS (NMEA) port type support requires exporting it via the GNSS subsystem. On another hand, we still need to do basic WWAN core work: find or allocate the WWAN device, make it the port parent, etc. To reuse as much code as possible, split the port creation function into the registration of a regular WWAN port device, and basic port struct initialization. To be able to use put_device() uniformly, break the device_register() call into device_initialize() and device_add() and call device initialization earlier. While at it, fix a minor number leak upon WWAN port registration failure. Signed-off-by: Sergey Ryazanov --- drivers/net/wwan/wwan_core.c | 69 ++++++++++++++++++++++-------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/drivers/net/wwan/wwan_core.c b/drivers/net/wwan/wwan_core.c index 1da935e84008..76f1d759d255 100644 --- a/drivers/net/wwan/wwan_core.c +++ b/drivers/net/wwan/wwan_core.c @@ -357,7 +357,8 @@ static void wwan_port_destroy(struct device *dev) { struct wwan_port *port =3D to_wwan_port(dev); =20 - ida_free(&minors, MINOR(port->dev.devt)); + if (dev->class =3D=3D &wwan_class) + ida_free(&minors, MINOR(dev->devt)); mutex_destroy(&port->data_lock); mutex_destroy(&port->ops_lock); kfree(port); @@ -436,6 +437,44 @@ static int __wwan_port_dev_assign_name(struct wwan_por= t *port, const char *fmt) return dev_set_name(&port->dev, "%s", buf); } =20 +/* Register a regular WWAN port device (e.g. AT, MBIM, etc.) */ +static int wwan_port_register_wwan(struct wwan_port *port) +{ + struct wwan_device *wwandev =3D to_wwan_dev(port->dev.parent); + char namefmt[0x20]; + int minor, err; + + /* A port is exposed as character device, get a minor */ + minor =3D ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL); + if (minor < 0) + return minor; + + port->dev.class =3D &wwan_class; + port->dev.devt =3D MKDEV(wwan_major, minor); + + /* allocate unique name based on wwan device id, port type and number */ + snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id, + wwan_port_types[port->type].devsuf); + + /* Serialize ports registration */ + mutex_lock(&wwan_register_lock); + + __wwan_port_dev_assign_name(port, namefmt); + err =3D device_add(&port->dev); + + mutex_unlock(&wwan_register_lock); + + if (err) { + ida_free(&minors, minor); + port->dev.class =3D NULL; + return err; + } + + dev_info(&wwandev->dev, "port %s attached\n", dev_name(&port->dev)); + + return 0; +} + struct wwan_port *wwan_create_port(struct device *parent, enum wwan_port_type type, const struct wwan_port_ops *ops, @@ -444,8 +483,7 @@ struct wwan_port *wwan_create_port(struct device *paren= t, { struct wwan_device *wwandev; struct wwan_port *port; - char namefmt[0x20]; - int minor, err; + int err; =20 if (type > WWAN_PORT_MAX || !ops) return ERR_PTR(-EINVAL); @@ -457,17 +495,9 @@ struct wwan_port *wwan_create_port(struct device *pare= nt, if (IS_ERR(wwandev)) return ERR_CAST(wwandev); =20 - /* A port is exposed as character device, get a minor */ - minor =3D ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL); - if (minor < 0) { - err =3D minor; - goto error_wwandev_remove; - } - port =3D kzalloc(sizeof(*port), GFP_KERNEL); if (!port) { err =3D -ENOMEM; - ida_free(&minors, minor); goto error_wwandev_remove; } =20 @@ -481,27 +511,14 @@ struct wwan_port *wwan_create_port(struct device *par= ent, mutex_init(&port->data_lock); =20 port->dev.parent =3D &wwandev->dev; - port->dev.class =3D &wwan_class; port->dev.type =3D &wwan_port_dev_type; - port->dev.devt =3D MKDEV(wwan_major, minor); dev_set_drvdata(&port->dev, drvdata); + device_initialize(&port->dev); =20 - /* allocate unique name based on wwan device id, port type and number */ - snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id, - wwan_port_types[port->type].devsuf); - - /* Serialize ports registration */ - mutex_lock(&wwan_register_lock); - - __wwan_port_dev_assign_name(port, namefmt); - err =3D device_register(&port->dev); - - mutex_unlock(&wwan_register_lock); - + err =3D wwan_port_register_wwan(port); if (err) goto error_put_device; =20 - dev_info(&wwandev->dev, "port %s attached\n", dev_name(&port->dev)); return port; =20 error_put_device: --=20 2.25.1