-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusbms.c
More file actions
130 lines (110 loc) · 3.9 KB
/
usbms.c
File metadata and controls
130 lines (110 loc) · 3.9 KB
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
/* This file is part of actionpro-cli.
*
* Actionpro-cli 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.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <libusb-1.0/libusb.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "usbms.h"
#include "xusb.h"
#include "config.h"
static struct libusb_device_handle *handle = NULL;
int send_command(const uint8_t opcode, const uint8_t *buf,
const uint8_t buflen) {
int rc;
uint32_t expected_tag = 0;
int size;
uint8_t cdb[CDB_MAX_LENGTH];
uint8_t sense[REQUEST_SENSE_LENGTH];
memset(cdb, 0, sizeof(cdb));
cdb[0] = 0x03; // Request Sense
cdb[4] = REQUEST_SENSE_LENGTH;
send_mass_storage_command(handle, 0x01, 0, cdb, LIBUSB_ENDPOINT_IN,
REQUEST_SENSE_LENGTH, &expected_tag);
rc = libusb_bulk_transfer(handle, 0x81, (unsigned char *)&sense,
REQUEST_SENSE_LENGTH, &size, 1000);
if (rc < 0) {
printf("libusb_bulk_transfer failed: %s\n", libusb_error_name(rc));
return ACTIONPRO_CMD_ERROR;
}
rc = libusb_bulk_transfer(handle, 0x81, (unsigned char *)&sense,
REQUEST_SENSE_LENGTH, &size, 1000);
if (rc < 0) {
printf("libusb_bulk_transfer failed: %s\n", libusb_error_name(rc));
return ACTIONPRO_CMD_ERROR;
}
memset(cdb, 0, sizeof(cdb));
cdb[0] = opcode;
memcpy(cdb + 2, buf, buflen);
send_mass_storage_command(handle, 0x01, 0, cdb, LIBUSB_ENDPOINT_IN,
REQUEST_SENSE_LENGTH, &expected_tag);
rc = libusb_bulk_transfer(handle, 0x81, (unsigned char *)&sense,
REQUEST_SENSE_LENGTH, &size, 1000);
if (rc < 0) {
printf("libusb_bulk_transfer failed: %s\n", libusb_error_name(rc));
return ACTIONPRO_CMD_ERROR;
}
return ACTIONPRO_CMD_OK;
}
int open_device() {
int rc;
if (handle)
return ACTIONPRO_OK;
rc = libusb_init(NULL);
if (rc < 0) {
printf("\nFailed to initialize libusb\n");
return ACTIONPRO_ERROR;
}
handle = libusb_open_device_with_vid_pid(NULL, ACTION_VENDOR_ID,
ACTION_PRODUCT_ID);
if (handle == NULL) {
fprintf(stderr, "Cannot open camera device %04X:%04X.\n", ACTION_VENDOR_ID,
ACTION_PRODUCT_ID);
return ACTIONPRO_NOT_FOUND;
}
libusb_set_configuration(handle, ACTIONPRO_USB_CONFIGURATION);
if (libusb_kernel_driver_active(handle, ACTIONPRO_USB_INTERFACE) == 1) {
rc = libusb_detach_kernel_driver(handle, ACTIONPRO_USB_INTERFACE);
if (rc != 0) {
fprintf(stderr, "Failed to detach kernel driver.\n");
return ACTIONPRO_ERROR;
}
}
rc = libusb_claim_interface(handle, ACTIONPRO_USB_INTERFACE);
if (rc != 0) {
fprintf(stderr, "Failed to claim usb interface.\n");
return ACTIONPRO_ERROR;
}
return ACTIONPRO_OK;
}
int close_device() {
if (handle) {
libusb_release_interface(handle, 0);
libusb_reset_device(handle);
libusb_close(handle);
handle = NULL;
}
libusb_exit(NULL);
return ACTIONPRO_OK;
}