aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/src/ext.c
blob: c53cd95febf26187aa5663b0b5c3b77b4f5d525a (plain)
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
/*
    ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
                 2011,2012 Giovanni Di Sirio.

    This file is part of ChibiOS/RT.

    ChibiOS/RT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    ChibiOS/RT is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file    ext.c
 * @brief   EXT Driver code.
 *
 * @addtogroup EXT
 * @{
 */

#include "ch.h"
#include "hal.h"

#if HAL_USE_EXT || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver local definitions.                                                 */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local variables.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   EXT Driver initialization.
 * @note    This function is implicitly invoked by @p halInit(), there is
 *          no need to explicitly initialize the driver.
 *
 * @init
 */
void extInit(void) {

  ext_lld_init();
}

/**
 * @brief   Initializes the standard part of a @p EXTDriver structure.
 *
 * @param[out] extp     pointer to the @p EXTDriver object
 *
 * @init
 */
void extObjectInit(EXTDriver *extp) {

  extp->state  = EXT_STOP;
  extp->config = NULL;
}

/**
 * @brief   Configures and activates the EXT peripheral.
 * @post    After activation all EXT channels are in the disabled state,
 *          use @p extChannelEnable() in order to activate them.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 * @param[in] config    pointer to the @p EXTConfig object
 *
 * @api
 */
void extStart(EXTDriver *extp, const EXTConfig *config) {

  chDbgCheck((extp != NULL) && (config != NULL), "extStart");

  chSysLock();
  chDbgAssert((extp->state == EXT_STOP) || (extp->state == EXT_ACTIVE),
              "extStart(), #1", "invalid state");
  extp->config = config;
  ext_lld_start(extp);
  extp->state = EXT_ACTIVE;
  chSysUnlock();
}

/**
 * @brief   Deactivates the EXT peripheral.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 *
 * @api
 */
void extStop(EXTDriver *extp) {

  chDbgCheck(extp != NULL, "extStop");

  chSysLock();
  chDbgAssert((extp->state == EXT_STOP) || (extp->state == EXT_ACTIVE),
              "extStop(), #1", "invalid state");
  ext_lld_stop(extp);
  extp->state = EXT_STOP;
  chSysUnlock();
}

/**
 * @brief   Enables an EXT channel.
 * @pre     The channel must not be in @p EXT_CH_MODE_DISABLED mode.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 * @param[in] channel   channel to be enabled
 *
 * @api
 */
void extChannelEnable(EXTDriver *extp, expchannel_t channel) {

  chDbgCheck((extp != NULL) && (channel < EXT_MAX_CHANNELS),
             "extChannelEnable");

  chSysLock();
  chDbgAssert((extp->state == EXT_ACTIVE) &&
              ((extp->config->channels[channel].mode &
                EXT_CH_MODE_EDGES_MASK) != EXT_CH_MODE_DISABLED),
              "extChannelEnable(), #1", "invalid state");
  extChannelEnableI(extp, channel);
  chSysUnlock();
}

/**
 * @brief   Disables an EXT channel.
 * @pre     The channel must not be in @p EXT_CH_MODE_DISABLED mode.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 * @param[in] channel   channel to be disabled
 *
 * @api
 */
void extChannelDisable(EXTDriver *extp, expchannel_t channel) {

  chDbgCheck((extp != NULL) && (channel < EXT_MAX_CHANNELS),
             "extChannelDisable");

  chSysLock();
  chDbgAssert((extp->state == EXT_ACTIVE) &&
              ((extp->config->channels[channel].mode &
                EXT_CH_MODE_EDGES_MASK) != EXT_CH_MODE_DISABLED),
              "extChannelDisable(), #1", "invalid state");
  extChannelDisableI(extp, channel);
  chSysUnlock();
}

/**
 * @brief   Changes the operation mode of a channel.
 * @note    This function attempts to write over the current configuration
 *          structure that must have been not declared constant. This
 *          violates the @p const qualifier in @p extStart() but it is
 *          intentional.
 * @note    This function cannot be used if the configuration structure is
 *          declared @p const.
 * @note    The effect of this function on constant configuration structures
 *          is not defined.
 *
 * @param[in] extp      pointer to the @p EXTDriver object
 * @param[in] channel   channel to be changed
 * @param[in] extcp     new configuration for the channel
 *
 * @iclass
 */
