blob: 5969a3c80228536bfac0aa2b725823680771389a [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* arch/arm/mach-msm/smd_tty.c
2 *
3 * Copyright (C) 2007 Google, Inc.
Karthikeyan Ramasubramanianc41fcf52013-01-29 17:45:15 -07004 * Copyright (c) 2009-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07005 * 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 Boyd77db8bb2012-06-27 15:15:16 -070033#include <mach/subsystem_restart.h>
Jeff Hugo4c0ba6c2011-07-15 11:47:13 -060034#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070035
36#include "smd_private.h"
37
38#define MAX_SMD_TTYS 37
39#define MAX_TTY_BUF_SIZE 2048
Karthikeyan Ramasubramanianc41fcf52013-01-29 17:45:15 -070040#define MAX_RA_WAKE_LOCK_NAME_LEN 32
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070041
42static DEFINE_MUTEX(smd_tty_lock);
43
44static uint smd_tty_modem_wait;
45module_param_named(modem_wait, smd_tty_modem_wait,
46 uint, S_IRUGO | S_IWUSR | S_IWGRP);
47
48struct smd_tty_info {
49 smd_channel_t *ch;
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -070050 struct tty_port port;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051 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 Ramasubramaniand5e63af2011-07-29 11:30:59 -060060 int is_open;
61 wait_queue_head_t ch_opened_wait_queue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062 spinlock_t reset_lock;
Karthikeyan Ramasubramanianc41fcf52013-01-29 17:45:15 -070063 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 Holmberg513ad582011-12-14 16:27:13 -070066 struct smd_config *smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067};
68
Eric Holmberg513ad582011-12-14 16:27:13 -070069/**
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 */
77struct smd_config {
78 uint32_t tty_dev_index;
79 const char *port_name;
80 const char *dev_name;
81 uint32_t edge;
82};
83
84static 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 Hugodfb9c9a2012-04-10 14:22:47 -060090 {5, "APPS_RIVA_ANT_CMD", NULL, SMD_APPS_WCNSS},
91 {6, "APPS_RIVA_ANT_DATA", NULL, SMD_APPS_WCNSS},
Eric Holmberg513ad582011-12-14 16:27:13 -070092 {7, "DATA1", NULL, SMD_APPS_MODEM},
Eric Holmbergbe1dc5f2011-12-14 21:35:12 -070093 {11, "DATA11", NULL, SMD_APPS_MODEM},
Eric Holmberg513ad582011-12-14 16:27:13 -070094 {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 Huntsman3f2bc4d2011-08-16 17:27:22 -070099#define LOOPBACK_IDX 36
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700100
101static struct delayed_work loopback_work;
102static struct smd_tty_info smd_tty[MAX_SMD_TTYS];
103
104static int is_in_reset(struct smd_tty_info *info)
105{
106 return info->in_reset;
107}
108
109static void buf_req_retry(unsigned long param)
110{
111 struct smd_tty_info *info = (struct smd_tty_info *)param;
Karthikeyan Ramasubramanianf8ad4132011-11-22 09:11:18 -0700112 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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700121}
122
123static 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;
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700128 struct tty_struct *tty = tty_port_tty_get(&info->port);
Karthikeyan Ramasubramanianc41fcf52013-01-29 17:45:15 -0700129 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130
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 Ramasubramanianc41fcf52013-01-29 17:45:15 -0700143 spin_lock_irqsave(&info->ra_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700144 avail = smd_read_avail(info->ch);
Karthikeyan Ramasubramanianc41fcf52013-01-29 17:45:15 -0700145 if (avail == 0) {
146 wake_unlock(&info->ra_wake_lock);
147 spin_unlock_irqrestore(&info->ra_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700148 break;
Karthikeyan Ramasubramanianc41fcf52013-01-29 17:45:15 -0700149 }
150 spin_unlock_irqrestore(&info->ra_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151
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 Boyd49a1e882012-07-02 17:28:13 -0700157 mod_timer(&info->buf_req_timer,
158 jiffies + msecs_to_jiffies(30));
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700159 tty_kref_put(tty);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160 return;
161 }
162
163 if (smd_read(info->ch, ptr, avail) != avail) {
164 /* shouldn't be possible since we're in interrupt
165 ** context here and nobody else could 'steal' our
166 ** characters.
167 */
168 printk(KERN_ERR "OOPS - smd_tty_buffer mismatch?!");
169 }
170
171 wake_lock_timeout(&info->wake_lock, HZ / 2);
172 tty_flip_buffer_push(tty);
173 }
174
175 /* XXX only when writable and necessary */
176 tty_wakeup(tty);
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700177 tty_kref_put(tty);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700178}
179
180static void smd_tty_notify(void *priv, unsigned event)
181{
182 struct smd_tty_info *info = priv;
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700183 struct tty_struct *tty;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184 unsigned long flags;
185
186 switch (event) {
187 case SMD_EVENT_DATA:
Karthikeyan Ramasubramanianf8ad4132011-11-22 09:11:18 -0700188 spin_lock_irqsave(&info->reset_lock, flags);
189 if (!info->is_open) {
190 spin_unlock_irqrestore(&info->reset_lock, flags);
191 break;
192 }
193 spin_unlock_irqrestore(&info->reset_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194 /* There may be clients (tty framework) that are blocked
195 * waiting for space to write data, so if a possible read
196 * interrupt came in wake anyone waiting and disable the
197 * interrupts
198 */
199 if (smd_write_avail(info->ch)) {
200 smd_disable_read_intr(info->ch);
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700201 tty = tty_port_tty_get(&info->port);
202 if (tty)
203 wake_up_interruptible(&tty->write_wait);
204 tty_kref_put(tty);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700205 }
Karthikeyan Ramasubramanianc41fcf52013-01-29 17:45:15 -0700206 spin_lock_irqsave(&info->ra_lock, flags);
207 if (smd_read_avail(info->ch)) {
208 wake_lock(&info->ra_wake_lock);
209 tasklet_hi_schedule(&info->tty_tsklt);
210 }
211 spin_unlock_irqrestore(&info->ra_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212 break;
213
214 case SMD_EVENT_OPEN:
215 spin_lock_irqsave(&info->reset_lock, flags);
216 info->in_reset = 0;
217 info->in_reset_updated = 1;
Karthikeyan Ramasubramaniand5e63af2011-07-29 11:30:59 -0600218 info->is_open = 1;
219 wake_up_interruptible(&info->ch_opened_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 spin_unlock_irqrestore(&info->reset_lock, flags);
221 break;
222
223 case SMD_EVENT_CLOSE:
224 spin_lock_irqsave(&info->reset_lock, flags);
225 info->in_reset = 1;
226 info->in_reset_updated = 1;
Karthikeyan Ramasubramaniand5e63af2011-07-29 11:30:59 -0600227 info->is_open = 0;
228 wake_up_interruptible(&info->ch_opened_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 spin_unlock_irqrestore(&info->reset_lock, flags);
230 /* schedule task to send TTY_BREAK */
231 tasklet_hi_schedule(&info->tty_tsklt);
232
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700233 tty = tty_port_tty_get(&info->port);
234 if (tty->index == LOOPBACK_IDX)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700235 schedule_delayed_work(&loopback_work,
236 msecs_to_jiffies(1000));
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700237 tty_kref_put(tty);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 break;
239 }
240}
241
242static uint32_t is_modem_smsm_inited(void)
243{
244 uint32_t modem_state;
245 uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT);
246
247 modem_state = smsm_get_state(SMSM_MODEM_STATE);
248 return (modem_state & ready_state) == ready_state;
249}
250
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700251static int smd_tty_port_activate(struct tty_port *tport,
252 struct tty_struct *tty)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700253{
254 int res = 0;
Eric Holmberg513ad582011-12-14 16:27:13 -0700255 unsigned int n = tty->index;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700256 struct smd_tty_info *info;
Eric Holmberga53cf232012-02-28 13:41:44 -0700257 const char *peripheral = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258
259
Eric Holmberg513ad582011-12-14 16:27:13 -0700260 if (n >= MAX_SMD_TTYS || !smd_tty[n].smd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700261 return -ENODEV;
262
263 info = smd_tty + n;
264
265 mutex_lock(&smd_tty_lock);
266 tty->driver_data = info;
267
268 if (info->open_count++ == 0) {
Eric Holmberga53cf232012-02-28 13:41:44 -0700269 peripheral = smd_edge_to_subsystem(smd_tty[n].smd->edge);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270 if (peripheral) {
Stephen Boyd77db8bb2012-06-27 15:15:16 -0700271 info->pil = subsystem_get(peripheral);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 if (IS_ERR(info->pil)) {
273 res = PTR_ERR(info->pil);
274 goto out;
275 }
276
277 /* Wait for the modem SMSM to be inited for the SMD
278 * Loopback channel to be allocated at the modem. Since
279 * the wait need to be done atmost once, using msleep
280 * doesn't degrade the performance.
281 */
Eric Holmberg513ad582011-12-14 16:27:13 -0700282 if (n == LOOPBACK_IDX) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 if (!is_modem_smsm_inited())
284 msleep(5000);
285 smsm_change_state(SMSM_APPS_STATE,
286 0, SMSM_SMD_LOOPBACK);
287 msleep(100);
288 }
289
290
291 /*
292 * Wait for a channel to be allocated so we know
293 * the modem is ready enough.
294 */
295 if (smd_tty_modem_wait) {
296 res = wait_for_completion_interruptible_timeout(
297 &info->ch_allocated,
298 msecs_to_jiffies(smd_tty_modem_wait *
299 1000));
300
301 if (res == 0) {
302 pr_err("Timed out waiting for SMD"
303 " channel\n");
304 res = -ETIMEDOUT;
305 goto release_pil;
306 } else if (res < 0) {
307 pr_err("Error waiting for SMD channel:"
308 " %d\n",
309 res);
310 goto release_pil;
311 }
312
313 res = 0;
314 }
315 }
316
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700317 tasklet_init(&info->tty_tsklt, smd_tty_read,
318 (unsigned long)info);
319 wake_lock_init(&info->wake_lock, WAKE_LOCK_SUSPEND,
Eric Holmberg513ad582011-12-14 16:27:13 -0700320 smd_tty[n].smd->port_name);
Karthikeyan Ramasubramanianc41fcf52013-01-29 17:45:15 -0700321 scnprintf(info->ra_wake_lock_name,
322 MAX_RA_WAKE_LOCK_NAME_LEN,
323 "SMD_TTY_%s_RA", smd_tty[n].smd->port_name);
324 wake_lock_init(&info->ra_wake_lock, WAKE_LOCK_SUSPEND,
325 info->ra_wake_lock_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700326 if (!info->ch) {
Eric Holmberg513ad582011-12-14 16:27:13 -0700327 res = smd_named_open_on_edge(smd_tty[n].smd->port_name,
328 smd_tty[n].smd->edge,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700329 &info->ch, info,
330 smd_tty_notify);
Karthikeyan Ramasubramaniand5e63af2011-07-29 11:30:59 -0600331 if (res < 0) {
332 pr_err("%s: %s open failed %d\n", __func__,
Eric Holmberg513ad582011-12-14 16:27:13 -0700333 smd_tty[n].smd->port_name, res);
Karthikeyan Ramasubramaniand5e63af2011-07-29 11:30:59 -0600334 goto release_pil;
335 }
336
337 res = wait_event_interruptible_timeout(
338 info->ch_opened_wait_queue,
339 info->is_open, (2 * HZ));
340 if (res == 0)
341 res = -ETIMEDOUT;
342 if (res < 0) {
343 pr_err("%s: wait for %s smd_open failed %d\n",
Eric Holmberg513ad582011-12-14 16:27:13 -0700344 __func__, smd_tty[n].smd->port_name,
345 res);
Karthikeyan Ramasubramaniand5e63af2011-07-29 11:30:59 -0600346 goto release_pil;
347 }
348 res = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700349 }
350 }
351
352release_pil:
353 if (res < 0)
Stephen Boyd77db8bb2012-06-27 15:15:16 -0700354 subsystem_put(info->pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355 else
356 smd_disable_read_intr(info->ch);
357out:
358 mutex_unlock(&smd_tty_lock);
359
360 return res;
361}
362
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700363static void smd_tty_port_shutdown(struct tty_port *tport)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364{
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700365 struct smd_tty_info *info;
366 struct tty_struct *tty = tty_port_tty_get(tport);
Karthikeyan Ramasubramaniand563de52011-11-22 08:38:17 -0700367 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700368
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700369 info = tty->driver_data;
370 if (info == 0) {
371 tty_kref_put(tty);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372 return;
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700373 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700374
375 mutex_lock(&smd_tty_lock);
376 if (--info->open_count == 0) {
Karthikeyan Ramasubramaniand563de52011-11-22 08:38:17 -0700377 spin_lock_irqsave(&info->reset_lock, flags);
378 info->is_open = 0;
379 spin_unlock_irqrestore(&info->reset_lock, flags);
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700380 if (tty) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700381 tasklet_kill(&info->tty_tsklt);
382 wake_lock_destroy(&info->wake_lock);
Jeff Hugo7f40a9c2013-02-15 15:34:39 -0700383 wake_lock_destroy(&info->ra_wake_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700384 }
385 tty->driver_data = 0;
386 del_timer(&info->buf_req_timer);
387 if (info->ch) {
388 smd_close(info->ch);
389 info->ch = 0;
Stephen Boyd77db8bb2012-06-27 15:15:16 -0700390 subsystem_put(info->pil);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391 }
392 }
393 mutex_unlock(&smd_tty_lock);
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700394 tty_kref_put(tty);
395}
396
397static int smd_tty_open(struct tty_struct *tty, struct file *f)
398{
399 struct smd_tty_info *info = smd_tty + tty->index;
400
401 return tty_port_open(&info->port, tty, f);
402}
403
404static void smd_tty_close(struct tty_struct *tty, struct file *f)
405{
406 struct smd_tty_info *info = tty->driver_data;
407
408 tty_port_close(&info->port, tty, f);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700409}
410
411static int smd_tty_write(struct tty_struct *tty, const unsigned char *buf, int len)
412{
413 struct smd_tty_info *info = tty->driver_data;
414 int avail;
415
416 /* if we're writing to a packet channel we will
417 ** never be able to write more data than there
418 ** is currently space for
419 */
420 if (is_in_reset(info))
421 return -ENETRESET;
422
423 avail = smd_write_avail(info->ch);
424 /* if no space, we'll have to setup a notification later to wake up the
425 * tty framework when space becomes avaliable
426 */
427 if (!avail) {
428 smd_enable_read_intr(info->ch);
429 return 0;
430 }
431 if (len > avail)
432 len = avail;
433
434 return smd_write(info->ch, buf, len);
435}
436
437static int smd_tty_write_room(struct tty_struct *tty)
438{
439 struct smd_tty_info *info = tty->driver_data;
440 return smd_write_avail(info->ch);
441}
442
443static int smd_tty_chars_in_buffer(struct tty_struct *tty)
444{
445 struct smd_tty_info *info = tty->driver_data;
446 return smd_read_avail(info->ch);
447}
448
449static void smd_tty_unthrottle(struct tty_struct *tty)
450{
451 struct smd_tty_info *info = tty->driver_data;
Karthikeyan Ramasubramanianf8ad4132011-11-22 09:11:18 -0700452 unsigned long flags;
453
454 spin_lock_irqsave(&info->reset_lock, flags);
455 if (info->is_open) {
456 spin_unlock_irqrestore(&info->reset_lock, flags);
457 tasklet_hi_schedule(&info->tty_tsklt);
458 return;
459 }
460 spin_unlock_irqrestore(&info->reset_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700461}
462
463/*
464 * Returns the current TIOCM status bits including:
465 * SMD Signals (DTR/DSR, CTS/RTS, CD, RI)
466 * TIOCM_OUT1 - reset state (1=in reset)
467 * TIOCM_OUT2 - reset state updated (1=updated)
468 */
469static int smd_tty_tiocmget(struct tty_struct *tty)
470{
471 struct smd_tty_info *info = tty->driver_data;
472 unsigned long flags;
473 int tiocm;
474
475 tiocm = smd_tiocmget(info->ch);
476
477 spin_lock_irqsave(&info->reset_lock, flags);
478 tiocm |= (info->in_reset ? TIOCM_OUT1 : 0);
479 if (info->in_reset_updated) {
480 tiocm |= TIOCM_OUT2;
481 info->in_reset_updated = 0;
482 }
483 spin_unlock_irqrestore(&info->reset_lock, flags);
484
485 return tiocm;
486}
487
488static int smd_tty_tiocmset(struct tty_struct *tty,
489 unsigned int set, unsigned int clear)
490{
491 struct smd_tty_info *info = tty->driver_data;
492
493 if (info->in_reset)
494 return -ENETRESET;
495
496 return smd_tiocmset(info->ch, set, clear);
497}
498
499static void loopback_probe_worker(struct work_struct *work)
500{
501 /* wait for modem to restart before requesting loopback server */
502 if (!is_modem_smsm_inited())
503 schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000));
504 else
505 smsm_change_state(SMSM_APPS_STATE,
506 0, SMSM_SMD_LOOPBACK);
507}
508
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700509static const struct tty_port_operations smd_tty_port_ops = {
510 .shutdown = smd_tty_port_shutdown,
511 .activate = smd_tty_port_activate,
512};
513
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700514static struct tty_operations smd_tty_ops = {
515 .open = smd_tty_open,
516 .close = smd_tty_close,
517 .write = smd_tty_write,
518 .write_room = smd_tty_write_room,
519 .chars_in_buffer = smd_tty_chars_in_buffer,
520 .unthrottle = smd_tty_unthrottle,
521 .tiocmget = smd_tty_tiocmget,
522 .tiocmset = smd_tty_tiocmset,
523};
524
525static int smd_tty_dummy_probe(struct platform_device *pdev)
526{
Eric Holmberg513ad582011-12-14 16:27:13 -0700527 int n;
528 int idx;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700529
Eric Holmberg513ad582011-12-14 16:27:13 -0700530 for (n = 0; n < ARRAY_SIZE(smd_configs); ++n) {
531 idx = smd_configs[n].tty_dev_index;
532
533 if (!smd_configs[n].dev_name)
534 continue;
535
Eric Holmbergdaf36d12012-02-08 17:05:21 -0700536 if (pdev->id == smd_configs[n].edge &&
537 !strncmp(pdev->name, smd_configs[n].dev_name,
Eric Holmberg513ad582011-12-14 16:27:13 -0700538 SMD_MAX_CH_NAME_LEN)) {
539 complete_all(&smd_tty[idx].ch_allocated);
540 return 0;
541 }
542 }
543 pr_err("%s: unknown device '%s'\n", __func__, pdev->name);
544
545 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700546}
547
548static struct tty_driver *smd_tty_driver;
549
550static int __init smd_tty_init(void)
551{
552 int ret;
Eric Holmberg513ad582011-12-14 16:27:13 -0700553 int n;
554 int idx;
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700555 struct tty_port *port;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700556
557 smd_tty_driver = alloc_tty_driver(MAX_SMD_TTYS);
558 if (smd_tty_driver == 0)
559 return -ENOMEM;
560
561 smd_tty_driver->owner = THIS_MODULE;
562 smd_tty_driver->driver_name = "smd_tty_driver";
563 smd_tty_driver->name = "smd";
564 smd_tty_driver->major = 0;
565 smd_tty_driver->minor_start = 0;
566 smd_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
567 smd_tty_driver->subtype = SERIAL_TYPE_NORMAL;
568 smd_tty_driver->init_termios = tty_std_termios;
569 smd_tty_driver->init_termios.c_iflag = 0;
570 smd_tty_driver->init_termios.c_oflag = 0;
571 smd_tty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
572 smd_tty_driver->init_termios.c_lflag = 0;
573 smd_tty_driver->flags = TTY_DRIVER_RESET_TERMIOS |
574 TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
575 tty_set_operations(smd_tty_driver, &smd_tty_ops);
576
577 ret = tty_register_driver(smd_tty_driver);
Eric Holmberg513ad582011-12-14 16:27:13 -0700578 if (ret) {
579 put_tty_driver(smd_tty_driver);
580 pr_err("%s: driver registration failed %d\n", __func__, ret);
581 return ret;
Jeff Hugo4c0ba6c2011-07-15 11:47:13 -0600582 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700583
Eric Holmberg513ad582011-12-14 16:27:13 -0700584 for (n = 0; n < ARRAY_SIZE(smd_configs); ++n) {
585 idx = smd_configs[n].tty_dev_index;
586
587 if (smd_configs[n].dev_name == NULL)
588 smd_configs[n].dev_name = smd_configs[n].port_name;
589
590 if (idx == DS_IDX) {
591 /*
592 * DS port uses the kernel API starting with
593 * 8660 Fusion. Only register the userspace
594 * platform device for older targets.
595 */
596 int legacy_ds = 0;
597
598 legacy_ds |= cpu_is_msm7x01() || cpu_is_msm7x25();
599 legacy_ds |= cpu_is_msm7x27() || cpu_is_msm7x30();
600 legacy_ds |= cpu_is_qsd8x50() || cpu_is_msm8x55();
Angshuman Sarkar4b931ed2012-01-05 13:38:36 +0530601 /*
602 * use legacy mode for 8660 Standalone (subtype 0)
603 */
Eric Holmberg513ad582011-12-14 16:27:13 -0700604 legacy_ds |= cpu_is_msm8x60() &&
Angshuman Sarkar4b931ed2012-01-05 13:38:36 +0530605 (socinfo_get_platform_subtype() == 0x0);
Eric Holmberg513ad582011-12-14 16:27:13 -0700606
607 if (!legacy_ds)
608 continue;
609 }
610
Karthikeyan Ramasubramanianfc37fea2013-02-14 12:32:49 -0700611 port = &smd_tty[idx].port;
612 tty_port_init(port);
613 port->ops = &smd_tty_port_ops;
614 /* TODO: For kernel >= 3.7 use tty_port_register_device */
Eric Holmberg513ad582011-12-14 16:27:13 -0700615 tty_register_device(smd_tty_driver, idx, 0);
616 init_completion(&smd_tty[idx].ch_allocated);
617
618 /* register platform device */
619 smd_tty[idx].driver.probe = smd_tty_dummy_probe;
620 smd_tty[idx].driver.driver.name = smd_configs[n].dev_name;
621 smd_tty[idx].driver.driver.owner = THIS_MODULE;
622 spin_lock_init(&smd_tty[idx].reset_lock);
Karthikeyan Ramasubramanianc41fcf52013-01-29 17:45:15 -0700623 spin_lock_init(&smd_tty[idx].ra_lock);
Eric Holmberg513ad582011-12-14 16:27:13 -0700624 smd_tty[idx].is_open = 0;
Stephen Boyd49a1e882012-07-02 17:28:13 -0700625 setup_timer(&smd_tty[idx].buf_req_timer, buf_req_retry,
626 (unsigned long)&smd_tty[idx]);
Eric Holmberg513ad582011-12-14 16:27:13 -0700627 init_waitqueue_head(&smd_tty[idx].ch_opened_wait_queue);
628 ret = platform_driver_register(&smd_tty[idx].driver);
629
630 if (ret) {
631 pr_err("%s: init failed %d (%d)\n", __func__, idx, ret);
632 smd_tty[idx].driver.probe = NULL;
633 goto out;
634 }
635 smd_tty[idx].smd = &smd_configs[n];
636 }
637 INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 return 0;
639
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700640out:
Eric Holmberg513ad582011-12-14 16:27:13 -0700641 /* unregister platform devices */
642 for (n = 0; n < ARRAY_SIZE(smd_configs); ++n) {
643 idx = smd_configs[n].tty_dev_index;
644
645 if (smd_tty[idx].driver.probe) {
646 platform_driver_unregister(&smd_tty[idx].driver);
647 tty_unregister_device(smd_tty_driver, idx);
648 }
649 }
650
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 tty_unregister_driver(smd_tty_driver);
652 put_tty_driver(smd_tty_driver);
653 return ret;
654}
655
656module_init(smd_tty_init);