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