Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

net: wwan: core: split port creation and registration

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 <ryazanov.s.a@gmail.com>
Link: https://patch.msgid.link/20260126062158.308598-4-slark_xiao@163.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Sergey Ryazanov and committed by
Jakub Kicinski
dccd23a6 b9879ba7

+43 -26
+43 -26
drivers/net/wwan/wwan_core.c
··· 357 357 { 358 358 struct wwan_port *port = to_wwan_port(dev); 359 359 360 - ida_free(&minors, MINOR(port->dev.devt)); 360 + if (dev->class == &wwan_class) 361 + ida_free(&minors, MINOR(dev->devt)); 361 362 mutex_destroy(&port->data_lock); 362 363 mutex_destroy(&port->ops_lock); 363 364 kfree(port); ··· 437 436 return dev_set_name(&port->dev, "%s", buf); 438 437 } 439 438 439 + /* Register a regular WWAN port device (e.g. AT, MBIM, etc.) */ 440 + static int wwan_port_register_wwan(struct wwan_port *port) 441 + { 442 + struct wwan_device *wwandev = to_wwan_dev(port->dev.parent); 443 + char namefmt[0x20]; 444 + int minor, err; 445 + 446 + /* A port is exposed as character device, get a minor */ 447 + minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL); 448 + if (minor < 0) 449 + return minor; 450 + 451 + port->dev.class = &wwan_class; 452 + port->dev.devt = MKDEV(wwan_major, minor); 453 + 454 + /* allocate unique name based on wwan device id, port type and number */ 455 + snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id, 456 + wwan_port_types[port->type].devsuf); 457 + 458 + /* Serialize ports registration */ 459 + mutex_lock(&wwan_register_lock); 460 + 461 + __wwan_port_dev_assign_name(port, namefmt); 462 + err = device_add(&port->dev); 463 + 464 + mutex_unlock(&wwan_register_lock); 465 + 466 + if (err) { 467 + ida_free(&minors, minor); 468 + port->dev.class = NULL; 469 + return err; 470 + } 471 + 472 + dev_info(&wwandev->dev, "port %s attached\n", dev_name(&port->dev)); 473 + 474 + return 0; 475 + } 476 + 440 477 struct wwan_port *wwan_create_port(struct device *parent, 441 478 enum wwan_port_type type, 442 479 const struct wwan_port_ops *ops, ··· 483 444 { 484 445 struct wwan_device *wwandev; 485 446 struct wwan_port *port; 486 - char namefmt[0x20]; 487 - int minor, err; 447 + int err; 488 448 489 449 if (type > WWAN_PORT_MAX || !ops) 490 450 return ERR_PTR(-EINVAL); ··· 495 457 if (IS_ERR(wwandev)) 496 458 return ERR_CAST(wwandev); 497 459 498 - /* A port is exposed as character device, get a minor */ 499 - minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL); 500 - if (minor < 0) { 501 - err = minor; 502 - goto error_wwandev_remove; 503 - } 504 - 505 460 port = kzalloc(sizeof(*port), GFP_KERNEL); 506 461 if (!port) { 507 462 err = -ENOMEM; 508 - ida_free(&minors, minor); 509 463 goto error_wwandev_remove; 510 464 } 511 465 ··· 511 481 mutex_init(&port->data_lock); 512 482 513 483 port->dev.parent = &wwandev->dev; 514 - port->dev.class = &wwan_class; 515 484 port->dev.type = &wwan_port_dev_type; 516 - port->dev.devt = MKDEV(wwan_major, minor); 517 485 dev_set_drvdata(&port->dev, drvdata); 486 + device_initialize(&port->dev); 518 487 519 - /* allocate unique name based on wwan device id, port type and number */ 520 - snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id, 521 - wwan_port_types[port->type].devsuf); 522 - 523 - /* Serialize ports registration */ 524 - mutex_lock(&wwan_register_lock); 525 - 526 - __wwan_port_dev_assign_name(port, namefmt); 527 - err = device_register(&port->dev); 528 - 529 - mutex_unlock(&wwan_register_lock); 530 - 488 + err = wwan_port_register_wwan(port); 531 489 if (err) 532 490 goto error_put_device; 533 491 534 - dev_info(&wwandev->dev, "port %s attached\n", dev_name(&port->dev)); 535 492 return port; 536 493 537 494 error_put_device: