blob: 118bbb3bf8342751484c7f9b76ce53e8ff2b3a14 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/*
2 * HCI_SMD (HCI Shared Memory Driver) is Qualcomm's Shared memory driver
3 * for the BT HCI protocol.
4 *
5 * Copyright (c) 2000-2001, 2011 Code Aurora Forum. All rights reserved.
6 * Copyright (C) 2002-2003 Maxim Krasnyansky <maxk@qualcomm.com>
7 * Copyright (C) 2004-2006 Marcel Holtmann <marcel@holtmann.org>
8 *
9 * This file is based on drivers/bluetooth/hci_vhci.c
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2
13 * as published by the Free Software Foundation
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 */
20
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/init.h>
24#include <linux/errno.h>
25#include <linux/string.h>
26#include <linux/skbuff.h>
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -070027#include <linux/wakelock.h>
28#include <linux/uaccess.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
31#include <net/bluetooth/hci.h>
32#include <mach/msm_smd.h>
33
Bhasker Netid08d9012011-09-05 19:37:54 +053034#define EVENT_CHANNEL "APPS_RIVA_BT_CMD"
35#define DATA_CHANNEL "APPS_RIVA_BT_ACL"
36#define RX_Q_MONITOR (1) /* 1 milli second */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070037
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -070038
39static unsigned int driver_state;
40
41static int hcismd_set;
42static DEFINE_MUTEX(hci_smd_enable);
43
44static int hcismd_set_enable(const char *val, struct kernel_param *kp);
45module_param_call(hcismd_set, hcismd_set_enable, NULL, &hcismd_set, 0644);
46
47
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048struct hci_smd_data {
49 struct hci_dev *hdev;
50
51 struct smd_channel *event_channel;
52 struct smd_channel *data_channel;
Bhasker Netid08d9012011-09-05 19:37:54 +053053 struct wake_lock wake_lock_tx;
54 struct wake_lock wake_lock_rx;
55 struct timer_list rx_q_timer;
Ankur Nandwani034ed892011-10-07 11:15:53 -070056 struct tasklet_struct hci_event_task;
57 struct tasklet_struct hci_data_task;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058};
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -070059static struct hci_smd_data hs;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070060
Bhasker Netid08d9012011-09-05 19:37:54 +053061/* Rx queue monitor timer function */
62static int is_rx_q_empty(unsigned long arg)
63{
64 struct hci_dev *hdev = (struct hci_dev *) arg;
65 struct sk_buff_head *list_ = &hdev->rx_q;
66 struct sk_buff *list = ((struct sk_buff *)list_)->next;
67 BT_DBG("%s Rx timer triggered", hdev->name);
68
69 if (list == (struct sk_buff *)list_) {
70 BT_DBG("%s RX queue empty", hdev->name);
71 return 1;
72 } else{
73 BT_DBG("%s RX queue not empty", hdev->name);
74 return 0;
75 }
76}
77
78static void release_lock(void)
79{
80 struct hci_smd_data *hsmd = &hs;
81 BT_DBG("Releasing Rx Lock");
82 if (is_rx_q_empty((unsigned long)hsmd->hdev) &&
83 wake_lock_active(&hs.wake_lock_rx))
84 wake_unlock(&hs.wake_lock_rx);
85}
86
87/* Rx timer callback function */
88static void schedule_timer(unsigned long arg)
89{
90 struct hci_dev *hdev = (struct hci_dev *) arg;
91 struct hci_smd_data *hsmd = &hs;
92 BT_DBG("%s Schedule Rx timer", hdev->name);
93
94 if (is_rx_q_empty(arg) && wake_lock_active(&hs.wake_lock_rx)) {
95 BT_DBG("%s RX queue empty", hdev->name);
96 /*
97 * Since the queue is empty, its ideal
98 * to release the wake lock on Rx
99 */
100 wake_unlock(&hs.wake_lock_rx);
101 } else{
102 BT_DBG("%s RX queue not empty", hdev->name);
103 /*
104 * Restart the timer to monitor whether the Rx queue is
105 * empty for releasing the Rx wake lock
106 */
107 mod_timer(&hsmd->rx_q_timer,
108 jiffies + msecs_to_jiffies(RX_Q_MONITOR));
109 }
110}
111
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700112static int hci_smd_open(struct hci_dev *hdev)
113{
114 set_bit(HCI_RUNNING, &hdev->flags);
115 return 0;
116}
117
118
119static int hci_smd_close(struct hci_dev *hdev)
120{
121 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
122 return 0;
123 else
124 return -EPERM;
125}
126
127
128static void hci_smd_destruct(struct hci_dev *hdev)
129{
130 kfree(hdev->driver_data);
131}
132
133static void hci_smd_recv_data(unsigned long arg)
134{
Bhasker Netid08d9012011-09-05 19:37:54 +0530135 int len = 0;
136 int rc = 0;
137 struct sk_buff *skb = NULL;
138 unsigned char *buf = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700139 struct hci_smd_data *hsmd = &hs;
Bhasker Netid08d9012011-09-05 19:37:54 +0530140 wake_lock(&hs.wake_lock_rx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141
142 len = smd_read_avail(hsmd->data_channel);
Bhasker Netid08d9012011-09-05 19:37:54 +0530143 if (len <= 0) {
144 BT_ERR("Nothing to read from SMD channel\n");
145 goto out_data;
146 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700147 while (len > 0) {
Ankur Nandwanic8150962011-08-24 13:06:23 -0700148 skb = bt_skb_alloc(len, GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 if (!skb) {
150 BT_ERR("Error in allocating socket buffer\n");
Bhasker Netid08d9012011-09-05 19:37:54 +0530151 goto out_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152 }
153
Ankur Nandwanic8150962011-08-24 13:06:23 -0700154 buf = kmalloc(len, GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700155 if (!buf) {
156 BT_ERR("Error in allocating buffer\n");
Bhasker Netid08d9012011-09-05 19:37:54 +0530157 rc = -ENOMEM;
158 goto out_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700159 }
160
Ankur Nandwani531333f2011-10-25 15:47:43 -0700161 rc = smd_read(hsmd->data_channel, (void *)buf, len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162 if (rc < len) {
163 BT_ERR("Error in reading from the channel");
Bhasker Netid08d9012011-09-05 19:37:54 +0530164 goto out_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700165 }
166
167 memcpy(skb_put(skb, len), buf, len);
168 skb->dev = (void *)hsmd->hdev;
169 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
170
171 skb_orphan(skb);
172
173 rc = hci_recv_frame(skb);
174 if (rc < 0) {
175 BT_ERR("Error in passing the packet to HCI Layer");
Bhasker Neti44a792b2011-10-11 19:25:22 +0530176 /*
177 * skb is getting freed in hci_recv_frame, making it
178 * to null to avoid multiple access
179 */
180 skb = NULL;
Bhasker Netid08d9012011-09-05 19:37:54 +0530181 goto out_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700182 }
183
184 kfree(buf);
Bhasker Netid08d9012011-09-05 19:37:54 +0530185 buf = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186 len = smd_read_avail(hsmd->data_channel);
Bhasker Netid08d9012011-09-05 19:37:54 +0530187 /*
188 * Start the timer to monitor whether the Rx queue is
189 * empty for releasing the Rx wake lock
190 */
191 BT_DBG("Rx Timer is starting\n");
192 mod_timer(&hsmd->rx_q_timer,
193 jiffies + msecs_to_jiffies(RX_Q_MONITOR));
194 }
195out_data:
196 release_lock();
197 if (rc) {
198 if (skb)
199 kfree_skb(skb);
200 kfree(buf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201 }
202}
203
204static void hci_smd_recv_event(unsigned long arg)
205{
Bhasker Netid08d9012011-09-05 19:37:54 +0530206 int len = 0;
207 int rc = 0;
208 struct sk_buff *skb = NULL;
209 unsigned char *buf = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 struct hci_smd_data *hsmd = &hs;
Bhasker Netid08d9012011-09-05 19:37:54 +0530211 wake_lock(&hs.wake_lock_rx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212
213 len = smd_read_avail(hsmd->event_channel);
214 if (len > HCI_MAX_FRAME_SIZE) {
215 BT_ERR("Frame larger than the allowed size");
Bhasker Netid08d9012011-09-05 19:37:54 +0530216 goto out_event;
217 } else if (len <= 0) {
218 BT_ERR("Nothing to read from SMD channel\n");
219 goto out_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700220 }
221
222 while (len > 0) {
Ankur Nandwanic8150962011-08-24 13:06:23 -0700223 skb = bt_skb_alloc(len, GFP_ATOMIC);
Bhasker Netid08d9012011-09-05 19:37:54 +0530224 if (!skb) {
225 BT_ERR("Error in allocating socket buffer\n");
226 goto out_event;
227 }
Ankur Nandwanic8150962011-08-24 13:06:23 -0700228 buf = kmalloc(len, GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700229 if (!buf) {
Bhasker Netid08d9012011-09-05 19:37:54 +0530230 BT_ERR("Error in allocating buffer\n");
231 rc = -ENOMEM;
232 goto out_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700233 }
Ankur Nandwani531333f2011-10-25 15:47:43 -0700234 rc = smd_read(hsmd->event_channel, (void *)buf, len);
Bhasker Netid08d9012011-09-05 19:37:54 +0530235 if (rc < len) {
236 BT_ERR("Error in reading from the event channel");
237 goto out_event;
238 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700239
240 memcpy(skb_put(skb, len), buf, len);
241 skb->dev = (void *)hsmd->hdev;
242 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
243
244 skb_orphan(skb);
245
246 rc = hci_recv_frame(skb);
247 if (rc < 0) {
248 BT_ERR("Error in passing the packet to HCI Layer");
Bhasker Neti44a792b2011-10-11 19:25:22 +0530249 /*
250 * skb is getting freed in hci_recv_frame, making it
251 * to null to avoid multiple access
252 */
253 skb = NULL;
Bhasker Netid08d9012011-09-05 19:37:54 +0530254 goto out_event;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 }
256
257 kfree(buf);
Bhasker Netid08d9012011-09-05 19:37:54 +0530258 buf = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259 len = smd_read_avail(hsmd->event_channel);
Bhasker Netid08d9012011-09-05 19:37:54 +0530260 /*
261 * Start the timer to monitor whether the Rx queue is
262 * empty for releasing the Rx wake lock
263 */
264 BT_DBG("Rx Timer is starting\n");
265 mod_timer(&hsmd->rx_q_timer,
266 jiffies + msecs_to_jiffies(RX_Q_MONITOR));
267 }
268out_event:
269 release_lock();
270 if (rc) {
271 if (skb)
272 kfree_skb(skb);
273 kfree(buf);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274 }
275}
276
277static int hci_smd_send_frame(struct sk_buff *skb)
278{
279 int len;
Bhasker Netid08d9012011-09-05 19:37:54 +0530280 int avail;
281 int ret = 0;
282 wake_lock(&hs.wake_lock_tx);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283
284 switch (bt_cb(skb)->pkt_type) {
285 case HCI_COMMAND_PKT:
Bhasker Netid08d9012011-09-05 19:37:54 +0530286 avail = smd_write_avail(hs.event_channel);
287 if (!avail) {
288 BT_ERR("No space available for smd frame");
289 ret = -ENOSPC;
290 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291 len = smd_write(hs.event_channel, skb->data, skb->len);
292 if (len < skb->len) {
293 BT_ERR("Failed to write Command %d", len);
Bhasker Netid08d9012011-09-05 19:37:54 +0530294 ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700295 }
296 break;
297 case HCI_ACLDATA_PKT:
298 case HCI_SCODATA_PKT:
Ankur Nandwani23abeb22011-10-26 16:35:22 -0700299 avail = smd_write_avail(hs.data_channel);
Bhasker Netid08d9012011-09-05 19:37:54 +0530300 if (!avail) {
301 BT_ERR("No space available for smd frame");
302 ret = -ENOSPC;
303 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700304 len = smd_write(hs.data_channel, skb->data, skb->len);
305 if (len < skb->len) {
306 BT_ERR("Failed to write Data %d", len);
Bhasker Netid08d9012011-09-05 19:37:54 +0530307 ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700308 }
309 break;
310 default:
311 BT_ERR("Uknown packet type\n");
Bhasker Netid08d9012011-09-05 19:37:54 +0530312 ret = -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700313 break;
314 }
Bhasker Netid08d9012011-09-05 19:37:54 +0530315
Anubhav Guptaddf48ec2011-10-03 14:24:13 +0530316 kfree_skb(skb);
Bhasker Netid08d9012011-09-05 19:37:54 +0530317 wake_unlock(&hs.wake_lock_tx);
318 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700319}
320
321
322static void hci_smd_notify_event(void *data, unsigned int event)
323{
324 struct hci_dev *hdev = hs.hdev;
325
326 if (!hdev) {
327 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
328 return;
329 }
330
331 switch (event) {
332 case SMD_EVENT_DATA:
Ankur Nandwani034ed892011-10-07 11:15:53 -0700333 tasklet_hi_schedule(&hs.hci_event_task);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700334 break;
335 case SMD_EVENT_OPEN:
336 hci_smd_open(hdev);
337 break;
338 case SMD_EVENT_CLOSE:
339 hci_smd_close(hdev);
340 break;
341 default:
342 break;
343 }
344}
345
346static void hci_smd_notify_data(void *data, unsigned int event)
347{
348 struct hci_dev *hdev = hs.hdev;
349 if (!hdev) {
350 BT_ERR("HCI device (hdev=NULL)");
351 return;
352 }
353
354 switch (event) {
355 case SMD_EVENT_DATA:
Ankur Nandwani034ed892011-10-07 11:15:53 -0700356 tasklet_hi_schedule(&hs.hci_data_task);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700357 break;
358 case SMD_EVENT_OPEN:
359 hci_smd_open(hdev);
360 break;
361 case SMD_EVENT_CLOSE:
362 hci_smd_close(hdev);
363 break;
364 default:
365 break;
366 }
367
368}
369
370static int hci_smd_register_dev(struct hci_smd_data *hsmd)
371{
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -0700372 static struct hci_dev *hdev;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700373 int rc;
374
375 /* Initialize and register HCI device */
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -0700376 if (!driver_state) {
377 hdev = hci_alloc_dev();
378 if (!hdev) {
379 BT_ERR("Can't allocate HCI device");
380 return -ENOMEM;
381 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -0700383 hsmd->hdev = hdev;
384 hdev->bus = HCI_SMD;
385 hdev->driver_data = hsmd;
386 hdev->open = hci_smd_open;
387 hdev->close = hci_smd_close;
388 hdev->send = hci_smd_send_frame;
389 hdev->destruct = hci_smd_destruct;
390 hdev->owner = THIS_MODULE;
391 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392
Ankur Nandwani034ed892011-10-07 11:15:53 -0700393 tasklet_init(&hsmd->hci_event_task,
394 hci_smd_recv_event, (unsigned long) hsmd);
395 tasklet_init(&hsmd->hci_data_task,
396 hci_smd_recv_data, (unsigned long) hsmd);
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -0700397 if (!driver_state) {
398 wake_lock_init(&hs.wake_lock_rx, WAKE_LOCK_SUSPEND,
399 "msm_smd_Rx");
400 wake_lock_init(&hs.wake_lock_tx, WAKE_LOCK_SUSPEND,
401 "msm_smd_Tx");
402 }
Bhasker Netid08d9012011-09-05 19:37:54 +0530403 /*
404 * Setup the timer to monitor whether the Rx queue is empty,
405 * to control the wake lock release
406 */
407 setup_timer(&hsmd->rx_q_timer, schedule_timer,
408 (unsigned long) hsmd->hdev);
409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700410 /* Open the SMD Channel and device and register the callback function */
411 rc = smd_named_open_on_edge(EVENT_CHANNEL, SMD_APPS_WCNSS,
412 &hsmd->event_channel, hdev, hci_smd_notify_event);
413 if (rc < 0) {
414 BT_ERR("Cannot open the command channel");
415 hci_free_dev(hdev);
416 return -ENODEV;
417 }
418
419 rc = smd_named_open_on_edge(DATA_CHANNEL, SMD_APPS_WCNSS,
420 &hsmd->data_channel, hdev, hci_smd_notify_data);
421 if (rc < 0) {
422 BT_ERR("Failed to open the Data channel\n");
423 hci_free_dev(hdev);
424 return -ENODEV;
425 }
426
427 /* Disable the read interrupts on the channel */
428 smd_disable_read_intr(hsmd->event_channel);
429 smd_disable_read_intr(hsmd->data_channel);
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -0700430 if (!driver_state) {
431 if (hci_register_dev(hdev) < 0) {
432 BT_ERR("Can't register HCI device");
433 hci_free_dev(hdev);
434 return -ENODEV;
435 }
436 driver_state = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700437 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700438 return 0;
439}
440
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -0700441static void hci_smd_deregister_dev(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442{
443 smd_close(hs.event_channel);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700444 smd_close(hs.data_channel);
Bhasker Netid08d9012011-09-05 19:37:54 +0530445
Ankur Nandwani327e0502011-11-11 15:47:25 -0800446 if (wake_lock_active(&hs.wake_lock_rx))
447 wake_unlock(&hs.wake_lock_rx);
448
Bhasker Netid08d9012011-09-05 19:37:54 +0530449 /*Destroy the timer used to monitor the Rx queue for emptiness */
450 del_timer_sync(&hs.rx_q_timer);
Ankur Nandwani034ed892011-10-07 11:15:53 -0700451 tasklet_kill(&hs.hci_event_task);
452 tasklet_kill(&hs.hci_data_task);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453}
454
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -0700455static int hcismd_set_enable(const char *val, struct kernel_param *kp)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700456{
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -0700457 int ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -0700459 mutex_lock(&hci_smd_enable);
460
461 ret = param_set_int(val, kp);
462
463 if (ret)
464 goto done;
465
466 switch (hcismd_set) {
467
468 case 1:
469 hci_smd_register_dev(&hs);
470 break;
471 case 0:
472 hci_smd_deregister_dev();
473 break;
474 default:
475 ret = -EFAULT;
476 }
477
478done:
479 mutex_unlock(&hci_smd_enable);
480 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481}
Ankur Nandwani8ffe4e72011-10-10 21:51:48 -0700482
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700483
484MODULE_AUTHOR("Ankur Nandwani <ankurn@codeaurora.org>");
485MODULE_DESCRIPTION("Bluetooth SMD driver");
486MODULE_LICENSE("GPL v2");