Commit | Line | Data |
---|---|---|
e3b3d0f5 | 1 | // SPDX-License-Identifier: GPL-2.0 |
9e48565d AC |
2 | /* |
3 | * Tty port functions | |
4 | */ | |
5 | ||
6 | #include <linux/types.h> | |
7 | #include <linux/errno.h> | |
8 | #include <linux/tty.h> | |
9 | #include <linux/tty_driver.h> | |
10 | #include <linux/tty_flip.h> | |
3e61696b | 11 | #include <linux/serial.h> |
9e48565d AC |
12 | #include <linux/timer.h> |
13 | #include <linux/string.h> | |
14 | #include <linux/slab.h> | |
174cd4b1 | 15 | #include <linux/sched/signal.h> |
9e48565d AC |
16 | #include <linux/wait.h> |
17 | #include <linux/bitops.h> | |
18 | #include <linux/delay.h> | |
19 | #include <linux/module.h> | |
8cde11b2 | 20 | #include <linux/serdev.h> |
98602c01 | 21 | #include "tty.h" |
9e48565d | 22 | |
0468a807 JSS |
23 | static size_t tty_port_default_receive_buf(struct tty_port *port, const u8 *p, |
24 | const u8 *f, size_t count) | |
c3485ee0 | 25 | { |
c3485ee0 | 26 | struct tty_struct *tty; |
c6e37fe0 | 27 | struct tty_ldisc *ld; |
c3485ee0 RH |
28 | |
29 | tty = READ_ONCE(port->itty); | |
30 | if (!tty) | |
31 | return 0; | |
32 | ||
c6e37fe0 JSS |
33 | ld = tty_ldisc_ref(tty); |
34 | if (!ld) | |
c3485ee0 RH |
35 | return 0; |
36 | ||
892bc209 | 37 | count = tty_ldisc_receive_buf(ld, p, f, count); |
c3485ee0 | 38 | |
c6e37fe0 | 39 | tty_ldisc_deref(ld); |
c3485ee0 | 40 | |
0468a807 | 41 | return count; |
c3485ee0 RH |
42 | } |
43 | ||
0b7a2b28 | 44 | static void tty_port_default_lookahead_buf(struct tty_port *port, const u8 *p, |
0468a807 | 45 | const u8 *f, size_t count) |
6bb6fa69 IJ |
46 | { |
47 | struct tty_struct *tty; | |
c6e37fe0 | 48 | struct tty_ldisc *ld; |
6bb6fa69 IJ |
49 | |
50 | tty = READ_ONCE(port->itty); | |
51 | if (!tty) | |
52 | return; | |
53 | ||
c6e37fe0 JSS |
54 | ld = tty_ldisc_ref(tty); |
55 | if (!ld) | |
6bb6fa69 IJ |
56 | return; |
57 | ||
c6e37fe0 JSS |
58 | if (ld->ops->lookahead_buf) |
59 | ld->ops->lookahead_buf(ld->tty, p, f, count); | |
6bb6fa69 | 60 | |
c6e37fe0 | 61 | tty_ldisc_deref(ld); |
6bb6fa69 IJ |
62 | } |
63 | ||
c3485ee0 RH |
64 | static void tty_port_default_wakeup(struct tty_port *port) |
65 | { | |
66 | struct tty_struct *tty = tty_port_tty_get(port); | |
67 | ||
68 | if (tty) { | |
69 | tty_wakeup(tty); | |
70 | tty_kref_put(tty); | |
71 | } | |
72 | } | |
73 | ||
0c5aae59 | 74 | const struct tty_port_client_operations tty_port_default_client_ops = { |
c3485ee0 | 75 | .receive_buf = tty_port_default_receive_buf, |
6bb6fa69 | 76 | .lookahead_buf = tty_port_default_lookahead_buf, |
c3485ee0 RH |
77 | .write_wakeup = tty_port_default_wakeup, |
78 | }; | |
0c5aae59 | 79 | EXPORT_SYMBOL_GPL(tty_port_default_client_ops); |
c3485ee0 | 80 | |
3be491d7 | 81 | /** |
c38f45ef | 82 | * tty_port_init - initialize tty_port |
3be491d7 JS |
83 | * @port: tty_port to initialize |
84 | * | |
85 | * Initializes the state of struct tty_port. When a port was initialized using | |
86 | * this function, one has to destroy the port by tty_port_destroy(). Either | |
87 | * indirectly by using &tty_port refcounting (tty_port_put()) or directly if | |
88 | * refcounting is not used. | |
89 | */ | |
9e48565d AC |
90 | void tty_port_init(struct tty_port *port) |
91 | { | |
92 | memset(port, 0, sizeof(*port)); | |
ecbbfd44 | 93 | tty_buffer_init(port); |
9e48565d | 94 | init_waitqueue_head(&port->open_wait); |
bdc04e31 | 95 | init_waitqueue_head(&port->delta_msr_wait); |
9e48565d | 96 | mutex_init(&port->mutex); |
44e4909e | 97 | mutex_init(&port->buf_mutex); |
4a90f09b | 98 | spin_lock_init(&port->lock); |
9e48565d AC |
99 | port->close_delay = (50 * HZ) / 100; |
100 | port->closing_wait = (3000 * HZ) / 100; | |
0c5aae59 | 101 | port->client_ops = &tty_port_default_client_ops; |
568aafc6 | 102 | kref_init(&port->kref); |
9e48565d AC |
103 | } |
104 | EXPORT_SYMBOL(tty_port_init); | |
105 | ||
2cb4ca02 JS |
106 | /** |
107 | * tty_port_link_device - link tty and tty_port | |
108 | * @port: tty_port of the device | |
109 | * @driver: tty_driver for this device | |
110 | * @index: index of the tty | |
111 | * | |
1926e5d3 | 112 | * Provide the tty layer with a link from a tty (specified by @index) to a |
cb6f6f98 JS |
113 | * tty_port (@port). Use this only if neither tty_port_register_device() nor |
114 | * tty_port_install() is used in the driver. If used, this has to be called | |
115 | * before tty_register_driver(). | |
2cb4ca02 JS |
116 | */ |
117 | void tty_port_link_device(struct tty_port *port, | |
118 | struct tty_driver *driver, unsigned index) | |
119 | { | |
120 | if (WARN_ON(index >= driver->num)) | |
121 | return; | |
273f6329 | 122 | driver->ports[index] = port; |
2cb4ca02 JS |
123 | } |
124 | EXPORT_SYMBOL_GPL(tty_port_link_device); | |
125 | ||
72a33bf5 JS |
126 | /** |
127 | * tty_port_register_device - register tty device | |
128 | * @port: tty_port of the device | |
129 | * @driver: tty_driver for this device | |
130 | * @index: index of the tty | |
131 | * @device: parent if exists, otherwise NULL | |
132 | * | |
cb6f6f98 JS |
133 | * It is the same as tty_register_device() except the provided @port is linked |
134 | * to a concrete tty specified by @index. Use this or tty_port_install() (or | |
135 | * both). Call tty_port_link_device() as a last resort. | |
72a33bf5 | 136 | */ |
057eb856 JS |
137 | struct device *tty_port_register_device(struct tty_port *port, |
138 | struct tty_driver *driver, unsigned index, | |
139 | struct device *device) | |
140 | { | |
3086365d | 141 | return tty_port_register_device_attr(port, driver, index, device, NULL, NULL); |
057eb856 JS |
142 | } |
143 | EXPORT_SYMBOL_GPL(tty_port_register_device); | |
144 | ||
b1b79916 TH |
145 | /** |
146 | * tty_port_register_device_attr - register tty device | |
147 | * @port: tty_port of the device | |
148 | * @driver: tty_driver for this device | |
149 | * @index: index of the tty | |
150 | * @device: parent if exists, otherwise NULL | |
151 | * @drvdata: Driver data to be set to device. | |
152 | * @attr_grp: Attribute group to be set on device. | |
153 | * | |
cb6f6f98 JS |
154 | * It is the same as tty_register_device_attr() except the provided @port is |
155 | * linked to a concrete tty specified by @index. Use this or tty_port_install() | |
156 | * (or both). Call tty_port_link_device() as a last resort. | |
b1b79916 TH |
157 | */ |
158 | struct device *tty_port_register_device_attr(struct tty_port *port, | |
159 | struct tty_driver *driver, unsigned index, | |
160 | struct device *device, void *drvdata, | |
161 | const struct attribute_group **attr_grp) | |
162 | { | |
163 | tty_port_link_device(port, driver, index); | |
164 | return tty_register_device_attr(driver, index, device, drvdata, | |
165 | attr_grp); | |
166 | } | |
167 | EXPORT_SYMBOL_GPL(tty_port_register_device_attr); | |
168 | ||
8cde11b2 JH |
169 | /** |
170 | * tty_port_register_device_attr_serdev - register tty or serdev device | |
171 | * @port: tty_port of the device | |
172 | * @driver: tty_driver for this device | |
173 | * @index: index of the tty | |
174 | * @device: parent if exists, otherwise NULL | |
175 | * @drvdata: driver data for the device | |
176 | * @attr_grp: attribute group for the device | |
177 | * | |
178 | * Register a serdev or tty device depending on if the parent device has any | |
179 | * defined serdev clients or not. | |
180 | */ | |
181 | struct device *tty_port_register_device_attr_serdev(struct tty_port *port, | |
182 | struct tty_driver *driver, unsigned index, | |
183 | struct device *device, void *drvdata, | |
184 | const struct attribute_group **attr_grp) | |
185 | { | |
186 | struct device *dev; | |
187 | ||
188 | tty_port_link_device(port, driver, index); | |
189 | ||
190 | dev = serdev_tty_port_register(port, device, driver, index); | |
191 | if (PTR_ERR(dev) != -ENODEV) { | |
192 | /* Skip creating cdev if we registered a serdev device */ | |
193 | return dev; | |
194 | } | |
195 | ||
196 | return tty_register_device_attr(driver, index, device, drvdata, | |
197 | attr_grp); | |
198 | } | |
199 | EXPORT_SYMBOL_GPL(tty_port_register_device_attr_serdev); | |
200 | ||
201 | /** | |
202 | * tty_port_register_device_serdev - register tty or serdev device | |
203 | * @port: tty_port of the device | |
204 | * @driver: tty_driver for this device | |
205 | * @index: index of the tty | |
206 | * @device: parent if exists, otherwise NULL | |
207 | * | |
208 | * Register a serdev or tty device depending on if the parent device has any | |
209 | * defined serdev clients or not. | |
210 | */ | |
211 | struct device *tty_port_register_device_serdev(struct tty_port *port, | |
212 | struct tty_driver *driver, unsigned index, | |
213 | struct device *device) | |
214 | { | |
215 | return tty_port_register_device_attr_serdev(port, driver, index, | |
216 | device, NULL, NULL); | |
217 | } | |
218 | EXPORT_SYMBOL_GPL(tty_port_register_device_serdev); | |
219 | ||
220 | /** | |
221 | * tty_port_unregister_device - deregister a tty or serdev device | |
222 | * @port: tty_port of the device | |
223 | * @driver: tty_driver for this device | |
224 | * @index: index of the tty | |
225 | * | |
226 | * If a tty or serdev device is registered with a call to | |
227 | * tty_port_register_device_serdev() then this function must be called when | |
228 | * the device is gone. | |
229 | */ | |
230 | void tty_port_unregister_device(struct tty_port *port, | |
231 | struct tty_driver *driver, unsigned index) | |
232 | { | |
233 | int ret; | |
234 | ||
235 | ret = serdev_tty_port_unregister(port); | |
236 | if (ret == 0) | |
237 | return; | |
238 | ||
239 | tty_unregister_device(driver, index); | |
240 | } | |
241 | EXPORT_SYMBOL_GPL(tty_port_unregister_device); | |
242 | ||
9e48565d AC |
243 | int tty_port_alloc_xmit_buf(struct tty_port *port) |
244 | { | |
245 | /* We may sleep in get_zeroed_page() */ | |
44e4909e | 246 | mutex_lock(&port->buf_mutex); |
4e2a44c1 | 247 | if (port->xmit_buf == NULL) { |
9e48565d | 248 | port->xmit_buf = (unsigned char *)get_zeroed_page(GFP_KERNEL); |
4e2a44c1 JS |
249 | if (port->xmit_buf) |
250 | kfifo_init(&port->xmit_fifo, port->xmit_buf, PAGE_SIZE); | |
251 | } | |
44e4909e | 252 | mutex_unlock(&port->buf_mutex); |
9e48565d AC |
253 | if (port->xmit_buf == NULL) |
254 | return -ENOMEM; | |
255 | return 0; | |
256 | } | |
257 | EXPORT_SYMBOL(tty_port_alloc_xmit_buf); | |
258 | ||
259 | void tty_port_free_xmit_buf(struct tty_port *port) | |
260 | { | |
44e4909e | 261 | mutex_lock(&port->buf_mutex); |
3c5b2f5b AS |
262 | free_page((unsigned long)port->xmit_buf); |
263 | port->xmit_buf = NULL; | |
264 | INIT_KFIFO(port->xmit_fifo); | |
44e4909e | 265 | mutex_unlock(&port->buf_mutex); |
9e48565d AC |
266 | } |
267 | EXPORT_SYMBOL(tty_port_free_xmit_buf); | |
268 | ||
de274bfe | 269 | /** |
c38f45ef | 270 | * tty_port_destroy - destroy inited port |
1926e5d3 | 271 | * @port: tty port to be destroyed |
de274bfe | 272 | * |
cb6f6f98 JS |
273 | * When a port was initialized using tty_port_init(), one has to destroy the |
274 | * port by this function. Either indirectly by using &tty_port refcounting | |
275 | * (tty_port_put()) or directly if refcounting is not used. | |
de274bfe JS |
276 | */ |
277 | void tty_port_destroy(struct tty_port *port) | |
278 | { | |
e176058f | 279 | tty_buffer_cancel_work(port); |
de274bfe JS |
280 | tty_buffer_free_all(port); |
281 | } | |
282 | EXPORT_SYMBOL(tty_port_destroy); | |
283 | ||
568aafc6 AC |
284 | static void tty_port_destructor(struct kref *kref) |
285 | { | |
286 | struct tty_port *port = container_of(kref, struct tty_port, kref); | |
e3bfea23 PH |
287 | |
288 | /* check if last port ref was dropped before tty release */ | |
289 | if (WARN_ON(port->itty)) | |
290 | return; | |
3c5b2f5b | 291 | free_page((unsigned long)port->xmit_buf); |
de274bfe | 292 | tty_port_destroy(port); |
81c79838 | 293 | if (port->ops && port->ops->destruct) |
568aafc6 AC |
294 | port->ops->destruct(port); |
295 | else | |
296 | kfree(port); | |
297 | } | |
298 | ||
3be491d7 | 299 | /** |
c38f45ef | 300 | * tty_port_put - drop a reference to tty_port |
3be491d7 JS |
301 | * @port: port to drop a reference of (can be NULL) |
302 | * | |
303 | * The final put will destroy and free up the @port using | |
304 | * @port->ops->destruct() hook, or using kfree() if not provided. | |
305 | */ | |
568aafc6 AC |
306 | void tty_port_put(struct tty_port *port) |
307 | { | |
308 | if (port) | |
309 | kref_put(&port->kref, tty_port_destructor); | |
310 | } | |
311 | EXPORT_SYMBOL(tty_port_put); | |
9e48565d | 312 | |
4a90f09b | 313 | /** |
cb6f6f98 JS |
314 | * tty_port_tty_get - get a tty reference |
315 | * @port: tty port | |
4a90f09b | 316 | * |
cb6f6f98 JS |
317 | * Return a refcount protected tty instance or %NULL if the port is not |
318 | * associated with a tty (eg due to close or hangup). | |
4a90f09b | 319 | */ |
4a90f09b AC |
320 | struct tty_struct *tty_port_tty_get(struct tty_port *port) |
321 | { | |
322 | unsigned long flags; | |
323 | struct tty_struct *tty; | |
324 | ||
325 | spin_lock_irqsave(&port->lock, flags); | |
326 | tty = tty_kref_get(port->tty); | |
327 | spin_unlock_irqrestore(&port->lock, flags); | |
328 | return tty; | |
329 | } | |
330 | EXPORT_SYMBOL(tty_port_tty_get); | |
331 | ||
332 | /** | |
cb6f6f98 JS |
333 | * tty_port_tty_set - set the tty of a port |
334 | * @port: tty port | |
335 | * @tty: the tty | |
4a90f09b | 336 | * |
cb6f6f98 JS |
337 | * Associate the port and tty pair. Manages any internal refcounts. Pass %NULL |
338 | * to deassociate a port. | |
4a90f09b | 339 | */ |
4a90f09b AC |
340 | void tty_port_tty_set(struct tty_port *port, struct tty_struct *tty) |
341 | { | |
342 | unsigned long flags; | |
343 | ||
344 | spin_lock_irqsave(&port->lock, flags); | |
a211b1af | 345 | tty_kref_put(port->tty); |
cb4bca35 | 346 | port->tty = tty_kref_get(tty); |
4a90f09b AC |
347 | spin_unlock_irqrestore(&port->lock, flags); |
348 | } | |
349 | EXPORT_SYMBOL(tty_port_tty_set); | |
31f35939 | 350 | |
3be491d7 JS |
351 | /** |
352 | * tty_port_shutdown - internal helper to shutdown the device | |
353 | * @port: tty port to be shut down | |
354 | * @tty: the associated tty | |
355 | * | |
356 | * It is used by tty_port_hangup() and tty_port_close(). Its task is to | |
357 | * shutdown the device if it was initialized (note consoles remain | |
358 | * functioning). It lowers DTR/RTS (if @tty has HUPCL set) and invokes | |
359 | * @port->ops->shutdown(). | |
360 | */ | |
957dacae | 361 | static void tty_port_shutdown(struct tty_port *port, struct tty_struct *tty) |
7ca0ff9a | 362 | { |
64bc3979 | 363 | mutex_lock(&port->mutex); |
8bde9658 JH |
364 | if (port->console) |
365 | goto out; | |
366 | ||
d41861ca | 367 | if (tty_port_initialized(port)) { |
515be7ba | 368 | tty_port_set_initialized(port, false); |
957dacae JH |
369 | /* |
370 | * Drop DTR/RTS if HUPCL is set. This causes any attached | |
371 | * modem to hang up the line. | |
372 | */ | |
373 | if (tty && C_HUPCL(tty)) | |
374 | tty_port_lower_dtr_rts(port); | |
375 | ||
0d3cb6f6 | 376 | if (port->ops->shutdown) |
7ca0ff9a | 377 | port->ops->shutdown(port); |
8bde9658 JH |
378 | } |
379 | out: | |
64bc3979 | 380 | mutex_unlock(&port->mutex); |
7ca0ff9a AC |
381 | } |
382 | ||
3e61696b | 383 | /** |
cb6f6f98 JS |
384 | * tty_port_hangup - hangup helper |
385 | * @port: tty port | |
3e61696b | 386 | * |
cb6f6f98 JS |
387 | * Perform port level tty hangup flag and count changes. Drop the tty |
388 | * reference. | |
9c9928bd | 389 | * |
cb6f6f98 | 390 | * Caller holds tty lock. |
3e61696b | 391 | */ |
3e61696b AC |
392 | void tty_port_hangup(struct tty_port *port) |
393 | { | |
957dacae | 394 | struct tty_struct *tty; |
3e61696b AC |
395 | unsigned long flags; |
396 | ||
397 | spin_lock_irqsave(&port->lock, flags); | |
398 | port->count = 0; | |
957dacae JH |
399 | tty = port->tty; |
400 | if (tty) | |
401 | set_bit(TTY_IO_ERROR, &tty->flags); | |
3e61696b AC |
402 | port->tty = NULL; |
403 | spin_unlock_irqrestore(&port->lock, flags); | |
9b5aa549 | 404 | tty_port_set_active(port, false); |
957dacae JH |
405 | tty_port_shutdown(port, tty); |
406 | tty_kref_put(tty); | |
3e61696b | 407 | wake_up_interruptible(&port->open_wait); |
bdc04e31 | 408 | wake_up_interruptible(&port->delta_msr_wait); |
3e61696b AC |
409 | } |
410 | EXPORT_SYMBOL(tty_port_hangup); | |
411 | ||
aa27a094 JS |
412 | /** |
413 | * tty_port_tty_hangup - helper to hang up a tty | |
aa27a094 | 414 | * @port: tty port |
cb6f6f98 | 415 | * @check_clocal: hang only ttys with %CLOCAL unset? |
aa27a094 JS |
416 | */ |
417 | void tty_port_tty_hangup(struct tty_port *port, bool check_clocal) | |
418 | { | |
419 | struct tty_struct *tty = tty_port_tty_get(port); | |
420 | ||
1d9e689c | 421 | if (tty && (!check_clocal || !C_CLOCAL(tty))) |
aa27a094 | 422 | tty_hangup(tty); |
1d9e689c | 423 | tty_kref_put(tty); |
aa27a094 JS |
424 | } |
425 | EXPORT_SYMBOL_GPL(tty_port_tty_hangup); | |
426 | ||
6aad04f2 JS |
427 | /** |
428 | * tty_port_tty_wakeup - helper to wake up a tty | |
6aad04f2 JS |
429 | * @port: tty port |
430 | */ | |
431 | void tty_port_tty_wakeup(struct tty_port *port) | |
432 | { | |
c3485ee0 | 433 | port->client_ops->write_wakeup(port); |
6aad04f2 JS |
434 | } |
435 | EXPORT_SYMBOL_GPL(tty_port_tty_wakeup); | |
436 | ||
31f35939 | 437 | /** |
cb6f6f98 JS |
438 | * tty_port_carrier_raised - carrier raised check |
439 | * @port: tty port | |
31f35939 | 440 | * |
cb6f6f98 JS |
441 | * Wrapper for the carrier detect logic. For the moment this is used |
442 | * to hide some internal details. This will eventually become entirely | |
443 | * internal to the tty port. | |
31f35939 | 444 | */ |
b300fb26 | 445 | bool tty_port_carrier_raised(struct tty_port *port) |
31f35939 | 446 | { |
0d3cb6f6 | 447 | if (port->ops->carrier_raised == NULL) |
b300fb26 | 448 | return true; |
31f35939 AC |
449 | return port->ops->carrier_raised(port); |
450 | } | |
451 | EXPORT_SYMBOL(tty_port_carrier_raised); | |
5d951fb4 AC |
452 | |
453 | /** | |
cb6f6f98 JS |
454 | * tty_port_raise_dtr_rts - Raise DTR/RTS |
455 | * @port: tty port | |
5d951fb4 | 456 | * |
cb6f6f98 JS |
457 | * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide |
458 | * some internal details. This will eventually become entirely internal to the | |
459 | * tty port. | |
5d951fb4 | 460 | */ |
5d951fb4 AC |
461 | void tty_port_raise_dtr_rts(struct tty_port *port) |
462 | { | |
0d3cb6f6 | 463 | if (port->ops->dtr_rts) |
5d420399 | 464 | port->ops->dtr_rts(port, true); |
5d951fb4 AC |
465 | } |
466 | EXPORT_SYMBOL(tty_port_raise_dtr_rts); | |
36c621d8 | 467 | |
fcc8ac18 | 468 | /** |
cb6f6f98 JS |
469 | * tty_port_lower_dtr_rts - Lower DTR/RTS |
470 | * @port: tty port | |
fcc8ac18 | 471 | * |
cb6f6f98 JS |
472 | * Wrapper for the DTR/RTS raise logic. For the moment this is used to hide |
473 | * some internal details. This will eventually become entirely internal to the | |
474 | * tty port. | |
fcc8ac18 | 475 | */ |
fcc8ac18 AC |
476 | void tty_port_lower_dtr_rts(struct tty_port *port) |
477 | { | |
0d3cb6f6 | 478 | if (port->ops->dtr_rts) |
5d420399 | 479 | port->ops->dtr_rts(port, false); |
fcc8ac18 AC |
480 | } |
481 | EXPORT_SYMBOL(tty_port_lower_dtr_rts); | |
482 | ||
36c621d8 | 483 | /** |
cb6f6f98 JS |
484 | * tty_port_block_til_ready - Waiting logic for tty open |
485 | * @port: the tty port being opened | |
486 | * @tty: the tty device being bound | |
487 | * @filp: the file pointer of the opener or %NULL | |
488 | * | |
489 | * Implement the core POSIX/SuS tty behaviour when opening a tty device. | |
490 | * Handles: | |
36c621d8 | 491 | * |
cb6f6f98 JS |
492 | * - hangup (both before and during) |
493 | * - non blocking open | |
494 | * - rts/dtr/dcd | |
495 | * - signals | |
496 | * - port flags and counts | |
36c621d8 | 497 | * |
cb6f6f98 JS |
498 | * The passed @port must implement the @port->ops->carrier_raised method if it |
499 | * can do carrier detect and the @port->ops->dtr_rts method if it supports | |
500 | * software management of these lines. Note that the dtr/rts raise is done each | |
501 | * iteration as a hangup may have previously dropped them while we wait. | |
c590f6b6 | 502 | * |
cb6f6f98 | 503 | * Caller holds tty lock. |
c590f6b6 | 504 | * |
cb6f6f98 JS |
505 | * Note: May drop and reacquire tty lock when blocking, so @tty and @port may |
506 | * have changed state (eg., may have been hung up). | |
36c621d8 | 507 | */ |
36c621d8 AC |
508 | int tty_port_block_til_ready(struct tty_port *port, |
509 | struct tty_struct *tty, struct file *filp) | |
510 | { | |
511 | int do_clocal = 0, retval; | |
512 | unsigned long flags; | |
6af9a43d | 513 | DEFINE_WAIT(wait); |
36c621d8 | 514 | |
36c621d8 | 515 | /* if non-blocking mode is set we can pass directly to open unless |
1df92640 XT |
516 | * the port has just hung up or is in another error state. |
517 | */ | |
18900ca6 | 518 | if (tty_io_error(tty)) { |
9b5aa549 | 519 | tty_port_set_active(port, true); |
8627b96d AC |
520 | return 0; |
521 | } | |
ed3f0af8 | 522 | if (filp == NULL || (filp->f_flags & O_NONBLOCK)) { |
4175f3e3 | 523 | /* Indicate we are open */ |
9db276f8 | 524 | if (C_BAUD(tty)) |
4175f3e3 | 525 | tty_port_raise_dtr_rts(port); |
9b5aa549 | 526 | tty_port_set_active(port, true); |
36c621d8 AC |
527 | return 0; |
528 | } | |
529 | ||
530 | if (C_CLOCAL(tty)) | |
531 | do_clocal = 1; | |
532 | ||
533 | /* Block waiting until we can proceed. We may need to wait for the | |
1df92640 XT |
534 | * carrier, but we must also wait for any close that is in progress |
535 | * before the next open may complete. | |
536 | */ | |
36c621d8 AC |
537 | |
538 | retval = 0; | |
36c621d8 AC |
539 | |
540 | /* The port lock protects the port counts */ | |
541 | spin_lock_irqsave(&port->lock, flags); | |
e359a4e3 | 542 | port->count--; |
36c621d8 AC |
543 | port->blocked_open++; |
544 | spin_unlock_irqrestore(&port->lock, flags); | |
545 | ||
546 | while (1) { | |
547 | /* Indicate we are open */ | |
d41861ca | 548 | if (C_BAUD(tty) && tty_port_initialized(port)) |
7834909f | 549 | tty_port_raise_dtr_rts(port); |
36c621d8 | 550 | |
3e3b5c08 | 551 | prepare_to_wait(&port->open_wait, &wait, TASK_INTERRUPTIBLE); |
d774a56d | 552 | /* Check for a hangup or uninitialised port. |
1df92640 XT |
553 | * Return accordingly. |
554 | */ | |
d41861ca | 555 | if (tty_hung_up_p(filp) || !tty_port_initialized(port)) { |
36c621d8 AC |
556 | if (port->flags & ASYNC_HUP_NOTIFY) |
557 | retval = -EAGAIN; | |
558 | else | |
559 | retval = -ERESTARTSYS; | |
560 | break; | |
561 | } | |
0eee50af JS |
562 | /* |
563 | * Probe the carrier. For devices with no carrier detect | |
564 | * tty_port_carrier_raised will always return true. | |
565 | * Never ask drivers if CLOCAL is set, this causes troubles | |
566 | * on some hardware. | |
567 | */ | |
fef062cb | 568 | if (do_clocal || tty_port_carrier_raised(port)) |
36c621d8 AC |
569 | break; |
570 | if (signal_pending(current)) { | |
571 | retval = -ERESTARTSYS; | |
572 | break; | |
573 | } | |
89c8d91e | 574 | tty_unlock(tty); |
36c621d8 | 575 | schedule(); |
89c8d91e | 576 | tty_lock(tty); |
36c621d8 | 577 | } |
3e3b5c08 | 578 | finish_wait(&port->open_wait, &wait); |
36c621d8 AC |
579 | |
580 | /* Update counts. A parallel hangup will have set count to zero and | |
1df92640 XT |
581 | * we must not mess that up further. |
582 | */ | |
36c621d8 AC |
583 | spin_lock_irqsave(&port->lock, flags); |
584 | if (!tty_hung_up_p(filp)) | |
585 | port->count++; | |
586 | port->blocked_open--; | |
36c621d8 | 587 | spin_unlock_irqrestore(&port->lock, flags); |
807c8d81 | 588 | if (retval == 0) |
9b5aa549 | 589 | tty_port_set_active(port, true); |
ecc2e05e | 590 | return retval; |
36c621d8 AC |
591 | } |
592 | EXPORT_SYMBOL(tty_port_block_til_ready); | |
593 | ||
b74414f5 JH |
594 | static void tty_port_drain_delay(struct tty_port *port, struct tty_struct *tty) |
595 | { | |
596 | unsigned int bps = tty_get_baud_rate(tty); | |
597 | long timeout; | |
598 | ||
599 | if (bps > 1200) { | |
600 | timeout = (HZ * 10 * port->drain_delay) / bps; | |
601 | timeout = max_t(long, timeout, HZ / 10); | |
602 | } else { | |
603 | timeout = 2 * HZ; | |
604 | } | |
605 | schedule_timeout_interruptible(timeout); | |
606 | } | |
607 | ||
3be491d7 JS |
608 | /** |
609 | * tty_port_close_start - helper for tty->ops->close, part 1/2 | |
610 | * @port: tty_port of the device | |
611 | * @tty: tty being closed | |
612 | * @filp: passed file pointer | |
613 | * | |
614 | * Decrements and checks open count. Flushes the port if this is the last | |
615 | * close. That means, dropping the data from the outpu buffer on the device and | |
616 | * waiting for sending logic to finish. The rest of close handling is performed | |
617 | * in tty_port_close_end(). | |
618 | * | |
619 | * Locking: Caller holds tty lock. | |
620 | * | |
621 | * Return: 1 if this is the last close, otherwise 0 | |
622 | */ | |
d774a56d AC |
623 | int tty_port_close_start(struct tty_port *port, |
624 | struct tty_struct *tty, struct file *filp) | |
a6614999 AC |
625 | { |
626 | unsigned long flags; | |
627 | ||
633caba8 | 628 | if (tty_hung_up_p(filp)) |
a6614999 | 629 | return 0; |
a6614999 | 630 | |
633caba8 | 631 | spin_lock_irqsave(&port->lock, flags); |
d774a56d | 632 | if (tty->count == 1 && port->count != 1) { |
339f36ba PH |
633 | tty_warn(tty, "%s: tty->count = 1 port count = %d\n", __func__, |
634 | port->count); | |
a6614999 AC |
635 | port->count = 1; |
636 | } | |
637 | if (--port->count < 0) { | |
339f36ba PH |
638 | tty_warn(tty, "%s: bad port count (%d)\n", __func__, |
639 | port->count); | |
a6614999 AC |
640 | port->count = 0; |
641 | } | |
642 | ||
643 | if (port->count) { | |
644 | spin_unlock_irqrestore(&port->lock, flags); | |
645 | return 0; | |
646 | } | |
a6614999 | 647 | spin_unlock_irqrestore(&port->lock, flags); |
0b2588ca | 648 | |
ddc7b758 PH |
649 | tty->closing = 1; |
650 | ||
d41861ca | 651 | if (tty_port_initialized(port)) { |
0b2588ca | 652 | /* Don't block on a stalled port, just pull the chain */ |
6e94dbc7 | 653 | if (tty->flow.tco_stopped) |
0b2588ca JH |
654 | tty_driver_flush_buffer(tty); |
655 | if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE) | |
79c1faa4 | 656 | tty_wait_until_sent(tty, port->closing_wait); |
0b2588ca JH |
657 | if (port->drain_delay) |
658 | tty_port_drain_delay(port, tty); | |
659 | } | |
e707c35c AC |
660 | /* Flush the ldisc buffering */ |
661 | tty_ldisc_flush(tty); | |
662 | ||
469d6d06 | 663 | /* Report to caller this is the last port reference */ |
a6614999 AC |
664 | return 1; |
665 | } | |
666 | EXPORT_SYMBOL(tty_port_close_start); | |
667 | ||
3be491d7 JS |
668 | /** |
669 | * tty_port_close_end - helper for tty->ops->close, part 2/2 | |
670 | * @port: tty_port of the device | |
671 | * @tty: tty being closed | |
672 | * | |
673 | * This is a continuation of the first part: tty_port_close_start(). This | |
674 | * should be called after turning off the device. It flushes the data from the | |
675 | * line discipline and delays the close by @port->close_delay. | |
676 | * | |
677 | * Locking: Caller holds tty lock. | |
678 | */ | |
a6614999 AC |
679 | void tty_port_close_end(struct tty_port *port, struct tty_struct *tty) |
680 | { | |
681 | unsigned long flags; | |
682 | ||
3f40f5b2 | 683 | tty_ldisc_flush(tty); |
a6614999 AC |
684 | tty->closing = 0; |
685 | ||
ddc7b758 PH |
686 | spin_lock_irqsave(&port->lock, flags); |
687 | ||
a6614999 AC |
688 | if (port->blocked_open) { |
689 | spin_unlock_irqrestore(&port->lock, flags); | |
5823323e PH |
690 | if (port->close_delay) |
691 | msleep_interruptible(jiffies_to_msecs(port->close_delay)); | |
a6614999 AC |
692 | spin_lock_irqsave(&port->lock, flags); |
693 | wake_up_interruptible(&port->open_wait); | |
694 | } | |
a6614999 | 695 | spin_unlock_irqrestore(&port->lock, flags); |
9b5aa549 | 696 | tty_port_set_active(port, false); |
a6614999 AC |
697 | } |
698 | EXPORT_SYMBOL(tty_port_close_end); | |
7ca0ff9a | 699 | |
cb6f6f98 JS |
700 | /** |
701 | * tty_port_close - generic tty->ops->close handler | |
702 | * @port: tty_port of the device | |
703 | * @tty: tty being closed | |
704 | * @filp: passed file pointer | |
705 | * | |
706 | * It is a generic helper to be used in driver's @tty->ops->close. It wraps a | |
707 | * sequence of tty_port_close_start(), tty_port_shutdown(), and | |
708 | * tty_port_close_end(). The latter two are called only if this is the last | |
709 | * close. See the respective functions for the details. | |
0733db91 | 710 | * |
cb6f6f98 | 711 | * Locking: Caller holds tty lock |
0733db91 | 712 | */ |
7ca0ff9a AC |
713 | void tty_port_close(struct tty_port *port, struct tty_struct *tty, |
714 | struct file *filp) | |
715 | { | |
716 | if (tty_port_close_start(port, tty, filp) == 0) | |
717 | return; | |
957dacae | 718 | tty_port_shutdown(port, tty); |
2a486026 CP |
719 | if (!port->console) |
720 | set_bit(TTY_IO_ERROR, &tty->flags); | |
7ca0ff9a AC |
721 | tty_port_close_end(port, tty); |
722 | tty_port_tty_set(port, NULL); | |
723 | } | |
724 | EXPORT_SYMBOL(tty_port_close); | |
64bc3979 | 725 | |
72a33bf5 JS |
726 | /** |
727 | * tty_port_install - generic tty->ops->install handler | |
728 | * @port: tty_port of the device | |
729 | * @driver: tty_driver for this device | |
730 | * @tty: tty to be installed | |
731 | * | |
cb6f6f98 JS |
732 | * It is the same as tty_standard_install() except the provided @port is linked |
733 | * to a concrete tty specified by @tty. Use this or tty_port_register_device() | |
734 | * (or both). Call tty_port_link_device() as a last resort. | |
72a33bf5 | 735 | */ |
695586ca JS |
736 | int tty_port_install(struct tty_port *port, struct tty_driver *driver, |
737 | struct tty_struct *tty) | |
738 | { | |
739 | tty->port = port; | |
740 | return tty_standard_install(driver, tty); | |
741 | } | |
742 | EXPORT_SYMBOL_GPL(tty_port_install); | |
743 | ||
cb6f6f98 JS |
744 | /** |
745 | * tty_port_open - generic tty->ops->open handler | |
746 | * @port: tty_port of the device | |
747 | * @tty: tty to be opened | |
748 | * @filp: passed file pointer | |
addd4672 | 749 | * |
cb6f6f98 JS |
750 | * It is a generic helper to be used in driver's @tty->ops->open. It activates |
751 | * the devices using @port->ops->activate if not active already. And waits for | |
752 | * the device to be ready using tty_port_block_til_ready() (e.g. raises | |
753 | * DTR/CTS and waits for carrier). | |
754 | * | |
d56738a3 JS |
755 | * Note that @port->ops->shutdown is not called when @port->ops->activate |
756 | * returns an error (on the contrary, @tty->ops->close is). | |
757 | * | |
cb6f6f98 | 758 | * Locking: Caller holds tty lock. |
addd4672 | 759 | * |
cb6f6f98 JS |
760 | * Note: may drop and reacquire tty lock (in tty_port_block_til_ready()) so |
761 | * @tty and @port may have changed state (eg., may be hung up now). | |
addd4672 | 762 | */ |
64bc3979 | 763 | int tty_port_open(struct tty_port *port, struct tty_struct *tty, |
d774a56d | 764 | struct file *filp) |
64bc3979 AC |
765 | { |
766 | spin_lock_irq(&port->lock); | |
e359a4e3 | 767 | ++port->count; |
64bc3979 AC |
768 | spin_unlock_irq(&port->lock); |
769 | tty_port_tty_set(port, tty); | |
770 | ||
771 | /* | |
772 | * Do the device-specific open only if the hardware isn't | |
773 | * already initialized. Serialize open and shutdown using the | |
774 | * port mutex. | |
775 | */ | |
776 | ||
777 | mutex_lock(&port->mutex); | |
778 | ||
d41861ca | 779 | if (!tty_port_initialized(port)) { |
a9a37ec3 | 780 | clear_bit(TTY_IO_ERROR, &tty->flags); |
0d3cb6f6 | 781 | if (port->ops->activate) { |
64bc3979 | 782 | int retval = port->ops->activate(port, tty); |
54ad59a2 | 783 | |
64bc3979 | 784 | if (retval) { |
d774a56d AC |
785 | mutex_unlock(&port->mutex); |
786 | return retval; | |
787 | } | |
788 | } | |
515be7ba | 789 | tty_port_set_initialized(port, true); |
64bc3979 AC |
790 | } |
791 | mutex_unlock(&port->mutex); | |
792 | return tty_port_block_til_ready(port, tty, filp); | |
793 | } | |
64bc3979 | 794 | EXPORT_SYMBOL(tty_port_open); |