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. |
Angshuman Sarkar | 4b931ed | 2012-01-05 13:38:36 +0530 | [diff] [blame] | 4 | * Copyright (c) 2009-2012, Code Aurora Forum. 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 (;;) { |
| 142 | if (is_in_reset(info)) { |
| 143 | /* signal TTY clients using TTY_BREAK */ |
| 144 | tty_insert_flip_char(tty, 0x00, TTY_BREAK); |
| 145 | tty_flip_buffer_push(tty); |
| 146 | break; |
| 147 | } |
| 148 | |
| 149 | if (test_bit(TTY_THROTTLED, &tty->flags)) break; |
| 150 | avail = smd_read_avail(info->ch); |
| 151 | if (avail == 0) |
| 152 | break; |
| 153 | |
| 154 | if (avail > MAX_TTY_BUF_SIZE) |
| 155 | avail = MAX_TTY_BUF_SIZE; |
| 156 | |
| 157 | avail = tty_prepare_flip_string(tty, &ptr, avail); |
| 158 | if (avail <= 0) { |
Stephen Boyd | 49a1e88 | 2012-07-02 17:28:13 -0700 | [diff] [blame] | 159 | mod_timer(&info->buf_req_timer, |
| 160 | jiffies + msecs_to_jiffies(30)); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 161 | return; |
| 162 | } |
| 163 | |
| 164 | if (smd_read(info->ch, ptr, avail) != avail) { |
| 165 | /* shouldn't be possible since we're in interrupt |
| 166 | ** context here and nobody else could 'steal' our |
| 167 | ** characters. |
| 168 | */ |
| 169 | printk(KERN_ERR "OOPS - smd_tty_buffer mismatch?!"); |
| 170 | } |
| 171 | |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame^] | 172 | #ifdef CONFIG_HAS_WAKELOCK |
| 173 | pr_debug("%s: lock wakelock %s\n", __func__, info->wake_lock.name); |
| 174 | #endif |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 175 | wake_lock_timeout(&info->wake_lock, HZ / 2); |
| 176 | tty_flip_buffer_push(tty); |
| 177 | } |
| 178 | |
| 179 | /* XXX only when writable and necessary */ |
| 180 | tty_wakeup(tty); |
| 181 | } |
| 182 | |
| 183 | static void smd_tty_notify(void *priv, unsigned event) |
| 184 | { |
| 185 | struct smd_tty_info *info = priv; |
| 186 | unsigned long flags; |
| 187 | |
| 188 | switch (event) { |
| 189 | case SMD_EVENT_DATA: |
Karthikeyan Ramasubramanian | f8ad413 | 2011-11-22 09:11:18 -0700 | [diff] [blame] | 190 | spin_lock_irqsave(&info->reset_lock, flags); |
| 191 | if (!info->is_open) { |
| 192 | spin_unlock_irqrestore(&info->reset_lock, flags); |
| 193 | break; |
| 194 | } |
| 195 | spin_unlock_irqrestore(&info->reset_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 196 | /* There may be clients (tty framework) that are blocked |
| 197 | * waiting for space to write data, so if a possible read |
| 198 | * interrupt came in wake anyone waiting and disable the |
| 199 | * interrupts |
| 200 | */ |
| 201 | if (smd_write_avail(info->ch)) { |
| 202 | smd_disable_read_intr(info->ch); |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame^] | 203 | if (info->tty) { |
| 204 | unsigned int n = info->tty->index; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 205 | wake_up_interruptible(&info->tty->write_wait); |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame^] | 206 | |
| 207 | /* use pm_qos for BT performance */ |
| 208 | if (n == BT_ACL_IDX || n == BT_CMD_IDX) |
| 209 | schedule_work(&pm_qos_set_work); |
| 210 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 211 | } |
| 212 | tasklet_hi_schedule(&info->tty_tsklt); |
| 213 | break; |
| 214 | |
| 215 | case SMD_EVENT_OPEN: |
| 216 | spin_lock_irqsave(&info->reset_lock, flags); |
| 217 | info->in_reset = 0; |
| 218 | info->in_reset_updated = 1; |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 219 | info->is_open = 1; |
| 220 | wake_up_interruptible(&info->ch_opened_wait_queue); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 221 | spin_unlock_irqrestore(&info->reset_lock, flags); |
| 222 | break; |
| 223 | |
| 224 | case SMD_EVENT_CLOSE: |
| 225 | spin_lock_irqsave(&info->reset_lock, flags); |
| 226 | info->in_reset = 1; |
| 227 | info->in_reset_updated = 1; |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 228 | info->is_open = 0; |
| 229 | wake_up_interruptible(&info->ch_opened_wait_queue); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 230 | spin_unlock_irqrestore(&info->reset_lock, flags); |
| 231 | /* schedule task to send TTY_BREAK */ |
| 232 | tasklet_hi_schedule(&info->tty_tsklt); |
| 233 | |
| 234 | if (info->tty->index == LOOPBACK_IDX) |
| 235 | schedule_delayed_work(&loopback_work, |
| 236 | msecs_to_jiffies(1000)); |
| 237 | break; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static uint32_t is_modem_smsm_inited(void) |
| 242 | { |
| 243 | uint32_t modem_state; |
| 244 | uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT); |
| 245 | |
| 246 | modem_state = smsm_get_state(SMSM_MODEM_STATE); |
| 247 | return (modem_state & ready_state) == ready_state; |
| 248 | } |
| 249 | |
| 250 | static int smd_tty_open(struct tty_struct *tty, struct file *f) |
| 251 | { |
| 252 | int res = 0; |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 253 | unsigned int n = tty->index; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 254 | struct smd_tty_info *info; |
Eric Holmberg | a53cf23 | 2012-02-28 13:41:44 -0700 | [diff] [blame] | 255 | const char *peripheral = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 256 | |
| 257 | |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 258 | if (n >= MAX_SMD_TTYS || !smd_tty[n].smd) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | return -ENODEV; |
| 260 | |
| 261 | info = smd_tty + n; |
| 262 | |
| 263 | mutex_lock(&smd_tty_lock); |
| 264 | tty->driver_data = info; |
| 265 | |
| 266 | if (info->open_count++ == 0) { |
Eric Holmberg | a53cf23 | 2012-02-28 13:41:44 -0700 | [diff] [blame] | 267 | peripheral = smd_edge_to_subsystem(smd_tty[n].smd->edge); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 268 | if (peripheral) { |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 269 | info->pil = pil_get(peripheral); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 270 | if (IS_ERR(info->pil)) { |
| 271 | res = PTR_ERR(info->pil); |
| 272 | goto out; |
| 273 | } |
| 274 | |
| 275 | /* Wait for the modem SMSM to be inited for the SMD |
| 276 | * Loopback channel to be allocated at the modem. Since |
| 277 | * the wait need to be done atmost once, using msleep |
| 278 | * doesn't degrade the performance. |
| 279 | */ |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 280 | if (n == LOOPBACK_IDX) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 281 | if (!is_modem_smsm_inited()) |
| 282 | msleep(5000); |
| 283 | smsm_change_state(SMSM_APPS_STATE, |
| 284 | 0, SMSM_SMD_LOOPBACK); |
| 285 | msleep(100); |
| 286 | } |
| 287 | |
| 288 | |
| 289 | /* |
| 290 | * Wait for a channel to be allocated so we know |
| 291 | * the modem is ready enough. |
| 292 | */ |
| 293 | if (smd_tty_modem_wait) { |
| 294 | res = wait_for_completion_interruptible_timeout( |
| 295 | &info->ch_allocated, |
| 296 | msecs_to_jiffies(smd_tty_modem_wait * |
| 297 | 1000)); |
| 298 | |
| 299 | if (res == 0) { |
| 300 | pr_err("Timed out waiting for SMD" |
| 301 | " channel\n"); |
| 302 | res = -ETIMEDOUT; |
| 303 | goto release_pil; |
| 304 | } else if (res < 0) { |
| 305 | pr_err("Error waiting for SMD channel:" |
| 306 | " %d\n", |
| 307 | res); |
| 308 | goto release_pil; |
| 309 | } |
| 310 | |
| 311 | res = 0; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | |
| 316 | info->tty = tty; |
| 317 | tasklet_init(&info->tty_tsklt, smd_tty_read, |
| 318 | (unsigned long)info); |
| 319 | wake_lock_init(&info->wake_lock, WAKE_LOCK_SUSPEND, |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 320 | smd_tty[n].smd->port_name); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 321 | if (!info->ch) { |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 322 | res = smd_named_open_on_edge(smd_tty[n].smd->port_name, |
| 323 | smd_tty[n].smd->edge, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 324 | &info->ch, info, |
| 325 | smd_tty_notify); |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 326 | if (res < 0) { |
| 327 | pr_err("%s: %s open failed %d\n", __func__, |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 328 | smd_tty[n].smd->port_name, res); |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 329 | goto release_pil; |
| 330 | } |
| 331 | |
| 332 | res = wait_event_interruptible_timeout( |
| 333 | info->ch_opened_wait_queue, |
| 334 | info->is_open, (2 * HZ)); |
| 335 | if (res == 0) |
| 336 | res = -ETIMEDOUT; |
| 337 | if (res < 0) { |
| 338 | pr_err("%s: wait for %s smd_open failed %d\n", |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 339 | __func__, smd_tty[n].smd->port_name, |
| 340 | res); |
Karthikeyan Ramasubramanian | d5e63af | 2011-07-29 11:30:59 -0600 | [diff] [blame] | 341 | goto release_pil; |
| 342 | } |
| 343 | res = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
| 347 | release_pil: |
| 348 | if (res < 0) |
| 349 | pil_put(info->pil); |
| 350 | else |
| 351 | smd_disable_read_intr(info->ch); |
| 352 | out: |
| 353 | mutex_unlock(&smd_tty_lock); |
| 354 | |
| 355 | return res; |
| 356 | } |
| 357 | |
| 358 | static void smd_tty_close(struct tty_struct *tty, struct file *f) |
| 359 | { |
| 360 | struct smd_tty_info *info = tty->driver_data; |
Karthikeyan Ramasubramanian | d563de5 | 2011-11-22 08:38:17 -0700 | [diff] [blame] | 361 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 362 | |
| 363 | if (info == 0) |
| 364 | return; |
| 365 | |
| 366 | mutex_lock(&smd_tty_lock); |
| 367 | if (--info->open_count == 0) { |
Karthikeyan Ramasubramanian | d563de5 | 2011-11-22 08:38:17 -0700 | [diff] [blame] | 368 | spin_lock_irqsave(&info->reset_lock, flags); |
| 369 | info->is_open = 0; |
| 370 | spin_unlock_irqrestore(&info->reset_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 371 | if (info->tty) { |
| 372 | tasklet_kill(&info->tty_tsklt); |
| 373 | wake_lock_destroy(&info->wake_lock); |
| 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; |
| 381 | pil_put(info->pil); |
| 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); |
| 589 | smd_tty[idx].is_open = 0; |
Stephen Boyd | 49a1e88 | 2012-07-02 17:28:13 -0700 | [diff] [blame] | 590 | setup_timer(&smd_tty[idx].buf_req_timer, buf_req_retry, |
| 591 | (unsigned long)&smd_tty[idx]); |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 592 | init_waitqueue_head(&smd_tty[idx].ch_opened_wait_queue); |
| 593 | ret = platform_driver_register(&smd_tty[idx].driver); |
| 594 | |
| 595 | if (ret) { |
| 596 | pr_err("%s: init failed %d (%d)\n", __func__, idx, ret); |
| 597 | smd_tty[idx].driver.probe = NULL; |
| 598 | goto out; |
| 599 | } |
| 600 | smd_tty[idx].smd = &smd_configs[n]; |
| 601 | } |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame^] | 602 | INIT_WORK(&pm_qos_set_work, pm_qos_set_worker); |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 603 | INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker); |
Devin Kim | 36b2b8c | 2012-08-29 23:21:22 -0700 | [diff] [blame^] | 604 | pm_qos_add_request(&smd_tty_qos_req, PM_QOS_CPU_DMA_LATENCY, |
| 605 | PM_QOS_DEFAULT_VALUE); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 606 | return 0; |
| 607 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 608 | out: |
Eric Holmberg | 513ad58 | 2011-12-14 16:27:13 -0700 | [diff] [blame] | 609 | /* unregister platform devices */ |
| 610 | for (n = 0; n < ARRAY_SIZE(smd_configs); ++n) { |
| 611 | idx = smd_configs[n].tty_dev_index; |
| 612 | |
| 613 | if (smd_tty[idx].driver.probe) { |
| 614 | platform_driver_unregister(&smd_tty[idx].driver); |
| 615 | tty_unregister_device(smd_tty_driver, idx); |
| 616 | } |
| 617 | } |
| 618 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 619 | tty_unregister_driver(smd_tty_driver); |
| 620 | put_tty_driver(smd_tty_driver); |
| 621 | return ret; |
| 622 | } |
| 623 | |
| 624 | module_init(smd_tty_init); |