Eric Holmberg | 9d89067 | 2012-06-13 17:58:13 -0600 | [diff] [blame] | 1 | /* drivers/tty/smux_debug.c |
| 2 | * |
Duy Truong | 790f06d | 2013-02-13 16:38:12 -0800 | [diff] [blame] | 3 | * Copyright (c) 2012, The Linux Foundation. All rights reserved. |
Eric Holmberg | 9d89067 | 2012-06-13 17:58:13 -0600 | [diff] [blame] | 4 | * |
| 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. |
| 8 | * |
| 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. |
| 13 | * |
| 14 | */ |
| 15 | #include <linux/debugfs.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/ctype.h> |
| 18 | #include <linux/jiffies.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/completion.h> |
| 22 | #include <linux/termios.h> |
| 23 | #include <linux/smux.h> |
| 24 | #include "smux_private.h" |
| 25 | |
| 26 | #define DEBUG_BUFMAX 4096 |
| 27 | |
| 28 | |
| 29 | |
| 30 | /** |
| 31 | * smux_dump_ch() - Dumps the information of a channel to the screen. |
| 32 | * @buf: Buffer for status message. |
| 33 | * @max: Size of status queue. |
| 34 | * @lch_number: Number of the logical channel. |
| 35 | * @lch: Pointer to the lch_number'th instance of struct smux_lch_t. |
| 36 | * |
| 37 | */ |
| 38 | static int smux_dump_ch(char *buf, int max, struct smux_lch_t *lch) |
| 39 | { |
| 40 | int bytes_written; |
| 41 | long tiocm_bits; |
| 42 | unsigned long flags; |
| 43 | |
| 44 | spin_lock_irqsave(&lch->state_lock_lhb1, flags); |
| 45 | spin_lock(&lch->tx_lock_lhb2); |
| 46 | |
| 47 | tiocm_bits = msm_smux_tiocm_get_atomic(lch); |
| 48 | |
| 49 | bytes_written = scnprintf( |
| 50 | buf, max, |
| 51 | "ch%02d: " |
| 52 | "%s(%s) " |
| 53 | "%c%c%c%c%c " |
| 54 | "%d " |
| 55 | "%s(%s) " |
| 56 | "%c%c\n", |
| 57 | lch->lcid, |
| 58 | local_lch_state(lch->local_state), lch_mode(lch->local_mode), |
| 59 | (tiocm_bits & TIOCM_DSR) ? 'D' : 'd', |
| 60 | (tiocm_bits & TIOCM_CTS) ? 'T' : 't', |
| 61 | (tiocm_bits & TIOCM_RI) ? 'I' : 'i', |
| 62 | (tiocm_bits & TIOCM_CD) ? 'C' : 'c', |
| 63 | lch->tx_flow_control ? 'F' : 'f', |
| 64 | lch->tx_pending_data_cnt, |
| 65 | remote_lch_state(lch->remote_state), lch_mode(lch->remote_mode), |
| 66 | (tiocm_bits & TIOCM_DTR) ? 'R' : 'r', |
| 67 | (tiocm_bits & TIOCM_RTS) ? 'S' : 's' |
| 68 | ); |
| 69 | |
| 70 | spin_unlock(&lch->tx_lock_lhb2); |
| 71 | spin_unlock_irqrestore(&lch->state_lock_lhb1, flags); |
| 72 | |
| 73 | return bytes_written; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * smux_dump_format_ch() - Informs user of format for channel dump |
| 78 | * @buf: Buffer for status message. |
| 79 | * @max: Size of status queue. |
| 80 | * |
| 81 | */ |
| 82 | static int smux_dump_format_ch(char *buf, int max) |
| 83 | { |
| 84 | return scnprintf( |
| 85 | buf, max, |
| 86 | "ch_id " |
| 87 | "local state(mode) tiocm " |
| 88 | "tx_queue " |
| 89 | "remote state(mode) tiocm\n" |
| 90 | "local tiocm: DSR(D) CTS(T) RI(I) DCD(C) FLOW_CONTROL(F)\n" |
| 91 | "remote tiocm: DTR(R) RTS(S)\n" |
| 92 | "A capital letter indicates set, otherwise, it is not set.\n\n" |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * smux_debug_ch() - Log following information about each channel |
| 98 | * local open, local mode, remote open, remote mode, |
| 99 | * tiocm bits, flow control state and transmit queue size. |
| 100 | * Returns the number of bytes written to buf. |
| 101 | * @buf Buffer for status message |
| 102 | * @max Size of status queue |
| 103 | * |
| 104 | */ |
| 105 | static int smux_debug_ch(char *buf, int max) |
| 106 | { |
| 107 | int ch_id; |
| 108 | |
| 109 | int bytes_written = smux_dump_format_ch(buf, max); |
| 110 | |
| 111 | for (ch_id = 0; ch_id < SMUX_NUM_LOGICAL_CHANNELS; ch_id++) { |
| 112 | bytes_written += smux_dump_ch(buf + bytes_written, |
| 113 | max - bytes_written, |
| 114 | &smux_lch[ch_id]); |
| 115 | } |
| 116 | |
| 117 | return bytes_written; |
| 118 | |
| 119 | } |
| 120 | |
| 121 | static char debug_buffer[DEBUG_BUFMAX]; |
| 122 | |
| 123 | static ssize_t debug_read(struct file *file, char __user *buf, |
| 124 | size_t count, loff_t *ppos) |
| 125 | { |
| 126 | int (*fill)(char *buf, int max) = file->private_data; |
| 127 | int bsize; |
| 128 | |
| 129 | if (*ppos != 0) |
| 130 | return 0; |
| 131 | |
| 132 | bsize = fill(debug_buffer, DEBUG_BUFMAX); |
| 133 | return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize); |
| 134 | } |
| 135 | |
| 136 | static int debug_open(struct inode *inode, struct file *file) |
| 137 | { |
| 138 | file->private_data = inode->i_private; |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static const struct file_operations debug_ops = { |
| 143 | .read = debug_read, |
| 144 | .open = debug_open, |
| 145 | }; |
| 146 | |
| 147 | |
| 148 | static void debug_create(const char *name, mode_t mode, |
| 149 | struct dentry *dent, |
| 150 | int (*fill)(char *buf, int max)) |
| 151 | { |
| 152 | debugfs_create_file(name, mode, dent, fill, &debug_ops); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | static int __init smux_debugfs_init(void) |
| 157 | { |
| 158 | struct dentry *dent; |
| 159 | |
| 160 | dent = debugfs_create_dir("n_smux", 0); |
| 161 | if (IS_ERR(dent)) |
| 162 | return PTR_ERR(dent); |
| 163 | |
| 164 | debug_create("ch", 0444, dent, smux_debug_ch); |
| 165 | |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | late_initcall(smux_debugfs_init); |