Lines Matching +full:frame +full:- +full:buffer
1 // SPDX-License-Identifier: GPL-1.0+
16 * tty device drivers that support bit-synchronous HDLC communications.
18 * All HDLC data is frame oriented which means:
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
26 * CCITT CRC32, 4 crc bytes are required, so the maximum size frame
28 * CRC16, the maximum application frame size would be 65533.
32 * one received frame. The device driver should bypass
33 * the tty flip buffer and call the line discipline receive
41 * 3. tty read calls returns an entire frame of data or nothing.
49 * otherwise the count of the next available frame is returned.
50 * (instead of the sum of all received frame counts).
54 * this line discipline (or another line discipline that is frame
64 * and frame orientation of HDLC communications.
126 * struct n_hdlc - per device instance data structure
130 * @tx_buf_list: list of pending transmit frame buffers
131 * @rx_buf_list: list of received frame buffers
132 * @tx_free_buf_list: list unused transmit frame buffers
133 * @rx_free_buf_list: list unused received frame buffers
148 * HDLC buffer list manipulation functions
161 /* max frame size for memory allocations */
166 struct n_hdlc *n_hdlc = tty->disc_data; in flush_rx_queue()
169 while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list))) in flush_rx_queue()
170 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf); in flush_rx_queue()
175 struct n_hdlc *n_hdlc = tty->disc_data; in flush_tx_queue()
178 while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list))) in flush_tx_queue()
179 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf); in flush_tx_queue()
193 * n_hdlc_tty_close - line discipline close
201 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_close()
203 if (n_hdlc->magic != HDLC_MAGIC) { in n_hdlc_tty_close()
208 clear_bit(TTY_NO_WRITE_SPLIT, &tty->flags); in n_hdlc_tty_close()
210 tty->disc_data = NULL; in n_hdlc_tty_close()
213 wake_up_interruptible(&tty->read_wait); in n_hdlc_tty_close()
214 wake_up_interruptible(&tty->write_wait); in n_hdlc_tty_close()
216 cancel_work_sync(&n_hdlc->write_work); in n_hdlc_tty_close()
218 n_hdlc_free_buf_list(&n_hdlc->rx_free_buf_list); in n_hdlc_tty_close()
219 n_hdlc_free_buf_list(&n_hdlc->tx_free_buf_list); in n_hdlc_tty_close()
220 n_hdlc_free_buf_list(&n_hdlc->rx_buf_list); in n_hdlc_tty_close()
221 n_hdlc_free_buf_list(&n_hdlc->tx_buf_list); in n_hdlc_tty_close()
226 * n_hdlc_tty_open - called when line discipline changed to n_hdlc
233 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_open()
235 pr_debug("%s() called (device=%s)\n", __func__, tty->name); in n_hdlc_tty_open()
240 return -EEXIST; in n_hdlc_tty_open()
246 return -ENFILE; in n_hdlc_tty_open()
249 INIT_WORK(&n_hdlc->write_work, n_hdlc_tty_write_work); in n_hdlc_tty_open()
250 n_hdlc->tty_for_write_work = tty; in n_hdlc_tty_open()
251 tty->disc_data = n_hdlc; in n_hdlc_tty_open()
252 tty->receive_room = 65536; in n_hdlc_tty_open()
255 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags); in n_hdlc_tty_open()
265 * n_hdlc_send_frames - send frames on pending send buffer list
269 * Send frames on pending send buffer list until the driver does not accept a
270 * frame (busy) this function is called after adding a frame to the send buffer
281 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_send_frames()
282 if (n_hdlc->tbusy) { in n_hdlc_send_frames()
283 n_hdlc->woke_up = true; in n_hdlc_send_frames()
284 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_send_frames()
287 n_hdlc->tbusy = true; in n_hdlc_send_frames()
288 n_hdlc->woke_up = false; in n_hdlc_send_frames()
289 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_send_frames()
291 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); in n_hdlc_send_frames()
293 pr_debug("sending frame %p, count=%d\n", tbuf, tbuf->count); in n_hdlc_send_frames()
296 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in n_hdlc_send_frames()
297 actual = tty->ops->write(tty, tbuf->buf, tbuf->count); in n_hdlc_send_frames()
300 if (actual == -ERESTARTSYS) { in n_hdlc_send_frames()
301 n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf); in n_hdlc_send_frames()
304 /* if transmit error, throw frame away by */ in n_hdlc_send_frames()
307 actual = tbuf->count; in n_hdlc_send_frames()
309 if (actual == tbuf->count) { in n_hdlc_send_frames()
310 pr_debug("frame %p completed\n", tbuf); in n_hdlc_send_frames()
312 /* free current transmit buffer */ in n_hdlc_send_frames()
313 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf); in n_hdlc_send_frames()
316 wake_up_interruptible(&tty->write_wait); in n_hdlc_send_frames()
318 /* get next pending transmit buffer */ in n_hdlc_send_frames()
319 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); in n_hdlc_send_frames()
321 pr_debug("frame %p pending\n", tbuf); in n_hdlc_send_frames()
324 * the buffer was not accepted by driver, in n_hdlc_send_frames()
327 n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf); in n_hdlc_send_frames()
333 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in n_hdlc_send_frames()
335 /* Clear the re-entry flag */ in n_hdlc_send_frames()
336 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_send_frames()
337 n_hdlc->tbusy = false; in n_hdlc_send_frames()
338 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_send_frames()
340 if (n_hdlc->woke_up) in n_hdlc_send_frames()
345 * n_hdlc_tty_write_work - Asynchronous callback for transmit wakeup
353 struct tty_struct *tty = n_hdlc->tty_for_write_work; in n_hdlc_tty_write_work()
359 * n_hdlc_tty_wakeup - Callback for transmit wakeup
366 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_wakeup()
368 schedule_work(&n_hdlc->write_work); in n_hdlc_tty_wakeup()
372 * n_hdlc_tty_receive - Called by tty driver when receive data is available
379 * interpreted as one HDLC frame.
384 register struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_receive()
390 if (n_hdlc->magic != HDLC_MAGIC) { in n_hdlc_tty_receive()
400 /* get a free HDLC buffer */ in n_hdlc_tty_receive()
401 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list); in n_hdlc_tty_receive()
405 * buffer unless the maximum count has been reached in n_hdlc_tty_receive()
407 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT) in n_hdlc_tty_receive()
417 /* copy received data to HDLC buffer */ in n_hdlc_tty_receive()
418 memcpy(buf->buf, data, count); in n_hdlc_tty_receive()
419 buf->count = count; in n_hdlc_tty_receive()
421 /* add HDLC buffer to list of received frames */ in n_hdlc_tty_receive()
422 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf); in n_hdlc_tty_receive()
425 wake_up_interruptible(&tty->read_wait); in n_hdlc_tty_receive()
426 if (tty->fasync != NULL) in n_hdlc_tty_receive()
427 kill_fasync(&tty->fasync, SIGIO, POLL_IN); in n_hdlc_tty_receive()
432 * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
435 * @buf: pointer to returned data buffer
436 * @nr: size of returned data buffer
444 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_read()
454 add_wait_queue(&tty->read_wait, &wait); in n_hdlc_tty_read()
457 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { in n_hdlc_tty_read()
458 ret = -EIO; in n_hdlc_tty_read()
466 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list); in n_hdlc_tty_read()
472 ret = -EAGAIN; in n_hdlc_tty_read()
479 ret = -EINTR; in n_hdlc_tty_read()
484 remove_wait_queue(&tty->read_wait, &wait); in n_hdlc_tty_read()
493 if (offset >= rbuf->count) in n_hdlc_tty_read()
497 ret = -EOVERFLOW; in n_hdlc_tty_read()
502 ret = rbuf->count - offset; in n_hdlc_tty_read()
505 memcpy(kbuf, rbuf->buf+offset, ret); in n_hdlc_tty_read()
509 if (offset < rbuf->count) in n_hdlc_tty_read()
515 if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT) in n_hdlc_tty_read()
518 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf); in n_hdlc_tty_read()
525 * n_hdlc_tty_write - write a single frame of data to device
528 * @data: pointer to transmit data (one frame)
529 * @count: size of transmit frame in bytes
536 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_write()
543 if (n_hdlc->magic != HDLC_MAGIC) in n_hdlc_tty_write()
544 return -EIO; in n_hdlc_tty_write()
546 /* verify frame size */ in n_hdlc_tty_write()
553 add_wait_queue(&tty->write_wait, &wait); in n_hdlc_tty_write()
558 tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list); in n_hdlc_tty_write()
563 error = -EAGAIN; in n_hdlc_tty_write()
569 error = -EINTR; in n_hdlc_tty_write()
575 remove_wait_queue(&tty->write_wait, &wait); in n_hdlc_tty_write()
578 /* Retrieve the user's buffer */ in n_hdlc_tty_write()
579 memcpy(tbuf->buf, data, count); in n_hdlc_tty_write()
582 tbuf->count = error = count; in n_hdlc_tty_write()
583 n_hdlc_buf_put(&n_hdlc->tx_buf_list, tbuf); in n_hdlc_tty_write()
592 * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
603 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_ioctl()
612 if (n_hdlc->magic != HDLC_MAGIC) in n_hdlc_tty_ioctl()
613 return -EBADF; in n_hdlc_tty_ioctl()
618 /* in next available frame (if any) */ in n_hdlc_tty_ioctl()
619 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock, flags); in n_hdlc_tty_ioctl()
620 buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list, in n_hdlc_tty_ioctl()
623 count = buf->count; in n_hdlc_tty_ioctl()
626 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags); in n_hdlc_tty_ioctl()
633 /* add size of next output frame in queue */ in n_hdlc_tty_ioctl()
634 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_tty_ioctl()
635 buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list, in n_hdlc_tty_ioctl()
638 count += buf->count; in n_hdlc_tty_ioctl()
639 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_tty_ioctl()
660 * n_hdlc_tty_poll - TTY callback for poll system call
672 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_poll()
675 if (n_hdlc->magic != HDLC_MAGIC) in n_hdlc_tty_poll()
682 poll_wait(filp, &tty->read_wait, wait); in n_hdlc_tty_poll()
683 poll_wait(filp, &tty->write_wait, wait); in n_hdlc_tty_poll()
686 if (!list_empty(&n_hdlc->rx_buf_list.list)) in n_hdlc_tty_poll()
688 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) in n_hdlc_tty_poll()
693 !list_empty(&n_hdlc->tx_free_buf_list.list)) in n_hdlc_tty_poll()
708 pr_debug("%s(), kmalloc() failed for %s buffer %u\n", in n_hdlc_alloc_buf()
717 * n_hdlc_alloc - allocate an n_hdlc instance data structure
728 spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock); in n_hdlc_alloc()
729 spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock); in n_hdlc_alloc()
730 spin_lock_init(&n_hdlc->rx_buf_list.spinlock); in n_hdlc_alloc()
731 spin_lock_init(&n_hdlc->tx_buf_list.spinlock); in n_hdlc_alloc()
733 INIT_LIST_HEAD(&n_hdlc->rx_free_buf_list.list); in n_hdlc_alloc()
734 INIT_LIST_HEAD(&n_hdlc->tx_free_buf_list.list); in n_hdlc_alloc()
735 INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list); in n_hdlc_alloc()
736 INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list); in n_hdlc_alloc()
738 n_hdlc_alloc_buf(&n_hdlc->rx_free_buf_list, DEFAULT_RX_BUF_COUNT, "rx"); in n_hdlc_alloc()
739 n_hdlc_alloc_buf(&n_hdlc->tx_free_buf_list, DEFAULT_TX_BUF_COUNT, "tx"); in n_hdlc_alloc()
742 n_hdlc->magic = HDLC_MAGIC; in n_hdlc_alloc()
749 * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
750 * @buf_list: pointer to the buffer list
751 * @buf: pointer to the buffer
758 spin_lock_irqsave(&buf_list->spinlock, flags); in n_hdlc_buf_return()
760 list_add(&buf->list_item, &buf_list->list); in n_hdlc_buf_return()
761 buf_list->count++; in n_hdlc_buf_return()
763 spin_unlock_irqrestore(&buf_list->spinlock, flags); in n_hdlc_buf_return()
767 * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
768 * @buf_list: pointer to buffer list
769 * @buf: pointer to buffer
776 spin_lock_irqsave(&buf_list->spinlock, flags); in n_hdlc_buf_put()
778 list_add_tail(&buf->list_item, &buf_list->list); in n_hdlc_buf_put()
779 buf_list->count++; in n_hdlc_buf_put()
781 spin_unlock_irqrestore(&buf_list->spinlock, flags); in n_hdlc_buf_put()
785 * n_hdlc_buf_get - remove and return an HDLC buffer from list
786 * @buf_list: pointer to HDLC buffer list
788 * Remove and return an HDLC buffer from the head of the specified HDLC buffer
790 * Returns a pointer to HDLC buffer if available, otherwise %NULL.
797 spin_lock_irqsave(&buf_list->spinlock, flags); in n_hdlc_buf_get()
799 buf = list_first_entry_or_null(&buf_list->list, in n_hdlc_buf_get()
802 list_del(&buf->list_item); in n_hdlc_buf_get()
803 buf_list->count--; in n_hdlc_buf_get()
806 spin_unlock_irqrestore(&buf_list->spinlock, flags); in n_hdlc_buf_get()