n_hdlc: invert conditions in n_hdlc_tty_close and n_hdlc_tty_poll
[linux-block.git] / drivers / tty / n_hdlc.c
CommitLineData
e3b3d0f5 1// SPDX-License-Identifier: GPL-1.0+
1da177e4
LT
2/* generic HDLC line discipline for Linux
3 *
4 * Written by Paul Fulghum paulkf@microgate.com
5 * for Microgate Corporation
6 *
7 * Microgate and SyncLink are registered trademarks of Microgate Corporation
8 *
9 * Adapted from ppp.c, written by Michael Callahan <callahan@maths.ox.ac.uk>,
10 * Al Longyear <longyear@netcom.com>,
11 * Paul Mackerras <Paul.Mackerras@cs.anu.edu.au>
12 *
13 * Original release 01/11/99
1da177e4 14 *
1da177e4
LT
15 * This module implements the tty line discipline N_HDLC for use with
16 * tty device drivers that support bit-synchronous HDLC communications.
17 *
18 * All HDLC data is frame oriented which means:
19 *
20 * 1. tty write calls represent one complete transmit frame of data
21 * The device driver should accept the complete frame or none of
22 * the frame (busy) in the write method. Each write call should have
23 * a byte count in the range of 2-65535 bytes (2 is min HDLC frame
24 * with 1 addr byte and 1 ctrl byte). The max byte count of 65535
25 * should include any crc bytes required. For example, when using
26 * CCITT CRC32, 4 crc bytes are required, so the maximum size frame
27 * the application may transmit is limited to 65531 bytes. For CCITT
28 * CRC16, the maximum application frame size would be 65533.
29 *
30 *
31 * 2. receive callbacks from the device driver represents
32 * one received frame. The device driver should bypass
33 * the tty flip buffer and call the line discipline receive
34 * callback directly to avoid fragmenting or concatenating
35 * multiple frames into a single receive callback.
36 *
37 * The HDLC line discipline queues the receive frames in separate
38 * buffers so complete receive frames can be returned by the
39 * tty read calls.
40 *
41 * 3. tty read calls returns an entire frame of data or nothing.
42 *
43 * 4. all send and receive data is considered raw. No processing
44 * or translation is performed by the line discipline, regardless
45 * of the tty flags
46 *
47 * 5. When line discipline is queried for the amount of receive
48 * data available (FIOC), 0 is returned if no data available,
49 * otherwise the count of the next available frame is returned.
50 * (instead of the sum of all received frame counts).
51 *
52 * These conventions allow the standard tty programming interface
53 * to be used for synchronous HDLC applications when used with
54 * this line discipline (or another line discipline that is frame
55 * oriented such as N_PPP).
56 *
57 * The SyncLink driver (synclink.c) implements both asynchronous
58 * (using standard line discipline N_TTY) and synchronous HDLC
59 * (using N_HDLC) communications, with the latter using the above
60 * conventions.
61 *
62 * This implementation is very basic and does not maintain
63 * any statistics. The main point is to enforce the raw data
64 * and frame orientation of HDLC communications.
65 *
66 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
67 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
68 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
69 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
70 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
71 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
72 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
74 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
75 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
76 * OF THE POSSIBILITY OF SUCH DAMAGE.
77 */
78
79#define HDLC_MAGIC 0x239e
1da177e4 80
1da177e4
LT
81#include <linux/module.h>
82#include <linux/init.h>
83#include <linux/kernel.h>
84#include <linux/sched.h>
85#include <linux/types.h>
86#include <linux/fcntl.h>
87#include <linux/interrupt.h>
88#include <linux/ptrace.h>
89
1da177e4
LT
90#include <linux/poll.h>
91#include <linux/in.h>
92#include <linux/ioctl.h>
93#include <linux/slab.h>
94#include <linux/tty.h>
95#include <linux/errno.h>
96#include <linux/string.h> /* used in new tty drivers */
97#include <linux/signal.h> /* used in new tty drivers */
98#include <linux/if.h>
99#include <linux/bitops.h>
100
1da177e4 101#include <asm/termios.h>
7c0f6ba6 102#include <linux/uaccess.h>
1da177e4
LT
103
104/*
105 * Buffers for individual HDLC frames
106 */
107#define MAX_HDLC_FRAME_SIZE 65535
108#define DEFAULT_RX_BUF_COUNT 10
109#define MAX_RX_BUF_COUNT 60
be10eb75 110#define DEFAULT_TX_BUF_COUNT 3
1da177e4
LT
111
112struct n_hdlc_buf {
82f2341c 113 struct list_head list_item;
1da177e4 114 int count;
85f4c951 115 char buf[];
1da177e4
LT
116};
117
1da177e4 118struct n_hdlc_buf_list {
82f2341c 119 struct list_head list;
1da177e4
LT
120 int count;
121 spinlock_t spinlock;
122};
123
124/**
125 * struct n_hdlc - per device instance data structure
126 * @magic - magic value for structure
127 * @flags - miscellaneous control flags
128 * @tty - ptr to TTY structure
129 * @backup_tty - TTY to use if tty gets closed
130 * @tbusy - reentrancy flag for tx wakeup code
131 * @woke_up - FIXME: describe this field
1da177e4
LT
132 * @tx_buf_list - list of pending transmit frame buffers
133 * @rx_buf_list - list of received frame buffers
134 * @tx_free_buf_list - list unused transmit frame buffers
135 * @rx_free_buf_list - list unused received frame buffers
136 */
137struct n_hdlc {
138 int magic;
139 __u32 flags;
140 struct tty_struct *tty;
141 struct tty_struct *backup_tty;
142 int tbusy;
143 int woke_up;
1da177e4
LT
144 struct n_hdlc_buf_list tx_buf_list;
145 struct n_hdlc_buf_list rx_buf_list;
146 struct n_hdlc_buf_list tx_free_buf_list;
147 struct n_hdlc_buf_list rx_free_buf_list;
148};
149
150/*
151 * HDLC buffer list manipulation functions
152 */
82f2341c
AP
153static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
154 struct n_hdlc_buf *buf);
1da177e4
LT
155static void n_hdlc_buf_put(struct n_hdlc_buf_list *list,
156 struct n_hdlc_buf *buf);
157static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *list);
158
159/* Local functions */
160
161static struct n_hdlc *n_hdlc_alloc (void);
162
1da177e4
LT
163/* max frame size for memory allocations */
164static int maxframe = 4096;
165
166/* TTY callbacks */
167
168static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
169 __u8 __user *buf, size_t nr);
170static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
171 const unsigned char *buf, size_t nr);
172static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
173 unsigned int cmd, unsigned long arg);
afc9a42b 174static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
1da177e4
LT
175 poll_table *wait);
176static int n_hdlc_tty_open(struct tty_struct *tty);
177static void n_hdlc_tty_close(struct tty_struct *tty);
55db4c64
LT
178static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
179 char *fp, int count);
1da177e4
LT
180static void n_hdlc_tty_wakeup(struct tty_struct *tty);
181
1da177e4
LT
182#define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
183#define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
184
be10eb75
PF
185static void flush_rx_queue(struct tty_struct *tty)
186{
187 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
188 struct n_hdlc_buf *buf;
189
190 while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
191 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
192}
193
194static void flush_tx_queue(struct tty_struct *tty)
195{
196 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
197 struct n_hdlc_buf *buf;
be10eb75
PF
198
199 while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
200 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
be10eb75
PF
201}
202
a352def2 203static struct tty_ldisc_ops n_hdlc_ldisc = {
1da177e4
LT
204 .owner = THIS_MODULE,
205 .magic = TTY_LDISC_MAGIC,
206 .name = "hdlc",
207 .open = n_hdlc_tty_open,
208 .close = n_hdlc_tty_close,
209 .read = n_hdlc_tty_read,
210 .write = n_hdlc_tty_write,
211 .ioctl = n_hdlc_tty_ioctl,
212 .poll = n_hdlc_tty_poll,
213 .receive_buf = n_hdlc_tty_receive,
1da177e4 214 .write_wakeup = n_hdlc_tty_wakeup,
be10eb75 215 .flush_buffer = flush_rx_queue,
1da177e4
LT
216};
217
30fafd92
JS
218static void n_hdlc_free_buf_list(struct n_hdlc_buf_list *list)
219{
220 struct n_hdlc_buf *buf;
221
222 do {
223 buf = n_hdlc_buf_get(list);
224 kfree(buf);
225 } while (buf);
226}
227
1da177e4
LT
228/**
229 * n_hdlc_release - release an n_hdlc per device line discipline info structure
230 * @n_hdlc - per device line discipline info structure
231 */
232static void n_hdlc_release(struct n_hdlc *n_hdlc)
233{
234 struct tty_struct *tty = n_hdlc2tty (n_hdlc);
66c3bdf1 235
1da177e4
LT
236 /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
237 wake_up_interruptible (&tty->read_wait);
238 wake_up_interruptible (&tty->write_wait);
239
827afdf0 240 if (tty->disc_data == n_hdlc)
1da177e4
LT
241 tty->disc_data = NULL; /* Break the tty->n_hdlc link */
242
30fafd92
JS
243 n_hdlc_free_buf_list(&n_hdlc->rx_free_buf_list);
244 n_hdlc_free_buf_list(&n_hdlc->tx_free_buf_list);
245 n_hdlc_free_buf_list(&n_hdlc->rx_buf_list);
246 n_hdlc_free_buf_list(&n_hdlc->tx_buf_list);
1da177e4 247 kfree(n_hdlc);
1da177e4
LT
248} /* end of n_hdlc_release() */
249
250/**
251 * n_hdlc_tty_close - line discipline close
252 * @tty - pointer to tty info structure
253 *
254 * Called when the line discipline is changed to something
255 * else, the tty is closed, or the tty detects a hangup.
256 */
257static void n_hdlc_tty_close(struct tty_struct *tty)
258{
259 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
260
5f289514
JS
261 if (!n_hdlc)
262 return;
263
264 if (n_hdlc->magic != HDLC_MAGIC) {
265 printk(KERN_WARNING "n_hdlc: trying to close unopened tty!\n");
266 return;
267 }
1da177e4 268#if defined(TTY_NO_WRITE_SPLIT)
5f289514 269 clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
1da177e4 270#endif
5f289514
JS
271 tty->disc_data = NULL;
272 if (tty == n_hdlc->backup_tty)
273 n_hdlc->backup_tty = NULL;
274 if (tty != n_hdlc->tty)
275 return;
276 if (n_hdlc->backup_tty) {
277 n_hdlc->tty = n_hdlc->backup_tty;
278 } else {
279 n_hdlc_release (n_hdlc);
1da177e4 280 }
1da177e4
LT
281} /* end of n_hdlc_tty_close() */
282
283/**
284 * n_hdlc_tty_open - called when line discipline changed to n_hdlc
285 * @tty - pointer to tty info structure
286 *
287 * Returns 0 if success, otherwise error code
288 */
289static int n_hdlc_tty_open (struct tty_struct *tty)
290{
291 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
292
f3c2e277
JS
293 pr_debug("%s(%d)%s() called (device=%s)\n",
294 __FILE__, __LINE__, __func__, tty->name);
295
1da177e4
LT
296 /* There should not be an existing table for this slot. */
297 if (n_hdlc) {
298 printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
299 return -EEXIST;
300 }
301
302 n_hdlc = n_hdlc_alloc();
303 if (!n_hdlc) {
304 printk (KERN_ERR "n_hdlc_alloc failed\n");
305 return -ENFILE;
306 }
307
308 tty->disc_data = n_hdlc;
309 n_hdlc->tty = tty;
33f0f88f 310 tty->receive_room = 65536;
1da177e4
LT
311
312#if defined(TTY_NO_WRITE_SPLIT)
313 /* change tty_io write() to not split large writes into 8K chunks */
314 set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
315#endif
316
be10eb75 317 /* flush receive data from driver */
f34d7a5b 318 tty_driver_flush_buffer(tty);
66c3bdf1 319
1da177e4
LT
320 return 0;
321
322} /* end of n_tty_hdlc_open() */
323
324/**
325 * n_hdlc_send_frames - send frames on pending send buffer list
326 * @n_hdlc - pointer to ldisc instance data
327 * @tty - pointer to tty instance data
328 *
329 * Send frames on pending send buffer list until the driver does not accept a
330 * frame (busy) this function is called after adding a frame to the send buffer
331 * list and by the tty wakeup callback.
332 */
333static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
334{
335 register int actual;
336 unsigned long flags;
337 struct n_hdlc_buf *tbuf;
338
1da177e4
LT
339 check_again:
340
341 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
342 if (n_hdlc->tbusy) {
343 n_hdlc->woke_up = 1;
344 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
345 return;
346 }
347 n_hdlc->tbusy = 1;
348 n_hdlc->woke_up = 0;
349 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
350
82f2341c 351 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
1da177e4 352 while (tbuf) {
f3c2e277
JS
353 pr_debug("%s(%d)sending frame %p, count=%d\n",
354 __FILE__, __LINE__, tbuf, tbuf->count);
355
1da177e4 356 /* Send the next block of data to device */
7962fce9 357 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
f34d7a5b 358 actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
b0fed314
JS
359
360 /* rollback was possible and has been done */
361 if (actual == -ERESTARTSYS) {
82f2341c 362 n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
b0fed314
JS
363 break;
364 }
1da177e4
LT
365 /* if transmit error, throw frame away by */
366 /* pretending it was accepted by driver */
367 if (actual < 0)
368 actual = tbuf->count;
369
370 if (actual == tbuf->count) {
f3c2e277
JS
371 pr_debug("%s(%d)frame %p completed\n",
372 __FILE__, __LINE__, tbuf);
373
1da177e4
LT
374 /* free current transmit buffer */
375 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
82f2341c 376
1da177e4
LT
377 /* wait up sleeping writers */
378 wake_up_interruptible(&tty->write_wait);
379
380 /* get next pending transmit buffer */
381 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
382 } else {
f3c2e277
JS
383 pr_debug("%s(%d)frame %p pending\n",
384 __FILE__, __LINE__, tbuf);
82f2341c
AP
385
386 /*
387 * the buffer was not accepted by driver,
388 * return it back into tx queue
389 */
390 n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
1da177e4
LT
391 break;
392 }
393 }
394
395 if (!tbuf)
7962fce9 396 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1da177e4
LT
397
398 /* Clear the re-entry flag */
399 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
400 n_hdlc->tbusy = 0;
401 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
402
403 if (n_hdlc->woke_up)
404 goto check_again;
1da177e4
LT
405} /* end of n_hdlc_send_frames() */
406
407/**
408 * n_hdlc_tty_wakeup - Callback for transmit wakeup
409 * @tty - pointer to associated tty instance data
410 *
411 * Called when low level device driver can accept more send data.
412 */
413static void n_hdlc_tty_wakeup(struct tty_struct *tty)
414{
415 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
416
1da177e4
LT
417 if (!n_hdlc)
418 return;
419
420 if (tty != n_hdlc->tty) {
7962fce9 421 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1da177e4
LT
422 return;
423 }
424
425 n_hdlc_send_frames (n_hdlc, tty);
426
427} /* end of n_hdlc_tty_wakeup() */
428
1da177e4
LT
429/**
430 * n_hdlc_tty_receive - Called by tty driver when receive data is available
431 * @tty - pointer to tty instance data
432 * @data - pointer to received data
433 * @flags - pointer to flags for data
434 * @count - count of received data in bytes
435 *
436 * Called by tty low level driver when receive data is available. Data is
437 * interpreted as one HDLC frame.
438 */
55db4c64
LT
439static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
440 char *flags, int count)
1da177e4
LT
441{
442 register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
443 register struct n_hdlc_buf *buf;
444
f3c2e277
JS
445 pr_debug("%s(%d)%s() called count=%d\n",
446 __FILE__, __LINE__, __func__, count);
447
1da177e4 448 /* This can happen if stuff comes in on the backup tty */
a01e035e 449 if (!n_hdlc || tty != n_hdlc->tty)
55db4c64 450 return;
1da177e4
LT
451
452 /* verify line is using HDLC discipline */
453 if (n_hdlc->magic != HDLC_MAGIC) {
454 printk("%s(%d) line not using HDLC discipline\n",
455 __FILE__,__LINE__);
55db4c64 456 return;
1da177e4
LT
457 }
458
459 if ( count>maxframe ) {
f3c2e277
JS
460 pr_debug("%s(%d) rx count>maxframesize, data discarded\n",
461 __FILE__, __LINE__);
55db4c64 462 return;
1da177e4
LT
463 }
464
465 /* get a free HDLC buffer */
466 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
467 if (!buf) {
468 /* no buffers in free list, attempt to allocate another rx buffer */
469 /* unless the maximum count has been reached */
470 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
85f4c951
GS
471 buf = kmalloc(struct_size(buf, buf, maxframe),
472 GFP_ATOMIC);
1da177e4
LT
473 }
474
475 if (!buf) {
f3c2e277
JS
476 pr_debug("%s(%d) no more rx buffers, data discarded\n",
477 __FILE__, __LINE__);
55db4c64 478 return;
1da177e4
LT
479 }
480
481 /* copy received data to HDLC buffer */
482 memcpy(buf->buf,data,count);
483 buf->count=count;
484
485 /* add HDLC buffer to list of received frames */
486 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
487
488 /* wake up any blocked reads and perform async signalling */
489 wake_up_interruptible (&tty->read_wait);
490 if (n_hdlc->tty->fasync != NULL)
491 kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
492
493} /* end of n_hdlc_tty_receive() */
494
495/**
4a4efbde 496 * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
1da177e4
LT
497 * @tty - pointer to tty instance data
498 * @file - pointer to open file object
499 * @buf - pointer to returned data buffer
500 * @nr - size of returned data buffer
501 *
502 * Returns the number of bytes returned or error code.
503 */
504static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
505 __u8 __user *buf, size_t nr)
506{
507 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
1035b63d 508 int ret = 0;
1da177e4 509 struct n_hdlc_buf *rbuf;
1035b63d 510 DECLARE_WAITQUEUE(wait, current);
1da177e4 511
1da177e4
LT
512 /* Validate the pointers */
513 if (!n_hdlc)
514 return -EIO;
515
516 /* verify user access to buffer */
96d4f267 517 if (!access_ok(buf, nr)) {
1da177e4
LT
518 printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
519 "buffer\n", __FILE__, __LINE__);
520 return -EFAULT;
521 }
522
1035b63d 523 add_wait_queue(&tty->read_wait, &wait);
04f378b1 524
1da177e4 525 for (;;) {
0f40fbbc 526 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1035b63d
PF
527 ret = -EIO;
528 break;
04f378b1 529 }
1035b63d
PF
530 if (tty_hung_up_p(file))
531 break;
1da177e4 532
1035b63d 533 set_current_state(TASK_INTERRUPTIBLE);
1da177e4
LT
534
535 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
1035b63d
PF
536 if (rbuf) {
537 if (rbuf->count > nr) {
538 /* too large for caller's buffer */
539 ret = -EOVERFLOW;
540 } else {
fc01d8c6 541 __set_current_state(TASK_RUNNING);
1035b63d
PF
542 if (copy_to_user(buf, rbuf->buf, rbuf->count))
543 ret = -EFAULT;
544 else
545 ret = rbuf->count;
546 }
547
548 if (n_hdlc->rx_free_buf_list.count >
549 DEFAULT_RX_BUF_COUNT)
550 kfree(rbuf);
551 else
552 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
1da177e4 553 break;
1035b63d 554 }
1da177e4
LT
555
556 /* no data */
c96cf923 557 if (tty_io_nonblock(tty, file)) {
1035b63d
PF
558 ret = -EAGAIN;
559 break;
04f378b1 560 }
1035b63d
PF
561
562 schedule();
563
04f378b1 564 if (signal_pending(current)) {
1035b63d
PF
565 ret = -EINTR;
566 break;
04f378b1 567 }
1da177e4 568 }
1035b63d
PF
569
570 remove_wait_queue(&tty->read_wait, &wait);
571 __set_current_state(TASK_RUNNING);
572
1da177e4
LT
573 return ret;
574
575} /* end of n_hdlc_tty_read() */
576
577/**
578 * n_hdlc_tty_write - write a single frame of data to device
579 * @tty - pointer to associated tty device instance data
580 * @file - pointer to file object data
581 * @data - pointer to transmit data (one frame)
582 * @count - size of transmit frame in bytes
583 *
584 * Returns the number of bytes written (or error code).
585 */
586static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
587 const unsigned char *data, size_t count)
588{
589 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
590 int error = 0;
591 DECLARE_WAITQUEUE(wait, current);
592 struct n_hdlc_buf *tbuf;
593
f3c2e277
JS
594 pr_debug("%s(%d)%s() called count=%zd\n", __FILE__, __LINE__, __func__,
595 count);
596
1da177e4
LT
597 /* Verify pointers */
598 if (!n_hdlc)
599 return -EIO;
600
601 if (n_hdlc->magic != HDLC_MAGIC)
602 return -EIO;
603
604 /* verify frame size */
605 if (count > maxframe ) {
f3c2e277
JS
606 pr_debug("%s: truncating user packet from %zu to %d\n",
607 __func__, count, maxframe);
1da177e4
LT
608 count = maxframe;
609 }
610
611 add_wait_queue(&tty->write_wait, &wait);
1035b63d
PF
612
613 for (;;) {
614 set_current_state(TASK_INTERRUPTIBLE);
1da177e4 615
1035b63d
PF
616 tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
617 if (tbuf)
618 break;
619
c96cf923 620 if (tty_io_nonblock(tty, file)) {
c72f527c
PF
621 error = -EAGAIN;
622 break;
623 }
1da177e4
LT
624 schedule();
625
626 n_hdlc = tty2n_hdlc (tty);
627 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
628 tty != n_hdlc->tty) {
629 printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
630 error = -EIO;
631 break;
632 }
633
634 if (signal_pending(current)) {
635 error = -EINTR;
636 break;
637 }
638 }
639
1035b63d 640 __set_current_state(TASK_RUNNING);
1da177e4
LT
641 remove_wait_queue(&tty->write_wait, &wait);
642
643 if (!error) {
644 /* Retrieve the user's buffer */
645 memcpy(tbuf->buf, data, count);
646
647 /* Send the data */
648 tbuf->count = error = count;
649 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
650 n_hdlc_send_frames(n_hdlc,tty);
651 }
1035b63d 652
1da177e4
LT
653 return error;
654
655} /* end of n_hdlc_tty_write() */
656
657/**
658 * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
659 * @tty - pointer to tty instance data
660 * @file - pointer to open file object for device
661 * @cmd - IOCTL command code
662 * @arg - argument for IOCTL call (cmd dependent)
663 *
664 * Returns command dependent result.
665 */
666static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
667 unsigned int cmd, unsigned long arg)
668{
669 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
670 int error = 0;
671 int count;
672 unsigned long flags;
82f2341c
AP
673 struct n_hdlc_buf *buf = NULL;
674
f3c2e277
JS
675 pr_debug("%s(%d)%s() called %d\n", __FILE__, __LINE__, __func__, cmd);
676
1da177e4
LT
677 /* Verify the status of the device */
678 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
679 return -EBADF;
680
681 switch (cmd) {
682 case FIONREAD:
683 /* report count of read data available */
684 /* in next available frame (if any) */
685 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
82f2341c
AP
686 buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list,
687 struct n_hdlc_buf, list_item);
688 if (buf)
689 count = buf->count;
1da177e4
LT
690 else
691 count = 0;
692 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
693 error = put_user(count, (int __user *)arg);
694 break;
695
696 case TIOCOUTQ:
697 /* get the pending tx byte count in the driver */
f34d7a5b 698 count = tty_chars_in_buffer(tty);
1da177e4
LT
699 /* add size of next output frame in queue */
700 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
82f2341c
AP
701 buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list,
702 struct n_hdlc_buf, list_item);
703 if (buf)
704 count += buf->count;
1da177e4
LT
705 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
706 error = put_user(count, (int __user *)arg);
707 break;
708
be10eb75
PF
709 case TCFLSH:
710 switch (arg) {
711 case TCIOFLUSH:
712 case TCOFLUSH:
713 flush_tx_queue(tty);
714 }
361bf8a9 715 /* fall through - to default */
be10eb75 716
1da177e4 717 default:
47afa7a5 718 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1da177e4
LT
719 break;
720 }
721 return error;
722
723} /* end of n_hdlc_tty_ioctl() */
724
725/**
726 * n_hdlc_tty_poll - TTY callback for poll system call
727 * @tty - pointer to tty instance data
728 * @filp - pointer to open file object for device
729 * @poll_table - wait queue for operations
730 *
731 * Determine which operations (read/write) will not block and return info
732 * to caller.
733 * Returns a bit mask containing info on which ops will not block.
734 */
afc9a42b 735static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
1da177e4
LT
736 poll_table *wait)
737{
738 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
afc9a42b 739 __poll_t mask = 0;
1da177e4 740
5f289514
JS
741 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC || tty != n_hdlc->tty)
742 return 0;
743
744 /*
745 * queue the current process into any wait queue that may awaken in the
746 * future (read and write)
747 */
748 poll_wait(filp, &tty->read_wait, wait);
749 poll_wait(filp, &tty->write_wait, wait);
750
751 /* set bits for operations that won't block */
752 if (!list_empty(&n_hdlc->rx_buf_list.list))
753 mask |= EPOLLIN | EPOLLRDNORM; /* readable */
754 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
755 mask |= EPOLLHUP;
756 if (tty_hung_up_p(filp))
757 mask |= EPOLLHUP;
758 if (!tty_is_writelocked(tty) &&
759 !list_empty(&n_hdlc->tx_free_buf_list.list))
760 mask |= EPOLLOUT | EPOLLWRNORM; /* writable */
761
1da177e4
LT
762 return mask;
763} /* end of n_hdlc_tty_poll() */
764
765/**
766 * n_hdlc_alloc - allocate an n_hdlc instance data structure
767 *
768 * Returns a pointer to newly created structure if success, otherwise %NULL
769 */
770static struct n_hdlc *n_hdlc_alloc(void)
771{
772 struct n_hdlc_buf *buf;
773 int i;
8e25f8ce 774 struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL);
1da177e4
LT
775
776 if (!n_hdlc)
777 return NULL;
778
e9b736d8
JS
779 spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock);
780 spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock);
781 spin_lock_init(&n_hdlc->rx_buf_list.spinlock);
782 spin_lock_init(&n_hdlc->tx_buf_list.spinlock);
82f2341c
AP
783
784 INIT_LIST_HEAD(&n_hdlc->rx_free_buf_list.list);
785 INIT_LIST_HEAD(&n_hdlc->tx_free_buf_list.list);
786 INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list);
787 INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list);
788
1da177e4
LT
789 /* allocate free rx buffer list */
790 for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
85f4c951 791 buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL);
1da177e4
LT
792 if (buf)
793 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
f3c2e277
JS
794 else
795 pr_debug("%s(%d)%s(), kmalloc() failed for rx buffer %d\n",
796 __FILE__, __LINE__, __func__, i);
1da177e4
LT
797 }
798
799 /* allocate free tx buffer list */
800 for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
85f4c951 801 buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL);
1da177e4
LT
802 if (buf)
803 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
f3c2e277
JS
804 else
805 pr_debug("%s(%d)%s(), kmalloc() failed for tx buffer %d\n",
806 __FILE__, __LINE__, __func__, i);
1da177e4
LT
807 }
808
809 /* Initialize the control block */
810 n_hdlc->magic = HDLC_MAGIC;
811 n_hdlc->flags = 0;
812
813 return n_hdlc;
814
815} /* end of n_hdlc_alloc() */
816
82f2341c
AP
817/**
818 * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
819 * @buf_list - pointer to the buffer list
820 * @buf - pointer to the buffer
821 */
822static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
823 struct n_hdlc_buf *buf)
824{
825 unsigned long flags;
826
827 spin_lock_irqsave(&buf_list->spinlock, flags);
828
829 list_add(&buf->list_item, &buf_list->list);
830 buf_list->count++;
831
832 spin_unlock_irqrestore(&buf_list->spinlock, flags);
833}
834
1da177e4
LT
835/**
836 * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
82f2341c 837 * @buf_list - pointer to buffer list
1da177e4
LT
838 * @buf - pointer to buffer
839 */
82f2341c 840static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
1da177e4
LT
841 struct n_hdlc_buf *buf)
842{
843 unsigned long flags;
82f2341c
AP
844
845 spin_lock_irqsave(&buf_list->spinlock, flags);
846
847 list_add_tail(&buf->list_item, &buf_list->list);
848 buf_list->count++;
849
850 spin_unlock_irqrestore(&buf_list->spinlock, flags);
1da177e4
LT
851} /* end of n_hdlc_buf_put() */
852
853/**
854 * n_hdlc_buf_get - remove and return an HDLC buffer from list
82f2341c 855 * @buf_list - pointer to HDLC buffer list
1da177e4
LT
856 *
857 * Remove and return an HDLC buffer from the head of the specified HDLC buffer
858 * list.
859 * Returns a pointer to HDLC buffer if available, otherwise %NULL.
860 */
82f2341c 861static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list)
1da177e4
LT
862{
863 unsigned long flags;
864 struct n_hdlc_buf *buf;
82f2341c
AP
865
866 spin_lock_irqsave(&buf_list->spinlock, flags);
867
868 buf = list_first_entry_or_null(&buf_list->list,
869 struct n_hdlc_buf, list_item);
1da177e4 870 if (buf) {
82f2341c
AP
871 list_del(&buf->list_item);
872 buf_list->count--;
1da177e4 873 }
82f2341c
AP
874
875 spin_unlock_irqrestore(&buf_list->spinlock, flags);
1da177e4 876 return buf;
1da177e4
LT
877} /* end of n_hdlc_buf_get() */
878
1da177e4
LT
879static int __init n_hdlc_init(void)
880{
881 int status;
882
883 /* range check maxframe arg */
c549725f 884 maxframe = clamp(maxframe, 4096, MAX_HDLC_FRAME_SIZE);
1da177e4 885
1da177e4
LT
886 status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
887 if (!status)
cda3756c
JS
888 pr_info("N_HDLC line discipline registered with maxframe=%d\n",
889 maxframe);
1da177e4 890 else
cda3756c
JS
891 pr_err("N_HDLC: error registering line discipline: %d\n",
892 status);
1da177e4 893
1da177e4
LT
894 return status;
895
896} /* end of init_module() */
897
1da177e4
LT
898static void __exit n_hdlc_exit(void)
899{
900 /* Release tty registration of line discipline */
64ccd715 901 int status = tty_unregister_ldisc(N_HDLC);
1da177e4
LT
902
903 if (status)
aebe5fc3
JS
904 pr_err("N_HDLC: can't unregister line discipline (err = %d)\n",
905 status);
1da177e4 906 else
aebe5fc3 907 pr_info("N_HDLC: line discipline unregistered\n");
1da177e4
LT
908}
909
910module_init(n_hdlc_init);
911module_exit(n_hdlc_exit);
912
913MODULE_LICENSE("GPL");
914MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
1da177e4
LT
915module_param(maxframe, int, 0);
916MODULE_ALIAS_LDISC(N_HDLC);