summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/i386/shared/comm/tty_drv.c
blob: 502ab6c4f96c957030b948ab07779e16ad4c9acf (plain) (blame)
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
/***************************************************************************
 *
 * $Header$
 *
 * MODULE DESCRIPTION:
 * This module implements the RTEMS drivers for the PC serial ports
 * as /dev/ttyS1 for COM1 and /dev/ttyS2 as COM2. If one of the ports
 * is used as the console, this driver would fail to initialize.
 *
 * This code was based on the console driver. It is based on the 
 * current termios framework. This is just a shell around the
 * termios support.
 *
 * by: Rosimildo da Silva:
 *     rdasilva@connecttel.com
 *     http://www.connecttel.com
 *
 * MODIFICATION/HISTORY:
 *
 * $Log$
 * Revision 1.3  2000/12/05 16:37:38  joel
 * 2000-12-01	Joel Sherrill <joel@OARcorp.com>
 *
 * 	* pc386/console/console.c, pc386/console/serial_mouse.c,
 * 	pc386/console/vgainit.c, shared/comm/tty_drv.c: Remove warnings.
 *
 * Revision 1.2  2000/10/18 16:10:50  joel
 * 2000-10-18	 Charles-Antoine Gauthier <charles.gauthier@nrc.ca>
 *
 * 	* comm/i386-stub-glue.c, comm/tty_drv.c, comm/uart.c, comm/uart.h:
 * 	Add the ability to set parity, number of data bits and
 * 	number of stop bits to the existing i386 serial drivers.
 *
 * Revision 1.1  2000/08/30 08:18:56  joel
 * 2000-08-26  Rosimildo da Silva  <rdasilva@connecttel.com>
 *
 * 	* shared/comm: Added "/dev/ttyS1" & "/dev/ttyS2" support for
 * 	the i386 BSPs.
 * 	* shared/comm/gdb_glue.c: New file.
 * 	* shared/comm/i386_io.c: New file.
 * 	* shared/comm/tty_drv.c: New file.
 * 	* shared/comm/tty_drv.h: New file.
 * 	* shared/comm/Makefile.am: Account for new files.
 * 	* shared/comm/uart.c: Adds support for sending characters to
 * 	another "line discipline."
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include <bsp.h>
#include <irq.h>
#include <rtems/libio.h>
#include <rtems/termiostypes.h>
#include <termios.h>
#include <uart.h>
#include <libcpu/cpuModel.h>

int BSP_poll_read(int);

/* Internal routines */
static int tty1_conSetAttr( int minor, const struct termios *t);
static int tty2_conSetAttr( int minor, const struct termios *t);
static void isr_on(const rtems_irq_connect_data *);
static void isr_off(const rtems_irq_connect_data *);
static int  isr_is_on(const rtems_irq_connect_data *);


extern BSP_polling_getchar_function_type BSP_poll_char;
extern int BSPConsolePort;
extern void rtems_set_waiting_id_comx( int port,  rtems_id id, rtems_event_set event );


/*
 * Interrupt structure for tty1
 */
static rtems_irq_connect_data tty1_isr_data = 
{ 
  BSP_UART_COM1_IRQ,
  BSP_uart_termios_isr_com1,
  isr_on,
  isr_off,
  isr_is_on};

/*
 * Interrupt structure for tty2
 */
static rtems_irq_connect_data tty2_isr_data = 
{ 
  BSP_UART_COM2_IRQ,
  BSP_uart_termios_isr_com2,
  isr_on,
  isr_off,
  isr_is_on};

static void
isr_on(const rtems_irq_connect_data *unused)
{
  return;
}
						   
static void
isr_off(const rtems_irq_connect_data *unused)
{
  return;
}

static int
isr_is_on(const rtems_irq_connect_data *irq)
{
  return BSP_irq_enabled_at_i8259s(irq->name);
}

void tty_reserve_resources(rtems_configuration_table *conf)
{
  rtems_termios_reserve_resources(conf, 1);
  return;
}

/*
 *  TTYS1 - device driver INITIALIZE entry point.
 */
rtems_device_driver
tty1_initialize(rtems_device_major_number major,
                   rtems_device_minor_number minor,
                   void                      *arg)
{
  rtems_status_code status;

  /* Check if this port is not been used as console */
  if( BSPConsolePort == BSP_UART_COM1 )
  {
    status = -1;
    printk("TTYS1: port selected as console.\n");
    rtems_fatal_error_occurred( status );
  }

  /*
   * Set up TERMIOS
   */
  rtems_termios_initialize();
  
  /*
   * Do device-specific initialization
   */
  /* 9600-8-N-1, without hardware flow control */
  BSP_uart_init( BSP_UART_COM1, 9600, CHR_8_BITS, 0, 0, 0 );
  status = BSP_install_rtems_irq_handler( &tty1_isr_data );
  if( !status )
  {
    printk("Error installing ttyS1 interrupt handler!\n");
    rtems_fatal_error_occurred(status);
  }
  /*
   * Register the device
   */
  status = rtems_io_register_name ("/dev/ttyS1", major, 0);
  if (status != RTEMS_SUCCESSFUL)
  {
      printk("Error registering ttyS1 device!\n");
      rtems_fatal_error_occurred (status);
  }
  printk("Device: /dev/ttyS1 initialized.\n");
  return RTEMS_SUCCESSFUL;
} /* tty_initialize */


