Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* arch/arm/mach-msm/smd_tty.c |
| 2 | * |
| 3 | * Copyright (C) 2007 Google, Inc. |
Duy Truong | e833aca | 2013-02-12 13:35:08 -0800 | [diff] [blame] | 4 | * Copyright (c) 2009-2012, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 5 | * Author: Brian Swetland <swetland@google.com> |
| 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/cdev.h> |
| 21 | #include <linux/device.h> |
| 22 | #include <linux/interrupt.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/wakelock.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/sched.h> |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame] | 27 | #include <linux/pm_qos.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 28 | |
| 29 | #include <linux/tty.h> |
| 30 | #include <linux/tty_driver.h> |
| 31 | #include <linux/tty_flip.h> |
| 32 | |
| 33 | #include <mach/msm_smd.h> |
| 34 | #include <mach/peripheral-loader.h> |
Jeff Hugo | 4c0ba6c | 2011-07-15 11:47:13 -0600 | [diff] [blame] | 35 | #include <mach/socinfo.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 36 | |
| 37 | #include "smd_private.h" |
| 38 | |
| 39 | #define MAX_SMD_TTYS 37 |
| 40 | #define MAX_TTY_BUF_SIZE 2048 |
| 41 | |
| 42 | static DEFINE_MUTEX(smd_tty_lock); |
| 43 | |
| 44 | static uint smd_tty_modem_wait; |
| 45 | module_param_named(modem_wait, smd_tty_modem_wait, |
| 46 | uint, S_IRUGO | S_IWUSR | S_IWGRP); |
| 47 | |
| 48 | struct smd_tty_info { |
| 49 | smd_channel_t *ch; |
| 50 | struct tty_struct *tty; |
| 51 | struct wake_lock wake_lock; |
| 52 | int open_count; |
| 53 | struct tasklet_struct tty_tsklt; |
| 54 | struct timer_list buf_req_timer; |
| 55 | struct completion ch_allocated; |
| 56 | struct platform_driver driver; |
| 57 | void *pil; |
| 58 | int in_reset; |
| 59 | int in_reset_updated; |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 60 | int is_open; |
| 61 | wait_queue_head_t ch_opened_wait_queue; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 62 | spinlock_t reset_lock; |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 63 | struct smd_config *smd; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 66 | /** |
| 67 | * SMD port configuration. |
| 68 | * |
| 69 | * @tty_dev_index Index into smd_tty[] |
| 70 | * @port_name Name of the SMD port |
| 71 | * @dev_name Name of the TTY Device (if NULL, @port_name is used) |
| 72 | * @edge SMD edge |
| 73 | */ |
| 74 | struct smd_config { |
| 75 | uint32_t tty_dev_index; |
| 76 | const char *port_name; |
| 77 | const char *dev_name; |
| 78 | uint32_t edge; |
| 79 | }; |
| 80 | |
| 81 | static struct smd_config smd_configs[] = { |
| 82 | {0, "DS", NULL, SMD_APPS_MODEM}, |
| 83 | {1, "APPS_FM", NULL, SMD_APPS_WCNSS}, |
| 84 | {2, "APPS_RIVA_BT_ACL", NULL, SMD_APPS_WCNSS}, |
| 85 | {3, "APPS_RIVA_BT_CMD", NULL, SMD_APPS_WCNSS}, |
| 86 | {4, "MBALBRIDGE", NULL, SMD_APPS_MODEM}, |
Jeff Hugo | dfb9c9a | 2012-04-10 14:22:47 -0600 | [diff] [blame] | 87 | {5, "APPS_RIVA_ANT_CMD", NULL, SMD_APPS_WCNSS}, |
| 88 | {6, "APPS_RIVA_ANT_DATA", NULL, SMD_APPS_WCNSS}, |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 89 | {7, "DATA1", NULL, SMD_APPS_MODEM}, |
Eric Holmberg | be1dc5f | 2011-12-14 21:35:12 -0700 | [diff] [blame] | 90 | {11, "DATA11", NULL, SMD_APPS_MODEM}, |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 91 | {21, "DATA21", NULL, SMD_APPS_MODEM}, |
| 92 | {27, "GPSNMEA", NULL, SMD_APPS_MODEM}, |
| 93 | {36, "LOOPBACK", "LOOPBACK_TTY", SMD_APPS_MODEM}, |
| 94 | }; |
| 95 | #define DS_IDX 0 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 96 | #define LOOPBACK_IDX 36 |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame] | 97 | #define BT_ACL_IDX 2 |
| 98 | #define BT_CMD_IDX 3 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 99 | |
| 100 | static struct delayed_work loopback_work; |
| 101 | static struct smd_tty_info smd_tty[MAX_SMD_TTYS]; |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame] | 102 | static struct pm_qos_request smd_tty_qos_req; |
| 103 | static struct work_struct pm_qos_set_work; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 104 | |
| 105 | static int is_in_reset(struct smd_tty_info *info) |
| 106 | { |
| 107 | return info->in_reset; |
| 108 | } |
| 109 | |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame] | 110 | static void pm_qos_set_worker(struct work_struct *work) |
| 111 | { |
| 112 | /* keep the request for 500ms */ |
| 113 | pm_qos_update_request_timeout(&smd_tty_qos_req, |
| 114 | 0, jiffies_to_usecs(HZ / 2)); |
| 115 | } |
| 116 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 117 | static void buf_req_retry(unsigned long param) |
| 118 | { |
| 119 | struct smd_tty_info *info = (struct smd_tty_info *)param; |
Karthikeyan Ramasubramanian | f8ad413 | 2011-11-22 09:11:18 -0700 | [diff] [blame] | 120 | unsigned long flags; |
| 121 | |
| 122 | spin_lock_irqsave(&info->reset_lock, flags); |
| 123 | if (info->is_open) { |
| 124 | spin_unlock_irqrestore(&info->reset_lock, flags); |
| 125 | tasklet_hi_schedule(&info->tty_tsklt); |
| 126 | return; |
| 127 | } |
| 128 | spin_unlock_irqrestore(&info->reset_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static void smd_tty_read(unsigned long param) |
| 132 | { |
| 133 | unsigned char *ptr; |
| 134 | int avail; |
| 135 | struct smd_tty_info *info = (struct smd_tty_info *)param; |
| 136 | struct tty_struct *tty = info->tty; |
| 137 | |
| 138 | if (!tty) |
| 139 | return; |
| 140 | |
| 141 | for (;;) { |
Ajay Dudani | 1e7bcb7 | 2012-09-15 21:32:23 -0700 | [diff] [blame] | 142 | unsigned int n = info->tty->index; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 143 | if (is_in_reset(info)) { |
Ajay Dudani | 1e7bcb7 | 2012-09-15 21:32:23 -0700 | [diff] [blame] | 144 | if (n == BT_ACL_IDX || n == BT_CMD_IDX) |
| 145 | pr_err("%s: BT_IDX read in reset %d \n", __func__, n); |
| 146 | if ((n != BT_ACL_IDX) && (n != BT_CMD_IDX)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 147 | /* signal TTY clients using TTY_BREAK */ |
Ajay Dudani | 1e7bcb7 | 2012-09-15 21:32:23 -0700 | [diff] [blame] | 148 | tty_insert_flip_char(tty, 0x00, TTY_BREAK); |
| 149 | tty_flip_buffer_push(tty); |
| 150 | break; |
| 151 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | if (test_bit(TTY_THROTTLED, &tty->flags)) break; |
| 155 | avail = smd_read_avail(info->ch); |
| 156 | if (avail == 0) |
| 157 | break; |
| 158 | |
| 159 | if (avail > MAX_TTY_BUF_SIZE) |
| 160 | avail = MAX_TTY_BUF_SIZE; |
| 161 | |
| 162 | avail = tty_prepare_flip_string(tty, &ptr, avail); |
| 163 | if (avail <= 0) { |
Stephen Boyd | 49a1e88 | 2012-07-02 17:28:13 -0700 | [diff] [blame] | 164 | mod_timer(&info->buf_req_timer, |
| 165 | jiffies + msecs_to_jiffies(30)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 166 | return; |
| 167 | } |
| 168 | |
| 169 | if (smd_read(info->ch, ptr, avail) != avail) { |
| 170 | /* shouldn't be possible since we're in interrupt |
| 171 | ** context here and nobody else could 'steal' our |
| 172 | ** characters. |
| 173 | */ |
| 174 | printk(KERN_ERR "OOPS - smd_tty_buffer mismatch?!"); |
| 175 | } |
| 176 | |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame] | 177 | #ifdef CONFIG_HAS_WAKELOCK |
| 178 | pr_debug("%s: lock wakelock %s\n", __func__, info->wake_lock.name); |
| 179 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 180 | wake_lock_timeout(&info->wake_lock, HZ / 2); |
| 181 | tty_flip_buffer_push(tty); |
| 182 | } |
| 183 | |
| 184 | /* XXX only when writable and necessary */ |
| 185 | tty_wakeup(tty); |
| 186 | } |
| 187 | |
| 188 | static void smd_tty_notify(void *priv, unsigned event) |
| 189 | { |
| 190 | struct smd_tty_info *info = priv; |
| 191 | unsigned long flags; |
Ajay Dudani | 1e7bcb7 | 2012-09-15 21:32:23 -0700 | [diff] [blame] | 192 | unsigned char *ptr; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 193 | |
| 194 | switch (event) { |
| 195 | case SMD_EVENT_DATA: |
Karthikeyan Ramasubramanian | f8ad413 | 2011-11-22 09:11:18 -0700 | [diff] [blame] | 196 | spin_lock_irqsave(&info->reset_lock, flags); |
| 197 | if (!info->is_open) { |
| 198 | spin_unlock_irqrestore(&info->reset_lock, flags); |
| 199 | break; |
| 200 | } |
| 201 | spin_unlock_irqrestore(&info->reset_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 202 | /* There may be clients (tty framework) that are blocked |
| 203 | * waiting for space to write data, so if a possible read |
| 204 | * interrupt came in wake anyone waiting and disable the |
| 205 | * interrupts |
| 206 | */ |
| 207 | if (smd_write_avail(info->ch)) { |
| 208 | smd_disable_read_intr(info->ch); |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame] | 209 | if (info->tty) { |
| 210 | unsigned int n = info->tty->index; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 211 | wake_up_interruptible(&info->tty->write_wait); |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame] | 212 | |
| 213 | /* use pm_qos for BT performance */ |
| 214 | if (n == BT_ACL_IDX || n == BT_CMD_IDX) |
| 215 | schedule_work(&pm_qos_set_work); |
| 216 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 217 | } |
| 218 | tasklet_hi_schedule(&info->tty_tsklt); |
| 219 | break; |
| 220 | |
| 221 | case SMD_EVENT_OPEN: |
Ajay Dudani | 1e7bcb7 | 2012-09-15 21:32:23 -0700 | [diff] [blame] | 222 | if (is_in_reset(info)) { |
| 223 | unsigned int n = info->tty->index; |
| 224 | if (n == BT_CMD_IDX) { |
| 225 | pr_err("%s: BT_CMD_IDX Sending hardware error event to stack\n", __func__); |
| 226 | tty_prepare_flip_string(info->tty, &ptr, 0x03); |
| 227 | ptr[0] = 0x10; |
| 228 | ptr[1] = 0x01; |
| 229 | ptr[2] = 0x0A; |
| 230 | tty_flip_buffer_push(info->tty); |
| 231 | } |
| 232 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 233 | spin_lock_irqsave(&info->reset_lock, flags); |
| 234 | info->in_reset = 0; |
| 235 | info->in_reset_updated = 1; |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 236 | info->is_open = 1; |
| 237 | wake_up_interruptible(&info->ch_opened_wait_queue); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 238 | spin_unlock_irqrestore(&info->reset_lock, flags); |
Ajay Dudani | 1e7bcb7 | 2012-09-15 21:32:23 -0700 | [diff] [blame] | 239 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 240 | break; |
| 241 | |
| 242 | case SMD_EVENT_CLOSE: |
| 243 | spin_lock_irqsave(&info->reset_lock, flags); |
| 244 | info->in_reset = 1; |
| 245 | info->in_reset_updated = 1; |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 246 | info->is_open = 0; |
| 247 | wake_up_interruptible(&info->ch_opened_wait_queue); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 248 | spin_unlock_irqrestore(&info->reset_lock, flags); |
| 249 | /* schedule task to send TTY_BREAK */ |
| 250 | tasklet_hi_schedule(&info->tty_tsklt); |
| 251 | |
| 252 | if (info->tty->index == LOOPBACK_IDX) |
| 253 | schedule_delayed_work(&loopback_work, |
| 254 | msecs_to_jiffies(1000)); |
| 255 | break; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | static uint32_t is_modem_smsm_inited(void) |
| 260 | { |
| 261 | uint32_t modem_state; |
| 262 | uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT); |
| 263 | |
| 264 | modem_state = smsm_get_state(SMSM_MODEM_STATE); |
| 265 | return (modem_state & ready_state) == ready_state; |
| 266 | } |
| 267 | |
| 268 | static int smd_tty_open(struct tty_struct *tty, struct file *f) |
| 269 | { |
| 270 | int res = 0; |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 271 | unsigned int n = tty->index; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 272 | struct smd_tty_info *info; |
Eric Holmberg | a53cf23 | 2012-02-28 13:41:44 -0700 | [diff] [blame] | 273 | const char *peripheral = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 274 | |
| 275 | |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 276 | if (n >= MAX_SMD_TTYS || !smd_tty[n].smd) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 277 | return -ENODEV; |
| 278 | |
| 279 | info = smd_tty + n; |
| 280 | |
| 281 | mutex_lock(&smd_tty_lock); |
| 282 | tty->driver_data = info; |
| 283 | |
| 284 | if (info->open_count++ == 0) { |
Eric Holmberg | a53cf23 | 2012-02-28 13:41:44 -0700 | [diff] [blame] | 285 | peripheral = smd_edge_to_subsystem(smd_tty[n].smd->edge); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 286 | if (peripheral) { |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 287 | info->pil = pil_get(peripheral); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 288 | if (IS_ERR(info->pil)) { |
| 289 | res = PTR_ERR(info->pil); |
| 290 | goto out; |
| 291 | } |
| 292 | |
| 293 | /* Wait for the modem SMSM to be inited for the SMD |
| 294 | * Loopback channel to be allocated at the modem. Since |
| 295 | * the wait need to be done atmost once, using msleep |
| 296 | * doesn't degrade the performance. |
| 297 | */ |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 298 | if (n == LOOPBACK_IDX) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 299 | if (!is_modem_smsm_inited()) |
| 300 | msleep(5000); |
| 301 | smsm_change_state(SMSM_APPS_STATE, |
| 302 | 0, SMSM_SMD_LOOPBACK); |
| 303 | msleep(100); |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /* |
| 308 | * Wait for a channel to be allocated so we know |
| 309 | * the modem is ready enough. |
| 310 | */ |
| 311 | if (smd_tty_modem_wait) { |
| 312 | res = wait_for_completion_interruptible_timeout( |
| 313 | &info->ch_allocated, |
| 314 | msecs_to_jiffies(smd_tty_modem_wait * |
| 315 | 1000)); |
| 316 | |
| 317 | if (res == 0) { |
| 318 | pr_err("Timed out waiting for SMD" |
| 319 | " channel\n"); |
| 320 | res = -ETIMEDOUT; |
| 321 | goto release_pil; |
| 322 | } else if (res < 0) { |
| 323 | pr_err("Error waiting for SMD channel:" |
| 324 | " %d\n", |
| 325 | res); |
| 326 | goto release_pil; |
| 327 | } |
| 328 | |
| 329 | res = 0; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | |
| 334 | info->tty = tty; |
| 335 | tasklet_init(&info->tty_tsklt, smd_tty_read, |
| 336 | (unsigned long)info); |
| 337 | wake_lock_init(&info->wake_lock, WAKE_LOCK_SUSPEND, |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 338 | smd_tty[n].smd->port_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 339 | if (!info->ch) { |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 340 | res = smd_named_open_on_edge(smd_tty[n].smd->port_name, |
| 341 | smd_tty[n].smd->edge, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 342 | &info->ch, info, |
| 343 | smd_tty_notify); |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 344 | if (res < 0) { |
| 345 | pr_err("%s: %s open failed %d\n", __func__, |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 346 | smd_tty[n].smd->port_name, res); |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 347 | goto release_pil; |
| 348 | } |
| 349 | |
| 350 | res = wait_event_interruptible_timeout( |
| 351 | info->ch_opened_wait_queue, |
| 352 | info->is_open, (2 * HZ)); |
| 353 | if (res == 0) |
| 354 | res = -ETIMEDOUT; |
| 355 | if (res < 0) { |
| 356 | pr_err("%s: wait for %s smd_open failed %d\n", |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 357 | __func__, smd_tty[n].smd->port_name, |
| 358 | res); |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 359 | goto release_pil; |
| 360 | } |
| 361 | res = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | release_pil: |
| 366 | if (res < 0) |
| 367 | pil_put(info->pil); |
| 368 | else |
| 369 | smd_disable_read_intr(info->ch); |
| 370 | out: |
| 371 | mutex_unlock(&smd_tty_lock); |
| 372 | |
| 373 | return res; |
| 374 | } |
| 375 | |
| 376 | static void smd_tty_close(struct tty_struct *tty, struct file *f) |
| 377 | { |
| 378 | struct smd_tty_info *info = tty->driver_data; |
Karthikeyan Ramasubramanian | d563de5 | 2011-11-22 08:38:17 -0700 | [diff] [blame] | 379 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 380 | |
| 381 | if (info == 0) |
| 382 | return; |
| 383 | |
| 384 | mutex_lock(&smd_tty_lock); |
| 385 | if (--info->open_count == 0) { |
Karthikeyan Ramasubramanian | d563de5 | 2011-11-22 08:38:17 -0700 | [diff] [blame] | 386 | spin_lock_irqsave(&info->reset_lock, flags); |
| 387 | info->is_open = 0; |
| 388 | spin_unlock_irqrestore(&info->reset_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 389 | if (info->tty) { |
| 390 | tasklet_kill(&info->tty_tsklt); |
| 391 | wake_lock_destroy(&info->wake_lock); |
| 392 | info->tty = 0; |
| 393 | } |
| 394 | tty->driver_data = 0; |
| 395 | del_timer(&info->buf_req_timer); |
| 396 | if (info->ch) { |
| 397 | smd_close(info->ch); |
| 398 | info->ch = 0; |
| 399 | pil_put(info->pil); |
| 400 | } |
| 401 | } |
| 402 | mutex_unlock(&smd_tty_lock); |
| 403 | } |
| 404 | |
| 405 | static int smd_tty_write(struct tty_struct *tty, const unsigned char *buf, int len) |
| 406 | { |
| 407 | struct smd_tty_info *info = tty->driver_data; |
| 408 | int avail; |
| 409 | |
| 410 | /* if we're writing to a packet channel we will |
| 411 | ** never be able to write more data than there |
| 412 | ** is currently space for |
| 413 | */ |
| 414 | if (is_in_reset(info)) |
| 415 | return -ENETRESET; |
| 416 | |
| 417 | avail = smd_write_avail(info->ch); |
| 418 | /* if no space, we'll have to setup a notification later to wake up the |
| 419 | * tty framework when space becomes avaliable |
| 420 | */ |
| 421 | if (!avail) { |
| 422 | smd_enable_read_intr(info->ch); |
| 423 | return 0; |
| 424 | } |
| 425 | if (len > avail) |
| 426 | len = avail; |
| 427 | |
| 428 | return smd_write(info->ch, buf, len); |
| 429 | } |
| 430 | |
| 431 | static int smd_tty_write_room(struct tty_struct *tty) |
| 432 | { |
| 433 | struct smd_tty_info *info = tty->driver_data; |
| 434 | return smd_write_avail(info->ch); |
| 435 | } |
| 436 | |
| 437 | static int smd_tty_chars_in_buffer(struct tty_struct *tty) |
| 438 | { |
| 439 | struct smd_tty_info *info = tty->driver_data; |
| 440 | return smd_read_avail(info->ch); |
| 441 | } |
| 442 | |
| 443 | static void smd_tty_unthrottle(struct tty_struct *tty) |
| 444 | { |
| 445 | struct smd_tty_info *info = tty->driver_data; |
Karthikeyan Ramasubramanian | f8ad413 | 2011-11-22 09:11:18 -0700 | [diff] [blame] | 446 | unsigned long flags; |
| 447 | |
| 448 | spin_lock_irqsave(&info->reset_lock, flags); |
| 449 | if (info->is_open) { |
| 450 | spin_unlock_irqrestore(&info->reset_lock, flags); |
| 451 | tasklet_hi_schedule(&info->tty_tsklt); |
| 452 | return; |
| 453 | } |
| 454 | spin_unlock_irqrestore(&info->reset_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | /* |
| 458 | * Returns the current TIOCM status bits including: |
| 459 | * SMD Signals (DTR/DSR, CTS/RTS, CD, RI) |
| 460 | * TIOCM_OUT1 - reset state (1=in reset) |
| 461 | * TIOCM_OUT2 - reset state updated (1=updated) |
| 462 | */ |
| 463 | static int smd_tty_tiocmget(struct tty_struct *tty) |
| 464 | { |
| 465 | struct smd_tty_info *info = tty->driver_data; |
| 466 | unsigned long flags; |
| 467 | int tiocm; |
| 468 | |
| 469 | tiocm = smd_tiocmget(info->ch); |
| 470 | |
| 471 | spin_lock_irqsave(&info->reset_lock, flags); |
| 472 | tiocm |= (info->in_reset ? TIOCM_OUT1 : 0); |
| 473 | if (info->in_reset_updated) { |
| 474 | tiocm |= TIOCM_OUT2; |
| 475 | info->in_reset_updated = 0; |
| 476 | } |
| 477 | spin_unlock_irqrestore(&info->reset_lock, flags); |
| 478 | |
| 479 | return tiocm; |
| 480 | } |
| 481 | |
| 482 | static int smd_tty_tiocmset(struct tty_struct *tty, |
| 483 | unsigned int set, unsigned int clear) |
| 484 | { |
| 485 | struct smd_tty_info *info = tty->driver_data; |
| 486 | |
| 487 | if (info->in_reset) |
| 488 | return -ENETRESET; |
| 489 | |
| 490 | return smd_tiocmset(info->ch, set, clear); |
| 491 | } |
| 492 | |
| 493 | static void loopback_probe_worker(struct work_struct *work) |
| 494 | { |
| 495 | /* wait for modem to restart before requesting loopback server */ |
| 496 | if (!is_modem_smsm_inited()) |
| 497 | schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000)); |
| 498 | else |
| 499 | smsm_change_state(SMSM_APPS_STATE, |
| 500 | 0, SMSM_SMD_LOOPBACK); |
| 501 | } |
| 502 | |
| 503 | static struct tty_operations smd_tty_ops = { |
| 504 | .open = smd_tty_open, |
| 505 | .close = smd_tty_close, |
| 506 | .write = smd_tty_write, |
| 507 | .write_room = smd_tty_write_room, |
| 508 | .chars_in_buffer = smd_tty_chars_in_buffer, |
| 509 | .unthrottle = smd_tty_unthrottle, |
| 510 | .tiocmget = smd_tty_tiocmget, |
| 511 | .tiocmset = smd_tty_tiocmset, |
| 512 | }; |
| 513 | |
| 514 | static int smd_tty_dummy_probe(struct platform_device *pdev) |
| 515 | { |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 516 | int n; |
| 517 | int idx; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 518 | |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 519 | for (n = 0; n < ARRAY_SIZE(smd_configs); ++n) { |
| 520 | idx = smd_configs[n].tty_dev_index; |
| 521 | |
| 522 | if (!smd_configs[n].dev_name) |
| 523 | continue; |
| 524 | |
Eric Holmberg | daf36d1 | 2012-02-08 17:05:21 -0700 | [diff] [blame] | 525 | if (pdev->id == smd_configs[n].edge && |
| 526 | !strncmp(pdev->name, smd_configs[n].dev_name, |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 527 | SMD_MAX_CH_NAME_LEN)) { |
| 528 | complete_all(&smd_tty[idx].ch_allocated); |
| 529 | return 0; |
| 530 | } |
| 531 | } |
| 532 | pr_err("%s: unknown device '%s'\n", __func__, pdev->name); |
| 533 | |
| 534 | return -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | static struct tty_driver *smd_tty_driver; |
| 538 | |
| 539 | static int __init smd_tty_init(void) |
| 540 | { |
| 541 | int ret; |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 542 | int n; |
| 543 | int idx; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 544 | |
| 545 | smd_tty_driver = alloc_tty_driver(MAX_SMD_TTYS); |
| 546 | if (smd_tty_driver == 0) |
| 547 | return -ENOMEM; |
| 548 | |
| 549 | smd_tty_driver->owner = THIS_MODULE; |
| 550 | smd_tty_driver->driver_name = "smd_tty_driver"; |
| 551 | smd_tty_driver->name = "smd"; |
| 552 | smd_tty_driver->major = 0; |
| 553 | smd_tty_driver->minor_start = 0; |
| 554 | smd_tty_driver->type = TTY_DRIVER_TYPE_SERIAL; |
| 555 | smd_tty_driver->subtype = SERIAL_TYPE_NORMAL; |
| 556 | smd_tty_driver->init_termios = tty_std_termios; |
| 557 | smd_tty_driver->init_termios.c_iflag = 0; |
| 558 | smd_tty_driver->init_termios.c_oflag = 0; |
| 559 | smd_tty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD; |
| 560 | smd_tty_driver->init_termios.c_lflag = 0; |
| 561 | smd_tty_driver->flags = TTY_DRIVER_RESET_TERMIOS | |
| 562 | TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
| 563 | tty_set_operations(smd_tty_driver, &smd_tty_ops); |
| 564 | |
| 565 | ret = tty_register_driver(smd_tty_driver); |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 566 | if (ret) { |
| 567 | put_tty_driver(smd_tty_driver); |
| 568 | pr_err("%s: driver registration failed %d\n", __func__, ret); |
| 569 | return ret; |
Jeff Hugo | 4c0ba6c | 2011-07-15 11:47:13 -0600 | [diff] [blame] | 570 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 571 | |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 572 | for (n = 0; n < ARRAY_SIZE(smd_configs); ++n) { |
| 573 | idx = smd_configs[n].tty_dev_index; |
| 574 | |
| 575 | if (smd_configs[n].dev_name == NULL) |
| 576 | smd_configs[n].dev_name = smd_configs[n].port_name; |
| 577 | |
| 578 | if (idx == DS_IDX) { |
| 579 | /* |
| 580 | * DS port uses the kernel API starting with |
| 581 | * 8660 Fusion. Only register the userspace |
| 582 | * platform device for older targets. |
| 583 | */ |
| 584 | int legacy_ds = 0; |
| 585 | |
| 586 | legacy_ds |= cpu_is_msm7x01() || cpu_is_msm7x25(); |
| 587 | legacy_ds |= cpu_is_msm7x27() || cpu_is_msm7x30(); |
| 588 | legacy_ds |= cpu_is_qsd8x50() || cpu_is_msm8x55(); |
Angshuman Sarkar | 4b931ed | 2012-01-05 13:38:36 +0530 | [diff] [blame] | 589 | /* |
| 590 | * use legacy mode for 8660 Standalone (subtype 0) |
| 591 | */ |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 592 | legacy_ds |= cpu_is_msm8x60() && |
Angshuman Sarkar | 4b931ed | 2012-01-05 13:38:36 +0530 | [diff] [blame] | 593 | (socinfo_get_platform_subtype() == 0x0); |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 594 | |
| 595 | if (!legacy_ds) |
| 596 | continue; |
| 597 | } |
| 598 | |
| 599 | tty_register_device(smd_tty_driver, idx, 0); |
| 600 | init_completion(&smd_tty[idx].ch_allocated); |
| 601 | |
| 602 | /* register platform device */ |
| 603 | smd_tty[idx].driver.probe = smd_tty_dummy_probe; |
| 604 | smd_tty[idx].driver.driver.name = smd_configs[n].dev_name; |
| 605 | smd_tty[idx].driver.driver.owner = THIS_MODULE; |
| 606 | spin_lock_init(&smd_tty[idx].reset_lock); |
| 607 | smd_tty[idx].is_open = 0; |
Stephen Boyd | 49a1e88 | 2012-07-02 17:28:13 -0700 | [diff] [blame] | 608 | setup_timer(&smd_tty[idx].buf_req_timer, buf_req_retry, |
| 609 | (unsigned long)&smd_tty[idx]); |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 610 | init_waitqueue_head(&smd_tty[idx].ch_opened_wait_queue); |
| 611 | ret = platform_driver_register(&smd_tty[idx].driver); |
| 612 | |
| 613 | if (ret) { |
| 614 | pr_err("%s: init failed %d (%d)\n", __func__, idx, ret); |
| 615 | smd_tty[idx].driver.probe = NULL; |
| 616 | goto out; |
| 617 | } |
| 618 | smd_tty[idx].smd = &smd_configs[n]; |
| 619 | } |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame] | 620 | INIT_WORK(&pm_qos_set_work, pm_qos_set_worker); |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 621 | INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker); |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame] | 622 | pm_qos_add_request(&smd_tty_qos_req, PM_QOS_CPU_DMA_LATENCY, |
| 623 | PM_QOS_DEFAULT_VALUE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 624 | return 0; |
| 625 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 626 | out: |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 627 | /* unregister platform devices */ |
| 628 | for (n = 0; n < ARRAY_SIZE(smd_configs); ++n) { |
| 629 | idx = smd_configs[n].tty_dev_index; |
| 630 | |
| 631 | if (smd_tty[idx].driver.probe) { |
| 632 | platform_driver_unregister(&smd_tty[idx].driver); |
| 633 | tty_unregister_device(smd_tty_driver, idx); |
| 634 | } |
| 635 | } |
| 636 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 637 | tty_unregister_driver(smd_tty_driver); |
| 638 | put_tty_driver(smd_tty_driver); |
| 639 | return ret; |
| 640 | } |
| 641 | |
| 642 | module_init(smd_tty_init); |