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