/*------------------------------------------------------------------------------ * 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; }
/*------------------------------------------------------------------------------ * 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_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] = NULL; 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); } }
/*------------------------------------------------------------------------------ * 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 == NULL) 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; }
/* * 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); } }