2 * Copyright (C) 2009 Ubixum, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 fx2::fx2():dev_handle(NULL) {
27 int rv=libusb_init(&libusb_ctx);
29 libusb_set_debug(libusb_ctx,0);
32 void fx2::set_debug_level(int n) {
33 libusb_set_debug(libusb_ctx,n);
38 if (isopen()) close();
40 libusb_exit(libusb_ctx);
43 #define CHECK_OPEN(r) if (!dev_handle) {\
44 printf ( "Device not opened.\n" ); \
49 void fx2::open(int vid,int pid,int idx) {
52 int devices = libusb_get_device_list( libusb_ctx, &list );
54 for ( int i=0;i<devices;++i) {
55 libusb_device_descriptor dscr;
56 if ( !libusb_get_device_descriptor ( list[i], &dscr ) ) {
57 if ( dscr.idVendor == vid && dscr.idProduct == pid ) {
58 if ( idx == cur_idx++ ) {
59 int rv = libusb_open( list[i], &dev_handle);
61 rv=libusb_claim_interface(dev_handle,0);
62 if (!rv) { interface=0;
63 rv=libusb_set_interface_alt_setting(dev_handle,0,0);
65 libusb_close(dev_handle);
70 libusb_close(dev_handle);
74 printf ( "Unable to open device idx: %d, ret: %d\n", idx, rv );
82 printf ( "Device not opened.\n" );
84 libusb_free_device_list(list,1);
86 void fx2::set_interface(int iface, int alt){
88 if (interface != iface) {
89 libusb_release_interface(dev_handle,interface);
90 int rv=libusb_claim_interface(dev_handle,iface);
92 this->interface=iface;
94 int rv=libusb_set_interface_alt_setting(dev_handle,interface,alt);
100 libusb_release_interface(dev_handle,interface);
101 libusb_close(dev_handle);
103 interface=0;alt_setting=0;
107 int fx2::do_usb_command(char* buf, int size, unsigned char type, unsigned char request, unsigned short value, unsigned short index, unsigned short length, int timeout ) {
109 return libusb_control_transfer (
120 int fx2::clear_halt(char ep) {
122 return libusb_clear_halt(dev_handle,(unsigned char)ep);
127 int rv=libusb_reset_device(dev_handle);
128 if (rv==LIBUSB_ERROR_NO_DEVICE) {
129 printf ( "Device Changed. Closing\n");
130 libusb_close(dev_handle);
131 interface=0;alt_setting=0;
136 int fx2::set_configuration(int configuration) {
138 libusb_release_interface(dev_handle,interface);
139 int rv=libusb_set_configuration(dev_handle,configuration);
141 libusb_claim_interface(dev_handle,interface);
146 bool fx2::ep_bulk(char* buf, int size, unsigned char ep, int timeout) {
149 int rv=libusb_bulk_transfer ( dev_handle, ep, (unsigned char*)buf, size, &transferred, timeout );
151 if (!rv) return true;
153 if (rv==LIBUSB_ERROR_TIMEOUT) {
154 printf ( "Transfer Timeout. %d bytes transferred.\n", transferred );
156 printf ( "Transfer Error: %d\n", rv );