1 | /*
|
---|
2 | * TOPPERS/JSP Kernel
|
---|
3 | * Toyohashi Open Platform for Embedded Real-Time Systems/
|
---|
4 | * Just Standard Profile Kernel
|
---|
5 | *
|
---|
6 | * Copyright (C) 2000-2003 by Embedded and Real-Time Systems Laboratory
|
---|
7 | * Toyohashi Univ. of Technology, JAPAN
|
---|
8 | *
|
---|
9 | * ä¸è¨è使¨©è
|
---|
10 | ã¯ï¼ä»¥ä¸ã® (1)ã(4) ã®æ¡ä»¶ãï¼Free Software Foundation
|
---|
11 | * ã«ãã£ã¦å
|
---|
12 | ¬è¡¨ããã¦ãã GNU General Public License ã® Version 2 ã«è¨
|
---|
13 | * è¿°ããã¦ããæ¡ä»¶ãæºããå ´åã«éãï¼æ¬ã½ããã¦ã§ã¢ï¼æ¬ã½ããã¦ã§ã¢
|
---|
14 | * ãæ¹å¤ãããã®ãå«ãï¼ä»¥ä¸åãï¼ã使ç¨ã»è¤è£½ã»æ¹å¤ã»åé
|
---|
15 | å¸ï¼ä»¥ä¸ï¼
|
---|
16 | * å©ç¨ã¨å¼ã¶ï¼ãããã¨ãç¡åã§è¨±è«¾ããï¼
|
---|
17 | * (1) æ¬ã½ããã¦ã§ã¢ãã½ã¼ã¹ã³ã¼ãã®å½¢ã§å©ç¨ããå ´åã«ã¯ï¼ä¸è¨ã®èä½
|
---|
18 | * 権表示ï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãï¼ãã®ã¾ã¾ã®å½¢ã§ã½ã¼
|
---|
19 | * ã¹ã³ã¼ãä¸ã«å«ã¾ãã¦ãããã¨ï¼
|
---|
20 | * (2) æ¬ã½ããã¦ã§ã¢ãï¼ã©ã¤ãã©ãªå½¢å¼ãªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
21 | * ç¨ã§ããå½¢ã§åé
|
---|
22 | å¸ããå ´åã«ã¯ï¼åé
|
---|
23 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨
|
---|
24 | * è
|
---|
25 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è使¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨
|
---|
26 | * ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
27 | * (3) æ¬ã½ããã¦ã§ã¢ãï¼æ©å¨ã«çµã¿è¾¼ããªã©ï¼ä»ã®ã½ããã¦ã§ã¢éçºã«ä½¿
|
---|
28 | * ç¨ã§ããªãå½¢ã§åé
|
---|
29 | å¸ããå ´åã«ã¯ï¼æ¬¡ã®ããããã®æ¡ä»¶ãæºããã
|
---|
30 | * ã¨ï¼
|
---|
31 | * (a) åé
|
---|
32 | å¸ã«ä¼´ãããã¥ã¡ã³ãï¼å©ç¨è
|
---|
33 | ããã¥ã¢ã«ãªã©ï¼ã«ï¼ä¸è¨ã®è
|
---|
34 | * 使¨©è¡¨ç¤ºï¼ãã®å©ç¨æ¡ä»¶ããã³ä¸è¨ã®ç¡ä¿è¨¼è¦å®ãæ²è¼ãããã¨ï¼
|
---|
35 | * (b) åé
|
---|
36 | å¸ã®å½¢æ
|
---|
37 | ãï¼å¥ã«å®ããæ¹æ³ã«ãã£ã¦ï¼TOPPERSããã¸ã§ã¯ãã«
|
---|
38 | * å ±åãããã¨ï¼
|
---|
39 | * (4) æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´æ¥çã¾ãã¯éæ¥çã«çãããããªãæ
|
---|
40 | * 害ãããï¼ä¸è¨è使¨©è
|
---|
41 | ããã³TOPPERSããã¸ã§ã¯ããå
|
---|
42 | 責ãããã¨ï¼
|
---|
43 | *
|
---|
44 | * æ¬ã½ããã¦ã§ã¢ã¯ï¼ç¡ä¿è¨¼ã§æä¾ããã¦ãããã®ã§ããï¼ä¸è¨è使¨©è
|
---|
45 | ã
|
---|
46 | * ãã³TOPPERSããã¸ã§ã¯ãã¯ï¼æ¬ã½ããã¦ã§ã¢ã«é¢ãã¦ï¼ãã®é©ç¨å¯è½æ§ã
|
---|
47 | * å«ãã¦ï¼ãããªãä¿è¨¼ãè¡ããªãï¼ã¾ãï¼æ¬ã½ããã¦ã§ã¢ã®å©ç¨ã«ããç´
|
---|
48 | * æ¥çã¾ãã¯éæ¥çã«çãããããªãæå®³ã«é¢ãã¦ãï¼ãã®è²¬ä»»ãè² ããªãï¼
|
---|
49 | *
|
---|
50 | * @(#) $Id: linux_serial.c,v 1.11 2003/12/11 00:36:49 honda Exp $
|
---|
51 | */
|
---|
52 |
|
---|
53 | #define _LINX_SERIAL_
|
---|
54 |
|
---|
55 | #include <t_services.h>
|
---|
56 | #include <s_services.h>
|
---|
57 | #include <signal.h>
|
---|
58 | #include <termios.h>
|
---|
59 | #include <fcntl.h>
|
---|
60 | #include <errno.h>
|
---|
61 | #undef __USE_MISC
|
---|
62 | #include <unistd.h>
|
---|
63 | #include <linux_sigio.h>
|
---|
64 | #include "kernel_id.h"
|
---|
65 |
|
---|
66 |
|
---|
67 | /*
|
---|
68 | * ã·ãªã¢ã«ãã¼ãã®ä½ã¬ãã«å®ç¾©
|
---|
69 | */
|
---|
70 |
|
---|
71 | typedef struct hardware_serial_port_descripter {
|
---|
72 | char *path; /* UNIX ä¸ã§ã®ãã¡ã¤ã«å */
|
---|
73 | int fd; /* ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ */
|
---|
74 | struct termios current_term; /* 端æ«å¶å¾¡æ
|
---|
75 | å ± */
|
---|
76 | struct termios saved_term;
|
---|
77 | } HWPORT;
|
---|
78 |
|
---|
79 | #define NUM_PORT 1
|
---|
80 |
|
---|
81 | #define RAWPORT1 { 0 }
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * UNIX ã¬ãã«ã®ãã¼ãåæå/ã·ã£ãããã¦ã³å¦ç
|
---|
85 | *
|
---|
86 | * ç¾å¨ã®å®è£
|
---|
87 | ã§ã¯ï¼ç«¯æ«ã使ãã±ã¼ã¹ããèãã¦ããªãï¼æ¬æ¥ã¯ï¼open ã
|
---|
88 | * ãã®ã端æ«ãã©ããã§å¦çãå¤ããã¹ãï¼
|
---|
89 | */
|
---|
90 |
|
---|
91 | Inline void
|
---|
92 | hw_port_initialize(HWPORT *p)
|
---|
93 | {
|
---|
94 | if (p->path) {
|
---|
95 | p->fd = open(p->path, O_RDWR|O_NDELAY);
|
---|
96 | }
|
---|
97 | else {
|
---|
98 | p->fd = 0; /* æ¨æºå
|
---|
99 | ¥åºåã使ã */
|
---|
100 | }
|
---|
101 | fcntl(p->fd, F_SETOWN, getpid());
|
---|
102 | fcntl(p->fd, F_SETFL, FASYNC|FNDELAY);
|
---|
103 |
|
---|
104 |
|
---|
105 | tcgetattr(p->fd, &(p->saved_term));
|
---|
106 |
|
---|
107 | p->current_term = p->saved_term;
|
---|
108 | p->current_term.c_lflag &= ~(ECHO);
|
---|
109 | p->current_term.c_lflag &= ~(ICANON);
|
---|
110 | p->current_term.c_cc[VMIN] = 1;
|
---|
111 | p->current_term.c_cc[VTIME] = 0;
|
---|
112 |
|
---|
113 | tcsetattr(p->fd, TCSAFLUSH, &(p->current_term));
|
---|
114 | }
|
---|
115 |
|
---|
116 | Inline void
|
---|
117 | hw_port_terminate(HWPORT *p)
|
---|
118 | {
|
---|
119 | tcsetattr(p->fd, TCSAFLUSH, &(p->saved_term));
|
---|
120 | fcntl(p->fd, F_SETFL, 0);
|
---|
121 | if (p->path) {
|
---|
122 | close(p->fd);
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | /*
|
---|
128 | * ã·ãªã¢ã«ã¤ã³ã¿ãã§ã¼ã¹ãã©ã¤ãç¨ã® SIGIO éç¥ã¤ãã³ããããã¯
|
---|
129 | */
|
---|
130 |
|
---|
131 | static SIGIOEB serial_sigioeb;
|
---|
132 |
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * ã·ãªã¢ã«ãã¼ã管çãããã¯ã®å®ç¾©
|
---|
136 | */
|
---|
137 |
|
---|
138 | typedef struct ioctl_descripter {
|
---|
139 | int echo;
|
---|
140 | int input;
|
---|
141 | int newline;
|
---|
142 | int flowc;
|
---|
143 | } IOCTL;
|
---|
144 |
|
---|
145 | #define SERIAL_BUFSZ 256 /* ã·ãªã¢ã«ã¤ã³ã¿ãã§ã¼ã¹ç¨ãããã¡ã®ãµã¤ãº */
|
---|
146 |
|
---|
147 | #define inc(x) (((x)+1 < SERIAL_BUFSZ) ? (x)+1 : 0)
|
---|
148 | #define INC(x) ((x) = inc(x))
|
---|
149 |
|
---|
150 |
|
---|
151 | typedef struct serial_port_control_block {
|
---|
152 | BOOL init_flag; /* åæåæ¸ãï¼ */
|
---|
153 | HWPORT hwport; /* ãã¼ãã¦ã§ã¢ä¾åæ
|
---|
154 | å ± */
|
---|
155 | ID in_semid; /* åä¿¡ãããã¡ç®¡çç¨ã»ããã©ã® ID */
|
---|
156 | ID out_semid; /* éä¿¡ãããã¡ç®¡çç¨ã»ããã©ã® ID */
|
---|
157 |
|
---|
158 | int in_read_ptr; /* åä¿¡ãããã¡èªã¿åºããã¤ã³ã¿ */
|
---|
159 | int in_write_ptr; /* åä¿¡ãããã¡æ¸ãè¾¼ã¿ãã¤ã³ã¿ */
|
---|
160 | int out_read_ptr; /* éä¿¡ãããã¡èªã¿åºããã¤ã³ã¿ */
|
---|
161 | int out_write_ptr; /* éä¿¡ãããã¡æ¸ãè¾¼ã¿ãã¤ã³ã¿ */
|
---|
162 | UINT ioctl; /* ioctl ã«ããè¨å®å
|
---|
163 | 容 */
|
---|
164 | BOOL send_enabled; /* éä¿¡ãã¤ãã¼ãã«ãã¦ãããï¼ */
|
---|
165 | BOOL ixon_stopped; /* STOP ãåãåã£ãç¶æ
|
---|
166 | ãï¼ */
|
---|
167 | BOOL ixoff_stopped; /* ç¸æã« STOP ãéã£ãç¶æ
|
---|
168 | ãï¼ */
|
---|
169 | char ixoff_send; /* ç¸æã« START/STOP ãéããï¼ */
|
---|
170 |
|
---|
171 | char in_buffer[SERIAL_BUFSZ]; /* åä¿¡ãããã¡ã¨ãªã¢ */
|
---|
172 | char out_buffer[SERIAL_BUFSZ]; /* åä¿¡ãããã¡ã¨ãªã¢ */
|
---|
173 | } SPCB;
|
---|
174 |
|
---|
175 |
|
---|
176 | #define IN_BUFFER_EMPTY(spcb) \
|
---|
177 | ((spcb)->in_read_ptr == (spcb)->in_write_ptr)
|
---|
178 | #define IN_BUFFER_FULL(spcb) \
|
---|
179 | ((spcb)->in_read_ptr == inc((spcb)->in_write_ptr))
|
---|
180 | #define OUT_BUFFER_FULL(spcb) \
|
---|
181 | ((spcb)->out_read_ptr == inc((spcb)->out_write_ptr))
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * ã¢ã¸ã¥ã¼ã«å
|
---|
185 | ã§ä½¿ã颿°
|
---|
186 | */
|
---|
187 | static void sigint_handler();
|
---|
188 | static BOOL serial_getc(SPCB *spcb, char *c);
|
---|
189 | static BOOL serial_putc(SPCB *spcb, char c);
|
---|
190 |
|
---|
191 |
|
---|
192 | /*
|
---|
193 | * ã·ãªã¢ã«ãã¼ã管çãããã¯ã®å®ç¾©ã¨åæå
|
---|
194 | */
|
---|
195 |
|
---|
196 | static SPCB spcb_table[NUM_PORT] = {
|
---|
197 | {0, RAWPORT1, SEM_SERIAL1_IN, SEM_SERIAL1_OUT }
|
---|
198 | };
|
---|
199 |
|
---|
200 | #define get_spcb(portid) (&(spcb_table[(portid)-1]))
|
---|
201 | #define get_spcb_def(portid) get_spcb((portid) ? (portid) : CONSOLE_PORTID)
|
---|
202 |
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * ãã¼ãã®åæå
|
---|
206 | */
|
---|
207 | int
|
---|
208 | serial_opn_por(ID portid)
|
---|
209 | {
|
---|
210 |
|
---|
211 | SPCB *spcb;
|
---|
212 | ER ercd = E_OK;;
|
---|
213 |
|
---|
214 | if (!(1 <= portid && portid <= NUM_PORT)) {
|
---|
215 | return(E_PAR);
|
---|
216 | }
|
---|
217 | spcb = get_spcb(portid);
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * è¤æ°ã®ã¿ã¹ã¯ãåæã« serial_open ãå¼ã¶ç¶æ³ã«ã¯å¯¾å¿ãã¦ã
|
---|
221 | * ãªãï¼
|
---|
222 | */
|
---|
223 | if (spcb->init_flag) { /* åæåæ¸ãã®ãã§ã㯠*/
|
---|
224 | return(E_OK);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * 夿°ã®åæå
|
---|
229 | */
|
---|
230 | spcb->in_read_ptr = spcb->in_write_ptr = 0;
|
---|
231 | spcb->out_read_ptr = spcb->out_write_ptr = 0;
|
---|
232 | spcb->ixon_stopped = spcb->ixoff_stopped = FALSE;
|
---|
233 | spcb->ixoff_send = 0;
|
---|
234 |
|
---|
235 | /*
|
---|
236 | * ãã¼ãã¦ã§ã¢ä¾åã®åæå
|
---|
237 | */
|
---|
238 | hw_port_initialize(&(spcb->hwport));
|
---|
239 |
|
---|
240 | /*
|
---|
241 | * ããã»ã¹ãçµäºãããã·ã°ãã«ãæã¾ãã
|
---|
242 | * sigaction()ã§æ¸ãç´ããæ¹ãããã®ã?
|
---|
243 | */
|
---|
244 | signal(SIGHUP, sigint_handler);
|
---|
245 | signal(SIGINT, sigint_handler);
|
---|
246 | signal(SIGTERM, sigint_handler);
|
---|
247 |
|
---|
248 | spcb->init_flag = TRUE;
|
---|
249 | spcb->send_enabled = FALSE;
|
---|
250 | return(ercd);
|
---|
251 | }
|
---|
252 |
|
---|
253 | /*
|
---|
254 | * ãã¼ãã®ã·ã£ãããã¦ã³
|
---|
255 | */
|
---|
256 |
|
---|
257 | ER
|
---|
258 | serial_cls_por(ID portid)
|
---|
259 | {
|
---|
260 | SPCB *spcb;
|
---|
261 |
|
---|
262 | if (!(1 <= portid && portid <= NUM_PORT)) {
|
---|
263 | return(E_PAR); /* ãã¼ãçªå·ã®ãã§ã㯠*/
|
---|
264 | }
|
---|
265 |
|
---|
266 | spcb = get_spcb(portid);
|
---|
267 | if (!(spcb->init_flag)) { /* åæåæ¸ãã®ãã§ã㯠*/
|
---|
268 | return(E_OBJ);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /*
|
---|
272 | * ãã¼ãã¦ã§ã¢ä¾åã®ã·ã£ãããã¦ã³å¦ç
|
---|
273 | */
|
---|
274 | syscall(loc_cpu());
|
---|
275 | hw_port_terminate(&(spcb->hwport));
|
---|
276 | syscall(unl_cpu());
|
---|
277 |
|
---|
278 | spcb->init_flag = FALSE;
|
---|
279 | return(E_OK);
|
---|
280 | }
|
---|
281 |
|
---|
282 | /*
|
---|
283 | * ããã»ã¹ãçµäºãããã·ã°ãã«ã«å¯¾ãããã³ãã©
|
---|
284 | */
|
---|
285 | void
|
---|
286 | sigint_handler()
|
---|
287 | {
|
---|
288 | SPCB *spcb;
|
---|
289 | int i;
|
---|
290 |
|
---|
291 | for (i = 1; i <= NUM_PORT; i++) {
|
---|
292 | spcb = get_spcb(i);
|
---|
293 | if (spcb->init_flag) {
|
---|
294 | hw_port_terminate(&(spcb->hwport));
|
---|
295 | }
|
---|
296 | }
|
---|
297 | exit(0);
|
---|
298 | }
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * ããã¼ã³ã³ããã¼ã«é¢ä¿ã®å®ç¾©
|
---|
302 | */
|
---|
303 | #define STOP '\023' /* Control-S */
|
---|
304 | #define START '\021' /* Control-Q */
|
---|
305 |
|
---|
306 | #define IXOFF_STOP 64 /* buffer area size to send STOP */
|
---|
307 | #define IXOFF_START 128 /* buffer area size to send START */
|
---|
308 |
|
---|
309 | #define in_buf_area(p) \
|
---|
310 | ((spcb->in_read_ptr >= spcb->in_write_ptr) ? \
|
---|
311 | (spcb->in_read_ptr - spcb->in_write_ptr) : \
|
---|
312 | (spcb->in_read_ptr + SERIAL_BUFSZ - spcb->in_write_ptr))
|
---|
313 | /*
|
---|
314 | * ã¦ã¼ãã£ãªãã£ã«ã¼ãã³
|
---|
315 | */
|
---|
316 |
|
---|
317 | Inline BOOL
|
---|
318 | read_char(SPCB *spcb, char *c)
|
---|
319 | {
|
---|
320 | int n;
|
---|
321 |
|
---|
322 | if ((n = read(spcb->hwport.fd, c, 1)) == 1) {
|
---|
323 | return(1);
|
---|
324 | }
|
---|
325 | assert(n < 0 && errno == EWOULDBLOCK);
|
---|
326 | return(0);
|
---|
327 | }
|
---|
328 |
|
---|
329 | Inline BOOL
|
---|
330 | write_char(SPCB *spcb, char c)
|
---|
331 | {
|
---|
332 | int n;
|
---|
333 |
|
---|
334 | if ((n = write(spcb->hwport.fd, &c, 1)) == 1) {
|
---|
335 | return(1);
|
---|
336 | }
|
---|
337 | assert(n < 0 && errno == EWOULDBLOCK);
|
---|
338 | return(0);
|
---|
339 | }
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * ã·ãªã¢ã«ãã¼ãããã®åä¿¡
|
---|
343 | */
|
---|
344 |
|
---|
345 | static BOOL
|
---|
346 | serial_getc(SPCB *spcb, char *c)
|
---|
347 | {
|
---|
348 | BOOL buffer_empty;
|
---|
349 |
|
---|
350 | syscall(loc_cpu());
|
---|
351 | *c = spcb->in_buffer[spcb->in_read_ptr];
|
---|
352 | if (inc(spcb->in_write_ptr) == spcb->in_read_ptr) {
|
---|
353 | /*
|
---|
354 | * ãããã¡ãã«ç¶æ
|
---|
355 | ãè§£é¤ãããããå²ãè¾¼ã¿ãå
|
---|
356 | ¥ã£ãã®ã¨
|
---|
357 | * åãæ¯ãèãããããã
|
---|
358 | */
|
---|
359 | kill(getpid(), SIGIO);
|
---|
360 | }
|
---|
361 |
|
---|
362 | INC(spcb->in_read_ptr);
|
---|
363 |
|
---|
364 | if (spcb->ixoff_stopped && (in_buf_area(spcb) > IXOFF_START)) {
|
---|
365 | if (!write_char(spcb, START)) {
|
---|
366 | spcb->ixoff_send = START;
|
---|
367 | }
|
---|
368 | spcb->ixoff_stopped = FALSE;
|
---|
369 | }
|
---|
370 | buffer_empty = IN_BUFFER_EMPTY(spcb);
|
---|
371 | syscall(unl_cpu());
|
---|
372 | return(buffer_empty);
|
---|
373 | }
|
---|
374 |
|
---|
375 | ER_UINT
|
---|
376 | serial_rea_dat(ID portid, char *buf, UINT len)
|
---|
377 | {
|
---|
378 | SPCB *spcb;
|
---|
379 | BOOL buffer_empty;
|
---|
380 | char c;
|
---|
381 | int i;
|
---|
382 |
|
---|
383 |
|
---|
384 | if (sns_dpn()) { /* ã³ã³ããã¹ãã®ãã§ã㯠*/
|
---|
385 | return(E_CTX);
|
---|
386 | }
|
---|
387 | if (!(0 <= portid && portid <= NUM_PORT)) {
|
---|
388 | return(E_PAR); /* ãã¼ãçªå·ã®ãã§ã㯠*/
|
---|
389 | }
|
---|
390 |
|
---|
391 | spcb = get_spcb_def(portid);
|
---|
392 | if (!(spcb->init_flag)) { /* åæåæ¸ãã®ãã§ã㯠*/
|
---|
393 | return(E_OBJ);
|
---|
394 | }
|
---|
395 | if (len == 0) {
|
---|
396 | return(len);
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | syscall(wai_sem(spcb->in_semid));
|
---|
401 | buffer_empty = FALSE;
|
---|
402 | for (i = 0; i < len; i++) {
|
---|
403 | buffer_empty = serial_getc(spcb, &c);
|
---|
404 | if ((spcb->ioctl & IOCTL_ECHO) != 0) {
|
---|
405 | syscall(wai_sem(spcb->out_semid));
|
---|
406 | if (!serial_putc(spcb, c)) {
|
---|
407 | syscall(sig_sem(spcb->out_semid));
|
---|
408 | }
|
---|
409 | }
|
---|
410 | *buf++ = c;
|
---|
411 | if (buffer_empty && i < len - 1) {
|
---|
412 | syscall(wai_sem(spcb->in_semid));
|
---|
413 | }
|
---|
414 | }
|
---|
415 | if (!buffer_empty) {
|
---|
416 | syscall(sig_sem(spcb->in_semid));
|
---|
417 | }
|
---|
418 | return(len);
|
---|
419 | }
|
---|
420 |
|
---|
421 |
|
---|
422 | /*
|
---|
423 | * ã·ãªã¢ã«ãã¼ãã¸ã®éä¿¡
|
---|
424 | */
|
---|
425 |
|
---|
426 | static BOOL
|
---|
427 | serial_putc(SPCB *spcb, char c)
|
---|
428 | {
|
---|
429 | BOOL buffer_full;
|
---|
430 |
|
---|
431 | if (c == '\n' && (spcb->ioctl & IOCTL_CRLF) != 0) {
|
---|
432 | if (serial_putc(spcb, '\r')) {
|
---|
433 | syscall(wai_sem(spcb->out_semid));
|
---|
434 | }
|
---|
435 | }
|
---|
436 |
|
---|
437 | syscall(loc_cpu());
|
---|
438 | if (!(spcb->ixon_stopped) && write_char(spcb, c)) {
|
---|
439 | buffer_full = FALSE;
|
---|
440 | }
|
---|
441 | else {
|
---|
442 | spcb->out_buffer[spcb->out_write_ptr] = c;
|
---|
443 | INC(spcb->out_write_ptr);
|
---|
444 | buffer_full = OUT_BUFFER_FULL(spcb);
|
---|
445 | }
|
---|
446 | syscall(unl_cpu());
|
---|
447 | return(buffer_full);
|
---|
448 | }
|
---|
449 |
|
---|
450 | ER_UINT
|
---|
451 | serial_wri_dat(ID portid, char *buf, UINT len)
|
---|
452 | {
|
---|
453 | SPCB *spcb;
|
---|
454 | BOOL buffer_full;
|
---|
455 | int i;
|
---|
456 |
|
---|
457 | if (sns_dpn()) { /* ã³ã³ããã¹ãã®ãã§ã㯠*/
|
---|
458 | return(E_CTX);
|
---|
459 | }
|
---|
460 | if (!(0 <= portid && portid <= NUM_PORT)) {
|
---|
461 | return(E_PAR); /* ãã¼ãçªå·ã®ãã§ã㯠*/
|
---|
462 | }
|
---|
463 |
|
---|
464 | spcb = get_spcb_def(portid);
|
---|
465 | if (!(spcb->init_flag)) { /* åæåæ¸ãã®ãã§ã㯠*/
|
---|
466 | return(E_OBJ);
|
---|
467 | }
|
---|
468 |
|
---|
469 | syscall(wai_sem(spcb->out_semid));
|
---|
470 | buffer_full = FALSE;
|
---|
471 | for (i = 0; i < len; i++) {
|
---|
472 | buffer_full = serial_putc(spcb, *buf++);
|
---|
473 | if (buffer_full && i < len - 1) {
|
---|
474 | syscall(wai_sem(spcb->out_semid));
|
---|
475 | }
|
---|
476 | }
|
---|
477 | if (!buffer_full) {
|
---|
478 | syscall(sig_sem(spcb->out_semid));
|
---|
479 | }
|
---|
480 | return(len);
|
---|
481 | }
|
---|
482 |
|
---|
483 | /*
|
---|
484 | * ã·ãªã¢ã«ãã¼ãã®å¶å¾¡
|
---|
485 | */
|
---|
486 |
|
---|
487 | int
|
---|
488 | serial_ctl_por(ID portid, UINT ioctl)
|
---|
489 | {
|
---|
490 | SPCB *spcb;
|
---|
491 |
|
---|
492 | if (sns_ctx()) { /* ã³ã³ããã¹ãã®ãã§ã㯠*/
|
---|
493 | return(E_CTX);
|
---|
494 | }
|
---|
495 | if (!(0 <= portid && portid <= NUM_PORT)) {
|
---|
496 | return(E_PAR); /* ãã¼ãçªå·ã®ãã§ã㯠*/
|
---|
497 | }
|
---|
498 |
|
---|
499 | spcb = get_spcb_def(portid);
|
---|
500 | if (!(spcb->init_flag)) { /* åæåæ¸ãã®ãã§ã㯠*/
|
---|
501 | return(E_OBJ);
|
---|
502 | }
|
---|
503 |
|
---|
504 | spcb->ioctl = ioctl;
|
---|
505 | return(E_OK);
|
---|
506 | }
|
---|
507 |
|
---|
508 |
|
---|
509 | /*
|
---|
510 | * ã·ãªã¢ã«ãã¼ãå²è¾¼ã¿ãã³ãã©
|
---|
511 | */
|
---|
512 |
|
---|
513 | static BOOL
|
---|
514 | serial_int_handler(ID portid)
|
---|
515 | {
|
---|
516 | SPCB *spcb;
|
---|
517 | BOOL flag;
|
---|
518 | char c;
|
---|
519 |
|
---|
520 | spcb = get_spcb(portid);
|
---|
521 | flag = 0;
|
---|
522 |
|
---|
523 | /*
|
---|
524 | * 1æååä¿¡å¦ç
|
---|
525 | *
|
---|
526 | * ã¾ãï¼ãããã¡ãã«ã§ãªãå ´åã«ï¼1æåèªãã§ã¿ãï¼èªããã°ï¼
|
---|
527 | * ããã«å¿ããå¦çãè¡ãï¼
|
---|
528 | */
|
---|
529 | if (inc(spcb->in_write_ptr) != spcb->in_read_ptr
|
---|
530 | && read_char(spcb, &c)) {
|
---|
531 | if ((spcb->ioctl & IOCTL_FCSND) != 0 && c == STOP) {
|
---|
532 | spcb->ixon_stopped = TRUE;
|
---|
533 | }
|
---|
534 | else if (((spcb->ioctl & IOCTL_FCSND) != 0 || spcb->ixon_stopped)
|
---|
535 | && (c == START || (spcb->ioctl & IOCTL_FCANY) != 0)) {
|
---|
536 | spcb->ixon_stopped = FALSE;
|
---|
537 | }
|
---|
538 | else {
|
---|
539 | spcb->in_buffer[spcb->in_write_ptr] = c;
|
---|
540 | if(spcb->in_read_ptr == spcb->in_write_ptr){
|
---|
541 | syscall(sig_sem(spcb->in_semid));
|
---|
542 | }
|
---|
543 |
|
---|
544 | INC(spcb->in_write_ptr);
|
---|
545 |
|
---|
546 | if ((spcb->ioctl & IOCTL_FCRCV) != 0 && !(spcb->ixoff_stopped)
|
---|
547 | && (in_buf_area(p) < IXOFF_STOP)) {
|
---|
548 | spcb->ixoff_stopped = TRUE;
|
---|
549 | spcb->ixoff_send = STOP;
|
---|
550 | }
|
---|
551 | }
|
---|
552 | flag = 1;
|
---|
553 | }
|
---|
554 |
|
---|
555 | /*
|
---|
556 | * 1æåéä¿¡å¦ç
|
---|
557 | */
|
---|
558 | if (spcb->ixoff_send) {
|
---|
559 | if (write_char(spcb, spcb->ixoff_send)) {
|
---|
560 | spcb->ixoff_send = 0;
|
---|
561 | flag = 1;
|
---|
562 | }
|
---|
563 | }
|
---|
564 | else if (!(spcb->ixon_stopped)
|
---|
565 | && spcb->out_read_ptr != spcb->out_write_ptr) {
|
---|
566 | if (write_char(spcb, spcb->out_buffer[spcb->out_read_ptr])) {
|
---|
567 | if(OUT_BUFFER_FULL(spcb)){
|
---|
568 | syscall(sig_sem(spcb->out_semid));
|
---|
569 | }
|
---|
570 | INC(spcb->out_read_ptr);
|
---|
571 | flag = 1;
|
---|
572 | }
|
---|
573 | }
|
---|
574 | return(flag);
|
---|
575 | }
|
---|
576 |
|
---|
577 |
|
---|
578 |
|
---|
579 | /*
|
---|
580 | * SIGIO ã³ã¼ã«ããã¯ã«ã¼ãã³
|
---|
581 | */
|
---|
582 |
|
---|
583 | static BOOL
|
---|
584 | serial_sigio_callback(VP arg)
|
---|
585 | {
|
---|
586 | BOOL flag;
|
---|
587 |
|
---|
588 | do {
|
---|
589 | // syscall(loc_cpu());
|
---|
590 | flag = serial_int_handler(1);
|
---|
591 | // syscall(unl_cpu());
|
---|
592 | } while (flag);
|
---|
593 | return(0);
|
---|
594 | }
|
---|
595 |
|
---|
596 |
|
---|
597 |
|
---|
598 | /*
|
---|
599 | * ã·ãªã¢ã«ã¤ã³ã¿ãã§ã¼ã¹ãã©ã¤ãã®èµ·å
|
---|
600 | */
|
---|
601 |
|
---|
602 | void
|
---|
603 | serial_initialize(VP_INT exinf)
|
---|
604 | {
|
---|
605 | serial_sigioeb.callback = serial_sigio_callback;
|
---|
606 | serial_sigioeb.arg = (VP) 0;
|
---|
607 | syscall(enqueue_sigioeb_initialize(&serial_sigioeb));
|
---|
608 | }
|
---|