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