Skip to content

Commit 0e594da

Browse files
committed
ks: Add support for libusb communication alongside file descriptor
1 parent 30ac3a8 commit 0e594da

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RAMDUMP_SRCS := ramdump.c sahara.c usb.c util.c ux.c
1313
RAMDUMP_OBJS := $(RAMDUMP_SRCS:.c=.o)
1414

1515
KS_OUT := ks
16-
KS_SRCS := ks.c sahara.c util.c ux.c
16+
KS_SRCS := ks.c sahara.c util.c ux.c usb.c
1717
KS_OBJS := $(KS_SRCS:.c=.o)
1818

1919
default: $(QDL) $(RAMDUMP) $(KS_OUT)

ks.c

+38-36
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,17 @@ static struct qdl_device qdl;
2121

2222
bool qdl_debug;
2323

24-
int qdl_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout)
25-
{
26-
return read(qdl->fd, buf, len);
27-
}
28-
29-
int qdl_write(struct qdl_device *qdl, const void *buf, size_t len)
30-
{
31-
32-
return write(qdl->fd, buf, len);
33-
}
34-
3524
static void print_usage(void)
3625
{
3726
extern const char *__progname;
3827
fprintf(stderr,
39-
"%s -p <sahara dev_node> -s <id:file path> ...\n",
28+
"%s [--debug] [--serial <NUM>] [--port <sahara dev_node>] -s <id:file path> ...\n",
4029
__progname);
4130
fprintf(stderr,
4231
" -p --port Sahara device node to use\n"
4332
" -s <id:file path> --sahara <id:file path> Sahara protocol file mapping\n"
4433
"\n"
45-
"One -p instance is required. One or more -s instances are required.\n"
34+
"One or more -s instances are required\n"
4635
"\n"
4736
"Example: \n"
4837
"ks -p /dev/mhi0_QAIC_SAHARA -s 1:/opt/qti-aic/firmware/fw1.bin -s 2:/opt/qti-aic/firmware/fw2.bin\n");
@@ -52,6 +41,7 @@ int main(int argc, char **argv)
5241
{
5342
bool found_mapping = false;
5443
char *dev_node = NULL;
44+
char *serial = NULL;
5545
long file_id;
5646
char *colon;
5747
int opt;
@@ -62,6 +52,7 @@ int main(int argc, char **argv)
6252
{"version", no_argument, 0, 'v'},
6353
{"port", required_argument, 0, 'p'},
6454
{"sahara", required_argument, 0, 's'},
55+
{"serial", required_argument, 0, 'S'},
6556
{0, 0, 0, 0}
6657
};
6758

@@ -80,49 +71,60 @@ int main(int argc, char **argv)
8071
case 's':
8172
found_mapping = true;
8273
file_id = strtol(optarg, NULL, 10);
83-
if (file_id < 0) {
84-
print_usage();
85-
return 1;
86-
}
87-
if (file_id >= MAPPING_SZ) {
88-
fprintf(stderr,
89-
"ID:%ld exceeds the max value of %d\n",
90-
file_id,
91-
MAPPING_SZ - 1);
92-
return 1;
93-
}
74+
if (file_id < 0 || file_id >= MAPPING_SZ)
75+
errx(1, "ID:%ld has to be in range of 0 - %d\n", file_id, MAPPING_SZ - 1);
76+
9477
colon = strchr(optarg, ':');
95-
if (!colon) {
96-
print_usage();
97-
return 1;
98-
}
78+
if (!colon)
79+
errx(1, "Sahara mapping requires ID and file path to be divided by a colon");
80+
9981
qdl.mappings[file_id] = &optarg[colon - optarg + 1];
10082
printf("Created mapping ID:%ld File:%s\n", file_id, qdl.mappings[file_id]);
10183
break;
84+
case 'S':
85+
serial = optarg;
86+
break;
10287
default:
10388
print_usage();
10489
return 1;
10590
}
10691
}
10792

108-
// -p and -s is required
109-
if (!dev_node || !found_mapping) {
93+
// -s is required
94+
if (!found_mapping) {
11095
print_usage();
11196
return 1;
11297
}
11398

11499
if (qdl_debug)
115100
print_version();
116101

117-
qdl.fd = open(dev_node, O_RDWR);
118-
if (qdl.fd < 0) {
119-
fprintf(stderr, "Unable to open %s\n", dev_node);
120-
return 1;
102+
if (dev_node) {
103+
qdl.fd = open(dev_node, O_RDWR);
104+
if (qdl.fd < 0) {
105+
ret = 0;
106+
printf("Unable to open %s\n", dev_node);
107+
goto out_cleanup;
108+
}
109+
}
110+
else {
111+
ret = qdl_open(&qdl, serial);
112+
if (ret) {
113+
printf("Failed to find edl device\n");
114+
goto out_cleanup;
115+
}
121116
}
122117

118+
123119
ret = sahara_run(&qdl, qdl.mappings, false, NULL, NULL);
124120
if (ret < 0)
125-
return 1;
121+
goto out_cleanup;
122+
123+
out_cleanup:
124+
if (dev_node)
125+
close(qdl.fd);
126+
else
127+
qdl_close(&qdl);
126128

127-
return 0;
129+
return !!ret;
128130
}

usb.c

+6
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ int qdl_read(struct qdl_device *qdl, void *buf, size_t len, unsigned int timeout
217217
int actual;
218218
int ret;
219219

220+
if (qdl->fd != 0)
221+
return read(qdl->fd, buf, len);
222+
220223
ret = libusb_bulk_transfer(qdl->usb_handle, qdl->in_ep, buf, len, &actual, timeout);
221224
if ((ret != 0 && ret != LIBUSB_ERROR_TIMEOUT) ||
222225
(ret == LIBUSB_ERROR_TIMEOUT && actual == 0))
@@ -234,6 +237,9 @@ int qdl_write(struct qdl_device *qdl, const void *buf, size_t len)
234237
int xfer;
235238
int ret;
236239

240+
if (qdl->fd != 0)
241+
return write(qdl->fd, buf, len);
242+
237243
while (len > 0) {
238244
xfer = (len > qdl->out_chunk_size) ? qdl->out_chunk_size : len;
239245

0 commit comments

Comments
 (0)