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