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