7e3eaf4eb9fea923972e94f26493a60cbb8e3a12
[firefly-linux-kernel-4.4.55.git] / drivers / tty / tty_port.c
1 /*
2  * Tty port functions
3  */
4
5 #include <linux/types.h>
6 #include <linux/errno.h>
7 #include <linux/tty.h>
8 #include <linux/tty_driver.h>
9 #include <linux/tty_flip.h>
10 #include <linux/serial.h>
11 #include <linux/timer.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/init.h>
16 #include <linux/wait.h>
17 #include <linux/bitops.h>
18 #include <linux/delay.h>
19 #include <linux/module.h>
20
21 void tty_port_init(struct tty_port *port)
22 {
23         memset(port, 0, sizeof(*port));
24         tty_buffer_init(port);
25         init_waitqueue_head(&port->open_wait);
26         init_waitqueue_head(&port->close_wait);
27         init_waitqueue_head(&port->delta_msr_wait);
28         mutex_init(&port->mutex);
29         mutex_init(&port->buf_mutex);
30         spin_lock_init(&port->lock);
31         port->close_delay = (50 * HZ) / 100;
32         port->closing_wait = (3000 * HZ) / 100;
33         kref_init(&port->kref);
34 }
35 EXPORT_SYMBOL(tty_port_init);
36
37 /**
38  * tty_port_link_device - link tty and tty_port
39  * @port: tty_port of the device
40  * @driver: tty_driver for this device
41  * @index: index of the tty
42  *
43  * Provide the tty layer wit ha link from a tty (specified by @index) to a
44  * tty_port (@port). Use this only if neither tty_port_register_device nor
45  * tty_port_install is used in the driver. If used, this has to be called before
46  * tty_register_driver.
47  */
48 void tty_port_link_device(struct tty_port *port,
49                 struct tty_driver *driver, unsigned index)
50 {
51         if (WARN_ON(index >= driver->num))
52                 return;
53         driver->ports[index] = port;
54 }
55 EXPORT_SYMBOL_GPL(tty_port_link_device);
56
57 /**
58  * tty_port_register_device - register tty device
59  * @port: tty_port of the device
60  * @driver: tty_driver for this device
61  * @index: index of the tty
62  * @device: parent if exists, otherwise NULL
63  *
64  * It is the same as tty_register_device except the provided @port is linked to
65  * a concrete tty specified by @index. Use this or tty_port_install (or both).
66  * Call tty_port_link_device as a last resort.
67  */
68 struct device *tty_port_register_device(struct tty_port *port,
69                 struct tty_driver *driver, unsigned index,
70                 struct device *device)
71 {
72         tty_port_link_device(port, driver, index);
73         return tty_register_device(driver, index, device);
74 }
75 EXPORT_SYMBOL_GPL(tty_port_register_device);
76
77 /**
78  * tty_port_register_device_attr - register tty device
79  * @port: tty_port of the device
80  * @driver: tty_driver for this device
81  * @index: index of the tty
82  * @device: parent if exists, otherwise NULL
83  * @drvdata: Driver data to be set to device.
84  * @attr_grp: Attribute group to be set on device.
85  *
86  * It is the same as tty_register_device_attr except the provided @port is
87  * linked to a concrete tty specified by @index. Use this or tty_port_install
88  * (or both). Call tty_port_link_device as a last resort.
89  */
90 struct device *tty_port_register_device_attr(struct tty_port *port,
91                 struct tty_driver *driver, unsigned index,
92                 struct device *device, void *drvdata,
93                 const struct attribute_group **attr_grp)
94 {
95         tty_port_link_device(port, driver, index);
96         return tty_register_device_attr(driver, index, device, drvdata,
97                         attr_grp);
98 }
99 EXPORT_SYMBOL_GPL(tty_port_register_device_attr);
100
101 int tty_port_alloc_xmit_buf(struct tty_port *port)
102 {
103         /* We may sleep in get_zeroed_page() */
104         mutex_lock(&port->buf_mutex);
105         if (port->xmit_buf == NULL)
106                 port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL);
107         mutex_unlock(&port->buf_mutex);
108         if (port->xmit_buf == NULL)
109                 return -ENOMEM;
110         return 0;
111 }
112 EXPORT_SYMBOL(tty_port_alloc_xmit_buf);
113
114 void tty_port_free_xmit_buf(struct tty_port *port)
115 {
116         mutex_lock(&port->buf_mutex);
117         if (port->xmit_buf != NULL) {
118                 free_page((unsigned long)port->xmit_buf);
119                 port->xmit_buf = NULL;
120         }
121         mutex_unlock(&port->buf_mutex);
122 }
123 EXPORT_SYMBOL(tty_port_free_xmit_buf);
124
125 /**
126  * tty_port_destroy -- destroy inited port
127  * @port: tty port to be doestroyed
128  *
129  * When a port was initialized using tty_port_init, one has to destroy the
130  * port by this function. Either indirectly by using tty_port refcounting
131  * (tty_port_put) or directly if refcounting is not used.
132  */
133 void tty_port_destroy(struct tty_port *port)
134 {
135         tty_buffer_free_all(port);
136 }
137 EXPORT_SYMBOL(tty_port_destroy);
138
139 static void tty_port_destructor(struct kref *kref)
140 {
141         struct tty_port *port = container_of(kref, struct tty_port, kref);
142         if (port->xmit_buf)
143                 free_page((unsigned long)port->xmit_buf);
144         tty_port_destroy(port);
145         if (port->ops && port->ops->destruct)
146                 port->ops->destruct(port);
147         else
148                 kfree(port);
149 }
150
151 void tty_port_put(struct tty_port *port)
152 {
153         if (port)
154                 kref_put(&port->kref, tty_port_destructor);
155 }
156 EXPORT_SYMBOL(tty_port_put);
157
158 /**
159  *      tty_port_tty_get        -       get a tty reference
160  *      @port: tty port
161  *
162  *      Return a refcount protected tty instance or NULL if the port is not
163  *      associated with a tty (eg due to close or hangup)
164  */
165
166 struct tty_struct *tty_port_tty_get(struct tty_port *port)
167 {
168         unsigned long flags;
169         struct tty_struct *tty;
170
171         spin_lock_irqsave(&port->lock, flags);
172         tty = tty_kref_get(port->tty);
173         spin_unlock_irqrestore(&port->lock, flags);
174         return tty;
175 }
176 EXPORT_SYMBOL(tty_port_tty_get);
177
178 /**
179  *      tty_port_tty_set        -       set the tty of a port
180  *      @port: tty port
181  *      @tty: the tty
182  *
183  *      Associate the port and tty pair. Manages any internal refcounts.
184  *      Pass NULL to deassociate a port
185  */
186
187 void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty)
188 {
189         unsigned long flags;
190
191         spin_lock_irqsave(&port->lock, flags);
192         if (port->tty)
193                 tty_kref_put(port->tty);
194         port->tty = tty_kref_get(tty);
195         spin_unlock_irqrestore(&port->lock, flags);
196 }
197 EXPORT_SYMBOL(tty_port_tty_set);
198
199 static void tty_port_shutdown(struct tty_port *port)
200 {
201         mutex_lock(&port->mutex);
202         if (port->console)
203                 goto out;
204
205         if (test_and_clear_bit(ASYNCB_INITIALIZED, &port->flags)) {
206                 if (port->ops->shutdown)
207                         port->ops->shutdown(port);
208         }
209 out:
210         mutex_unlock(&port->mutex);
211 }
212
213 /**
214  *      tty_port_hangup         -       hangup helper
215  *      @port: tty port
216  *
217  *      Perform port level tty hangup flag and count changes. Drop the tty
218  *      reference.
219  */
220
221 void tty_port_hangup(struct tty_port *port)
222 {
223         unsigned long flags;
224
225         spin_lock_irqsave(&port->lock, flags);
226         port->count = 0;
227         port->flags &= ~ASYNC_NORMAL_ACTIVE;
228         if (port->tty) {
229                 set_bit(TTY_IO_ERROR, &port->tty->flags);
230                 tty_kref_put(port->tty);
231         }
232         port->tty = NULL;
233         spin_unlock_irqrestore(&port->lock, flags);
234         tty_port_shutdown(port);
235         wake_up_interruptible(&port->open_wait);
236         wake_up_interruptible(&port->delta_msr_wait);
237 }
238 EXPORT_SYMBOL(tty_port_hangup);
239
240 /**
241  * tty_port_tty_hangup - helper to hang up a tty
242  *
243  * @port: tty port
244  * @check_clocal: hang only ttys with CLOCAL unset?
245  */
246 void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
247 {
248         struct tty_struct *tty = tty_port_tty_get(port);
249
250         if (tty && (!check_clocal || !C_CLOCAL(tty))) {
251                 tty_hangup(tty);
252                 tty_kref_put(tty);
253         }
254 }
255 EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
256
257 /**
258  * tty_port_tty_wakeup - helper to wake up a tty
259  *
260  * @port: tty port
261  */
262 void tty_port_tty_wakeup(struct tty_port *port)
263 {
264         struct tty_struct *tty = tty_port_tty_get(port);
265
266         if (tty) {
267                 tty_wakeup(tty);
268                 tty_kref_put(tty);
269         }
270 }
271 EXPORT_SYMBOL_GPL(tty_port_tty_wakeup);
272
273 /**
274  *      tty_port_carrier_raised -       carrier raised check
275  *      @port: tty port
276  *
277  *      Wrapper for the carrier detect logic. For the moment this is used
278  *      to hide some internal details. This will eventually become entirely
279  *      internal to the tty port.
280  */
281
282 int tty_port_carrier_raised(struct tty_port *port)
283 {
284         if (port->ops->carrier_raised == NULL)
285                 return 1;
286         return port->ops->carrier_raised(port);
287 }
288 EXPORT_SYMBOL(tty_port_carrier_raised);
289
290 /**
291  *      tty_port_raise_dtr_rts  -       Raise DTR/RTS
292  *      @port: tty port
293  *
294  *      Wrapper for the DTR/RTS raise logic. For the moment this is used
295  *      to hide some internal details. This will eventually become entirely
296  *      internal to the tty port.
297  */
298
299 void tty_port_raise_dtr_rts(struct tty_port *port)
300 {
301         if (port->ops->dtr_rts)
302                 port->ops->dtr_rts(port, 1);
303 }
304 EXPORT_SYMBOL(tty_port_raise_dtr_rts);
305
306 /**
307  *      tty_port_lower_dtr_rts  -       Lower DTR/RTS
308  *      @port: tty port
309  *
310  *      Wrapper for the DTR/RTS raise logic. For the moment this is used
311  *      to hide some internal details. This will eventually become entirely
312  *      internal to the tty port.
313  */
314
315 void tty_port_lower_dtr_rts(struct tty_port *port)
316 {
317         if (port->ops->dtr_rts)
318                 port->ops->dtr_rts(port, 0);
319 }
320 EXPORT_SYMBOL(tty_port_lower_dtr_rts);
321
322 /**
323  *      tty_port_block_til_ready        -       Waiting logic for tty open
324  *      @port: the tty port being opened
325  *      @tty: the tty device being bound
326  *      @filp: the file pointer of the opener
327  *
328  *      Implement the core POSIX/SuS tty behaviour when opening a tty device.
329  *      Handles:
330  *              - hangup (both before and during)
331  *              - non blocking open
332  *              - rts/dtr/dcd
333  *              - signals
334  *              - port flags and counts
335  *
336  *      The passed tty_port must implement the carrier_raised method if it can
337  *      do carrier detect and the dtr_rts method if it supports software
338  *      management of these lines. Note that the dtr/rts raise is done each
339  *      iteration as a hangup may have previously dropped them while we wait.
340  */
341
342 int tty_port_block_til_ready(struct tty_port *port,
343                                 struct tty_struct *tty, struct file *filp)
344 {
345         int do_clocal = 0, retval;
346         unsigned long flags;
347         DEFINE_WAIT(wait);
348
349         /* block if port is in the process of being closed */
350         if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
351                 wait_event_interruptible_tty(tty, port->close_wait,
352                                 !(port->flags & ASYNC_CLOSING));
353                 if (port->flags & ASYNC_HUP_NOTIFY)
354                         return -EAGAIN;
355                 else
356                         return -ERESTARTSYS;
357         }
358
359         /* if non-blocking mode is set we can pass directly to open unless
360            the port has just hung up or is in another error state */
361         if (tty->flags & (1 << TTY_IO_ERROR)) {
362                 port->flags |= ASYNC_NORMAL_ACTIVE;
363                 return 0;
364         }
365         if (filp->f_flags & O_NONBLOCK) {
366                 /* Indicate we are open */
367                 if (tty->termios.c_cflag & CBAUD)
368                         tty_port_raise_dtr_rts(port);
369                 port->flags |= ASYNC_NORMAL_ACTIVE;
370                 return 0;
371         }
372
373         if (C_CLOCAL(tty))
374                 do_clocal = 1;
375
376         /* Block waiting until we can proceed. We may need to wait for the
377            carrier, but we must also wait for any close that is in progress
378            before the next open may complete */
379
380         retval = 0;
381
382         /* The port lock protects the port counts */
383         spin_lock_irqsave(&port->lock, flags);
384         if (!tty_hung_up_p(filp))
385                 port->count--;
386         port->blocked_open++;
387         spin_unlock_irqrestore(&port->lock, flags);
388
389         while (1) {
390                 /* Indicate we are open */
391                 if (C_BAUD(tty) && test_bit(ASYNCB_INITIALIZED, &port->flags))
392                         tty_port_raise_dtr_rts(port);
393
394                 prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE);
395                 /* Check for a hangup or uninitialised port.
396                                                         Return accordingly */
397                 if (tty_hung_up_p(filp) || !(port->flags & ASYNC_INITIALIZED)) {
398                         if (port->flags & ASYNC_HUP_NOTIFY)
399                                 retval = -EAGAIN;
400                         else
401                                 retval = -ERESTARTSYS;
402                         break;
403                 }
404                 /*
405                  * Probe the carrier. For devices with no carrier detect
406                  * tty_port_carrier_raised will always return true.
407                  * Never ask drivers if CLOCAL is set, this causes troubles
408                  * on some hardware.
409                  */
410                 if (!(port->flags & ASYNC_CLOSING) &&
411                                 (do_clocal || tty_port_carrier_raised(port)))
412                         break;
413                 if (signal_pending(current)) {
414                         retval = -ERESTARTSYS;
415                         break;
416                 }
417                 tty_unlock(tty);
418                 schedule();
419                 tty_lock(tty);
420         }
421         finish_wait(&port->open_wait, &wait);
422
423         /* Update counts. A parallel hangup will have set count to zero and
424            we must not mess that up further */
425         spin_lock_irqsave(&port->lock, flags);
426         if (!tty_hung_up_p(filp))
427                 port->count++;
428         port->blocked_open--;
429         if (retval == 0)
430                 port->flags |= ASYNC_NORMAL_ACTIVE;
431         spin_unlock_irqrestore(&port->lock, flags);
432         return retval;
433 }
434 EXPORT_SYMBOL(tty_port_block_til_ready);
435
436 int tty_port_close_start(struct tty_port *port,
437                                 struct tty_struct *tty, struct file *filp)
438 {
439         unsigned long flags;
440
441         spin_lock_irqsave(&port->lock, flags);
442         if (tty_hung_up_p(filp)) {
443                 spin_unlock_irqrestore(&port->lock, flags);
444                 return 0;
445         }
446
447         if (tty->count == 1 && port->count != 1) {
448                 printk(KERN_WARNING
449                     "tty_port_close_start: tty->count = 1 port count = %d.\n",
450                                                                 port->count);
451                 port->count = 1;
452         }
453         if (--port->count < 0) {
454                 printk(KERN_WARNING "tty_port_close_start: count = %d\n",
455                                                                 port->count);
456                 port->count = 0;
457         }
458
459         if (port->count) {
460                 spin_unlock_irqrestore(&port->lock, flags);
461                 if (port->ops->drop)
462                         port->ops->drop(port);
463                 return 0;
464         }
465         set_bit(ASYNCB_CLOSING, &port->flags);
466         tty->closing = 1;
467         spin_unlock_irqrestore(&port->lock, flags);
468         /* Don't block on a stalled port, just pull the chain */
469         if (tty->flow_stopped)
470                 tty_driver_flush_buffer(tty);
471         if (test_bit(ASYNCB_INITIALIZED, &port->flags) &&
472                         port->closing_wait != ASYNC_CLOSING_WAIT_NONE)
473                 tty_wait_until_sent_from_close(tty, port->closing_wait);
474         if (port->drain_delay) {
475                 unsigned int bps = tty_get_baud_rate(tty);
476                 long timeout;
477
478                 if (bps > 1200)
479                         timeout = max_t(long,
480                                 (HZ * 10 * port->drain_delay) / bps, HZ / 10);
481                 else
482                         timeout = 2 * HZ;
483                 schedule_timeout_interruptible(timeout);
484         }
485         /* Flush the ldisc buffering */
486         tty_ldisc_flush(tty);
487
488         /* Drop DTR/RTS if HUPCL is set. This causes any attached modem to
489            hang up the line */
490         if (tty->termios.c_cflag & HUPCL)
491                 tty_port_lower_dtr_rts(port);
492
493         /* Don't call port->drop for the last reference. Callers will want
494            to drop the last active reference in ->shutdown() or the tty
495            shutdown path */
496         return 1;
497 }
498 EXPORT_SYMBOL(tty_port_close_start);
499
500 void tty_port_close_end(struct tty_port *port, struct tty_struct *tty)
501 {
502         unsigned long flags;
503
504         spin_lock_irqsave(&port->lock, flags);
505         tty->closing = 0;
506
507         if (port->blocked_open) {
508                 spin_unlock_irqrestore(&port->lock, flags);
509                 if (port->close_delay) {
510                         msleep_interruptible(
511                                 jiffies_to_msecs(port->close_delay));
512                 }
513                 spin_lock_irqsave(&port->lock, flags);
514                 wake_up_interruptible(&port->open_wait);
515         }
516         port->flags &= ~(ASYNC_NORMAL_ACTIVE | ASYNC_CLOSING);
517         wake_up_interruptible(&port->close_wait);
518         spin_unlock_irqrestore(&port->lock, flags);
519 }
520 EXPORT_SYMBOL(tty_port_close_end);
521
522 void tty_port_close(struct tty_port *port, struct tty_struct *tty,
523                                                         struct file *filp)
524 {
525         if (tty_port_close_start(port, tty, filp) == 0)
526                 return;
527         tty_port_shutdown(port);
528         set_bit(TTY_IO_ERROR, &tty->flags);
529         tty_port_close_end(port, tty);
530         tty_port_tty_set(port, NULL);
531 }
532 EXPORT_SYMBOL(tty_port_close);
533
534 /**
535  * tty_port_install - generic tty->ops->install handler
536  * @port: tty_port of the device
537  * @driver: tty_driver for this device
538  * @tty: tty to be installed
539  *
540  * It is the same as tty_standard_install except the provided @port is linked
541  * to a concrete tty specified by @tty. Use this or tty_port_register_device
542  * (or both). Call tty_port_link_device as a last resort.
543  */
544 int tty_port_install(struct tty_port *port, struct tty_driver *driver,
545                 struct tty_struct *tty)
546 {
547         tty->port = port;
548         return tty_standard_install(driver, tty);
549 }
550 EXPORT_SYMBOL_GPL(tty_port_install);
551
552 int tty_port_open(struct tty_port *port, struct tty_struct *tty,
553                                                         struct file *filp)
554 {
555         spin_lock_irq(&port->lock);
556         if (!tty_hung_up_p(filp))
557                 ++port->count;
558         spin_unlock_irq(&port->lock);
559         tty_port_tty_set(port, tty);
560
561         /*
562          * Do the device-specific open only if the hardware isn't
563          * already initialized. Serialize open and shutdown using the
564          * port mutex.
565          */
566
567         mutex_lock(&port->mutex);
568
569         if (!test_bit(ASYNCB_INITIALIZED, &port->flags)) {
570                 clear_bit(TTY_IO_ERROR, &tty->flags);
571                 if (port->ops->activate) {
572                         int retval = port->ops->activate(port, tty);
573                         if (retval) {
574                                 mutex_unlock(&port->mutex);
575                                 return retval;
576                         }
577                 }
578                 set_bit(ASYNCB_INITIALIZED, &port->flags);
579         }
580         mutex_unlock(&port->mutex);
581         return tty_port_block_til_ready(port, tty, filp);
582 }
583
584 EXPORT_SYMBOL(tty_port_open);