87d0d3d5ae548f2f49c7060268a206a546173e77
[linux-3.10.git] / drivers / bluetooth / btmrvl_main.c
1 /**
2  * Marvell Bluetooth driver
3  *
4  * Copyright (C) 2009, Marvell International Ltd.
5  *
6  * This software file (the "File") is distributed by Marvell International
7  * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8  * (the "License").  You may use, redistribute and/or modify this File in
9  * accordance with the terms and conditions of the License, a copy of which
10  * is available by writing to the Free Software Foundation, Inc.,
11  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12  * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13  *
14  *
15  * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
17  * ARE EXPRESSLY DISCLAIMED.  The License provides additional details about
18  * this warranty disclaimer.
19  **/
20
21 #include <net/bluetooth/bluetooth.h>
22 #include <net/bluetooth/hci_core.h>
23
24 #include "btmrvl_drv.h"
25
26 #define VERSION "1.0"
27
28 /*
29  * This function is called by interface specific interrupt handler.
30  * It updates Power Save & Host Sleep states, and wakes up the main
31  * thread.
32  */
33 void btmrvl_interrupt(struct btmrvl_private *priv)
34 {
35         priv->adapter->ps_state = PS_AWAKE;
36
37         priv->adapter->wakeup_tries = 0;
38
39         priv->adapter->int_count++;
40
41         wake_up_interruptible(&priv->main_thread.wait_q);
42 }
43 EXPORT_SYMBOL_GPL(btmrvl_interrupt);
44
45 void btmrvl_check_evtpkt(struct btmrvl_private *priv, struct sk_buff *skb)
46 {
47         struct hci_event_hdr *hdr = (void *) skb->data;
48         struct hci_ev_cmd_complete *ec;
49         u16 opcode, ocf;
50
51         if (hdr->evt == HCI_EV_CMD_COMPLETE) {
52                 ec = (void *) (skb->data + HCI_EVENT_HDR_SIZE);
53                 opcode = __le16_to_cpu(ec->opcode);
54                 ocf = hci_opcode_ocf(opcode);
55                 if (ocf == BT_CMD_MODULE_CFG_REQ &&
56                                         priv->btmrvl_dev.sendcmdflag) {
57                         priv->btmrvl_dev.sendcmdflag = false;
58                         priv->adapter->cmd_complete = true;
59                         wake_up_interruptible(&priv->adapter->cmd_wait_q);
60                 }
61         }
62 }
63 EXPORT_SYMBOL_GPL(btmrvl_check_evtpkt);
64
65 int btmrvl_process_event(struct btmrvl_private *priv, struct sk_buff *skb)
66 {
67         struct btmrvl_adapter *adapter = priv->adapter;
68         struct btmrvl_event *event;
69         int ret = 0;
70
71         event = (struct btmrvl_event *) skb->data;
72         if (event->ec != 0xff) {
73                 BT_DBG("Not Marvell Event=%x", event->ec);
74                 ret = -EINVAL;
75                 goto exit;
76         }
77
78         switch (event->data[0]) {
79         case BT_CMD_AUTO_SLEEP_MODE:
80                 if (!event->data[2]) {
81                         if (event->data[1] == BT_PS_ENABLE)
82                                 adapter->psmode = 1;
83                         else
84                                 adapter->psmode = 0;
85                         BT_DBG("PS Mode:%s",
86                                 (adapter->psmode) ? "Enable" : "Disable");
87                 } else {
88                         BT_DBG("PS Mode command failed");
89                 }
90                 break;
91
92         case BT_CMD_HOST_SLEEP_CONFIG:
93                 if (!event->data[3])
94                         BT_DBG("gpio=%x, gap=%x", event->data[1],
95                                                         event->data[2]);
96                 else
97                         BT_DBG("HSCFG command failed");
98                 break;
99
100         case BT_CMD_HOST_SLEEP_ENABLE:
101                 if (!event->data[1]) {
102                         adapter->hs_state = HS_ACTIVATED;
103                         if (adapter->psmode)
104                                 adapter->ps_state = PS_SLEEP;
105                         wake_up_interruptible(&adapter->cmd_wait_q);
106                         BT_DBG("HS ACTIVATED!");
107                 } else {
108                         BT_DBG("HS Enable failed");
109                 }
110                 break;
111
112         case BT_CMD_MODULE_CFG_REQ:
113                 if (priv->btmrvl_dev.sendcmdflag &&
114                                 event->data[1] == MODULE_BRINGUP_REQ) {
115                         BT_DBG("EVENT:%s",
116                                 ((event->data[2] == MODULE_BROUGHT_UP) ||
117                                 (event->data[2] == MODULE_ALREADY_UP)) ?
118                                 "Bring-up succeed" : "Bring-up failed");
119                 } else if (priv->btmrvl_dev.sendcmdflag &&
120                                 event->data[1] == MODULE_SHUTDOWN_REQ) {
121                         BT_DBG("EVENT:%s", (event->data[2]) ?
122                                 "Shutdown failed" : "Shutdown succeed");
123                 } else {
124                         BT_DBG("BT_CMD_MODULE_CFG_REQ resp for APP");
125                         ret = -EINVAL;
126                 }
127                 break;
128
129         case BT_EVENT_POWER_STATE:
130                 if (event->data[1] == BT_PS_SLEEP)
131                         adapter->ps_state = PS_SLEEP;
132                 BT_DBG("EVENT:%s",
133                         (adapter->ps_state) ? "PS_SLEEP" : "PS_AWAKE");
134                 break;
135
136         default:
137                 BT_DBG("Unknown Event=%d", event->data[0]);
138                 ret = -EINVAL;
139                 break;
140         }
141
142 exit:
143         if (!ret)
144                 kfree_skb(skb);
145
146         return ret;
147 }
148 EXPORT_SYMBOL_GPL(btmrvl_process_event);
149
150 int btmrvl_send_module_cfg_cmd(struct btmrvl_private *priv, int subcmd)
151 {
152         struct sk_buff *skb;
153         struct btmrvl_cmd *cmd;
154         int ret = 0;
155
156         skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
157         if (skb == NULL) {
158                 BT_ERR("No free skb");
159                 return -ENOMEM;
160         }
161
162         cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
163         cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_MODULE_CFG_REQ));
164         cmd->length = 1;
165         cmd->data[0] = subcmd;
166
167         bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
168
169         skb->dev = (void *) priv->btmrvl_dev.hcidev;
170         skb_queue_head(&priv->adapter->tx_queue, skb);
171
172         priv->btmrvl_dev.sendcmdflag = true;
173
174         priv->adapter->cmd_complete = false;
175
176         BT_DBG("Queue module cfg Command");
177
178         wake_up_interruptible(&priv->main_thread.wait_q);
179
180         if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
181                                 priv->adapter->cmd_complete,
182                                 msecs_to_jiffies(WAIT_UNTIL_CMD_RESP))) {
183                 ret = -ETIMEDOUT;
184                 BT_ERR("module_cfg_cmd(%x): timeout: %d",
185                                         subcmd, priv->btmrvl_dev.sendcmdflag);
186         }
187
188         BT_DBG("module cfg Command done");
189
190         return ret;
191 }
192 EXPORT_SYMBOL_GPL(btmrvl_send_module_cfg_cmd);
193
194 int btmrvl_enable_ps(struct btmrvl_private *priv)
195 {
196         struct sk_buff *skb;
197         struct btmrvl_cmd *cmd;
198
199         skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
200         if (skb == NULL) {
201                 BT_ERR("No free skb");
202                 return -ENOMEM;
203         }
204
205         cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
206         cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF,
207                                         BT_CMD_AUTO_SLEEP_MODE));
208         cmd->length = 1;
209
210         if (priv->btmrvl_dev.psmode)
211                 cmd->data[0] = BT_PS_ENABLE;
212         else
213                 cmd->data[0] = BT_PS_DISABLE;
214
215         bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
216
217         skb->dev = (void *) priv->btmrvl_dev.hcidev;
218         skb_queue_head(&priv->adapter->tx_queue, skb);
219
220         BT_DBG("Queue PSMODE Command:%d", cmd->data[0]);
221
222         return 0;
223 }
224 EXPORT_SYMBOL_GPL(btmrvl_enable_ps);
225
226 static int btmrvl_enable_hs(struct btmrvl_private *priv)
227 {
228         struct sk_buff *skb;
229         struct btmrvl_cmd *cmd;
230         int ret = 0;
231
232         skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
233         if (skb == NULL) {
234                 BT_ERR("No free skb");
235                 return -ENOMEM;
236         }
237
238         cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
239         cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_HOST_SLEEP_ENABLE));
240         cmd->length = 0;
241
242         bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
243
244         skb->dev = (void *) priv->btmrvl_dev.hcidev;
245         skb_queue_head(&priv->adapter->tx_queue, skb);
246
247         BT_DBG("Queue hs enable Command");
248
249         wake_up_interruptible(&priv->main_thread.wait_q);
250
251         if (!wait_event_interruptible_timeout(priv->adapter->cmd_wait_q,
252                         priv->adapter->hs_state,
253                         msecs_to_jiffies(WAIT_UNTIL_HS_STATE_CHANGED))) {
254                 ret = -ETIMEDOUT;
255                 BT_ERR("timeout: %d, %d,%d", priv->adapter->hs_state,
256                                                 priv->adapter->ps_state,
257                                                 priv->adapter->wakeup_tries);
258         }
259
260         return ret;
261 }
262
263 int btmrvl_prepare_command(struct btmrvl_private *priv)
264 {
265         struct sk_buff *skb = NULL;
266         struct btmrvl_cmd *cmd;
267         int ret = 0;
268
269         if (priv->btmrvl_dev.hscfgcmd) {
270                 priv->btmrvl_dev.hscfgcmd = 0;
271
272                 skb = bt_skb_alloc(sizeof(*cmd), GFP_ATOMIC);
273                 if (skb == NULL) {
274                         BT_ERR("No free skb");
275                         return -ENOMEM;
276                 }
277
278                 cmd = (struct btmrvl_cmd *) skb_put(skb, sizeof(*cmd));
279                 cmd->ocf_ogf = cpu_to_le16(hci_opcode_pack(OGF, BT_CMD_HOST_SLEEP_CONFIG));
280                 cmd->length = 2;
281                 cmd->data[0] = (priv->btmrvl_dev.gpio_gap & 0xff00) >> 8;
282                 cmd->data[1] = (u8) (priv->btmrvl_dev.gpio_gap & 0x00ff);
283
284                 bt_cb(skb)->pkt_type = MRVL_VENDOR_PKT;
285
286                 skb->dev = (void *) priv->btmrvl_dev.hcidev;
287                 skb_queue_head(&priv->adapter->tx_queue, skb);
288
289                 BT_DBG("Queue HSCFG Command, gpio=0x%x, gap=0x%x",
290                                                 cmd->data[0], cmd->data[1]);
291         }
292
293         if (priv->btmrvl_dev.pscmd) {
294                 priv->btmrvl_dev.pscmd = 0;
295                 btmrvl_enable_ps(priv);
296         }
297
298         if (priv->btmrvl_dev.hscmd) {
299                 priv->btmrvl_dev.hscmd = 0;
300
301                 if (priv->btmrvl_dev.hsmode) {
302                         ret = btmrvl_enable_hs(priv);
303                 } else {
304                         ret = priv->hw_wakeup_firmware(priv);
305                         priv->adapter->hs_state = HS_DEACTIVATED;
306                 }
307         }
308
309         return ret;
310 }
311
312 static int btmrvl_tx_pkt(struct btmrvl_private *priv, struct sk_buff *skb)
313 {
314         int ret = 0;
315
316         if (!skb || !skb->data)
317                 return -EINVAL;
318
319         if (!skb->len || ((skb->len + BTM_HEADER_LEN) > BTM_UPLD_SIZE)) {
320                 BT_ERR("Tx Error: Bad skb length %d : %d",
321                                                 skb->len, BTM_UPLD_SIZE);
322                 return -EINVAL;
323         }
324
325         if (skb_headroom(skb) < BTM_HEADER_LEN) {
326                 struct sk_buff *tmp = skb;
327
328                 skb = skb_realloc_headroom(skb, BTM_HEADER_LEN);
329                 if (!skb) {
330                         BT_ERR("Tx Error: realloc_headroom failed %d",
331                                 BTM_HEADER_LEN);
332                         skb = tmp;
333                         return -EINVAL;
334                 }
335
336                 kfree_skb(tmp);
337         }
338
339         skb_push(skb, BTM_HEADER_LEN);
340
341         /* header type: byte[3]
342          * HCI_COMMAND = 1, ACL_DATA = 2, SCO_DATA = 3, 0xFE = Vendor
343          * header length: byte[2][1][0]
344          */
345
346         skb->data[0] = (skb->len & 0x0000ff);
347         skb->data[1] = (skb->len & 0x00ff00) >> 8;
348         skb->data[2] = (skb->len & 0xff0000) >> 16;
349         skb->data[3] = bt_cb(skb)->pkt_type;
350
351         if (priv->hw_host_to_card)
352                 ret = priv->hw_host_to_card(priv, skb->data, skb->len);
353
354         return ret;
355 }
356
357 static void btmrvl_init_adapter(struct btmrvl_private *priv)
358 {
359         skb_queue_head_init(&priv->adapter->tx_queue);
360
361         priv->adapter->ps_state = PS_AWAKE;
362
363         init_waitqueue_head(&priv->adapter->cmd_wait_q);
364 }
365
366 static void btmrvl_free_adapter(struct btmrvl_private *priv)
367 {
368         skb_queue_purge(&priv->adapter->tx_queue);
369
370         kfree(priv->adapter);
371
372         priv->adapter = NULL;
373 }
374
375 static int btmrvl_ioctl(struct hci_dev *hdev,
376                                 unsigned int cmd, unsigned long arg)
377 {
378         return -ENOIOCTLCMD;
379 }
380
381 static void btmrvl_destruct(struct hci_dev *hdev)
382 {
383 }
384
385 static int btmrvl_send_frame(struct sk_buff *skb)
386 {
387         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
388         struct btmrvl_private *priv = NULL;
389
390         BT_DBG("type=%d, len=%d", skb->pkt_type, skb->len);
391
392         if (!hdev || !hdev->driver_data) {
393                 BT_ERR("Frame for unknown HCI device");
394                 return -ENODEV;
395         }
396
397         priv = (struct btmrvl_private *) hdev->driver_data;
398         if (!test_bit(HCI_RUNNING, &hdev->flags)) {
399                 BT_ERR("Failed testing HCI_RUNING, flags=%lx", hdev->flags);
400                 print_hex_dump_bytes("data: ", DUMP_PREFIX_OFFSET,
401                                                         skb->data, skb->len);
402                 return -EBUSY;
403         }
404
405         switch (bt_cb(skb)->pkt_type) {
406         case HCI_COMMAND_PKT:
407                 hdev->stat.cmd_tx++;
408                 break;
409
410         case HCI_ACLDATA_PKT:
411                 hdev->stat.acl_tx++;
412                 break;
413
414         case HCI_SCODATA_PKT:
415                 hdev->stat.sco_tx++;
416                 break;
417         }
418
419         skb_queue_tail(&priv->adapter->tx_queue, skb);
420
421         wake_up_interruptible(&priv->main_thread.wait_q);
422
423         return 0;
424 }
425
426 static int btmrvl_flush(struct hci_dev *hdev)
427 {
428         struct btmrvl_private *priv = hdev->driver_data;
429
430         skb_queue_purge(&priv->adapter->tx_queue);
431
432         return 0;
433 }
434
435 static int btmrvl_close(struct hci_dev *hdev)
436 {
437         struct btmrvl_private *priv = hdev->driver_data;
438
439         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
440                 return 0;
441
442         skb_queue_purge(&priv->adapter->tx_queue);
443
444         return 0;
445 }
446
447 static int btmrvl_open(struct hci_dev *hdev)
448 {
449         set_bit(HCI_RUNNING, &hdev->flags);
450
451         return 0;
452 }
453
454 /*
455  * This function handles the event generated by firmware, rx data
456  * received from firmware, and tx data sent from kernel.
457  */
458 static int btmrvl_service_main_thread(void *data)
459 {
460         struct btmrvl_thread *thread = data;
461         struct btmrvl_private *priv = thread->priv;
462         struct btmrvl_adapter *adapter = priv->adapter;
463         wait_queue_t wait;
464         struct sk_buff *skb;
465         ulong flags;
466
467         init_waitqueue_entry(&wait, current);
468
469         current->flags |= PF_NOFREEZE;
470
471         for (;;) {
472                 add_wait_queue(&thread->wait_q, &wait);
473
474                 set_current_state(TASK_INTERRUPTIBLE);
475
476                 if (adapter->wakeup_tries ||
477                                 ((!adapter->int_count) &&
478                                 (!priv->btmrvl_dev.tx_dnld_rdy ||
479                                 skb_queue_empty(&adapter->tx_queue)))) {
480                         BT_DBG("main_thread is sleeping...");
481                         schedule();
482                 }
483
484                 set_current_state(TASK_RUNNING);
485
486                 remove_wait_queue(&thread->wait_q, &wait);
487
488                 BT_DBG("main_thread woke up");
489
490                 if (kthread_should_stop()) {
491                         BT_DBG("main_thread: break from main thread");
492                         break;
493                 }
494
495                 spin_lock_irqsave(&priv->driver_lock, flags);
496                 if (adapter->int_count) {
497                         adapter->int_count = 0;
498                 } else if (adapter->ps_state == PS_SLEEP &&
499                                         !skb_queue_empty(&adapter->tx_queue)) {
500                         spin_unlock_irqrestore(&priv->driver_lock, flags);
501                         adapter->wakeup_tries++;
502                         priv->hw_wakeup_firmware(priv);
503                         continue;
504                 }
505                 spin_unlock_irqrestore(&priv->driver_lock, flags);
506
507                 if (adapter->ps_state == PS_SLEEP)
508                         continue;
509
510                 if (!priv->btmrvl_dev.tx_dnld_rdy)
511                         continue;
512
513                 skb = skb_dequeue(&adapter->tx_queue);
514                 if (skb) {
515                         if (btmrvl_tx_pkt(priv, skb))
516                                 priv->btmrvl_dev.hcidev->stat.err_tx++;
517                         else
518                                 priv->btmrvl_dev.hcidev->stat.byte_tx += skb->len;
519
520                         kfree_skb(skb);
521                 }
522         }
523
524         return 0;
525 }
526
527 struct btmrvl_private *btmrvl_add_card(void *card)
528 {
529         struct hci_dev *hdev = NULL;
530         struct btmrvl_private *priv;
531         int ret;
532
533         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
534         if (!priv) {
535                 BT_ERR("Can not allocate priv");
536                 goto err_priv;
537         }
538
539         priv->adapter = kzalloc(sizeof(*priv->adapter), GFP_KERNEL);
540         if (!priv->adapter) {
541                 BT_ERR("Allocate buffer for btmrvl_adapter failed!");
542                 goto err_adapter;
543         }
544
545         btmrvl_init_adapter(priv);
546
547         hdev = hci_alloc_dev();
548         if (!hdev) {
549                 BT_ERR("Can not allocate HCI device");
550                 goto err_hdev;
551         }
552
553         BT_DBG("Starting kthread...");
554         priv->main_thread.priv = priv;
555         spin_lock_init(&priv->driver_lock);
556
557         init_waitqueue_head(&priv->main_thread.wait_q);
558         priv->main_thread.task = kthread_run(btmrvl_service_main_thread,
559                                 &priv->main_thread, "btmrvl_main_service");
560
561         priv->btmrvl_dev.hcidev = hdev;
562         priv->btmrvl_dev.card = card;
563
564         hdev->driver_data = priv;
565
566         priv->btmrvl_dev.tx_dnld_rdy = true;
567
568         hdev->bus = HCI_SDIO;
569         hdev->open = btmrvl_open;
570         hdev->close = btmrvl_close;
571         hdev->flush = btmrvl_flush;
572         hdev->send = btmrvl_send_frame;
573         hdev->destruct = btmrvl_destruct;
574         hdev->ioctl = btmrvl_ioctl;
575         hdev->owner = THIS_MODULE;
576
577         ret = hci_register_dev(hdev);
578         if (ret < 0) {
579                 BT_ERR("Can not register HCI device");
580                 goto err_hci_register_dev;
581         }
582
583 #ifdef CONFIG_DEBUG_FS
584         btmrvl_debugfs_init(hdev);
585 #endif
586
587         return priv;
588
589 err_hci_register_dev:
590         /* Stop the thread servicing the interrupts */
591         kthread_stop(priv->main_thread.task);
592
593         hci_free_dev(hdev);
594
595 err_hdev:
596         btmrvl_free_adapter(priv);
597
598 err_adapter:
599         kfree(priv);
600
601 err_priv:
602         return NULL;
603 }
604 EXPORT_SYMBOL_GPL(btmrvl_add_card);
605
606 int btmrvl_remove_card(struct btmrvl_private *priv)
607 {
608         struct hci_dev *hdev;
609
610         hdev = priv->btmrvl_dev.hcidev;
611
612         wake_up_interruptible(&priv->adapter->cmd_wait_q);
613
614         kthread_stop(priv->main_thread.task);
615
616 #ifdef CONFIG_DEBUG_FS
617         btmrvl_debugfs_remove(hdev);
618 #endif
619
620         hci_unregister_dev(hdev);
621
622         hci_free_dev(hdev);
623
624         priv->btmrvl_dev.hcidev = NULL;
625
626         btmrvl_free_adapter(priv);
627
628         kfree(priv);
629
630         return 0;
631 }
632 EXPORT_SYMBOL_GPL(btmrvl_remove_card);
633
634 MODULE_AUTHOR("Marvell International Ltd.");
635 MODULE_DESCRIPTION("Marvell Bluetooth driver ver " VERSION);
636 MODULE_VERSION(VERSION);
637 MODULE_LICENSE("GPL v2");