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