void extSetChannelModeI(EXTDriver *extp,
                        expchannel_t channel,
                        const EXTChannelConfig *extcp) {
  EXTChannelConfig *oldcp;

  chDbgCheck((extp != NULL) && (channel < EXT_MAX_CHANNELS) &&
             (extcp != NULL), "extSetChannelModeI");

  chDbgAssert(extp->state == EXT_ACTIVE,
              "extSetChannelModeI(), #1", "invalid state");

  /* Note that here the access is enforced as non-const, known access
     violation.*/
  oldcp = (EXTChannelConfig *)&extp->config->channels[channel];

  /* Overwiting the old channels configuration then the channel is reconfigured
     by the low level driver.*/
  *oldcp = *extcp;
  ext_lld_channel_enable(extp, channel);
}

#endif /* HAL_USE_EXT */

/** @} */
">uint8_t data[PS2_QUEUE_SIZE]; int rptr, wptr, count; } PS2Queue; typedef struct { PS2Queue queue; int32_t write_cmd; void (*update_irq)(void *, int); void *update_arg; } PS2State; typedef struct { PS2State common; int scan_enabled; /* Qemu uses translated PC scancodes internally. To avoid multiple conversions we do the translation (if any) in the PS/2 emulation not the keyboard controller. */ int translate; } PS2KbdState; typedef struct { PS2State common; uint8_t mouse_status; uint8_t mouse_resolution; uint8_t mouse_sample_rate; uint8_t mouse_wrap; uint8_t mouse_type; /* 0 = PS2, 3 = IMPS/2, 4 = IMEX */ uint8_t mouse_detect_state; int mouse_dx; /* current values, needed for 'poll' mode */ int mouse_dy; int mouse_dz; uint8_t mouse_buttons; } PS2MouseState; /* Table to convert from PC scancodes to raw scancodes. */ static const unsigned char ps2_raw_keycode[128] = { 0,118, 22, 30, 38, 37, 46, 54, 61, 62, 70, 69, 78, 85,102, 13, 21, 29, 36, 45, 44, 53, 60, 67, 68, 77, 84, 91, 90, 20, 28, 27, 35, 43, 52, 51, 59, 66, 75, 76, 82, 14, 18, 93, 26, 34, 33, 42, 50, 49, 58, 65, 73, 74, 89,124, 17, 41, 88, 5, 6, 4, 12, 3, 11, 2, 10, 1, 9,119,126,108,117,125,123,107,115,116,121,105, 114,122,112,113,127, 96, 97,120, 7, 15, 23, 31, 39, 47, 55, 63, 71, 79, 86, 94, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 87,111, 19, 25, 57, 81, 83, 92, 95, 98, 99,100,101,103,104,106,109,110 }; void ps2_queue(void *opaque, int b) { PS2State *s = (PS2State *)opaque; PS2Queue *q = &s->queue; if (q->count >= PS2_QUEUE_SIZE) return; q->data[q->wptr] = b; if (++q->wptr == PS2_QUEUE_SIZE) q->wptr = 0; q->count++; s->update_irq(s->update_arg, 1); } static void ps2_put_keycode(void *opaque, int keycode) { PS2KbdState *s = opaque; if (!s->translate && keycode < 0xe0) { if (keycode & 0x80) ps2_queue(&s->common, 0xf0); keycode = ps2_raw_keycode[keycode & 0x7f]; } ps2_queue(&s->common, keycode); } uint32_t ps2_read_data(void *opaque) { PS2State *s = (PS2State *)opaque; PS2Queue *q; int val, index; q = &s->queue; if (q->count == 0) { /* NOTE: if no data left, we return the last keyboard one (needed for EMM386) */ /* XXX: need a timer to do things correctly */ index = q->rptr - 1; if (index < 0) index = PS2_QUEUE_SIZE - 1; val = q->data[index]; } else { val = q->data[q->rptr]; if (++q->rptr == PS2_QUEUE_SIZE) q->rptr = 0; q->count--; /* reading deasserts IRQ */ s->update_irq(s->update_arg, 0); /* reassert IRQs if data left */ s->update_irq(s->update_arg, q->count != 0); } return val; } static void ps2_reset_keyboard(PS2KbdState *s) { s->scan_enabled = 1; } void ps2_write_keyboard(void *opaque, int val) { PS2KbdState *s = (PS2KbdState *)opaque; switch(s->common.write_cmd) { default: case -1: switch(val) { case 0x00: ps2_queue(&s->common, KBD_REPLY_ACK); break; case 0x05: ps2_queue(&s->common, KBD_REPLY_RESEND); break; case KBD_CMD_GET_ID: ps2_queue(&s->common, KBD_REPLY_ACK); ps2_queue(&s->common, 0xab); ps2_queue(&s->common, 0x83); break; case KBD_CMD_ECHO: ps2_queue(&s->common, KBD_CMD_ECHO); break; case KBD_CMD_ENABLE: s->scan_enabled = 1; ps2_queue(&s->common, KBD_REPLY_ACK); break; case KBD_CMD_SET_LEDS: case KBD_CMD_SET_RATE: s->common.write_cmd = val; ps2_queue(&s->common, KBD_REPLY_ACK); break; case KBD_CMD_RESET_DISABLE: ps2_reset_keyboard(s); s->scan_enabled = 0; ps2_queue(&s->common, KBD_REPLY_ACK); break; case KBD_CMD_RESET_ENABLE: ps2_reset_keyboard(s); s->scan_enabled = 1; ps2_queue(&s->common, KBD_REPLY_ACK); break; case KBD_CMD_RESET: ps2_reset_keyboard(s); ps2_queue(&s->common, KBD_REPLY_ACK); ps2_queue(&s->common, KBD_REPLY_POR); break; default: ps2_queue(&s->common, KBD_REPLY_ACK); break; } break; case KBD_CMD_SET_LEDS: ps2_queue(&s->common, KBD_REPLY_ACK); s->common.write_cmd = -1; break; case KBD_CMD_SET_RATE: ps2_queue(&s->common, KBD_REPLY_ACK); s->common.write_cmd = -1; break; } } /* Set the scancode translation mode. 0 = raw scancodes. 1 = translated scancodes (used by qemu internally). */ void ps2_keyboard_set_translation(void *opaque, int mode) { PS2KbdState *s = (PS2KbdState *)opaque; s->translate = mode; } static void ps2_mouse_send_packet(PS2MouseState *s) { unsigned int b; int dx1, dy1, dz1; dx1 = s->mouse_dx; dy1 = s->mouse_dy; dz1 = s->mouse_dz; /* XXX: increase range to 8 bits ? */ if (dx1 > 127) dx1 = 127; else if (dx1 < -127) dx1 = -127; if (dy1 > 127) dy1 = 127; else if (dy1 < -127) dy1 = -127; b = 0x08 | ((dx1 < 0) << 4) | ((dy1 < 0) << 5) | (s->mouse_buttons & 0x07); ps2_queue(&s->common, b); ps2_queue(&s->common, dx1 & 0xff); ps2_queue(&s->common, dy1 & 0xff); /* extra byte for IMPS/2 or IMEX */ switch(s->mouse_type) { default: break; case 3: if (dz1 > 127) dz1 = 127; else if (dz1 < -127) dz1 = -127; ps2_queue(&s->common, dz1 & 0xff); break; case 4: if (dz1 > 7) dz1 = 7; else if (dz1 < -7) dz1 = -7; b = (dz1 & 0x0f) | ((s->mouse_buttons & 0x18) << 1); ps2_queue(&s->common, b); break; } /* update deltas */ s->mouse_dx -= dx1; s->mouse_dy -= dy1; s->mouse_dz -= dz1; } static void ps2_mouse_event(void *opaque, int dx, int dy, int dz, int buttons_state) { PS2MouseState *s = opaque; /* check if deltas are recorded when disabled */ if (!(s->mouse_status & MOUSE_STATUS_ENABLED)) return; s->mouse_dx += dx; s->mouse_dy -= dy; s->mouse_dz += dz; /* XXX: SDL sometimes generates nul events: we delete them */ if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0 && s->mouse_buttons == buttons_state) return; s->mouse_buttons = buttons_state; if (!(s->mouse_status & MOUSE_STATUS_REMOTE) && (s->common.queue.count < (PS2_QUEUE_SIZE - 16))) { for(;;) { /* if not remote, send event. Multiple events are sent if too big deltas */ ps2_mouse_send_packet(s); if (s->mouse_dx == 0 && s->mouse_dy == 0 && s->mouse_dz == 0) break; } } } void ps2_write_mouse(void *opaque, int val) { PS2MouseState *s = (PS2MouseState *)opaque; #ifdef DEBUG_MOUSE printf("kbd: write mouse 0x%02x\n", val); #endif switch(s->common.write_cmd) { default: case -1: /* mouse command */ if (s->mouse_wrap) { if (val == AUX_RESET_WRAP) { s->mouse_wrap = 0; ps2_queue(&s->common, AUX_ACK); return; } else if (val != AUX_RESET) { ps2_queue(&s->common, val); return; } } switch(val) { case AUX_SET_SCALE11: s->mouse_status &= ~MOUSE_STATUS_SCALE21; ps2_queue(&s->common, AUX_ACK); break; case AUX_SET_SCALE21: s->mouse_status |= MOUSE_STATUS_SCALE21; ps2_queue(&s->common, AUX_ACK); break; case AUX_SET_STREAM: s->mouse_status &= ~MOUSE_STATUS_REMOTE; ps2_queue(&s->common, AUX_ACK); break; case AUX_SET_WRAP: s->mouse_wrap = 1; ps2_queue(&s->common, AUX_ACK); break; case AUX_SET_REMOTE: s->mouse_status |= MOUSE_STATUS_REMOTE; ps2_queue(&s->common, AUX_ACK); break; case AUX_GET_TYPE: ps2_queue(&s->common, AUX_ACK); ps2_queue(&s->common, s->mouse_type); break; case AUX_SET_RES: case AUX_SET_SAMPLE: s->common.write_cmd = val; ps2_queue(&s->common, AUX_ACK); break; case AUX_GET_SCALE: ps2_queue(&s->common, AUX_ACK); ps2_queue(&s->common, s->mouse_status); ps2_queue(&s->common, s->mouse_resolution); ps2_queue(&s->common, s->mouse_sample_rate); break; case AUX_POLL: ps2_queue(&s->common, AUX_ACK); ps2_mouse_send_packet(s); break; case AUX_ENABLE_DEV: s->mouse_status |= MOUSE_STATUS_ENABLED; ps2_queue(&s->common, AUX_ACK); break; case AUX_DISABLE_DEV: s->mouse_status &= ~MOUSE_STATUS_ENABLED; ps2_queue(&s->common, AUX_ACK); break; case AUX_SET_DEFAULT: s->mouse_sample_rate = 100; s->mouse_resolution = 2; s->mouse_status = 0; ps2_queue(&s->common, AUX_ACK); break; case AUX_RESET: s->mouse_sample_rate = 100; s->mouse_resolution = 2; s->mouse_status = 0; s->mouse_type = 0; ps2_queue(&s->common, AUX_ACK); ps2_queue(&s->common, 0xaa); ps2_queue(&s->common, s->mouse_type); break; default: break; } break; case AUX_SET_SAMPLE: s->mouse_sample_rate = val; /* detect IMPS/2 or IMEX */ switch(s->mouse_detect_state) { default: case 0: if (val == 200) s->mouse_detect_state = 1; break; case 1: if (val == 100) s->mouse_detect_state = 2; else if (val == 200) s->mouse_detect_state = 3; else s->mouse_detect_state = 0; break; case 2: if (val == 80) s->mouse_type = 3; /* IMPS/2 */ s->mouse_detect_state = 0; break; case 3: if (val == 80) s->mouse_type = 4; /* IMEX */ s->mouse_detect_state = 0; break; } ps2_queue(&s->common, AUX_ACK); s->common.write_cmd = -1; break; case AUX_SET_RES: s->mouse_resolution = val; ps2_queue(&s->common, AUX_ACK); s->common.write_cmd = -1; break; } } static void ps2_reset(void *opaque) { PS2State *s = (PS2State *)opaque; PS2Queue *q; s->write_cmd = -1; q = &s->queue; q->rptr = 0; q->wptr = 0; q->count = 0; } static void ps2_common_save (QEMUFile *f, PS2State *s) { qemu_put_be32s (f, &s->write_cmd); qemu_put_be32s (f, &s->queue.rptr); qemu_put_be32s (f, &s->queue.wptr); qemu_put_be32s (f, &s->queue.count); qemu_put_buffer (f, s->queue.data, sizeof (s->queue.data)); } static void ps2_common_load (QEMUFile *f, PS2State *s) { qemu_get_be32s (f, &s->write_cmd); qemu_get_be32s (f, &s->queue.rptr); qemu_get_be32s (f, &s->queue.wptr); qemu_get_be32s (f, &s->queue.count); qemu_get_buffer (f, s->queue.data, sizeof (s->queue.data)); } static void ps2_kbd_save(QEMUFile* f, void* opaque) { PS2KbdState *s = (PS2KbdState*)opaque; ps2_common_save (f, &s->common); qemu_put_be32s(f, &s->scan_enabled); qemu_put_be32s(f, &s->translate); } static void ps2_mouse_save(QEMUFile* f, void* opaque) { PS2MouseState *s = (PS2MouseState*)opaque; ps2_common_save (f, &s->common); qemu_put_8s(f, &s->mouse_status); qemu_put_8s(f, &s->mouse_resolution); qemu_put_8s(f, &s->mouse_sample_rate); qemu_put_8s(f, &s->mouse_wrap); qemu_put_8s(f, &s->mouse_type); qemu_put_8s(f, &s->mouse_detect_state); qemu_put_be32s(f, &s->mouse_dx); qemu_put_be32s(f, &s->mouse_dy); qemu_put_be32s(f, &s->mouse_dz); qemu_put_8s(f, &s->mouse_buttons); } static int ps2_kbd_load(QEMUFile* f, void* opaque, int version_id) { PS2KbdState *s = (PS2KbdState*)opaque; if (version_id != 2) return -EINVAL; ps2_common_load (f, &s->common); qemu_get_be32s(f, &s->scan_enabled); qemu_get_be32s(f, &s->translate); return 0; } static int ps2_mouse_load(QEMUFile* f, void* opaque, int version_id) { PS2MouseState *s = (PS2MouseState*)opaque; if (version_id != 2) return -EINVAL; ps2_common_load (f, &s->common); qemu_get_8s(f, &s->mouse_status); qemu_get_8s(f, &s->mouse_resolution); qemu_get_8s(f, &s->mouse_sample_rate); qemu_get_8s(f, &s->mouse_wrap); qemu_get_8s(f, &s->mouse_type); qemu_get_8s(f, &s->mouse_detect_state); qemu_get_be32s(f, &s->mouse_dx); qemu_get_be32s(f, &s->mouse_dy); qemu_get_be32s(f, &s->mouse_dz); qemu_get_8s(f, &s->mouse_buttons); return 0; } void *ps2_kbd_init(void (*update_irq)(void *, int), void *update_arg) { PS2KbdState *s = (PS2KbdState *)qemu_mallocz(sizeof(PS2KbdState)); s->common.update_irq = update_irq; s->common.update_arg = update_arg; ps2_reset(&s->common); register_savevm("ps2kbd", 0, 2, ps2_kbd_save, ps2_kbd_load, s); qemu_add_kbd_event_handler(ps2_put_keycode, s); qemu_register_reset(ps2_reset, &s->common); return s; } void *ps2_mouse_init(void (*update_irq)(void *, int), void *update_arg) { PS2MouseState *s = (PS2MouseState *)qemu_mallocz(sizeof(PS2MouseState)); s->common.update_irq = update_irq; s->common.update_arg = update_arg; ps2_reset(&s->common); register_savevm("ps2mouse", 0, 2, ps2_mouse_save, ps2_mouse_load, s); qemu_add_mouse_event_handler(ps2_mouse_event, s, 0); qemu_register_reset(ps2_reset, &s->common); return s; }