-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserialsim.c
1506 lines (1270 loc) · 35.8 KB
/
serialsim.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0+
/*
* serialsim - Emulate a serial device in a loopback and/or pipe
*
* See the Documentation/serial/serialsim.rts for more details.
*
* Copyright 2019, Corey Minyard <[email protected]>
*/
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/ioport.h>
#include <linux/init.h>
#include <linux/serial.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/device.h>
#include <linux/serial_core.h>
#include <linux/kthread.h>
#include <linux/hrtimer.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/ctype.h>
#include <linux/string.h>
#include <linux/uaccess.h>
#include <linux/version.h>
#include <linux/idr.h>
#include <linux/miscdevice.h>
#include <linux/platform_device.h>
#include <linux/serialsim.h>
#ifndef TCGETS2
#error "This module is only supported with TCGETS2"
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,1,0)
/* This went away in 6.1. */
static inline int kernel_termios_to_user_termios(struct termios2 __user *u,
struct ktermios *k)
{
return copy_to_user(u, k, sizeof(struct termios2));
}
/* New in 6.1 */
#define CONST_KTERMIOS const
#else
#define CONST_KTERMIOS
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6,0,0)
/* New in 6.0. */
#define RS485_HAS_TERMIOS struct ktermios *termios,
/* Also new in 6.0. */
#define HAS_RS485_SUPPORTED
#else
#define RS485_HAS_TERMIOS
#endif
/*
* Port types for the various interfaces.
*/
#define PORT_SERIALECHO 72549
#define PORT_SERIALPIPEA 72550
#define PORT_SERIALPIPEB 72551
#ifdef CONFIG_HIGH_RES_TIMERS
#define SERIALSIM_WAKES_PER_SEC 1000
#else
#define SERIALSIM_WAKES_PER_SEC HZ
#endif
#define SERIALSIM_XBUFSIZE 32
/* For things to send on the line, in flags field. */
#define DO_FRAME_ERR (1 << TTY_FRAME)
#define DO_PARITY_ERR (1 << TTY_PARITY)
#define DO_OVERRUN_ERR (1 << TTY_OVERRUN)
#define DO_BREAK (1 << TTY_BREAK)
#define FLAGS_MASK (DO_FRAME_ERR | DO_PARITY_ERR | DO_OVERRUN_ERR | DO_BREAK)
struct serialsim_intf {
struct uart_port port;
/* We need a device for the interface, just use a platform device. */
struct platform_device *pdev;
/* idr we are stored in, or NULL if not in an idr (pipeb). */
struct idr *idr;
/* The driver we are registered against, NULL if none. */
struct uart_driver *driver;
/*
* This is my transmit buffer, my thread picks this up and
* injects them into the other port's uart.
*/
unsigned char xmitbuf[SERIALSIM_XBUFSIZE];
struct circ_buf buf;
/* Error flags to send. */
bool break_reported;
unsigned int flags;
/* Modem state. */
unsigned int mctrl;
bool do_null_modem;
spinlock_t mctrl_lock;
struct tasklet_struct mctrl_tasklet;
/*
* The modem control state handling is not perfect.
* Currently, if data is being transferred in the thread, it
* will delay the modemcontrol until all the data is
* transferred. Otherwise it will deliver the data
* immediately in the tasklet.
*
* The problem is that there is a race between setting the
* mctrl and transferring the data. When the last byte is
* read from the origin serial port on a close, the modem
* control lines are set immediately by close code. But you
* want the data being transferred into the receive buffer
* before you issue the modem control to the destination port.
*
* The current scheme doesn't handle multiple modem control
* changes, and the modem control changes should really ride
* with the data to be after specific bytes. But the current
* scheme works ok.
*/
/* Modem state to send to the other side. */
bool mctrl_pending;
bool hold_mctrl;
unsigned int new_mctrl;
/* My transmitter is enabled. */
bool tx_enabled;
/* I can receive characters. */
bool rx_enabled;
/*
* The serial echo port on the other side of this pipe (or points
* to myself in loopback mode.
*/
struct serialsim_intf *ointf;
unsigned int div;
unsigned int bytes_per_interval;
unsigned int per_interval_residual;
struct ktermios termios;
const char *threadname;
struct task_struct *thread;
struct serial_rs485 rs485;
};
#define circ_sbuf_space(buf) CIRC_SPACE((buf)->head, (buf)->tail, \
SERIALSIM_XBUFSIZE)
#define circ_sbuf_empty(buf) ((buf)->head == (buf)->tail)
#define circ_sbuf_next(idx) (((idx) + 1) % SERIALSIM_XBUFSIZE)
static struct serialsim_intf *serialsim_port_to_intf(struct uart_port *port)
{
return container_of(port, struct serialsim_intf, port);
}
static unsigned int serialsim_tx_empty(struct uart_port *port)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
if (circ_sbuf_empty(&intf->buf))
return TIOCSER_TEMT;
return 0;
}
/*
* We have to lock multiple locks, make sure to do it in the same order all
* the time.
*/
static void serialsim_null_modem_lock_irq(struct serialsim_intf *intf)
__acquires(intf->port.lock) __acquires(intf->mctrl_lock)
__acquires(intf->ointf->mctrl_lock)
{
spin_lock_irq(&intf->port.lock);
if (intf == intf->ointf) {
spin_lock(&intf->mctrl_lock);
} else if (intf < intf->ointf) {
spin_lock(&intf->mctrl_lock);
spin_lock(&intf->ointf->mctrl_lock);
} else {
spin_lock(&intf->ointf->mctrl_lock);
spin_lock(&intf->mctrl_lock);
}
}
static void serialsim_null_modem_unlock_irq(struct serialsim_intf *intf)
__releases(intf->port.lock) __releases(intf->mctrl_lock)
__releases(intf->ointf->mctrl_lock)
{
if (intf == intf->ointf) {
spin_unlock(&intf->mctrl_lock);
} else {
/* Order doesn't matter here. */
spin_unlock(&intf->mctrl_lock);
spin_unlock(&intf->ointf->mctrl_lock);
}
spin_unlock_irq(&intf->port.lock);
}
/*
* This must be called holding intf->port.lock and intf->mctrl_lock.
*/
static void _serialsim_set_modem_lines(struct serialsim_intf *intf,
unsigned int mask,
unsigned int new_mctrl)
{
unsigned int changes;
unsigned int mctrl = (intf->mctrl & ~mask) | (new_mctrl & mask);
if (mctrl == intf->mctrl)
return;
if (!intf->rx_enabled) {
intf->mctrl = mctrl;
return;
}
changes = mctrl ^ intf->mctrl;
intf->mctrl = mctrl;
if (changes & TIOCM_CAR)
uart_handle_dcd_change(&intf->port, mctrl & TIOCM_CAR);
if (changes & TIOCM_CTS)
uart_handle_cts_change(&intf->port, mctrl & TIOCM_CTS);
if (changes & TIOCM_RNG)
intf->port.icount.rng++;
if (changes & TIOCM_DSR)
intf->port.icount.dsr++;
}
#define NULL_MODEM_MCTRL (TIOCM_CAR | TIOCM_CTS | TIOCM_DSR)
#define LOCAL_MCTRL (NULL_MODEM_MCTRL | TIOCM_RNG)
/*
* Must be called holding intf->port.lock, intf->mctrl_lock, and
* intf->ointf.mctrl_lock.
*/
static void serialsim_handle_null_modem_update(struct serialsim_intf *intf)
{
unsigned int mctrl = 0;
/* Pull the values from the remote side for myself. */
if (intf->ointf->mctrl & TIOCM_DTR)
mctrl |= TIOCM_CAR | TIOCM_DSR;
if (intf->ointf->mctrl & TIOCM_RTS)
mctrl |= TIOCM_CTS;
_serialsim_set_modem_lines(intf, NULL_MODEM_MCTRL, mctrl);
}
static void serialsim_set_null_modem(struct serialsim_intf *intf, bool val)
{
serialsim_null_modem_lock_irq(intf);
if (!!val == !!intf->do_null_modem)
goto out_unlock;
if (!val) {
intf->do_null_modem = false;
goto out_unlock;
}
/* Enabling NULL modem. */
intf->do_null_modem = true;
serialsim_handle_null_modem_update(intf);
out_unlock:
serialsim_null_modem_unlock_irq(intf);
}
static void serialsim_set_modem_lines(struct serialsim_intf *intf,
unsigned int mask,
unsigned int new_mctrl)
{
mask &= LOCAL_MCTRL;
spin_lock_irq(&intf->port.lock);
spin_lock(&intf->mctrl_lock);
if (intf->do_null_modem)
mask &= ~NULL_MODEM_MCTRL;
_serialsim_set_modem_lines(intf, mask, new_mctrl);
spin_unlock(&intf->mctrl_lock);
spin_unlock_irq(&intf->port.lock);
}
static void mctrl_tasklet(unsigned long data)
{
struct serialsim_intf *intf = (void *) data;
serialsim_null_modem_lock_irq(intf);
if (intf->ointf->do_null_modem)
serialsim_handle_null_modem_update(intf);
serialsim_null_modem_unlock_irq(intf);
}
#define SETTABLE_MCTRL (TIOCM_RTS | TIOCM_DTR)
static void serialsim_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
bool do_tasklet = false;
spin_lock(&intf->mctrl_lock);
if (circ_sbuf_empty(&intf->buf) && !intf->mctrl_pending &&
!intf->hold_mctrl) {
intf->mctrl &= ~SETTABLE_MCTRL;
intf->mctrl |= mctrl & SETTABLE_MCTRL;
do_tasklet = true;
} else {
intf->mctrl_pending = true;
intf->new_mctrl = mctrl;
}
spin_unlock(&intf->mctrl_lock);
/*
* We are called holding port->lock, but we must be able to claim
* intf->ointf->port.lock, and that can result in deadlock. So
* we have to run this elsewhere. Note that we run the other
* end's tasklet.
*/
if (do_tasklet)
tasklet_schedule(&intf->ointf->mctrl_tasklet);
}
static unsigned int serialsim_get_mctrl(struct uart_port *port)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
unsigned int rv;
spin_lock(&intf->mctrl_lock);
rv = intf->mctrl;
spin_unlock(&intf->mctrl_lock);
return rv;
}
static void serialsim_stop_tx(struct uart_port *port)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
intf->tx_enabled = false;
}
static void serialsim_set_baud_rate(struct serialsim_intf *intf,
unsigned int baud, unsigned int cflag)
{
unsigned int bits_per_char;
switch (cflag & CSIZE) {
case CS5:
bits_per_char = 7;
break;
case CS6:
bits_per_char = 8;
break;
case CS7:
bits_per_char = 9;
break;
default:
bits_per_char = 10;
break; /* CS8 and others. */
}
if (cflag & CSTOPB)
bits_per_char++;
intf->div = SERIALSIM_WAKES_PER_SEC * bits_per_char;
intf->bytes_per_interval = baud / intf->div;
intf->per_interval_residual = baud % intf->div;
}
static void serialsim_transfer_data(struct uart_port *port,
struct circ_buf *tbuf)
{
struct circ_buf *cbuf = &port->state->xmit;
while (!uart_circ_empty(cbuf) && circ_sbuf_space(tbuf)) {
unsigned char c = cbuf->buf[cbuf->tail];
cbuf->tail = (cbuf->tail + 1) % UART_XMIT_SIZE;
tbuf->buf[tbuf->head] = c;
tbuf->head = circ_sbuf_next(tbuf->head);
port->icount.tx++;
}
if (uart_circ_chars_pending(cbuf) < WAKEUP_CHARS)
uart_write_wakeup(port);
}
static unsigned int serialsim_get_flag(struct serialsim_intf *intf,
unsigned int *status)
{
unsigned int flags = intf->flags;
*status = flags;
/* Overrun is always reported through a different way. */
if (flags & DO_OVERRUN_ERR) {
intf->port.icount.overrun++;
intf->flags &= ~DO_OVERRUN_ERR;
}
/* Break is handled separately. */
if (flags & DO_FRAME_ERR) {
intf->port.icount.frame++;
intf->flags &= ~DO_FRAME_ERR;
return TTY_FRAME;
}
if (flags & DO_PARITY_ERR) {
intf->port.icount.parity++;
intf->flags &= ~DO_PARITY_ERR;
return TTY_PARITY;
}
return TTY_NORMAL;
}
static void serialsim_set_flags(struct serialsim_intf *intf,
unsigned int status)
{
spin_lock_irq(&intf->port.lock);
intf->flags |= status;
spin_unlock_irq(&intf->port.lock);
}
static void serialsim_thread_delay(void)
{
#ifdef CONFIG_HIGH_RES_TIMERS
ktime_t timeout;
timeout = ns_to_ktime(1000000000 / SERIALSIM_WAKES_PER_SEC);
set_current_state(TASK_INTERRUPTIBLE);
schedule_hrtimeout(&timeout, HRTIMER_MODE_REL);
#else
schedule_timeout_interruptible(1);
#endif
}
static int serialsim_thread(void *data)
{
struct serialsim_intf *intf = data;
struct serialsim_intf *ointf = intf->ointf;
struct uart_port *port = &intf->port;
struct uart_port *oport = &ointf->port;
struct circ_buf *tbuf = &intf->buf;
unsigned int residual = 0;
while (!kthread_should_stop()) {
unsigned int to_send, i;
unsigned int div;
unsigned int flag;
unsigned int status = 0;
bool mctrl_pending;
unsigned int new_mctrl;
spin_lock_irq(&intf->mctrl_lock);
intf->hold_mctrl = true;
spin_unlock_irq(&intf->mctrl_lock);
spin_lock_irq(&oport->lock);
if (ointf->tx_enabled && oport->state->xmit.buf)
/*
* Move bytes from the other port's transmit buffer to
* the interface buffer.
*/
serialsim_transfer_data(oport, tbuf);
spin_unlock_irq(&oport->lock);
/*
* Calculate how many bytes to send based on the
* simulated serial speed.
*/
to_send = intf->bytes_per_interval;
residual += intf->per_interval_residual;
div = intf->div;
if (residual >= div) {
residual -= div;
to_send++;
}
/*
* Move from the interface buffer into my receive
* buffer.
*/
spin_lock_irq(&port->lock);
if (intf->rx_enabled) {
if (intf->flags & DO_BREAK && !intf->break_reported) {
intf->port.icount.brk++;
intf->break_reported = true;
uart_insert_char(port, intf->flags,
DO_OVERRUN_ERR, 0, TTY_BREAK);
if (to_send == 0) {
to_send = 1; /* Force a push. */
goto skip_send;
}
}
for (i = 0; i < to_send && !circ_sbuf_empty(tbuf);
i++) {
unsigned char c = tbuf->buf[tbuf->tail];
tbuf->tail = circ_sbuf_next(tbuf->tail);
flag = serialsim_get_flag(intf, &status);
port->icount.rx++;
uart_insert_char(port, status,
DO_OVERRUN_ERR,
c, flag);
}
}
skip_send:
spin_unlock_irq(&port->lock);
if (to_send)
tty_flip_buffer_push(&port->state->port);
spin_lock_irq(&intf->mctrl_lock);
intf->hold_mctrl = false;
mctrl_pending = intf->mctrl_pending;
new_mctrl = intf->new_mctrl;
intf->mctrl_pending = false;
if (mctrl_pending) {
intf->mctrl &= ~SETTABLE_MCTRL;
intf->mctrl |= new_mctrl & SETTABLE_MCTRL;
}
spin_unlock_irq(&intf->mctrl_lock);
if (mctrl_pending)
mctrl_tasklet((unsigned long) ointf);
serialsim_thread_delay();
}
return 0;
}
static void serialsim_start_tx(struct uart_port *port)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
intf->tx_enabled = true;
}
static void serialsim_stop_rx(struct uart_port *port)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
intf->rx_enabled = false;
}
static void serialsim_break_ctl(struct uart_port *port, int break_state)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
struct serialsim_intf *ointf = intf->ointf;
spin_lock_irq(&ointf->port.lock);
if (!break_state && ointf->flags & DO_BREAK) {
/* Turning break off. */
ointf->break_reported = false;
ointf->flags &= ~DO_BREAK;
} else if (break_state) {
ointf->flags |= DO_BREAK;
}
spin_unlock_irq(&ointf->port.lock);
}
static int serialsim_startup(struct uart_port *port)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
int rv = 0;
struct circ_buf *cbuf = &port->state->xmit;
unsigned long flags;
/*
* This is technically wrong, but otherwise tests using it
* can get stale data.
*/
spin_lock_irqsave(&port->lock, flags);
cbuf->head = cbuf->tail = 0;
spin_unlock_irqrestore(&port->lock, flags);
intf->buf.head = intf->buf.tail = 0;
intf->thread = kthread_run(serialsim_thread, intf,
"%s%d", intf->threadname, port->line);
if (IS_ERR(intf->thread)) {
rv = PTR_ERR(intf->thread);
intf->thread = NULL;
pr_err("serialsim: Could not start thread: %d", rv);
} else {
spin_lock_irqsave(&port->lock, flags);
intf->tx_enabled = true;
intf->rx_enabled = true;
serialsim_set_baud_rate(intf, 9600, CS8);
spin_unlock_irqrestore(&port->lock, flags);
}
return rv;
}
static void serialsim_shutdown(struct uart_port *port)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
unsigned long flags;
spin_lock_irqsave(&port->lock, flags);
spin_unlock_irqrestore(&port->lock, flags);
kthread_stop(intf->thread);
}
static void serialsim_release_port(struct uart_port *port)
{
}
static void
serialsim_set_termios(struct uart_port *port, struct ktermios *termios,
CONST_KTERMIOS struct ktermios *old)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
unsigned int baud = uart_get_baud_rate(port, termios, old,
10, 100000000);
unsigned long flags;
spin_lock_irqsave(&port->lock, flags);
serialsim_set_baud_rate(intf, baud, termios->c_cflag);
intf->termios = *termios;
spin_unlock_irqrestore(&port->lock, flags);
}
static int serialsim_rs485(struct uart_port *port, RS485_HAS_TERMIOS
struct serial_rs485 *newrs485)
{
struct serialsim_intf *intf = serialsim_port_to_intf(port);
intf->rs485 = *newrs485;
return 0;
}
static const char *serialecho_type(struct uart_port *port)
{
return "SerialEcho";
}
static const char *serialpipea_type(struct uart_port *port)
{
return "SerialPipeA";
}
static const char *serialpipeb_type(struct uart_port *port)
{
return "SerialPipeB";
}
static void serialecho_config_port(struct uart_port *port, int type)
{
port->type = PORT_SERIALECHO;
}
static void serialpipea_config_port(struct uart_port *port, int type)
{
port->type = PORT_SERIALPIPEA;
}
static void serialpipeb_config_port(struct uart_port *port, int type)
{
port->type = PORT_SERIALPIPEB;
}
static int serialpipe_ioctl(struct uart_port *port, unsigned int cmd,
unsigned long arg)
{
int rv = 0;
struct serialsim_intf *intf = serialsim_port_to_intf(port);
unsigned int mask;
int val;
switch (cmd) {
case TIOCSERSREMNULLMODEM:
serialsim_set_null_modem(intf->ointf, !!arg);
break;
case TIOCSERGREMNULLMODEM:
val = intf->ointf->do_null_modem;
if (copy_to_user((int __user *) arg, &val, sizeof(int)))
rv = -EFAULT;
break;
case TIOCSERSREMMCTRL:
mask = (arg >> 16) & 0xffff;
arg &= 0xffff;
if (mask & ~LOCAL_MCTRL || arg & ~LOCAL_MCTRL)
rv = -EINVAL;
else
serialsim_set_modem_lines(intf->ointf, mask, arg);
break;
case TIOCSERGREMMCTRL:
if (copy_to_user((unsigned int __user *) arg,
&intf->ointf->mctrl,
sizeof(unsigned int)))
rv = -EFAULT;
break;
case TIOCSERSREMERR:
if (arg & ~FLAGS_MASK)
rv = -EINVAL;
else
serialsim_set_flags(intf, arg);
break;
case TIOCSERGREMERR:
if (copy_to_user((unsigned int __user *) arg, &intf->flags,
sizeof(unsigned int)))
rv = -EFAULT;
break;
case TIOCSERGREMTERMIOS:
{
struct ktermios otermios;
spin_lock_irq(&intf->ointf->port.lock);
otermios = intf->ointf->termios;
spin_unlock_irq(&intf->ointf->port.lock);
#ifdef TCGETS2
rv = kernel_termios_to_user_termios((struct termios2 __user *)
arg,
&otermios);
#else
rv = kernel_termios_to_user_termios((struct termios __user *)
arg,
&otermios);
#endif
if (rv)
rv = -EFAULT;
break;
}
case TIOCSERGREMRS485:
{
struct serial_rs485 ors485;
spin_lock_irq(&intf->ointf->port.lock);
ors485 = intf->ointf->rs485;
spin_unlock_irq(&intf->ointf->port.lock);
if (copy_to_user((struct serial_rs485 __user *) arg,
&ors485, sizeof(ors485)))
rv = -EFAULT;
break;
}
default:
rv = -ENOIOCTLCMD;
}
return rv;
}
static unsigned int nr_echo_ports = 4;
module_param(nr_echo_ports, uint, 0444);
MODULE_PARM_DESC(nr_echo_ports,
"The number of echo ports to create. Defaults to 4");
static unsigned int nr_dyn_echo_ports = 16;
module_param(nr_dyn_echo_ports, uint, 0444);
MODULE_PARM_DESC(nr_dyn_echo_ports,
"Max dynamic echo ports available. Defaults to 16");
static unsigned int nr_pipe_ports = 4;
module_param(nr_pipe_ports, uint, 0444);
MODULE_PARM_DESC(nr_pipe_ports,
"The number of pipe ports to create. Defaults to 4");
static unsigned int nr_dyn_pipe_ports = 16;
module_param(nr_dyn_pipe_ports, uint, 0444);
MODULE_PARM_DESC(nr_dyn_pipe_ports,
"Max dynamic pipe ports available. Defaults to 16");
static char *gettok(char **s)
{
char *t = skip_spaces(*s);
char *p = t;
while (*p && !isspace(*p))
p++;
if (*p)
*p++ = '\0';
*s = p;
return t;
}
static bool tokeq(const char *t, const char *m)
{
return strcmp(t, m) == 0;
}
static unsigned int parse_modem_line(char op, unsigned int flag,
unsigned int *mctrl)
{
if (op == '+')
*mctrl |= flag;
else
*mctrl &= ~flag;
return flag;
}
static void serialsim_ctrl_append(char **val, int *left, char *n, bool enabled)
{
int count;
count = snprintf(*val, *left, " %c%s", enabled ? '+' : '-', n);
*left -= count;
*val += count;
}
static ssize_t serialsim_ctrl_read(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct tty_port *tport = dev_get_drvdata(dev);
struct uart_state *state = container_of(tport, struct uart_state, port);
struct uart_port *port = state->uart_port;
struct serialsim_intf *intf = serialsim_port_to_intf(port);
unsigned int mctrl = intf->mctrl;
char *val = buf;
int left = PAGE_SIZE;
serialsim_ctrl_append(&val, &left, "nullmodem", intf->do_null_modem);
serialsim_ctrl_append(&val, &left, "cd", mctrl & TIOCM_CAR);
serialsim_ctrl_append(&val, &left, "dsr", mctrl & TIOCM_DSR);
serialsim_ctrl_append(&val, &left, "cts", mctrl & TIOCM_CTS);
serialsim_ctrl_append(&val, &left, "ring", mctrl & TIOCM_RNG);
serialsim_ctrl_append(&val, &left, "dtr", mctrl & TIOCM_DTR);
serialsim_ctrl_append(&val, &left, "rts", mctrl & TIOCM_RTS);
*val++ = '\n';
return val - buf;
}
static ssize_t serialsim_ctrl_write(struct device *dev,
struct device_attribute *attr,
const char *val, size_t count)
{
struct tty_port *tport = dev_get_drvdata(dev);
struct uart_state *state = container_of(tport, struct uart_state, port);
struct uart_port *port = state->uart_port;
struct serialsim_intf *intf = serialsim_port_to_intf(port);
char *str = kstrndup(val, count, GFP_KERNEL);
char *p, *s = str;
int rv = count;
unsigned int flags = 0;
unsigned int nullmodem = 0;
unsigned int mctrl_mask = 0, mctrl = 0;
if (!str)
return -ENOMEM;
p = gettok(&s);
while (*p) {
char op = '\0';
int err = 0;
switch (*p) {
case '+':
case '-':
op = *p++;
break;
default:
break;
}
if (tokeq(p, "frame"))
flags |= DO_FRAME_ERR;
else if (tokeq(p, "parity"))
flags |= DO_PARITY_ERR;
else if (tokeq(p, "overrun"))
flags |= DO_OVERRUN_ERR;
else if (tokeq(p, "nullmodem"))
nullmodem = op;
else if (tokeq(p, "dsr"))
mctrl_mask |= parse_modem_line(op, TIOCM_DSR, &mctrl);
else if (tokeq(p, "cts"))
mctrl_mask |= parse_modem_line(op, TIOCM_CTS, &mctrl);
else if (tokeq(p, "cd"))
mctrl_mask |= parse_modem_line(op, TIOCM_CAR, &mctrl);
else if (tokeq(p, "ring"))
mctrl_mask |= parse_modem_line(op, TIOCM_RNG, &mctrl);
else
err = -EINVAL;
if (err) {
rv = err;
goto out;
}
p = gettok(&s);
}
if (flags)
serialsim_set_flags(intf, flags);
if (nullmodem)
serialsim_set_null_modem(intf, nullmodem == '+');
if (mctrl_mask)
serialsim_set_modem_lines(intf, mctrl_mask, mctrl);
out:
kfree(str);
return rv;
}
static DEVICE_ATTR(ctrl, 0660, serialsim_ctrl_read, serialsim_ctrl_write);
static struct attribute *serialsim_dev_attrs[] = {
&dev_attr_ctrl.attr,
NULL,
};
/*
* We pass this into the uart_port and it does the register. It's
* not const in uart_port, so we can't make it const here.
*/
static struct attribute_group serialsim_dev_attr_group = {
.attrs = serialsim_dev_attrs,
};
static const struct uart_ops serialecho_ops = {
.tx_empty = serialsim_tx_empty,
.set_mctrl = serialsim_set_mctrl,
.get_mctrl = serialsim_get_mctrl,
.stop_tx = serialsim_stop_tx,
.start_tx = serialsim_start_tx,
.stop_rx = serialsim_stop_rx,
.break_ctl = serialsim_break_ctl,
.startup = serialsim_startup,
.shutdown = serialsim_shutdown,
.release_port = serialsim_release_port,
.set_termios = serialsim_set_termios,
.type = serialecho_type,
.config_port = serialecho_config_port
};
static const struct uart_ops serialpipea_ops = {
.tx_empty = serialsim_tx_empty,
.set_mctrl = serialsim_set_mctrl,
.get_mctrl = serialsim_get_mctrl,
.stop_tx = serialsim_stop_tx,
.start_tx = serialsim_start_tx,
.stop_rx = serialsim_stop_rx,
.break_ctl = serialsim_break_ctl,
.startup = serialsim_startup,
.shutdown = serialsim_shutdown,
.release_port = serialsim_release_port,
.set_termios = serialsim_set_termios,
.type = serialpipea_type,
.config_port = serialpipea_config_port,
.ioctl = serialpipe_ioctl
};
static const struct uart_ops serialpipeb_ops = {
.tx_empty = serialsim_tx_empty,
.set_mctrl = serialsim_set_mctrl,
.get_mctrl = serialsim_get_mctrl,
.stop_tx = serialsim_stop_tx,
.start_tx = serialsim_start_tx,
.stop_rx = serialsim_stop_rx,
.break_ctl = serialsim_break_ctl,
.startup = serialsim_startup,
.shutdown = serialsim_shutdown,
.release_port = serialsim_release_port,
.set_termios = serialsim_set_termios,
.type = serialpipeb_type,
.config_port = serialpipeb_config_port,
.ioctl = serialpipe_ioctl
};
static struct uart_driver serialecho_driver = {
.owner = THIS_MODULE,
.driver_name = "ttyEcho",
.dev_name = "ttyEcho"
};
static struct uart_driver serialpipea_driver = {
.owner = THIS_MODULE,
.driver_name = "ttyPipeA",
.dev_name = "ttyPipeA"
};