[Nexus 4 Q&A, Help & Troubleshooting] vivekb1985:
Hi,

I am trying to develop a simple program to communicate with the Nexus-4 bootloader in firmware update mode.

Nexus -4 has three usb interfaces. Interface-1 has two 2 endpoints - 2 and 131.

I wrote a program to write the command, get-device-info, through endpoint 2 and listen at endpoint 131 for the reply.

(I tried all permutations of interfaces and endpoints!).

The program successfully writes the command to the device but nothing is read from the device. The following is the program.

#include <stdio.h>
#include <stdlib.h>
#include <libusb-1.0/libusb.h>

#define INTERFACE 1
#define EP_OUT 2
#define EP_IN 131

int main() {

libusb_device **devs; // retrieve a list of devices
libusb_device_handle *dev_handle; // device handler
libusb_context *ctx = NULL; //a libusb session

int r, r2, i;
ssize_t cnt; //holding number of devices in list

unsigned char data[30],read_data[512]; //data to write

data[0]=0x7e;data[1]=0x00;data[2]=0x78;data[3]=0xf0;data[4]=0x7e; // get-device-info command in HLDC format

int actual,read_actual;

r = libusb_init(&ctx);

if(r < 0) {
printf("Init Error\n");
return 1;
}

libusb_set_debug(ctx, 3);

cnt = libusb_get_device_list(ctx, &devs); //get the list of devices

if(cnt < 0) {
printf("Get Device Error\n");
return 1;
}
printf("%d Devices in list\n",(int)cnt);

dev_handle = libusb_open_device_with_vid_pid(ctx, 4100, 25371); //these are vendorID and productID I found for Nexus-4 firmware update

if(dev_handle == NULL)
printf("Cannot open device\n");
else
printf("Device opened\n");

libusb_free_device_list(devs, 0); //free the device list

if(libusb_kernel_driver_active(dev_handle, INTERFACE) == 1) { //find out if kernel driver is attached
printf("Kernel Driver Active\n");
if(libusb_detach_kernel_driver(dev_handle, INTERFACE) == 0) //detach it
printf("Kernel Driver Detached!\n");
}

r = libusb_claim_interface(dev_handle, INTERFACE); //claim interface 1 Nexus-5/4 FUM
if(r < 0) {
printf("Cannot Claim Interface\n");
printf("%s\n",libusb_error_name(r));
return 1;
}
printf("Claimed Interface\n");

printf("Data to be send -> %s\n",data); //just to see the data that we are writing
printf("Writing Data...\n");

r = libusb_bulk_transfer(dev_handle, (EP_OUT | LIBUSB_ENDPOINT_OUT), data, 5, &actual, 0);

if(r == 0 && actual == 5){ //we wrote successfully 5 bytes to the device
printf("Writing Successful!\n");

printf("Waiting to read from device!\n");
r2 = libusb_bulk_transfer(dev_handle, (EP_IN | LIBUSB_ENDPOINT_IN), read_data, 512, &read_actual, 5000);

if (r2 >=0){
if (read_actual > 0){
printf("Data received by bulk transfer\n");

printf("Data is ");
for (i=0; i<read_actual; i++)
printf("%x ",read_data[i]);
printf("\n");
}
else{
printf(stderr, "No data received in bulk transfer (%d)\n", r2);
return -1;
}
}
else{
fprintf(stderr, "Error receiving data via bulk transfer %d\n", r2);
return r2;
}
}
else
printf("Write Error\n");

r = libusb_release_interface(dev_handle, INTERFACE); //release the claimed interface
if(r!=0) {
printf("Cannot Release Interface\n");
return 1;
}
printf("Released Interface\n");

libusb_close(dev_handle);
libusb_exit(ctx);

return 0;
}

And when I run the program, I get the following output:

----------------------------------------------------------------------

10 Devices in list

Device opened

Kernel Driver Active

Kernel Driver Detached!

Claimed Interface

Data to be send -> ~

Writing Data...

Writing Successful!

Waiting to read from device!

Error receiving data via bulk transfer -7

---------------------------------------------------------------------

Can someone tell me what am I doing wrong in the program?