Skip to content

Commit d019930

Browse files
ChrisCH-LuVudentz
authored andcommitted
Bluetooth: btmtk: move btusb_mtk_hci_wmt_sync to btmtk.c
Move btusb_mtk_hci_wmt_sync from btusb.c to btmtk.c which holds vendor specific stuff and would make btusb.c clean. Add usb.h header to btmtksdio.c/btmtkuart.c for usb related element defined in btmtk.h Signed-off-by: Sean Wang <[email protected]> Signed-off-by: Chris Lu <[email protected]> Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent 95f9292 commit d019930

File tree

5 files changed

+305
-283
lines changed

5 files changed

+305
-283
lines changed

drivers/bluetooth/btmtk.c

Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
#include <linux/module.h>
66
#include <linux/firmware.h>
7+
#include <linux/usb.h>
78

89
#include <net/bluetooth/bluetooth.h>
910
#include <net/bluetooth/hci_core.h>
@@ -431,6 +432,270 @@ int btmtk_process_coredump(struct hci_dev *hdev, struct sk_buff *skb)
431432
}
432433
EXPORT_SYMBOL_GPL(btmtk_process_coredump);
433434

435+
static void btmtk_usb_wmt_recv(struct urb *urb)
436+
{
437+
struct hci_dev *hdev = urb->context;
438+
struct btmtk_data *data = hci_get_priv(hdev);
439+
struct sk_buff *skb;
440+
int err;
441+
442+
if (urb->status == 0 && urb->actual_length > 0) {
443+
hdev->stat.byte_rx += urb->actual_length;
444+
445+
/* WMT event shouldn't be fragmented and the size should be
446+
* less than HCI_WMT_MAX_EVENT_SIZE.
447+
*/
448+
skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
449+
if (!skb) {
450+
hdev->stat.err_rx++;
451+
kfree(urb->setup_packet);
452+
return;
453+
}
454+
455+
hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
456+
skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
457+
458+
/* When someone waits for the WMT event, the skb is being cloned
459+
* and being processed the events from there then.
460+
*/
461+
if (test_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags)) {
462+
data->evt_skb = skb_clone(skb, GFP_ATOMIC);
463+
if (!data->evt_skb) {
464+
kfree_skb(skb);
465+
kfree(urb->setup_packet);
466+
return;
467+
}
468+
}
469+
470+
err = hci_recv_frame(hdev, skb);
471+
if (err < 0) {
472+
kfree_skb(data->evt_skb);
473+
data->evt_skb = NULL;
474+
kfree(urb->setup_packet);
475+
return;
476+
}
477+
478+
if (test_and_clear_bit(BTMTK_TX_WAIT_VND_EVT,
479+
&data->flags)) {
480+
/* Barrier to sync with other CPUs */
481+
smp_mb__after_atomic();
482+
wake_up_bit(&data->flags,
483+
BTMTK_TX_WAIT_VND_EVT);
484+
}
485+
kfree(urb->setup_packet);
486+
return;
487+
} else if (urb->status == -ENOENT) {
488+
/* Avoid suspend failed when usb_kill_urb */
489+
return;
490+
}
491+
492+
usb_mark_last_busy(data->udev);
493+
494+
/* The URB complete handler is still called with urb->actual_length = 0
495+
* when the event is not available, so we should keep re-submitting
496+
* URB until WMT event returns, Also, It's necessary to wait some time
497+
* between the two consecutive control URBs to relax the target device
498+
* to generate the event. Otherwise, the WMT event cannot return from
499+
* the device successfully.
500+
*/
501+
udelay(500);
502+
503+
usb_anchor_urb(urb, data->ctrl_anchor);
504+
err = usb_submit_urb(urb, GFP_ATOMIC);
505+
if (err < 0) {
506+
kfree(urb->setup_packet);
507+
/* -EPERM: urb is being killed;
508+
* -ENODEV: device got disconnected
509+
*/
510+
if (err != -EPERM && err != -ENODEV)
511+
bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
512+
urb, -err);
513+
usb_unanchor_urb(urb);
514+
}
515+
}
516+
517+
static int btmtk_usb_submit_wmt_recv_urb(struct hci_dev *hdev)
518+
{
519+
struct btmtk_data *data = hci_get_priv(hdev);
520+
struct usb_ctrlrequest *dr;
521+
unsigned char *buf;
522+
int err, size = 64;
523+
unsigned int pipe;
524+
struct urb *urb;
525+
526+
urb = usb_alloc_urb(0, GFP_KERNEL);
527+
if (!urb)
528+
return -ENOMEM;
529+
530+
dr = kmalloc(sizeof(*dr), GFP_KERNEL);
531+
if (!dr) {
532+
usb_free_urb(urb);
533+
return -ENOMEM;
534+
}
535+
536+
dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
537+
dr->bRequest = 1;
538+
dr->wIndex = cpu_to_le16(0);
539+
dr->wValue = cpu_to_le16(48);
540+
dr->wLength = cpu_to_le16(size);
541+
542+
buf = kmalloc(size, GFP_KERNEL);
543+
if (!buf) {
544+
kfree(dr);
545+
usb_free_urb(urb);
546+
return -ENOMEM;
547+
}
548+
549+
pipe = usb_rcvctrlpipe(data->udev, 0);
550+
551+
usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
552+
buf, size, btmtk_usb_wmt_recv, hdev);
553+
554+
urb->transfer_flags |= URB_FREE_BUFFER;
555+
556+
usb_anchor_urb(urb, data->ctrl_anchor);
557+
err = usb_submit_urb(urb, GFP_KERNEL);
558+
if (err < 0) {
559+
if (err != -EPERM && err != -ENODEV)
560+
bt_dev_err(hdev, "urb %p submission failed (%d)",
561+
urb, -err);
562+
usb_unanchor_urb(urb);
563+
}
564+
565+
usb_free_urb(urb);
566+
567+
return err;
568+
}
569+
570+
int btmtk_usb_hci_wmt_sync(struct hci_dev *hdev,
571+
struct btmtk_hci_wmt_params *wmt_params)
572+
{
573+
struct btmtk_data *data = hci_get_priv(hdev);
574+
struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
575+
u32 hlen, status = BTMTK_WMT_INVALID;
576+
struct btmtk_hci_wmt_evt *wmt_evt;
577+
struct btmtk_hci_wmt_cmd *wc;
578+
struct btmtk_wmt_hdr *hdr;
579+
int err;
580+
581+
/* Send the WMT command and wait until the WMT event returns */
582+
hlen = sizeof(*hdr) + wmt_params->dlen;
583+
if (hlen > 255)
584+
return -EINVAL;
585+
586+
wc = kzalloc(hlen, GFP_KERNEL);
587+
if (!wc)
588+
return -ENOMEM;
589+
590+
hdr = &wc->hdr;
591+
hdr->dir = 1;
592+
hdr->op = wmt_params->op;
593+
hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
594+
hdr->flag = wmt_params->flag;
595+
memcpy(wc->data, wmt_params->data, wmt_params->dlen);
596+
597+
set_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags);
598+
599+
/* WMT cmd/event doesn't follow up the generic HCI cmd/event handling,
600+
* it needs constantly polling control pipe until the host received the
601+
* WMT event, thus, we should require to specifically acquire PM counter
602+
* on the USB to prevent the interface from entering auto suspended
603+
* while WMT cmd/event in progress.
604+
*/
605+
err = usb_autopm_get_interface(data->intf);
606+
if (err < 0)
607+
goto err_free_wc;
608+
609+
err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
610+
611+
if (err < 0) {
612+
clear_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags);
613+
usb_autopm_put_interface(data->intf);
614+
goto err_free_wc;
615+
}
616+
617+
/* Submit control IN URB on demand to process the WMT event */
618+
err = btmtk_usb_submit_wmt_recv_urb(hdev);
619+
620+
usb_autopm_put_interface(data->intf);
621+
622+
if (err < 0)
623+
goto err_free_wc;
624+
625+
/* The vendor specific WMT commands are all answered by a vendor
626+
* specific event and will have the Command Status or Command
627+
* Complete as with usual HCI command flow control.
628+
*
629+
* After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
630+
* state to be cleared. The driver specific event receive routine
631+
* will clear that state and with that indicate completion of the
632+
* WMT command.
633+
*/
634+
err = wait_on_bit_timeout(&data->flags, BTMTK_TX_WAIT_VND_EVT,
635+
TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
636+
if (err == -EINTR) {
637+
bt_dev_err(hdev, "Execution of wmt command interrupted");
638+
clear_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags);
639+
goto err_free_wc;
640+
}
641+
642+
if (err) {
643+
bt_dev_err(hdev, "Execution of wmt command timed out");
644+
clear_bit(BTMTK_TX_WAIT_VND_EVT, &data->flags);
645+
err = -ETIMEDOUT;
646+
goto err_free_wc;
647+
}
648+
649+
if (data->evt_skb == NULL)
650+
goto err_free_wc;
651+
652+
/* Parse and handle the return WMT event */
653+
wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
654+
if (wmt_evt->whdr.op != hdr->op) {
655+
bt_dev_err(hdev, "Wrong op received %d expected %d",
656+
wmt_evt->whdr.op, hdr->op);
657+
err = -EIO;
658+
goto err_free_skb;
659+
}
660+
661+
switch (wmt_evt->whdr.op) {
662+
case BTMTK_WMT_SEMAPHORE:
663+
if (wmt_evt->whdr.flag == 2)
664+
status = BTMTK_WMT_PATCH_UNDONE;
665+
else
666+
status = BTMTK_WMT_PATCH_DONE;
667+
break;
668+
case BTMTK_WMT_FUNC_CTRL:
669+
wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
670+
if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
671+
status = BTMTK_WMT_ON_DONE;
672+
else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
673+
status = BTMTK_WMT_ON_PROGRESS;
674+
else
675+
status = BTMTK_WMT_ON_UNDONE;
676+
break;
677+
case BTMTK_WMT_PATCH_DWNLD:
678+
if (wmt_evt->whdr.flag == 2)
679+
status = BTMTK_WMT_PATCH_DONE;
680+
else if (wmt_evt->whdr.flag == 1)
681+
status = BTMTK_WMT_PATCH_PROGRESS;
682+
else
683+
status = BTMTK_WMT_PATCH_UNDONE;
684+
break;
685+
}
686+
687+
if (wmt_params->status)
688+
*wmt_params->status = status;
689+
690+
err_free_skb:
691+
kfree_skb(data->evt_skb);
692+
data->evt_skb = NULL;
693+
err_free_wc:
694+
kfree(wc);
695+
return err;
696+
}
697+
EXPORT_SYMBOL_GPL(btmtk_usb_hci_wmt_sync);
698+
434699
MODULE_AUTHOR("Sean Wang <[email protected]>");
435700
MODULE_AUTHOR("Mark Chen <[email protected]>");
436701
MODULE_DESCRIPTION("Bluetooth support for MediaTek devices ver " VERSION);

