source: trunk/kernel/devices/dev_txt.c @ 107

Last change on this file since 107 was 101, checked in by alain, 7 years ago

euh...

File size: 6.4 KB
Line 
1/*
2 * dev_txt.c - TXT (Text Terminal) generic device API implementation.
3 *
4 * Author  Alain Greiner    (2016)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MK
9 *
10 * ALMOS-MKH.is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH.is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-kernel; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <hal_types.h>
25#include <hal_special.h>
26#include <hal_remote.h>
27#include <hal_drivers.h>
28#include <thread.h>
29#include <rpc.h>
30#include <printk.h>
31#include <dev_txt.h>
32#include <soclib_tty.h>
33
34/////////////////////////////////////////////////////////////////////////////////////////
35// Extern global variables
36/////////////////////////////////////////////////////////////////////////////////////////
37
38extern chdev_directory_t  chdev_dir;         // allocated in kernel_init.c
39
40extern chdev_pic_input_t  chdev_pic_input;   // allocated in kernel_init.c
41
42////////////////////////////////////
43void dev_txt_init( chdev_t * chdev )
44{
45    // the local ICU chdev must be initialized before the TXT chdev, because
46    // the TXT chdev initialization requires allocation of a WTI from local ICU
47    xptr_t  icu_xp  = chdev_dir.icu[local_cxy];
48    assert( (icu_xp != XPTR_NULL) , __FUNCTION__ , "ICU not initialised before TXT" );
49
50    // get implementation and channel index
51    uint32_t  impl    = chdev->impl;
52    uint32_t  channel = chdev->channel;
53
54    // set chdev name
55    snprintf( chdev->name , 16 , "txt_%d" , chdev->channel );
56
57    // set fields "cmd", "isr", and call driver init function
58    if( impl == IMPL_TXT_TTY )
59    {
60        chdev->cmd = &soclib_tty_cmd;
61        chdev->isr = &soclib_tty_isr;
62        soclib_tty_init( chdev );
63    }
64    else
65    {
66        assert( false , __FUNCTION__ , "undefined TXT device implementation" );
67    }
68
69    // get a WTI mailbox from local ICU
70    uint32_t wti_id = dev_icu_wti_alloc();
71
72    assert( (wti_id != -1) , __FUNCTION__ , "cannot allocate WTI mailbox" );
73
74    // select a core
75    lid_t lid = cluster_select_local_core();
76
77    // enable WTI IRQ and update WTI interrupt vector
78    dev_icu_enable_irq( lid , WTI_TYPE , wti_id , chdev );
79
80    // link IOC IRQ to WTI mailbox in PIC component
81    uint32_t irq_id = chdev_pic_input.txt[channel];
82    dev_pic_bind_irq( irq_id , local_cxy , wti_id );
83
84    // create server thread
85    thread_t * new_thread;
86    error_t    error;
87
88    error = thread_kernel_create( &new_thread,
89                                  THREAD_DEV,
90                                  &chdev_sequencial_server,
91                                  chdev,
92                                  lid );
93    assert( (error == 0) , __FUNCTION__ , "cannot create server thread" );
94
95    // set "server" field in chdev descriptor
96    chdev->server = new_thread;
97
98    // start server thread
99    thread_unblock( XPTR( local_cxy , new_thread ) , THREAD_BLOCKED_GLOBAL );
100}
101
102//////////////////////////////////////////////////////////////////////////////////
103// This static function is called by dev_txt_read(), dev_txt_write() functions.
104////////////////////////////////////i/////////////////////////////////////////////
105static error_t dev_txt_access( uint32_t   type,
106                               uint32_t   channel,
107                               char     * buffer,
108                               uint32_t   count )
109{
110    thread_t * this = CURRENT_THREAD;
111
112    txt_dmsg("\n[INFO] in %s : thread %x in process %x enters\n",
113                 __FUNCTION__ , this->trdid , this->process->pid );
114
115    // check channel argument
116    assert( (channel < CONFIG_MAX_TXT_CHANNELS) , __FUNCTION__ , "illegal channel index" );
117
118    // get extended pointer on remote TXT chdev descriptor
119    xptr_t  dev_xp = chdev_dir.txt[channel];
120
121    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT chdev descriptor" );
122
123    // register command in calling thread descriptor
124    this->command.txt.dev_xp  = dev_xp;
125    this->command.txt.type    = type;
126    this->command.txt.buf_xp  = XPTR( local_cxy , buffer );
127    this->command.txt.count   = count;
128
129    // register client thread in waiting queue, activate server thread
130    // block client thread on THREAD_BLOCKED_IO and deschedule.
131    // it is re-activated by the ISR signaling IO operation completion.
132    chdev_register_command( dev_xp , this );
133
134    txt_dmsg("\n[INFO] in %s : thread %x in process %x completes / error = %d\n",
135             __FUNCTION__ , this->trdid , this->process->pid , this->command.txt.error );
136
137    // return I/O operation status from calling thread descriptor
138    return this->command.txt.error;
139}
140
141/////////////////////////////////////////
142error_t dev_txt_write( uint32_t   channel,
143                       char     * buffer,
144                       uint32_t   count )
145{
146    return dev_txt_access( TXT_WRITE , channel , buffer , count );
147}
148
149/////////////////////////////////////////
150error_t dev_txt_read( uint32_t   channel,
151                      char     * buffer )
152{
153    return dev_txt_access( TXT_READ , channel , buffer , 1 );
154}
155
156///////////////////////////////////////////////
157error_t dev_txt_sync_write( uint32_t   channel,
158                            char     * buffer,
159                            uint32_t   count )
160{
161    // get pointer on calling thread
162    thread_t * this = CURRENT_THREAD;
163
164    // get extended pointer on TXT[0] chdev
165    xptr_t  dev_xp = chdev_dir.txt[channel];
166
167    assert( (dev_xp != XPTR_NULL) , __FUNCTION__ , "undefined TXT0 chdev descriptor" );
168
169    // register command in calling thread
170    this->command.txt.dev_xp  = dev_xp;
171    this->command.txt.type    = TXT_SYNC_WRITE;
172    this->command.txt.buf_xp  = XPTR( local_cxy , buffer );
173    this->command.txt.count   = count;
174
175    // get driver command function
176    cxy_t       dev_cxy = GET_CXY( dev_xp );
177    chdev_t   * dev_ptr = (chdev_t *)GET_PTR( dev_xp );
178    dev_cmd_t * cmd = (dev_cmd_t *)hal_remote_lpt( XPTR( dev_cxy , &dev_ptr->cmd ) );
179
180    // call directly driver command
181    cmd( XPTR( local_cxy , this ) );
182
183    // return I/O operation status from calling thread descriptor
184    return this->command.txt.error;
185}
186
Note: See TracBrowser for help on using the repository browser.