home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / usbd0906.zip / usb_20020906.zip / usbcalls / test / TESTIDE2.CPP < prev    next >
C/C++ Source or Header  |  2001-11-13  |  9KB  |  310 lines

  1. #define INCL_DOS
  2. #include <os2.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #pragma pack(1)
  8.  
  9. typedef struct
  10. {
  11.   USHORT usVendorID;
  12.   USHORT usDeviceID;
  13.   USHORT usBCDDevice;
  14.   USHORT usDeviceNumber; // Get the usDeviceNumber device in the system fi. if 2 aquire the 2nd device
  15.                          // 0 means first not aquired device.
  16. }AQUIREDEV, *NPAQUIREDEV, FAR *PAQUIREDEV;
  17.  
  18. typedef struct
  19. {
  20.   ULONG  ulHandle;
  21.   USHORT usLangID;
  22.   UCHAR  ucStringID;
  23.   UCHAR  ucReserved;
  24. }GETSTRINGPARM, *NGETSTRINGPARM, FAR *PGETSTRINGPARM;
  25.  
  26. typedef struct
  27. {
  28.   UCHAR  bRequestType;
  29.   UCHAR  bRequest;
  30.   USHORT wValue;
  31.   USHORT wIndex;
  32.   USHORT wLength;
  33.   ULONG  ulTimeout; /* in milliseconds */
  34. }SETUPPACKET, *NSETUPPACKET, FAR *PSETUPPACKET;
  35.  
  36. typedef struct
  37. {
  38.   ULONG  ulHandle;
  39.   UCHAR  bRequestType;
  40.   UCHAR  bRequest;
  41.   USHORT wValue;
  42.   USHORT wIndex;
  43.   USHORT wLength;
  44.   ULONG  ulTimeout; /* in milliseconds */
  45. }LIBUSB_CTRL_REQ, *NPLIBUSB_CTRL_REQ, FAR *PLIBUSB_CTRL_REQ;
  46.  
  47. typedef struct
  48. {
  49.   ULONG  ulHandle;
  50.   ULONG  ulTimeout; /* in milliseconds */
  51.   UCHAR  Endpoint;
  52.   UCHAR  Interface;
  53.   USHORT usDataLen;
  54.   UCHAR  Data[1];
  55. }LIBUSB_BULK_REQ, *NPLIBUSB_BULK_REQ, FAR *PLIBUSB_BULK_REQ;
  56.  
  57. #pragma pack()
  58.  
  59. HFILE hDriver;
  60.  
  61. int sendCtrlReq( ULONG ulUSBHandle,
  62.                   PSETUPPACKET pReq,
  63.                   UCHAR* pData)
  64. {
  65.   ULONG i,ulParmLen, ulDataLen;
  66.   LIBUSB_CTRL_REQ CtrlRequest;
  67.   int rc;
  68.   UCHAR *pSP;
  69.   ulParmLen = sizeof(LIBUSB_CTRL_REQ);
  70.   CtrlRequest.ulHandle     = ulUSBHandle;
  71.   CtrlRequest.bRequestType = pReq->bRequestType;
  72.   CtrlRequest.bRequest     = pReq->bRequest;
  73.   CtrlRequest.wValue       = pReq->wValue;
  74.   CtrlRequest.wIndex       = pReq->wIndex;
  75.   CtrlRequest.wLength      = pReq->wLength;
  76.   CtrlRequest.ulTimeout    = pReq->ulTimeout;
  77.   ulDataLen = pReq->wLength;
  78. #if 0
  79.   pSP = (UCHAR*)pReq;
  80.   printf("SetupPacket : ");
  81.   for(i=0;i<(sizeof(SETUPPACKET)-4);i++)
  82.     printf("%02X ",*(pSP+i));
  83.   printf("\r\n");    
  84.   if((pReq->bRequestType & 0x80)==0 && ulDataLen>0)
  85.   {
  86.     printf("Sending CtrlData: ");
  87.     for(i=0;i<ulDataLen;i++)
  88.       printf("%02X ",*(pData+i));
  89.     printf("\r\n");    
  90.   }
  91. #endif
  92.   rc = DosDevIOCtl( hDriver, 0xA0, 0x36, // Send Ctrl Req
  93.                     (PVOID)&CtrlRequest, ulParmLen, &ulParmLen,
  94.                     ulDataLen>0?(PVOID)pData:NULL, 
  95.                     ulDataLen, 
  96.                     ulDataLen>0?&ulDataLen:NULL);
  97.   if(rc)
  98.     printf("Send Ctrl Req, rc=%d\r\n",rc);
  99.   if(ulDataLen && (pReq->bRequestType &0x80)== 0x80)
  100.   {
  101.     printf("Returned Data: ");    
  102.     for(i=0;i<ulDataLen;i++)
  103.     {
  104.       printf("%02X ",*(pData+i));
  105.       if(((i+1)%16)==0)
  106.        printf("\r\n               ");    
  107.     }
  108.     printf("\r\n");    
  109.   }
  110.   return rc;
  111. }
  112.  
  113.  
  114. int sendBulkReq( ULONG ulUSBHandle,
  115.                  ULONG  ulTimeout,
  116.                  UCHAR  Endpoint,
  117.                  UCHAR  Interface,
  118.                  USHORT usDataLen,
  119.                  UCHAR* pData)
  120. {
  121.   ULONG i,ulParmLen, ulDataLen;
  122.   PLIBUSB_BULK_REQ pBulkRequest;
  123.   int rc;
  124.   UCHAR *pBR;
  125.   if(Endpoint & 0x80) // Dir In
  126.   { 
  127.     ulParmLen = sizeof(LIBUSB_BULK_REQ);
  128.     pBR = (UCHAR*) malloc(ulParmLen);
  129.     pBulkRequest = (PLIBUSB_BULK_REQ) pBR;
  130.     ulDataLen = usDataLen;
  131.   }
  132.   else
  133.   {
  134.     ulParmLen = usDataLen+sizeof(LIBUSB_BULK_REQ);
  135.     pBR = (UCHAR*) malloc(ulParmLen);
  136.     pBulkRequest = (PLIBUSB_BULK_REQ) pBR;
  137.     memcpy(pBulkRequest->Data,pData, usDataLen);
  138.     ulDataLen = 0;
  139.   }
  140.   ulDataLen = sizeof(LIBUSB_CTRL_REQ);
  141.   pBulkRequest->ulHandle  = ulUSBHandle;
  142.   pBulkRequest->Endpoint  = Endpoint;
  143.   pBulkRequest->ulTimeout = ulTimeout;
  144.   pBulkRequest->Interface = pBulkRequest->Interface;
  145.   pBulkRequest->usDataLen = usDataLen;
  146.  
  147. #if 1
  148.   if((pBulkRequest->Endpoint & 0x80)==0 && usDataLen>0)
  149.   {
  150.     printf("Sending BulkData: ");
  151.     for(i=0;i<usDataLen;i++)
  152.     {
  153.       printf("%02X ",pBulkRequest->Data[i]);
  154.       if(((i+1)%16)==0)
  155.        printf("\r\n               ");    
  156.     }
  157.     printf("\r\n");    
  158.   }
  159. #endif
  160.   rc = DosDevIOCtl( hDriver, 0xA0, 0x37, // Send Bulk Req
  161.                     (PVOID)pBR, ulParmLen, &ulParmLen,
  162.                     ulDataLen>0?(PVOID)pData:NULL, 
  163.                     ulDataLen, 
  164.                     ulDataLen>0?&ulDataLen:NULL);
  165.   if(rc)
  166.     printf("Send Bulk Req, rc=%d\r\n",rc);
  167.   if(ulDataLen && (pBulkRequest->Endpoint &0x80)== 0x80)
  168.   {
  169.     printf("Returned Data: ");    
  170.     for(i=0;i<ulDataLen;i++)
  171.     {
  172.       printf("%02X ",*(pData+i));
  173.       if(((i+1)%16)==0)
  174.        printf("\r\n               ");    
  175.     }
  176.     printf("\r\n");    
  177.   }
  178.   return rc;
  179. }
  180.  
  181. int SenseDevice(HFILE hDrv, ULONG ulUSBHandle)
  182. {
  183.   int rc;
  184. //  UCHAR ucCommandSense[8] = { 0x00, 0x01, 0x00, 0x00, 
  185. //                              0x00, 0xa0, 0xec, 0x01};
  186.   UCHAR ucCommandSense[0x1f] = { 0x55, 0x53, 0x42, 0x43, 0x00, 0x00, 0x00, 0x00, 
  187.                                  0x60, 0x00, 0x00, 0x00, 0x80, 0x00, 0x0c, 0x12, 
  188.                                  0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 
  189.                                  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  190.  
  191.   UCHAR ucData[512];
  192.   memset(ucData,0,sizeof(ucData));
  193.   rc = sendBulkReq( ulUSBHandle, 0, 0x02, 0x00, 0x1f, ucCommandSense);
  194.   //if (rc==0)
  195.     rc = sendBulkReq(ulUSBHandle, 0, 0x81, 0x00, 0x60, ucData);
  196.   //if (rc==0)
  197.     rc = sendBulkReq(ulUSBHandle, 0, 0x81, 0x00, 0x0d, ucData);
  198.   return rc;
  199. }
  200.  
  201. int ReadSector(HFILE hDrv, ULONG ulUSBHandle, ULONG ulSector)
  202. {
  203.   int rc;
  204.   UCHAR ucCommandRead[8] = { 0x00, 0x01, 0x00, 0x00, 
  205.                              0x00, 0xe0, 0x20, 0x01};
  206.   UCHAR ucData[512];
  207.   rc = sendBulkReq( ulUSBHandle, 0, 0x01, 0x00, 8, ucCommandRead);
  208.   return rc==0? sendBulkReq(ulUSBHandle, 0, 0x82, 0x00, 512, ucData):rc;
  209. }
  210.  
  211.  
  212. void main()
  213. {
  214.   int rc,i;
  215.   ULONG ulAction, ulParmLen, ulDataLen, ulUSBHandle, *pData;
  216.   AQUIREDEV  USBDevice;
  217.   SETUPPACKET SPConfigure;
  218.   UCHAR ucBuffer[640];
  219.   rc = DosOpen( "USBRESM$",
  220.                 &hDriver,
  221.                 &ulAction,
  222.                 0,
  223.                 FILE_NORMAL,
  224.                 OPEN_ACTION_OPEN_IF_EXISTS,
  225.                 OPEN_ACCESS_READWRITE |
  226.                 OPEN_FLAGS_NOINHERIT |
  227.                 OPEN_SHARE_DENYNONE,
  228.                 0 );
  229.   printf("Open returned %d\r\n",rc);
  230.  
  231.   ulDataLen = sizeof(ulUSBHandle);
  232.   ulUSBHandle = 0;
  233.   rc = DosDevIOCtl( hDriver, 0xA0, 0x31, // Aquire Device
  234.                     NULL,0,0,
  235.                     (PVOID)&ulUSBHandle, ulDataLen, &ulDataLen);
  236.     printf("%d USB Devices\r\n",ulUSBHandle);
  237.  
  238.   if(!rc)
  239.   {
  240.     USBDevice.usVendorID = 0x04CE;
  241.     USBDevice.usDeviceID = 0x0002;
  242.     USBDevice.usBCDDevice = 0xFFFF; //0x026C;
  243.     USBDevice.usDeviceNumber = 0; // Get the usDeviceNumber device in the system fi. if 2 aquire the 2nd device
  244.     ulParmLen = sizeof(USBDevice);
  245.     ulDataLen = sizeof(ulUSBHandle);
  246.     ulUSBHandle = 0xdeadbeef;
  247.     rc = DosDevIOCtl( hDriver, 0xA0, 0x33, // Aquire Device
  248.                       (PVOID)&USBDevice, ulParmLen, &ulParmLen,
  249.                       (PVOID)&ulUSBHandle, ulDataLen, &ulDataLen);
  250.     printf("Aquire returned %d Handle is 0x%08x\r\n",rc,ulUSBHandle);
  251.   }
  252.  
  253.   if(!rc && ulUSBHandle!=0)
  254.   {
  255.     UCHAR ucData[20];
  256.     SPConfigure.bRequestType = 0; // standard
  257.     SPConfigure.bRequest     = 9; // Set config
  258.     SPConfigure.wValue       = 1;
  259.     SPConfigure.wIndex       = 0;
  260.     SPConfigure.wLength      = 0;
  261.     SPConfigure.ulTimeout    = 0;
  262.  
  263.     rc = sendCtrlReq( ulUSBHandle, &SPConfigure, NULL);
  264.     memset(ucData,0,sizeof(ucData));
  265.     SPConfigure.bRequestType = 0x80; // standard
  266.     SPConfigure.bRequest     = 0x08; // Get config
  267.     SPConfigure.wValue       = 0;
  268.     SPConfigure.wIndex       = 0;
  269.     SPConfigure.wLength      = sizeof(ucData);
  270.     SPConfigure.ulTimeout    = 0;
  271.     rc = sendCtrlReq( ulUSBHandle, &SPConfigure, ucData);
  272.     memset(ucData,0,sizeof(ucData));
  273.     ucData[0] = 0x05;
  274.     SPConfigure.bRequestType = 0xC0; 
  275.     SPConfigure.bRequest     = 0xFF; 
  276.     SPConfigure.wValue       = 0x0007;
  277.     SPConfigure.wIndex       = 0xfffc;
  278.     SPConfigure.wLength      = 2;
  279.     SPConfigure.ulTimeout    = 0;
  280.     rc = sendCtrlReq( ulUSBHandle, &SPConfigure, ucData);
  281.     memset(ucData,0,sizeof(ucData));
  282.     SPConfigure.bRequestType = 0xC0; 
  283.     SPConfigure.bRequest     = 0xFF; 
  284.     SPConfigure.wValue       = 0x0007;
  285.     SPConfigure.wIndex       = 0x00ae;
  286.     SPConfigure.wLength      = 2;
  287.     SPConfigure.ulTimeout    = 0;
  288.     rc = sendCtrlReq( ulUSBHandle, &SPConfigure, ucData);
  289.     memset(ucData,0,sizeof(ucData));
  290.     SPConfigure.bRequestType = 0xa0; 
  291.     SPConfigure.bRequest     = 0xFE; 
  292.     SPConfigure.wValue       = 0x0000;
  293.     SPConfigure.wIndex       = 0x0000;
  294.     SPConfigure.wLength      = 1;
  295.     SPConfigure.ulTimeout    = 0;
  296.     rc = sendCtrlReq( ulUSBHandle, &SPConfigure, ucData);
  297.  
  298.     rc = SenseDevice(hDriver,ulUSBHandle);
  299. //    if(!rc)
  300. //      rc = ReadSector(hDriver,ulUSBHandle,0);
  301.  
  302.     ulParmLen = sizeof(ulUSBHandle);
  303.     ulDataLen = 0;
  304.     rc = DosDevIOCtl( hDriver, 0xA0, 0x34, // Release Device
  305.                       (PVOID)&ulUSBHandle, ulParmLen, &ulParmLen,
  306.                       NULL, ulDataLen, &ulDataLen);
  307.   }
  308.  
  309.   DosClose(hDriver);
  310. }