Ignore:
File:
1 edited

Legend:

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

    r8fb45e08 rfaa44e58  
    6363static const int USB_HID_MAX_SUBDRIVERS = 10;
    6464
    65 /** @todo What happens if this is not fibril local? */
    66 //static fibril_local bool report_number;
     65static fibril_local bool report_received;
    6766
    6867/*----------------------------------------------------------------------------*/
     
    204203       
    205204        usb_log_debug("Compare flags: %d\n", 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        
     205        size_t size = usb_hid_report_input_length(hid_dev->report, usage_path,
     206            mapping->compare);
    218207        usb_log_debug("Size of the input report: %zuB\n", size);
     208       
    219209        usb_hid_report_path_free(usage_path);
    220210       
     
    235225        }
    236226       
    237         // add one generic HID subdriver per device
    238        
    239         hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc((count + 1) *
     227        hid_dev->subdrivers = (usb_hid_subdriver_t *)malloc(count *
    240228            sizeof(usb_hid_subdriver_t));
    241229        if (hid_dev->subdrivers == NULL) {
     
    250238        }
    251239       
    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;
     240        hid_dev->subdriver_count = count;
    258241       
    259242        return EOK;
     
    315298               
    316299                if (matched) {
    317                         usb_log_debug("Subdriver matched.\n");
    318300                        subdrivers[count++] = &mapping->subdriver;
    319301                }
     
    357339/*----------------------------------------------------------------------------*/
    358340
    359 static 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 
    394341usb_hid_dev_t *usb_hid_new(void)
    395342{
     
    446393        /* Get the report descriptor and parse it. */
    447394        rc = usb_hid_process_report_descriptor(hid_dev->usb_dev,
    448             hid_dev->report, &hid_dev->report_desc, &hid_dev->report_desc_size);
     395            hid_dev->report);
    449396       
    450397        bool fallback = false;
     
    510457                        if (hid_dev->subdrivers[i].init != NULL) {
    511458                                usb_log_debug("Initializing subdriver %d.\n",i);
    512                                 rc = hid_dev->subdrivers[i].init(hid_dev,
    513                                     &hid_dev->subdrivers[i].data);
     459                                rc = hid_dev->subdrivers[i].init(hid_dev);
    514460                                if (rc != EOK) {
    515461                                        usb_log_warning("Failed to initialize"
     
    527473        }
    528474       
    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        
    535475        return rc;
    536476}
     
    550490        usb_hid_dev_t *hid_dev = (usb_hid_dev_t *)arg;
    551491       
    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 //      }
     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        }
    578514       
    579515        /*! @todo This should probably be atomic. */
    580516        memcpy(hid_dev->input_report, buffer, buffer_size);
    581517        hid_dev->input_report_size = buffer_size;
    582         usb_hid_new_report(hid_dev);
    583518       
    584519        bool cont = false;
     
    587522        for (i = 0; i < hid_dev->subdriver_count; ++i) {
    588523                if (hid_dev->subdrivers[i].poll != NULL
    589                     && hid_dev->subdrivers[i].poll(hid_dev,
    590                         hid_dev->subdrivers[i].data, buffer, buffer_size)) {
     524                    && hid_dev->subdrivers[i].poll(hid_dev, buffer,
     525                    buffer_size)) {
    591526                        cont = true;
    592527                }
     
    611546        for (i = 0; i < hid_dev->subdriver_count; ++i) {
    612547                if (hid_dev->subdrivers[i].poll_end != NULL) {
    613                         hid_dev->subdrivers[i].poll_end(hid_dev,
    614                             hid_dev->subdrivers[i].data, reason);
     548                        hid_dev->subdrivers[i].poll_end(hid_dev, reason);
    615549                }
    616550        }
     
    656590/*----------------------------------------------------------------------------*/
    657591
    658 void usb_hid_new_report(usb_hid_dev_t *hid_dev)
    659 {
    660         ++hid_dev->report_nr;
    661 }
    662 
    663 /*----------------------------------------------------------------------------*/
    664 
    665 int 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 //}
     592void usb_hid_new_report(void)
     593{
     594        report_received = false;
     595}
     596
     597/*----------------------------------------------------------------------------*/
     598
     599void usb_hid_report_received(void)
     600{
     601        report_received = true;
     602}
     603
     604/*----------------------------------------------------------------------------*/
     605
     606bool usb_hid_report_ready(void)
     607{
     608        return !report_received;
     609}
    683610
    684611/*----------------------------------------------------------------------------*/
     
    700627        for (i = 0; i < (*hid_dev)->subdriver_count; ++i) {
    701628                if ((*hid_dev)->subdrivers[i].deinit != NULL) {
    702                         (*hid_dev)->subdrivers[i].deinit(*hid_dev,
    703                             (*hid_dev)->subdrivers[i].data);
     629                        (*hid_dev)->subdrivers[i].deinit(*hid_dev);
    704630                }
    705631        }
Note: See TracChangeset for help on using the changeset viewer.