usrUsbTool.c
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:98k
源码类别:

VxWorks

开发平台:

C/C++

  1. /* usrUsbTool.c - USB Driver (USBD) and USB HCD Exerciser. */
  2. /* Copyright 2000-2002 Wind River Systems, Inc. */
  3. /*
  4. Modification history
  5. --------------------
  6. 01p,16mar02,wef  fixed a bug w/ speaker operation.
  7. 01n,13dec01,wef  merge from veloce view
  8. 01m,16sep00,wef  changed all device component names to have _USB_ in them
  9. 01l,12apr00,wef  reverted to older version of usrUsbTool.c
  10. 01k,07mar00,rcb  Add casts to convert GENERIC_HANDLEs to UINT32 where necesary.
  11. 01j,29jan00,wef  made usbTool in to a component configlette
  12. 01i,17jan00,rcb  Add functions to test usbSpeakerLib.
  13. 01h,29nov99,rcb  Increase frame number fields to 32-bits in 
  14.  cmdGetCurrentFrame().
  15. 01g,23nov99,rcb  Added "Attach" and "Detach" commands to control attach/detach
  16.  of UHCI or OHCI HCD modules.  Removed automatic attachment
  17.  from "UsbInit" command.
  18. 01f,23nov99,rcb  Change #include ../xxx references to lower case.
  19. 01e,12nov99,rcb  Shorted path names...affects "#include" directives.
  20. 01d,07oct99,rcb  Add code to test usbMouseLib.c.
  21. 01c,04oct99,rcb  Add "dump" command.
  22. 01b,24sep99,rcb  Add "print" function to dump files to printer.
  23. 01a,01jun99,rcb  First.
  24. */
  25. /*
  26. DESCRIPTION
  27. usbTool is a command line-driven program which allows the user to exercise
  28. USB capabilities through the USB Driver (USBD) and USB HCD (Host Controller
  29. Driver).  
  30. In the vxWorks environment, the user starts usbTool by invoking the "usbTool"
  31. entry point from the vxWorks shell. usbTool then displays a prompt:
  32.     USB>
  33. The user may now enter commands which will be parsed and executed by usbTool.
  34. Each command follows the format:
  35.     USB>command [optional parameter(s)]
  36. Commands are executed after the user presses [enter].  The user may enter 
  37. the "help" or "?" commands to see a list of currently supported commands.
  38. Multiple commands may be entered on the same command line separated by 
  39. semicolons (';').  usbTool which execute each command as if it had been 
  40. entered on a separate line (unless a command terminates with an error, in 
  41. which case all remaining commands entered on the same line will be ignored).
  42. The "quit"/"exit"/"bye" command terminates usbTool.
  43. */
  44. /* Include files */
  45. #include "stdio.h"
  46. #include "stdlib.h"
  47. #include "string.h"
  48. #include "ioLib.h"
  49. #include "ctype.h"
  50. #include "usb/usbPlatform.h"     /* Basic definitions */
  51. #include "usb/tools/cmdParser.h"     /* Command parser util funcs */
  52. #include "usb/usbPciLib.h"     /* PCI interface */
  53. #include "usb/usbdLib.h"     /* USBD interface */
  54. #ifdef INCLUDE_UHCI
  55.     #include "drv/usb/usbUhci.h" /* UHCI definitions */
  56.     #include "drv/usb/usbHcdUhciLib.h" /* UHCI HCD entry point, etc. */
  57. #endif
  58. #ifdef INCLUDE_OHCI
  59.     #include "drv/usb/usbOhci.h" /* OHCI definitions */
  60.     #include "drv/usb/usbHcdOhciLib.h" /* OHCI HCD entry point, etc. */
  61. #endif
  62. #include "usb/usbHid.h"      /* USB HID definitions */
  63. #ifdef INCLUDE_USB_KEYBOARD
  64.     #include "drv/usb/usbKeyboardLib.h"  /* USB keyboard SIO driver */
  65. #endif
  66. #ifdef INCLUDE_USB_MOUSE
  67.     #include "drv/usb/usbMouseLib.h" /* USB mouse SIO driver */
  68. #endif
  69. #ifdef INCLUDE_USB_PRINTER
  70.     #include "usb/usbPrinter.h"
  71.     #include "drv/usb/usbPrinterLib.h" /* USB printer SIO driver */
  72. #endif
  73. #ifdef INCLUDE_USB_SPEAKER
  74.     #include "usb/tools/wavFormat.h" /* Microsoft .wav file format */
  75.     #include "usb/usbAudio.h" /* USB audio definitions */
  76.     #include "drv/usb/usbSpeakerLib.h" /* USB speaker SEQ_DEV driver */
  77. #endif
  78. #ifdef INCLUDE_USB_TARG
  79.     #include "usb/target/usbTargLib.h" /* USB target library */
  80.     #include "drv/usb/target/usbPdiusbd12Eval.h"    /* Philips target eval */
  81.     #include "drv/usb/target/usbTcdPdiusbd12EvalLib.h"     /* Philips TCD */
  82.     #include "drv/usb/target/usbTargPhilipsD12EvalLib.h"    /* Philips eval target */
  83.     #include "drv/usb/target/usbTargKbdLib.h" /* USB keyboard emulator */
  84.     #include "drv/usb/target/usbTargPrnLib.h" /* USB printer emulator */
  85. #endif
  86. /* defines */
  87. #define PGM_NAME "usbTool"
  88. #define PGM_DESCR "USB exerciser"
  89. #define PGM_VERSION "01f"
  90. #define PGM_COPYRIGHT "Copyright (c) 2002, Wind River Systems, Inc.n"
  91. /* Prompt definition */
  92. #define PROMPT "usb>" /* Program prompt */
  93. #define INDENT 2 /* indent used during bus enumerate */
  94. /* misc defines */
  95. #define INT_PIPE_SRVC_INTERVAL     20 /* milliseconds */
  96. #define CTRL_Z     26 /* ASCI code for ^Z */
  97. #define MAX_MFG_STR_LEN      30
  98. #define MAX_PROD_STR_LEN     30
  99. #define TX_BFR_SIZE     0x1000
  100. #define AUDIO_BFR_SIZE     0x8000
  101. #define GENERIC_USB_BFR 256
  102. /* locals */
  103. #ifdef INCLUDE_USB
  104. LOCAL BOOL initialized = FALSE;
  105. LOCAL USBD_CLIENT_HANDLE usbdClientHandle = NULL;
  106. LOCAL GENERIC_HANDLE uhciAttachToken = NULL;
  107. LOCAL GENERIC_HANDLE ohciAttachToken = NULL;
  108. LOCAL BOOL enterPressed;
  109. LOCAL BOOL irpCallbackInvoked;
  110. #endif
  111. #ifdef INCLUDE_USB_PRINTER
  112. LOCAL BOOL patternTest;
  113. LOCAL UINT32 txCharCount; /* count of chars to "print" */
  114. LOCAL UINT16 nextCharVal; /* next value for pattern test */
  115. LOCAL FILE *txFile = NULL; /* file being "printed" */
  116. LOCAL char txBfr [TX_BFR_SIZE]; /* Working buffer */
  117. LOCAL UINT16 txBfrCount; /* count of chars in working bfr */
  118. LOCAL UINT16 txBfrIndex; /* ptr to next char in bfr */
  119. #endif
  120. #ifdef INCLUDE_USB_TARG
  121. LOCAL BOOL targInit = FALSE;
  122. LOCAL USB_TARG_CHANNEL targChannel = NULL;
  123. LOCAL UINT16 numEndpoints = 0;
  124. LOCAL pUSB_TARG_ENDPOINT_INFO pEndpoints = NULL;
  125. LOCAL BOOL targEnable = FALSE;
  126. #endif /* #ifdef INCLUDE_USB_TARG */
  127. #ifdef INCLUDE_USB
  128. /*************************************************************************
  129. *
  130. * cmdUsbInit - Initialize USBD
  131. *
  132. * RETURNS:  RET_CONTINUE
  133. */
  134. LOCAL UINT16 cmdUsbInit
  135.     (
  136.     pVOID Param, /* Generic parameter passed down */
  137.     char **ppCmd, /* Ptr to remainder of cmd line */
  138.     FILE *fin, /* stream for input (if any) */
  139.     FILE *fout /* stream for output (if any) */
  140.     )
  141.     {
  142.     UINT16 usbdVersion;
  143.     char usbdMfg [USBD_NAME_LEN+1];
  144.     UINT16 s;
  145.     /* if already initialized, just show a warning */
  146.     if (initialized)
  147. {
  148. fprintf (fout, "Already initialized.n");
  149. return RET_CONTINUE;
  150. }
  151.     /* Initialize the USBD */
  152.     s = usbdInitialize ();
  153.     fprintf (fout, "usbdInitialize() returned %dn", s);
  154.     if (s == OK)
  155. {
  156. /* Register ourselves with the USBD */
  157. s = usbdClientRegister (PGM_NAME, &usbdClientHandle);
  158. fprintf (fout, "usbdClientRegister() returned %dn", s);
  159. if (s == OK)
  160.     {
  161.     fprintf (fout, "usbdClientHandle = 0x%xn", (UINT32) usbdClientHandle);
  162.     /* Display the USBD version */
  163.     if ((s = usbdVersionGet (&usbdVersion, usbdMfg)) != OK)
  164. {
  165. fprintf (fout, "usbdVersionGet() returned %dn", s);
  166. }
  167.     else
  168. {
  169. fprintf (fout, "USBD version = 0x%4.4xn", usbdVersion);
  170. fprintf (fout, "USBD mfg = '%s'n", usbdMfg);
  171. }
  172.     if (s == OK)
  173. initialized = TRUE;
  174.     }
  175. }
  176.     
  177.     if (s != OK) 
  178. {
  179. fprintf (fout, "initialization failedn");
  180. }
  181.     return RET_CONTINUE;
  182.     }
  183. /*************************************************************************
  184. *
  185. * cmdUsbDown - Shut down USBD
  186. *
  187. * RETURNS:  RET_CONTINUE
  188. */
  189. LOCAL UINT16 cmdUsbDown
  190.     (
  191.     pVOID Param, /* Generic parameter passed down */
  192.     char **ppCmd, /* Ptr to remainder of cmd line */
  193.     FILE *fin, /* stream for input (if any) */
  194.     FILE *fout /* stream for output (if any) */
  195.     )
  196.     {
  197.     UINT16 s;
  198.     initialized = FALSE;
  199.     uhciAttachToken = ohciAttachToken = NULL;
  200.     /* Unregister our client */
  201.     if (usbdClientHandle == NULL)
  202. {
  203. fprintf (fout, "usbdClientHandle == NULL.  not registeredn");
  204. }
  205.     else
  206. {
  207. s = usbdClientUnregister (usbdClientHandle);
  208. usbdClientHandle = NULL;
  209. fprintf (fout, "usbdClientUnregister() returned %dn", s);
  210. }
  211.     /* shut down USBD */
  212.     s = usbdShutdown ();
  213.     fprintf (fout, "usbdShutdown() returned %dn", s);
  214.     return RET_CONTINUE;
  215.     }
  216. /*************************************************************************
  217. *
  218. * getHcdType - Determines type of HCD specified by caller
  219. *
  220. * RETURNS: N/A
  221. */
  222. LOCAL VOID getHcdType
  223.     (
  224.     char **ppCmd, /* Ptr to remainder of cmd line */
  225.     FILE *fout,  /* stream for output (if any) */
  226.     HCD_EXEC_FUNC *execFunc, /* pointer to HCD exec func */
  227.     GENERIC_HANDLE **ppHandle, /* pointer to generic handle pointer */
  228.     pUINT8 pPciClass, /* pointer to pci class var */
  229.     pUINT8 pPciSubclass, /* pointer to pci subclass var */
  230.     pUINT8 pPciPgmIf, /* pointer to pci pgmif var */
  231.     char **ppHcdName /* pointer to name string var */
  232.     )
  233.     {
  234.     char hcdName [32];
  235.     /* Get HCD type */
  236.     *ppCmd = GetNextToken (*ppCmd, hcdName, sizeof (hcdName));
  237.     if (KeywordMatch (hcdName, "uhci", 4) == 0)
  238. {
  239. #ifdef INCLUDE_UHCI
  240. *execFunc = usbHcdUhciExec;
  241. *ppHandle = &uhciAttachToken;
  242. *pPciClass = UHCI_CLASS;
  243. *pPciSubclass = UHCI_SUBCLASS;
  244. *pPciPgmIf = UHCI_PGMIF;
  245. *ppHcdName = "UHCI";
  246. #else
  247.         fprintf (fout, "UHCI Component Not Included.n");
  248.         *execFunc = NULL;
  249. #endif
  250. }
  251.     else if (KeywordMatch (hcdName, "ohci", 4) == 0)
  252. {
  253. #ifdef INCLUDE_OHCI
  254. *execFunc = usbHcdOhciExec;
  255. *ppHandle = &ohciAttachToken;
  256. *pPciClass = OHCI_CLASS;
  257. *pPciSubclass = OHCI_SUBCLASS;
  258. *pPciPgmIf = OHCI_PGMIF;
  259. *ppHcdName = "OHCI";
  260. #else
  261.         fprintf (fout, "OHCI Component Not Included.n");
  262.         *execFunc = NULL;
  263. #endif
  264. }
  265.     else
  266. {
  267. fprintf (fout, "Must specify HCD type as 'uhci' or 'ohci'.n");
  268. *execFunc = NULL;
  269. }
  270.     return;
  271.     }
  272. /*************************************************************************
  273. *
  274. * cmdAttach - Attaches HCD to USBD
  275. *
  276. * RETURNS:  RET_CONTINUE
  277. */
  278. LOCAL UINT16 cmdAttach
  279.     (
  280.     pVOID Param, /* Generic parameter passed down */
  281.     char **ppCmd, /* Ptr to remainder of cmd line */
  282.     FILE *fin, /* stream for input (if any) */
  283.     FILE *fout /* stream for output (if any) */
  284.     )
  285.     {
  286.     HCD_EXEC_FUNC execFunc;
  287.     GENERIC_HANDLE *pToken;
  288.     UINT8 pciClass;
  289.     UINT8 pciSubclass;
  290.     UINT8 pciPgmIf;
  291.     char *hcdName;
  292.     UINT8 busNo;
  293.     UINT8 deviceNo;
  294.     UINT8 funcNo;
  295.     PCI_CFG_HEADER pciCfgHdr;
  296.     UINT16 s;
  297.     
  298.     getHcdType (ppCmd, fout, &execFunc, &pToken, &pciClass, &pciSubclass,
  299. &pciPgmIf, &hcdName);
  300.     if (execFunc != NULL)
  301. {
  302. if (*pToken != NULL)
  303.     {
  304.     fprintf (fout, "%s already attached.n", hcdName);
  305.     return RET_CONTINUE;
  306.     }
  307. /* Find an instance of a USB host controller */
  308. if (!usbPciClassFind (pciClass, pciSubclass, pciPgmIf, 0, 
  309.     &busNo, &deviceNo, &funcNo))
  310.     {
  311.     fprintf (fout, "No %s host controller found.n", hcdName);
  312.     return RET_CONTINUE;
  313.     }
  314. usbPciConfigHeaderGet (busNo, deviceNo, funcNo, &pciCfgHdr);
  315. /* Attach the UHCI HCD to the USBD. */
  316. s = usbdHcdAttach (execFunc, &pciCfgHdr, pToken);
  317. fprintf (fout, "usbdHcdAttach() returned %dn", s);
  318. if (s == OK)
  319.     {
  320.     fprintf (fout, "AttachToken = 0x%xn", (UINT32) *pToken);
  321.     }
  322. }
  323.     return RET_CONTINUE;
  324.     }
  325. /*************************************************************************
  326. *
  327. * cmdDetach - Detaches HCD from USBD
  328. *
  329. * RETURNS:  RET_CONTINUE
  330. */
  331. LOCAL UINT16 cmdDetach
  332.     (
  333.     pVOID Param, /* Generic parameter passed down */
  334.     char **ppCmd, /* Ptr to remainder of cmd line */
  335.     FILE *fin, /* stream for input (if any) */
  336.     FILE *fout /* stream for output (if any) */
  337.     )
  338.     {
  339.     HCD_EXEC_FUNC execFunc;
  340.     GENERIC_HANDLE *pToken;
  341.     UINT8 pciClass;
  342.     UINT8 pciSubclass;
  343.     UINT8 pciPgmIf;
  344.     char *hcdName;
  345.     UINT16 s;
  346.     getHcdType (ppCmd, fout, &execFunc, &pToken, &pciClass, &pciSubclass,
  347. &pciPgmIf, &hcdName);
  348.     if (execFunc != NULL)
  349. {
  350. /* Detach the HCD */
  351. if (*pToken == NULL)
  352.     {
  353.     fprintf (fout, "%s not attached.n", hcdName);
  354.     }
  355. else
  356.     {
  357.     fprintf (fout, "Detaching %s HCD.n", hcdName);
  358.     s = usbdHcdDetach (*pToken);
  359.     *pToken = NULL;
  360.     fprintf (fout, "usbdHcdDetach() returned %dn", s);
  361.     }
  362. }
  363.     return RET_CONTINUE;
  364.     }
  365. /***************************************************************************
  366. *
  367. * showUsbString - reads a string descriptor and displays it
  368. *
  369. * RETURNS: N/A
  370. */
  371. LOCAL VOID showUsbString
  372.     (
  373.     USBD_CLIENT_HANDLE clientHandle,
  374.     USBD_NODE_ID nodeId,
  375.     FILE *fout,
  376.     UINT8 stringId,
  377.     UINT16 maxLen
  378.     )
  379.     {
  380.     USB_STRING_DESCR * pString;
  381.     UINT16 actLen;
  382.     UINT16 i;
  383.     char c;
  384.     if ((pString = OSS_MALLOC (USB_MAX_DESCR_LEN)) == NULL)  
  385. return;
  386.     /* Read device descriptor to get string Ids for mfg/model */
  387.     if (usbdDescriptorGet (clientHandle, 
  388.    nodeId, 
  389.    USB_RT_STANDARD | USB_RT_DEVICE, 
  390.    USB_DESCR_STRING, 
  391.    stringId, 
  392.    0x0409,  /* unicode code for English */
  393.    USB_MAX_DESCR_LEN, 
  394.    (UINT8 *) pString, 
  395.    &actLen) 
  396. != OK)
  397. {
  398. fprintf (fout, "<<can't read string>>");
  399. OSS_FREE (pString);
  400. return;
  401. }
  402.     for (i = 0; i + 2 < actLen && i < maxLen * 2; i += 2)
  403. {
  404. c = pString->string [i];
  405. fprintf (fout, "%c", (isprint ((int) c)) ? c : '_');
  406. }
  407.     if (i + 2 < actLen)
  408. fprintf (fout, "...");
  409.     OSS_FREE (pString);
  410.     }
  411. /***************************************************************************
  412. *
  413. * showMfgModel - Display mfg/model strings for a USB device
  414. *
  415. * RETURNS: N/A
  416. */
  417. LOCAL VOID showMfgModel
  418.     (
  419.     USBD_CLIENT_HANDLE clientHandle,
  420.     USBD_NODE_ID nodeId,
  421.     FILE *fout
  422.     )
  423.     {
  424.     USB_DEVICE_DESCR * pDevDescr;
  425.     UINT16 actLen;
  426.     if((pDevDescr = OSS_MALLOC (USB_DEVICE_DESCR_LEN)) == NULL)
  427. return;
  428.     /* Read device descriptor to get string Ids for mfg/model */
  429.     if (usbdDescriptorGet (clientHandle, 
  430.    nodeId, 
  431.    USB_RT_STANDARD | USB_RT_DEVICE, 
  432.    USB_DESCR_DEVICE, 
  433.    0, 
  434.    0, 
  435.    USB_DEVICE_DESCR_LEN, 
  436.    (UINT8 *) pDevDescr, 
  437.    &actLen) 
  438. != OK || 
  439.   actLen < USB_DEVICE_DESCR_LEN)
  440. {
  441.         OSS_FREE (pDevDescr);
  442. return;
  443. }
  444.     if (pDevDescr->manufacturerIndex != 0 || pDevDescr->productIndex != 0)
  445. fprintf (fout, " = ");
  446.     if (pDevDescr->manufacturerIndex != 0)
  447. {
  448. showUsbString (clientHandle, nodeId, fout, pDevDescr->manufacturerIndex,
  449.     MAX_MFG_STR_LEN);
  450. fprintf (fout, "/");
  451. }
  452.     if (pDevDescr->productIndex != 0)
  453. {
  454. showUsbString (clientHandle, nodeId, fout, pDevDescr->productIndex,
  455.     MAX_PROD_STR_LEN);
  456. }
  457.     }
  458. /***************************************************************************
  459. *
  460. * HubEnumerate - Enumerate all ports on the specified hub
  461. * This routine enumerates all devices from the specified HubId down.
  462. * <clientHandle> must be a valid USBD_CLIENT_HANDLE.  <hubId> specifies
  463. * the Node Id of the first USB hub to be enumerated.
  464. *
  465. * RETURNS: OK, or ERROR if USBD returns an error.
  466. */
  467. LOCAL UINT16 HubEnumerate
  468.     (
  469.     USBD_CLIENT_HANDLE clientHandle, /* Caller抯 USBD client handle */
  470.     USBD_NODE_ID hubId,  /* Node Id for hub to enumerate */
  471.     FILE *fout,
  472.     UINT16 indent
  473.     )
  474.     {
  475.     UINT16 portCount; /* Number of ports on this hub */
  476.     UINT16 portIndex; /* current port index */
  477.     UINT16 nodeType; /* type of node being enumerated */
  478.     USBD_NODE_ID nodeId; /* id of node being enumerated */
  479.     UINT16 s;
  480.     /* Retrieve the number of ports for this hub. */
  481.     fprintf (fout, "%*shub 0x%x", indent, "", (UINT32) hubId);
  482.     showMfgModel (clientHandle, hubId, fout);
  483.     fprintf (fout, "n");
  484.     if ((s = usbdHubPortCountGet (clientHandle, hubId, &portCount)) 
  485. != OK)
  486. {
  487. fprintf (fout, "usbdHubPortCountGet() returned %dn", s);
  488. return ERROR;
  489. }
  490.     fprintf (fout, "%*sport count = %dn", indent+INDENT, "", portCount);
  491.     /* See if a device is attached to each of the hub抯 ports. */
  492.     for (portIndex = 0; portIndex < portCount; portIndex++) 
  493. {
  494. if ((s = usbdNodeIdGet (clientHandle, hubId, portIndex, &nodeType, 
  495.     &nodeId)) != OK)
  496.     {
  497.     fprintf (fout, "%*susbdNodeIdGet() returned %dn", indent+INDENT, "", 
  498. s);
  499.     return ERROR;
  500.     }
  501. switch (nodeType)
  502.     {
  503.     case USB_NODETYPE_NONE: /* No device attached. */
  504. fprintf (fout, "%*sport %d not connectedn", indent+INDENT, "", 
  505.     portIndex);
  506. break;
  507.     case USB_NODETYPE_HUB: /* Another hub found. */
  508. fprintf (fout, "%*sport %d is hub 0x%xn", indent+INDENT, "",
  509.     portIndex, (UINT32) nodeId);
  510. if (HubEnumerate (clientHandle, nodeId, fout, indent+INDENT) 
  511.     != OK)
  512.     return ERROR;
  513. break;
  514.     case USB_NODETYPE_DEVICE: /* Device attached to port. */
  515. fprintf (fout, "%*sport %d is device 0x%x", indent+INDENT, "",
  516.     portIndex, (UINT32) nodeId);
  517. showMfgModel (clientHandle, nodeId, fout);
  518. fprintf (fout, "n");
  519. break;
  520.     default: /* Unknown node type code */
  521. fprintf (fout, "%*snode type not recognized for node 0x%xn",
  522.     indent+INDENT, "", (UINT32) nodeId);
  523. break;
  524.     }
  525. }
  526.     return OK;
  527.     }
  528. /***************************************************************************
  529. *
  530. * USBEnumerate - Enumerate all USB host controllers in the system.
  531. * This routine enumerates all USB host controllers, hubs, and devices
  532. * currently connected to the system.  The caller must register with the
  533. * USBD prior to calling this function and supply its USBD_CLIENT_HANDLE
  534. * in <clientHandle>.
  535. *
  536. * RETURNS: OK, or ERROR if USBD returns an error.
  537. */
  538. LOCAL UINT16 USBEnumerate
  539.     (
  540.     USBD_CLIENT_HANDLE clientHandle, /* Caller抯 USBD client handle */
  541.     FILE *fout
  542.     )
  543.     {
  544.     UINT16 busCount; /* Number of USB host controllers */
  545.     UINT16 busIndex; /* current bus index */
  546.     USBD_NODE_ID rootId; /* Root hub id for current bus */
  547.     UINT16 s;
  548.     /* Retrieve the number of USB host controllers in the system. */
  549.     if ((s = usbdBusCountGet (clientHandle, &busCount)) != OK)
  550. {
  551. fprintf (fout, "usbdBusCountGet() returned %dn", s);
  552. return ERROR;
  553. }
  554.     fprintf (fout, "bus count = %dn", busCount);
  555.     /* Retrieve the root hub id for each host controller and enumerate it. */
  556.     for (busIndex = 0; busIndex < busCount; busIndex++) 
  557. {
  558. if ((s = usbdRootNodeIdGet (clientHandle, busIndex, &rootId))
  559.     != OK)
  560.     {
  561.     fprintf (fout, "usbdRootNodeIdGet() returned %dn", s);
  562.     return ERROR;
  563.     }
  564. fprintf (fout, "enumerating bus %dn", busIndex);
  565. if (HubEnumerate (clientHandle, rootId, fout, INDENT) != OK)
  566.     return ERROR;
  567. }
  568.     return OK;
  569.     }
  570. /*************************************************************************
  571. *
  572. * cmdUsbEnum - Enumerate USBs attached to system
  573. *
  574. * RETURNS:  RET_CONTINUE;
  575. */
  576. LOCAL UINT16 cmdUsbEnum
  577.     (
  578.     pVOID Param, /* Generic parameter passed down */
  579.     char **ppCmd, /* Ptr to remainder of cmd line */
  580.     FILE *fin, /* stream for input (if any) */
  581.     FILE *fout /* stream for output (if any) */
  582.     )
  583.     {
  584.     USBEnumerate (usbdClientHandle, fout);
  585.     return RET_CONTINUE;
  586.     }
  587. /*************************************************************************
  588. *
  589. * cmdUsbStats - Show bus statistics
  590. *
  591. * RETURNS:  RET_CONTINUE;
  592. */
  593. LOCAL UINT16 cmdUsbStats
  594.     (
  595.     pVOID Param, /* Generic parameter passed down */
  596.     char **ppCmd, /* Ptr to remainder of cmd line */
  597.     FILE *fin, /* stream for input (if any) */
  598.     FILE *fout /* stream for output (if any) */
  599.     )
  600.     {
  601.     long nodeId;
  602.     USBD_STATS stats;
  603.     UINT16 s;
  604.     /* Extract parameters. */
  605.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  606.     /* Validate parameters */
  607.     if (nodeId == -1)
  608. {
  609. fprintf (fout, "Must specify a node id.n");
  610. return RET_CONTINUE;
  611. }
  612.     /* Execute function. */
  613.     if ((s = usbdStatisticsGet (usbdClientHandle, (GENERIC_HANDLE) nodeId, &stats, sizeof (stats)))
  614. != OK)
  615. fprintf (fout, "usbdStatisticsGet() returned %dn", s);
  616.     else
  617. {
  618. fprintf (fout, "totalTransfersIn    = %ldn", (long) stats.totalTransfersIn);
  619. fprintf (fout, "totalTransfersout   = %ldn", (long) stats.totalTransfersOut);
  620. fprintf (fout, "totalReceiveErrors  = %ldn", (long) stats.totalReceiveErrors);
  621. fprintf (fout, "totalTransmitErrors = %ldn", (long) stats.totalTransmitErrors);
  622. }
  623.     return RET_CONTINUE;
  624.     }
  625. /*************************************************************************
  626. *
  627. * cmdGetConfig - get current configuration value for a device
  628. *
  629. * RETURNS:  RET_CONTINUE;
  630. */
  631. LOCAL UINT16 cmdGetConfig
  632.     (
  633.     pVOID Param, /* Generic parameter passed down */
  634.     char **ppCmd, /* Ptr to remainder of cmd line */
  635.     FILE *fin, /* stream for input (if any) */
  636.     FILE *fout /* stream for output (if any) */
  637.     )
  638.     {
  639.     long nodeId;
  640.     UINT16 * pConfiguration;
  641.     UINT16 s;
  642.     /* Extract parameters. */
  643.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  644.     /* Validate parameters */
  645.     if (nodeId == -1)
  646. {
  647. fprintf (fout, "Must specify a node id.n");
  648. return RET_CONTINUE;
  649. }
  650.     if ((pConfiguration = OSS_MALLOC (sizeof (UINT16))) == NULL)
  651. return RET_CONTINUE;
  652.     /* Execute function. */
  653.     if ((s = usbdConfigurationGet (usbdClientHandle, 
  654.    (GENERIC_HANDLE) nodeId, 
  655.    pConfiguration))
  656.     != OK)
  657. fprintf (fout, "usbdConfigurationGet() returned %dn", s);
  658.     else
  659. fprintf (fout, "current configuration = 0x%xn", *pConfiguration);
  660.     OSS_FREE (pConfiguration);
  661.     return RET_CONTINUE;
  662.     }
  663. /*************************************************************************
  664. *
  665. * cmdSetConfig - set current configuration value for a device
  666. *
  667. * RETURNS:  RET_CONTINUE;
  668. */
  669. LOCAL UINT16 cmdSetConfig
  670.     (
  671.     pVOID Param, /* Generic parameter passed down */
  672.     char **ppCmd, /* Ptr to remainder of cmd line */
  673.     FILE *fin, /* stream for input (if any) */
  674.     FILE *fout /* stream for output (if any) */
  675.     )
  676.     {
  677.     long nodeId;
  678.     long configuration;
  679.     UINT16 s;
  680.     /* Extract parameters. */
  681.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  682.     *ppCmd = GetHexToken (*ppCmd, &configuration, -1);
  683.     /* Validate parameters */
  684.     if (nodeId == -1 || configuration == -1)
  685. {
  686. fprintf (fout, "Must specify a node id and configuration value.n");
  687. return RET_CONTINUE;
  688. }
  689.     /* Execute function. */
  690.     s = usbdConfigurationSet (usbdClientHandle, (GENERIC_HANDLE) nodeId, (UINT16) configuration, 0);
  691.     fprintf (fout, "usbdConfigurationSet() returned %dn", s);
  692.     return RET_CONTINUE;
  693.     }
  694. /*************************************************************************
  695. *
  696. * cmdGetInterface - get alt. setting for an interface
  697. *
  698. * RETURNS:  RET_CONTINUE;
  699. */
  700. LOCAL UINT16 cmdGetInterface
  701.     (
  702.     pVOID Param, /* Generic parameter passed down */
  703.     char **ppCmd, /* Ptr to remainder of cmd line */
  704.     FILE *fin, /* stream for input (if any) */
  705.     FILE *fout /* stream for output (if any) */
  706.     )
  707.     {
  708.     long nodeId;
  709.     long index;
  710.     UINT16 * pAltSetting;
  711.     UINT16 s;
  712.     /* Extract parameters. */
  713.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  714.     *ppCmd = GetHexToken (*ppCmd, &index, -1);
  715.     /* Validate parameters */
  716.     if (nodeId == -1 || index == -1)
  717. {
  718. fprintf (fout, "Must specify a node id and interface number.n");
  719. return RET_CONTINUE;
  720. }
  721.     if ((pAltSetting = OSS_MALLOC (sizeof (UINT16))) == NULL)
  722.         return RET_CONTINUE;
  723.     /* Execute function. */
  724.     if ((s = usbdInterfaceGet (usbdClientHandle, 
  725.        (GENERIC_HANDLE) nodeId, 
  726.        index, 
  727.        pAltSetting))
  728. != OK)
  729. fprintf (fout, "usbdInterfaceGet() returned %dn", s);
  730.     else
  731. fprintf (fout, "alternate setting = 0x%xn", *pAltSetting);
  732.     OSS_FREE (pAltSetting);
  733.     return RET_CONTINUE;
  734.     }
  735. /*************************************************************************
  736. *
  737. * cmdSetInterface - set alt. setting for an interface
  738. *
  739. * RETURNS:  RET_CONTINUE;
  740. */
  741. LOCAL UINT16 cmdSetInterface
  742.     (
  743.     pVOID Param, /* Generic parameter passed down */
  744.     char **ppCmd, /* Ptr to remainder of cmd line */
  745.     FILE *fin, /* stream for input (if any) */
  746.     FILE *fout /* stream for output (if any) */
  747.     )
  748.     {
  749.     long nodeId;
  750.     long index;
  751.     long altSetting;
  752.     UINT16 s;
  753.     /* Extract parameters. */
  754.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  755.     *ppCmd = GetHexToken (*ppCmd, &index, -1);
  756.     *ppCmd = GetHexToken (*ppCmd, &altSetting, -1);
  757.     /* Validate parameters */
  758.     if (nodeId == -1 || index == -1 || altSetting == -1)
  759. {
  760. fprintf (fout, "Must specify a node id, interface number, and alt. setting.n");
  761. return RET_CONTINUE;
  762. }
  763.     /* Execute function. */
  764.     s = usbdInterfaceSet (usbdClientHandle, (GENERIC_HANDLE) nodeId, index, altSetting);
  765.     fprintf (fout, "usbdInterfaceSet() returned %dn", s);
  766.     return RET_CONTINUE;
  767.     }
  768. #endif /* INCLUDE_USB from way above*/
  769. /*************************************************************************
  770. *
  771. * displayMem - dumps memory to display
  772. *
  773. * RETURNS:  N/A
  774. */
  775. LOCAL VOID displayMem
  776.     (
  777.     UINT16 bfrLen,
  778.     pUINT8 pBfr,
  779.     FILE *fout
  780.     )
  781.     {
  782.     int i,j;
  783.     for (i = 0; i < bfrLen; i += 16)
  784. {
  785. for (j = i; j < bfrLen && j < i+16; j++)
  786.     {
  787.     fprintf (fout, "%2.2x ", pBfr [j]);
  788.     if (j % 16 == 7)
  789. fprintf (fout, " ");
  790.     }
  791. fprintf (fout, " ");
  792. for (j = i; j < bfrLen && j < i+16; j++)
  793.     {
  794.     fprintf (fout, "%c", (pBfr [j] >= 32 && pBfr [j] <= 127) ? 
  795. pBfr [j] : '.');
  796.     }
  797. fprintf (fout, "n");
  798. }
  799.     }
  800. #ifdef INCLUDE_USB
  801. /*************************************************************************
  802. *
  803. * cmdGetStatus - get status for a device/interface/endpoint
  804. *
  805. * RETURNS:  RET_CONTINUE;
  806. */
  807. LOCAL UINT16 cmdGetStatus
  808.     (
  809.     pVOID Param, /* Generic parameter passed down */
  810.     char **ppCmd, /* Ptr to remainder of cmd line */
  811.     FILE *fin, /* stream for input (if any) */
  812.     FILE *fout /* stream for output (if any) */
  813.     )
  814.     {
  815.     long nodeId;
  816.     long statusType;
  817.     long index;
  818.     long length;
  819.     UINT8 * pStatusBfr;
  820.     UINT16 actLen;
  821.     UINT16 s;
  822.     /* Extract parameters. */
  823.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  824.     *ppCmd = GetHexToken (*ppCmd, &statusType, -1);
  825.     *ppCmd = GetHexToken (*ppCmd, &index, -1);
  826.     *ppCmd = GetHexToken (*ppCmd, &length, -1);
  827.     /* Validate parameters */
  828.     if (nodeId == -1 || statusType == -1 || index == -1 || length == -1)
  829. {
  830. fprintf (fout, "Must specify a node id, status type, and index.n");
  831. return RET_CONTINUE;
  832. }
  833.     if (length > GENERIC_USB_BFR)
  834. {
  835. fprintf (fout, "length must be 0x%x or less.n", 
  836.     (unsigned int) GENERIC_USB_BFR);
  837. return RET_CONTINUE;
  838. }
  839.     if ((pStatusBfr = OSS_MALLOC (GENERIC_USB_BFR)) == NULL)
  840.         return RET_CONTINUE;
  841.     /* Execute function. */
  842.     if ((s = usbdStatusGet (usbdClientHandle, 
  843.     (GENERIC_HANDLE) nodeId, 
  844.     statusType, 
  845.     index, 
  846.     length, 
  847.     pStatusBfr, 
  848.     &actLen)) != OK)
  849. fprintf (fout, "usbdStatusGet() returned %dn", s);
  850.     else
  851. {
  852. fprintf (fout, "actLen = 0x%x (%d) bytesn", actLen, actLen);
  853. displayMem (actLen, pStatusBfr, fout);
  854. }
  855.     OSS_FREE (pStatusBfr);
  856.     return RET_CONTINUE;
  857.     }
  858. /*************************************************************************
  859. *
  860. * cmdGetAddress - get USB address for a node
  861. *
  862. * RETURNS:  RET_CONTINUE;
  863. */
  864. LOCAL UINT16 cmdGetAddress
  865.     (
  866.     pVOID Param, /* Generic parameter passed down */
  867.     char **ppCmd, /* Ptr to remainder of cmd line */
  868.     FILE *fin, /* stream for input (if any) */
  869.     FILE *fout /* stream for output (if any) */
  870.     )
  871.     {
  872.     long nodeId;
  873.     UINT16 address;
  874.     UINT16 s;
  875.     /* Extract parameters. */
  876.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  877.     /* Validate parameters */
  878.     if (nodeId == -1)
  879. {
  880. fprintf (fout, "Must specify a node id.n");
  881. return RET_CONTINUE;
  882. }
  883.     /* Execute function. */
  884.     if ((s = usbdAddressGet (usbdClientHandle, (GENERIC_HANDLE) nodeId, &address))
  885. != OK)
  886. fprintf (fout, "usbdAddressGet() returned %dn", s);
  887.     else
  888. fprintf (fout, "current address = 0x%xn", address);
  889.     return RET_CONTINUE;
  890.     }
  891. /*************************************************************************
  892. *
  893. * cmdSetAddress - set USB address for a node
  894. *
  895. * RETURNS:  RET_CONTINUE;
  896. */
  897. LOCAL UINT16 cmdSetAddress
  898.     (
  899.     pVOID Param, /* Generic parameter passed down */
  900.     char **ppCmd, /* Ptr to remainder of cmd line */
  901.     FILE *fin, /* stream for input (if any) */
  902.     FILE *fout /* stream for output (if any) */
  903.     )
  904.     {
  905.     long nodeId;
  906.     long address;
  907.     UINT16 s;
  908.     /* Extract parameters. */
  909.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  910.     *ppCmd = GetHexToken (*ppCmd, &address, -1);
  911.     /* Validate parameters */
  912.     if (nodeId == -1 || address == -1)
  913. {
  914. fprintf (fout, "Must specify a node id and address value.n");
  915. return RET_CONTINUE;
  916. }
  917.     /* Execute function. */
  918.     s = usbdAddressSet (usbdClientHandle, (GENERIC_HANDLE) nodeId, (UINT16) address);
  919.     fprintf (fout, "usbdAddressnSet() returned %dn", s);
  920.     return RET_CONTINUE;
  921.     }
  922. /*************************************************************************
  923. *
  924. * cmdSetFeature - set a USB feature
  925. *
  926. * RETURNS:  RET_CONTINUE;
  927. */
  928. LOCAL UINT16 cmdSetFeature
  929.     (
  930.     pVOID Param, /* Generic parameter passed down */
  931.     char **ppCmd, /* Ptr to remainder of cmd line */
  932.     FILE *fin, /* stream for input (if any) */
  933.     FILE *fout /* stream for output (if any) */
  934.     )
  935.     {
  936.     long nodeId;
  937.     long featureType;
  938.     long feature;
  939.     long index;
  940.     UINT16 s;
  941.     /* Extract parameters. */
  942.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  943.     *ppCmd = GetHexToken (*ppCmd, &featureType, -1);
  944.     *ppCmd = GetHexToken (*ppCmd, &feature, -1);
  945.     *ppCmd = GetHexToken (*ppCmd, &index, -1);
  946.     /* Validate parameters */
  947.     if (nodeId == -1 || featureType == -1 || feature == -1 || index == -1)
  948. {
  949. fprintf (fout, "Must specify a node id, feature type, selector, and index.n");
  950. return RET_CONTINUE;
  951. }
  952.     /* Execute function. */
  953.     s = usbdFeatureSet (usbdClientHandle, (GENERIC_HANDLE) nodeId, featureType, feature, index);
  954.     fprintf (fout, "usbdFeatureSet() returned %dn", s);
  955.     return RET_CONTINUE;
  956.     }
  957. /*************************************************************************
  958. *
  959. * cmdClrFeature - clear a USB feature
  960. *
  961. * RETURNS:  RET_CONTINUE;
  962. */
  963. LOCAL UINT16 cmdClrFeature
  964.     (
  965.     pVOID Param, /* Generic parameter passed down */
  966.     char **ppCmd, /* Ptr to remainder of cmd line */
  967.     FILE *fin, /* stream for input (if any) */
  968.     FILE *fout /* stream for output (if any) */
  969.     )
  970.     {
  971.     long nodeId;
  972.     long featureType;
  973.     long feature;
  974.     long index;
  975.     UINT16 s;
  976.     /* Extract parameters. */
  977.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  978.     *ppCmd = GetHexToken (*ppCmd, &featureType, -1);
  979.     *ppCmd = GetHexToken (*ppCmd, &feature, -1);
  980.     *ppCmd = GetHexToken (*ppCmd, &index, -1);
  981.     /* Validate parameters */
  982.     if (nodeId == -1 || featureType == -1 || feature == -1 || index == -1)
  983. {
  984. fprintf (fout, "Must specify a node id, feature type, selector, and index.n");
  985. return RET_CONTINUE;
  986. }
  987.     /* Execute function. */
  988.     s = usbdFeatureClear (usbdClientHandle, (GENERIC_HANDLE) nodeId, featureType, feature, index);
  989.     fprintf (fout, "usbdFeatureClear() returned %dn", s);
  990.     return RET_CONTINUE;
  991.     }
  992. /*************************************************************************
  993. *
  994. * cmdGetDescr - retrieve a USB descriptor
  995. *
  996. * RETURNS:  RET_CONTINUE;
  997. */
  998. LOCAL UINT16 cmdGetDescr
  999.     (
  1000.     pVOID Param, /* Generic parameter passed down */
  1001.     char **ppCmd, /* Ptr to remainder of cmd line */
  1002.     FILE *fin, /* stream for input (if any) */
  1003.     FILE *fout /* stream for output (if any) */
  1004.     )
  1005.     {
  1006.     long nodeId;
  1007.     long requestType;
  1008.     long descriptorType;
  1009.     long index;
  1010.     long languageId;
  1011.     long length;
  1012.     UINT8 * pDescrBuf;
  1013.     UINT16 actLen;
  1014.     UINT16 s;
  1015.     /* Extract parameters. */
  1016.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  1017.     *ppCmd = GetHexToken (*ppCmd, &requestType, -1);
  1018.     *ppCmd = GetHexToken (*ppCmd, &descriptorType, -1);
  1019.     *ppCmd = GetHexToken (*ppCmd, &index, -1);
  1020.     *ppCmd = GetHexToken (*ppCmd, &languageId, -1);
  1021.     *ppCmd = GetHexToken (*ppCmd, &length, -1);
  1022.     /* Validate parameters */
  1023.     if (nodeId == -1 || requestType == -1 || descriptorType == -1 || index == -1 
  1024. || languageId == -1 || length == -1)
  1025. {
  1026. fprintf (fout, "Must specify a node id, descr. type, index, language, and length.n");
  1027. return RET_CONTINUE;
  1028. }
  1029.     if (length > GENERIC_USB_BFR)
  1030. {
  1031. fprintf (fout, "length must be 0x%x or less.n", 
  1032.     (unsigned int) GENERIC_USB_BFR);
  1033. return RET_CONTINUE;
  1034. }
  1035.     if ((pDescrBuf = OSS_MALLOC (GENERIC_USB_BFR)) == NULL)
  1036.         return RET_CONTINUE;
  1037.     /* Execute function. */
  1038.     s = usbdDescriptorGet (usbdClientHandle, 
  1039.    (GENERIC_HANDLE) nodeId, 
  1040.    requestType, 
  1041.    descriptorType, 
  1042.    index, 
  1043.    languageId, 
  1044.    length, 
  1045.    pDescrBuf, 
  1046.    &actLen);
  1047.     if (s != OK)
  1048. fprintf (fout, "usbdDescriptorGet() returned %dn", s);
  1049.     else
  1050. {
  1051. fprintf (fout, "actLen = 0x%x (%d) bytesn", actLen, actLen);
  1052. displayMem (actLen, pDescrBuf, fout);
  1053. }
  1054.     OSS_FREE (pDescrBuf);
  1055.     return RET_CONTINUE;
  1056.     }
  1057. /*************************************************************************
  1058. *
  1059. * cmdGetSynchFrame - retrieve current synch frame from a device endpoint
  1060. *
  1061. * RETURNS:  RET_CONTINUE;
  1062. */
  1063. LOCAL UINT16 cmdGetSynchFrame
  1064.     (
  1065.     pVOID Param, /* Generic parameter passed down */
  1066.     char **ppCmd, /* Ptr to remainder of cmd line */
  1067.     FILE *fin, /* stream for input (if any) */
  1068.     FILE *fout /* stream for output (if any) */
  1069.     )
  1070.     {
  1071.     long nodeId;
  1072.     long endpoint;
  1073.     UINT16 * pSynchFrame;
  1074.     UINT16 s;
  1075.     /* Extract parameters. */
  1076.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  1077.     *ppCmd = GetHexToken (*ppCmd, &endpoint, -1);
  1078.     /* Validate parameters */
  1079.     if (nodeId == -1 || endpoint == -1)
  1080. {
  1081. fprintf (fout, "Must specify a node id and endpoint.n");
  1082. return RET_CONTINUE;
  1083. }
  1084.     if ((pSynchFrame = OSS_MALLOC (sizeof (UINT16))) == NULL)
  1085.         return RET_CONTINUE;
  1086.     /* Execute function. */
  1087.     if ((s = usbdSynchFrameGet (usbdClientHandle, 
  1088. (GENERIC_HANDLE) nodeId, 
  1089. endpoint, 
  1090. pSynchFrame)) 
  1091. != OK)
  1092. fprintf (fout, "usbdSynchFrameGet() returned %dn", s);
  1093.     else
  1094. fprintf (fout, "synch frame = 0x%4.4xn", FROM_LITTLEW (*pSynchFrame));
  1095.     OSS_FREE (pSynchFrame);
  1096.     return RET_CONTINUE;
  1097.     }
  1098. /*************************************************************************
  1099. *
  1100. * cmdGetCurrentFrame - retrieve current frame for a USB
  1101. *
  1102. * RETURNS:  RET_CONTINUE
  1103. */
  1104. LOCAL UINT16 cmdGetCurrentFrame
  1105.     (
  1106.     pVOID Param, /* Generic parameter passed down */
  1107.     char **ppCmd, /* Ptr to remainder of cmd line */
  1108.     FILE *fin, /* stream for input (if any) */
  1109.     FILE *fout /* stream for output (if any) */
  1110.     )
  1111.     {
  1112.     long nodeId;
  1113.     UINT32 frameNo;
  1114.     UINT32 frameWindow;
  1115.     UINT16 s;
  1116.     /* Extract parameters. */
  1117.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  1118.     /* Validate parameters */
  1119.     if (nodeId == -1)
  1120. {
  1121. fprintf (fout, "Must specify a node id.n");
  1122. return RET_CONTINUE;
  1123. }
  1124.     /* Execute function. */
  1125.     if ((s = usbdCurrentFrameGet (usbdClientHandle, (GENERIC_HANDLE) nodeId, 
  1126. &frameNo, &frameWindow)) != OK)
  1127. fprintf (fout, "usbdCurrentFrameGet() returned %dn", s);
  1128.     else
  1129. {
  1130. fprintf (fout, "frame number = %ldn", (long) frameNo);
  1131. fprintf (fout, "frame window = %ldn", (long) frameWindow);
  1132. }
  1133.     return RET_CONTINUE;
  1134.     }
  1135. /*************************************************************************
  1136. *
  1137. * enterThread - waits for user to press [enter]
  1138. *
  1139. * RETURNS:  N/A
  1140. */
  1141. LOCAL VOID enterThread
  1142.     (
  1143.     pVOID param
  1144.     )
  1145.     {
  1146.     FILE *fout = (FILE *) param;
  1147.     char bfr [256];
  1148.     fprintf (fout, "Press [enter] to terminate polling.n");
  1149.     gets (bfr);
  1150.     enterPressed = TRUE;
  1151.     }
  1152. /*************************************************************************
  1153. *
  1154. * irpCallback - called when IRP completes
  1155. *
  1156. * RETURNS:  N/A
  1157. */
  1158. LOCAL VOID irpCallback
  1159.     (
  1160.     pVOID pIrp
  1161.     )
  1162.     {
  1163.     irpCallbackInvoked = TRUE;
  1164.     }
  1165. /*************************************************************************
  1166. *
  1167. * cmdIntPoll - polls interrupt endpoint for input
  1168. *
  1169. * Continuously reads IN packets from interrupt endpoint until key 
  1170. * pressed.
  1171. *
  1172. * RETURNS:  RET_CONTINUE
  1173. */
  1174. LOCAL UINT16 cmdIntPoll
  1175.     (
  1176.     pVOID Param, /* Generic parameter passed down */
  1177.     char **ppCmd, /* Ptr to remainder of cmd line */
  1178.     FILE *fin, /* stream for input (if any) */
  1179.     FILE *fout /* stream for output (if any) */
  1180.     )
  1181.     {
  1182.     THREAD_HANDLE thread;
  1183.     long nodeId;
  1184.     long endpoint;
  1185.     long length;
  1186.     USBD_PIPE_HANDLE handle;
  1187.     char bfr [256];
  1188.     USB_IRP irp;
  1189.     UINT16 s;
  1190.     /* Extract parameters. */
  1191.     *ppCmd = GetHexToken (*ppCmd, &nodeId, -1);
  1192.     *ppCmd = GetHexToken (*ppCmd, &endpoint, -1);
  1193.     *ppCmd = GetHexToken (*ppCmd, &length, USB_MIN_CTRL_PACKET_SIZE);
  1194.     /* Validate parameters */
  1195.     if (nodeId == -1 || endpoint == -1)
  1196. {
  1197. fprintf (fout, "Must specify a node id and endpoint.n");
  1198. return RET_CONTINUE;
  1199. }
  1200.     if (length > sizeof (bfr))
  1201. {
  1202. fprintf (fout, "Length must be %d or less.n", (int) sizeof (bfr));
  1203. return RET_CONTINUE;
  1204. }
  1205.     /* Create a pipe to talk to this endpoint */
  1206.     if ((s = usbdPipeCreate (usbdClientHandle, (GENERIC_HANDLE) nodeId, (UINT16) endpoint, 0, 0,
  1207. USB_XFRTYPE_INTERRUPT, USB_DIR_IN, length, length, INT_PIPE_SRVC_INTERVAL,
  1208. &handle)) != OK)
  1209. {
  1210. fprintf (fout, "usbdPipeCreate() returned %dn", s);
  1211. return RET_CONTINUE;
  1212. }
  1213.     /* Create thread to watch for keypress */
  1214.     enterPressed = FALSE;
  1215.     if (OSS_THREAD_CREATE (enterThread, (pVOID) fout, OSS_PRIORITY_INHERIT, "tEnter",
  1216. &thread) != OK)
  1217. goto pipe_done;
  1218.     while (!enterPressed)
  1219. {
  1220. /* initialize IRP */
  1221. memset (&irp, 0, sizeof (irp));
  1222. irp.irpLen = sizeof (irp);
  1223. irp.userCallback = irpCallback;
  1224. irp.transferLen = length;
  1225. irp.bfrCount = 1;
  1226. irp.bfrList [0].pid = USB_PID_IN;
  1227. irp.bfrList [0].pBfr = bfr;
  1228. irp.bfrList [0].bfrLen = length;
  1229.     
  1230. irpCallbackInvoked = FALSE;
  1231. if ((s = usbdTransfer (usbdClientHandle, handle, &irp)) != OK)
  1232.     {
  1233.     fprintf (fout, "usbdTransfer() returned %dn", s);
  1234.     break;
  1235.     }
  1236. while (!enterPressed && !irpCallbackInvoked)
  1237.     OSS_THREAD_SLEEP (1);
  1238. if (irpCallbackInvoked)
  1239.     {
  1240.     if (irp.result != OK)
  1241. {
  1242. fprintf (fout, "irp.result == %dn", irp.result);
  1243. break;
  1244. }
  1245.     fprintf (fout, "actLen = %d: ", irp.bfrList [0].actLen);
  1246.     displayMem (irp.bfrList [0].actLen, bfr, fout);
  1247.     }
  1248. else
  1249.     {
  1250.     if ((s = usbdTransferAbort (usbdClientHandle, handle, &irp)) 
  1251. != OK)
  1252. {
  1253. fprintf (fout, "usbdTransferAbort() returned %dn", s);
  1254. break;
  1255. }
  1256.     /* wait for the callback to be invoked */
  1257.     while (!irpCallbackInvoked)
  1258. OSS_THREAD_SLEEP (1);
  1259.     }
  1260. }
  1261.     OSS_THREAD_DESTROY (thread);
  1262. pipe_done:
  1263.     if ((s = usbdPipeDestroy (usbdClientHandle, handle)) != OK)
  1264. {
  1265. fprintf (fout, "usbdPipeDestroy() returned %dn", s);
  1266. return RET_CONTINUE;
  1267. }
  1268.     return RET_CONTINUE;
  1269.     }
  1270. #ifdef INCLUDE_USB_KEYBOARD
  1271. /*************************************************************************
  1272. *
  1273. * cmdKbdInit - initializes USB keyboard SIO driver
  1274. *
  1275. * RETURNS: RET_CONTINUE
  1276. */
  1277. LOCAL UINT16 cmdKbdInit
  1278.     (
  1279.     pVOID Param, /* Generic parameter passed down */
  1280.     char **ppCmd, /* Ptr to remainder of cmd line */
  1281.     FILE *fin, /* stream for input (if any) */
  1282.     FILE *fout /* stream for output (if any) */
  1283.     )
  1284.     {
  1285.     if (usbKeyboardDevInit () == OK)
  1286. fprintf (fout, "usbKeyboardDevInit() returned OKn");
  1287.     else
  1288. fprintf (fout, "usbKeyboardDevInit() returned ERRORn");
  1289.     return RET_CONTINUE;
  1290.     }
  1291. /*************************************************************************
  1292. *
  1293. * cmdKbdDown - shuts down USB keyboard SIO driver
  1294. *
  1295. * RETURNS: RET_CONTINUE
  1296. */
  1297. LOCAL UINT16 cmdKbdDown
  1298.     (
  1299.     pVOID Param, /* Generic parameter passed down */
  1300.     char **ppCmd, /* Ptr to remainder of cmd line */
  1301.     FILE *fin, /* stream for input (if any) */
  1302.     FILE *fout /* stream for output (if any) */
  1303.     )
  1304.     {
  1305.     if (usbKeyboardDevShutdown () == OK)
  1306. fprintf (fout, "usbKeyboardDevShutdown() returned OKn");
  1307.     else
  1308. fprintf (fout, "usbKeyboardDevShutdown() returned ERRORn");
  1309.     return RET_CONTINUE;
  1310.     }
  1311. /*************************************************************************
  1312. *
  1313. * kbdAttachCallback - receives callbacks from USB keyboard SIO driver
  1314. *
  1315. * RETURNS: N/A
  1316. */
  1317. LOCAL SIO_CHAN *pKbdSioChan;
  1318. LOCAL VOID kbdAttachCallback
  1319.     (
  1320.     pVOID arg,     /* caller-defined argument */
  1321.     SIO_CHAN *pChan,     /* pointer to affected SIO_CHAN */
  1322.     UINT16 attachCode     /* defined as USB_KBD_xxxx */
  1323.     )
  1324.     {
  1325.     FILE *fout = (FILE *) arg;
  1326.     fprintf (fout, "pChan = %p, attach code = %sn", pChan,
  1327. (attachCode == USB_KBD_ATTACH) ? "USB_KBD_ATTACH" : "USB_KBD_REMOVE");
  1328.     if (attachCode == USB_KBD_ATTACH)
  1329. {
  1330. if (pKbdSioChan == NULL)
  1331.     {
  1332.     if (usbKeyboardSioChanLock (pChan) != OK)
  1333. fprintf (fout, "usbKeyboardSioChanLock() returned ERRORn");
  1334.     else
  1335. pKbdSioChan = pChan;
  1336.     }
  1337. else
  1338.     {
  1339.     fprintf (fout, "Another channel already in use, ignored.n");
  1340.     }
  1341. }
  1342.     else
  1343. {
  1344. if (pChan == pKbdSioChan)
  1345.     {
  1346.     if (usbKeyboardSioChanUnlock (pChan) != OK)
  1347. fprintf (fout, "usbKeyboardSioChanUnlock() returned ERRORn");
  1348.     pKbdSioChan = NULL;
  1349.     }
  1350. }
  1351.     }
  1352. /*************************************************************************
  1353. *
  1354. * cmdKbdPoll - Polls keyboard SIO driver for input
  1355. *
  1356. * RETURNS: RET_CONTINUE
  1357. */
  1358. LOCAL UINT16 cmdKbdPoll
  1359.     (
  1360.     pVOID Param, /* Generic parameter passed down */
  1361.     char **ppCmd, /* Ptr to remainder of cmd line */
  1362.     FILE *fin, /* stream for input (if any) */
  1363.     FILE *fout /* stream for output (if any) */
  1364.     )
  1365.     {
  1366.     THREAD_HANDLE thread;
  1367.     char inChar;
  1368.     /* register for callbacks */
  1369.     pKbdSioChan = NULL;
  1370.     if (usbKeyboardDynamicAttachRegister (kbdAttachCallback, (pVOID) fout) != OK)
  1371. {
  1372. fprintf (fout, "usbKeyboardDynamicAttachRegister() returned ERRORn");
  1373. return RET_CONTINUE;
  1374. }
  1375.     /* Poll for input or until user presses CTRL-Z on USB keyboard or
  1376.      * [enter] on main keyboard. */
  1377.     /* Create thread to watch for keypress */
  1378.     enterPressed = FALSE;
  1379.     if (OSS_THREAD_CREATE (enterThread, (pVOID) fout, OSS_PRIORITY_INHERIT, "tEnter",
  1380. &thread) != OK)
  1381. goto pipe_done;
  1382.     fprintf (fout, "Press CTRL-Z to terminate polling.n");
  1383.     while (!enterPressed)
  1384. {
  1385. if (pKbdSioChan != NULL)
  1386.     {
  1387.     if ((*pKbdSioChan->pDrvFuncs->pollInput) (pKbdSioChan, &inChar) == OK)
  1388. {
  1389. fprintf (fout, "ASCII %3d", inChar);
  1390. if (inChar >= 32)
  1391.     fprintf (fout, " '%c'", inChar);
  1392. fprintf (fout, "n");
  1393. if (inChar == CTRL_Z)
  1394.     {
  1395.     fprintf (fout, "Stopped by CTRL-Zn");
  1396.     break;
  1397.     }
  1398. }
  1399.     }
  1400. OSS_THREAD_SLEEP (1);
  1401. }
  1402.     OSS_THREAD_DESTROY (thread);
  1403. pipe_done:
  1404.     /* unregister */
  1405.     if (usbKeyboardDynamicAttachUnRegister (kbdAttachCallback, (pVOID) fout) != OK)
  1406. fprintf (fout, "usbKeyboardDynamicAttachUnRegister() returned ERRORn");
  1407.     return RET_CONTINUE;
  1408.     }
  1409. #endif /*INCLUDE_USB_KEYBOARD*/
  1410. #ifdef INCLUDE_USB_MOUSE
  1411. /*************************************************************************
  1412. *
  1413. * showMouseState
  1414. *
  1415. * RETURNS: N/A
  1416. */
  1417. LOCAL long mouseX;
  1418. LOCAL long mouseY;
  1419. LOCAL VOID showMouseState
  1420.     (
  1421.     FILE *fout,
  1422.     UINT8 buttons
  1423.     )
  1424.     {
  1425.     fprintf (fout, "rx:%5ld  y:%5ld  B1:%3.3s B2:%3.3s B3:%3.3s",
  1426. mouseX, mouseY, 
  1427. (buttons & MOUSE_BUTTON_1) == 0 ? "UP " : "DWN",
  1428. (buttons & MOUSE_BUTTON_2) == 0 ? "UP " : "DWN",
  1429. (buttons & MOUSE_BUTTON_3) == 0 ? "UP " : "DWN");
  1430.     }
  1431. /*************************************************************************
  1432. *
  1433. * signExtend - extends sign from char to long
  1434. *
  1435. * RETURNS: sign-extended long value
  1436. */
  1437. LOCAL long signExtend
  1438.     (
  1439.     char value
  1440.     )
  1441.     {
  1442.     if ((value & 0x80) != 0)
  1443. return value | 0xffffff00;
  1444.     
  1445.     return value;
  1446.     }
  1447. /*************************************************************************
  1448. *
  1449. * mseRptCallback - invoked when reports received from mouse
  1450. *
  1451. * RETURNS: OK
  1452. */
  1453. LOCAL STATUS mseRptCallback
  1454.     (
  1455.     void *arg,
  1456.     pHID_MSE_BOOT_REPORT pReport
  1457.     )
  1458.     {
  1459.     FILE *fout = (FILE *) arg;
  1460.     long xChange = signExtend (pReport->xDisplacement);
  1461.     long yChange = signExtend (pReport->yDisplacement);
  1462.     mouseX += xChange;
  1463.     mouseY += yChange;
  1464.     showMouseState (fout, pReport->buttonState);
  1465.     return OK;
  1466.     }
  1467. /*************************************************************************
  1468. *
  1469. * mseAttachCallback - receives callbacks from USB mouse SIO driver
  1470. *
  1471. * RETURNS: N/A
  1472. */
  1473. LOCAL SIO_CHAN *pMseSioChan;
  1474. LOCAL VOID mseAttachCallback
  1475.     (
  1476.     pVOID arg,     /* caller-defined argument */
  1477.     SIO_CHAN *pChan,     /* pointer to affected SIO_CHAN */
  1478.     UINT16 attachCode     /* defined as USB_KBD_xxxx */
  1479.     )
  1480.     {
  1481.     FILE *fout = (FILE *) arg;
  1482.     fprintf (fout, "pChan = %p, attach code = %sn", pChan,
  1483. (attachCode == USB_MSE_ATTACH) ? "USB_MSE_ATTACH" : "USB_MSE_REMOVE");
  1484.     if (attachCode == USB_MSE_ATTACH)
  1485. {
  1486. if (pMseSioChan == NULL)
  1487.     {
  1488.     if (usbMouseSioChanLock (pChan) != OK)
  1489. fprintf (fout, "usbMouseSioChanLock() returned ERRORn");
  1490.     else
  1491. {
  1492. pMseSioChan = pChan;
  1493. /* Register for report callbacks */
  1494.     
  1495. if (pMseSioChan != NULL)
  1496.     {
  1497.     if ((*pMseSioChan->pDrvFuncs->callbackInstall) (pMseSioChan,
  1498. SIO_CALLBACK_PUT_MOUSE_REPORT, 
  1499.     (STATUS  (*) (void *, ...)) mseRptCallback, 
  1500. fout) != OK)
  1501. {
  1502. fprintf (fout, "callbackInstall() returned ERROR.n");
  1503. }
  1504.     }
  1505. }
  1506.     }
  1507. else
  1508.     {
  1509.     fprintf (fout, "Another channel already in use, ignored.n");
  1510.     }
  1511. }
  1512.     else
  1513. {
  1514. if (pChan == pMseSioChan)
  1515.     {
  1516.     if (usbMouseSioChanUnlock (pChan) != OK)
  1517. fprintf (fout, "usbMouseSioChanUnlock() returned ERRORn");
  1518.     pMseSioChan = NULL;
  1519.     }
  1520. }
  1521.     }
  1522. /*************************************************************************
  1523. *
  1524. * cmdMouseTest - Tests mouse SIO driver for input
  1525. *
  1526. * RETURNS: RET_CONTINUE
  1527. */
  1528. LOCAL UINT16 cmdMouseTest
  1529.     (
  1530.     pVOID Param, /* Generic parameter passed down */
  1531.     char **ppCmd, /* Ptr to remainder of cmd line */
  1532.     FILE *fin, /* stream for input (if any) */
  1533.     FILE *fout /* stream for output (if any) */
  1534.     )
  1535.     {
  1536.     THREAD_HANDLE thread;
  1537.     /* Initialize usbMouseLib. */
  1538.     if (usbMouseDevInit () != OK)
  1539. {
  1540. fprintf (fout, "usbMouseDevInit() returned ERRORn");
  1541. return RET_CONTINUE;
  1542. }
  1543.     /* register for attach callbacks */
  1544.     pMseSioChan = NULL;
  1545.     mouseX = 0;
  1546.     mouseY = 0;
  1547.     if (usbMouseDynamicAttachRegister (mseAttachCallback, (pVOID) fout) != OK)
  1548. {
  1549. fprintf (fout, "usbMouseDynamicAttachRegister() returned ERRORn");
  1550. goto mouseDown;
  1551. }
  1552.     /* Wait for use to press [enter] to terminate test. */
  1553.     /* Create thread to watch for keypress */
  1554.     enterPressed = FALSE;
  1555.     if (OSS_THREAD_CREATE (enterThread, (pVOID) fout, OSS_PRIORITY_INHERIT, "tEnter",
  1556. &thread) != OK)
  1557. {
  1558. fprintf (fout, "Failed to create tEnter thread.n");
  1559. goto mouseUnreg;
  1560. }
  1561.     /* Wait for mouse to be attached */
  1562.     while (pMseSioChan == NULL)
  1563. {
  1564. if (enterPressed)
  1565.     goto threadDone;
  1566. OSS_THREAD_SLEEP (1);
  1567. }
  1568.     /* Let background threads show mouse state */
  1569.     showMouseState (fout, 0);
  1570.     while (!enterPressed)
  1571. OSS_THREAD_SLEEP (1);
  1572.     fprintf (fout, "n");
  1573. threadDone:
  1574.     OSS_THREAD_DESTROY (thread);
  1575. mouseUnreg:
  1576.     /* unregister for attach callbacks */
  1577.     if (usbMouseDynamicAttachUnRegister (mseAttachCallback, (pVOID) fout) != OK)
  1578. fprintf (fout, "usbMouseDynamicAttachUnRegister() returned ERRORn");
  1579.     /* Terminate report callbacks */
  1580.     if (pMseSioChan != NULL)
  1581. if ((*pMseSioChan->pDrvFuncs->callbackInstall) (pMseSioChan, 
  1582.     SIO_CALLBACK_PUT_MOUSE_REPORT, NULL, NULL) != OK)
  1583.     {
  1584.     fprintf (fout, "callbackInstall() returned ERROR.n");
  1585.     }
  1586. mouseDown:
  1587.     if (usbMouseDevShutdown () != OK)
  1588. fprintf (fout, "usbMouseDevShutdown() returned ERRORn");
  1589.     return RET_CONTINUE;
  1590.     }
  1591. #endif /*INCLUDE_USB_KEYBOARD*/
  1592. #ifdef INCLUDE_USB_PRINTER
  1593. /*************************************************************************
  1594. *
  1595. * closeTxFile - closes any open print tx file 
  1596. *
  1597. * RETURNS: N/A
  1598. */
  1599. LOCAL VOID closeTxFile (void)
  1600.     {
  1601.     txCharCount = 0;
  1602.     
  1603.     if (txFile != NULL)
  1604. {
  1605. fclose (txFile);
  1606. txFile = NULL;
  1607. }
  1608.     }
  1609. /*************************************************************************
  1610. *
  1611. * prnTxCallback - feeds characters to USB printer SIO driver
  1612. *
  1613. * RETURNS: OK
  1614. */
  1615. LOCAL STATUS prnTxCallback
  1616.     (
  1617.     void *callbackParam,
  1618.     char *txChar
  1619.     )
  1620.     {
  1621.     /* If no more chars to send, return an error */
  1622.     if (txCharCount == 0)
  1623. return ERROR;
  1624.     txCharCount--;
  1625.     /* Check if running a pattern test or a file dump */
  1626.     if (patternTest)
  1627. {
  1628. /* Running a pattern test.  Return the next pattern char */
  1629. if ((nextCharVal & 1) == 0)
  1630.     *txChar = nextCharVal >> 8;
  1631. else
  1632.     *txChar = nextCharVal & 0xff;
  1633. nextCharVal++;
  1634. }
  1635.     else
  1636. {
  1637. /* Running a file dump test.  Return the next char from file */
  1638. if (txBfrCount == 0 && txCharCount > 0)
  1639.     {
  1640.     /* Read next buffer from file */
  1641.     txBfrCount = fread (txBfr, sizeof (char), sizeof (txBfr), txFile);
  1642.     txBfrIndex = 0;
  1643.     }
  1644. if (txCharCount == 0 || txBfrCount == 0)
  1645.     {
  1646.     closeTxFile ();
  1647.     txCharCount = 0;
  1648.     }
  1649. if (txBfrCount == 0)
  1650.     return ERROR;
  1651. *txChar = txBfr [txBfrIndex++];
  1652. --txBfrCount;
  1653. }
  1654.     return OK;
  1655.     }
  1656. /*************************************************************************
  1657. *
  1658. * prnAttachCallback - receives attach callbacks from printer SIO driver
  1659. *
  1660. * RETURNS: N/A
  1661. */
  1662. LOCAL SIO_CHAN *pPrnSioChan;
  1663. LOCAL VOID prnAttachCallback
  1664.     (
  1665.     pVOID arg,     /* caller-defined argument */
  1666.     SIO_CHAN *pChan,     /* pointer to affected SIO_CHAN */
  1667.     UINT16 attachCode     /* defined as USB_KBD_xxxx */
  1668.     )
  1669.     {
  1670.     FILE *fout = (FILE *) arg;
  1671.     fprintf (fout, "pChan = %p, attach code = %sn", pChan,
  1672. (attachCode == USB_PRN_ATTACH) ? "USB_PRN_ATTACH" : "USB_PRN_REMOVE");
  1673.     if (attachCode == USB_PRN_ATTACH)
  1674. {
  1675. if (pPrnSioChan == NULL)
  1676.     {
  1677.     if (usbPrinterSioChanLock (pChan) != OK)
  1678. fprintf (fout, "usbPrinterSioChanLock() returned ERRORn");
  1679.     else
  1680. {
  1681. pPrnSioChan = pChan;
  1682. if ((*pPrnSioChan->pDrvFuncs->callbackInstall) (pPrnSioChan, 
  1683.      SIO_CALLBACK_GET_TX_CHAR, 
  1684.     (STATUS  (*) (void *, ...)) prnTxCallback, 
  1685. NULL) != OK)
  1686.     {
  1687.     fprintf (fout, "callbackInstall() returned ERROR.n");
  1688.     }
  1689. }
  1690.     }
  1691. else
  1692.     {
  1693.     fprintf (fout, "Another channel already in use, ignored.n");
  1694.     }
  1695. }
  1696.     else
  1697. {
  1698. if (pChan == pPrnSioChan)
  1699.     {
  1700.     if (usbPrinterSioChanUnlock (pChan) != OK)
  1701. fprintf (fout, "usbPrinterSioChanUnlock() returned ERRORn");
  1702.     pPrnSioChan = NULL;
  1703.     }
  1704. }
  1705.     }
  1706. /*************************************************************************
  1707. *
  1708. * cmdPrnInit - initializes USB printer SIO driver
  1709. *
  1710. * RETURNS: RET_CONTINUE
  1711. */
  1712. LOCAL BOOL prnInitialized = FALSE;
  1713. LOCAL UINT16 cmdPrnInit
  1714.     (
  1715.     pVOID Param, /* Generic parameter passed down */
  1716.     char **ppCmd, /* Ptr to remainder of cmd line */
  1717.     FILE *fin, /* stream for input (if any) */
  1718.     FILE *fout /* stream for output (if any) */
  1719.     )
  1720.     {
  1721.     if (prnInitialized)
  1722. {
  1723. fprintf (fout, "USB printer SIO driver already initialized.n");
  1724. return RET_CONTINUE;
  1725. }
  1726.     if (usbPrinterDevInit () == OK)
  1727. {
  1728. fprintf (fout, "usbPrinterDevInit() returned OKn");
  1729. prnInitialized = TRUE;
  1730. /* Register for attach notification */
  1731. if (usbPrinterDynamicAttachRegister (prnAttachCallback, (pVOID) fout) != OK)
  1732.     {
  1733.     fprintf (fout, "usbPrinterDynamicAttachRegister() returned ERRORn");
  1734.     return RET_CONTINUE;
  1735.     }
  1736. }
  1737.     else
  1738. fprintf (fout, "usbPrinterDevInit() returned ERRORn");
  1739.     return RET_CONTINUE;
  1740.     }
  1741. /*************************************************************************
  1742. *
  1743. * cmdPrnDown - shuts down USB printer SIO driver
  1744. *
  1745. * RETURNS: RET_CONTINUE
  1746. */
  1747. LOCAL UINT16 cmdPrnDown
  1748.     (
  1749.     pVOID Param, /* Generic parameter passed down */
  1750.     char **ppCmd, /* Ptr to remainder of cmd line */
  1751.     FILE *fin, /* stream for input (if any) */
  1752.     FILE *fout /* stream for output (if any) */
  1753.     )
  1754.     {
  1755.     if (!prnInitialized)
  1756. {
  1757. fprintf (fout, "USB printer SIO driver not initialized.n");
  1758. return RET_CONTINUE;
  1759. }
  1760.     prnInitialized = FALSE;
  1761.     pPrnSioChan = NULL;
  1762.     /* unregister */
  1763.     if (usbPrinterDynamicAttachUnRegister (prnAttachCallback, (pVOID) fout) != OK)
  1764. fprintf (fout, "usbPrinterDynamicAttachUnRegister() returned ERRORn");
  1765.     if (usbPrinterDevShutdown () == OK)
  1766. fprintf (fout, "usbPrinterDevShutdown() returned OKn");
  1767.     else
  1768. fprintf (fout, "usbPrinterDevShutdown() returned ERRORn");
  1769.     return RET_CONTINUE;
  1770.     }
  1771. /*************************************************************************
  1772. *
  1773. * waitForPrinter - waits for a printer to be connected
  1774. *
  1775. * RETURNS: OK if printer connected, else ERROR
  1776. */
  1777. LOCAL STATUS waitForPrinter 
  1778.     (
  1779.     FILE *fout
  1780.     )
  1781.     {
  1782.     THREAD_HANDLE thread;
  1783.     UINT8 * pBfr;
  1784.     USB_PRINTER_CAPABILITIES * pCaps;
  1785.     UINT16 idLen;
  1786.     UINT8 protocol;
  1787.     UINT16 i;
  1788.     /* Create thread to watch for keypress */
  1789.     enterPressed = FALSE;
  1790.     if (OSS_THREAD_CREATE (enterThread, 
  1791.    (pVOID) fout, 
  1792.    OSS_PRIORITY_INHERIT, 
  1793.    "tEnter",
  1794.    &thread) 
  1795. != OK)
  1796. {
  1797. fprintf (fout, "Error creating thread.n");
  1798. return ERROR;
  1799. }
  1800.     /* Wait for a printer to be attached. */
  1801.     if (pPrnSioChan == NULL)
  1802. {
  1803. fprintf (fout, "Waiting for printer to be attached...n");
  1804. while (!enterPressed && pPrnSioChan == NULL)
  1805.     OSS_THREAD_SLEEP (1);
  1806. }
  1807.     /* kill keypress thread */
  1808.     OSS_THREAD_DESTROY (thread);
  1809.     if (enterPressed)
  1810. return ERROR;
  1811.     if ((pBfr = OSS_MALLOC (USB_PRN_MAX_DEVICE_ID_LEN)) == NULL)
  1812.         return ERROR;
  1813.     pCaps = (USB_PRINTER_CAPABILITIES *) pBfr;
  1814.     /* Display the printer characteristics. */
  1815.     if ((*pPrnSioChan->pDrvFuncs->ioctl) (pPrnSioChan, 
  1816.   SIO_USB_PRN_DEVICE_ID_GET, 
  1817.   (void *) pBfr) 
  1818. != OK)
  1819. fprintf (fout, "ioctl (SIO_USB_PRN_DEVICE_ID_GET) returned ERROR.n");
  1820.     else
  1821. {
  1822. idLen = FROM_BIGW (pCaps->length);
  1823. fprintf (fout, "Device ID length = %dn", idLen);
  1824. fprintf (fout, "Device ID = ");
  1825. for (i = 0; i < idLen - 2; i++)
  1826.     fprintf (fout, "%c", pCaps->caps [i]);
  1827. fprintf (fout, "n");
  1828. }
  1829.     if ((*pPrnSioChan->pDrvFuncs->ioctl) (pPrnSioChan, SIO_USB_PRN_PROTOCOL_GET,
  1830. (void *) &protocol) != OK)
  1831. fprintf (fout, "ioctl (SIO_USB_PRN_PROTOCOL_GET) returned ERROR.n");
  1832.     else
  1833. {
  1834. fprintf (fout, "protocol = 0x%x ", protocol);
  1835. switch (protocol)
  1836.     {
  1837.     case USB_PROTOCOL_PRINTER_UNIDIR:
  1838. fprintf (fout, "(USB_PROTOCOL_PRINTER_UNIDIR)n");
  1839. break;
  1840.     case USB_PROTOCOL_PRINTER_BIDIR:
  1841. fprintf (fout, "(USB_PROTOCOL_PRINTER_BIDIR)n");
  1842. break;
  1843.     default:
  1844. fprintf (fout, "(unknown)n");
  1845. break;
  1846.     }
  1847. }
  1848.     OSS_FREE (pBfr);
  1849.     return OK;
  1850.     }
  1851. /*************************************************************************
  1852. *
  1853. * cmdPrint - performs printer test
  1854. *
  1855. * RETURNS: RET_CONTINUE
  1856. */
  1857. LOCAL UINT16 cmdPrint
  1858.     (
  1859.     pVOID Param, /* Generic parameter passed down */
  1860.     char **ppCmd, /* Ptr to remainder of cmd line */
  1861.     FILE *fin, /* stream for input (if any) */
  1862.     FILE *fout /* stream for output (if any) */
  1863.     )
  1864.     {
  1865.     long blocks;
  1866.     /* Get parameters */
  1867.     *ppCmd = GetHexToken (*ppCmd, &blocks, 1);
  1868.     /* Wait for a printer to be connected */
  1869.     if (waitForPrinter (fout) == OK)
  1870. {
  1871. /* trigger a transmission. */
  1872. patternTest = TRUE;
  1873. nextCharVal = 0;
  1874. txCharCount = blocks * 4096;
  1875. fprintf (fout, "sending %d 4k blocks to printer...n", (UINT16) blocks);
  1876. if ((*pPrnSioChan->pDrvFuncs->txStartup) (pPrnSioChan) != OK)
  1877.     fprintf (fout, "txStartup() returned ERROR.n");
  1878. }
  1879.     return RET_CONTINUE;
  1880.     }
  1881. /*************************************************************************
  1882. *
  1883. * cmdPrintFnm - sends file to printer
  1884. *
  1885. * RETURNS: RET_CONTINUE
  1886. */
  1887. LOCAL UINT16 cmdPrintFnm
  1888.     (
  1889.     pVOID Param, /* Generic parameter passed down */
  1890.     char **ppCmd, /* Ptr to remainder of cmd line */
  1891.     FILE *fin, /* stream for input (if any) */
  1892.     FILE *fout /* stream for output (if any) */
  1893.     )
  1894.     {
  1895.     char fnm [MAX_CMD_LEN];
  1896.     FILE *f;
  1897.     long fsize;
  1898.     /* Get filename parameter */
  1899.     *ppCmd = GetNextToken (*ppCmd, fnm, sizeof (fnm));
  1900.     /* Terminate any current dump test */
  1901.     closeTxFile ();
  1902.     /* Attempt to open file */
  1903.     if ((f = fopen (fnm, "rb")) == NULL)
  1904. {
  1905. fprintf (fout, "Unable to open '%s'.n", fnm);
  1906. return RET_CONTINUE;
  1907. }
  1908.     /* Determine file size */
  1909.     fseek (f, 0, SEEK_END);
  1910.     fsize = ftell (f);
  1911.     fprintf (fout, "file '%s' is %ld bytes.n", fnm, fsize);
  1912.     fseek (f, 0, SEEK_SET);
  1913.     /* Wait for printer */
  1914.     if (waitForPrinter (fout) == OK)
  1915. {
  1916. /* Initiate transmission */
  1917. patternTest = FALSE;
  1918. txFile = f;
  1919. txBfrCount = 0;
  1920. txCharCount = fsize;
  1921. fprintf (fout, "sending %ld bytes to printer...n", fsize);
  1922. if ((*pPrnSioChan->pDrvFuncs->txStartup) (pPrnSioChan) != OK)
  1923.     {
  1924.     fprintf (fout, "txStartup() returned ERROR.n");
  1925.     closeTxFile ();
  1926.     }
  1927. /* NOTE: If this path is successful, the file will be closed by
  1928.  * the prnTxCallback().
  1929.  */
  1930. }
  1931.     else
  1932. {
  1933. /* Close input file */
  1934. fclose (f);
  1935. }
  1936.     
  1937.     return RET_CONTINUE;
  1938.     }
  1939. #endif /*INCLUDE_USB_PRINTER*/
  1940. #ifdef INCLUDE_USB_SPEAKER
  1941. /*************************************************************************
  1942. *
  1943. * spkrAttachCallback - receives attach callbacks from speaker SEQ_DEV driver
  1944. *
  1945. * RETURNS: N/A
  1946. */
  1947. LOCAL SEQ_DEV *pSpkrSeqDev = NULL;
  1948. LOCAL VOID spkrAttachCallback
  1949.     (
  1950.     pVOID arg,     /* caller-defined argument */
  1951.     SEQ_DEV *pSeqDev,     /* pointer to affected SEQ_DEV */
  1952.     UINT16 attachCode     /* defined as USB_KBD_xxxx */
  1953.     )
  1954.     {
  1955.     FILE *fout = (FILE *) arg;
  1956.     fprintf (fout, "pSeqDev = %p, attach code = %sn", pSeqDev,
  1957. (attachCode == USB_SPKR_ATTACH) ? "USB_SPKR_ATTACH" : "USB_SPKR_REMOVE");
  1958.     if (attachCode == USB_SPKR_ATTACH)
  1959. {
  1960. if (pSpkrSeqDev == NULL)
  1961.     {
  1962.     if (usbSpeakerSeqDevLock (pSeqDev) != OK)
  1963. fprintf (fout, "usbSpeakerSeqDevLock() returned ERRORn");
  1964.     else
  1965. {
  1966. pSpkrSeqDev = pSeqDev;
  1967. }
  1968.     }
  1969. else
  1970.     {
  1971.     fprintf (fout, "Another channel already in use, ignored.n");
  1972.     }
  1973. }
  1974.     else
  1975. {
  1976. if (pSeqDev == pSpkrSeqDev)
  1977.     {
  1978.     if (usbSpeakerSeqDevUnlock (pSeqDev) != OK)
  1979. fprintf (fout, "usbSpeakerSeqDevUnlock() returned ERRORn");
  1980.     pSpkrSeqDev = NULL;
  1981.     }
  1982. }
  1983.     }
  1984. /***************************************************************************
  1985. *
  1986. * audioThread - Dumps audio data to usbSpeakerLib
  1987. *
  1988. * By convention, <param> is the file handle for the file to be played and 
  1989. * the global "wavDataLength" should be the length of the data chunk.  The
  1990. * file position should be set to the beginning of the data in the data chunk.
  1991. *
  1992. * This thread closes the file after reading all data.
  1993. *
  1994. * RETURNS: N/A
  1995. */
  1996. LOCAL BOOL audioThreadBusy = FALSE;
  1997. LOCAL UINT32 wavDataLen;
  1998. LOCAL VOID audioThread
  1999.     (
  2000.     pVOID param
  2001.     )
  2002.     {
  2003.     FILE *wavFile = (FILE *) param;
  2004.     pUINT8 pBfr;
  2005.     UINT32 remDataLen = wavDataLen;
  2006.     UINT32 actLen;
  2007.     /* Create a buffer for audio data */
  2008.     if ((pBfr = OSS_MALLOC (AUDIO_BFR_SIZE)) == NULL)
  2009. {
  2010. printf ("Out of memory creating audio buffer.n");
  2011. }
  2012.     else
  2013. {
  2014. /* open the audio stream. */
  2015. if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev, 
  2016.     USB_SPKR_IOCTL_OPEN_AUDIO_STREAM, 0) != OK)
  2017.     {
  2018.     printf ("IOCTL OPEN_AUDIO_STREAM returned ERROR.n");
  2019.     }
  2020. else
  2021.     {
  2022.     /* Read audio data and pass it to usbSpeakerLib. */
  2023.     while (remDataLen > 0 &&
  2024. (actLen = fread (pBfr, 1, min (remDataLen, AUDIO_BFR_SIZE), 
  2025.     wavFile)) > 0)
  2026. {
  2027. if ((*pSpkrSeqDev->sd_seqWrt) (pSpkrSeqDev, actLen,
  2028.     pBfr, FALSE) != OK)
  2029.     {
  2030.     printf ("sd_seqWrt() returned ERROR.n");
  2031.     break;
  2032.     }
  2033. else
  2034.     {
  2035.     remDataLen -= actLen;
  2036.     }
  2037. }
  2038.     /* Mark the end of the audio stream. */
  2039.     if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev, 
  2040. USB_SPKR_IOCTL_CLOSE_AUDIO_STREAM, 0) != OK)
  2041. {
  2042. printf ("IOCTL CLOSE_AUDIO_STREAM returned ERROR.n");
  2043. }
  2044.     }
  2045. OSS_FREE (pBfr);
  2046. }
  2047.     /* Close the input file. */
  2048.     fclose (wavFile);
  2049.     audioThreadBusy = FALSE;
  2050.     }
  2051. /*************************************************************************
  2052. *
  2053. * cmdSpkrInit - initializes USB speaker SEQ_DEV driver
  2054. *
  2055. * RETURNS: RET_CONTINUE
  2056. */
  2057. LOCAL BOOL spkrInitialized = FALSE;
  2058. LOCAL UINT16 cmdSpkrInit
  2059.     (
  2060.     pVOID Param, /* Generic parameter passed down */
  2061.     char **ppCmd, /* Ptr to remainder of cmd line */
  2062.     FILE *fin, /* stream for input (if any) */
  2063.     FILE *fout /* stream for output (if any) */
  2064.     )
  2065.     {
  2066.     if (spkrInitialized)
  2067. {
  2068. fprintf (fout, "USB speaker SEQ_DEV driver already initialized.n");
  2069. return RET_CONTINUE;
  2070. }
  2071.     if (usbSpeakerDevInit () == OK)
  2072. {
  2073. fprintf (fout, "usbSpeakerDevInit() returned OKn");
  2074. spkrInitialized = TRUE;
  2075. /* Register for attach notification */
  2076. if (usbSpeakerDynamicAttachRegister (spkrAttachCallback, (pVOID) fout) != OK)
  2077.     {
  2078.     fprintf (fout, "usbSpeakerDynamicAttachRegister() returned ERRORn");
  2079.     return RET_CONTINUE;
  2080.     }
  2081. }
  2082.     else
  2083. fprintf (fout, "usbSpeakerDevInit() returned ERRORn");
  2084.     audioThreadBusy = FALSE;
  2085.     return RET_CONTINUE;
  2086.     }
  2087. /*************************************************************************
  2088. *
  2089. * cmdSpkrDown - shuts down USB speaker SEQ_DEV driver
  2090. *
  2091. * RETURNS: RET_CONTINUE
  2092. */
  2093. LOCAL UINT16 cmdSpkrDown
  2094.     (
  2095.     pVOID Param, /* Generic parameter passed down */
  2096.     char **ppCmd, /* Ptr to remainder of cmd line */
  2097.     FILE *fin, /* stream for input (if any) */
  2098.     FILE *fout /* stream for output (if any) */
  2099.     )
  2100.     {
  2101.     if (!spkrInitialized)
  2102. {
  2103. fprintf (fout, "USB speaker SEQ_DEV driver not initialized.n");
  2104. return RET_CONTINUE;
  2105. }
  2106.     if (audioThreadBusy)
  2107. {
  2108. fprintf (fout, "audioThread is busy.n");
  2109. return RET_CONTINUE;
  2110. }
  2111.     spkrInitialized = FALSE;
  2112.     pSpkrSeqDev = NULL;
  2113.     /* unregister */
  2114.     if (usbSpeakerDynamicAttachUnRegister (spkrAttachCallback, (pVOID) fout) != OK)
  2115. fprintf (fout, "usbSpeakerDynamicAttachUnRegister() returned ERRORn");
  2116.     if (usbSpeakerDevShutdown () == OK)
  2117. fprintf (fout, "usbSpeakerDevShutdown() returned OKn");
  2118.     else
  2119. fprintf (fout, "usbSpeakerDevShutdown() returned ERRORn");
  2120.     return RET_CONTINUE;
  2121.     }
  2122. /*************************************************************************
  2123. *
  2124. * waitForSpeaker - waits for a speaker to be connected
  2125. *
  2126. * RETURNS: OK if speaker connected, else ERROR
  2127. */
  2128. LOCAL STATUS waitForSpeaker
  2129.     (
  2130.     FILE *fout
  2131.     )
  2132.     {
  2133.     THREAD_HANDLE thread;
  2134.     /* Create thread to watch for keypress */
  2135.     enterPressed = FALSE;
  2136.     if (OSS_THREAD_CREATE (enterThread, (pVOID) fout, OSS_PRIORITY_INHERIT, "tEnter",
  2137. &thread) != OK)
  2138. {
  2139. fprintf (fout, "Error creating thread.n");
  2140. return ERROR;
  2141. }
  2142.     /* Wait for a speaker to be attached. */
  2143.     if (pSpkrSeqDev == NULL)
  2144. {
  2145. fprintf (fout, "Waiting for speaker to be attached...n");
  2146. while (!enterPressed && pSpkrSeqDev == NULL)
  2147.     OSS_THREAD_SLEEP (1);
  2148. }
  2149.     /* kill keypress thread */
  2150.     OSS_THREAD_DESTROY (thread);
  2151.     if (enterPressed)
  2152. return ERROR;
  2153.     return OK;
  2154.     }
  2155. /*************************************************************************
  2156. *
  2157. * cmdSpkrFmt - Displays available speaker formats
  2158. *
  2159. * RETURNS: RET_CONTINUE
  2160. */
  2161. LOCAL UINT16 cmdSpkrFmt
  2162.     (
  2163.     pVOID Param, /* Generic parameter passed down */
  2164.     char **ppCmd, /* Ptr to remainder of cmd line */
  2165.     FILE *fin, /* stream for input (if any) */
  2166.     FILE *fout /* stream for output (if any) */
  2167.     )
  2168.     {
  2169.     UINT16 fmtCount, i;
  2170.     pUSB_SPKR_AUDIO_FORMAT pFmt;
  2171.     if (!spkrInitialized)
  2172. {
  2173. fprintf (fout, "USB speaker SEQ_DEV driver not initialized.n");
  2174. return RET_CONTINUE;
  2175. }
  2176.     if (waitForSpeaker (fout) != OK)
  2177. return RET_CONTINUE;
  2178.     /* Get number and info about speaker formats */
  2179.     if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev, 
  2180. USB_SPKR_IOCTL_GET_FORMAT_COUNT, (int) &fmtCount) != OK)
  2181. {
  2182. fprintf (fout, "IOCTL GET_FORMAT_COUNT returned ERRORn");
  2183. return RET_CONTINUE;
  2184. }
  2185.     fprintf (fout, "Speaker supports %d formats.n", fmtCount);
  2186.     if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev, 
  2187. USB_SPKR_IOCTL_GET_FORMAT_LIST, (int) &pFmt) != OK)
  2188. {
  2189. fprintf (fout, "IOCTL GET_FORMAT_LIST returned ERRORn");
  2190. return RET_CONTINUE;
  2191. }
  2192.     for (i = 0; i < fmtCount; i++)
  2193. {
  2194. fprintf (fout, "format [%d]:n", i);
  2195. fprintf (fout, "  interface = %d, alt setting = %dn", 
  2196.     pFmt [i].interface, pFmt [i].altSetting);
  2197. fprintf (fout, "  endpoint = 0x%x, maxPacketSize = %d, delay = %dn",
  2198.     pFmt [i].endpoint, pFmt [i].maxPacketSize, pFmt [i].delay);
  2199. fprintf (fout, "  formatTag = 0x%x, formatType = %dn",
  2200.     pFmt [i].formatTag, pFmt [i].formatType);
  2201. switch (pFmt [i].formatType)
  2202.     {
  2203.     case USB_AUDIO_FORMAT_TYPE1:
  2204.     case USB_AUDIO_FORMAT_TYPE3:
  2205. fprintf (fout, "  channels = %dn", pFmt [i].channels);
  2206. fprintf (fout, "  subFrameSize = %dn", pFmt [i].subFrameSize);
  2207. fprintf (fout, "  bitRes = %dn", pFmt [i].bitRes);
  2208. break;
  2209.     case USB_AUDIO_FORMAT_TYPE2:
  2210. fprintf (fout, "  maxBitRate = %dn", pFmt [i].maxBitRate);
  2211. fprintf (fout, "  samplesPerFrame = %dn", pFmt [i].samplesPerFrame);
  2212. break;
  2213.     default:
  2214. fprintf (fout, "  <<unrecognized format type>>n");
  2215. break;
  2216.     }
  2217. }
  2218.     return RET_CONTINUE;
  2219.     }
  2220. /***************************************************************************
  2221. *
  2222. * parseWavFile - parses and displays info about a .wav file
  2223. *
  2224. * Attempts to play the .wav file.
  2225. *
  2226. * NOTE: If this function returns TRUE, the caller SHOULD NOT close the
  2227. * wavFile.  That will be done automatically when playing is finished.
  2228. * RETURNS: OK if able to play file, else ERROR.
  2229. */
  2230. LOCAL STATUS parseWavFile
  2231.     (
  2232.     FILE *wavFile,
  2233.     FILE *fout
  2234.     )
  2235.     {
  2236.     RIFF_HDR riffHdr;
  2237.     char wavSig [RIFF_WAV_DATA_SIG_LEN];
  2238.     RIFF_CHUNK_HDR chunkHdr;
  2239.     WAV_FORMAT_CHUNK fmtChunk;
  2240.     UINT32 fileLen;
  2241.     UINT32 chunkLen;
  2242.     int i;
  2243.     USB_SPKR_AUDIO_FORMAT fmt;
  2244.     THREAD_HANDLE thread;
  2245.     /* Check the RIFF/WAV header. */
  2246.     if (fseek (wavFile, 0L, SEEK_SET) != 0) 
  2247. {
  2248. fprintf (fout, "Cannot seek to beginning of file.n");
  2249. return ERROR;
  2250. }
  2251.     if (fread (&riffHdr, 1, sizeof (riffHdr), wavFile) < sizeof (riffHdr) ||
  2252. fread (&wavSig, 1, sizeof (wavSig), wavFile) < sizeof (wavSig)) 
  2253. {
  2254. fprintf (fout, "Unexpected end of file reading RIFF header.n");
  2255. return ERROR;
  2256. }
  2257.     if (memcmp (&riffHdr.signature, RIFF_HDR_SIG, RIFF_HDR_SIG_LEN) != 0 ||
  2258. memcmp (wavSig, RIFF_WAV_DATA_SIG, RIFF_WAV_DATA_SIG_LEN) != 0) 
  2259. {
  2260. fprintf (fout, "Not a .wav file.n");
  2261. return ERROR;
  2262. }
  2263.     /* Read and display known chunks */
  2264.     fileLen = FROM_LITTLEL (riffHdr.length) + sizeof (riffHdr);
  2265.     fprintf (fout, ".wav file size = %lu bytes.n", (unsigned long) fileLen);
  2266.     while ((UINT32) ftell (wavFile) < fileLen)
  2267. {
  2268. fprintf (fout, "n");
  2269. /* Read the next chunk header. */
  2270. if (fread (&chunkHdr, 1, sizeof (chunkHdr), wavFile) < sizeof (chunkHdr))
  2271.     {
  2272.     fprintf (fout, "Unexpected end of file reading chunk header.n");
  2273.     return ERROR;
  2274.     }
  2275. fprintf (fout, "ChunkId = ");
  2276. for (i = 0; i < RIFF_CHUNK_ID_LEN; i++)
  2277.     fprintf (fout, "%c", chunkHdr.chunkId [i]);
  2278. chunkLen = FROM_LITTLEL (chunkHdr.length);
  2279. fprintf (fout, "nChunkLen = %lun", (unsigned long) chunkLen);
  2280. /* If we recognize the chunkId, then display the chunk. */
  2281. if (memcmp (chunkHdr.chunkId, RIFF_WAV_FMT_CHUNK_ID, RIFF_CHUNK_ID_LEN) == 0)
  2282.     {
  2283.     /* Read the format chunk. */
  2284.     if (fread (&fmtChunk, 1, sizeof (fmtChunk), wavFile) < sizeof (fmtChunk))
  2285. {
  2286. fprintf (fout, "Unexpected end of file reading format chunk.n");
  2287. return ERROR;
  2288. }
  2289.     /* Display the format chunk */
  2290.     fprintf (fout, "formatTag = 0x%4.4xn", FROM_LITTLEW (fmtChunk.formatTag));
  2291.     fprintf (fout, "channels = %dn", FROM_LITTLEW (fmtChunk.channels));
  2292.     fprintf (fout, "samplesPerSec = %lun", 
  2293. (unsigned long) FROM_LITTLEL (fmtChunk.samplesPerSec));
  2294.     fprintf (fout, "avgBytesPerSec = %lun", 
  2295. (unsigned long) FROM_LITTLEL (fmtChunk.avgBytesPerSec));
  2296.     fprintf (fout, "blockAlign = %dn", FROM_LITTLEW (fmtChunk.blockAlign));
  2297.     if (FROM_LITTLEW (fmtChunk.formatTag) == WAV_FMT_MS_PCM)
  2298. fprintf (fout, "bitsPerSample = %dn", 
  2299.     FROM_LITTLEW (fmtChunk.fmt.msPcm.bitsPerSample));
  2300.     /* Attempt to set the audio format to match */
  2301.     if (FROM_LITTLEW (fmtChunk.formatTag) == WAV_FMT_MS_PCM)
  2302. {
  2303. memset (&fmt, 0, sizeof (fmt));
  2304. fmt.formatTag = USB_AUDIO_TYPE1_PCM;
  2305. fmt.formatType = USB_AUDIO_FORMAT_TYPE1;
  2306. fmt.channels = FROM_LITTLEW (fmtChunk.channels);
  2307. fmt.subFrameSize = FROM_LITTLEW (fmtChunk.blockAlign) / 
  2308.     FROM_LITTLEW (fmtChunk.channels);
  2309. fmt.bitRes = FROM_LITTLEW (fmtChunk.fmt.msPcm.bitsPerSample);
  2310. fmt.sampleFrequency = FROM_LITTLEL (fmtChunk.samplesPerSec);
  2311. if (pSpkrSeqDev->sd_ioctl (pSpkrSeqDev, 
  2312.     USB_SPKR_IOCTL_SET_AUDIO_FORMAT, (int) &fmt) == OK)
  2313.     {
  2314.     fprintf (fout, "nusbSpeakerLib format set successfully.n");
  2315.     }
  2316. else
  2317.     {
  2318.     fprintf (fout, "nFailed to set usbSpeakerLib format.n");
  2319.     return ERROR;
  2320.     }
  2321. }
  2322.     }
  2323. else if (memcmp (chunkHdr.chunkId, RIFF_WAV_DATA_CHUNK_SIG, RIFF_CHUNK_ID_LEN) == 0)
  2324.     {
  2325.     /* data chunk found */
  2326.     /* launch thread to dump audio data. */
  2327.     wavDataLen = FROM_LITTLEL (chunkHdr.length);
  2328.     if (OSS_THREAD_CREATE (audioThread, (pVOID) wavFile, OSS_PRIORITY_INHERIT, 
  2329. "tPlay", &thread) != OK)
  2330. {
  2331. fprintf (fout, "Cannot create audio play thread.n");
  2332. return ERROR;
  2333. }
  2334.     else
  2335. {
  2336. audioThreadBusy = TRUE;
  2337. fprintf (fout, "Thread created to play audio data.n");
  2338. return OK;
  2339. }
  2340.     }
  2341. else
  2342.     {
  2343.     /* Skip over the chunk. */
  2344.     fseek (wavFile, chunkLen, SEEK_CUR);
  2345.     }
  2346. }
  2347.     return ERROR;
  2348.     }
  2349. /*************************************************************************
  2350. *
  2351. * cmdPlay - sends a .wav file to a speaker
  2352. *
  2353. * RETURNS: RET_CONTINUE
  2354. */
  2355. LOCAL UINT16 cmdPlay
  2356.     (
  2357.     pVOID Param, /* Generic parameter passed down */
  2358.     char **ppCmd, /* Ptr to remainder of cmd line */
  2359.     FILE *fin, /* stream for input (if any) */
  2360.     FILE *fout /* stream for output (if any) */
  2361.     )
  2362.     {
  2363.     char fnm [MAX_CMD_LEN];
  2364.     FILE *f;
  2365.     /* Get filename parameter */
  2366.     *ppCmd = GetNextToken (*ppCmd, fnm, sizeof (fnm));
  2367.     /* Make sure usbSpeakerLib is initialized and a speaker is available */
  2368.     if (!spkrInitialized)
  2369. {
  2370. fprintf (fout, "USB speaker SEQ_DEV driver not initialized.n");
  2371. return RET_CONTINUE;
  2372. }
  2373.     if (audioThreadBusy)
  2374. {
  2375. fprintf (fout, "audioThread is busy.n");
  2376. return RET_CONTINUE;
  2377. }
  2378.     if (waitForSpeaker (fout) != OK)
  2379. return RET_CONTINUE;
  2380.     /* Attempt to open the file. */
  2381.     if ((f = fopen (fnm, "rb")) == NULL) 
  2382. {
  2383. fprintf (fout, "Unable to open '%s'.n", fnm);
  2384. return RET_CONTINUE;
  2385. }
  2386.     /* Parse the file */
  2387.     if (parseWavFile (f, fout) != OK)
  2388. fclose (f);
  2389.     return RET_CONTINUE;
  2390.     }
  2391. /*************************************************************************
  2392. *
  2393. * cmdVolume - displays or sets speaker volume
  2394. *
  2395. * RETURNS: RET_CONTINUE
  2396. */
  2397. LOCAL UINT16 cmdVolume
  2398.     (
  2399.     pVOID Param, /* Generic parameter passed down */
  2400.     char **ppCmd, /* Ptr to remainder of cmd line */
  2401.     FILE *fin, /* stream for input (if any) */
  2402.     FILE *fout /* stream for output (if any) */
  2403.     )
  2404.     {
  2405.     long volume;
  2406.     short volSetting;
  2407.     UINT16 channels;
  2408.     pUSB_SPKR_CHANNEL_CAPS pCaps;
  2409.     UINT16 i;
  2410.     /* verify a speaker is available */
  2411.     if (!spkrInitialized)
  2412. {
  2413. fprintf (fout, "USB speaker SEQ_DEV driver not initialized.n");
  2414. return RET_CONTINUE;
  2415. }
  2416.     if (waitForSpeaker (fout) != OK)
  2417. return RET_CONTINUE;
  2418.     /* Try to get channel information. */
  2419.     if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev, USB_SPKR_IOCTL_GET_CHANNEL_COUNT, 
  2420. (int) &channels) != OK)
  2421. {
  2422. fprintf (fout, "IOCTL GET_CHANNEL_COUNT returned ERROR.n");
  2423. return RET_CONTINUE;
  2424. }
  2425.     if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev, USB_SPKR_IOCTL_GET_CHANNEL_CAPS, 
  2426. (int) &pCaps) != OK)
  2427. {
  2428. fprintf (fout, "IOCTL GET_CHANNEL_CAPS returned ERROR.n");
  2429. return RET_CONTINUE;
  2430. }
  2431.     /* Get volume parameter (if specified). */
  2432.     *ppCmd = GetHexToken (*ppCmd, &volume, -1);
  2433.     /* If volume specified, then set it, else display current volume info */
  2434.     for (i = 0; i <= channels; i++)
  2435. {
  2436. fprintf (fout, "Channel %d: ", i);
  2437. if (!pCaps [i].volume.supported)
  2438.     {
  2439.     fprintf (fout, "Volume not supported.n");
  2440.     }
  2441. else
  2442.     {
  2443.     if (volume != -1)
  2444. {
  2445. /* Set volume */
  2446. volSetting = (short) (volume & 0xffff);
  2447. if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev, 
  2448.     USB_SPKR_IOCTL_SET_VOLUME, 
  2449.     (int) ((i << 16) | ((UINT16) volSetting))) == OK)
  2450.     {
  2451.     fprintf (fout, "Volume set to %hx.n", volSetting);
  2452.     }
  2453. else
  2454.     {
  2455.     fprintf (fout, "Error setting volume.n");
  2456.     }
  2457. }
  2458.     else
  2459. {
  2460. /* Show volume settings. */
  2461. fprintf (fout, "res = %hx, min = %hx, max = %hx, cur = %hx.n",
  2462.     pCaps [i].volume.res, pCaps [i].volume.min, 
  2463.     pCaps [i].volume.max, pCaps [i].volume.cur);
  2464. }
  2465.     }
  2466. }
  2467.     return RET_CONTINUE;
  2468.     }
  2469. #endif /*INCLUDE_USB_SPEAKER*/
  2470. #endif /*INCLUDE_USB  from way above*/
  2471. #ifdef INCLUDE_USB_TARG
  2472. /*************************************************************************
  2473. *
  2474. * cmdTargInit - initializes USB target stack
  2475. *
  2476. * RETURNS: RET_CONTINUE
  2477. */
  2478. LOCAL UINT16 cmdTargInit
  2479.     (
  2480.     pVOID Param, /* Generic parameter passed down */
  2481.     char **ppCmd, /* Ptr to remainder of cmd line */
  2482.     FILE *fin, /* stream for input (if any) */
  2483.     FILE *fout /* stream for output (if any) */
  2484.     )
  2485.     {
  2486.     USB_TCD_PDIUSBD12_PARAMS params;
  2487.     char targType [MAX_CMD_LEN];
  2488.     pUSB_TARG_CALLBACK_TABLE callbackTable;
  2489.     pVOID callbackParam;
  2490.     long ioBase;
  2491.     long irq;
  2492.     long dma;
  2493.     /* if already initialized, just show a warning */
  2494.     if (targInit)
  2495. {
  2496. fprintf (fout, "Already initialized.n");
  2497. return RET_CONTINUE;
  2498. }
  2499.     /* get parameters */
  2500.     *ppCmd = GetNextToken (*ppCmd, targType, sizeof (targType));
  2501.     *ppCmd = GetHexToken (*ppCmd, &ioBase, D12EVAL_DEFAULT_IOBASE);
  2502.     *ppCmd = GetHexToken (*ppCmd, &irq, D12EVAL_DEFAULT_IRQ);
  2503.     *ppCmd = GetHexToken (*ppCmd, &dma, D12EVAL_DEFAULT_DMA);
  2504.     /* Validate target type */
  2505.     if (strcmp (targType, "kbd") == 0)
  2506. usbTargKbdCallbackInfo (&callbackTable, &callbackParam);
  2507.     else if (strcmp (targType, "prn") == 0)
  2508. usbTargPrnCallbackInfo (&callbackTable, &callbackParam);
  2509.     else if (strcmp (targType, "d12") == 0)
  2510. usbTargPhilipsD12EvalCallbackInfo (&callbackTable, &callbackParam);
  2511.     else
  2512. {
  2513. fprintf (fout, "Must specify target type as 'kbd', 'prn', or 'd12'.n");
  2514. return RET_CONTINUE;
  2515. }
  2516.     /* Initialize usbTargLib */
  2517.     if (usbTargInitialize () == OK)
  2518. fprintf (fout, "usbTargInitialize() returned OKn");
  2519.     else
  2520. {
  2521. fprintf (fout, "usbTargInitialize() returned ERRORn");
  2522. return RET_CONTINUE;
  2523. }
  2524.     targInit = TRUE;
  2525.     /* Attach the Philips TCD to usbTargLib */
  2526.     memset (&params, 0, sizeof (params));
  2527.     params.ioBase = (UINT32) ioBase;
  2528.     params.irq = (UINT16) irq;
  2529.     params.dma = (UINT16) dma;
  2530.     fprintf (fout, "Philips PDIUSBD12: ioBase = 0x%lx, irq = %d, dma = %dn",
  2531. ioBase, (int) irq, (int) dma);
  2532.     if (usbTargTcdAttach (usbTcdPdiusbd12EvalExec, (pVOID) &params,
  2533. callbackTable, callbackParam, &targChannel, 
  2534. &numEndpoints, &pEndpoints) == OK)
  2535. {
  2536. fprintf (fout, "usbTargTcdAttach() returned OKn");
  2537. }
  2538.     else
  2539. {
  2540. fprintf (fout, "usbTargTcdAttach() returned ERRORn");
  2541. targChannel = NULL;
  2542. return RET_CONTINUE;
  2543. }
  2544.     fprintf (fout, "targChannel = 0x%xn", (UINT32) targChannel);
  2545.     fprintf (fout, "numEndpoints = %dn", numEndpoints);
  2546.     fprintf (fout, "pEndpoints = 0x%lxn", (long) pEndpoints);
  2547.     if (usbTargEnable (targChannel) == OK)
  2548. {
  2549. fprintf (fout, "usbTargEnable() returned OKn");
  2550. targEnable = TRUE;
  2551. }
  2552.     else
  2553. fprintf (fout, "usbTargEnable() returned ERRORn");
  2554.     return RET_CONTINUE;
  2555.     }
  2556. /*************************************************************************
  2557. *
  2558. * cmdTargDown - shuts down USB target stack
  2559. *
  2560. * RETURNS: RET_CONTINUE
  2561. */
  2562. LOCAL UINT16 cmdTargDown
  2563.     (
  2564.     pVOID Param, /* Generic parameter passed down */
  2565.     char **ppCmd, /* Ptr to remainder of cmd line */
  2566.     FILE *fin, /* stream for input (if any) */
  2567.     FILE *fout /* stream for output (if any) */
  2568.     )
  2569.     {
  2570.     /* Disabled the TCD */
  2571.     if (targEnable)
  2572. {
  2573. if (usbTargDisable (targChannel) == OK)
  2574.     fprintf (fout, "usbTargDisable() returned OKn");
  2575. else
  2576.     fprintf (fout, "usbTargDisable() returned ERRORn");
  2577. targEnable = FALSE;
  2578. }
  2579.     /* Detach the TCD */
  2580.     if (targChannel != NULL)
  2581. {
  2582. if (usbTargTcdDetach (targChannel) == OK)
  2583.     fprintf (fout, "usbTargTcdDetach() returned OKn");
  2584. else
  2585.     fprintf (fout, "usbTargTcdDetach() returned ERRORn");
  2586. targChannel = NULL;
  2587. }
  2588.     /* Shut down usbTargLib */
  2589.     if (targInit)
  2590. {
  2591. if (usbTargShutdown () == OK)
  2592.     fprintf (fout, "usbTargShutdown() returned OKn");
  2593. else
  2594.     fprintf (fout, "usbTargShutdown() returned ERRORn");
  2595. targInit = FALSE;
  2596. }
  2597.     return RET_CONTINUE;
  2598.     }
  2599. /*************************************************************************
  2600. *
  2601. * cmdKbdReport - injects a keyboard report into keyboard emulator
  2602. *
  2603. * RETURNS: RET_CONTINUE
  2604. */
  2605. LOCAL UINT16 cmdKbdReport
  2606.     (
  2607.     pVOID Param, /* Generic parameter passed down */
  2608.     char **ppCmd, /* Ptr to remainder of cmd line */
  2609.     FILE *fin, /* stream for input (if any) */
  2610.     FILE *fout /* stream for output (if any) */
  2611.     )
  2612.     {
  2613.     HID_KBD_BOOT_REPORT report;
  2614.     pUINT8 pBfr = (pUINT8) &report;
  2615.     long value;
  2616.     UINT16 i;
  2617.     /* The following fills a report with as much info as provided by user */
  2618.     memset (&report, 0, sizeof (report));
  2619.     for (i = 0; i < sizeof (report); i++)
  2620. {
  2621. *ppCmd = GetHexToken (*ppCmd, &value, 0);
  2622. pBfr [i] = (UINT8) value;
  2623. }
  2624.     if (usbTargKbdInjectReport (&report, sizeof (report)) == OK)
  2625. fprintf (fout, "usbTargKbdInjectReport() returned OKn");
  2626.     else
  2627. fprintf (fout, "usbTargKbdInjectReport() return ERRORn");
  2628.     return RET_CONTINUE;
  2629.     }
  2630. /*************************************************************************
  2631. *
  2632. * cmdPrnDump - dumps printer data received by printer emulator
  2633. *
  2634. * RETURNS: RET_CONTINUE
  2635. */
  2636. LOCAL UINT16 cmdPrnDump
  2637.     (
  2638.     pVOID Param, /* Generic parameter passed down */
  2639.     char **ppCmd, /* Ptr to remainder of cmd line */
  2640.     FILE *fin, /* stream for input (if any) */
  2641.     FILE *fout /* stream for output (if any) */
  2642.     )
  2643.     {
  2644.     long length;
  2645.     pUINT8 pBfr;
  2646.     UINT16 actLen;
  2647.     *ppCmd = GetHexToken (*ppCmd, &length, 128);
  2648.     
  2649.     /* See if the printer emulator has received any data */
  2650.     if (usbTargPrnDataInfo (&pBfr, &actLen) != OK)
  2651. {
  2652. fprintf (fout, "Printer has no data.n");
  2653. }
  2654.     else
  2655. {
  2656. fprintf (fout, "Printer received %d bytes.n", actLen);
  2657. displayMem (min (length, actLen), pBfr, fout);
  2658. }
  2659.     if (usbTargPrnDataRestart () != OK)
  2660. fprintf (fout, "usbTargPrnDataRestart() returned ERROR.n");
  2661.     return RET_CONTINUE;
  2662.     }
  2663. #endif /* #ifdef INCLUDE_USB_TARG */
  2664. /*************************************************************************
  2665. *
  2666. * PciClassSearch - Lists devices within a specific Class/Sub-Class/PgmIf
  2667. *
  2668. * List all devices which match the <Class>, <SubClass>, <PgmIf> spec.
  2669. *
  2670. * RETURNS:  N/A
  2671. */
  2672. LOCAL void PciClassSearch
  2673.     (
  2674.     UINT8 pciClass, /* Class to match */
  2675.     UINT8 subClass, /* Subclass to match */
  2676.     UINT8 pgmIf, /* Programming interface to match */
  2677.     FILE *fout /* output stream */
  2678.     )
  2679.     {
  2680.     int i;
  2681.     UINT8 busNo;
  2682.     UINT8 deviceNo;
  2683.     UINT8 funcNo;
  2684.     i = 0;
  2685.     
  2686.     while (usbPciClassFind (pciClass, subClass, pgmIf, i, &busNo, &deviceNo, &funcNo))
  2687. {
  2688. fprintf (fout, 
  2689.     "Class 0x%2.2x%2.2x%2.2x index %d: {Bus,Device,Func} = {0x%x,0x%x,0x%x}n",
  2690.     pciClass, subClass, pgmIf, i, busNo, deviceNo, funcNo);
  2691. i++;
  2692. }
  2693.     }
  2694. /*************************************************************************
  2695. *
  2696. * PciBusSearch - Search for a range of classes/sub-classes/pgmif's
  2697. *
  2698. * Search for devices matching the ranges <classMin>..<classMax>, 
  2699. * <subClassMin>..<subClassMax>, <pgmIfMin>..<pgmIfMax>.  Display
  2700. * matching devices on <fout>.
  2701. *
  2702. * RETURNS:  N/A
  2703. */
  2704. LOCAL void PciBusSearch 
  2705.     (
  2706.     UINT8 classMin, /* Low class */
  2707.     UINT8 classMax, /* High class */
  2708.     UINT8 subClassMin, /* Low subclass */
  2709.     UINT8 subClassMax, /* High subclass */
  2710.     UINT8 pgmIfMin, /* Low pgm if */
  2711.     UINT8 pgmIfMax, /* High pgm if */
  2712.     FILE *fout /* output stream */
  2713.     )
  2714.     {
  2715.     UINT16 pciClass;
  2716.     UINT16 subClass;
  2717.     UINT16 pgmIf;
  2718.     /* Loop through all PCI class/sub-class combinations, searching for
  2719.     devices. */
  2720.     for (pciClass = classMin; pciClass <= classMax; pciClass++)
  2721. {
  2722. for (subClass = subClassMin; subClass <= subClassMax; subClass++)
  2723.     {
  2724.     for (pgmIf = pgmIfMin; pgmIf <= pgmIfMax; pgmIf++)
  2725. {
  2726. PciClassSearch (pciClass, subClass, pgmIf, fout);
  2727. }
  2728.     }
  2729. }
  2730.     }
  2731. /*************************************************************************
  2732. *
  2733. * cmdPciEnumerate - Enumerates devices on PCI bus
  2734. *
  2735. * Enumerates PCI devices of all classes.  <ppCmd> is parsed for options
  2736. * and updated on return to point to the remainder of the command line.
  2737. * <Param> is ignored.  Output is written to <fout>.
  2738. *
  2739. * RETURNS:  RET_CONTINUE
  2740. */
  2741. LOCAL UINT16 cmdPciEnumerate
  2742.     (
  2743.     pVOID Param, /* Generic parameter passed down */
  2744.     char **ppCmd, /* Ptr to remainder of cmd line */
  2745.     FILE *fin, /* stream for input (if any) */
  2746.     FILE *fout /* stream for output (if any) */
  2747.     )
  2748.     {
  2749.     long pgmIfMax;
  2750.     /* Extract parameters. */
  2751.     *ppCmd = GetHexToken (*ppCmd, &pgmIfMax, 0);
  2752.     /* Enumerate bus */
  2753.     fprintf (fout, "Enumerating PCI bus...n");
  2754.     PciBusSearch (0, 255, 0, 255, 0, pgmIfMax, fout);
  2755.     fprintf (fout, "Enumeration complete.n");
  2756.     return RET_CONTINUE;
  2757.     }
  2758. /*************************************************************************
  2759. *
  2760. * cmdPciFindClass
  2761. *
  2762. * Lists all PCI devices matching the class/sub-class/pgmif specification.
  2763. * <ppCmd> is parsed for the class, sub-class, and option pgmif.  <ppCmd>
  2764. * is updated on return to point to remainder of command line.  <Param>
  2765. * is ignored.
  2766. *
  2767. * RETURNS:  RET_CONTINUE
  2768. */
  2769. LOCAL UINT16 cmdPciFindClass
  2770.     (
  2771.     pVOID Param, /* Generic parameter passed down */
  2772.     char **ppCmd, /* Ptr to remainder of cmd line */
  2773.     FILE *fin, /* stream for input (if any) */
  2774.     FILE *fout /* stream for output (if any) */
  2775.     )
  2776.     {
  2777.     long pciClass;
  2778.     long subClass;
  2779.     long pgmIf;
  2780.     UINT8 subClassMin, subClassMax;
  2781.     UINT8 pgmIfMin, pgmIfMax;
  2782.     /* Extract parameters. */
  2783.     *ppCmd = GetHexToken (*ppCmd, &pciClass, -1);
  2784.     *ppCmd = GetHexToken (*ppCmd, &subClass, -1);
  2785.     *ppCmd = GetHexToken (*ppCmd, &pgmIf, -1);
  2786.     /* Validate parameters */
  2787.     if (pciClass < 0)
  2788. {
  2789. fprintf (fout, "No class specified.n");
  2790. return RET_CONTINUE;
  2791. }
  2792.     if (subClass < 0)
  2793. {
  2794. subClassMin = 0;
  2795. subClassMax = 255;
  2796. }
  2797.     else
  2798. subClassMin = subClassMax = (UINT8) subClass;
  2799.     if (pgmIf < 0)
  2800. {
  2801. pgmIfMin = 0;
  2802. pgmIfMax = 255;
  2803. }
  2804.     else
  2805. pgmIfMin = pgmIfMax = (UINT8) pgmIf;
  2806.     /* Search for devices. */
  2807.     PciBusSearch ((UINT8) pciClass, (UINT8) pciClass, subClassMin, subClassMax,
  2808. pgmIfMin, pgmIfMax, fout);
  2809.     return RET_CONTINUE;
  2810.     }
  2811. /*************************************************************************
  2812. *
  2813. * cmdPciDevInfo - Displays configuration info for specified device.
  2814. *
  2815. * Displays PCI configuration information for specified device. <ppCmd>
  2816. * is parsed for a bus number, device number, and optional function number.
  2817. * <ppCmd> is updated upon return to point to the remainder of the command
  2818. * line.
  2819. *
  2820. * RETURNS:  RET_CONTINUE
  2821. */
  2822. LOCAL UINT16 cmdPciDevInfo
  2823.     (
  2824.     pVOID Param, /* Generic parameter passed down */
  2825.     char **ppCmd, /* Ptr to remainder of cmd line */
  2826.     FILE *fin, /* stream for input (if any) */
  2827.     FILE *fout /* stream for output (if any) */
  2828.     )
  2829.     {
  2830.     int j;
  2831.     long bus;
  2832.     long device;
  2833.     long func;
  2834.     PCI_CFG_HEADER cfgHdr;
  2835.     /* Extract parameters. */
  2836.     *ppCmd = GetHexToken (*ppCmd, &bus, -1);
  2837.     *ppCmd = GetHexToken (*ppCmd, &device, -1);
  2838.     *ppCmd = GetHexToken (*ppCmd, &func, -1);
  2839.     /* Validate parameters */
  2840.     if (bus < 0 || device < 0 || func < 0)
  2841. {
  2842. fprintf (fout, "Must specify bus, device, and function numbers.n");
  2843. return RET_CONTINUE;
  2844. }
  2845.     /* Get device info. */
  2846.     usbPciConfigHeaderGet ((UINT8) bus, (UINT8) device, (UINT8) func, &cfgHdr);
  2847.     fprintf (fout, "Bus 0x%2.2lx Device 0x%2.2lx Func 0x%2.2lx:n", bus, device, func);
  2848.     fprintf (fout, "Vendor Id   = 0x%4.4x (%d)n", cfgHdr.vendorId, cfgHdr.vendorId);
  2849.     fprintf (fout, "Device Id   = 0x%4.4x (%d)n", cfgHdr.deviceId, cfgHdr.deviceId);
  2850.     fprintf (fout, "Command   = 0x%4.4xn", cfgHdr.command);
  2851.     fprintf (fout, "Status   = 0x%4.4xn", cfgHdr.status);
  2852.     fprintf (fout, "Revision Id   = 0x%2.2xn", cfgHdr.revisionId);
  2853.     fprintf (fout, "Class   = 0x%2.2xn", cfgHdr.pciClass);
  2854.     fprintf (fout, "SubClass   = 0x%2.2xn", cfgHdr.subClass);
  2855.     fprintf (fout, "PgmIf   = 0x%2.2xn", cfgHdr.pgmIf);
  2856. /*  fprintf (fout, "BIST   = 0x%2.2xn", cfgHdr.bist);  */
  2857. /*  fprintf (fout, "HeaderType   = 0x%2.2xn", cfgHdr.headerType);  */
  2858.     fprintf (fout, "LatencyTimer  = 0x%2.2xn", cfgHdr.latencyTimer);
  2859.     fprintf (fout, "CacheLineSize = 0x%2.2xn", cfgHdr.cacheLineSize);
  2860.     for (j = 0; j < 6; j++)
  2861. if (cfgHdr.baseReg [j] != 0)
  2862.     fprintf (fout, "BaseReg %d    = 0x%8.8xn", j, cfgHdr.baseReg [j]);
  2863.     fprintf (fout, "RomBase   = 0x%8.8xn", cfgHdr.romBase);
  2864.     fprintf (fout, "Int Pin   = 0x%2.2xn", cfgHdr.intPin);
  2865.     fprintf (fout, "Int Line   = 0x%2.2xn", cfgHdr.intLine);
  2866.     fprintf (fout, "MinGrant   = 0x%2.2xn", cfgHdr.minGrant);
  2867.     fprintf (fout, "MaxLatency   = 0x%2.2xn", cfgHdr.maxLatency);
  2868.     return RET_CONTINUE;
  2869.     }
  2870. /*************************************************************************
  2871. *
  2872. * cmdChdir - sets the current default path
  2873. *
  2874. * RETURNS: RET_CONTINUE
  2875. */
  2876. LOCAL UINT16 cmdChdir
  2877.     (
  2878.     pVOID Param, /* Generic parameter passed down */
  2879.     char **ppCmd, /* Ptr to remainder of cmd line */
  2880.     FILE *fin, /* stream for input (if any) */
  2881.     FILE *fout /* stream for output (if any) */
  2882.     )
  2883.     {
  2884.     char path [MAX_CMD_LEN];
  2885.     /* Get filename parameter */
  2886.     *ppCmd = GetNextToken (*ppCmd, path, sizeof (path));
  2887.     fprintf (fout, "setting path = %sn", path);
  2888.     /* Change the default directory */
  2889.     if (chdir (path) != OK)
  2890. fprintf (fout, "chdir() returned ERROR.n");
  2891.     return RET_CONTINUE;
  2892.     }
  2893. /*************************************************************************
  2894. *
  2895. * cmdPath - displays current default path
  2896. *
  2897. * RETURNS: RET_CONTINUE
  2898. */
  2899. LOCAL UINT16 cmdPath
  2900.     (
  2901.     pVOID Param, /* Generic parameter passed down */
  2902.     char **ppCmd, /* Ptr to remainder of cmd line */
  2903.     FILE *fin, /* stream for input (if any) */
  2904.     FILE *fout /* stream for output (if any) */
  2905.     )
  2906.     {
  2907.     char path [MAX_CMD_LEN];
  2908.     /* Change the default directory */
  2909.     if (getcwd (path, sizeof (path)) == NULL)
  2910. fprintf (fout, "getcwd() returned ERROR.n");
  2911.     else
  2912. fprintf (fout, "path = %sn", path);
  2913.     return RET_CONTINUE;
  2914.     }
  2915. /*************************************************************************
  2916. *
  2917. * cmdPciInByte - Input a byte from a PCI I/O address
  2918. *
  2919. * RETURNS:  RET_CONTINUE
  2920. */
  2921. LOCAL UINT16 cmdPciInByte
  2922.     (
  2923.     pVOID Param, /* Generic parameter passed down */
  2924.     char **ppCmd, /* Ptr to remainder of cmd line */
  2925.     FILE *fin, /* stream for input (if any) */
  2926.     FILE *fout /* stream for output (if any) */
  2927.     )
  2928.     {
  2929.     long port;
  2930.     long count;
  2931.     UINT8 value;
  2932.     *ppCmd = GetHexToken (*ppCmd, &port, -1);
  2933.     *ppCmd = GetHexToken (*ppCmd, &count, 1);
  2934.     if (port == -1)
  2935. {
  2936. fprintf (fout, "Must specify port address.n");
  2937. return RET_CONTINUE;
  2938. }
  2939.     for (; count > 0; count--)
  2940. {
  2941. value = USB_PCI_BYTE_IN (port);
  2942. fprintf (fout, "0x%2.2x (%u)n", value, value);
  2943. }
  2944.     return RET_CONTINUE;
  2945.     }
  2946. /*************************************************************************
  2947. *
  2948. * cmdPciInWord - Input a word from a PCI I/O address
  2949. *
  2950. * RETURNS:  RET_CONTINUE
  2951. */
  2952. LOCAL UINT16 cmdPciInWord
  2953.     (
  2954.     pVOID Param, /* Generic parameter passed down */
  2955.     char **ppCmd, /* Ptr to remainder of cmd line */
  2956.     FILE *fin, /* stream for input (if any) */
  2957.     FILE *fout /* stream for output (if any) */
  2958.     )
  2959.     {
  2960.     long port;
  2961.     long count;
  2962.     UINT16 value;
  2963.     *ppCmd = GetHexToken (*ppCmd, &port, -1);
  2964.     *ppCmd = GetHexToken (*ppCmd, &count, 1);
  2965.     if (port == -1)
  2966. {
  2967. fprintf (fout, "Must specify port address.n");
  2968. return RET_CONTINUE;
  2969. }
  2970.     for (; count > 0; count--)
  2971. {
  2972. value = USB_PCI_WORD_IN (port);
  2973. fprintf (fout, "0x%4.4x (%u)n", value, value);
  2974. }
  2975.     return RET_CONTINUE;
  2976.     }
  2977. /*************************************************************************
  2978. *
  2979. * cmdPciInDword - Input a dword from a PCI I/O address
  2980. *
  2981. * RETURNS:  RET_CONTINUE
  2982. */
  2983. LOCAL UINT16 cmdPciInDword
  2984.     (
  2985.     pVOID Param, /* Generic parameter passed down */
  2986.     char **ppCmd, /* Ptr to remainder of cmd line */
  2987.     FILE *fin, /* stream for input (if any) */
  2988.     FILE *fout /* stream for output (if any) */
  2989.     )
  2990.     {
  2991.     long port;
  2992.     long count;
  2993.     UINT32 value;
  2994.     *ppCmd = GetHexToken (*ppCmd, &port, -1);
  2995.     *ppCmd = GetHexToken (*ppCmd, &count, 1);
  2996.     if (port == -1)
  2997. {
  2998. fprintf (fout, "Must specify port address.n");
  2999. return RET_CONTINUE;
  3000. }
  3001.     for (; count > 0; count--)
  3002. {
  3003. value = USB_PCI_DWORD_IN (port);
  3004. fprintf (fout, "0x%8.8lx (%lu)n", (long) value, (long) value);
  3005. }
  3006.     return RET_CONTINUE;
  3007.     }
  3008. /*************************************************************************
  3009. *
  3010. * cmdPciOutByte - Output a byte to a PCI I/O address
  3011. *
  3012. * RETURNS:  RET_CONTINUE
  3013. */
  3014. LOCAL UINT16 cmdPciOutByte
  3015.     (
  3016.     pVOID Param, /* Generic parameter passed down */
  3017.     char **ppCmd, /* Ptr to remainder of cmd line */
  3018.     FILE *fin, /* stream for input (if any) */
  3019.     FILE *fout /* stream for output (if any) */
  3020.     )
  3021.     {
  3022.     long port;
  3023.     long value;
  3024.     *ppCmd = GetHexToken (*ppCmd, &port, -1);
  3025.     *ppCmd = GetHexToken (*ppCmd, &value, -1);
  3026.     if (port == -1 || value == -1)
  3027. {
  3028. fprintf (fout, "Must specify port address and a value.n");
  3029. return RET_CONTINUE;
  3030. }
  3031.     USB_PCI_BYTE_OUT (port, (UINT8) value);
  3032.     return RET_CONTINUE;
  3033.     }
  3034. /*************************************************************************
  3035. *
  3036. * cmdPciOutWord - Output a word to a PCI I/O address
  3037. *
  3038. * RETURNS:  RET_CONTINUE
  3039. */
  3040. LOCAL UINT16 cmdPciOutWord
  3041.     (
  3042.     pVOID Param, /* Generic parameter passed down */
  3043.     char **ppCmd, /* Ptr to remainder of cmd line */
  3044.     FILE *fin, /* stream for input (if any) */
  3045.     FILE *fout /* stream for output (if any) */
  3046.     )
  3047.     {
  3048.     long port;
  3049.     long value;
  3050.     *ppCmd = GetHexToken (*ppCmd, &port, -1);
  3051.     *ppCmd = GetHexToken (*ppCmd, &value, -1);
  3052.     if (port == -1 || value == -1)
  3053. {
  3054. fprintf (fout, "Must specify port address and a value.n");
  3055. return RET_CONTINUE;
  3056. }
  3057.     USB_PCI_WORD_OUT (port, (UINT16) value);
  3058.     return RET_CONTINUE;
  3059.     }
  3060. /*************************************************************************
  3061. *
  3062. * cmdPciOutDword - Output a dword to a PCI I/O address
  3063. *
  3064. * RETURNS:  RET_CONTINUE
  3065. */
  3066. LOCAL UINT16 cmdPciOutDword
  3067.     (
  3068.     pVOID Param, /* Generic parameter passed down */
  3069.     char **ppCmd, /* Ptr to remainder of cmd line */
  3070.     FILE *fin, /* stream for input (if any) */
  3071.     FILE *fout /* stream for output (if any) */
  3072.     )
  3073.     {
  3074.     long port;
  3075.     long value;
  3076.     *ppCmd = GetHexToken (*ppCmd, &port, -1);
  3077.     *ppCmd = GetHexToken (*ppCmd, &value, -1);
  3078.     if (port == -1 || value == -1)
  3079. {
  3080. fprintf (fout, "Must specify port address and a value.n");
  3081. return RET_CONTINUE;
  3082. }
  3083.     USB_PCI_DWORD_OUT (port, (UINT32) value);
  3084.     return RET_CONTINUE;
  3085.     }
  3086. /*************************************************************************
  3087. *
  3088. * cmdDump - dumps memory
  3089. *
  3090. * RETURNS: RET_CONTINUE
  3091. */
  3092. LOCAL UINT16 cmdDump
  3093.     (
  3094.     pVOID Param, /* Generic parameter passed down */
  3095.     char **ppCmd, /* Ptr to remainder of cmd line */
  3096.     FILE *fin, /* stream for input (if any) */
  3097.     FILE *fout /* stream for output (if any) */
  3098.     )
  3099.     {
  3100.     static long adrs = 0;
  3101.     long temp;
  3102.     long len;
  3103.     *ppCmd = GetHexToken (*ppCmd, &temp, -1);
  3104.     *ppCmd = GetHexToken (*ppCmd, &len, -1);
  3105.     if (temp == -1 && len == -1)
  3106. {
  3107. /* cmd entered without args...Dump from previous location. */
  3108. len = 128;
  3109. }
  3110.     else
  3111. {
  3112. adrs = temp;
  3113. if (len == -1)
  3114.     len = 128;
  3115. }
  3116.     displayMem ((UINT16) len, (pUINT8) adrs, fout);
  3117.     adrs += len;
  3118.     return RET_CONTINUE;
  3119.     }
  3120. /*************************************************************************
  3121. *
  3122. * cmdErrno - Show current errno
  3123. *
  3124. * RETURNS: RET_CONTINUE
  3125. */
  3126. LOCAL UINT16 cmdErrno
  3127.     (
  3128.     pVOID Param, /* Generic parameter passed down */
  3129.     char **ppCmd, /* Ptr to remainder of cmd line */
  3130.     FILE *fin, /* stream for input (if any) */
  3131.     FILE *fout /* stream for output (if any) */
  3132.     )
  3133.     {
  3134.     fprintf (fout, "errno = 0x%xn", errno);
  3135.     return RET_CONTINUE;
  3136.     }
  3137. /*************************************************************************
  3138. *
  3139. * cmdTable - Table of supported commands
  3140. */
  3141. LOCAL CMD_DESCR cmdTable [] =
  3142.     {
  3143.     
  3144. #ifdef INCLUDE_USB
  3145.     {"UsbInit", 4, "UsbInit", "Initialize USBD", cmdUsbInit},
  3146.     {"UsbDown", 4, "UsbDown", "Shut down USBD", cmdUsbDown},
  3147.     {"Attach", 2, "Attach uhci | ohci", "Attaches HCD to USBD", cmdAttach},
  3148.     {"Detach", 2, "Detach uhci | ohci", "Detaches HCD from USBD", cmdDetach},
  3149.     {"UsbEnum", 4, "UsbEnum", "Enumerate USBs attached to system", cmdUsbEnum},
  3150.     {"UsbStats", 4, "UsbStats n", "Display statistics for bus connected to node n", cmdUsbStats},
  3151.     {"stats", 4, NULL, NULL, cmdUsbStats},
  3152.     {"GetConfig", 4, "GetConfig n", "Get USB configuration for node n", cmdGetConfig},
  3153.     {"gc", 2, NULL, NULL, cmdGetConfig},
  3154.     {"SetConfig", 4, "SetConfig n v", "Set USB configuration to v for node n", cmdSetConfig},
  3155.     {"sc", 2, NULL, NULL, cmdSetConfig},
  3156.     {"GetInterface", 4, "GetInterface n i", "Get alt. setting for node n interface i",
  3157. cmdGetInterface},
  3158.     {"gi", 2, NULL, NULL, cmdGetInterface},
  3159.     {"SetInterface", 4, "SetInterface n i as", "Set alt. setting as for node n interface i",
  3160. cmdSetInterface},
  3161.     {"si", 2, NULL, NULL, cmdSetInterface},
  3162.     {"GetStatus", 4, "GetStatus n typ idx len", "Get USB status typ/idx for node n", cmdGetStatus},
  3163.     {"gs", 2, NULL, NULL, cmdGetStatus},
  3164.     {"GetAddress", 4, "GetAddress n", "Get USB address for node n", cmdGetAddress},
  3165.     {"ga", 2, NULL, NULL, cmdGetAddress},
  3166.     {"SetAddress", 4, "SetAddress n a", "Set USB address to a for node n", cmdSetAddress},
  3167.     {"sa", 2, NULL, NULL, cmdSetAddress},
  3168.     {"SetFeature", 4, "SetFeature n t fs idx", "Set USB feature t/fs/idx on node n", 
  3169. cmdSetFeature},
  3170.     {"sf", 2, NULL, NULL, cmdSetFeature},
  3171.     {"ClrFeature", 4, "ClrFeature n t fs idx", "Clear USB feature t/fs/idx on node n",
  3172. cmdClrFeature},
  3173.     {"cf", 2, NULL, NULL, cmdClrFeature},
  3174.     {"GetDescr", 4, "GetDescr n rt t idx l len", "Get USB descr rt/t/idx (lang = l) on node n",
  3175. cmdGetDescr},
  3176.     {"gd", 2, NULL, NULL, cmdGetDescr},
  3177.     {"SynchFrame", 4, "SynchFrame n e", "Get current synch frame for endpoint e on node n",
  3178. cmdGetSynchFrame},
  3179.     {"CurrentFrame", 3, "CurrentFrame n", "Get current frame number for bus connected to node n", 
  3180. cmdGetCurrentFrame},
  3181.     {"IntPoll", 4, "IntPoll n e [len]", "Polls interrupt endpoint e on node n for input",
  3182. cmdIntPoll},
  3183. #endif
  3184. #ifdef INCLUDE_USB_KEYBOARD
  3185.     {"kbdInit", 4, "kbdInit", "Initializes USB keyboard SIO driver", cmdKbdInit},
  3186.     {"kbdDown", 4, "kbdDown", "Shuts down USB keyboard SIO driver", cmdKbdDown},
  3187.     {"kbdPoll", 4, "kbdPoll", "Polls USB keyboard SIO driver for input", cmdKbdPoll},
  3188. #endif
  3189. #ifdef INCLUDE_USB_MOUSE
  3190.     {"mouseTest", 6, "mouseTest", "Tests USB mouse SIO driver", cmdMouseTest},
  3191. #endif
  3192. #ifdef INCLUDE_USB_PRINTER
  3193.     {"prnInit", 4, "prnInit", "Initializes USB printer SIO driver", cmdPrnInit},
  3194.     {"prnDown", 4, "prnDown", "Shuts down USB printer SIO driver", cmdPrnDown},
  3195.     {"print4k", 7, "print4k [n]", "Sends n 4k blocks of printer data", cmdPrint},
  3196.     {"print", 5, "print filename", "Sends file to printer", cmdPrintFnm},
  3197. #endif
  3198. #ifdef INCLUDE_USB_SPEAKER
  3199.     {"spkrInit", 5, "spkrInit", "Initializes USDB speaker SEQ_DEV driver", cmdSpkrInit},
  3200.     {"spkrDown", 5, "spkrDown", "Shuts down USB speaker SEQ_DEV driver", cmdSpkrDown},
  3201.     {"spkrFmt", 5, "spkrFmt", "Displays available speaker formats", cmdSpkrFmt},
  3202.     {"play", 4, "play filename", "Plays the specified .wav file", cmdPlay},
  3203.     {"volume", 3, "volume [level]", "Displays/sets volume", cmdVolume},
  3204. #endif
  3205. #ifdef INCLUDE_USB_TARG
  3206.     {"targInit", 5, "targInit 'kbd' | 'prn' | 'd12' [io [irq [dma]]]", "Initializes USB target stack", cmdTargInit},
  3207.     {"targDown", 5, "targDown", "Shuts down USB target stack", cmdTargDown},
  3208.     {"kbdReport", 4, "kbdReport [xxh x 8]", "Injects a keboard report", cmdKbdReport},
  3209.     {"kr", 2, NULL, NULL, cmdKbdReport},
  3210.     {"prnDump", 5, "prnDump [length]", "Dumps data received by printer emulator", cmdPrnDump},
  3211.     {"pdu", 3, NULL, NULL, cmdPrnDump},
  3212. #endif
  3213.     {"PCIEnum", 4, "PCIEnum [p]", "Enumerates PCI devices thru PgmIf p", cmdPciEnumerate},
  3214.     {"PCIFindClass", 8, "PCIFindClass c [s [p]]", 
  3215. "Lists PCI devices of Class c, Sub-class s, PgmIf p", cmdPciFindClass},
  3216.     {"pfc", 3, NULL, NULL, cmdPciFindClass},
  3217.     {"PCIDevInfo", 7, "PCIDevInfo b d f", 
  3218. "Displays info for PCI Device d Func f on Bus b", cmdPciDevInfo},
  3219.     {"pdi", 3, NULL, NULL, cmdPciDevInfo},
  3220.     {"chdir", 5, "chdir directory", "Sets the current default path", cmdChdir},
  3221.     {"path", 4, "path", "Displays current default path", cmdPath},
  3222. /*  {"InByte", 3, "InByte a [n]", "Input a byte from PCI I/O address a", cmdPciInByte}, */
  3223.     {"ib", 1, NULL, NULL, cmdPciInByte},
  3224. /*  {"InWord", 3, "InWord a [n]", "Input a word from PCI I/O address a", cmdPciInWord}, */
  3225.     {"iw", 2, NULL, NULL, cmdPciInWord},
  3226. /*  {"InDword", 3, "InDword a [n]", "Input a dword from PCI I/O address a", cmdPciInDword}, */
  3227.     {"id", 2, NULL, NULL, cmdPciInDword},
  3228. /*  {"OutByte", 4, "OutByte a v", "Output byte value v to PCI I/O address a", cmdPciOutByte}, */
  3229.     {"ob", 1, NULL, NULL, cmdPciOutByte},
  3230. /*  {"OutWord", 4, "OutWord a v", "Output word value v to PCI I/O address a", cmdPciOutWord}, */
  3231.     {"ow", 2, NULL, NULL, cmdPciOutWord},
  3232. /*  {"OutDword", 4, "OutDword a v", "Output dword value v to PCI I/O address a", cmdPciOutDword}, */
  3233.     {"od", 2, NULL, NULL, cmdPciOutDword},
  3234.     {"dump", 1, "dump [adrs [len]]", "Dumps memory at specified adrs", cmdDump},
  3235.     {"errno", 3, NULL, NULL, cmdErrno},
  3236.     {"Help", 4, "Help/?", "Displays list of supported commands.", CmdParserHelpFunc},
  3237.     {"?", 1, NULL, NULL, CmdParserHelpFunc},
  3238.     {"Exit", 4, "Exit/Quit/Bye", "Exits program.", CmdParserExitFunc},
  3239.     {"Quit", 1, NULL, NULL, CmdParserExitFunc},
  3240.     {"Bye", 3, NULL, NULL, CmdParserExitFunc},
  3241.     {NULL, 0, NULL, NULL, NULL}
  3242.     };
  3243. /*************************************************************************
  3244. *
  3245. * usbTool - Primary entry point for USB bus exerciser.
  3246. *
  3247. * usbTool presents the user with a command prompt.  The user enters
  3248. * commands to invoke USB functions through the USBD and USB HCD.
  3249. *
  3250. * RETURNS:  RET_OK for normal termination.
  3251. *     RET_ERROR for program failure.
  3252. */
  3253. UINT16 usbTool (void)
  3254.     {
  3255.     UINT16 s; /* Execution status */
  3256.     /* Set buffering for stdin/stdout to "no buffering" so screen updates
  3257.     will be timely. */
  3258.     setvbuf (stdin, NULL, _IONBF, 0);
  3259.     setvbuf (stdout, NULL, _IONBF, 0);
  3260.     /* Display program banner */
  3261.     fprintf (stdout, PGM_NAME ": " PGM_DESCR ", version " PGM_VERSION "n"
  3262. PGM_COPYRIGHT "nn");
  3263.     /* Prompt the user for commands so long as the execution status
  3264.     remains ok. */
  3265.     while ((s = PromptAndExecCmd (NULL, PROMPT, stdin, stdout, cmdTable)) 
  3266. == RET_CONTINUE)
  3267. ;
  3268.     /* Display reason for termination */
  3269.     if (s == RET_OK)
  3270. fprintf (stdout, PGM_NAME " terminating normally.n");
  3271.     else if (s == RET_ERROR)
  3272. fprintf (stdout, PGM_NAME " terminating with error.n");
  3273.     else
  3274. fprintf (stdout, PGM_NAME " terminating with code %d.n", s);
  3275.     return s;
  3276.     }
  3277. /* End of file. */