Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame^] | 1 | /* Copyright (c) 2010-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 | * SDIO Control Driver -- Provides a binary SDIO muxed control port |
| 15 | * interface. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/cdev.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/device.h> |
| 22 | #include <linux/delay.h> |
| 23 | #include <linux/sched.h> |
| 24 | #include <linux/spinlock.h> |
| 25 | #include <linux/mutex.h> |
| 26 | #include <linux/uaccess.h> |
| 27 | #include <linux/workqueue.h> |
| 28 | #include <asm/ioctls.h> |
| 29 | #include <linux/platform_device.h> |
| 30 | #include <mach/msm_smd.h> |
| 31 | #include <mach/sdio_al.h> |
| 32 | #include <mach/sdio_cmux.h> |
| 33 | #include "modem_notifier.h" |
| 34 | #include <linux/slab.h> |
| 35 | |
| 36 | #define MAX_WRITE_RETRY 5 |
| 37 | #define MAGIC_NO_V1 0x33FC |
| 38 | #define NUM_SDIO_CTL_PORTS 9 |
| 39 | #define DEVICE_NAME "sdioctl" |
| 40 | #define MAX_BUF_SIZE 2048 |
| 41 | #define DEBUG |
| 42 | |
| 43 | static int msm_sdio_ctl_debug_mask; |
| 44 | module_param_named(debug_mask, msm_sdio_ctl_debug_mask, |
| 45 | int, S_IRUGO | S_IWUSR | S_IWGRP); |
| 46 | |
| 47 | struct sdio_ctl_dev { |
| 48 | int id; |
| 49 | char name[9]; |
| 50 | struct cdev cdev; |
| 51 | struct device *devicep; |
| 52 | struct mutex dev_lock; |
| 53 | int ref_count; |
| 54 | |
| 55 | struct mutex rx_lock; |
| 56 | uint32_t read_avail; |
| 57 | struct list_head rx_list; |
| 58 | |
| 59 | wait_queue_head_t read_wait_queue; |
| 60 | wait_queue_head_t write_wait_queue; |
| 61 | } *sdio_ctl_devp[NUM_SDIO_CTL_PORTS]; |
| 62 | |
| 63 | struct sdio_ctl_pkt { |
| 64 | int data_size; |
| 65 | void *data; |
| 66 | }; |
| 67 | |
| 68 | struct sdio_ctl_list_elem { |
| 69 | struct list_head list; |
| 70 | struct sdio_ctl_pkt ctl_pkt; |
| 71 | }; |
| 72 | |
| 73 | struct class *sdio_ctl_classp; |
| 74 | static dev_t sdio_ctl_number; |
| 75 | static uint32_t sdio_ctl_inited; |
| 76 | |
| 77 | enum { |
| 78 | MSM_SDIO_CTL_DEBUG = 1U << 0, |
| 79 | MSM_SDIO_CTL_DUMP_BUFFER = 1U << 1, |
| 80 | }; |
| 81 | |
| 82 | #if defined(DEBUG) |
| 83 | #define D_DUMP_BUFFER(prestr, cnt, buf) \ |
| 84 | do { \ |
| 85 | if (msm_sdio_ctl_debug_mask & MSM_SDIO_CTL_DUMP_BUFFER) { \ |
| 86 | int i; \ |
| 87 | pr_info("%s", prestr); \ |
| 88 | for (i = 0; i < cnt; i++) \ |
| 89 | pr_info("%.2x", buf[i]); \ |
| 90 | pr_info("\n"); \ |
| 91 | } \ |
| 92 | } while (0) |
| 93 | |
| 94 | #define D(x...) \ |
| 95 | do { \ |
| 96 | if (msm_sdio_ctl_debug_mask & MSM_SDIO_CTL_DEBUG) \ |
| 97 | pr_info(x); \ |
| 98 | } while (0) |
| 99 | |
| 100 | #else |
| 101 | #define D_DUMP_BUFFER(prestr, cnt, buf) do {} while (0) |
| 102 | #define D(x...) do {} while (0) |
| 103 | #endif |
| 104 | |
| 105 | static void sdio_ctl_receive_cb(void *data, int size, void *priv) |
| 106 | { |
| 107 | struct sdio_ctl_list_elem *list_elem = NULL; |
| 108 | int id = ((struct sdio_ctl_dev *)(priv))->id; |
| 109 | |
| 110 | if (id < 0 || id >= NUM_SDIO_CTL_PORTS) |
| 111 | return; |
| 112 | if (!data || size <= 0) { |
| 113 | wake_up(&sdio_ctl_devp[id]->read_wait_queue); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | list_elem = kmalloc(sizeof(struct sdio_ctl_list_elem), GFP_KERNEL); |
| 118 | if (!list_elem) { |
| 119 | pr_err("%s: list_elem alloc failed\n", __func__); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | list_elem->ctl_pkt.data = kmalloc(size, GFP_KERNEL); |
| 124 | if (!list_elem->ctl_pkt.data) { |
| 125 | pr_err("%s: list_elem->data alloc failed\n", __func__); |
| 126 | kfree(list_elem); |
| 127 | return; |
| 128 | } |
| 129 | memcpy(list_elem->ctl_pkt.data, data, size); |
| 130 | list_elem->ctl_pkt.data_size = size; |
| 131 | mutex_lock(&sdio_ctl_devp[id]->rx_lock); |
| 132 | list_add_tail(&list_elem->list, &sdio_ctl_devp[id]->rx_list); |
| 133 | sdio_ctl_devp[id]->read_avail += size; |
| 134 | mutex_unlock(&sdio_ctl_devp[id]->rx_lock); |
| 135 | wake_up(&sdio_ctl_devp[id]->read_wait_queue); |
| 136 | } |
| 137 | |
| 138 | static void sdio_ctl_write_done(void *data, int size, void *priv) |
| 139 | { |
| 140 | int id = ((struct sdio_ctl_dev *)(priv))->id; |
| 141 | if (id < 0 || id >= NUM_SDIO_CTL_PORTS) |
| 142 | return; |
| 143 | |
| 144 | wake_up(&sdio_ctl_devp[id]->write_wait_queue); |
| 145 | } |
| 146 | |
| 147 | static long sdio_ctl_ioctl(struct file *file, unsigned int cmd, |
| 148 | unsigned long arg) |
| 149 | { |
| 150 | int ret; |
| 151 | struct sdio_ctl_dev *sdio_ctl_devp; |
| 152 | |
| 153 | sdio_ctl_devp = file->private_data; |
| 154 | if (!sdio_ctl_devp) |
| 155 | return -ENODEV; |
| 156 | |
| 157 | switch (cmd) { |
| 158 | case TIOCMGET: |
| 159 | ret = sdio_cmux_tiocmget(sdio_ctl_devp->id); |
| 160 | break; |
| 161 | case TIOCMSET: |
| 162 | ret = sdio_cmux_tiocmset(sdio_ctl_devp->id, arg, ~arg); |
| 163 | break; |
| 164 | default: |
| 165 | ret = -EINVAL; |
| 166 | } |
| 167 | |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | ssize_t sdio_ctl_read(struct file *file, |
| 172 | char __user *buf, |
| 173 | size_t count, |
| 174 | loff_t *ppos) |
| 175 | { |
| 176 | int r = 0, id, bytes_to_read; |
| 177 | struct sdio_ctl_dev *sdio_ctl_devp; |
| 178 | struct sdio_ctl_list_elem *list_elem = NULL; |
| 179 | |
| 180 | sdio_ctl_devp = file->private_data; |
| 181 | |
| 182 | if (!sdio_ctl_devp) |
| 183 | return -ENODEV; |
| 184 | |
| 185 | D("%s: read from ch%d\n", __func__, sdio_ctl_devp->id); |
| 186 | |
| 187 | id = sdio_ctl_devp->id; |
| 188 | mutex_lock(&sdio_ctl_devp->rx_lock); |
| 189 | while (sdio_ctl_devp->read_avail <= 0) { |
| 190 | mutex_unlock(&sdio_ctl_devp->rx_lock); |
| 191 | r = wait_event_interruptible(sdio_ctl_devp->read_wait_queue, |
| 192 | sdio_ctl_devp->read_avail > 0 || |
| 193 | !is_remote_open(id)); |
| 194 | if (sdio_cmux_is_channel_reset(id)) |
| 195 | return -ENETRESET; |
| 196 | |
| 197 | if (!is_remote_open(id)) |
| 198 | return -ENODEV; |
| 199 | |
| 200 | if (r < 0) { |
| 201 | /* qualify error message */ |
| 202 | /* we get this anytime a signal comes in */ |
| 203 | if (r != -ERESTARTSYS) |
| 204 | pr_err("ERROR:%s: wait_event_interruptible " |
| 205 | "ret %i\n", __func__, r); |
| 206 | return r; |
| 207 | } |
| 208 | mutex_lock(&sdio_ctl_devp->rx_lock); |
| 209 | } |
| 210 | |
| 211 | if (list_empty(&sdio_ctl_devp->rx_list)) { |
| 212 | mutex_unlock(&sdio_ctl_devp->rx_lock); |
| 213 | D("%s: Nothing in ch%d's rx_list\n", __func__, |
| 214 | sdio_ctl_devp->id); |
| 215 | return -EAGAIN; |
| 216 | } |
| 217 | |
| 218 | list_elem = list_first_entry(&sdio_ctl_devp->rx_list, |
| 219 | struct sdio_ctl_list_elem, list); |
| 220 | bytes_to_read = (uint32_t)(list_elem->ctl_pkt.data_size); |
| 221 | if (bytes_to_read > count) { |
| 222 | mutex_unlock(&sdio_ctl_devp->rx_lock); |
| 223 | pr_err("%s: Packet size %d > buf size %d\n", __func__, |
| 224 | bytes_to_read, count); |
| 225 | return -ENOMEM; |
| 226 | } |
| 227 | |
| 228 | if (copy_to_user(buf, list_elem->ctl_pkt.data, bytes_to_read)) { |
| 229 | mutex_unlock(&sdio_ctl_devp->rx_lock); |
| 230 | pr_err("%s: copy_to_user failed for ch%d\n", __func__, |
| 231 | sdio_ctl_devp->id); |
| 232 | return -EFAULT; |
| 233 | } |
| 234 | sdio_ctl_devp->read_avail -= bytes_to_read; |
| 235 | list_del(&list_elem->list); |
| 236 | kfree(list_elem->ctl_pkt.data); |
| 237 | kfree(list_elem); |
| 238 | mutex_unlock(&sdio_ctl_devp->rx_lock); |
| 239 | D("%s: Returning %d bytes to ch%d\n", __func__, |
| 240 | bytes_to_read, sdio_ctl_devp->id); |
| 241 | return bytes_to_read; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | ssize_t sdio_ctl_write(struct file *file, |
| 246 | const char __user *buf, |
| 247 | size_t count, |
| 248 | loff_t *ppos) |
| 249 | { |
| 250 | int r = 0, id; |
| 251 | char *temp_buf; |
| 252 | struct sdio_ctl_dev *sdio_ctl_devp; |
| 253 | |
| 254 | if (count <= 0) |
| 255 | return -EINVAL; |
| 256 | |
| 257 | sdio_ctl_devp = file->private_data; |
| 258 | if (!sdio_ctl_devp) |
| 259 | return -ENODEV; |
| 260 | |
| 261 | D("%s: writing %i bytes on ch%d\n", |
| 262 | __func__, count, sdio_ctl_devp->id); |
| 263 | id = sdio_ctl_devp->id; |
| 264 | mutex_lock(&sdio_ctl_devp->dev_lock); |
| 265 | while (sdio_cmux_write_avail(id) < count) { |
| 266 | mutex_unlock(&sdio_ctl_devp->dev_lock); |
| 267 | r = wait_event_interruptible(sdio_ctl_devp->write_wait_queue, |
| 268 | sdio_cmux_write_avail(id) >= count |
| 269 | || !is_remote_open(id)); |
| 270 | |
| 271 | if (sdio_cmux_is_channel_reset(id)) |
| 272 | return -ENETRESET; |
| 273 | |
| 274 | if (!is_remote_open(id)) |
| 275 | return -ENODEV; |
| 276 | |
| 277 | if (r < 0) { |
| 278 | /* qualify error message */ |
| 279 | /* we get this anytime a signal comes in */ |
| 280 | if (r != -ERESTARTSYS) |
| 281 | pr_err("ERROR:%s: wait_event_interruptible " |
| 282 | "ret %i\n", __func__, r); |
| 283 | return r; |
| 284 | } |
| 285 | mutex_lock(&sdio_ctl_devp->dev_lock); |
| 286 | } |
| 287 | |
| 288 | temp_buf = kmalloc(count, GFP_KERNEL); |
| 289 | if (!temp_buf) { |
| 290 | mutex_unlock(&sdio_ctl_devp->dev_lock); |
| 291 | pr_err("%s: temp_buf alloc failed\n", __func__); |
| 292 | return -ENOMEM; |
| 293 | } |
| 294 | |
| 295 | if (copy_from_user(temp_buf, buf, count)) { |
| 296 | mutex_unlock(&sdio_ctl_devp->dev_lock); |
| 297 | pr_err("%s: copy_from_user failed\n", __func__); |
| 298 | kfree(temp_buf); |
| 299 | return -EFAULT; |
| 300 | } |
| 301 | |
| 302 | r = sdio_cmux_write(id, (void *)temp_buf, count); |
| 303 | kfree(temp_buf); |
| 304 | mutex_unlock(&sdio_ctl_devp->dev_lock); |
| 305 | return r; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | int sdio_ctl_open(struct inode *inode, struct file *file) |
| 310 | { |
| 311 | int r = 0; |
| 312 | struct sdio_ctl_dev *sdio_ctl_devp; |
| 313 | |
| 314 | if (!sdio_ctl_inited) |
| 315 | return -EIO; |
| 316 | |
| 317 | sdio_ctl_devp = container_of(inode->i_cdev, struct sdio_ctl_dev, cdev); |
| 318 | |
| 319 | if (!sdio_ctl_devp) |
| 320 | return -ENODEV; |
| 321 | |
| 322 | D("%s called on sdioctl%d device\n", __func__, sdio_ctl_devp->id); |
| 323 | r = sdio_cmux_open(sdio_ctl_devp->id, sdio_ctl_receive_cb, |
| 324 | sdio_ctl_write_done, NULL, |
| 325 | sdio_ctl_devp); |
| 326 | if (r < 0) { |
| 327 | pr_err("ERROR %s: sdio_cmux_open failed with rc %d\n", |
| 328 | __func__, r); |
| 329 | return r; |
| 330 | } |
| 331 | |
| 332 | mutex_lock(&sdio_ctl_devp->dev_lock); |
| 333 | sdio_ctl_devp->ref_count++; |
| 334 | mutex_unlock(&sdio_ctl_devp->dev_lock); |
| 335 | |
| 336 | file->private_data = sdio_ctl_devp; |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | int sdio_ctl_release(struct inode *inode, struct file *file) |
| 341 | { |
| 342 | struct sdio_ctl_dev *sdio_ctl_devp; |
| 343 | struct sdio_ctl_list_elem *list_elem = NULL; |
| 344 | |
| 345 | sdio_ctl_devp = file->private_data; |
| 346 | if (!sdio_ctl_devp) |
| 347 | return -EINVAL; |
| 348 | |
| 349 | D("%s called on sdioctl%d device\n", __func__, sdio_ctl_devp->id); |
| 350 | |
| 351 | mutex_lock(&sdio_ctl_devp->dev_lock); |
| 352 | if (sdio_ctl_devp->ref_count > 0) { |
| 353 | sdio_ctl_devp->ref_count--; |
| 354 | if (!sdio_ctl_devp->ref_count) { |
| 355 | mutex_lock(&sdio_ctl_devp->rx_lock); |
| 356 | while (!list_empty(&sdio_ctl_devp->rx_list)) { |
| 357 | list_elem = list_first_entry( |
| 358 | &sdio_ctl_devp->rx_list, |
| 359 | struct sdio_ctl_list_elem, |
| 360 | list); |
| 361 | list_del(&list_elem->list); |
| 362 | kfree(list_elem->ctl_pkt.data); |
| 363 | kfree(list_elem); |
| 364 | } |
| 365 | sdio_ctl_devp->read_avail = 0; |
| 366 | mutex_unlock(&sdio_ctl_devp->rx_lock); |
| 367 | sdio_cmux_close(sdio_ctl_devp->id); |
| 368 | } |
| 369 | } |
| 370 | mutex_unlock(&sdio_ctl_devp->dev_lock); |
| 371 | |
| 372 | file->private_data = NULL; |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static const struct file_operations sdio_ctl_fops = { |
| 377 | .owner = THIS_MODULE, |
| 378 | .open = sdio_ctl_open, |
| 379 | .release = sdio_ctl_release, |
| 380 | .read = sdio_ctl_read, |
| 381 | .write = sdio_ctl_write, |
| 382 | .unlocked_ioctl = sdio_ctl_ioctl, |
| 383 | }; |
| 384 | |
| 385 | static int sdio_ctl_probe(struct platform_device *pdev) |
| 386 | { |
| 387 | int i; |
| 388 | int r; |
| 389 | |
| 390 | pr_info("%s Begins\n", __func__); |
| 391 | for (i = 0; i < NUM_SDIO_CTL_PORTS; ++i) { |
| 392 | sdio_ctl_devp[i] = kzalloc(sizeof(struct sdio_ctl_dev), |
| 393 | GFP_KERNEL); |
| 394 | if (IS_ERR(sdio_ctl_devp[i])) { |
| 395 | pr_err("ERROR:%s kmalloc() ENOMEM\n", __func__); |
| 396 | r = -ENOMEM; |
| 397 | goto error0; |
| 398 | } |
| 399 | |
| 400 | sdio_ctl_devp[i]->id = i; |
| 401 | sdio_ctl_devp[i]->ref_count = 0; |
| 402 | |
| 403 | mutex_init(&sdio_ctl_devp[i]->dev_lock); |
| 404 | init_waitqueue_head(&sdio_ctl_devp[i]->read_wait_queue); |
| 405 | init_waitqueue_head(&sdio_ctl_devp[i]->write_wait_queue); |
| 406 | mutex_init(&sdio_ctl_devp[i]->rx_lock); |
| 407 | INIT_LIST_HEAD(&sdio_ctl_devp[i]->rx_list); |
| 408 | sdio_ctl_devp[i]->read_avail = 0; |
| 409 | } |
| 410 | |
| 411 | r = alloc_chrdev_region(&sdio_ctl_number, 0, NUM_SDIO_CTL_PORTS, |
| 412 | DEVICE_NAME); |
| 413 | if (IS_ERR_VALUE(r)) { |
| 414 | pr_err("ERROR:%s: alloc_chrdev_region() ret %i.\n", |
| 415 | __func__, r); |
| 416 | goto error0; |
| 417 | } |
| 418 | |
| 419 | sdio_ctl_classp = class_create(THIS_MODULE, DEVICE_NAME); |
| 420 | if (IS_ERR(sdio_ctl_classp)) { |
| 421 | pr_err("ERROR:%s: class_create() ENOMEM\n", __func__); |
| 422 | r = -ENOMEM; |
| 423 | goto error1; |
| 424 | } |
| 425 | |
| 426 | for (i = 0; i < NUM_SDIO_CTL_PORTS; ++i) { |
| 427 | cdev_init(&sdio_ctl_devp[i]->cdev, &sdio_ctl_fops); |
| 428 | sdio_ctl_devp[i]->cdev.owner = THIS_MODULE; |
| 429 | |
| 430 | r = cdev_add(&sdio_ctl_devp[i]->cdev, (sdio_ctl_number + i), |
| 431 | 1); |
| 432 | |
| 433 | if (IS_ERR_VALUE(r)) { |
| 434 | pr_err("%s: cdev_add() ret %i\n", __func__, r); |
| 435 | kfree(sdio_ctl_devp[i]); |
| 436 | goto error2; |
| 437 | } |
| 438 | |
| 439 | sdio_ctl_devp[i]->devicep = |
| 440 | device_create(sdio_ctl_classp, NULL, |
| 441 | (sdio_ctl_number + i), NULL, |
| 442 | DEVICE_NAME "%d", i); |
| 443 | |
| 444 | if (IS_ERR(sdio_ctl_devp[i]->devicep)) { |
| 445 | pr_err("%s: device_create() ENOMEM\n", __func__); |
| 446 | r = -ENOMEM; |
| 447 | cdev_del(&sdio_ctl_devp[i]->cdev); |
| 448 | kfree(sdio_ctl_devp[i]); |
| 449 | goto error2; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | sdio_ctl_inited = 1; |
| 454 | D("SDIO Control Port Driver Initialized.\n"); |
| 455 | return 0; |
| 456 | |
| 457 | error2: |
| 458 | while (--i >= 0) { |
| 459 | cdev_del(&sdio_ctl_devp[i]->cdev); |
| 460 | device_destroy(sdio_ctl_classp, |
| 461 | MKDEV(MAJOR(sdio_ctl_number), i)); |
| 462 | } |
| 463 | |
| 464 | class_destroy(sdio_ctl_classp); |
| 465 | i = NUM_SDIO_CTL_PORTS; |
| 466 | error1: |
| 467 | unregister_chrdev_region(MAJOR(sdio_ctl_number), NUM_SDIO_CTL_PORTS); |
| 468 | error0: |
| 469 | while (--i >= 0) |
| 470 | kfree(sdio_ctl_devp[i]); |
| 471 | return r; |
| 472 | } |
| 473 | |
| 474 | static int sdio_ctl_remove(struct platform_device *pdev) |
| 475 | { |
| 476 | int i; |
| 477 | |
| 478 | for (i = 0; i < NUM_SDIO_CTL_PORTS; ++i) { |
| 479 | cdev_del(&sdio_ctl_devp[i]->cdev); |
| 480 | kfree(sdio_ctl_devp[i]); |
| 481 | device_destroy(sdio_ctl_classp, |
| 482 | MKDEV(MAJOR(sdio_ctl_number), i)); |
| 483 | } |
| 484 | class_destroy(sdio_ctl_classp); |
| 485 | unregister_chrdev_region(MAJOR(sdio_ctl_number), NUM_SDIO_CTL_PORTS); |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static struct platform_driver sdio_ctl_driver = { |
| 491 | .probe = sdio_ctl_probe, |
| 492 | .remove = sdio_ctl_remove, |
| 493 | .driver = { |
| 494 | .name = "SDIO_CTL", |
| 495 | .owner = THIS_MODULE, |
| 496 | }, |
| 497 | }; |
| 498 | |
| 499 | static int __init sdio_ctl_init(void) |
| 500 | { |
| 501 | msm_sdio_ctl_debug_mask = 0; |
| 502 | return platform_driver_register(&sdio_ctl_driver); |
| 503 | } |
| 504 | |
| 505 | module_init(sdio_ctl_init); |
| 506 | MODULE_DESCRIPTION("MSM SDIO Control Port"); |
| 507 | MODULE_LICENSE("GPL v2"); |