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