main.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010 Vojtech Horky
00003  * Copyright (c) 2011 Lubos Slovak
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * - Redistributions of source code must retain the above copyright
00011  *   notice, this list of conditions and the following disclaimer.
00012  * - Redistributions in binary form must reproduce the above copyright
00013  *   notice, this list of conditions and the following disclaimer in the
00014  *   documentation and/or other materials provided with the distribution.
00015  * - The name of the author may not be used to endorse or promote products
00016  *   derived from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00019  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00020  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00021  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00023  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00024  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00025  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00027  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00038 #include <ddf/driver.h>
00039 #include <usb/debug.h>
00040 #include <errno.h>
00041 #include <str_error.h>
00042 
00043 #include <usb/dev/driver.h>
00044 #include <usb/dev/poll.h>
00045 
00046 #include "usbhid.h"
00047 
00048 /*----------------------------------------------------------------------------*/
00049 
00050 #define NAME "usbhid"
00051 
00075 static int usb_hid_try_add_device(usb_device_t *dev)
00076 {
00077         assert(dev != NULL);
00078         
00079         /* 
00080          * Initialize device (get and process descriptors, get address, etc.)
00081          */
00082         usb_log_debug("Initializing USB/HID device...\n");
00083         
00084         usb_hid_dev_t *hid_dev = usb_hid_new();
00085         if (hid_dev == NULL) {
00086                 usb_log_error("Error while creating USB/HID device "
00087                     "structure.\n");
00088                 return ENOMEM;
00089         }
00090         
00091         int rc = usb_hid_init(hid_dev, dev);
00092         
00093         if (rc != EOK) {
00094                 usb_log_error("Failed to initialize USB/HID device.\n");
00095                 usb_hid_free(&hid_dev);
00096                 return rc;
00097         }       
00098         
00099         usb_log_debug("USB/HID device structure initialized.\n");
00100         
00101         /*
00102          * 1) subdriver vytvori vlastnu ddf_fun, vlastne ddf_dev_ops, ktore da
00103          *    do nej.
00104          * 2) do tych ops do .interfaces[DEV_IFACE_USBHID (asi)] priradi 
00105          *    vyplnenu strukturu usbhid_iface_t.
00106          * 3) klientska aplikacia - musi si rucne vytvorit telefon
00107          *    (devman_device_connect() - cesta k zariadeniu (/hw/pci0/...) az 
00108          *    k tej fcii.
00109          *    pouzit usb/classes/hid/iface.h - prvy int je telefon
00110          */
00111         
00112         /* Start automated polling function.
00113          * This will create a separate fibril that will query the device
00114          * for the data continuously 
00115          */
00116        rc = usb_device_auto_poll(dev,
00117            /* Index of the polling pipe. */
00118            hid_dev->poll_pipe_index,
00119            /* Callback when data arrives. */
00120            usb_hid_polling_callback,
00121            /* How much data to request. */
00122            dev->pipes[hid_dev->poll_pipe_index].pipe->max_packet_size,
00123            /* Callback when the polling ends. */
00124            usb_hid_polling_ended_callback,
00125            /* Custom argument. */
00126            hid_dev);
00127         
00128         
00129         if (rc != EOK) {
00130                 usb_log_error("Failed to start polling fibril for `%s'.\n",
00131                     dev->ddf_dev->name);
00132                 return rc;
00133         }
00134 
00135         /*
00136          * Hurrah, device is initialized.
00137          */
00138         return EOK;
00139 }
00140 
00141 /*----------------------------------------------------------------------------*/
00152 static int usb_hid_add_device(usb_device_t *dev)
00153 {
00154         usb_log_debug("usb_hid_add_device()\n");
00155         
00156         if (dev == NULL) {
00157                 usb_log_warning("Wrong parameter given for add_device().\n");
00158                 return EINVAL;
00159         }
00160         
00161         if (dev->interface_no < 0) {
00162                 usb_log_warning("Device is not a supported HID device.\n");
00163                 usb_log_error("Failed to add HID device: endpoints not found."
00164                     "\n");
00165                 return ENOTSUP;
00166         }
00167         
00168         int rc = usb_hid_try_add_device(dev);
00169         
00170         if (rc != EOK) {
00171                 usb_log_warning("Device is not a supported HID device.\n");
00172                 usb_log_error("Failed to add HID device: %s.\n",
00173                     str_error(rc));
00174                 return rc;
00175         }
00176         
00177         usb_log_info("HID device `%s' ready to use.\n", dev->ddf_dev->name);
00178 
00179         return EOK;
00180 }
00181 
00182 /*----------------------------------------------------------------------------*/
00183 
00184 /* Currently, the framework supports only device adding. Once the framework
00185  * supports unplug, more callbacks will be added. */
00186 static usb_driver_ops_t usb_hid_driver_ops = {
00187         .add_device = usb_hid_add_device,
00188 };
00189 
00190 
00191 /* The driver itself. */
00192 static usb_driver_t usb_hid_driver = {
00193         .name = NAME,
00194         .ops = &usb_hid_driver_ops,
00195         .endpoints = usb_hid_endpoints
00196 };
00197 
00198 /*----------------------------------------------------------------------------*/
00199 
00200 int main(int argc, char *argv[])
00201 {
00202         printf(NAME ": HelenOS USB HID driver.\n");
00203 
00204         usb_log_enable(USB_LOG_LEVEL_DEFAULT, NAME);
00205 
00206         return usb_driver_main(&usb_hid_driver);
00207 }
00208 

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