/*------------------------------------------------------------------------------ * 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 = kzalloc(sizeof(struct oz_usb_ctx), GFP_ATOMIC); if (usb_ctx == 0) 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 == 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"); 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 == 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; }
/*------------------------------------------------------------------------------ * Context: softirq-serialized */ int oz_cdev_start(struct oz_pd *pd, int resume) { struct oz_serial_ctx *ctx; struct oz_serial_ctx *old_ctx = 0; oz_event_log(OZ_EVT_SERVICE, 3, OZ_APPID_SERIAL, 0, resume); if (resume) { oz_trace("Serial service resumed.\n"); return 0; } ctx = kzalloc(sizeof(struct oz_serial_ctx), GFP_ATOMIC); if (ctx == 0) return -ENOMEM; atomic_set(&ctx->ref_count, 1); ctx->tx_seq_num = 1; spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]); old_ctx = pd->app_ctx[OZ_APPID_SERIAL-1]; if (old_ctx) { spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]); kfree(ctx); } else { pd->app_ctx[OZ_APPID_SERIAL-1] = ctx; spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]); } spin_lock(&g_cdev.lock); if ((g_cdev.active_pd == 0) && (memcmp(pd->mac_addr, g_cdev.active_addr, ETH_ALEN) == 0)) { oz_pd_get(pd); g_cdev.active_pd = pd; oz_trace("Active PD arrived.\n"); } spin_unlock(&g_cdev.lock); oz_trace("Serial service started.\n"); return 0; }
/*------------------------------------------------------------------------------ * Context: process */ ssize_t oz_cdev_write(struct file *filp, const char __user *buf, size_t count, loff_t *fpos) { struct oz_pd *pd; struct oz_elt_buf *eb; struct oz_elt_info *ei = 0; struct oz_elt *elt; struct oz_app_hdr *app_hdr; struct oz_serial_ctx *ctx; if (count > sizeof(ei->data) - sizeof(*elt) - sizeof(*app_hdr)) return -EINVAL; spin_lock_bh(&g_cdev.lock); pd = g_cdev.active_pd; if (pd) oz_pd_get(pd); spin_unlock_bh(&g_cdev.lock); if (pd == 0) return -1; eb = &pd->elt_buff; ei = oz_elt_info_alloc(eb); if (ei == 0) { count = 0; goto out; } elt = (struct oz_elt *)ei->data; app_hdr = (struct oz_app_hdr *)(elt+1); elt->length = sizeof(struct oz_app_hdr) + count; elt->type = OZ_ELT_APP_DATA; ei->app_id = OZ_APPID_SERIAL; ei->length = elt->length + sizeof(struct oz_elt); app_hdr->app_id = OZ_APPID_SERIAL; if (copy_from_user(app_hdr+1, buf, count)) goto out; spin_lock_bh(&pd->app_lock[OZ_APPID_USB-1]); ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1]; if (ctx) { app_hdr->elt_seq_num = ctx->tx_seq_num++; if (ctx->tx_seq_num == 0) ctx->tx_seq_num = 1; spin_lock(&eb->lock); if (oz_queue_elt_info(eb, 0, 0, ei) == 0) ei = 0; spin_unlock(&eb->lock); } spin_unlock_bh(&pd->app_lock[OZ_APPID_USB-1]); out: if (ei) { count = 0; spin_lock_bh(&eb->lock); oz_elt_info_free(eb, ei); spin_unlock_bh(&eb->lock); } oz_pd_put(pd); return count; }
/*------------------------------------------------------------------------------ */ void oz_pd_notify_uevent(struct oz_pd *pd) { int ret; oz_pd_get(pd); ret = schedule_work(&pd->uevent_workitem); if (!ret) { pr_info("%s: failed to schedule workitem\n", __func__); oz_pd_put(pd); } }
/*------------------------------------------------------------------------------ * Context: process */ ssize_t oz_cdev_read(struct file *filp, char __user *buf, size_t count, loff_t *fpos) { int n; int ix; struct oz_pd *pd; struct oz_serial_ctx *ctx = 0; spin_lock_bh(&g_cdev.lock); pd = g_cdev.active_pd; if (pd) oz_pd_get(pd); spin_unlock_bh(&g_cdev.lock); if (pd == 0) return -1; ctx = oz_cdev_claim_ctx(pd); if (ctx == 0) goto out2; n = ctx->rd_in - ctx->rd_out; if (n < 0) n += OZ_RD_BUF_SZ; if (count > n) count = n; ix = ctx->rd_out; n = OZ_RD_BUF_SZ - ix; if (n > count) n = count; if (copy_to_user(buf, &ctx->rd_buf[ix], n)) { count = 0; goto out1; } ix += n; if (ix == OZ_RD_BUF_SZ) ix = 0; if (n < count) { if (copy_to_user(&buf[n], ctx->rd_buf, count-n)) { count = 0; goto out1; } ix = count-n; } ctx->rd_out = ix; out1: oz_cdev_release_ctx(ctx); out2: oz_pd_put(pd); return count; }
/*------------------------------------------------------------------------------ * 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; }