1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2021, Linaro Ltd <loic.poulain@linaro.org> */ 3 4 #include <linux/err.h> 5 #include <linux/errno.h> 6 #include <linux/fs.h> 7 #include <linux/init.h> 8 #include <linux/idr.h> 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/poll.h> 12 #include <linux/skbuff.h> 13 #include <linux/slab.h> 14 #include <linux/types.h> 15 #include <linux/termios.h> 16 #include <linux/wwan.h> 17 #include <net/rtnetlink.h> 18 #include <uapi/linux/wwan.h> 19 20 /* Maximum number of minors in use */ 21 #define WWAN_MAX_MINORS (1 << MINORBITS) 22 23 static DEFINE_MUTEX(wwan_register_lock); /* WWAN device create|remove lock */ 24 static DEFINE_IDA(minors); /* minors for WWAN port chardevs */ 25 static DEFINE_IDA(wwan_dev_ids); /* for unique WWAN device IDs */ 26 static struct class *wwan_class; 27 static int wwan_major; 28 29 #define to_wwan_dev(d) container_of(d, struct wwan_device, dev) 30 #define to_wwan_port(d) container_of(d, struct wwan_port, dev) 31 32 /* WWAN port flags */ 33 #define WWAN_PORT_TX_OFF 0 34 35 /** 36 * struct wwan_device - The structure that defines a WWAN device 37 * 38 * @id: WWAN device unique ID. 39 * @dev: Underlying device. 40 * @port_id: Current available port ID to pick. 41 * @ops: wwan device ops 42 * @ops_ctxt: context to pass to ops 43 */ 44 struct wwan_device { 45 unsigned int id; 46 struct device dev; 47 atomic_t port_id; 48 const struct wwan_ops *ops; 49 void *ops_ctxt; 50 }; 51 52 /** 53 * struct wwan_port - The structure that defines a WWAN port 54 * @type: Port type 55 * @start_count: Port start counter 56 * @flags: Store port state and capabilities 57 * @ops: Pointer to WWAN port operations 58 * @ops_lock: Protect port ops 59 * @dev: Underlying device 60 * @rxq: Buffer inbound queue 61 * @waitqueue: The waitqueue for port fops (read/write/poll) 62 * @data_lock: Port specific data access serialization 63 * @at_data: AT port specific data 64 */ 65 struct wwan_port { 66 enum wwan_port_type type; 67 unsigned int start_count; 68 unsigned long flags; 69 const struct wwan_port_ops *ops; 70 struct mutex ops_lock; /* Serialize ops + protect against removal */ 71 struct device dev; 72 struct sk_buff_head rxq; 73 wait_queue_head_t waitqueue; 74 struct mutex data_lock; /* Port specific data access serialization */ 75 union { 76 struct { 77 struct ktermios termios; 78 int mdmbits; 79 } at_data; 80 }; 81 }; 82 83 static ssize_t index_show(struct device *dev, struct device_attribute *attr, char *buf) 84 { 85 struct wwan_device *wwan = to_wwan_dev(dev); 86 87 return sprintf(buf, "%d\n", wwan->id); 88 } 89 static DEVICE_ATTR_RO(index); 90 91 static struct attribute *wwan_dev_attrs[] = { 92 &dev_attr_index.attr, 93 NULL, 94 }; 95 ATTRIBUTE_GROUPS(wwan_dev); 96 97 static void wwan_dev_destroy(struct device *dev) 98 { 99 struct wwan_device *wwandev = to_wwan_dev(dev); 100 101 ida_free(&wwan_dev_ids, wwandev->id); 102 kfree(wwandev); 103 } 104 105 static const struct device_type wwan_dev_type = { 106 .name = "wwan_dev", 107 .release = wwan_dev_destroy, 108 .groups = wwan_dev_groups, 109 }; 110 111 static int wwan_dev_parent_match(struct device *dev, const void *parent) 112 { 113 return (dev->type == &wwan_dev_type && 114 (dev->parent == parent || dev == parent)); 115 } 116 117 static struct wwan_device *wwan_dev_get_by_parent(struct device *parent) 118 { 119 struct device *dev; 120 121 dev = class_find_device(wwan_class, NULL, parent, wwan_dev_parent_match); 122 if (!dev) 123 return ERR_PTR(-ENODEV); 124 125 return to_wwan_dev(dev); 126 } 127 128 static int wwan_dev_name_match(struct device *dev, const void *name) 129 { 130 return dev->type == &wwan_dev_type && 131 strcmp(dev_name(dev), name) == 0; 132 } 133 134 static struct wwan_device *wwan_dev_get_by_name(const char *name) 135 { 136 struct device *dev; 137 138 dev = class_find_device(wwan_class, NULL, name, wwan_dev_name_match); 139 if (!dev) 140 return ERR_PTR(-ENODEV); 141 142 return to_wwan_dev(dev); 143 } 144 145 /* This function allocates and registers a new WWAN device OR if a WWAN device 146 * already exist for the given parent, it gets a reference and return it. 147 * This function is not exported (for now), it is called indirectly via 148 * wwan_create_port(). 149 */ 150 static struct wwan_device *wwan_create_dev(struct device *parent) 151 { 152 struct wwan_device *wwandev; 153 int err, id; 154 155 /* The 'find-alloc-register' operation must be protected against 156 * concurrent execution, a WWAN device is possibly shared between 157 * multiple callers or concurrently unregistered from wwan_remove_dev(). 158 */ 159 mutex_lock(&wwan_register_lock); 160 161 /* If wwandev already exists, return it */ 162 wwandev = wwan_dev_get_by_parent(parent); 163 if (!IS_ERR(wwandev)) 164 goto done_unlock; 165 166 id = ida_alloc(&wwan_dev_ids, GFP_KERNEL); 167 if (id < 0) 168 goto done_unlock; 169 170 wwandev = kzalloc(sizeof(*wwandev), GFP_KERNEL); 171 if (!wwandev) { 172 ida_free(&wwan_dev_ids, id); 173 goto done_unlock; 174 } 175 176 wwandev->dev.parent = parent; 177 wwandev->dev.class = wwan_class; 178 wwandev->dev.type = &wwan_dev_type; 179 wwandev->id = id; 180 dev_set_name(&wwandev->dev, "wwan%d", wwandev->id); 181 182 err = device_register(&wwandev->dev); 183 if (err) { 184 put_device(&wwandev->dev); 185 wwandev = NULL; 186 } 187 188 done_unlock: 189 mutex_unlock(&wwan_register_lock); 190 191 return wwandev; 192 } 193 194 static int is_wwan_child(struct device *dev, void *data) 195 { 196 return dev->class == wwan_class; 197 } 198 199 static void wwan_remove_dev(struct wwan_device *wwandev) 200 { 201 int ret; 202 203 /* Prevent concurrent picking from wwan_create_dev */ 204 mutex_lock(&wwan_register_lock); 205 206 /* WWAN device is created and registered (get+add) along with its first 207 * child port, and subsequent port registrations only grab a reference 208 * (get). The WWAN device must then be unregistered (del+put) along with 209 * its last port, and reference simply dropped (put) otherwise. In the 210 * same fashion, we must not unregister it when the ops are still there. 211 */ 212 if (wwandev->ops) 213 ret = 1; 214 else 215 ret = device_for_each_child(&wwandev->dev, NULL, is_wwan_child); 216 217 if (!ret) 218 device_unregister(&wwandev->dev); 219 else 220 put_device(&wwandev->dev); 221 222 mutex_unlock(&wwan_register_lock); 223 } 224 225 /* ------- WWAN port management ------- */ 226 227 static const struct { 228 const char * const name; /* Port type name */ 229 const char * const devsuf; /* Port devce name suffix */ 230 } wwan_port_types[WWAN_PORT_MAX + 1] = { 231 [WWAN_PORT_AT] = { 232 .name = "AT", 233 .devsuf = "at", 234 }, 235 [WWAN_PORT_MBIM] = { 236 .name = "MBIM", 237 .devsuf = "mbim", 238 }, 239 [WWAN_PORT_QMI] = { 240 .name = "QMI", 241 .devsuf = "qmi", 242 }, 243 [WWAN_PORT_QCDM] = { 244 .name = "QCDM", 245 .devsuf = "qcdm", 246 }, 247 [WWAN_PORT_FIREHOSE] = { 248 .name = "FIREHOSE", 249 .devsuf = "firehose", 250 }, 251 }; 252 253 static ssize_t type_show(struct device *dev, struct device_attribute *attr, 254 char *buf) 255 { 256 struct wwan_port *port = to_wwan_port(dev); 257 258 return sprintf(buf, "%s\n", wwan_port_types[port->type].name); 259 } 260 static DEVICE_ATTR_RO(type); 261 262 static struct attribute *wwan_port_attrs[] = { 263 &dev_attr_type.attr, 264 NULL, 265 }; 266 ATTRIBUTE_GROUPS(wwan_port); 267 268 static void wwan_port_destroy(struct device *dev) 269 { 270 struct wwan_port *port = to_wwan_port(dev); 271 272 ida_free(&minors, MINOR(port->dev.devt)); 273 mutex_destroy(&port->data_lock); 274 mutex_destroy(&port->ops_lock); 275 kfree(port); 276 } 277 278 static const struct device_type wwan_port_dev_type = { 279 .name = "wwan_port", 280 .release = wwan_port_destroy, 281 .groups = wwan_port_groups, 282 }; 283 284 static int wwan_port_minor_match(struct device *dev, const void *minor) 285 { 286 return (dev->type == &wwan_port_dev_type && 287 MINOR(dev->devt) == *(unsigned int *)minor); 288 } 289 290 static struct wwan_port *wwan_port_get_by_minor(unsigned int minor) 291 { 292 struct device *dev; 293 294 dev = class_find_device(wwan_class, NULL, &minor, wwan_port_minor_match); 295 if (!dev) 296 return ERR_PTR(-ENODEV); 297 298 return to_wwan_port(dev); 299 } 300 301 /* Allocate and set unique name based on passed format 302 * 303 * Name allocation approach is highly inspired by the __dev_alloc_name() 304 * function. 305 * 306 * To avoid names collision, the caller must prevent the new port device 307 * registration as well as concurrent invocation of this function. 308 */ 309 static int __wwan_port_dev_assign_name(struct wwan_port *port, const char *fmt) 310 { 311 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent); 312 const unsigned int max_ports = PAGE_SIZE * 8; 313 struct class_dev_iter iter; 314 unsigned long *idmap; 315 struct device *dev; 316 char buf[0x20]; 317 int id; 318 319 idmap = (unsigned long *)get_zeroed_page(GFP_KERNEL); 320 if (!idmap) 321 return -ENOMEM; 322 323 /* Collect ids of same name format ports */ 324 class_dev_iter_init(&iter, wwan_class, NULL, &wwan_port_dev_type); 325 while ((dev = class_dev_iter_next(&iter))) { 326 if (dev->parent != &wwandev->dev) 327 continue; 328 if (sscanf(dev_name(dev), fmt, &id) != 1) 329 continue; 330 if (id < 0 || id >= max_ports) 331 continue; 332 set_bit(id, idmap); 333 } 334 class_dev_iter_exit(&iter); 335 336 /* Allocate unique id */ 337 id = find_first_zero_bit(idmap, max_ports); 338 free_page((unsigned long)idmap); 339 340 snprintf(buf, sizeof(buf), fmt, id); /* Name generation */ 341 342 dev = device_find_child_by_name(&wwandev->dev, buf); 343 if (dev) { 344 put_device(dev); 345 return -ENFILE; 346 } 347 348 return dev_set_name(&port->dev, buf); 349 } 350 351 struct wwan_port *wwan_create_port(struct device *parent, 352 enum wwan_port_type type, 353 const struct wwan_port_ops *ops, 354 void *drvdata) 355 { 356 struct wwan_device *wwandev; 357 struct wwan_port *port; 358 int minor, err = -ENOMEM; 359 char namefmt[0x20]; 360 361 if (type > WWAN_PORT_MAX || !ops) 362 return ERR_PTR(-EINVAL); 363 364 /* A port is always a child of a WWAN device, retrieve (allocate or 365 * pick) the WWAN device based on the provided parent device. 366 */ 367 wwandev = wwan_create_dev(parent); 368 if (IS_ERR(wwandev)) 369 return ERR_CAST(wwandev); 370 371 /* A port is exposed as character device, get a minor */ 372 minor = ida_alloc_range(&minors, 0, WWAN_MAX_MINORS - 1, GFP_KERNEL); 373 if (minor < 0) 374 goto error_wwandev_remove; 375 376 port = kzalloc(sizeof(*port), GFP_KERNEL); 377 if (!port) { 378 ida_free(&minors, minor); 379 goto error_wwandev_remove; 380 } 381 382 port->type = type; 383 port->ops = ops; 384 mutex_init(&port->ops_lock); 385 skb_queue_head_init(&port->rxq); 386 init_waitqueue_head(&port->waitqueue); 387 mutex_init(&port->data_lock); 388 389 port->dev.parent = &wwandev->dev; 390 port->dev.class = wwan_class; 391 port->dev.type = &wwan_port_dev_type; 392 port->dev.devt = MKDEV(wwan_major, minor); 393 dev_set_drvdata(&port->dev, drvdata); 394 395 /* allocate unique name based on wwan device id, port type and number */ 396 snprintf(namefmt, sizeof(namefmt), "wwan%u%s%%d", wwandev->id, 397 wwan_port_types[port->type].devsuf); 398 399 /* Serialize ports registration */ 400 mutex_lock(&wwan_register_lock); 401 402 __wwan_port_dev_assign_name(port, namefmt); 403 err = device_register(&port->dev); 404 405 mutex_unlock(&wwan_register_lock); 406 407 if (err) 408 goto error_put_device; 409 410 return port; 411 412 error_put_device: 413 put_device(&port->dev); 414 error_wwandev_remove: 415 wwan_remove_dev(wwandev); 416 417 return ERR_PTR(err); 418 } 419 EXPORT_SYMBOL_GPL(wwan_create_port); 420 421 void wwan_remove_port(struct wwan_port *port) 422 { 423 struct wwan_device *wwandev = to_wwan_dev(port->dev.parent); 424 425 mutex_lock(&port->ops_lock); 426 if (port->start_count) 427 port->ops->stop(port); 428 port->ops = NULL; /* Prevent any new port operations (e.g. from fops) */ 429 mutex_unlock(&port->ops_lock); 430 431 wake_up_interruptible(&port->waitqueue); 432 433 skb_queue_purge(&port->rxq); 434 dev_set_drvdata(&port->dev, NULL); 435 device_unregister(&port->dev); 436 437 /* Release related wwan device */ 438 wwan_remove_dev(wwandev); 439 } 440 EXPORT_SYMBOL_GPL(wwan_remove_port); 441 442 void wwan_port_rx(struct wwan_port *port, struct sk_buff *skb) 443 { 444 skb_queue_tail(&port->rxq, skb); 445 wake_up_interruptible(&port->waitqueue); 446 } 447 EXPORT_SYMBOL_GPL(wwan_port_rx); 448 449 void wwan_port_txon(struct wwan_port *port) 450 { 451 clear_bit(WWAN_PORT_TX_OFF, &port->flags); 452 wake_up_interruptible(&port->waitqueue); 453 } 454 EXPORT_SYMBOL_GPL(wwan_port_txon); 455 456 void wwan_port_txoff(struct wwan_port *port) 457 { 458 set_bit(WWAN_PORT_TX_OFF, &port->flags); 459 } 460 EXPORT_SYMBOL_GPL(wwan_port_txoff); 461 462 void *wwan_port_get_drvdata(struct wwan_port *port) 463 { 464 return dev_get_drvdata(&port->dev); 465 } 466 EXPORT_SYMBOL_GPL(wwan_port_get_drvdata); 467 468 static int wwan_port_op_start(struct wwan_port *port) 469 { 470 int ret = 0; 471 472 mutex_lock(&port->ops_lock); 473 if (!port->ops) { /* Port got unplugged */ 474 ret = -ENODEV; 475 goto out_unlock; 476 } 477 478 /* If port is already started, don't start again */ 479 if (!port->start_count) 480 ret = port->ops->start(port); 481 482 if (!ret) 483 port->start_count++; 484 485 out_unlock: 486 mutex_unlock(&port->ops_lock); 487 488 return ret; 489 } 490 491 static void wwan_port_op_stop(struct wwan_port *port) 492 { 493 mutex_lock(&port->ops_lock); 494 port->start_count--; 495 if (!port->start_count) { 496 if (port->ops) 497 port->ops->stop(port); 498 skb_queue_purge(&port->rxq); 499 } 500 mutex_unlock(&port->ops_lock); 501 } 502 503 static int wwan_port_op_tx(struct wwan_port *port, struct sk_buff *skb) 504 { 505 int ret; 506 507 mutex_lock(&port->ops_lock); 508 if (!port->ops) { /* Port got unplugged */ 509 ret = -ENODEV; 510 goto out_unlock; 511 } 512 513 ret = port->ops->tx(port, skb); 514 515 out_unlock: 516 mutex_unlock(&port->ops_lock); 517 518 return ret; 519 } 520 521 static bool is_read_blocked(struct wwan_port *port) 522 { 523 return skb_queue_empty(&port->rxq) && port->ops; 524 } 525 526 static bool is_write_blocked(struct wwan_port *port) 527 { 528 return test_bit(WWAN_PORT_TX_OFF, &port->flags) && port->ops; 529 } 530 531 static int wwan_wait_rx(struct wwan_port *port, bool nonblock) 532 { 533 if (!is_read_blocked(port)) 534 return 0; 535 536 if (nonblock) 537 return -EAGAIN; 538 539 if (wait_event_interruptible(port->waitqueue, !is_read_blocked(port))) 540 return -ERESTARTSYS; 541 542 return 0; 543 } 544 545 static int wwan_wait_tx(struct wwan_port *port, bool nonblock) 546 { 547 if (!is_write_blocked(port)) 548 return 0; 549 550 if (nonblock) 551 return -EAGAIN; 552 553 if (wait_event_interruptible(port->waitqueue, !is_write_blocked(port))) 554 return -ERESTARTSYS; 555 556 return 0; 557 } 558 559 static int wwan_port_fops_open(struct inode *inode, struct file *file) 560 { 561 struct wwan_port *port; 562 int err = 0; 563 564 port = wwan_port_get_by_minor(iminor(inode)); 565 if (IS_ERR(port)) 566 return PTR_ERR(port); 567 568 file->private_data = port; 569 stream_open(inode, file); 570 571 err = wwan_port_op_start(port); 572 if (err) 573 put_device(&port->dev); 574 575 return err; 576 } 577 578 static int wwan_port_fops_release(struct inode *inode, struct file *filp) 579 { 580 struct wwan_port *port = filp->private_data; 581 582 wwan_port_op_stop(port); 583 put_device(&port->dev); 584 585 return 0; 586 } 587 588 static ssize_t wwan_port_fops_read(struct file *filp, char __user *buf, 589 size_t count, loff_t *ppos) 590 { 591 struct wwan_port *port = filp->private_data; 592 struct sk_buff *skb; 593 size_t copied; 594 int ret; 595 596 ret = wwan_wait_rx(port, !!(filp->f_flags & O_NONBLOCK)); 597 if (ret) 598 return ret; 599 600 skb = skb_dequeue(&port->rxq); 601 if (!skb) 602 return -EIO; 603 604 copied = min_t(size_t, count, skb->len); 605 if (copy_to_user(buf, skb->data, copied)) { 606 kfree_skb(skb); 607 return -EFAULT; 608 } 609 skb_pull(skb, copied); 610 611 /* skb is not fully consumed, keep it in the queue */ 612 if (skb->len) 613 skb_queue_head(&port->rxq, skb); 614 else 615 consume_skb(skb); 616 617 return copied; 618 } 619 620 static ssize_t wwan_port_fops_write(struct file *filp, const char __user *buf, 621 size_t count, loff_t *offp) 622 { 623 struct wwan_port *port = filp->private_data; 624 struct sk_buff *skb; 625 int ret; 626 627 ret = wwan_wait_tx(port, !!(filp->f_flags & O_NONBLOCK)); 628 if (ret) 629 return ret; 630 631 skb = alloc_skb(count, GFP_KERNEL); 632 if (!skb) 633 return -ENOMEM; 634 635 if (copy_from_user(skb_put(skb, count), buf, count)) { 636 kfree_skb(skb); 637 return -EFAULT; 638 } 639 640 ret = wwan_port_op_tx(port, skb); 641 if (ret) { 642 kfree_skb(skb); 643 return ret; 644 } 645 646 return count; 647 } 648 649 static __poll_t wwan_port_fops_poll(struct file *filp, poll_table *wait) 650 { 651 struct wwan_port *port = filp->private_data; 652 __poll_t mask = 0; 653 654 poll_wait(filp, &port->waitqueue, wait); 655 656 if (!is_write_blocked(port)) 657 mask |= EPOLLOUT | EPOLLWRNORM; 658 if (!is_read_blocked(port)) 659 mask |= EPOLLIN | EPOLLRDNORM; 660 if (!port->ops) 661 mask |= EPOLLHUP | EPOLLERR; 662 663 return mask; 664 } 665 666 /* Implements minimalistic stub terminal IOCTLs support */ 667 static long wwan_port_fops_at_ioctl(struct wwan_port *port, unsigned int cmd, 668 unsigned long arg) 669 { 670 int ret = 0; 671 672 mutex_lock(&port->data_lock); 673 674 switch (cmd) { 675 case TCFLSH: 676 break; 677 678 case TCGETS: 679 if (copy_to_user((void __user *)arg, &port->at_data.termios, 680 sizeof(struct termios))) 681 ret = -EFAULT; 682 break; 683 684 case TCSETS: 685 case TCSETSW: 686 case TCSETSF: 687 if (copy_from_user(&port->at_data.termios, (void __user *)arg, 688 sizeof(struct termios))) 689 ret = -EFAULT; 690 break; 691 692 #ifdef TCGETS2 693 case TCGETS2: 694 if (copy_to_user((void __user *)arg, &port->at_data.termios, 695 sizeof(struct termios2))) 696 ret = -EFAULT; 697 break; 698 699 case TCSETS2: 700 case TCSETSW2: 701 case TCSETSF2: 702 if (copy_from_user(&port->at_data.termios, (void __user *)arg, 703 sizeof(struct termios2))) 704 ret = -EFAULT; 705 break; 706 #endif 707 708 case TIOCMGET: 709 ret = put_user(port->at_data.mdmbits, (int __user *)arg); 710 break; 711 712 case TIOCMSET: 713 case TIOCMBIC: 714 case TIOCMBIS: { 715 int mdmbits; 716 717 if (copy_from_user(&mdmbits, (int __user *)arg, sizeof(int))) { 718 ret = -EFAULT; 719 break; 720 } 721 if (cmd == TIOCMBIC) 722 port->at_data.mdmbits &= ~mdmbits; 723 else if (cmd == TIOCMBIS) 724 port->at_data.mdmbits |= mdmbits; 725 else 726 port->at_data.mdmbits = mdmbits; 727 break; 728 } 729 730 default: 731 ret = -ENOIOCTLCMD; 732 } 733 734 mutex_unlock(&port->data_lock); 735 736 return ret; 737 } 738 739 static long wwan_port_fops_ioctl(struct file *filp, unsigned int cmd, 740 unsigned long arg) 741 { 742 struct wwan_port *port = filp->private_data; 743 int res; 744 745 if (port->type == WWAN_PORT_AT) { /* AT port specific IOCTLs */ 746 res = wwan_port_fops_at_ioctl(port, cmd, arg); 747 if (res != -ENOIOCTLCMD) 748 return res; 749 } 750 751 switch (cmd) { 752 case TIOCINQ: { /* aka SIOCINQ aka FIONREAD */ 753 unsigned long flags; 754 struct sk_buff *skb; 755 int amount = 0; 756 757 spin_lock_irqsave(&port->rxq.lock, flags); 758 skb_queue_walk(&port->rxq, skb) 759 amount += skb->len; 760 spin_unlock_irqrestore(&port->rxq.lock, flags); 761 762 return put_user(amount, (int __user *)arg); 763 } 764 765 default: 766 return -ENOIOCTLCMD; 767 } 768 } 769 770 static const struct file_operations wwan_port_fops = { 771 .owner = THIS_MODULE, 772 .open = wwan_port_fops_open, 773 .release = wwan_port_fops_release, 774 .read = wwan_port_fops_read, 775 .write = wwan_port_fops_write, 776 .poll = wwan_port_fops_poll, 777 .unlocked_ioctl = wwan_port_fops_ioctl, 778 #ifdef CONFIG_COMPAT 779 .compat_ioctl = compat_ptr_ioctl, 780 #endif 781 .llseek = noop_llseek, 782 }; 783 784 /** 785 * wwan_register_ops - register WWAN device ops 786 * @parent: Device to use as parent and shared by all WWAN ports and 787 * created netdevs 788 * @ops: operations to register 789 * @ctxt: context to pass to operations 790 * 791 * Returns: 0 on success, a negative error code on failure 792 */ 793 int wwan_register_ops(struct device *parent, const struct wwan_ops *ops, 794 void *ctxt) 795 { 796 struct wwan_device *wwandev; 797 798 if (WARN_ON(!parent || !ops)) 799 return -EINVAL; 800 801 wwandev = wwan_create_dev(parent); 802 if (!wwandev) 803 return -ENOMEM; 804 805 if (WARN_ON(wwandev->ops)) { 806 wwan_remove_dev(wwandev); 807 return -EBUSY; 808 } 809 810 if (!try_module_get(ops->owner)) { 811 wwan_remove_dev(wwandev); 812 return -ENODEV; 813 } 814 815 wwandev->ops = ops; 816 wwandev->ops_ctxt = ctxt; 817 818 return 0; 819 } 820 EXPORT_SYMBOL_GPL(wwan_register_ops); 821 822 /** 823 * wwan_unregister_ops - remove WWAN device ops 824 * @parent: Device to use as parent and shared by all WWAN ports and 825 * created netdevs 826 */ 827 void wwan_unregister_ops(struct device *parent) 828 { 829 struct wwan_device *wwandev = wwan_dev_get_by_parent(parent); 830 bool has_ops; 831 832 if (WARN_ON(IS_ERR(wwandev))) 833 return; 834 835 has_ops = wwandev->ops; 836 837 /* put the reference obtained by wwan_dev_get_by_parent(), 838 * we should still have one (that the owner is giving back 839 * now) due to the ops being assigned, check that below 840 * and return if not. 841 */ 842 put_device(&wwandev->dev); 843 844 if (WARN_ON(!has_ops)) 845 return; 846 847 module_put(wwandev->ops->owner); 848 849 wwandev->ops = NULL; 850 wwandev->ops_ctxt = NULL; 851 wwan_remove_dev(wwandev); 852 } 853 EXPORT_SYMBOL_GPL(wwan_unregister_ops); 854 855 static int wwan_rtnl_validate(struct nlattr *tb[], struct nlattr *data[], 856 struct netlink_ext_ack *extack) 857 { 858 if (!data) 859 return -EINVAL; 860 861 if (!tb[IFLA_PARENT_DEV_NAME]) 862 return -EINVAL; 863 864 if (!data[IFLA_WWAN_LINK_ID]) 865 return -EINVAL; 866 867 return 0; 868 } 869 870 static struct device_type wwan_type = { .name = "wwan" }; 871 872 static struct net_device *wwan_rtnl_alloc(struct nlattr *tb[], 873 const char *ifname, 874 unsigned char name_assign_type, 875 unsigned int num_tx_queues, 876 unsigned int num_rx_queues) 877 { 878 const char *devname = nla_data(tb[IFLA_PARENT_DEV_NAME]); 879 struct wwan_device *wwandev = wwan_dev_get_by_name(devname); 880 struct net_device *dev; 881 882 if (IS_ERR(wwandev)) 883 return ERR_CAST(wwandev); 884 885 /* only supported if ops were registered (not just ports) */ 886 if (!wwandev->ops) { 887 dev = ERR_PTR(-EOPNOTSUPP); 888 goto out; 889 } 890 891 dev = alloc_netdev_mqs(wwandev->ops->priv_size, ifname, name_assign_type, 892 wwandev->ops->setup, num_tx_queues, num_rx_queues); 893 894 if (dev) { 895 SET_NETDEV_DEV(dev, &wwandev->dev); 896 SET_NETDEV_DEVTYPE(dev, &wwan_type); 897 } 898 899 out: 900 /* release the reference */ 901 put_device(&wwandev->dev); 902 return dev; 903 } 904 905 static int wwan_rtnl_newlink(struct net *src_net, struct net_device *dev, 906 struct nlattr *tb[], struct nlattr *data[], 907 struct netlink_ext_ack *extack) 908 { 909 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent); 910 u32 link_id = nla_get_u32(data[IFLA_WWAN_LINK_ID]); 911 int ret; 912 913 if (IS_ERR(wwandev)) 914 return PTR_ERR(wwandev); 915 916 /* shouldn't have a netdev (left) with us as parent so WARN */ 917 if (WARN_ON(!wwandev->ops)) { 918 ret = -EOPNOTSUPP; 919 goto out; 920 } 921 922 if (wwandev->ops->newlink) 923 ret = wwandev->ops->newlink(wwandev->ops_ctxt, dev, 924 link_id, extack); 925 else 926 ret = register_netdevice(dev); 927 928 out: 929 /* release the reference */ 930 put_device(&wwandev->dev); 931 return ret; 932 } 933 934 static void wwan_rtnl_dellink(struct net_device *dev, struct list_head *head) 935 { 936 struct wwan_device *wwandev = wwan_dev_get_by_parent(dev->dev.parent); 937 938 if (IS_ERR(wwandev)) 939 return; 940 941 /* shouldn't have a netdev (left) with us as parent so WARN */ 942 if (WARN_ON(!wwandev->ops)) 943 goto out; 944 945 if (wwandev->ops->dellink) 946 wwandev->ops->dellink(wwandev->ops_ctxt, dev, head); 947 else 948 unregister_netdevice(dev); 949 950 out: 951 /* release the reference */ 952 put_device(&wwandev->dev); 953 } 954 955 static const struct nla_policy wwan_rtnl_policy[IFLA_WWAN_MAX + 1] = { 956 [IFLA_WWAN_LINK_ID] = { .type = NLA_U32 }, 957 }; 958 959 static struct rtnl_link_ops wwan_rtnl_link_ops __read_mostly = { 960 .kind = "wwan", 961 .maxtype = __IFLA_WWAN_MAX, 962 .alloc = wwan_rtnl_alloc, 963 .validate = wwan_rtnl_validate, 964 .newlink = wwan_rtnl_newlink, 965 .dellink = wwan_rtnl_dellink, 966 .policy = wwan_rtnl_policy, 967 }; 968 969 static int __init wwan_init(void) 970 { 971 int err; 972 973 err = rtnl_link_register(&wwan_rtnl_link_ops); 974 if (err) 975 return err; 976 977 wwan_class = class_create(THIS_MODULE, "wwan"); 978 if (IS_ERR(wwan_class)) { 979 err = PTR_ERR(wwan_class); 980 goto unregister; 981 } 982 983 /* chrdev used for wwan ports */ 984 wwan_major = __register_chrdev(0, 0, WWAN_MAX_MINORS, "wwan_port", 985 &wwan_port_fops); 986 if (wwan_major < 0) { 987 err = wwan_major; 988 goto destroy; 989 } 990 991 return 0; 992 993 destroy: 994 class_destroy(wwan_class); 995 unregister: 996 rtnl_link_unregister(&wwan_rtnl_link_ops); 997 return err; 998 } 999 1000 static void __exit wwan_exit(void) 1001 { 1002 __unregister_chrdev(wwan_major, 0, WWAN_MAX_MINORS, "wwan_port"); 1003 rtnl_link_unregister(&wwan_rtnl_link_ops); 1004 class_destroy(wwan_class); 1005 } 1006 1007 module_init(wwan_init); 1008 module_exit(wwan_exit); 1009 1010 MODULE_AUTHOR("Loic Poulain <loic.poulain@linaro.org>"); 1011 MODULE_DESCRIPTION("WWAN core"); 1012 MODULE_LICENSE("GPL v2"); 1013