static int smd_tty_open(struct tty_struct *tty, struct file *f) { int res = 0; unsigned int n = tty->index; struct smd_tty_info *info; const char *peripheral = NULL; if (n >= MAX_SMD_TTYS || !smd_tty[n].smd) return -ENODEV; info = smd_tty + n; mutex_lock(&smd_tty_lock); tty->driver_data = info; if (info->open_count++ == 0) { peripheral = smd_edge_to_subsystem(smd_tty[n].smd->edge); if (peripheral) { info->pil = pil_get(peripheral); if (IS_ERR(info->pil)) { res = PTR_ERR(info->pil); goto out; } /* Wait for the modem SMSM to be inited for the SMD * Loopback channel to be allocated at the modem. Since * the wait need to be done atmost once, using msleep * doesn't degrade the performance. */ if (n == LOOPBACK_IDX) { if (!is_modem_smsm_inited()) msleep(5000); smsm_change_state(SMSM_APPS_STATE, 0, SMSM_SMD_LOOPBACK); msleep(100); } /* * Wait for a channel to be allocated so we know * the modem is ready enough. */ if (smd_tty_modem_wait) { res = wait_for_completion_interruptible_timeout( &info->ch_allocated, msecs_to_jiffies(smd_tty_modem_wait * 1000)); if (res == 0) { pr_err("Timed out waiting for SMD" " channel\n"); res = -ETIMEDOUT; goto release_pil; } else if (res < 0) { pr_err("Error waiting for SMD channel:" " %d\n", res); goto release_pil; } res = 0; } } info->tty = tty; tasklet_init(&info->tty_tsklt, smd_tty_read, (unsigned long)info); wake_lock_init(&info->wake_lock, WAKE_LOCK_SUSPEND, smd_tty[n].smd->port_name); if (!info->ch) { res = smd_named_open_on_edge(smd_tty[n].smd->port_name, smd_tty[n].smd->edge, &info->ch, info, smd_tty_notify); if (res < 0) { pr_err("%s: %s open failed %d\n", __func__, smd_tty[n].smd->port_name, res); goto release_pil; } res = wait_event_interruptible_timeout( info->ch_opened_wait_queue, info->is_open, (2 * HZ)); if (res == 0) res = -ETIMEDOUT; if (res < 0) { pr_err("%s: wait for %s smd_open failed %d\n", __func__, smd_tty[n].smd->port_name, res); goto release_pil; } res = 0; } } release_pil: if (res < 0) pil_put(info->pil); else smd_disable_read_intr(info->ch); out: mutex_unlock(&smd_tty_lock); return res; }
static int smd_tty_port_activate(struct tty_port *tport, struct tty_struct *tty) { int res = 0; unsigned int n = tty->index; struct smd_tty_info *info; const char *peripheral = NULL; if (n >= MAX_SMD_TTYS || !smd_tty[n].smd) return -ENODEV; info = smd_tty + n; mutex_lock(&smd_tty_lock); tty->driver_data = info; peripheral = smd_edge_to_subsystem(smd_tty[n].smd->edge); if (peripheral) { info->pil = subsystem_get(peripheral); if (IS_ERR(info->pil)) { SMD_TTY_INFO( "%s failed on smd_tty device :%s subsystem_get failed for %s", __func__, smd_tty[n].smd->port_name, peripheral); /* * Sleep, inorder to reduce the frequency of * retry by user-space modules and to avoid * possible watchdog bite. */ msleep((smd_tty[n].open_wait * 1000)); res = PTR_ERR(info->pil); goto out; } /* Wait for the modem SMSM to be inited for the SMD * Loopback channel to be allocated at the modem. Since * the wait need to be done atmost once, using msleep * doesn't degrade the performance. */ if (n == LOOPBACK_IDX) { if (!is_modem_smsm_inited()) msleep(5000); smsm_change_state(SMSM_APPS_STATE, 0, SMSM_SMD_LOOPBACK); msleep(100); } /* * Wait for a channel to be allocated so we know * the modem is ready enough. */ if (smd_tty[n].open_wait) { res = wait_for_completion_interruptible_timeout( &info->ch_allocated, msecs_to_jiffies(smd_tty[n].open_wait * 1000)); if (res == 0) { SMD_TTY_INFO( "Timed out waiting for SMD channel %s", smd_tty[n].smd->port_name); res = -ETIMEDOUT; goto release_pil; } else if (res < 0) { SMD_TTY_INFO( "Error waiting for SMD channel %s : %d\n", smd_tty[n].smd->port_name, res); goto release_pil; } #ifdef CONFIG_MSM_SMD_TTY_DS_LEGACY /* * on boot, process tried to open smd0 sleeps until * modem is ready or timeout. */ if (n == DS_IDX) { /* wait for open ready status in seconds */ pr_info("%s: checking DS modem status\n", __func__); res = wait_event_interruptible_timeout( info->ch_opened_wait_queue, info->is_dsmodem_ready, (smd_tty_ds_modem_wait * HZ)); if (!res) { res = -ETIMEDOUT; pr_err("%s: timeout to wait for %s modem: %d\n", __func__, smd_tty[n].smd->port_name, res); goto release_pil; } else if (res < 0) { pr_err("%s: Error waiting for %s modem: %d\n", __func__, smd_tty[n].smd->port_name, res); goto release_pil; } pr_info("%s: DS modem is OK, open smd0..\n", __func__); } #endif } } tasklet_init(&info->tty_tsklt, smd_tty_read, (unsigned long)info); wake_lock_init(&info->wake_lock, WAKE_LOCK_SUSPEND, smd_tty[n].smd->port_name); scnprintf(info->ra_wake_lock_name, MAX_RA_WAKE_LOCK_NAME_LEN, "SMD_TTY_%s_RA", smd_tty[n].smd->port_name); wake_lock_init(&info->ra_wake_lock, WAKE_LOCK_SUSPEND, info->ra_wake_lock_name); res = smd_named_open_on_edge(smd_tty[n].smd->port_name, smd_tty[n].smd->edge, &info->ch, info, smd_tty_notify); if (res < 0) { SMD_TTY_INFO("%s: %s open failed %d\n", __func__, smd_tty[n].smd->port_name, res); goto release_wl_tl; } res = wait_event_interruptible_timeout(info->ch_opened_wait_queue, info->is_open, (2 * HZ)); if (res == 0) res = -ETIMEDOUT; if (res < 0) { SMD_TTY_INFO("%s: wait for %s smd_open failed %d\n", __func__, smd_tty[n].smd->port_name, res); goto close_ch; } SMD_TTY_INFO("%s with PID %u opened port %s", current->comm, current->pid, smd_tty[n].smd->port_name); smd_disable_read_intr(info->ch); mutex_unlock(&smd_tty_lock); return 0; close_ch: smd_close(info->ch); info->ch = NULL; release_wl_tl: tasklet_kill(&info->tty_tsklt); wake_lock_destroy(&info->wake_lock); wake_lock_destroy(&info->ra_wake_lock); release_pil: subsystem_put(info->pil); out: mutex_unlock(&smd_tty_lock); return res; }