blob: 4416a7909b2ee785768284b7ee9a5554755a82ed [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#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/sched.h>
16#include <linux/wait.h>
17#include <linux/workqueue.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070018#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070019#include <mach/sdio_al.h>
20#include <mach/sdio_smem.h>
21
22static void sdio_smem_read(struct work_struct *work);
23
24static struct sdio_channel *channel;
25static struct workqueue_struct *workq;
26static DECLARE_WORK(work_read, sdio_smem_read);
27static DECLARE_WAIT_QUEUE_HEAD(waitq);
28static int bytes_avail;
Maya Erez2bfba2c2011-09-19 11:41:55 +030029static int sdio_ch_opened;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030
31static void sdio_smem_release(struct device *dev)
32{
33 pr_debug("sdio smem released\n");
34}
35
36static struct sdio_smem_client client;
37
38static void sdio_smem_read(struct work_struct *work)
39{
40 int err;
41 int read_avail;
42 char *data = client.buf;
43
Maya Erez2bfba2c2011-09-19 11:41:55 +030044 if (!sdio_ch_opened)
45 return;
46
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070047 read_avail = sdio_read_avail(channel);
48 if (read_avail > bytes_avail ||
49 read_avail < 0) {
50 pr_err("Error: read_avail=%d bytes_avail=%d\n",
51 read_avail, bytes_avail);
Maya Erez2bfba2c2011-09-19 11:41:55 +030052 goto read_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053 }
54
Maya Erez2bfba2c2011-09-19 11:41:55 +030055 if (read_avail == 0)
56 return;
57
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058 err = sdio_read(channel,
59 &data[client.size - bytes_avail],
60 read_avail);
Maya Erez2bfba2c2011-09-19 11:41:55 +030061 if (err) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062 pr_err("sdio_read error (%d)", err);
Maya Erez2bfba2c2011-09-19 11:41:55 +030063 goto read_err;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070064 }
65
66 bytes_avail -= read_avail;
67 pr_debug("read %d bytes (bytes_avail = %d)\n",
68 read_avail, bytes_avail);
69
70 if (!bytes_avail) {
71 bytes_avail = client.size;
72 err = client.cb_func(SDIO_SMEM_EVENT_READ_DONE);
73 }
74 if (err)
75 pr_err("error (%d) on callback\n", err);
Maya Erez2bfba2c2011-09-19 11:41:55 +030076
77 return;
78
79read_err:
80 if (sdio_ch_opened)
81 client.cb_func(SDIO_SMEM_EVENT_READ_ERR);
82 return;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083}
84
85static void sdio_smem_notify(void *priv, unsigned event)
86{
87 pr_debug("%d event received\n", event);
88
89 if (event == SDIO_EVENT_DATA_READ_AVAIL ||
90 event == SDIO_EVENT_DATA_WRITE_AVAIL)
91 queue_work(workq, &work_read);
92}
93
94int sdio_smem_register_client(void)
95{
96 int err = 0;
97
98 if (!client.buf || !client.size || !client.cb_func)
99 return -EINVAL;
100
101 pr_debug("buf = %p\n", client.buf);
102 pr_debug("size = 0x%x\n", client.size);
103
104 bytes_avail = client.size;
105 workq = create_singlethread_workqueue("sdio_smem");
106 if (!workq)
107 return -ENOMEM;
108
Maya Erez2bfba2c2011-09-19 11:41:55 +0300109 sdio_ch_opened = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700110 err = sdio_open("SDIO_SMEM", &channel, NULL, sdio_smem_notify);
Maya Erez2bfba2c2011-09-19 11:41:55 +0300111 if (err) {
112 sdio_ch_opened = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700113 pr_err("sdio_open error (%d)\n", err);
114 destroy_workqueue(workq);
115 return err;
116 }
117 pr_debug("SDIO SMEM channel opened\n");
118 return err;
119}
120
121int sdio_smem_unregister_client(void)
122{
Maya Erez2bfba2c2011-09-19 11:41:55 +0300123 int err = 0;
124
125 sdio_ch_opened = 0;
126 err = sdio_close(channel);
127 if (err) {
128 pr_err("sdio_close error (%d)\n", err);
129 return err;
130 }
131 pr_debug("SDIO SMEM channel closed\n");
132 flush_workqueue(workq);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700133 destroy_workqueue(workq);
134 bytes_avail = 0;
135 client.buf = NULL;
136 client.cb_func = NULL;
137 client.size = 0;
Maya Erez2bfba2c2011-09-19 11:41:55 +0300138
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139 return 0;
140}
141
142static int sdio_smem_probe(struct platform_device *pdev)
143{
144 client.plat_dev.name = "SDIO_SMEM_CLIENT";
145 client.plat_dev.id = -1;
146 client.plat_dev.dev.release = sdio_smem_release;
147
148 return platform_device_register(&client.plat_dev);
149}
150
151static int sdio_smem_remove(struct platform_device *pdev)
152{
153 platform_device_unregister(&client.plat_dev);
154 memset(&client, 0, sizeof(client));
Maya Erez2bfba2c2011-09-19 11:41:55 +0300155 sdio_ch_opened = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700156 return 0;
157}
158static struct platform_driver sdio_smem_drv = {
159 .probe = sdio_smem_probe,
160 .remove = sdio_smem_remove,
161 .driver = {
162 .name = "SDIO_SMEM",
163 .owner = THIS_MODULE,
164 },
165};
166
167static int __init sdio_smem_init(void)
168{
169 return platform_driver_register(&sdio_smem_drv);
170};
171
172module_init(sdio_smem_init);
173
174MODULE_DESCRIPTION("SDIO SMEM");
175MODULE_LICENSE("GPL v2");