forked from Baael/moxa-nport-driver-3.11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npreal2.c
5309 lines (4872 loc) · 144 KB
/
npreal2.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
/*
* npreal2.c -- MOXA NPort Server family Real TTY driver.
*
* Copyright (C) 1999-2012 Moxa Inc. (support@moxa.com.tw).
*
* This code is loosely based on the Linux serial driver, written by
* Linus Torvalds, Theodore T'so and others.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
*/
#ifdef MODVERSIONS
#ifndef MODULE
#define MODULE
#endif
#endif
#define JOY 1
#include <linux/version.h>
#define VERSION_CODE(ver,rel,seq) ((ver << 16) | (rel << 8) | seq)
#ifndef _DEBIAN_
#ifdef MODULE
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,18))
#include <linux/config.h>
#endif
#ifdef MODVERSIONS
#include <linux/modversions.h>
#endif
#include <linux/module.h>
#else
#define MOD_INC_USE_COUNT
#define MOD_DEC_USE_COUNT
#endif
#include <linux/init.h>
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,32))
#include <linux/autoconf.h>
#endif
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/serial.h>
#include <linux/serial_reg.h>
#include <linux/major.h>
#include <linux/string.h>
#include <linux/fcntl.h>
#include <linux/ptrace.h>
#include <linux/ioport.h>
#include <linux/mm.h>
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,39))
#include <linux/smp_lock.h>
#endif
#include <linux/proc_fs.h>
#else
#ifdef MODULE
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,18))
#include <config/config.h>
#endif
#ifdef MODVERSIONS
#include <config/modversions.h>
#endif
#include <config/module.h>
#else
#define MOD_INC_USE_COUNT
#define MOD_DEC_USE_COUNT
#endif
#include <config/init.h>
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,32))
#include <config/autoconf.h>
#endif
#include <config/errno.h>
#include <config/signal.h>
#include <config/sched.h>
#include <config/timer.h>
#include <config/interrupt.h>
#include <config/tty.h>
#include <config/tty_flip.h>
#include <config/serial.h>
#include <config/serial_reg.h>
#include <config/major.h>
#include <config/string.h>
#include <config/fcntl.h>
#include <config/ptrace.h>
#include <config/ioport.h>
#include <config/mm.h>
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,39))
#include <config/smp_lock.h>
#endif
#include <config/proc_fs.h>
#endif
#define NPREAL_VERSION "1.18.29 Build 14052013"
#if (LINUX_VERSION_CODE > VERSION_CODE(2,6,36))
/* include/linux/semaphore.h modification */
#define init_MUTEX(sem) sema_init(sem, 1)
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,1,0))
#define copy_from_user memcpy_fromfs
#define copy_to_user memcpy_tofs
#define put_to_user(arg1, arg2) put_fs_long(arg1, (unsigned long *)arg2)
#define get_from_user(arg1, arg2) arg1 = get_fs_long((unsigned long *)arg2)
#define schedule_timeout(x) {current->timeout = jiffies + (x); schedule();}
#define signal_pending(x) ((x)->signal & ~(x)->blocked)
#else
#include <asm/uaccess.h>
#include <linux/poll.h>
#define put_to_user(arg1, arg2) put_user(arg1, arg2)
#define get_from_user(arg1, arg2) get_user(arg1, arg2)
#endif
#include "npreal2.h"
# if (LINUX_VERSION_CODE > VERSION_CODE(3,10,0))
#include <linux/slab.h>
#endif
#define NPREAL_EVENT_TXLOW 1
#define NPREAL_EVENT_HANGUP 2
#define SERIAL_DO_RESTART
#define SERIAL_TYPE_NORMAL 1
#define SERIAL_TYPE_CALLOUT 2
#define WAKEUP_CHARS 256
#ifndef MAX_SCHEDULE_TIMEOUT
#define MAX_SCHEDULE_TIMEOUT ((long)(~0UL>>1))
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
#define PORTNO(x) (MINOR((x)->device) - (x)->driver.minor_start)
#else
#define PORTNO(x) ((x)->index)
#endif
#define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
#ifndef MIN
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#endif
#define NPREALMAJOR 33
#define NPREALCUMAJOR 38
static int ttymajor=NPREALMAJOR;
static int calloutmajor=NPREALCUMAJOR;
static int verbose=1;
int MXDebugLevel = MX_DEBUG_ERROR;
#ifdef MODULE
/* Variables for insmod */
# if (LINUX_VERSION_CODE > VERSION_CODE(2,1,11))
MODULE_AUTHOR("Moxa Tech.,www.moxa.com.tw");
MODULE_DESCRIPTION("MOXA Async/NPort Server Family Real TTY Driver");
# if (LINUX_VERSION_CODE < VERSION_CODE(2,6,6))
MODULE_PARM(ttymajor, "i");
MODULE_PARM(calloutmajor, "i");
MODULE_PARM(verbose, "i");
# else
module_param(ttymajor, int, 0);
module_param(calloutmajor, int, 0);
module_param(verbose, int, 0644);
MODULE_VERSION(NPREAL_VERSION);
# endif
# if (LINUX_VERSION_CODE > VERSION_CODE(2,4,0))
#ifdef MODULE_LICENSE
MODULE_LICENSE("GPL");
#endif
# endif
# endif
#endif /* MODULE */
#define NPREAL_PORTS 256
#define DE211 211
#define DE311 311
#define DE301 301
#define DE302 302
#define DE304 304
#define DE331 331
#define DE332 332
#define DE334 334
#define DE303 303
#define DE308 308
#define DE309 309
#define CN2100 2100
#define CN2500 2500
#ifndef B921600
#define B921600 (B460800 + 1)
#endif
#define NPREAL_ASPP_COMMAND_SET 1
#define NPREAL_LOCAL_COMMAND_SET 2
// local command set
#define LOCAL_CMD_TTY_USED 1
#define LOCAL_CMD_TTY_UNUSED 2
#define NPREAL_NET_CONNECTED 3
#define NPREAL_NET_DISCONNECTED 4
#define NPREAL_NET_SETTING 5
#define NPREAL_NET_GET_TTY_STATUS 6
#define NPREAL_CMD_TIMEOUT 10*HZ // 10 seconds
#define NPREAL_NET_CMD_RETRIEVE 1
#define NPREAL_NET_CMD_RESPONSE 2
#define NPREAL_NET_NODE_OPENED 0x01
#define NPREAL_NET_NODE_CONNECTED 0x02
#define NPREAL_NET_NODE_DISCONNECTED 0x04
#define NPREAL_NET_DO_SESSION_RECOVERY 0x08
#define NPREAL_NET_DO_INITIALIZE 0x10
#define NPREAL_NET_TTY_INUSED 0x20
// ASPP command set
#define ASPP_NOTIFY 0x26
#define ASPP_NOTIFY_PARITY 0x01
#define ASPP_NOTIFY_FRAMING 0x02
#define ASPP_NOTIFY_HW_OVERRUN 0x04
#define ASPP_NOTIFY_SW_OVERRUN 0x08
#define ASPP_NOTIFY_BREAK 0x10
#define ASPP_NOTIFY_MSR_CHG 0x20
#define ASPP_CMD_IOCTL 16
#define ASPP_CMD_FLOWCTRL 17
#define ASPP_CMD_LSTATUS 19
#define ASPP_CMD_LINECTRL 18
#define ASPP_CMD_FLUSH 20
#define ASPP_CMD_OQUEUE 22
#define ASPP_CMD_SETBAUD 23
#define ASPP_CMD_START_BREAK 33
#define ASPP_CMD_STOP_BREAK 34
#define ASPP_CMD_START_NOTIFY 36
#define ASPP_CMD_STOP_NOTIFY 37
#define ASPP_CMD_HOST 43
#define ASPP_CMD_PORT_INIT 44
#define ASPP_CMD_WAIT_OQUEUE 47
#define ASPP_CMD_IQUEUE 21
#define ASPP_CMD_XONXOFF 24
#define ASPP_CMD_PORT_RESET 32
#define ASPP_CMD_RESENT_TIME 46
#define ASPP_CMD_TX_FIFO 48
#define ASPP_CMD_SETXON 51
#define ASPP_CMD_SETXOFF 52
#define ASPP_FLUSH_RX_BUFFER 0
#define ASPP_FLUSH_TX_BUFFER 1
#define ASPP_FLUSH_ALL_BUFFER 2
#define ASPP_IOCTL_B300 0
#define ASPP_IOCTL_B600 1
#define ASPP_IOCTL_B1200 2
#define ASPP_IOCTL_B2400 3
#define ASPP_IOCTL_B4800 4
#define ASPP_IOCTL_B7200 5
#define ASPP_IOCTL_B9600 6
#define ASPP_IOCTL_B19200 7
#define ASPP_IOCTL_B38400 8
#define ASPP_IOCTL_B57600 9
#define ASPP_IOCTL_B115200 10
#define ASPP_IOCTL_B230400 11
#define ASPP_IOCTL_B460800 12
#define ASPP_IOCTL_B921600 13
#define ASPP_IOCTL_B150 14
#define ASPP_IOCTL_B134 15
#define ASPP_IOCTL_B110 16
#define ASPP_IOCTL_B75 17
#define ASPP_IOCTL_B50 18
#define ASPP_IOCTL_BITS8 3
#define ASPP_IOCTL_BITS7 2
#define ASPP_IOCTL_BITS6 1
#define ASPP_IOCTL_BITS5 0
#define ASPP_IOCTL_STOP1 0
#define ASPP_IOCTL_STOP2 4
#define ASPP_IOCTL_EVEN 8
#define ASPP_IOCTL_ODD 16
#define ASPP_IOCTL_MARK 24
#define ASPP_IOCTL_SPACE 32
#define ASPP_IOCTL_NONE 0
struct server_setting_struct
{
int32_t server_type;
int32_t disable_fifo;
};
struct npreal_struct
{
int port;
#if (LINUX_VERSION_CODE >= VERSION_CODE(3,7,0))
struct tty_port ttyPort;
#endif
int flags; /* defined in tty.h */
int type; /* UART type */
struct tty_struct * tty;
int xmit_fifo_size;
int custom_divisor;
unsigned long baud_base;
int x_char; /* xon/xoff character */
int close_delay;
unsigned short closing_wait;
int modem_control; /* Modem control register */
int modem_status; /* Line status */
unsigned long event;
int count; /* # of fd on device */
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,31))
struct pid* session;
struct pid* pgrp;
#else
pid_t session; /* Session of opening process */
pid_t pgrp; /* pgrp of opening process */
#endif
unsigned char *xmit_buf;
int xmit_head;
int xmit_tail;
int xmit_cnt;
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
struct tq_struct tqueue;
struct tq_struct process_flip_tqueue;
#else
struct work_struct tqueue;
struct work_struct process_flip_tqueue;
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,20))
struct termios normal_termios;
struct termios callout_termios;
#else
struct ktermios normal_termios;
struct ktermios callout_termios;
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,4,0))
struct wait_queue *open_wait;
struct wait_queue *close_wait;
struct wait_queue *delta_msr_wait;
#else
wait_queue_head_t open_wait;
wait_queue_head_t close_wait;
wait_queue_head_t delta_msr_wait;
#endif
struct async_icount icount; /* kernel counters for the 4 input interrupts */
struct nd_struct *net_node;
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,4,0))
/* We use spin_lock_irqsave instead of semaphonre here.
Reason: When we use pppd to dialout via Real TTY driver,
some driver functions, such as npreal_write(), would be
invoked under interrpute mode which causes warning in
down/up tx_semaphore.
*/
// struct semaphore tx_lock;
spinlock_t tx_lock;
struct semaphore rx_semaphore;
#else
struct semaphore tx_lock;
struct semaphore rx_semaphore;
#endif
};
struct nd_struct
{
int32_t server_type;
#if (LINUX_VERSION_CODE < VERSION_CODE(2,4,0))
struct wait_queue *initialize_wait;
struct wait_queue *select_in_wait;
struct wait_queue *select_out_wait;
struct wait_queue *select_ex_wait;
struct wait_queue *cmd_rsp_wait;
#else
wait_queue_head_t initialize_wait;
wait_queue_head_t select_in_wait;
wait_queue_head_t select_out_wait;
wait_queue_head_t select_ex_wait;
wait_queue_head_t cmd_rsp_wait;
#endif
int tx_ready;
int rx_ready;
int cmd_ready;
int wait_oqueue_responsed;
int oqueue;
unsigned char cmd_buffer[84];
unsigned char rsp_buffer[84];
struct semaphore cmd_semaphore;
int rsp_length;
unsigned long flag;
struct proc_dir_entry *node_entry;
struct npreal_struct *tty_node;
struct semaphore semaphore;
int do_session_recovery_len;
};
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
static struct tty_driver npvar_sdriver;
static struct tty_driver npvar_cdriver;
#else
static struct tty_driver *npvar_sdriver;
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
static int npvar_refcount;
#endif
static struct npreal_struct npvar_table[NPREAL_PORTS];
static struct nd_struct npvar_net_nodes[NPREAL_PORTS];
#if (LINUX_VERSION_CODE < VERSION_CODE(3,7,0))
static struct tty_struct * npvar_tty[NPREAL_PORTS];
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,20))
static struct termios * npvar_termios[NPREAL_PORTS];
static struct termios * npvar_termios_locked[NPREAL_PORTS];
#else
#if (LINUX_VERSION_CODE < VERSION_CODE(3,7,0))
static struct ktermios * npvar_termios[NPREAL_PORTS];
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(3,3,0))
static struct ktermios * npvar_termios_locked[NPREAL_PORTS];
#endif
#endif
static int npvar_diagflag;
static struct proc_dir_entry * npvar_proc_root;
/*
* npvar_tmp_buf is used as a temporary buffer by serial_write. We need
* to lock it in case the memcpy_fromfs blocks while swapping in a page,
* and some other program tries to do a serial write at the same time.
* Since the lock will only come under contention when the system is
* swapping and available memory is low, it makes sense to share one
* buffer across all the serial ports, since it significantly saves
* memory if large numbers of serial ports are open.
*/
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,4,0))
static unsigned char * npvar_tmp_buf;
static struct semaphore npvar_tmp_buf_sem;
#else
static unsigned char * npvar_tmp_buf = 0;
static struct semaphore npvar_tmp_buf_sem = MUTEX;
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,4,0))
static struct inode_operations npreal_net_iops;
#endif
static struct file_operations npreal_net_fops;
#ifdef MODULE
int init_module(void);
void cleanup_module(void);
#endif
static void npreal_disconnect(struct nd_struct *, char *, int *);
int npreal_init(void);
static int npreal_init_tty(void);
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,20))
static void npreal_do_softint(void *);
static void npreal_flush_to_ldisc(void *);
#else
static void npreal_do_softint(struct work_struct *work);
static void npreal_flush_to_ldisc(struct work_struct *work);
#endif
static int npreal_open(struct tty_struct *,struct file *);
static void npreal_close(struct tty_struct *,struct file *);
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,10))
static int npreal_write(struct tty_struct *,int,const unsigned char *,int);
#else
static int npreal_write(struct tty_struct *,const unsigned char *,int);
#endif
static int npreal_write_room(struct tty_struct *);
static void npreal_flush_buffer(struct tty_struct *);
static void npreal_ldisc_flush_buffer(struct tty_struct *);
static int npreal_chars_in_buffer(struct tty_struct *);
static void npreal_flush_chars(struct tty_struct *);
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,26))
static void npreal_put_char(struct tty_struct *,unsigned char);
#else
static int npreal_put_char(struct tty_struct *,unsigned char);
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,39))
static int npreal_ioctl(struct tty_struct *,struct file *,uint,ulong);
#else
static int npreal_ioctl(struct tty_struct *,uint,ulong);
#endif
static void npreal_throttle(struct tty_struct *);
static void npreal_unthrottle(struct tty_struct *);
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,20))
static void npreal_set_termios(struct tty_struct *,struct termios *);
static int npreal_port_init(struct npreal_struct *,struct termios *);
#else
static void npreal_set_termios(struct tty_struct *,struct ktermios *);
static int npreal_port_init(struct npreal_struct *,struct ktermios *);
#endif
static void npreal_stop(struct tty_struct *);
static void npreal_start(struct tty_struct *);
static void npreal_hangup(struct tty_struct *);
static inline void npreal_check_modem_status(struct npreal_struct *,int);
static int npreal_block_til_ready(struct tty_struct *,struct file *, struct npreal_struct *);
static int npreal_startup(struct npreal_struct *,struct file *, struct tty_struct *);
static void npreal_shutdown(struct npreal_struct *);
static int npreal_port_shutdown(struct npreal_struct *);
static int npreal_get_serial_info(struct npreal_struct *, struct serial_struct *);
static int npreal_set_serial_info(struct npreal_struct *, struct serial_struct *);
static int npreal_get_lsr_info(struct npreal_struct *,unsigned int *);
static void npreal_send_break(struct npreal_struct *,int);
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,39))
static int npreal_tiocmget(struct tty_struct *, struct file *);
static int npreal_tiocmset(struct tty_struct *, struct file *, unsigned int, unsigned int);
#else
static int npreal_tiocmget(struct tty_struct *);
static int npreal_tiocmset(struct tty_struct *, unsigned int, unsigned int);
#endif
#else
static int npreal_get_modem_info(struct npreal_struct *,unsigned int *);
static int npreal_set_modem_info(struct npreal_struct *,unsigned int, unsigned int *);
#endif
static void npreal_process_notify(struct nd_struct *,char *,int);
static void npreal_do_session_recovery(struct npreal_struct *);
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,1,0))
static void npreal_wait_until_sent(struct tty_struct *,int);
#endif
static int npreal_wait_and_set_command(struct nd_struct *,char,char);
static int npreal_wait_command_completed(struct nd_struct *,char,char, long,char *,int *);
static long npreal_wait_oqueue(struct npreal_struct *,long);
static int npreal_linectrl(struct nd_struct *nd,int modem_control);
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,27))
static int npreal_break(struct tty_struct * ttyinfo, int break_state);
#else
static void npreal_break(struct tty_struct * ttyinfo, int break_state);
#endif
static void npreal_start_break(struct nd_struct *nd);
static void npreal_stop_break(struct nd_struct *nd);
static int npreal_setxon_xoff(struct npreal_struct * info, int cmd);
/*
* File operation declarations
*/
static int npreal_net_open(struct inode *, struct file * );
#if (LINUX_VERSION_CODE > VERSION_CODE(2,6,35))
/* /include/linux/fs.h modification */
static long npreal_net_ioctl(struct file *,unsigned int, unsigned long );
#else
static int npreal_net_ioctl(struct inode *,struct file *,unsigned int, unsigned long );
#endif
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,1,0))
static int npreal_net_close(struct inode *,struct file * );
static ssize_t npreal_net_read (struct file *file,char *buf,size_t count, loff_t *ppos);
static ssize_t npreal_net_write(struct file *file,const char *buf, size_t count,loff_t *ppos);
static unsigned int npreal_net_select(struct file *file, struct poll_table_struct *);
#else
static void npreal_net_close(struct inode *,struct file * );
static int npreal_net_read (struct inode *,struct file *,char *,int);
static int npreal_net_write(struct inode *,struct file *,const char *,int);
static int npreal_net_select(struct inode *,struct file *file,int, select_table *);
#endif
/*
* "proc" table manipulation functions
*/
#if (LINUX_VERSION_CODE < VERSION_CODE(3,10,0))
static struct proc_dir_entry *npreal_create_proc_entry(const char *, mode_t, struct proc_dir_entry *);
static void npreal_remove_proc_entry( struct proc_dir_entry *);
#else
static void npreal_remove_proc_entry( struct proc_dir_entry *, int idx);
#endif
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
static struct tty_operations mpvar_ops =
{
.open = npreal_open,
.close = npreal_close,
.write = npreal_write,
.put_char = npreal_put_char,
.flush_chars = npreal_flush_chars,
.write_room = npreal_write_room,
.chars_in_buffer = npreal_chars_in_buffer,
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,26))
.flush_buffer = npreal_ldisc_flush_buffer,
.wait_until_sent = npreal_wait_until_sent,
.break_ctl = npreal_break,
#else
.flush_buffer = npreal_flush_buffer,
#endif
.ioctl = npreal_ioctl,
.throttle = npreal_throttle,
.unthrottle = npreal_unthrottle,
.set_termios = npreal_set_termios,
.stop = npreal_stop,
.start = npreal_start,
.hangup = npreal_hangup,
.tiocmget = npreal_tiocmget,
.tiocmset = npreal_tiocmset,
};
#endif
/*
* The MOXA NPort server Real TTY driver boot-time initialization code!
*/
#ifdef MODULE
INIT_FUNC_RET INIT_FUNC(void)
{
int ret;
DBGPRINT(MX_DEBUG_INFO, "Loading module npreal major(%d), coutmajor(%d)...\n", ttymajor, calloutmajor);
ret = npreal_init();
DBGPRINT(MX_DEBUG_INFO, "Done.\n");
return (ret);
}
CLEAR_FUNC_RET CLEAR_FUNC(void)
{
int i,err = 0;
struct npreal_struct *info;
struct proc_dir_entry *de;
info = &npvar_table[0];
for (i = 0; i < NPREAL_PORTS; i++,info++)
{
if (info->net_node)
{
#if (LINUX_VERSION_CODE >= VERSION_CODE(3,7,0))
/* Remove device node if it is opened*/
if(info->net_node->flag & NPREAL_NET_NODE_OPENED)
tty_unregister_device(DRV_VAR, i);
#endif
if ((de=((struct nd_struct *)(info->net_node))->node_entry))
#if (LINUX_VERSION_CODE < VERSION_CODE(3,10,0))
npreal_remove_proc_entry(de);
#else
npreal_remove_proc_entry(de, i);
#endif
((struct nd_struct *)(info->net_node))->node_entry = NULL;
}
}
if (npvar_proc_root)
{
#if (LINUX_VERSION_CODE < VERSION_CODE(3,10,0))
npreal_remove_proc_entry( npvar_proc_root);
#else
npreal_remove_proc_entry( npvar_proc_root, 404);
#endif
npvar_proc_root = NULL;
}
DBGPRINT(MX_DEBUG_INFO, "Unloading module npreal ...\n");
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
if ((err |= tty_unregister_driver(&npvar_cdriver)))
{
DBGPRINT(MX_DEBUG_ERROR, "Couldn't unregister MOXA Async/NPort server family Real TTY callout driver\n");
}
#endif
if ((err |= tty_unregister_driver(DRV_VAR)))
{
DBGPRINT(MX_DEBUG_ERROR, "Couldn't unregister MOXA Async/NPort server family Real TTY driver\n");
}
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
put_tty_driver(DRV_VAR);
#endif
DBGPRINT(MX_DEBUG_INFO, "Done.\n");
}
#endif
static int
npreal_init_tty(void)
{
struct npreal_struct *tty_node;
int i;
struct proc_dir_entry *de;
struct nd_struct *net_node;
char buf[4];
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,4,0))
init_MUTEX(&npvar_tmp_buf_sem);
#else
npvar_tmp_buf_sem = MUTEX;
#endif
//create "npreal2" dir
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,26))
npvar_proc_root = proc_mkdir("npreal2", NULL);
#else
npvar_proc_root = proc_mkdir("npreal2", &proc_root);
#endif
//npvar_proc_root = npreal_create_proc_entry( "npreal2",S_IFDIR, &proc_root);
if ( !npvar_proc_root )
return -ENOMEM;
tty_node = &npvar_table[0];
net_node = &npvar_net_nodes[0];
for ( i=0; i<NPREAL_PORTS; i++, tty_node++,net_node++ )
{
sprintf(buf,"%d",i);
#if (LINUX_VERSION_CODE < VERSION_CODE(3,10,0))
de = npreal_create_proc_entry(buf, S_IRUGO | S_IWUGO | S_IFREG,
npvar_proc_root);
if ( !de )
return -ENOMEM;
de->data = (void *) net_node;
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,4,0))
de->proc_fops = &npreal_net_fops;
#else
de->ops = &npreal_net_iops;
#endif /* 2,4,0 */
net_node->tty_node = tty_node;
net_node->node_entry = de;
net_node->flag = 0;
#else
de = proc_create_data(buf, S_IRUGO | S_IWUGO | S_IFREG,
npvar_proc_root, &npreal_net_fops, (void *) net_node);
if ( !de )
return -ENOMEM;
// PDE_DATA(de) = (void *) net_node;
net_node->tty_node = tty_node;
net_node->node_entry = de;
net_node->flag = 0;
#endif /* 3,10, 0 */
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,4,0))
init_MUTEX(&net_node->semaphore);
init_MUTEX(&net_node->cmd_semaphore);
init_waitqueue_head(&net_node->initialize_wait);
init_waitqueue_head(&net_node->select_in_wait);
init_waitqueue_head(&net_node->select_out_wait);
init_waitqueue_head(&net_node->select_ex_wait);
init_waitqueue_head(&net_node->cmd_rsp_wait);
#else
net_node->semaphore = MUTEX;
net_node->cmd_semaphore = MUTEX;
#endif
tty_node->net_node = net_node;
tty_node->port = i;
tty_node->type = PORT_16550A;
tty_node->flags = 0;
tty_node->xmit_fifo_size = 16;
tty_node->baud_base = 921600L;
tty_node->close_delay = 5*HZ/10;
tty_node->closing_wait = 30*HZ;
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,20))
INIT_WORK(&tty_node->tqueue, npreal_do_softint, tty_node);
INIT_WORK(&tty_node->process_flip_tqueue, npreal_flush_to_ldisc, tty_node);
#else
INIT_WORK(&tty_node->tqueue, npreal_do_softint);
INIT_WORK(&tty_node->process_flip_tqueue, npreal_flush_to_ldisc);
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
tty_node->callout_termios = npvar_cdriver.init_termios;
#endif
tty_node->normal_termios = DRV_VAR_P(init_termios);
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,4,0))
//init_MUTEX(&tty_node->tx_lock);
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,39))
tty_node->tx_lock = SPIN_LOCK_UNLOCKED;
#else
tty_node->tx_lock = __SPIN_LOCK_UNLOCKED(tty_node->tx_lock);
#endif
init_MUTEX(&tty_node->rx_semaphore);
init_waitqueue_head(&tty_node->open_wait);
init_waitqueue_head(&tty_node->close_wait);
init_waitqueue_head(&tty_node->delta_msr_wait);
#else
tty_node->tx_lock = MUTEX;
tty_node->rx_semaphore = MUTEX;
tty_node->modem_control = 0;
#endif
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,1,0))
tty_node->icount.rx = tty_node->icount.tx = 0;
#endif
tty_node->icount.cts = tty_node->icount.dsr = tty_node->icount.dcd = 0;
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,1,0))
tty_node->icount.frame = tty_node->icount.overrun =
tty_node->icount.brk = tty_node->icount.parity = 0;
#endif
}
return 0;
}
int
npreal_init(void)
{
int ret1, ret2;
#if (LINUX_VERSION_CODE >= VERSION_CODE(3,7,0))
int i;
#endif
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
npvar_sdriver = alloc_tty_driver(NPREAL_PORTS+1);
if (!npvar_sdriver)
return -ENOMEM;
#endif
printk("MOXA Async/NPort server family Real TTY driver ttymajor %d calloutmajor %d verbose %d (%s)\n", ttymajor, calloutmajor, verbose, NPREAL_VERSION);
/* Initialize the tty_driver structure */
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,28))
memset(DRV_VAR, 0, sizeof(struct tty_driver));
DRV_VAR_P(magic) = TTY_DRIVER_MAGIC;
DRV_VAR_P(num) = NPREAL_PORTS;
#endif
DRV_VAR_P(name) = "ttyr";
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,18))
DRV_VAR_P(devfs_name) = "tts/r";
#endif
#endif
DRV_VAR_P(major) = ttymajor;
DRV_VAR_P(minor_start) = 0;
DRV_VAR_P(type) = TTY_DRIVER_TYPE_SERIAL;
DRV_VAR_P(subtype) = SERIAL_TYPE_NORMAL;
DRV_VAR_P(init_termios) = tty_std_termios;
DRV_VAR_P(init_termios.c_cflag) = B9600|CS8|CREAD|HUPCL|CLOCAL;
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,18))
DRV_VAR_P(flags) = TTY_DRIVER_REAL_RAW | TTY_DRIVER_NO_DEVFS;
#else
DRV_VAR_P(flags) = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
#endif
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
tty_set_operations(DRV_VAR, &mpvar_ops);
#if (LINUX_VERSION_CODE < VERSION_CODE(3,7,0))
DRV_VAR_P(ttys) = npvar_tty;
#endif
#else
DRV_VAR_P(refcount) = &npvar_refcount;
DRV_VAR_P(table) = npvar_tty;
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(3,7,0))
DRV_VAR_P(termios) = npvar_termios;
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(3,3,0))
DRV_VAR_P(termios_locked) = npvar_termios_locked;
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,26))
DRV_VAR_P(open) = npreal_open;
DRV_VAR_P(close) = npreal_close;
DRV_VAR_P(write) = npreal_write;
DRV_VAR_P(put_char) = npreal_put_char;
DRV_VAR_P(flush_chars) = npreal_flush_chars;
DRV_VAR_P(write_room) = npreal_write_room;
DRV_VAR_P(chars_in_buffer) = npreal_chars_in_buffer;
DRV_VAR_P(flush_buffer) = npreal_ldisc_flush_buffer;
// DRV_VAR_P(flush_buffer) = npreal_flush_buffer;
DRV_VAR_P(ioctl) = npreal_ioctl;
DRV_VAR_P(throttle) = npreal_throttle;
DRV_VAR_P(unthrottle) = npreal_unthrottle;
DRV_VAR_P(set_termios) = npreal_set_termios;
DRV_VAR_P(stop) = npreal_stop;
DRV_VAR_P(start) = npreal_start;
DRV_VAR_P(hangup) = npreal_hangup;
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,1,0))
DRV_VAR_P(wait_until_sent) = npreal_wait_until_sent;
#endif
DRV_VAR_P(break_ctl) = npreal_break;
#endif
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
/*
* The callout device is just like normal device except for
* major number and the subtype code.
*/
npvar_cdriver = npvar_sdriver;
npvar_cdriver.name = "cur";
npvar_cdriver.major = calloutmajor;
npvar_cdriver.subtype = SERIAL_TYPE_CALLOUT;
#endif
DBGPRINT(MX_DEBUG_INFO, "Tty devices major number = %d, callout devices major number = %d\n",ttymajor,calloutmajor);
npvar_diagflag = 0;
memset(npvar_table, 0, NPREAL_PORTS * sizeof(struct npreal_struct));
#if (LINUX_VERSION_CODE >= VERSION_CODE(3,7,0))
for(i = 0; i < NPREAL_PORTS;i++)
{
tty_port_init(&npvar_table[i].ttyPort);
tty_port_link_device(&npvar_table[i].ttyPort, npvar_sdriver, i);
}
#endif
/*register driver*/
ret1 = 0;
ret2 = 0;
if ( !(ret1=tty_register_driver(DRV_VAR)) )
{
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
if ( (ret2=tty_register_driver(&npvar_cdriver)) )
{
tty_unregister_driver(DRV_VAR);
DBGPRINT(MX_DEBUG_ERROR, "Couldn't install MOXA Async/NPort server family Real TTY callout driver !\n");
}
#endif
}
else
{
DBGPRINT(MX_DEBUG_ERROR, "Couldn't install MOXA Async/NPort server family driver !\n");
}
if (ret1 || ret2)
{
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,6,0))
put_tty_driver(DRV_VAR);
#endif
return -1;
}
/* Initialize the net node structure */
memset(&npreal_net_fops,0,sizeof(struct file_operations));
npreal_net_fops.read = npreal_net_read;
npreal_net_fops.write = npreal_net_write;
#if (LINUX_VERSION_CODE > VERSION_CODE(2,6,35))
npreal_net_fops.unlocked_ioctl = npreal_net_ioctl;
#else
npreal_net_fops.ioctl = npreal_net_ioctl;
#endif
npreal_net_fops.open = npreal_net_open;
npreal_net_fops.release = npreal_net_close;
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,4,0))
npreal_net_fops.poll = npreal_net_select;
#else
#if (LINUX_VERSION_CODE >= VERSION_CODE(2,1,0))
npreal_net_fops.poll = npreal_net_select;
memset(&npreal_net_iops,0,sizeof(struct inode_operations));
npreal_net_iops.default_file_ops = &npreal_net_fops;
#else
npreal_net_fops.select = npreal_net_select;
memset(&npreal_net_iops,0,sizeof(struct inode_operations));
npreal_net_iops.default_file_ops = &npreal_net_fops;
#endif
#endif
if (npreal_init_tty() != 0)
{
tty_unregister_driver(DRV_VAR);
#if (LINUX_VERSION_CODE < VERSION_CODE(2,6,0))
tty_unregister_driver(&npvar_cdriver);
#endif
DBGPRINT(MX_DEBUG_ERROR, "Couldn't install MOXA Async/NPort server family Real TTY driver !\n");
}
return(0);
}
static void npreal_disconnect(struct nd_struct *nd, char *buf, int *size)
{
nd->cmd_buffer[0] = 0;
npreal_wait_and_set_command(nd,
NPREAL_LOCAL_COMMAND_SET,
LOCAL_CMD_TTY_UNUSED);
nd->cmd_buffer[2] = 0;
nd->cmd_ready = 1;
if (waitqueue_active(&nd->select_ex_wait)) {
wake_up_interruptible( &nd->select_ex_wait );
}
if (npreal_wait_command_completed(nd,
NPREAL_LOCAL_COMMAND_SET,
LOCAL_CMD_TTY_UNUSED,
NPREAL_CMD_TIMEOUT,
buf,
size) != 0) {
npreal_wait_and_set_command(nd,
NPREAL_LOCAL_COMMAND_SET,
LOCAL_CMD_TTY_UNUSED);
nd->cmd_buffer[2] = 0;
nd->cmd_ready = 1;
if (waitqueue_active(&nd->select_ex_wait)) {
wake_up_interruptible( &nd->select_ex_wait );
}
npreal_wait_command_completed(nd,
NPREAL_LOCAL_COMMAND_SET,
LOCAL_CMD_TTY_UNUSED,
NPREAL_CMD_TIMEOUT,