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