forked from rchovan/npreal2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
npreal2.c.orig
4692 lines (4205 loc) · 106 KB
/
npreal2.c.orig
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 Moxa Inc. ([email protected]).
*
* 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
#include <linux/version.h>
#define VERSION_CODE(ver,rel,seq) ((ver << 16) | (rel << 8) | seq)
#ifndef _DEBIAN_
#ifdef MODULE
#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>
#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>
#include <linux/proc_fs.h>
#else
#ifdef MODULE
#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>
#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>
#include <config/proc_fs.h>
#endif
#include "np_ver.h"
#if (LINUX_VERSION_CODE < VERSION_CODE(5,0,0))
#define ACCESS_OK(x,y,z) access_ok(x,y,z)
#else
#define ACCESS_OK(x,y,z) access_ok(y,z)
#endif
/* include/linux/semaphore.h modification */
#define init_MUTEX(sem) sema_init(sem, 1)
#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)
#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
#define PORTNO(x) ((x)->index)
#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 */
MODULE_AUTHOR("Moxa Tech.,www.moxa.com.tw");
MODULE_DESCRIPTION("MOXA Async/NPort Server Family Real TTY Driver");
module_param(ttymajor, int, 0);
module_param(calloutmajor, int, 0);
module_param(verbose, int, 0644);
MODULE_VERSION(NPREAL_VERSION);
#ifdef MODULE_LICENSE
MODULE_LICENSE("GPL");
# 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_CMD_TRY 10
#define NPREAL_NET_CMD_RETRIEVE 1
#define NPREAL_NET_CMD_RESPONSE 2
#define NPREAL_NET_NODE_OPENED 0x01 /* proc node is opened */
#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 /* tty port is opened */
// 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 */
struct pid* session;
struct pid* pgrp;
unsigned char *xmit_buf;
int xmit_head;
int xmit_tail;
int xmit_cnt;
struct work_struct tqueue;
struct work_struct process_flip_tqueue;
struct ktermios normal_termios;
struct ktermios callout_termios;
wait_queue_head_t open_wait;
wait_queue_head_t close_wait;
wait_queue_head_t delta_msr_wait;
struct async_icount icount; /* kernel counters for the 4 input interrupts */
struct nd_struct *net_node;
/* 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;
};
struct nd_struct
{
int32_t server_type;
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;
int cmd_rsp_flag;
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;
};
static struct tty_driver *npvar_sdriver;
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];
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
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.
*/
static unsigned char * npvar_tmp_buf;
static struct semaphore npvar_tmp_buf_sem;
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);
static void npreal_do_softint(struct work_struct *work);
static void npreal_flush_to_ldisc(struct work_struct *work);
static int npreal_open(struct tty_struct *,struct file *);
static void npreal_close(struct tty_struct *,struct file *);
static int npreal_write(struct tty_struct *,const unsigned char *,int);
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 *);
static int npreal_put_char(struct tty_struct *,unsigned char);
static int npreal_ioctl(struct tty_struct *,uint,ulong);
static void npreal_throttle(struct tty_struct *);
static void npreal_unthrottle(struct tty_struct *);
static void npreal_set_termios(struct tty_struct *,struct ktermios *);
static int npreal_port_init(struct npreal_struct *,struct ktermios *);
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);
static int npreal_tiocmget(struct tty_struct *);
static int npreal_tiocmset(struct tty_struct *, unsigned int, unsigned int);
static void npreal_process_notify(struct nd_struct *,char *,int);
static void npreal_do_session_recovery(struct npreal_struct *);
static void npreal_wait_until_sent(struct tty_struct *,int);
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);
static int npreal_break(struct tty_struct * ttyinfo, int break_state);
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);
static long _get_delta_giffies(long base);
/*
* File operation declarations
*/
static int npreal_net_open(struct inode *, struct file * );
/* /include/linux/fs.h modification */
static long npreal_net_ioctl(struct file *,unsigned int, unsigned long );
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 *);
/*
* "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
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,
.flush_buffer = npreal_ldisc_flush_buffer,
.wait_until_sent = npreal_wait_until_sent,
.break_ctl = npreal_break,
.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,
};
/*
* The MOXA NPort server Real TTY driver boot-time initialization code!
*/
#ifdef MODULE
static int __init npreal2_module_init(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);
}
static void __exit npreal2_module_exit(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 ((err |= tty_unregister_driver(DRV_VAR)))
{
DBGPRINT(MX_DEBUG_ERROR, "Couldn't unregister MOXA Async/NPort server family Real TTY driver\n");
}
put_tty_driver(DRV_VAR);
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];
init_MUTEX(&npvar_tmp_buf_sem);
//create "npreal2" dir
npvar_proc_root = proc_mkdir("npreal2", NULL);
//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;
de->proc_fops = &npreal_net_fops;
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 */
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);
net_node->cmd_rsp_flag = 0;
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;
INIT_WORK(&tty_node->tqueue, npreal_do_softint);
INIT_WORK(&tty_node->process_flip_tqueue, npreal_flush_to_ldisc);
tty_node->normal_termios = DRV_VAR_P(init_termios);
//init_MUTEX(&tty_node->tx_lock);
//tty_node->tx_lock = __SPIN_LOCK_UNLOCKED(tty_node->tx_lock);
spin_lock_init(&tty_node->tx_lock);
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);
tty_node->icount.rx = tty_node->icount.tx = 0;
tty_node->icount.cts = tty_node->icount.dsr = tty_node->icount.dcd = 0;
tty_node->icount.frame = tty_node->icount.overrun =
tty_node->icount.brk = tty_node->icount.parity = 0;
}
return 0;
}
int
npreal_init(void)
{
int ret1, ret2;
#if (LINUX_VERSION_CODE >= VERSION_CODE(3,7,0))
int i;
#endif
npvar_sdriver = alloc_tty_driver(NPREAL_PORTS+1);
if (!npvar_sdriver)
return -ENOMEM;
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 */
DRV_VAR_P(name) = "ttyr";
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;
DRV_VAR_P(flags) = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
tty_set_operations(DRV_VAR, &mpvar_ops);
#if (LINUX_VERSION_CODE < VERSION_CODE(3,7,0))
DRV_VAR_P(ttys) = npvar_tty;
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
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)) )
{
}
else
{
DBGPRINT(MX_DEBUG_ERROR, "Couldn't install MOXA Async/NPort server family driver !\n");
}
if (ret1 || ret2)
{
put_tty_driver(DRV_VAR);
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;
npreal_net_fops.unlocked_ioctl = npreal_net_ioctl;
npreal_net_fops.open = npreal_net_open;
npreal_net_fops.release = npreal_net_close;
npreal_net_fops.poll = npreal_net_select;
if (npreal_init_tty() != 0)
{
tty_unregister_driver(DRV_VAR);
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,
buf,
size);
}
}
static void
npreal_do_softint(struct work_struct *work)
{
struct npreal_struct * info =
container_of(work, struct npreal_struct, tqueue);
struct tty_struct * tty;
if (!info)
goto done;
tty = info->tty;
if (tty)
{
if ( test_and_clear_bit(NPREAL_EVENT_TXLOW, &info->event) )
{
if ( (tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
tty->ldisc->ops->write_wakeup)
(tty->ldisc->ops->write_wakeup)(tty);
wake_up_interruptible(&tty->write_wait);
}
if ( test_and_clear_bit(NPREAL_EVENT_HANGUP, &info->event) )
{
// Scott: 2005-09-05
// Do it when entering npreal_hangup().
// Scott info->flags |= ASYNC_CLOSING;
tty_hangup(tty);
}
}
done:
;
}
/*
* This routine is called whenever a serial port is opened.
*/
/* Scott: 2005/07/13
* Note that on failure, we don't decrement the module use count - the tty driver
* later will call npreal_close, which will decrement it for us as long as
* tty->driver_data is set non-NULL.
*/
static int
npreal_open(
struct tty_struct * tty,
struct file * filp)
{
struct npreal_struct *info;
int line;
unsigned long page;
struct nd_struct *nd;
int error_try;
#ifndef CONCURRENT_SSL
long st;
long tmp_t;
#endif
int ret;
DBGPRINT(MX_DEBUG_LOUD, "(Entry)\n");
MX_MOD_INC;
line = PORTNO(tty);
if ( (line < 0) || (line >= NPREAL_PORTS) )
{
DBGPRINT(MX_DEBUG_ERROR, "invalid line (%d)\n", line);
MX_MOD_DEC;
return(-ENODEV);
}
info = npvar_table + line;
nd = info->net_node;
if ( !nd || !(nd->flag&NPREAL_NET_NODE_OPENED))
{
DBGPRINT(MX_DEBUG_ERROR, "net device not ready\n");
MX_MOD_DEC;
return(-ENODEV);
}
if ( !npvar_tmp_buf )
{
page = GET_FPAGE(GFP_KERNEL);
if ( !page )
{
DBGPRINT(MX_DEBUG_ERROR, "allocate npvar_tmp_buf failed\n");
MX_MOD_DEC;
return(-ENOMEM);
}
if ( npvar_tmp_buf )
free_page(page);
else
npvar_tmp_buf = (unsigned char *)page;
}
/*
* Start up serial port
*/
// Scott: 2005/07/13
// Set tty->driver_data before entering npreal_startup(), so that the tty driver
// can decrease refcount if npreal_startup() failed, by calling npreal_close().
tty->driver_data = info;
info->count++;
// Scott: end
error_try=NPREAL_CMD_TRY;
while(1)
{
// For some circumstance, device may reset the connection during the
// port opening. These code is to reopen the port without telling
// application. Considering a real situation of connection lost, we
// use -ETIME to exit the retry loop.
ret = npreal_startup(info,filp,tty);
if( ret==0 )
break;
#ifndef CONCURRENT_SSL
else if( ret==(-ETIME) ){
DBGPRINT(MX_DEBUG_ERROR, "npreal_startup failed(%d)\n", ret);
return(-EIO);
}
//DBGPRINT(MX_DEBUG_ERROR, "npreal_startup return(%d, %d, %d)\n", info->port, error_try, ret);
st = jiffies;
error_try--;
while(1){
tmp_t = _get_delta_giffies(st);
if( tmp_t >= ((NPREAL_CMD_TRY-error_try)*HZ/2) )
break;
}
#endif
if( error_try>0 ){
DBGPRINT(MX_DEBUG_WARN, "npreal_startup retry\n");
continue;
}else{
DBGPRINT(MX_DEBUG_ERROR, "npreal_startup failed\n");
return(-EIO);
}
}
if (npreal_block_til_ready(tty, filp, info))
{
DBGPRINT(MX_DEBUG_ERROR, "npreal_block_til_ready failed\n");
return(-EIO);
}
if ( (info->count == 1) && (info->flags & ASYNC_SPLIT_TERMIOS) )
{
#if (LINUX_VERSION_CODE < VERSION_CODE(3,7,0))
if ( MX_TTY_DRV(subtype) == SERIAL_TYPE_NORMAL )
*tty->termios = info->normal_termios;
else
*tty->termios = info->callout_termios;
#else
if ( MX_TTY_DRV(subtype) == SERIAL_TYPE_NORMAL )
tty->termios = info->normal_termios;
else
tty->termios = info->callout_termios;
#endif
if (npreal_port_init(info, 0))
{
DBGPRINT(MX_DEBUG_ERROR, "npreal_port_init failed\n");
return(-EIO);
}
}
info->session = MX_SESSION();
info->pgrp = MX_CGRP();
#if (LINUX_VERSION_CODE < VERSION_CODE(3,9,0))
#if (LINUX_VERSION_CODE < VERSION_CODE(3,2,0))
/* It must be always on */
tty->low_latency = 1;
#else
tty->low_latency = 1; /* Read Issue */
#endif /* 3,2,0 */
#else
info->ttyPort.low_latency = 1;
#endif /* 3,9,0 */
return(0);
}
/*
* This routine is called when the serial port gets closed. First, we
* wait for the last remaining data to be sent.
*/
static void
npreal_close(
struct tty_struct * tty,
struct file * filp)
{
struct npreal_struct * info = (struct npreal_struct *)tty->driver_data;
long timeout,et,ret;
int cnt;
DBGPRINT(MX_DEBUG_LOUD, "(Entry)\n");
if ( !info ) {
return;
}
if ( tty_hung_up_p(filp) )
{
info->count--;
MX_MOD_DEC;
return;
}
// Scott: 2005/07/13
// Comment out the following two if's.
#if 0
if (!(info->flags & ASYNC_INITIALIZED))
{
info->count--;
MX_MOD_DEC;
return;
}
if (info->flags & ASYNC_CLOSING)
{
info->count--;
MX_MOD_DEC;
return;
}
#endif
#ifndef SP1
if ( (tty->count == 1) && (info->count != 1) )
{
#else
if ( (atomic_read(&tty->count) == 1) && (info->count != 1) )
{
#endif
/*
* Uh, oh. tty->count is 1, which means that the tty
* structure will be freed. Info->count should always
* be one in these conditions. If it's greater than
* one, we've got real problems, since it means the
* serial port won't be shutdown.
*/
DBGPRINT(MX_DEBUG_WARN, "[%d] npreal_close: bad serial port count; tty->count is 1, info->count is %d\n", current->pid, info->count);
info->count = 1;
}
if ( --info->count < 0 )
{
DBGPRINT(MX_DEBUG_WARN, "npreal_close: bad serial port count for port %d: %d\n", info->port, info->count);
info->count = 0;
}
if ( info->count )
{
MX_MOD_DEC;
return;
}
// Scott: 2005-09-05
// Prevent race condition on closing.
if (info->flags & ASYNC_CLOSING) {
return;
}
info->flags |= ASYNC_CLOSING;
tty->closing = 1;
/*
* Save the termios structure, since this port may have
* separate termios for callout and dialin.
*/
#if (LINUX_VERSION_CODE < VERSION_CODE(3,7,0))
if ( info->flags & ASYNC_NORMAL_ACTIVE )
info->normal_termios = *tty->termios;
if ( info->flags & ASYNC_CALLOUT_ACTIVE )