-
Notifications
You must be signed in to change notification settings - Fork 19
/
usb-linux.c
158 lines (139 loc) · 6 KB
/
usb-linux.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/****************************************************************************
File : usb-linux.c
Description : Encapsulates all nonportable, Linux-specific USB I/O code
within the mphidflash program. Each supported operating
system has its own source file, providing a common calling
syntax to the portable sections of the code.
History : 2009-03-16 Phillip Burgess
* Initial linux support for 'ubw32' program
2009-12-26 Thomas Fischl, Dominik Fisch (www.FundF.net)
* Renamed 'ubw32' to 'mphidflash'
2010-12-28 Petr Olivka
* program and verify only data for defined memory areas
* send only even length of data to PIC
License : Copyright (C) 2009 Phillip Burgess
Copyright (C) 2009 Thomas Fischl, Dominik Fisch (www.FundF.net)
Copyright (C) 2010 Petr Olivka
This file is part of 'mphidflash' program.
'mphidflash' is free software: you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version
3 of the License, or (at your option) any later version.
'mphidflash' is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with 'mphidflash' source code. If not,
see <http://www.gnu.org/licenses/>.
****************************************************************************/
#include <stdio.h>
#include <usb.h>
#include <hid.h>
#include "mphidflash.h"
#include <errno.h>
static HIDInterface *hid = NULL;
unsigned char usbBufX[64];
unsigned char * usbBuf = usbBufX;
/****************************************************************************
Function : usbOpen
Description : Searches for and opens the first available Bootloader device.
Parameters : unsigned short Vendor ID to search for.
unsigned short Product ID to search for.
Returns : Status code:
ERR_NONE Success; device open and ready for I/O.
ERR_USB_INIT1 Initialization error in HID init code.
ERR_USB_INIT2 New HID alloc failed.
ERR_DEVICE_NOT_FOUND Device not detected on any USB bus
(might be connected but not in
Bootloader mode).
Notes : If multiple devices are connected, only the first device
found (and not in use by another application) is returned.
This code sets no particular preference or sequence in the
search ordering; whatever the default libhid 'matching
function' decides.
****************************************************************************/
ErrorCode usbOpen(
const unsigned short vendorID,
const unsigned short productID)
{
ErrorCode status = ERR_USB_INIT1;
HIDInterfaceMatcher matcher;
matcher.vendor_id = vendorID;
matcher.product_id = productID;
matcher.matcher_fn = NULL;
if (debugLevel >= DEBUG_USB) {
hid_set_debug(HID_DEBUG_ALL);
hid_set_usb_debug(HID_DEBUG_ALL);
}
if(HID_RET_SUCCESS == hid_init()) {
status = ERR_USB_INIT2;
if((hid = hid_new_HIDInterface())) {
if(HID_RET_SUCCESS ==
hid_force_open(hid,0,&matcher,3)) {
return ERR_NONE;
}
status = ERR_DEVICE_NOT_FOUND;
hid_delete_HIDInterface(&hid);
}
hid_cleanup();
}
return status;
}
/****************************************************************************
Function : usbWrite
Description : Write data packet to currently-open USB device, optionally
followed by a packet read operation. Data source is always
global array usbBuf[]. For read operation, destination is
always usbBuf[] also, overwriting contents there.
Parameters : char Size of source data in bytes (max 64).
char If set, read response packet.
Returns : ErrorCode ERR_NONE on success, ERR_USB_WRITE on error.
Notes : Device is assumed to have already been successfully opened
by the time this function is called; no checks performed here.
****************************************************************************/
ErrorCode usbWrite(
const char len,
const char read)
{
#ifdef DEBUG
int i;
(void)puts("Sending:");
for(i=0;i<8;i++) (void)printf("%02x ",((unsigned char *)usbBuf)[i]);
(void)printf(": ");
for(;i<64;i++) (void)printf("%02x ",((unsigned char *)usbBuf)[i]);
(void)putchar('\n'); fflush(stdout);
DEBUGMSG("\nAbout to write");
#endif
if(HID_RET_SUCCESS != hid_interrupt_write(hid,0x01,usbBuf,len,0))
return ERR_USB_WRITE;
DEBUGMSG("Done w/write");
if(read) {
DEBUGMSG("About to read");
if(HID_RET_SUCCESS != hid_interrupt_read(hid,0x81,usbBuf,64,0))
return ERR_USB_READ;
#ifdef DEBUG
(void)puts("Done reading\nReceived:");
for(i=0;i<8;i++) (void)printf("%02x ",usbBuf[i]);
(void)printf(": ");
for(;i<64;i++) (void)printf("%02x ",usbBuf[i]);
(void)putchar('\n'); fflush(stdout);
#endif
}
return ERR_NONE;
}
/****************************************************************************
Function : usbClose
Description : Closes previously-opened USB device.
Parameters : None (void)
Returns : Nothing (void)
Notes : Device is assumed to have already been successfully opened
by the time this function is called; no checks performed here.
****************************************************************************/
void usbClose(void)
{
(void)hid_close(hid);
hid_delete_HIDInterface(&hid);
(void)hid_cleanup();
hid = NULL;
}