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 | |
| 39 | static unsigned int driver_state; |
| 40 | |
| 41 | static int hcismd_set; |
| 42 | static DEFINE_MUTEX(hci_smd_enable); |
| 43 | |
| 44 | static int hcismd_set_enable(const char *val, struct kernel_param *kp); |
| 45 | module_param_call(hcismd_set, hcismd_set_enable, NULL, &hcismd_set, 0644); |
| 46 | |
| 47 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 48 | struct hci_smd_data { |
| 49 | struct hci_dev *hdev; |
| 50 | |
| 51 | struct smd_channel *event_channel; |
| 52 | struct smd_channel *data_channel; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 53 | struct wake_lock wake_lock_tx; |
| 54 | struct wake_lock wake_lock_rx; |
| 55 | struct timer_list rx_q_timer; |
Ankur Nandwani | 034ed89 | 2011-10-07 11:15:53 -0700 | [diff] [blame] | 56 | struct tasklet_struct hci_event_task; |
| 57 | struct tasklet_struct hci_data_task; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 58 | }; |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 59 | static struct hci_smd_data hs; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 60 | |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 61 | /* Rx queue monitor timer function */ |
| 62 | static 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 | |
| 78 | static 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 */ |
| 88 | static 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 112 | static int hci_smd_open(struct hci_dev *hdev) |
| 113 | { |
| 114 | set_bit(HCI_RUNNING, &hdev->flags); |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | static 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 | |
| 128 | static void hci_smd_destruct(struct hci_dev *hdev) |
| 129 | { |
| 130 | kfree(hdev->driver_data); |
| 131 | } |
| 132 | |
| 133 | static void hci_smd_recv_data(unsigned long arg) |
| 134 | { |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 135 | int len = 0; |
| 136 | int rc = 0; |
| 137 | struct sk_buff *skb = NULL; |
| 138 | unsigned char *buf = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 139 | struct hci_smd_data *hsmd = &hs; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 140 | wake_lock(&hs.wake_lock_rx); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 141 | |
| 142 | len = smd_read_avail(hsmd->data_channel); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 143 | if (len <= 0) { |
| 144 | BT_ERR("Nothing to read from SMD channel\n"); |
| 145 | goto out_data; |
| 146 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 147 | while (len > 0) { |
Ankur Nandwani | c815096 | 2011-08-24 13:06:23 -0700 | [diff] [blame] | 148 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 149 | if (!skb) { |
| 150 | BT_ERR("Error in allocating socket buffer\n"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 151 | goto out_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Ankur Nandwani | c815096 | 2011-08-24 13:06:23 -0700 | [diff] [blame] | 154 | buf = kmalloc(len, GFP_ATOMIC); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 155 | if (!buf) { |
| 156 | BT_ERR("Error in allocating buffer\n"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 157 | rc = -ENOMEM; |
| 158 | goto out_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Ankur Nandwani | 531333f | 2011-10-25 15:47:43 -0700 | [diff] [blame] | 161 | rc = smd_read(hsmd->data_channel, (void *)buf, len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 162 | if (rc < len) { |
| 163 | BT_ERR("Error in reading from the channel"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 164 | goto out_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 165 | } |
| 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 Neti | 44a792b | 2011-10-11 19:25:22 +0530 | [diff] [blame] | 176 | /* |
| 177 | * skb is getting freed in hci_recv_frame, making it |
| 178 | * to null to avoid multiple access |
| 179 | */ |
| 180 | skb = NULL; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 181 | goto out_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | kfree(buf); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 185 | buf = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 186 | len = smd_read_avail(hsmd->data_channel); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 187 | /* |
| 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 | } |
| 195 | out_data: |
| 196 | release_lock(); |
| 197 | if (rc) { |
| 198 | if (skb) |
| 199 | kfree_skb(skb); |
| 200 | kfree(buf); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
| 204 | static void hci_smd_recv_event(unsigned long arg) |
| 205 | { |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 206 | int len = 0; |
| 207 | int rc = 0; |
| 208 | struct sk_buff *skb = NULL; |
| 209 | unsigned char *buf = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 210 | struct hci_smd_data *hsmd = &hs; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 211 | wake_lock(&hs.wake_lock_rx); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 212 | |
| 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 Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 216 | goto out_event; |
| 217 | } else if (len <= 0) { |
| 218 | BT_ERR("Nothing to read from SMD channel\n"); |
| 219 | goto out_event; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | while (len > 0) { |
Ankur Nandwani | c815096 | 2011-08-24 13:06:23 -0700 | [diff] [blame] | 223 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 224 | if (!skb) { |
| 225 | BT_ERR("Error in allocating socket buffer\n"); |
| 226 | goto out_event; |
| 227 | } |
Ankur Nandwani | c815096 | 2011-08-24 13:06:23 -0700 | [diff] [blame] | 228 | buf = kmalloc(len, GFP_ATOMIC); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 229 | if (!buf) { |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 230 | BT_ERR("Error in allocating buffer\n"); |
| 231 | rc = -ENOMEM; |
| 232 | goto out_event; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 233 | } |
Ankur Nandwani | 531333f | 2011-10-25 15:47:43 -0700 | [diff] [blame] | 234 | rc = smd_read(hsmd->event_channel, (void *)buf, len); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 235 | if (rc < len) { |
| 236 | BT_ERR("Error in reading from the event channel"); |
| 237 | goto out_event; |
| 238 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 239 | |
| 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 Neti | 44a792b | 2011-10-11 19:25:22 +0530 | [diff] [blame] | 249 | /* |
| 250 | * skb is getting freed in hci_recv_frame, making it |
| 251 | * to null to avoid multiple access |
| 252 | */ |
| 253 | skb = NULL; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 254 | goto out_event; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | kfree(buf); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 258 | buf = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 259 | len = smd_read_avail(hsmd->event_channel); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 260 | /* |
| 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 | } |
| 268 | out_event: |
| 269 | release_lock(); |
| 270 | if (rc) { |
| 271 | if (skb) |
| 272 | kfree_skb(skb); |
| 273 | kfree(buf); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | static int hci_smd_send_frame(struct sk_buff *skb) |
| 278 | { |
| 279 | int len; |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 280 | int avail; |
| 281 | int ret = 0; |
| 282 | wake_lock(&hs.wake_lock_tx); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 283 | |
| 284 | switch (bt_cb(skb)->pkt_type) { |
| 285 | case HCI_COMMAND_PKT: |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 286 | avail = smd_write_avail(hs.event_channel); |
| 287 | if (!avail) { |
| 288 | BT_ERR("No space available for smd frame"); |
| 289 | ret = -ENOSPC; |
| 290 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 291 | 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 Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 294 | ret = -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 295 | } |
| 296 | break; |
| 297 | case HCI_ACLDATA_PKT: |
| 298 | case HCI_SCODATA_PKT: |
Ankur Nandwani | 23abeb2 | 2011-10-26 16:35:22 -0700 | [diff] [blame] | 299 | avail = smd_write_avail(hs.data_channel); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 300 | if (!avail) { |
| 301 | BT_ERR("No space available for smd frame"); |
| 302 | ret = -ENOSPC; |
| 303 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 304 | 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 Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 307 | ret = -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 308 | } |
| 309 | break; |
| 310 | default: |
| 311 | BT_ERR("Uknown packet type\n"); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 312 | ret = -ENODEV; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 313 | break; |
| 314 | } |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 315 | |
Anubhav Gupta | ddf48ec | 2011-10-03 14:24:13 +0530 | [diff] [blame] | 316 | kfree_skb(skb); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 317 | wake_unlock(&hs.wake_lock_tx); |
| 318 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | |
| 322 | static 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 Nandwani | 034ed89 | 2011-10-07 11:15:53 -0700 | [diff] [blame] | 333 | tasklet_hi_schedule(&hs.hci_event_task); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 334 | 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 | |
| 346 | static 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 Nandwani | 034ed89 | 2011-10-07 11:15:53 -0700 | [diff] [blame] | 356 | tasklet_hi_schedule(&hs.hci_data_task); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 357 | 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 | |
| 370 | static int hci_smd_register_dev(struct hci_smd_data *hsmd) |
| 371 | { |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 372 | static struct hci_dev *hdev; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 373 | int rc; |
| 374 | |
| 375 | /* Initialize and register HCI device */ |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 376 | if (!driver_state) { |
| 377 | hdev = hci_alloc_dev(); |
| 378 | if (!hdev) { |
| 379 | BT_ERR("Can't allocate HCI device"); |
| 380 | return -ENOMEM; |
| 381 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 382 | |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 383 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 392 | |
Ankur Nandwani | 034ed89 | 2011-10-07 11:15:53 -0700 | [diff] [blame] | 393 | 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 Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 397 | 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 Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 403 | /* |
| 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 410 | /* 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 Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 430 | 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 Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 437 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 441 | static void hci_smd_deregister_dev(void) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 442 | { |
| 443 | smd_close(hs.event_channel); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 444 | smd_close(hs.data_channel); |
Bhasker Neti | d08d901 | 2011-09-05 19:37:54 +0530 | [diff] [blame] | 445 | |
| 446 | /*Destroy the timer used to monitor the Rx queue for emptiness */ |
| 447 | del_timer_sync(&hs.rx_q_timer); |
Ankur Nandwani | 034ed89 | 2011-10-07 11:15:53 -0700 | [diff] [blame] | 448 | tasklet_kill(&hs.hci_event_task); |
| 449 | tasklet_kill(&hs.hci_data_task); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 452 | static int hcismd_set_enable(const char *val, struct kernel_param *kp) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 453 | { |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 454 | int ret = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 455 | |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 456 | mutex_lock(&hci_smd_enable); |
| 457 | |
| 458 | ret = param_set_int(val, kp); |
| 459 | |
| 460 | if (ret) |
| 461 | goto done; |
| 462 | |
| 463 | switch (hcismd_set) { |
| 464 | |
| 465 | case 1: |
| 466 | hci_smd_register_dev(&hs); |
| 467 | break; |
| 468 | case 0: |
| 469 | hci_smd_deregister_dev(); |
| 470 | break; |
| 471 | default: |
| 472 | ret = -EFAULT; |
| 473 | } |
| 474 | |
| 475 | done: |
| 476 | mutex_unlock(&hci_smd_enable); |
| 477 | return ret; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 478 | } |
Ankur Nandwani | 8ffe4e7 | 2011-10-10 21:51:48 -0700 | [diff] [blame^] | 479 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 480 | |
| 481 | MODULE_AUTHOR("Ankur Nandwani <ankurn@codeaurora.org>"); |
| 482 | MODULE_DESCRIPTION("Bluetooth SMD driver"); |
| 483 | MODULE_LICENSE("GPL v2"); |