/** * @brief submit asynchronous urb * @param host struct uhci_host * @param device struct usb_device * @param epdesc struct usb_endpoint_descriptor * @param data void * * @param size u16 * @param callback int * * @param arg void* * @param ioc int */ static struct usb_request_block * uhci_submit_async(struct uhci_host *host, struct usb_device *device, struct usb_endpoint_descriptor *epdesc, void *data, u16 size, int (*callback)(struct usb_host *, struct usb_request_block *, void *), void *arg, int ioc) { struct usb_request_block *urb; size_t pktsize; u32 lospeed = 0; urb = uhci_create_urb(host); if (!urb) return (struct usb_request_block *)NULL; if (device) { spinlock_lock(&device->lock_dev); init_urb(urb, device->devnum, epdesc, callback, arg); spinlock_unlock(&device->lock_dev); } /* determine if we are dealing with a low speed device or not */ if (device) { if (device->speed == UD_SPEED_UNDEF) { u16 portsc; ASSERT (device->portno <= UHCI_NUM_PORTS_HC); portsc = host->portsc[(device->portno-1)]; device->speed = (portsc & UHCI_PORTSC_LOSPEED) ? UD_SPEED_LOW : UD_SPEED_FULL; } lospeed = (device->speed == UD_SPEED_LOW) ? UHCI_TD_STAT_LS : 0; } /* create a QH */ URB_UHCI(urb)->qh = uhci_alloc_qh(host, &URB_UHCI(urb)->qh_phys); if (!URB_UHCI(urb)->qh) goto fail_submit_async; URB_UHCI(urb)->qh->link = UHCI_QH_LINK_TE; pktsize = epdesc->wMaxPacketSize; /* buffer and TD */ if (size > 0) { struct usb_buffer_list *b; b = zalloc_usb_buffer_list(); b->pid = USB_PID_IN; b->len = size; b->vadr = (virt_t)alloc2_aligned(b->len, &b->padr); if (!b->vadr) { free(b); goto fail_submit_async; } /* copy data if OUT direction */ if (!USB_EP_DIRECT(epdesc)) { b->pid = USB_PID_OUT; memcpy((void *)b->vadr, data, b->len); } urb->buffers = b; } if (device) { spinlock_lock(&device->lock_dev); URB_UHCI(urb)->tdm_head = prepare_buffer_tds(host, (urb->buffers) ? (phys32_t)urb->buffers->padr : 0U, size, device->devnum, epdesc, (size_t)pktsize, UHCI_TD_STAT_AC | UHCI_TD_STAT_SP | lospeed | uhci_td_maxerr(3)); spinlock_unlock(&device->lock_dev); } if (!URB_UHCI(urb)->tdm_head) goto fail_submit_async; /* link the TDs into the QH */ URB_UHCI(urb)->qh->element = URB_UHCI(urb)->tdm_head->td_phys; /* set IOC */ if (ioc) URB_UHCI(urb)->tdm_head->td->status |= UHCI_TD_STAT_IC; /* set up toggles in TDs */ epdesc->toggle = uhci_fixup_toggles(URB_UHCI(urb)->tdm_head, epdesc->toggle); /* link the QH into the frame list */ if (uhci_activate_urb(host, urb) != URB_STATUS_RUN) goto fail_submit_async; URB_UHCI(urb)->tdm_acttail = NULL; return urb; fail_submit_async: uhci_destroy_urb(host, urb); return (struct usb_request_block *)NULL; }
/** * @brief submit asynchronous urb * @param host struct uhci_host * @param device struct usb_device * @param epdesc struct usb_endpoint_descriptor * @param data void * * @param size u16 * @param callback int * * @param arg void* * @param ioc int */ static struct usb_request_block * uhci_submit_async(struct uhci_host *host, struct usb_device *device, struct usb_endpoint_descriptor *epdesc, void *data, u16 size, int (*callback)(struct usb_host *, struct usb_request_block *, void *), void *arg, int ioc) { struct usb_request_block *urb; size_t pktsize; urb = create_urb(host); if (!urb) return (struct usb_request_block *)NULL; if (device){ spinlock_lock(&device->lock_dev); init_urb(urb, device->devnum, epdesc, callback, arg); spinlock_unlock(&device->lock_dev); } /* create a QH */ URB_UHCI(urb)->qh = uhci_alloc_qh(host, &URB_UHCI(urb)->qh_phys); if (!URB_UHCI(urb)->qh) goto fail_submit_async; URB_UHCI(urb)->qh->link = UHCI_QH_LINK_TE; pktsize = epdesc->wMaxPacketSize; /* buffer and TD */ if (size > 0) { struct usb_buffer_list *b; b = zalloc_usb_buffer_list(); b->len = size; b->vadr = malloc_from_pool(host->pool, b->len, &b->padr); if (!b->vadr) { free(b); goto fail_submit_async; } /* copy data if OUT direction */ if (!USB_EP_DIRECT(epdesc)) memcpy((void *)b->vadr, data, b->len); urb->buffers = b; } if (device){ spinlock_lock(&device->lock_dev); URB_UHCI(urb)->tdm_head = prepare_buffer_tds(host, (urb->buffers) ? (phys32_t)urb->buffers->padr : 0U, size, device->devnum, epdesc, UHCI_TD_STAT_AC | UHCI_TD_STAT_SP | uhci_td_maxerr(3)); spinlock_unlock(&device->lock_dev); } if (!URB_UHCI(urb)->tdm_head) goto fail_submit_async; /* link the TDs into the QH */ URB_UHCI(urb)->qh->element = URB_UHCI(urb)->tdm_head->td_phys; /* set IOC */ if (ioc) URB_UHCI(urb)->tdm_head->td->status |= UHCI_TD_STAT_IC; /* set up toggles in TDs */ epdesc->toggle = uhci_fixup_toggles(URB_UHCI(urb)->tdm_head, epdesc->toggle); /* link the QH into the frame list */ if (uhci_activate_urb(host, urb) != URB_STATUS_RUN) goto fail_submit_async; link_urb(&host->inproc_urbs, urb); return urb; fail_submit_async: destroy_urb(host, urb); return (struct usb_request_block *)NULL; }
/** * @brief submit the control messagie * @param host struct uhci_host * @param device struct usb_device * @param endpoint u8 * @param csetup struct usb_device * @param callback int * * @param arg void* * @param ioc int */ struct usb_request_block * uhci_submit_control(struct usb_host *usbhc, struct usb_device *device, u8 endpoint, u16 pktsz, struct usb_ctrl_setup *csetup, int (*callback)(struct usb_host *, struct usb_request_block *, void *), void *arg, int ioc) { struct uhci_host *host = (struct uhci_host *)usbhc->private; struct usb_request_block *urb; struct usb_endpoint_descriptor *epdesc; struct uhci_td_meta *tdm; struct usb_buffer_list *b; u32 lospeed = 0; epdesc = get_edesc_by_address(device, endpoint); if (!epdesc) { if (endpoint != 0) { dprintft(2, "%04x: %s: no endpoint(%d) found.\n", host->iobase, __FUNCTION__, endpoint); return (struct usb_request_block *)NULL; } /* use the default endpoint */ epdesc = &default_ep0; } /* determine if we are dealing with a low speed device or not */ if (device) { if (device->speed == UD_SPEED_UNDEF) { u16 portsc; ASSERT (device->portno <= UHCI_NUM_PORTS_HC); portsc = host->portsc[(device->portno-1)]; device->speed = (portsc & UHCI_PORTSC_LOSPEED) ? UD_SPEED_LOW : UD_SPEED_FULL; } lospeed = (device->speed == UD_SPEED_LOW) ? UHCI_TD_STAT_LS : 0; } dprintft(5, "%s: epdesc->wMaxPacketSize = %d\n", __FUNCTION__, epdesc->wMaxPacketSize); urb = uhci_create_urb(host); if (!urb) return (struct usb_request_block *)NULL; if (device) { spinlock_lock(&device->lock_dev); init_urb(urb, device->devnum, epdesc, callback, arg); spinlock_unlock(&device->lock_dev); } /* create a QH */ URB_UHCI(urb)->qh = uhci_alloc_qh(host, &URB_UHCI(urb)->qh_phys); if (!URB_UHCI(urb)->qh) goto fail_submit_control; URB_UHCI(urb)->qh->link = UHCI_QH_LINK_TE; if (pktsz == 0) pktsz = epdesc->wMaxPacketSize; /* SETUP TD */ URB_UHCI(urb)->tdm_head = tdm = uhci_new_td_meta(host, NULL); if (!tdm) goto fail_submit_control; URB_UHCI(urb)->qh->element = URB_UHCI(urb)->qh_element_copy = tdm->td_phys; b = zalloc_usb_buffer_list(); b->pid = USB_PID_SETUP; b->len = sizeof(*csetup); b->vadr = (virt_t)alloc2_aligned(b->len, &b->padr); if (!b->vadr) { free(b); goto fail_submit_control; } urb->buffers = b; memcpy((void *)b->vadr, (void *)csetup, b->len); tdm->td->status = tdm->status_copy = UHCI_TD_STAT_AC | lospeed | uhci_td_maxerr(3); if (device) { spinlock_lock(&device->lock_dev); tdm->td->token = tdm->token_copy = uhci_td_explen(sizeof(*csetup)) | UHCI_TD_TOKEN_ENDPOINT(epdesc->bEndpointAddress) | UHCI_TD_TOKEN_DEVADDRESS(device->devnum) | UHCI_TD_TOKEN_PID_SETUP; spinlock_unlock(&device->lock_dev); } tdm->td->buffer = (phys32_t)b->padr; if (csetup->wLength > 0) { b = zalloc_usb_buffer_list(); b->pid = USB_PID_IN; b->len = csetup->wLength; b->vadr = (virt_t)alloc2_aligned(b->len, &b->padr); if (!b->vadr) { free(b); goto fail_submit_control; } b->next = urb->buffers; urb->buffers = b; if (device) { spinlock_lock(&device->lock_dev); tdm = prepare_buffer_tds(host, (phys32_t)b->padr, b->len, device->devnum, epdesc, (size_t)pktsz, UHCI_TD_STAT_AC | UHCI_TD_STAT_SP | lospeed | uhci_td_maxerr(3)); spinlock_unlock(&device->lock_dev); } if (!tdm) goto fail_submit_control; dprintft(5, "%s: tdm->td_phys = %llx\n", __FUNCTION__, tdm->td_phys); URB_UHCI(urb)->tdm_head->next = tdm; URB_UHCI(urb)->tdm_head->td->link = tdm->td_phys; } /* The 1st toggle for SETUP must be 0. */ uhci_fixup_toggles(URB_UHCI(urb)->tdm_head, epdesc->toggle); /* append one more TD for the status stage */ for (tdm = URB_UHCI(urb)->tdm_head; tdm->next; tdm = tdm->next); tdm->next = uhci_new_td_meta(host, NULL); if (!tdm->next) goto fail_submit_control; tdm->next->td->link = UHCI_TD_LINK_TE; tdm->next->td->status = UHCI_TD_STAT_AC | lospeed | uhci_td_maxerr(3); if (ioc) tdm->next->td->status |= UHCI_TD_STAT_IC; if (device) { spinlock_lock(&device->lock_dev); tdm->next->td->token = uhci_td_explen(0) | UHCI_TD_TOKEN_ENDPOINT(epdesc->bEndpointAddress) | UHCI_TD_TOKEN_DEVADDRESS(device->devnum) | UHCI_TD_TOKEN_DT1_TOGGLE; spinlock_unlock(&device->lock_dev); } tdm->next->td->token |= (csetup->wLength > 0) ? UHCI_TD_TOKEN_PID_OUT : UHCI_TD_TOKEN_PID_IN; tdm->next->td->buffer = 0U; tdm->td->link = (phys32_t)tdm->next->td_phys; /* link the QH into the frame list */ if (uhci_activate_urb(host, urb) != URB_STATUS_RUN) goto fail_submit_control; URB_UHCI(urb)->tdm_acttail = NULL; return urb; fail_submit_control: uhci_destroy_urb(host, urb); return (struct usb_request_block *)NULL; }
/** * @brief submit the control messagie * @param host struct uhci_host * @param device struct usb_device * @param endpoint u8 * @param csetup struct usb_device * @param callback int * * @param arg void* * @param ioc int */ struct usb_request_block * uhci_submit_control(struct uhci_host *host, struct usb_device *device, u8 endpoint, struct usb_ctrl_setup *csetup, int (*callback)(struct usb_host *, struct usb_request_block *, void *), void *arg, int ioc) { struct usb_request_block *urb; struct usb_endpoint_descriptor *epdesc; struct uhci_td_meta *tdm; struct usb_buffer_list *b; size_t pktsize; epdesc = usb_epdesc(device, endpoint); if (!epdesc) { dprintft(2, "%04x: %s: no endpoint(%d) found.\n", host->iobase, __FUNCTION__, endpoint); return (struct usb_request_block *)NULL; } dprintft(5, "%s: epdesc->wMaxPacketSize = %d\n", __FUNCTION__, epdesc->wMaxPacketSize); urb = create_urb(host); if (!urb) return (struct usb_request_block *)NULL; if (device){ spinlock_lock(&device->lock_dev); init_urb(urb, device->devnum, epdesc, callback, arg); spinlock_unlock(&device->lock_dev); } /* create a QH */ URB_UHCI(urb)->qh = uhci_alloc_qh(host, &URB_UHCI(urb)->qh_phys); if (!URB_UHCI(urb)->qh) goto fail_submit_control; URB_UHCI(urb)->qh->link = UHCI_QH_LINK_TE; pktsize = epdesc->wMaxPacketSize; /* SETUP TD */ URB_UHCI(urb)->tdm_head = tdm = uhci_new_td_meta(host, NULL); if (!tdm) goto fail_submit_control; URB_UHCI(urb)->qh->element = URB_UHCI(urb)->qh_element_copy = tdm->td_phys; b = zalloc_usb_buffer_list(); b->len = sizeof(*csetup); b->vadr = malloc_from_pool(host->pool, b->len, &b->padr); if (!b->vadr) { free(b); goto fail_submit_control; } urb->buffers = b; memcpy((void *)b->vadr, (void *)csetup, b->len); tdm->td->status = tdm->status_copy = UHCI_TD_STAT_AC | uhci_td_maxerr(3); if (device){ spinlock_lock(&device->lock_dev); tdm->td->token = tdm->token_copy = uhci_td_explen(sizeof(*csetup)) | UHCI_TD_TOKEN_ENDPOINT(epdesc->bEndpointAddress) | UHCI_TD_TOKEN_DEVADDRESS(device->devnum) | UHCI_TD_TOKEN_PID_SETUP; spinlock_unlock(&device->lock_dev); } tdm->td->buffer = (phys32_t)b->padr; if (csetup->wLength > 0) { b = zalloc_usb_buffer_list(); b->len = csetup->wLength; b->vadr = malloc_from_pool(host->pool, b->len, &b->padr); if (!b->vadr) { free(b); goto fail_submit_control; } b->next = urb->buffers; urb->buffers = b; if(device){ spinlock_lock(&device->lock_dev); tdm = prepare_buffer_tds(host, (phys32_t)b->padr, b->len, device->devnum, epdesc, UHCI_TD_STAT_AC | UHCI_TD_STAT_SP | uhci_td_maxerr(3)); spinlock_unlock(&device->lock_dev); } if (!tdm) goto fail_submit_control; dprintft(5, "%s: tdm->td_phys = %llx\n", __FUNCTION__, tdm->td_phys); URB_UHCI(urb)->tdm_head->next = tdm; URB_UHCI(urb)->tdm_head->td->link = tdm->td_phys; } /* The 1st toggle for SETUP must be 0. */ uhci_fixup_toggles(URB_UHCI(urb)->tdm_head, epdesc->toggle); /* append one more TD for the status stage */ for (tdm = URB_UHCI(urb)->tdm_head; tdm->next; tdm = tdm->next); tdm->next = uhci_new_td_meta(host, NULL); if (!tdm->next) goto fail_submit_control; tdm->next->td->link = UHCI_TD_LINK_TE; tdm->next->td->status = UHCI_TD_STAT_AC | uhci_td_maxerr(3); if (ioc) tdm->next->td->status |= UHCI_TD_STAT_IC; if (device){ spinlock_lock(&device->lock_dev); tdm->next->td->token = uhci_td_explen(0) | UHCI_TD_TOKEN_ENDPOINT(epdesc->bEndpointAddress) | UHCI_TD_TOKEN_DEVADDRESS(device->devnum) | UHCI_TD_TOKEN_DT1_TOGGLE; spinlock_unlock(&device->lock_dev); } tdm->next->td->token |= (csetup->wLength > 0) ? UHCI_TD_TOKEN_PID_OUT : UHCI_TD_TOKEN_PID_IN; tdm->next->td->buffer = 0U; tdm->td->link = (phys32_t)tdm->next->td_phys; /* link the QH into the frame list */ if (uhci_activate_urb(host, urb) != URB_STATUS_RUN) goto fail_submit_control; link_urb(&host->inproc_urbs, urb); return urb; fail_submit_control: destroy_urb(host, urb); return (struct usb_request_block *)NULL; }