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 | 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 | };
|
---|
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 |
|
---|
94 | return EOK;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | int usb_mouse_create(ddf_dev_t *dev)
|
---|
99 | {
|
---|
100 | usb_mouse_t *mouse = malloc(sizeof(usb_mouse_t));
|
---|
101 | if (mouse == NULL) {
|
---|
102 | return ENOMEM;
|
---|
103 | }
|
---|
104 | mouse->device = dev;
|
---|
105 |
|
---|
106 | int rc;
|
---|
107 |
|
---|
108 | /* Initialize the backing connection. */
|
---|
109 | rc = usb_device_connection_initialize_from_device(&mouse->wire, dev);
|
---|
110 | if (rc != EOK) {
|
---|
111 | goto leave;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /* Initialize the default control pipe. */
|
---|
115 | rc = usb_endpoint_pipe_initialize_default_control(&mouse->ctrl_pipe,
|
---|
116 | &mouse->wire);
|
---|
117 | if (rc != EOK) {
|
---|
118 | goto leave;
|
---|
119 | }
|
---|
120 |
|
---|
121 | rc = usb_endpoint_pipe_start_session(&mouse->ctrl_pipe);
|
---|
122 | if (rc != EOK) {
|
---|
123 | goto leave;
|
---|
124 | }
|
---|
125 |
|
---|
126 | rc = intialize_poll_pipe(mouse, usb_device_get_assigned_interface(dev));
|
---|
127 |
|
---|
128 | /* We can ignore error here. */
|
---|
129 | usb_endpoint_pipe_end_session(&mouse->ctrl_pipe);
|
---|
130 |
|
---|
131 | if (rc != EOK) {
|
---|
132 | goto leave;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /* Create DDF function. */
|
---|
136 | mouse->mouse_fun = ddf_fun_create(dev, fun_exposed, "mouse");
|
---|
137 | if (mouse->mouse_fun == NULL) {
|
---|
138 | rc = ENOMEM;
|
---|
139 | goto leave;
|
---|
140 | }
|
---|
141 | rc = ddf_fun_bind(mouse->mouse_fun);
|
---|
142 | if (rc != EOK) {
|
---|
143 | goto leave;
|
---|
144 | }
|
---|
145 |
|
---|
146 | /* Everything allright. */
|
---|
147 | dev->driver_data = mouse;
|
---|
148 |
|
---|
149 | return EOK;
|
---|
150 |
|
---|
151 | leave:
|
---|
152 | free(mouse);
|
---|
153 |
|
---|
154 | return rc;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * @}
|
---|
159 | */
|
---|