blob: c7962afca2075626924e46d1613c3bd9798054a9 [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.
Duy Truonge833aca2013-02-12 13:35:08 -08004 * Copyright (c) 2009-2012, 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>
Devin Kim36b2b8c2012-08-29 23:21:22 -070027#include <linux/pm_qos.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028
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 Hugo4c0ba6c2011-07-15 11:47:13 -060035#include <mach/socinfo.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036
37#include "smd_private.h"
38
39#define MAX_SMD_TTYS 37
40#define MAX_TTY_BUF_SIZE 2048
41
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;
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 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;
Eric Holmberg513ad582011-12-14 16:27:13 -070063 struct smd_config *smd;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064};
65
Eric Holmberg513ad582011-12-14 16:27:13 -070066/**
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 */
74struct smd_config {
75 uint32_t tty_dev_index;
76 const char *port_name;
77 const char *dev_name;
78 uint32_t edge;
79};
80
81static 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 Hugodfb9c9a2012-04-10 14:22:47 -060087 {5, "APPS_RIVA_ANT_CMD", NULL, SMD_APPS_WCNSS},
88 {6, "APPS_RIVA_ANT_DATA", NULL, SMD_APPS_WCNSS},
Eric Holmberg513ad582011-12-14 16:27:13 -070089 {7, "DATA1", NULL, SMD_APPS_MODEM},
Eric Holmbergbe1dc5f2011-12-14 21:35:12 -070090 {11, "DATA11", NULL, SMD_APPS_MODEM},
Eric Holmberg513ad582011-12-14 16:27:13 -070091 {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 Huntsman3f2bc4d2011-08-16 17:27:22 -070096#define LOOPBACK_IDX 36
Devin Kim36b2b8c2012-08-29 23:21:22 -070097#define BT_ACL_IDX 2
98#define BT_CMD_IDX 3
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070099
100static struct delayed_work loopback_work;
101static struct smd_tty_info smd_tty[MAX_SMD_TTYS];
Devin Kim36b2b8c2012-08-29 23:21:22 -0700102static struct pm_qos_request smd_tty_qos_req;
103static struct work_struct pm_qos_set_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104
105static int is_in_reset(struct smd_tty_info *info)
106{
107 return info->in_reset;
108}
109
Devin Kim36b2b8c2012-08-29 23:21:22 -0700110static 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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700117static void buf_req_retry(unsigned long param)
118{
119 struct smd_tty_info *info = (struct smd_tty_info *)param;
Karthikeyan Ramasubramanianf8ad4132011-11-22 09:11:18 -0700120 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 Huntsman3f2bc4d2011-08-16 17:27:22 -0700129}
130
131static void smd_tty_read(unsigned long param)
132{
133 unsigned char *ptr;
134 int avail;
135 struct smd_tty_info *info = (struct smd_tty_info *)param;
136 struct tty_struct *tty = info->tty;
137
138 if (!tty)
139 return;
140
141 for (;;) {
Ajay Dudani1e7bcb72012-09-15 21:32:23 -0700142 unsigned int n = info->tty->index;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 if (is_in_reset(info)) {
Ajay Dudani1e7bcb72012-09-15 21:32:23 -0700144 if (n == BT_ACL_IDX || n == BT_CMD_IDX)
145 pr_err("%s: BT_IDX read in reset %d \n", __func__, n);
146 if ((n != BT_ACL_IDX) && (n != BT_CMD_IDX)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700147 /* signal TTY clients using TTY_BREAK */
Ajay Dudani1e7bcb72012-09-15 21:32:23 -0700148 tty_insert_flip_char(tty, 0x00, TTY_BREAK);
149 tty_flip_buffer_push(tty);
150 break;
151 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152 }
153
154 if (test_bit(TTY_THROTTLED, &tty->flags)) break;
155 avail = smd_read_avail(info->ch);
156 if (avail == 0)
157 break;
158
159 if (avail > MAX_TTY_BUF_SIZE)
160 avail = MAX_TTY_BUF_SIZE;
161
162 avail = tty_prepare_flip_string(tty, &ptr, avail);
163 if (avail <= 0) {
Stephen Boyd49a1e882012-07-02 17:28:13 -0700164 mod_timer(&info->buf_req_timer,
165 jiffies + msecs_to_jiffies(30));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700166 return;
167 }
168
169 if (smd_read(info->ch, ptr, avail) != avail) {
170 /* shouldn't be possible since we're in interrupt
171 ** context here and nobody else could 'steal' our
172 ** characters.
173 */
174 printk(KERN_ERR "OOPS - smd_tty_buffer mismatch?!");
175 }
176
Devin Kim36b2b8c2012-08-29 23:21:22 -0700177#ifdef CONFIG_HAS_WAKELOCK
178 pr_debug("%s: lock wakelock %s\n", __func__, info->wake_lock.name);
179#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180 wake_lock_timeout(&info->wake_lock, HZ / 2);
181 tty_flip_buffer_push(tty);
182 }
183
184 /* XXX only when writable and necessary */
185 tty_wakeup(tty);
186}
187
188static void smd_tty_notify(void *priv, unsigned event)
189{
190 struct smd_tty_info *info = priv;
191 unsigned long flags;
Ajay Dudani1e7bcb72012-09-15 21:32:23 -0700192 unsigned char *ptr;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193
194 switch (event) {
195 case SMD_EVENT_DATA:
Karthikeyan Ramasubramanianf8ad4132011-11-22 09:11:18 -0700196 spin_lock_irqsave(&info->reset_lock, flags);
197 if (!info->is_open) {
198 spin_unlock_irqrestore(&info->reset_lock, flags);
199 break;
200 }
201 spin_unlock_irqrestore(&info->reset_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 /* There may be clients (tty framework) that are blocked
203 * waiting for space to write data, so if a possible read
204 * interrupt came in wake anyone waiting and disable the
205 * interrupts
206 */
207 if (smd_write_avail(info->ch)) {
208 smd_disable_read_intr(info->ch);
Devin Kim36b2b8c2012-08-29 23:21:22 -0700209 if (info->tty) {
210 unsigned int n = info->tty->index;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211 wake_up_interruptible(&info->tty->write_wait);
Devin Kim36b2b8c2012-08-29 23:21:22 -0700212
213 /* use pm_qos for BT performance */
214 if (n == BT_ACL_IDX || n == BT_CMD_IDX)
215 schedule_work(&pm_qos_set_work);
216 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700217 }
218 tasklet_hi_schedule(&info->tty_tsklt);
219 break;
220
221 case SMD_EVENT_OPEN:
Ajay Dudani1e7bcb72012-09-15 21:32:23 -0700222 if (is_in_reset(info)) {
223 unsigned int n = info->tty->index;
224 if (n == BT_CMD_IDX) {
225 pr_err("%s: BT_CMD_IDX Sending hardware error event to stack\n", __func__);
226 tty_prepare_flip_string(info->tty, &ptr, 0x03);
227 ptr[0] = 0x10;
228 ptr[1] = 0x01;
229 ptr[2] = 0x0A;
230 tty_flip_buffer_push(info->tty);
231 }
232 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 spin_lock_irqsave(&info->reset_lock, flags);
234 info->in_reset = 0;
235 info->in_reset_updated = 1;
Karthikeyan Ramasubramaniand5e63af2011-07-29 11:30:59 -0600236 info->is_open = 1;
237 wake_up_interruptible(&info->ch_opened_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700238 spin_unlock_irqrestore(&info->reset_lock, flags);
Ajay Dudani1e7bcb72012-09-15 21:32:23 -0700239
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 break;
241
242 case SMD_EVENT_CLOSE:
243 spin_lock_irqsave(&info->reset_lock, flags);
244 info->in_reset = 1;
245 info->in_reset_updated = 1;
Karthikeyan Ramasubramaniand5e63af2011-07-29 11:30:59 -0600246 info->is_open = 0;
247 wake_up_interruptible(&info->ch_opened_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700248 spin_unlock_irqrestore(&info->reset_lock, flags);
249 /* schedule task to send TTY_BREAK */
250 tasklet_hi_schedule(&info->tty_tsklt);
251
252 if (info->tty->index == LOOPBACK_IDX)
253 schedule_delayed_work(&loopback_work,
254 msecs_to_jiffies(1000));
255 break;
256 }
257}
258
259static uint32_t is_modem_smsm_inited(void)
260{
261 uint32_t modem_state;
262 uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT);
263
264 modem_state = smsm_get_state(SMSM_MODEM_STATE);
265 return (modem_state & ready_state) == ready_state;
266}
267
268static int smd_tty_open(struct tty_struct *tty, struct file *f)
269{
270 int res = 0;
Eric Holmberg513ad582011-12-14 16:27:13 -0700271 unsigned int n = tty->index;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700272 struct smd_tty_info *info;
Eric Holmberga53cf232012-02-28 13:41:44 -0700273 const char *peripheral = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274
275
Eric Holmberg513ad582011-12-14 16:27:13 -0700276 if (n >= MAX_SMD_TTYS || !smd_tty[n].smd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277 return -ENODEV;
278
279 info = smd_tty + n;
280
281 mutex_lock(&smd_tty_lock);
282 tty->driver_data = info;
283
284 if (info->open_count++ == 0) {
Eric Holmberga53cf232012-02-28 13:41:44 -0700285 peripheral = smd_edge_to_subsystem(smd_tty[n].smd->edge);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700286 if (peripheral) {
Eric Holmberg513ad582011-12-14 16:27:13 -0700287 info->pil = pil_get(peripheral);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700288 if (IS_ERR(info->pil)) {
289 res = PTR_ERR(info->pil);
290 goto out;
291 }
292
293 /* Wait for the modem SMSM to be inited for the SMD
294 * Loopback channel to be allocated at the modem. Since
295 * the wait need to be done atmost once, using msleep
296 * doesn't degrade the performance.
297 */
Eric Holmberg513ad582011-12-14 16:27:13 -0700298 if (n == LOOPBACK_IDX) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700299 if (!is_modem_smsm_inited())
300 msleep(5000);
301 smsm_change_state(SMSM_APPS_STATE,
302 0, SMSM_SMD_LOOPBACK);
303 msleep(100);
304 }
305
306
307 /*
308 * Wait for a channel to be allocated so we know
309 * the modem is ready enough.
310 */
311 if (smd_tty_modem_wait) {
312 res = wait_for_completion_interruptible_timeout(
313 &info->ch_allocated,
314 msecs_to_jiffies(smd_tty_modem_wait *
315 1000));
316
317 if (res == 0) {
318 pr_err("Timed out waiting for SMD"
319 " channel\n");
320 res = -ETIMEDOUT;
321 goto release_pil;
322 } else if (res < 0) {
323 pr_err("Error waiting for SMD channel:"
324 " %d\n",
325 res);
326 goto release_pil;
327 }
328
329 res = 0;
330 }
331 }
332
333
334 info->tty = tty;
335 tasklet_init(&info->tty_tsklt, smd_tty_read,
336 (unsigned long)info);
337 wake_lock_init(&info->wake_lock, WAKE_LOCK_SUSPEND,
Eric Holmberg513ad582011-12-14 16:27:13 -0700338 smd_tty[n].smd->port_name);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700339 if (!info->ch) {
Eric Holmberg513ad582011-12-14 16:27:13 -0700340 res = smd_named_open_on_edge(smd_tty[n].smd->port_name,
341 smd_tty[n].smd->edge,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700342 &info->ch, info,
343 smd_tty_notify);
Karthikeyan Ramasubramaniand5e63af2011-07-29 11:30:59 -0600344 if (res < 0) {
345 pr_err("%s: %s open failed %d\n", __func__,
Eric Holmberg513ad582011-12-14 16:27:13 -0700346 smd_tty[n].smd->port_name, res);
Karthikeyan Ramasubramaniand5e63af2011-07-29 11:30:59 -0600347 goto release_pil;
348 }
349
350 res = wait_event_interruptible_timeout(
351 info->ch_opened_wait_queue,
352 info->is_open, (2 * HZ));
353 if (res == 0)
354 res = -ETIMEDOUT;
355 if (res < 0) {
356 pr_err("%s: wait for %s smd_open failed %d\n",
Eric Holmberg513ad582011-12-14 16:27:13 -0700357 __func__, smd_tty[n].smd->port_name,
358 res);
Karthikeyan Ramasubramaniand5e63af2011-07-29 11:30:59 -0600359 goto release_pil;
360 }
361 res = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700362 }
363 }
364
365release_pil:
366 if (res < 0)
367 pil_put(info->pil);
368 else
369 smd_disable_read_intr(info->ch);
370out:
371 mutex_unlock(&smd_tty_lock);
372
373 return res;
374}
375
376static void smd_tty_close(struct tty_struct *tty, struct file *f)
377{
378 struct smd_tty_info *info = tty->driver_data;
Karthikeyan Ramasubramaniand563de52011-11-22 08:38:17 -0700379 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700380
381 if (info == 0)
382 return;
383
384 mutex_lock(&smd_tty_lock);
385 if (--info->open_count == 0) {
Karthikeyan Ramasubramaniand563de52011-11-22 08:38:17 -0700386 spin_lock_irqsave(&info->reset_lock, flags);
387 info->is_open = 0;
388 spin_unlock_irqrestore(&info->reset_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389 if (info->tty) {
390 tasklet_kill(&info->tty_tsklt);
391 wake_lock_destroy(&info->wake_lock);
392 info->tty = 0;
393 }
394 tty->driver_data = 0;
395 del_timer(&info->buf_req_timer);
396 if (info->ch) {
397 smd_close(info->ch);
398 info->ch = 0;
399 pil_put(info->pil);
400 }
401 }
402 mutex_unlock(&smd_tty_lock);
403}
404
405static int smd_tty_write(struct tty_struct *tty, const unsigned char *buf, int len)
406{
407 struct smd_tty_info *info = tty->driver_data;
408 int avail;
409
410 /* if we're writing to a packet channel we will
411 ** never be able to write more data than there
412 ** is currently space for
413 */
414 if (is_in_reset(info))
415 return -ENETRESET;
416
417 avail = smd_write_avail(info->ch);
418 /* if no space, we'll have to setup a notification later to wake up the
419 * tty framework when space becomes avaliable
420 */
421 if (!avail) {
422 smd_enable_read_intr(info->ch);
423 return 0;
424 }
425 if (len > avail)
426 len = avail;
427
428 return smd_write(info->ch, buf, len);
429}
430
431static int smd_tty_write_room(struct tty_struct *tty)
432{
433 struct smd_tty_info *info = tty->driver_data;
434 return smd_write_avail(info->ch);
435}
436
437static int smd_tty_chars_in_buffer(struct tty_struct *tty)
438{
439 struct smd_tty_info *info = tty->driver_data;
440 return smd_read_avail(info->ch);
441}
442
443static void smd_tty_unthrottle(struct tty_struct *tty)
444{
445 struct smd_tty_info *info = tty->driver_data;
Karthikeyan Ramasubramanianf8ad4132011-11-22 09:11:18 -0700446 unsigned long flags;
447
448 spin_lock_irqsave(&info->reset_lock, flags);
449 if (info->is_open) {
450 spin_unlock_irqrestore(&info->reset_lock, flags);
451 tasklet_hi_schedule(&info->tty_tsklt);
452 return;
453 }
454 spin_unlock_irqrestore(&info->reset_lock, flags);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700455}
456
457/*
458 * Returns the current TIOCM status bits including:
459 * SMD Signals (DTR/DSR, CTS/RTS, CD, RI)
460 * TIOCM_OUT1 - reset state (1=in reset)
461 * TIOCM_OUT2 - reset state updated (1=updated)
462 */
463static int smd_tty_tiocmget(struct tty_struct *tty)
464{
465 struct smd_tty_info *info = tty->driver_data;
466 unsigned long flags;
467 int tiocm;
468
469 tiocm = smd_tiocmget(info->ch);
470
471 spin_lock_irqsave(&info->reset_lock, flags);
472 tiocm |= (info->in_reset ? TIOCM_OUT1 : 0);
473 if (info->in_reset_updated) {
474 tiocm |= TIOCM_OUT2;
475 info->in_reset_updated = 0;
476 }
477 spin_unlock_irqrestore(&info->reset_lock, flags);
478
479 return tiocm;
480}
481
482static int smd_tty_tiocmset(struct tty_struct *tty,
483 unsigned int set, unsigned int clear)
484{
485 struct smd_tty_info *info = tty->driver_data;
486
487 if (info->in_reset)
488 return -ENETRESET;
489
490 return smd_tiocmset(info->ch, set, clear);
491}
492
493static void loopback_probe_worker(struct work_struct *work)
494{
495 /* wait for modem to restart before requesting loopback server */
496 if (!is_modem_smsm_inited())
497 schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000));
498 else
499 smsm_change_state(SMSM_APPS_STATE,
500 0, SMSM_SMD_LOOPBACK);
501}
502
503static struct tty_operations smd_tty_ops = {
504 .open = smd_tty_open,
505 .close = smd_tty_close,
506 .write = smd_tty_write,
507 .write_room = smd_tty_write_room,
508 .chars_in_buffer = smd_tty_chars_in_buffer,
509 .unthrottle = smd_tty_unthrottle,
510 .tiocmget = smd_tty_tiocmget,
511 .tiocmset = smd_tty_tiocmset,
512};
513
514static int smd_tty_dummy_probe(struct platform_device *pdev)
515{
Eric Holmberg513ad582011-12-14 16:27:13 -0700516 int n;
517 int idx;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700518
Eric Holmberg513ad582011-12-14 16:27:13 -0700519 for (n = 0; n < ARRAY_SIZE(smd_configs); ++n) {
520 idx = smd_configs[n].tty_dev_index;
521
522 if (!smd_configs[n].dev_name)
523 continue;
524
Eric Holmbergdaf36d12012-02-08 17:05:21 -0700525 if (pdev->id == smd_configs[n].edge &&
526 !strncmp(pdev->name, smd_configs[n].dev_name,
Eric Holmberg513ad582011-12-14 16:27:13 -0700527 SMD_MAX_CH_NAME_LEN)) {
528 complete_all(&smd_tty[idx].ch_allocated);
529 return 0;
530 }
531 }
532 pr_err("%s: unknown device '%s'\n", __func__, pdev->name);
533
534 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700535}
536
537static struct tty_driver *smd_tty_driver;
538
539static int __init smd_tty_init(void)
540{
541 int ret;
Eric Holmberg513ad582011-12-14 16:27:13 -0700542 int n;
543 int idx;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700544
545 smd_tty_driver = alloc_tty_driver(MAX_SMD_TTYS);
546 if (smd_tty_driver == 0)
547 return -ENOMEM;
548
549 smd_tty_driver->owner = THIS_MODULE;
550 smd_tty_driver->driver_name = "smd_tty_driver";
551 smd_tty_driver->name = "smd";
552 smd_tty_driver->major = 0;
553 smd_tty_driver->minor_start = 0;
554 smd_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
555 smd_tty_driver->subtype = SERIAL_TYPE_NORMAL;
556 smd_tty_driver->init_termios = tty_std_termios;
557 smd_tty_driver->init_termios.c_iflag = 0;
558 smd_tty_driver->init_termios.c_oflag = 0;
559 smd_tty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
560 smd_tty_driver->init_termios.c_lflag = 0;
561 smd_tty_driver->flags = TTY_DRIVER_RESET_TERMIOS |
562 TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
563 tty_set_operations(smd_tty_driver, &smd_tty_ops);
564
565 ret = tty_register_driver(smd_tty_driver);
Eric Holmberg513ad582011-12-14 16:27:13 -0700566 if (ret) {
567 put_tty_driver(smd_tty_driver);
568 pr_err("%s: driver registration failed %d\n", __func__, ret);
569 return ret;
Jeff Hugo4c0ba6c2011-07-15 11:47:13 -0600570 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700571
Eric Holmberg513ad582011-12-14 16:27:13 -0700572 for (n = 0; n < ARRAY_SIZE(smd_configs); ++n) {
573 idx = smd_configs[n].tty_dev_index;
574
575 if (smd_configs[n].dev_name == NULL)
576 smd_configs[n].dev_name = smd_configs[n].port_name;
577
578 if (idx == DS_IDX) {
579 /*
580 * DS port uses the kernel API starting with
581 * 8660 Fusion. Only register the userspace
582 * platform device for older targets.
583 */
584 int legacy_ds = 0;
585
586 legacy_ds |= cpu_is_msm7x01() || cpu_is_msm7x25();
587 legacy_ds |= cpu_is_msm7x27() || cpu_is_msm7x30();
588 legacy_ds |= cpu_is_qsd8x50() || cpu_is_msm8x55();
Angshuman Sarkar4b931ed2012-01-05 13:38:36 +0530589 /*
590 * use legacy mode for 8660 Standalone (subtype 0)
591 */
Eric Holmberg513ad582011-12-14 16:27:13 -0700592 legacy_ds |= cpu_is_msm8x60() &&
Angshuman Sarkar4b931ed2012-01-05 13:38:36 +0530593 (socinfo_get_platform_subtype() == 0x0);
Eric Holmberg513ad582011-12-14 16:27:13 -0700594
595 if (!legacy_ds)
596 continue;
597 }
598
599 tty_register_device(smd_tty_driver, idx, 0);
600 init_completion(&smd_tty[idx].ch_allocated);
601
602 /* register platform device */
603 smd_tty[idx].driver.probe = smd_tty_dummy_probe;
604 smd_tty[idx].driver.driver.name = smd_configs[n].dev_name;
605 smd_tty[idx].driver.driver.owner = THIS_MODULE;
606 spin_lock_init(&smd_tty[idx].reset_lock);
607 smd_tty[idx].is_open = 0;
Stephen Boyd49a1e882012-07-02 17:28:13 -0700608 setup_timer(&smd_tty[idx].buf_req_timer, buf_req_retry,
609 (unsigned long)&smd_tty[idx]);
Eric Holmberg513ad582011-12-14 16:27:13 -0700610 init_waitqueue_head(&smd_tty[idx].ch_opened_wait_queue);
611 ret = platform_driver_register(&smd_tty[idx].driver);
612
613 if (ret) {
614 pr_err("%s: init failed %d (%d)\n", __func__, idx, ret);
615 smd_tty[idx].driver.probe = NULL;
616 goto out;
617 }
618 smd_tty[idx].smd = &smd_configs[n];
619 }
Devin Kim36b2b8c2012-08-29 23:21:22 -0700620 INIT_WORK(&pm_qos_set_work, pm_qos_set_worker);
Eric Holmberg513ad582011-12-14 16:27:13 -0700621 INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
Devin Kim36b2b8c2012-08-29 23:21:22 -0700622 pm_qos_add_request(&smd_tty_qos_req, PM_QOS_CPU_DMA_LATENCY,
623 PM_QOS_DEFAULT_VALUE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624 return 0;
625
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700626out:
Eric Holmberg513ad582011-12-14 16:27:13 -0700627 /* unregister platform devices */
628 for (n = 0; n < ARRAY_SIZE(smd_configs); ++n) {
629 idx = smd_configs[n].tty_dev_index;
630
631 if (smd_tty[idx].driver.probe) {
632 platform_driver_unregister(&smd_tty[idx].driver);
633 tty_unregister_device(smd_tty_driver, idx);
634 }
635 }
636
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637 tty_unregister_driver(smd_tty_driver);
638 put_tty_driver(smd_tty_driver);
639 return ret;
640}
641
642module_init(smd_tty_init);