drivers/bluetooth/btmtk.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@
2828
#define MTK_COREDUMP_END_LEN (sizeof(MTK_COREDUMP_END))
2929
#define MTK_COREDUMP_NUM 255
3030

31+
/* UHW CR mapping */
32+
#define MTK_BT_MISC 0x70002510
33+
#define MTK_BT_SUBSYS_RST 0x70002610
34+
#define MTK_UDMA_INT_STA_BT 0x74000024
35+
#define MTK_UDMA_INT_STA_BT1 0x74000308
36+
#define MTK_BT_WDT_STATUS 0x740003A0
37+
#define MTK_EP_RST_OPT 0x74011890
38+
#define MTK_EP_RST_IN_OUT_OPT 0x00010001
39+
#define MTK_BT_RST_DONE 0x00000100
40+
#define MTK_BT_RESET_REG_CONNV3 0x70028610
41+
#define MTK_BT_READ_DEV_ID 0x70010200
42+
3143
enum {
3244
BTMTK_WMT_PATCH_DWNLD = 0x1,
3345
BTMTK_WMT_TEST = 0x2,
@@ -126,6 +138,10 @@ struct btmtk_hci_wmt_params {
126138
u32 *status;
127139
};
128140

141+
enum {
142+
BTMTK_TX_WAIT_VND_EVT,
143+
};
144+
129145
typedef int (*btmtk_reset_sync_func_t)(struct hci_dev *, void *);
130146

131147
struct btmtk_coredump_info {
@@ -136,9 +152,15 @@ struct btmtk_coredump_info {
136152
};
137153

138154
struct btmtk_data {
155+
unsigned long flags;
139156
u32 dev_id;
140157
btmtk_reset_sync_func_t reset_sync;
141158
struct btmtk_coredump_info cd_info;
159+
160+
struct usb_device *udev;
161+
struct usb_interface *intf;
162+
struct usb_anchor *ctrl_anchor;
163+
struct sk_buff *evt_skb;
142164
};
143165

144166
typedef int (*wmt_cmd_sync_func_t)(struct hci_dev *,
@@ -163,6 +185,9 @@ int btmtk_process_coredump(struct hci_dev *hdev, struct sk_buff *skb);
163185

164186
void btmtk_fw_get_filename(char *buf, size_t size, u32 dev_id, u32 fw_ver,
165187
u32 fw_flavor);
188+
189+
int btmtk_usb_hci_wmt_sync(struct hci_dev *hdev,
190+
struct btmtk_hci_wmt_params *wmt_params);
166191
#else
167192

168193
static inline int btmtk_set_bdaddr(struct hci_dev *hdev,
@@ -202,4 +227,10 @@ static void btmtk_fw_get_filename(char *buf, size_t size, u32 dev_id,
202227
u32 fw_ver, u32 fw_flavor)
203228
{
204229
}
230+
231+
static int btmtk_usb_hci_wmt_sync(struct hci_dev *hdev,
232+
struct btmtk_hci_wmt_params *wmt_params)
233+
{
234+
return -EOPNOTSUPP;
235+
}
205236
#endif

drivers/bluetooth/btmtksdio.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/of.h>
2121
#include <linux/pm_runtime.h>
2222
#include <linux/skbuff.h>
23+
#include <linux/usb.h>
2324

2425
#include <linux/mmc/host.h>
2526
#include <linux/mmc/sdio_ids.h>

drivers/bluetooth/btmtkuart.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <linux/regulator/consumer.h>
2323
#include <linux/serdev.h>
2424
#include <linux/skbuff.h>
25+
#include <linux/usb.h>
2526

2627
#include <net/bluetooth/bluetooth.h>
2728
#include <net/bluetooth/hci_core.h>

0 commit comments

Comments
 (0)