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