2 * Copyright (C) 2015, Marvell International Ltd.
4 * This software file (the "File") is distributed by Marvell International
5 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
6 * (the "License"). You may use, redistribute and/or modify this File in
7 * accordance with the terms and conditions of the License, a copy of which
8 * is available on the worldwide web at
9 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
11 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
12 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
13 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
14 * this warranty disclaimer.
17 /* Inspired (hugely) by HCI LDISC implementation in Bluetooth.
19 * Copyright (C) 2000-2001 Qualcomm Incorporated
20 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
21 * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
24 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/fcntl.h>
30 #include <linux/interrupt.h>
31 #include <linux/ptrace.h>
32 #include <linux/poll.h>
34 #include <linux/slab.h>
35 #include <linux/tty.h>
36 #include <linux/errno.h>
37 #include <linux/string.h>
38 #include <linux/signal.h>
39 #include <linux/ioctl.h>
40 #include <linux/skbuff.h>
42 #include <net/nfc/nci.h>
43 #include <net/nfc/nci_core.h>
46 #define NCI_UART_SENDING 1
47 #define NCI_UART_TX_WAKEUP 2
49 static struct nci_uart *nci_uart_drivers[NCI_UART_DRIVER_MAX];
51 static inline struct sk_buff *nci_uart_dequeue(struct nci_uart *nu)
53 struct sk_buff *skb = nu->tx_skb;
56 skb = skb_dequeue(&nu->tx_q);
63 static inline int nci_uart_queue_empty(struct nci_uart *nu)
68 return skb_queue_empty(&nu->tx_q);
71 static int nci_uart_tx_wakeup(struct nci_uart *nu)
73 if (test_and_set_bit(NCI_UART_SENDING, &nu->tx_state)) {
74 set_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
78 schedule_work(&nu->write_work);
83 static void nci_uart_write_work(struct work_struct *work)
85 struct nci_uart *nu = container_of(work, struct nci_uart, write_work);
86 struct tty_struct *tty = nu->tty;
90 clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
95 while ((skb = nci_uart_dequeue(nu))) {
98 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
99 len = tty->ops->write(tty, skb->data, skb->len);
108 if (test_bit(NCI_UART_TX_WAKEUP, &nu->tx_state))
111 if (nu->ops.tx_done && nci_uart_queue_empty(nu))
114 clear_bit(NCI_UART_SENDING, &nu->tx_state);
117 static int nci_uart_set_driver(struct tty_struct *tty, unsigned int driver)
119 struct nci_uart *nu = NULL;
122 if (driver >= NCI_UART_DRIVER_MAX)
125 if (!nci_uart_drivers[driver])
128 nu = kzalloc(sizeof(*nu), GFP_KERNEL);
132 memcpy(nu, nci_uart_drivers[driver], sizeof(struct nci_uart));
135 skb_queue_head_init(&nu->tx_q);
136 INIT_WORK(&nu->write_work, nci_uart_write_work);
137 spin_lock_init(&nu->rx_lock);
139 ret = nu->ops.open(nu);
141 tty->disc_data = NULL;
143 } else if (!try_module_get(nu->owner)) {
145 tty->disc_data = NULL;
152 /* ------ LDISC part ------ */
156 * Called when line discipline changed to NCI_UART.
159 * tty pointer to tty info structure
161 * 0 if success, otherwise error code
163 static int nci_uart_tty_open(struct tty_struct *tty)
165 /* Error if the tty has no write op instead of leaving an exploitable
168 if (!tty->ops->write)
171 tty->disc_data = NULL;
172 tty->receive_room = 65536;
174 /* Flush any pending characters in the driver */
175 tty_driver_flush_buffer(tty);
180 /* nci_uart_tty_close()
182 * Called when the line discipline is changed to something
183 * else, the tty is closed, or the tty detects a hangup.
185 static void nci_uart_tty_close(struct tty_struct *tty)
187 struct nci_uart *nu = (void *)tty->disc_data;
189 /* Detach from the tty */
190 tty->disc_data = NULL;
195 kfree_skb(nu->tx_skb);
196 kfree_skb(nu->rx_skb);
198 skb_queue_purge(&nu->tx_q);
202 module_put(nu->owner);
204 cancel_work_sync(&nu->write_work);
209 /* nci_uart_tty_wakeup()
211 * Callback for transmit wakeup. Called when low level
212 * device driver can accept more send data.
214 * Arguments: tty pointer to associated tty instance data
217 static void nci_uart_tty_wakeup(struct tty_struct *tty)
219 struct nci_uart *nu = (void *)tty->disc_data;
224 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
229 nci_uart_tx_wakeup(nu);
232 /* -- Default recv_buf handler --
234 * This handler supposes that NCI frames are sent over UART link without any
235 * framing. It reads NCI header, retrieve the packet size and once all packet
236 * bytes are received it passes it to nci_uart driver for processing.
238 static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data,
244 nfc_err(nu->tty->dev,
245 "receive data from tty but no NCI dev is attached yet, drop buffer\n");
249 /* Decode all incoming data in packets
250 * and enqueue then for processing.
253 /* If this is the first data of a packet, allocate a buffer */
255 nu->rx_packet_len = -1;
256 nu->rx_skb = nci_skb_alloc(nu->ndev,
263 /* Eat byte after byte till full packet header is received */
264 if (nu->rx_skb->len < NCI_CTRL_HDR_SIZE) {
265 skb_put_u8(nu->rx_skb, *data++);
270 /* Header was received but packet len was not read */
271 if (nu->rx_packet_len < 0)
272 nu->rx_packet_len = NCI_CTRL_HDR_SIZE +
273 nci_plen(nu->rx_skb->data);
275 /* Compute how many bytes are missing and how many bytes can
278 chunk_len = nu->rx_packet_len - nu->rx_skb->len;
279 if (count < chunk_len)
281 skb_put_data(nu->rx_skb, data, chunk_len);
285 /* Check if packet is fully received */
286 if (nu->rx_packet_len == nu->rx_skb->len) {
287 /* Pass RX packet to driver */
288 if (nu->ops.recv(nu, nu->rx_skb) != 0)
289 nfc_err(nu->tty->dev, "corrupted RX packet\n");
290 /* Next packet will be a new one */
298 /* nci_uart_tty_receive()
300 * Called by tty low level driver when receive data is
303 * Arguments: tty pointer to tty instance data
304 * data pointer to received data
305 * flags pointer to flags for data
306 * count count of received data in bytes
310 static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
311 const char *flags, int count)
313 struct nci_uart *nu = (void *)tty->disc_data;
315 if (!nu || tty != nu->tty)
318 spin_lock(&nu->rx_lock);
319 nci_uart_default_recv_buf(nu, data, count);
320 spin_unlock(&nu->rx_lock);
325 /* nci_uart_tty_ioctl()
327 * Process IOCTL system call for the tty device.
331 * tty pointer to tty instance data
332 * file pointer to open file object for device
333 * cmd IOCTL command code
334 * arg argument for IOCTL call (cmd dependent)
336 * Return Value: Command dependent
338 static int nci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
339 unsigned int cmd, unsigned long arg)
341 struct nci_uart *nu = (void *)tty->disc_data;
345 case NCIUARTSETDRIVER:
347 return nci_uart_set_driver(tty, (unsigned int)arg);
352 err = n_tty_ioctl_helper(tty, file, cmd, arg);
359 /* We don't provide read/write/poll interface for user space. */
360 static ssize_t nci_uart_tty_read(struct tty_struct *tty, struct file *file,
361 unsigned char *buf, size_t nr,
362 void **cookie, unsigned long offset)
367 static ssize_t nci_uart_tty_write(struct tty_struct *tty, struct file *file,
368 const unsigned char *data, size_t count)
373 static __poll_t nci_uart_tty_poll(struct tty_struct *tty,
374 struct file *filp, poll_table *wait)
379 static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb)
381 /* Queue TX packet */
382 skb_queue_tail(&nu->tx_q, skb);
384 /* Try to start TX (if possible) */
385 nci_uart_tx_wakeup(nu);
390 int nci_uart_register(struct nci_uart *nu)
392 if (!nu || !nu->ops.open ||
393 !nu->ops.recv || !nu->ops.close)
396 /* Set the send callback */
397 nu->ops.send = nci_uart_send;
399 /* Add this driver in the driver list */
400 if (nci_uart_drivers[nu->driver]) {
401 pr_err("driver %d is already registered\n", nu->driver);
404 nci_uart_drivers[nu->driver] = nu;
406 pr_info("NCI uart driver '%s [%d]' registered\n", nu->name, nu->driver);
410 EXPORT_SYMBOL_GPL(nci_uart_register);
412 void nci_uart_unregister(struct nci_uart *nu)
414 pr_info("NCI uart driver '%s [%d]' unregistered\n", nu->name,
417 /* Remove this driver from the driver list */
418 nci_uart_drivers[nu->driver] = NULL;
420 EXPORT_SYMBOL_GPL(nci_uart_unregister);
422 void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
424 struct ktermios new_termios;
429 down_read(&nu->tty->termios_rwsem);
430 new_termios = nu->tty->termios;
431 up_read(&nu->tty->termios_rwsem);
432 tty_termios_encode_baud_rate(&new_termios, baudrate, baudrate);
435 new_termios.c_cflag |= CRTSCTS;
437 new_termios.c_cflag &= ~CRTSCTS;
439 tty_set_termios(nu->tty, &new_termios);
441 EXPORT_SYMBOL_GPL(nci_uart_set_config);
443 static struct tty_ldisc_ops nci_uart_ldisc = {
444 .owner = THIS_MODULE,
447 .open = nci_uart_tty_open,
448 .close = nci_uart_tty_close,
449 .read = nci_uart_tty_read,
450 .write = nci_uart_tty_write,
451 .poll = nci_uart_tty_poll,
452 .receive_buf = nci_uart_tty_receive,
453 .write_wakeup = nci_uart_tty_wakeup,
454 .ioctl = nci_uart_tty_ioctl,
455 .compat_ioctl = nci_uart_tty_ioctl,
458 static int __init nci_uart_init(void)
460 return tty_register_ldisc(&nci_uart_ldisc);
463 static void __exit nci_uart_exit(void)
465 tty_unregister_ldisc(&nci_uart_ldisc);
468 module_init(nci_uart_init);
469 module_exit(nci_uart_exit);
471 MODULE_AUTHOR("Marvell International Ltd.");
472 MODULE_DESCRIPTION("NFC NCI UART driver");
473 MODULE_LICENSE("GPL");
474 MODULE_ALIAS_LDISC(N_NCI);