| 1 | /*
|
|---|
| 2 | * Copyright (c) 2011 Vojtech Horky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup drvusbmouse
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * Actual handling of USB mouse protocol.
|
|---|
| 35 | */
|
|---|
| 36 | #include "mouse.h"
|
|---|
| 37 | #include <usb/debug.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <str_error.h>
|
|---|
| 40 | #include <ipc/mouse.h>
|
|---|
| 41 |
|
|---|
| 42 | /** Fibril function for polling the mouse device.
|
|---|
| 43 | *
|
|---|
| 44 | * This function shall not terminate unless the device breaks and fails
|
|---|
| 45 | * to send data (e.g. stalls on data request).
|
|---|
| 46 | *
|
|---|
| 47 | * @param arg ddf_dev_t type representing the mouse device.
|
|---|
| 48 | * @return EOK Always.
|
|---|
| 49 | */
|
|---|
| 50 | int usb_mouse_polling_fibril(void *arg)
|
|---|
| 51 | {
|
|---|
| 52 | assert(arg != NULL);
|
|---|
| 53 | ddf_dev_t *dev = (ddf_dev_t *) arg;
|
|---|
| 54 | usb_mouse_t *mouse = (usb_mouse_t *) dev->driver_data;
|
|---|
| 55 |
|
|---|
| 56 | assert(mouse);
|
|---|
| 57 |
|
|---|
| 58 | size_t buffer_size = mouse->poll_pipe.max_packet_size;
|
|---|
| 59 |
|
|---|
| 60 | if (buffer_size < 4) {
|
|---|
| 61 | usb_log_error("Weird mouse, results will be skewed.\n");
|
|---|
| 62 | buffer_size = 4;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | uint8_t *buffer = malloc(buffer_size);
|
|---|
| 66 | if (buffer == NULL) {
|
|---|
| 67 | usb_log_error("Out of memory, poll fibril aborted.\n");
|
|---|
| 68 | return ENOMEM;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | while (true) {
|
|---|
| 72 | async_usleep(mouse->poll_interval_us);
|
|---|
| 73 |
|
|---|
| 74 | size_t actual_size;
|
|---|
| 75 | int rc;
|
|---|
| 76 |
|
|---|
| 77 | /*
|
|---|
| 78 | * Error checking note:
|
|---|
| 79 | * - failure when starting a session is considered
|
|---|
| 80 | * temporary (e.g. out of phones, next try might succeed)
|
|---|
| 81 | * - failure of transfer considered fatal (probably the
|
|---|
| 82 | * device was unplugged)
|
|---|
| 83 | * - session closing not checked (shall not fail anyway)
|
|---|
| 84 | */
|
|---|
| 85 |
|
|---|
| 86 | rc = usb_endpoint_pipe_start_session(&mouse->poll_pipe);
|
|---|
| 87 | if (rc != EOK) {
|
|---|
| 88 | usb_log_warning("Failed to start session, will try again: %s.\n",
|
|---|
| 89 | str_error(rc));
|
|---|
| 90 | continue;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | rc = usb_endpoint_pipe_read(&mouse->poll_pipe,
|
|---|
| 94 | buffer, buffer_size, &actual_size);
|
|---|
| 95 |
|
|---|
| 96 | usb_endpoint_pipe_end_session(&mouse->poll_pipe);
|
|---|
| 97 |
|
|---|
| 98 | if (rc != EOK) {
|
|---|
| 99 | usb_log_error("Failed reading mouse input: %s.\n",
|
|---|
| 100 | str_error(rc));
|
|---|
| 101 | break;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | usb_log_debug2("got buffer: %s.\n",
|
|---|
| 105 | usb_debug_str_buffer(buffer, buffer_size, 0));
|
|---|
| 106 |
|
|---|
| 107 | uint8_t butt = buffer[0];
|
|---|
| 108 | char str_buttons[4] = {
|
|---|
| 109 | butt & 1 ? '#' : '.',
|
|---|
| 110 | butt & 2 ? '#' : '.',
|
|---|
| 111 | butt & 4 ? '#' : '.',
|
|---|
| 112 | 0
|
|---|
| 113 | };
|
|---|
| 114 |
|
|---|
| 115 | int shift_x = ((int) buffer[1]) - 127;
|
|---|
| 116 | int shift_y = ((int) buffer[2]) - 127;
|
|---|
| 117 | int wheel = ((int) buffer[3]) - 127;
|
|---|
| 118 |
|
|---|
| 119 | if (buffer[1] == 0) {
|
|---|
| 120 | shift_x = 0;
|
|---|
| 121 | }
|
|---|
| 122 | if (buffer[2] == 0) {
|
|---|
| 123 | shift_y = 0;
|
|---|
| 124 | }
|
|---|
| 125 | if (buffer[3] == 0) {
|
|---|
| 126 | wheel = 0;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | if (mouse->console_phone >= 0) {
|
|---|
| 130 | if ((shift_x != 0) || (shift_y != 0)) {
|
|---|
| 131 | /* FIXME: guessed for QEMU */
|
|---|
| 132 | async_req_2_0(mouse->console_phone,
|
|---|
| 133 | MEVENT_MOVE,
|
|---|
| 134 | - shift_x / 10, - shift_y / 10);
|
|---|
| 135 | }
|
|---|
| 136 | if (butt) {
|
|---|
| 137 | /* FIXME: proper button clicking. */
|
|---|
| 138 | async_req_2_0(mouse->console_phone,
|
|---|
| 139 | MEVENT_BUTTON, 1, 1);
|
|---|
| 140 | async_req_2_0(mouse->console_phone,
|
|---|
| 141 | MEVENT_BUTTON, 1, 0);
|
|---|
| 142 | }
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | usb_log_debug("buttons=%s dX=%+3d dY=%+3d wheel=%+3d\n",
|
|---|
| 146 | str_buttons, shift_x, shift_y, wheel);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /*
|
|---|
| 150 | * Device was probably unplugged.
|
|---|
| 151 | * Hang-up the phone to the console.
|
|---|
| 152 | * FIXME: release allocated memory.
|
|---|
| 153 | */
|
|---|
| 154 | async_hangup(mouse->console_phone);
|
|---|
| 155 | mouse->console_phone = -1;
|
|---|
| 156 |
|
|---|
| 157 | usb_log_error("Mouse polling fibril terminated.\n");
|
|---|
| 158 |
|
|---|
| 159 | return EOK;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 | /**
|
|---|
| 164 | * @}
|
|---|
| 165 | */
|
|---|