Changeset 252e30c in mainline


Ignore:
Timestamp:
2011-03-24T22:55:29Z (14 years ago)
Author:
Lubos Slovak <lubos.slovak@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0d92638
Parents:
476b71ff
Message:

Huge refactoring of KBD driver code.

  • Separated code for retrieval of Report descriptor into hidreport.h/c (will be common for all HID drivers).
  • Removed unused files hiddev.h/c.
  • Changed prefix of usbkbd functions to usb_kbd_*.
  • Removed unused code.
  • Parsing of Report descriptor with fallback to boot protocol in KBD driver (not finished (boot report descriptor missing).
Location:
uspace
Files:
2 added
2 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/usbkbd/conv.h

    r476b71ff r252e30c  
    3434 */
    3535
    36 #ifndef USBHID_CONV_H_
    37 #define USBHID_CONV_H_
     36#ifndef USB_KBD_CONV_H_
     37#define USB_KBD_CONV_H_
    3838
    3939unsigned int usbhid_parse_scancode(int scancode);
    4040
    41 #endif /* USBHID_CONV_H_ */
     41#endif /* USB_KBD_CONV_H_ */
    4242
    4343/**
  • uspace/drv/usbkbd/kbddev.c

    r476b71ff r252e30c  
    5555#include <usb/classes/hidut.h>
    5656#include <usb/classes/hidreq.h>
     57#include <usb/classes/hidreport.h>
    5758
    5859#include <usb/devdrv.h>
     
    8889/** Delay between two repeats of a pressed key when auto-repeating. */
    8990static const unsigned int DEFAULT_REPEAT_DELAY = 50 * 1000;
     91
     92/*----------------------------------------------------------------------------*/
    9093
    9194/** Keyboard polling endpoint description for boot protocol class. */
     
    99102};
    100103
    101 /** General keyboard polling endpoint description. */
    102 //static usb_endpoint_description_t general_poll_endpoint_description = {
    103 //      .transfer_type = USB_TRANSFER_INTERRUPT,
    104 //      .direction = USB_DIRECTION_IN,
    105 //      .interface_class = USB_CLASS_HID,
    106 //      .flags = 0
    107 //};
    108 
    109104/* Array of endpoints expected on the device, NULL terminated. */
    110105usb_endpoint_description_t
    111     *usbhid_kbd_endpoints[USBHID_KBD_POLL_EP_COUNT + 1] = {
     106    *usb_kbd_endpoints[USB_KBD_POLL_EP_COUNT + 1] = {
    112107        &boot_poll_endpoint_description,
    113 //      &general_poll_endpoint_description,
    114108        NULL
    115109};
    116110
    117 
    118 typedef enum usbhid_kbd_flags {
    119         USBHID_KBD_STATUS_UNINITIALIZED = 0,
    120         USBHID_KBD_STATUS_INITIALIZED = 1,
    121         USBHID_KBD_STATUS_TO_DESTROY = -1
    122 } usbhid_kbd_flags;
     111/*----------------------------------------------------------------------------*/
     112
     113enum {
     114        BOOT_REPORT_DESCRIPTOR_SIZE = 0
     115};
     116
     117static const uint8_t BOOT_REPORT_DESCRIPTOR[BOOT_REPORT_DESCRIPTOR_SIZE] = {};
     118
     119/*----------------------------------------------------------------------------*/
     120
     121typedef enum usb_kbd_flags {
     122        USB_KBD_STATUS_UNINITIALIZED = 0,
     123        USB_KBD_STATUS_INITIALIZED = 1,
     124        USB_KBD_STATUS_TO_DESTROY = -1
     125} usb_kbd_flags;
    123126
    124127/*----------------------------------------------------------------------------*/
     
    153156
    154157typedef enum usbhid_lock_code {
    155         USBHID_LOCK_NUM = 0x53,
    156         USBHID_LOCK_CAPS = 0x39,
    157         USBHID_LOCK_SCROLL = 0x47,
    158         USBHID_LOCK_COUNT = 3
     158        USB_KBD_LOCK_NUM = 0x53,
     159        USB_KBD_LOCK_CAPS = 0x39,
     160        USB_KBD_LOCK_SCROLL = 0x47,
     161        USB_KBD_LOCK_COUNT = 3
    159162} usbhid_lock_code;
    160163
    161 static const usbhid_lock_code usbhid_lock_codes[USBHID_LOCK_COUNT] = {
    162         USBHID_LOCK_NUM,
    163         USBHID_LOCK_CAPS,
    164         USBHID_LOCK_SCROLL
     164static const usbhid_lock_code usbhid_lock_codes[USB_KBD_LOCK_COUNT] = {
     165        USB_KBD_LOCK_NUM,
     166        USB_KBD_LOCK_CAPS,
     167        USB_KBD_LOCK_SCROLL
    165168};
    166169
     
    190193        sysarg_t method = IPC_GET_IMETHOD(*icall);
    191194       
    192         usbhid_kbd_t *kbd_dev = (usbhid_kbd_t *)fun->driver_data;
     195        usb_kbd_t *kbd_dev = (usb_kbd_t *)fun->driver_data;
    193196        assert(kbd_dev != NULL);
    194197
     
    224227 * @param kbd_dev Keyboard device structure.
    225228 */
    226 static void usbhid_kbd_set_led(usbhid_kbd_t *kbd_dev)
     229static void usb_kbd_set_led(usb_kbd_t *kbd_dev)
    227230{
    228231        uint8_t buffer[BOOTP_BUFFER_OUT_SIZE];
     
    282285 * @param key Key code of the key according to HID Usage Tables.
    283286 */
    284 void usbhid_kbd_push_ev(usbhid_kbd_t *kbd_dev, int type, unsigned int key)
     287void usb_kbd_push_ev(usb_kbd_t *kbd_dev, int type, unsigned int key)
    285288{
    286289        console_event_t ev;
     
    332335                        /* Update keyboard lock indicator lights. */
    333336                        if (kbd_dev->lock_keys != locks_old) {
    334                                 usbhid_kbd_set_led(kbd_dev);
     337                                usb_kbd_set_led(kbd_dev);
    335338                        }
    336339                } else {
     
    380383
    381384/*----------------------------------------------------------------------------*/
    382 /**
    383  * Checks if modifiers were pressed or released and generates key events.
    384  *
    385  * @param kbd_dev Keyboard device structure.
    386  * @param modifiers Bitmap of modifiers.
    387  *
    388  * @sa usbhid_kbd_push_ev()
    389  */
    390 //static void usbhid_kbd_check_modifier_changes(usbhid_kbd_t *kbd_dev,
    391 //    const uint8_t *key_codes, size_t count)
    392 //{
    393 //      /*
    394 //       *  why the USB keyboard has NUM_, SCROLL_ and CAPS_LOCK
    395 //       *       both as modifiers and as keyUSB_HID_LOCK_COUNTs with their own scancodes???
    396 //       *
    397 //       * modifiers should be sent as normal keys to usbhid_parse_scancode()!!
    398 //       * so maybe it would be better if I received it from report parser in
    399 //       * that way
    400 //       */
    401        
    402 //      int i;
    403 //      for (i = 0; i < count; ++i) {
    404 //              if ((modifiers & usb_hid_modifiers_consts[i]) &&
    405 //                  !(kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
    406 //                      // modifier pressed
    407 //                      if (usbhid_modifiers_keycodes[i] != 0) {
    408 //                              usbhid_kbd_push_ev(kbd_dev, KEY_PRESS,
    409 //                                  usbhid_modifiers_keycodes[i]);
    410 //                      }
    411 //              } else if (!(modifiers & usb_hid_modifiers_consts[i]) &&
    412 //                  (kbd_dev->modifiers & usb_hid_modifiers_consts[i])) {
    413 //                      // modifier released
    414 //                      if (usbhid_modifiers_keycodes[i] != 0) {
    415 //                              usbhid_kbd_push_ev(kbd_dev, KEY_RELEASE,
    416 //                                  usbhid_modifiers_keycodes[i]);
    417 //                      }
    418 //              }       // no change
    419 //      }
    420        
    421 //      kbd_dev->modifiers = modifiers;
    422 //}
    423 
    424 /*----------------------------------------------------------------------------*/
    425 
    426 static inline int usbhid_kbd_is_lock(unsigned int key_code)
     385
     386static inline int usb_kbd_is_lock(unsigned int key_code)
    427387{
    428388        return (key_code == KC_NUM_LOCK
     
    436396 *
    437397 * An event is created only when key is pressed or released. Besides handling
    438  * the events (usbhid_kbd_push_ev()), the auto-repeat fibril is notified about
    439  * key presses and releases (see usbhid_kbd_repeat_start() and
    440  * usbhid_kbd_repeat_stop()).
     398 * the events (usb_kbd_push_ev()), the auto-repeat fibril is notified about
     399 * key presses and releases (see usb_kbd_repeat_start() and
     400 * usb_kbd_repeat_stop()).
    441401 *
    442402 * @param kbd_dev Keyboard device structure.
     
    445405 * @param count Number of key codes in report (size of the report).
    446406 *
    447  * @sa usbhid_kbd_push_ev(), usbhid_kbd_repeat_start(), usbhid_kbd_repeat_stop()
    448  */
    449 static void usbhid_kbd_check_key_changes(usbhid_kbd_t *kbd_dev,
     407 * @sa usb_kbd_push_ev(), usb_kbd_repeat_start(), usb_kbd_repeat_stop()
     408 */
     409static void usb_kbd_check_key_changes(usb_kbd_t *kbd_dev,
    450410    const uint8_t *key_codes, size_t count)
    451411{
     
    488448                        // not found, i.e. the key was released
    489449                        key = usbhid_parse_scancode(kbd_dev->keys[j]);
    490                         if (!usbhid_kbd_is_lock(key)) {
    491                                 usbhid_kbd_repeat_stop(kbd_dev, key);
     450                        if (!usb_kbd_is_lock(key)) {
     451                                usb_kbd_repeat_stop(kbd_dev, key);
    492452                        }
    493                         usbhid_kbd_push_ev(kbd_dev, KEY_RELEASE, key);
     453                        usb_kbd_push_ev(kbd_dev, KEY_RELEASE, key);
    494454                        usb_log_debug2("Key released: %d\n", key);
    495455                } else {
     
    513473                        usb_log_debug2("Key pressed: %d (keycode: %d)\n", key,
    514474                            key_codes[i]);
    515                         usbhid_kbd_push_ev(kbd_dev, KEY_PRESS, key);
    516                         if (!usbhid_kbd_is_lock(key)) {
    517                                 usbhid_kbd_repeat_start(kbd_dev, key);
     475                        usb_kbd_push_ev(kbd_dev, KEY_PRESS, key);
     476                        if (!usb_kbd_is_lock(key)) {
     477                                usb_kbd_repeat_start(kbd_dev, key);
    518478                        }
    519479                } else {
     
    545505 *            structure representing the keyboard.
    546506 *
    547  * @sa usbhid_kbd_check_key_changes(), usbhid_kbd_check_modifier_changes()
    548  */
    549 static void usbhid_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
     507 * @sa usb_kbd_check_key_changes(), usb_kbd_check_modifier_changes()
     508 */
     509static void usb_kbd_process_keycodes(const uint8_t *key_codes, size_t count,
    550510    uint8_t modifiers, void *arg)
    551511{
     
    556516        }
    557517       
    558         usbhid_kbd_t *kbd_dev = (usbhid_kbd_t *)arg;
     518        usb_kbd_t *kbd_dev = (usb_kbd_t *)arg;
    559519        assert(kbd_dev != NULL);
    560520
     
    568528        }
    569529       
    570         ///usbhid_kbd_check_modifier_changes(kbd_dev, key_codes, count);
    571         usbhid_kbd_check_key_changes(kbd_dev, key_codes, count);
     530        ///usb_kbd_check_modifier_changes(kbd_dev, key_codes, count);
     531        usb_kbd_check_key_changes(kbd_dev, key_codes, count);
    572532}
    573533
     
    580540 * This function uses the HID report parser to translate the data received from
    581541 * the device into generic USB HID key codes and into generic modifiers bitmap.
    582  * The parser then calls the given callback (usbhid_kbd_process_keycodes()).
     542 * The parser then calls the given callback (usb_kbd_process_keycodes()).
    583543 *
    584544 * @note Currently, only the boot protocol is supported.
     
    588548 * @param actual_size Size of the data from keyboard (report size) in bytes.
    589549 *
    590  * @sa usbhid_kbd_process_keycodes(), usb_hid_boot_keyboard_input_report(),
     550 * @sa usb_kbd_process_keycodes(), usb_hid_boot_keyboard_input_report(),
    591551 *     usb_hid_parse_report().
    592552 */
    593 static void usbhid_kbd_process_data(usbhid_kbd_t *kbd_dev,
     553static void usb_kbd_process_data(usb_kbd_t *kbd_dev,
    594554                                    uint8_t *buffer, size_t actual_size)
    595555{
    596         assert(kbd_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
     556        assert(kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED);
    597557        assert(kbd_dev->parser != NULL);
    598558       
     
    601561                sizeof(usb_hid_report_in_callbacks_t));
    602562       
    603         callbacks->keyboard = usbhid_kbd_process_keycodes;
     563        callbacks->keyboard = usb_kbd_process_keycodes;
    604564
    605565        usb_log_debug("Calling usb_hid_parse_report() with "
     
    621581/*----------------------------------------------------------------------------*/
    622582
    623 static void usbhid_kbd_mark_unusable(usbhid_kbd_t *kbd_dev)
    624 {
    625         kbd_dev->initialized = USBHID_KBD_STATUS_TO_DESTROY;
    626 }
    627 
    628 /*----------------------------------------------------------------------------*/
    629 /* HID/KBD polling                                                            */
    630 /*----------------------------------------------------------------------------*/
    631 /**
    632  * Main keyboard polling function.
    633  *
    634  * This function uses the Interrupt In pipe of the keyboard to poll for events.
    635  * The keyboard is initialized in a way that it reports only when a key is
    636  * pressed or released, so there is no actual need for any sleeping between
    637  * polls (see usbhid_kbd_try_add_device() or usbhid_kbd_init()).
    638  *
    639  * @param kbd_dev Initialized keyboard structure representing the device to
    640  *                poll.
    641  *
    642  * @sa usbhid_kbd_process_data()
    643  */
    644 //static void usbhid_kbd_poll(usbhid_kbd_t *kbd_dev)
    645 //{
    646 //      int rc, sess_rc;
    647 //      uint8_t buffer[BOOTP_BUFFER_SIZE];
    648 //      size_t actual_size;
    649        
    650 //      usb_log_debug("Polling keyboard...\n");
    651        
    652 //      if (!kbd_dev->initialized) {
    653 //              usb_log_error("HID/KBD device not initialized!\n");
    654 //              return;
    655 //      }
    656        
    657 //      assert(kbd_dev->hid_dev != NULL);
    658 //      assert(kbd_dev->hid_dev->initialized);
    659 
    660 //      while (true) {
    661 //              sess_rc = usb_pipe_start_session(
    662 //                  &kbd_dev->hid_dev->poll_pipe);
    663 //              if (sess_rc != EOK) {
    664 //                      usb_log_warning("Failed to start a session: %s.\n",
    665 //                          str_error(sess_rc));
    666 //                      break;
    667 //              }
    668 
    669 //              rc = usb_pipe_read(&kbd_dev->hid_dev->poll_pipe,
    670 //                  buffer, BOOTP_BUFFER_SIZE, &actual_size);
    671                
    672 //              sess_rc = usb_pipe_end_session(
    673 //                  &kbd_dev->hid_dev->poll_pipe);
    674 
    675 //              if (rc != EOK) {
    676 //                      usb_log_warning("Error polling the keyboard: %s.\n",
    677 //                          str_error(rc));
    678 //                      break;
    679 //              }
    680 
    681 //              if (sess_rc != EOK) {
    682 //                      usb_log_warning("Error closing session: %s.\n",
    683 //                          str_error(sess_rc));
    684 //                      break;
    685 //              }
    686 
    687 //              /*
    688 //               * If the keyboard answered with NAK, it returned no data.
    689 //               * This implies that no change happened since last query.
    690 //               */
    691 //              if (actual_size == 0) {
    692 //                      usb_log_debug("Keyboard returned NAK\n");
    693 //                      continue;
    694 //              }
    695 
    696 //              /*
    697 //               * TODO: Process pressed keys.
    698 //               */
    699 //              usb_log_debug("Calling usbhid_kbd_process_data()\n");
    700 //              usbhid_kbd_process_data(kbd_dev, buffer, actual_size);
    701                
    702 //              // disabled for now, no reason to sleep
    703 //              //async_usleep(kbd_dev->hid_dev->poll_interval);
    704 //      }
    705 //}
    706 
    707 /*----------------------------------------------------------------------------*/
    708 /**
    709  * Function executed by the main driver fibril.
    710  *
    711  * Just starts polling the keyboard for events.
    712  *
    713  * @param arg Initialized keyboard device structure (of type usbhid_kbd_t)
    714  *            representing the device.
    715  *
    716  * @retval EOK if the fibril finished polling the device.
    717  * @retval EINVAL if no device was given in the argument.
    718  *
    719  * @sa usbhid_kbd_poll()
    720  *
    721  * @todo Change return value - only case when the fibril finishes is in case
    722  *       of some error, so the error should probably be propagated from function
    723  *       usbhid_kbd_poll() to here and up.
    724  */
    725 //static int usbhid_kbd_fibril(void *arg)
    726 //{
    727 //      if (arg == NULL) {
    728 //              usb_log_error("No device!\n");
    729 //              return EINVAL;
    730 //      }
    731        
    732 //      usbhid_kbd_t *kbd_dev = (usbhid_kbd_t *)arg;
    733 
    734 //      usbhid_kbd_poll(kbd_dev);
    735        
    736 //      // as there is another fibril using this device, so we must leave the
    737 //      // structure to it, but mark it for destroying.
    738 //      usbhid_kbd_mark_unusable(kbd_dev);
    739 //      // at the end, properly destroy the KBD structure
    740 ////    usbhid_kbd_free(&kbd_dev);
    741 ////    assert(kbd_dev == NULL);
    742 
    743 //      return EOK;
    744 //}
    745 
    746 static int usbhid_dev_get_report_descriptor(usbhid_kbd_t *kbd_dev)
    747 {
    748         assert(kbd_dev != NULL);
    749         assert(kbd_dev->usb_dev != NULL);
    750         assert(kbd_dev->usb_dev->interface_no >= 0);
    751        
    752         usb_dp_parser_t parser =  {
    753                 .nesting = usb_dp_standard_descriptor_nesting
    754         };
    755        
    756         usb_dp_parser_data_t parser_data = {
    757                 .data = kbd_dev->usb_dev->descriptors.configuration,
    758                 .size = kbd_dev->usb_dev->descriptors.configuration_size,
    759                 .arg = NULL
    760         };
    761        
    762         /*
    763          * First nested descriptor of the configuration descriptor.
    764          */
    765         uint8_t *d =
    766             usb_dp_get_nested_descriptor(&parser, &parser_data,
    767             kbd_dev->usb_dev->descriptors.configuration);
    768        
    769         /*
    770          * Find the interface descriptor corresponding to our interface number.
    771          */
    772         int i = 0;
    773         while (d != NULL && i < kbd_dev->usb_dev->interface_no) {
    774                 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
    775                     kbd_dev->usb_dev->descriptors.configuration, d);
    776         }
    777        
    778         if (d == NULL) {
    779                 usb_log_fatal("The %. interface descriptor not found!\n",
    780                     kbd_dev->usb_dev->interface_no);
    781                 return ENOENT;
    782         }
    783        
    784         /*
    785          * First nested descriptor of the interface descriptor.
    786          */
    787         uint8_t *iface_desc = d;
    788         d = usb_dp_get_nested_descriptor(&parser, &parser_data, iface_desc);
    789        
    790         /*
    791          * Search through siblings until the HID descriptor is found.
    792          */
    793         while (d != NULL && *(d + 1) != USB_DESCTYPE_HID) {
    794                 d = usb_dp_get_sibling_descriptor(&parser, &parser_data,
    795                     iface_desc, d);
    796         }
    797        
    798         if (d == NULL) {
    799                 usb_log_fatal("No HID descriptor found!\n");
    800                 return ENOENT;
    801         }
    802        
    803         if (*d != sizeof(usb_standard_hid_descriptor_t)) {
    804                 usb_log_fatal("HID descriptor hass wrong size (%u, expected %u"
    805                     ")\n", *d, sizeof(usb_standard_hid_descriptor_t));
    806                 return EINVAL;
    807         }
    808        
    809         usb_standard_hid_descriptor_t *hid_desc =
    810             (usb_standard_hid_descriptor_t *)d;
    811        
    812         uint16_t length =  hid_desc->report_desc_info.length;
    813         size_t actual_size = 0;
    814        
    815         /*
    816          * Start session for the control transfer.
    817          */
    818         int sess_rc = usb_pipe_start_session(&kbd_dev->usb_dev->ctrl_pipe);
    819         if (sess_rc != EOK) {
    820                 usb_log_warning("Failed to start a session: %s.\n",
    821                     str_error(sess_rc));
    822                 return sess_rc;
    823         }
    824 
    825         /*
    826          * Allocate space for the report descriptor.
    827          */
    828         kbd_dev->report_desc = (uint8_t *)malloc(length);
    829         if (kbd_dev->report_desc == NULL) {
    830                 usb_log_fatal("Failed to allocate space for Report descriptor."
    831                     "\n");
    832                 return ENOMEM;
    833         }
    834        
    835         usb_log_debug("Getting Report descriptor, expected size: %u\n", length);
    836        
    837         /*
    838          * Get the descriptor from the device.
    839          */
    840         int rc = usb_request_get_descriptor(&kbd_dev->usb_dev->ctrl_pipe,
    841             USB_REQUEST_TYPE_STANDARD, USB_REQUEST_RECIPIENT_INTERFACE,
    842             USB_DESCTYPE_HID_REPORT, 0, kbd_dev->usb_dev->interface_no,
    843             kbd_dev->report_desc, length, &actual_size);
    844 
    845         if (rc != EOK) {
    846                 free(kbd_dev->report_desc);
    847                 kbd_dev->report_desc = NULL;
    848                 return rc;
    849         }
    850 
    851         if (actual_size != length) {
    852                 free(kbd_dev->report_desc);
    853                 kbd_dev->report_desc = NULL;
    854                 usb_log_fatal("Report descriptor has wrong size (%u, expected "
    855                     "%u)\n", actual_size, length);
    856                 return EINVAL;
    857         }
    858        
    859         /*
    860          * End session for the control transfer.
    861          */
    862         sess_rc = usb_pipe_end_session(&kbd_dev->usb_dev->ctrl_pipe);
    863         if (sess_rc != EOK) {
    864                 usb_log_warning("Failed to end a session: %s.\n",
    865                     str_error(sess_rc));
    866                 free(kbd_dev->report_desc);
    867                 kbd_dev->report_desc = NULL;
    868                 return sess_rc;
    869         }
    870        
    871         kbd_dev->report_desc_size = length;
    872        
    873         usb_log_debug("Done.\n");
    874        
    875         return EOK;
    876 }
    877 
    878 /*----------------------------------------------------------------------------*/
    879 
    880 static int usbhid_kbd_process_report_descriptor(usbhid_kbd_t *kbd_dev)
    881 {
    882         int rc = usbhid_dev_get_report_descriptor(kbd_dev);
    883        
    884         if (rc != EOK) {
    885                 usb_log_warning("Problem with getting Report descriptor: %s.\n",
    886                     str_error(rc));
    887                 return rc;
    888         }
    889        
    890         rc = usb_hid_parse_report_descriptor(kbd_dev->parser,
    891             kbd_dev->report_desc, kbd_dev->report_desc_size);
    892         if (rc != EOK) {
    893                 usb_log_warning("Problem parsing Report descriptor: %s.\n",
    894                     str_error(rc));
    895                 return rc;
    896         }
    897        
    898         usb_hid_descriptor_print(kbd_dev->parser);
    899        
    900         /*
    901          * TODO: if failed, try to parse the boot report descriptor.
    902          */
    903        
    904         return EOK;
    905 }
     583static void usb_kbd_mark_unusable(usb_kbd_t *kbd_dev)
     584{
     585        kbd_dev->initialized = USB_KBD_STATUS_TO_DESTROY;
     586}
     587
    906588
    907589/*----------------------------------------------------------------------------*/
     
    912594 *
    913595 * The structure returned by this function is not initialized. Use
    914  * usbhid_kbd_init() to initialize it prior to polling.
     596 * usb_kbd_init() to initialize it prior to polling.
    915597 *
    916598 * @return New uninitialized structure for representing a USB/HID keyboard or
    917599 *         NULL if not successful (memory error).
    918600 */
    919 usbhid_kbd_t *usbhid_kbd_new(void)
    920 {
    921         usbhid_kbd_t *kbd_dev =
    922             (usbhid_kbd_t *)malloc(sizeof(usbhid_kbd_t));
     601usb_kbd_t *usb_kbd_new(void)
     602{
     603        usb_kbd_t *kbd_dev =
     604            (usb_kbd_t *)malloc(sizeof(usb_kbd_t));
    923605
    924606        if (kbd_dev == NULL) {
     
    927609        }
    928610       
    929         memset(kbd_dev, 0, sizeof(usbhid_kbd_t));
     611        memset(kbd_dev, 0, sizeof(usb_kbd_t));
    930612       
    931613        kbd_dev->parser = (usb_hid_report_parser_t *)(malloc(sizeof(
     
    938620       
    939621        kbd_dev->console_phone = -1;
    940         kbd_dev->initialized = USBHID_KBD_STATUS_UNINITIALIZED;
     622        kbd_dev->initialized = USB_KBD_STATUS_UNINITIALIZED;
    941623       
    942624        return kbd_dev;
     
    964646 * @return Other value inherited from function usbhid_dev_init().
    965647 */
    966 int usbhid_kbd_init(usbhid_kbd_t *kbd_dev, usb_device_t *dev)
     648int usb_kbd_init(usb_kbd_t *kbd_dev, usb_device_t *dev)
    967649{
    968650        int rc;
     
    982664        }
    983665       
    984         if (kbd_dev->initialized == USBHID_KBD_STATUS_INITIALIZED) {
     666        if (kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED) {
    985667                usb_log_warning("Keyboard structure already initialized.\n");
    986668                return EINVAL;
     
    996678//      }
    997679       
    998 //      assert(kbd_dev->hid_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
     680//      assert(kbd_dev->hid_dev->initialized == USB_KBD_STATUS_INITIALIZED);
    999681       
    1000682        // save the size of the report (boot protocol report by default)
     
    1012694       
    1013695        /* Get the report descriptor and parse it. */
    1014         rc = usbhid_kbd_process_report_descriptor(kbd_dev);
     696        rc = usb_hid_process_report_descriptor(kbd_dev->usb_dev,
     697            kbd_dev->parser);
    1015698        if (rc != EOK) {
    1016                 usb_log_warning("Could not process report descriptor.\n");
    1017                 return rc;
     699                usb_log_warning("Could not process report descriptor, "
     700                    "falling back to boot protocol.\n");
     701                rc = usb_hid_parse_report_descriptor(kbd_dev->parser,
     702                    BOOT_REPORT_DESCRIPTOR, BOOT_REPORT_DESCRIPTOR_SIZE);
     703                if (rc != EOK) {
     704                        usb_log_error("Failed to parse boot report descriptor:"
     705                            " %s.\n", str_error(rc));
     706                        return rc;
     707                }
     708               
     709                rc = usbhid_req_set_protocol(&kbd_dev->usb_dev->ctrl_pipe,
     710                    kbd_dev->usb_dev->interface_no, USB_HID_PROTOCOL_BOOT);
     711               
     712                if (rc != EOK) {
     713                        usb_log_warning("Failed to set boot protocol to the "
     714                            "device: %s\n", str_error(rc));
     715                        return rc;
     716                }
    1018717        }
    1019718       
     
    1056755       
    1057756        /*
    1058          * Set boot protocol.
    1059757         * Set LEDs according to initial setup.
    1060758         * Set Idle rate
    1061759         */
    1062         //assert(kbd_dev->hid_dev != NULL);
    1063         //assert(kbd_dev->hid_dev->initialized);
    1064         //usbhid_req_set_protocol(kbd_dev->hid_dev, USB_HID_PROTOCOL_BOOT);
    1065        
    1066         usbhid_kbd_set_led(kbd_dev);
     760        usb_kbd_set_led(kbd_dev);
    1067761       
    1068762        usbhid_req_set_idle(&kbd_dev->usb_dev->ctrl_pipe,
    1069763            kbd_dev->usb_dev->interface_no, IDLE_RATE);
    1070764       
    1071         kbd_dev->initialized = USBHID_KBD_STATUS_INITIALIZED;
     765        kbd_dev->initialized = USB_KBD_STATUS_INITIALIZED;
    1072766        usb_log_debug("HID/KBD device structure initialized.\n");
    1073767       
     
    1077771/*----------------------------------------------------------------------------*/
    1078772
    1079 bool usbhid_kbd_polling_callback(usb_device_t *dev, uint8_t *buffer,
     773bool usb_kbd_polling_callback(usb_device_t *dev, uint8_t *buffer,
    1080774     size_t buffer_size, void *arg)
    1081775{
     
    1085779        }
    1086780       
    1087         usbhid_kbd_t *kbd_dev = (usbhid_kbd_t *)arg;
     781        usb_kbd_t *kbd_dev = (usb_kbd_t *)arg;
    1088782       
    1089783        // TODO: add return value from this function
    1090         usbhid_kbd_process_data(kbd_dev, buffer, buffer_size);
     784        usb_kbd_process_data(kbd_dev, buffer, buffer_size);
    1091785       
    1092786        return true;
     
    1095789/*----------------------------------------------------------------------------*/
    1096790
    1097 void usbhid_kbd_polling_ended_callback(usb_device_t *dev, bool reason,
     791void usb_kbd_polling_ended_callback(usb_device_t *dev, bool reason,
    1098792     void *arg)
    1099793{
     
    1102796        }
    1103797       
    1104         usbhid_kbd_t *kbd = (usbhid_kbd_t *)arg;
    1105        
    1106         usbhid_kbd_mark_unusable(kbd);
    1107 }
    1108 
    1109 /*----------------------------------------------------------------------------*/
    1110 
    1111 int usbhid_kbd_is_initialized(const usbhid_kbd_t *kbd_dev)
    1112 {
    1113         return (kbd_dev->initialized == USBHID_KBD_STATUS_INITIALIZED);
    1114 }
    1115 
    1116 /*----------------------------------------------------------------------------*/
    1117 
    1118 int usbhid_kbd_is_ready_to_destroy(const usbhid_kbd_t *kbd_dev)
    1119 {
    1120         return (kbd_dev->initialized == USBHID_KBD_STATUS_TO_DESTROY);
     798        usb_kbd_t *kbd = (usb_kbd_t *)arg;
     799       
     800        usb_kbd_mark_unusable(kbd);
     801}
     802
     803/*----------------------------------------------------------------------------*/
     804
     805int usb_kbd_is_initialized(const usb_kbd_t *kbd_dev)
     806{
     807        return (kbd_dev->initialized == USB_KBD_STATUS_INITIALIZED);
     808}
     809
     810/*----------------------------------------------------------------------------*/
     811
     812int usb_kbd_is_ready_to_destroy(const usb_kbd_t *kbd_dev)
     813{
     814        return (kbd_dev->initialized == USB_KBD_STATUS_TO_DESTROY);
    1121815}
    1122816
     
    1127821 * @param kbd_dev Pointer to the structure to be destroyed.
    1128822 */
    1129 void usbhid_kbd_free(usbhid_kbd_t **kbd_dev)
     823void usb_kbd_free(usb_kbd_t **kbd_dev)
    1130824{
    1131825        if (kbd_dev == NULL || *kbd_dev == NULL) {
  • uspace/drv/usbkbd/kbddev.h

    r476b71ff r252e30c  
    3434 */
    3535
    36 #ifndef USBHID_KBDDEV_H_
    37 #define USBHID_KBDDEV_H_
     36#ifndef USB_KBDDEV_H_
     37#define USB_KBDDEV_H_
    3838
    3939#include <stdint.h>
     
    6262 *       being device-specific.
    6363 */
    64 typedef struct usbhid_kbd_t {
     64typedef struct usb_kbd_t {
    6565        /** Structure holding generic USB device information. */
    6666        //usbhid_dev_t *hid_dev;
     
    8484       
    8585        /** Information for auto-repeat of keys. */
    86         usbhid_kbd_repeat_t repeat;
     86        usb_kbd_repeat_t repeat;
    8787       
    8888        /** Mutex for accessing the information about auto-repeat. */
     
    105105         */
    106106        int initialized;
    107 } usbhid_kbd_t;
     107} usb_kbd_t;
    108108
    109109/*----------------------------------------------------------------------------*/
    110110
    111111enum {
    112         USBHID_KBD_POLL_EP_NO = 0,
    113         USBHID_KBD_POLL_EP_COUNT = 1
     112        USB_KBD_POLL_EP_NO = 0,
     113        USB_KBD_POLL_EP_COUNT = 1
    114114};
    115115
    116 usb_endpoint_description_t *usbhid_kbd_endpoints[USBHID_KBD_POLL_EP_COUNT + 1];
     116usb_endpoint_description_t *usb_kbd_endpoints[USB_KBD_POLL_EP_COUNT + 1];
    117117
    118118ddf_dev_ops_t keyboard_ops;
     
    120120/*----------------------------------------------------------------------------*/
    121121
    122 usbhid_kbd_t *usbhid_kbd_new(void);
     122usb_kbd_t *usb_kbd_new(void);
    123123
    124 int usbhid_kbd_init(usbhid_kbd_t *kbd_dev, usb_device_t *dev);
     124int usb_kbd_init(usb_kbd_t *kbd_dev, usb_device_t *dev);
    125125
    126 bool usbhid_kbd_polling_callback(usb_device_t *dev, uint8_t *buffer,
     126bool usb_kbd_polling_callback(usb_device_t *dev, uint8_t *buffer,
    127127     size_t buffer_size, void *arg);
    128128
    129 void usbhid_kbd_polling_ended_callback(usb_device_t *dev, bool reason,
     129void usb_kbd_polling_ended_callback(usb_device_t *dev, bool reason,
    130130     void *arg);
    131131
    132 int usbhid_kbd_is_initialized(const usbhid_kbd_t *kbd_dev);
     132int usb_kbd_is_initialized(const usb_kbd_t *kbd_dev);
    133133
    134 int usbhid_kbd_is_ready_to_destroy(const usbhid_kbd_t *kbd_dev);
     134int usb_kbd_is_ready_to_destroy(const usb_kbd_t *kbd_dev);
    135135
    136 void usbhid_kbd_free(usbhid_kbd_t **kbd_dev);
     136void usb_kbd_free(usb_kbd_t **kbd_dev);
    137137
    138 void usbhid_kbd_push_ev(usbhid_kbd_t *kbd_dev, int type, unsigned int key);
     138void usb_kbd_push_ev(usb_kbd_t *kbd_dev, int type, unsigned int key);
    139139
    140 #endif /* USBHID_KBDDEV_H_ */
     140#endif /* USB_KBDDEV_H_ */
    141141
    142142/**
  • uspace/drv/usbkbd/kbdrepeat.c

    r476b71ff r252e30c  
    6262 *
    6363 * If the currently repeated key is not pressed any more (
    64  * usbhid_kbd_repeat_stop() was called), it stops repeating it and starts
     64 * usb_kbd_repeat_stop() was called), it stops repeating it and starts
    6565 * checking again.
    6666 *
     
    7070 * @param kbd Keyboard device structure.
    7171 */
    72 static void usbhid_kbd_repeat_loop(usbhid_kbd_t *kbd)
     72static void usb_kbd_repeat_loop(usb_kbd_t *kbd)
    7373{
    7474        unsigned int delay = 0;
     
    7878        while (true) {
    7979                // check if the kbd structure is usable
    80                 if (!usbhid_kbd_is_initialized(kbd)) {
    81                         if (usbhid_kbd_is_ready_to_destroy(kbd)) {
    82                                 usbhid_kbd_free(&kbd);
     80                if (!usb_kbd_is_initialized(kbd)) {
     81                        if (usb_kbd_is_ready_to_destroy(kbd)) {
     82                                usb_kbd_free(&kbd);
    8383                                assert(kbd == NULL);
    8484                        }
     
    9292                                usb_log_debug2("Repeating key: %u.\n",
    9393                                    kbd->repeat.key_repeated);
    94                                 usbhid_kbd_push_ev(kbd, KEY_PRESS,
     94                                usb_kbd_push_ev(kbd, KEY_PRESS,
    9595                                    kbd->repeat.key_repeated);
    9696                                delay = kbd->repeat.delay_between;
     
    127127 * @retval EINVAL if no argument is supplied.
    128128 */
    129 int usbhid_kbd_repeat_fibril(void *arg)
     129int usb_kbd_repeat_fibril(void *arg)
    130130{
    131131        usb_log_debug("Autorepeat fibril spawned.\n");
     
    136136        }
    137137       
    138         usbhid_kbd_t *kbd = (usbhid_kbd_t *)arg;
     138        usb_kbd_t *kbd = (usb_kbd_t *)arg;
    139139       
    140         usbhid_kbd_repeat_loop(kbd);
     140        usb_kbd_repeat_loop(kbd);
    141141       
    142142        return EOK;
     
    154154 * @param key Key to start repeating.
    155155 */
    156 void usbhid_kbd_repeat_start(usbhid_kbd_t *kbd, unsigned int key)
     156void usb_kbd_repeat_start(usb_kbd_t *kbd, unsigned int key)
    157157{
    158158        fibril_mutex_lock(kbd->repeat_mtx);
     
    172172 * @param key Key to stop repeating.
    173173 */
    174 void usbhid_kbd_repeat_stop(usbhid_kbd_t *kbd, unsigned int key)
     174void usb_kbd_repeat_stop(usb_kbd_t *kbd, unsigned int key)
    175175{
    176176        fibril_mutex_lock(kbd->repeat_mtx);
  • uspace/drv/usbkbd/kbdrepeat.h

    r476b71ff r252e30c  
    3434 */
    3535
    36 #ifndef USBHID_KBDREPEAT_H_
    37 #define USBHID_KBDREPEAT_H_
     36#ifndef USB_KBDREPEAT_H_
     37#define USB_KBDREPEAT_H_
    3838
    39 struct usbhid_kbd_t;
     39struct usb_kbd_t;
    4040
    4141/*----------------------------------------------------------------------------*/
     
    5252        /** Delay between repeats in microseconds. */
    5353        unsigned int delay_between;
    54 } usbhid_kbd_repeat_t;
     54} usb_kbd_repeat_t;
    5555
    5656/*----------------------------------------------------------------------------*/
    5757
    58 int usbhid_kbd_repeat_fibril(void *arg);
     58int usb_kbd_repeat_fibril(void *arg);
    5959
    60 void usbhid_kbd_repeat_start(struct usbhid_kbd_t *kbd, unsigned int key);
     60void usb_kbd_repeat_start(struct usb_kbd_t *kbd, unsigned int key);
    6161
    62 void usbhid_kbd_repeat_stop(struct usbhid_kbd_t *kbd, unsigned int key);
     62void usb_kbd_repeat_stop(struct usb_kbd_t *kbd, unsigned int key);
    6363
    64 #endif /* USBHID_KBDREPEAT_H_ */
     64#endif /* USB_KBDREPEAT_H_ */
    6565
    6666/**
  • uspace/drv/usbkbd/layout.h

    r476b71ff r252e30c  
    3636 */
    3737
    38 #ifndef USBHID_LAYOUT_H_
    39 #define USBHID_LAYOUT_H_
     38#ifndef USB_KBD_LAYOUT_H_
     39#define USB_KBD_LAYOUT_H_
    4040
    4141#include <sys/types.h>
  • uspace/drv/usbkbd/main.c

    r476b71ff r252e30c  
    7070 * @retval EOK if successful.
    7171 * @retval ENOMEM if there
    72  * @return Other error code inherited from one of functions usbhid_kbd_init(),
     72 * @return Other error code inherited from one of functions usb_kbd_init(),
    7373 *         ddf_fun_bind() and ddf_fun_add_to_class().
    7474 *
    75  * @sa usbhid_kbd_fibril(), usbhid_kbd_repeat_fibril()
     75 * @sa usb_kbd_fibril(), usb_kbd_repeat_fibril()
    7676 */
    7777static int usbhid_try_add_device(usb_device_t *dev)
     
    9090        usb_log_debug("Initializing USB/HID KBD device...\n");
    9191       
    92         usbhid_kbd_t *kbd_dev = usbhid_kbd_new();
     92        usb_kbd_t *kbd_dev = usb_kbd_new();
    9393        if (kbd_dev == NULL) {
    9494                usb_log_error("Error while creating USB/HID KBD device "
     
    9898        }
    9999       
    100         int rc = usbhid_kbd_init(kbd_dev, dev);
     100        int rc = usb_kbd_init(kbd_dev, dev);
    101101       
    102102        if (rc != EOK) {
    103103                usb_log_error("Failed to initialize USB/HID KBD device.\n");
    104104                ddf_fun_destroy(kbd_fun);
    105                 usbhid_kbd_free(&kbd_dev);
     105                usb_kbd_free(&kbd_dev);
    106106                return rc;
    107107        }       
     
    122122                // TODO: Can / should I destroy the DDF function?
    123123                ddf_fun_destroy(kbd_fun);
    124                 usbhid_kbd_free(&kbd_dev);
     124                usb_kbd_free(&kbd_dev);
    125125                return rc;
    126126        }
     
    133133                // TODO: Can / should I destroy the DDF function?
    134134                ddf_fun_destroy(kbd_fun);
    135                 usbhid_kbd_free(&kbd_dev);
     135                usb_kbd_free(&kbd_dev);
    136136                return rc;
    137137        }
     
    140140         * Create new fibril for handling this keyboard
    141141         */
    142         //fid_t fid = fibril_create(usbhid_kbd_fibril, kbd_dev);
     142        //fid_t fid = fibril_create(usb_kbd_fibril, kbd_dev);
    143143       
    144144        /* Start automated polling function.
     
    148148       rc = usb_device_auto_poll(dev,
    149149           /* Index of the polling pipe. */
    150            USBHID_KBD_POLL_EP_NO,
     150           USB_KBD_POLL_EP_NO,
    151151           /* Callback when data arrives. */
    152            usbhid_kbd_polling_callback,
     152           usb_kbd_polling_callback,
    153153           /* How much data to request. */
    154            dev->pipes[USBHID_KBD_POLL_EP_NO].pipe->max_packet_size,
     154           dev->pipes[USB_KBD_POLL_EP_NO].pipe->max_packet_size,
    155155           /* Callback when the polling ends. */
    156            usbhid_kbd_polling_ended_callback,
     156           usb_kbd_polling_ended_callback,
    157157           /* Custom argument. */
    158158           kbd_dev);
     
    169169         * Create new fibril for auto-repeat
    170170         */
    171         fid_t fid = fibril_create(usbhid_kbd_repeat_fibril, kbd_dev);
     171        fid_t fid = fibril_create(usb_kbd_repeat_fibril, kbd_dev);
    172172        if (fid == 0) {
    173173                usb_log_error("Failed to start fibril for KBD auto-repeat");
     
    233233        .name = NAME,
    234234        .ops = &usbhid_driver_ops,
    235         .endpoints = usbhid_kbd_endpoints
     235        .endpoints = usb_kbd_endpoints
    236236};
    237237
  • uspace/lib/usb/Makefile

    r476b71ff r252e30c  
    5151        src/usbdevice.c \
    5252        src/hidreq.c \
     53        src/hidreport.c \
    5354        src/host/device_keeper.c \
    5455        src/host/batch.c
  • uspace/lib/usb/include/usb/classes/hidreq.h

    r476b71ff r252e30c  
    2727 */
    2828
    29 /** @addtogroup drvusbhid
     29/** @addtogroup libusb
    3030 * @{
    3131 */
     
    3434 */
    3535
    36 #ifndef USBHID_HIDREQ_H_
    37 #define USBHID_HIDREQ_H_
     36#ifndef USB_KBD_HIDREQ_H_
     37#define USB_KBD_HIDREQ_H_
    3838
    3939#include <stdint.h>
     
    6363/*----------------------------------------------------------------------------*/
    6464
    65 #endif /* USBHID_HIDREQ_H_ */
     65#endif /* USB_KBD_HIDREQ_H_ */
    6666
    6767/**
Note: See TracChangeset for help on using the changeset viewer.