1 // SPDX-License-Identifier: GPL-2.0-only 2 /* WWAN Driver Core 3 * 4 * Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> 5 * Copyright (c) 2025, Sergey Ryazanov <ryazanov.s.a@gmail.com> 6 */ 7 8 #include <linux/bitmap.h> 9 #include <linux/err.h> 10 #include <linux/errno.h> 11 #include <linux/debugfs.h> 12 #include <linux/fs.h> 13 #include <linux/init.h> 14 #include <linux/idr.h> 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/poll.h> 18 #include <linux/skbuff.h> 19 #include <linux/slab.h> 20 #include <linux/types.h> 21 #include <linux/uaccess.h> 22 #include <linux/termios.h> 23 #include <linux/gnss.h> 24 #include <linux/wwan.h> 25 #include <net/rtnetlink.h> 26 #include <uapi/linux/wwan.h> 27 28 /* Modem control bits userspace may set on an AT port, as for tty_tiocmset() */ 29 #define WWAN_TIOCM_OUTPUTS (TIOCM_DTR | TIOCM_RTS | TIOCM_OUT1 | \ 30 TIOCM_OUT2 | TIOCM_LOOP) 31 32 /* Maximum number of minors in use */ 33 #define WWAN_MAX_MINORS (1 << MINORBITS) 34 35 static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */ 36 static DEFINE_IDA(minors); /* minors for WWAN port chardevs */ 37 static DEFINE_IDA(wwan_dev_ids); /* for unique WWAN device IDs */ 38 static const struct class wwan_class = { 39 .name = "wwan", 40 }; 41 static int wwan_major; 42 static struct dentry *wwan_debugfs_dir; 43 44 #define to_wwan_dev(d) container_of(d, struct wwan_device, dev) 45 #define to_wwan_port(d) container_of(d, struct wwan_port, dev) 46 47 /* WWAN port flags */ 48 #define WWAN_PORT_TX_OFF 0 49 #define WWAN_PORT_EXCLUSIVE 1 50 51 /** 52 * struct wwan_device - The structure that defines a WWAN device 53 * 54 * @id: WWAN device unique ID. 55 * @refcount: Reference count of this WWAN device. When this refcount reaches 56 * zero, the device is deleted. NB: access is protected by global 57 * wwan_register_lock mutex. 58 * @dev: Underlying device. 59 * @ops: wwan device ops 60 * @ops_ctxt: context to pass to ops 61 * @debugfs_dir: WWAN device debugfs dir 62 */ 63 struct wwan_device { 64 unsigned int id; 65 int refcount; 66 struct device dev; 67 const struct wwan_ops *ops; 68 void *ops_ctxt; 69 #ifdef CONFIG_WWAN_DEBUGFS 70 struct dentry *debugfs_dir; 71 #endif 72 }; 73 74 /** 75 * struct wwan_port - The structure that defines a WWAN port 76 * @type: Port type 77 * @start_count: Port start counter 78 * @flags: Store port state and capabilities 79 * @ops: Pointer to WWAN port operations 80 * @ops_lock: Protect port ops, and the AT/QCDM termios and TIOCM state 81 * @dev: Underlying device 82 * @rxq: Buffer inbound queue 83 * @waitqueue: The waitqueue for port fops (read/write/poll) 84 * @headroom_len: SKB reserved headroom size 85 * @frag_len: Length to fragment packet 86 * @at_data: AT port specific data 87 * @gnss: Pointer to GNSS device associated with this port 88 */ 89 struct wwan_port { 90 enum wwan_port_type type; 91 unsigned int start_count; 92 unsigned long flags; 93 const struct wwan_port_ops *ops; 94 struct mutex ops_lock; /* Serialize ops + at_data, protect against removal */ 95 struct device dev; 96 struct sk_buff_head rxq; 97 wait_queue_head_t waitqueue; 98 size_t headroom_len; 99 size_t frag_len; 100 union { 101 struct { 102 struct ktermios termios; 103 int mdmbits; 104 } at_data; 105 struct gnss_device *gnss; 106 }; 107 }; 108 109 static int wwan_port_op_start(struct wwan_port *port); 110 static void wwan_port_op_stop(struct wwan_port *port); 111 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb, 112 bool nonblock); 113 static int wwan_wait_tx(struct wwan_port *port, bool nonblock); 114 115 static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf) 116 { 117 struct wwan_device *wwan = to_wwan_dev(dev); 118 119 return sprintf(buf, "%d\n", wwan->id); 120 } 121 static DEVICE_ATTR_RO(index); 122 123 static struct attribute *wwan_dev_attrs[] = { 124 &dev_attr_index.attr, 125 NULL, 126 }; 127 ATTRIBUTE_GROUPS(wwan_dev); 128 129 static void wwan_dev_destroy(struct device *dev) 130 { 131 struct wwan_device *wwandev = to_wwan_dev(dev); 132 133 ida_free(&wwan_dev_ids, wwandev->id); 134 kfree(wwandev); 135 } 136 137 static const struct device_type wwan_dev_type = { 138 .name = "wwan_dev", 139 .release = wwan_dev_destroy, 140 .groups = wwan_dev_groups, 141 }; 142 143 static int wwan_dev_parent_match(struct device *dev, const void *parent) 144 { 145 return (dev->type == &wwan_dev_type && 146 (dev->parent == parent || dev == parent)); 147 } 148 149 static struct wwan_device *wwan_dev_get_by_parent(struct device *parent) 150 { 151 struct device *dev; 152 153 dev = class_find_device(&wwan_class, NULL, parent, wwan_dev_parent_match); 154 if (!dev) 155 return ERR_PTR(-ENODEV); 156 157 return to_wwan_dev(dev); 158 } 159 160 static int wwan_dev_name_match(struct device *dev, const void *name) 161 { 162 return dev->type == &wwan_dev_type && 163 strcmp(dev_name(dev), name) == 0; 164 } 165 166 static struct wwan_device *wwan_dev_get_by_name(const char *name) 167 { 168 struct device *dev; 169 170 dev = class_find_device(&wwan_class, NULL, name, wwan_dev_name_match); 171 if (!dev) 172 return ERR_PTR(-ENODEV); 173 174 return to_wwan_dev(dev); 175 } 176 177 #ifdef CONFIG_WWAN_DEBUGFS 178 struct dentry *wwan_get_debugfs_dir(struct device *parent) 179 { 180 struct wwan_device *wwandev; 181 182 wwandev = wwan_dev_get_by_parent(parent); 183 if (IS_ERR(wwandev)) 184 return ERR_CAST(wwandev); 185 186 return wwandev->debugfs_dir; 187 } 188 EXPORT_SYMBOL_GPL(wwan_get_debugfs_dir); 189 190 static int wwan_dev_debugfs_match(struct device *dev, const void *dir) 191 { 192 struct wwan_device *wwandev; 193 194 if (dev->type != &wwan_dev_type) 195 return 0; 196 197 wwandev = to_wwan_dev(dev); 198 199 return wwandev->debugfs_dir == dir; 200 } 201 202 static struct wwan_device *wwan_dev_get_by_debugfs(struct dentry *dir) 203 { 204 struct device *dev; 205 206 dev = class_find_device(&wwan_class, NULL, dir, wwan_dev_debugfs_match); 207 if (!dev) 208 return ERR_PTR(-ENODEV); 209 210 return to_wwan_dev(dev); 211 } 212 213 void wwan_put_debugfs_dir(struct dentry *dir) 214 { 215 struct wwan_device *wwandev = wwan_dev_get_by_debugfs(dir); 216 217 if (WARN_ON(IS_ERR(wwandev))) 218 return; 219 220 /* wwan_dev_get_by_debugfs() also got a reference */ 221 put_device(&wwandev->dev); 222 put_device(&wwandev->dev); 223 } 224 EXPORT_SYMBOL_GPL(wwan_put_debugfs_dir); 225 #endif 226 227 /* This function allocates and registers a new WWAN device OR if a WWAN device 228 * already exist for the given parent, it gets a reference and return it. 229 * This function is not exported (for now), it is called indirectly via 230 * wwan_create_port(). 231 */ 232 static struct wwan_device *wwan_create_dev(struct device *parent) 233 { 234 struct wwan_device *wwandev; 235 int err, id; 236 237 /* The 'find-alloc-register' operation must be protected against 238 * concurrent execution, a WWAN device is possibly shared between 239 * multiple callers or concurrently unregistered from wwan_remove_dev(). 240 */ 241 mutex_lock(&wwan_register_lock); 242 243 /* If wwandev already exists, return it */ 244 wwandev = wwan_dev_get_by_parent(parent); 245 if (!IS_ERR(wwandev)) { 246 wwandev->refcount++; 247 goto done_unlock; 248 } 249 250 id = ida_alloc(&wwan_dev_ids, GFP_KERNEL); 251 if (id < 0) { 252 wwandev = ERR_PTR(id); 253 goto done_unlock; 254 } 255 256 wwandev = kzalloc_obj(*wwandev); 257 if (!wwandev) { 258 wwandev = ERR_PTR(-ENOMEM); 259 ida_free(&wwan_dev_ids, id); 260 goto done_unlock; 261 } 262 263 wwandev->dev.parent = parent; 264 wwandev->dev.class = &wwan_class; 265 wwandev->dev.type = &wwan_dev_type; 266 wwandev->id = id; 267 wwandev->refcount = 1; 268 dev_set_name(&wwandev->dev, "wwan%d", wwandev->id); 269 270 err = device_register(&wwandev->dev); 271 if (err) { 272 put_device(&wwandev->dev); 273 wwandev = ERR_PTR(err); 274 goto done_unlock; 275 } 276 277 #ifdef CONFIG_WWAN_DEBUGFS 278 wwandev->debugfs_dir = 279 debugfs_create_dir(kobject_name(&wwandev->dev.kobj), 280 wwan_debugfs_dir); 281 #endif 282 283 done_unlock: 284 mutex_unlock(&wwan_register_lock); 285 286 return wwandev; 287 } 288 289 static void wwan_remove_dev(struct wwan_device *wwandev) 290 { 291 /* Prevent concurrent picking from wwan_create_dev */ 292 mutex_lock(&wwan_register_lock); 293 294 if (--wwandev->refcount <= 0) { 295 struct device *child = device_find_any_child(&wwandev->dev); 296 297 put_device(child); 298 if (WARN_ON(wwandev->ops || child)) /* Paranoid */ 299 goto out_unlock; 300 301 #ifdef CONFIG_WWAN_DEBUGFS 302 debugfs_remove_recursive(wwandev->debugfs_dir); 303 #endif 304 device_unregister(&wwandev->dev); 305 } else { 306 put_device(&wwandev->dev); 307 } 308 309 out_unlock: 310 mutex_unlock(&wwan_register_lock); 311 } 312 313 /* ------- WWAN port management ------- */ 314 315 static const struct { 316 const char * const name; /* Port type name */ 317 const char * const devsuf; /* Port device name suffix */ 318 } wwan_port_types[WWAN_PORT_MAX + 1] = { 319 [WWAN_PORT_AT] = { 320 .name = "AT", 321 .devsuf = "at", 322 }, 323 [WWAN_PORT_MBIM] = { 324 .name = "MBIM", 325 .devsuf = "mbim", 326 }, 327 [WWAN_PORT_QMI] = { 328 .name = "QMI", 329 .devsuf = "qmi", 330 }, 331 [WWAN_PORT_QCDM] = { 332 .name = "QCDM", 333 .devsuf = "qcdm", 334 }, 335 [WWAN_PORT_FIREHOSE] = { 336 .name = "FIREHOSE", 337 .devsuf = "firehose", 338 }, 339 [WWAN_PORT_XMMRPC] = { 340 .name = "XMMRPC", 341 .devsuf = "xmmrpc", 342 }, 343 [WWAN_PORT_FASTBOOT] = { 344 .name = "FASTBOOT", 345 .devsuf = "fastboot", 346 }, 347 [WWAN_PORT_ADB] = { 348 .name = "ADB", 349 .devsuf = "adb", 350 }, 351 [WWAN_PORT_MIPC] = { 352 .name = "MIPC", 353 .devsuf = "mipc", 354 }, 355 /* WWAN_PORT_NMEA is exported via the GNSS subsystem */ 356 }; 357 358 static ssize_t type_show(struct device *dev, struct device_attribute *attr, 359 char *buf) 360 { 361 struct wwan_port *port = to_wwan_port(dev); 362 363 return sprintf(buf, "%s\n", wwan_port_types[port->type].name); 364 } 365 static DEVICE_ATTR_RO(type); 366 367 static struct attribute *wwan_port_attrs[] = { 368 &dev_attr_type.attr, 369 NULL, 370 }; 371 ATTRIBUTE_GROUPS(wwan_port); 372 373 static void wwan_port_destroy(struct device *dev) 374 { 375 struct wwan_port *port = to_wwan_port(dev); 376 377 if (dev->class == &wwan_class) 378 ida_free(&minors, MINOR(dev->devt)); 379 mutex_destroy(&port->ops_lock); 380 kfree(port); 381 } 382 383 static const struct device_type wwan_port_dev_type = { 384 .name = "wwan_port", 385 .release = wwan_port_destroy, 386 .groups = wwan_port_groups, 387 }; 388 389 static int wwan_port_minor_match(struct device *dev, const void *minor) 390 { 391 return (dev->type == &wwan_port_dev_type && 392 MINOR(dev->devt) == *(unsigned int *)minor); 393 } 394 395 static struct wwan_port *wwan_port_get_by_minor(unsigned int minor) 396 { 397 struct device *dev; 398 399 dev = class_find_device(&wwan_class, NULL, &minor, wwan_port_minor_match); 400 if (!dev) 401 return ERR_PTR(-ENODEV); 402 403 return to_wwan_port(dev); 404 } 405 406 /* Allocate and set unique name based on passed format 407 * 408 * Name allocation approach is highly inspired by the __dev_alloc_name() 409 * function. 410 * 411 * To avoid names collision, the caller must prevent the new port device 412 * registration as well as concurrent invocation of this function. 413 */ 414 static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt) 415 { 416 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent); 417 const unsigned int max_ports = PAGE_SIZE * 8; 418 struct class_dev_iter iter; 419 unsigned long *idmap; 420 struct device *dev; 421 char buf[0x20]; 422 int id; 423 424 idmap = bitmap_zalloc(max_ports, GFP_KERNEL); 425 if (!idmap) 426 return -ENOMEM; 427 428 /* Collect ids of same name format ports */ 429 class_dev_iter_init(&iter, &wwan_class, NULL, &wwan_port_dev_type); 430 while ((dev = class_dev_iter_next(&iter))) { 431 if (dev->parent != &wwandev->dev) 432 continue; 433 if (sscanf(dev_name(dev), fmt, &id) != 1) 434 continue; 435 if (id < 0 || id >= max_ports) 436 continue; 437 set_bit(id, idmap); 438 } 439 class_dev_iter_exit(&iter); 440 441 /* Allocate unique id */ 442 id = find_first_zero_bit(idmap, max_ports); 443 bitmap_free(idmap); 444 445 snprintf(buf, sizeof(buf), fmt, id); /* Name generation */ 446 447 dev = device_find_child_by_name(&wwandev->dev, buf); 448 if (dev) { 449 put_device(dev); 450 return -ENFILE; 451 } 452 453 return dev_set_name(&port->dev, "%s", buf); 454 } 455 456 /* Register a regular WWAN port device (e.g. AT, MBIM, etc.) */ 457 static int wwan_port_register_wwan(struct wwan_port *port) 458 { 459 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent); 460 char namefmt[0x20]; 461 int minor, err; 462 463 /* A port is exposed as character device, get a minor */ 464 minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL); 465 if (minor < 0) 466 return minor; 467 468 port->dev.class = &wwan_class; 469 port->dev.devt = MKDEV(wwan_major, minor); 470 471 /* allocate unique name based on wwan device id, port type and number */ 472 snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id, 473 wwan_port_types[port->type].devsuf); 474 475 /* Serialize ports registration */ 476 mutex_lock(&wwan_register_lock); 477 478 __wwan_port_dev_assign_name(port, namefmt); 479 err = device_add(&port->dev); 480 481 mutex_unlock(&wwan_register_lock); 482 483 if (err) { 484 ida_free(&minors, minor); 485 port->dev.class = NULL; 486 return err; 487 } 488 489 dev_info(&wwandev->dev, "port %s attached\n", dev_name(&port->dev)); 490 491 return 0; 492 } 493 494 /* Unregister a regular WWAN port (e.g. AT, MBIM, etc) */ 495 static void wwan_port_unregister_wwan(struct wwan_port *port) 496 { 497 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent); 498 499 dev_set_drvdata(&port->dev, NULL); 500 501 dev_info(&wwandev->dev, "port %s disconnected\n", dev_name(&port->dev)); 502 503 device_del(&port->dev); 504 } 505 506 #if IS_ENABLED(CONFIG_GNSS) 507 static int wwan_gnss_open(struct gnss_device *gdev) 508 { 509 return wwan_port_op_start(gnss_get_drvdata(gdev)); 510 } 511 512 static void wwan_gnss_close(struct gnss_device *gdev) 513 { 514 wwan_port_op_stop(gnss_get_drvdata(gdev)); 515 } 516 517 static int wwan_gnss_write(struct gnss_device *gdev, const unsigned char *buf, 518 size_t count) 519 { 520 struct wwan_port *port = gnss_get_drvdata(gdev); 521 struct sk_buff *skb, *head = NULL, *tail = NULL; 522 size_t frag_len, remain = count; 523 int ret; 524 525 ret = wwan_wait_tx(port, false); 526 if (ret) 527 return ret; 528 529 do { 530 frag_len = min(remain, port->frag_len); 531 skb = alloc_skb(frag_len + port->headroom_len, GFP_KERNEL); 532 if (!skb) { 533 ret = -ENOMEM; 534 goto freeskb; 535 } 536 skb_reserve(skb, port->headroom_len); 537 memcpy(skb_put(skb, frag_len), buf + count - remain, frag_len); 538 539 if (!head) { 540 head = skb; 541 } else { 542 if (!tail) 543 skb_shinfo(head)->frag_list = skb; 544 else 545 tail->next = skb; 546 547 tail = skb; 548 head->data_len += skb->len; 549 head->len += skb->len; 550 head->truesize += skb->truesize; 551 } 552 } while (remain -= frag_len); 553 554 ret = wwan_port_op_tx(port, head, false); 555 if (!ret) 556 return count; 557 558 freeskb: 559 kfree_skb(head); 560 return ret; 561 } 562 563 static struct gnss_operations wwan_gnss_ops = { 564 .open = wwan_gnss_open, 565 .close = wwan_gnss_close, 566 .write_raw = wwan_gnss_write, 567 }; 568 569 /* GNSS port specific device registration */ 570 static int wwan_port_register_gnss(struct wwan_port *port) 571 { 572 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent); 573 struct gnss_device *gdev; 574 int err; 575 576 gdev = gnss_allocate_device(&wwandev->dev); 577 if (!gdev) 578 return -ENOMEM; 579 580 /* NB: for now we support only NMEA WWAN port type, so hardcode 581 * the GNSS port type. If more GNSS WWAN port types will be added, 582 * then we should dynamically map WWAN port type to GNSS type. 583 */ 584 gdev->type = GNSS_TYPE_NMEA; 585 gdev->ops = &wwan_gnss_ops; 586 gnss_set_drvdata(gdev, port); 587 588 port->gnss = gdev; 589 590 err = gnss_register_device(gdev); 591 if (err) { 592 gnss_put_device(gdev); 593 return err; 594 } 595 596 dev_info(&wwandev->dev, "port %s attached\n", dev_name(&gdev->dev)); 597 598 return 0; 599 } 600 601 /* GNSS port specific device unregistration */ 602 static void wwan_port_unregister_gnss(struct wwan_port *port) 603 { 604 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent); 605 struct gnss_device *gdev = port->gnss; 606 607 dev_info(&wwandev->dev, "port %s disconnected\n", dev_name(&gdev->dev)); 608 609 gnss_deregister_device(gdev); 610 gnss_put_device(gdev); 611 } 612 #else 613 static int wwan_port_register_gnss(struct wwan_port *port) 614 { 615 return -EOPNOTSUPP; 616 } 617 618 static void wwan_port_unregister_gnss(struct wwan_port *port) 619 { 620 WARN_ON(1); /* This handler cannot be called */ 621 } 622 #endif 623 624 struct wwan_port *wwan_create_port(struct device *parent, 625 enum wwan_port_type type, 626 const struct wwan_port_ops *ops, 627 struct wwan_port_caps *caps, 628 void *drvdata) 629 { 630 struct wwan_device *wwandev; 631 struct wwan_port *port; 632 int err; 633 634 if (type > WWAN_PORT_MAX || !ops) 635 return ERR_PTR(-EINVAL); 636 637 /* A port is always a child of a WWAN device, retrieve (allocate or 638 * pick) the WWAN device based on the provided parent device. 639 */ 640 wwandev = wwan_create_dev(parent); 641 if (IS_ERR(wwandev)) 642 return ERR_CAST(wwandev); 643 644 port = kzalloc_obj(*port); 645 if (!port) { 646 err = -ENOMEM; 647 goto error_wwandev_remove; 648 } 649 650 port->type = type; 651 port->ops = ops; 652 port->frag_len = caps ? caps->frag_len : SIZE_MAX; 653 port->headroom_len = caps ? caps->headroom_len : 0; 654 mutex_init(&port->ops_lock); 655 skb_queue_head_init(&port->rxq); 656 init_waitqueue_head(&port->waitqueue); 657 658 /* AT ports default to HUPCL, so DTR/RTS are dropped on last close */ 659 if (type == WWAN_PORT_AT) 660 port->at_data.termios.c_cflag = HUPCL; 661 662 port->dev.parent = &wwandev->dev; 663 port->dev.type = &wwan_port_dev_type; 664 dev_set_drvdata(&port->dev, drvdata); 665 device_initialize(&port->dev); 666 667 if (port->type == WWAN_PORT_NMEA) 668 err = wwan_port_register_gnss(port); 669 else 670 err = wwan_port_register_wwan(port); 671 672 if (err) 673 goto error_put_device; 674 675 return port; 676 677 error_put_device: 678 put_device(&port->dev); 679 error_wwandev_remove: 680 wwan_remove_dev(wwandev); 681 682 return ERR_PTR(err); 683 } 684 EXPORT_SYMBOL_GPL(wwan_create_port); 685 686 /* Raise DTR/RTS when an AT port is first opened, as a TTY does on open. 687 * Unlike a TTY the termios baud rate is not used. Called with ops_lock held. 688 */ 689 static void wwan_port_raise_dtr_rts(struct wwan_port *port) 690 { 691 lockdep_assert_held(&port->ops_lock); 692 693 if (port->type != WWAN_PORT_AT || !port->ops->dtr_rts) 694 return; 695 696 port->at_data.mdmbits |= TIOCM_DTR | TIOCM_RTS; 697 port->ops->dtr_rts(port, port->at_data.mdmbits); 698 } 699 700 /* Drop DTR/RTS on an AT port. Called with ops_lock held. On last close 701 * the lines are only dropped if HUPCL is set, as for a TTY. When a port is 702 * removed while still open they are always dropped. 703 */ 704 static void wwan_port_drop_dtr_rts(struct wwan_port *port, bool hupcl_only) 705 { 706 lockdep_assert_held(&port->ops_lock); 707 708 if (port->type != WWAN_PORT_AT || !port->ops->dtr_rts) 709 return; 710 if (hupcl_only && !(port->at_data.termios.c_cflag & HUPCL)) 711 return; 712 713 port->at_data.mdmbits &= ~(TIOCM_DTR | TIOCM_RTS); 714 port->ops->dtr_rts(port, port->at_data.mdmbits); 715 } 716 717 void wwan_remove_port(struct wwan_port *port) 718 { 719 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent); 720 721 mutex_lock(&port->ops_lock); 722 if (port->start_count) { 723 wwan_port_drop_dtr_rts(port, false); 724 port->ops->stop(port); 725 port->start_count = 0; 726 } 727 port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */ 728 mutex_unlock(&port->ops_lock); 729 730 wake_up_interruptible(&port->waitqueue); 731 skb_queue_purge(&port->rxq); 732 733 if (port->type == WWAN_PORT_NMEA) 734 wwan_port_unregister_gnss(port); 735 else 736 wwan_port_unregister_wwan(port); 737 738 put_device(&port->dev); 739 740 /* Release related wwan device */ 741 wwan_remove_dev(wwandev); 742 } 743 EXPORT_SYMBOL_GPL(wwan_remove_port); 744 745 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb) 746 { 747 if (port->type == WWAN_PORT_NMEA) { 748 #if IS_ENABLED(CONFIG_GNSS) 749 gnss_insert_raw(port->gnss, skb->data, skb->len); 750 #endif 751 consume_skb(skb); 752 } else { 753 skb_queue_tail(&port->rxq, skb); 754 wake_up_interruptible(&port->waitqueue); 755 } 756 } 757 EXPORT_SYMBOL_GPL(wwan_port_rx); 758 759 void wwan_port_txon(struct wwan_port *port) 760 { 761 clear_bit(WWAN_PORT_TX_OFF, &port->flags); 762 wake_up_interruptible(&port->waitqueue); 763 } 764 EXPORT_SYMBOL_GPL(wwan_port_txon); 765 766 void wwan_port_txoff(struct wwan_port *port) 767 { 768 set_bit(WWAN_PORT_TX_OFF, &port->flags); 769 } 770 EXPORT_SYMBOL_GPL(wwan_port_txoff); 771 772 void *wwan_port_get_drvdata(struct wwan_port *port) 773 { 774 return dev_get_drvdata(&port->dev); 775 } 776 EXPORT_SYMBOL_GPL(wwan_port_get_drvdata); 777 778 static int wwan_port_op_start(struct wwan_port *port) 779 { 780 int ret = 0; 781 782 mutex_lock(&port->ops_lock); 783 if (!port->ops) { /* Port got unplugged */ 784 ret = -ENODEV; 785 goto out_unlock; 786 } 787 788 if (test_bit(WWAN_PORT_EXCLUSIVE, &port->flags) && 789 !capable(CAP_SYS_ADMIN)) { 790 ret = -EBUSY; 791 goto out_unlock; 792 } 793 794 /* If port is already started, don't start again */ 795 if (!port->start_count) { 796 ret = port->ops->start(port); 797 if (!ret) 798 wwan_port_raise_dtr_rts(port); 799 } 800 801 if (!ret) 802 port->start_count++; 803 804 out_unlock: 805 mutex_unlock(&port->ops_lock); 806 807 return ret; 808 } 809 810 static void wwan_port_op_stop(struct wwan_port *port) 811 { 812 mutex_lock(&port->ops_lock); 813 port->start_count--; 814 if (!port->start_count) { 815 if (port->ops) { 816 wwan_port_drop_dtr_rts(port, true); 817 port->ops->stop(port); 818 } 819 skb_queue_purge(&port->rxq); 820 clear_bit(WWAN_PORT_EXCLUSIVE, &port->flags); 821 } 822 mutex_unlock(&port->ops_lock); 823 } 824 825 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb, 826 bool nonblock) 827 { 828 int ret; 829 830 mutex_lock(&port->ops_lock); 831 if (!port->ops) { /* Port got unplugged */ 832 ret = -ENODEV; 833 goto out_unlock; 834 } 835 836 if (nonblock || !port->ops->tx_blocking) 837 ret = port->ops->tx(port, skb); 838 else 839 ret = port->ops->tx_blocking(port, skb); 840 841 out_unlock: 842 mutex_unlock(&port->ops_lock); 843 844 return ret; 845 } 846 847 static bool is_read_blocked(struct wwan_port *port) 848 { 849 return skb_queue_empty(&port->rxq) && port->ops; 850 } 851 852 static bool is_write_blocked(struct wwan_port *port) 853 { 854 return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops; 855 } 856 857 static int wwan_wait_rx(struct wwan_port *port, bool nonblock) 858 { 859 if (!is_read_blocked(port)) 860 return 0; 861 862 if (nonblock) 863 return -EAGAIN; 864 865 if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port))) 866 return -ERESTARTSYS; 867 868 return 0; 869 } 870 871 static int wwan_wait_tx(struct wwan_port *port, bool nonblock) 872 { 873 if (!is_write_blocked(port)) 874 return 0; 875 876 if (nonblock) 877 return -EAGAIN; 878 879 if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port))) 880 return -ERESTARTSYS; 881 882 return 0; 883 } 884 885 static int wwan_port_fops_open(struct inode *inode, struct file *file) 886 { 887 struct wwan_port *port; 888 int err = 0; 889 890 port = wwan_port_get_by_minor(iminor(inode)); 891 if (IS_ERR(port)) 892 return PTR_ERR(port); 893 894 file->private_data = port; 895 stream_open(inode, file); 896 897 err = wwan_port_op_start(port); 898 if (err) 899 put_device(&port->dev); 900 901 return err; 902 } 903 904 static int wwan_port_fops_release(struct inode *inode, struct file *filp) 905 { 906 struct wwan_port *port = filp->private_data; 907 908 wwan_port_op_stop(port); 909 put_device(&port->dev); 910 911 return 0; 912 } 913 914 static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf, 915 size_t count, loff_t *ppos) 916 { 917 struct wwan_port *port = filp->private_data; 918 struct sk_buff *skb; 919 size_t copied; 920 int ret; 921 922 ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK)); 923 if (ret) 924 return ret; 925 926 skb = skb_dequeue(&port->rxq); 927 if (!skb) 928 return -EIO; 929 930 copied = min_t(size_t, count, skb->len); 931 if (copy_to_user(buf, skb->data, copied)) { 932 kfree_skb(skb); 933 return -EFAULT; 934 } 935 skb_pull(skb, copied); 936 937 /* skb is not fully consumed, keep it in the queue */ 938 if (skb->len) 939 skb_queue_head(&port->rxq, skb); 940 else 941 consume_skb(skb); 942 943 return copied; 944 } 945 946 static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf, 947 size_t count, loff_t *offp) 948 { 949 struct sk_buff *skb, *head = NULL, *tail = NULL; 950 struct wwan_port *port = filp->private_data; 951 size_t frag_len, remain = count; 952 int ret; 953 954 ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK)); 955 if (ret) 956 return ret; 957 958 do { 959 frag_len = min(remain, port->frag_len); 960 skb = alloc_skb(frag_len + port->headroom_len, GFP_KERNEL); 961 if (!skb) { 962 ret = -ENOMEM; 963 goto freeskb; 964 } 965 skb_reserve(skb, port->headroom_len); 966 967 if (!head) { 968 head = skb; 969 } else if (!tail) { 970 skb_shinfo(head)->frag_list = skb; 971 tail = skb; 972 } else { 973 tail->next = skb; 974 tail = skb; 975 } 976 977 if (copy_from_user(skb_put(skb, frag_len), buf + count - remain, frag_len)) { 978 ret = -EFAULT; 979 goto freeskb; 980 } 981 982 if (skb != head) { 983 head->data_len += skb->len; 984 head->len += skb->len; 985 head->truesize += skb->truesize; 986 } 987 } while (remain -= frag_len); 988 989 ret = wwan_port_op_tx(port, head, !!(filp->f_flags & O_NONBLOCK)); 990 if (!ret) 991 return count; 992 993 freeskb: 994 kfree_skb(head); 995 return ret; 996 } 997 998 static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait) 999 { 1000 struct wwan_port *port = filp->private_data; 1001 __poll_t mask = 0; 1002 1003 poll_wait(filp, &port->waitqueue, wait); 1004 1005 mutex_lock(&port->ops_lock); 1006 if (port->ops && port->ops->tx_poll) 1007 mask |= port->ops->tx_poll(port, filp, wait); 1008 else if (!is_write_blocked(port)) 1009 mask |= EPOLLOUT | EPOLLWRNORM; 1010 if (!is_read_blocked(port)) 1011 mask |= EPOLLIN | EPOLLRDNORM; 1012 if (!port->ops) 1013 mask |= EPOLLHUP | EPOLLERR; 1014 mutex_unlock(&port->ops_lock); 1015 1016 return mask; 1017 } 1018 1019 /* Implements minimalistic stub terminal IOCTLs support */ 1020 /* TCGETS/TCSETS and their termios2 variants. @size is the size of the user 1021 * structure, a prefix of struct ktermios. As for the TIOCM ioctls, user 1022 * memory is only accessed outside ops_lock. 1023 */ 1024 static long wwan_port_termios_ioctl(struct wwan_port *port, bool set, 1025 void __user *uarg, size_t size) 1026 { 1027 struct ktermios termios; 1028 1029 if (!set) { 1030 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->ops_lock) 1031 termios = port->at_data.termios; 1032 return copy_to_user(uarg, &termios, size) ? -EFAULT : 0; 1033 } 1034 1035 /* Commit only on success, a partial copy must not clear HUPCL */ 1036 if (copy_from_user(&termios, uarg, size)) 1037 return -EFAULT; 1038 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->ops_lock) 1039 memcpy(&port->at_data.termios, &termios, size); 1040 return 0; 1041 } 1042 1043 static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd, 1044 unsigned long arg) 1045 { 1046 /* ops_lock serialises the termios/TIOCM state with port ops and 1047 * removal. It is only held while that state is read or updated, never 1048 * across a user copy, and is taken interruptibly as a blocking write 1049 * holds it too. 1050 */ 1051 switch (cmd) { 1052 case TCFLSH: 1053 return 0; 1054 1055 case TCGETS: 1056 case TCSETS: 1057 case TCSETSW: 1058 case TCSETSF: 1059 return wwan_port_termios_ioctl(port, cmd != TCGETS, 1060 (void __user *)arg, 1061 sizeof(struct termios)); 1062 1063 #ifdef TCGETS2 1064 case TCGETS2: 1065 case TCSETS2: 1066 case TCSETSW2: 1067 case TCSETSF2: 1068 return wwan_port_termios_ioctl(port, cmd != TCGETS2, 1069 (void __user *)arg, 1070 sizeof(struct termios2)); 1071 #endif 1072 1073 case TIOCMGET: { 1074 int mdmbits; 1075 1076 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->ops_lock) 1077 mdmbits = port->at_data.mdmbits; 1078 return put_user(mdmbits, (int __user *)arg); 1079 } 1080 1081 case TIOCMSET: 1082 case TIOCMBIC: 1083 case TIOCMBIS: { 1084 int mdmbits; 1085 1086 if (get_user(mdmbits, (int __user *)arg)) 1087 return -EFAULT; 1088 mdmbits &= WWAN_TIOCM_OUTPUTS; 1089 1090 scoped_cond_guard(mutex_intr, return -ERESTARTSYS, &port->ops_lock) { 1091 int old = port->at_data.mdmbits; 1092 1093 if (cmd == TIOCMBIC) 1094 port->at_data.mdmbits &= ~mdmbits; 1095 else if (cmd == TIOCMBIS) 1096 port->at_data.mdmbits |= mdmbits; 1097 else 1098 port->at_data.mdmbits = mdmbits; 1099 /* Only signal the driver when DTR or RTS actually changes */ 1100 if (port->type == WWAN_PORT_AT && port->ops && 1101 port->ops->dtr_rts && 1102 (old ^ port->at_data.mdmbits) & (TIOCM_DTR | TIOCM_RTS)) 1103 port->ops->dtr_rts(port, port->at_data.mdmbits); 1104 } 1105 return 0; 1106 } 1107 1108 case TIOCEXCL: 1109 set_bit(WWAN_PORT_EXCLUSIVE, &port->flags); 1110 return 0; 1111 1112 case TIOCNXCL: 1113 clear_bit(WWAN_PORT_EXCLUSIVE, &port->flags); 1114 return 0; 1115 1116 case TIOCGEXCL: 1117 return put_user(test_bit(WWAN_PORT_EXCLUSIVE, &port->flags), 1118 (int __user *)arg); 1119 1120 default: 1121 return -ENOIOCTLCMD; 1122 } 1123 } 1124 1125 static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd, 1126 unsigned long arg) 1127 { 1128 struct wwan_port *port = filp->private_data; 1129 int res; 1130 1131 if (port->type == WWAN_PORT_AT || port->type == WWAN_PORT_QCDM) { 1132 /* AT and QCDM port specific IOCTLs */ 1133 res = wwan_port_fops_at_ioctl(port, cmd, arg); 1134 if (res != -ENOIOCTLCMD) 1135 return res; 1136 } 1137 1138 switch (cmd) { 1139 case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */ 1140 unsigned long flags; 1141 struct sk_buff *skb; 1142 int amount = 0; 1143 1144 spin_lock_irqsave(&port->rxq.lock, flags); 1145 skb_queue_walk(&port->rxq, skb) 1146 amount += skb->len; 1147 spin_unlock_irqrestore(&port->rxq.lock, flags); 1148 1149 return put_user(amount, (int __user *)arg); 1150 } 1151 1152 default: 1153 return -ENOIOCTLCMD; 1154 } 1155 } 1156 1157 static const struct file_operations wwan_port_fops = { 1158 .owner = THIS_MODULE, 1159 .open = wwan_port_fops_open, 1160 .release = wwan_port_fops_release, 1161 .read = wwan_port_fops_read, 1162 .write = wwan_port_fops_write, 1163 .poll = wwan_port_fops_poll, 1164 .unlocked_ioctl = wwan_port_fops_ioctl, 1165 #ifdef CONFIG_COMPAT 1166 .compat_ioctl = compat_ptr_ioctl, 1167 #endif 1168 .llseek = noop_llseek, 1169 }; 1170 1171 static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[], 1172 struct netlink_ext_ack *extack) 1173 { 1174 if (!data) 1175 return -EINVAL; 1176 1177 if (!tb[IFLA_PARENT_DEV_NAME]) 1178 return -EINVAL; 1179 1180 if (!data[IFLA_WWAN_LINK_ID]) 1181 return -EINVAL; 1182 1183 return 0; 1184 } 1185 1186 static const struct device_type wwan_type = { .name = "wwan" }; 1187 1188 static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[], 1189 const char *ifname, 1190 unsigned char name_assign_type, 1191 unsigned int num_tx_queues, 1192 unsigned int num_rx_queues) 1193 { 1194 const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]); 1195 struct wwan_device *wwandev = wwan_dev_get_by_name(devname); 1196 struct net_device *dev; 1197 unsigned int priv_size; 1198 1199 if (IS_ERR(wwandev)) 1200 return ERR_CAST(wwandev); 1201 1202 /* only supported if ops were registered (not just ports) */ 1203 if (!wwandev->ops) { 1204 dev = ERR_PTR(-EOPNOTSUPP); 1205 goto out; 1206 } 1207 1208 priv_size = sizeof(struct wwan_netdev_priv) + wwandev->ops->priv_size; 1209 dev = alloc_netdev_mqs(priv_size, ifname, name_assign_type, 1210 wwandev->ops->setup, num_tx_queues, num_rx_queues); 1211 1212 if (dev) { 1213 SET_NETDEV_DEV(dev, &wwandev->dev); 1214 SET_NETDEV_DEVTYPE(dev, &wwan_type); 1215 } 1216 1217 out: 1218 /* release the reference */ 1219 put_device(&wwandev->dev); 1220 return dev; 1221 } 1222 1223 static int wwan_rtnl_newlink(struct net_device *dev, 1224 struct rtnl_newlink_params *params, 1225 struct netlink_ext_ack *extack) 1226 { 1227 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent); 1228 struct wwan_netdev_priv *priv = netdev_priv(dev); 1229 struct nlattr **data = params->data; 1230 u32 link_id; 1231 int ret; 1232 1233 link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]); 1234 1235 if (IS_ERR(wwandev)) 1236 return PTR_ERR(wwandev); 1237 1238 /* shouldn't have a netdev (left) with us as parent so WARN */ 1239 if (WARN_ON(!wwandev->ops)) { 1240 ret = -EOPNOTSUPP; 1241 goto out; 1242 } 1243 1244 priv->link_id = link_id; 1245 if (wwandev->ops->newlink) 1246 ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev, 1247 link_id, extack); 1248 else 1249 ret = register_netdevice(dev); 1250 1251 out: 1252 /* release the reference */ 1253 put_device(&wwandev->dev); 1254 return ret; 1255 } 1256 1257 static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head) 1258 { 1259 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent); 1260 1261 if (IS_ERR(wwandev)) 1262 return; 1263 1264 /* shouldn't have a netdev (left) with us as parent so WARN */ 1265 if (WARN_ON(!wwandev->ops)) 1266 goto out; 1267 1268 if (wwandev->ops->dellink) 1269 wwandev->ops->dellink(wwandev->ops_ctxt, dev, head); 1270 else 1271 unregister_netdevice_queue(dev, head); 1272 1273 out: 1274 /* release the reference */ 1275 put_device(&wwandev->dev); 1276 } 1277 1278 static size_t wwan_rtnl_get_size(const struct net_device *dev) 1279 { 1280 return 1281 nla_total_size(4) + /* IFLA_WWAN_LINK_ID */ 1282 0; 1283 } 1284 1285 static int wwan_rtnl_fill_info(struct sk_buff *skb, 1286 const struct net_device *dev) 1287 { 1288 struct wwan_netdev_priv *priv = netdev_priv(dev); 1289 1290 if (nla_put_u32(skb, IFLA_WWAN_LINK_ID, priv->link_id)) 1291 goto nla_put_failure; 1292 1293 return 0; 1294 1295 nla_put_failure: 1296 return -EMSGSIZE; 1297 } 1298 1299 static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = { 1300 [IFLA_WWAN_LINK_ID] = { .type = NLA_U32 }, 1301 }; 1302 1303 static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = { 1304 .kind = "wwan", 1305 .maxtype = IFLA_WWAN_MAX, 1306 .alloc = wwan_rtnl_alloc, 1307 .validate = wwan_rtnl_validate, 1308 .newlink = wwan_rtnl_newlink, 1309 .dellink = wwan_rtnl_dellink, 1310 .get_size = wwan_rtnl_get_size, 1311 .fill_info = wwan_rtnl_fill_info, 1312 .policy = wwan_rtnl_policy, 1313 }; 1314 1315 static void wwan_create_default_link(struct wwan_device *wwandev, 1316 u32 def_link_id) 1317 { 1318 struct nlattr *tb[IFLA_MAX + 1], *linkinfo[IFLA_INFO_MAX + 1]; 1319 struct nlattr *data[IFLA_WWAN_MAX + 1]; 1320 struct rtnl_newlink_params params = { 1321 .src_net = &init_net, 1322 .tb = tb, 1323 .data = data, 1324 }; 1325 struct net_device *dev; 1326 struct nlmsghdr *nlh; 1327 struct sk_buff *msg; 1328 1329 /* Forge attributes required to create a WWAN netdev. We first 1330 * build a netlink message and then parse it. This looks 1331 * odd, but such approach is less error prone. 1332 */ 1333 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 1334 if (WARN_ON(!msg)) 1335 return; 1336 nlh = nlmsg_put(msg, 0, 0, RTM_NEWLINK, 0, 0); 1337 if (WARN_ON(!nlh)) 1338 goto free_attrs; 1339 1340 if (nla_put_string(msg, IFLA_PARENT_DEV_NAME, dev_name(&wwandev->dev))) 1341 goto free_attrs; 1342 tb[IFLA_LINKINFO] = nla_nest_start(msg, IFLA_LINKINFO); 1343 if (!tb[IFLA_LINKINFO]) 1344 goto free_attrs; 1345 linkinfo[IFLA_INFO_DATA] = nla_nest_start(msg, IFLA_INFO_DATA); 1346 if (!linkinfo[IFLA_INFO_DATA]) 1347 goto free_attrs; 1348 if (nla_put_u32(msg, IFLA_WWAN_LINK_ID, def_link_id)) 1349 goto free_attrs; 1350 nla_nest_end(msg, linkinfo[IFLA_INFO_DATA]); 1351 nla_nest_end(msg, tb[IFLA_LINKINFO]); 1352 1353 nlmsg_end(msg, nlh); 1354 1355 /* The next three parsing calls can not fail */ 1356 nlmsg_parse_deprecated(nlh, 0, tb, IFLA_MAX, NULL, NULL); 1357 nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO], 1358 NULL, NULL); 1359 nla_parse_nested_deprecated(data, IFLA_WWAN_MAX, 1360 linkinfo[IFLA_INFO_DATA], NULL, NULL); 1361 1362 rtnl_lock(); 1363 1364 dev = rtnl_create_link(&init_net, "wwan%d", NET_NAME_ENUM, 1365 &wwan_rtnl_link_ops, tb, NULL); 1366 if (WARN_ON(IS_ERR(dev))) 1367 goto unlock; 1368 1369 if (WARN_ON(wwan_rtnl_newlink(dev, ¶ms, NULL))) { 1370 free_netdev(dev); 1371 goto unlock; 1372 } 1373 1374 rtnl_configure_link(dev, NULL, 0, NULL); /* Link initialized, notify new link */ 1375 1376 unlock: 1377 rtnl_unlock(); 1378 1379 free_attrs: 1380 nlmsg_free(msg); 1381 } 1382 1383 /** 1384 * wwan_register_ops - register WWAN device ops 1385 * @parent: Device to use as parent and shared by all WWAN ports and 1386 * created netdevs 1387 * @ops: operations to register 1388 * @ctxt: context to pass to operations 1389 * @def_link_id: id of the default link that will be automatically created by 1390 * the WWAN core for the WWAN device. The default link will not be created 1391 * if the passed value is WWAN_NO_DEFAULT_LINK. 1392 * 1393 * Returns: 0 on success, a negative error code on failure 1394 */ 1395 int wwan_register_ops(struct device *parent, const struct wwan_ops *ops, 1396 void *ctxt, u32 def_link_id) 1397 { 1398 struct wwan_device *wwandev; 1399 1400 if (WARN_ON(!parent || !ops || !ops->setup)) 1401 return -EINVAL; 1402 1403 wwandev = wwan_create_dev(parent); 1404 if (IS_ERR(wwandev)) 1405 return PTR_ERR(wwandev); 1406 1407 if (WARN_ON(wwandev->ops)) { 1408 wwan_remove_dev(wwandev); 1409 return -EBUSY; 1410 } 1411 1412 wwandev->ops = ops; 1413 wwandev->ops_ctxt = ctxt; 1414 1415 /* NB: we do not abort ops registration in case of default link 1416 * creation failure. Link ops is the management interface, while the 1417 * default link creation is a service option. And we should not prevent 1418 * a user from manually creating a link latter if service option failed 1419 * now. 1420 */ 1421 if (def_link_id != WWAN_NO_DEFAULT_LINK) 1422 wwan_create_default_link(wwandev, def_link_id); 1423 1424 return 0; 1425 } 1426 EXPORT_SYMBOL_GPL(wwan_register_ops); 1427 1428 /* Enqueue child netdev deletion */ 1429 static int wwan_child_dellink(struct device *dev, void *data) 1430 { 1431 struct list_head *kill_list = data; 1432 1433 if (dev->type == &wwan_type) 1434 wwan_rtnl_dellink(to_net_dev(dev), kill_list); 1435 1436 return 0; 1437 } 1438 1439 /** 1440 * wwan_unregister_ops - remove WWAN device ops 1441 * @parent: Device to use as parent and shared by all WWAN ports and 1442 * created netdevs 1443 */ 1444 void wwan_unregister_ops(struct device *parent) 1445 { 1446 struct wwan_device *wwandev = wwan_dev_get_by_parent(parent); 1447 LIST_HEAD(kill_list); 1448 1449 if (WARN_ON(IS_ERR(wwandev))) 1450 return; 1451 if (WARN_ON(!wwandev->ops)) { 1452 put_device(&wwandev->dev); 1453 return; 1454 } 1455 1456 /* put the reference obtained by wwan_dev_get_by_parent(), 1457 * we should still have one (that the owner is giving back 1458 * now) due to the ops being assigned. 1459 */ 1460 put_device(&wwandev->dev); 1461 1462 rtnl_lock(); /* Prevent concurrent netdev(s) creation/destroying */ 1463 1464 /* Remove all child netdev(s), using batch removing */ 1465 device_for_each_child(&wwandev->dev, &kill_list, 1466 wwan_child_dellink); 1467 unregister_netdevice_many(&kill_list); 1468 1469 wwandev->ops = NULL; /* Finally remove ops */ 1470 1471 rtnl_unlock(); 1472 1473 wwandev->ops_ctxt = NULL; 1474 wwan_remove_dev(wwandev); 1475 } 1476 EXPORT_SYMBOL_GPL(wwan_unregister_ops); 1477 1478 static int __init wwan_init(void) 1479 { 1480 int err; 1481 1482 err = rtnl_link_register(&wwan_rtnl_link_ops); 1483 if (err) 1484 return err; 1485 1486 err = class_register(&wwan_class); 1487 if (err) 1488 goto unregister; 1489 1490 /* chrdev used for wwan ports */ 1491 wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port", 1492 &wwan_port_fops); 1493 if (wwan_major < 0) { 1494 err = wwan_major; 1495 goto destroy; 1496 } 1497 1498 #ifdef CONFIG_WWAN_DEBUGFS 1499 wwan_debugfs_dir = debugfs_create_dir("wwan", NULL); 1500 #endif 1501 1502 return 0; 1503 1504 destroy: 1505 class_unregister(&wwan_class); 1506 unregister: 1507 rtnl_link_unregister(&wwan_rtnl_link_ops); 1508 return err; 1509 } 1510 1511 static void __exit wwan_exit(void) 1512 { 1513 debugfs_remove_recursive(wwan_debugfs_dir); 1514 __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port"); 1515 rtnl_link_unregister(&wwan_rtnl_link_ops); 1516 class_unregister(&wwan_class); 1517 } 1518 1519 module_init(wwan_init); 1520 module_exit(wwan_exit); 1521 1522 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>"); 1523 MODULE_DESCRIPTION("WWAN core"); 1524 MODULE_LICENSE("GPL v2"); 1525