void init_mouse_jm2() { libusb_context *context = init_libusb ("mouse_jm2"); if (context == NULL) { return; } libusb_set_debug(context, 3); handle = open_device("mouse_jm2", VID, PID); if (handle != NULL) { transfer = start_usb_interrupt_transfer(handle, EPT, data_callback, NULL, -1, 0); } }
int main(int argc, char *argv[]) { printf("Headsetcontrol written by Sapd (Denis Arnst)\n\thttps://github.com/Sapd\n"); if (argc != 2) { goto usage; } int loudness = strtol(argv[1], NULL, 10); if (loudness > 128 || loudness < 0) goto usage; if (init_libusb()) return 1; libusb_device* device = find_device(); if (!device) { printf("Couldn't find device"); free_libusb(); return 1; } if (open_device(device)) { printf("Couldn't claim Device"); close_device(); free_libusb(); return 1; } send_sidetone(loudness); close_device(); free_libusb(); return 0; usage: printf("Usage: %s 0-128\n\t(0 silent, 128 very loud)\n", argv[0]); return 0; }
/* * ----------------------------------------------------------------------------- * Name : usb_custom * Purpose : set all fonction pointer for usb mode * Inputs : p : port structure * Outputs : <> * Return : <> * ----------------------------------------------------------------------------- */ void usb_custom(aps_class_t *p) { if (p == NULL) return; p->port.type=APS_USB; p->get_uri = usb_get_uri; p->create = usb_create; p->create_from_uri = usb_create_from_uri; p->open_ = usb_open; p->close = usb_close; p->write = usb_write; p->read = usb_read; p->sync = usb_sync; p->flush = usb_flush; if (libusb_ctx) { if (devs) libusb_free_device_list(devs, 1); libusb_exit(NULL); devs = 0; devcnt = 0; libusb_ctx = 0; } if (libusb_ctx == 0) { init_libusb(); } p->port.set.usb.pdev = 0; p->port.set.usb.was_kernel_driver_attached = 0; p->port.set.usb.pdev = 0; p->port.set.usb.hdev = 0; }
/*----------------------------------------------------------------------------- * Name : usb_list_ports * Purpose : List available USB ports * Inputs : p : array of port structures * max : maximum number of ports in list * Outputs : <> * Return : Number of ports or error code * -----------------------------------------------------------------------------*/ int usb_list_ports(aps_port_t **p,int max) { const char *usbfs = USBFS; int n; int i; int total; libusb_device * dev; if (libusb_ctx == 0) { init_libusb(); } /*list connected devices*/ n = devcnt; if (n <= 0) { return 0; } /*build ports from APS devices*/ total = 0; i = 0; while (total < max && (dev = devs[i]) != NULL) { struct libusb_device_descriptor desc; int r; r = libusb_get_device_descriptor(dev, &desc); if (r < 0) { fprintf(stderr, "ERROR: failed to get device descriptor"); libusb_free_device_list(devs, 1); libusb_exit(NULL); devs = 0; devcnt = 0; libusb_ctx = 0; return APS_USB_DEVICE_NOT_FOUND; } if (desc.idVendor == APS_VENDOR_ID || desc.idVendor == APS_VENDOR_ID0) { #ifdef DEBUG fprintf(stderr, "DEBUG: ok, aps printer found...\n"); #endif *p = calloc(1, sizeof(aps_class_t)); usb_custom(*p); (*p)->set.usb.pdev = dev; p ++; total ++; } i ++; } #if 0 for (i=0; i<n && total<max; i++) { libusb_device *dev = devs + i; if (dev->vendor_id==APS_VENDOR_ID || dev->vendor_id==APS_VENDOR_ID0) { *p = aps_create_usb_port_from_address(usbfs,dev->busnum,dev->devnum); if (*p!=NULL) { p++; total++; } } } #endif return total; }
int ldm_init() { int ret=0, i=0; struct libusb_device **devs=NULL; struct libusb_device *dev; struct libusb_device_handle *devhandle = NULL; struct libusb_device_descriptor desc; struct libusb_config_descriptor *conf_desc; const struct libusb_interface *dev_interface; const struct libusb_interface_descriptor *altsetting; int scan_result = 0; if (camera == NULL){ ret = init_libusb(&ctxt); if (ret) { TRACE("libusb_init failed\n"); return -1; } //scan and detect GEO devices if (libusb_get_device_list(ctxt, &devs) < 0) { TRACE("libusb_get_device_list error\n"); return -1; } while ((dev = devs[i++]) != NULL) { int data[2] = {-1, -1}; ret = libusb_get_device_descriptor(dev, &desc); if (ret < 0) continue; ret = libusb_get_config_descriptor_by_value(dev, 1, &conf_desc); if(ret < 0) continue; dev_interface = conf_desc->interface; altsetting = dev_interface->altsetting; /* We are only interested in devices whose first USB class is * - a Vendor specific class * - a UVC class * */ if (altsetting->bInterfaceClass != VENDOR_SPECIFIC && altsetting->bInterfaceClass != CC_VIDEO) { libusb_free_config_descriptor(conf_desc); continue; } /* Open the device to communicate with it */ ret = libusb_open(dev, &devhandle); if (ret < 0) { continue; } ret = libusb_control_transfer(devhandle, /* bmRequestType */ (LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_INTERFACE), /* bRequest */ CMD_WHO_R_U, /* wValue */ 0, /* MSB 4 bytes */ /* wIndex */ 0, /* Data */ (unsigned char *)&data, /* wLength */ 8, /* timeout*/ LIBUSB_CMD_TIMEOUT ); switch(data[0]) { case MAX64380: case MAX64480: case MAX64580: //got the Geo camera scan_result = 1; break; default: break; } if(scan_result == 1){ if(data[1] != 0) scan_result = 0; } if(scan_result == 1) break; else libusb_close(devhandle); } if(scan_result == 1) camera = devhandle; else { TRACE("ERR: Opening camera failed\n"); return -1; } //camera = libusb_open_device_with_vid_pid(ctxt, 0x0b6a, 0x4d52); if (camera == NULL) { TRACE("ERR: Opening camera failed\n"); return -1; } TRACE("ldm init done\n"); } unsigned int data[4] = {0}; // read csr registers ret = ldm_qcc_read(QCC_BID_PMU, 0x08, 2, &data[0]); ret = ldm_qcc_read(QCC_BID_PMC, 0x14, 1, &data[1]); ret = ldm_qcc_read(QCC_BID_PMC, 0x15, 1, &data[2]); ret = ldm_qcc_read(QCC_BID_PMC, 0x16, 1, &data[3]); YUVUtil_Init2(data[0], data[1], data[2], data[3]); int k, l, m; for(k=0; k < NUM_MUX_VID_CHANNELS; k++) { for(l=0; l < NUM_OVERLAY_TEXT_IDX; l++) { chbidx[k][l] = 0; } for(m=0; m < NUM_OVERLAY_IMAGE_IDX; m++) { choidx[k][m] = 0; } } return ret; }
/* Initialize the audio path */ int mxuvc_audio_init(const char *backend, const char *options) { RECORD("\"%s\", \"%s\"", backend, options); struct libusb_device *dev = NULL; int ret=0, i, config; uint16_t vendor_id=0xdead, product_id=0xbeef; char *str=NULL, *opt, *value; int audio_sampling_rate; TRACE("Initializing the audio\n"); /* Check that the correct video backend was requested*/ if(strncmp(backend, "libusb-uac", 10)) { ERROR(-1, "The audio backend requested (%s) does not match " "the implemented one (libusb-uac)", backend); } /* Set init parameters to their default values */ packets_per_transfer = PACKETS_PER_TRANSFER_DEFAULT; num_transfers = NUM_TRANSFERS_DEFAULT; audio_duration_ms = AUDIO_DURATION_MS_DEFAULT; audio_sampling_rate = AUDIO_SAMPLING_RATE_DEFAULT; /* Copy the options string to a new buffer since next_opt() needs * non const strings and options could be a const string */ if(options != NULL) { str = (char*)malloc(strlen(options)+1); strncpy(str, options, strlen(options)); *(str + strlen(options)) = '\0'; } /* Get backend option from the option string */ ret = next_opt(str, &opt, &value); while(ret == 0) { if(strncmp(opt, "vid", 3) == 0) { vendor_id = (uint16_t) strtoul(value, NULL, 16); } else if(strncmp(opt, "pid", 3) == 0) { product_id = (uint16_t) strtoul(value, NULL, 16); } else if(strncmp(opt, "packets_per_transfer", 19) == 0) { packets_per_transfer = (unsigned int) strtoul(value, NULL, 10); } else if(strncmp(opt, "num_transfers", 12) == 0) { num_transfers = (unsigned int) strtoul(value, NULL, 10); } else if(strncmp(opt, "audio_duration_ms", 17) == 0) { audio_duration_ms = (unsigned int) strtoul(value, NULL, 10); } else if (strncmp (opt, "audio_sampling_rate", 19) == 0) { audio_sampling_rate = (unsigned int) strtoul (value, NULL, 10); } else { WARNING("Unrecognized option: '%s'", opt); } ret = next_opt(NULL, &opt, &value); } /* Display the values we are going to use */ TRACE("Using vid = 0x%x\n", vendor_id); TRACE("Using pid = 0x%x\n", product_id); TRACE("Using packets_per_transfer = %i\n", packets_per_transfer); TRACE("Using num_transfers = %i\n", num_transfers); TRACE("Using audio_duration_ms = %i\n", audio_duration_ms); TRACE("Using audio_sampling_rate = %i\n", audio_sampling_rate); /* Free the memory allocated to parse 'options' */ if(str) free(str); /* Initialize the backend */ aud_started = 0; audio_disconnected = 0; ret = init_libusb(&audio_ctx); if(ret < 0) return -1; audio_hdl = libusb_open_device_with_vid_pid(audio_ctx, vendor_id, product_id); CHECK_ERROR(audio_hdl == NULL, -1, "Could not open USB device " "%x:%x", vendor_id, product_id); dev = libusb_get_device(audio_hdl); if(dev == NULL) { printf("Unexpected error: libusb_get_device returned a NULL " "pointer."); mxuvc_audio_deinit(); return -1; } /* Get active USB configuration */ libusb_get_configuration(audio_hdl, &config); /* Parse USB decriptors from active USB configuration * to get all the UVC/UAC info needed */ ret = aparse_usb_config(dev, config); if(ret < 0){ mxuvc_audio_deinit(); return -1; } /* Initialize audio */ /* Claim audio control interface */ /* Check if a kernel driver is active on the audio control interface */ ret = libusb_kernel_driver_active(audio_hdl, aud_cfg.ctrlif); if(ret < 0) printf("Error: libusb_kernel_driver_active failed %d\n", ret); if(ret == 1) { TRACE("Detach the kernel driver...\n"); /* If kernel driver is active, detach it so that we can claim * the interface */ ret = libusb_detach_kernel_driver(audio_hdl, aud_cfg.ctrlif); if(ret < 0) printf("Error: libusb_detach_kernel_driver failed " "%d\n", ret); } /* Claim audio control interface */ ret = libusb_claim_interface(audio_hdl, aud_cfg.ctrlif); if(ret < 0) { printf("Error: libusb_claim_interface failed %d\n", ret); } /* Claim audio streaming interface */ /* Check if a kernel driver is active on the audio interface */ ret = libusb_kernel_driver_active(audio_hdl, aud_cfg.interface); if(ret < 0) printf("Error: libusb_kernel_driver_active failed %d\n", ret); if(ret == 1) { TRACE("Detach the kernel driver...\n"); /* If kernel driver is active, detach it so that we can claim * the interface */ ret = libusb_detach_kernel_driver(audio_hdl, aud_cfg.interface); if(ret < 0) printf("Error: libusb_detach_kernel_driver failed " "%d\n",ret); } /* Claim audio streaming interface */ ret = libusb_claim_interface(audio_hdl, aud_cfg.interface); if(ret < 0) { printf("Error: libusb_claim_interface failed %d\n",ret); } /* Select sampling rate */ for(i=0;i<MAX_AUD_FMTS;i++) { if(aud_cfg.format[i].samFr == audio_sampling_rate){ aud_cfg.fmt_idx = i; break; } CHECK_ERROR(i == MAX_AUD_FMTS-1, -1, "Unable to set the sampling rate to %i", audio_sampling_rate); } /* Map default UAC format to Audio format */ cur_aud_format = AUD_FORMAT_PCM_RAW; /* Get min, max and real unit id for ctrl */ AUDIO_CTRL *ctrl = uac_controls; int16_t min = 0, max = 0; uint16_t res = 0; while(ctrl->id != CTRL_NONE) { switch(ctrl->unit) { TRACE(">>>>>id:%d unit:%d\n", ctrl->id,ctrl->unit); case FEATURE: ctrl->unit = aud_cfg.ctrl_feature; break; default: ERROR(-1, "Unsupported control unit (%i) for " "audio control %i", ctrl->unit, ctrl->id); } if (ctrl->id == CTRL_MUTE) { ctrl++; continue; } ret = get_ctrl(ctrl->id, GET_MIN, (void*) &min); CHECK_ERROR(ret < 0, -1, "Unable to get min (GET_MIN) for audio " "control: id=%i, cs=%i, cn=%i.", ctrl->id, ctrl->cs, ctrl->cn); ctrl->min = min; ret = get_ctrl(ctrl->id, GET_MAX, (void*) &max); CHECK_ERROR(ret < 0, -1, "Unable to get max (GET_MAX) for audio " "control: id=%i, cs=%i, cn=%i.", ctrl->id, ctrl->cs, ctrl->cn); ctrl->max = max; ret = get_ctrl(ctrl->id, GET_RES, (void*) &res); CHECK_ERROR(ret < 0, -1, "Unable to get res (GET_RES) for audio " "control: id=%i, cs=%i, cn=%i.", ctrl->id, ctrl->cs, ctrl->cn); ctrl->res = res; ctrl++; } /* Register removal USB event*/ register_libusb_removal_cb((libusb_pollfd_removed_cb) audio_removed, audio_hdl); /* Start event thread/loop */ ret = start_libusb_events(); if(ret < 0) return -1; audio_initialized = 1; return 0; }