1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
46#define NCI_UART_SENDING 1
47#define NCI_UART_TX_WAKEUP 2
48
49static struct nci_uart *nci_uart_drivers[NCI_UART_DRIVER_MAX];
50
51static 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
63static 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
71static 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
83static 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
89restart:
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
117static 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
153
154
155
156
157
158
159
160
161
162
163static int nci_uart_tty_open(struct tty_struct *tty)
164{
165
166
167
168 if (!tty->ops->write)
169 return -EOPNOTSUPP;
170
171 tty->disc_data = NULL;
172 tty->receive_room = 65536;
173
174
175 tty_driver_flush_buffer(tty);
176
177 return 0;
178}
179
180
181
182
183
184
185static void nci_uart_tty_close(struct tty_struct *tty)
186{
187 struct nci_uart *nu = (void *)tty->disc_data;
188
189
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
210
211
212
213
214
215
216
217static 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
233
234
235
236
237
238static int nci_uart_default_recv_buf(struct nci_uart *nu, const u8 *data,
239 int count)
240{
241 int chunk_len;
242
243 if (!nu->ndev) {
244 nfc_err(nu->tty->dev,
245 "receive data from tty but no NCI dev is attached yet, drop buffer\n");
246 return 0;
247 }
248
249
250
251
252 while (count > 0) {
253
254 if (!nu->rx_skb) {
255 nu->rx_packet_len = -1;
256 nu->rx_skb = nci_skb_alloc(nu->ndev,
257 NCI_MAX_PACKET_SIZE,
258 GFP_ATOMIC);
259 if (!nu->rx_skb)
260 return -ENOMEM;
261 }
262
263
264 if (nu->rx_skb->len < NCI_CTRL_HDR_SIZE) {
265 skb_put_u8(nu->rx_skb, *data++);
266 --count;
267 continue;
268 }
269
270
271 if (nu->rx_packet_len < 0)
272 nu->rx_packet_len = NCI_CTRL_HDR_SIZE +
273 nci_plen(nu->rx_skb->data);
274
275
276
277
278 chunk_len = nu->rx_packet_len - nu->rx_skb->len;
279 if (count < chunk_len)
280 chunk_len = count;
281 skb_put_data(nu->rx_skb, data, chunk_len);
282 data += chunk_len;
283 count -= chunk_len;
284
285
286 if (nu->rx_packet_len == nu->rx_skb->len) {
287
288 if (nu->ops.recv(nu, nu->rx_skb) != 0)
289 nfc_err(nu->tty->dev, "corrupted RX packet\n");
290
291 nu->rx_skb = NULL;
292 }
293 }
294
295 return 0;
296}
297
298
299
300
301
302
303
304
305
306
307
308
309
310static void nci_uart_tty_receive(struct tty_struct *tty, const u8 *data,
311 const char *flags, int count)
312{
313 struct nci_uart *nu = (void *)tty->disc_data;
314
315 if (!nu || tty != nu->tty)
316 return;
317
318 spin_lock(&nu->rx_lock);
319 nci_uart_default_recv_buf(nu, data, count);
320 spin_unlock(&nu->rx_lock);
321
322 tty_unthrottle(tty);
323}
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338static int nci_uart_tty_ioctl(struct tty_struct *tty, struct file *file,
339 unsigned int cmd, unsigned long arg)
340{
341 struct nci_uart *nu = (void *)tty->disc_data;
342 int err = 0;
343
344 switch (cmd) {
345 case NCIUARTSETDRIVER:
346 if (!nu)
347 return nci_uart_set_driver(tty, (unsigned int)arg);
348 else
349 return -EBUSY;
350 break;
351 default:
352 err = n_tty_ioctl_helper(tty, file, cmd, arg);
353 break;
354 }
355
356 return err;
357}
358
359
360static 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)
363{
364 return 0;
365}
366
367static ssize_t nci_uart_tty_write(struct tty_struct *tty, struct file *file,
368 const unsigned char *data, size_t count)
369{
370 return 0;
371}
372
373static __poll_t nci_uart_tty_poll(struct tty_struct *tty,
374 struct file *filp, poll_table *wait)
375{
376 return 0;
377}
378
379static int nci_uart_send(struct nci_uart *nu, struct sk_buff *skb)
380{
381
382 skb_queue_tail(&nu->tx_q, skb);
383
384
385 nci_uart_tx_wakeup(nu);
386
387 return 0;
388}
389
390int nci_uart_register(struct nci_uart *nu)
391{
392 if (!nu || !nu->ops.open ||
393 !nu->ops.recv || !nu->ops.close)
394 return -EINVAL;
395
396
397 nu->ops.send = nci_uart_send;
398
399
400 if (nci_uart_drivers[nu->driver]) {
401 pr_err("driver %d is already registered\n", nu->driver);
402 return -EBUSY;
403 }
404 nci_uart_drivers[nu->driver] = nu;
405
406 pr_info("NCI uart driver '%s [%d]' registered\n", nu->name, nu->driver);
407
408 return 0;
409}
410EXPORT_SYMBOL_GPL(nci_uart_register);
411
412void nci_uart_unregister(struct nci_uart *nu)
413{
414 pr_info("NCI uart driver '%s [%d]' unregistered\n", nu->name,
415 nu->driver);
416
417
418 nci_uart_drivers[nu->driver] = NULL;
419}
420EXPORT_SYMBOL_GPL(nci_uart_unregister);
421
422void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
423{
424 struct ktermios new_termios;
425
426 if (!nu->tty)
427 return;
428
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);
433
434 if (flow_ctrl)
435 new_termios.c_cflag |= CRTSCTS;
436 else
437 new_termios.c_cflag &= ~CRTSCTS;
438
439 tty_set_termios(nu->tty, &new_termios);
440}
441EXPORT_SYMBOL_GPL(nci_uart_set_config);
442
443static struct tty_ldisc_ops nci_uart_ldisc = {
444 .owner = THIS_MODULE,
445 .num = N_NCI,
446 .name = "n_nci",
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,
456};
457
458static int __init nci_uart_init(void)
459{
460 return tty_register_ldisc(&nci_uart_ldisc);
461}
462
463static void __exit nci_uart_exit(void)
464{
465 tty_unregister_ldisc(&nci_uart_ldisc);
466}
467
468module_init(nci_uart_init);
469module_exit(nci_uart_exit);
470
471MODULE_AUTHOR("Marvell International Ltd.");
472MODULE_DESCRIPTION("NFC NCI UART driver");
473MODULE_LICENSE("GPL");
474MODULE_ALIAS_LDISC(N_NCI);
475