source: mainline/uspace/drv/usbmouse/init.c@ 399a13c

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 399a13c was b59ec8c, checked in by Vojtech Horky <vojtechhorky@…>, 14 years ago

usbmouse: use library function

  • Property mode set to 100644
File size: 4.0 KB
Line 
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/hid/hid.h>
40#include <usb/dev/request.h>
41#include <usb/hid/request.h>
42#include <errno.h>
43
44/** Mouse polling endpoint description for boot protocol subclass. */
45usb_endpoint_description_t poll_endpoint_description = {
46 .transfer_type = USB_TRANSFER_INTERRUPT,
47 .direction = USB_DIRECTION_IN,
48 .interface_class = USB_CLASS_HID,
49 .interface_subclass = USB_HID_SUBCLASS_BOOT,
50 .interface_protocol = USB_HID_PROTOCOL_MOUSE,
51 .flags = 0
52};
53
54static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
55/** Device ops for USB mouse. */
56static ddf_dev_ops_t mouse_ops = {
57 .default_handler = default_connection_handler
58};
59
60/** Default handler for IPC methods not handled by DDF.
61 *
62 * @param fun Device function handling the call.
63 * @param icallid Call id.
64 * @param icall Call data.
65 */
66void default_connection_handler(ddf_fun_t *fun,
67 ipc_callid_t icallid, ipc_call_t *icall)
68{
69 sysarg_t method = IPC_GET_IMETHOD(*icall);
70
71 usb_mouse_t *mouse = (usb_mouse_t *) fun->driver_data;
72 assert(mouse != NULL);
73
74 if (method == IPC_M_CONNECT_TO_ME) {
75 int callback = IPC_GET_ARG5(*icall);
76
77 if (mouse->console_phone != -1) {
78 async_answer_0(icallid, ELIMIT);
79 return;
80 }
81
82 mouse->console_phone = callback;
83 async_answer_0(icallid, EOK);
84 return;
85 }
86
87 async_answer_0(icallid, EINVAL);
88}
89
90/** Create USB mouse device.
91 *
92 * The mouse device is stored into <code>dev-&gt;driver_data</code>.
93 *
94 * @param dev Generic device.
95 * @return Error code.
96 */
97int usb_mouse_create(usb_device_t *dev)
98{
99 usb_mouse_t *mouse = malloc(sizeof(usb_mouse_t));
100 if (mouse == NULL) {
101 return ENOMEM;
102 }
103 mouse->dev = dev;
104 mouse->console_phone = -1;
105
106 int rc;
107
108 /* Create DDF function. */
109 mouse->mouse_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "mouse");
110 if (mouse->mouse_fun == NULL) {
111 rc = ENOMEM;
112 goto leave;
113 }
114
115 mouse->mouse_fun->ops = &mouse_ops;
116
117 rc = ddf_fun_bind(mouse->mouse_fun);
118 if (rc != EOK) {
119 goto leave;
120 }
121
122 /* Add the function to mouse class. */
123 rc = ddf_fun_add_to_class(mouse->mouse_fun, "mouse");
124 if (rc != EOK) {
125 goto leave;
126 }
127
128 /* Set the boot protocol. */
129 rc = usbhid_req_set_protocol(&dev->ctrl_pipe, dev->interface_no,
130 USB_HID_PROTOCOL_BOOT);
131 if (rc != EOK) {
132 goto leave;
133 }
134
135 /* Everything all right. */
136 dev->driver_data = mouse;
137 mouse->mouse_fun->driver_data = mouse;
138
139 return EOK;
140
141leave:
142 free(mouse);
143
144 return rc;
145}
146
147/**
148 * @}
149 */
Note: See TracBrowser for help on using the repository browser.