From 6bac23b390afce5bf0a14772103f030c24137fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnim=20L=C3=A4uger?= Date: Sun, 25 Nov 2007 00:02:16 +0000 Subject: [PATCH] fix for '[ 1836616 ] Segfault/Memory corruption if USB device inaccessible' git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@784 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- jtag/src/tap/parport/ftd2xx.c | 63 ++++++++++++++++++++--------------- jtag/src/tap/parport/ftdi.c | 51 +++++++++++++++++----------- 2 files changed, 68 insertions(+), 46 deletions(-) diff --git a/jtag/src/tap/parport/ftd2xx.c b/jtag/src/tap/parport/ftd2xx.c index 59b4bba5..ae3fa7bd 100644 --- a/jtag/src/tap/parport/ftd2xx.c +++ b/jtag/src/tap/parport/ftd2xx.c @@ -238,6 +238,8 @@ ftd2xx_generic_open( parport_t *parport ) if ((status = FT_Open(0, &(p->fc))) != FT_OK) { fprintf( stderr, "Error: unable to open FTDI device: %li\n", status); + /* mark ftd2xx layer as not initialized */ + p->fc = NULL; return -1; } @@ -377,11 +379,13 @@ ftd2xx_close( parport_t *parport ) { ftd2xx_params_t *p = parport->params; - if(p->outcount > 0) ftd2xx_flush_output( p ); - p->outcount = 0; + if (p->fc) { + if(p->outcount > 0) ftd2xx_flush_output( p ); + p->outcount = 0; - FT_SetBitMode( p->fc, 0x00, 0x00 ); - FT_Close(p->fc); + FT_SetBitMode( p->fc, 0x00, 0x00 ); + FT_Close(p->fc); + } return 0; } @@ -390,23 +394,25 @@ static int ftd2xx_set_data( parport_t *parport, uint8_t data ) { ftd2xx_params_t *p = parport->params; - DWORD xferred; - FT_STATUS status; + DWORD xferred; + FT_STATUS status; - if(p->autoflush) - { - if ((status = FT_Write( p->fc, &data, 1 , &xferred)) != FT_OK) - fprintf( stderr, "Error: FT_Write() failed in %i\n", __LINE__ ); - if (xferred != 1) - printf("w\n"); - } - else - { - p->outbuf[p->outcount++] = data; + if (p->fc) { + if(p->autoflush) + { + if ((status = FT_Write( p->fc, &data, 1 , &xferred)) != FT_OK) + fprintf( stderr, "Error: FT_Write() failed in %i\n", __LINE__ ); + if (xferred != 1) + printf("w\n"); + } + else + { + p->outbuf[p->outcount++] = data; - if(p->outcount >= p->outbuf_len) - return ftd2xx_flush_output( p ); - }; + if(p->outcount >= p->outbuf_len) + return ftd2xx_flush_output( p ); + } + } return 0; } @@ -419,11 +425,14 @@ ftd2xx_get_data( parport_t *parport ) unsigned char d; ftd2xx_params_t *p = parport->params; - while (xferred == 0) { - if ((status = FT_Read( p->fc, &d, 1, &xferred )) != FT_OK) - printf( "Error: FT_Read() failed in %i\n", __LINE__ ); - } - return d; + if (p->fc) { + while (xferred == 0) { + if ((status = FT_Read( p->fc, &d, 1, &xferred )) != FT_OK) + printf( "Error: FT_Read() failed in %i\n", __LINE__ ); + } + return d; + } else + return 0; } static int @@ -437,8 +446,10 @@ ftd2xx_set_control( parport_t *parport, uint8_t data ) { ftd2xx_params_t *p = parport->params; - p->autoflush = data; - if(p->autoflush) ftd2xx_flush_output( p ); + if (p->fc) { + p->autoflush = data; + if(p->autoflush) ftd2xx_flush_output( p ); + } return 0; } diff --git a/jtag/src/tap/parport/ftdi.c b/jtag/src/tap/parport/ftdi.c index 58228bfb..36991e67 100644 --- a/jtag/src/tap/parport/ftdi.c +++ b/jtag/src/tap/parport/ftdi.c @@ -255,6 +255,8 @@ ftdi_generic_open( parport_t *parport ) fprintf (stderr, "Can't open ftdi device: %s\n", ftdi_get_error_string (fc)); ftdi_deinit(fc); + /* mark ftdi layer as not initialized */ + p->fc = NULL; return -1; }; @@ -426,12 +428,14 @@ ftdi_close( parport_t *parport ) { ftdi_params_t *p = parport->params; - if(p->outcount > 0) ftdi_flush_output( p ); - p->outcount = 0; + if (p->fc) { + if(p->outcount > 0) ftdi_flush_output( p ); + p->outcount = 0; - ftdi_disable_bitbang(p->fc); - ftdi_usb_close(p->fc); - ftdi_deinit(p->fc); + ftdi_disable_bitbang(p->fc); + ftdi_usb_close(p->fc); + ftdi_deinit(p->fc); + } return 0; } @@ -441,17 +445,19 @@ ftdi_set_data( parport_t *parport, uint8_t data ) { ftdi_params_t *p = parport->params; - if(p->autoflush) - { - if(ftdi_write_data(p->fc, &data, 1) != 1) printf("w\n"); - } - else - { - p->outbuf[p->outcount++] = data; + if (p->fc) { + if(p->autoflush) + { + if(ftdi_write_data(p->fc, &data, 1) != 1) printf("w\n"); + } + else + { + p->outbuf[p->outcount++] = data; - if(p->outcount >= p->outbuf_len) - return ftdi_flush_output( p ); - }; + if(p->outcount >= p->outbuf_len) + return ftdi_flush_output( p ); + } + } return 0; } @@ -462,8 +468,11 @@ ftdi_get_data( parport_t *parport ) unsigned char d; ftdi_params_t *p = parport->params; - while(ftdi_read_data( p->fc, &d, 1) == 0); - return d; + if (p->fc) { + while(ftdi_read_data( p->fc, &d, 1) == 0); + return d; + } else + return 0; } static int @@ -477,9 +486,11 @@ ftdi_set_control( parport_t *parport, uint8_t data ) { ftdi_params_t *p = parport->params; - p->autoflush = data; - if(p->autoflush) ftdi_flush_output( p ); - + if (p->fc) { + p->autoflush = data; + if(p->autoflush) ftdi_flush_output( p ); + } + return 0; }