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