[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 | */
|
---|
[79ae36dd] | 36 |
|
---|
[c081780] | 37 | #include "mouse.h"
|
---|
| 38 | #include <usb/debug.h>
|
---|
[a6567ed] | 39 | #include <usb/classes/classes.h>
|
---|
[faa44e58] | 40 | #include <usb/hid/hid.h>
|
---|
[7d521e24] | 41 | #include <usb/dev/request.h>
|
---|
[b59ec8c] | 42 | #include <usb/hid/request.h>
|
---|
[c081780] | 43 | #include <errno.h>
|
---|
| 44 |
|
---|
| 45 | /** Mouse polling endpoint description for boot protocol subclass. */
|
---|
[489c3e7] | 46 | usb_endpoint_description_t poll_endpoint_description = {
|
---|
[c081780] | 47 | .transfer_type = USB_TRANSFER_INTERRUPT,
|
---|
| 48 | .direction = USB_DIRECTION_IN,
|
---|
| 49 | .interface_class = USB_CLASS_HID,
|
---|
| 50 | .interface_subclass = USB_HID_SUBCLASS_BOOT,
|
---|
| 51 | .interface_protocol = USB_HID_PROTOCOL_MOUSE,
|
---|
| 52 | .flags = 0
|
---|
| 53 | };
|
---|
[a6567ed] | 54 |
|
---|
[21bb58d] | 55 | /** Default handler for IPC methods not handled by DDF.
|
---|
| 56 | *
|
---|
[4a4c8bcf] | 57 | * @param fun Device function handling the call.
|
---|
| 58 | * @param icallid Call ID.
|
---|
| 59 | * @param icall Call data.
|
---|
| 60 | *
|
---|
[21bb58d] | 61 | */
|
---|
[4a4c8bcf] | 62 | static void default_connection_handler(ddf_fun_t *fun, ipc_callid_t icallid,
|
---|
| 63 | ipc_call_t *icall)
|
---|
[21bb58d] | 64 | {
|
---|
| 65 | usb_mouse_t *mouse = (usb_mouse_t *) fun->driver_data;
|
---|
| 66 | assert(mouse != NULL);
|
---|
[4a4c8bcf] | 67 |
|
---|
| 68 | async_sess_t *callback =
|
---|
| 69 | async_callback_receive_start(EXCHANGE_SERIALIZE, icall);
|
---|
| 70 |
|
---|
| 71 | if (callback) {
|
---|
| 72 | if (mouse->console_sess == NULL) {
|
---|
| 73 | mouse->console_sess = callback;
|
---|
| 74 | async_answer_0(icallid, EOK);
|
---|
| 75 | } else
|
---|
[21bb58d] | 76 | async_answer_0(icallid, ELIMIT);
|
---|
[4a4c8bcf] | 77 | } else
|
---|
| 78 | async_answer_0(icallid, EINVAL);
|
---|
[21bb58d] | 79 | }
|
---|
| 80 |
|
---|
[4a4c8bcf] | 81 | /** Device ops for USB mouse. */
|
---|
| 82 | static ddf_dev_ops_t mouse_ops = {
|
---|
| 83 | .default_handler = default_connection_handler
|
---|
| 84 | };
|
---|
| 85 |
|
---|
[a6add7a] | 86 | /** Create USB mouse device.
|
---|
| 87 | *
|
---|
| 88 | * The mouse device is stored into <code>dev->driver_data</code>.
|
---|
| 89 | *
|
---|
| 90 | * @param dev Generic device.
|
---|
| 91 | * @return Error code.
|
---|
| 92 | */
|
---|
[489c3e7] | 93 | int usb_mouse_create(usb_device_t *dev)
|
---|
[c081780] | 94 | {
|
---|
| 95 | usb_mouse_t *mouse = malloc(sizeof(usb_mouse_t));
|
---|
[4a4c8bcf] | 96 | if (mouse == NULL)
|
---|
[c081780] | 97 | return ENOMEM;
|
---|
[4a4c8bcf] | 98 |
|
---|
[489c3e7] | 99 | mouse->dev = dev;
|
---|
[4a4c8bcf] | 100 | mouse->console_sess = NULL;
|
---|
| 101 |
|
---|
[c081780] | 102 | int rc;
|
---|
[4a4c8bcf] | 103 |
|
---|
[c081780] | 104 | /* Create DDF function. */
|
---|
[489c3e7] | 105 | mouse->mouse_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "mouse");
|
---|
[c081780] | 106 | if (mouse->mouse_fun == NULL) {
|
---|
| 107 | rc = ENOMEM;
|
---|
| 108 | goto leave;
|
---|
| 109 | }
|
---|
[4a4c8bcf] | 110 |
|
---|
[21bb58d] | 111 | mouse->mouse_fun->ops = &mouse_ops;
|
---|
[4a4c8bcf] | 112 |
|
---|
[c081780] | 113 | rc = ddf_fun_bind(mouse->mouse_fun);
|
---|
[4a4c8bcf] | 114 | if (rc != EOK)
|
---|
[c081780] | 115 | goto leave;
|
---|
[4a4c8bcf] | 116 |
|
---|
[fd5fed8] | 117 | /* Add the function to mouse class. */
|
---|
| 118 | rc = ddf_fun_add_to_class(mouse->mouse_fun, "mouse");
|
---|
[4a4c8bcf] | 119 | if (rc != EOK)
|
---|
[fd5fed8] | 120 | goto leave;
|
---|
[054ed84] | 121 |
|
---|
| 122 | /* Set the boot protocol. */
|
---|
[b59ec8c] | 123 | rc = usbhid_req_set_protocol(&dev->ctrl_pipe, dev->interface_no,
|
---|
| 124 | USB_HID_PROTOCOL_BOOT);
|
---|
[4a4c8bcf] | 125 | if (rc != EOK)
|
---|
[054ed84] | 126 | goto leave;
|
---|
| 127 |
|
---|
[4a4c8bcf] | 128 | /* Everything allright. */
|
---|
[c081780] | 129 | dev->driver_data = mouse;
|
---|
[21bb58d] | 130 | mouse->mouse_fun->driver_data = mouse;
|
---|
[4a4c8bcf] | 131 |
|
---|
[c081780] | 132 | return EOK;
|
---|
[4a4c8bcf] | 133 |
|
---|
[c081780] | 134 | leave:
|
---|
| 135 | free(mouse);
|
---|
| 136 | return rc;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | /**
|
---|
| 140 | * @}
|
---|
| 141 | */
|
---|