int main(int argc, char **argv) { int r; int config = 0; cyusb_handle *h = NULL; char tbuf[60]; struct libusb_config_descriptor *desc = NULL; r = cyusb_open(); if ( r < 0 ) { printf("Error opening library\n"); return -1; } else if ( r == 0 ) { printf("No device found\n"); return 0; } h = cyusb_gethandle(0); r = cyusb_get_configuration(h,&config); if ( r ) { cyusb_error(r); cyusb_close(); return r; } if ( config == 0 ) printf("The device is currently unconfigured\n"); else printf("Device configured. Current configuration = %d\n", config); r = cyusb_get_active_config_descriptor(h, &desc); if ( r ) { printf("Error retrieving config descriptor\n"); return r; } sprintf(tbuf,"bLength = %d\n", desc->bLength); printf("%s",tbuf); sprintf(tbuf,"bDescriptorType = %d\n", desc->bDescriptorType); printf("%s",tbuf); sprintf(tbuf,"TotalLength = %d\n", desc->wTotalLength); printf("%s",tbuf); sprintf(tbuf,"Num. of interfaces = %d\n", desc->bNumInterfaces); printf("%s",tbuf); sprintf(tbuf,"bConfigurationValue = %d\n", desc->bConfigurationValue); printf("%s",tbuf); sprintf(tbuf,"iConfiguration = %d\n", desc->iConfiguration); printf("%s",tbuf); sprintf(tbuf,"bmAttributes = %d\n", desc->bmAttributes); printf("%s",tbuf); sprintf(tbuf,"Max Power = %04d\n", desc->MaxPower); printf("%s",tbuf); cyusb_close(); return 0; }
static void handle_sigusr2(int signo) { unlink(pidfile); close(logfd); cyusb_close(); exit(0); }
int HostIO_CyUSB::Close() { m_log->Write(2, _T("Close")); cyusb_release_interface(h, 0); cyusb_close(); m_log->Write(2, _T("Close Done.") ); m_log->Close(); // flush to log file m_log->TestForLogging(); // turn off logging if appropriate return ALL_OK; }
static void handle_sigusr1(int signo) { int N; printf("Signal SIGUSR1 received !\n"); cyusb_close(); N = cyusb_open(); if ( N < 0 ) { printf("Error in opening library\n"); exit(-1); } else if ( N == 0 ) { printf("No device of interest found\n"); exit(0); } else printf("No of devices of interest found = %d\n",N); }
int HostIO_CyUSB::ListDevices(std::vector<CameraID> & vID ) { int numDevs; USHORT VID; USHORT PID; m_log->Write(2, _T("ListDevices started.")); numDevs = cyusb_open(); for (int i = 0; i < (int)numDevs; i++) { h = cyusb_gethandle(i); VID = cyusb_getvendor(h); PID = cyusb_getproduct(h); std::string SerialNum = std::string("None"); GetSerialNumber(SerialNum); std::string SerialToOpen = SerialNum; std::string Desc = std::string("None"); GetDesc(Desc); m_log->Write(2, _T("Dev %d:"), i); m_log->Write(2, _T(" SerialNumber=%s"), SerialNum.c_str()); m_log->Write(2, _T(" Description=%s"), Desc.c_str()); if (VID == QSICyVID && PID == QSICyPID && !SerialNum.empty() ) { m_log->Write(2, _T("USB ListDevices found QSI Cy device at index: %d, Serial: %s, Description: %s"), i, SerialNum.c_str(), Desc.c_str() ); CameraID id(SerialNum, SerialToOpen, Desc, VID, PID, CameraID::CP_CyUSB); vID.push_back(id); } } cyusb_close(); m_log->Write(2, _T("USB ListDevices Done. Number of devices found: %d"), numDevs); return S_OK; }
static void * writer(void *arg2) { int r, nbr; unsigned char buf[64]; int transferred = 0; memset(buf,'\0',64); while ( nbr = read(0,buf,64) ) { r = cyusb_bulk_transfer(h1, 0x02, buf, nbr, &transferred, timeout * 1000); if ( r == 0 ) { memset(buf,'\0',64); continue; } else { cyusb_error(r); cyusb_close(); return NULL; } } }
static void *reader(void *arg1) { int r; unsigned char buf[64]; int transferred = 0; memset(buf,'\0',64); while (1) { r = cyusb_bulk_transfer(h1, 0x86, buf, 64, &transferred, timeout * 1000); if ( r == 0 ) { printf("%s", buf); memset(buf,'\0',64); continue; } else { cyusb_error(r); cyusb_close(); return NULL; } } }
int main(int argc, char **argv) { int r; char user_input = 'n'; pthread_t tid1, tid2; program_name = argv[0]; while ( (next_option = getopt_long(argc, argv, short_options, long_options, NULL) ) != -1 ) { switch ( next_option ) { case 'h': /* -h or --help */ print_usage(stdout, 0); case 'v': /* -v or --version */ printf("%s (Ver 1.0)\n",program_name); printf("Copyright (C) 2012 Cypress Semiconductors Inc. / ATR-LABS\n"); exit(0); case 't': /* -t or --timeout */ timeout_provided = 1; timeout = atoi(optarg); break; case '?': /* Invalid option */ print_usage(stdout, 1); default : /* Something else, unexpected */ abort(); } } validate_inputs(); r = cyusb_open(); if ( r < 0 ) { printf("Error opening library\n"); return -1; } else if ( r == 0 ) { printf("No device found\n"); return 0; } if ( r > 1 ) { printf("More than 1 devices of interest found. Disconnect unwanted devices\n"); return 0; } h1 = cyusb_gethandle(0); if ( cyusb_getvendor(h1) != 0x04b4 ) { printf("Cypress chipset not detected\n"); cyusb_close(); return 0; } r = cyusb_kernel_driver_active(h1, 0); if ( r != 0 ) { printf("kernel driver active. Exitting\n"); cyusb_close(); return 0; } r = cyusb_claim_interface(h1, 0); if ( r != 0 ) { printf("Error in claiming interface\n"); cyusb_close(); return 0; } else printf("Successfully claimed interface\n"); r = pthread_create(&tid1, NULL, reader, NULL); r = pthread_create(&tid2, NULL, writer, NULL); while ( 1) { pause(); } cyusb_close(); return 0; }
int main(int argc, char **argv) { int rStatus; libusb_config_descriptor *configDesc; const struct libusb_endpoint_descriptor *endpoint; const struct libusb_interface *interface0 ; int num_bytes, maxPacketSize, numEndpoints, ep_index_out = 0, ep_index_in = 0; unsigned short int temp, ep_in[8], ep_out[8]; if (argc != 2){ printf ("Usage is ./cybulk_reader <num of bytes> \n Ex ./cybulk_reader 1024 \n"); return -1; } rStatus = cyusb_open(); if ( rStatus < 0 ) { printf("Error opening library\n"); cyusb_close(); return -1; } else if ( rStatus == 0 ) { printf("No device found\n"); cyusb_close(); return 0; } num_bytes = atoi (argv[1]); printf ("Number of bytes is %d ", num_bytes); h1 = cyusb_gethandle(0); rStatus = cyusb_kernel_driver_active(h1, 0); if ( rStatus != 0 ) { printf("kernel driver active. Exitting\n"); cyusb_close(); return 0; } rStatus = cyusb_claim_interface(h1, 0); if ( rStatus != 0 ) { printf("Error in claiming interface\n"); cyusb_close(); return 0; } rStatus = cyusb_get_config_descriptor (h1, 0, &configDesc); if (rStatus != 0){ printf ("Could not get Config descriptor \n"); cyusb_close(); return -1; } //Finding the endpoint address interface0 = configDesc->interface; numEndpoints = interface0->altsetting->bNumEndpoints; endpoint = interface0->altsetting->endpoint; while (numEndpoints){ if (endpoint->bEndpointAddress & LIBUSB_ENDPOINT_IN){ ep_in [ep_index_in] = endpoint->bEndpointAddress; ep_index_in++; } else{ ep_out [ep_index_out] = endpoint->bEndpointAddress; ep_index_out++; } numEndpoints --; endpoint = ((endpoint) + 1); } //Choosing 1 endpoint for read and one for write if (ep_in[0] == 0){ printf ("No IN endpoint in the device ... Cannot do bulk write\n"); cyusb_free_config_descriptor (configDesc); cyusb_close(); return -1; } gl_ep_in = ep_in [0]; gl_ep_out = ep_out [0]; printf ("The Endpoint address is 0x%x \n", gl_ep_in); maxPacketSize = cyusb_get_max_packet_size (h1, gl_ep_in); if (((num_bytes % maxPacketSize) + 1) != 1){ printf ("Number of bytes to read should be multiple of %d--The EP max packetsize \n", maxPacketSize); cyusb_free_config_descriptor(configDesc); cyusb_close(); return -1; } bulk_reader (num_bytes); cyusb_free_config_descriptor(configDesc); cyusb_close(); return 0; }
int main(int argc, char **argv) { int N; int pid; char tbuf[50]; int r; N = cyusb_open(); if ( N < 0 ) { printf("Error in opening library\n"); return -1; } else if ( N == 0 ) { printf("No device of interest found\n"); return 0; } else printf("No of devices of interest found = %d\n",N); logfd = open(logfile, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR ); if ( logfd < 0 ) { printf("Error opening log file for output %s\n",logfile); cyusb_close(); return -2; } program_name = argv[0]; while ( (next_option = getopt_long(argc, argv, short_options, long_options, NULL) ) != -1 ) { switch ( next_option ) { case 'h': /* -h or --help */ print_usage(stdout, 0); case 'v': /* -v or --version */ printf("cyusbd (Ver 1.0)\n"); printf("Copyright (C) 2012 Cypress Semiconductors / ATR-LABS\n"); exit(0); case '?': /* Invalid option */ print_usage(stdout, 1); default : /* Something else, unexpected */ cyusb_close(); abort(); } } validate_inputs(); pid = getpid(); pidfd = open(pidfile, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR ); if ( pidfd < 0 ) { printf("Error opening pid file for output %s\n",pidfile); cyusb_close(); return -3; } else { memset(tbuf,' ',50); sprintf(tbuf,"%d\n",pid); write(pidfd, tbuf, strlen(tbuf)); close(pidfd); } signal(SIGUSR1,handle_sigusr1); /* Signal to handle events received from the kernel */ signal(SIGUSR2,handle_sigusr2); /* Signal to stop this daemon and exit gracefully */ signal(SIGINT, handle_sigusr2); /* Ctrl_C will also stop this daemon and exit gracefully */ while (1) { pause(); } return 0; }
int main(int argc, char **argv) { int r; struct libusb_device_descriptor desc; char user_input = 'n'; program_name = argv[0]; while ( (next_option = getopt_long(argc, argv, short_options, long_options, NULL) ) != -1 ) { switch ( next_option ) { case 'h': /* -h or --help */ print_usage(stdout, 0); case 'v': /* -v or --version */ printf("%s (Ver 1.0)\n",program_name); printf("Copyright (C) 2012 Cypress Semiconductors Inc. / ATR-LABS\n"); exit(0); case 'o': /* -o or --output */ out_filename = optarg; fp = fopen(out_filename,"a"); if ( !fp ) print_usage(stdout, 1); break; case 'V': /* -V or --vendor */ vendor_provided = 1; vid = strtoul(optarg,NULL,16); break; case 'P': /* -P or --Product */ product_provided = 1; pid = strtoul(optarg,NULL,16); break; case '?': /* Invalid option */ print_usage(stdout, 1); default : /* Something else, unexpected */ abort(); } } validate_inputs(); if ( (vendor_provided) && (product_provided) ) user_input = 'y'; else user_input = 'n'; if ( user_input == 'y' ) { r = cyusb_open(vid, pid); if ( r < 0 ) { printf("Error opening library\n"); return -1; } else if ( r == 0 ) { printf("No device found\n"); return 0; } } else { r = cyusb_open(); if ( r < 0 ) { printf("Error opening library\n"); return -1; } else if ( r == 0 ) { printf("No device found\n"); return 0; } } r = cyusb_get_device_descriptor(cyusb_gethandle(0), &desc); if ( r ) { printf("error getting device descriptor\n"); return -2; } fprintf(fp,"bLength = %d\n", desc.bLength); fprintf(fp,"bDescriptorType = %d\n", desc.bDescriptorType); fprintf(fp,"bcdUSB = 0x%04x\n", desc.bcdUSB); fprintf(fp,"bDeviceClass = 0x%02x\n", desc.bDeviceClass); fprintf(fp,"bDeviceSubClass = 0x%02x\n", desc.bDeviceSubClass); fprintf(fp,"bDeviceProtocol = 0x%02x\n", desc.bDeviceProtocol); fprintf(fp,"bMaxPacketSize = %d\n", desc.bMaxPacketSize0); fprintf(fp,"idVendor = 0x%04x\n", desc.idVendor); fprintf(fp,"idProduct = 0x%04x\n", desc.idProduct); fprintf(fp,"bcdDevice = 0x%04x\n", desc.bcdDevice); fprintf(fp,"iManufacturer = %d\n", desc.iManufacturer); fprintf(fp,"iProduct = %d\n", desc.iProduct); fprintf(fp,"iSerialNumber = %d\n", desc.iSerialNumber); fprintf(fp,"bNumConfigurations = %d\n", desc.bNumConfigurations); cyusb_close(); return 0; }
int main ( int argc, char **argv) { cyusb_handle *h; const char *filename = NULL; const char *tgt_str = NULL; fx2_fw_tgt_p tgt = FW_TARGET_NONE; FILE *fp1 = NULL; FILE *fp2 = NULL; char buf[256]; char tbuf1[3]; char tbuf2[5]; char tbuf3[3]; unsigned char reset = 0; int r; unsigned char num_bytes = 0; unsigned short address = 0; unsigned char *dbuf = NULL; int i; /* Parse command line arguments. */ for (i = 1; i < argc; i++) { if ((strcasecmp (argv[i], "-h") == 0) || (strcasecmp (argv[i], "--help") == 0)) { fx2_dnld_print_usage (argv[0]); return 0; } else { if ((strcasecmp (argv[i], "-t") == 0) || (strcasecmp (argv[i], "--target") == 0)) { if (argc > (i + 1)) { tgt_str = argv[i + 1]; if (strcasecmp (argv[i + 1], "ram") == 0) tgt = FW_TARGET_RAM; if (strcasecmp (argv[i + 1], "si2c") == 0) tgt = FW_TARGET_SM_I2C; if (strcasecmp (argv[i + 1], "li2c") == 0) tgt = FW_TARGET_LR_I2C; if (tgt == FW_TARGET_NONE) { fprintf (stderr, "Error: Unknown target %s\n", argv[i + 1]); fx2_dnld_print_usage (argv[0]); return -EINVAL; } } i++; } else { if ((strcmp (argv[i], "-i") == 0) || (strcmp (argv[i], "--image") == 0)) { if (argc > (i + 1)) filename = argv[i + 1]; i++; } else { fprintf (stderr, "Error: Unknown parameter %s\n", argv[i]); fx2_dnld_print_usage (argv[0]); return -EINVAL; } } } } if ((filename == NULL) || (tgt == FW_TARGET_NONE)) { fprintf (stderr, "Error: Firmware binary or target not specified\n"); fx2_dnld_print_usage (argv[0]); return -EINVAL; } r = cyusb_open (); if (r < 0) { fprintf (stderr, "Error opening library\n"); return -ENODEV; } else if (r == 0) { fprintf (stderr, "Error: No FX2LP device found\n"); return -ENODEV; } else if (r > 1) { fprintf (stderr, "Error: More than one Cypress device found\n"); return -EINVAL; } h = cyusb_gethandle(0); switch (tgt) { case FW_TARGET_RAM: /* Assuming an external RAM download for now. This works for internal download as well. */ r = fx2_ram_download (h, filename, 1); break; case FW_TARGET_SM_I2C: r = fx2_eeprom_download (h, filename, 0); break; case FW_TARGET_LR_I2C: r = fx2_eeprom_download (h, filename, 1); break; default: break; } if (r != 0) { fprintf (stderr, "Error: FX2LP firmware programming failed\n"); } else { printf ("FX2LP firmware programming to %s completed\n", tgt_str); } cyusb_close (); return 0; }
/* Get the handle to the FX3 flash programmer device, if found. */ static int get_fx3_prog_handle ( cyusb_handle **h) { char *progfile_p, *tmp; cyusb_handle *handle; int i, j, r; struct stat filestat; handle = *h; r = check_fx3_flashprog (handle); if (r == 0) return 0; printf ("Info: Trying to download flash programmer to RAM\n"); tmp = getenv ("CYUSB_ROOT"); if (tmp != NULL) { i = strlen (tmp); progfile_p = (char *)malloc (i + 32); strcpy (progfile_p, tmp); strcat (progfile_p, "/fx3_images/cyfxflashprog.img"); } else { progfile_p = (char *)malloc (32); strcpy (progfile_p, "fx3_images/cyfxflashprog.img"); } r = stat (progfile_p, &filestat); if (r != 0) { fprintf (stderr, "Error: Failed to find cyfxflashprog.img file\n"); return -1; } r = fx3_usbboot_download (handle, progfile_p); free (progfile_p); if (r != 0) { fprintf (stderr, "Error: Failed to download flash prog utility\n"); return -1; } cyusb_close (); *h = NULL; // Now wait for the flash programmer to enumerate, and get a handle to it. for (j = 0; j < GETHANDLE_TIMEOUT; j++) { sleep (1); r = cyusb_open (); if (r > 0) { for (i = 0; i < r; i++) { handle = cyusb_gethandle (i); if (cyusb_getvendor (handle) == FLASHPROG_VID) { r = check_fx3_flashprog (handle); if (r == 0) { printf ("Info: Got handle to FX3 flash programmer\n"); *h = handle; return 0; } } } cyusb_close (); } } fprintf (stderr, "Error: Failed to get handle to flash programmer\n"); return -2; }