init.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011 Vojtech Horky
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00036 #include "mouse.h"
00037 #include <usb/debug.h>
00038 #include <usb/classes/classes.h>
00039 #include <usb/hid/hid.h>
00040 #include <usb/dev/request.h>
00041 #include <usb/hid/request.h>
00042 #include <errno.h>
00043 
00045 usb_endpoint_description_t poll_endpoint_description = {
00046         .transfer_type = USB_TRANSFER_INTERRUPT,
00047         .direction = USB_DIRECTION_IN,
00048         .interface_class = USB_CLASS_HID,
00049         .interface_subclass = USB_HID_SUBCLASS_BOOT,
00050         .interface_protocol = USB_HID_PROTOCOL_MOUSE,
00051         .flags = 0
00052 };
00053 
00054 static void default_connection_handler(ddf_fun_t *, ipc_callid_t, ipc_call_t *);
00056 static ddf_dev_ops_t mouse_ops = {
00057         .default_handler = default_connection_handler
00058 };
00059 
00066 void default_connection_handler(ddf_fun_t *fun,
00067     ipc_callid_t icallid, ipc_call_t *icall)
00068 {
00069         sysarg_t method = IPC_GET_IMETHOD(*icall);
00070 
00071         usb_mouse_t *mouse = (usb_mouse_t *) fun->driver_data;
00072         assert(mouse != NULL);
00073 
00074         if (method == IPC_M_CONNECT_TO_ME) {
00075                 int callback = IPC_GET_ARG5(*icall);
00076 
00077                 if (mouse->console_phone != -1) {
00078                         async_answer_0(icallid, ELIMIT);
00079                         return;
00080                 }
00081 
00082                 mouse->console_phone = callback;
00083                 async_answer_0(icallid, EOK);
00084                 return;
00085         }
00086 
00087         async_answer_0(icallid, EINVAL);
00088 }
00089 
00097 int usb_mouse_create(usb_device_t *dev)
00098 {
00099         usb_mouse_t *mouse = malloc(sizeof(usb_mouse_t));
00100         if (mouse == NULL) {
00101                 return ENOMEM;
00102         }
00103         mouse->dev = dev;
00104         mouse->console_phone = -1;
00105 
00106         int rc;
00107 
00108         /* Create DDF function. */
00109         mouse->mouse_fun = ddf_fun_create(dev->ddf_dev, fun_exposed, "mouse");
00110         if (mouse->mouse_fun == NULL) {
00111                 rc = ENOMEM;
00112                 goto leave;
00113         }
00114 
00115         mouse->mouse_fun->ops = &mouse_ops;
00116 
00117         rc = ddf_fun_bind(mouse->mouse_fun);
00118         if (rc != EOK) {
00119                 goto leave;
00120         }
00121 
00122         /* Add the function to mouse class. */
00123         rc = ddf_fun_add_to_class(mouse->mouse_fun, "mouse");
00124         if (rc != EOK) {
00125                 goto leave;
00126         }
00127         
00128         /* Set the boot protocol. */
00129         rc = usbhid_req_set_protocol(&dev->ctrl_pipe, dev->interface_no,
00130             USB_HID_PROTOCOL_BOOT);
00131         if (rc != EOK) {
00132                 goto leave;
00133         }
00134         
00135         /* Everything all right. */
00136         dev->driver_data = mouse;
00137         mouse->mouse_fun->driver_data = mouse;
00138 
00139         return EOK;
00140 
00141 leave:
00142         free(mouse);
00143 
00144         return rc;
00145 }
00146 

Generated on Thu Jun 2 07:45:41 2011 for HelenOS/USB by  doxygen 1.4.7