xref: /openbmc/linux/net/nfc/nci/uart.c (revision 31e67366)
1 /*
2  * Copyright (C) 2015, Marvell International Ltd.
3  *
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.
10  *
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.
15  */
16 
17 /* Inspired (hugely) by HCI LDISC implementation in Bluetooth.
18  *
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>
22  */
23 
24 #include <linux/module.h>
25 
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>
33 
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>
41 
42 #include <net/nfc/nci.h>
43 #include <net/nfc/nci_core.h>
44 
45 /* TX states  */
46 #define NCI_UART_SENDING	1
47 #define NCI_UART_TX_WAKEUP	2
48 
49 static struct nci_uart *nci_uart_drivers[NCI_UART_DRIVER_MAX];
50 
51 static inline struct sk_buff *nci_uart_dequeue(struct nci_uart *nu)
52 {
53 	struct sk_buff *skb = nu->tx_skb;
54 
55 	if (!skb)
56 		skb = skb_dequeue(&nu->tx_q);
57 	else
58 		nu->tx_skb = NULL;
59 
60 	return skb;
61 }
62 
63 static inline int nci_uart_queue_empty(struct nci_uart *nu)
64 {
65 	if (nu->tx_skb)
66 		return 0;
67 
68 	return skb_queue_empty(&nu->tx_q);
69 }
70 
71 static int nci_uart_tx_wakeup(struct nci_uart *nu)
72 {
73 	if (test_and_set_bit(NCI_UART_SENDING, &nu->tx_state)) {
74 		set_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
75 		return 0;
76 	}
77 
78 	schedule_work(&nu->write_work);
79 
80 	return 0;
81 }
82 
83 static void nci_uart_write_work(struct work_struct *work)
84 {
85 	struct nci_uart *nu = container_of(work, struct nci_uart, write_work);
86 	struct tty_struct *tty = nu->tty;
87 	struct sk_buff *skb;
88 
89 restart:
90 	clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
91 
92 	if (nu->ops.tx_start)
93 		nu->ops.tx_start(nu);
94 
95 	while ((skb = nci_uart_dequeue(nu))) {
96 		int len;
97 
98 		set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
99 		len = tty->ops->write(tty, skb->data, skb->len);
100 		skb_pull(skb, len);
101 		if (skb->len) {
102 			nu->tx_skb = skb;
103 			break;
104 		}
105 		kfree_skb(skb);
106 	}
107 
108 	if (test_bit(NCI_UART_TX_WAKEUP, &nu->tx_state))
109 		goto restart;
110 
111 	if (nu->ops.tx_done && nci_uart_queue_empty(nu))
112 		nu->ops.tx_done(nu);
113 
114 	clear_bit(NCI_UART_SENDING, &nu->tx_state);
115 }
116 
117 static int nci_uart_set_driver(struct tty_struct *tty, unsigned int driver)
118 {
119 	struct nci_uart *nu = NULL;
120 	int ret;
121 
122 	if (driver >= NCI_UART_DRIVER_MAX)
123 		return -EINVAL;
124 
125 	if (!nci_uart_drivers[driver])
126 		return -ENOENT;
127 
128 	nu = kzalloc(sizeof(*nu), GFP_KERNEL);
129 	if (!nu)
130 		return -ENOMEM;
131 
132 	memcpy(nu, nci_uart_drivers[driver], sizeof(struct nci_uart));
133 	nu->tty = tty;
134 	tty->disc_data = nu;
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);
138 
139 	ret = nu->ops.open(nu);
140 	if (ret) {
141 		tty->disc_data = NULL;
142 		kfree(nu);
143 	} else if (!try_module_get(nu->owner)) {
144 		nu->ops.close(nu);
145 		tty->disc_data = NULL;
146 		kfree(nu);
147 		return -ENOENT;
148 	}
149 	return ret;
150 }
151 
152 /* ------ LDISC part ------ */
153 
154 /* nci_uart_tty_open
155  *
156  *     Called when line discipline changed to NCI_UART.
157  *
158  * Arguments:
159  *     tty    pointer to tty info structure
160  * Return Value:
161  *     0 if success, otherwise error code
162  */
163 static int nci_uart_tty_open(struct tty_struct *tty)
164 {
165 	/* Error if the tty has no write op instead of leaving an exploitable
166 	 * hole
167 	 */
168 	if (!tty->ops->write)
169 		return -EOPNOTSUPP;
170 
171 	tty->disc_data = NULL;
172 	tty->receive_room = 65536;
173 
174 	/* Flush any pending characters in the driver */
175 	tty_driver_flush_buffer(tty);
176 
177 	return 0;
178 }
179 
180 /* nci_uart_tty_close()
181  *
182  *    Called when the line discipline is changed to something
183  *    else, the tty is closed, or the tty detects a hangup.
184  */
185 static void nci_uart_tty_close(struct tty_struct *tty)
186 {
187 	struct nci_uart *nu = (void *)tty->disc_data;
188 
189 	/* Detach from the tty */
190 	tty->disc_data = NULL;
191 
192 	if (!nu)
193 		return;
194 
195 	kfree_skb(nu->tx_skb);
196 	kfree_skb(nu->rx_skb);
197 
198 	skb_queue_purge(&nu->tx_q);
199 
200 	nu->ops.close(nu);
201 	nu->tty = NULL;
202 	module_put(nu->owner);
203 
204 	cancel_work_sync(&nu->write_work);
205 
206 	kfree(nu);
207 }
208 
209 /* nci_uart_tty_wakeup()
210  *
211  *    Callback for transmit wakeup. Called when low level
212  *    device driver can accept more send data.
213  *
214  * Arguments:        tty    pointer to associated tty instance data
215  * Return Value:    None
216  */
217 static void nci_uart_tty_wakeup(struct tty_struct *tty)
218 {
219 	struct nci_uart *nu = (void *)tty->disc_data;
220 
221 	if (!nu)
222 		return;
223 
224 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
225 
226 	if (tty != nu->tty)
227 		return;
228 
229 	nci_uart_tx_wakeup(nu);
230 }
231 
232 /* nci_uart_tty_receive()
233  *
234  *     Called by tty low level driver when receive data is
235  *     available.
236  *
237  * Arguments:  tty          pointer to tty isntance data
238  *             data         pointer to received data
239  *             flags        pointer to flags for data
240  *             count        count of received data in bytes
241  *
242  * Return Value:    None
243  */
244 static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
245 				 char *flags, int count)
246 {
247 	struct nci_uart *nu = (void *)tty->disc_data;
248 
249 	if (!nu || tty != nu->tty)
250 		return;
251 
252 	spin_lock(&nu->rx_lock);
253 	nu->ops.recv_buf(nu, (void *)data, flags, count);
254 	spin_unlock(&nu->rx_lock);
255 
256 	tty_unthrottle(tty);
257 }
258 
259 /* nci_uart_tty_ioctl()
260  *
261  *    Process IOCTL system call for the tty device.
262  *
263  * Arguments:
264  *
265  *    tty        pointer to tty instance data
266  *    file       pointer to open file object for device
267  *    cmd        IOCTL command code
268  *    arg        argument for IOCTL call (cmd dependent)
269  *
270  * Return Value:    Command dependent
271  */
272 static int nci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
273 			      unsigned int cmd, unsigned long arg)
274 {
275 	struct nci_uart *nu = (void *)tty->disc_data;
276 	int err = 0;
277 
278 	switch (cmd) {
279 	case NCIUARTSETDRIVER:
280 		if (!nu)
281 			return nci_uart_set_driver(tty, (unsigned int)arg);
282 		else
283 			return -EBUSY;
284 		break;
285 	default:
286 		err = n_tty_ioctl_helper(tty, file, cmd, arg);
287 		break;
288 	}
289 
290 	return err;
291 }
292 
293 /* We don't provide read/write/poll interface for user space. */
294 static ssize_t nci_uart_tty_read(struct tty_struct *tty, struct file *file,
295 				 unsigned char *buf, size_t nr,
296 				 void **cookie, unsigned long offset)
297 {
298 	return 0;
299 }
300 
301 static ssize_t nci_uart_tty_write(struct tty_struct *tty, struct file *file,
302 				  const unsigned char *data, size_t count)
303 {
304 	return 0;
305 }
306 
307 static __poll_t nci_uart_tty_poll(struct tty_struct *tty,
308 				      struct file *filp, poll_table *wait)
309 {
310 	return 0;
311 }
312 
313 static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb)
314 {
315 	/* Queue TX packet */
316 	skb_queue_tail(&nu->tx_q, skb);
317 
318 	/* Try to start TX (if possible) */
319 	nci_uart_tx_wakeup(nu);
320 
321 	return 0;
322 }
323 
324 /* -- Default recv_buf handler --
325  *
326  * This handler supposes that NCI frames are sent over UART link without any
327  * framing. It reads NCI header, retrieve the packet size and once all packet
328  * bytes are received it passes it to nci_uart driver for processing.
329  */
330 static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data,
331 				     char *flags, int count)
332 {
333 	int chunk_len;
334 
335 	if (!nu->ndev) {
336 		nfc_err(nu->tty->dev,
337 			"receive data from tty but no NCI dev is attached yet, drop buffer\n");
338 		return 0;
339 	}
340 
341 	/* Decode all incoming data in packets
342 	 * and enqueue then for processing.
343 	 */
344 	while (count > 0) {
345 		/* If this is the first data of a packet, allocate a buffer */
346 		if (!nu->rx_skb) {
347 			nu->rx_packet_len = -1;
348 			nu->rx_skb = nci_skb_alloc(nu->ndev,
349 						   NCI_MAX_PACKET_SIZE,
350 						   GFP_ATOMIC);
351 			if (!nu->rx_skb)
352 				return -ENOMEM;
353 		}
354 
355 		/* Eat byte after byte till full packet header is received */
356 		if (nu->rx_skb->len < NCI_CTRL_HDR_SIZE) {
357 			skb_put_u8(nu->rx_skb, *data++);
358 			--count;
359 			continue;
360 		}
361 
362 		/* Header was received but packet len was not read */
363 		if (nu->rx_packet_len < 0)
364 			nu->rx_packet_len = NCI_CTRL_HDR_SIZE +
365 				nci_plen(nu->rx_skb->data);
366 
367 		/* Compute how many bytes are missing and how many bytes can
368 		 * be consumed.
369 		 */
370 		chunk_len = nu->rx_packet_len - nu->rx_skb->len;
371 		if (count < chunk_len)
372 			chunk_len = count;
373 		skb_put_data(nu->rx_skb, data, chunk_len);
374 		data += chunk_len;
375 		count -= chunk_len;
376 
377 		/* Chcek if packet is fully received */
378 		if (nu->rx_packet_len == nu->rx_skb->len) {
379 			/* Pass RX packet to driver */
380 			if (nu->ops.recv(nu, nu->rx_skb) != 0)
381 				nfc_err(nu->tty->dev, "corrupted RX packet\n");
382 			/* Next packet will be a new one */
383 			nu->rx_skb = NULL;
384 		}
385 	}
386 
387 	return 0;
388 }
389 
390 /* -- Default recv handler -- */
391 static int nci_uart_default_recv(struct nci_uart *nu, struct sk_buff *skb)
392 {
393 	return nci_recv_frame(nu->ndev, skb);
394 }
395 
396 int nci_uart_register(struct nci_uart *nu)
397 {
398 	if (!nu || !nu->ops.open ||
399 	    !nu->ops.recv || !nu->ops.close)
400 		return -EINVAL;
401 
402 	/* Set the send callback */
403 	nu->ops.send = nci_uart_send;
404 
405 	/* Install default handlers if not overridden */
406 	if (!nu->ops.recv_buf)
407 		nu->ops.recv_buf = nci_uart_default_recv_buf;
408 	if (!nu->ops.recv)
409 		nu->ops.recv = nci_uart_default_recv;
410 
411 	/* Add this driver in the driver list */
412 	if (nci_uart_drivers[nu->driver]) {
413 		pr_err("driver %d is already registered\n", nu->driver);
414 		return -EBUSY;
415 	}
416 	nci_uart_drivers[nu->driver] = nu;
417 
418 	pr_info("NCI uart driver '%s [%d]' registered\n", nu->name, nu->driver);
419 
420 	return 0;
421 }
422 EXPORT_SYMBOL_GPL(nci_uart_register);
423 
424 void nci_uart_unregister(struct nci_uart *nu)
425 {
426 	pr_info("NCI uart driver '%s [%d]' unregistered\n", nu->name,
427 		nu->driver);
428 
429 	/* Remove this driver from the driver list */
430 	nci_uart_drivers[nu->driver] = NULL;
431 }
432 EXPORT_SYMBOL_GPL(nci_uart_unregister);
433 
434 void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
435 {
436 	struct ktermios new_termios;
437 
438 	if (!nu->tty)
439 		return;
440 
441 	down_read(&nu->tty->termios_rwsem);
442 	new_termios = nu->tty->termios;
443 	up_read(&nu->tty->termios_rwsem);
444 	tty_termios_encode_baud_rate(&new_termios, baudrate, baudrate);
445 
446 	if (flow_ctrl)
447 		new_termios.c_cflag |= CRTSCTS;
448 	else
449 		new_termios.c_cflag &= ~CRTSCTS;
450 
451 	tty_set_termios(nu->tty, &new_termios);
452 }
453 EXPORT_SYMBOL_GPL(nci_uart_set_config);
454 
455 static struct tty_ldisc_ops nci_uart_ldisc = {
456 	.magic		= TTY_LDISC_MAGIC,
457 	.owner		= THIS_MODULE,
458 	.name		= "n_nci",
459 	.open		= nci_uart_tty_open,
460 	.close		= nci_uart_tty_close,
461 	.read		= nci_uart_tty_read,
462 	.write		= nci_uart_tty_write,
463 	.poll		= nci_uart_tty_poll,
464 	.receive_buf	= nci_uart_tty_receive,
465 	.write_wakeup	= nci_uart_tty_wakeup,
466 	.ioctl		= nci_uart_tty_ioctl,
467 	.compat_ioctl	= nci_uart_tty_ioctl,
468 };
469 
470 static int __init nci_uart_init(void)
471 {
472 	memset(nci_uart_drivers, 0, sizeof(nci_uart_drivers));
473 	return tty_register_ldisc(N_NCI, &nci_uart_ldisc);
474 }
475 
476 static void __exit nci_uart_exit(void)
477 {
478 	tty_unregister_ldisc(N_NCI);
479 }
480 
481 module_init(nci_uart_init);
482 module_exit(nci_uart_exit);
483 
484 MODULE_AUTHOR("Marvell International Ltd.");
485 MODULE_DESCRIPTION("NFC NCI UART driver");
486 MODULE_LICENSE("GPL");
487 MODULE_ALIAS_LDISC(N_NCI);
488