smdk-dltool-0.20-libusb-1.0.patch 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. migrate to libusb-1 by me (Mike Frysinger)
  2. --- a/Makefile
  3. +++ b/Makefile
  4. @@ -10,8 +10,8 @@
  5. CFLAGS ?= -O2 -g
  6. CFLAGS += -Wall
  7. PKG_CONFIG ?= pkg-config
  8. -CPPFLAGS += $(shell $(PKG_CONFIG) --cflags libusb)
  9. -LDLIBS = $(shell $(PKG_CONFIG) --libs libusb)
  10. +CPPFLAGS += $(shell $(PKG_CONFIG) --cflags libusb-1.0)
  11. +LDLIBS = $(shell $(PKG_CONFIG) --libs libusb-1.0)
  12. all: dltool
  13. --- a/dltool.c
  14. +++ b/dltool.c
  15. @@ -13,9 +13,11 @@
  16. #include <stdio.h>
  17. #include <getopt.h>
  18. #include <fcntl.h>
  19. +#include <errno.h>
  20. +#include <unistd.h>
  21. -#include <usb.h>
  22. +#include <libusb.h>
  23. unsigned int debug = 0;
  24. unsigned long dl_addr = 0x30000000L;
  25. @@ -23,12 +25,22 @@ unsigned long dl_size = 0L;
  26. unsigned char *dl_data = NULL;
  27. char *dl_file = "download.dat";
  28. -char *dl_udev = NULL;
  29. -char *dl_ubus = NULL;
  30. +libusb_context *ctx = NULL;
  31. +libusb_device_handle *devh = NULL;
  32. int ep_out = 0;
  33. #define DBG(x) if (debug) { printf x; }
  34. +#define err(fmt, args...) \
  35. + do { \
  36. + if (devh) \
  37. + libusb_close(devh); \
  38. + if (ctx) \
  39. + libusb_exit(ctx); \
  40. + fprintf(stderr, "dltool: " fmt "\n", ## args); \
  41. + exit(1); \
  42. + } while (0)
  43. +#define errp(fmt, args...) err(fmt ": %s", ## args, strerror(errno))
  44. void write_u32(unsigned char *dp, unsigned long val)
  45. {
  46. @@ -108,25 +120,30 @@ void calc_cksum(unsigned char *data, ssize_t len)
  47. cp[1] = cksum >> 8;
  48. }
  49. -int verify_device(struct usb_device *dev)
  50. +int verify_device(libusb_device *dev)
  51. {
  52. + struct libusb_device_descriptor desc;
  53. +
  54. + if (libusb_get_device_descriptor(dev, &desc))
  55. + return 0;
  56. +
  57. DBG(("dev %p: configurations %d\n",
  58. - dev, dev->descriptor.bNumConfigurations));
  59. + dev, desc.bNumConfigurations));
  60. - if (dev->descriptor.bNumConfigurations != 1)
  61. + if (desc.bNumConfigurations != 1)
  62. return 0;
  63. - DBG(("\t=> bLength %d\n", dev->descriptor.bLength));
  64. - DBG(("\t=> bType %d\n", dev->descriptor.bDescriptorType));
  65. - DBG(("\t=> bcdUSB %x\n", dev->descriptor.bcdUSB));
  66. - DBG(("\t=> idVendor %x\n", dev->descriptor.idVendor));
  67. - DBG(("\t=> idProduct %x\n", dev->descriptor.idProduct));
  68. + DBG(("\t=> bLength %d\n", desc.bLength));
  69. + DBG(("\t=> bType %d\n", desc.bDescriptorType));
  70. + DBG(("\t=> bcdUSB %x\n", desc.bcdUSB));
  71. + DBG(("\t=> idVendor %x\n", desc.idVendor));
  72. + DBG(("\t=> idProduct %x\n", desc.idProduct));
  73. - if (dev->descriptor.idVendor == 0x5345 && dev->descriptor.idProduct == 0x1234) {
  74. + if (desc.idVendor == 0x5345 && desc.idProduct == 0x1234) {
  75. ep_out = 3;
  76. return 1;
  77. }
  78. - else if(dev->descriptor.idVendor == 0x4e8 && dev->descriptor.idProduct == 0x1234){
  79. + else if(desc.idVendor == 0x4e8 && desc.idProduct == 0x1234){
  80. printf("S3C64XX Detected!\n");
  81. ep_out = 2;
  82. return 1;
  83. @@ -173,6 +190,12 @@ struct option long_opts[] = {
  84. .val = 'x',
  85. },
  86. {
  87. + .name = "help",
  88. + .has_arg = 0,
  89. + .flag = NULL,
  90. + .val = 'h',
  91. + },
  92. + {
  93. .name = NULL
  94. }
  95. };
  96. @@ -181,12 +204,14 @@ int flg_show = 0;
  97. int main(int argc, char **argv)
  98. {
  99. - struct usb_bus *bus, *busp;
  100. - struct usb_device *result = NULL;
  101. - struct usb_device *found = NULL;
  102. + ssize_t num_devs, i;
  103. + libusb_device **list;
  104. + libusb_device *found;
  105. + int dl_ubus = -1;
  106. + int dl_udev = -1;
  107. + uint8_t bus_num, dev_num;
  108. unsigned long fsize;
  109. - usb_dev_handle *devh;
  110. - int ret;
  111. + int ret, transferred;
  112. printf("SMDK42XX,S3C64XX USB Download Tool\n");
  113. printf("Version 0.20 (c) 2004,2005,2006"
  114. @@ -197,7 +222,7 @@ int main(int argc, char **argv)
  115. int index = 0;
  116. int c;
  117. - c = getopt_long(argc, argv, "a:b:d:f:s", long_opts, &index);
  118. + c = getopt_long(argc, argv, "a:b:d:f:shx", long_opts, &index);
  119. DBG(("option index %d\n",c ));
  120. @@ -218,117 +243,127 @@ int main(int argc, char **argv)
  121. break;
  122. case 'b':
  123. - dl_ubus = optarg;
  124. + dl_ubus = atoi(optarg);
  125. break;
  126. case 'd':
  127. - dl_udev = optarg;
  128. + dl_udev = atoi(optarg);
  129. break;
  130. case 'x':
  131. debug = 1;
  132. + break;
  133. +
  134. + case 'h':
  135. + puts(
  136. + "Usage: dltool [options]\n"
  137. + "\n"
  138. + "-a <download addr>\n"
  139. + "-b <bus #>\n"
  140. + "-d <dev #>\n"
  141. + "-f <file>\n"
  142. + "-s Show found devices\n"
  143. + "-x Enable debug\n"
  144. + );
  145. + return 0;
  146. }
  147. }
  148. - usb_init();
  149. - usb_find_busses();
  150. - usb_find_devices();
  151. -
  152. - bus = usb_get_busses();
  153. -
  154. - DBG(("usb_get_busses: %p\n", bus));
  155. -
  156. - for (busp = bus; busp != NULL; busp = busp->next) {
  157. - struct usb_device *dev;
  158. -
  159. - DBG(("bus %p: dirname %s\n", busp, busp->dirname));
  160. -
  161. - if (dl_ubus) {
  162. - if (strcmp(busp->dirname, dl_ubus) != 0)
  163. - continue;
  164. + ret = libusb_init(&ctx);
  165. + if (ret)
  166. + errp("could not initialize usb stack");
  167. +
  168. + bus_num = dev_num = 0;
  169. + found = NULL;
  170. + num_devs = libusb_get_device_list(ctx, &list);
  171. + for (i = 0; i < num_devs; ++i) {
  172. + libusb_device *dev = list[i];
  173. + bus_num = libusb_get_bus_number(dev);
  174. + dev_num = libusb_get_device_address(dev);
  175. +
  176. + DBG(("bus %u; dev %u (%p)\n", bus_num, dev_num, dev));
  177. +
  178. + if (dl_ubus >= 0 && bus_num != dl_ubus)
  179. + continue;
  180. +
  181. + if (!verify_device(dev))
  182. + continue;
  183. +
  184. + if (flg_show) {
  185. + printf("bus %u: device %u\n", bus_num, dev_num);
  186. + continue;
  187. }
  188. - for (dev = busp->devices; dev != NULL; dev = dev->next) {
  189. - DBG(("dev %p filename %s\n", dev, dev->filename));
  190. + if (dl_udev >= 0 && dev_num != dl_udev)
  191. + continue;
  192. - if (!verify_device(dev))
  193. - continue;
  194. -
  195. - if (flg_show) {
  196. - printf("bus %s: device %s\n",
  197. - busp->dirname, dev->filename);
  198. - continue;
  199. - }
  200. -
  201. - found = dev;
  202. -
  203. - if (dl_udev) {
  204. - if (strcmp(dev->filename, dl_udev) == 0) {
  205. - result = dev;
  206. - break;
  207. - }
  208. - }
  209. - }
  210. -
  211. - if (result != NULL)
  212. - break;
  213. + found = dev;
  214. + break;
  215. }
  216. if (flg_show)
  217. return 0;
  218. - DBG(("device %p, found %p\n", result, found));
  219. + DBG(("found %p\n", found));
  220. - if (result == NULL && found != NULL)
  221. - result = found;
  222. -
  223. - if (result == NULL) {
  224. - fprintf(stderr, "failed to find device\n");
  225. - return 1;
  226. - }
  227. + if (found == NULL)
  228. + err("failed to find device\n");
  229. - printf("=> found device: bus %s, dev %s\n",
  230. - result->bus->dirname, result->filename);
  231. + printf("=> found device: bus %u, dev %u\n",
  232. + bus_num, dev_num);
  233. dl_data = load_file(dl_file, &dl_size, &fsize);
  234. - if (dl_data == NULL) {
  235. - printf("failed to load %s\n", dl_file);
  236. - return 1;
  237. - }
  238. + if (dl_data == NULL)
  239. + errp("failed to load %s", dl_file);
  240. printf("=> loaded %ld bytes from %s\n", fsize, dl_file);
  241. - devh = usb_open(result);
  242. - if (devh == NULL) {
  243. - perror("usb_open");
  244. - return 1;
  245. - }
  246. + ret = libusb_open(found, &devh);
  247. + if (ret == 0) {
  248. + /*
  249. + * Seems to break some recovery modes :(
  250. + * http://crosbug.com/26083
  251. + * These fail:
  252. + * smdk-dltool -a 0x02021400 -f bl1.bin
  253. + * smdk-dltool -a 0x02023400 -f bl2.bin
  254. + */
  255. +#if 0
  256. + uint8_t configuration;
  257. + struct libusb_config_descriptor *config;
  258. + libusb_get_active_config_descriptor(found, &config);
  259. + configuration = config->bConfigurationValue;
  260. + libusb_free_config_descriptor(config);
  261. + libusb_set_configuration(devh, configuration);
  262. +#endif
  263. + } else
  264. + errp("libusb_open");
  265. DBG(("claim interface\n"));
  266. - if (usb_claim_interface(devh, 0) < 0) {
  267. - perror("usb_claim_interface");
  268. - usb_close(devh);
  269. - return 1;
  270. - }
  271. + ret = libusb_claim_interface(devh, 0);
  272. + if (ret)
  273. + errp("libusb_claim_interface");
  274. printf("=> Downloading %ld bytes to 0x%08lx\n", dl_size, dl_addr);
  275. write_header(dl_data, dl_addr, dl_size);
  276. calc_cksum(dl_data, dl_size);
  277. - //ret = usb_bulk_write(devh, 3, (void *)dl_data, dl_size, 5*1000*1000);
  278. - ret = usb_bulk_write(devh, ep_out, (void *)dl_data, dl_size, 5*1000*1000);
  279. + //ret = libusb_bulk_transfer(devh, 3, dl_data, dl_size, &transferred, 5*1000*1000);
  280. + ret = libusb_bulk_transfer(devh, ep_out, dl_data, dl_size, &transferred, 5*1000*1000);
  281. printf("=> usb_bulk_write() returned %d\n", ret);
  282. - if (ret != dl_size) {
  283. - printf("failed to write %ld bytes\n", dl_size);
  284. + if (ret || transferred != dl_size) {
  285. + printf("failed to write %ld bytes (wrote %d): %s\n",
  286. + dl_size, transferred, strerror(errno));
  287. + ret = 1;
  288. }
  289. free(dl_data);
  290. - usb_release_interface(devh, 0);
  291. - usb_close(devh);
  292. + libusb_release_interface(devh, 0);
  293. + libusb_close(devh);
  294. + libusb_exit(ctx);
  295. - return 0;
  296. + return ret;
  297. }