| 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> | 
|---|
| 38 | #include <usb/classes/classes.h> | 
|---|
| 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 | 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 | }; | 
|---|
| 52 |  | 
|---|
| 53 | static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *); | 
|---|
| 54 | /** Device ops for USB mouse. */ | 
|---|
| 55 | static ddf_dev_ops_t mouse_ops = { | 
|---|
| 56 | .default_handler = default_connection_handler | 
|---|
| 57 | }; | 
|---|
| 58 |  | 
|---|
| 59 | /** Default handler for IPC methods not handled by DDF. | 
|---|
| 60 | * | 
|---|
| 61 | * @param fun Device function handling the call. | 
|---|
| 62 | * @param icallid Call id. | 
|---|
| 63 | * @param icall Call data. | 
|---|
| 64 | */ | 
|---|
| 65 | void default_connection_handler(ddf_fun_t *fun, | 
|---|
| 66 | ipc_callid_t icallid, ipc_call_t *icall) | 
|---|
| 67 | { | 
|---|
| 68 | sysarg_t method = IPC_GET_IMETHOD(*icall); | 
|---|
| 69 |  | 
|---|
| 70 | usb_mouse_t *mouse = (usb_mouse_t *) fun->driver_data; | 
|---|
| 71 | assert(mouse != NULL); | 
|---|
| 72 |  | 
|---|
| 73 | if (method == IPC_M_CONNECT_TO_ME) { | 
|---|
| 74 | int callback = IPC_GET_ARG5(*icall); | 
|---|
| 75 |  | 
|---|
| 76 | if (mouse->console_phone != -1) { | 
|---|
| 77 | async_answer_0(icallid, ELIMIT); | 
|---|
| 78 | return; | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 | mouse->console_phone = callback; | 
|---|
| 82 | async_answer_0(icallid, EOK); | 
|---|
| 83 | return; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | async_answer_0(icallid, EINVAL); | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | /** Create USB mouse device. | 
|---|
| 90 | * | 
|---|
| 91 | * The mouse device is stored into <code>dev->driver_data</code>. | 
|---|
| 92 | * | 
|---|
| 93 | * @param dev Generic device. | 
|---|
| 94 | * @return Error code. | 
|---|
| 95 | */ | 
|---|
| 96 | int usb_mouse_create(usb_device_t *dev) | 
|---|
| 97 | { | 
|---|
| 98 | usb_mouse_t *mouse = malloc(sizeof(usb_mouse_t)); | 
|---|
| 99 | if (mouse == NULL) { | 
|---|
| 100 | return ENOMEM; | 
|---|
| 101 | } | 
|---|
| 102 | mouse->dev = dev; | 
|---|
| 103 | mouse->console_phone = -1; | 
|---|
| 104 |  | 
|---|
| 105 | int rc; | 
|---|
| 106 |  | 
|---|
| 107 | /* Create DDF function. */ | 
|---|
| 108 | mouse->mouse_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "mouse"); | 
|---|
| 109 | if (mouse->mouse_fun == NULL) { | 
|---|
| 110 | rc = ENOMEM; | 
|---|
| 111 | goto leave; | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | mouse->mouse_fun->ops = &mouse_ops; | 
|---|
| 115 |  | 
|---|
| 116 | rc = ddf_fun_bind(mouse->mouse_fun); | 
|---|
| 117 | if (rc != EOK) { | 
|---|
| 118 | goto leave; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | /* Add the function to mouse class. */ | 
|---|
| 122 | rc = ddf_fun_add_to_class(mouse->mouse_fun, "mouse"); | 
|---|
| 123 | if (rc != EOK) { | 
|---|
| 124 | goto leave; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | /* Open the control pipe. */ | 
|---|
| 128 | rc = usb_pipe_start_session(&dev->ctrl_pipe); | 
|---|
| 129 | if (rc != EOK) { | 
|---|
| 130 | goto leave; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | /* Set the boot protocol. */ | 
|---|
| 134 | rc = usb_control_request_set(&dev->ctrl_pipe, | 
|---|
| 135 | USB_REQUEST_TYPE_CLASS, USB_REQUEST_RECIPIENT_INTERFACE, | 
|---|
| 136 | USB_HIDREQ_SET_PROTOCOL, USB_HID_PROTOCOL_BOOT, dev->interface_no, | 
|---|
| 137 | NULL, 0); | 
|---|
| 138 | if (rc != EOK) { | 
|---|
| 139 | goto leave; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 142 | /* Close the control pipe (ignore errors). */ | 
|---|
| 143 | usb_pipe_end_session(&dev->ctrl_pipe); | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 | /* Everything allright. */ | 
|---|
| 147 | dev->driver_data = mouse; | 
|---|
| 148 | mouse->mouse_fun->driver_data = mouse; | 
|---|
| 149 |  | 
|---|
| 150 | return EOK; | 
|---|
| 151 |  | 
|---|
| 152 | leave: | 
|---|
| 153 | free(mouse); | 
|---|
| 154 |  | 
|---|
| 155 | return rc; | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | /** | 
|---|
| 159 | * @} | 
|---|
| 160 | */ | 
|---|