⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uglusbptr.c

📁 zinc60t22.tar.gz:zinc在tornado2.2.x下的补丁
💻 C
字号:
/* uglusbptr.c - UGL USB Pointer Handler *//* Copyright 2003 Wind River Systems, Inc. All Rights Reserved *//*modification history--------------------01b,24jun03,jlb  Correct spelling of included header file01a,18jun03,jlb  created*//*DESCRIPTIONThis file provides the USB Mouse protocol support for UGL.The USB libraries are built on top of each other.  There is a basicUSB support, followed by a support for particular USB device classes,and finally, support of devices such as keyboard and mouse.  Thehighest level support are configured as components from the config/comps/srcdirectory.  The lower levels are in the src/drv/usb directory.This driver is configured to use the class library for a mouse device anddoes not perform an iosDrvInstall(), or iosDevAdd() as the usb components do.This driver feeds the USB reports into a pipe that is opened by the inputtask rather than a device.  This is to bridge the problem caused by WindMLusing a "select()" in its input task, while the current underlying USB driverdoes not support "select()".  However pipes can be used in a select.A new pipe is created for each device.  For example, "/pipe/uglUsbMse/0".An alternative would be to implement select() functionality for this driverwhich sits on top of the USB drivers that do not support select().Difficulty that may arise:- Hot swappability.USB is inherently hot swappable and its API's reflect that fact.  WindML cansupport this feature in different ways.  For instance if the upper levelUSB component were used for WindML USB, hot swapping a device results in anew device instance.  For example /usbKyd/0 becomes /usbKbd/1 even though itmay be the same keyboard on the same physical port.  An analogous problemmust be accommodated in this driver.Since this driver uses the USB mouse class library, its attachment callbackonly receives mouse device attachment notices.  Therefore, if a particularphysical USB port is used by a mouse and it is removed and a keyboard is plugged into it, this driver will still consider that the port is for mouseinput, but is currently detached.  This will occur with keyboards as wellif their driver is similarly architected.An alternative is to do what the upper level component does and close thechannel, and its associated pipe in our case, completely.  Then newattachments can be re-associated appropriately.  This then brings up theissue of the naming scheme (algorithm) for the pipes; continuously incr-easing numbers, or re-use of instance numbers.Yet another alternative, would be to use only a single pipe for all mouse,keyboard, etc. devices.  This would probably require some changes to theinput task's coding.*/#include <stdlib.h>#include <string.h>#include <stdarg.h>#include <ctype.h>#include <ioLib.h>#include <stdio.h>#include <pipeDrv.h>#include <ugl/ugl.h>#include <ugl/uglos.h>#include <ugl/uglmem.h>#include <ugl/uglinput.h>#include <ugl/uglevent.h>#include <ugl/ugllog.h>#include <ugl/driver/pointer/uglusbptr.h>#include <drv/usb/usbMouseLib.h>/* defines */#define UGL_USB_MSE_PIPE_LEN     (10)#define UGL_USB_MSE_ATTACHED     (1)#define UGL_USB_MSE_UNATTACHED   (2)/* typedefs */typedef struct ugl_usb_ptr_device    {    UGL_INT8            packet [3];     /* data packet */    UGL_UINT8           state;          /* attachment state */    SIO_CHAN           *pSioChan;       /* The underlying SIO channel */    UGL_UINT16  oldButton;          /* old button state */    } UGL_USB_PTR_DEVICE;UGL_LOCAL UGL_USB_PTR_DEVICE ptrData;char mouseName[] = "/pipe/usbMse/0";/* forward declarations */UGL_LOCAL void uglUsbPtrCallback (void *cbArg, pHID_MSE_BOOT_REPORT pReport);UGL_LOCAL UGL_STATUS uglUsbPtrFormatter (UGL_INPUT_DEVICE * pDevice,                                         UGL_EVENT *  pEvent);UGL_LOCAL UGL_STATUS uglUsbPtrInfo (UGL_INPUT_DEVICE * pDevice,                                    UGL_DEVICE_REQ  devRequest, void * arg);UGL_LOCAL UGL_STATUS uglUsbPtrDestroy ( UGL_INPUT_DEVICE * pDevice );/*************************************************************************** uglPtrAttachCallback - receives attach callbacks from mouse SIO driver** RETURNS: N/A*/void uglPtrAttachCallback    (    void * arg,             /* caller-defined argument */    SIO_CHAN *pChan,        /* pointer to affected SIO_CHAN */    UINT16 attachCode       /* defined as USB_MSE_xxxx */    )    {    if (attachCode == USB_MSE_ATTACH)        {        if (usbMouseSioChanLock (pChan) != OK)            {            uglLog (UGL_ERR_TYPE_WARN,                    "usbMouseSioChanLock () returned ERROR\n", 0, 0, 0, 0, 0);            }        else            {            if ((*pChan->pDrvFuncs->callbackInstall)                    (pChan,                    SIO_CALLBACK_PUT_MOUSE_REPORT,                    (STATUS  (*) (void *, ...)) uglUsbPtrCallback,                    (pVOID) arg)                    != OK)                {                uglLog (UGL_ERR_TYPE_WARN,                        "uglUsbPtrCallback () failed to install\n", 0, 0, 0, 0, 0);                }            uglLog (UGL_ERR_TYPE_INFO,                    "USB Mouse attached\n", 0, 0, 0, 0, 0);            ptrData.state =  UGL_USB_MSE_ATTACHED;            ptrData.pSioChan = pChan;            }        }    else if (attachCode == USB_MSE_REMOVE)        {        if (usbMouseSioChanUnlock (pChan) != OK)            {            uglLog (UGL_ERR_TYPE_WARN,                    "usbMouseSioChanUnlock () returned ERROR\n", 0, 0, 0, 0, 0);            return;            }        uglLog (UGL_ERR_TYPE_INFO, "USB Mouse removed\n", 0, 0, 0, 0, 0);        ptrData.state = UGL_USB_MSE_UNATTACHED;        }    }/******************************************************************************* uglUsbPtrCallback - Accept reports from a USB mouse** This routine is called by a USB task when a mouse "report" is received.**/UGL_LOCAL void uglUsbPtrCallback    (    void *cbArg,    pHID_MSE_BOOT_REPORT pReport    )    {    /* Created the callback so that its arg is its associated pipe */    write((int)cbArg, (char *)pReport, sizeof(HID_MSE_BOOT_REPORT));    }/******************************************************************************* uglUsbPtrOpen - open a USB pointer input device** This routine initializes a USB pointer for use with UGL. It opens the* device <pDevName> and sets it to the proper mode.**/UGL_INPUT_DEVICE_ID uglUsbPtrInit     (    char * pDevName,	UGL_EVENT_SERVICE_ID eventServiceId    )    {    UGL_INPUT_DEVICE * pDevice;    int mousePipe;    if (usbMouseDevInit() != OK)        {        uglLog (UGL_ERR_TYPE_WARN,                "VxWorks USB mouse unable to initialize.\n", 0, 0, 0, 0, 0);        return (UGL_NULL);        }    /* Create the device */    pDevice = uglInputDeviceAdd (eventServiceId);    if (pDevice == UGL_NULL)        return (UGL_NULL);    /* initialize device data */    /* identify as relative pointer device type */    pDevice->deviceType = UGL_DEVICE_POINTER_RELATIVE;    /* set up function pointers for device */    pDevice->format = uglUsbPtrFormatter;    pDevice->destroy = uglUsbPtrDestroy;    pDevice->info = uglUsbPtrInfo;    /*     * Although the attachment callback is registered, the mouse device is     * unattached until that callback is successfully called by USB.     */    ptrData.state = UGL_USB_MSE_UNATTACHED;    /* Create pipe */    if (pipeDevCreate(mouseName,                  UGL_USB_MSE_PIPE_LEN,                  sizeof(HID_MSE_BOOT_REPORT)) != ERROR)        {        /*         * Opening the pipe here in the app.  It will be written to         * from a USB task while in the callback above and then read         * in the input task.         */        mousePipe = open(mouseName, O_RDWR, 0);        if (mousePipe != ERROR)            {            int await = 0;            pDevice->fd = mousePipe;            /* Register the attach callback */            if (usbMouseDynamicAttachRegister (uglPtrAttachCallback,                                             (pVOID) mousePipe) == ERROR)                {                close(mousePipe);                pipeDevDelete(mouseName,1);                UGL_FREE(pDevice);                usbMouseDevShutdown();                uglLog (UGL_ERR_TYPE_WARN,                        "Call to usbMouseDynamicAttachRegister() failed\n",                        0, 0, 0, 0, 0);                return (UGL_NULL);                }            /*             * Wait a moment to let USBD hook things up. If it takes longer             * than about a second; keep going it may not be hooked up yet.             */            while (await < 15)                {                if (ptrData.state == UGL_USB_MSE_ATTACHED)                    break;                taskDelay(1);                await++;                }            uglLog (UGL_ERR_TYPE_INFO,                    "waited %d ticks for Mouse attachment\n", await, 0,0,0,0);            if (ptrData.state != UGL_USB_MSE_ATTACHED)                {                uglLog (UGL_ERR_TYPE_WARN,                        "- Check U/OHCI config and device.\n", 0, 0, 0, 0, 0);                }            return ((UGL_INPUT_DEVICE_ID) pDevice);            }        else            {            pipeDevDelete(mouseName,1);            uglLog (UGL_ERR_TYPE_WARN,                     "unable to open mouse pipe %s\n", (int)mouseName,                    0, 0, 0, 0);            }        }    else        uglLog (UGL_ERR_TYPE_WARN,                "unable to create mouse pipe\n", 0, 0, 0, 0, 0);    /* If we had a problem... */    UGL_FREE (pDevice);    usbMouseDevShutdown();    return (UGL_NULL);    }/******************************************************************************* uglUsbPtrDestroy - close a USB pointer device**/UGL_LOCAL UGL_STATUS uglUsbPtrDestroy     (     UGL_INPUT_DEVICE * pDevice     )    {        /*      * Detach from USB stuff.  Unregistering requires the identical     * parameters as the Register to work.     */    if (usbMouseDynamicAttachUnRegister(uglPtrAttachCallback,                        (pVOID) (pDevice->fd)) == ERROR)        uglLog (UGL_ERR_TYPE_WARN,                    "Unregister of attachment callback failed\n",0,0,0,0,0);    /*      * Uninstall callback; null callback vector.     *     * This is only necessary in situations such as if there were     * multiple USB mice and one was closed, but the rest remained.     */    if (ptrData.pSioChan != 0)        {        if ((*ptrData.pSioChan->pDrvFuncs->callbackInstall)                    (ptrData.pSioChan,                    SIO_CALLBACK_PUT_MOUSE_REPORT,                    (STATUS  (*) (void *, ...)) 0,                    (pVOID) 0)                    != OK)            {            uglLog (UGL_ERR_TYPE_WARN,                    "uglUsbPtrCallback () failed to un-install\n", 0,0,0,0,0);            }        }    usbMouseDevShutdown();    /* close the pipe */    close (pDevice->fd);    /* remove the pipe */    pipeDevDelete(mouseName, 0);    /* free local storage */    UGL_FREE (pDevice);    return UGL_STATUS_OK;    }/******************************************************************************* uglUsbPtrInfo - control  a USB pointer device**/UGL_LOCAL UGL_STATUS uglUsbPtrInfo     (    UGL_INPUT_DEVICE * pDevice,    UGL_DEVICE_REQ  devRequest,     void * pArg    )    {    UGL_STATUS status = UGL_STATUS_ERROR;    if ((pDevice == UGL_NULL) || (ptrData.state == UGL_USB_MSE_UNATTACHED))        return(status);    switch (devRequest)        {        case UGL_DEVICE_GET_POINTER_TYPE:            {            if(pArg != UGL_NULL)                {                *(int *)pArg = UGL_DEVICE_POINTER_TYPE_MOUSE;                status = UGL_STATUS_OK;                }            break;            }        default:            break;        }    return(status);    }/***************************************************************************** uglUsbPtrFormatter - Read USB data and format as pointer event*/UGL_LOCAL UGL_STATUS uglUsbPtrFormatter     (    UGL_INPUT_DEVICE * pDevice,    UGL_EVENT *  pEvent    )    {    UGL_INPUT_EVENT * pInputEvent = (UGL_INPUT_EVENT *)pEvent;    int           readCnt = 0;    /* build input event */    pInputEvent->header.type = UGL_EVENT_TYPE_POINTER;    pInputEvent->header.category = UGL_EVENT_CATEGORY_INPUT;    /* Find amount of data waiting */    ioctl (pDevice->fd, FIONREAD, (int) &readCnt);    /*     * No need to compress pointer messages in 3.x, read all available data     */    while (readCnt >= sizeof(HID_MSE_BOOT_REPORT))        {        HID_MSE_BOOT_REPORT br;        read(pDevice->fd, (char *)(&br), sizeof(HID_MSE_BOOT_REPORT));        readCnt -= sizeof(HID_MSE_BOOT_REPORT);        /* get delta x and delta y */        pInputEvent->type.pointer.dx = (signed char)(br.xDisplacement);        pInputEvent->type.pointer.dy = (signed char)(br.yDisplacement);        /* get button state */        pInputEvent->type.pointer.buttonState = 0;        if (br.buttonState & MOUSE_BUTTON_1)            pInputEvent->type.pointer.buttonState = UGL_POINTER_BUTTON1;        if (br.buttonState & MOUSE_BUTTON_2)            pInputEvent->type.pointer.buttonState |= UGL_POINTER_BUTTON2;        if (br.buttonState & MOUSE_BUTTON_3)            pInputEvent->type.pointer.buttonState |= UGL_POINTER_BUTTON4;        pInputEvent->type.pointer.buttonChange =                         (pInputEvent->type.pointer.buttonState ^                         ptrData.oldButton);        ptrData.oldButton = pInputEvent->type.pointer.buttonState;        }    return (UGL_STATUS_OK);    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -