blob: 586e8905590ba7a4428174bd3c076af80a0f8123 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* 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
Karthikeyan Ramasubramanian5ff77842011-09-23 15:59:37 -060038#define NUM_SDIO_CTL_PORTS 10
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039#define DEVICE_NAME "sdioctl"
40#define MAX_BUF_SIZE 2048
41#define DEBUG
42
43static int msm_sdio_ctl_debug_mask;
44module_param_named(debug_mask, msm_sdio_ctl_debug_mask,
45 int, S_IRUGO | S_IWUSR | S_IWGRP);
46
47struct 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
63struct sdio_ctl_pkt {
64 int data_size;
65 void *data;
66};
67
68struct sdio_ctl_list_elem {
69 struct list_head list;
70 struct sdio_ctl_pkt ctl_pkt;
71};
72
73struct class *sdio_ctl_classp;
74static dev_t sdio_ctl_number;
75static uint32_t sdio_ctl_inited;
76
77enum {
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) \
84do { \
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...) \
95do { \
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
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600105static uint32_t cmux_ch_id[] = {
106 SDIO_CMUX_DATA_CTL_0,
107 SDIO_CMUX_DATA_CTL_1,
108 SDIO_CMUX_DATA_CTL_2,
109 SDIO_CMUX_DATA_CTL_3,
110 SDIO_CMUX_DATA_CTL_4,
111 SDIO_CMUX_DATA_CTL_5,
112 SDIO_CMUX_DATA_CTL_6,
113 SDIO_CMUX_DATA_CTL_7,
Karthikeyan Ramasubramanian5ff77842011-09-23 15:59:37 -0600114 SDIO_CMUX_USB_CTL_0,
115 SDIO_CMUX_CSVT_CTL_0
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600116};
117
118static int get_ctl_dev_index(int id)
119{
120 int dev_index;
121 for (dev_index = 0; dev_index < NUM_SDIO_CTL_PORTS; dev_index++) {
122 if (cmux_ch_id[dev_index] == id)
123 return dev_index;
124 }
125 return -ENODEV;
126}
127
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700128static void sdio_ctl_receive_cb(void *data, int size, void *priv)
129{
130 struct sdio_ctl_list_elem *list_elem = NULL;
131 int id = ((struct sdio_ctl_dev *)(priv))->id;
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600132 int dev_index;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600134 if (id < 0 || id > cmux_ch_id[NUM_SDIO_CTL_PORTS - 1])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700135 return;
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600136 dev_index = get_ctl_dev_index(id);
137 if (dev_index < 0) {
138 pr_err("%s: Ch%d is not exported to user-space\n",
139 __func__, id);
140 return;
141 }
142
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700143 if (!data || size <= 0) {
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600144 wake_up(&sdio_ctl_devp[dev_index]->read_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 return;
146 }
147
148 list_elem = kmalloc(sizeof(struct sdio_ctl_list_elem), GFP_KERNEL);
149 if (!list_elem) {
150 pr_err("%s: list_elem alloc failed\n", __func__);
151 return;
152 }
153
154 list_elem->ctl_pkt.data = kmalloc(size, GFP_KERNEL);
155 if (!list_elem->ctl_pkt.data) {
156 pr_err("%s: list_elem->data alloc failed\n", __func__);
157 kfree(list_elem);
158 return;
159 }
160 memcpy(list_elem->ctl_pkt.data, data, size);
161 list_elem->ctl_pkt.data_size = size;
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600162 mutex_lock(&sdio_ctl_devp[dev_index]->rx_lock);
163 list_add_tail(&list_elem->list, &sdio_ctl_devp[dev_index]->rx_list);
164 sdio_ctl_devp[dev_index]->read_avail += size;
165 mutex_unlock(&sdio_ctl_devp[dev_index]->rx_lock);
166 wake_up(&sdio_ctl_devp[dev_index]->read_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167}
168
169static void sdio_ctl_write_done(void *data, int size, void *priv)
170{
171 int id = ((struct sdio_ctl_dev *)(priv))->id;
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600172 int dev_index;
173 if (id < 0 || id > cmux_ch_id[NUM_SDIO_CTL_PORTS - 1])
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174 return;
175
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600176 dev_index = get_ctl_dev_index(id);
177 if (dev_index < 0) {
178 pr_err("%s: Ch%d is not exported to user-space\n",
179 __func__, id);
180 return;
181 }
182 wake_up(&sdio_ctl_devp[dev_index]->write_wait_queue);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183}
184
185static long sdio_ctl_ioctl(struct file *file, unsigned int cmd,
186 unsigned long arg)
187{
188 int ret;
189 struct sdio_ctl_dev *sdio_ctl_devp;
190
191 sdio_ctl_devp = file->private_data;
192 if (!sdio_ctl_devp)
193 return -ENODEV;
194
195 switch (cmd) {
196 case TIOCMGET:
197 ret = sdio_cmux_tiocmget(sdio_ctl_devp->id);
198 break;
199 case TIOCMSET:
200 ret = sdio_cmux_tiocmset(sdio_ctl_devp->id, arg, ~arg);
201 break;
202 default:
203 ret = -EINVAL;
204 }
205
206 return ret;
207}
208
209ssize_t sdio_ctl_read(struct file *file,
210 char __user *buf,
211 size_t count,
212 loff_t *ppos)
213{
214 int r = 0, id, bytes_to_read;
215 struct sdio_ctl_dev *sdio_ctl_devp;
216 struct sdio_ctl_list_elem *list_elem = NULL;
217
218 sdio_ctl_devp = file->private_data;
219
220 if (!sdio_ctl_devp)
221 return -ENODEV;
222
223 D("%s: read from ch%d\n", __func__, sdio_ctl_devp->id);
224
225 id = sdio_ctl_devp->id;
226 mutex_lock(&sdio_ctl_devp->rx_lock);
227 while (sdio_ctl_devp->read_avail <= 0) {
228 mutex_unlock(&sdio_ctl_devp->rx_lock);
229 r = wait_event_interruptible(sdio_ctl_devp->read_wait_queue,
230 sdio_ctl_devp->read_avail > 0 ||
231 !is_remote_open(id));
232 if (sdio_cmux_is_channel_reset(id))
233 return -ENETRESET;
234
235 if (!is_remote_open(id))
236 return -ENODEV;
237
238 if (r < 0) {
239 /* qualify error message */
240 /* we get this anytime a signal comes in */
241 if (r != -ERESTARTSYS)
242 pr_err("ERROR:%s: wait_event_interruptible "
243 "ret %i\n", __func__, r);
244 return r;
245 }
246 mutex_lock(&sdio_ctl_devp->rx_lock);
247 }
248
249 if (list_empty(&sdio_ctl_devp->rx_list)) {
250 mutex_unlock(&sdio_ctl_devp->rx_lock);
251 D("%s: Nothing in ch%d's rx_list\n", __func__,
252 sdio_ctl_devp->id);
253 return -EAGAIN;
254 }
255
256 list_elem = list_first_entry(&sdio_ctl_devp->rx_list,
257 struct sdio_ctl_list_elem, list);
258 bytes_to_read = (uint32_t)(list_elem->ctl_pkt.data_size);
259 if (bytes_to_read > count) {
260 mutex_unlock(&sdio_ctl_devp->rx_lock);
261 pr_err("%s: Packet size %d > buf size %d\n", __func__,
262 bytes_to_read, count);
263 return -ENOMEM;
264 }
265
266 if (copy_to_user(buf, list_elem->ctl_pkt.data, bytes_to_read)) {
267 mutex_unlock(&sdio_ctl_devp->rx_lock);
268 pr_err("%s: copy_to_user failed for ch%d\n", __func__,
269 sdio_ctl_devp->id);
270 return -EFAULT;
271 }
272 sdio_ctl_devp->read_avail -= bytes_to_read;
273 list_del(&list_elem->list);
274 kfree(list_elem->ctl_pkt.data);
275 kfree(list_elem);
276 mutex_unlock(&sdio_ctl_devp->rx_lock);
277 D("%s: Returning %d bytes to ch%d\n", __func__,
278 bytes_to_read, sdio_ctl_devp->id);
279 return bytes_to_read;
280}
281
282
283ssize_t sdio_ctl_write(struct file *file,
284 const char __user *buf,
285 size_t count,
286 loff_t *ppos)
287{
288 int r = 0, id;
289 char *temp_buf;
290 struct sdio_ctl_dev *sdio_ctl_devp;
291
292 if (count <= 0)
293 return -EINVAL;
294
295 sdio_ctl_devp = file->private_data;
296 if (!sdio_ctl_devp)
297 return -ENODEV;
298
299 D("%s: writing %i bytes on ch%d\n",
300 __func__, count, sdio_ctl_devp->id);
301 id = sdio_ctl_devp->id;
302 mutex_lock(&sdio_ctl_devp->dev_lock);
303 while (sdio_cmux_write_avail(id) < count) {
304 mutex_unlock(&sdio_ctl_devp->dev_lock);
305 r = wait_event_interruptible(sdio_ctl_devp->write_wait_queue,
306 sdio_cmux_write_avail(id) >= count
307 || !is_remote_open(id));
308
309 if (sdio_cmux_is_channel_reset(id))
310 return -ENETRESET;
311
312 if (!is_remote_open(id))
313 return -ENODEV;
314
315 if (r < 0) {
316 /* qualify error message */
317 /* we get this anytime a signal comes in */
318 if (r != -ERESTARTSYS)
319 pr_err("ERROR:%s: wait_event_interruptible "
320 "ret %i\n", __func__, r);
321 return r;
322 }
323 mutex_lock(&sdio_ctl_devp->dev_lock);
324 }
325
326 temp_buf = kmalloc(count, GFP_KERNEL);
327 if (!temp_buf) {
328 mutex_unlock(&sdio_ctl_devp->dev_lock);
329 pr_err("%s: temp_buf alloc failed\n", __func__);
330 return -ENOMEM;
331 }
332
333 if (copy_from_user(temp_buf, buf, count)) {
334 mutex_unlock(&sdio_ctl_devp->dev_lock);
335 pr_err("%s: copy_from_user failed\n", __func__);
336 kfree(temp_buf);
337 return -EFAULT;
338 }
339
340 r = sdio_cmux_write(id, (void *)temp_buf, count);
341 kfree(temp_buf);
342 mutex_unlock(&sdio_ctl_devp->dev_lock);
343 return r;
344}
345
346
347int sdio_ctl_open(struct inode *inode, struct file *file)
348{
349 int r = 0;
350 struct sdio_ctl_dev *sdio_ctl_devp;
351
352 if (!sdio_ctl_inited)
353 return -EIO;
354
355 sdio_ctl_devp = container_of(inode->i_cdev, struct sdio_ctl_dev, cdev);
356
357 if (!sdio_ctl_devp)
358 return -ENODEV;
359
360 D("%s called on sdioctl%d device\n", __func__, sdio_ctl_devp->id);
361 r = sdio_cmux_open(sdio_ctl_devp->id, sdio_ctl_receive_cb,
362 sdio_ctl_write_done, NULL,
363 sdio_ctl_devp);
364 if (r < 0) {
365 pr_err("ERROR %s: sdio_cmux_open failed with rc %d\n",
366 __func__, r);
367 return r;
368 }
369
370 mutex_lock(&sdio_ctl_devp->dev_lock);
371 sdio_ctl_devp->ref_count++;
372 mutex_unlock(&sdio_ctl_devp->dev_lock);
373
374 file->private_data = sdio_ctl_devp;
375 return 0;
376}
377
378int sdio_ctl_release(struct inode *inode, struct file *file)
379{
380 struct sdio_ctl_dev *sdio_ctl_devp;
381 struct sdio_ctl_list_elem *list_elem = NULL;
382
383 sdio_ctl_devp = file->private_data;
384 if (!sdio_ctl_devp)
385 return -EINVAL;
386
387 D("%s called on sdioctl%d device\n", __func__, sdio_ctl_devp->id);
388
389 mutex_lock(&sdio_ctl_devp->dev_lock);
390 if (sdio_ctl_devp->ref_count > 0) {
391 sdio_ctl_devp->ref_count--;
392 if (!sdio_ctl_devp->ref_count) {
393 mutex_lock(&sdio_ctl_devp->rx_lock);
394 while (!list_empty(&sdio_ctl_devp->rx_list)) {
395 list_elem = list_first_entry(
396 &sdio_ctl_devp->rx_list,
397 struct sdio_ctl_list_elem,
398 list);
399 list_del(&list_elem->list);
400 kfree(list_elem->ctl_pkt.data);
401 kfree(list_elem);
402 }
403 sdio_ctl_devp->read_avail = 0;
404 mutex_unlock(&sdio_ctl_devp->rx_lock);
405 sdio_cmux_close(sdio_ctl_devp->id);
406 }
407 }
408 mutex_unlock(&sdio_ctl_devp->dev_lock);
409
410 file->private_data = NULL;
411 return 0;
412}
413
414static const struct file_operations sdio_ctl_fops = {
415 .owner = THIS_MODULE,
416 .open = sdio_ctl_open,
417 .release = sdio_ctl_release,
418 .read = sdio_ctl_read,
419 .write = sdio_ctl_write,
420 .unlocked_ioctl = sdio_ctl_ioctl,
421};
422
423static int sdio_ctl_probe(struct platform_device *pdev)
424{
425 int i;
426 int r;
427
428 pr_info("%s Begins\n", __func__);
429 for (i = 0; i < NUM_SDIO_CTL_PORTS; ++i) {
430 sdio_ctl_devp[i] = kzalloc(sizeof(struct sdio_ctl_dev),
431 GFP_KERNEL);
432 if (IS_ERR(sdio_ctl_devp[i])) {
433 pr_err("ERROR:%s kmalloc() ENOMEM\n", __func__);
434 r = -ENOMEM;
435 goto error0;
436 }
437
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600438 sdio_ctl_devp[i]->id = cmux_ch_id[i];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700439 sdio_ctl_devp[i]->ref_count = 0;
440
441 mutex_init(&sdio_ctl_devp[i]->dev_lock);
442 init_waitqueue_head(&sdio_ctl_devp[i]->read_wait_queue);
443 init_waitqueue_head(&sdio_ctl_devp[i]->write_wait_queue);
444 mutex_init(&sdio_ctl_devp[i]->rx_lock);
445 INIT_LIST_HEAD(&sdio_ctl_devp[i]->rx_list);
446 sdio_ctl_devp[i]->read_avail = 0;
447 }
448
449 r = alloc_chrdev_region(&sdio_ctl_number, 0, NUM_SDIO_CTL_PORTS,
450 DEVICE_NAME);
451 if (IS_ERR_VALUE(r)) {
452 pr_err("ERROR:%s: alloc_chrdev_region() ret %i.\n",
453 __func__, r);
454 goto error0;
455 }
456
457 sdio_ctl_classp = class_create(THIS_MODULE, DEVICE_NAME);
458 if (IS_ERR(sdio_ctl_classp)) {
459 pr_err("ERROR:%s: class_create() ENOMEM\n", __func__);
460 r = -ENOMEM;
461 goto error1;
462 }
463
464 for (i = 0; i < NUM_SDIO_CTL_PORTS; ++i) {
465 cdev_init(&sdio_ctl_devp[i]->cdev, &sdio_ctl_fops);
466 sdio_ctl_devp[i]->cdev.owner = THIS_MODULE;
467
468 r = cdev_add(&sdio_ctl_devp[i]->cdev, (sdio_ctl_number + i),
469 1);
470
471 if (IS_ERR_VALUE(r)) {
472 pr_err("%s: cdev_add() ret %i\n", __func__, r);
473 kfree(sdio_ctl_devp[i]);
474 goto error2;
475 }
476
477 sdio_ctl_devp[i]->devicep =
478 device_create(sdio_ctl_classp, NULL,
479 (sdio_ctl_number + i), NULL,
Karthikeyan Ramasubramanian6425f692011-09-23 14:57:43 -0600480 DEVICE_NAME "%d", cmux_ch_id[i]);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481
482 if (IS_ERR(sdio_ctl_devp[i]->devicep)) {
483 pr_err("%s: device_create() ENOMEM\n", __func__);
484 r = -ENOMEM;
485 cdev_del(&sdio_ctl_devp[i]->cdev);
486 kfree(sdio_ctl_devp[i]);
487 goto error2;
488 }
489 }
490
491 sdio_ctl_inited = 1;
492 D("SDIO Control Port Driver Initialized.\n");
493 return 0;
494
495error2:
496 while (--i >= 0) {
497 cdev_del(&sdio_ctl_devp[i]->cdev);
498 device_destroy(sdio_ctl_classp,
499 MKDEV(MAJOR(sdio_ctl_number), i));
500 }
501
502 class_destroy(sdio_ctl_classp);
503 i = NUM_SDIO_CTL_PORTS;
504error1:
505 unregister_chrdev_region(MAJOR(sdio_ctl_number), NUM_SDIO_CTL_PORTS);
506error0:
507 while (--i >= 0)
508 kfree(sdio_ctl_devp[i]);
509 return r;
510}
511
512static int sdio_ctl_remove(struct platform_device *pdev)
513{
514 int i;
515
516 for (i = 0; i < NUM_SDIO_CTL_PORTS; ++i) {
517 cdev_del(&sdio_ctl_devp[i]->cdev);
518 kfree(sdio_ctl_devp[i]);
519 device_destroy(sdio_ctl_classp,
520 MKDEV(MAJOR(sdio_ctl_number), i));
521 }
522 class_destroy(sdio_ctl_classp);
523 unregister_chrdev_region(MAJOR(sdio_ctl_number), NUM_SDIO_CTL_PORTS);
524
525 return 0;
526}
527
528static struct platform_driver sdio_ctl_driver = {
529 .probe = sdio_ctl_probe,
530 .remove = sdio_ctl_remove,
531 .driver = {
532 .name = "SDIO_CTL",
533 .owner = THIS_MODULE,
534 },
535};
536
537static int __init sdio_ctl_init(void)
538{
539 msm_sdio_ctl_debug_mask = 0;
540 return platform_driver_register(&sdio_ctl_driver);
541}
542
543module_init(sdio_ctl_init);
544MODULE_DESCRIPTION("MSM SDIO Control Port");
545MODULE_LICENSE("GPL v2");