Changeset e765ccb in mainline for uspace/app/mkbd/main.c


Ignore:
Timestamp:
2011-05-20T10:44:53Z (13 years ago)
Author:
Lubos Slovak <lubos.slovak@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
e7079cf
Parents:
d1fb591
Message:

Changed HID driver interface and modified mkbd + fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/mkbd/main.c

    rd1fb591 re765ccb  
    4747#include <usb/host.h>
    4848#include <usb/driver.h>
     49#include <usb/hid/iface.h>
    4950#include <usb/dev/pipes.h>
    5051
     
    9394//}
    9495
    95 static bool try_parse_class_and_address(const char *path,
    96     devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
    97 {
    98         size_t class_index;
    99         size_t address;
    100         int rc;
    101         char *ptr;
    102 
    103         rc = str_size_t(path, &ptr, 10, false, &class_index);
    104         if (rc != EOK) {
    105                 return false;
    106         }
    107         if ((*ptr == ':') || (*ptr == '.')) {
    108                 ptr++;
    109         } else {
    110                 return false;
    111         }
    112         rc = str_size_t(ptr, NULL, 10, true, &address);
    113         if (rc != EOK) {
    114                 return false;
    115         }
    116         rc = usb_ddf_get_hc_handle_by_class(class_index, out_hc_handle);
    117         if (rc != EOK) {
    118                 return false;
    119         }
    120         if (out_device_address != NULL) {
    121                 *out_device_address = (usb_address_t) address;
    122         }
    123         return true;
    124 }
    125 
    126 static bool resolve_hc_handle_and_dev_addr(const char *devpath,
    127     devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
    128 {
    129         int rc;
    130 
    131         /* Hack for QEMU to save-up on typing ;-). */
    132         if (str_cmp(devpath, "qemu") == 0) {
    133                 devpath = "/hw/pci0/00:01.2/uhci-rh/usb00_a1";
    134         }
    135 
    136         /* Hack for virtual keyboard. */
    137         if (str_cmp(devpath, "virt") == 0) {
    138                 devpath = "/virt/usbhc/usb00_a1/usb00_a2";
    139         }
    140 
    141         if (try_parse_class_and_address(devpath,
    142             out_hc_handle, out_device_address)) {
    143                 return true;
    144         }
    145 
    146         char *path = str_dup(devpath);
    147         if (path == NULL) {
    148                 return ENOMEM;
    149         }
    150 
    151         devman_handle_t hc = 0;
    152         bool hc_found = false;
    153         usb_address_t addr = 0;
    154         bool addr_found = false;
    155 
    156         /* Remove suffixes and hope that we will encounter device node. */
    157         while (str_length(path) > 0) {
    158                 /* Get device handle first. */
    159                 devman_handle_t dev_handle;
    160                 rc = devman_device_get_handle(path, &dev_handle, 0);
    161                 if (rc != EOK) {
    162                         free(path);
    163                         return false;
    164                 }
    165 
    166                 /* Try to find its host controller. */
    167                 if (!hc_found) {
    168                         rc = usb_hc_find(dev_handle, &hc);
    169                         if (rc == EOK) {
    170                                 hc_found = true;
    171                         }
    172                 }
    173                 /* Try to get its address. */
    174                 if (!addr_found) {
    175                         addr = usb_device_get_assigned_address(dev_handle);
    176                         if (addr >= 0) {
    177                                 addr_found = true;
    178                         }
    179                 }
    180 
    181                 /* Speed-up. */
    182                 if (hc_found && addr_found) {
    183                         break;
    184                 }
    185 
    186                 /* Remove the last suffix. */
    187                 char *slash_pos = str_rchr(path, '/');
    188                 if (slash_pos != NULL) {
    189                         *slash_pos = 0;
    190                 }
    191         }
    192 
    193         free(path);
    194 
    195         if (hc_found && addr_found) {
    196                 if (out_hc_handle != NULL) {
    197                         *out_hc_handle = hc;
    198                 }
    199                 if (out_device_address != NULL) {
    200                         *out_device_address = addr;
    201                 }
    202                 return true;
    203         } else {
    204                 return false;
    205         }
    206 }
     96//static bool try_parse_class_and_address(const char *path,
     97//    devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
     98//{
     99//      size_t class_index;
     100//      size_t address;
     101//      int rc;
     102//      char *ptr;
     103
     104//      rc = str_size_t(path, &ptr, 10, false, &class_index);
     105//      if (rc != EOK) {
     106//              return false;
     107//      }
     108//      if ((*ptr == ':') || (*ptr == '.')) {
     109//              ptr++;
     110//      } else {
     111//              return false;
     112//      }
     113//      rc = str_size_t(ptr, NULL, 10, true, &address);
     114//      if (rc != EOK) {
     115//              return false;
     116//      }
     117//      rc = usb_ddf_get_hc_handle_by_class(class_index, out_hc_handle);
     118//      if (rc != EOK) {
     119//              return false;
     120//      }
     121//      if (out_device_address != NULL) {
     122//              *out_device_address = (usb_address_t) address;
     123//      }
     124//      return true;
     125//}
     126
     127//static bool resolve_hc_handle_and_dev_addr(const char *devpath,
     128//    devman_handle_t *out_hc_handle, usb_address_t *out_device_address)
     129//{
     130//      int rc;
     131
     132//      /* Hack for QEMU to save-up on typing ;-). */
     133//      if (str_cmp(devpath, "qemu") == 0) {
     134//              devpath = "/hw/pci0/00:01.2/uhci-rh/usb00_a1";
     135//      }
     136
     137//      /* Hack for virtual keyboard. */
     138//      if (str_cmp(devpath, "virt") == 0) {
     139//              devpath = "/virt/usbhc/usb00_a1/usb00_a2";
     140//      }
     141
     142//      if (try_parse_class_and_address(devpath,
     143//          out_hc_handle, out_device_address)) {
     144//              return true;
     145//      }
     146
     147//      char *path = str_dup(devpath);
     148//      if (path == NULL) {
     149//              return ENOMEM;
     150//      }
     151
     152//      devman_handle_t hc = 0;
     153//      bool hc_found = false;
     154//      usb_address_t addr = 0;
     155//      bool addr_found = false;
     156
     157//      /* Remove suffixes and hope that we will encounter device node. */
     158//      while (str_length(path) > 0) {
     159//              /* Get device handle first. */
     160//              devman_handle_t dev_handle;
     161//              rc = devman_device_get_handle(path, &dev_handle, 0);
     162//              if (rc != EOK) {
     163//                      free(path);
     164//                      return false;
     165//              }
     166
     167//              /* Try to find its host controller. */
     168//              if (!hc_found) {
     169//                      rc = usb_hc_find(dev_handle, &hc);
     170//                      if (rc == EOK) {
     171//                              hc_found = true;
     172//                      }
     173//              }
     174//              /* Try to get its address. */
     175//              if (!addr_found) {
     176//                      addr = usb_device_get_assigned_address(dev_handle);
     177//                      if (addr >= 0) {
     178//                              addr_found = true;
     179//                      }
     180//              }
     181
     182//              /* Speed-up. */
     183//              if (hc_found && addr_found) {
     184//                      break;
     185//              }
     186
     187//              /* Remove the last suffix. */
     188//              char *slash_pos = str_rchr(path, '/');
     189//              if (slash_pos != NULL) {
     190//                      *slash_pos = 0;
     191//              }
     192//      }
     193
     194//      free(path);
     195
     196//      if (hc_found && addr_found) {
     197//              if (out_hc_handle != NULL) {
     198//                      *out_hc_handle = hc;
     199//              }
     200//              if (out_device_address != NULL) {
     201//                      *out_device_address = addr;
     202//              }
     203//              return true;
     204//      } else {
     205//              return false;
     206//      }
     207//}
    207208
    208209static void print_usage(char *app_name)
     
    218219}
    219220
     221#define MAX_PATH_LENGTH 1024
     222
    220223int main(int argc, char *argv[])
    221224{
     
    228231        char *devpath = argv[1];
    229232
    230         /* The initialization is here only to make compiler happy. */
    231         devman_handle_t hc_handle = 0;
    232         usb_address_t dev_addr = 0;
    233         bool found = resolve_hc_handle_and_dev_addr(devpath,
    234             &hc_handle, &dev_addr);
    235         if (!found) {
    236                 fprintf(stderr, NAME ": device `%s' not found "
    237                     "or not of USB kind. Exiting.\n", devpath);
    238                 return -1;
    239         }
     233//      /* The initialization is here only to make compiler happy. */
     234//      devman_handle_t hc_handle = 0;
     235//      usb_address_t dev_addr = 0;
     236//      bool found = resolve_hc_handle_and_dev_addr(devpath,
     237//          &hc_handle, &dev_addr);
     238//      if (!found) {
     239//              fprintf(stderr, NAME ": device `%s' not found "
     240//                  "or not of USB kind. Exiting.\n", devpath);
     241//              return -1;
     242//      }
     243       
     244//      int rc;
     245//      usb_hc_connection_t conn;
     246
     247//      usb_hc_connection_initialize(&conn, hc_handle);
     248//      rc = usb_hc_connection_open(&conn);
     249//      if (rc != EOK) {
     250//              printf(NAME ": failed to connect to HC: %s.\n",
     251//                  str_error(rc));
     252//              return -1;
     253//      }
     254
     255//      devman_handle_t dev_handle;
     256//      rc = usb_hc_get_handle_by_address(&conn, dev_addr, &dev_handle);
     257//      if (rc != EOK) {
     258//              printf(NAME ": failed getting handle to the device: %s.\n",
     259//                     str_error(rc));
     260//              return -1;
     261//      }
     262
     263//      usb_hc_connection_close(&conn);
    240264       
    241265        int rc;
    242         usb_hc_connection_t conn;
    243 
    244         usb_hc_connection_initialize(&conn, hc_handle);
    245         rc = usb_hc_connection_open(&conn);
     266       
     267        devman_handle_t dev_handle = 0;
     268        rc = devman_device_get_handle(devpath, &dev_handle, 0);
    246269        if (rc != EOK) {
    247                 printf(NAME ": failed to connect to HC: %s.\n",
     270                printf("Failed to get handle from devman: %s.\n",
    248271                    str_error(rc));
    249                 return -1;
    250         }
    251         usb_address_t addr = 0;
    252 
    253         devman_handle_t dev_handle;
    254         rc = usb_hc_get_handle_by_address(&conn, addr, &dev_handle);
    255         if (rc != EOK) {
    256                 printf(NAME ": failed getting handle to the device: %s.\n",
    257                        str_error(rc));
    258                 return -1;
    259         }
    260 
    261         usb_hc_connection_close(&conn);
     272                return rc;
     273        }
    262274       
    263275        rc = devman_device_connect(dev_handle, 0);
    264276        if (rc < 0) {
    265                 printf(NAME ": failed to connect to the device: %s.\n",
    266                        str_error(rc));
    267                 return -1;
     277                printf(NAME ": failed to connect to the device (handle %"
     278                       PRIun "): %s.\n", dev_handle, str_error(rc));
     279                return rc;
    268280        }
    269281       
     
    271283        printf("Got phone to the device: %d\n", dev_phone);
    272284       
    273        
    274 //      size_t class_index = 0;
    275 //      size_t failed_attempts = 0;
    276 
    277 //      while (failed_attempts < MAX_FAILED_ATTEMPTS) {
    278 //              class_index++;
    279 //              devman_handle_t hc_handle = 0;
    280 //              int rc = usb_ddf_get_hc_handle_by_class(class_index, &hc_handle);
    281 //              if (rc != EOK) {
    282 //                      failed_attempts++;
    283 //                      continue;
    284 //              }
    285 //              char path[MAX_PATH_LENGTH];
    286 //              rc = devman_get_device_path(hc_handle, path, MAX_PATH_LENGTH);
    287 //              if (rc != EOK) {
    288 //                      continue;
    289 //              }
    290 //              print_found_hc(class_index, path);
    291 //              print_hc_devices(hc_handle);
    292 //      }
     285        char path[MAX_PATH_LENGTH];
     286        rc = devman_get_device_path(dev_handle, path, MAX_PATH_LENGTH);
     287        if (rc != EOK) {
     288                return ENOMEM;
     289        }
     290       
     291        printf("Device path: %s\n", path);
     292       
     293        size_t size;
     294        rc = usbhid_dev_get_event_length(dev_phone, &size);
     295        if (rc != EOK) {
     296                printf("Failed to get event length: %s.\n",
     297                    str_error(rc));
     298                return rc;
     299        }
     300       
     301        printf("Event length: %zu\n", size);
     302        int32_t *event = (int32_t *)malloc(size);
     303        if (event == NULL) {
     304                // hangup phone?
     305                return ENOMEM;
     306        }
     307       
     308        printf("Event length: %zu\n", size);
     309       
     310        size_t actual_size;
     311       
     312        while (1) {
     313                // get event from the driver
     314                printf("Getting event from the driver.\n");
     315                rc = usbhid_dev_get_event(dev_phone, event, size, &actual_size,
     316                    0);
     317                if (rc != EOK) {
     318                        // hangup phone?
     319                        printf("Error in getting event from the HID driver:"
     320                            "%s.\n", str_error(rc));
     321                        break;
     322                }
     323               
     324                printf("Got buffer: %p, size: %zu\n", event, actual_size);
     325        }
    293326       
    294327        return 0;
Note: See TracChangeset for help on using the changeset viewer.