From 1ac14835a37ba42cf1134391aeb06b80af1638d2 Mon Sep 17 00:00:00 2001 From: Mark Cannon Date: Wed, 13 Feb 2008 16:17:43 +0000 Subject: [PATCH] git-svn-id: https://openitg.svn.sourceforge.net/svnroot/openitg@18 83fadc84-e282-4d84-a09a-c4228d6ae7e5 --- src/io/PIUIO.cpp | 59 +++++++++++++++++++++++++++ src/io/PIUIO.h | 45 +++++++++++++++++++++ src/io/USBDevice.cpp | 15 +++++++ src/io/USBDevice.h | 50 +++++++++++++++++++++++ src/io/USBDriver.cpp | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/io/USBDriver.h | 50 +++++++++++++++++++++++ 6 files changed, 329 insertions(+) create mode 100644 src/io/PIUIO.cpp create mode 100644 src/io/PIUIO.h create mode 100644 src/io/USBDevice.cpp create mode 100644 src/io/USBDevice.h create mode 100644 src/io/USBDriver.cpp create mode 100644 src/io/USBDriver.h diff --git a/src/io/PIUIO.cpp b/src/io/PIUIO.cpp new file mode 100644 index 00000000..a31bf93d --- /dev/null +++ b/src/io/PIUIO.cpp @@ -0,0 +1,59 @@ +#include "global.h" +#include "RageLog.h" +#include "USBDevice.h" +#include "PIUIO.h" + +PIUIO::PIUIO() +{ + Open(); +} + +bool PIUIO::Matches( int idVendor, int idProduct ) +{ + if( idVendor == 0x547 && idProduct == 0x1002 ) + return true; + + return false; +} + +bool PIUIO::Read( u_int32_t *pData ) +{ + int iResult; + + while( 1 ) + { + iResult = usb_control_msg(m_pHandle, 192, 174, 0, 0, (char *)pData, 8, 10000); + if( iResult == 8 ) // all data read + break; + + // all data not read + LOG->Trace( "Device error: %s", usb_strerror() ); + Close(); + + while( !Open() ) + usleep( 100000 ); + } + + return true; +} + +bool PIUIO::Write( u_int32_t iData ) +{ + int iResult; + + while( 1 ) + { + iResult = usb_control_msg(m_pHandle, 64, 174, 0, 0, (char *)&iData, 8, 10000 ); + + if( iResult == 8 ) + break; + + LOG->Trace( "Device error: %s", usb_strerror() ); + Close(); + + while( !Open() ) + usleep( 100000 ); + } + + return true; +} diff --git a/src/io/PIUIO.h b/src/io/PIUIO.h new file mode 100644 index 00000000..e8a85777 --- /dev/null +++ b/src/io/PIUIO.h @@ -0,0 +1,45 @@ +#ifndef IO_PIUIO_H +#define IO_PIUIO_H + +#include +#include "USBDriver.h" + +class PIUIO: public USBDriver +{ +public: + PIUIO(); + + bool Read( uint32_t *pData ); + bool Write( uint32_t iData ); + +protected: + bool Matches( int idVendor, int idProduct ); +}; + +#endif /* IO_PIUIO_H */ + +/* + * (c) 2008 BoXoRRoXoRs + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons to + * whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF + * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS + * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT + * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + diff --git a/src/io/USBDevice.cpp b/src/io/USBDevice.cpp new file mode 100644 index 00000000..e406cb42 --- /dev/null +++ b/src/io/USBDevice.cpp @@ -0,0 +1,15 @@ +#include "global.h" +#include + +CString USBDevice::GetClassDescription( int iClass ) +{ + return "not implemented"; +} + +CString USBDevice::GetDescription() +{ + if( IsITGIO() || IsPIUIO() ) + return "Input/lights controller"; + + return "Description not implemented"; +} diff --git a/src/io/USBDevice.h b/src/io/USBDevice.h new file mode 100644 index 00000000..c1653023 --- /dev/null +++ b/src/io/USBDevice.h @@ -0,0 +1,50 @@ +#ifndef IO_USBDEVICE_H +#define IO_USBDEVICE_H + +#include + +/* A class we can use to characterize all USB devices on the system */ +class USBDevice +{ +public: + USBDevice(); + ~USBDevice(); + + bool Load(); + + CString GetDeviceProperty( CString sProperty ); + CString GetInterfaceProperty( CString sProperty, int iInterface ); + CString GetClassDescription( int iClass ); + CString GetDescription(); + + bool IsHub(); + bool IsITGIO(); + bool IsPIUIO(); +}; + +#endif /* IO_USBDEVICE_H */ + +/* + * (c) 2008 BoXoRRoXoRs + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons to + * whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF + * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS + * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT + * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ diff --git a/src/io/USBDriver.cpp b/src/io/USBDriver.cpp new file mode 100644 index 00000000..f18e7528 --- /dev/null +++ b/src/io/USBDriver.cpp @@ -0,0 +1,110 @@ +#include + +#include "global.h" +#include "RageLog.h" +#include "USBDriver.h" + +USBDriver::USBDriver() +{ +} + +USBDriver::~USBDriver() +{ + Close(); +} + +// this is overridden by derived USB drivers +bool USBDriver::Matches( int idVendor, int idProduct ) +{ + return false; +} + +struct usb_device *USBDriver::FindDevice() +{ + for( usb_bus *bus = usb_get_busses(); bus; bus->next ) + for( struct usb_device *dev = bus->devices; dev; dev->next ) + if( Matches(dev->descriptor.idVendor, dev->descriptor.idProduct) ) + return dev; + + // fall through + return NULL; +} + +void USBDriver::Close() +{ + // already closed + if( m_pHandle == NULL ) + return; + + usb_set_altinterface( m_pHandle, 0 ); + usb_reset( m_pHandle ); + usb_close( m_pHandle ); + m_pHandle = NULL; +} + +bool USBDriver::Open() +{ + Close(); + + usb_init(); + + if( usb_find_busses() < 0 ) + { + LOG->Warn( "usb_find_busses: %s", usb_strerror() ); + return false; + } + + if( usb_find_devices() < 0 ) + { + LOG->Warn( "usb_find_devices: %s", usb_strerror() ); + return false; + } + + if( !usb_busses ) + { + LOG->Warn( "usb_busses: %s", usb_strerror() ); + return false; + } + + // set the device + struct usb_device *device = FindDevice(); + + if( device == NULL ) + { + LOG->Warn( "USBDriver: could not set usb_device" ); + return false; + } + + m_pHandle = usb_open( device ); + if( m_pHandle == NULL ) + { + LOG->Warn( "usb_open: %s", usb_strerror() ); + return false; + } + + if( usb_set_configuration(m_pHandle, device->config->bConfigurationValue) != 0 ) + { + LOG->Warn( "usb_set_configuration: %s", usb_strerror() ); + Close(); + return false; + } + + if( usb_claim_interface(m_pHandle, device->config->interface->altsetting->bInterfaceNumber) != 0 ) + { +#ifdef LIBUSB_HAS_GET_DRIVER_NP + char sDriver[1025]; + usb_get_driver_np( m_pHandle, device->config->interface->altsetting->bInterfaceNumber, sDriver, 1024 ); + if( sDriver[0] != 0 ) + LOG->Warn( "usb_claim_interface: %s (claimed by %s)", usb_strerror(), sDriver ); + else +#endif + LOG->Warn( "usb_claim_interface: %s", usb_strerror() ); + + Close(); + return false; + } + + m_iInterfaceNumber = device->config->interface->altsetting->bInterfaceNumber; + + return true; +} diff --git a/src/io/USBDriver.h b/src/io/USBDriver.h new file mode 100644 index 00000000..251e94ac --- /dev/null +++ b/src/io/USBDriver.h @@ -0,0 +1,50 @@ +#ifndef IO_USBDRIVER_H +#define IO_USBDRIVER_H + +#include + +/* A class for USB-driven input or output device drivers. */ +class USBDriver +{ +public: + USBDriver(); + virtual ~USBDriver(); + + bool Open(); + void Close(); + +protected: + struct usb_device *FindDevice(); + + virtual bool Matches( int idVendor, int idProduct ); + + usb_dev_handle *m_pHandle; + int m_iInterfaceNumber; +}; + +#endif + +/* + * (c) 2008 BoXoRRoXoRs + * All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, and/or sell copies of the Software, and to permit persons to + * whom the Software is furnished to do so, provided that the above + * copyright notice(s) and this permission notice appear in all copies of + * the Software and that both the above copyright notice(s) and this + * permission notice appear in supporting documentation. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF + * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS + * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT + * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS + * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ -- 2.11.0