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) { |
| 648 | pr_err("%s on a closed smd_pkt_dev id:%d\n", |
| 649 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 650 | return; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 651 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 652 | |
| 653 | switch (event) { |
| 654 | case SMD_EVENT_DATA: { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 655 | D_STATUS("%s: DATA event in smd_pkt_dev id:%d\n", |
| 656 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 657 | check_and_wakeup_reader(smd_pkt_devp); |
| 658 | if (smd_pkt_devp->blocking_write) |
| 659 | check_and_wakeup_writer(smd_pkt_devp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 660 | break; |
| 661 | } |
| 662 | case SMD_EVENT_OPEN: |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 663 | D_STATUS("%s: OPEN event in smd_pkt_dev id:%d\n", |
| 664 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 665 | smd_pkt_devp->has_reset = 0; |
| 666 | smd_pkt_devp->is_open = 1; |
| 667 | wake_up_interruptible(&smd_pkt_devp->ch_opened_wait_queue); |
| 668 | break; |
| 669 | case SMD_EVENT_CLOSE: |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 670 | D_STATUS("%s: CLOSE event in smd_pkt_dev id:%d\n", |
| 671 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 672 | smd_pkt_devp->is_open = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 673 | /* put port into reset state */ |
| 674 | clean_and_signal(smd_pkt_devp); |
| 675 | if (smd_pkt_devp->i == LOOPBACK_INX) |
| 676 | schedule_delayed_work(&loopback_work, |
| 677 | msecs_to_jiffies(1000)); |
| 678 | break; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | #ifdef CONFIG_ARCH_FSM9XXX |
| 683 | static char *smd_pkt_dev_name[] = { |
| 684 | "smdcntl1", |
| 685 | "smdcntl2", |
| 686 | "smd22", |
| 687 | "smd_pkt_loopback", |
| 688 | }; |
| 689 | |
| 690 | static char *smd_ch_name[] = { |
| 691 | "DATA6_CNTL", |
| 692 | "DATA7_CNTL", |
| 693 | "DATA22", |
| 694 | "LOOPBACK", |
| 695 | }; |
| 696 | |
| 697 | static uint32_t smd_ch_edge[] = { |
| 698 | SMD_APPS_QDSP, |
| 699 | SMD_APPS_QDSP, |
| 700 | SMD_APPS_QDSP, |
| 701 | SMD_APPS_QDSP |
| 702 | }; |
| 703 | #else |
| 704 | static char *smd_pkt_dev_name[] = { |
| 705 | "smdcntl0", |
| 706 | "smdcntl1", |
| 707 | "smdcntl2", |
| 708 | "smdcntl3", |
| 709 | "smdcntl4", |
| 710 | "smdcntl5", |
| 711 | "smdcntl6", |
| 712 | "smdcntl7", |
| 713 | "smd22", |
Arun Kumar Neelakantam | 04a680a | 2012-10-31 13:48:00 +0530 | [diff] [blame] | 714 | "smdcnt_rev0", |
| 715 | "smdcnt_rev1", |
| 716 | "smdcnt_rev2", |
| 717 | "smdcnt_rev3", |
| 718 | "smdcnt_rev4", |
| 719 | "smdcnt_rev5", |
| 720 | "smdcnt_rev6", |
| 721 | "smdcnt_rev7", |
| 722 | "smdcnt_rev8", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 723 | "smd_sns_dsps", |
Angshuman Sarkar | a18a272 | 2011-07-29 13:41:13 +0530 | [diff] [blame] | 724 | "apr_apps2", |
Karthikeyan Ramasubramanian | d08e59a | 2012-03-01 17:35:34 -0700 | [diff] [blame] | 725 | "smdcntl8", |
Jeff Hugo | df5ee32 | 2012-04-16 11:56:12 -0600 | [diff] [blame] | 726 | "smd_sns_adsp", |
Karthikeyan Ramasubramanian | b7e7273 | 2012-05-22 12:31:31 -0600 | [diff] [blame] | 727 | "smd_cxm_qmi", |
Eric Holmberg | c1e645e | 2013-02-11 19:17:52 -0700 | [diff] [blame^] | 728 | "smd_test_framework", |
| 729 | "smd_logging_0", |
| 730 | "smd_data_0", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 731 | "smd_pkt_loopback", |
| 732 | }; |
| 733 | |
| 734 | static char *smd_ch_name[] = { |
| 735 | "DATA5_CNTL", |
| 736 | "DATA6_CNTL", |
| 737 | "DATA7_CNTL", |
| 738 | "DATA8_CNTL", |
| 739 | "DATA9_CNTL", |
| 740 | "DATA12_CNTL", |
| 741 | "DATA13_CNTL", |
| 742 | "DATA14_CNTL", |
| 743 | "DATA22", |
Arun Kumar Neelakantam | 04a680a | 2012-10-31 13:48:00 +0530 | [diff] [blame] | 744 | "DATA23_CNTL", |
| 745 | "DATA24_CNTL", |
| 746 | "DATA25_CNTL", |
| 747 | "DATA26_CNTL", |
| 748 | "DATA27_CNTL", |
| 749 | "DATA28_CNTL", |
| 750 | "DATA29_CNTL", |
| 751 | "DATA30_CNTL", |
| 752 | "DATA31_CNTL", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 753 | "SENSOR", |
Angshuman Sarkar | a18a272 | 2011-07-29 13:41:13 +0530 | [diff] [blame] | 754 | "apr_apps2", |
Karthikeyan Ramasubramanian | d08e59a | 2012-03-01 17:35:34 -0700 | [diff] [blame] | 755 | "DATA40_CNTL", |
Jeff Hugo | df5ee32 | 2012-04-16 11:56:12 -0600 | [diff] [blame] | 756 | "SENSOR", |
Karthikeyan Ramasubramanian | b7e7273 | 2012-05-22 12:31:31 -0600 | [diff] [blame] | 757 | "CXM_QMI_PORT_8064", |
Eric Holmberg | c1e645e | 2013-02-11 19:17:52 -0700 | [diff] [blame^] | 758 | "TESTFRAMEWORK", |
| 759 | "LOGGING", |
| 760 | "DATA", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 761 | "LOOPBACK", |
| 762 | }; |
| 763 | |
| 764 | static uint32_t smd_ch_edge[] = { |
| 765 | SMD_APPS_MODEM, |
| 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, |
Arun Kumar Neelakantam | 04a680a | 2012-10-31 13:48:00 +0530 | [diff] [blame] | 774 | SMD_APPS_MODEM, |
| 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, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 783 | SMD_APPS_DSPS, |
| 784 | SMD_APPS_QDSP, |
| 785 | SMD_APPS_MODEM, |
Jeff Hugo | df5ee32 | 2012-04-16 11:56:12 -0600 | [diff] [blame] | 786 | SMD_APPS_QDSP, |
Karthikeyan Ramasubramanian | b7e7273 | 2012-05-22 12:31:31 -0600 | [diff] [blame] | 787 | SMD_APPS_WCNSS, |
Eric Holmberg | c1e645e | 2013-02-11 19:17:52 -0700 | [diff] [blame^] | 788 | SMD_APPS_QDSP, |
| 789 | SMD_APPS_QDSP, |
| 790 | SMD_APPS_QDSP, |
Karthikeyan Ramasubramanian | d08e59a | 2012-03-01 17:35:34 -0700 | [diff] [blame] | 791 | SMD_APPS_MODEM, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 792 | }; |
| 793 | #endif |
Jeff Hugo | 223fd45 | 2012-07-18 14:35:45 -0600 | [diff] [blame] | 794 | module_param_named(loopback_edge, smd_ch_edge[LOOPBACK_INX], |
| 795 | int, S_IRUGO | S_IWUSR | S_IWGRP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 796 | |
| 797 | static int smd_pkt_dummy_probe(struct platform_device *pdev) |
| 798 | { |
| 799 | int i; |
| 800 | |
| 801 | for (i = 0; i < NUM_SMD_PKT_PORTS; i++) { |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 802 | if (smd_ch_edge[i] == pdev->id |
| 803 | && !strncmp(pdev->name, smd_ch_name[i], |
| 804 | SMD_MAX_CH_NAME_LEN) |
| 805 | && smd_pkt_devp[i]->driver.probe) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 806 | complete_all(&smd_pkt_devp[i]->ch_allocated); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 807 | D_STATUS("%s allocated SMD ch for smd_pkt_dev id:%d\n", |
| 808 | __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 809 | break; |
| 810 | } |
| 811 | } |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | static uint32_t is_modem_smsm_inited(void) |
| 816 | { |
| 817 | uint32_t modem_state; |
| 818 | uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT); |
| 819 | |
| 820 | modem_state = smsm_get_state(SMSM_MODEM_STATE); |
| 821 | return (modem_state & ready_state) == ready_state; |
| 822 | } |
| 823 | |
| 824 | int smd_pkt_open(struct inode *inode, struct file *file) |
| 825 | { |
| 826 | int r = 0; |
| 827 | struct smd_pkt_dev *smd_pkt_devp; |
Eric Holmberg | cce6daf | 2012-02-27 14:34:02 -0700 | [diff] [blame] | 828 | const char *peripheral = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 829 | |
| 830 | smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev); |
| 831 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 832 | if (!smd_pkt_devp) { |
| 833 | pr_err("%s on a NULL device\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 834 | return -EINVAL; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 835 | } |
| 836 | 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] | 837 | |
| 838 | file->private_data = smd_pkt_devp; |
| 839 | |
| 840 | mutex_lock(&smd_pkt_devp->ch_lock); |
| 841 | if (smd_pkt_devp->ch == 0) { |
Eric Holmberg | b399a4f | 2012-07-06 11:42:24 -0600 | [diff] [blame] | 842 | wake_lock_init(&smd_pkt_devp->pa_wake_lock, WAKE_LOCK_SUSPEND, |
| 843 | smd_pkt_dev_name[smd_pkt_devp->i]); |
| 844 | INIT_WORK(&smd_pkt_devp->packet_arrival_work, |
| 845 | packet_arrival_worker); |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 846 | init_completion(&smd_pkt_devp->ch_allocated); |
| 847 | smd_pkt_devp->driver.probe = smd_pkt_dummy_probe; |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 848 | scnprintf(smd_pkt_devp->pdriver_name, PDRIVER_NAME_MAX_SIZE, |
Karthikeyan Ramasubramanian | 61df193 | 2012-06-28 12:10:41 -0600 | [diff] [blame] | 849 | "%s", smd_ch_name[smd_pkt_devp->i]); |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 850 | smd_pkt_devp->driver.driver.name = smd_pkt_devp->pdriver_name; |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 851 | smd_pkt_devp->driver.driver.owner = THIS_MODULE; |
| 852 | r = platform_driver_register(&smd_pkt_devp->driver); |
| 853 | if (r) { |
| 854 | pr_err("%s: %s Platform driver reg. failed\n", |
| 855 | __func__, smd_ch_name[smd_pkt_devp->i]); |
| 856 | goto out; |
| 857 | } |
| 858 | |
Eric Holmberg | cce6daf | 2012-02-27 14:34:02 -0700 | [diff] [blame] | 859 | peripheral = smd_edge_to_subsystem( |
| 860 | smd_ch_edge[smd_pkt_devp->i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 861 | if (peripheral) { |
Stephen Boyd | 77db8bb | 2012-06-27 15:15:16 -0700 | [diff] [blame] | 862 | smd_pkt_devp->pil = subsystem_get(peripheral); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 863 | if (IS_ERR(smd_pkt_devp->pil)) { |
| 864 | r = PTR_ERR(smd_pkt_devp->pil); |
Stephen Boyd | 77db8bb | 2012-06-27 15:15:16 -0700 | [diff] [blame] | 865 | pr_err("%s failed on smd_pkt_dev id:%d - subsystem_get failed for %s\n", |
| 866 | __func__, smd_pkt_devp->i, peripheral); |
Karthikeyan Ramasubramanian | 608a2bc | 2012-12-21 18:52:33 -0700 | [diff] [blame] | 867 | /* |
| 868 | * Sleep inorder to reduce the frequency of |
| 869 | * retry by user-space modules and to avoid |
| 870 | * possible watchdog bite. |
| 871 | */ |
| 872 | msleep((smd_pkt_devp->open_modem_wait * 1000)); |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 873 | goto release_pd; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | /* Wait for the modem SMSM to be inited for the SMD |
| 877 | ** Loopback channel to be allocated at the modem. Since |
| 878 | ** the wait need to be done atmost once, using msleep |
| 879 | ** doesn't degrade the performance. */ |
Jeff Hugo | a5ee436 | 2011-07-15 13:48:48 -0600 | [diff] [blame] | 880 | if (!strncmp(smd_ch_name[smd_pkt_devp->i], "LOOPBACK", |
| 881 | SMD_MAX_CH_NAME_LEN)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 882 | if (!is_modem_smsm_inited()) |
| 883 | msleep(5000); |
| 884 | smsm_change_state(SMSM_APPS_STATE, |
| 885 | 0, SMSM_SMD_LOOPBACK); |
| 886 | msleep(100); |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | * Wait for a packet channel to be allocated so we know |
| 891 | * the modem is ready enough. |
| 892 | */ |
| 893 | if (smd_pkt_devp->open_modem_wait) { |
| 894 | r = wait_for_completion_interruptible_timeout( |
| 895 | &smd_pkt_devp->ch_allocated, |
| 896 | msecs_to_jiffies( |
| 897 | smd_pkt_devp->open_modem_wait |
| 898 | * 1000)); |
| 899 | if (r == 0) |
| 900 | r = -ETIMEDOUT; |
| 901 | if (r < 0) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 902 | pr_err("%s: wait on smd_pkt_dev id:%d" |
| 903 | " allocation failed rc:%d\n", |
| 904 | __func__, smd_pkt_devp->i, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 905 | goto release_pil; |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | r = smd_named_open_on_edge(smd_ch_name[smd_pkt_devp->i], |
| 911 | smd_ch_edge[smd_pkt_devp->i], |
| 912 | &smd_pkt_devp->ch, |
| 913 | smd_pkt_devp, |
| 914 | ch_notify); |
| 915 | if (r < 0) { |
| 916 | pr_err("%s: %s open failed %d\n", __func__, |
| 917 | smd_ch_name[smd_pkt_devp->i], r); |
| 918 | goto release_pil; |
| 919 | } |
| 920 | |
| 921 | r = wait_event_interruptible_timeout( |
| 922 | smd_pkt_devp->ch_opened_wait_queue, |
| 923 | smd_pkt_devp->is_open, (2 * HZ)); |
Jeff Hugo | 4d9fdf3 | 2012-04-30 11:10:54 -0600 | [diff] [blame] | 924 | if (r == 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 925 | r = -ETIMEDOUT; |
Jeff Hugo | 4d9fdf3 | 2012-04-30 11:10:54 -0600 | [diff] [blame] | 926 | /* close the ch to sync smd's state with smd_pkt */ |
| 927 | smd_close(smd_pkt_devp->ch); |
| 928 | smd_pkt_devp->ch = NULL; |
| 929 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 930 | |
| 931 | if (r < 0) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 932 | pr_err("%s: wait on smd_pkt_dev id:%d OPEN event failed" |
| 933 | " rc:%d\n", __func__, smd_pkt_devp->i, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 934 | } else if (!smd_pkt_devp->is_open) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 935 | pr_err("%s: Invalid OPEN event on smd_pkt_dev id:%d\n", |
| 936 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 937 | r = -ENODEV; |
| 938 | } else { |
| 939 | smd_disable_read_intr(smd_pkt_devp->ch); |
| 940 | smd_pkt_devp->ch_size = |
| 941 | smd_write_avail(smd_pkt_devp->ch); |
| 942 | r = 0; |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 943 | smd_pkt_devp->ref_cnt++; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 944 | D_STATUS("Finished %s on smd_pkt_dev id:%d\n", |
| 945 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 946 | } |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 947 | } else { |
| 948 | smd_pkt_devp->ref_cnt++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 949 | } |
| 950 | release_pil: |
| 951 | if (peripheral && (r < 0)) |
Stephen Boyd | 77db8bb | 2012-06-27 15:15:16 -0700 | [diff] [blame] | 952 | subsystem_put(smd_pkt_devp->pil); |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 953 | |
| 954 | release_pd: |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 955 | if (r < 0) { |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 956 | platform_driver_unregister(&smd_pkt_devp->driver); |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 957 | smd_pkt_devp->driver.probe = NULL; |
| 958 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 959 | out: |
Eric Holmberg | b399a4f | 2012-07-06 11:42:24 -0600 | [diff] [blame] | 960 | if (!smd_pkt_devp->ch) |
| 961 | wake_lock_destroy(&smd_pkt_devp->pa_wake_lock); |
| 962 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 963 | mutex_unlock(&smd_pkt_devp->ch_lock); |
| 964 | |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 965 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 966 | return r; |
| 967 | } |
| 968 | |
| 969 | int smd_pkt_release(struct inode *inode, struct file *file) |
| 970 | { |
| 971 | int r = 0; |
| 972 | struct smd_pkt_dev *smd_pkt_devp = file->private_data; |
| 973 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 974 | if (!smd_pkt_devp) { |
| 975 | pr_err("%s on a NULL device\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 976 | return -EINVAL; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 977 | } |
| 978 | D_STATUS("Begin %s on smd_pkt_dev id:%d\n", |
| 979 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 980 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 981 | mutex_lock(&smd_pkt_devp->ch_lock); |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 982 | mutex_lock(&smd_pkt_devp->rx_lock); |
| 983 | mutex_lock(&smd_pkt_devp->tx_lock); |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 984 | if (smd_pkt_devp->ref_cnt > 0) |
| 985 | smd_pkt_devp->ref_cnt--; |
| 986 | |
| 987 | if (smd_pkt_devp->ch != 0 && smd_pkt_devp->ref_cnt == 0) { |
| 988 | clean_and_signal(smd_pkt_devp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 989 | r = smd_close(smd_pkt_devp->ch); |
| 990 | smd_pkt_devp->ch = 0; |
| 991 | smd_pkt_devp->blocking_write = 0; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 992 | smd_pkt_devp->poll_mode = 0; |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 993 | platform_driver_unregister(&smd_pkt_devp->driver); |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 994 | smd_pkt_devp->driver.probe = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 995 | if (smd_pkt_devp->pil) |
Stephen Boyd | 77db8bb | 2012-06-27 15:15:16 -0700 | [diff] [blame] | 996 | subsystem_put(smd_pkt_devp->pil); |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 997 | smd_pkt_devp->has_reset = 0; |
| 998 | smd_pkt_devp->do_reset_notification = 0; |
| 999 | smd_pkt_devp->wakelock_locked = 0; |
| 1000 | wake_lock_destroy(&smd_pkt_devp->pa_wake_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1001 | } |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 1002 | mutex_unlock(&smd_pkt_devp->tx_lock); |
| 1003 | mutex_unlock(&smd_pkt_devp->rx_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1004 | mutex_unlock(&smd_pkt_devp->ch_lock); |
| 1005 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1006 | D_STATUS("Finished %s on smd_pkt_dev id:%d\n", |
| 1007 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1008 | |
| 1009 | return r; |
| 1010 | } |
| 1011 | |
| 1012 | static const struct file_operations smd_pkt_fops = { |
| 1013 | .owner = THIS_MODULE, |
| 1014 | .open = smd_pkt_open, |
| 1015 | .release = smd_pkt_release, |
| 1016 | .read = smd_pkt_read, |
| 1017 | .write = smd_pkt_write, |
| 1018 | .poll = smd_pkt_poll, |
| 1019 | .unlocked_ioctl = smd_pkt_ioctl, |
| 1020 | }; |
| 1021 | |
| 1022 | static int __init smd_pkt_init(void) |
| 1023 | { |
| 1024 | int i; |
| 1025 | int r; |
| 1026 | |
| 1027 | r = alloc_chrdev_region(&smd_pkt_number, |
| 1028 | 0, |
| 1029 | NUM_SMD_PKT_PORTS, |
| 1030 | DEVICE_NAME); |
| 1031 | if (IS_ERR_VALUE(r)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1032 | pr_err("%s: alloc_chrdev_region() failed ret:%i\n", |
| 1033 | __func__, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1034 | goto error0; |
| 1035 | } |
| 1036 | |
| 1037 | smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME); |
| 1038 | if (IS_ERR(smd_pkt_classp)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1039 | pr_err("%s: class_create() failed ENOMEM\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1040 | r = -ENOMEM; |
| 1041 | goto error1; |
| 1042 | } |
| 1043 | |
| 1044 | for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) { |
| 1045 | smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev), |
| 1046 | GFP_KERNEL); |
| 1047 | if (IS_ERR(smd_pkt_devp[i])) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1048 | pr_err("%s: kzalloc() failed for smd_pkt_dev id:%d\n", |
| 1049 | __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1050 | r = -ENOMEM; |
| 1051 | goto error2; |
| 1052 | } |
| 1053 | |
| 1054 | smd_pkt_devp[i]->i = i; |
| 1055 | |
| 1056 | init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue); |
| 1057 | init_waitqueue_head(&smd_pkt_devp[i]->ch_write_wait_queue); |
| 1058 | smd_pkt_devp[i]->is_open = 0; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 1059 | smd_pkt_devp[i]->poll_mode = 0; |
Jeff Hugo | 93476a3 | 2012-06-01 14:32:49 -0600 | [diff] [blame] | 1060 | smd_pkt_devp[i]->wakelock_locked = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1061 | init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue); |
| 1062 | |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 1063 | spin_lock_init(&smd_pkt_devp[i]->pa_spinlock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1064 | mutex_init(&smd_pkt_devp[i]->ch_lock); |
| 1065 | mutex_init(&smd_pkt_devp[i]->rx_lock); |
| 1066 | mutex_init(&smd_pkt_devp[i]->tx_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1067 | |
| 1068 | cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops); |
| 1069 | smd_pkt_devp[i]->cdev.owner = THIS_MODULE; |
| 1070 | |
| 1071 | r = cdev_add(&smd_pkt_devp[i]->cdev, |
| 1072 | (smd_pkt_number + i), |
| 1073 | 1); |
| 1074 | |
| 1075 | if (IS_ERR_VALUE(r)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1076 | pr_err("%s: cdev_add() failed for smd_pkt_dev id:%d" |
| 1077 | " ret:%i\n", __func__, i, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1078 | kfree(smd_pkt_devp[i]); |
| 1079 | goto error2; |
| 1080 | } |
| 1081 | |
| 1082 | smd_pkt_devp[i]->devicep = |
| 1083 | device_create(smd_pkt_classp, |
| 1084 | NULL, |
| 1085 | (smd_pkt_number + i), |
| 1086 | NULL, |
| 1087 | smd_pkt_dev_name[i]); |
| 1088 | |
| 1089 | if (IS_ERR(smd_pkt_devp[i]->devicep)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1090 | pr_err("%s: device_create() failed for smd_pkt_dev" |
| 1091 | " id:%d\n", __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1092 | r = -ENOMEM; |
| 1093 | cdev_del(&smd_pkt_devp[i]->cdev); |
| 1094 | kfree(smd_pkt_devp[i]); |
| 1095 | goto error2; |
| 1096 | } |
| 1097 | if (device_create_file(smd_pkt_devp[i]->devicep, |
| 1098 | &dev_attr_open_timeout)) |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1099 | pr_err("%s: unable to create device attr for" |
| 1100 | " smd_pkt_dev id:%d\n", __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker); |
| 1104 | |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 1105 | smd_pkt_ilctxt = ipc_log_context_create(SMD_PKT_IPC_LOG_PAGE_CNT, |
| 1106 | "smd_pkt"); |
| 1107 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1108 | D_STATUS("SMD Packet Port Driver Initialized.\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1109 | return 0; |
| 1110 | |
| 1111 | error2: |
| 1112 | if (i > 0) { |
| 1113 | while (--i >= 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1114 | cdev_del(&smd_pkt_devp[i]->cdev); |
| 1115 | kfree(smd_pkt_devp[i]); |
| 1116 | device_destroy(smd_pkt_classp, |
| 1117 | MKDEV(MAJOR(smd_pkt_number), i)); |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | class_destroy(smd_pkt_classp); |
| 1122 | error1: |
| 1123 | unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS); |
| 1124 | error0: |
| 1125 | return r; |
| 1126 | } |
| 1127 | |
| 1128 | static void __exit smd_pkt_cleanup(void) |
| 1129 | { |
| 1130 | int i; |
| 1131 | |
| 1132 | for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1133 | cdev_del(&smd_pkt_devp[i]->cdev); |
| 1134 | kfree(smd_pkt_devp[i]); |
| 1135 | device_destroy(smd_pkt_classp, |
| 1136 | MKDEV(MAJOR(smd_pkt_number), i)); |
| 1137 | } |
| 1138 | |
| 1139 | class_destroy(smd_pkt_classp); |
| 1140 | |
| 1141 | unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS); |
| 1142 | } |
| 1143 | |
| 1144 | module_init(smd_pkt_init); |
| 1145 | module_exit(smd_pkt_cleanup); |
| 1146 | |
| 1147 | MODULE_DESCRIPTION("MSM Shared Memory Packet Port"); |
| 1148 | MODULE_LICENSE("GPL v2"); |