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