0001
0002
0003
0004
0005
0006 #include <linux/types.h>
0007 #include <linux/errno.h>
0008 #include <linux/tty.h>
0009 #include <linux/tty_driver.h>
0010 #include <linux/tty_flip.h>
0011 #include <linux/serial.h>
0012 #include <linux/timer.h>
0013 #include <linux/string.h>
0014 #include <linux/slab.h>
0015 #include <linux/sched/signal.h>
0016 #include <linux/wait.h>
0017 #include <linux/bitops.h>
0018 #include <linux/delay.h>
0019 #include <linux/module.h>
0020 #include <linux/serdev.h>
0021 #include "tty.h"
0022
0023 static int tty_port_default_receive_buf(struct tty_port *port,
0024 const unsigned char *p,
0025 const unsigned char *f, size_t count)
0026 {
0027 int ret;
0028 struct tty_struct *tty;
0029 struct tty_ldisc *disc;
0030
0031 tty = READ_ONCE(port->itty);
0032 if (!tty)
0033 return 0;
0034
0035 disc = tty_ldisc_ref(tty);
0036 if (!disc)
0037 return 0;
0038
0039 ret = tty_ldisc_receive_buf(disc, p, (char *)f, count);
0040
0041 tty_ldisc_deref(disc);
0042
0043 return ret;
0044 }
0045
0046 static void tty_port_default_lookahead_buf(struct tty_port *port, const unsigned char *p,
0047 const unsigned char *f, unsigned int count)
0048 {
0049 struct tty_struct *tty;
0050 struct tty_ldisc *disc;
0051
0052 tty = READ_ONCE(port->itty);
0053 if (!tty)
0054 return;
0055
0056 disc = tty_ldisc_ref(tty);
0057 if (!disc)
0058 return;
0059
0060 if (disc->ops->lookahead_buf)
0061 disc->ops->lookahead_buf(disc->tty, p, f, count);
0062
0063 tty_ldisc_deref(disc);
0064 }
0065
0066 static void tty_port_default_wakeup(struct tty_port *port)
0067 {
0068 struct tty_struct *tty = tty_port_tty_get(port);
0069
0070 if (tty) {
0071 tty_wakeup(tty);
0072 tty_kref_put(tty);
0073 }
0074 }
0075
0076 const struct tty_port_client_operations tty_port_default_client_ops = {
0077 .receive_buf = tty_port_default_receive_buf,
0078 .lookahead_buf = tty_port_default_lookahead_buf,
0079 .write_wakeup = tty_port_default_wakeup,
0080 };
0081 EXPORT_SYMBOL_GPL(tty_port_default_client_ops);
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092 void tty_port_init(struct tty_port *port)
0093 {
0094 memset(port, 0, sizeof(*port));
0095 tty_buffer_init(port);
0096 init_waitqueue_head(&port->open_wait);
0097 init_waitqueue_head(&port->delta_msr_wait);
0098 mutex_init(&port->mutex);
0099 mutex_init(&port->buf_mutex);
0100 spin_lock_init(&port->lock);
0101 port->close_delay = (50 * HZ) / 100;
0102 port->closing_wait = (3000 * HZ) / 100;
0103 port->client_ops = &tty_port_default_client_ops;
0104 kref_init(&port->kref);
0105 }
0106 EXPORT_SYMBOL(tty_port_init);
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119 void tty_port_link_device(struct tty_port *port,
0120 struct tty_driver *driver, unsigned index)
0121 {
0122 if (WARN_ON(index >= driver->num))
0123 return;
0124 driver->ports[index] = port;
0125 }
0126 EXPORT_SYMBOL_GPL(tty_port_link_device);
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139 struct device *tty_port_register_device(struct tty_port *port,
0140 struct tty_driver *driver, unsigned index,
0141 struct device *device)
0142 {
0143 return tty_port_register_device_attr(port, driver, index, device, NULL, NULL);
0144 }
0145 EXPORT_SYMBOL_GPL(tty_port_register_device);
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160 struct device *tty_port_register_device_attr(struct tty_port *port,
0161 struct tty_driver *driver, unsigned index,
0162 struct device *device, void *drvdata,
0163 const struct attribute_group **attr_grp)
0164 {
0165 tty_port_link_device(port, driver, index);
0166 return tty_register_device_attr(driver, index, device, drvdata,
0167 attr_grp);
0168 }
0169 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183 struct device *tty_port_register_device_attr_serdev(struct tty_port *port,
0184 struct tty_driver *driver, unsigned index,
0185 struct device *device, void *drvdata,
0186 const struct attribute_group **attr_grp)
0187 {
0188 struct device *dev;
0189
0190 tty_port_link_device(port, driver, index);
0191
0192 dev = serdev_tty_port_register(port, device, driver, index);
0193 if (PTR_ERR(dev) != -ENODEV) {
0194
0195 return dev;
0196 }
0197
0198 return tty_register_device_attr(driver, index, device, drvdata,
0199 attr_grp);
0200 }
0201 EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev);
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213 struct device *tty_port_register_device_serdev(struct tty_port *port,
0214 struct tty_driver *driver, unsigned index,
0215 struct device *device)
0216 {
0217 return tty_port_register_device_attr_serdev(port, driver, index,
0218 device, NULL, NULL);
0219 }
0220 EXPORT_SYMBOL_GPL(tty_port_register_device_serdev);
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232 void tty_port_unregister_device(struct tty_port *port,
0233 struct tty_driver *driver, unsigned index)
0234 {
0235 int ret;
0236
0237 ret = serdev_tty_port_unregister(port);
0238 if (ret == 0)
0239 return;
0240
0241 tty_unregister_device(driver, index);
0242 }
0243 EXPORT_SYMBOL_GPL(tty_port_unregister_device);
0244
0245 int tty_port_alloc_xmit_buf(struct tty_port *port)
0246 {
0247
0248 mutex_lock(&port->buf_mutex);
0249 if (port->xmit_buf == NULL) {
0250 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
0251 if (port->xmit_buf)
0252 kfifo_init(&port->xmit_fifo, port->xmit_buf, PAGE_SIZE);
0253 }
0254 mutex_unlock(&port->buf_mutex);
0255 if (port->xmit_buf == NULL)
0256 return -ENOMEM;
0257 return 0;
0258 }
0259 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
0260
0261 void tty_port_free_xmit_buf(struct tty_port *port)
0262 {
0263 mutex_lock(&port->buf_mutex);
0264 free_page((unsigned long)port->xmit_buf);
0265 port->xmit_buf = NULL;
0266 INIT_KFIFO(port->xmit_fifo);
0267 mutex_unlock(&port->buf_mutex);
0268 }
0269 EXPORT_SYMBOL(tty_port_free_xmit_buf);
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 void tty_port_destroy(struct tty_port *port)
0280 {
0281 tty_buffer_cancel_work(port);
0282 tty_buffer_free_all(port);
0283 }
0284 EXPORT_SYMBOL(tty_port_destroy);
0285
0286 static void tty_port_destructor(struct kref *kref)
0287 {
0288 struct tty_port *port = container_of(kref, struct tty_port, kref);
0289
0290
0291 if (WARN_ON(port->itty))
0292 return;
0293 free_page((unsigned long)port->xmit_buf);
0294 tty_port_destroy(port);
0295 if (port->ops && port->ops->destruct)
0296 port->ops->destruct(port);
0297 else
0298 kfree(port);
0299 }
0300
0301
0302
0303
0304
0305
0306
0307
0308 void tty_port_put(struct tty_port *port)
0309 {
0310 if (port)
0311 kref_put(&port->kref, tty_port_destructor);
0312 }
0313 EXPORT_SYMBOL(tty_port_put);
0314
0315
0316
0317
0318
0319
0320
0321
0322 struct tty_struct *tty_port_tty_get(struct tty_port *port)
0323 {
0324 unsigned long flags;
0325 struct tty_struct *tty;
0326
0327 spin_lock_irqsave(&port->lock, flags);
0328 tty = tty_kref_get(port->tty);
0329 spin_unlock_irqrestore(&port->lock, flags);
0330 return tty;
0331 }
0332 EXPORT_SYMBOL(tty_port_tty_get);
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
0343 {
0344 unsigned long flags;
0345
0346 spin_lock_irqsave(&port->lock, flags);
0347 tty_kref_put(port->tty);
0348 port->tty = tty_kref_get(tty);
0349 spin_unlock_irqrestore(&port->lock, flags);
0350 }
0351 EXPORT_SYMBOL(tty_port_tty_set);
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363 static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty)
0364 {
0365 mutex_lock(&port->mutex);
0366 if (port->console)
0367 goto out;
0368
0369 if (tty_port_initialized(port)) {
0370 tty_port_set_initialized(port, 0);
0371
0372
0373
0374
0375 if (tty && C_HUPCL(tty))
0376 tty_port_lower_dtr_rts(port);
0377
0378 if (port->ops->shutdown)
0379 port->ops->shutdown(port);
0380 }
0381 out:
0382 mutex_unlock(&port->mutex);
0383 }
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394 void tty_port_hangup(struct tty_port *port)
0395 {
0396 struct tty_struct *tty;
0397 unsigned long flags;
0398
0399 spin_lock_irqsave(&port->lock, flags);
0400 port->count = 0;
0401 tty = port->tty;
0402 if (tty)
0403 set_bit(TTY_IO_ERROR, &tty->flags);
0404 port->tty = NULL;
0405 spin_unlock_irqrestore(&port->lock, flags);
0406 tty_port_set_active(port, 0);
0407 tty_port_shutdown(port, tty);
0408 tty_kref_put(tty);
0409 wake_up_interruptible(&port->open_wait);
0410 wake_up_interruptible(&port->delta_msr_wait);
0411 }
0412 EXPORT_SYMBOL(tty_port_hangup);
0413
0414
0415
0416
0417
0418
0419 void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
0420 {
0421 struct tty_struct *tty = tty_port_tty_get(port);
0422
0423 if (tty && (!check_clocal || !C_CLOCAL(tty)))
0424 tty_hangup(tty);
0425 tty_kref_put(tty);
0426 }
0427 EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
0428
0429
0430
0431
0432
0433 void tty_port_tty_wakeup(struct tty_port *port)
0434 {
0435 port->client_ops->write_wakeup(port);
0436 }
0437 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447 int tty_port_carrier_raised(struct tty_port *port)
0448 {
0449 if (port->ops->carrier_raised == NULL)
0450 return 1;
0451 return port->ops->carrier_raised(port);
0452 }
0453 EXPORT_SYMBOL(tty_port_carrier_raised);
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463 void tty_port_raise_dtr_rts(struct tty_port *port)
0464 {
0465 if (port->ops->dtr_rts)
0466 port->ops->dtr_rts(port, 1);
0467 }
0468 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
0469
0470
0471
0472
0473
0474
0475
0476
0477
0478 void tty_port_lower_dtr_rts(struct tty_port *port)
0479 {
0480 if (port->ops->dtr_rts)
0481 port->ops->dtr_rts(port, 0);
0482 }
0483 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493
0494
0495
0496
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510 int tty_port_block_til_ready(struct tty_port *port,
0511 struct tty_struct *tty, struct file *filp)
0512 {
0513 int do_clocal = 0, retval;
0514 unsigned long flags;
0515 DEFINE_WAIT(wait);
0516
0517
0518
0519
0520 if (tty_io_error(tty)) {
0521 tty_port_set_active(port, 1);
0522 return 0;
0523 }
0524 if (filp == NULL || (filp->f_flags & O_NONBLOCK)) {
0525
0526 if (C_BAUD(tty))
0527 tty_port_raise_dtr_rts(port);
0528 tty_port_set_active(port, 1);
0529 return 0;
0530 }
0531
0532 if (C_CLOCAL(tty))
0533 do_clocal = 1;
0534
0535
0536
0537
0538
0539
0540 retval = 0;
0541
0542
0543 spin_lock_irqsave(&port->lock, flags);
0544 port->count--;
0545 port->blocked_open++;
0546 spin_unlock_irqrestore(&port->lock, flags);
0547
0548 while (1) {
0549
0550 if (C_BAUD(tty) && tty_port_initialized(port))
0551 tty_port_raise_dtr_rts(port);
0552
0553 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
0554
0555
0556
0557 if (tty_hung_up_p(filp) || !tty_port_initialized(port)) {
0558 if (port->flags & ASYNC_HUP_NOTIFY)
0559 retval = -EAGAIN;
0560 else
0561 retval = -ERESTARTSYS;
0562 break;
0563 }
0564
0565
0566
0567
0568
0569
0570 if (do_clocal || tty_port_carrier_raised(port))
0571 break;
0572 if (signal_pending(current)) {
0573 retval = -ERESTARTSYS;
0574 break;
0575 }
0576 tty_unlock(tty);
0577 schedule();
0578 tty_lock(tty);
0579 }
0580 finish_wait(&port->open_wait, &wait);
0581
0582
0583
0584
0585 spin_lock_irqsave(&port->lock, flags);
0586 if (!tty_hung_up_p(filp))
0587 port->count++;
0588 port->blocked_open--;
0589 spin_unlock_irqrestore(&port->lock, flags);
0590 if (retval == 0)
0591 tty_port_set_active(port, 1);
0592 return retval;
0593 }
0594 EXPORT_SYMBOL(tty_port_block_til_ready);
0595
0596 static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty)
0597 {
0598 unsigned int bps = tty_get_baud_rate(tty);
0599 long timeout;
0600
0601 if (bps > 1200) {
0602 timeout = (HZ * 10 * port->drain_delay) / bps;
0603 timeout = max_t(long, timeout, HZ / 10);
0604 } else {
0605 timeout = 2 * HZ;
0606 }
0607 schedule_timeout_interruptible(timeout);
0608 }
0609
0610
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625 int tty_port_close_start(struct tty_port *port,
0626 struct tty_struct *tty, struct file *filp)
0627 {
0628 unsigned long flags;
0629
0630 if (tty_hung_up_p(filp))
0631 return 0;
0632
0633 spin_lock_irqsave(&port->lock, flags);
0634 if (tty->count == 1 && port->count != 1) {
0635 tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__,
0636 port->count);
0637 port->count = 1;
0638 }
0639 if (--port->count < 0) {
0640 tty_warn(tty, "%s: bad port count (%d)\n", __func__,
0641 port->count);
0642 port->count = 0;
0643 }
0644
0645 if (port->count) {
0646 spin_unlock_irqrestore(&port->lock, flags);
0647 return 0;
0648 }
0649 spin_unlock_irqrestore(&port->lock, flags);
0650
0651 tty->closing = 1;
0652
0653 if (tty_port_initialized(port)) {
0654
0655 if (tty->flow.tco_stopped)
0656 tty_driver_flush_buffer(tty);
0657 if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
0658 tty_wait_until_sent(tty, port->closing_wait);
0659 if (port->drain_delay)
0660 tty_port_drain_delay(port, tty);
0661 }
0662
0663 tty_ldisc_flush(tty);
0664
0665
0666 return 1;
0667 }
0668 EXPORT_SYMBOL(tty_port_close_start);
0669
0670
0671
0672
0673
0674
0675
0676
0677
0678
0679
0680
0681 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
0682 {
0683 unsigned long flags;
0684
0685 tty_ldisc_flush(tty);
0686 tty->closing = 0;
0687
0688 spin_lock_irqsave(&port->lock, flags);
0689
0690 if (port->blocked_open) {
0691 spin_unlock_irqrestore(&port->lock, flags);
0692 if (port->close_delay)
0693 msleep_interruptible(jiffies_to_msecs(port->close_delay));
0694 spin_lock_irqsave(&port->lock, flags);
0695 wake_up_interruptible(&port->open_wait);
0696 }
0697 spin_unlock_irqrestore(&port->lock, flags);
0698 tty_port_set_active(port, 0);
0699 }
0700 EXPORT_SYMBOL(tty_port_close_end);
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
0716 struct file *filp)
0717 {
0718 if (tty_port_close_start(port, tty, filp) == 0)
0719 return;
0720 tty_port_shutdown(port, tty);
0721 if (!port->console)
0722 set_bit(TTY_IO_ERROR, &tty->flags);
0723 tty_port_close_end(port, tty);
0724 tty_port_tty_set(port, NULL);
0725 }
0726 EXPORT_SYMBOL(tty_port_close);
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
0739 struct tty_struct *tty)
0740 {
0741 tty->port = port;
0742 return tty_standard_install(driver, tty);
0743 }
0744 EXPORT_SYMBOL_GPL(tty_port_install);
0745
0746
0747
0748
0749
0750
0751
0752
0753
0754
0755
0756
0757
0758
0759
0760
0761
0762
0763
0764
0765 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
0766 struct file *filp)
0767 {
0768 spin_lock_irq(&port->lock);
0769 ++port->count;
0770 spin_unlock_irq(&port->lock);
0771 tty_port_tty_set(port, tty);
0772
0773
0774
0775
0776
0777
0778
0779 mutex_lock(&port->mutex);
0780
0781 if (!tty_port_initialized(port)) {
0782 clear_bit(TTY_IO_ERROR, &tty->flags);
0783 if (port->ops->activate) {
0784 int retval = port->ops->activate(port, tty);
0785
0786 if (retval) {
0787 mutex_unlock(&port->mutex);
0788 return retval;
0789 }
0790 }
0791 tty_port_set_initialized(port, 1);
0792 }
0793 mutex_unlock(&port->mutex);
0794 return tty_port_block_til_ready(port, tty, filp);
0795 }
0796 EXPORT_SYMBOL(tty_port_open);