Saturday 15 September 2012

c++ - How to enumerate an USB hub that is attached to the first port of root hub on Windows Embedded 7 -



c++ - How to enumerate an USB hub that is attached to the first port of root hub on Windows Embedded 7 -

i posting here first time, hope clear , readable.

i trying test presence of usb devices on embedded scheme using specific hcd , port path programmatically using c++ , visual studio 2008.

the thought pass in port number , hcd value parameters of function, , function homecoming true or false indicates connection status.

i have written code populate root hub , prove device attached port 1 of root hub hub using bool deviceishub usbioctl.h.

however, when effort enumerate usb hub attached port 1 of root may test connection status of downstream ports presence of device(ports 1 , 2 of usb hub). not seem know how many downstream ports usb hub has.

i checked usbview/treeview, both application tells me devices there not sure ioctl command code utilize such can enumerate downstream ports can check connection status.

the construction of device based on usb view , usb tree provides following.

root hub - has 7 ports, first port beingness used.

a usb hub (it has 4 available ports) attached first port of root hub.

two usb devices (usb mouse , usb keyboard) attached port 1 , port 2 of usb hub.

i have tried ioctl_usb_get_connection_information, ioctl_usb_get_connection_name, ioctl_usb_get_connection_information_ex, ioctl_usb_port_connector_properties

(which not supported, can used in windows 8, exact ioctl phone call used enumerate ports).

please ignore messageboxes, me check command path status , determine route following.

please find code wrote effort enumerate/populate usb hub. did not include root hub code because create snippet big.

my questions resides in enumeration process of secondary usb hub believe. checked registry key of device. appears usb hub enumerated , nowadays on device since info shown under regedit hklm->system->currentcontrolset->enum->usb. believe not enumerating correctly within test application.

any help appreciated.

update

the part concerned deviceiocontrol calls attempts size , actual name of usb hub.

it takes in usb_get_node_information. other ioctl calls intended retrieve name of hub fail @ first deviceiocontrol attempts size of hub name know how much memory allocate it.

update part 2

from observing usbview open source code, believe need enumerate host controller , devices first before checking presence of device. drew conclusion such without doing enumeration of controller, goes far downwards tree (at best sec layer, external hub attached in case).

i attempting enumerate other devices , controllers in hope can 3rd layer of device. maintain on updating thread until either figure out problem myself or capable of answering questions.

//we connected external hub, can begin configuration , enumeration of external hub. ulong kbytes = 0; usb_hub_name subhubname; //create handle external hub driver char name[16]; wsprintf(name, "\\\\.\\hcd%d", hcdsub); handle subhub = createfile(name, generic_write, file_share_write, null, open_existing, 0, null); //check see if handle created if (subhub == invalid_handle_value) { messagebox(null,"subhandle fail ","test",mb_ok); homecoming false; } //query subhub/external hub structure, tell number of downwards stream ports need enumerate ioctlsuccess = deviceiocontrol(subhub, ioctl_usb_get_node_information, 0, 0, &subhubname, sizeof(subhubname), &kbytes, null); //if command failed, close handle , homecoming false. if(!ioctlsuccess) { closehandle(subhub); messagebox(null," sub hub size fail ","test",mb_ok); homecoming false; } //prepare receive subhubname kbytes = subhubname.actuallength; usb_hub_name *subhubnamew = (usb_hub_name *) malloc(sizeof(usb_hub_name) * kbytes); //check if allocation failed, if did, free memory allocated , homecoming false. if (subhubnamew == null) { free(subhubnamew); closehandle(subhub); messagebox(null,"subhubnamew=null ","test",mb_ok); homecoming false; } //send command retrieve name ioctlsuccess = deviceiocontrol(subhub, ioctl_usb_get_node_information, null, 0, subhubnamew, kbytes, &kbytes, null); //we no longer need handle. closehandle(subhub); if(!ioctlsuccess) { if(subhubnamew !=null) { free(subhubnamew); } messagebox(null,"get node info fail ","test",mb_ok); homecoming false; } //converts subhubname widechar cahr. messagebox(null,"begin convertion","test",mb_ok); kbytes = wcslen(subhubnamew->hubname) + 1; char *subhubname = (char *) malloc(sizeof(char)*kbytes); wcstombs(subhubname,subhubnamew->hubname, kbytes); //we no longer need subhubnamew info in subhubname. if(subhubnamew !=null) { free(subhubnamew); } //attempt open handle driver sub hub. int subhdnsize = strlen(subhubname) + sizeof("\\\\.\\"); char *subhubnamelength = (char *) malloc(sizeof(char) * subhdnsize); sprintf(subhubnamelength, "\\\\.\\%s", subhubname); //we no longer need subhubname, free it. if(subhubname !=null) free(subhubname); //attempt open handle enumerating ports on hub. handle externalhub = createfile(subhubnamelength, generic_write, file_share_write, null, open_existing, 0, null); //we no longer need subhubnamelength, free if not null if(subhubnamelength != null) free(subhubnamelength); //check , see if handle created successfully, if not, homecoming false. if(externalhub == invalid_handle_value) { closehandle(externalhub); messagebox(null,"ext handle fail ","test",mb_ok); homecoming false; } } usb_node_connection_attributes *portconnection = (usb_node_connection_attributes *) malloc(sizeof(usb_node_connection_attributes)); portconnection ->connectionindex = port; ioctlsuccess = deviceiocontrol(externalhub, ioctl_usb_get_node_connection_attributes, portconnection, sizeof(usb_node_connection_attributes), portconnection, sizeof(usb_node_connection_attributes), &kbytes, null); if(!ioctlsuccess) { messagebox(null,"device connection fail ","test",mb_ok); homecoming false; } if(portconnection->connectionstatus !=deviceconnected) { printf("the connection status returns: %d",portconnection->connectionstatus); printf("\n"); homecoming false; }

enumerating usb on windows 7 fail if uac enabled , application doesn't have plenty privilege. windows embedded 7 may fail on similar task.

there possibility enumerate through registry.

c++ windows embedded usb

No comments:

Post a Comment