From mboxrd@z Thu Jan 1 00:00:00 1970 Return-path: Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1aP6XX-0000CB-WF for barebox@lists.infradead.org; Fri, 29 Jan 2016 10:44:39 +0000 From: Sascha Hauer Date: Fri, 29 Jan 2016 11:43:55 +0100 Message-Id: <1454064243-26558-16-git-send-email-s.hauer@pengutronix.de> In-Reply-To: <1454064243-26558-1-git-send-email-s.hauer@pengutronix.de> References: <1454064243-26558-1-git-send-email-s.hauer@pengutronix.de> List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "barebox" Errors-To: barebox-bounces+u.kleine-koenig=pengutronix.de@lists.infradead.org Subject: [PATCH 15/23] scripts: imx: make libusb variables global To: Barebox List No need to pass the libusb context through from function to function. Make them globally visible. Signed-off-by: Sascha Hauer --- scripts/imx/imx-usb-loader.c | 173 ++++++++++++++++++++----------------------- 1 file changed, 81 insertions(+), 92 deletions(-) diff --git a/scripts/imx/imx-usb-loader.c b/scripts/imx/imx-usb-loader.c index 5a84721..7925d2d 100644 --- a/scripts/imx/imx-usb-loader.c +++ b/scripts/imx/imx-usb-loader.c @@ -42,6 +42,8 @@ #define FT_LOAD_ONLY 0x00 int verbose; +static struct libusb_device_handle *usb_dev_handle; +static struct usb_id *usb_id; struct mach_id { struct mach_id * next; @@ -292,12 +294,11 @@ static long get_file_size(FILE *xfile) * EP2IN - bulk in * (max packet size of 512 bytes) */ -static int transfer(struct libusb_device_handle *h, int report, unsigned char *p, unsigned cnt, - int* last_trans, struct usb_id *p_id) +static int transfer(int report, unsigned char *p, unsigned cnt, int *last_trans) { int err; - if (cnt > p_id->mach_id->max_transfer) - cnt = p_id->mach_id->max_transfer; + if (cnt > usb_id->mach_id->max_transfer) + cnt = usb_id->mach_id->max_transfer; if (verbose > 4) { printf("report=%i\n", report); @@ -305,9 +306,10 @@ static int transfer(struct libusb_device_handle *h, int report, unsigned char *p dump_bytes(p, cnt, 0); } - if (p_id->mach_id->mode == MODE_BULK) { + if (usb_id->mach_id->mode == MODE_BULK) { *last_trans = 0; - err = libusb_bulk_transfer(h, (report < 3) ? 1 : 2 + EP_IN, p, cnt, last_trans, 1000); + err = libusb_bulk_transfer(usb_dev_handle, + (report < 3) ? 1 : 2 + EP_IN, p, cnt, last_trans, 1000); } else { unsigned char tmp[1028]; @@ -315,7 +317,7 @@ static int transfer(struct libusb_device_handle *h, int report, unsigned char *p if (report < 3) { memcpy(&tmp[1], p, cnt); - err = libusb_control_transfer(h, + err = libusb_control_transfer(usb_dev_handle, CTRL_OUT, HID_SET_REPORT, (HID_REPORT_TYPE_OUTPUT << 8) | report, @@ -327,7 +329,8 @@ static int transfer(struct libusb_device_handle *h, int report, unsigned char *p } else { *last_trans = 0; memset(&tmp[1], 0, cnt); - err = libusb_interrupt_transfer(h, 1 + EP_IN, tmp, cnt + 1, last_trans, 1000); + err = libusb_interrupt_transfer(usb_dev_handle, + 1 + EP_IN, tmp, cnt + 1, last_trans, 1000); if (err >= 0) { if (tmp[0] == (unsigned char)report) { if (*last_trans > 1) { @@ -349,7 +352,7 @@ static int transfer(struct libusb_device_handle *h, int report, unsigned char *p return err; } -int do_status(libusb_device_handle *h, struct usb_id *p_id) +int do_status(void) { int last_trans; unsigned char tmp[64]; @@ -364,14 +367,14 @@ int do_status(libusb_device_handle *h, struct usb_id *p_id) }; for (;;) { - err = transfer(h, 1, (unsigned char*)status_command, 16, &last_trans, p_id); + err = transfer(1, (unsigned char*)status_command, 16, &last_trans); if (verbose > 2) printf("report 1, wrote %i bytes, err=%i\n", last_trans, err); memset(tmp, 0, sizeof(tmp)); - err = transfer(h, 3, tmp, 64, &last_trans, p_id); + err = transfer(3, tmp, 64, &last_trans); if (verbose > 2) { printf("report 3, read %i bytes, err=%i\n", last_trans, err); @@ -387,8 +390,8 @@ int do_status(libusb_device_handle *h, struct usb_id *p_id) break; } - if (p_id->mach_id->mode == MODE_HID) { - err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id); + if (usb_id->mach_id->mode == MODE_HID) { + err = transfer(4, tmp, sizeof(tmp), &last_trans); if (err) printf("4 in err=%i, last_trans=%i %02x %02x %02x %02x\n", err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); @@ -399,8 +402,7 @@ int do_status(libusb_device_handle *h, struct usb_id *p_id) #define V(a) (((a) >> 24) & 0xff), (((a) >> 16) & 0xff), (((a) >> 8) & 0xff), ((a) & 0xff) -static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id, - unsigned addr, unsigned char *dest, unsigned cnt) +static int read_memory(unsigned addr, unsigned char *dest, unsigned cnt) { static unsigned char read_reg_command[] = { 1, @@ -428,7 +430,7 @@ static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id, read_reg_command[10] = (unsigned char)(cnt); for (;;) { - err = transfer(h, 1, read_reg_command, 16, &last_trans, p_id); + err = transfer(1, read_reg_command, 16, &last_trans); if (!err) break; printf("read_reg_command err=%i, last_trans=%i\n", err, last_trans); @@ -438,7 +440,7 @@ static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id, retry++; } - err = transfer(h, 3, tmp, 4, &last_trans, p_id); + err = transfer(3, tmp, 4, &last_trans); if (err) { printf("r3 in err=%i, last_trans=%i %02x %02x %02x %02x\n", err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); @@ -449,7 +451,7 @@ static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id, while (rem) { tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; - err = transfer(h, 4, tmp, 64, &last_trans, p_id); + err = transfer(4, tmp, 64, &last_trans); if (err) { printf("r4 in err=%i, last_trans=%i %02x %02x %02x %02x cnt=%u rem=%d\n", err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3], cnt, rem); @@ -473,8 +475,7 @@ static int read_memory(struct libusb_device_handle *h, struct usb_id *p_id, return err; } -static int write_memory(struct libusb_device_handle *h, struct usb_id *p_id, - unsigned addr, unsigned val, int width) +static int write_memory(unsigned addr, unsigned val, int width) { int retry = 0; int last_trans; @@ -517,7 +518,7 @@ static int write_memory(struct libusb_device_handle *h, struct usb_id *p_id, write_reg_command[14] = (unsigned char)(val); for (;;) { - err = transfer(h, 1, write_reg_command, 16, &last_trans, p_id); + err = transfer(1, write_reg_command, 16, &last_trans); if (!err) break; printf("write_reg_command err=%i, last_trans=%i\n", err, last_trans); @@ -529,7 +530,7 @@ static int write_memory(struct libusb_device_handle *h, struct usb_id *p_id, memset(tmp, 0, sizeof(tmp)); - err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id); + err = transfer(3, tmp, sizeof(tmp), &last_trans); if (err) { printf("w3 in err=%i, last_trans=%i %02x %02x %02x %02x\n", err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); @@ -538,15 +539,14 @@ static int write_memory(struct libusb_device_handle *h, struct usb_id *p_id, memset(tmp, 0, sizeof(tmp)); - err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id); + err = transfer(4, tmp, sizeof(tmp), &last_trans); if (err) printf("w4 in err=%i, last_trans=%i %02x %02x %02x %02x\n", err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); return err; } -static int load_file(struct libusb_device_handle *h, struct usb_id *p_id, - void *buf, unsigned len, unsigned dladdr, unsigned char type) +static int load_file(void *buf, unsigned len, unsigned dladdr, unsigned char type) { static unsigned char dl_command[] = { 0x04, @@ -576,7 +576,7 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id, dl_command[15] = type; for (;;) { - err = transfer(h, 1, dl_command, 16, &last_trans, p_id); + err = transfer(1, dl_command, 16, &last_trans); if (!err) break; @@ -589,8 +589,8 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id, retry = 0; - if (p_id->mach_id->mode == MODE_BULK) { - err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id); + if (usb_id->mach_id->mode == MODE_BULK) { + err = transfer(3, tmp, sizeof(tmp), &last_trans); if (err) printf("in err=%i, last_trans=%i %02x %02x %02x %02x\n", err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); @@ -600,12 +600,12 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id, cnt = len; while (1) { - int now = get_min(cnt, p_id->mach_id->max_transfer); + int now = get_min(cnt, usb_id->mach_id->max_transfer); if (!now) break; - err = transfer(h, 2, p, now, &now, p_id); + err = transfer(2, p, now, &now); if (err) { printf("dl_command err=%i, last_trans=%i\n", err, last_trans); return err; @@ -615,24 +615,23 @@ static int load_file(struct libusb_device_handle *h, struct usb_id *p_id, cnt -= now; } - if (p_id->mach_id->mode == MODE_HID) { - err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id); + if (usb_id->mach_id->mode == MODE_HID) { + err = transfer(3, tmp, sizeof(tmp), &last_trans); if (err) printf("3 in err=%i, last_trans=%i %02x %02x %02x %02x\n", err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); - err = transfer(h, 4, tmp, sizeof(tmp), &last_trans, p_id); + err = transfer(4, tmp, sizeof(tmp), &last_trans); if (err) printf("4 in err=%i, last_trans=%i %02x %02x %02x %02x\n", err, last_trans, tmp[0], tmp[1], tmp[2], tmp[3]); } else { - do_status(h, p_id); + do_status(); } return transfer_size; } -static int write_dcd_table_ivt(struct libusb_device_handle *h, struct usb_id *p_id, - struct imx_flash_header_v2 *hdr, unsigned char *file_start, unsigned cnt) +static int write_dcd_table_ivt(struct imx_flash_header_v2 *hdr, unsigned char *file_start, unsigned cnt) { unsigned char *dcd_end; unsigned m_length; @@ -694,7 +693,7 @@ static int write_dcd_table_ivt(struct libusb_device_handle *h, struct usb_id *p_ unsigned val = (dcd[4] << 24) | (dcd[5] << 16) | (dcd[6] << 8) | dcd[7]; dcd += 8; - err = write_memory(h, p_id, addr, val, 4); + err = write_memory(addr, val, 4); if (err < 0) return err; } @@ -749,8 +748,7 @@ static int get_dcd_range_old(struct imx_flash_header *hdr, return 0; } -static int write_dcd_table_old(struct libusb_device_handle *h, struct usb_id *p_id, - struct imx_flash_header *hdr, unsigned char *file_start, unsigned cnt) +static int write_dcd_table_old(struct imx_flash_header *hdr, unsigned char *file_start, unsigned cnt) { unsigned val; unsigned char *dcd_end; @@ -771,14 +769,14 @@ static int write_dcd_table_old(struct libusb_device_handle *h, struct usb_id *p_ case 1: if (verbose > 1) printf("type=%08x *0x%08x = 0x%08x\n", type, addr, val); - err = write_memory(h, p_id, addr, val, 1); + err = write_memory(addr, val, 1); if (err < 0) return err; break; case 4: if (verbose > 1) printf("type=%08x *0x%08x = 0x%08x\n", type, addr, val); - err = write_memory(h, p_id, addr, val, 4); + err = write_memory(addr, val, 4); if (err < 0) return err; break; @@ -795,8 +793,7 @@ static int write_dcd_table_old(struct libusb_device_handle *h, struct usb_id *p_ return err; } -static int verify_memory(struct libusb_device_handle *h, struct usb_id *p_id, - const void *buf, unsigned len, unsigned addr) +static int verify_memory(const void *buf, unsigned len, unsigned addr) { int ret, mismatch = 0; void *readbuf; @@ -806,7 +803,7 @@ static int verify_memory(struct libusb_device_handle *h, struct usb_id *p_id, if (!readbuf) return -ENOMEM; - ret = read_memory(h, p_id, addr, readbuf, len); + ret = read_memory(addr, readbuf, len); if (ret < 0) goto err; @@ -834,12 +831,12 @@ err: return ret; } -static int is_header(struct usb_id *p_id, unsigned char *p) +static int is_header(unsigned char *p) { struct imx_flash_header *ohdr = (struct imx_flash_header *)p; struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p; - switch (p_id->mach_id->header_type) { + switch (usb_id->mach_id->header_type) { case HDR_MX51: if (ohdr->app_code_barker == 0xb1) return 1; @@ -852,21 +849,19 @@ static int is_header(struct usb_id *p_id, unsigned char *p) return 0; } -static int perform_dcd(struct libusb_device_handle *h, struct usb_id *p_id, unsigned char *p, - unsigned char *file_start, unsigned cnt) +static int perform_dcd(unsigned char *p, unsigned char *file_start, unsigned cnt) { struct imx_flash_header *ohdr = (struct imx_flash_header *)p; struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p; int ret = 0; - switch (p_id->mach_id->header_type) { + switch (usb_id->mach_id->header_type) { case HDR_MX51: - ret = write_dcd_table_old(h, p_id, ohdr, file_start, cnt); - ohdr->dcd = 0; + ret = write_dcd_table_old(ohdr, file_start, cnt); break; case HDR_MX53: - ret = write_dcd_table_ivt(h, p_id, hdr, file_start, cnt); + ret = write_dcd_table_ivt(hdr, file_start, cnt); hdr->dcd_ptr = 0; break; @@ -875,13 +870,12 @@ static int perform_dcd(struct libusb_device_handle *h, struct usb_id *p_id, unsi return ret; } -static int clear_dcd_ptr(struct libusb_device_handle *h, struct usb_id *p_id, - unsigned char *p, unsigned char *file_start, unsigned cnt) +static int clear_dcd_ptr(unsigned char *p, unsigned char *file_start, unsigned cnt) { struct imx_flash_header *ohdr = (struct imx_flash_header *)p; struct imx_flash_header_v2 *hdr = (struct imx_flash_header_v2 *)p; - switch (p_id->mach_id->header_type) { + switch (usb_id->mach_id->header_type) { case HDR_MX51: printf("clear dcd_ptr=0x%08x\n", ohdr->dcd); ohdr->dcd = 0; @@ -894,12 +888,12 @@ static int clear_dcd_ptr(struct libusb_device_handle *h, struct usb_id *p_id, return 0; } -static int get_dl_start(struct usb_id *p_id, unsigned char *p, unsigned char *file_start, +static int get_dl_start(unsigned char *p, unsigned char *file_start, unsigned cnt, unsigned *dladdr, unsigned *max_length, unsigned *plugin, unsigned *header_addr) { unsigned char* file_end = file_start + cnt; - switch (p_id->mach_id->header_type) { + switch (usb_id->mach_id->header_type) { case HDR_MX51: { struct imx_flash_header *ohdr = (struct imx_flash_header *)p; @@ -941,8 +935,7 @@ static int get_dl_start(struct usb_id *p_id, unsigned char *p, unsigned char *fi return 0; } -static int process_header(struct libusb_device_handle *h, struct usb_id *p_id, - struct usb_work *curr, unsigned char *buf, int cnt, +static int process_header(struct usb_work *curr, unsigned char *buf, int cnt, unsigned *p_dladdr, unsigned *p_max_length, unsigned *p_plugin, unsigned *p_header_addr) { @@ -955,17 +948,17 @@ static int process_header(struct libusb_device_handle *h, struct usb_id *p_id, for (header_offset = 0; header_offset < header_max; header_offset += header_inc, p += header_inc) { - if (!is_header(p_id, p)) + if (!is_header(p)) continue; - ret = get_dl_start(p_id, p, buf, cnt, p_dladdr, p_max_length, p_plugin, p_header_addr); + ret = get_dl_start(p, buf, cnt, p_dladdr, p_max_length, p_plugin, p_header_addr); if (ret < 0) { printf("!!get_dl_start returned %i\n", ret); return ret; } if (curr->dcd) { - ret = perform_dcd(h, p_id, p, buf, cnt); + ret = perform_dcd(p, buf, cnt); if (ret < 0) { printf("!!perform_dcd returned %i\n", ret); return ret; @@ -978,7 +971,7 @@ static int process_header(struct libusb_device_handle *h, struct usb_id *p_id, } if (curr->clear_dcd) { - ret = clear_dcd_ptr(h, p_id, p, buf, cnt); + ret = clear_dcd_ptr(p, buf, cnt); if (ret < 0) { printf("!!clear_dcd returned %i\n", ret); return ret; @@ -1004,8 +997,7 @@ static int process_header(struct libusb_device_handle *h, struct usb_id *p_id, return -ENODEV; } -static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id, - struct usb_work *curr, int verify) +static int do_irom_download(struct usb_work *curr, int verify) { static unsigned char jump_command[] = {0x0b,0x0b, V(0), 0x00, V(0x00000000), V(0), 0x00}; @@ -1058,7 +1050,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id, max_length = fsize; - ret = process_header(h, p_id, curr, buf, cnt, + ret = process_header(curr, buf, cnt, &dladdr, &max_length, &plugin, &header_addr); if (ret < 0) goto cleanup; @@ -1087,7 +1079,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id, type = (curr->plug || curr->jump_mode) ? FT_APP : FT_LOAD_ONLY; - if (p_id->mach_id->mode == MODE_BULK && type == FT_APP) { + if (usb_id->mach_id->mode == MODE_BULK && type == FT_APP) { /* No jump command, dladdr should point to header */ dladdr = header_addr; } @@ -1119,7 +1111,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id, memcpy(verify_buffer, p, 64); - if ((type == FT_APP) && (p_id->mach_id->mode != MODE_HID)) { + if ((type == FT_APP) && (usb_id->mach_id->mode != MODE_HID)) { type = FT_LOAD_ONLY; verify = 2; } @@ -1128,7 +1120,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id, printf("loading binary file(%s) to %08x, skip=0x%x, fsize=%u type=%d...\n", curr->filename, dladdr, skip, fsize, type); - ret = load_file(h, p_id, image, fsize, dladdr, type); + ret = load_file(image, fsize, dladdr, type); if (ret < 0) goto cleanup; @@ -1137,7 +1129,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id, if (verify) { printf("verifying file...\n"); - ret = verify_memory(h, p_id, image, fsize, dladdr); + ret = verify_memory(image, fsize, dladdr); if (ret < 0) { printf("verifying failed\n"); goto cleanup; @@ -1151,15 +1143,14 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id, * so we load part of the image again with type FT_APP * this time. */ - ret = load_file(h, p_id, verify_buffer, 64, - dladdr, FT_APP); + ret = load_file(verify_buffer, 64, dladdr, FT_APP); if (ret < 0) goto cleanup; } } - if (p_id->mach_id->mode == MODE_HID && type == FT_APP) { + if (usb_id->mach_id->mode == MODE_HID && type == FT_APP) { printf("jumping to 0x%08x\n", header_addr); jump_command[2] = (unsigned char)(header_addr >> 24); @@ -1171,7 +1162,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id, retry = 0; for (;;) { - err = transfer(h, 1, jump_command, 16, &last_trans, p_id); + err = transfer(1, jump_command, 16, &last_trans); if (!err) break; @@ -1184,7 +1175,7 @@ static int do_irom_download(struct libusb_device_handle *h, struct usb_id *p_id, } memset(tmp, 0, sizeof(tmp)); - err = transfer(h, 3, tmp, sizeof(tmp), &last_trans, p_id); + err = transfer(3, tmp, sizeof(tmp), &last_trans); if (err) printf("j3 in err=%i, last_trans=%i %02x %02x %02x %02x\n", @@ -1211,7 +1202,6 @@ static void usage(const char *prgname) int main(int argc, char *argv[]) { - struct usb_id *p_id = NULL; struct mach_id *mach; libusb_device **devs; libusb_device *dev; @@ -1219,7 +1209,6 @@ int main(int argc, char *argv[]) int err; int ret = 1; ssize_t cnt; - libusb_device_handle *h = NULL; int config = 0; int verify = 0; struct usb_work w = {}; @@ -1267,7 +1256,7 @@ int main(int argc, char *argv[]) goto out; } - err = libusb_open(dev, &h); + err = libusb_open(dev, &usb_dev_handle); if (err) { fprintf(stderr, "Could not open device vid=0x%x pid=0x%x err=%d\n", mach->vid, mach->pid, err); @@ -1276,44 +1265,44 @@ int main(int argc, char *argv[]) libusb_free_device_list(devs, 1); - libusb_get_configuration(h, &config); + libusb_get_configuration(usb_dev_handle, &config); - if (libusb_kernel_driver_active(h, 0)) - libusb_detach_kernel_driver(h, 0); + if (libusb_kernel_driver_active(usb_dev_handle, 0)) + libusb_detach_kernel_driver(usb_dev_handle, 0); - err = libusb_claim_interface(h, 0); + err = libusb_claim_interface(usb_dev_handle, 0); if (err) { printf("Claim failed\n"); goto out; } - p_id = malloc(sizeof(*p_id)); - if (!p_id) { + usb_id = malloc(sizeof(*usb_id)); + if (!usb_id) { perror("malloc"); exit(1); } - p_id->mach_id = mach; + usb_id->mach_id = mach; - err = do_status(h, p_id); + err = do_status(); if (err) { printf("status failed\n"); goto out; } - err = do_irom_download(h, p_id, &w, verify); + err = do_irom_download(&w, verify); if (err) { - err = do_status(h, p_id); + err = do_status(); goto out; } ret = 0; out: - if (p_id) - free(p_id); + if (usb_id) + free(usb_id); - if (h) - libusb_close(h); + if (usb_dev_handle) + libusb_close(usb_dev_handle); libusb_exit(NULL); -- 2.7.0.rc3 _______________________________________________ barebox mailing list barebox@lists.infradead.org http://lists.infradead.org/mailman/listinfo/barebox