]>
git.zerfleddert.de Git - usb-driver/blob - xpcu.c
de23d9eb8f69009d73362e3c6ce8fcde92c4a387
6 #include "usb-driver.h"
9 int xpcu_deviceinfo(struct xpcu_s
*xpcu
, unsigned char *buf
) {
12 WDU_CONFIGURATION
**pConfigs
, **pActiveConfig
;
13 WDU_INTERFACE
**pActiveInterface
;
16 struct usb_device_info
*udi
= (struct usb_device_info
*)(buf
+len
);
18 udi
->Descriptor
.bLength
= sizeof(WDU_DEVICE_DESCRIPTOR
);
19 udi
->Descriptor
.bDescriptorType
= xpcu
->dev
->descriptor
.bDescriptorType
;
20 udi
->Descriptor
.bcdUSB
= xpcu
->dev
->descriptor
.bcdUSB
;
21 udi
->Descriptor
.bDeviceClass
= xpcu
->dev
->descriptor
.bDeviceClass
;
22 udi
->Descriptor
.bDeviceSubClass
= xpcu
->dev
->descriptor
.bDeviceSubClass
;
23 udi
->Descriptor
.bDeviceProtocol
= xpcu
->dev
->descriptor
.bDeviceProtocol
;
24 udi
->Descriptor
.bMaxPacketSize0
= xpcu
->dev
->descriptor
.bMaxPacketSize0
;
25 udi
->Descriptor
.idVendor
= xpcu
->dev
->descriptor
.idVendor
;
26 udi
->Descriptor
.idProduct
= xpcu
->dev
->descriptor
.idProduct
;
27 udi
->Descriptor
.bcdDevice
= xpcu
->dev
->descriptor
.bcdDevice
;
28 udi
->Descriptor
.iManufacturer
= xpcu
->dev
->descriptor
.iManufacturer
;
29 udi
->Descriptor
.iProduct
= xpcu
->dev
->descriptor
.iProduct
;
30 udi
->Descriptor
.iSerialNumber
= xpcu
->dev
->descriptor
.iSerialNumber
;
31 udi
->Descriptor
.bNumConfigurations
= xpcu
->dev
->descriptor
.bNumConfigurations
;
33 /* TODO: Fix Pipe0! */
34 udi
->Pipe0
.dwNumber
= 0x00;
35 udi
->Pipe0
.dwMaximumPacketSize
= xpcu
->dev
->descriptor
.bMaxPacketSize0
;
37 udi
->Pipe0
.direction
= WDU_DIR_IN_OUT
;
38 udi
->Pipe0
.dwInterval
= 0;
40 pConfigs
= &(udi
->pConfigs
);
41 pActiveConfig
= &(udi
->pActiveConfig
);
42 pActiveInterface
= &(udi
->pActiveInterface
[0]);
45 len
= sizeof(struct usb_device_info
);
47 for (i
=0; i
<xpcu
->dev
->descriptor
.bNumConfigurations
; i
++)
49 struct usb_config_descriptor
*conf_desc
= &xpcu
->dev
->config
[i
];
50 WDU_INTERFACE
**pInterfaces
;
51 WDU_ALTERNATE_SETTING
**pAlternateSettings
[conf_desc
->bNumInterfaces
];
52 WDU_ALTERNATE_SETTING
**pActiveAltSetting
[conf_desc
->bNumInterfaces
];
55 WDU_CONFIGURATION
*cfg
= (WDU_CONFIGURATION
*)(buf
+len
);
60 cfg
->Descriptor
.bLength
= conf_desc
->bLength
;
61 cfg
->Descriptor
.bDescriptorType
= conf_desc
->bDescriptorType
;
62 cfg
->Descriptor
.wTotalLength
= conf_desc
->wTotalLength
;
63 cfg
->Descriptor
.bNumInterfaces
= conf_desc
->bNumInterfaces
;
64 cfg
->Descriptor
.bConfigurationValue
= conf_desc
->bConfigurationValue
;
65 cfg
->Descriptor
.iConfiguration
= conf_desc
->iConfiguration
;
66 cfg
->Descriptor
.bmAttributes
= conf_desc
->bmAttributes
;
67 cfg
->Descriptor
.MaxPower
= conf_desc
->MaxPower
;
69 cfg
->dwNumInterfaces
= conf_desc
->bNumInterfaces
;
71 pInterfaces
= &(cfg
->pInterfaces
);
73 len
+= sizeof(WDU_CONFIGURATION
);
76 *pInterfaces
= (WDU_INTERFACE
*)(buf
+len
);
77 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++) {
78 WDU_INTERFACE
*iface
= (WDU_INTERFACE
*)(buf
+len
);
80 pActiveInterface
[j
] = iface
;
82 pAlternateSettings
[j
] = &(iface
->pAlternateSettings
);
83 iface
->dwNumAltSettings
= xpcu
->dev
->config
[i
].interface
[j
].num_altsetting
;
84 pActiveAltSetting
[j
] = &(iface
->pActiveAltSetting
);
86 len
+= sizeof(WDU_INTERFACE
);
89 len
+= sizeof(WDU_INTERFACE
) * conf_desc
->bNumInterfaces
;
92 for (j
=0; j
<conf_desc
->bNumInterfaces
; j
++)
94 struct usb_interface
*interface
= &xpcu
->dev
->config
[i
].interface
[j
];
97 *pAlternateSettings
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
99 *pActiveAltSetting
[j
] = (WDU_ALTERNATE_SETTING
*)(buf
+len
);
102 for(k
=0; k
<interface
->num_altsetting
; k
++)
104 unsigned char bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
105 WDU_ENDPOINT_DESCRIPTOR
**pEndpointDescriptors
;
106 WDU_PIPE_INFO
**pPipes
;
109 WDU_ALTERNATE_SETTING
*altset
= (WDU_ALTERNATE_SETTING
*)(buf
+len
);
111 altset
->Descriptor
.bLength
= interface
->altsetting
[k
].bLength
;
112 altset
->Descriptor
.bDescriptorType
= interface
->altsetting
[k
].bDescriptorType
;
113 altset
->Descriptor
.bInterfaceNumber
= interface
->altsetting
[k
].bInterfaceNumber
;
114 altset
->Descriptor
.bAlternateSetting
= interface
->altsetting
[k
].bAlternateSetting
;
115 altset
->Descriptor
.bNumEndpoints
= interface
->altsetting
[k
].bNumEndpoints
;
116 altset
->Descriptor
.bInterfaceClass
= interface
->altsetting
[k
].bInterfaceClass
;
117 altset
->Descriptor
.bInterfaceSubClass
= interface
->altsetting
[k
].bInterfaceSubClass
;
118 altset
->Descriptor
.bInterfaceProtocol
= interface
->altsetting
[k
].bInterfaceProtocol
;
119 altset
->Descriptor
.iInterface
= interface
->altsetting
[k
].iInterface
;
120 pEndpointDescriptors
= &(altset
->pEndpointDescriptors
);
121 pPipes
= &(altset
->pPipes
);
124 len
+=sizeof(WDU_ALTERNATE_SETTING
);
127 *pEndpointDescriptors
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
128 for (l
= 0; l
< bNumEndpoints
; l
++) {
129 WDU_ENDPOINT_DESCRIPTOR
*ed
= (WDU_ENDPOINT_DESCRIPTOR
*)(buf
+len
);
131 ed
->bLength
= interface
->altsetting
[k
].endpoint
[l
].bLength
;
132 ed
->bDescriptorType
= interface
->altsetting
[k
].endpoint
[l
].bDescriptorType
;
133 ed
->bEndpointAddress
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
134 ed
->bmAttributes
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
;
135 ed
->wMaxPacketSize
= interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
;
136 ed
->bInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
138 len
+= sizeof(WDU_ENDPOINT_DESCRIPTOR
);
141 *pPipes
= (WDU_PIPE_INFO
*)(buf
+len
);
142 for (l
= 0; l
< bNumEndpoints
; l
++) {
143 WDU_PIPE_INFO
*pi
= (WDU_PIPE_INFO
*)(buf
+len
);
145 pi
->dwNumber
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
;
146 pi
->dwMaximumPacketSize
= WDU_GET_MAX_PACKET_SIZE(interface
->altsetting
[k
].endpoint
[l
].wMaxPacketSize
);
147 pi
->type
= interface
->altsetting
[k
].endpoint
[l
].bmAttributes
& USB_ENDPOINT_TYPE_MASK
;
148 if (pi
->type
== PIPE_TYPE_CONTROL
)
149 pi
->direction
= WDU_DIR_IN_OUT
;
152 pi
->direction
= interface
->altsetting
[k
].endpoint
[l
].bEndpointAddress
& USB_ENDPOINT_DIR_MASK
? WDU_DIR_IN
: WDU_DIR_OUT
;
155 pi
->dwInterval
= interface
->altsetting
[k
].endpoint
[l
].bInterval
;
157 len
+= sizeof(WDU_PIPE_INFO
);
160 len
+=(sizeof(WDU_ENDPOINT_DESCRIPTOR
)+sizeof(WDU_PIPE_INFO
))*bNumEndpoints
;
169 static int xpcu_claim(struct xpcu_s
*xpcu
, int claim
) {
171 static int claimed
= 0;
173 if (xpcu
->interface
< 0)
176 if (claim
== XPCU_CLAIM
) {
180 ret
= usb_claim_interface(xpcu
->handle
, xpcu
->interface
);
183 ret
= usb_set_altinterface(xpcu
->handle
, xpcu
->alternate
);
185 fprintf(stderr
, "usb_set_altinterface: %d\n", ret
);
187 fprintf(stderr
, "usb_claim_interface: %d -> %d (%s)\n",
188 xpcu
->interface
, ret
, usb_strerror());
194 ret
= usb_release_interface(xpcu
->handle
, xpcu
->interface
);
202 int xpcu_transfer(struct xpcu_s
*xpcu
, struct usb_transfer
*ut
) {
205 xpcu_claim(xpcu
, XPCU_CLAIM
);
206 /* http://www.jungo.com/support/documentation/windriver/802/wdusb_man_mhtml/node55.html#SECTION001213000000000000000 */
207 if (ut
->dwPipeNum
== 0) { /* control pipe */
208 int requesttype
, request
, value
, index
, size
;
209 requesttype
= ut
->SetupPacket
[0];
210 request
= ut
->SetupPacket
[1];
211 value
= ut
->SetupPacket
[2] | (ut
->SetupPacket
[3] << 8);
212 index
= ut
->SetupPacket
[4] | (ut
->SetupPacket
[5] << 8);
213 size
= ut
->SetupPacket
[6] | (ut
->SetupPacket
[7] << 8);
214 DPRINTF("requesttype: %x, request: %x, value: %u, index: %u, size: %u\n", requesttype
, request
, value
, index
, size
);
215 ret
= usb_control_msg(xpcu
->handle
, requesttype
, request
, value
, index
, ut
->pBuffer
, size
, ut
->dwTimeout
);
218 ret
= usb_bulk_read(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
220 ret
= usb_bulk_write(xpcu
->handle
, ut
->dwPipeNum
, ut
->pBuffer
, ut
->dwBufferSize
, ut
->dwTimeout
);
222 xpcu_claim(xpcu
, XPCU_RELEASE
);
226 fprintf(stderr
, "usb_transfer: %d (%s)\n", ret
, usb_strerror());
228 ut
->dwBytesTransferred
= ret
;
235 void xpcu_set_interface(struct xpcu_s
*xpcu
, struct usb_set_interface
*usi
) {
238 xpcu
->handle
= usb_open(xpcu
->dev
);
241 usb_reset(xpcu
->handle
);
242 xpcu
->handle
= usb_open(xpcu
->dev
);
247 xpcu
->interface
= xpcu
->dev
->config
[0].interface
[usi
->dwInterfaceNum
].altsetting
[usi
->dwAlternateSetting
].bInterfaceNumber
;
248 xpcu
->alternate
= usi
->dwAlternateSetting
;
252 struct xpcu_s
*xpcu_open(void) {
253 static struct xpcu_s xpcu
;
255 bzero(&xpcu
, sizeof(xpcu
));
263 xpcu
.busses
= usb_get_busses();
268 void xpcu_close(struct xpcu_s
*xpcu
) {
270 xpcu_claim(xpcu
, XPCU_RELEASE
);
271 usb_close(xpcu
->handle
);
275 xpcu
->interface
= -1;
276 xpcu
->alternate
= -1;