Jeff Hugo | 31f83b4 | 2012-01-25 15:15:26 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2008-2012, Code Aurora Forum. 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> |
| 37 | #include <mach/peripheral-loader.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 |
Karthikeyan Ramasubramanian | b7e7273 | 2012-05-22 12:31:31 -0600 | [diff] [blame] | 44 | #define NUM_SMD_PKT_PORTS 15 |
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", |
| 714 | "smd_sns_dsps", |
Angshuman Sarkar | a18a272 | 2011-07-29 13:41:13 +0530 | [diff] [blame] | 715 | "apr_apps2", |
Karthikeyan Ramasubramanian | d08e59a | 2012-03-01 17:35:34 -0700 | [diff] [blame] | 716 | "smdcntl8", |
Jeff Hugo | df5ee32 | 2012-04-16 11:56:12 -0600 | [diff] [blame] | 717 | "smd_sns_adsp", |
Karthikeyan Ramasubramanian | b7e7273 | 2012-05-22 12:31:31 -0600 | [diff] [blame] | 718 | "smd_cxm_qmi", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 719 | "smd_pkt_loopback", |
| 720 | }; |
| 721 | |
| 722 | static char *smd_ch_name[] = { |
| 723 | "DATA5_CNTL", |
| 724 | "DATA6_CNTL", |
| 725 | "DATA7_CNTL", |
| 726 | "DATA8_CNTL", |
| 727 | "DATA9_CNTL", |
| 728 | "DATA12_CNTL", |
| 729 | "DATA13_CNTL", |
| 730 | "DATA14_CNTL", |
| 731 | "DATA22", |
| 732 | "SENSOR", |
Angshuman Sarkar | a18a272 | 2011-07-29 13:41:13 +0530 | [diff] [blame] | 733 | "apr_apps2", |
Karthikeyan Ramasubramanian | d08e59a | 2012-03-01 17:35:34 -0700 | [diff] [blame] | 734 | "DATA40_CNTL", |
Jeff Hugo | df5ee32 | 2012-04-16 11:56:12 -0600 | [diff] [blame] | 735 | "SENSOR", |
Karthikeyan Ramasubramanian | b7e7273 | 2012-05-22 12:31:31 -0600 | [diff] [blame] | 736 | "CXM_QMI_PORT_8064", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 737 | "LOOPBACK", |
| 738 | }; |
| 739 | |
| 740 | static uint32_t smd_ch_edge[] = { |
| 741 | SMD_APPS_MODEM, |
| 742 | SMD_APPS_MODEM, |
| 743 | SMD_APPS_MODEM, |
| 744 | SMD_APPS_MODEM, |
| 745 | SMD_APPS_MODEM, |
| 746 | SMD_APPS_MODEM, |
| 747 | SMD_APPS_MODEM, |
| 748 | SMD_APPS_MODEM, |
| 749 | SMD_APPS_MODEM, |
| 750 | SMD_APPS_DSPS, |
| 751 | SMD_APPS_QDSP, |
| 752 | SMD_APPS_MODEM, |
Jeff Hugo | df5ee32 | 2012-04-16 11:56:12 -0600 | [diff] [blame] | 753 | SMD_APPS_QDSP, |
Karthikeyan Ramasubramanian | b7e7273 | 2012-05-22 12:31:31 -0600 | [diff] [blame] | 754 | SMD_APPS_WCNSS, |
Karthikeyan Ramasubramanian | d08e59a | 2012-03-01 17:35:34 -0700 | [diff] [blame] | 755 | SMD_APPS_MODEM, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 756 | }; |
| 757 | #endif |
Jeff Hugo | 223fd45 | 2012-07-18 14:35:45 -0600 | [diff] [blame] | 758 | module_param_named(loopback_edge, smd_ch_edge[LOOPBACK_INX], |
| 759 | int, S_IRUGO | S_IWUSR | S_IWGRP); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 760 | |
| 761 | static int smd_pkt_dummy_probe(struct platform_device *pdev) |
| 762 | { |
| 763 | int i; |
| 764 | |
| 765 | for (i = 0; i < NUM_SMD_PKT_PORTS; i++) { |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 766 | if (smd_ch_edge[i] == pdev->id |
| 767 | && !strncmp(pdev->name, smd_ch_name[i], |
| 768 | SMD_MAX_CH_NAME_LEN) |
| 769 | && smd_pkt_devp[i]->driver.probe) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 770 | complete_all(&smd_pkt_devp[i]->ch_allocated); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 771 | D_STATUS("%s allocated SMD ch for smd_pkt_dev id:%d\n", |
| 772 | __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 773 | break; |
| 774 | } |
| 775 | } |
| 776 | return 0; |
| 777 | } |
| 778 | |
| 779 | static uint32_t is_modem_smsm_inited(void) |
| 780 | { |
| 781 | uint32_t modem_state; |
| 782 | uint32_t ready_state = (SMSM_INIT | SMSM_SMDINIT); |
| 783 | |
| 784 | modem_state = smsm_get_state(SMSM_MODEM_STATE); |
| 785 | return (modem_state & ready_state) == ready_state; |
| 786 | } |
| 787 | |
| 788 | int smd_pkt_open(struct inode *inode, struct file *file) |
| 789 | { |
| 790 | int r = 0; |
| 791 | struct smd_pkt_dev *smd_pkt_devp; |
Eric Holmberg | cce6daf | 2012-02-27 14:34:02 -0700 | [diff] [blame] | 792 | const char *peripheral = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 793 | |
| 794 | smd_pkt_devp = container_of(inode->i_cdev, struct smd_pkt_dev, cdev); |
| 795 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 796 | if (!smd_pkt_devp) { |
| 797 | pr_err("%s on a NULL device\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 798 | return -EINVAL; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 799 | } |
| 800 | 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] | 801 | |
| 802 | file->private_data = smd_pkt_devp; |
| 803 | |
| 804 | mutex_lock(&smd_pkt_devp->ch_lock); |
| 805 | if (smd_pkt_devp->ch == 0) { |
Eric Holmberg | b399a4f | 2012-07-06 11:42:24 -0600 | [diff] [blame] | 806 | wake_lock_init(&smd_pkt_devp->pa_wake_lock, WAKE_LOCK_SUSPEND, |
| 807 | smd_pkt_dev_name[smd_pkt_devp->i]); |
| 808 | INIT_WORK(&smd_pkt_devp->packet_arrival_work, |
| 809 | packet_arrival_worker); |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 810 | init_completion(&smd_pkt_devp->ch_allocated); |
| 811 | smd_pkt_devp->driver.probe = smd_pkt_dummy_probe; |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 812 | scnprintf(smd_pkt_devp->pdriver_name, PDRIVER_NAME_MAX_SIZE, |
Karthikeyan Ramasubramanian | 61df193 | 2012-06-28 12:10:41 -0600 | [diff] [blame] | 813 | "%s", smd_ch_name[smd_pkt_devp->i]); |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 814 | smd_pkt_devp->driver.driver.name = smd_pkt_devp->pdriver_name; |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 815 | smd_pkt_devp->driver.driver.owner = THIS_MODULE; |
| 816 | r = platform_driver_register(&smd_pkt_devp->driver); |
| 817 | if (r) { |
| 818 | pr_err("%s: %s Platform driver reg. failed\n", |
| 819 | __func__, smd_ch_name[smd_pkt_devp->i]); |
| 820 | goto out; |
| 821 | } |
| 822 | |
Eric Holmberg | cce6daf | 2012-02-27 14:34:02 -0700 | [diff] [blame] | 823 | peripheral = smd_edge_to_subsystem( |
| 824 | smd_ch_edge[smd_pkt_devp->i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 825 | if (peripheral) { |
| 826 | smd_pkt_devp->pil = pil_get(peripheral); |
| 827 | if (IS_ERR(smd_pkt_devp->pil)) { |
| 828 | r = PTR_ERR(smd_pkt_devp->pil); |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 829 | pr_err("%s failed on smd_pkt_dev id:%d -" |
| 830 | " pil_get failed for %s\n", __func__, |
| 831 | smd_pkt_devp->i, peripheral); |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 832 | goto release_pd; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /* Wait for the modem SMSM to be inited for the SMD |
| 836 | ** Loopback channel to be allocated at the modem. Since |
| 837 | ** the wait need to be done atmost once, using msleep |
| 838 | ** doesn't degrade the performance. */ |
Jeff Hugo | a5ee436 | 2011-07-15 13:48:48 -0600 | [diff] [blame] | 839 | if (!strncmp(smd_ch_name[smd_pkt_devp->i], "LOOPBACK", |
| 840 | SMD_MAX_CH_NAME_LEN)) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 841 | if (!is_modem_smsm_inited()) |
| 842 | msleep(5000); |
| 843 | smsm_change_state(SMSM_APPS_STATE, |
| 844 | 0, SMSM_SMD_LOOPBACK); |
| 845 | msleep(100); |
| 846 | } |
| 847 | |
| 848 | /* |
| 849 | * Wait for a packet channel to be allocated so we know |
| 850 | * the modem is ready enough. |
| 851 | */ |
| 852 | if (smd_pkt_devp->open_modem_wait) { |
| 853 | r = wait_for_completion_interruptible_timeout( |
| 854 | &smd_pkt_devp->ch_allocated, |
| 855 | msecs_to_jiffies( |
| 856 | smd_pkt_devp->open_modem_wait |
| 857 | * 1000)); |
| 858 | if (r == 0) |
| 859 | r = -ETIMEDOUT; |
| 860 | if (r < 0) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 861 | pr_err("%s: wait on smd_pkt_dev id:%d" |
| 862 | " allocation failed rc:%d\n", |
| 863 | __func__, smd_pkt_devp->i, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 864 | goto release_pil; |
| 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | r = smd_named_open_on_edge(smd_ch_name[smd_pkt_devp->i], |
| 870 | smd_ch_edge[smd_pkt_devp->i], |
| 871 | &smd_pkt_devp->ch, |
| 872 | smd_pkt_devp, |
| 873 | ch_notify); |
| 874 | if (r < 0) { |
| 875 | pr_err("%s: %s open failed %d\n", __func__, |
| 876 | smd_ch_name[smd_pkt_devp->i], r); |
| 877 | goto release_pil; |
| 878 | } |
| 879 | |
| 880 | r = wait_event_interruptible_timeout( |
| 881 | smd_pkt_devp->ch_opened_wait_queue, |
| 882 | smd_pkt_devp->is_open, (2 * HZ)); |
Jeff Hugo | 4d9fdf3 | 2012-04-30 11:10:54 -0600 | [diff] [blame] | 883 | if (r == 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 884 | r = -ETIMEDOUT; |
Jeff Hugo | 4d9fdf3 | 2012-04-30 11:10:54 -0600 | [diff] [blame] | 885 | /* close the ch to sync smd's state with smd_pkt */ |
| 886 | smd_close(smd_pkt_devp->ch); |
| 887 | smd_pkt_devp->ch = NULL; |
| 888 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 889 | |
| 890 | if (r < 0) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 891 | pr_err("%s: wait on smd_pkt_dev id:%d OPEN event failed" |
| 892 | " rc:%d\n", __func__, smd_pkt_devp->i, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 893 | } else if (!smd_pkt_devp->is_open) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 894 | pr_err("%s: Invalid OPEN event on smd_pkt_dev id:%d\n", |
| 895 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 896 | r = -ENODEV; |
| 897 | } else { |
| 898 | smd_disable_read_intr(smd_pkt_devp->ch); |
| 899 | smd_pkt_devp->ch_size = |
| 900 | smd_write_avail(smd_pkt_devp->ch); |
| 901 | r = 0; |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 902 | smd_pkt_devp->ref_cnt++; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 903 | D_STATUS("Finished %s on smd_pkt_dev id:%d\n", |
| 904 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 905 | } |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 906 | } else { |
| 907 | smd_pkt_devp->ref_cnt++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 908 | } |
| 909 | release_pil: |
| 910 | if (peripheral && (r < 0)) |
| 911 | pil_put(smd_pkt_devp->pil); |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 912 | |
| 913 | release_pd: |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 914 | if (r < 0) { |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 915 | platform_driver_unregister(&smd_pkt_devp->driver); |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 916 | smd_pkt_devp->driver.probe = NULL; |
| 917 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 918 | out: |
Eric Holmberg | b399a4f | 2012-07-06 11:42:24 -0600 | [diff] [blame] | 919 | if (!smd_pkt_devp->ch) |
| 920 | wake_lock_destroy(&smd_pkt_devp->pa_wake_lock); |
| 921 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 922 | mutex_unlock(&smd_pkt_devp->ch_lock); |
| 923 | |
Eric Holmberg | c3c5cd9 | 2012-02-07 18:19:49 -0700 | [diff] [blame] | 924 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 925 | return r; |
| 926 | } |
| 927 | |
| 928 | int smd_pkt_release(struct inode *inode, struct file *file) |
| 929 | { |
| 930 | int r = 0; |
| 931 | struct smd_pkt_dev *smd_pkt_devp = file->private_data; |
| 932 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 933 | if (!smd_pkt_devp) { |
| 934 | pr_err("%s on a NULL device\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 935 | return -EINVAL; |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 936 | } |
| 937 | D_STATUS("Begin %s on smd_pkt_dev id:%d\n", |
| 938 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 939 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 940 | mutex_lock(&smd_pkt_devp->ch_lock); |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 941 | mutex_lock(&smd_pkt_devp->rx_lock); |
| 942 | mutex_lock(&smd_pkt_devp->tx_lock); |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 943 | if (smd_pkt_devp->ref_cnt > 0) |
| 944 | smd_pkt_devp->ref_cnt--; |
| 945 | |
| 946 | if (smd_pkt_devp->ch != 0 && smd_pkt_devp->ref_cnt == 0) { |
| 947 | clean_and_signal(smd_pkt_devp); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 948 | r = smd_close(smd_pkt_devp->ch); |
| 949 | smd_pkt_devp->ch = 0; |
| 950 | smd_pkt_devp->blocking_write = 0; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 951 | smd_pkt_devp->poll_mode = 0; |
Karthikeyan Ramasubramanian | 63a913c | 2012-03-06 15:43:48 -0700 | [diff] [blame] | 952 | platform_driver_unregister(&smd_pkt_devp->driver); |
Karthikeyan Ramasubramanian | 5fa8825 | 2012-04-19 23:45:08 -0600 | [diff] [blame] | 953 | smd_pkt_devp->driver.probe = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 954 | if (smd_pkt_devp->pil) |
| 955 | pil_put(smd_pkt_devp->pil); |
Karthikeyan Ramasubramanian | 540dbf3 | 2012-09-14 13:16:50 -0600 | [diff] [blame] | 956 | smd_pkt_devp->has_reset = 0; |
| 957 | smd_pkt_devp->do_reset_notification = 0; |
| 958 | smd_pkt_devp->wakelock_locked = 0; |
| 959 | wake_lock_destroy(&smd_pkt_devp->pa_wake_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 960 | } |
Karthikeyan Ramasubramanian | 96a14fb | 2012-04-30 12:04:35 -0600 | [diff] [blame] | 961 | mutex_unlock(&smd_pkt_devp->tx_lock); |
| 962 | mutex_unlock(&smd_pkt_devp->rx_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 963 | mutex_unlock(&smd_pkt_devp->ch_lock); |
| 964 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 965 | D_STATUS("Finished %s on smd_pkt_dev id:%d\n", |
| 966 | __func__, smd_pkt_devp->i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 967 | |
| 968 | return r; |
| 969 | } |
| 970 | |
| 971 | static const struct file_operations smd_pkt_fops = { |
| 972 | .owner = THIS_MODULE, |
| 973 | .open = smd_pkt_open, |
| 974 | .release = smd_pkt_release, |
| 975 | .read = smd_pkt_read, |
| 976 | .write = smd_pkt_write, |
| 977 | .poll = smd_pkt_poll, |
| 978 | .unlocked_ioctl = smd_pkt_ioctl, |
| 979 | }; |
| 980 | |
| 981 | static int __init smd_pkt_init(void) |
| 982 | { |
| 983 | int i; |
| 984 | int r; |
| 985 | |
| 986 | r = alloc_chrdev_region(&smd_pkt_number, |
| 987 | 0, |
| 988 | NUM_SMD_PKT_PORTS, |
| 989 | DEVICE_NAME); |
| 990 | if (IS_ERR_VALUE(r)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 991 | pr_err("%s: alloc_chrdev_region() failed ret:%i\n", |
| 992 | __func__, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 993 | goto error0; |
| 994 | } |
| 995 | |
| 996 | smd_pkt_classp = class_create(THIS_MODULE, DEVICE_NAME); |
| 997 | if (IS_ERR(smd_pkt_classp)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 998 | pr_err("%s: class_create() failed ENOMEM\n", __func__); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 999 | r = -ENOMEM; |
| 1000 | goto error1; |
| 1001 | } |
| 1002 | |
| 1003 | for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) { |
| 1004 | smd_pkt_devp[i] = kzalloc(sizeof(struct smd_pkt_dev), |
| 1005 | GFP_KERNEL); |
| 1006 | if (IS_ERR(smd_pkt_devp[i])) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1007 | pr_err("%s: kzalloc() failed for smd_pkt_dev id:%d\n", |
| 1008 | __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1009 | r = -ENOMEM; |
| 1010 | goto error2; |
| 1011 | } |
| 1012 | |
| 1013 | smd_pkt_devp[i]->i = i; |
| 1014 | |
| 1015 | init_waitqueue_head(&smd_pkt_devp[i]->ch_read_wait_queue); |
| 1016 | init_waitqueue_head(&smd_pkt_devp[i]->ch_write_wait_queue); |
| 1017 | smd_pkt_devp[i]->is_open = 0; |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 1018 | smd_pkt_devp[i]->poll_mode = 0; |
Jeff Hugo | 93476a3 | 2012-06-01 14:32:49 -0600 | [diff] [blame] | 1019 | smd_pkt_devp[i]->wakelock_locked = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1020 | init_waitqueue_head(&smd_pkt_devp[i]->ch_opened_wait_queue); |
| 1021 | |
Karthikeyan Ramasubramanian | 048ef38 | 2012-02-15 10:49:10 -0700 | [diff] [blame] | 1022 | spin_lock_init(&smd_pkt_devp[i]->pa_spinlock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1023 | mutex_init(&smd_pkt_devp[i]->ch_lock); |
| 1024 | mutex_init(&smd_pkt_devp[i]->rx_lock); |
| 1025 | mutex_init(&smd_pkt_devp[i]->tx_lock); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1026 | |
| 1027 | cdev_init(&smd_pkt_devp[i]->cdev, &smd_pkt_fops); |
| 1028 | smd_pkt_devp[i]->cdev.owner = THIS_MODULE; |
| 1029 | |
| 1030 | r = cdev_add(&smd_pkt_devp[i]->cdev, |
| 1031 | (smd_pkt_number + i), |
| 1032 | 1); |
| 1033 | |
| 1034 | if (IS_ERR_VALUE(r)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1035 | pr_err("%s: cdev_add() failed for smd_pkt_dev id:%d" |
| 1036 | " ret:%i\n", __func__, i, r); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1037 | kfree(smd_pkt_devp[i]); |
| 1038 | goto error2; |
| 1039 | } |
| 1040 | |
| 1041 | smd_pkt_devp[i]->devicep = |
| 1042 | device_create(smd_pkt_classp, |
| 1043 | NULL, |
| 1044 | (smd_pkt_number + i), |
| 1045 | NULL, |
| 1046 | smd_pkt_dev_name[i]); |
| 1047 | |
| 1048 | if (IS_ERR(smd_pkt_devp[i]->devicep)) { |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1049 | pr_err("%s: device_create() failed for smd_pkt_dev" |
| 1050 | " id:%d\n", __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1051 | r = -ENOMEM; |
| 1052 | cdev_del(&smd_pkt_devp[i]->cdev); |
| 1053 | kfree(smd_pkt_devp[i]); |
| 1054 | goto error2; |
| 1055 | } |
| 1056 | if (device_create_file(smd_pkt_devp[i]->devicep, |
| 1057 | &dev_attr_open_timeout)) |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1058 | pr_err("%s: unable to create device attr for" |
| 1059 | " smd_pkt_dev id:%d\n", __func__, i); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | INIT_DELAYED_WORK(&loopback_work, loopback_probe_worker); |
| 1063 | |
Karthikeyan Ramasubramanian | 085bd62 | 2012-09-10 21:45:00 -0600 | [diff] [blame] | 1064 | smd_pkt_ilctxt = ipc_log_context_create(SMD_PKT_IPC_LOG_PAGE_CNT, |
| 1065 | "smd_pkt"); |
| 1066 | |
Karthikeyan Ramasubramanian | 05142ff | 2012-03-30 17:39:24 -0600 | [diff] [blame] | 1067 | D_STATUS("SMD Packet Port Driver Initialized.\n"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1068 | return 0; |
| 1069 | |
| 1070 | error2: |
| 1071 | if (i > 0) { |
| 1072 | while (--i >= 0) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1073 | cdev_del(&smd_pkt_devp[i]->cdev); |
| 1074 | kfree(smd_pkt_devp[i]); |
| 1075 | device_destroy(smd_pkt_classp, |
| 1076 | MKDEV(MAJOR(smd_pkt_number), i)); |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | class_destroy(smd_pkt_classp); |
| 1081 | error1: |
| 1082 | unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS); |
| 1083 | error0: |
| 1084 | return r; |
| 1085 | } |
| 1086 | |
| 1087 | static void __exit smd_pkt_cleanup(void) |
| 1088 | { |
| 1089 | int i; |
| 1090 | |
| 1091 | for (i = 0; i < NUM_SMD_PKT_PORTS; ++i) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1092 | cdev_del(&smd_pkt_devp[i]->cdev); |
| 1093 | kfree(smd_pkt_devp[i]); |
| 1094 | device_destroy(smd_pkt_classp, |
| 1095 | MKDEV(MAJOR(smd_pkt_number), i)); |
| 1096 | } |
| 1097 | |
| 1098 | class_destroy(smd_pkt_classp); |
| 1099 | |
| 1100 | unregister_chrdev_region(MAJOR(smd_pkt_number), NUM_SMD_PKT_PORTS); |
| 1101 | } |
| 1102 | |
| 1103 | module_init(smd_pkt_init); |
| 1104 | module_exit(smd_pkt_cleanup); |
| 1105 | |
| 1106 | MODULE_DESCRIPTION("MSM Shared Memory Packet Port"); |
| 1107 | MODULE_LICENSE("GPL v2"); |