/*------------------------------------------------------------------------------ * Context: process */ static int oz_set_active_pd(u8 *addr) { int rc = 0; struct oz_pd *pd; struct oz_pd *old_pd; pd = oz_pd_find(addr); if (pd) { spin_lock_bh(&g_cdev.lock); memcpy(g_cdev.active_addr, addr, ETH_ALEN); old_pd = g_cdev.active_pd; g_cdev.active_pd = pd; spin_unlock_bh(&g_cdev.lock); if (old_pd) oz_pd_put(old_pd); } else { if (!memcmp(addr, "\0\0\0\0\0\0", sizeof(addr))) { spin_lock_bh(&g_cdev.lock); pd = g_cdev.active_pd; g_cdev.active_pd = 0; memset(g_cdev.active_addr, 0, sizeof(g_cdev.active_addr)); spin_unlock_bh(&g_cdev.lock); if (pd) oz_pd_put(pd); } else { rc = -1; } } return rc; }
/*------------------------------------------------------------------------------ * Context: softirq or process */ void oz_cdev_stop(struct oz_pd *pd, int pause) { struct oz_serial_ctx *ctx; oz_event_log(OZ_EVT_SERVICE, 4, OZ_APPID_SERIAL, 0, pause); if (pause) { oz_trace("Serial service paused.\n"); return; } spin_lock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]); ctx = (struct oz_serial_ctx *)pd->app_ctx[OZ_APPID_SERIAL-1]; pd->app_ctx[OZ_APPID_SERIAL-1] = 0; spin_unlock_bh(&pd->app_lock[OZ_APPID_SERIAL-1]); if (ctx) oz_cdev_release_ctx(ctx); spin_lock(&g_cdev.lock); if (pd == g_cdev.active_pd) g_cdev.active_pd = 0; else pd = 0; spin_unlock(&g_cdev.lock); if (pd) { oz_pd_put(pd); oz_trace("Active PD departed.\n"); } oz_trace("Serial service stopped.\n"); }
/*------------------------------------------------------------------------------ * Context: softirq or process */ void oz_pd_stop(struct oz_pd *pd) { u16 stop_apps = 0; oz_trace_msg(M, "oz_pd_stop() State = 0x%x\n", pd->state); oz_polling_lock_bh(); oz_pd_indicate_farewells(pd); 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); if (hrtimer_active(&pd->timeout)) { oz_trace_msg(M, "hrtimer timeout active\n"); hrtimer_cancel(&pd->timeout); } if (hrtimer_active(&pd->heartbeat)) { oz_trace_msg(M, "hrtimer heartbeat active\n"); hrtimer_cancel(&pd->heartbeat); } /* connect_req will restart timers */ /* Remove from PD list.*/ list_del(&pd->link); oz_polling_unlock_bh(); oz_trace_msg(M, "pd ref count = %d\n", atomic_read(&pd->ref_count)); oz_pd_put(pd); }
/*------------------------------------------------------------------------------ * 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); kfree(usb_ctx); } }
/*------------------------------------------------------------------------------ * 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; }
/*------------------------------------------------------------------------------ */ static void oz_pd_uevent_workitem(struct work_struct *work) { struct oz_pd *pd; char mac_buf[20]; char *envp[2]; pd = container_of(work, struct oz_pd, uevent_workitem); oz_trace_msg(D, "uevent ID_MAC:%pm\n", pd->mac_addr); snprintf(mac_buf, sizeof(mac_buf), "ID_MAC=%pm", pd->mac_addr); envp[0] = mac_buf; envp[1] = NULL; kobject_uevent_env(&g_oz_wpan_dev->kobj, KOBJ_CHANGE, envp); oz_pd_put(pd); }
/*------------------------------------------------------------------------------ * 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_pd_stop(struct oz_pd *pd) { u16 stop_apps = 0; oz_trace("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_trace("pd ref count = %d\n", atomic_read(&pd->ref_count)); oz_timer_delete(pd, 0); oz_pd_put(pd); }