📄 usrusbprninit.c
字号:
{ printf("usbPrnDevFind() returned ERROR\n"); return; } /* Install a callback to spew characters to the printer * usbPrnTxCallback () handles sending chunks when the printer * is ready for them */ if ((*pChan->pDrvFuncs->callbackInstall) (pChan, SIO_CALLBACK_GET_TX_CHAR, usbPrnTxCallback, (void *) usbPrnDev) != OK) { printf("prnAttachCallback () failed to install\n"); } printf("USB Printer attached as %s\n", prnName); } } /* A printer has been detached */ else if (attachCode == USB_PRN_REMOVE) { /* find the related device */ if (usbPrnDevFind (pChan, (USB_PRN_DEV *) &usbPrnDev) != OK) { printf ("usbPrnDevFind could not find channel 0x%d", pChan); return; } /* delete the device */ usbPrnDevDelete ((USB_PRN_DEV *) usbPrnDev); /* Unlock this channel since the device that was plugged into it * was removed */ if (usbPrinterSioChanUnlock (pChan) != OK) { printf("usbPrinterSioChanUnlock () returned ERROR\n"); return; } } }/********************************************************************************* usbPrnOpen - open a usbPrnDrv serial device.** Increments a counter that holds the number of open paths to device. */LOCAL int usbPrnOpen ( USB_PRN_DEV * pUsbPrnDev, /* printer device to read from */ char * name, /* device name */ int flags, /* flags */ int mode /* mode selected */ ) { pUsbPrnDev->numOpen++; /* increment number of open paths */ sioIoctl (pUsbPrnDev->pSioChan, SIO_OPEN, NULL); return ((int) pUsbPrnDev); }/********************************************************************************* usbPrnDrvUnInit - shuts down an I/O USB printer driver*** This is supplied to for the user, but it should be noted that iosDrvRemove()* may cause unpredictable results.**/STATUS usbPrnDrvUnInit (void) { if (!usbPrnDrvNum) return (OK); /* remove the driver */ if (iosDrvRemove (usbPrnDrvNum, TRUE) != OK) { printf("iosDrvRemove () returned ERROR\n"); return (ERROR); } /* HELP */ usbPrnDrvNum = 0; /* delete the mutex semaphores */ if (semDelete (usbPrnMutex) == ERROR) { printf("semDelete (usbPrnMutex) returned ERROR\n"); return (ERROR); } if (semDelete (usbPrnListMutex) == ERROR) { printf("semDelete (usbPrnListMutex) returned ERROR\n"); return (ERROR); } /* unregister */ if (usbPrinterDynamicAttachUnRegister (prnAttachCallback, NULL) != OK) { printf("usbPrinterDynamicAttachUnRegister () returned ERROR\n"); return (ERROR); } /* shutdown */ if (usbPrinterDevShutdown () != OK) { printf("usbPrinterDynamicAttachUnRegister () returned ERROR\n"); return (ERROR); } return (OK); }/********************************************************************************* usbPrnClose - close a usbPrnDrv serial device.** Decrements the counter of open paths to device and alerts the driver * with an ioctl call when the count reaches zero. This scheme is used to* implement the HUPCL(hang up on last close). */LOCAL int usbPrnClose ( USB_PRN_DEV * pUsbPrnDev /* printer device to read from */ ) { /* if there are no open channels */ if (!(--pUsbPrnDev->numOpen)) { sioIoctl (pUsbPrnDev->pSioChan, SIO_HUP, NULL); } return ((int) pUsbPrnDev); }/********************************************************************************* usbPrnIoctl - issue device control commands** This routine issues device control commands to an USB printer device.** RETURNS: depends on the function invoked.*/LOCAL int usbPrnIoctl ( USB_PRN_DEV * pUsbPrnDev, /* printer device to read from */ int request, /* request code */ void * arg /* some argument */ ) { return (sioIoctl (pUsbPrnDev->pSioChan, request, arg)); }/********************************************************************************* usbPrnRead - read from the USB printer** This routines is a no-op for a read from the USB printer.** RETURNS: OK, always.*/LOCAL int usbPrnRead ( USB_PRN_DEV * pUsbPrnDev /* printer device to read from */ ) { int status = OK; /* holder for the return value */ return (status); }/********************************************************************************* usbPrnWrite - write to the USB printer** This routine writes <nBytes> bytes of data from the buffer pointed to by* <buff> to the USB printer described by <pUsbPrnDev>.** RETURNS: number of bytes written or ERROR*/int usbPrnWrite ( USB_PRN_DEV * pUsbPrnDev, /* printer device to read from */ UCHAR * buffer, /* buffer of data to write */ UINT32 nBytes /* number of bytes in buffer */ ) { int status = OK; /* holder for the return value */ /* protect the critical region */ USB_PRN_MUTEX_TAKE (WAIT_FOREVER); /* store buffer information */ txCallbackBfr = buffer; txBytesNum = nBytes; pUsbPrnDev->buff = buffer; pUsbPrnDev->bufSize = nBytes; if ((*(pUsbPrnDev->pSioChan->pDrvFuncs->txStartup)) (pUsbPrnDev->pSioChan) != OK) { status = ERROR; } /* end of the critical region */ USB_PRN_MUTEX_GIVE; /* wake up any select-blocked readers */ selWakeupAll (&pUsbPrnDev->selList, SELWRITE); status = (int) (nBytes - txBytesNum); return (status); }/*************************************************************************** usbPrnTxCallback - feeds characters to USB printer SIO driver** RETURNS: OK*/LOCAL STATUS usbPrnTxCallback ( void *callbackParam, char *txChar ) { USB_PRN_DEV * pDev = (USB_PRN_DEV *) callbackParam; /* printer device */ if (!(pDev->bufSize)) { return (ERROR); } pDev->bufSize--; *txChar = *pDev->buff++; return OK; }/******************************************************************************* usrUsbPrnInit - initialize the USB printer driver** This function initializes the USB printer driver and registers for attach * callbacks. ** RETURNS: Nothing */void usrUsbPrnInit (void) { /* Check if driver already installed */ if (usbPrnDrvNum > 0) { printf ("Printer already initilaized.\n"); } /* Initialize USB printer SIO driver */ if (usbPrinterDevInit () == OK) { printf ("usbPrinterDevInit() returned OK\n"); /* Register our device for attach callbacks. */ if (usbPrinterDynamicAttachRegister (prnAttachCallback, (void *) NULL) != OK) { printf ("usbPrinterDynamicAttachRegister() returned ERROR\n"); } } else printf ("usbPrinterDevInit() returned ERROR\n"); }/*************************************************************************** usbPrnWrTest - test the USB printer write function** This is given as a sample application to print a file using the i/o * system calls to access the printer device. It has been assumed that the * file that it sent to the printer has been "canned" for the particular* printer being used. Refer to the USB Developer's Kit manual for further* details.** RETURNS: OK, or ERROR.*/STATUS usbPrnWrTest ( char * fileName /* file to print */ ) { int testFd = 0; UCHAR * tempBuff = NULL; FILE * testFile; UINT32 fsize = 0; if ((testFile = fopen (fileName, "rb")) == NULL) { printf ("fopen() failed open the input file\n"); return (ERROR); } fseek (testFile, 0, SEEK_END); fsize = ftell (testFile); fseek (testFile, 0, SEEK_SET); printf ("usbPrnTxCallback() file size =0x%x \n",fsize); if ((tempBuff = calloc (1, fsize)) == NULL) return (ERROR); if (fread (tempBuff, 1, fsize, testFile) == 0) return (ERROR); if ((testFd = open ("/usbPr/0", 2,0)) == ERROR) return (ERROR); if (write (testFd, tempBuff, fsize) == ERROR) return (ERROR); return (OK); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -