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