00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
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
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 rc = usb_device_auto_poll(dev,
00117
00118 hid_dev->poll_pipe_index,
00119
00120 usb_hid_polling_callback,
00121
00122 dev->pipes[hid_dev->poll_pipe_index].pipe->max_packet_size,
00123
00124 usb_hid_polling_ended_callback,
00125
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
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
00185
00186 static usb_driver_ops_t usb_hid_driver_ops = {
00187 .add_device = usb_hid_add_device,
00188 };
00189
00190
00191
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