Exemple #1
0
void my_prepare_ep2_in(void)
{
    last_send_was_null = (ep2_num_bytes_to_send < MY_EP2_BUFFER_SIZE);
    EP_IN_BD(2).Cnt = ep2_num_bytes_to_send;
    EP_IN_BD(2).ADR = (uchar __data *)&ep2_InBuffer;
    fill_in_buffer(2, &ep2_source_data, MY_EP2_BUFFER_SIZE, &ep2_num_bytes_to_send);
    if(EP_IN_BD(2).Stat.DTS == 0)
    {
        EP_IN_BD(2).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;
    }
    else
    {
        EP_IN_BD(2).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN;
    }
}
Exemple #2
0
void my_ep2_in(void)
{
    if(UEIEbits.BTOEE && UEIRbits.BTOEF)
    {
        // Error during last transfert. Redoing it.
        UEIRbits.BTOEF = 0;
        if(EP_IN_BD(2).Stat.DTS == 0)
        {
            EP_IN_BD(2).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN;
        }
        else
        {
            EP_IN_BD(2).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;
        }
    }
    else
    {
        if((ep2_num_bytes_to_send == 0) && last_send_was_null)
        {
            // There is nothing more to send, so keep
            // the EP2 buffer under the CPU responsability
            return;
        }
        last_send_was_null = (ep2_num_bytes_to_send < MY_EP2_BUFFER_SIZE);
        EP_IN_BD(2).Cnt = ep2_num_bytes_to_send;
        fill_in_buffer(2, &ep2_source_data, MY_EP2_BUFFER_SIZE, &ep2_num_bytes_to_send);
        if(EP_IN_BD(2).Stat.DTS == 0)
        {
            EP_IN_BD(2).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;
        }
        else
        {
            EP_IN_BD(2).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN;
        }
    }
}
Exemple #3
0
static int
gz_head(FILE_F state)
{
	/* get some data in the input buffer */
	if (state->avail_in == 0) {
		if (fill_in_buffer(state) == -1)
			return -1;
		if (state->avail_in == 0)
			return 0;
	}

	/* look for the gzip magic header bytes 31 and 139 */
#ifdef HAVE_LIBZ
	if (state->next_in[0] == 31) {
		state->avail_in--;
		state->next_in++;
		if (state->avail_in == 0 && fill_in_buffer(state) == -1)
			return -1;
		if (state->avail_in && state->next_in[0] == 139) {
			guint8 cm;
			guint8 flags;
			guint16 len;
			guint16 hcrc;

			/* we have a gzip header, woo hoo! */
			state->avail_in--;
			state->next_in++;

			/* read rest of header */

			/* compression method (CM) */
			if (gz_next1(state, &cm) == -1)
				return -1;
			if (cm != 8) {
				state->err = FTAP_ERR_DECOMPRESS;
				state->err_info = "unknown compression method";
				return -1;
			}

			/* flags (FLG) */
			if (gz_next1(state, &flags) == -1)
				return -1;
			if (flags & 0xe0) {     /* reserved flag bits */
				state->err = FTAP_ERR_DECOMPRESS;
				state->err_info = "reserved flag bits set";
				return -1;
			}

			/* modification time (MTIME) */
			if (gz_skipn(state, 4) == -1)
				return -1;

			/* extra flags (XFL) */
			if (gz_skipn(state, 1) == -1)
				return -1;

			/* operating system (OS) */
			if (gz_skipn(state, 1) == -1)
				return -1;

			if (flags & 4) {
				/* extra field - get XLEN */
				if (gz_next2(state, &len) == -1)
					return -1;

				/* skip the extra field */
				if (gz_skipn(state, len) == -1)
					return -1;
			}
			if (flags & 8) {
				/* file name */
				if (gz_skipzstr(state) == -1)
					return -1;
			}
			if (flags & 16) {
				/* comment */
				if (gz_skipzstr(state) == -1)
					return -1;
			}
			if (flags & 2) {
				/* header crc */
				if (gz_next2(state, &hcrc) == -1)
					return -1;
				/* XXX - check the CRC? */
			}

			/* set up for decompression */
			inflateReset(&(state->strm));
			state->strm.adler = crc32(0L, Z_NULL, 0);
			state->compression = ZLIB;
			state->is_compressed = TRUE;
#ifdef Z_BLOCK
			if (state->fast_seek) {
				struct zlib_cur_seek_point *cur = g_new(struct zlib_cur_seek_point,1);

				cur->pos = cur->have = 0;
				g_free(state->fast_seek_cur);
				state->fast_seek_cur = cur;
				fast_seek_header(state, state->raw_pos - state->avail_in, state->pos, GZIP_AFTER_HEADER);
			}
#endif
			return 0;
		}
Exemple #4
0
static void /* gz_decomp */
zlib_read(FILE_F state, unsigned char *buf, unsigned int count)
{
	int ret = 0;	/* XXX */
	guint32 crc, len;
	z_streamp strm = &(state->strm);

	unsigned char *buf2 = buf;
	unsigned int count2 = count;

	strm->avail_out = count;
	strm->next_out = buf;

	/* fill output buffer up to end of deflate stream or error */
	do {
		/* get more input for inflate() */
		if (state->avail_in == 0 && fill_in_buffer(state) == -1)
			break;
		if (state->avail_in == 0) {
			/* EOF */
			state->err = FTAP_ERR_SHORT_READ;
			state->err_info = NULL;
			break;
		}

		strm->avail_in = state->avail_in;
		strm->next_in = state->next_in;
		/* decompress and handle errors */
#ifdef Z_BLOCK
		ret = inflate(strm, Z_BLOCK);
#else
		ret = inflate(strm, Z_NO_FLUSH);
#endif
		state->avail_in = strm->avail_in;
		state->next_in = strm->next_in;
		if (ret == Z_STREAM_ERROR) {
			state->err = FTAP_ERR_DECOMPRESS;
			state->err_info = strm->msg;
			break;
		}
		if (ret == Z_NEED_DICT) {
			state->err = FTAP_ERR_DECOMPRESS;
			state->err_info = "preset dictionary needed";
			break;
		}
		if (ret == Z_MEM_ERROR) {
			/* This means "not enough memory". */
			state->err = ENOMEM;
			state->err_info = NULL;
			break;
		}
		if (ret == Z_DATA_ERROR) {              /* deflate stream invalid */
			state->err = FTAP_ERR_DECOMPRESS;
			state->err_info = strm->msg;
			break;
		}
		/*
		 * XXX - Z_BUF_ERROR?
		 */

		strm->adler = crc32(strm->adler, buf2, count2 - strm->avail_out);
#ifdef Z_BLOCK
		if (state->fast_seek_cur) {
			struct zlib_cur_seek_point *cur = (struct zlib_cur_seek_point *) state->fast_seek_cur;
			unsigned int ready = count2 - strm->avail_out;

			if (ready < ZLIB_WINSIZE) {
				guint left = ZLIB_WINSIZE - cur->pos;

				if (ready >= left) {
					memcpy(cur->window + cur->pos, buf2, left);
					if (ready != left)
						memcpy(cur->window, buf2 + left, ready - left);

					cur->pos = ready - left;
					cur->have += ready;
				} else {
					memcpy(cur->window + cur->pos, buf2, ready);
					cur->pos += ready;
					cur->have += ready;
				}

				if (cur->have >= ZLIB_WINSIZE)
					cur->have = ZLIB_WINSIZE;

			} else {
				memcpy(cur->window, buf2 + (ready - ZLIB_WINSIZE), ZLIB_WINSIZE);
				cur->pos = 0;
				cur->have = ZLIB_WINSIZE;
			}

			if (cur->have >= ZLIB_WINSIZE && ret != Z_STREAM_END && (strm->data_type & 128) && !(strm->data_type & 64))
				zlib_fast_seek_add(state, cur, (strm->data_type & 7), state->raw_pos - strm->avail_in, state->pos + (count - strm->avail_out));
		}
#endif
		buf2 = (buf2 + count2 - strm->avail_out);
		count2 = strm->avail_out;

	} while (strm->avail_out && ret != Z_STREAM_END);

	/* update available output and crc check value */
	state->next = buf;
	state->have = count - strm->avail_out;

	/* Check gzip trailer if at end of deflate stream.
	   We don't fail immediately here, we just set an error
	   indication, so that we try to process what data we
	   got before the error.  The next attempt to read
	   something past that data will get the error. */
	if (ret == Z_STREAM_END) {
		if (gz_next4(state, &crc) != -1 &&
		    gz_next4(state, &len) != -1) {
			if (crc != strm->adler && !state->dont_check_crc) {
				state->err = FTAP_ERR_DECOMPRESS;
				state->err_info = "bad CRC";
			} else if (len != (strm->total_out & 0xffffffffL)) {
				state->err = FTAP_ERR_DECOMPRESS;
				state->err_info = "length field wrong";
			}
		}
		state->compression = UNKNOWN;      /* ready for next stream, once have is 0 */
		g_free(state->fast_seek_cur);
		state->fast_seek_cur = NULL;
	}
}
void ep0_setup(void) {
	debug_usb("ep0_setup\n");

	ep0_state = WAIT_SETUP;
	num_bytes_to_be_send = 0;

	if (ep0_usb_std_request()) {
		UCONbits.PKTDIS = 0;
		if (SetupBuffer.data_transfer_direction == DEVICE_TO_HOST) {
			ep0_state = WAIT_IN;

			EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE;
			EP_OUT_BD(0).ADR = (u8 __data *)&SetupBuffer;
			EP_OUT_BD(0).Stat.uc = BDS_USIE;

			EP_IN_BD(0).ADR = (u8 __data *)InBuffer;
			if (SetupBuffer.wLength < num_bytes_to_be_send) {
				num_bytes_to_be_send = SetupBuffer.wLength;
			} debug2_usb("bytes to send: %d\r\n", num_bytes_to_be_send);
			fill_in_buffer(0, &sourceData, EP0_BUFFER_SIZE,
					&num_bytes_to_be_send);
			EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;

		} else // HOST_TO_DEVICE
		{
			ep0_state = WAIT_OUT;

			EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE;
			EP_OUT_BD(0).ADR = (u8 __data *)InBuffer;
			EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;

			EP_IN_BD(0).Cnt = 0;
			EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;
		}
	} else if (ep0_dfu_request()) {
		UCONbits.PKTDIS = 0;
		if (SetupBuffer.data_transfer_direction == DEVICE_TO_HOST) {
			ep0_state = WAIT_DFU_IN;

			EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE;
			EP_OUT_BD(0).ADR = (u8 __data *)&SetupBuffer;
			EP_OUT_BD(0).Stat.uc = BDS_USIE;

			EP_IN_BD(0).ADR = (u8 __data *)InBuffer;
			if (SetupBuffer.wLength < num_bytes_to_be_send) {
				num_bytes_to_be_send = SetupBuffer.wLength;
			}
			debug2_usb("bytes to send: %d\n", num_bytes_to_be_send);
			// debug2("2: %x\n", sourceData[0]);
			fill_in_buffer(0, &sourceData, EP0_BUFFER_SIZE,
					&num_bytes_to_be_send);
			EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;

		} else // HOST_TO_DEVICE
		{
			ep0_state = WAIT_DFU_OUT;

			EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE;
			EP_OUT_BD(0).ADR = (u8 __data *)InBuffer;
			EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;

			EP_IN_BD(0).Cnt = 0;
			EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;

		}
	} else {
		debug_usb("unknown request\n");
		UCONbits.PKTDIS = 0;
		EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE;
		EP_OUT_BD(0).ADR = (u8 __data *)&SetupBuffer;
		EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_BSTALL;

		EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_BSTALL;
	}
}
void ep0_in(void) {
	debug2_usb("ep0_in %d\n", (u16) num_bytes_to_be_send);
	if (GET_DEVICE_STATE() == ADDRESS_PENDING_STATE) {
		UADDR = SetupBuffer.bAddress;
		if (UADDR != 0) {
			SET_DEVICE_STATE(ADDRESS_STATE);
		} else {
			SET_DEVICE_STATE(DEFAULT_STATE);
		}
	}

	if (ep0_state == WAIT_IN) {
		fill_in_buffer(0, &sourceData, EP0_BUFFER_SIZE, &num_bytes_to_be_send);

		if (EP_IN_BD(0).Stat.DTS == 0) {
			EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;
		} else {
			EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN;
		}
	} else if (ep0_state == WAIT_DFU_IN) {
		fill_in_buffer(0, &sourceData, EP0_BUFFER_SIZE, &num_bytes_to_be_send);


		if (EP_IN_BD(0).Stat.DTS == 0) {
			EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT1 | BDS_DTSEN;
		} else {
			EP_IN_BD(0).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN;
		}
	} else {
		ep0_state = WAIT_SETUP;
		EP_OUT_BD(0).Cnt = EP0_BUFFER_SIZE;
		EP_OUT_BD(0).ADR = (u8 __data *)&SetupBuffer;
		EP_OUT_BD(0).Stat.uc = BDS_USIE | BDS_DAT0 | BDS_DTSEN;
		EP_IN_BD(0).Stat.uc = BDS_UCPU;
		UEP0 = EPINEN_EN | EPOUTEN_EN | EPHSHK_EN;
	}

	if (GET_DEVICE_STATE() == CONFIGURATION_PENDING_STATE) {

		// First, disable all endpoints.
		// UEP0 is never disabled
		UEP1 = 0;
		UEP2 = 0;
		UEP3 = 0;
		UEP4 = 0;
		UEP5 = 0;
		UEP6 = 0;
		UEP7 = 0;
		UEP8 = 0;
		UEP9 = 0;
		UEP10 = 0;
		UEP11 = 0;
		UEP12 = 0;
		UEP13 = 0;
		UEP14 = 0;
		UEP15 = 0;

		// switch the functions vectors
		/*
		 if(coming_cfg <= FLASH_CONFIGURATION)
		 {
		 // switch back to the bootloader vectors
		 ep_init  = boot_ep_init;
		 ep_in    = boot_ep_in;
		 ep_out   = boot_ep_out;
		 ep_setup = boot_ep_setup;
		 }
		 else
		 {
		 // switch to the application vectors
		 ep_init  = application_data.ep_init;
		 ep_in    = application_data.ep_in;
		 ep_out   = application_data.ep_out;
		 ep_setup = application_data.ep_setup;
		 }
		 */
		ep_init = boot_ep_init;
		ep_in = boot_ep_in;
		ep_out = boot_ep_out;
		ep_setup = boot_ep_setup;

		SET_ACTIVE_CONFIGURATION(coming_cfg);

		if (coming_cfg == 0) {
			SET_DEVICE_STATE(ADDRESS_STATE);
		} else {
			static u8 i;

			// Switch to decrement loop because of a sdcc bug
			for (i = 15; i > 0; i--)
//            for(i = 1; i < 16; i++)
					{
				ep_init[coming_cfg][i]();
			}

			SET_DEVICE_STATE(CONFIGURED_STATE);
		}
	}
}