[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$ |
---|
| 16 | // |
---|
| 17 | ///////////////////////////////////////////////////////////////////////////// |
---|
| 18 | */ |
---|
| 19 | #include <stdio.h> |
---|
| 20 | #include <fcntl.h> |
---|
| 21 | #include <sys/ioctl.h> |
---|
| 22 | #include <errno.h> |
---|
| 23 | #include <rtems.h> |
---|
| 24 | #include <bsp.h> |
---|
| 25 | |
---|
| 26 | #include <rtems/mw_uid.h> |
---|
| 27 | #include <rtems/posix/seterr.h> |
---|
| 28 | |
---|
| 29 | static rtems_id queue_id = 0; |
---|
| 30 | static int open_count = 0; |
---|
| 31 | |
---|
| 32 | /* |
---|
| 33 | #define MW_DEBUG_ON 1 |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | /* open a message queue with the kernel */ |
---|
| 37 | int uid_open_queue( const char *q_name, int flags, size_t max_msgs ) |
---|
| 38 | { |
---|
| 39 | static rtems_name queue_name; |
---|
| 40 | |
---|
| 41 | /* |
---|
| 42 | * For the first device calling this function we would create the queue. |
---|
| 43 | * It is assumed that this call is done at initialization, and no concerns |
---|
| 44 | * regarding multi-threading is taken in consideration here. |
---|
| 45 | */ |
---|
| 46 | if( !open_count ) |
---|
| 47 | { |
---|
| 48 | rtems_status_code status; |
---|
| 49 | queue_name = rtems_build_name( q_name[0], |
---|
| 50 | q_name[1], |
---|
| 51 | q_name[2], |
---|
| 52 | q_name[3] ); |
---|
| 53 | status = rtems_message_queue_create( queue_name, |
---|
| 54 | max_msgs, |
---|
| 55 | sizeof( struct MW_UID_MESSAGE ), |
---|
| 56 | RTEMS_FIFO | RTEMS_LOCAL, |
---|
| 57 | &queue_id ); |
---|
| 58 | if( status != RTEMS_SUCCESSFUL ) |
---|
| 59 | { |
---|
| 60 | #ifdef MW_DEBUG_ON |
---|
| 61 | printk( "UID_Queue: error creating queue: %d\n", status ); |
---|
| 62 | #endif |
---|
| 63 | return -1; |
---|
| 64 | } |
---|
| 65 | #ifdef MW_DEBUG_ON |
---|
| 66 | printk( "UID_Queue: id=%X\n", queue_id ); |
---|
| 67 | #endif |
---|
| 68 | } |
---|
| 69 | open_count++; |
---|
| 70 | return 0; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | /* close message queue */ |
---|
| 75 | int uid_close_queue( void ) |
---|
| 76 | { |
---|
| 77 | if( open_count == 1 ) |
---|
| 78 | { |
---|
| 79 | rtems_message_queue_delete( queue_id ); |
---|
| 80 | queue_id = 0; |
---|
| 81 | } |
---|
| 82 | open_count--; |
---|
| 83 | return 0; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /* reads for a message from the device */ |
---|
| 87 | int uid_read_message( struct MW_UID_MESSAGE *m, unsigned long timeout ) |
---|
| 88 | { |
---|
| 89 | rtems_status_code status; |
---|
| 90 | rtems_unsigned32 size = 0; |
---|
| 91 | unsigned long micro_secs = timeout*1000; |
---|
| 92 | int wait = ( timeout != 0 ); |
---|
| 93 | |
---|
| 94 | status = rtems_message_queue_receive( queue_id, |
---|
| 95 | (void*)m, |
---|
| 96 | &size, |
---|
| 97 | wait ? RTEMS_WAIT : RTEMS_NO_WAIT, |
---|
| 98 | TOD_MICROSECONDS_TO_TICKS(micro_secs ) ); |
---|
| 99 | |
---|
| 100 | if( status == RTEMS_SUCCESSFUL ) |
---|
| 101 | { |
---|
| 102 | return size; |
---|
| 103 | } |
---|
| 104 | else if( ( status == RTEMS_UNSATISFIED ) || ( status == RTEMS_TIMEOUT ) ) |
---|
| 105 | { |
---|
| 106 | /* this macro returns -1 */ |
---|
| 107 | set_errno_and_return_minus_one( ETIMEDOUT ); |
---|
| 108 | } |
---|
| 109 | /* Here we have one error condition */ |
---|
| 110 | #ifdef MW_DEBUG_ON |
---|
| 111 | printk( "UID_Queue: error reading queue: %d\n", status ); |
---|
| 112 | #endif |
---|
| 113 | return -1; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | /* |
---|
| 118 | * add a message to the queue of events. This method cna be used to |
---|
| 119 | * simulate hardware events, and it can be very handy during development |
---|
| 120 | * a new interface. |
---|
| 121 | */ |
---|
| 122 | int uid_send_message( struct MW_UID_MESSAGE *m ) |
---|
| 123 | { |
---|
| 124 | rtems_status_code status; |
---|
| 125 | status = rtems_message_queue_send( queue_id, ( void * )m, |
---|
| 126 | sizeof( struct MW_UID_MESSAGE ) ); |
---|
| 127 | return status == RTEMS_SUCCESSFUL ? 0 : -1; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /* |
---|
| 131 | * register the device to insert events to the message |
---|
| 132 | * queue named as the value passed in q_name |
---|
| 133 | */ |
---|
| 134 | int uid_register_device( int fd, const char *q_name ) |
---|
| 135 | { |
---|
| 136 | return ioctl( fd, MW_UID_REGISTER_DEVICE, q_name ); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /* tell this device to stop adding events to the queue */ |
---|
| 140 | int uid_unregister_device( int fd ) |
---|
| 141 | { |
---|
| 142 | return ioctl( fd, MW_UID_UNREGISTER_DEVICE, NULL ); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /* set the keyboard */ |
---|
| 146 | int uid_set_kbd_mode( int fd, int mode, int *old_mode ) |
---|
| 147 | { |
---|
| 148 | if (ioctl( fd, MV_KDGKBMODE, old_mode) < 0) |
---|
| 149 | { |
---|
| 150 | return -1; |
---|
| 151 | } |
---|
| 152 | if (ioctl(fd, MV_KDSKBMODE, mode ) < 0 ) |
---|
| 153 | { |
---|
| 154 | return -1; |
---|
| 155 | } |
---|
| 156 | return 0; |
---|
| 157 | } |
---|