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