static usbd_status
alloc_all_endpoints_yamaha(struct umidi_softc *sc)
{
	/* This driver currently supports max 1in/1out bulk endpoints */
	usb_descriptor_t *desc;
	umidi_cs_descriptor_t *udesc;
	usb_endpoint_descriptor_t *epd;
	int out_addr, in_addr, i;
	int dir;
	size_t remain, descsize;

	sc->sc_out_num_jacks = sc->sc_in_num_jacks = 0;
	out_addr = in_addr = 0;

	/* detect endpoints */
	desc = TO_D(usbd_get_interface_descriptor(sc->sc_iface));
	for (i=(int)TO_IFD(desc)->bNumEndpoints-1; i>=0; i--) {
		epd = usbd_interface2endpoint_descriptor(sc->sc_iface, i);
		KASSERT(epd != NULL);
		if (UE_GET_XFERTYPE(epd->bmAttributes) == UE_BULK) {
			dir = UE_GET_DIR(epd->bEndpointAddress);
			if (dir==UE_DIR_OUT && !out_addr)
				out_addr = epd->bEndpointAddress;
			else if (dir==UE_DIR_IN && !in_addr)
				in_addr = epd->bEndpointAddress;
		}
	}
	udesc = (umidi_cs_descriptor_t *)NEXT_D(desc);

	/* count jacks */
	if (!(udesc->bDescriptorType==UDESC_CS_INTERFACE &&
	      udesc->bDescriptorSubtype==UMIDI_MS_HEADER))
		return USBD_INVAL;
	remain = (size_t)UGETW(TO_CSIFD(udesc)->wTotalLength) -
		(size_t)udesc->bLength;
	udesc = (umidi_cs_descriptor_t *)NEXT_D(udesc);

	while (remain>=sizeof(usb_descriptor_t)) {
		descsize = udesc->bLength;
		if (descsize>remain || descsize==0)
			break;
		if (udesc->bDescriptorType==UDESC_CS_INTERFACE &&
		    remain>=UMIDI_JACK_DESCRIPTOR_SIZE) {
			if (udesc->bDescriptorSubtype==UMIDI_OUT_JACK)
				sc->sc_out_num_jacks++;
			else if (udesc->bDescriptorSubtype==UMIDI_IN_JACK)
				sc->sc_in_num_jacks++;
		}
		udesc = (umidi_cs_descriptor_t *)NEXT_D(udesc);
		remain-=descsize;
	}

	/* validate some parameters */
	if (sc->sc_out_num_jacks>UMIDI_MAX_EPJACKS)
		sc->sc_out_num_jacks = UMIDI_MAX_EPJACKS;
	if (sc->sc_in_num_jacks>UMIDI_MAX_EPJACKS)
		sc->sc_in_num_jacks = UMIDI_MAX_EPJACKS;
	if (sc->sc_out_num_jacks && out_addr) {
		sc->sc_out_num_endpoints = 1;
	} else {
		sc->sc_out_num_endpoints = 0;
		sc->sc_out_num_jacks = 0;
	}
	if (sc->sc_in_num_jacks && in_addr) {
		sc->sc_in_num_endpoints = 1;
	} else {
		sc->sc_in_num_endpoints = 0;
		sc->sc_in_num_jacks = 0;
	}
	sc->sc_endpoints = malloc(sizeof(struct umidi_endpoint)*
				  (sc->sc_out_num_endpoints+
				   sc->sc_in_num_endpoints),
				  M_USBDEV, M_WAITOK);
	if (!sc->sc_endpoints)
		return USBD_NOMEM;
	if (sc->sc_out_num_endpoints) {
		sc->sc_out_ep = sc->sc_endpoints;
		sc->sc_out_ep->sc = sc;
		sc->sc_out_ep->addr = out_addr;
		sc->sc_out_ep->num_jacks = sc->sc_out_num_jacks;
		sc->sc_out_ep->num_open = 0;
		memset(sc->sc_out_ep->jacks, 0, sizeof(sc->sc_out_ep->jacks));
	} else
		sc->sc_out_ep = NULL;

	if (sc->sc_in_num_endpoints) {
		sc->sc_in_ep = sc->sc_endpoints+sc->sc_out_num_endpoints;
		sc->sc_in_ep->sc = sc;
		sc->sc_in_ep->addr = in_addr;
		sc->sc_in_ep->num_jacks = sc->sc_in_num_jacks;
		sc->sc_in_ep->num_open = 0;
		memset(sc->sc_in_ep->jacks, 0, sizeof(sc->sc_in_ep->jacks));
	} else
		sc->sc_in_ep = NULL;

	return USBD_NORMAL_COMPLETION;
}
Example #2
0
// 转换为标准形式为: YYYY-MM-DD YYYY-M-D
static char *date_transfer(int date_id,const char *from_date,char *to_date)
{
  int l,n;
  const char *b,*e;

  if(!from_date || !*from_date || !to_date)
    return NULL;

  n = 0;
  switch(date_id) {
    case 0: // YYYY年MM月DD[日]
      b = from_date;
      e = strstr(b,"年"); if(!e) return NULL;
      TO_YYYY(b,e,l,n,to_date);
      
      b = e+strlen("年");
      e = strstr(b,"月"); if(!e) return NULL;
      TO_M(b,e,l,n,to_date);
      
      b = e+strlen("月");
      e = strstr(b,"日"); if(!e) e = from_date+strlen(from_date);
      TO_D(b,e,l,n,to_date);
      break;
    case 1: // MM月DD日YYYY[年]
      b = strstr(from_date,"日"); if(!b) return NULL;
      b += strlen("日");
      e = strstr(b,"年"); if(!e) e = from_date+strlen(from_date);
      TO_YYYY(b,e,l,n,to_date);

      b = from_date;
      e = strstr(b,"月"); if(!e) return NULL;
      TO_M(b,e,l,n,to_date);

      b = e+strlen("月");
      e = strstr(b,"日"); if(!e) return NULL;
      TO_D(b,e,l,n,to_date);
      break;   
    case 2: // YYYY-MM-DD YYYY/MM/DD YYYY.MM.DD YYYY_MM_DD YYYY MM DD YYYYMMDD
      b = from_date;
      e = strpbrk(b,"-/._ "); if(!e) return NULL;
      TO_YYYY(b,e,l,n,to_date);

      b = ++e;
      e = strpbrk(b,"-/._ "); if(!e) return NULL;
      TO_M(b,e,l,n,to_date);

      b = ++e;
      e = from_date+strlen(from_date);
      TO_D(b,e,l,n,to_date);
      break;
    case 3: // MM-DD-YYYY MM/DD/YYYY MM.DD.YYYY MM_DD_YYYY MM DD YYYY MMDDYYYY
      b = strpbrk(from_date,"-/._ "); if(!b) return NULL;
      b = strpbrk(++b,"-/._ "); if(!b) return NULL;
      ++b;
      e = from_date+strlen(from_date);
      TO_YYYY(b,e,l,n,to_date);

      b = from_date;
      e = strpbrk(b,"-/._ "); if(!e) return NULL;
      TO_M(b,e,l,n,to_date);

      b = ++e;
      e = strpbrk(b,"-/._ "); if(!e) return NULL;
      TO_D(b,e,l,n,to_date);
      break; 
    case 4: // MM DD,YYYY
      b = strchr(from_date,','); if(!b) return NULL;
      ++b;
      e = from_date+strlen(from_date);
      TO_YYYY(b,e,l,n,to_date);

      b = from_date;
      e = strchr(b,' '); if(!e) return NULL;
      TO_M(b,e,l,n,to_date);

      b = ++e;
      e = strchr(b,','); if(!e) return NULL;
      TO_D(b,e,l,n,to_date);
      break;
    case 5: // YYYYMMDD
      b = from_date;
      e = b+4;
      TO_YYYY(b,e,l,n,to_date);

      b = e;
      e = b+2;
      TO_M(b,e,l,n,to_date);

      b = e;
      e = b+2;
      TO_D(b,e,l,n,to_date);
      break;
    case 6: // MMDDYYYY
      b = from_date+4;
      e = b+4;
      TO_YYYY(b,e,l,n,to_date);

      b = from_date;
      e = b+2;
      TO_M(b,e,l,n,to_date);

      b = e;
      e = b+2;
      TO_D(b,e,l,n,to_date);
      break;
    default:
      return NULL;
  }

  return to_date;
}