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.
123 * struct n_hdlc - per device instance data structure
126 * @tx_buf_list: list of pending transmit frame buffers
127 * @rx_buf_list: list of received frame buffers
128 * @tx_free_buf_list: list unused transmit frame buffers
129 * @rx_free_buf_list: list unused received frame buffers
143 * HDLC buffer list manipulation functions
156 /* max frame size for memory allocations */
161 struct n_hdlc *n_hdlc = tty->disc_data; in flush_rx_queue()
164 while ((buf = n_hdlc_buf_get(&n_hdlc->rx_buf_list))) in flush_rx_queue()
165 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, buf); in flush_rx_queue()
170 struct n_hdlc *n_hdlc = tty->disc_data; in flush_tx_queue()
173 while ((buf = n_hdlc_buf_get(&n_hdlc->tx_buf_list))) in flush_tx_queue()
174 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, buf); in flush_tx_queue()
188 * n_hdlc_tty_close - line discipline close
196 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_close()
199 clear_bit(TTY_NO_WRITE_SPLIT, &tty->flags); in n_hdlc_tty_close()
201 tty->disc_data = NULL; in n_hdlc_tty_close()
204 wake_up_interruptible(&tty->read_wait); in n_hdlc_tty_close()
205 wake_up_interruptible(&tty->write_wait); in n_hdlc_tty_close()
207 cancel_work_sync(&n_hdlc->write_work); in n_hdlc_tty_close()
209 n_hdlc_free_buf_list(&n_hdlc->rx_free_buf_list); in n_hdlc_tty_close()
210 n_hdlc_free_buf_list(&n_hdlc->tx_free_buf_list); in n_hdlc_tty_close()
211 n_hdlc_free_buf_list(&n_hdlc->rx_buf_list); in n_hdlc_tty_close()
212 n_hdlc_free_buf_list(&n_hdlc->tx_buf_list); in n_hdlc_tty_close()
217 * n_hdlc_tty_open - called when line discipline changed to n_hdlc
224 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_open()
226 pr_debug("%s() called (device=%s)\n", __func__, tty->name); in n_hdlc_tty_open()
231 return -EEXIST; in n_hdlc_tty_open()
237 return -ENFILE; in n_hdlc_tty_open()
240 INIT_WORK(&n_hdlc->write_work, n_hdlc_tty_write_work); in n_hdlc_tty_open()
241 n_hdlc->tty_for_write_work = tty; in n_hdlc_tty_open()
242 tty->disc_data = n_hdlc; in n_hdlc_tty_open()
243 tty->receive_room = 65536; in n_hdlc_tty_open()
246 set_bit(TTY_NO_WRITE_SPLIT, &tty->flags); in n_hdlc_tty_open()
256 * n_hdlc_send_frames - send frames on pending send buffer list
260 * Send frames on pending send buffer list until the driver does not accept a
261 * frame (busy) this function is called after adding a frame to the send buffer
272 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_send_frames()
273 if (n_hdlc->tbusy) { in n_hdlc_send_frames()
274 n_hdlc->woke_up = true; in n_hdlc_send_frames()
275 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_send_frames()
278 n_hdlc->tbusy = true; in n_hdlc_send_frames()
279 n_hdlc->woke_up = false; in n_hdlc_send_frames()
280 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_send_frames()
282 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); in n_hdlc_send_frames()
284 pr_debug("sending frame %p, count=%zu\n", tbuf, tbuf->count); in n_hdlc_send_frames()
287 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in n_hdlc_send_frames()
288 actual = tty->ops->write(tty, tbuf->buf, tbuf->count); in n_hdlc_send_frames()
291 if (actual == -ERESTARTSYS) { in n_hdlc_send_frames()
292 n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf); in n_hdlc_send_frames()
295 /* if transmit error, throw frame away by */ in n_hdlc_send_frames()
298 actual = tbuf->count; in n_hdlc_send_frames()
300 if (actual == tbuf->count) { in n_hdlc_send_frames()
301 pr_debug("frame %p completed\n", tbuf); in n_hdlc_send_frames()
303 /* free current transmit buffer */ in n_hdlc_send_frames()
304 n_hdlc_buf_put(&n_hdlc->tx_free_buf_list, tbuf); in n_hdlc_send_frames()
307 wake_up_interruptible(&tty->write_wait); in n_hdlc_send_frames()
309 /* get next pending transmit buffer */ in n_hdlc_send_frames()
310 tbuf = n_hdlc_buf_get(&n_hdlc->tx_buf_list); in n_hdlc_send_frames()
312 pr_debug("frame %p pending\n", tbuf); in n_hdlc_send_frames()
315 * the buffer was not accepted by driver, in n_hdlc_send_frames()
318 n_hdlc_buf_return(&n_hdlc->tx_buf_list, tbuf); in n_hdlc_send_frames()
324 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); in n_hdlc_send_frames()
326 /* Clear the re-entry flag */ in n_hdlc_send_frames()
327 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_send_frames()
328 n_hdlc->tbusy = false; in n_hdlc_send_frames()
329 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_send_frames()
331 if (n_hdlc->woke_up) in n_hdlc_send_frames()
336 * n_hdlc_tty_write_work - Asynchronous callback for transmit wakeup
344 struct tty_struct *tty = n_hdlc->tty_for_write_work; in n_hdlc_tty_write_work()
350 * n_hdlc_tty_wakeup - Callback for transmit wakeup
357 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_wakeup()
359 schedule_work(&n_hdlc->write_work); in n_hdlc_tty_wakeup()
363 * n_hdlc_tty_receive - Called by tty driver when receive data is available
370 * interpreted as one HDLC frame.
375 register struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_receive()
385 /* get a free HDLC buffer */ in n_hdlc_tty_receive()
386 buf = n_hdlc_buf_get(&n_hdlc->rx_free_buf_list); in n_hdlc_tty_receive()
390 * buffer unless the maximum count has been reached in n_hdlc_tty_receive()
392 if (n_hdlc->rx_buf_list.count < MAX_RX_BUF_COUNT) in n_hdlc_tty_receive()
402 /* copy received data to HDLC buffer */ in n_hdlc_tty_receive()
403 memcpy(buf->buf, data, count); in n_hdlc_tty_receive()
404 buf->count = count; in n_hdlc_tty_receive()
406 /* add HDLC buffer to list of received frames */ in n_hdlc_tty_receive()
407 n_hdlc_buf_put(&n_hdlc->rx_buf_list, buf); in n_hdlc_tty_receive()
410 wake_up_interruptible(&tty->read_wait); in n_hdlc_tty_receive()
411 if (tty->fasync != NULL) in n_hdlc_tty_receive()
412 kill_fasync(&tty->fasync, SIGIO, POLL_IN); in n_hdlc_tty_receive()
417 * n_hdlc_tty_read - Called to retrieve one frame of data (if available)
420 * @kbuf: pointer to returned data buffer
421 * @nr: size of returned data buffer
423 * @offset: offset into the data buffer
431 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_read()
441 add_wait_queue(&tty->read_wait, &wait); in n_hdlc_tty_read()
444 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { in n_hdlc_tty_read()
445 ret = -EIO; in n_hdlc_tty_read()
453 rbuf = n_hdlc_buf_get(&n_hdlc->rx_buf_list); in n_hdlc_tty_read()
459 ret = -EAGAIN; in n_hdlc_tty_read()
466 ret = -EINTR; in n_hdlc_tty_read()
471 remove_wait_queue(&tty->read_wait, &wait); in n_hdlc_tty_read()
480 if (offset >= rbuf->count) in n_hdlc_tty_read()
484 ret = -EOVERFLOW; in n_hdlc_tty_read()
489 ret = rbuf->count - offset; in n_hdlc_tty_read()
492 memcpy(kbuf, rbuf->buf+offset, ret); in n_hdlc_tty_read()
496 if (offset < rbuf->count) in n_hdlc_tty_read()
502 if (n_hdlc->rx_free_buf_list.count > DEFAULT_RX_BUF_COUNT) in n_hdlc_tty_read()
505 n_hdlc_buf_put(&n_hdlc->rx_free_buf_list, rbuf); in n_hdlc_tty_read()
512 * n_hdlc_tty_write - write a single frame of data to device
515 * @data: pointer to transmit data (one frame)
516 * @count: size of transmit frame in bytes
523 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_write()
530 /* verify frame size */ in n_hdlc_tty_write()
537 add_wait_queue(&tty->write_wait, &wait); in n_hdlc_tty_write()
542 tbuf = n_hdlc_buf_get(&n_hdlc->tx_free_buf_list); in n_hdlc_tty_write()
547 error = -EAGAIN; in n_hdlc_tty_write()
553 error = -EINTR; in n_hdlc_tty_write()
559 remove_wait_queue(&tty->write_wait, &wait); in n_hdlc_tty_write()
562 /* Retrieve the user's buffer */ in n_hdlc_tty_write()
563 memcpy(tbuf->buf, data, count); in n_hdlc_tty_write()
566 tbuf->count = error = count; in n_hdlc_tty_write()
567 n_hdlc_buf_put(&n_hdlc->tx_buf_list, tbuf); in n_hdlc_tty_write()
576 * n_hdlc_tty_ioctl - process IOCTL system call for the tty device.
586 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_ioctl()
597 /* in next available frame (if any) */ in n_hdlc_tty_ioctl()
598 spin_lock_irqsave(&n_hdlc->rx_buf_list.spinlock, flags); in n_hdlc_tty_ioctl()
599 buf = list_first_entry_or_null(&n_hdlc->rx_buf_list.list, in n_hdlc_tty_ioctl()
602 count = buf->count; in n_hdlc_tty_ioctl()
605 spin_unlock_irqrestore(&n_hdlc->rx_buf_list.spinlock, flags); in n_hdlc_tty_ioctl()
612 /* add size of next output frame in queue */ in n_hdlc_tty_ioctl()
613 spin_lock_irqsave(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_tty_ioctl()
614 buf = list_first_entry_or_null(&n_hdlc->tx_buf_list.list, in n_hdlc_tty_ioctl()
617 count += buf->count; in n_hdlc_tty_ioctl()
618 spin_unlock_irqrestore(&n_hdlc->tx_buf_list.spinlock, flags); in n_hdlc_tty_ioctl()
639 * n_hdlc_tty_poll - TTY callback for poll system call
651 struct n_hdlc *n_hdlc = tty->disc_data; in n_hdlc_tty_poll()
658 poll_wait(filp, &tty->read_wait, wait); in n_hdlc_tty_poll()
659 poll_wait(filp, &tty->write_wait, wait); in n_hdlc_tty_poll()
662 if (!list_empty(&n_hdlc->rx_buf_list.list)) in n_hdlc_tty_poll()
664 if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) in n_hdlc_tty_poll()
669 !list_empty(&n_hdlc->tx_free_buf_list.list)) in n_hdlc_tty_poll()
684 pr_debug("%s(), kmalloc() failed for %s buffer %u\n", in n_hdlc_alloc_buf()
693 * n_hdlc_alloc - allocate an n_hdlc instance data structure
704 spin_lock_init(&n_hdlc->rx_free_buf_list.spinlock); in n_hdlc_alloc()
705 spin_lock_init(&n_hdlc->tx_free_buf_list.spinlock); in n_hdlc_alloc()
706 spin_lock_init(&n_hdlc->rx_buf_list.spinlock); in n_hdlc_alloc()
707 spin_lock_init(&n_hdlc->tx_buf_list.spinlock); in n_hdlc_alloc()
709 INIT_LIST_HEAD(&n_hdlc->rx_free_buf_list.list); in n_hdlc_alloc()
710 INIT_LIST_HEAD(&n_hdlc->tx_free_buf_list.list); in n_hdlc_alloc()
711 INIT_LIST_HEAD(&n_hdlc->rx_buf_list.list); in n_hdlc_alloc()
712 INIT_LIST_HEAD(&n_hdlc->tx_buf_list.list); in n_hdlc_alloc()
714 n_hdlc_alloc_buf(&n_hdlc->rx_free_buf_list, DEFAULT_RX_BUF_COUNT, "rx"); in n_hdlc_alloc()
715 n_hdlc_alloc_buf(&n_hdlc->tx_free_buf_list, DEFAULT_TX_BUF_COUNT, "tx"); in n_hdlc_alloc()
722 * n_hdlc_buf_return - put the HDLC buffer after the head of the specified list
723 * @buf_list: pointer to the buffer list
724 * @buf: pointer to the buffer
731 spin_lock_irqsave(&buf_list->spinlock, flags); in n_hdlc_buf_return()
733 list_add(&buf->list_item, &buf_list->list); in n_hdlc_buf_return()
734 buf_list->count++; in n_hdlc_buf_return()
736 spin_unlock_irqrestore(&buf_list->spinlock, flags); in n_hdlc_buf_return()
740 * n_hdlc_buf_put - add specified HDLC buffer to tail of specified list
741 * @buf_list: pointer to buffer list
742 * @buf: pointer to buffer
749 spin_lock_irqsave(&buf_list->spinlock, flags); in n_hdlc_buf_put()
751 list_add_tail(&buf->list_item, &buf_list->list); in n_hdlc_buf_put()
752 buf_list->count++; in n_hdlc_buf_put()
754 spin_unlock_irqrestore(&buf_list->spinlock, flags); in n_hdlc_buf_put()
758 * n_hdlc_buf_get - remove and return an HDLC buffer from list
759 * @buf_list: pointer to HDLC buffer list
761 * Remove and return an HDLC buffer from the head of the specified HDLC buffer
763 * Returns a pointer to HDLC buffer if available, otherwise %NULL.
770 spin_lock_irqsave(&buf_list->spinlock, flags); in n_hdlc_buf_get()
772 buf = list_first_entry_or_null(&buf_list->list, in n_hdlc_buf_get()
775 list_del(&buf->list_item); in n_hdlc_buf_get()
776 buf_list->count--; in n_hdlc_buf_get()
779 spin_unlock_irqrestore(&buf_list->spinlock, flags); in n_hdlc_buf_get()