1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief MicroWindows UID |
---|
5 | * @ingroup libmisc_fb_mw Input Devices for MicroWindows |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * Copyright (c) 2000 - Rosimildo da Silva |
---|
10 | * |
---|
11 | * MODULE DESCRIPTION: |
---|
12 | * This module implements the input devices interface used by MicroWindows |
---|
13 | * in an embedded system environment. It uses the RTEMS message queue as |
---|
14 | * the repository for the messages posted by the devices registered. |
---|
15 | */ |
---|
16 | |
---|
17 | #ifdef HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <stdio.h> |
---|
22 | #include <fcntl.h> |
---|
23 | #include <sys/ioctl.h> |
---|
24 | #include <errno.h> |
---|
25 | #include <rtems.h> |
---|
26 | |
---|
27 | #include <rtems/mw_uid.h> |
---|
28 | #include <rtems/seterr.h> |
---|
29 | |
---|
30 | static rtems_id queue_id = 0; |
---|
31 | static int open_count = 0; |
---|
32 | |
---|
33 | /* |
---|
34 | #define MW_DEBUG_ON 1 |
---|
35 | */ |
---|
36 | |
---|
37 | /* open a message queue with the kernel */ |
---|
38 | int uid_open_queue( |
---|
39 | const char *q_name, |
---|
40 | int flags __attribute__((unused)), |
---|
41 | size_t max_msgs |
---|
42 | ) |
---|
43 | { |
---|
44 | rtems_status_code status; |
---|
45 | |
---|
46 | /* |
---|
47 | * For the first device calling this function we would create the queue. |
---|
48 | * It is assumed that this call is done at initialization, and no concerns |
---|
49 | * regarding multi-threading is taken in consideration here. |
---|
50 | */ |
---|
51 | if ( open_count ) { |
---|
52 | open_count++; |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | status = rtems_message_queue_create( |
---|
57 | rtems_build_name( q_name[0], q_name[1], q_name[2], q_name[3] ), |
---|
58 | max_msgs, |
---|
59 | sizeof( struct MW_UID_MESSAGE ), |
---|
60 | RTEMS_FIFO | RTEMS_LOCAL, |
---|
61 | &queue_id |
---|
62 | ); |
---|
63 | if ( status != RTEMS_SUCCESSFUL ) { |
---|
64 | #ifdef MW_DEBUG_ON |
---|
65 | printk( "UID_Queue: error creating queue: %d\n", status ); |
---|
66 | #endif |
---|
67 | return -1; |
---|
68 | } |
---|
69 | #ifdef MW_DEBUG_ON |
---|
70 | printk( "UID_Queue: id=%X\n", queue_id ); |
---|
71 | #endif |
---|
72 | open_count++; |
---|
73 | return 0; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | /* close message queue */ |
---|
78 | int uid_close_queue( void ) |
---|
79 | { |
---|
80 | if ( open_count == 1 ) { |
---|
81 | rtems_message_queue_delete( queue_id ); |
---|
82 | queue_id = 0; |
---|
83 | } |
---|
84 | open_count--; |
---|
85 | return 0; |
---|
86 | } |
---|
87 | |
---|
88 | /* reads for a message from the device */ |
---|
89 | int uid_read_message( struct MW_UID_MESSAGE *m, unsigned long timeout ) |
---|
90 | { |
---|
91 | rtems_status_code status; |
---|
92 | size_t size = 0; |
---|
93 | unsigned long micro_secs = timeout*1000; |
---|
94 | int wait = (timeout != 0); |
---|
95 | |
---|
96 | status = rtems_message_queue_receive( |
---|
97 | queue_id, |
---|
98 | (void*)m, |
---|
99 | &size, |
---|
100 | wait ? RTEMS_WAIT : RTEMS_NO_WAIT, |
---|
101 | RTEMS_MICROSECONDS_TO_TICKS(micro_secs) |
---|
102 | ); |
---|
103 | |
---|
104 | if( status == RTEMS_SUCCESSFUL ) { |
---|
105 | return size; |
---|
106 | } else if( ( status == RTEMS_UNSATISFIED ) || ( status == RTEMS_TIMEOUT ) ) { |
---|
107 | rtems_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 | * add a message to the queue of events. This method can be used to |
---|
118 | * simulate hardware events, and it can be very handy during development |
---|
119 | * a new interface. |
---|
120 | */ |
---|
121 | int uid_send_message( struct MW_UID_MESSAGE *m ) |
---|
122 | { |
---|
123 | rtems_status_code status; |
---|
124 | status = rtems_message_queue_send( |
---|
125 | queue_id, ( void * )m, sizeof( struct MW_UID_MESSAGE ) ); |
---|
126 | return (status == RTEMS_SUCCESSFUL) ? 0 : -1; |
---|
127 | } |
---|
128 | |
---|
129 | /* |
---|
130 | * register the device to insert events to the message |
---|
131 | * queue named as the value passed in q_name |
---|
132 | */ |
---|
133 | int uid_register_device( int fd, const char *q_name ) |
---|
134 | { |
---|
135 | return ioctl( fd, MW_UID_REGISTER_DEVICE, q_name ); |
---|
136 | } |
---|
137 | |
---|
138 | /* tell this device to stop adding events to the queue */ |
---|
139 | int uid_unregister_device( int fd ) |
---|
140 | { |
---|
141 | return ioctl( fd, MW_UID_UNREGISTER_DEVICE, NULL ); |
---|
142 | } |
---|
143 | |
---|
144 | /* set the keyboard */ |
---|
145 | int uid_set_kbd_mode( int fd, int mode, int *old_mode ) |
---|
146 | { |
---|
147 | if (ioctl( fd, MV_KDGKBMODE, old_mode) < 0) { |
---|
148 | return -1; |
---|
149 | } |
---|
150 | if (ioctl(fd, MV_KDSKBMODE, mode ) < 0 ) { |
---|
151 | return -1; |
---|
152 | } |
---|
153 | return 0; |
---|
154 | } |
---|