1 /* Copyright (C) 2007 Google, Inc.
2 * Copyright (c) 2009-2015, The Linux Foundation. All rights reserved.
3 * Author: Brian Swetland <swetland@google.com>
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/module.h>
17 #include <linux/cdev.h>
18 #include <linux/device.h>
19 #include <linux/interrupt.h>
20 #include <linux/delay.h>
22 #include <linux/platform_device.h>
23 #include <linux/sched.h>
24 #include <linux/slab.h>
25 #include <linux/ipc_logging.h>
27 #include <linux/suspend.h>
29 #include <linux/tty.h>
30 #include <linux/tty_driver.h>
31 #include <linux/tty_flip.h>
33 #include <soc/qcom/smd.h>
34 #include <soc/qcom/smsm.h>
35 #include <soc/qcom/subsystem_restart.h>
37 #define MODULE_NAME "msm_smdtty"
38 #define MAX_SMD_TTYS 37
39 #define MAX_TTY_BUF_SIZE 2048
40 #define TTY_PUSH_WS_DELAY 500
41 #define TTY_PUSH_WS_POST_SUSPEND_DELAY 100
42 #define MAX_RA_WAKE_LOCK_NAME_LEN 32
43 #define SMD_TTY_LOG_PAGES 2
45 #define SMD_TTY_INFO(buf...) \
47 if (smd_tty_log_ctx) { \
48 ipc_log_string(smd_tty_log_ctx, buf); \
52 #define SMD_TTY_ERR(buf...) \
54 if (smd_tty_log_ctx) \
55 ipc_log_string(smd_tty_log_ctx, buf); \
59 static void *smd_tty_log_ctx;
60 static bool smd_tty_in_suspend;
61 static bool smd_tty_read_in_suspend;
62 static struct wakeup_source read_in_suspend_ws;
65 * struct smd_tty_info - context for an individual SMD TTY device
67 * @ch: SMD channel handle
68 * @port: TTY port context structure
69 * @device_ptr: TTY device pointer
70 * @pending_ws: pending-data wakeup source
71 * @tty_tsklt: read tasklet
72 * @buf_req_timer: RX buffer retry timer
73 * @ch_allocated: completion set when SMD channel is allocated
74 * @pil: Peripheral Image Loader handle
75 * @edge: SMD edge associated with port
76 * @ch_name: SMD channel name associated with port
77 * @dev_name: SMD platform device name associated with port
79 * @open_lock_lha1: open/close lock - used to serialize open/close operations
80 * @open_wait: Timeout in seconds to wait for SMD port to be created / opened
82 * @reset_lock_lha2: lock for reset and open state
83 * @in_reset: True if SMD channel is closed / in SSR
84 * @in_reset_updated: reset state changed
85 * @is_open: True if SMD port is open
86 * @ch_opened_wait_queue: SMD port open/close wait queue
88 * @ra_lock_lha3: Read-available lock - used to synchronize reads from SMD
89 * @ra_wakeup_source_name: Name of the read-available wakeup source
90 * @ra_wakeup_source: Read-available wakeup source
95 struct device *device_ptr;
96 struct wakeup_source pending_ws;
97 struct tasklet_struct tty_tsklt;
98 struct timer_list buf_req_timer;
99 struct completion ch_allocated;
102 char ch_name[SMD_MAX_CH_NAME_LEN];
103 char dev_name[SMD_MAX_CH_NAME_LEN];
105 struct mutex open_lock_lha1;
106 unsigned int open_wait;
108 spinlock_t reset_lock_lha2;
110 int in_reset_updated;
112 wait_queue_head_t ch_opened_wait_queue;
114 spinlock_t ra_lock_lha3;
115 char ra_wakeup_source_name[MAX_RA_WAKE_LOCK_NAME_LEN];
116 struct wakeup_source ra_wakeup_source;
120 * struct smd_tty_pfdriver - SMD tty channel platform driver structure
122 * @list: Adds this structure into smd_tty_platform_driver_list::list.
123 * @ref_cnt: reference count for this structure.
124 * @driver: SMD channel platform driver context structure
126 struct smd_tty_pfdriver {
127 struct list_head list;
129 struct platform_driver driver;
132 #define LOOPBACK_IDX 36
134 static struct delayed_work loopback_work;
135 static struct smd_tty_info smd_tty[MAX_SMD_TTYS];
137 static DEFINE_MUTEX(smd_tty_pfdriver_lock_lha1);
138 static LIST_HEAD(smd_tty_pfdriver_list);
140 static int is_in_reset(struct smd_tty_info *info)
142 return info->in_reset;
145 static void buf_req_retry(unsigned long param)
147 struct smd_tty_info *info = (struct smd_tty_info *)param;
150 spin_lock_irqsave(&info->reset_lock_lha2, flags);
152 spin_unlock_irqrestore(&info->reset_lock_lha2, flags);
153 tasklet_hi_schedule(&info->tty_tsklt);
156 spin_unlock_irqrestore(&info->reset_lock_lha2, flags);
159 static ssize_t open_timeout_store(struct device *dev,
160 struct device_attribute *attr,
161 const char *buf, size_t n)
163 unsigned int num_dev;
166 SMD_TTY_INFO("%s: Invalid Device passed", __func__);
169 for (num_dev = 0; num_dev < MAX_SMD_TTYS; num_dev++) {
170 if (dev == smd_tty[num_dev].device_ptr)
173 if (num_dev >= MAX_SMD_TTYS) {
174 SMD_TTY_ERR("[%s]: Device Not found", __func__);
177 if (!kstrtoul(buf, 10, &wait)) {
178 mutex_lock(&smd_tty[num_dev].open_lock_lha1);
179 smd_tty[num_dev].open_wait = wait;
180 mutex_unlock(&smd_tty[num_dev].open_lock_lha1);
183 SMD_TTY_INFO("[%s]: Unable to convert %s to an int",
189 static ssize_t open_timeout_show(struct device *dev,
190 struct device_attribute *attr, char *buf)
192 unsigned int num_dev;
193 unsigned int open_wait;
196 SMD_TTY_INFO("%s: Invalid Device passed", __func__);
199 for (num_dev = 0; num_dev < MAX_SMD_TTYS; num_dev++) {
200 if (dev == smd_tty[num_dev].device_ptr)
203 if (num_dev >= MAX_SMD_TTYS) {
204 SMD_TTY_ERR("[%s]: Device Not Found", __func__);
208 mutex_lock(&smd_tty[num_dev].open_lock_lha1);
209 open_wait = smd_tty[num_dev].open_wait;
210 mutex_unlock(&smd_tty[num_dev].open_lock_lha1);
212 return snprintf(buf, PAGE_SIZE, "%d\n", open_wait);
216 (open_timeout, 0664, open_timeout_show, open_timeout_store);
218 static void smd_tty_read(unsigned long param)
222 struct smd_tty_info *info = (struct smd_tty_info *)param;
223 struct tty_struct *tty = tty_port_tty_get(&info->port);
230 if (is_in_reset(info)) {
231 /* signal TTY clients using TTY_BREAK */
232 tty_insert_flip_char(tty->port, 0x00, TTY_BREAK);
233 tty_flip_buffer_push(tty->port);
237 if (test_bit(TTY_THROTTLED, &tty->flags))
239 spin_lock_irqsave(&info->ra_lock_lha3, flags);
240 avail = smd_read_avail(info->ch);
242 __pm_relax(&info->ra_wakeup_source);
243 spin_unlock_irqrestore(&info->ra_lock_lha3, flags);
246 spin_unlock_irqrestore(&info->ra_lock_lha3, flags);
248 if (avail > MAX_TTY_BUF_SIZE)
249 avail = MAX_TTY_BUF_SIZE;
251 avail = tty_prepare_flip_string(tty->port, &ptr, avail);
253 mod_timer(&info->buf_req_timer,
254 jiffies + msecs_to_jiffies(30));
259 if (smd_read(info->ch, ptr, avail) != avail) {
260 /* shouldn't be possible since we're in interrupt
261 ** context here and nobody else could 'steal' our
265 "%s - Possible smd_tty_buffer mismatch for %s",
266 __func__, info->ch_name);
270 * Keep system awake long enough to allow the TTY
271 * framework to pass the flip buffer to any waiting
274 __pm_wakeup_event(&info->pending_ws, TTY_PUSH_WS_DELAY);
276 if (smd_tty_in_suspend)
277 smd_tty_read_in_suspend = true;
279 tty_flip_buffer_push(tty->port);
282 /* XXX only when writable and necessary */
287 static void smd_tty_notify(void *priv, unsigned event)
289 struct smd_tty_info *info = priv;
290 struct tty_struct *tty;
295 spin_lock_irqsave(&info->reset_lock_lha2, flags);
296 if (!info->is_open) {
297 spin_unlock_irqrestore(&info->reset_lock_lha2, flags);
300 spin_unlock_irqrestore(&info->reset_lock_lha2, flags);
301 /* There may be clients (tty framework) that are blocked
302 * waiting for space to write data, so if a possible read
303 * interrupt came in wake anyone waiting and disable the
306 if (smd_write_avail(info->ch)) {
307 smd_disable_read_intr(info->ch);
308 tty = tty_port_tty_get(&info->port);
310 wake_up_interruptible(&tty->write_wait);
313 spin_lock_irqsave(&info->ra_lock_lha3, flags);
314 if (smd_read_avail(info->ch)) {
315 __pm_stay_awake(&info->ra_wakeup_source);
316 tasklet_hi_schedule(&info->tty_tsklt);
318 spin_unlock_irqrestore(&info->ra_lock_lha3, flags);
322 tty = tty_port_tty_get(&info->port);
323 spin_lock_irqsave(&info->reset_lock_lha2, flags);
325 clear_bit(TTY_OTHER_CLOSED, &tty->flags);
327 info->in_reset_updated = 1;
329 wake_up_interruptible(&info->ch_opened_wait_queue);
330 spin_unlock_irqrestore(&info->reset_lock_lha2, flags);
334 case SMD_EVENT_CLOSE:
335 spin_lock_irqsave(&info->reset_lock_lha2, flags);
337 info->in_reset_updated = 1;
339 wake_up_interruptible(&info->ch_opened_wait_queue);
340 spin_unlock_irqrestore(&info->reset_lock_lha2, flags);
342 tty = tty_port_tty_get(&info->port);
344 /* send TTY_BREAK through read tasklet */
345 set_bit(TTY_OTHER_CLOSED, &tty->flags);
346 tasklet_hi_schedule(&info->tty_tsklt);
348 if (tty->index == LOOPBACK_IDX)
349 schedule_delayed_work(&loopback_work,
350 msecs_to_jiffies(1000));
357 static uint32_t is_modem_smsm_inited(void)
359 uint32_t modem_state;
360 uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT);
362 modem_state = smsm_get_state(SMSM_MODEM_STATE);
363 return (modem_state & ready_state) == ready_state;
366 static int smd_tty_dummy_probe(struct platform_device *pdev)
370 for (n = 0; n < MAX_SMD_TTYS; ++n) {
371 if (!smd_tty[n].dev_name)
374 if (pdev->id == smd_tty[n].edge &&
375 !strcmp(pdev->name, smd_tty[n].dev_name)) {
376 complete_all(&smd_tty[n].ch_allocated);
380 SMD_TTY_ERR("[ERR]%s: unknown device '%s'\n", __func__, pdev->name);
386 * smd_tty_add_driver() - Add platform drivers for smd tty device
388 * @info: context for an individual SMD TTY device
390 * @returns: 0 for success, standard Linux error code otherwise
392 * This function is used to register platform driver once for all
393 * smd tty devices which have same names and increment the reference
394 * count for 2nd to nth devices.
396 static int smd_tty_add_driver(struct smd_tty_info *info)
399 struct smd_tty_pfdriver *smd_tty_pfdriverp;
400 struct smd_tty_pfdriver *item;
403 pr_err("%s on a NULL device structure\n", __func__);
407 SMD_TTY_INFO("Begin %s on smd_tty[%s]\n", __func__,
410 mutex_lock(&smd_tty_pfdriver_lock_lha1);
411 list_for_each_entry(item, &smd_tty_pfdriver_list, list) {
412 if (!strcmp(item->driver.driver.name, info->dev_name)) {
413 SMD_TTY_INFO("%s:%s Driver Already reg. cnt:%d\n",
414 __func__, info->ch_name, item->ref_cnt);
420 smd_tty_pfdriverp = kzalloc(sizeof(*smd_tty_pfdriverp), GFP_KERNEL);
421 if (IS_ERR_OR_NULL(smd_tty_pfdriverp)) {
422 pr_err("%s: kzalloc() failed for smd_tty_pfdriver[%s]\n",
423 __func__, info->ch_name);
428 smd_tty_pfdriverp->driver.probe = smd_tty_dummy_probe;
429 smd_tty_pfdriverp->driver.driver.name = info->dev_name;
430 smd_tty_pfdriverp->driver.driver.owner = THIS_MODULE;
431 r = platform_driver_register(&smd_tty_pfdriverp->driver);
433 pr_err("%s: %s Platform driver reg. failed\n",
434 __func__, info->ch_name);
435 kfree(smd_tty_pfdriverp);
438 ++smd_tty_pfdriverp->ref_cnt;
439 list_add(&smd_tty_pfdriverp->list, &smd_tty_pfdriver_list);
442 SMD_TTY_INFO("End %s on smd_tty_ch[%s]\n", __func__, info->ch_name);
443 mutex_unlock(&smd_tty_pfdriver_lock_lha1);
448 * smd_tty_remove_driver() - Remove the platform drivers for smd tty device
450 * @info: context for an individual SMD TTY device
452 * This function is used to decrement the reference count on
453 * platform drivers for smd pkt devices and removes the drivers
454 * when the reference count becomes zero.
456 static void smd_tty_remove_driver(struct smd_tty_info *info)
458 struct smd_tty_pfdriver *smd_tty_pfdriverp;
459 bool found_item = false;
462 pr_err("%s on a NULL device\n", __func__);
466 SMD_TTY_INFO("Begin %s on smd_tty_ch[%s]\n", __func__,
468 mutex_lock(&smd_tty_pfdriver_lock_lha1);
469 list_for_each_entry(smd_tty_pfdriverp, &smd_tty_pfdriver_list, list) {
470 if (!strcmp(smd_tty_pfdriverp->driver.driver.name,
473 SMD_TTY_INFO("%s:%s Platform driver cnt:%d\n",
474 __func__, info->ch_name,
475 smd_tty_pfdriverp->ref_cnt);
476 if (smd_tty_pfdriverp->ref_cnt > 0)
477 --smd_tty_pfdriverp->ref_cnt;
479 pr_warn("%s reference count <= 0\n", __func__);
484 SMD_TTY_ERR("%s:%s No item found in list.\n",
485 __func__, info->ch_name);
487 if (found_item && smd_tty_pfdriverp->ref_cnt == 0) {
488 platform_driver_unregister(&smd_tty_pfdriverp->driver);
489 smd_tty_pfdriverp->driver.probe = NULL;
490 list_del(&smd_tty_pfdriverp->list);
491 kfree(smd_tty_pfdriverp);
493 mutex_unlock(&smd_tty_pfdriver_lock_lha1);
494 SMD_TTY_INFO("End %s on smd_tty_ch[%s]\n", __func__, info->ch_name);
497 static int smd_tty_port_activate(struct tty_port *tport,
498 struct tty_struct *tty)
501 unsigned int n = tty->index;
502 struct smd_tty_info *info;
503 const char *peripheral = NULL;
505 if (n >= MAX_SMD_TTYS || !smd_tty[n].ch_name)
510 mutex_lock(&info->open_lock_lha1);
511 tty->driver_data = info;
513 res = smd_tty_add_driver(info);
515 SMD_TTY_ERR("%s:%d Idx smd_tty_driver register failed %d\n",
520 peripheral = smd_edge_to_pil_str(smd_tty[n].edge);
521 if (!IS_ERR_OR_NULL(peripheral)) {
522 info->pil = subsystem_get(peripheral);
523 if (IS_ERR(info->pil)) {
525 "%s failed on smd_tty device :%s subsystem_get failed for %s",
526 __func__, info->ch_name,
530 * Sleep, inorder to reduce the frequency of
531 * retry by user-space modules and to avoid
532 * possible watchdog bite.
534 msleep((smd_tty[n].open_wait * 1000));
535 res = PTR_ERR(info->pil);
536 goto platform_unregister;
540 /* Wait for the modem SMSM to be inited for the SMD
541 * Loopback channel to be allocated at the modem. Since
542 * the wait need to be done atmost once, using msleep
543 * doesn't degrade the performance.
545 if (n == LOOPBACK_IDX) {
546 if (!is_modem_smsm_inited())
548 smsm_change_state(SMSM_APPS_STATE,
549 0, SMSM_SMD_LOOPBACK);
554 * Wait for a channel to be allocated so we know
555 * the modem is ready enough.
557 if (smd_tty[n].open_wait) {
558 res = wait_for_completion_interruptible_timeout(
560 msecs_to_jiffies(smd_tty[n].open_wait *
565 "Timed out waiting for SMD channel %s",
569 } else if (res < 0) {
571 "Error waiting for SMD channel %s : %d\n",
577 tasklet_init(&info->tty_tsklt, smd_tty_read, (unsigned long)info);
578 wakeup_source_init(&info->pending_ws, info->ch_name);
579 scnprintf(info->ra_wakeup_source_name, MAX_RA_WAKE_LOCK_NAME_LEN,
580 "SMD_TTY_%s_RA", info->ch_name);
581 wakeup_source_init(&info->ra_wakeup_source,
582 info->ra_wakeup_source_name);
584 res = smd_named_open_on_edge(info->ch_name,
585 smd_tty[n].edge, &info->ch, info,
588 SMD_TTY_INFO("%s: %s open failed %d\n",
589 __func__, info->ch_name, res);
593 res = wait_event_interruptible_timeout(info->ch_opened_wait_queue,
594 info->is_open, (2 * HZ));
598 SMD_TTY_INFO("%s: wait for %s smd_open failed %d\n",
599 __func__, info->ch_name, res);
602 SMD_TTY_INFO("%s with PID %u opened port %s",
603 current->comm, current->pid, info->ch_name);
604 smd_disable_read_intr(info->ch);
605 mutex_unlock(&info->open_lock_lha1);
613 tasklet_kill(&info->tty_tsklt);
614 wakeup_source_trash(&info->pending_ws);
615 wakeup_source_trash(&info->ra_wakeup_source);
618 subsystem_put(info->pil);
621 smd_tty_remove_driver(info);
624 mutex_unlock(&info->open_lock_lha1);
629 static void smd_tty_port_shutdown(struct tty_port *tport)
631 struct smd_tty_info *info;
632 struct tty_struct *tty = tty_port_tty_get(tport);
635 info = tty->driver_data;
641 mutex_lock(&info->open_lock_lha1);
643 spin_lock_irqsave(&info->reset_lock_lha2, flags);
645 spin_unlock_irqrestore(&info->reset_lock_lha2, flags);
647 tasklet_kill(&info->tty_tsklt);
648 wakeup_source_trash(&info->pending_ws);
649 wakeup_source_trash(&info->ra_wakeup_source);
651 SMD_TTY_INFO("%s with PID %u closed port %s",
652 current->comm, current->pid,
654 tty->driver_data = NULL;
655 del_timer(&info->buf_req_timer);
659 subsystem_put(info->pil);
660 smd_tty_remove_driver(info);
662 mutex_unlock(&info->open_lock_lha1);
666 static int smd_tty_open(struct tty_struct *tty, struct file *f)
668 struct smd_tty_info *info = smd_tty + tty->index;
670 return tty_port_open(&info->port, tty, f);
673 static void smd_tty_close(struct tty_struct *tty, struct file *f)
675 struct smd_tty_info *info = smd_tty + tty->index;
677 tty_port_close(&info->port, tty, f);
680 static int smd_tty_write(struct tty_struct *tty, const unsigned char *buf,
683 struct smd_tty_info *info = tty->driver_data;
686 /* if we're writing to a packet channel we will
687 ** never be able to write more data than there
688 ** is currently space for
690 if (is_in_reset(info))
693 avail = smd_write_avail(info->ch);
694 /* if no space, we'll have to setup a notification later to wake up the
695 * tty framework when space becomes avaliable
698 smd_enable_read_intr(info->ch);
703 SMD_TTY_INFO("[WRITE]: PID %u -> port %s %x bytes",
704 current->pid, info->ch_name, len);
706 return smd_write(info->ch, buf, len);
709 static int smd_tty_write_room(struct tty_struct *tty)
711 struct smd_tty_info *info = tty->driver_data;
712 return smd_write_avail(info->ch);
715 static int smd_tty_chars_in_buffer(struct tty_struct *tty)
717 struct smd_tty_info *info = tty->driver_data;
718 return smd_read_avail(info->ch);
721 static void smd_tty_unthrottle(struct tty_struct *tty)
723 struct smd_tty_info *info = tty->driver_data;
726 spin_lock_irqsave(&info->reset_lock_lha2, flags);
728 spin_unlock_irqrestore(&info->reset_lock_lha2, flags);
729 tasklet_hi_schedule(&info->tty_tsklt);
732 spin_unlock_irqrestore(&info->reset_lock_lha2, flags);
736 * Returns the current TIOCM status bits including:
737 * SMD Signals (DTR/DSR, CTS/RTS, CD, RI)
738 * TIOCM_OUT1 - reset state (1=in reset)
739 * TIOCM_OUT2 - reset state updated (1=updated)
741 static int smd_tty_tiocmget(struct tty_struct *tty)
743 struct smd_tty_info *info = tty->driver_data;
747 tiocm = smd_tiocmget(info->ch);
749 spin_lock_irqsave(&info->reset_lock_lha2, flags);
750 tiocm |= (info->in_reset ? TIOCM_OUT1 : 0);
751 if (info->in_reset_updated) {
753 info->in_reset_updated = 0;
755 SMD_TTY_INFO("PID %u --> %s TIOCM is %x ",
756 current->pid, __func__, tiocm);
757 spin_unlock_irqrestore(&info->reset_lock_lha2, flags);
762 static int smd_tty_tiocmset(struct tty_struct *tty,
763 unsigned int set, unsigned int clear)
765 struct smd_tty_info *info = tty->driver_data;
770 SMD_TTY_INFO("PID %u --> %s Set: %x Clear: %x",
771 current->pid, __func__, set, clear);
772 return smd_tiocmset(info->ch, set, clear);
775 static void loopback_probe_worker(struct work_struct *work)
777 /* wait for modem to restart before requesting loopback server */
778 if (!is_modem_smsm_inited())
779 schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000));
781 smsm_change_state(SMSM_APPS_STATE,
782 0, SMSM_SMD_LOOPBACK);
785 static const struct tty_port_operations smd_tty_port_ops = {
786 .shutdown = smd_tty_port_shutdown,
787 .activate = smd_tty_port_activate,
790 static const struct tty_operations smd_tty_ops = {
791 .open = smd_tty_open,
792 .close = smd_tty_close,
793 .write = smd_tty_write,
794 .write_room = smd_tty_write_room,
795 .chars_in_buffer = smd_tty_chars_in_buffer,
796 .unthrottle = smd_tty_unthrottle,
797 .tiocmget = smd_tty_tiocmget,
798 .tiocmset = smd_tty_tiocmset,
801 static int smd_tty_pm_notifier(struct notifier_block *nb,
802 unsigned long event, void *unused)
805 case PM_SUSPEND_PREPARE:
806 smd_tty_read_in_suspend = false;
807 smd_tty_in_suspend = true;
810 case PM_POST_SUSPEND:
811 smd_tty_in_suspend = false;
812 if (smd_tty_read_in_suspend) {
813 smd_tty_read_in_suspend = false;
814 __pm_wakeup_event(&read_in_suspend_ws,
815 TTY_PUSH_WS_POST_SUSPEND_DELAY);
822 static struct notifier_block smd_tty_pm_nb = {
823 .notifier_call = smd_tty_pm_notifier,
828 * smd_tty_log_init()- Init function for IPC logging
830 * Initialize the buffer that is used to provide the log information
831 * pertaining to the smd_tty module.
833 static void smd_tty_log_init(void)
835 smd_tty_log_ctx = ipc_log_context_create(SMD_TTY_LOG_PAGES,
837 if (!smd_tty_log_ctx)
838 pr_err("%s: Unable to create IPC log", __func__);
841 static struct tty_driver *smd_tty_driver;
843 static int smd_tty_register_driver(void)
847 smd_tty_driver = alloc_tty_driver(MAX_SMD_TTYS);
848 if (smd_tty_driver == 0) {
849 SMD_TTY_ERR("%s - Driver allocation failed", __func__);
853 smd_tty_driver->owner = THIS_MODULE;
854 smd_tty_driver->driver_name = "smd_tty_driver";
855 smd_tty_driver->name = "smd";
856 smd_tty_driver->major = 0;
857 smd_tty_driver->minor_start = 0;
858 smd_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
859 smd_tty_driver->subtype = SERIAL_TYPE_NORMAL;
860 smd_tty_driver->init_termios = tty_std_termios;
861 smd_tty_driver->init_termios.c_iflag = 0;
862 smd_tty_driver->init_termios.c_oflag = 0;
863 smd_tty_driver->init_termios.c_cflag = B38400 | CS8 | CREAD;
864 smd_tty_driver->init_termios.c_lflag = 0;
865 smd_tty_driver->flags = TTY_DRIVER_RESET_TERMIOS |
866 TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;
867 tty_set_operations(smd_tty_driver, &smd_tty_ops);
869 ret = tty_register_driver(smd_tty_driver);
871 put_tty_driver(smd_tty_driver);
872 SMD_TTY_ERR("%s: driver registration failed %d", __func__, ret);
878 static void smd_tty_device_init(int idx)
880 struct tty_port *port;
882 port = &smd_tty[idx].port;
884 port->ops = &smd_tty_port_ops;
885 smd_tty[idx].device_ptr = tty_port_register_device(port, smd_tty_driver,
887 if (IS_ERR_OR_NULL(smd_tty[idx].device_ptr)) {
888 SMD_TTY_ERR("%s: Unable to register tty port %s reason %d\n",
890 smd_tty[idx].ch_name,
891 PTR_ERR_OR_ZERO(smd_tty[idx].device_ptr));
894 init_completion(&smd_tty[idx].ch_allocated);
895 mutex_init(&smd_tty[idx].open_lock_lha1);
896 spin_lock_init(&smd_tty[idx].reset_lock_lha2);
897 spin_lock_init(&smd_tty[idx].ra_lock_lha3);
898 smd_tty[idx].is_open = 0;
899 setup_timer(&smd_tty[idx].buf_req_timer, buf_req_retry,
900 (unsigned long)&smd_tty[idx]);
901 init_waitqueue_head(&smd_tty[idx].ch_opened_wait_queue);
903 if (device_create_file(smd_tty[idx].device_ptr, &dev_attr_open_timeout))
904 SMD_TTY_ERR("%s: Unable to create device attributes for %s",
905 __func__, smd_tty[idx].ch_name);
908 static int smd_tty_devicetree_init(struct platform_device *pdev)
915 const char *dev_name;
916 const char *remote_ss;
917 struct device_node *node;
919 ret = smd_tty_register_driver();
921 SMD_TTY_ERR("%s: driver registration failed %d\n",
926 for_each_child_of_node(pdev->dev.of_node, node) {
928 ret = of_alias_get_id(node, "smd");
929 SMD_TTY_INFO("%s:adding smd%d\n", __func__, ret);
931 if (ret < 0 || ret >= MAX_SMD_TTYS)
935 key = "qcom,smdtty-remote";
936 remote_ss = of_get_property(node, key, NULL);
940 edge = smd_remote_ss_to_edge(remote_ss);
943 smd_tty[idx].edge = edge;
945 key = "qcom,smdtty-port-name";
946 ch_name = of_get_property(node, key, NULL);
949 strlcpy(smd_tty[idx].ch_name, ch_name,
950 SMD_MAX_CH_NAME_LEN);
952 key = "qcom,smdtty-dev-name";
953 dev_name = of_get_property(node, key, NULL);
955 strlcpy(smd_tty[idx].dev_name, smd_tty[idx].ch_name,
956 SMD_MAX_CH_NAME_LEN);
958 strlcpy(smd_tty[idx].dev_name, dev_name,
959 SMD_MAX_CH_NAME_LEN);
962 smd_tty_device_init(idx);
964 INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker);
966 ret = register_pm_notifier(&smd_tty_pm_nb);
968 pr_err("%s: power state notif error %d\n", __func__, ret);
973 SMD_TTY_ERR("%s:Initialization error, key[%s]\n", __func__, key);
974 /* Unregister tty platform devices */
975 for_each_child_of_node(pdev->dev.of_node, node) {
977 ret = of_alias_get_id(node, "smd");
978 SMD_TTY_INFO("%s:Removing smd%d\n", __func__, ret);
980 if (ret < 0 || ret >= MAX_SMD_TTYS)
984 if (smd_tty[idx].device_ptr) {
985 device_remove_file(smd_tty[idx].device_ptr,
986 &dev_attr_open_timeout);
987 tty_unregister_device(smd_tty_driver, idx);
991 tty_unregister_driver(smd_tty_driver);
992 put_tty_driver(smd_tty_driver);
996 static int msm_smd_tty_probe(struct platform_device *pdev)
1001 if (pdev->dev.of_node) {
1002 ret = smd_tty_devicetree_init(pdev);
1004 SMD_TTY_ERR("%s: device tree init failed\n",
1014 static struct of_device_id msm_smd_tty_match_table[] = {
1015 { .compatible = "qcom,smdtty" },
1019 static struct platform_driver msm_smd_tty_driver = {
1020 .probe = msm_smd_tty_probe,
1022 .name = MODULE_NAME,
1023 .owner = THIS_MODULE,
1024 .of_match_table = msm_smd_tty_match_table,
1029 static int __init smd_tty_init(void)
1034 rc = platform_driver_register(&msm_smd_tty_driver);
1036 SMD_TTY_ERR("%s: msm_smd_tty_driver register failed %d\n",
1041 wakeup_source_init(&read_in_suspend_ws, "SMDTTY_READ_IN_SUSPEND");
1045 module_init(smd_tty_init);