1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015, Marvell International Ltd.
5 * Inspired (hugely) by HCI LDISC implementation in Bluetooth.
7 * Copyright (C) 2000-2001 Qualcomm Incorporated
8 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
9 * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
12 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/types.h>
17 #include <linux/fcntl.h>
18 #include <linux/interrupt.h>
19 #include <linux/ptrace.h>
20 #include <linux/poll.h>
22 #include <linux/slab.h>
23 #include <linux/tty.h>
24 #include <linux/errno.h>
25 #include <linux/string.h>
26 #include <linux/signal.h>
27 #include <linux/ioctl.h>
28 #include <linux/skbuff.h>
30 #include <net/nfc/nci.h>
31 #include <net/nfc/nci_core.h>
34 #define NCI_UART_SENDING 1
35 #define NCI_UART_TX_WAKEUP 2
37 static struct nci_uart *nci_uart_drivers[NCI_UART_DRIVER_MAX];
39 static inline struct sk_buff *nci_uart_dequeue(struct nci_uart *nu)
41 struct sk_buff *skb = nu->tx_skb;
44 skb = skb_dequeue(&nu->tx_q);
51 static inline int nci_uart_queue_empty(struct nci_uart *nu)
56 return skb_queue_empty(&nu->tx_q);
59 static int nci_uart_tx_wakeup(struct nci_uart *nu)
61 if (test_and_set_bit(NCI_UART_SENDING, &nu->tx_state)) {
62 set_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
66 schedule_work(&nu->write_work);
71 static void nci_uart_write_work(struct work_struct *work)
73 struct nci_uart *nu = container_of(work, struct nci_uart, write_work);
74 struct tty_struct *tty = nu->tty;
78 clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
83 while ((skb = nci_uart_dequeue(nu))) {
86 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
87 len = tty->ops->write(tty, skb->data, skb->len);
96 if (test_bit(NCI_UART_TX_WAKEUP, &nu->tx_state))
99 if (nu->ops.tx_done && nci_uart_queue_empty(nu))
102 clear_bit(NCI_UART_SENDING, &nu->tx_state);
105 static int nci_uart_set_driver(struct tty_struct *tty, unsigned int driver)
107 struct nci_uart *nu = NULL;
110 if (driver >= NCI_UART_DRIVER_MAX)
113 if (!nci_uart_drivers[driver])
116 nu = kzalloc(sizeof(*nu), GFP_KERNEL);
120 memcpy(nu, nci_uart_drivers[driver], sizeof(struct nci_uart));
123 skb_queue_head_init(&nu->tx_q);
124 INIT_WORK(&nu->write_work, nci_uart_write_work);
125 spin_lock_init(&nu->rx_lock);
127 ret = nu->ops.open(nu);
129 tty->disc_data = NULL;
131 } else if (!try_module_get(nu->owner)) {
133 tty->disc_data = NULL;
140 /* ------ LDISC part ------ */
144 * Called when line discipline changed to NCI_UART.
147 * tty pointer to tty info structure
149 * 0 if success, otherwise error code
151 static int nci_uart_tty_open(struct tty_struct *tty)
153 /* Error if the tty has no write op instead of leaving an exploitable
156 if (!tty->ops->write)
159 tty->disc_data = NULL;
160 tty->receive_room = 65536;
162 /* Flush any pending characters in the driver */
163 tty_driver_flush_buffer(tty);
168 /* nci_uart_tty_close()
170 * Called when the line discipline is changed to something
171 * else, the tty is closed, or the tty detects a hangup.
173 static void nci_uart_tty_close(struct tty_struct *tty)
175 struct nci_uart *nu = (void *)tty->disc_data;
177 /* Detach from the tty */
178 tty->disc_data = NULL;
183 kfree_skb(nu->tx_skb);
184 kfree_skb(nu->rx_skb);
186 skb_queue_purge(&nu->tx_q);
190 module_put(nu->owner);
192 cancel_work_sync(&nu->write_work);
197 /* nci_uart_tty_wakeup()
199 * Callback for transmit wakeup. Called when low level
200 * device driver can accept more send data.
202 * Arguments: tty pointer to associated tty instance data
205 static void nci_uart_tty_wakeup(struct tty_struct *tty)
207 struct nci_uart *nu = (void *)tty->disc_data;
212 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
217 nci_uart_tx_wakeup(nu);
220 /* -- Default recv_buf handler --
222 * This handler supposes that NCI frames are sent over UART link without any
223 * framing. It reads NCI header, retrieve the packet size and once all packet
224 * bytes are received it passes it to nci_uart driver for processing.
226 static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data,
232 nfc_err(nu->tty->dev,
233 "receive data from tty but no NCI dev is attached yet, drop buffer\n");
237 /* Decode all incoming data in packets
238 * and enqueue then for processing.
241 /* If this is the first data of a packet, allocate a buffer */
243 nu->rx_packet_len = -1;
244 nu->rx_skb = nci_skb_alloc(nu->ndev,
251 /* Eat byte after byte till full packet header is received */
252 if (nu->rx_skb->len < NCI_CTRL_HDR_SIZE) {
253 skb_put_u8(nu->rx_skb, *data++);
258 /* Header was received but packet len was not read */
259 if (nu->rx_packet_len < 0)
260 nu->rx_packet_len = NCI_CTRL_HDR_SIZE +
261 nci_plen(nu->rx_skb->data);
263 /* Compute how many bytes are missing and how many bytes can
266 chunk_len = nu->rx_packet_len - nu->rx_skb->len;
267 if (count < chunk_len)
269 skb_put_data(nu->rx_skb, data, chunk_len);
273 /* Check if packet is fully received */
274 if (nu->rx_packet_len == nu->rx_skb->len) {
275 /* Pass RX packet to driver */
276 if (nu->ops.recv(nu, nu->rx_skb) != 0)
277 nfc_err(nu->tty->dev, "corrupted RX packet\n");
278 /* Next packet will be a new one */
286 /* nci_uart_tty_receive()
288 * Called by tty low level driver when receive data is
291 * Arguments: tty pointer to tty instance data
292 * data pointer to received data
293 * flags pointer to flags for data
294 * count count of received data in bytes
298 static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
299 const char *flags, int count)
301 struct nci_uart *nu = (void *)tty->disc_data;
303 if (!nu || tty != nu->tty)
306 spin_lock(&nu->rx_lock);
307 nci_uart_default_recv_buf(nu, data, count);
308 spin_unlock(&nu->rx_lock);
313 /* nci_uart_tty_ioctl()
315 * Process IOCTL system call for the tty device.
319 * tty pointer to tty instance data
320 * file pointer to open file object for device
321 * cmd IOCTL command code
322 * arg argument for IOCTL call (cmd dependent)
324 * Return Value: Command dependent
326 static int nci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
327 unsigned int cmd, unsigned long arg)
329 struct nci_uart *nu = (void *)tty->disc_data;
333 case NCIUARTSETDRIVER:
335 return nci_uart_set_driver(tty, (unsigned int)arg);
340 err = n_tty_ioctl_helper(tty, cmd, arg);
347 /* We don't provide read/write/poll interface for user space. */
348 static ssize_t nci_uart_tty_read(struct tty_struct *tty, struct file *file,
349 unsigned char *buf, size_t nr,
350 void **cookie, unsigned long offset)
355 static ssize_t nci_uart_tty_write(struct tty_struct *tty, struct file *file,
356 const unsigned char *data, size_t count)
361 static __poll_t nci_uart_tty_poll(struct tty_struct *tty,
362 struct file *filp, poll_table *wait)
367 static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb)
369 /* Queue TX packet */
370 skb_queue_tail(&nu->tx_q, skb);
372 /* Try to start TX (if possible) */
373 nci_uart_tx_wakeup(nu);
378 int nci_uart_register(struct nci_uart *nu)
380 if (!nu || !nu->ops.open ||
381 !nu->ops.recv || !nu->ops.close)
384 /* Set the send callback */
385 nu->ops.send = nci_uart_send;
387 /* Add this driver in the driver list */
388 if (nci_uart_drivers[nu->driver]) {
389 pr_err("driver %d is already registered\n", nu->driver);
392 nci_uart_drivers[nu->driver] = nu;
394 pr_info("NCI uart driver '%s [%d]' registered\n", nu->name, nu->driver);
398 EXPORT_SYMBOL_GPL(nci_uart_register);
400 void nci_uart_unregister(struct nci_uart *nu)
402 pr_info("NCI uart driver '%s [%d]' unregistered\n", nu->name,
405 /* Remove this driver from the driver list */
406 nci_uart_drivers[nu->driver] = NULL;
408 EXPORT_SYMBOL_GPL(nci_uart_unregister);
410 void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
412 struct ktermios new_termios;
417 down_read(&nu->tty->termios_rwsem);
418 new_termios = nu->tty->termios;
419 up_read(&nu->tty->termios_rwsem);
420 tty_termios_encode_baud_rate(&new_termios, baudrate, baudrate);
423 new_termios.c_cflag |= CRTSCTS;
425 new_termios.c_cflag &= ~CRTSCTS;
427 tty_set_termios(nu->tty, &new_termios);
429 EXPORT_SYMBOL_GPL(nci_uart_set_config);
431 static struct tty_ldisc_ops nci_uart_ldisc = {
432 .owner = THIS_MODULE,
435 .open = nci_uart_tty_open,
436 .close = nci_uart_tty_close,
437 .read = nci_uart_tty_read,
438 .write = nci_uart_tty_write,
439 .poll = nci_uart_tty_poll,
440 .receive_buf = nci_uart_tty_receive,
441 .write_wakeup = nci_uart_tty_wakeup,
442 .ioctl = nci_uart_tty_ioctl,
443 .compat_ioctl = nci_uart_tty_ioctl,
446 static int __init nci_uart_init(void)
448 return tty_register_ldisc(&nci_uart_ldisc);
451 static void __exit nci_uart_exit(void)
453 tty_unregister_ldisc(&nci_uart_ldisc);
456 module_init(nci_uart_init);
457 module_exit(nci_uart_exit);
459 MODULE_AUTHOR("Marvell International Ltd.");
460 MODULE_DESCRIPTION("NFC NCI UART driver");
461 MODULE_LICENSE("GPL");
462 MODULE_ALIAS_LDISC(N_NCI);