Gentoo Archives: gentoo-commits

From: "Mike Frysinger (vapier)" <vapier@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] gentoo-x86 commit in dev-embedded/smdk-dltool/files: smdk-dltool-0.20-libusb-1.0.patch smdk-dltool-0.20-build.patch
Date: Mon, 30 Jan 2012 23:35:49
Message-Id: 20120130233538.1604A2004C@flycatcher.gentoo.org
1 vapier 12/01/30 23:35:38
2
3 Modified: smdk-dltool-0.20-build.patch
4 Added: smdk-dltool-0.20-libusb-1.0.patch
5 Log:
6 Port to libusb-1.0.
7
8 (Portage version: 2.2.0_alpha84/cvs/Linux x86_64)
9
10 Revision Changes Path
11 1.2 dev-embedded/smdk-dltool/files/smdk-dltool-0.20-build.patch
12
13 file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/dev-embedded/smdk-dltool/files/smdk-dltool-0.20-build.patch?rev=1.2&view=markup
14 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/dev-embedded/smdk-dltool/files/smdk-dltool-0.20-build.patch?rev=1.2&content-type=text/plain
15 diff : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/dev-embedded/smdk-dltool/files/smdk-dltool-0.20-build.patch?r1=1.1&r2=1.2
16
17 Index: smdk-dltool-0.20-build.patch
18 ===================================================================
19 RCS file: /var/cvsroot/gentoo-x86/dev-embedded/smdk-dltool/files/smdk-dltool-0.20-build.patch,v
20 retrieving revision 1.1
21 retrieving revision 1.2
22 diff -u -r1.1 -r1.2
23 --- smdk-dltool-0.20-build.patch 20 Jan 2012 22:44:16 -0000 1.1
24 +++ smdk-dltool-0.20-build.patch 30 Jan 2012 23:35:37 -0000 1.2
25 @@ -2,7 +2,7 @@
26
27 --- a/releases/smdk-tools-v0.20/dltool/Makefile
28 +++ b/releases/smdk-tools-v0.20/dltool/Makefile
29 -@@ -7,8 +7,11 @@
30 +@@ -7,8 +7,13 @@
31
32 .PHONY: clean
33
34 @@ -10,7 +10,9 @@
35 - $(CC) -O2 -Wall -g -lusb -o smdk-usbdl dltool.c
36 +CFLAGS ?= -O2 -g
37 +CFLAGS += -Wall
38 -+LDLIBS = -lusb
39 ++PKG_CONFIG ?= pkg-config
40 ++CPPFLAGS += $(shell $(PKG_CONFIG) --cflags libusb)
41 ++LDLIBS = $(shell $(PKG_CONFIG) --libs libusb)
42 +
43 +all: dltool
44
45
46
47
48 1.1 dev-embedded/smdk-dltool/files/smdk-dltool-0.20-libusb-1.0.patch
49
50 file : http://sources.gentoo.org/viewvc.cgi/gentoo-x86/dev-embedded/smdk-dltool/files/smdk-dltool-0.20-libusb-1.0.patch?rev=1.1&view=markup
51 plain: http://sources.gentoo.org/viewvc.cgi/gentoo-x86/dev-embedded/smdk-dltool/files/smdk-dltool-0.20-libusb-1.0.patch?rev=1.1&content-type=text/plain
52
53 Index: smdk-dltool-0.20-libusb-1.0.patch
54 ===================================================================
55 migrate to libusb-1 by me (Mike Frysinger)
56
57 --- a/Makefile
58 +++ b/Makefile
59 @@ -10,8 +10,8 @@
60 CFLAGS ?= -O2 -g
61 CFLAGS += -Wall
62 PKG_CONFIG ?= pkg-config
63 -CPPFLAGS += $(shell $(PKG_CONFIG) --cflags libusb)
64 -LDLIBS = $(shell $(PKG_CONFIG) --libs libusb)
65 +CPPFLAGS += $(shell $(PKG_CONFIG) --cflags libusb-1.0)
66 +LDLIBS = $(shell $(PKG_CONFIG) --libs libusb-1.0)
67
68 all: dltool
69
70 --- a/dltool.c
71 +++ b/dltool.c
72 @@ -13,9 +13,11 @@
73 #include <stdio.h>
74 #include <getopt.h>
75 #include <fcntl.h>
76 +#include <errno.h>
77 +#include <unistd.h>
78
79
80 -#include <usb.h>
81 +#include <libusb.h>
82
83 unsigned int debug = 0;
84 unsigned long dl_addr = 0x30000000L;
85 @@ -23,12 +25,22 @@ unsigned long dl_size = 0L;
86 unsigned char *dl_data = NULL;
87
88 char *dl_file = "download.dat";
89 -char *dl_udev = NULL;
90 -char *dl_ubus = NULL;
91 +libusb_context *ctx = NULL;
92 +libusb_device_handle *devh = NULL;
93
94 int ep_out = 0;
95
96 #define DBG(x) if (debug) { printf x; }
97 +#define err(fmt, args...) \
98 + do { \
99 + if (devh) \
100 + libusb_close(devh); \
101 + if (ctx) \
102 + libusb_exit(ctx); \
103 + fprintf(stderr, "dltool: " fmt "\n", ## args); \
104 + exit(1); \
105 + } while (0)
106 +#define errp(fmt, args...) err(fmt ": %s", ## args, strerror(errno))
107
108 void write_u32(unsigned char *dp, unsigned long val)
109 {
110 @@ -108,25 +120,30 @@ void calc_cksum(unsigned char *data, ssize_t len)
111 cp[1] = cksum >> 8;
112 }
113
114 -int verify_device(struct usb_device *dev)
115 +int verify_device(libusb_device *dev)
116 {
117 + struct libusb_device_descriptor desc;
118 +
119 + if (libusb_get_device_descriptor(dev, &desc))
120 + return 0;
121 +
122 DBG(("dev %p: configurations %d\n",
123 - dev, dev->descriptor.bNumConfigurations));
124 + dev, desc.bNumConfigurations));
125
126 - if (dev->descriptor.bNumConfigurations != 1)
127 + if (desc.bNumConfigurations != 1)
128 return 0;
129
130 - DBG(("\t=> bLength %d\n", dev->descriptor.bLength));
131 - DBG(("\t=> bType %d\n", dev->descriptor.bDescriptorType));
132 - DBG(("\t=> bcdUSB %x\n", dev->descriptor.bcdUSB));
133 - DBG(("\t=> idVendor %x\n", dev->descriptor.idVendor));
134 - DBG(("\t=> idProduct %x\n", dev->descriptor.idProduct));
135 + DBG(("\t=> bLength %d\n", desc.bLength));
136 + DBG(("\t=> bType %d\n", desc.bDescriptorType));
137 + DBG(("\t=> bcdUSB %x\n", desc.bcdUSB));
138 + DBG(("\t=> idVendor %x\n", desc.idVendor));
139 + DBG(("\t=> idProduct %x\n", desc.idProduct));
140
141 - if (dev->descriptor.idVendor == 0x5345 && dev->descriptor.idProduct == 0x1234) {
142 + if (desc.idVendor == 0x5345 && desc.idProduct == 0x1234) {
143 ep_out = 3;
144 return 1;
145 }
146 - else if(dev->descriptor.idVendor == 0x4e8 && dev->descriptor.idProduct == 0x1234){
147 + else if(desc.idVendor == 0x4e8 && desc.idProduct == 0x1234){
148 printf("S3C64XX Detected!\n");
149 ep_out = 2;
150 return 1;
151 @@ -173,6 +190,12 @@ struct option long_opts[] = {
152 .val = 'x',
153 },
154 {
155 + .name = "help",
156 + .has_arg = 0,
157 + .flag = NULL,
158 + .val = 'h',
159 + },
160 + {
161 .name = NULL
162 }
163 };
164 @@ -181,12 +198,14 @@ int flg_show = 0;
165
166 int main(int argc, char **argv)
167 {
168 - struct usb_bus *bus, *busp;
169 - struct usb_device *result = NULL;
170 - struct usb_device *found = NULL;
171 + ssize_t num_devs, i;
172 + libusb_device **list;
173 + libusb_device *found;
174 + int dl_ubus = -1;
175 + int dl_udev = -1;
176 + uint8_t bus_num, dev_num;
177 unsigned long fsize;
178 - usb_dev_handle *devh;
179 - int ret;
180 + int ret, transferred;
181
182 printf("SMDK42XX,S3C64XX USB Download Tool\n");
183 printf("Version 0.20 (c) 2004,2005,2006"
184 @@ -197,7 +216,7 @@ int main(int argc, char **argv)
185 int index = 0;
186 int c;
187
188 - c = getopt_long(argc, argv, "a:b:d:f:s", long_opts, &index);
189 + c = getopt_long(argc, argv, "a:b:d:f:shx", long_opts, &index);
190
191 DBG(("option index %d\n",c ));
192
193 @@ -218,117 +237,116 @@ int main(int argc, char **argv)
194 break;
195
196 case 'b':
197 - dl_ubus = optarg;
198 + dl_ubus = atoi(optarg);
199 break;
200
201 case 'd':
202 - dl_udev = optarg;
203 + dl_udev = atoi(optarg);
204 break;
205
206 case 'x':
207 debug = 1;
208 + break;
209 +
210 + case 'h':
211 + puts(
212 + "Usage: dltool [options]\n"
213 + "\n"
214 + "-a <download addr>\n"
215 + "-b <bus #>\n"
216 + "-d <dev #>\n"
217 + "-f <file>\n"
218 + "-s Show found devices\n"
219 + "-x Enable debug\n"
220 + );
221 + return 0;
222 }
223 }
224
225 - usb_init();
226 - usb_find_busses();
227 - usb_find_devices();
228 -
229 - bus = usb_get_busses();
230 -
231 - DBG(("usb_get_busses: %p\n", bus));
232 -
233 - for (busp = bus; busp != NULL; busp = busp->next) {
234 - struct usb_device *dev;
235 -
236 - DBG(("bus %p: dirname %s\n", busp, busp->dirname));
237 -
238 - if (dl_ubus) {
239 - if (strcmp(busp->dirname, dl_ubus) != 0)
240 - continue;
241 + ret = libusb_init(&ctx);
242 + if (ret)
243 + errp("could not initialize usb stack");
244 +
245 + bus_num = dev_num = 0;
246 + found = NULL;
247 + num_devs = libusb_get_device_list(ctx, &list);
248 + for (i = 0; i < num_devs; ++i) {
249 + libusb_device *dev = list[i];
250 + bus_num = libusb_get_bus_number(dev);
251 + dev_num = libusb_get_device_address(dev);
252 +
253 + DBG(("bus %u; dev %u (%p)\n", bus_num, dev_num, dev));
254 +
255 + if (dl_ubus >= 0 && bus_num != dl_ubus)
256 + continue;
257 +
258 + if (!verify_device(dev))
259 + continue;
260 +
261 + if (flg_show) {
262 + printf("bus %u: device %u\n", bus_num, dev_num);
263 + continue;
264 }
265
266 - for (dev = busp->devices; dev != NULL; dev = dev->next) {
267 - DBG(("dev %p filename %s\n", dev, dev->filename));
268 + if (dl_udev >= 0 && dev_num != dl_udev)
269 + continue;
270
271 - if (!verify_device(dev))
272 - continue;
273 -
274 - if (flg_show) {
275 - printf("bus %s: device %s\n",
276 - busp->dirname, dev->filename);
277 - continue;
278 - }
279 -
280 - found = dev;
281 -
282 - if (dl_udev) {
283 - if (strcmp(dev->filename, dl_udev) == 0) {
284 - result = dev;
285 - break;
286 - }
287 - }
288 - }
289 -
290 - if (result != NULL)
291 - break;
292 + found = dev;
293 + break;
294 }
295
296 if (flg_show)
297 return 0;
298
299 - DBG(("device %p, found %p\n", result, found));
300 + DBG(("found %p\n", found));
301
302 - if (result == NULL && found != NULL)
303 - result = found;
304 -
305 - if (result == NULL) {
306 - fprintf(stderr, "failed to find device\n");
307 - return 1;
308 - }
309 + if (found == NULL)
310 + err("failed to find device\n");
311
312 - printf("=> found device: bus %s, dev %s\n",
313 - result->bus->dirname, result->filename);
314 + printf("=> found device: bus %u, dev %u\n",
315 + bus_num, dev_num);
316
317 dl_data = load_file(dl_file, &dl_size, &fsize);
318 - if (dl_data == NULL) {
319 - printf("failed to load %s\n", dl_file);
320 - return 1;
321 - }
322 + if (dl_data == NULL)
323 + errp("failed to load %s", dl_file);
324
325 printf("=> loaded %ld bytes from %s\n", fsize, dl_file);
326
327 - devh = usb_open(result);
328 - if (devh == NULL) {
329 - perror("usb_open");
330 - return 1;
331 - }
332 + ret = libusb_open(found, &devh);
333 + if (ret == 0) {
334 + uint8_t configuration;
335 + struct libusb_config_descriptor *config;
336 + libusb_get_active_config_descriptor(found, &config);
337 + configuration = config->bConfigurationValue;
338 + libusb_free_config_descriptor(config);
339 + libusb_set_configuration(devh, configuration);
340 + } else
341 + errp("libusb_open");
342
343 DBG(("claim interface\n"));
344
345 - if (usb_claim_interface(devh, 0) < 0) {
346 - perror("usb_claim_interface");
347 - usb_close(devh);
348 - return 1;
349 - }
350 + ret = libusb_claim_interface(devh, 0);
351 + if (ret)
352 + errp("libusb_claim_interface");
353
354 printf("=> Downloading %ld bytes to 0x%08lx\n", dl_size, dl_addr);
355
356 write_header(dl_data, dl_addr, dl_size);
357 calc_cksum(dl_data, dl_size);
358
359 - //ret = usb_bulk_write(devh, 3, (void *)dl_data, dl_size, 5*1000*1000);
360 - ret = usb_bulk_write(devh, ep_out, (void *)dl_data, dl_size, 5*1000*1000);
361 + //ret = libusb_bulk_transfer(devh, 3, dl_data, dl_size, &transferred, 5*1000*1000);
362 + ret = libusb_bulk_transfer(devh, ep_out, dl_data, dl_size, &transferred, 5*1000*1000);
363 printf("=> usb_bulk_write() returned %d\n", ret);
364
365 - if (ret != dl_size) {
366 - printf("failed to write %ld bytes\n", dl_size);
367 + if (ret || transferred != dl_size) {
368 + printf("failed to write %ld bytes (wrote %d)\n", dl_size, transferred);
369 }
370
371 free(dl_data);
372
373 - usb_release_interface(devh, 0);
374 - usb_close(devh);
375 + libusb_release_interface(devh, 0);
376 + libusb_close(devh);
377 + libusb_exit(ctx);
378
379 return 0;
380 }