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