static int tty1_last_close(int major, int minor, void *arg)
{
  BSP_remove_rtems_irq_handler( &tty1_isr_data );
  return 0;
}

/*
 * TTY1 - device driver OPEN entry point
 */
rtems_device_driver
tty1_open(rtems_device_major_number major,
                rtems_device_minor_number minor,
                void                      *arg)
{
  rtems_status_code  status;
#ifndef USE_TASK_DRIVEN
  static rtems_termios_callbacks cb = 
  {
    NULL,                        /* firstOpen */
    tty1_last_close,             /* lastClose */
    NULL,                        /* poll read */
    BSP_uart_termios_write_com1, /* write */
    tty1_conSetAttr,             /* setAttributes */
    NULL,                        /* stopRemoteTx */
    NULL,                        /* startRemoteTx */
    TERMIOS_IRQ_DRIVEN           /* outputUsesInterrupts */
  };
#else
  static rtems_termios_callbacks cb = 
  {
    NULL,                        /* firstOpen */
    tty1_last_close,             /* lastClose */
    BSP_uart_termios_read_com1,  /* poll read */
    BSP_uart_termios_write_com1, /* write */
    tty1_conSetAttr,             /* setAttributes */
    NULL,                        /* stopRemoteTx */
    NULL,                        /* startRemoteTx */
    TERMIOS_TASK_DRIVEN          /* outputUsesInterrupts */
  };
#endif

  status = rtems_termios_open( major, minor, arg, &cb );
  if(status != RTEMS_SUCCESSFUL)
  {
     printk("Error openning tty1 device\n");
     return status;
  }

  /*
   * Pass data area info down to driver
   */
  BSP_uart_termios_set( BSP_UART_COM1, 
                       ((rtems_libio_open_close_args_t *)arg)->iop->data1 );
  /* Enable interrupts  on channel */
  BSP_uart_intr_ctrl( BSP_UART_COM1, BSP_UART_INTR_CTRL_TERMIOS);
  return RTEMS_SUCCESSFUL;
}

/*
 * TTY - device driver CLOSE entry point
 */
rtems_device_driver
tty_close(rtems_device_major_number major,
              rtems_device_minor_number minor,
              void                      *arg)
{

  return (rtems_termios_close (arg));
  
} /* tty_close */

 
/*
 * TTY device driver READ entry point.
 * Read characters from the tty device.
 */
rtems_device_driver
tty_read(rtems_device_major_number major,
             rtems_device_minor_number minor,
             void                      *arg)
{
  return rtems_termios_read (arg);
} /* tty_read */
 

/*
 * TTY device driver WRITE entry point.
 * Write characters to the tty device.
 */
rtems_device_driver
tty_write(rtems_device_major_number major,
              rtems_device_minor_number minor,
              void                    * arg)
{
    return rtems_termios_write (arg);
 
} /* tty_write */

/*
 * Handle ioctl request. This is a generic internal
 * routine to handle both devices.
 */
static rtems_device_driver tty_control( int port, void  *arg )
{ 
	rtems_libio_ioctl_args_t *args = arg;
	switch( args->command ) 
	{
	   default:
      return rtems_termios_ioctl (arg);
		break;
   }
 	args->ioctl_return = 0;
   return RTEMS_SUCCESSFUL;
}



/*
 * Handle ioctl request for ttyS1.
 */
rtems_device_driver 
tty1_control(rtems_device_major_number major,
		rtems_device_minor_number minor,
		void                      * arg
)
{ 
  return tty_control( BSP_UART_COM1, arg );
}


static int
conSetAttr(int port, int minor, const struct termios *t)
{
  unsigned long baud, databits, parity, stopbits;

  switch (t->c_cflag & CBAUD) 
    {
    case B50:	
      baud = 50;
      break;
    case B75:	
      baud = 75;	
      break;
    case B110:	
      baud = 110;	
      break;
    case B134:	
      baud = 134;	
      break;
    case B150:	
      baud = 150;	
      break;
    case B200:
      baud = 200;	
      break;
    case B300:	
      baud = 300;
      break;
    case B600:	
      baud = 600;	
      break;
    case B1200:	
      baud = 1200;
      break;
    case B1800:	
      baud = 1800;	
      break;
    case B2400:	
      baud = 2400;
      break;
    case B4800:	
      baud = 4800;
      break;
    case B9600:	
      baud = 9600;
      break;
    case B19200:
      baud = 19200;
      break;
    case B38400:
      baud = 38400;
      break;
    case B57600:	
      baud = 57600;
      break;
    case B115200:
      baud = 115200;
      break;
    default:
      baud = 0;
      rtems_fatal_error_occurred (RTEMS_INTERNAL_ERROR);
      return 0;
    }
  if (t->c_cflag & PARENB) {
    /* Parity is enabled */
    if (t->c_cflag & PARODD) {
      /* Parity is odd */
      parity = PEN;
    }
    else {
      /* Parity is even */
      parity = PEN | EPS;
    }
  }
  else {
    /* No parity */
    parity = 0;
  }
  
  switch (t->c_cflag & CSIZE) {
    case CS5: databits = CHR_5_BITS; break;
    case CS6: databits = CHR_6_BITS; break;
    case CS7: databits = CHR_7_BITS; break;
    default:  /* just to avoid warnings -- all cases are covered */
    case CS8: databits = CHR_8_BITS; break;
  }

  if (t->c_cflag & CSTOPB) {
    /* 2 stop bits */
    stopbits = STB;
  }
  else {
    /* 1 stop bit */
    stopbits = 0;
  }

  printk("Setting attributes, port=%X, baud=%d, linemode = 0x%02x\n", port, baud, databits | parity | stopbits );
  BSP_uart_set_attributes(port, baud, databits, parity, stopbits);
  return 0;
}

