[c081780] | 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 | * Initialization routines for USB mouse driver.
|
---|
| 35 | */
|
---|
| 36 | #include "mouse.h"
|
---|
| 37 | #include <usb/debug.h>
|
---|
[a6567ed] | 38 | #include <usb/classes/classes.h>
|
---|
[c081780] | 39 | #include <usb/classes/hid.h>
|
---|
| 40 | #include <usb/request.h>
|
---|
| 41 | #include <errno.h>
|
---|
| 42 |
|
---|
| 43 | /** Mouse polling endpoint description for boot protocol subclass. */
|
---|
| 44 | static usb_endpoint_description_t poll_endpoint_description = {
|
---|
| 45 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
| 46 | .direction = USB_DIRECTION_IN,
|
---|
| 47 | .interface_class = USB_CLASS_HID,
|
---|
| 48 | .interface_subclass = USB_HID_SUBCLASS_BOOT,
|
---|
| 49 | .interface_protocol = USB_HID_PROTOCOL_MOUSE,
|
---|
| 50 | .flags = 0
|
---|
| 51 | };
|
---|
[a6567ed] | 52 |
|
---|
| 53 | /** Initialize poll pipe.
|
---|
| 54 | *
|
---|
| 55 | * Expects that session is already started on control pipe zero.
|
---|
| 56 | *
|
---|
| 57 | * @param mouse Mouse device.
|
---|
| 58 | * @param my_interface Interface number.
|
---|
| 59 | * @return Error code.
|
---|
| 60 | */
|
---|
| 61 | static int intialize_poll_pipe(usb_mouse_t *mouse, int my_interface)
|
---|
| 62 | {
|
---|
| 63 | assert(usb_endpoint_pipe_is_session_started(&mouse->ctrl_pipe));
|
---|
| 64 |
|
---|
| 65 | int rc;
|
---|
| 66 |
|
---|
| 67 | void *config_descriptor;
|
---|
| 68 | size_t config_descriptor_size;
|
---|
| 69 |
|
---|
| 70 | rc = usb_request_get_full_configuration_descriptor_alloc(
|
---|
| 71 | &mouse->ctrl_pipe, 0, &config_descriptor, &config_descriptor_size);
|
---|
| 72 | if (rc != EOK) {
|
---|
| 73 | return rc;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | usb_endpoint_mapping_t endpoint_mapping[1] = {
|
---|
| 77 | {
|
---|
| 78 | .pipe = &mouse->poll_pipe,
|
---|
| 79 | .description = &poll_endpoint_description,
|
---|
| 80 | .interface_no = my_interface
|
---|
| 81 | }
|
---|
| 82 | };
|
---|
| 83 |
|
---|
| 84 | rc = usb_endpoint_pipe_initialize_from_configuration(endpoint_mapping,
|
---|
| 85 | 1, config_descriptor, config_descriptor_size, &mouse->wire);
|
---|
| 86 | if (rc != EOK) {
|
---|
| 87 | return rc;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | if (!endpoint_mapping[0].present) {
|
---|
| 91 | return ENOENT;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[8d4b844] | 94 | mouse->poll_interval_us = 1000 * endpoint_mapping[0].descriptor->poll_interval;
|
---|
| 95 |
|
---|
| 96 | usb_log_debug("prepared polling endpoint %d (interval %zu).\n",
|
---|
| 97 | mouse->poll_pipe.endpoint_no, mouse->poll_interval_us);
|
---|
| 98 |
|
---|
[a6567ed] | 99 | return EOK;
|
---|
| 100 | }
|
---|
[c081780] | 101 |
|
---|
| 102 |
|
---|
| 103 | int usb_mouse_create(ddf_dev_t *dev)
|
---|
| 104 | {
|
---|
| 105 | usb_mouse_t *mouse = malloc(sizeof(usb_mouse_t));
|
---|
| 106 | if (mouse == NULL) {
|
---|
| 107 | return ENOMEM;
|
---|
| 108 | }
|
---|
| 109 | mouse->device = dev;
|
---|
| 110 |
|
---|
| 111 | int rc;
|
---|
| 112 |
|
---|
| 113 | /* Initialize the backing connection. */
|
---|
| 114 | rc = usb_device_connection_initialize_from_device(&mouse->wire, dev);
|
---|
| 115 | if (rc != EOK) {
|
---|
| 116 | goto leave;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | /* Initialize the default control pipe. */
|
---|
| 120 | rc = usb_endpoint_pipe_initialize_default_control(&mouse->ctrl_pipe,
|
---|
| 121 | &mouse->wire);
|
---|
| 122 | if (rc != EOK) {
|
---|
| 123 | goto leave;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[a6567ed] | 126 | rc = usb_endpoint_pipe_start_session(&mouse->ctrl_pipe);
|
---|
| 127 | if (rc != EOK) {
|
---|
| 128 | goto leave;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | rc = intialize_poll_pipe(mouse, usb_device_get_assigned_interface(dev));
|
---|
| 132 |
|
---|
| 133 | /* We can ignore error here. */
|
---|
| 134 | usb_endpoint_pipe_end_session(&mouse->ctrl_pipe);
|
---|
| 135 |
|
---|
[c081780] | 136 | if (rc != EOK) {
|
---|
| 137 | goto leave;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | /* Create DDF function. */
|
---|
| 141 | mouse->mouse_fun = ddf_fun_create(dev, fun_exposed, "mouse");
|
---|
| 142 | if (mouse->mouse_fun == NULL) {
|
---|
| 143 | rc = ENOMEM;
|
---|
| 144 | goto leave;
|
---|
| 145 | }
|
---|
| 146 | rc = ddf_fun_bind(mouse->mouse_fun);
|
---|
| 147 | if (rc != EOK) {
|
---|
| 148 | goto leave;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[fd5fed8] | 151 | /* Add the function to mouse class. */
|
---|
| 152 | rc = ddf_fun_add_to_class(mouse->mouse_fun, "mouse");
|
---|
| 153 | if (rc != EOK) {
|
---|
| 154 | goto leave;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[c081780] | 157 | /* Everything allright. */
|
---|
| 158 | dev->driver_data = mouse;
|
---|
| 159 |
|
---|
| 160 | return EOK;
|
---|
| 161 |
|
---|
| 162 | leave:
|
---|
| 163 | free(mouse);
|
---|
| 164 |
|
---|
| 165 | return rc;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /**
|
---|
| 169 | * @}
|
---|
| 170 | */
|
---|