Karthikeyan Ramasubramanian | 608a2bc | 2012-12-21 18:52:33 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2008-2013, The Linux Foundation. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | /* |
| 14 | * SMD Packet Driver -- Provides a binary SMD non-muxed packet port |
| 15 | * interface. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/cdev.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/device.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/delay.h> |
| 27 | #include <linux/uaccess.h> |
| 28 | #include <linux/workqueue.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <linux/completion.h> |
| 31 | #include <linux/msm_smd_pkt.h> |
| 32 | #include <linux/poll.h> |
| 33 | #include <asm/ioctls.h> |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 34 | #include <linux/wakelock.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 35 | |
| 36 | #include <mach/msm_smd.h> |
Stephen Boyd | 77db8bb | 2012-06-27 15:15:16 -0700 | [diff] [blame] | 37 | #include <mach/subsystem_restart.h> |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 38 | #include <mach/msm_ipc_logging.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 39 | |
| 40 | #include "smd_private.h" |
| 41 | #ifdef CONFIG_ARCH_FSM9XXX |
| 42 | #define NUM_SMD_PKT_PORTS 4 |
| 43 | #else |
Eric Holmberg | c1e645e | 2013-02-11 19:17:52 -0700 | [diff] [blame] | 44 | #define NUM_SMD_PKT_PORTS 27 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 45 | #endif |
| 46 | |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 47 | #define PDRIVER_NAME_MAX_SIZE 32 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 48 | #define LOOPBACK_INX (NUM_SMD_PKT_PORTS - 1) |
| 49 | |
| 50 | #define DEVICE_NAME "smdpkt" |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 51 | #define WAKELOCK_TIMEOUT (2*HZ) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 52 | |
| 53 | struct smd_pkt_dev { |
| 54 | struct cdev cdev; |
| 55 | struct device *devicep; |
| 56 | void *pil; |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 57 | char pdriver_name[PDRIVER_NAME_MAX_SIZE]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 58 | struct platform_driver driver; |
| 59 | |
| 60 | struct smd_channel *ch; |
| 61 | struct mutex ch_lock; |
| 62 | struct mutex rx_lock; |
| 63 | struct mutex tx_lock; |
| 64 | wait_queue_head_t ch_read_wait_queue; |
| 65 | wait_queue_head_t ch_write_wait_queue; |
| 66 | wait_queue_head_t ch_opened_wait_queue; |
| 67 | |
| 68 | int i; |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 69 | int ref_cnt; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 70 | |
| 71 | int blocking_write; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 72 | int is_open; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 73 | int poll_mode; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 74 | unsigned ch_size; |
| 75 | uint open_modem_wait; |
| 76 | |
| 77 | int has_reset; |
| 78 | int do_reset_notification; |
| 79 | struct completion ch_allocated; |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 80 | struct wake_lock pa_wake_lock; /* Packet Arrival Wake lock*/ |
| 81 | struct work_struct packet_arrival_work; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 82 | struct spinlock pa_spinlock; |
Jeff Hugo | 93476a3 | 2012-06-01 14:32:49 -0600 | [diff] [blame] | 83 | int wakelock_locked; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 84 | } *smd_pkt_devp[NUM_SMD_PKT_PORTS]; |
| 85 | |
| 86 | struct class *smd_pkt_classp; |
| 87 | static dev_t smd_pkt_number; |
| 88 | static struct delayed_work loopback_work; |
| 89 | static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp); |
| 90 | static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp); |
| 91 | static uint32_t is_modem_smsm_inited(void); |
| 92 | |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 93 | #define SMD_PKT_IPC_LOG_PAGE_CNT 2 |
| 94 | static void *smd_pkt_ilctxt; |
| 95 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 96 | static int msm_smd_pkt_debug_mask; |
| 97 | module_param_named(debug_mask, msm_smd_pkt_debug_mask, |
| 98 | int, S_IRUGO | S_IWUSR | S_IWGRP); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 99 | |
| 100 | enum { |
| 101 | SMD_PKT_STATUS = 1U << 0, |
| 102 | SMD_PKT_READ = 1U << 1, |
| 103 | SMD_PKT_WRITE = 1U << 2, |
| 104 | SMD_PKT_READ_DUMP_BUFFER = 1U << 3, |
| 105 | SMD_PKT_WRITE_DUMP_BUFFER = 1U << 4, |
| 106 | SMD_PKT_POLL = 1U << 5, |
| 107 | }; |
| 108 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 109 | #define DEBUG |
| 110 | |
| 111 | #ifdef DEBUG |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 112 | |
| 113 | #define SMD_PKT_LOG_STRING(x...) \ |
| 114 | do { \ |
| 115 | if (smd_pkt_ilctxt) \ |
| 116 | ipc_log_string(smd_pkt_ilctxt, "<SMD_PKT>: "x); \ |
| 117 | } while (0) |
| 118 | |
| 119 | #define SMD_PKT_LOG_BUF(buf, cnt) \ |
| 120 | do { \ |
| 121 | char log_buf[128]; \ |
| 122 | int i; \ |
| 123 | if (smd_pkt_ilctxt) { \ |
| 124 | i = cnt < 16 ? cnt : 16; \ |
| 125 | hex_dump_to_buffer(buf, i, 16, 1, log_buf, \ |
| 126 | sizeof(log_buf), false); \ |
| 127 | ipc_log_string(smd_pkt_ilctxt, "<SMD_PKT>: %s", log_buf); \ |
| 128 | } \ |
| 129 | } while (0) |
| 130 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 131 | #define D_STATUS(x...) \ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 132 | do { \ |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 133 | if (msm_smd_pkt_debug_mask & SMD_PKT_STATUS) \ |
| 134 | pr_info("Status: "x); \ |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 135 | SMD_PKT_LOG_STRING(x); \ |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 136 | } while (0) |
| 137 | |
| 138 | #define D_READ(x...) \ |
| 139 | do { \ |
| 140 | if (msm_smd_pkt_debug_mask & SMD_PKT_READ) \ |
| 141 | pr_info("Read: "x); \ |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 142 | SMD_PKT_LOG_STRING(x); \ |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 143 | } while (0) |
| 144 | |
| 145 | #define D_WRITE(x...) \ |
| 146 | do { \ |
| 147 | if (msm_smd_pkt_debug_mask & SMD_PKT_WRITE) \ |
| 148 | pr_info("Write: "x); \ |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 149 | SMD_PKT_LOG_STRING(x); \ |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 150 | } while (0) |
| 151 | |
| 152 | #define D_READ_DUMP_BUFFER(prestr, cnt, buf) \ |
| 153 | do { \ |
| 154 | if (msm_smd_pkt_debug_mask & SMD_PKT_READ_DUMP_BUFFER) \ |
| 155 | print_hex_dump(KERN_INFO, prestr, \ |
| 156 | DUMP_PREFIX_NONE, 16, 1, \ |
| 157 | buf, cnt, 1); \ |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 158 | SMD_PKT_LOG_BUF(buf, cnt); \ |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 159 | } while (0) |
| 160 | |
| 161 | #define D_WRITE_DUMP_BUFFER(prestr, cnt, buf) \ |
| 162 | do { \ |
| 163 | if (msm_smd_pkt_debug_mask & SMD_PKT_WRITE_DUMP_BUFFER) \ |
| 164 | print_hex_dump(KERN_INFO, prestr, \ |
| 165 | DUMP_PREFIX_NONE, 16, 1, \ |
| 166 | buf, cnt, 1); \ |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 167 | SMD_PKT_LOG_BUF(buf, cnt); \ |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 168 | } while (0) |
| 169 | |
| 170 | #define D_POLL(x...) \ |
| 171 | do { \ |
| 172 | if (msm_smd_pkt_debug_mask & SMD_PKT_POLL) \ |
| 173 | pr_info("Poll: "x); \ |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 174 | SMD_PKT_LOG_STRING(x); \ |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 175 | } while (0) |
Karthikeyan Ramasubramanian | a6dc4b0 | 2012-09-07 14:20:06 -0600 | [diff] [blame] | 176 | |
| 177 | #define E_SMD_PKT_SSR(x) \ |
| 178 | do { \ |
| 179 | if (x->do_reset_notification) \ |
| 180 | pr_err("%s notifying reset for smd_pkt_dev id:%d\n", \ |
| 181 | __func__, x->i); \ |
| 182 | } while (0) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 183 | #else |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 184 | #define D_STATUS(x...) do {} while (0) |
| 185 | #define D_READ(x...) do {} while (0) |
| 186 | #define D_WRITE(x...) do {} while (0) |
| 187 | #define D_READ_DUMP_BUFFER(prestr, cnt, buf) do {} while (0) |
| 188 | #define D_WRITE_DUMP_BUFFER(prestr, cnt, buf) do {} while (0) |
| 189 | #define D_POLL(x...) do {} while (0) |
Karthikeyan Ramasubramanian | a6dc4b0 | 2012-09-07 14:20:06 -0600 | [diff] [blame] | 190 | #define E_SMD_PKT_SSR(x) do {} while (0) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 191 | #endif |
| 192 | |
| 193 | static ssize_t open_timeout_store(struct device *d, |
| 194 | struct device_attribute *attr, |
| 195 | const char *buf, |
| 196 | size_t n) |
| 197 | { |
| 198 | int i; |
| 199 | unsigned long tmp; |
| 200 | for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) { |
| 201 | if (smd_pkt_devp[i]->devicep == d) |
| 202 | break; |
| 203 | } |
Jeff Hugo | 31f83b4 | 2012-01-25 15:15:26 -0700 | [diff] [blame] | 204 | if (i >= NUM_SMD_PKT_PORTS) { |
| 205 | pr_err("%s: unable to match device to valid smd_pkt port\n", |
| 206 | __func__); |
| 207 | return -EINVAL; |
| 208 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | if (!strict_strtoul(buf, 10, &tmp)) { |
| 210 | smd_pkt_devp[i]->open_modem_wait = tmp; |
| 211 | return n; |
| 212 | } else { |
| 213 | pr_err("%s: unable to convert: %s to an int\n", __func__, |
| 214 | buf); |
| 215 | return -EINVAL; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | static ssize_t open_timeout_show(struct device *d, |
| 220 | struct device_attribute *attr, |
| 221 | char *buf) |
| 222 | { |
| 223 | int i; |
| 224 | for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) { |
| 225 | if (smd_pkt_devp[i]->devicep == d) |
| 226 | break; |
| 227 | } |
Jeff Hugo | 31f83b4 | 2012-01-25 15:15:26 -0700 | [diff] [blame] | 228 | if (i >= NUM_SMD_PKT_PORTS) { |
| 229 | pr_err("%s: unable to match device to valid smd_pkt port\n", |
| 230 | __func__); |
| 231 | return -EINVAL; |
| 232 | } |
Karthikeyan Ramasubramanian | 63fa3d3 | 2011-09-29 17:06:26 -0600 | [diff] [blame] | 233 | return snprintf(buf, PAGE_SIZE, "%d\n", |
| 234 | smd_pkt_devp[i]->open_modem_wait); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static DEVICE_ATTR(open_timeout, 0664, open_timeout_show, open_timeout_store); |
| 238 | |
| 239 | static int notify_reset(struct smd_pkt_dev *smd_pkt_devp) |
| 240 | { |
| 241 | smd_pkt_devp->do_reset_notification = 0; |
| 242 | |
| 243 | return -ENETRESET; |
| 244 | } |
| 245 | |
| 246 | static void clean_and_signal(struct smd_pkt_dev *smd_pkt_devp) |
| 247 | { |
| 248 | smd_pkt_devp->do_reset_notification = 1; |
| 249 | smd_pkt_devp->has_reset = 1; |
| 250 | |
| 251 | smd_pkt_devp->is_open = 0; |
| 252 | |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 253 | wake_up(&smd_pkt_devp->ch_read_wait_queue); |
Karthikeyan Ramasubramanian | ce2c115 | 2011-11-07 14:17:41 -0700 | [diff] [blame] | 254 | wake_up(&smd_pkt_devp->ch_write_wait_queue); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 255 | wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 256 | D_STATUS("%s smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | static void loopback_probe_worker(struct work_struct *work) |
| 260 | { |
| 261 | |
| 262 | /* Wait for the modem SMSM to be inited for the SMD |
| 263 | ** Loopback channel to be allocated at the modem. Since |
| 264 | ** the wait need to be done atmost once, using msleep |
| 265 | ** doesn't degrade the performance. */ |
| 266 | if (!is_modem_smsm_inited()) |
| 267 | schedule_delayed_work(&loopback_work, msecs_to_jiffies(1000)); |
| 268 | else |
| 269 | smsm_change_state(SMSM_APPS_STATE, |
| 270 | 0, SMSM_SMD_LOOPBACK); |
| 271 | |
| 272 | } |
| 273 | |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 274 | static void packet_arrival_worker(struct work_struct *work) |
| 275 | { |
| 276 | struct smd_pkt_dev *smd_pkt_devp; |
Jeff Hugo | 93476a3 | 2012-06-01 14:32:49 -0600 | [diff] [blame] | 277 | unsigned long flags; |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 278 | |
| 279 | smd_pkt_devp = container_of(work, struct smd_pkt_dev, |
| 280 | packet_arrival_work); |
| 281 | mutex_lock(&smd_pkt_devp->ch_lock); |
Jeff Hugo | 93476a3 | 2012-06-01 14:32:49 -0600 | [diff] [blame] | 282 | spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags); |
| 283 | if (smd_pkt_devp->ch && smd_pkt_devp->wakelock_locked) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 284 | D_READ("%s locking smd_pkt_dev id:%d wakelock\n", |
| 285 | __func__, smd_pkt_devp->i); |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 286 | wake_lock_timeout(&smd_pkt_devp->pa_wake_lock, |
| 287 | WAKELOCK_TIMEOUT); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 288 | } |
Jeff Hugo | 93476a3 | 2012-06-01 14:32:49 -0600 | [diff] [blame] | 289 | spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags); |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 290 | mutex_unlock(&smd_pkt_devp->ch_lock); |
| 291 | } |
| 292 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 293 | static long smd_pkt_ioctl(struct file *file, unsigned int cmd, |
| 294 | unsigned long arg) |
| 295 | { |
| 296 | int ret; |
| 297 | struct smd_pkt_dev *smd_pkt_devp; |
| 298 | |
| 299 | smd_pkt_devp = file->private_data; |
| 300 | if (!smd_pkt_devp) |
| 301 | return -EINVAL; |
| 302 | |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 303 | mutex_lock(&smd_pkt_devp->ch_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 304 | switch (cmd) { |
| 305 | case TIOCMGET: |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 306 | D_STATUS("%s TIOCMGET command on smd_pkt_dev id:%d\n", |
| 307 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 308 | ret = smd_tiocmget(smd_pkt_devp->ch); |
| 309 | break; |
| 310 | case TIOCMSET: |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 311 | D_STATUS("%s TIOCSET command on smd_pkt_dev id:%d\n", |
| 312 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 313 | ret = smd_tiocmset(smd_pkt_devp->ch, arg, ~arg); |
| 314 | break; |
| 315 | case SMD_PKT_IOCTL_BLOCKING_WRITE: |
| 316 | ret = get_user(smd_pkt_devp->blocking_write, (int *)arg); |
| 317 | break; |
| 318 | default: |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 319 | pr_err("%s: Unrecognized ioctl command %d\n", __func__, cmd); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 320 | ret = -1; |
| 321 | } |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 322 | mutex_unlock(&smd_pkt_devp->ch_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 323 | |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | ssize_t smd_pkt_read(struct file *file, |
| 328 | char __user *buf, |
| 329 | size_t count, |
| 330 | loff_t *ppos) |
| 331 | { |
| 332 | int r; |
| 333 | int bytes_read; |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 334 | int pkt_size; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 335 | struct smd_pkt_dev *smd_pkt_devp; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 336 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 337 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 338 | smd_pkt_devp = file->private_data; |
| 339 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 340 | if (!smd_pkt_devp) { |
| 341 | pr_err("%s on NULL smd_pkt_dev\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 342 | return -EINVAL; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | if (!smd_pkt_devp->ch) { |
| 346 | pr_err("%s on a closed smd_pkt_dev id:%d\n", |
| 347 | __func__, smd_pkt_devp->i); |
| 348 | return -EINVAL; |
| 349 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 350 | |
| 351 | if (smd_pkt_devp->do_reset_notification) { |
| 352 | /* notify client that a reset occurred */ |
Karthikeyan Ramasubramanian | a6dc4b0 | 2012-09-07 14:20:06 -0600 | [diff] [blame] | 353 | E_SMD_PKT_SSR(smd_pkt_devp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 354 | return notify_reset(smd_pkt_devp); |
| 355 | } |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 356 | D_READ("Begin %s on smd_pkt_dev id:%d buffer_size %d\n", |
| 357 | __func__, smd_pkt_devp->i, count); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 358 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 359 | wait_for_packet: |
| 360 | r = wait_event_interruptible(smd_pkt_devp->ch_read_wait_queue, |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 361 | !smd_pkt_devp->ch || |
| 362 | (smd_cur_packet_size(smd_pkt_devp->ch) > 0 |
| 363 | && smd_read_avail(smd_pkt_devp->ch)) || |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 364 | smd_pkt_devp->has_reset); |
| 365 | |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 366 | mutex_lock(&smd_pkt_devp->rx_lock); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 367 | if (smd_pkt_devp->has_reset) { |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 368 | mutex_unlock(&smd_pkt_devp->rx_lock); |
Karthikeyan Ramasubramanian | a6dc4b0 | 2012-09-07 14:20:06 -0600 | [diff] [blame] | 369 | E_SMD_PKT_SSR(smd_pkt_devp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 370 | return notify_reset(smd_pkt_devp); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 371 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 372 | |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 373 | if (!smd_pkt_devp->ch) { |
| 374 | mutex_unlock(&smd_pkt_devp->rx_lock); |
| 375 | pr_err("%s on a closed smd_pkt_dev id:%d\n", |
| 376 | __func__, smd_pkt_devp->i); |
| 377 | return -EINVAL; |
| 378 | } |
| 379 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 380 | if (r < 0) { |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 381 | mutex_unlock(&smd_pkt_devp->rx_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 382 | /* qualify error message */ |
| 383 | if (r != -ERESTARTSYS) { |
| 384 | /* we get this anytime a signal comes in */ |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 385 | pr_err("%s: wait_event_interruptible on smd_pkt_dev" |
| 386 | " id:%d ret %i\n", |
| 387 | __func__, smd_pkt_devp->i, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 388 | } |
| 389 | return r; |
| 390 | } |
| 391 | |
| 392 | /* Here we have a whole packet waiting for us */ |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 393 | pkt_size = smd_cur_packet_size(smd_pkt_devp->ch); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 394 | |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 395 | if (!pkt_size) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 396 | pr_err("%s: No data on smd_pkt_dev id:%d, False wakeup\n", |
| 397 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 398 | mutex_unlock(&smd_pkt_devp->rx_lock); |
| 399 | goto wait_for_packet; |
| 400 | } |
| 401 | |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 402 | if (pkt_size > count) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 403 | pr_err("%s: failure on smd_pkt_dev id: %d - packet size %d" |
| 404 | " > buffer size %d,", __func__, smd_pkt_devp->i, |
| 405 | pkt_size, count); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 406 | mutex_unlock(&smd_pkt_devp->rx_lock); |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 407 | return -ETOOSMALL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 410 | bytes_read = 0; |
| 411 | do { |
| 412 | r = smd_read_user_buffer(smd_pkt_devp->ch, |
| 413 | (buf + bytes_read), |
| 414 | (pkt_size - bytes_read)); |
| 415 | if (r < 0) { |
| 416 | mutex_unlock(&smd_pkt_devp->rx_lock); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 417 | if (smd_pkt_devp->has_reset) { |
Karthikeyan Ramasubramanian | a6dc4b0 | 2012-09-07 14:20:06 -0600 | [diff] [blame] | 418 | E_SMD_PKT_SSR(smd_pkt_devp); |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 419 | return notify_reset(smd_pkt_devp); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 420 | } |
| 421 | pr_err("%s Error while reading %d\n", __func__, r); |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 422 | return r; |
| 423 | } |
| 424 | bytes_read += r; |
| 425 | if (pkt_size != bytes_read) |
| 426 | wait_event(smd_pkt_devp->ch_read_wait_queue, |
| 427 | smd_read_avail(smd_pkt_devp->ch) || |
| 428 | smd_pkt_devp->has_reset); |
| 429 | if (smd_pkt_devp->has_reset) { |
| 430 | mutex_unlock(&smd_pkt_devp->rx_lock); |
Karthikeyan Ramasubramanian | a6dc4b0 | 2012-09-07 14:20:06 -0600 | [diff] [blame] | 431 | E_SMD_PKT_SSR(smd_pkt_devp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 432 | return notify_reset(smd_pkt_devp); |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 433 | } |
| 434 | } while (pkt_size != bytes_read); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 435 | D_READ_DUMP_BUFFER("Read: ", (bytes_read > 16 ? 16 : bytes_read), buf); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 436 | mutex_unlock(&smd_pkt_devp->rx_lock); |
| 437 | |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 438 | mutex_lock(&smd_pkt_devp->ch_lock); |
| 439 | spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags); |
| 440 | if (smd_pkt_devp->poll_mode && |
| 441 | !smd_cur_packet_size(smd_pkt_devp->ch)) { |
| 442 | wake_unlock(&smd_pkt_devp->pa_wake_lock); |
Jeff Hugo | 93476a3 | 2012-06-01 14:32:49 -0600 | [diff] [blame] | 443 | smd_pkt_devp->wakelock_locked = 0; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 444 | smd_pkt_devp->poll_mode = 0; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 445 | D_READ("%s unlocked smd_pkt_dev id:%d wakelock\n", |
| 446 | __func__, smd_pkt_devp->i); |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 447 | } |
| 448 | spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags); |
| 449 | mutex_unlock(&smd_pkt_devp->ch_lock); |
| 450 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 451 | D_READ("Finished %s on smd_pkt_dev id:%d %d bytes\n", |
| 452 | __func__, smd_pkt_devp->i, bytes_read); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 453 | |
| 454 | /* check and wakeup read threads waiting on this device */ |
| 455 | check_and_wakeup_reader(smd_pkt_devp); |
| 456 | |
| 457 | return bytes_read; |
| 458 | } |
| 459 | |
| 460 | ssize_t smd_pkt_write(struct file *file, |
| 461 | const char __user *buf, |
| 462 | size_t count, |
| 463 | loff_t *ppos) |
| 464 | { |
Karthikeyan Ramasubramanian | ce2c115 | 2011-11-07 14:17:41 -0700 | [diff] [blame] | 465 | int r = 0, bytes_written; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 466 | struct smd_pkt_dev *smd_pkt_devp; |
| 467 | DEFINE_WAIT(write_wait); |
| 468 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 469 | smd_pkt_devp = file->private_data; |
| 470 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 471 | if (!smd_pkt_devp) { |
| 472 | pr_err("%s on NULL smd_pkt_dev\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 473 | return -EINVAL; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | if (!smd_pkt_devp->ch) { |
| 477 | pr_err("%s on a closed smd_pkt_dev id:%d\n", |
| 478 | __func__, smd_pkt_devp->i); |
| 479 | return -EINVAL; |
| 480 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 481 | |
Karthikeyan Ramasubramanian | 762e52a | 2012-04-30 12:52:14 -0600 | [diff] [blame] | 482 | if (smd_pkt_devp->do_reset_notification || smd_pkt_devp->has_reset) { |
Karthikeyan Ramasubramanian | a6dc4b0 | 2012-09-07 14:20:06 -0600 | [diff] [blame] | 483 | E_SMD_PKT_SSR(smd_pkt_devp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 484 | /* notify client that a reset occurred */ |
| 485 | return notify_reset(smd_pkt_devp); |
| 486 | } |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 487 | D_WRITE("Begin %s on smd_pkt_dev id:%d data_size %d\n", |
| 488 | __func__, smd_pkt_devp->i, count); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 489 | |
Karthikeyan Ramasubramanian | ce2c115 | 2011-11-07 14:17:41 -0700 | [diff] [blame] | 490 | mutex_lock(&smd_pkt_devp->tx_lock); |
| 491 | if (!smd_pkt_devp->blocking_write) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 492 | if (smd_write_avail(smd_pkt_devp->ch) < count) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 493 | pr_err("%s: Not enough space in smd_pkt_dev id:%d\n", |
| 494 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 495 | mutex_unlock(&smd_pkt_devp->tx_lock); |
| 496 | return -ENOMEM; |
| 497 | } |
| 498 | } |
| 499 | |
Karthikeyan Ramasubramanian | ce2c115 | 2011-11-07 14:17:41 -0700 | [diff] [blame] | 500 | r = smd_write_start(smd_pkt_devp->ch, count); |
| 501 | if (r < 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 502 | mutex_unlock(&smd_pkt_devp->tx_lock); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 503 | pr_err("%s: Error:%d in smd_pkt_dev id:%d @ smd_write_start\n", |
| 504 | __func__, r, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 505 | return r; |
| 506 | } |
Karthikeyan Ramasubramanian | ce2c115 | 2011-11-07 14:17:41 -0700 | [diff] [blame] | 507 | |
| 508 | bytes_written = 0; |
| 509 | do { |
| 510 | prepare_to_wait(&smd_pkt_devp->ch_write_wait_queue, |
| 511 | &write_wait, TASK_UNINTERRUPTIBLE); |
| 512 | if (!smd_write_avail(smd_pkt_devp->ch) && |
| 513 | !smd_pkt_devp->has_reset) { |
| 514 | smd_enable_read_intr(smd_pkt_devp->ch); |
| 515 | schedule(); |
| 516 | } |
| 517 | finish_wait(&smd_pkt_devp->ch_write_wait_queue, &write_wait); |
| 518 | smd_disable_read_intr(smd_pkt_devp->ch); |
| 519 | |
| 520 | if (smd_pkt_devp->has_reset) { |
| 521 | mutex_unlock(&smd_pkt_devp->tx_lock); |
Karthikeyan Ramasubramanian | a6dc4b0 | 2012-09-07 14:20:06 -0600 | [diff] [blame] | 522 | E_SMD_PKT_SSR(smd_pkt_devp); |
Karthikeyan Ramasubramanian | ce2c115 | 2011-11-07 14:17:41 -0700 | [diff] [blame] | 523 | return notify_reset(smd_pkt_devp); |
| 524 | } else { |
| 525 | r = smd_write_segment(smd_pkt_devp->ch, |
| 526 | (void *)(buf + bytes_written), |
| 527 | (count - bytes_written), 1); |
| 528 | if (r < 0) { |
| 529 | mutex_unlock(&smd_pkt_devp->tx_lock); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 530 | if (smd_pkt_devp->has_reset) { |
Karthikeyan Ramasubramanian | a6dc4b0 | 2012-09-07 14:20:06 -0600 | [diff] [blame] | 531 | E_SMD_PKT_SSR(smd_pkt_devp); |
Karthikeyan Ramasubramanian | ce2c115 | 2011-11-07 14:17:41 -0700 | [diff] [blame] | 532 | return notify_reset(smd_pkt_devp); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 533 | } |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 534 | pr_err("%s on smd_pkt_dev id:%d failed r:%d\n", |
| 535 | __func__, smd_pkt_devp->i, r); |
| 536 | return r; |
Karthikeyan Ramasubramanian | ce2c115 | 2011-11-07 14:17:41 -0700 | [diff] [blame] | 537 | } |
| 538 | bytes_written += r; |
| 539 | } |
| 540 | } while (bytes_written != count); |
| 541 | smd_write_end(smd_pkt_devp->ch); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 542 | mutex_unlock(&smd_pkt_devp->tx_lock); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 543 | D_WRITE_DUMP_BUFFER("Write: ", |
| 544 | (bytes_written > 16 ? 16 : bytes_written), buf); |
| 545 | D_WRITE("Finished %s on smd_pkt_dev id:%d %d bytes\n", |
| 546 | __func__, smd_pkt_devp->i, count); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 547 | |
| 548 | return count; |
| 549 | } |
| 550 | |
| 551 | static unsigned int smd_pkt_poll(struct file *file, poll_table *wait) |
| 552 | { |
| 553 | struct smd_pkt_dev *smd_pkt_devp; |
| 554 | unsigned int mask = 0; |
| 555 | |
| 556 | smd_pkt_devp = file->private_data; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 557 | if (!smd_pkt_devp) { |
| 558 | pr_err("%s on a NULL device\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 559 | return POLLERR; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 560 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 561 | |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 562 | smd_pkt_devp->poll_mode = 1; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 563 | poll_wait(file, &smd_pkt_devp->ch_read_wait_queue, wait); |
Karthikeyan Ramasubramanian | 762e52a | 2012-04-30 12:52:14 -0600 | [diff] [blame] | 564 | mutex_lock(&smd_pkt_devp->ch_lock); |
| 565 | if (smd_pkt_devp->has_reset || !smd_pkt_devp->ch) { |
| 566 | mutex_unlock(&smd_pkt_devp->ch_lock); |
Karthikeyan Ramasubramanian | 762e52a | 2012-04-30 12:52:14 -0600 | [diff] [blame] | 567 | return POLLERR; |
| 568 | } |
| 569 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 570 | if (smd_read_avail(smd_pkt_devp->ch)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 571 | mask |= POLLIN | POLLRDNORM; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 572 | D_POLL("%s sets POLLIN for smd_pkt_dev id: %d\n", |
| 573 | __func__, smd_pkt_devp->i); |
| 574 | } |
Karthikeyan Ramasubramanian | 762e52a | 2012-04-30 12:52:14 -0600 | [diff] [blame] | 575 | mutex_unlock(&smd_pkt_devp->ch_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 576 | |
| 577 | return mask; |
| 578 | } |
| 579 | |
| 580 | static void check_and_wakeup_reader(struct smd_pkt_dev *smd_pkt_devp) |
| 581 | { |
| 582 | int sz; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 583 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 584 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 585 | if (!smd_pkt_devp) { |
| 586 | pr_err("%s on a NULL device\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 587 | return; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | if (!smd_pkt_devp->ch) { |
| 591 | pr_err("%s on a closed smd_pkt_dev id:%d\n", |
| 592 | __func__, smd_pkt_devp->i); |
| 593 | return; |
| 594 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 595 | |
| 596 | sz = smd_cur_packet_size(smd_pkt_devp->ch); |
| 597 | if (sz == 0) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 598 | D_READ("%s: No packet in smd_pkt_dev id:%d\n", |
| 599 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 600 | return; |
| 601 | } |
Karthikeyan Ramasubramanian | 703e8a1 | 2011-11-08 16:50:06 -0700 | [diff] [blame] | 602 | if (!smd_read_avail(smd_pkt_devp->ch)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 603 | D_READ("%s: packet size is %d in smd_pkt_dev id:%d -" |
| 604 | " but the data isn't here\n", |
| 605 | __func__, sz, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 606 | return; |
| 607 | } |
| 608 | |
| 609 | /* here we have a packet of size sz ready */ |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 610 | spin_lock_irqsave(&smd_pkt_devp->pa_spinlock, flags); |
| 611 | wake_lock(&smd_pkt_devp->pa_wake_lock); |
Jeff Hugo | 93476a3 | 2012-06-01 14:32:49 -0600 | [diff] [blame] | 612 | smd_pkt_devp->wakelock_locked = 1; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 613 | spin_unlock_irqrestore(&smd_pkt_devp->pa_spinlock, flags); |
Jeff Hugo | 93476a3 | 2012-06-01 14:32:49 -0600 | [diff] [blame] | 614 | wake_up(&smd_pkt_devp->ch_read_wait_queue); |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 615 | schedule_work(&smd_pkt_devp->packet_arrival_work); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 616 | D_READ("%s: wake_up smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | static void check_and_wakeup_writer(struct smd_pkt_dev *smd_pkt_devp) |
| 620 | { |
| 621 | int sz; |
| 622 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 623 | if (!smd_pkt_devp) { |
| 624 | pr_err("%s on a NULL device\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 625 | return; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if (!smd_pkt_devp->ch) { |
| 629 | pr_err("%s on a closed smd_pkt_dev id:%d\n", |
| 630 | __func__, smd_pkt_devp->i); |
| 631 | return; |
| 632 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 633 | |
| 634 | sz = smd_write_avail(smd_pkt_devp->ch); |
Karthikeyan Ramasubramanian | ce2c115 | 2011-11-07 14:17:41 -0700 | [diff] [blame] | 635 | if (sz) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 636 | D_WRITE("%s: %d bytes write space in smd_pkt_dev id:%d\n", |
| 637 | __func__, sz, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 638 | smd_disable_read_intr(smd_pkt_devp->ch); |
Karthikeyan Ramasubramanian | ce2c115 | 2011-11-07 14:17:41 -0700 | [diff] [blame] | 639 | wake_up(&smd_pkt_devp->ch_write_wait_queue); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
| 643 | static void ch_notify(void *priv, unsigned event) |
| 644 | { |
| 645 | struct smd_pkt_dev *smd_pkt_devp = priv; |
| 646 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 647 | if (smd_pkt_devp->ch == 0) { |
Eric Holmberg | dcdaadd | 2013-03-13 18:34:11 -0600 | [diff] [blame] | 648 | if (event != SMD_EVENT_CLOSE) |
| 649 | pr_err("%s on a closed smd_pkt_dev id:%d\n", |
| 650 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 651 | return; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 652 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 653 | |
| 654 | switch (event) { |
| 655 | case SMD_EVENT_DATA: { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 656 | D_STATUS("%s: DATA event in smd_pkt_dev id:%d\n", |
| 657 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 658 | check_and_wakeup_reader(smd_pkt_devp); |
| 659 | if (smd_pkt_devp->blocking_write) |
| 660 | check_and_wakeup_writer(smd_pkt_devp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | case SMD_EVENT_OPEN: |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 664 | D_STATUS("%s: OPEN event in smd_pkt_dev id:%d\n", |
| 665 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 666 | smd_pkt_devp->has_reset = 0; |
| 667 | smd_pkt_devp->is_open = 1; |
| 668 | wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue); |
| 669 | break; |
| 670 | case SMD_EVENT_CLOSE: |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 671 | D_STATUS("%s: CLOSE event in smd_pkt_dev id:%d\n", |
| 672 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 673 | smd_pkt_devp->is_open = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 674 | /* put port into reset state */ |
| 675 | clean_and_signal(smd_pkt_devp); |
| 676 | if (smd_pkt_devp->i == LOOPBACK_INX) |
| 677 | schedule_delayed_work(&loopback_work, |
| 678 | msecs_to_jiffies(1000)); |
| 679 | break; |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | #ifdef CONFIG_ARCH_FSM9XXX |
| 684 | static char *smd_pkt_dev_name[] = { |
| 685 | "smdcntl1", |
| 686 | "smdcntl2", |
| 687 | "smd22", |
| 688 | "smd_pkt_loopback", |
| 689 | }; |
| 690 | |
| 691 | static char *smd_ch_name[] = { |
| 692 | "DATA6_CNTL", |
| 693 | "DATA7_CNTL", |
| 694 | "DATA22", |
| 695 | "LOOPBACK", |
| 696 | }; |
| 697 | |
| 698 | static uint32_t smd_ch_edge[] = { |
| 699 | SMD_APPS_QDSP, |
| 700 | SMD_APPS_QDSP, |
| 701 | SMD_APPS_QDSP, |
| 702 | SMD_APPS_QDSP |
| 703 | }; |
| 704 | #else |
| 705 | static char *smd_pkt_dev_name[] = { |
| 706 | "smdcntl0", |
| 707 | "smdcntl1", |
| 708 | "smdcntl2", |
| 709 | "smdcntl3", |
| 710 | "smdcntl4", |
| 711 | "smdcntl5", |
| 712 | "smdcntl6", |
| 713 | "smdcntl7", |
| 714 | "smd22", |
Arun Kumar Neelakantam | 04a680a | 2012-10-31 13:48:00 +0530 | [diff] [blame] | 715 | "smdcnt_rev0", |
| 716 | "smdcnt_rev1", |
| 717 | "smdcnt_rev2", |
| 718 | "smdcnt_rev3", |
| 719 | "smdcnt_rev4", |
| 720 | "smdcnt_rev5", |
| 721 | "smdcnt_rev6", |
| 722 | "smdcnt_rev7", |
| 723 | "smdcnt_rev8", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 724 | "smd_sns_dsps", |
Angshuman Sarkar | a18a272 | 2011-07-29 13:41:13 +0530 | [diff] [blame] | 725 | "apr_apps2", |
Karthikeyan Ramasubramanian | d08e59a | 2012-03-01 17:35:34 -0700 | [diff] [blame] | 726 | "smdcntl8", |
Jeff Hugo | df5ee32 | 2012-04-16 11:56:12 -0600 | [diff] [blame] | 727 | "smd_sns_adsp", |
Karthikeyan Ramasubramanian | b7e7273 | 2012-05-22 12:31:31 -0600 | [diff] [blame] | 728 | "smd_cxm_qmi", |
Eric Holmberg | c1e645e | 2013-02-11 19:17:52 -0700 | [diff] [blame] | 729 | "smd_test_framework", |
| 730 | "smd_logging_0", |
| 731 | "smd_data_0", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 732 | "smd_pkt_loopback", |
| 733 | }; |
| 734 | |
| 735 | static char *smd_ch_name[] = { |
| 736 | "DATA5_CNTL", |
| 737 | "DATA6_CNTL", |
| 738 | "DATA7_CNTL", |
| 739 | "DATA8_CNTL", |
| 740 | "DATA9_CNTL", |
| 741 | "DATA12_CNTL", |
| 742 | "DATA13_CNTL", |
| 743 | "DATA14_CNTL", |
| 744 | "DATA22", |
Arun Kumar Neelakantam | 04a680a | 2012-10-31 13:48:00 +0530 | [diff] [blame] | 745 | "DATA23_CNTL", |
| 746 | "DATA24_CNTL", |
| 747 | "DATA25_CNTL", |
| 748 | "DATA26_CNTL", |
| 749 | "DATA27_CNTL", |
| 750 | "DATA28_CNTL", |
| 751 | "DATA29_CNTL", |
| 752 | "DATA30_CNTL", |
| 753 | "DATA31_CNTL", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 754 | "SENSOR", |
Angshuman Sarkar | a18a272 | 2011-07-29 13:41:13 +0530 | [diff] [blame] | 755 | "apr_apps2", |
Karthikeyan Ramasubramanian | d08e59a | 2012-03-01 17:35:34 -0700 | [diff] [blame] | 756 | "DATA40_CNTL", |
Jeff Hugo | df5ee32 | 2012-04-16 11:56:12 -0600 | [diff] [blame] | 757 | "SENSOR", |
Karthikeyan Ramasubramanian | b7e7273 | 2012-05-22 12:31:31 -0600 | [diff] [blame] | 758 | "CXM_QMI_PORT_8064", |
Eric Holmberg | c1e645e | 2013-02-11 19:17:52 -0700 | [diff] [blame] | 759 | "TESTFRAMEWORK", |
| 760 | "LOGGING", |
| 761 | "DATA", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 762 | "LOOPBACK", |
| 763 | }; |
| 764 | |
| 765 | static uint32_t smd_ch_edge[] = { |
| 766 | SMD_APPS_MODEM, |
| 767 | SMD_APPS_MODEM, |
| 768 | SMD_APPS_MODEM, |
| 769 | SMD_APPS_MODEM, |
| 770 | SMD_APPS_MODEM, |
| 771 | SMD_APPS_MODEM, |
| 772 | SMD_APPS_MODEM, |
| 773 | SMD_APPS_MODEM, |
| 774 | SMD_APPS_MODEM, |
Arun Kumar Neelakantam | 04a680a | 2012-10-31 13:48:00 +0530 | [diff] [blame] | 775 | SMD_APPS_MODEM, |
| 776 | SMD_APPS_MODEM, |
| 777 | SMD_APPS_MODEM, |
| 778 | SMD_APPS_MODEM, |
| 779 | SMD_APPS_MODEM, |
| 780 | SMD_APPS_MODEM, |
| 781 | SMD_APPS_MODEM, |
| 782 | SMD_APPS_MODEM, |
| 783 | SMD_APPS_MODEM, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 784 | SMD_APPS_DSPS, |
| 785 | SMD_APPS_QDSP, |
| 786 | SMD_APPS_MODEM, |
Jeff Hugo | df5ee32 | 2012-04-16 11:56:12 -0600 | [diff] [blame] | 787 | SMD_APPS_QDSP, |
Karthikeyan Ramasubramanian | b7e7273 | 2012-05-22 12:31:31 -0600 | [diff] [blame] | 788 | SMD_APPS_WCNSS, |
Eric Holmberg | c1e645e | 2013-02-11 19:17:52 -0700 | [diff] [blame] | 789 | SMD_APPS_QDSP, |
| 790 | SMD_APPS_QDSP, |
| 791 | SMD_APPS_QDSP, |
Karthikeyan Ramasubramanian | d08e59a | 2012-03-01 17:35:34 -0700 | [diff] [blame] | 792 | SMD_APPS_MODEM, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 793 | }; |
| 794 | #endif |
Jeff Hugo | 223fd45 | 2012-07-18 14:35:45 -0600 | [diff] [blame] | 795 | module_param_named(loopback_edge, smd_ch_edge[LOOPBACK_INX], |
| 796 | int, S_IRUGO | S_IWUSR | S_IWGRP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 797 | |
| 798 | static int smd_pkt_dummy_probe(struct platform_device *pdev) |
| 799 | { |
| 800 | int i; |
| 801 | |
| 802 | for (i = 0; i < NUM_SMD_PKT_PORTS; i++) { |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 803 | if (smd_ch_edge[i] == pdev->id |
| 804 | && !strncmp(pdev->name, smd_ch_name[i], |
| 805 | SMD_MAX_CH_NAME_LEN) |
| 806 | && smd_pkt_devp[i]->driver.probe) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 807 | complete_all(&smd_pkt_devp[i]->ch_allocated); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 808 | D_STATUS("%s allocated SMD ch for smd_pkt_dev id:%d\n", |
| 809 | __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 810 | break; |
| 811 | } |
| 812 | } |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static uint32_t is_modem_smsm_inited(void) |
| 817 | { |
| 818 | uint32_t modem_state; |
| 819 | uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT); |
| 820 | |
| 821 | modem_state = smsm_get_state(SMSM_MODEM_STATE); |
| 822 | return (modem_state & ready_state) == ready_state; |
| 823 | } |
| 824 | |
| 825 | int smd_pkt_open(struct inode *inode, struct file *file) |
| 826 | { |
| 827 | int r = 0; |
| 828 | struct smd_pkt_dev *smd_pkt_devp; |
Eric Holmberg | cce6daf | 2012-02-27 14:34:02 -0700 | [diff] [blame] | 829 | const char *peripheral = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 830 | |
| 831 | smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev); |
| 832 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 833 | if (!smd_pkt_devp) { |
| 834 | pr_err("%s on a NULL device\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 835 | return -EINVAL; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 836 | } |
| 837 | D_STATUS("Begin %s on smd_pkt_dev id:%d\n", __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 838 | |
| 839 | file->private_data = smd_pkt_devp; |
| 840 | |
| 841 | mutex_lock(&smd_pkt_devp->ch_lock); |
| 842 | if (smd_pkt_devp->ch == 0) { |
Eric Holmberg | b399a4f | 2012-07-06 11:42:24 -0600 | [diff] [blame] | 843 | wake_lock_init(&smd_pkt_devp->pa_wake_lock, WAKE_LOCK_SUSPEND, |
| 844 | smd_pkt_dev_name[smd_pkt_devp->i]); |
| 845 | INIT_WORK(&smd_pkt_devp->packet_arrival_work, |
| 846 | packet_arrival_worker); |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 847 | init_completion(&smd_pkt_devp->ch_allocated); |
| 848 | smd_pkt_devp->driver.probe = smd_pkt_dummy_probe; |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 849 | scnprintf(smd_pkt_devp->pdriver_name, PDRIVER_NAME_MAX_SIZE, |
Karthikeyan Ramasubramanian | 61df193 | 2012-06-28 12:10:41 -0600 | [diff] [blame] | 850 | "%s", smd_ch_name[smd_pkt_devp->i]); |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 851 | smd_pkt_devp->driver.driver.name = smd_pkt_devp->pdriver_name; |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 852 | smd_pkt_devp->driver.driver.owner = THIS_MODULE; |
| 853 | r = platform_driver_register(&smd_pkt_devp->driver); |
| 854 | if (r) { |
| 855 | pr_err("%s: %s Platform driver reg. failed\n", |
| 856 | __func__, smd_ch_name[smd_pkt_devp->i]); |
| 857 | goto out; |
| 858 | } |
| 859 | |
Eric Holmberg | cce6daf | 2012-02-27 14:34:02 -0700 | [diff] [blame] | 860 | peripheral = smd_edge_to_subsystem( |
| 861 | smd_ch_edge[smd_pkt_devp->i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 862 | if (peripheral) { |
Stephen Boyd | 77db8bb | 2012-06-27 15:15:16 -0700 | [diff] [blame] | 863 | smd_pkt_devp->pil = subsystem_get(peripheral); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 864 | if (IS_ERR(smd_pkt_devp->pil)) { |
| 865 | r = PTR_ERR(smd_pkt_devp->pil); |
Stephen Boyd | 77db8bb | 2012-06-27 15:15:16 -0700 | [diff] [blame] | 866 | pr_err("%s failed on smd_pkt_dev id:%d - subsystem_get failed for %s\n", |
| 867 | __func__, smd_pkt_devp->i, peripheral); |
Karthikeyan Ramasubramanian | 608a2bc | 2012-12-21 18:52:33 -0700 | [diff] [blame] | 868 | /* |
| 869 | * Sleep inorder to reduce the frequency of |
| 870 | * retry by user-space modules and to avoid |
| 871 | * possible watchdog bite. |
| 872 | */ |
| 873 | msleep((smd_pkt_devp->open_modem_wait * 1000)); |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 874 | goto release_pd; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /* Wait for the modem SMSM to be inited for the SMD |
| 878 | ** Loopback channel to be allocated at the modem. Since |
| 879 | ** the wait need to be done atmost once, using msleep |
| 880 | ** doesn't degrade the performance. */ |
Jeff Hugo | a5ee436 | 2011-07-15 13:48:48 -0600 | [diff] [blame] | 881 | if (!strncmp(smd_ch_name[smd_pkt_devp->i], "LOOPBACK", |
| 882 | SMD_MAX_CH_NAME_LEN)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 883 | if (!is_modem_smsm_inited()) |
| 884 | msleep(5000); |
| 885 | smsm_change_state(SMSM_APPS_STATE, |
| 886 | 0, SMSM_SMD_LOOPBACK); |
| 887 | msleep(100); |
| 888 | } |
| 889 | |
| 890 | /* |
| 891 | * Wait for a packet channel to be allocated so we know |
| 892 | * the modem is ready enough. |
| 893 | */ |
| 894 | if (smd_pkt_devp->open_modem_wait) { |
| 895 | r = wait_for_completion_interruptible_timeout( |
| 896 | &smd_pkt_devp->ch_allocated, |
| 897 | msecs_to_jiffies( |
| 898 | smd_pkt_devp->open_modem_wait |
| 899 | * 1000)); |
| 900 | if (r == 0) |
| 901 | r = -ETIMEDOUT; |
| 902 | if (r < 0) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 903 | pr_err("%s: wait on smd_pkt_dev id:%d" |
| 904 | " allocation failed rc:%d\n", |
| 905 | __func__, smd_pkt_devp->i, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 906 | goto release_pil; |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | r = smd_named_open_on_edge(smd_ch_name[smd_pkt_devp->i], |
| 912 | smd_ch_edge[smd_pkt_devp->i], |
| 913 | &smd_pkt_devp->ch, |
| 914 | smd_pkt_devp, |
| 915 | ch_notify); |
| 916 | if (r < 0) { |
| 917 | pr_err("%s: %s open failed %d\n", __func__, |
| 918 | smd_ch_name[smd_pkt_devp->i], r); |
| 919 | goto release_pil; |
| 920 | } |
| 921 | |
| 922 | r = wait_event_interruptible_timeout( |
| 923 | smd_pkt_devp->ch_opened_wait_queue, |
| 924 | smd_pkt_devp->is_open, (2 * HZ)); |
Jeff Hugo | 4d9fdf3 | 2012-04-30 11:10:54 -0600 | [diff] [blame] | 925 | if (r == 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 926 | r = -ETIMEDOUT; |
Jeff Hugo | 4d9fdf3 | 2012-04-30 11:10:54 -0600 | [diff] [blame] | 927 | /* close the ch to sync smd's state with smd_pkt */ |
| 928 | smd_close(smd_pkt_devp->ch); |
| 929 | smd_pkt_devp->ch = NULL; |
| 930 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 931 | |
| 932 | if (r < 0) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 933 | pr_err("%s: wait on smd_pkt_dev id:%d OPEN event failed" |
| 934 | " rc:%d\n", __func__, smd_pkt_devp->i, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 935 | } else if (!smd_pkt_devp->is_open) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 936 | pr_err("%s: Invalid OPEN event on smd_pkt_dev id:%d\n", |
| 937 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 938 | r = -ENODEV; |
| 939 | } else { |
| 940 | smd_disable_read_intr(smd_pkt_devp->ch); |
| 941 | smd_pkt_devp->ch_size = |
| 942 | smd_write_avail(smd_pkt_devp->ch); |
| 943 | r = 0; |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 944 | smd_pkt_devp->ref_cnt++; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 945 | D_STATUS("Finished %s on smd_pkt_dev id:%d\n", |
| 946 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 947 | } |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 948 | } else { |
| 949 | smd_pkt_devp->ref_cnt++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 950 | } |
| 951 | release_pil: |
| 952 | if (peripheral && (r < 0)) |
Stephen Boyd | 77db8bb | 2012-06-27 15:15:16 -0700 | [diff] [blame] | 953 | subsystem_put(smd_pkt_devp->pil); |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 954 | |
| 955 | release_pd: |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 956 | if (r < 0) { |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 957 | platform_driver_unregister(&smd_pkt_devp->driver); |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 958 | smd_pkt_devp->driver.probe = NULL; |
| 959 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 960 | out: |
Eric Holmberg | b399a4f | 2012-07-06 11:42:24 -0600 | [diff] [blame] | 961 | if (!smd_pkt_devp->ch) |
| 962 | wake_lock_destroy(&smd_pkt_devp->pa_wake_lock); |
| 963 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 964 | mutex_unlock(&smd_pkt_devp->ch_lock); |
| 965 | |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 966 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 967 | return r; |
| 968 | } |
| 969 | |
| 970 | int smd_pkt_release(struct inode *inode, struct file *file) |
| 971 | { |
| 972 | int r = 0; |
| 973 | struct smd_pkt_dev *smd_pkt_devp = file->private_data; |
| 974 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 975 | if (!smd_pkt_devp) { |
| 976 | pr_err("%s on a NULL device\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 977 | return -EINVAL; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 978 | } |
| 979 | D_STATUS("Begin %s on smd_pkt_dev id:%d\n", |
| 980 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 981 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 982 | mutex_lock(&smd_pkt_devp->ch_lock); |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 983 | mutex_lock(&smd_pkt_devp->rx_lock); |
| 984 | mutex_lock(&smd_pkt_devp->tx_lock); |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 985 | if (smd_pkt_devp->ref_cnt > 0) |
| 986 | smd_pkt_devp->ref_cnt--; |
| 987 | |
| 988 | if (smd_pkt_devp->ch != 0 && smd_pkt_devp->ref_cnt == 0) { |
| 989 | clean_and_signal(smd_pkt_devp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 990 | r = smd_close(smd_pkt_devp->ch); |
| 991 | smd_pkt_devp->ch = 0; |
| 992 | smd_pkt_devp->blocking_write = 0; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 993 | smd_pkt_devp->poll_mode = 0; |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 994 | platform_driver_unregister(&smd_pkt_devp->driver); |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 995 | smd_pkt_devp->driver.probe = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 996 | if (smd_pkt_devp->pil) |
Stephen Boyd | 77db8bb | 2012-06-27 15:15:16 -0700 | [diff] [blame] | 997 | subsystem_put(smd_pkt_devp->pil); |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 998 | smd_pkt_devp->has_reset = 0; |
| 999 | smd_pkt_devp->do_reset_notification = 0; |
| 1000 | smd_pkt_devp->wakelock_locked = 0; |
| 1001 | wake_lock_destroy(&smd_pkt_devp->pa_wake_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1002 | } |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 1003 | mutex_unlock(&smd_pkt_devp->tx_lock); |
| 1004 | mutex_unlock(&smd_pkt_devp->rx_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1005 | mutex_unlock(&smd_pkt_devp->ch_lock); |
| 1006 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1007 | D_STATUS("Finished %s on smd_pkt_dev id:%d\n", |
| 1008 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1009 | |
| 1010 | return r; |
| 1011 | } |
| 1012 | |
| 1013 | static const struct file_operations smd_pkt_fops = { |
| 1014 | .owner = THIS_MODULE, |
| 1015 | .open = smd_pkt_open, |
| 1016 | .release = smd_pkt_release, |
| 1017 | .read = smd_pkt_read, |
| 1018 | .write = smd_pkt_write, |
| 1019 | .poll = smd_pkt_poll, |
| 1020 | .unlocked_ioctl = smd_pkt_ioctl, |
| 1021 | }; |
| 1022 | |
| 1023 | static int __init smd_pkt_init(void) |
| 1024 | { |
| 1025 | int i; |
| 1026 | int r; |
| 1027 | |
| 1028 | r = alloc_chrdev_region(&smd_pkt_number, |
| 1029 | 0, |
| 1030 | NUM_SMD_PKT_PORTS, |
| 1031 | DEVICE_NAME); |
| 1032 | if (IS_ERR_VALUE(r)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1033 | pr_err("%s: alloc_chrdev_region() failed ret:%i\n", |
| 1034 | __func__, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1035 | goto error0; |
| 1036 | } |
| 1037 | |
| 1038 | smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME); |
| 1039 | if (IS_ERR(smd_pkt_classp)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1040 | pr_err("%s: class_create() failed ENOMEM\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1041 | r = -ENOMEM; |
| 1042 | goto error1; |
| 1043 | } |
| 1044 | |
| 1045 | for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) { |
| 1046 | smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev), |
| 1047 | GFP_KERNEL); |
| 1048 | if (IS_ERR(smd_pkt_devp[i])) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1049 | pr_err("%s: kzalloc() failed for smd_pkt_dev id:%d\n", |
| 1050 | __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1051 | r = -ENOMEM; |
| 1052 | goto error2; |
| 1053 | } |
| 1054 | |
| 1055 | smd_pkt_devp[i]->i = i; |
| 1056 | |
| 1057 | init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue); |
| 1058 | init_waitqueue_head(&smd_pkt_devp[i]->ch_write_wait_queue); |
| 1059 | smd_pkt_devp[i]->is_open = 0; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 1060 | smd_pkt_devp[i]->poll_mode = 0; |
Jeff Hugo | 93476a3 | 2012-06-01 14:32:49 -0600 | [diff] [blame] | 1061 | smd_pkt_devp[i]->wakelock_locked = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1062 | init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue); |
| 1063 | |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 1064 | spin_lock_init(&smd_pkt_devp[i]->pa_spinlock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1065 | mutex_init(&smd_pkt_devp[i]->ch_lock); |
| 1066 | mutex_init(&smd_pkt_devp[i]->rx_lock); |
| 1067 | mutex_init(&smd_pkt_devp[i]->tx_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1068 | |
| 1069 | cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops); |
| 1070 | smd_pkt_devp[i]->cdev.owner = THIS_MODULE; |
| 1071 | |
| 1072 | r = cdev_add(&smd_pkt_devp[i]->cdev, |
| 1073 | (smd_pkt_number + i), |
| 1074 | 1); |
| 1075 | |
| 1076 | if (IS_ERR_VALUE(r)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1077 | pr_err("%s: cdev_add() failed for smd_pkt_dev id:%d" |
| 1078 | " ret:%i\n", __func__, i, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1079 | kfree(smd_pkt_devp[i]); |
| 1080 | goto error2; |
| 1081 | } |
| 1082 | |
| 1083 | smd_pkt_devp[i]->devicep = |
| 1084 | device_create(smd_pkt_classp, |
| 1085 | NULL, |
| 1086 | (smd_pkt_number + i), |
| 1087 | NULL, |
| 1088 | smd_pkt_dev_name[i]); |
| 1089 | |
| 1090 | if (IS_ERR(smd_pkt_devp[i]->devicep)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1091 | pr_err("%s: device_create() failed for smd_pkt_dev" |
| 1092 | " id:%d\n", __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1093 | r = -ENOMEM; |
| 1094 | cdev_del(&smd_pkt_devp[i]->cdev); |
| 1095 | kfree(smd_pkt_devp[i]); |
| 1096 | goto error2; |
| 1097 | } |
| 1098 | if (device_create_file(smd_pkt_devp[i]->devicep, |
| 1099 | &dev_attr_open_timeout)) |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1100 | pr_err("%s: unable to create device attr for" |
| 1101 | " smd_pkt_dev id:%d\n", __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker); |
| 1105 | |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 1106 | smd_pkt_ilctxt = ipc_log_context_create(SMD_PKT_IPC_LOG_PAGE_CNT, |
| 1107 | "smd_pkt"); |
| 1108 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1109 | D_STATUS("SMD Packet Port Driver Initialized.\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1110 | return 0; |
| 1111 | |
| 1112 | error2: |
| 1113 | if (i > 0) { |
| 1114 | while (--i >= 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1115 | cdev_del(&smd_pkt_devp[i]->cdev); |
| 1116 | kfree(smd_pkt_devp[i]); |
| 1117 | device_destroy(smd_pkt_classp, |
| 1118 | MKDEV(MAJOR(smd_pkt_number), i)); |
| 1119 | } |
| 1120 | } |
| 1121 | |
| 1122 | class_destroy(smd_pkt_classp); |
| 1123 | error1: |
| 1124 | unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS); |
| 1125 | error0: |
| 1126 | return r; |
| 1127 | } |
| 1128 | |
| 1129 | static void __exit smd_pkt_cleanup(void) |
| 1130 | { |
| 1131 | int i; |
| 1132 | |
| 1133 | for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1134 | cdev_del(&smd_pkt_devp[i]->cdev); |
| 1135 | kfree(smd_pkt_devp[i]); |
| 1136 | device_destroy(smd_pkt_classp, |
| 1137 | MKDEV(MAJOR(smd_pkt_number), i)); |
| 1138 | } |
| 1139 | |
| 1140 | class_destroy(smd_pkt_classp); |
| 1141 | |
| 1142 | unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS); |
| 1143 | } |
| 1144 | |
| 1145 | module_init(smd_pkt_init); |
| 1146 | module_exit(smd_pkt_cleanup); |
| 1147 | |
| 1148 | MODULE_DESCRIPTION("MSM Shared Memory Packet Port"); |
| 1149 | MODULE_LICENSE("GPL v2"); |