[152b1e3] | 1 | /* |
---|
| 2 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 3 | // $Header$ |
---|
| 4 | // |
---|
| 5 | // Copyright (c) 2000 - Rosimildo da Silva |
---|
| 6 | // |
---|
| 7 | // MODULE DESCRIPTION: |
---|
| 8 | // This module implements the input devices interface used by MicroWindows |
---|
| 9 | // in an embedded system environment. |
---|
| 10 | // It uses the RTEMS message queue as the repository for the messages posted |
---|
| 11 | // by the devices registered. |
---|
| 12 | // |
---|
| 13 | // MODIFICATION/HISTORY: |
---|
| 14 | // |
---|
| 15 | // $Log$ |
---|
[f8a913da] | 16 | // Revision 1.3 2000/11/30 14:36:46 joel |
---|
| 17 | // 2000-11-30 Joel Sherrill <joel@OARcorp.com> |
---|
| 18 | // |
---|
| 19 | // * mw-fb/mw_uid.c: Removed unnecessary dependency on <bsp.h>. |
---|
| 20 | // |
---|
[4a14b7be] | 21 | // Revision 1.2 2000/08/30 17:12:55 joel |
---|
| 22 | // 2000-08-30 Joel Sherrill <joel@OARcorp.com> |
---|
| 23 | // |
---|
| 24 | // * Many files: Moved posix/include/rtems/posix/seterr.h to |
---|
| 25 | // score/include/rtems/seterr.h so it would be available within |
---|
| 26 | // all APIs. |
---|
| 27 | // |
---|
[188c82b] | 28 | // Revision 1.1 2000/08/30 08:21:24 joel |
---|
| 29 | // 2000-08-26 Rosimildo da Silva <rdasilva@connecttel.com> |
---|
| 30 | // |
---|
| 31 | // * Added generic Micro FrameBuffer interface for MicroWindows. |
---|
| 32 | // This interface allows MicroWindows to under RTEMS. A sample |
---|
| 33 | // driver has been developed for the pc386 BSP. See |
---|
| 34 | // pc386/fb_vga.c as a sample. |
---|
| 35 | // * Added Uniform Input Device interface for MicroWindows. |
---|
| 36 | // See PC386 bsp for sample drivers for mouse and keyboard (console). |
---|
| 37 | // * mw-bf: New directory. |
---|
| 38 | // * Makefile.am, configure.in, wrapup/Makefile.am: Account for mw-fb. |
---|
| 39 | // * mw-fb/Makefile.am: New file. |
---|
| 40 | // * mw-fb/mw_fb.c: New file. |
---|
| 41 | // * mw-fb/mw_fb.h: New file. |
---|
| 42 | // * mw-fb/mw_uid.c: New file. |
---|
| 43 | // * mw-fb/mw_uid.h: New file. |
---|
| 44 | // |
---|
[152b1e3] | 45 | // |
---|
| 46 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 47 | */ |
---|
| 48 | #include <stdio.h> |
---|
| 49 | #include <fcntl.h> |
---|
| 50 | #include <sys/ioctl.h> |
---|
| 51 | #include <errno.h> |
---|
| 52 | #include <rtems.h> |
---|
| 53 | |
---|
| 54 | #include <rtems/mw_uid.h> |
---|
[188c82b] | 55 | #include <rtems/seterr.h> |
---|
[152b1e3] | 56 | |
---|
| 57 | static rtems_id queue_id = 0; |
---|
| 58 | static int open_count = 0; |
---|
| 59 | |
---|
| 60 | /* |
---|
| 61 | #define MW_DEBUG_ON 1 |
---|
| 62 | */ |
---|
| 63 | |
---|
| 64 | /* open a message queue with the kernel */ |
---|
| 65 | int uid_open_queue( const char *q_name, int flags, size_t max_msgs ) |
---|
| 66 | { |
---|
| 67 | static rtems_name queue_name; |
---|
| 68 | |
---|
| 69 | /* |
---|
| 70 | * For the first device calling this function we would create the queue. |
---|
| 71 | * It is assumed that this call is done at initialization, and no concerns |
---|
| 72 | * regarding multi-threading is taken in consideration here. |
---|
| 73 | */ |
---|
| 74 | if( !open_count ) |
---|
| 75 | { |
---|
| 76 | rtems_status_code status; |
---|
| 77 | queue_name = rtems_build_name( q_name[0], |
---|
| 78 | q_name[1], |
---|
| 79 | q_name[2], |
---|
| 80 | q_name[3] ); |
---|
| 81 | status = rtems_message_queue_create( queue_name, |
---|
| 82 | max_msgs, |
---|
| 83 | sizeof( struct MW_UID_MESSAGE ), |
---|
| 84 | RTEMS_FIFO | RTEMS_LOCAL, |
---|
| 85 | &queue_id ); |
---|
| 86 | if( status != RTEMS_SUCCESSFUL ) |
---|
| 87 | { |
---|
| 88 | #ifdef MW_DEBUG_ON |
---|
| 89 | printk( "UID_Queue: error creating queue: %d\n", status ); |
---|
| 90 | #endif |
---|
| 91 | return -1; |
---|
| 92 | } |
---|
| 93 | #ifdef MW_DEBUG_ON |
---|
| 94 | printk( "UID_Queue: id=%X\n", queue_id ); |
---|
| 95 | #endif |
---|
| 96 | } |
---|
| 97 | open_count++; |
---|
| 98 | return 0; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | |
---|
| 102 | /* close message queue */ |
---|
| 103 | int uid_close_queue( void ) |
---|
| 104 | { |
---|
| 105 | if( open_count == 1 ) |
---|
| 106 | { |
---|
| 107 | rtems_message_queue_delete( queue_id ); |
---|
| 108 | queue_id = 0; |
---|
| 109 | } |
---|
| 110 | open_count--; |
---|
| 111 | return 0; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /* reads for a message from the device */ |
---|
| 115 | int uid_read_message( struct MW_UID_MESSAGE *m, unsigned long timeout ) |
---|
| 116 | { |
---|
| 117 | rtems_status_code status; |
---|
| 118 | rtems_unsigned32 size = 0; |
---|
| 119 | unsigned long micro_secs = timeout*1000; |
---|
| 120 | int wait = ( timeout != 0 ); |
---|
| 121 | |
---|
| 122 | status = rtems_message_queue_receive( queue_id, |
---|
| 123 | (void*)m, |
---|
| 124 | &size, |
---|
| 125 | wait ? RTEMS_WAIT : RTEMS_NO_WAIT, |
---|
| 126 | TOD_MICROSECONDS_TO_TICKS(micro_secs ) ); |
---|
| 127 | |
---|
| 128 | if( status == RTEMS_SUCCESSFUL ) |
---|
| 129 | { |
---|
| 130 | return size; |
---|
| 131 | } |
---|
| 132 | else if( ( status == RTEMS_UNSATISFIED ) || ( status == RTEMS_TIMEOUT ) ) |
---|
| 133 | { |
---|
| 134 | /* this macro returns -1 */ |
---|
[f8a913da] | 135 | rtems_set_errno_and_return_minus_one( ETIMEDOUT ); |
---|
[152b1e3] | 136 | } |
---|
| 137 | /* Here we have one error condition */ |
---|
| 138 | #ifdef MW_DEBUG_ON |
---|
| 139 | printk( "UID_Queue: error reading queue: %d\n", status ); |
---|
| 140 | #endif |
---|
| 141 | return -1; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | |
---|
| 145 | /* |
---|
| 146 | * add a message to the queue of events. This method cna be used to |
---|
| 147 | * simulate hardware events, and it can be very handy during development |
---|
| 148 | * a new interface. |
---|
| 149 | */ |
---|
| 150 | int uid_send_message( struct MW_UID_MESSAGE *m ) |
---|
| 151 | { |
---|
| 152 | rtems_status_code status; |
---|
| 153 | status = rtems_message_queue_send( queue_id, ( void * )m, |
---|
| 154 | sizeof( struct MW_UID_MESSAGE ) ); |
---|
| 155 | return status == RTEMS_SUCCESSFUL ? 0 : -1; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | /* |
---|
| 159 | * register the device to insert events to the message |
---|
| 160 | * queue named as the value passed in q_name |
---|
| 161 | */ |
---|
| 162 | int uid_register_device( int fd, const char *q_name ) |
---|
| 163 | { |
---|
| 164 | return ioctl( fd, MW_UID_REGISTER_DEVICE, q_name ); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /* tell this device to stop adding events to the queue */ |
---|
| 168 | int uid_unregister_device( int fd ) |
---|
| 169 | { |
---|
| 170 | return ioctl( fd, MW_UID_UNREGISTER_DEVICE, NULL ); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | /* set the keyboard */ |
---|
| 174 | int uid_set_kbd_mode( int fd, int mode, int *old_mode ) |
---|
| 175 | { |
---|
| 176 | if (ioctl( fd, MV_KDGKBMODE, old_mode) < 0) |
---|
| 177 | { |
---|
| 178 | return -1; |
---|
| 179 | } |
---|
| 180 | if (ioctl(fd, MV_KDSKBMODE, mode ) < 0 ) |
---|
| 181 | { |
---|
| 182 | return -1; |
---|
| 183 | } |
---|
| 184 | return 0; |
---|
| 185 | } |
---|