Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/usbhid/usbhid.c

    rfaa44e58 r8fb45e08  
    6363static const int USB_HID_MAX_SUBDRIVERS = 10;
    6464
    65 static fibril_local bool report_received;
     65/** @todo What happens if this is not fibril local? */
     66//static fibril_local bool report_number;
    6667
    6768/*----------------------------------------------------------------------------*/
     
    203204       
    204205        usb_log_debug("Compare flags: %d\n", mapping->compare);
    205         size_t size = usb_hid_report_input_length(hid_dev->report, usage_path,
    206             mapping->compare);
     206//      size_t size = usb_hid_report_size(hid_dev->report, 0,
     207//          USB_HID_REPORT_TYPE_INPUT);
     208        size_t size = 0;
     209        usb_hid_report_field_t *field = usb_hid_report_get_sibling (hid_dev->report,
     210                NULL, usage_path, mapping->compare, USB_HID_REPORT_TYPE_INPUT);
     211        while(field != NULL) {
     212                size++;
     213                field = usb_hid_report_get_sibling (hid_dev->report,
     214                                        field, usage_path, mapping->compare,
     215                            USB_HID_REPORT_TYPE_INPUT);
     216        }
     217       
    207218        usb_log_debug("Size of the input report: %zuB\n", size);
    208        
    209219        usb_hid_report_path_free(usage_path);
    210220       
     
    225235        }
    226236       
    227         hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(count *
     237        // add one generic HID subdriver per device
     238       
     239        hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc((count + 1) *
    228240            sizeof(usb_hid_subdriver_t));
    229241        if (hid_dev->subdrivers == NULL) {
     
    238250        }
    239251       
    240         hid_dev->subdriver_count = count;
     252        hid_dev->subdrivers[count].init = usb_generic_hid_init;
     253        hid_dev->subdrivers[count].poll = usb_generic_hid_polling_callback;
     254        hid_dev->subdrivers[count].deinit = NULL;
     255        hid_dev->subdrivers[count].poll_end = NULL;
     256       
     257        hid_dev->subdriver_count = count + 1;
    241258       
    242259        return EOK;
     
    298315               
    299316                if (matched) {
     317                        usb_log_debug("Subdriver matched.\n");
    300318                        subdrivers[count++] = &mapping->subdriver;
    301319                }
     
    339357/*----------------------------------------------------------------------------*/
    340358
     359static int usb_hid_init_report(usb_hid_dev_t *hid_dev)
     360{
     361        assert(hid_dev != NULL && hid_dev->report != NULL);
     362       
     363        uint8_t report_id = 0;
     364        size_t size;/* = usb_hid_report_byte_size(hid_dev->report, report_id,
     365            USB_HID_REPORT_TYPE_INPUT);*/
     366       
     367        size_t max_size = 0;
     368       
     369        do {
     370                size = usb_hid_report_byte_size(hid_dev->report, report_id,
     371                    USB_HID_REPORT_TYPE_INPUT);
     372                usb_log_debug("Report ID: %u, size: %zu\n", report_id, size);
     373                max_size = (size > max_size) ? size : max_size;
     374                report_id = usb_hid_get_next_report_id(hid_dev->report,
     375                    report_id, USB_HID_REPORT_TYPE_INPUT);
     376        } while (report_id != 0);
     377       
     378        usb_log_debug("Max size of input report: %zu\n", max_size);
     379       
     380        hid_dev->max_input_report_size = max_size;
     381        assert(hid_dev->input_report == NULL);
     382       
     383        hid_dev->input_report = malloc(max_size);
     384        if (hid_dev->input_report == NULL) {
     385                return ENOMEM;
     386        }
     387        memset(hid_dev->input_report, 0, max_size);
     388       
     389        return EOK;
     390}
     391
     392/*----------------------------------------------------------------------------*/
     393
    341394usb_hid_dev_t *usb_hid_new(void)
    342395{
     
    393446        /* Get the report descriptor and parse it. */
    394447        rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
    395             hid_dev->report);
     448            hid_dev->report, &hid_dev->report_desc, &hid_dev->report_desc_size);
    396449       
    397450        bool fallback = false;
     
    457510                        if (hid_dev->subdrivers[i].init != NULL) {
    458511                                usb_log_debug("Initializing subdriver %d.\n",i);
    459                                 rc = hid_dev->subdrivers[i].init(hid_dev);
     512                                rc = hid_dev->subdrivers[i].init(hid_dev,
     513                                    &hid_dev->subdrivers[i].data);
    460514                                if (rc != EOK) {
    461515                                        usb_log_warning("Failed to initialize"
     
    473527        }
    474528       
     529        // save max input report size and allocate space for the report
     530        rc = usb_hid_init_report(hid_dev);
     531        if (rc != EOK) {
     532                usb_log_error("Failed to initialize input report buffer.\n");
     533        }
     534       
    475535        return rc;
    476536}
     
    490550        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
    491551       
    492         int allocated = (hid_dev->input_report != NULL);
    493        
    494         if (!allocated
    495             || hid_dev->input_report_size < buffer_size) {
    496                 uint8_t *input_old = hid_dev->input_report;
    497                 uint8_t *input_new = (uint8_t *)malloc(buffer_size);
    498                
    499                 if (input_new == NULL) {
    500                         usb_log_error("Failed to allocate space for input "
    501                             "buffer. This event may not be reported\n");
    502                         memset(hid_dev->input_report, 0,
    503                             hid_dev->input_report_size);
    504                 } else {
    505                         memcpy(input_new, input_old,
    506                             hid_dev->input_report_size);
    507                         hid_dev->input_report = input_new;
    508                         if (allocated) {
    509                                 free(input_old);
    510                         }
    511                         usb_hid_new_report();
    512                 }
    513         }
     552//      int allocated = (hid_dev->input_report != NULL);
     553        assert(hid_dev->input_report != NULL);
     554        usb_log_debug("Max input report size: %zu, buffer size: %zu\n",
     555            hid_dev->max_input_report_size, buffer_size);
     556        assert(hid_dev->max_input_report_size >= buffer_size);
     557       
     558//      if (/*!allocated*/
     559//          /*|| *//*hid_dev->input_report_size < buffer_size*/) {
     560//              uint8_t *input_old = hid_dev->input_report;
     561//              uint8_t *input_new = (uint8_t *)malloc(buffer_size);
     562               
     563//              if (input_new == NULL) {
     564//                      usb_log_error("Failed to allocate space for input "
     565//                          "buffer. This event may not be reported\n");
     566//                      memset(hid_dev->input_report, 0,
     567//                          hid_dev->input_report_size);
     568//              } else {
     569//                      memcpy(input_new, input_old,
     570//                          hid_dev->input_report_size);
     571//                      hid_dev->input_report = input_new;
     572//                      if (allocated) {
     573//                              free(input_old);
     574//                      }
     575//                      usb_hid_new_report();
     576//              }
     577//      }
    514578       
    515579        /*! @todo This should probably be atomic. */
    516580        memcpy(hid_dev->input_report, buffer, buffer_size);
    517581        hid_dev->input_report_size = buffer_size;
     582        usb_hid_new_report(hid_dev);
    518583       
    519584        bool cont = false;
     
    522587        for (i = 0; i < hid_dev->subdriver_count; ++i) {
    523588                if (hid_dev->subdrivers[i].poll != NULL
    524                     && hid_dev->subdrivers[i].poll(hid_dev, buffer,
    525                     buffer_size)) {
     589                    && hid_dev->subdrivers[i].poll(hid_dev,
     590                        hid_dev->subdrivers[i].data, buffer, buffer_size)) {
    526591                        cont = true;
    527592                }
     
    546611        for (i = 0; i < hid_dev->subdriver_count; ++i) {
    547612                if (hid_dev->subdrivers[i].poll_end != NULL) {
    548                         hid_dev->subdrivers[i].poll_end(hid_dev, reason);
     613                        hid_dev->subdrivers[i].poll_end(hid_dev,
     614                            hid_dev->subdrivers[i].data, reason);
    549615                }
    550616        }
     
    590656/*----------------------------------------------------------------------------*/
    591657
    592 void usb_hid_new_report(void)
    593 {
    594         report_received = false;
    595 }
    596 
    597 /*----------------------------------------------------------------------------*/
    598 
    599 void usb_hid_report_received(void)
    600 {
    601         report_received = true;
    602 }
    603 
    604 /*----------------------------------------------------------------------------*/
    605 
    606 bool usb_hid_report_ready(void)
    607 {
    608         return !report_received;
    609 }
     658void usb_hid_new_report(usb_hid_dev_t *hid_dev)
     659{
     660        ++hid_dev->report_nr;
     661}
     662
     663/*----------------------------------------------------------------------------*/
     664
     665int usb_hid_report_number(usb_hid_dev_t *hid_dev)
     666{
     667        return hid_dev->report_nr;
     668}
     669
     670/*----------------------------------------------------------------------------*/
     671
     672//void usb_hid_report_received(void)
     673//{
     674//      ++report_number;
     675//}
     676
     677/*----------------------------------------------------------------------------*/
     678
     679//bool usb_hid_report_ready(void)
     680//{
     681//      return !report_received;
     682//}
    610683
    611684/*----------------------------------------------------------------------------*/
     
    627700        for (i = 0; i < (*hid_dev)->subdriver_count; ++i) {
    628701                if ((*hid_dev)->subdrivers[i].deinit != NULL) {
    629                         (*hid_dev)->subdrivers[i].deinit(*hid_dev);
     702                        (*hid_dev)->subdrivers[i].deinit(*hid_dev,
     703                            (*hid_dev)->subdrivers[i].data);
    630704                }
    631705        }
Note: See TracChangeset for help on using the changeset viewer.