1 | /******************************************************************************* |
---|
2 | Copyright(c) 2011 Gerry Rozema. All rights reserved. |
---|
3 | |
---|
4 | This library is free software; you can redistribute it and/or |
---|
5 | modify it under the terms of the GNU Library General Public |
---|
6 | License version 2 as published by the Free Software Foundation. |
---|
7 | |
---|
8 | This library is distributed in the hope that it will be useful, |
---|
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
11 | Library General Public License for more details. |
---|
12 | |
---|
13 | You should have received a copy of the GNU Library General Public License |
---|
14 | along with this library; see the file COPYING.LIB. If not, write to |
---|
15 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
---|
16 | Boston, MA 02110-1301, USA. |
---|
17 | *******************************************************************************/ |
---|
18 | #include "indiusbdevice.h" |
---|
19 | |
---|
20 | #include <string.h> |
---|
21 | |
---|
22 | INDI::USBDevice::USBDevice() |
---|
23 | { |
---|
24 | dev=NULL; |
---|
25 | usb_handle=NULL; |
---|
26 | OutputEndpoint=0; |
---|
27 | InputEndpoint=0; |
---|
28 | |
---|
29 | usb_init(); |
---|
30 | usb_find_busses(); |
---|
31 | usb_find_devices(); |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | INDI::USBDevice::~USBDevice() |
---|
36 | { |
---|
37 | } |
---|
38 | |
---|
39 | struct usb_device * INDI::USBDevice::FindDevice(int vendor, int product, int searchindex) |
---|
40 | { |
---|
41 | struct usb_device *dev; |
---|
42 | struct usb_bus *usb_bus; |
---|
43 | int index=0; |
---|
44 | |
---|
45 | for(usb_bus=usb_busses; usb_bus; usb_bus=usb_bus->next) { |
---|
46 | for(dev=usb_bus->devices; dev; dev=dev->next) { |
---|
47 | if(dev->descriptor.idVendor==vendor) { |
---|
48 | if(dev->descriptor.idProduct==product) { |
---|
49 | if(index==searchindex) { |
---|
50 | fprintf(stderr,"Device has %d configurations\n",dev->descriptor.bNumConfigurations); |
---|
51 | return dev; |
---|
52 | } |
---|
53 | else index++; |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | } |
---|
58 | return NULL; |
---|
59 | |
---|
60 | } |
---|
61 | |
---|
62 | int INDI::USBDevice::Open() |
---|
63 | { |
---|
64 | if(dev==NULL) return -1; |
---|
65 | |
---|
66 | usb_handle=usb_open(dev); |
---|
67 | if(usb_handle != NULL) { |
---|
68 | //printf("Opened ok\n"); |
---|
69 | |
---|
70 | return FindEndpoints(); |
---|
71 | //return 0; |
---|
72 | } |
---|
73 | return -1; |
---|
74 | } |
---|
75 | |
---|
76 | int INDI::USBDevice::FindEndpoints() |
---|
77 | { |
---|
78 | |
---|
79 | int rc=0; |
---|
80 | struct usb_interface_descriptor *intf; |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | intf=&dev->config[0].interface[0].altsetting[0]; |
---|
85 | for(int i=0; i<intf->bNumEndpoints; i++) { |
---|
86 | fprintf(stderr,"%04x %04x\n", |
---|
87 | intf->endpoint[i].bEndpointAddress, |
---|
88 | intf->endpoint[i].bmAttributes |
---|
89 | ); |
---|
90 | |
---|
91 | int dir; |
---|
92 | int addr; |
---|
93 | addr=intf->endpoint[i].bEndpointAddress; |
---|
94 | addr = addr & (USB_ENDPOINT_DIR_MASK^0xffff); |
---|
95 | //printf("%02x ",addr); |
---|
96 | |
---|
97 | int attr; |
---|
98 | int tp; |
---|
99 | attr=intf->endpoint[i].bmAttributes; |
---|
100 | tp=attr&USB_ENDPOINT_TYPE_MASK; |
---|
101 | //if(tp==USB_ENDPOINT_TYPE_BULK) printf("Bulk "); |
---|
102 | //if(tp==USB_ENDPOINT_TYPE_INTERRUPT) printf("Interrupt "); |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | dir=intf->endpoint[i].bEndpointAddress; |
---|
107 | dir=dir&USB_ENDPOINT_DIR_MASK; |
---|
108 | if(dir==USB_ENDPOINT_IN) { |
---|
109 | //printf("Input "); |
---|
110 | fprintf(stderr,"Got an input endpoint\n"); |
---|
111 | InputEndpoint=addr; |
---|
112 | InputType=tp; |
---|
113 | } |
---|
114 | if(dir==USB_ENDPOINT_OUT) { |
---|
115 | //printf("Output "); |
---|
116 | fprintf(stderr,"got an output endpoint\n"); |
---|
117 | OutputEndpoint=addr; |
---|
118 | OutputType=tp; |
---|
119 | } |
---|
120 | //printf("\n"); |
---|
121 | } |
---|
122 | |
---|
123 | //printf("claim interface returns %d\n",rc); |
---|
124 | return rc; |
---|
125 | |
---|
126 | } |
---|
127 | |
---|
128 | int INDI::USBDevice::ReadInterrupt(char *buf,int c,int timeout) |
---|
129 | { |
---|
130 | int rc; |
---|
131 | |
---|
132 | rc=usb_interrupt_read(usb_handle,InputEndpoint,buf,c,timeout); |
---|
133 | //rc=usb_bulk_read(usb_handle,InputEndpoint,buf,c,timeout); |
---|
134 | return rc; |
---|
135 | |
---|
136 | } |
---|
137 | |
---|
138 | int INDI::USBDevice::WriteInterrupt(char *buf,int c,int timeout) |
---|
139 | { |
---|
140 | int rc; |
---|
141 | |
---|
142 | //printf("Writing %02x to endpoint %d\n",buf[0],OutputEndpoint); |
---|
143 | rc=usb_interrupt_write(usb_handle,OutputEndpoint,buf,c,timeout); |
---|
144 | return rc; |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | int INDI::USBDevice::ReadBulk(char *buf,int nbytes,int timeout) |
---|
149 | { |
---|
150 | int rc; |
---|
151 | |
---|
152 | //rc=usb_interrupt_read(usb_handle,InputEndpoint,buf,c,timeout); |
---|
153 | rc=usb_bulk_read(usb_handle,InputEndpoint,buf,nbytes,timeout); |
---|
154 | return rc; |
---|
155 | |
---|
156 | } |
---|
157 | |
---|
158 | int INDI::USBDevice::WriteBulk(char *buf,int nbytes,int timeout) |
---|
159 | { |
---|
160 | int rc; |
---|
161 | |
---|
162 | //printf("Writing %02x to endpoint %d\n",buf[0],OutputEndpoint); |
---|
163 | //rc=usb_interrupt_write(usb_handle,OutputEndpoint,buf,c,timeout); |
---|
164 | rc=usb_bulk_write(usb_handle,OutputEndpoint,buf,nbytes,timeout); |
---|
165 | return rc; |
---|
166 | |
---|
167 | } |
---|
168 | |
---|
169 | int INDI::USBDevice::ControlMessage() |
---|
170 | { |
---|
171 | char buf[3]; |
---|
172 | int rc; |
---|
173 | |
---|
174 | buf[0]=0; |
---|
175 | buf[1]=0; |
---|
176 | buf[2]=0; |
---|
177 | |
---|
178 | rc=usb_control_msg(usb_handle,0xc2&USB_ENDPOINT_IN, 0x12,0,0,buf,3,1000); |
---|
179 | fprintf(stderr,"UsbControlMessage returns %d\n",rc); |
---|
180 | return 0; |
---|
181 | } |
---|