One of the USB series: List your USB devices

Source: Internet
Author: User

USB has become one of the indispensable interfaces of PC, almost all devices can be connected to USB devices, USB keyboard, mouse, printer, camera, and commonly used U disk and so on, starting from this article, will focus on the use of USB devices in Dos method, There will be several temporary difficult to set, write where to calculate where it, 三、四篇 is always indispensable.
This article describes how to use the knowledge described in my previous article to find the USB device in your machine and determine the device type.
A USB system generally consists of a USB host (host), one or more USB hubs (hub, but not a local area network hub) and one or more USB device nodes (node), a system with only one host, The USB in our PC is actually host and hub two parts, your PC may have 4 USB ports, in fact, a host, a hub,hub for you to provide 4 ports, we plug in the USB port of the device, generally USB devices, such as U disk, USB printer, etc., Of course we can also plug in a hub to make one of your USB ports expand into multiple.
In fact, we say that the use of USB in DOS, is the USB system host programming management, according to the USB specification, the host will be connected to the above hub and USB device management, do not worry about us. The Host device currently has three specifications, OHCI (Open Host controller Interface), UHCI (Universal Host controller Interface) support USB1.1,EHCI (enhanced Host Controller Interface) supports USB2.0, and in future articles we will focus on OHCI and EHCI.
Learn USB programming, read specifications are indispensable, the following are some should read the specification download:
OHCI Specification:http://blog.hengch.com/specification/usb_ohci_r10a.pdf
EHCI Specification:http://blog.hengch.com/specification/usb_ehci_r10.pdf
USB Specification 1.1:http://blog.hengch.com/specification/usb_spec11.pdf
USB Specification 2.0:http://blog.hengch.com/specification/usb_spec20.pdf
The content described in this article does not require a learning specification.

Below to get to the point, list your USB device, USB host is hooked up on the PCI bus, so through the traversal of the PCI device can find all the USB devices on your machine, in the previous introduction of PCI configuration space, I've introduced a three-byte categorical code field in the configuration space (if you don't know, see my previous blog post"Traverse PCI Device"), in the byte offset to 0x0b is called the Basic classification code, in the offset of 0x0a bytes called sub-classification code, in the offset of 0x09 bytes called Programming Interface code, for the USB device class said, the basic classification code is 0x0c, sub-classification code 0x03, For different specifications of the host device, the programming interface code is different, UHCI programming interface code is 0X00,OHCI programming interface code is 0X10,EHCI programming interface code is 0x20, I want to understand these is enough.
The source programs for USB devices are listed below.

#include <stdio.h>#include<stdlib.h>#include<dpmi.h>typedef unsignedLongUdword;typedef Short intword;typedef unsigned Short intuword;typedef unsignedCharUbyte;typedef Union {struct{Udword edi;    Udword ESI;    Udword EBP;    Udword Res;    Udword ebx;    Udword edx;    Udword ecx;  Udword eax;  } D; struct{Uword di, di_hi;    Uword Si, Si_hi;    Uword BP, Bp_hi;    Uword Res, Res_hi;    Uword BX, Bx_hi;    Uword DX, Dx_hi;    Uword CX, Cx_hi;    Uword Ax, Ax_hi;    Uword flags;    Uword es;    Uword ds;    Uword FS;    Uword GS;    Uword IP;    Uword CS;    Uword sp;  Uword SS;  } x; struct{UBYTE edi[4]; UBYTE esi[4]; UBYTE ebp[4]; UBYTE res[4];    UBYTE bl, BH, ebx_b2, ebx_b3;    UBYTE DL, DH, edx_b2, edx_b3;    UBYTE cl, ch, ecx_b2, ecx_b3;  UBYTE al, Ah, eax_b2, eax_b3; } h;} X86_regs;/************************************************************* * Excute soft interrupt in real mode *************** **********************************************/intX86_int (intInt_num, X86_regs *X86_reg)  {__dpmi_regs d_regs; intReturn_value; D_regs.d.edi= x86_reg->D.edi; D_regs.d.esi= x86_reg->D.esi; D_REGS.D.EBP= x86_reg->D.EBP; D_regs.d.res= x86_reg->D.res; D_REGS.D.EBX= x86_reg->D.EBX; D_REGS.D.ECX= x86_reg->d.ecx; D_regs.d.edx= x86_reg->D.edx; D_regs.d.eax= x86_reg->D.eax; D_regs.x.flags= x86_reg->X.flags; d_regs.x.es= x86_reg->x.es; D_regs.x.ds= x86_reg->X.ds; D_regs.x.fs= x86_reg->X.fs; D_regs.x.gs= x86_reg->X.gs; D_regs.x.ip= x86_reg->X.ip; D_regs.x.cs= x86_reg->X.cs; D_REGS.X.SP= x86_reg->x.sp; D_REGS.X.SS= x86_reg->X.SS; Return_value= __dpmi_int (Int_num, &D_regs); X86_reg->d.edi =D_regs.d.edi; X86_reg->d.esi =D_regs.d.esi; X86_reg-&GT;D.EBP =D_REGS.D.EBP; X86_reg->d.res =D_regs.d.res; X86_reg-&GT;D.EBX =D_REGS.D.EBX; X86_reg-&GT;D.ECX =d_regs.d.ecx; X86_reg->d.edx =D_regs.d.edx; X86_reg->d.eax =D_regs.d.eax; X86_reg->x.flags =D_regs.x.flags; X86_reg->x.es =d_regs.x.es; X86_reg->x.ds =D_regs.x.ds; X86_reg->x.fs =D_regs.x.fs; X86_reg->x.gs =D_regs.x.gs; X86_reg->x.ip =D_regs.x.ip; X86_reg->x.cs =D_regs.x.cs; X86_reg-&GT;X.SP =d_regs.x.sp; X86_reg-&GT;X.SS =D_REGS.X.SS; returnReturn_value;}/********************************** * Read Configuration WORD if PCI **********************************/Uword Readconfigword (WORD pciaddr,intreg)  {X86_regs inregs; INREGS.X.AX=0xb109;//Read Configuration WordINREGS.X.BX =pciaddr; Inregs.x.di= reg;//Register NumberX86_int (0x1A, &Inregs); returnINREGS.D.ECX;//The value}//Main ProgramintMainvoid) {Uword pciaddr;  Uword Subclass; intEhcicount =0, Ohcicount =0, Uhcicount =0;  for(pciaddr =0; Pciaddr <0xFFFF; pciaddr++) {    if(Readconfigword (PCIADDR,0) !=0xFFFF) {      //Read Class Code      if(Readconfigword (PCIADDR,0x000a) ==0x0c03) {//USB Host Controller//Read Subclass CodeSubclass = Readconfigword (PCIADDR,0x0008); if((Subclass &0xff00) ==0x2000) {//UHCIehcicount++; } Else if((Subclass &0xff00) ==0x1000) {//OHCIohcicount++; } Else if((Subclass &0xff00) ==0x00) {//UHCIuhcicount++; } }}} printf ("There is%d OHCI device (s). \ n", Ohcicount); printf ("There is%d EHCI device (s). \ n", Ehcicount); printf ("There is%d UHCI device (s). \ n", Uhcicount);}

The program is very simple, all the concepts in the previous blog has been introduced, the subroutines are mostly used in the previous program example, so there is no more explanation, the program, we only listed the number of devices, but obviously, in this way, we can read from the configuration space of the base address and other information, These will be used in future articles.

One of the USB series: List your USB devices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.