/*
 * Handle ioctl request for ttyS2.
 */
static int
tty1_conSetAttr( int minor, const struct termios *t)
{
  return conSetAttr( BSP_UART_COM1, minor, t );
}



/*
 * TTY2 device driver INITIALIZE entry point.
 */
rtems_device_driver
tty2_initialize(rtems_device_major_number major,
                   rtems_device_minor_number minor,
                   void                      *arg)
{
  rtems_status_code status;

  /* Check if this port is not been used as console */
  if( BSPConsolePort == BSP_UART_COM2 )
  {
    status = -1;
    printk("TTY2: port selected as console.\n");
    rtems_fatal_error_occurred( status );
  }

  /*
   * Set up TERMIOS
   */
  rtems_termios_initialize();
  
  /*
   * Do device-specific initialization
   */
  /* 9600-8-N-1, without hardware flow control */
  BSP_uart_init( BSP_UART_COM2, 9600, CHR_8_BITS, 0, 0, 0);
  status = BSP_install_rtems_irq_handler( &tty2_isr_data );
  if( !status )
  {
    printk("Error installing serial console interrupt handler!\n");
    rtems_fatal_error_occurred(status);
  }
  /*
   * Register the device
   */
  status = rtems_io_register_name ("/dev/ttyS2", major, 0);
  if (status != RTEMS_SUCCESSFUL)
  {
      printk("Error registering tty2 device!\n");
      rtems_fatal_error_occurred (status);
  }
  printk("Device: /dev/ttyS2 initialized.\n");
  return RTEMS_SUCCESSFUL;
} /* tty_initialize */


static int tty2_last_close(int major, int minor, void *arg)
{
  BSP_remove_rtems_irq_handler( &tty2_isr_data );
  return 0;
}

/*
 * TTY2 device driver OPEN entry point
 */
rtems_device_driver
tty2_open(rtems_device_major_number major,
                rtems_device_minor_number minor,
                void                      *arg)
{
  rtems_status_code              status;
#ifndef USE_TASK_DRIVEN
  static rtems_termios_callbacks cb = 
  {
    NULL,                        /* firstOpen */
    tty2_last_close,             /* lastClose */
    NULL,                        /* poll read */
    BSP_uart_termios_write_com2, /* write */
    tty2_conSetAttr,             /* setAttributes */
    NULL,                        /* stopRemoteTx */
    NULL,                        /* startRemoteTx */
    TERMIOS_IRQ_DRIVEN           /* outputUsesInterrupts */
  };
#else
  static rtems_termios_callbacks cb = 
  {
    NULL,                        /* firstOpen */
    tty2_last_close,             /* lastClose */
    BSP_uart_termios_read_com2,  /* poll read */
    BSP_uart_termios_write_com2, /* write */
    tty2_conSetAttr,             /* setAttributes */
    NULL,                        /* stopRemoteTx */
    NULL,                        /* startRemoteTx */
    TERMIOS_TASK_DRIVEN          /* outputUsesInterrupts */
  };
#endif

  status = rtems_termios_open (major, minor, arg, &cb);
  if(status != RTEMS_SUCCESSFUL)
  {
     printk("Error openning tty1 device\n");
     return status;
  }

  /*
   * Pass data area info down to driver
   */
  BSP_uart_termios_set( BSP_UART_COM2, 
			 ((rtems_libio_open_close_args_t *)arg)->iop->data1 );
   /* Enable interrupts  on channel */
  BSP_uart_intr_ctrl( BSP_UART_COM2, BSP_UART_INTR_CTRL_TERMIOS);
  return RTEMS_SUCCESSFUL;
}

/*
 * Handle ioctl request for TTY2
 */
rtems_device_driver 
tty2_control(rtems_device_major_number major,
		rtems_device_minor_number minor,
		void                      * arg
)
{ 
   return tty_control( BSP_UART_COM2, arg );
}

static int
tty2_conSetAttr( int minor, const struct termios *t)
{
  return conSetAttr( BSP_UART_COM2, minor, t );
}