/* 
 * Wait on a semaphore for at most timeout millisecs
 * Return -1 if timed out otherwise time spent waiting.
 */ 
u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout)
{
	cyg_bool_t r;
	cyg_tick_count_t end_time = 0, start_time = 0;

	if (timeout) {
		start_time = cyg_current_time();
		r = cyg_semaphore_timed_wait(sem, start_time + msec_to_tick(timeout));
		end_time = cyg_current_time();

		if (r == false) {
			return SYS_ARCH_TIMEOUT;
		}	
	} else {
		cyg_semaphore_wait(sem);
	}

	return tick_to_msec(end_time - start_time);	
}
void dhcp_init(cyg_addrword_t arg)
{
	uint no_timeout = 1;
	int need_LinkLocal = 1;
	
	while( Network_TCPIP_ON == 0 )
		ppause(100);
	
//ZOTIPS	dhcp_start(WLanface);
	dhcp_start(Lanface);	//ZOTIPS
	ppause(2000);
	
	if ( !strcmp(EEPROM_Data.WLESSID, "") || !strcmp(EEPROM_Data.WLESSID, "< ANY >")) 
	    dhcp_serch(45);		// original:25.	Jesse modified this at build0006 of 716U2W on April 28, 2011.
	else
	    dhcp_serch(45);		// original:13.	Jesse modified this at build0006 of 716U2W on April 28, 2011.
	
	while(1)
	{
		cyg_semaphore_init( &dhcp_sem, 0);
		no_timeout = 1;
			
#ifdef LINKLOCAL_IP
	// Ron Add 11/28/04 
		
		if ( (mib_DHCP_p->IPAddr == 0x0) && need_LinkLocal )
//ZOTIPS			Give_ip_by_myself(WLanface);
			Give_ip_by_myself(Lanface);	//ZOTIPS
		else
		{
			if(EEPROM_Data.RENVEnable == 1)
			{
				if( rendezvous_TaskHdl == 0)
					cyg_semaphore_post( &rendezvous_sem);
				else
					Need_Rendezous_Reload = 1;	
			}
		}
		
#else
		if( mib_DHCP_p->IPAddr == 0x0 )
			set_factory_ip();
#endif	
	
		if( (mib_DHCP_p->IPAddr == 0x0 ) || ((NGET32(EEPROM_Data.BoxIPAddress) & 0x0000FFFF)==0x0000FEA9) )
			no_timeout = cyg_semaphore_timed_wait( &dhcp_sem, cyg_current_time() + 90000);
		else
			cyg_semaphore_wait( &dhcp_sem);
			
		
		if( no_timeout == 0 )
			need_LinkLocal = 0;
		else
			need_LinkLocal = 1;
		
		erase_netif_ipaddr();
		delete_dhcp_time();
		ppause(500);	
		dhcp_serch(10);		// original:3.	Jesse modified this at build0006 of 716U2W on April 28, 2011.
				
	}
	
}
void LinkLocal_ip_Task(cyg_addrword_t arg)
{
	int prob_conut, retry_count;
	uint32 LinkLocalIP;
	int get_conflict; //get arp reply to conflict our IP
		
	LinkLocal_set_state( NO_USE );
	
	while(1){
		cyg_semaphore_init(&linklocal_sem, 0);
		cyg_semaphore_wait(&linklocal_sem);	
		
		init_LinkLocal();
		
		if(rendezvous_TaskHdl != 0)
		{ 
			cyg_thread_suspend(rendezvous_TaskHdl);
		}
		
		prob_conut = 0;
		retry_count = 0;
		
		while(1){
			switch( LinkLocal_get_current_state() ){
				case PROBE:
//ZOTIPS					LinkLocalIP = SetLinkLocalIP(WLanface, 0 );
					LinkLocalIP = SetLinkLocalIP(Lanface, 0 );	//ZOTIPS
					break;	
				case RETRY:
//ZOTIPS					LinkLocalIP = SetLinkLocalIP(WLanface, 1 );
					LinkLocalIP = SetLinkLocalIP(Lanface, 1 );	//ZOTIPS
					break;
				case ANNOUNCE:
//ZOTIPS					LinkLocalIP = SetLinkLocalIP(WLanface, 0 );
//ZOTIPS					set_netif_ip(WLanface);
					LinkLocalIP = SetLinkLocalIP(Lanface, 0 );	//ZOTIPS
					set_netif_ip(Lanface);	//ZOTIPS
					break;
			}
			
//ZOTIPS			LinkLocal_IP_Query(LinkLocalIP, WLanface);
			LinkLocal_IP_Query(LinkLocalIP, Lanface);	//ZOTIPS
			get_conflict = cyg_semaphore_timed_wait(&linklocal_conflict, cyg_current_time() +(900/MSPTICK));			
			
			switch( LinkLocal_get_current_state() ){
				case PROBE:
						if( get_conflict )
						{
							LinkLocal_set_state( RETRY );
							ppause(1000);
						}
						else	
							prob_conut++;
						if( prob_conut == 3)
						{
							ppause(900);
							LinkLocal_set_state( ANNOUNCE );
						}
					break;	
				case RETRY:
						if( get_conflict )
						{	
							retry_count++;
						}
						else{
							LinkLocal_set_state( PROBE );
							prob_conut = 1;
						}
						ppause(1000);
						if( retry_count == 15)
						{
							ppause(900);
							LinkLocal_set_state( ANNOUNCE );		
						}
					break;
				case ANNOUNCE:
						if(get_conflict)
							ppause(1000);
//ZOTIPS						LinkLocal_IP_Query(LinkLocalIP, WLanface);
						LinkLocal_IP_Query(LinkLocalIP, Lanface);	//ZOTIPS
						WriteToEEPROM(&EEPROM_Data); 
						LinkLocal_set_state( IDLE );
					break;
			}
			
			if( LinkLocal_get_current_state() == IDLE)
				break;
		}	

		if( rendezvous_TaskHdl == 0)
		cyg_semaphore_post( &rendezvous_sem);
		else
		{
			cyg_thread_resume(rendezvous_TaskHdl);
		Need_Rendezous_Reload = 1;
		}
	
		cyg_semaphore_post(&DhcpWaitLinklocal_sem);
		
	}	
}
static int usblp_write( struct usblp *usblp, const char *buffer, size_t count, int *ppos)
{
	int timeout, errno = 0, writecount = 0;
	uint32 start;

//ZOT716u2	armond_printf("Start usblp_write\n");

	while (writecount < count) {
		// FIXME:  only use urb->status inside completion
		// callbacks; this way is racey...
		if (usblp->writeurb.status == -EINPROGRESS) {

//			if (file->f_flags & O_NONBLOCK)
//				return -EAGAIN;
#if 0	//ZOT716u2
			start = jiffies;
			timeout = USBLP_WRITE_TIMEOUT;

			while ( usblp->writeurb.status == -EINPROGRESS) {
	
//				if (signal_pending(current))
//					return writecount ? writecount : -EINTR;
				//cyg_thread_yield();
				
//				timeout = interruptible_sleep_on_timeout(&usblp->wait, timeout);
				
				if( jiffies - start > timeout ) break;
				
				cyg_thread_yield();
			}
#else	//ZOT716u2
			timeout = USBLP_WRITE_TIMEOUT;
			if (timeout && usblp->writeurb.status == -EINPROGRESS) {
				timeout = cyg_semaphore_timed_wait(usblp->wait, cyg_current_time() + ((timeout / MSPTICK)?(timeout / MSPTICK):1));
			}
#endif	//ZOT716u2			
		}

		if (!usblp->dev)
			return -ENODEV;

		if (usblp->writeurb.status != 0) {

//Print Server Recover Mechanism for OHCI	//635u2
			if (usblp->writeurb.status != -EINPROGRESS) {
				usblp->writeurb.status = 0;
				return writecount;
			}

			if (usblp->quirks & USBLP_QUIRK_BIDIR) {
//{{MARK_DEBUG
//				if (usblp->writeurb.status != -EINPROGRESS)
//					err("usblp%d: error %d writing to printer",
//						usblp->minor, usblp->writeurb.status);
//}}MARK_DEBUG
				errno = usblp->writeurb.status;
			}
			else {
//				errno = usblp_check_status(usblp, errno);
				errno = usblp->writeurb.status;
			}
			continue;
		}

		writecount += usblp->writeurb.transfer_buffer_length;
		usblp->writeurb.transfer_buffer_length = 0;

		if (writecount == count)
			continue;

		usblp->writeurb.transfer_buffer_length = (count - writecount) < USBLP_BUF_SIZE ?
							 (count - writecount) : USBLP_BUF_SIZE;

		copy_from_user(usblp->writeurb.transfer_buffer, buffer + writecount,
				usblp->writeurb.transfer_buffer_length);

		usblp->writeurb.dev = usblp->dev;
		usb_submit_urb(&usblp->writeurb);
//635u2
/*
#if defined(N635U2)|| defined(N635U2W)
		if(usblp->dev->ttport == 0)
		{
			if(usblp->dev->speed == USB_SPEED_HIGH)
				PrnLightToggle[1]++;
			else
				PrnLightToggle[3]++;	
		}
		else
		{
			if(usblp->dev->speed == USB_SPEED_HIGH)
				PrnLightToggle[2]++;
			else	
				PrnLightToggle[4]++;
		}
#else
		USBLightToggle++;
#endif	
*/	
#if 0	//ZOT716u2
		USBLightToggle++;
#else	//ZOT716u2
		if(usblp->dev->speed == USB_SPEED_HIGH)
			USB20LightToggle++;
		else
			USB11LightToggle++;
#endif	//ZOT716u2
	}

	return count;
}
Exemple #5
0
// ------------------------------------------------------------------------
// Wait for an event with timeout
//   tsleep(event, priority, state, timeout)
//     event - the thing to wait for
//     priority - unused
//     state    - a descriptive message
//     timeout  - max time (in ticks) to wait
//   returns:
//     0         - event was "signalled"
//     ETIMEDOUT - timeout occurred
//     EINTR     - thread broken out of sleep
//
int       
cyg_tsleep(void *chan, int pri, char *wmesg, int timo)
{
    int i, res = 0;
    struct wakeup_event *ev;    
    cyg_tick_count_t sleep_time;
    cyg_handle_t self = cyg_thread_self();
    int old_splflags = 0; // no flags held

    cyg_scheduler_lock();

    // Safely find a free slot:
    for (i = 0, ev = wakeup_list;  i < CYGPKG_NET_NUM_WAKEUP_EVENTS;  i++, ev++) {
        if (ev->chan == 0) {
            ev->chan = chan;
            break;
        }
    }
    CYG_ASSERT( i <  CYGPKG_NET_NUM_WAKEUP_EVENTS, "no sleep slots" );
    CYG_ASSERT( 1 == cyg_scheduler_read_lock(),
                "Tsleep - called with scheduler locked" );
    // Defensive:
    if ( i >= CYGPKG_NET_NUM_WAKEUP_EVENTS ) {
        cyg_scheduler_unlock();
        return ETIMEDOUT;
    }

    // If we are the owner, then we must release the mutex when
    // we wait.
    if ( self == splx_thread ) {
        old_splflags = spl_state; // Keep them for restoration
        CYG_ASSERT( spl_state, "spl_state not set" );
        // Also want to assert that the mutex is locked...
        CYG_ASSERT( splx_mutex.locked, "Splx mutex not locked" );
        CYG_ASSERT( (cyg_handle_t)splx_mutex.owner == self, "Splx mutex not mine" );
        splx_thread = 0;
        spl_state = 0;
        cyg_mutex_unlock( &splx_mutex );
    }

    // Re-initialize the semaphore - it might have counted up arbitrarily
    // in the time between a prior sleeper being signalled and them
    // actually running.
    cyg_semaphore_init(&ev->sem, 0);    

    // This part actually does the wait:
    // As of the new kernel, we can do this without unlocking the scheduler
    if (timo) {
        sleep_time = cyg_current_time() + timo;
        if (!cyg_semaphore_timed_wait(&ev->sem, sleep_time)) {
            if( cyg_current_time() >= sleep_time )
                res = ETIMEDOUT;
            else
                res = EINTR;
        }
    } else {
        if (!cyg_semaphore_wait(&ev->sem) ) {
            res = EINTR;
        }
    }

    ev->chan = 0;  // Free the slot - the wakeup call cannot do this.
    
    if ( old_splflags ) { // restore to previous state
        // As of the new kernel, we can do this with the scheduler locked
        cyg_mutex_lock( &splx_mutex ); // this might wait
        CYG_ASSERT( 0 == splx_thread, "Splx thread set in tsleep" );
        CYG_ASSERT( 0 == spl_state, "spl_state set in tsleep" );
        splx_thread = self; // got it now...
        spl_state = old_splflags;
    }

    cyg_scheduler_unlock();
    return res;
}