From b3147863b523912633bfd4876d242cd5ae7d8e7a Mon Sep 17 00:00:00 2001 From: Chris Kelly Date: Mon, 20 Feb 2012 21:12:08 +0000 Subject: [PATCH] staging: ozwpan: Added USB service to protocol The L2 protocol supports various services, one of which is USB. This provides the implementation of that service and plumbs it to the virtual USB HCD. Signed-off-by: Chris Kelly Signed-off-by: Greg Kroah-Hartman --- drivers/staging/ozwpan/ozusbif.h | 43 +++ drivers/staging/ozwpan/ozusbsvc.c | 248 ++++++++++++++++ drivers/staging/ozwpan/ozusbsvc.h | 32 +++ drivers/staging/ozwpan/ozusbsvc1.c | 438 +++++++++++++++++++++++++++++ 4 files changed, 761 insertions(+) create mode 100644 drivers/staging/ozwpan/ozusbif.h create mode 100644 drivers/staging/ozwpan/ozusbsvc.c create mode 100644 drivers/staging/ozwpan/ozusbsvc.h create mode 100644 drivers/staging/ozwpan/ozusbsvc1.c diff --git a/drivers/staging/ozwpan/ozusbif.h b/drivers/staging/ozwpan/ozusbif.h new file mode 100644 index 000000000000..3acf5980d7cc --- /dev/null +++ b/drivers/staging/ozwpan/ozusbif.h @@ -0,0 +1,43 @@ +/* ----------------------------------------------------------------------------- + * Copyright (c) 2011 Ozmo Inc + * Released under the GNU General Public License Version 2 (GPLv2). + * ----------------------------------------------------------------------------- + */ +#ifndef _OZUSBIF_H +#define _OZUSBIF_H + +#include + +/* Reference counting functions. + */ +void oz_usb_get(void *hpd); +void oz_usb_put(void *hpd); + +/* Stream functions. + */ +int oz_usb_stream_create(void *hpd, u8 ep_num); +int oz_usb_stream_delete(void *hpd, u8 ep_num); + +/* Request functions. + */ +int oz_usb_control_req(void *hpd, u8 req_id, struct usb_ctrlrequest *setup, + u8 *data, int data_len); +int oz_usb_get_desc_req(void *hpd, u8 req_id, u8 req_type, u8 desc_type, + u8 index, u16 windex, int offset, int len); +int oz_usb_send_isoc(void *hpd, u8 ep_num, struct urb *urb); +void oz_usb_request_heartbeat(void *hpd); + +/* Confirmation functions. + */ +void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, + u8 *desc, int length, int offset, int total_size); +void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, + u8 *data, int data_len); + +/* Indication functions. + */ +void oz_hcd_data_ind(void *hport, u8 endpoint, u8 *data, int data_len); + +int oz_hcd_heartbeat(void *hport); + +#endif /* _OZUSBIF_H */ diff --git a/drivers/staging/ozwpan/ozusbsvc.c b/drivers/staging/ozwpan/ozusbsvc.c new file mode 100644 index 000000000000..1da98f6ecbe9 --- /dev/null +++ b/drivers/staging/ozwpan/ozusbsvc.c @@ -0,0 +1,248 @@ +/* ----------------------------------------------------------------------------- + * Copyright (c) 2011 Ozmo Inc + * Released under the GNU General Public License Version 2 (GPLv2). + * + * This file provides protocol independent part of the implementation of the USB + * service for a PD. + * The implementation of this service is split into two parts the first of which + * is protocol independent and the second contains protocol specific details. + * This split is to allow alternative protocols to be defined. + * The implemenation of this service uses ozhcd.c to implement a USB HCD. + * ----------------------------------------------------------------------------- + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include "ozconfig.h" +#include "ozprotocol.h" +#include "ozeltbuf.h" +#include "ozpd.h" +#include "ozproto.h" +#include "ozusbif.h" +#include "ozhcd.h" +#include "oztrace.h" +#include "ozalloc.h" +#include "ozusbsvc.h" +#include "ozevent.h" +/*------------------------------------------------------------------------------ + * This is called once when the driver is loaded to initialise the USB service. + * Context: process + */ +int oz_usb_init(void) +{ + oz_event_log(OZ_EVT_SERVICE, 1, OZ_APPID_USB, 0, 0); + return oz_hcd_init(); +} +/*------------------------------------------------------------------------------ + * This is called once when the driver is unloaded to terminate the USB service. + * Context: process + */ +void oz_usb_term(void) +{ + oz_event_log(OZ_EVT_SERVICE, 2, OZ_APPID_USB, 0, 0); + oz_hcd_term(); +} +/*------------------------------------------------------------------------------ + * This is called when the USB service is started or resumed for a PD. + * Context: softirq + */ +int oz_usb_start(struct oz_pd *pd, int resume) +{ + int rc = 0; + struct oz_usb_ctx *usb_ctx; + struct oz_usb_ctx *old_ctx = 0; + oz_event_log(OZ_EVT_SERVICE, 3, OZ_APPID_USB, 0, resume); + if (resume) { + oz_trace("USB service resumed.\n"); + return 0; + } + oz_trace("USB service started.\n"); + /* Create a USB context in case we need one. If we find the PD already + * has a USB context then we will destroy it. + */ + usb_ctx = (struct oz_usb_ctx *) + oz_alloc(sizeof(struct oz_usb_ctx), GFP_ATOMIC); + if (usb_ctx == 0) + return -1; + memset(usb_ctx, 0, sizeof(struct oz_usb_ctx)); + atomic_set(&usb_ctx->ref_count, 1); + usb_ctx->pd = pd; + usb_ctx->stopped = 0; + /* Install the USB context if the PD doesn't already have one. + * If it does already have one then destroy the one we have just + * created. + */ + spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); + old_ctx = pd->app_ctx[OZ_APPID_USB-1]; + if (old_ctx == 0) + pd->app_ctx[OZ_APPID_USB-1] = usb_ctx; + oz_usb_get(pd->app_ctx[OZ_APPID_USB-1]); + spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); + if (old_ctx) { + oz_trace("Already have USB context.\n"); + oz_free(usb_ctx); + usb_ctx = old_ctx; + } else if (usb_ctx) { + /* Take a reference to the PD. This will be released when + * the USB context is destroyed. + */ + oz_pd_get(pd); + } + /* If we already had a USB context and had obtained a port from + * the USB HCD then just reset the port. If we didn't have a port + * then report the arrival to the USB HCD so we get one. + */ + if (usb_ctx->hport) { + oz_hcd_pd_reset(usb_ctx, usb_ctx->hport); + } else { + usb_ctx->hport = oz_hcd_pd_arrived(usb_ctx); + if (usb_ctx->hport == 0) { + oz_trace("USB hub returned null port.\n"); + spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); + pd->app_ctx[OZ_APPID_USB-1] = 0; + spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); + oz_usb_put(usb_ctx); + rc = -1; + } + } + oz_usb_put(usb_ctx); + return rc; +} +/*------------------------------------------------------------------------------ + * This is called when the USB service is stopped or paused for a PD. + * Context: softirq or process + */ +void oz_usb_stop(struct oz_pd *pd, int pause) +{ + struct oz_usb_ctx *usb_ctx; + oz_event_log(OZ_EVT_SERVICE, 4, OZ_APPID_USB, 0, pause); + if (pause) { + oz_trace("USB service paused.\n"); + return; + } + spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); + usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1]; + pd->app_ctx[OZ_APPID_USB-1] = 0; + spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); + if (usb_ctx) { + unsigned long tout = jiffies + HZ; + oz_trace("USB service stopping...\n"); + usb_ctx->stopped = 1; + /* At this point the reference count on the usb context should + * be 2 - one from when we created it and one from the hcd + * which claims a reference. Since stopped = 1 no one else + * should get in but someone may already be in. So wait + * until they leave but timeout after 1 second. + */ + while ((atomic_read(&usb_ctx->ref_count) > 2) && + time_before(jiffies, tout)) + ; + oz_trace("USB service stopped.\n"); + oz_hcd_pd_departed(usb_ctx->hport); + /* Release the reference taken in oz_usb_start. + */ + oz_usb_put(usb_ctx); + } +} +/*------------------------------------------------------------------------------ + * This increments the reference count of the context area for a specific PD. + * This ensures this context area does not disappear while still in use. + * Context: softirq + */ +void oz_usb_get(void *hpd) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + atomic_inc(&usb_ctx->ref_count); +} +/*------------------------------------------------------------------------------ + * This decrements the reference count of the context area for a specific PD + * and destroys the context area if the reference count becomes zero. + * Context: softirq or process + */ +void oz_usb_put(void *hpd) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + if (atomic_dec_and_test(&usb_ctx->ref_count)) { + oz_trace("Dealloc USB context.\n"); + oz_pd_put(usb_ctx->pd); + oz_free(usb_ctx); + } +} +/*------------------------------------------------------------------------------ + * Context: softirq + */ +int oz_usb_heartbeat(struct oz_pd *pd) +{ + struct oz_usb_ctx *usb_ctx; + int rc = 0; + spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); + usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1]; + if (usb_ctx) + oz_usb_get(usb_ctx); + spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); + if (usb_ctx == 0) + return rc; + if (usb_ctx->stopped) + goto done; + if (usb_ctx->hport) + if (oz_hcd_heartbeat(usb_ctx->hport)) + rc = 1; +done: + oz_usb_put(usb_ctx); + return rc; +} +/*------------------------------------------------------------------------------ + * Context: softirq + */ +int oz_usb_stream_create(void *hpd, u8 ep_num) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + struct oz_pd *pd = usb_ctx->pd; + oz_trace("oz_usb_stream_create(0x%x)\n", ep_num); + if (pd->mode & OZ_F_ISOC_NO_ELTS) { + oz_isoc_stream_create(pd, ep_num); + } else { + oz_pd_get(pd); + if (oz_elt_stream_create(&pd->elt_buff, ep_num, + 4*pd->max_tx_size)) { + oz_pd_put(pd); + return -1; + } + } + return 0; +} +/*------------------------------------------------------------------------------ + * Context: softirq + */ +int oz_usb_stream_delete(void *hpd, u8 ep_num) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + if (usb_ctx) { + struct oz_pd *pd = usb_ctx->pd; + if (pd) { + oz_trace("oz_usb_stream_delete(0x%x)\n", ep_num); + if (pd->mode & OZ_F_ISOC_NO_ELTS) { + oz_isoc_stream_delete(pd, ep_num); + } else { + if (oz_elt_stream_delete(&pd->elt_buff, ep_num)) + return -1; + oz_pd_put(pd); + } + } + } + return 0; +} +/*------------------------------------------------------------------------------ + * Context: softirq or process + */ +void oz_usb_request_heartbeat(void *hpd) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + if (usb_ctx && usb_ctx->pd) + oz_pd_request_heartbeat(usb_ctx->pd); +} diff --git a/drivers/staging/ozwpan/ozusbsvc.h b/drivers/staging/ozwpan/ozusbsvc.h new file mode 100644 index 000000000000..58e05a59be31 --- /dev/null +++ b/drivers/staging/ozwpan/ozusbsvc.h @@ -0,0 +1,32 @@ +/* ----------------------------------------------------------------------------- + * Copyright (c) 2011 Ozmo Inc + * Released under the GNU General Public License Version 2 (GPLv2). + * ----------------------------------------------------------------------------- + */ +#ifndef _OZUSBSVC_H +#define _OZUSBSVC_H + +/*------------------------------------------------------------------------------ + * Per PD context info stored in application context area of PD. + * This object is reference counted to ensure it doesn't disappear while + * still in use. + */ +struct oz_usb_ctx { + atomic_t ref_count; + u8 tx_seq_num; + u8 rx_seq_num; + struct oz_pd *pd; + void *hport; + int stopped; +}; + +int oz_usb_init(void); +void oz_usb_term(void); +int oz_usb_start(struct oz_pd *pd, int resume); +void oz_usb_stop(struct oz_pd *pd, int pause); +void oz_usb_rx(struct oz_pd *pd, struct oz_elt *elt); +int oz_usb_heartbeat(struct oz_pd *pd); +void oz_usb_farewell(struct oz_pd *pd, u8 ep_num, u8 *data, u8 len); + +#endif /* _OZUSBSVC_H */ + diff --git a/drivers/staging/ozwpan/ozusbsvc1.c b/drivers/staging/ozwpan/ozusbsvc1.c new file mode 100644 index 000000000000..3982194cd0ee --- /dev/null +++ b/drivers/staging/ozwpan/ozusbsvc1.c @@ -0,0 +1,438 @@ +/* ----------------------------------------------------------------------------- + * Copyright (c) 2011 Ozmo Inc + * Released under the GNU General Public License Version 2 (GPLv2). + * + * This file implements the protocol specific parts of the USB service for a PD. + * ----------------------------------------------------------------------------- + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include "ozconfig.h" +#include "ozprotocol.h" +#include "ozeltbuf.h" +#include "ozpd.h" +#include "ozproto.h" +#include "ozusbif.h" +#include "ozhcd.h" +#include "oztrace.h" +#include "ozalloc.h" +#include "ozusbsvc.h" +#include "ozevent.h" +/*------------------------------------------------------------------------------ + */ +#define MAX_ISOC_FIXED_DATA (253-sizeof(struct oz_isoc_fixed)) +/*------------------------------------------------------------------------------ + * Context: softirq + */ +static int oz_usb_submit_elt(struct oz_elt_buf *eb, struct oz_elt_info *ei, + struct oz_usb_ctx *usb_ctx, u8 strid, u8 isoc) +{ + int ret; + struct oz_elt *elt = (struct oz_elt *)ei->data; + struct oz_app_hdr *app_hdr = (struct oz_app_hdr *)(elt+1); + elt->type = OZ_ELT_APP_DATA; + ei->app_id = OZ_APPID_USB; + ei->length = elt->length + sizeof(struct oz_elt); + app_hdr->app_id = OZ_APPID_USB; + spin_lock_bh(&eb->lock); + if (isoc == 0) { + app_hdr->elt_seq_num = usb_ctx->tx_seq_num++; + if (usb_ctx->tx_seq_num == 0) + usb_ctx->tx_seq_num = 1; + } + ret = oz_queue_elt_info(eb, isoc, strid, ei); + if (ret) + oz_elt_info_free(eb, ei); + spin_unlock_bh(&eb->lock); + return ret; +} +/*------------------------------------------------------------------------------ + * Context: softirq + */ +int oz_usb_get_desc_req(void *hpd, u8 req_id, u8 req_type, u8 desc_type, + u8 index, u16 windex, int offset, int len) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + struct oz_pd *pd = usb_ctx->pd; + struct oz_elt *elt; + struct oz_get_desc_req *body; + struct oz_elt_buf *eb = &pd->elt_buff; + struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff); + oz_trace(" req_type = 0x%x\n", req_type); + oz_trace(" desc_type = 0x%x\n", desc_type); + oz_trace(" index = 0x%x\n", index); + oz_trace(" windex = 0x%x\n", windex); + oz_trace(" offset = 0x%x\n", offset); + oz_trace(" len = 0x%x\n", len); + if (len > 200) + len = 200; + if (ei == 0) + return -1; + elt = (struct oz_elt *)ei->data; + elt->length = sizeof(struct oz_get_desc_req); + body = (struct oz_get_desc_req *)(elt+1); + body->type = OZ_GET_DESC_REQ; + body->req_id = req_id; + put_unaligned(cpu_to_le16(offset), &body->offset); + put_unaligned(cpu_to_le16(len), &body->size); + body->req_type = req_type; + body->desc_type = desc_type; + body->w_index = windex; + body->index = index; + return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0); +} +/*------------------------------------------------------------------------------ + * Context: tasklet + */ +static int oz_usb_set_config_req(void *hpd, u8 req_id, u8 index) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + struct oz_pd *pd = usb_ctx->pd; + struct oz_elt *elt; + struct oz_elt_buf *eb = &pd->elt_buff; + struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff); + struct oz_set_config_req *body; + if (ei == 0) + return -1; + elt = (struct oz_elt *)ei->data; + elt->length = sizeof(struct oz_set_config_req); + body = (struct oz_set_config_req *)(elt+1); + body->type = OZ_SET_CONFIG_REQ; + body->req_id = req_id; + body->index = index; + return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0); +} +/*------------------------------------------------------------------------------ + * Context: tasklet + */ +static int oz_usb_set_interface_req(void *hpd, u8 req_id, u8 index, u8 alt) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + struct oz_pd *pd = usb_ctx->pd; + struct oz_elt *elt; + struct oz_elt_buf *eb = &pd->elt_buff; + struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff); + struct oz_set_interface_req *body; + if (ei == 0) + return -1; + elt = (struct oz_elt *)ei->data; + elt->length = sizeof(struct oz_set_interface_req); + body = (struct oz_set_interface_req *)(elt+1); + body->type = OZ_SET_INTERFACE_REQ; + body->req_id = req_id; + body->index = index; + body->alternative = alt; + return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0); +} +/*------------------------------------------------------------------------------ + * Context: tasklet + */ +static int oz_usb_set_clear_feature_req(void *hpd, u8 req_id, u8 type, + u8 recipient, u8 index, __le16 feature) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + struct oz_pd *pd = usb_ctx->pd; + struct oz_elt *elt; + struct oz_elt_buf *eb = &pd->elt_buff; + struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff); + struct oz_feature_req *body; + if (ei == 0) + return -1; + elt = (struct oz_elt *)ei->data; + elt->length = sizeof(struct oz_feature_req); + body = (struct oz_feature_req *)(elt+1); + body->type = type; + body->req_id = req_id; + body->recipient = recipient; + body->index = index; + put_unaligned(feature, &body->feature); + return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0); +} +/*------------------------------------------------------------------------------ + * Context: tasklet + */ +static int oz_usb_vendor_class_req(void *hpd, u8 req_id, u8 req_type, + u8 request, __le16 value, __le16 index, u8 *data, int data_len) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + struct oz_pd *pd = usb_ctx->pd; + struct oz_elt *elt; + struct oz_elt_buf *eb = &pd->elt_buff; + struct oz_elt_info *ei = oz_elt_info_alloc(&pd->elt_buff); + struct oz_vendor_class_req *body; + if (ei == 0) + return -1; + elt = (struct oz_elt *)ei->data; + elt->length = sizeof(struct oz_vendor_class_req) - 1 + data_len; + body = (struct oz_vendor_class_req *)(elt+1); + body->type = OZ_VENDOR_CLASS_REQ; + body->req_id = req_id; + body->req_type = req_type; + body->request = request; + put_unaligned(value, &body->value); + put_unaligned(index, &body->index); + if (data_len) + memcpy(body->data, data, data_len); + return oz_usb_submit_elt(eb, ei, usb_ctx, 0, 0); +} +/*------------------------------------------------------------------------------ + * Context: tasklet + */ +int oz_usb_control_req(void *hpd, u8 req_id, struct usb_ctrlrequest *setup, + u8 *data, int data_len) +{ + unsigned wvalue = le16_to_cpu(setup->wValue); + unsigned windex = le16_to_cpu(setup->wIndex); + unsigned wlength = le16_to_cpu(setup->wLength); + int rc = 0; + oz_event_log(OZ_EVT_CTRL_REQ, setup->bRequest, req_id, + (void *)(((unsigned long)(setup->wValue))<<16 | + ((unsigned long)setup->wIndex)), + setup->bRequestType); + if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) { + switch (setup->bRequest) { + case USB_REQ_GET_DESCRIPTOR: + rc = oz_usb_get_desc_req(hpd, req_id, + setup->bRequestType, (u8)(wvalue>>8), + (u8)wvalue, setup->wIndex, 0, wlength); + break; + case USB_REQ_SET_CONFIGURATION: + rc = oz_usb_set_config_req(hpd, req_id, (u8)wvalue); + break; + case USB_REQ_SET_INTERFACE: { + u8 if_num = (u8)windex; + u8 alt = (u8)wvalue; + rc = oz_usb_set_interface_req(hpd, req_id, + if_num, alt); + } + break; + case USB_REQ_SET_FEATURE: + rc = oz_usb_set_clear_feature_req(hpd, req_id, + OZ_SET_FEATURE_REQ, + setup->bRequestType & 0xf, (u8)windex, + setup->wValue); + break; + case USB_REQ_CLEAR_FEATURE: + rc = oz_usb_set_clear_feature_req(hpd, req_id, + OZ_CLEAR_FEATURE_REQ, + setup->bRequestType & 0xf, + (u8)windex, setup->wValue); + break; + } + } else { + rc = oz_usb_vendor_class_req(hpd, req_id, setup->bRequestType, + setup->bRequest, setup->wValue, setup->wIndex, + data, data_len); + } + return rc; +} +/*------------------------------------------------------------------------------ + * Context: softirq + */ +int oz_usb_send_isoc(void *hpd, u8 ep_num, struct urb *urb) +{ + struct oz_usb_ctx *usb_ctx = (struct oz_usb_ctx *)hpd; + struct oz_pd *pd = usb_ctx->pd; + struct oz_elt_buf *eb; + int i; + int hdr_size; + u8 *data; + struct usb_iso_packet_descriptor *desc; + + if (pd->mode & OZ_F_ISOC_NO_ELTS) { + for (i = 0; i < urb->number_of_packets; i++) { + u8 *data; + desc = &urb->iso_frame_desc[i]; + data = ((u8 *)urb->transfer_buffer)+desc->offset; + oz_send_isoc_unit(pd, ep_num, data, desc->length); + } + return 0; + } + + hdr_size = sizeof(struct oz_isoc_fixed) - 1; + eb = &pd->elt_buff; + i = 0; + while (i < urb->number_of_packets) { + struct oz_elt_info *ei = oz_elt_info_alloc(eb); + struct oz_elt *elt; + struct oz_isoc_fixed *body; + int unit_count; + int unit_size; + int rem; + if (ei == 0) + return -1; + rem = MAX_ISOC_FIXED_DATA; + elt = (struct oz_elt *)ei->data; + body = (struct oz_isoc_fixed *)(elt + 1); + body->type = OZ_USB_ENDPOINT_DATA; + body->endpoint = ep_num; + body->format = OZ_DATA_F_ISOC_FIXED; + unit_size = urb->iso_frame_desc[i].length; + body->unit_size = (u8)unit_size; + data = ((u8 *)(elt+1)) + hdr_size; + unit_count = 0; + while (i < urb->number_of_packets) { + desc = &urb->iso_frame_desc[i]; + if ((unit_size == desc->length) && + (desc->length <= rem)) { + memcpy(data, ((u8 *)urb->transfer_buffer) + + desc->offset, unit_size); + data += unit_size; + rem -= unit_size; + unit_count++; + desc->status = 0; + desc->actual_length = desc->length; + i++; + } else { + break; + } + } + elt->length = hdr_size + MAX_ISOC_FIXED_DATA - rem; + /* Store the number of units in body->frame_number for the + * moment. This field will be correctly determined before + * the element is sent. */ + body->frame_number = (u8)unit_count; + oz_usb_submit_elt(eb, ei, usb_ctx, ep_num, + pd->mode & OZ_F_ISOC_ANYTIME); + } + return 0; +} +/*------------------------------------------------------------------------------ + * Context: softirq-serialized + */ +void oz_usb_handle_ep_data(struct oz_usb_ctx *usb_ctx, + struct oz_usb_hdr *usb_hdr, int len) +{ + struct oz_data *data_hdr = (struct oz_data *)usb_hdr; + switch (data_hdr->format) { + case OZ_DATA_F_MULTIPLE_FIXED: { + struct oz_multiple_fixed *body = + (struct oz_multiple_fixed *)data_hdr; + u8 *data = body->data; + int n = (len - sizeof(struct oz_multiple_fixed)+1) + / body->unit_size; + while (n--) { + oz_hcd_data_ind(usb_ctx->hport, body->endpoint, + data, body->unit_size); + data += body->unit_size; + } + } + break; + case OZ_DATA_F_ISOC_FIXED: { + struct oz_isoc_fixed *body = + (struct oz_isoc_fixed *)data_hdr; + int data_len = len-sizeof(struct oz_isoc_fixed)+1; + int unit_size = body->unit_size; + u8 *data = body->data; + int count; + int i; + if (!unit_size) + break; + count = data_len/unit_size; + for (i = 0; i < count; i++) { + oz_hcd_data_ind(usb_ctx->hport, + body->endpoint, data, unit_size); + data += unit_size; + } + } + break; + } + +} +/*------------------------------------------------------------------------------ + * This is called when the PD has received a USB element. The type of element + * is determined and is then passed to an appropriate handler function. + * Context: softirq-serialized + */ +void oz_usb_rx(struct oz_pd *pd, struct oz_elt *elt) +{ + struct oz_usb_hdr *usb_hdr = (struct oz_usb_hdr *)(elt + 1); + struct oz_usb_ctx *usb_ctx; + + spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); + usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1]; + if (usb_ctx) + oz_usb_get(usb_ctx); + spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); + if (usb_ctx == 0) + return; /* Context has gone so nothing to do. */ + if (usb_ctx->stopped) + goto done; + /* If sequence number is non-zero then check it is not a duplicate. + * Zero sequence numbers are always accepted. + */ + if (usb_hdr->elt_seq_num != 0) { + if (((usb_ctx->rx_seq_num - usb_hdr->elt_seq_num) & 0x80) == 0) + /* Reject duplicate element. */ + goto done; + } + usb_ctx->rx_seq_num = usb_hdr->elt_seq_num; + switch (usb_hdr->type) { + case OZ_GET_DESC_RSP: { + struct oz_get_desc_rsp *body = + (struct oz_get_desc_rsp *)usb_hdr; + int data_len = elt->length - + sizeof(struct oz_get_desc_rsp) + 1; + u16 offs = le16_to_cpu(get_unaligned(&body->offset)); + u16 total_size = + le16_to_cpu(get_unaligned(&body->total_size)); + oz_trace("USB_REQ_GET_DESCRIPTOR - cnf\n"); + oz_hcd_get_desc_cnf(usb_ctx->hport, body->req_id, + body->rcode, body->data, + data_len, offs, total_size); + } + break; + case OZ_SET_CONFIG_RSP: { + struct oz_set_config_rsp *body = + (struct oz_set_config_rsp *)usb_hdr; + oz_hcd_control_cnf(usb_ctx->hport, body->req_id, + body->rcode, 0, 0); + } + break; + case OZ_SET_INTERFACE_RSP: { + struct oz_set_interface_rsp *body = + (struct oz_set_interface_rsp *)usb_hdr; + oz_hcd_control_cnf(usb_ctx->hport, + body->req_id, body->rcode, 0, 0); + } + break; + case OZ_VENDOR_CLASS_RSP: { + struct oz_vendor_class_rsp *body = + (struct oz_vendor_class_rsp *)usb_hdr; + oz_hcd_control_cnf(usb_ctx->hport, body->req_id, + body->rcode, body->data, elt->length- + sizeof(struct oz_vendor_class_rsp)+1); + } + break; + case OZ_USB_ENDPOINT_DATA: + oz_usb_handle_ep_data(usb_ctx, usb_hdr, elt->length); + break; + } +done: + oz_usb_put(usb_ctx); +} +/*------------------------------------------------------------------------------ + * Context: softirq, process + */ +void oz_usb_farewell(struct oz_pd *pd, u8 ep_num, u8 *data, u8 len) +{ + struct oz_usb_ctx *usb_ctx; + spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); + usb_ctx = (struct oz_usb_ctx *)pd->app_ctx[OZ_APPID_USB-1]; + if (usb_ctx) + oz_usb_get(usb_ctx); + spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); + if (usb_ctx == 0) + return; /* Context has gone so nothing to do. */ + if (!usb_ctx->stopped) { + oz_trace("Farewell indicated ep = 0x%x\n", ep_num); + oz_hcd_data_ind(usb_ctx->hport, ep_num, data, len); + } + oz_usb_put(usb_ctx); +} -- 2.20.1