/* * 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; if (resume) { oz_dbg(ON, "USB service resumed\n"); return 0; } oz_dbg(ON, "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 = kzalloc(sizeof(struct oz_usb_ctx), GFP_ATOMIC); if (usb_ctx == NULL) return -ENOMEM; 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 == NULL) 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_dbg(ON, "Already have USB context\n"); kfree(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 == NULL) { oz_dbg(ON, "USB hub returned null port\n"); spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); pd->app_ctx[OZ_APPID_USB-1] = NULL; 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 decrements the reference count of the context area for a specific PD * and destroys the context area if the reference count becomes zero. * Context: irq 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_dbg(ON, "Dealloc USB context\n"); oz_pd_put(usb_ctx->pd); kfree(usb_ctx); } }
/* * Context: softirq or process */ void oz_pd_stop(struct oz_pd *pd) { u16 stop_apps; oz_dbg(ON, "oz_pd_stop() State = 0x%x\n", pd->state); oz_pd_indicate_farewells(pd); oz_polling_lock_bh(); stop_apps = pd->total_apps; pd->total_apps = 0; pd->paused_apps = 0; oz_polling_unlock_bh(); oz_services_stop(pd, stop_apps, 0); oz_polling_lock_bh(); oz_pd_set_state(pd, OZ_PD_S_STOPPED); /* Remove from PD list.*/ list_del(&pd->link); oz_polling_unlock_bh(); oz_dbg(ON, "pd ref count = %d\n", atomic_read(&pd->ref_count)); oz_pd_put(pd); }
/* * 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_dbg(ON, " req_type = 0x%x\n", req_type); oz_dbg(ON, " desc_type = 0x%x\n", desc_type); oz_dbg(ON, " index = 0x%x\n", index); oz_dbg(ON, " windex = 0x%x\n", windex); oz_dbg(ON, " offset = 0x%x\n", offset); oz_dbg(ON, " len = 0x%x\n", len); if (len > 200) len = 200; if (ei == NULL) 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: softirq or process */ struct oz_elt_info *oz_elt_info_alloc(struct oz_elt_buf *buf) { struct oz_elt_info *ei; spin_lock_bh(&buf->lock); if (buf->free_elts && buf->elt_pool) { ei = container_of(buf->elt_pool, struct oz_elt_info, link); buf->elt_pool = ei->link.next; buf->free_elts--; spin_unlock_bh(&buf->lock); if (ei->magic != OZ_ELT_INFO_MAGIC_FREE) { oz_dbg(ON, "%s: ei with bad magic: 0x%x\n", __func__, ei->magic); } } else {
/* * 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; if (pause) { oz_dbg(ON, "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] = NULL; spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); if (usb_ctx) { struct timespec ts, now; getnstimeofday(&ts); oz_dbg(ON, "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)) { getnstimeofday(&now); /*Approx 1 Sec. this is not perfect calculation*/ if (now.tv_sec != ts.tv_sec) break; } oz_dbg(ON, "USB service stopped\n"); oz_hcd_pd_departed(usb_ctx->hport); /* Release the reference taken in oz_usb_start. */ oz_usb_put(usb_ctx); } }
/* * 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_dbg(ON, "%s: (0x%x)\n", __func__, 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_dbg(ON, "%s: (0x%x)\n", __func__, 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; }