n_hdlc: remove unused macros
[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
163/* debug level can be set by insmod for debugging purposes */
164#define DEBUG_LEVEL_INFO 1
165static int debuglevel;
166
167/* max frame size for memory allocations */
168static int maxframe = 4096;
169
170/* TTY callbacks */
171
172static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
173 __u8 __user *buf, size_t nr);
174static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
175 const unsigned char *buf, size_t nr);
176static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
177 unsigned int cmd, unsigned long arg);
afc9a42b 178static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
1da177e4
LT
179 poll_table *wait);
180static int n_hdlc_tty_open(struct tty_struct *tty);
181static void n_hdlc_tty_close(struct tty_struct *tty);
55db4c64
LT
182static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *cp,
183 char *fp, int count);
1da177e4
LT
184static void n_hdlc_tty_wakeup(struct tty_struct *tty);
185
1da177e4
LT
186#define tty2n_hdlc(tty) ((struct n_hdlc *) ((tty)->disc_data))
187#define n_hdlc2tty(n_hdlc) ((n_hdlc)->tty)
188
be10eb75
PF
189static void flush_rx_queue(struct tty_struct *tty)
190{
191 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
192 struct n_hdlc_buf *buf;
193
194 while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list)))
195 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf);
196}
197
198static void flush_tx_queue(struct tty_struct *tty)
199{
200 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
201 struct n_hdlc_buf *buf;
be10eb75
PF
202
203 while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list)))
204 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf);
be10eb75
PF
205}
206
a352def2 207static struct tty_ldisc_ops n_hdlc_ldisc = {
1da177e4
LT
208 .owner = THIS_MODULE,
209 .magic = TTY_LDISC_MAGIC,
210 .name = "hdlc",
211 .open = n_hdlc_tty_open,
212 .close = n_hdlc_tty_close,
213 .read = n_hdlc_tty_read,
214 .write = n_hdlc_tty_write,
215 .ioctl = n_hdlc_tty_ioctl,
216 .poll = n_hdlc_tty_poll,
217 .receive_buf = n_hdlc_tty_receive,
1da177e4 218 .write_wakeup = n_hdlc_tty_wakeup,
be10eb75 219 .flush_buffer = flush_rx_queue,
1da177e4
LT
220};
221
222/**
223 * n_hdlc_release - release an n_hdlc per device line discipline info structure
224 * @n_hdlc - per device line discipline info structure
225 */
226static void n_hdlc_release(struct n_hdlc *n_hdlc)
227{
228 struct tty_struct *tty = n_hdlc2tty (n_hdlc);
229 struct n_hdlc_buf *buf;
66c3bdf1 230
1da177e4
LT
231 /* Ensure that the n_hdlcd process is not hanging on select()/poll() */
232 wake_up_interruptible (&tty->read_wait);
233 wake_up_interruptible (&tty->write_wait);
234
827afdf0 235 if (tty->disc_data == n_hdlc)
1da177e4
LT
236 tty->disc_data = NULL; /* Break the tty->n_hdlc link */
237
238 /* Release transmit and receive buffers */
239 for(;;) {
240 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
241 if (buf) {
242 kfree(buf);
243 } else
244 break;
245 }
246 for(;;) {
247 buf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
248 if (buf) {
249 kfree(buf);
250 } else
251 break;
252 }
253 for(;;) {
254 buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
255 if (buf) {
256 kfree(buf);
257 } else
258 break;
259 }
260 for(;;) {
261 buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
262 if (buf) {
263 kfree(buf);
264 } else
265 break;
266 }
1da177e4
LT
267 kfree(n_hdlc);
268
269} /* end of n_hdlc_release() */
270
271/**
272 * n_hdlc_tty_close - line discipline close
273 * @tty - pointer to tty info structure
274 *
275 * Called when the line discipline is changed to something
276 * else, the tty is closed, or the tty detects a hangup.
277 */
278static void n_hdlc_tty_close(struct tty_struct *tty)
279{
280 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
281
1da177e4
LT
282 if (n_hdlc != NULL) {
283 if (n_hdlc->magic != HDLC_MAGIC) {
284 printk (KERN_WARNING"n_hdlc: trying to close unopened tty!\n");
285 return;
286 }
287#if defined(TTY_NO_WRITE_SPLIT)
288 clear_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
289#endif
290 tty->disc_data = NULL;
291 if (tty == n_hdlc->backup_tty)
292 n_hdlc->backup_tty = NULL;
293 if (tty != n_hdlc->tty)
294 return;
295 if (n_hdlc->backup_tty) {
296 n_hdlc->tty = n_hdlc->backup_tty;
297 } else {
298 n_hdlc_release (n_hdlc);
299 }
300 }
1da177e4
LT
301} /* end of n_hdlc_tty_close() */
302
303/**
304 * n_hdlc_tty_open - called when line discipline changed to n_hdlc
305 * @tty - pointer to tty info structure
306 *
307 * Returns 0 if success, otherwise error code
308 */
309static int n_hdlc_tty_open (struct tty_struct *tty)
310{
311 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
312
313 if (debuglevel >= DEBUG_LEVEL_INFO)
314 printk("%s(%d)n_hdlc_tty_open() called (device=%s)\n",
315 __FILE__,__LINE__,
316 tty->name);
317
318 /* There should not be an existing table for this slot. */
319 if (n_hdlc) {
320 printk (KERN_ERR"n_hdlc_tty_open:tty already associated!\n" );
321 return -EEXIST;
322 }
323
324 n_hdlc = n_hdlc_alloc();
325 if (!n_hdlc) {
326 printk (KERN_ERR "n_hdlc_alloc failed\n");
327 return -ENFILE;
328 }
329
330 tty->disc_data = n_hdlc;
331 n_hdlc->tty = tty;
33f0f88f 332 tty->receive_room = 65536;
1da177e4
LT
333
334#if defined(TTY_NO_WRITE_SPLIT)
335 /* change tty_io write() to not split large writes into 8K chunks */
336 set_bit(TTY_NO_WRITE_SPLIT,&tty->flags);
337#endif
338
be10eb75 339 /* flush receive data from driver */
f34d7a5b 340 tty_driver_flush_buffer(tty);
66c3bdf1 341
1da177e4
LT
342 return 0;
343
344} /* end of n_tty_hdlc_open() */
345
346/**
347 * n_hdlc_send_frames - send frames on pending send buffer list
348 * @n_hdlc - pointer to ldisc instance data
349 * @tty - pointer to tty instance data
350 *
351 * Send frames on pending send buffer list until the driver does not accept a
352 * frame (busy) this function is called after adding a frame to the send buffer
353 * list and by the tty wakeup callback.
354 */
355static void n_hdlc_send_frames(struct n_hdlc *n_hdlc, struct tty_struct *tty)
356{
357 register int actual;
358 unsigned long flags;
359 struct n_hdlc_buf *tbuf;
360
1da177e4
LT
361 check_again:
362
363 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
364 if (n_hdlc->tbusy) {
365 n_hdlc->woke_up = 1;
366 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
367 return;
368 }
369 n_hdlc->tbusy = 1;
370 n_hdlc->woke_up = 0;
371 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
372
82f2341c 373 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
1da177e4
LT
374 while (tbuf) {
375 if (debuglevel >= DEBUG_LEVEL_INFO)
376 printk("%s(%d)sending frame %p, count=%d\n",
377 __FILE__,__LINE__,tbuf,tbuf->count);
378
379 /* Send the next block of data to device */
7962fce9 380 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
f34d7a5b 381 actual = tty->ops->write(tty, tbuf->buf, tbuf->count);
b0fed314
JS
382
383 /* rollback was possible and has been done */
384 if (actual == -ERESTARTSYS) {
82f2341c 385 n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
b0fed314
JS
386 break;
387 }
1da177e4
LT
388 /* if transmit error, throw frame away by */
389 /* pretending it was accepted by driver */
390 if (actual < 0)
391 actual = tbuf->count;
392
393 if (actual == tbuf->count) {
394 if (debuglevel >= DEBUG_LEVEL_INFO)
395 printk("%s(%d)frame %p completed\n",
396 __FILE__,__LINE__,tbuf);
397
398 /* free current transmit buffer */
399 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf);
82f2341c 400
1da177e4
LT
401 /* wait up sleeping writers */
402 wake_up_interruptible(&tty->write_wait);
403
404 /* get next pending transmit buffer */
405 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list);
406 } else {
407 if (debuglevel >= DEBUG_LEVEL_INFO)
408 printk("%s(%d)frame %p pending\n",
409 __FILE__,__LINE__,tbuf);
82f2341c
AP
410
411 /*
412 * the buffer was not accepted by driver,
413 * return it back into tx queue
414 */
415 n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf);
1da177e4
LT
416 break;
417 }
418 }
419
420 if (!tbuf)
7962fce9 421 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1da177e4
LT
422
423 /* Clear the re-entry flag */
424 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags);
425 n_hdlc->tbusy = 0;
426 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags);
427
428 if (n_hdlc->woke_up)
429 goto check_again;
1da177e4
LT
430} /* end of n_hdlc_send_frames() */
431
432/**
433 * n_hdlc_tty_wakeup - Callback for transmit wakeup
434 * @tty - pointer to associated tty instance data
435 *
436 * Called when low level device driver can accept more send data.
437 */
438static void n_hdlc_tty_wakeup(struct tty_struct *tty)
439{
440 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
441
1da177e4
LT
442 if (!n_hdlc)
443 return;
444
445 if (tty != n_hdlc->tty) {
7962fce9 446 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
1da177e4
LT
447 return;
448 }
449
450 n_hdlc_send_frames (n_hdlc, tty);
451
452} /* end of n_hdlc_tty_wakeup() */
453
1da177e4
LT
454/**
455 * n_hdlc_tty_receive - Called by tty driver when receive data is available
456 * @tty - pointer to tty instance data
457 * @data - pointer to received data
458 * @flags - pointer to flags for data
459 * @count - count of received data in bytes
460 *
461 * Called by tty low level driver when receive data is available. Data is
462 * interpreted as one HDLC frame.
463 */
55db4c64
LT
464static void n_hdlc_tty_receive(struct tty_struct *tty, const __u8 *data,
465 char *flags, int count)
1da177e4
LT
466{
467 register struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
468 register struct n_hdlc_buf *buf;
469
470 if (debuglevel >= DEBUG_LEVEL_INFO)
471 printk("%s(%d)n_hdlc_tty_receive() called count=%d\n",
472 __FILE__,__LINE__, count);
473
474 /* This can happen if stuff comes in on the backup tty */
a01e035e 475 if (!n_hdlc || tty != n_hdlc->tty)
55db4c64 476 return;
1da177e4
LT
477
478 /* verify line is using HDLC discipline */
479 if (n_hdlc->magic != HDLC_MAGIC) {
480 printk("%s(%d) line not using HDLC discipline\n",
481 __FILE__,__LINE__);
55db4c64 482 return;
1da177e4
LT
483 }
484
485 if ( count>maxframe ) {
486 if (debuglevel >= DEBUG_LEVEL_INFO)
487 printk("%s(%d) rx count>maxframesize, data discarded\n",
488 __FILE__,__LINE__);
55db4c64 489 return;
1da177e4
LT
490 }
491
492 /* get a free HDLC buffer */
493 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list);
494 if (!buf) {
495 /* no buffers in free list, attempt to allocate another rx buffer */
496 /* unless the maximum count has been reached */
497 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT)
85f4c951
GS
498 buf = kmalloc(struct_size(buf, buf, maxframe),
499 GFP_ATOMIC);
1da177e4
LT
500 }
501
502 if (!buf) {
503 if (debuglevel >= DEBUG_LEVEL_INFO)
504 printk("%s(%d) no more rx buffers, data discarded\n",
505 __FILE__,__LINE__);
55db4c64 506 return;
1da177e4
LT
507 }
508
509 /* copy received data to HDLC buffer */
510 memcpy(buf->buf,data,count);
511 buf->count=count;
512
513 /* add HDLC buffer to list of received frames */
514 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf);
515
516 /* wake up any blocked reads and perform async signalling */
517 wake_up_interruptible (&tty->read_wait);
518 if (n_hdlc->tty->fasync != NULL)
519 kill_fasync (&n_hdlc->tty->fasync, SIGIO, POLL_IN);
520
521} /* end of n_hdlc_tty_receive() */
522
523/**
4a4efbde 524 * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
1da177e4
LT
525 * @tty - pointer to tty instance data
526 * @file - pointer to open file object
527 * @buf - pointer to returned data buffer
528 * @nr - size of returned data buffer
529 *
530 * Returns the number of bytes returned or error code.
531 */
532static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
533 __u8 __user *buf, size_t nr)
534{
535 struct n_hdlc *n_hdlc = tty2n_hdlc(tty);
1035b63d 536 int ret = 0;
1da177e4 537 struct n_hdlc_buf *rbuf;
1035b63d 538 DECLARE_WAITQUEUE(wait, current);
1da177e4 539
1da177e4
LT
540 /* Validate the pointers */
541 if (!n_hdlc)
542 return -EIO;
543
544 /* verify user access to buffer */
96d4f267 545 if (!access_ok(buf, nr)) {
1da177e4
LT
546 printk(KERN_WARNING "%s(%d) n_hdlc_tty_read() can't verify user "
547 "buffer\n", __FILE__, __LINE__);
548 return -EFAULT;
549 }
550
1035b63d 551 add_wait_queue(&tty->read_wait, &wait);
04f378b1 552
1da177e4 553 for (;;) {
0f40fbbc 554 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
1035b63d
PF
555 ret = -EIO;
556 break;
04f378b1 557 }
1035b63d
PF
558 if (tty_hung_up_p(file))
559 break;
1da177e4 560
1035b63d 561 set_current_state(TASK_INTERRUPTIBLE);
1da177e4
LT
562
563 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list);
1035b63d
PF
564 if (rbuf) {
565 if (rbuf->count > nr) {
566 /* too large for caller's buffer */
567 ret = -EOVERFLOW;
568 } else {
fc01d8c6 569 __set_current_state(TASK_RUNNING);
1035b63d
PF
570 if (copy_to_user(buf, rbuf->buf, rbuf->count))
571 ret = -EFAULT;
572 else
573 ret = rbuf->count;
574 }
575
576 if (n_hdlc->rx_free_buf_list.count >
577 DEFAULT_RX_BUF_COUNT)
578 kfree(rbuf);
579 else
580 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf);
1da177e4 581 break;
1035b63d 582 }
1da177e4
LT
583
584 /* no data */
c96cf923 585 if (tty_io_nonblock(tty, file)) {
1035b63d
PF
586 ret = -EAGAIN;
587 break;
04f378b1 588 }
1035b63d
PF
589
590 schedule();
591
04f378b1 592 if (signal_pending(current)) {
1035b63d
PF
593 ret = -EINTR;
594 break;
04f378b1 595 }
1da177e4 596 }
1035b63d
PF
597
598 remove_wait_queue(&tty->read_wait, &wait);
599 __set_current_state(TASK_RUNNING);
600
1da177e4
LT
601 return ret;
602
603} /* end of n_hdlc_tty_read() */
604
605/**
606 * n_hdlc_tty_write - write a single frame of data to device
607 * @tty - pointer to associated tty device instance data
608 * @file - pointer to file object data
609 * @data - pointer to transmit data (one frame)
610 * @count - size of transmit frame in bytes
611 *
612 * Returns the number of bytes written (or error code).
613 */
614static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
615 const unsigned char *data, size_t count)
616{
617 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
618 int error = 0;
619 DECLARE_WAITQUEUE(wait, current);
620 struct n_hdlc_buf *tbuf;
621
622 if (debuglevel >= DEBUG_LEVEL_INFO)
5b5e0928 623 printk("%s(%d)n_hdlc_tty_write() called count=%zd\n",
1da177e4
LT
624 __FILE__,__LINE__,count);
625
626 /* Verify pointers */
627 if (!n_hdlc)
628 return -EIO;
629
630 if (n_hdlc->magic != HDLC_MAGIC)
631 return -EIO;
632
633 /* verify frame size */
634 if (count > maxframe ) {
635 if (debuglevel & DEBUG_LEVEL_INFO)
636 printk (KERN_WARNING
637 "n_hdlc_tty_write: truncating user packet "
638 "from %lu to %d\n", (unsigned long) count,
639 maxframe );
640 count = maxframe;
641 }
642
643 add_wait_queue(&tty->write_wait, &wait);
1035b63d
PF
644
645 for (;;) {
646 set_current_state(TASK_INTERRUPTIBLE);
1da177e4 647
1035b63d
PF
648 tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list);
649 if (tbuf)
650 break;
651
c96cf923 652 if (tty_io_nonblock(tty, file)) {
c72f527c
PF
653 error = -EAGAIN;
654 break;
655 }
1da177e4
LT
656 schedule();
657
658 n_hdlc = tty2n_hdlc (tty);
659 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC ||
660 tty != n_hdlc->tty) {
661 printk("n_hdlc_tty_write: %p invalid after wait!\n", n_hdlc);
662 error = -EIO;
663 break;
664 }
665
666 if (signal_pending(current)) {
667 error = -EINTR;
668 break;
669 }
670 }
671
1035b63d 672 __set_current_state(TASK_RUNNING);
1da177e4
LT
673 remove_wait_queue(&tty->write_wait, &wait);
674
675 if (!error) {
676 /* Retrieve the user's buffer */
677 memcpy(tbuf->buf, data, count);
678
679 /* Send the data */
680 tbuf->count = error = count;
681 n_hdlc_buf_put(&n_hdlc->tx_buf_list,tbuf);
682 n_hdlc_send_frames(n_hdlc,tty);
683 }
1035b63d 684
1da177e4
LT
685 return error;
686
687} /* end of n_hdlc_tty_write() */
688
689/**
690 * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
691 * @tty - pointer to tty instance data
692 * @file - pointer to open file object for device
693 * @cmd - IOCTL command code
694 * @arg - argument for IOCTL call (cmd dependent)
695 *
696 * Returns command dependent result.
697 */
698static int n_hdlc_tty_ioctl(struct tty_struct *tty, struct file *file,
699 unsigned int cmd, unsigned long arg)
700{
701 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
702 int error = 0;
703 int count;
704 unsigned long flags;
82f2341c
AP
705 struct n_hdlc_buf *buf = NULL;
706
1da177e4
LT
707 if (debuglevel >= DEBUG_LEVEL_INFO)
708 printk("%s(%d)n_hdlc_tty_ioctl() called %d\n",
709 __FILE__,__LINE__,cmd);
710
711 /* Verify the status of the device */
712 if (!n_hdlc || n_hdlc->magic != HDLC_MAGIC)
713 return -EBADF;
714
715 switch (cmd) {
716 case FIONREAD:
717 /* report count of read data available */
718 /* in next available frame (if any) */
719 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock,flags);
82f2341c
AP
720 buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list,
721 struct n_hdlc_buf, list_item);
722 if (buf)
723 count = buf->count;
1da177e4
LT
724 else
725 count = 0;
726 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock,flags);
727 error = put_user(count, (int __user *)arg);
728 break;
729
730 case TIOCOUTQ:
731 /* get the pending tx byte count in the driver */
f34d7a5b 732 count = tty_chars_in_buffer(tty);
1da177e4
LT
733 /* add size of next output frame in queue */
734 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock,flags);
82f2341c
AP
735 buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list,
736 struct n_hdlc_buf, list_item);
737 if (buf)
738 count += buf->count;
1da177e4
LT
739 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock,flags);
740 error = put_user(count, (int __user *)arg);
741 break;
742
be10eb75
PF
743 case TCFLSH:
744 switch (arg) {
745 case TCIOFLUSH:
746 case TCOFLUSH:
747 flush_tx_queue(tty);
748 }
361bf8a9 749 /* fall through - to default */
be10eb75 750
1da177e4 751 default:
47afa7a5 752 error = n_tty_ioctl_helper(tty, file, cmd, arg);
1da177e4
LT
753 break;
754 }
755 return error;
756
757} /* end of n_hdlc_tty_ioctl() */
758
759/**
760 * n_hdlc_tty_poll - TTY callback for poll system call
761 * @tty - pointer to tty instance data
762 * @filp - pointer to open file object for device
763 * @poll_table - wait queue for operations
764 *
765 * Determine which operations (read/write) will not block and return info
766 * to caller.
767 * Returns a bit mask containing info on which ops will not block.
768 */
afc9a42b 769static __poll_t n_hdlc_tty_poll(struct tty_struct *tty, struct file *filp,
1da177e4
LT
770 poll_table *wait)
771{
772 struct n_hdlc *n_hdlc = tty2n_hdlc (tty);
afc9a42b 773 __poll_t mask = 0;
1da177e4 774
1da177e4
LT
775 if (n_hdlc && n_hdlc->magic == HDLC_MAGIC && tty == n_hdlc->tty) {
776 /* queue current process into any wait queue that */
777 /* may awaken in the future (read and write) */
778
779 poll_wait(filp, &tty->read_wait, wait);
780 poll_wait(filp, &tty->write_wait, wait);
781
782 /* set bits for operations that won't block */
82f2341c 783 if (!list_empty(&n_hdlc->rx_buf_list.list))
a9a08845 784 mask |= EPOLLIN | EPOLLRDNORM; /* readable */
0f40fbbc 785 if (test_bit(TTY_OTHER_CLOSED, &tty->flags))
a9a08845 786 mask |= EPOLLHUP;
9c1729db 787 if (tty_hung_up_p(filp))
a9a08845 788 mask |= EPOLLHUP;
9c1729db 789 if (!tty_is_writelocked(tty) &&
82f2341c 790 !list_empty(&n_hdlc->tx_free_buf_list.list))
a9a08845 791 mask |= EPOLLOUT | EPOLLWRNORM; /* writable */
1da177e4
LT
792 }
793 return mask;
794} /* end of n_hdlc_tty_poll() */
795
796/**
797 * n_hdlc_alloc - allocate an n_hdlc instance data structure
798 *
799 * Returns a pointer to newly created structure if success, otherwise %NULL
800 */
801static struct n_hdlc *n_hdlc_alloc(void)
802{
803 struct n_hdlc_buf *buf;
804 int i;
8e25f8ce 805 struct n_hdlc *n_hdlc = kzalloc(sizeof(*n_hdlc), GFP_KERNEL);
1da177e4
LT
806
807 if (!n_hdlc)
808 return NULL;
809
e9b736d8
JS
810 spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock);
811 spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock);
812 spin_lock_init(&n_hdlc->rx_buf_list.spinlock);
813 spin_lock_init(&n_hdlc->tx_buf_list.spinlock);
82f2341c
AP
814
815 INIT_LIST_HEAD(&n_hdlc->rx_free_buf_list.list);
816 INIT_LIST_HEAD(&n_hdlc->tx_free_buf_list.list);
817 INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list);
818 INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list);
819
1da177e4
LT
820 /* allocate free rx buffer list */
821 for(i=0;i<DEFAULT_RX_BUF_COUNT;i++) {
85f4c951 822 buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL);
1da177e4
LT
823 if (buf)
824 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list,buf);
825 else if (debuglevel >= DEBUG_LEVEL_INFO)
826 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for rx buffer %d\n",__FILE__,__LINE__, i);
827 }
828
829 /* allocate free tx buffer list */
830 for(i=0;i<DEFAULT_TX_BUF_COUNT;i++) {
85f4c951 831 buf = kmalloc(struct_size(buf, buf, maxframe), GFP_KERNEL);
1da177e4
LT
832 if (buf)
833 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list,buf);
834 else if (debuglevel >= DEBUG_LEVEL_INFO)
835 printk("%s(%d)n_hdlc_alloc(), kalloc() failed for tx buffer %d\n",__FILE__,__LINE__, i);
836 }
837
838 /* Initialize the control block */
839 n_hdlc->magic = HDLC_MAGIC;
840 n_hdlc->flags = 0;
841
842 return n_hdlc;
843
844} /* end of n_hdlc_alloc() */
845
82f2341c
AP
846/**
847 * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
848 * @buf_list - pointer to the buffer list
849 * @buf - pointer to the buffer
850 */
851static void n_hdlc_buf_return(struct n_hdlc_buf_list *buf_list,
852 struct n_hdlc_buf *buf)
853{
854 unsigned long flags;
855
856 spin_lock_irqsave(&buf_list->spinlock, flags);
857
858 list_add(&buf->list_item, &buf_list->list);
859 buf_list->count++;
860
861 spin_unlock_irqrestore(&buf_list->spinlock, flags);
862}
863
1da177e4
LT
864/**
865 * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
82f2341c 866 * @buf_list - pointer to buffer list
1da177e4
LT
867 * @buf - pointer to buffer
868 */
82f2341c 869static void n_hdlc_buf_put(struct n_hdlc_buf_list *buf_list,
1da177e4
LT
870 struct n_hdlc_buf *buf)
871{
872 unsigned long flags;
82f2341c
AP
873
874 spin_lock_irqsave(&buf_list->spinlock, flags);
875
876 list_add_tail(&buf->list_item, &buf_list->list);
877 buf_list->count++;
878
879 spin_unlock_irqrestore(&buf_list->spinlock, flags);
1da177e4
LT
880} /* end of n_hdlc_buf_put() */
881
882/**
883 * n_hdlc_buf_get - remove and return an HDLC buffer from list
82f2341c 884 * @buf_list - pointer to HDLC buffer list
1da177e4
LT
885 *
886 * Remove and return an HDLC buffer from the head of the specified HDLC buffer
887 * list.
888 * Returns a pointer to HDLC buffer if available, otherwise %NULL.
889 */
82f2341c 890static struct n_hdlc_buf *n_hdlc_buf_get(struct n_hdlc_buf_list *buf_list)
1da177e4
LT
891{
892 unsigned long flags;
893 struct n_hdlc_buf *buf;
82f2341c
AP
894
895 spin_lock_irqsave(&buf_list->spinlock, flags);
896
897 buf = list_first_entry_or_null(&buf_list->list,
898 struct n_hdlc_buf, list_item);
1da177e4 899 if (buf) {
82f2341c
AP
900 list_del(&buf->list_item);
901 buf_list->count--;
1da177e4 902 }
82f2341c
AP
903
904 spin_unlock_irqrestore(&buf_list->spinlock, flags);
1da177e4 905 return buf;
1da177e4
LT
906} /* end of n_hdlc_buf_get() */
907
06324664 908static const char hdlc_banner[] __initconst =
be10eb75 909 KERN_INFO "HDLC line discipline maxframe=%u\n";
06324664 910static const char hdlc_register_ok[] __initconst =
1da177e4 911 KERN_INFO "N_HDLC line discipline registered.\n";
06324664 912static const char hdlc_register_fail[] __initconst =
1da177e4 913 KERN_ERR "error registering line discipline: %d\n";
1da177e4
LT
914
915static int __init n_hdlc_init(void)
916{
917 int status;
918
919 /* range check maxframe arg */
920 if (maxframe < 4096)
921 maxframe = 4096;
922 else if (maxframe > 65535)
923 maxframe = 65535;
924
925 printk(hdlc_banner, maxframe);
926
927 status = tty_register_ldisc(N_HDLC, &n_hdlc_ldisc);
928 if (!status)
929 printk(hdlc_register_ok);
930 else
931 printk(hdlc_register_fail, status);
932
1da177e4
LT
933 return status;
934
935} /* end of init_module() */
936
47a7e5e9
RD
937#ifdef CONFIG_SPARC
938#undef __exitdata
939#define __exitdata
940#endif
941
06324664 942static const char hdlc_unregister_ok[] __exitdata =
1da177e4 943 KERN_INFO "N_HDLC: line discipline unregistered\n";
06324664 944static const char hdlc_unregister_fail[] __exitdata =
1da177e4
LT
945 KERN_ERR "N_HDLC: can't unregister line discipline (err = %d)\n";
946
947static void __exit n_hdlc_exit(void)
948{
949 /* Release tty registration of line discipline */
64ccd715 950 int status = tty_unregister_ldisc(N_HDLC);
1da177e4
LT
951
952 if (status)
953 printk(hdlc_unregister_fail, status);
954 else
955 printk(hdlc_unregister_ok);
956}
957
958module_init(n_hdlc_init);
959module_exit(n_hdlc_exit);
960
961MODULE_LICENSE("GPL");
962MODULE_AUTHOR("Paul Fulghum paulkf@microgate.com");
963module_param(debuglevel, int, 0);
964module_param(maxframe, int, 0);
965MODULE_ALIAS_LDISC(N_HDLC);