of: MSI: Simplify irqdomain lookup
[linux/fpc-iii.git] / net / nfc / nci / uart.c
blob21d8875673a42229f233ca20af640c53e325a4bb
1 /*
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>
45 /* TX states */
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;
55 if (!skb)
56 skb = skb_dequeue(&nu->tx_q);
57 else
58 nu->tx_skb = NULL;
60 return skb;
63 static inline int nci_uart_queue_empty(struct nci_uart *nu)
65 if (nu->tx_skb)
66 return 0;
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);
75 return 0;
78 schedule_work(&nu->write_work);
80 return 0;
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;
87 struct sk_buff *skb;
89 restart:
90 clear_bit(NCI_UART_TX_WAKEUP, &nu->tx_state);
92 if (nu->ops.tx_start)
93 nu->ops.tx_start(nu);
95 while ((skb = nci_uart_dequeue(nu))) {
96 int len;
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;
105 kfree_skb(skb);
108 if (test_bit(NCI_UART_TX_WAKEUP, &nu->tx_state))
109 goto restart;
111 if (nu->ops.tx_done && nci_uart_queue_empty(nu))
112 nu->ops.tx_done(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;
120 int ret;
122 if (driver >= NCI_UART_DRIVER_MAX)
123 return -EINVAL;
125 if (!nci_uart_drivers[driver])
126 return -ENOENT;
128 nu = kzalloc(sizeof(*nu), GFP_KERNEL);
129 if (!nu)
130 return -ENOMEM;
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);
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;
149 return ret;
152 /* ------ LDISC part ------ */
154 /* nci_uart_tty_open
156 * Called when line discipline changed to NCI_UART.
158 * Arguments:
159 * tty pointer to tty info structure
160 * Return Value:
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
166 * hole
168 if (!tty->ops->write)
169 return -EOPNOTSUPP;
171 tty->disc_data = NULL;
172 tty->receive_room = 65536;
174 /* Flush any pending characters in the driver and line discipline. */
176 /* FIXME: why is this needed. Note don't use ldisc_ref here as the
177 * open path is before the ldisc is referencable.
180 if (tty->ldisc->ops->flush_buffer)
181 tty->ldisc->ops->flush_buffer(tty);
182 tty_driver_flush_buffer(tty);
184 return 0;
187 /* nci_uart_tty_close()
189 * Called when the line discipline is changed to something
190 * else, the tty is closed, or the tty detects a hangup.
192 static void nci_uart_tty_close(struct tty_struct *tty)
194 struct nci_uart *nu = (void *)tty->disc_data;
196 /* Detach from the tty */
197 tty->disc_data = NULL;
199 if (!nu)
200 return;
202 if (nu->tx_skb)
203 kfree_skb(nu->tx_skb);
204 if (nu->rx_skb)
205 kfree_skb(nu->rx_skb);
207 skb_queue_purge(&nu->tx_q);
209 nu->ops.close(nu);
210 nu->tty = NULL;
211 module_put(nu->owner);
213 cancel_work_sync(&nu->write_work);
215 kfree(nu);
218 /* nci_uart_tty_wakeup()
220 * Callback for transmit wakeup. Called when low level
221 * device driver can accept more send data.
223 * Arguments: tty pointer to associated tty instance data
224 * Return Value: None
226 static void nci_uart_tty_wakeup(struct tty_struct *tty)
228 struct nci_uart *nu = (void *)tty->disc_data;
230 if (!nu)
231 return;
233 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
235 if (tty != nu->tty)
236 return;
238 nci_uart_tx_wakeup(nu);
241 /* nci_uart_tty_receive()
243 * Called by tty low level driver when receive data is
244 * available.
246 * Arguments: tty pointer to tty isntance data
247 * data pointer to received data
248 * flags pointer to flags for data
249 * count count of received data in bytes
251 * Return Value: None
253 static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
254 char *flags, int count)
256 struct nci_uart *nu = (void *)tty->disc_data;
258 if (!nu || tty != nu->tty)
259 return;
261 spin_lock(&nu->rx_lock);
262 nu->ops.recv_buf(nu, (void *)data, flags, count);
263 spin_unlock(&nu->rx_lock);
265 tty_unthrottle(tty);
268 /* nci_uart_tty_ioctl()
270 * Process IOCTL system call for the tty device.
272 * Arguments:
274 * tty pointer to tty instance data
275 * file pointer to open file object for device
276 * cmd IOCTL command code
277 * arg argument for IOCTL call (cmd dependent)
279 * Return Value: Command dependent
281 static int nci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
282 unsigned int cmd, unsigned long arg)
284 struct nci_uart *nu = (void *)tty->disc_data;
285 int err = 0;
287 switch (cmd) {
288 case NCIUARTSETDRIVER:
289 if (!nu)
290 return nci_uart_set_driver(tty, (unsigned int)arg);
291 else
292 return -EBUSY;
293 break;
294 default:
295 err = n_tty_ioctl_helper(tty, file, cmd, arg);
296 break;
299 return err;
302 /* We don't provide read/write/poll interface for user space. */
303 static ssize_t nci_uart_tty_read(struct tty_struct *tty, struct file *file,
304 unsigned char __user *buf, size_t nr)
306 return 0;
309 static ssize_t nci_uart_tty_write(struct tty_struct *tty, struct file *file,
310 const unsigned char *data, size_t count)
312 return 0;
315 static unsigned int nci_uart_tty_poll(struct tty_struct *tty,
316 struct file *filp, poll_table *wait)
318 return 0;
321 static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb)
323 /* Queue TX packet */
324 skb_queue_tail(&nu->tx_q, skb);
326 /* Try to start TX (if possible) */
327 nci_uart_tx_wakeup(nu);
329 return 0;
332 /* -- Default recv_buf handler --
334 * This handler supposes that NCI frames are sent over UART link without any
335 * framing. It reads NCI header, retrieve the packet size and once all packet
336 * bytes are received it passes it to nci_uart driver for processing.
338 static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data,
339 char *flags, int count)
341 int chunk_len;
343 if (!nu->ndev) {
344 nfc_err(nu->tty->dev,
345 "receive data from tty but no NCI dev is attached yet, drop buffer\n");
346 return 0;
349 /* Decode all incoming data in packets
350 * and enqueue then for processing.
352 while (count > 0) {
353 /* If this is the first data of a packet, allocate a buffer */
354 if (!nu->rx_skb) {
355 nu->rx_packet_len = -1;
356 nu->rx_skb = nci_skb_alloc(nu->ndev,
357 NCI_MAX_PACKET_SIZE,
358 GFP_KERNEL);
359 if (!nu->rx_skb)
360 return -ENOMEM;
363 /* Eat byte after byte till full packet header is received */
364 if (nu->rx_skb->len < NCI_CTRL_HDR_SIZE) {
365 *skb_put(nu->rx_skb, 1) = *data++;
366 --count;
367 continue;
370 /* Header was received but packet len was not read */
371 if (nu->rx_packet_len < 0)
372 nu->rx_packet_len = NCI_CTRL_HDR_SIZE +
373 nci_plen(nu->rx_skb->data);
375 /* Compute how many bytes are missing and how many bytes can
376 * be consumed.
378 chunk_len = nu->rx_packet_len - nu->rx_skb->len;
379 if (count < chunk_len)
380 chunk_len = count;
381 memcpy(skb_put(nu->rx_skb, chunk_len), data, chunk_len);
382 data += chunk_len;
383 count -= chunk_len;
385 /* Chcek if packet is fully received */
386 if (nu->rx_packet_len == nu->rx_skb->len) {
387 /* Pass RX packet to driver */
388 if (nu->ops.recv(nu, nu->rx_skb) != 0)
389 nfc_err(nu->tty->dev, "corrupted RX packet\n");
390 /* Next packet will be a new one */
391 nu->rx_skb = NULL;
395 return 0;
398 /* -- Default recv handler -- */
399 static int nci_uart_default_recv(struct nci_uart *nu, struct sk_buff *skb)
401 return nci_recv_frame(nu->ndev, skb);
404 int nci_uart_register(struct nci_uart *nu)
406 if (!nu || !nu->ops.open ||
407 !nu->ops.recv || !nu->ops.close)
408 return -EINVAL;
410 /* Set the send callback */
411 nu->ops.send = nci_uart_send;
413 /* Install default handlers if not overridden */
414 if (!nu->ops.recv_buf)
415 nu->ops.recv_buf = nci_uart_default_recv_buf;
416 if (!nu->ops.recv)
417 nu->ops.recv = nci_uart_default_recv;
419 /* Add this driver in the driver list */
420 if (nci_uart_drivers[nu->driver]) {
421 pr_err("driver %d is already registered\n", nu->driver);
422 return -EBUSY;
424 nci_uart_drivers[nu->driver] = nu;
426 pr_info("NCI uart driver '%s [%d]' registered\n", nu->name, nu->driver);
428 return 0;
430 EXPORT_SYMBOL_GPL(nci_uart_register);
432 void nci_uart_unregister(struct nci_uart *nu)
434 pr_info("NCI uart driver '%s [%d]' unregistered\n", nu->name,
435 nu->driver);
437 /* Remove this driver from the driver list */
438 nci_uart_drivers[nu->driver] = NULL;
440 EXPORT_SYMBOL_GPL(nci_uart_unregister);
442 void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
444 struct ktermios new_termios;
446 if (!nu->tty)
447 return;
449 down_read(&nu->tty->termios_rwsem);
450 new_termios = nu->tty->termios;
451 up_read(&nu->tty->termios_rwsem);
452 tty_termios_encode_baud_rate(&new_termios, baudrate, baudrate);
454 if (flow_ctrl)
455 new_termios.c_cflag |= CRTSCTS;
456 else
457 new_termios.c_cflag &= ~CRTSCTS;
459 tty_set_termios(nu->tty, &new_termios);
461 EXPORT_SYMBOL_GPL(nci_uart_set_config);
463 static struct tty_ldisc_ops nci_uart_ldisc = {
464 .magic = TTY_LDISC_MAGIC,
465 .owner = THIS_MODULE,
466 .name = "n_nci",
467 .open = nci_uart_tty_open,
468 .close = nci_uart_tty_close,
469 .read = nci_uart_tty_read,
470 .write = nci_uart_tty_write,
471 .poll = nci_uart_tty_poll,
472 .receive_buf = nci_uart_tty_receive,
473 .write_wakeup = nci_uart_tty_wakeup,
474 .ioctl = nci_uart_tty_ioctl,
477 static int __init nci_uart_init(void)
479 memset(nci_uart_drivers, 0, sizeof(nci_uart_drivers));
480 return tty_register_ldisc(N_NCI, &nci_uart_ldisc);
483 static void __exit nci_uart_exit(void)
485 tty_unregister_ldisc(N_NCI);
488 module_init(nci_uart_init);
489 module_exit(nci_uart_exit);
491 MODULE_AUTHOR("Marvell International Ltd.");
492 MODULE_DESCRIPTION("NFC NCI UART driver");
493 MODULE_LICENSE("GPL");
494 MODULE_ALIAS_LDISC(N_NCI);