Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 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 Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 27 | #include <linux/wakelock.h> |
| 28 | #include <linux/uaccess.h> |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 29 | #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 Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 34 | #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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 37 | |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 38 | |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 39 | static int hcismd_set; |
| 40 | static DEFINE_MUTEX(hci_smd_enable); |
| 41 | |
| 42 | static int hcismd_set_enable(const char *val, struct kernel_param *kp); |
| 43 | module_param_call(hcismd_set, hcismd_set_enable, NULL, &hcismd_set, 0644); |
| 44 | |
| 45 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 46 | struct hci_smd_data { |
| 47 | struct hci_dev *hdev; |
| 48 | |
| 49 | struct smd_channel *event_channel; |
| 50 | struct smd_channel *data_channel; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 51 | struct wake_lock wake_lock_tx; |
| 52 | struct wake_lock wake_lock_rx; |
| 53 | struct timer_list rx_q_timer; |
Ankur Nandwani | 034ed89 | 2011-10-07 11:15:53 -0700 | [diff] [blame] | 54 | struct tasklet_struct hci_event_task; |
| 55 | struct tasklet_struct hci_data_task; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 56 | }; |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 57 | static struct hci_smd_data hs; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 58 | |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 59 | /* Rx queue monitor timer function */ |
| 60 | static int is_rx_q_empty(unsigned long arg) |
| 61 | { |
| 62 | struct hci_dev *hdev = (struct hci_dev *) arg; |
| 63 | struct sk_buff_head *list_ = &hdev->rx_q; |
| 64 | struct sk_buff *list = ((struct sk_buff *)list_)->next; |
| 65 | BT_DBG("%s Rx timer triggered", hdev->name); |
| 66 | |
| 67 | if (list == (struct sk_buff *)list_) { |
| 68 | BT_DBG("%s RX queue empty", hdev->name); |
| 69 | return 1; |
| 70 | } else{ |
| 71 | BT_DBG("%s RX queue not empty", hdev->name); |
| 72 | return 0; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static void release_lock(void) |
| 77 | { |
| 78 | struct hci_smd_data *hsmd = &hs; |
| 79 | BT_DBG("Releasing Rx Lock"); |
| 80 | if (is_rx_q_empty((unsigned long)hsmd->hdev) && |
| 81 | wake_lock_active(&hs.wake_lock_rx)) |
| 82 | wake_unlock(&hs.wake_lock_rx); |
| 83 | } |
| 84 | |
| 85 | /* Rx timer callback function */ |
| 86 | static void schedule_timer(unsigned long arg) |
| 87 | { |
| 88 | struct hci_dev *hdev = (struct hci_dev *) arg; |
| 89 | struct hci_smd_data *hsmd = &hs; |
| 90 | BT_DBG("%s Schedule Rx timer", hdev->name); |
| 91 | |
| 92 | if (is_rx_q_empty(arg) && wake_lock_active(&hs.wake_lock_rx)) { |
| 93 | BT_DBG("%s RX queue empty", hdev->name); |
| 94 | /* |
| 95 | * Since the queue is empty, its ideal |
| 96 | * to release the wake lock on Rx |
| 97 | */ |
| 98 | wake_unlock(&hs.wake_lock_rx); |
| 99 | } else{ |
| 100 | BT_DBG("%s RX queue not empty", hdev->name); |
| 101 | /* |
| 102 | * Restart the timer to monitor whether the Rx queue is |
| 103 | * empty for releasing the Rx wake lock |
| 104 | */ |
| 105 | mod_timer(&hsmd->rx_q_timer, |
| 106 | jiffies + msecs_to_jiffies(RX_Q_MONITOR)); |
| 107 | } |
| 108 | } |
| 109 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 110 | static int hci_smd_open(struct hci_dev *hdev) |
| 111 | { |
| 112 | set_bit(HCI_RUNNING, &hdev->flags); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static int hci_smd_close(struct hci_dev *hdev) |
| 118 | { |
| 119 | if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) |
| 120 | return 0; |
| 121 | else |
| 122 | return -EPERM; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static void hci_smd_destruct(struct hci_dev *hdev) |
| 127 | { |
Bhasker Neti | 60576c6 | 2011-12-07 18:45:51 -0800 | [diff] [blame] | 128 | if (NULL != hdev->driver_data) |
| 129 | kfree(hdev->driver_data); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void hci_smd_recv_data(unsigned long arg) |
| 133 | { |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 134 | int len = 0; |
| 135 | int rc = 0; |
| 136 | struct sk_buff *skb = NULL; |
| 137 | unsigned char *buf = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 138 | struct hci_smd_data *hsmd = &hs; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 139 | wake_lock(&hs.wake_lock_rx); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 140 | |
| 141 | len = smd_read_avail(hsmd->data_channel); |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 142 | if (len > HCI_MAX_FRAME_SIZE) { |
| 143 | BT_ERR("Frame larger than the allowed size"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 144 | goto out_data; |
| 145 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 146 | while (len > 0) { |
Ankur Nandwani | c815096 | 2011-08-24 13:06:23 -0700 | [diff] [blame] | 147 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 148 | if (!skb) { |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 149 | BT_ERR("Error in allocating socket buffer"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 150 | goto out_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Ankur Nandwani | c815096 | 2011-08-24 13:06:23 -0700 | [diff] [blame] | 153 | buf = kmalloc(len, GFP_ATOMIC); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 154 | if (!buf) { |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 155 | BT_ERR("Error in allocating buffer"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 156 | rc = -ENOMEM; |
| 157 | goto out_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Ankur Nandwani | 531333f | 2011-10-25 15:47:43 -0700 | [diff] [blame] | 160 | rc = smd_read(hsmd->data_channel, (void *)buf, len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 161 | if (rc < len) { |
| 162 | BT_ERR("Error in reading from the channel"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 163 | goto out_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | memcpy(skb_put(skb, len), buf, len); |
| 167 | skb->dev = (void *)hsmd->hdev; |
| 168 | bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT; |
| 169 | |
| 170 | skb_orphan(skb); |
| 171 | |
| 172 | rc = hci_recv_frame(skb); |
| 173 | if (rc < 0) { |
| 174 | BT_ERR("Error in passing the packet to HCI Layer"); |
Bhasker Neti | 44a792b | 2011-10-11 19:25:22 +0530 | [diff] [blame] | 175 | /* |
| 176 | * skb is getting freed in hci_recv_frame, making it |
| 177 | * to null to avoid multiple access |
| 178 | */ |
| 179 | skb = NULL; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 180 | goto out_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | kfree(buf); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 184 | buf = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 185 | len = smd_read_avail(hsmd->data_channel); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 186 | /* |
| 187 | * Start the timer to monitor whether the Rx queue is |
| 188 | * empty for releasing the Rx wake lock |
| 189 | */ |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 190 | BT_DBG("Rx Timer is starting"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 191 | mod_timer(&hsmd->rx_q_timer, |
| 192 | jiffies + msecs_to_jiffies(RX_Q_MONITOR)); |
| 193 | } |
| 194 | out_data: |
| 195 | release_lock(); |
| 196 | if (rc) { |
| 197 | if (skb) |
| 198 | kfree_skb(skb); |
| 199 | kfree(buf); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | static void hci_smd_recv_event(unsigned long arg) |
| 204 | { |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 205 | int len = 0; |
| 206 | int rc = 0; |
| 207 | struct sk_buff *skb = NULL; |
| 208 | unsigned char *buf = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 209 | struct hci_smd_data *hsmd = &hs; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 210 | wake_lock(&hs.wake_lock_rx); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 211 | |
| 212 | len = smd_read_avail(hsmd->event_channel); |
| 213 | if (len > HCI_MAX_FRAME_SIZE) { |
| 214 | BT_ERR("Frame larger than the allowed size"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 215 | goto out_event; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | while (len > 0) { |
Ankur Nandwani | c815096 | 2011-08-24 13:06:23 -0700 | [diff] [blame] | 219 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 220 | if (!skb) { |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 221 | BT_ERR("Error in allocating socket buffer"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 222 | goto out_event; |
| 223 | } |
Ankur Nandwani | c815096 | 2011-08-24 13:06:23 -0700 | [diff] [blame] | 224 | buf = kmalloc(len, GFP_ATOMIC); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 225 | if (!buf) { |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 226 | BT_ERR("Error in allocating buffer"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 227 | rc = -ENOMEM; |
| 228 | goto out_event; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 229 | } |
Ankur Nandwani | 531333f | 2011-10-25 15:47:43 -0700 | [diff] [blame] | 230 | rc = smd_read(hsmd->event_channel, (void *)buf, len); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 231 | if (rc < len) { |
| 232 | BT_ERR("Error in reading from the event channel"); |
| 233 | goto out_event; |
| 234 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 235 | |
| 236 | memcpy(skb_put(skb, len), buf, len); |
| 237 | skb->dev = (void *)hsmd->hdev; |
| 238 | bt_cb(skb)->pkt_type = HCI_EVENT_PKT; |
| 239 | |
| 240 | skb_orphan(skb); |
| 241 | |
| 242 | rc = hci_recv_frame(skb); |
| 243 | if (rc < 0) { |
| 244 | BT_ERR("Error in passing the packet to HCI Layer"); |
Bhasker Neti | 44a792b | 2011-10-11 19:25:22 +0530 | [diff] [blame] | 245 | /* |
| 246 | * skb is getting freed in hci_recv_frame, making it |
| 247 | * to null to avoid multiple access |
| 248 | */ |
| 249 | skb = NULL; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 250 | goto out_event; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | kfree(buf); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 254 | buf = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 255 | len = smd_read_avail(hsmd->event_channel); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 256 | /* |
| 257 | * Start the timer to monitor whether the Rx queue is |
| 258 | * empty for releasing the Rx wake lock |
| 259 | */ |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 260 | BT_DBG("Rx Timer is starting"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 261 | mod_timer(&hsmd->rx_q_timer, |
| 262 | jiffies + msecs_to_jiffies(RX_Q_MONITOR)); |
| 263 | } |
| 264 | out_event: |
| 265 | release_lock(); |
| 266 | if (rc) { |
| 267 | if (skb) |
| 268 | kfree_skb(skb); |
| 269 | kfree(buf); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
| 273 | static int hci_smd_send_frame(struct sk_buff *skb) |
| 274 | { |
| 275 | int len; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 276 | int avail; |
| 277 | int ret = 0; |
| 278 | wake_lock(&hs.wake_lock_tx); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 279 | |
| 280 | switch (bt_cb(skb)->pkt_type) { |
| 281 | case HCI_COMMAND_PKT: |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 282 | avail = smd_write_avail(hs.event_channel); |
| 283 | if (!avail) { |
| 284 | BT_ERR("No space available for smd frame"); |
| 285 | ret = -ENOSPC; |
| 286 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 287 | len = smd_write(hs.event_channel, skb->data, skb->len); |
| 288 | if (len < skb->len) { |
| 289 | BT_ERR("Failed to write Command %d", len); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 290 | ret = -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 291 | } |
| 292 | break; |
| 293 | case HCI_ACLDATA_PKT: |
| 294 | case HCI_SCODATA_PKT: |
Ankur Nandwani | 23abeb2 | 2011-10-26 16:35:22 -0700 | [diff] [blame] | 295 | avail = smd_write_avail(hs.data_channel); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 296 | if (!avail) { |
| 297 | BT_ERR("No space available for smd frame"); |
| 298 | ret = -ENOSPC; |
| 299 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 300 | len = smd_write(hs.data_channel, skb->data, skb->len); |
| 301 | if (len < skb->len) { |
| 302 | BT_ERR("Failed to write Data %d", len); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 303 | ret = -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 304 | } |
| 305 | break; |
| 306 | default: |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 307 | BT_ERR("Uknown packet type"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 308 | ret = -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 309 | break; |
| 310 | } |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 311 | |
Anubhav Gupta | ddf48ec | 2011-10-03 14:24:13 +0530 | [diff] [blame] | 312 | kfree_skb(skb); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 313 | wake_unlock(&hs.wake_lock_tx); |
| 314 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | |
| 318 | static void hci_smd_notify_event(void *data, unsigned int event) |
| 319 | { |
| 320 | struct hci_dev *hdev = hs.hdev; |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 321 | struct hci_smd_data *hsmd = &hs; |
| 322 | int len = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 323 | |
| 324 | if (!hdev) { |
| 325 | BT_ERR("Frame for unknown HCI device (hdev=NULL)"); |
| 326 | return; |
| 327 | } |
| 328 | |
| 329 | switch (event) { |
| 330 | case SMD_EVENT_DATA: |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 331 | len = smd_read_avail(hsmd->event_channel); |
| 332 | if (len > 0) |
| 333 | tasklet_hi_schedule(&hs.hci_event_task); |
| 334 | else if (len < 0) |
| 335 | BT_ERR("Failed to read event from smd %d", len); |
| 336 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 337 | break; |
| 338 | case SMD_EVENT_OPEN: |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 339 | BT_INFO("opening HCI-SMD channel :%s", EVENT_CHANNEL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 340 | hci_smd_open(hdev); |
| 341 | break; |
| 342 | case SMD_EVENT_CLOSE: |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 343 | BT_INFO("Closing HCI-SMD channel :%s", EVENT_CHANNEL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 344 | hci_smd_close(hdev); |
| 345 | break; |
| 346 | default: |
| 347 | break; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | static void hci_smd_notify_data(void *data, unsigned int event) |
| 352 | { |
| 353 | struct hci_dev *hdev = hs.hdev; |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 354 | struct hci_smd_data *hsmd = &hs; |
| 355 | int len = 0; |
| 356 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 357 | if (!hdev) { |
| 358 | BT_ERR("HCI device (hdev=NULL)"); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | switch (event) { |
| 363 | case SMD_EVENT_DATA: |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 364 | len = smd_read_avail(hsmd->data_channel); |
| 365 | if (len > 0) |
| 366 | tasklet_hi_schedule(&hs.hci_data_task); |
| 367 | else if (len < 0) |
| 368 | BT_ERR("Failed to read data from smd %d", len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 369 | break; |
| 370 | case SMD_EVENT_OPEN: |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 371 | BT_INFO("opening HCI-SMD channel :%s", DATA_CHANNEL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 372 | hci_smd_open(hdev); |
| 373 | break; |
| 374 | case SMD_EVENT_CLOSE: |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 375 | BT_INFO("Closing HCI-SMD channel :%s", DATA_CHANNEL); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 376 | hci_smd_close(hdev); |
| 377 | break; |
| 378 | default: |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | } |
| 383 | |
| 384 | static int hci_smd_register_dev(struct hci_smd_data *hsmd) |
| 385 | { |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 386 | static struct hci_dev *hdev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 387 | int rc; |
| 388 | |
| 389 | /* Initialize and register HCI device */ |
Bhasker Neti | 60576c6 | 2011-12-07 18:45:51 -0800 | [diff] [blame] | 390 | hdev = hci_alloc_dev(); |
| 391 | if (!hdev) { |
| 392 | BT_ERR("Can't allocate HCI device"); |
| 393 | return -ENOMEM; |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 394 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 395 | |
Bhasker Neti | 60576c6 | 2011-12-07 18:45:51 -0800 | [diff] [blame] | 396 | hsmd->hdev = hdev; |
| 397 | hdev->bus = HCI_SMD; |
| 398 | hdev->driver_data = NULL; |
| 399 | hdev->open = hci_smd_open; |
| 400 | hdev->close = hci_smd_close; |
| 401 | hdev->send = hci_smd_send_frame; |
| 402 | hdev->destruct = hci_smd_destruct; |
| 403 | hdev->owner = THIS_MODULE; |
| 404 | |
Ankur Nandwani | 034ed89 | 2011-10-07 11:15:53 -0700 | [diff] [blame] | 405 | tasklet_init(&hsmd->hci_event_task, |
| 406 | hci_smd_recv_event, (unsigned long) hsmd); |
| 407 | tasklet_init(&hsmd->hci_data_task, |
| 408 | hci_smd_recv_data, (unsigned long) hsmd); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 409 | /* |
| 410 | * Setup the timer to monitor whether the Rx queue is empty, |
| 411 | * to control the wake lock release |
| 412 | */ |
| 413 | setup_timer(&hsmd->rx_q_timer, schedule_timer, |
| 414 | (unsigned long) hsmd->hdev); |
| 415 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 416 | /* Open the SMD Channel and device and register the callback function */ |
| 417 | rc = smd_named_open_on_edge(EVENT_CHANNEL, SMD_APPS_WCNSS, |
| 418 | &hsmd->event_channel, hdev, hci_smd_notify_event); |
| 419 | if (rc < 0) { |
| 420 | BT_ERR("Cannot open the command channel"); |
| 421 | hci_free_dev(hdev); |
Royston Rodrigues | eee6c96 | 2011-12-22 16:42:45 +0530 | [diff] [blame] | 422 | hdev = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 423 | return -ENODEV; |
| 424 | } |
| 425 | |
| 426 | rc = smd_named_open_on_edge(DATA_CHANNEL, SMD_APPS_WCNSS, |
| 427 | &hsmd->data_channel, hdev, hci_smd_notify_data); |
| 428 | if (rc < 0) { |
Bhasker Neti | 60734c0 | 2011-11-23 15:17:54 -0800 | [diff] [blame] | 429 | BT_ERR("Failed to open the Data channel"); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 430 | hci_free_dev(hdev); |
Royston Rodrigues | eee6c96 | 2011-12-22 16:42:45 +0530 | [diff] [blame] | 431 | hdev = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 432 | return -ENODEV; |
| 433 | } |
| 434 | |
| 435 | /* Disable the read interrupts on the channel */ |
| 436 | smd_disable_read_intr(hsmd->event_channel); |
| 437 | smd_disable_read_intr(hsmd->data_channel); |
Bhasker Neti | 60576c6 | 2011-12-07 18:45:51 -0800 | [diff] [blame] | 438 | if (hci_register_dev(hdev) < 0) { |
| 439 | BT_ERR("Can't register HCI device"); |
| 440 | hci_free_dev(hdev); |
| 441 | return -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 442 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
Bhasker Neti | 60576c6 | 2011-12-07 18:45:51 -0800 | [diff] [blame] | 446 | static void hci_smd_deregister_dev(struct hci_smd_data *hsmd) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 447 | { |
Royston Rodrigues | eee6c96 | 2011-12-22 16:42:45 +0530 | [diff] [blame] | 448 | if (hsmd->hdev) { |
| 449 | if (hci_unregister_dev(hsmd->hdev) < 0) |
| 450 | BT_ERR("Can't unregister HCI device %s", |
| 451 | hsmd->hdev->name); |
| 452 | |
| 453 | hci_free_dev(hsmd->hdev); |
| 454 | hsmd->hdev = NULL; |
| 455 | } |
| 456 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 457 | smd_close(hs.event_channel); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 458 | smd_close(hs.data_channel); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 459 | |
Ankur Nandwani | 327e050 | 2011-11-11 15:47:25 -0800 | [diff] [blame] | 460 | if (wake_lock_active(&hs.wake_lock_rx)) |
| 461 | wake_unlock(&hs.wake_lock_rx); |
Bhasker Neti | 60576c6 | 2011-12-07 18:45:51 -0800 | [diff] [blame] | 462 | if (wake_lock_active(&hs.wake_lock_tx)) |
| 463 | wake_unlock(&hs.wake_lock_tx); |
Ankur Nandwani | 327e050 | 2011-11-11 15:47:25 -0800 | [diff] [blame] | 464 | |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 465 | /*Destroy the timer used to monitor the Rx queue for emptiness */ |
Royston Rodrigues | eee6c96 | 2011-12-22 16:42:45 +0530 | [diff] [blame] | 466 | if (hs.rx_q_timer.function) { |
| 467 | del_timer_sync(&hs.rx_q_timer); |
| 468 | hs.rx_q_timer.function = NULL; |
| 469 | hs.rx_q_timer.data = 0; |
| 470 | } |
| 471 | |
Ankur Nandwani | 034ed89 | 2011-10-07 11:15:53 -0700 | [diff] [blame] | 472 | tasklet_kill(&hs.hci_event_task); |
| 473 | tasklet_kill(&hs.hci_data_task); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 476 | static int hcismd_set_enable(const char *val, struct kernel_param *kp) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 477 | { |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 478 | int ret = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 479 | |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 480 | mutex_lock(&hci_smd_enable); |
| 481 | |
| 482 | ret = param_set_int(val, kp); |
| 483 | |
| 484 | if (ret) |
| 485 | goto done; |
| 486 | |
| 487 | switch (hcismd_set) { |
| 488 | |
| 489 | case 1: |
| 490 | hci_smd_register_dev(&hs); |
| 491 | break; |
| 492 | case 0: |
Bhasker Neti | 60576c6 | 2011-12-07 18:45:51 -0800 | [diff] [blame] | 493 | hci_smd_deregister_dev(&hs); |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 494 | break; |
| 495 | default: |
| 496 | ret = -EFAULT; |
| 497 | } |
| 498 | |
| 499 | done: |
| 500 | mutex_unlock(&hci_smd_enable); |
| 501 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 502 | } |
Bhasker Neti | 60576c6 | 2011-12-07 18:45:51 -0800 | [diff] [blame] | 503 | static int __init hci_smd_init(void) |
| 504 | { |
| 505 | wake_lock_init(&hs.wake_lock_rx, WAKE_LOCK_SUSPEND, |
| 506 | "msm_smd_Rx"); |
| 507 | wake_lock_init(&hs.wake_lock_tx, WAKE_LOCK_SUSPEND, |
| 508 | "msm_smd_Tx"); |
| 509 | return 0; |
| 510 | } |
| 511 | module_init(hci_smd_init); |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame] | 512 | |
Bhasker Neti | 60576c6 | 2011-12-07 18:45:51 -0800 | [diff] [blame] | 513 | static void __exit hci_smd_exit(void) |
| 514 | { |
| 515 | wake_lock_destroy(&hs.wake_lock_rx); |
| 516 | wake_lock_destroy(&hs.wake_lock_tx); |
| 517 | } |
| 518 | module_exit(hci_smd_exit); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 519 | |
| 520 | MODULE_AUTHOR("Ankur Nandwani <ankurn@codeaurora.org>"); |
| 521 | MODULE_DESCRIPTION("Bluetooth SMD driver"); |
| 522 | MODULE_LICENSE("GPL v2"); |