1 // SPDX-License-Identifier: ISC
3 * Copyright (C) 2018 Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>
6 #include <linux/module.h>
11 #define MT_VEND_REQ_MAX_RETRY 10
12 #define MT_VEND_REQ_TOUT_MS 300
14 static bool disable_usb_sg
;
15 module_param_named(disable_usb_sg
, disable_usb_sg
, bool, 0644);
16 MODULE_PARM_DESC(disable_usb_sg
, "Disable usb scatter-gather support");
18 static int __mt76u_vendor_request(struct mt76_dev
*dev
, u8 req
,
19 u8 req_type
, u16 val
, u16 offset
,
20 void *buf
, size_t len
)
22 struct usb_interface
*uintf
= to_usb_interface(dev
->dev
);
23 struct usb_device
*udev
= interface_to_usbdev(uintf
);
27 lockdep_assert_held(&dev
->usb
.usb_ctrl_mtx
);
29 pipe
= (req_type
& USB_DIR_IN
) ? usb_rcvctrlpipe(udev
, 0)
30 : usb_sndctrlpipe(udev
, 0);
31 for (i
= 0; i
< MT_VEND_REQ_MAX_RETRY
; i
++) {
32 if (test_bit(MT76_REMOVED
, &dev
->state
))
35 ret
= usb_control_msg(udev
, pipe
, req
, req_type
, val
,
36 offset
, buf
, len
, MT_VEND_REQ_TOUT_MS
);
38 set_bit(MT76_REMOVED
, &dev
->state
);
39 if (ret
>= 0 || ret
== -ENODEV
)
41 usleep_range(5000, 10000);
44 dev_err(dev
->dev
, "vendor request req:%02x off:%04x failed:%d\n",
49 int mt76u_vendor_request(struct mt76_dev
*dev
, u8 req
,
50 u8 req_type
, u16 val
, u16 offset
,
51 void *buf
, size_t len
)
55 mutex_lock(&dev
->usb
.usb_ctrl_mtx
);
56 ret
= __mt76u_vendor_request(dev
, req
, req_type
,
57 val
, offset
, buf
, len
);
58 trace_usb_reg_wr(dev
, offset
, val
);
59 mutex_unlock(&dev
->usb
.usb_ctrl_mtx
);
63 EXPORT_SYMBOL_GPL(mt76u_vendor_request
);
65 static u32
__mt76u_rr(struct mt76_dev
*dev
, u32 addr
)
67 struct mt76_usb
*usb
= &dev
->usb
;
73 switch (addr
& MT_VEND_TYPE_MASK
) {
74 case MT_VEND_TYPE_EEPROM
:
75 req
= MT_VEND_READ_EEPROM
;
77 case MT_VEND_TYPE_CFG
:
78 req
= MT_VEND_READ_CFG
;
81 req
= MT_VEND_MULTI_READ
;
84 offset
= addr
& ~MT_VEND_TYPE_MASK
;
86 ret
= __mt76u_vendor_request(dev
, req
,
87 USB_DIR_IN
| USB_TYPE_VENDOR
,
88 0, offset
, &usb
->reg_val
, sizeof(__le32
));
89 if (ret
== sizeof(__le32
))
90 data
= le32_to_cpu(usb
->reg_val
);
91 trace_usb_reg_rr(dev
, addr
, data
);
96 static u32
mt76u_rr(struct mt76_dev
*dev
, u32 addr
)
100 mutex_lock(&dev
->usb
.usb_ctrl_mtx
);
101 ret
= __mt76u_rr(dev
, addr
);
102 mutex_unlock(&dev
->usb
.usb_ctrl_mtx
);
107 static void __mt76u_wr(struct mt76_dev
*dev
, u32 addr
, u32 val
)
109 struct mt76_usb
*usb
= &dev
->usb
;
113 switch (addr
& MT_VEND_TYPE_MASK
) {
114 case MT_VEND_TYPE_CFG
:
115 req
= MT_VEND_WRITE_CFG
;
118 req
= MT_VEND_MULTI_WRITE
;
121 offset
= addr
& ~MT_VEND_TYPE_MASK
;
123 usb
->reg_val
= cpu_to_le32(val
);
124 __mt76u_vendor_request(dev
, req
,
125 USB_DIR_OUT
| USB_TYPE_VENDOR
, 0,
126 offset
, &usb
->reg_val
, sizeof(__le32
));
127 trace_usb_reg_wr(dev
, addr
, val
);
130 static void mt76u_wr(struct mt76_dev
*dev
, u32 addr
, u32 val
)
132 mutex_lock(&dev
->usb
.usb_ctrl_mtx
);
133 __mt76u_wr(dev
, addr
, val
);
134 mutex_unlock(&dev
->usb
.usb_ctrl_mtx
);
137 static u32
mt76u_rmw(struct mt76_dev
*dev
, u32 addr
,
140 mutex_lock(&dev
->usb
.usb_ctrl_mtx
);
141 val
|= __mt76u_rr(dev
, addr
) & ~mask
;
142 __mt76u_wr(dev
, addr
, val
);
143 mutex_unlock(&dev
->usb
.usb_ctrl_mtx
);
148 static void mt76u_copy(struct mt76_dev
*dev
, u32 offset
,
149 const void *data
, int len
)
151 struct mt76_usb
*usb
= &dev
->usb
;
152 const u32
*val
= data
;
155 mutex_lock(&usb
->usb_ctrl_mtx
);
156 for (i
= 0; i
< DIV_ROUND_UP(len
, 4); i
++) {
157 put_unaligned(val
[i
], (u32
*)usb
->data
);
158 ret
= __mt76u_vendor_request(dev
, MT_VEND_MULTI_WRITE
,
159 USB_DIR_OUT
| USB_TYPE_VENDOR
,
160 0, offset
+ i
* 4, usb
->data
,
165 mutex_unlock(&usb
->usb_ctrl_mtx
);
168 void mt76u_single_wr(struct mt76_dev
*dev
, const u8 req
,
169 const u16 offset
, const u32 val
)
171 mutex_lock(&dev
->usb
.usb_ctrl_mtx
);
172 __mt76u_vendor_request(dev
, req
,
173 USB_DIR_OUT
| USB_TYPE_VENDOR
,
174 val
& 0xffff, offset
, NULL
, 0);
175 __mt76u_vendor_request(dev
, req
,
176 USB_DIR_OUT
| USB_TYPE_VENDOR
,
177 val
>> 16, offset
+ 2, NULL
, 0);
178 mutex_unlock(&dev
->usb
.usb_ctrl_mtx
);
180 EXPORT_SYMBOL_GPL(mt76u_single_wr
);
183 mt76u_req_wr_rp(struct mt76_dev
*dev
, u32 base
,
184 const struct mt76_reg_pair
*data
, int len
)
186 struct mt76_usb
*usb
= &dev
->usb
;
188 mutex_lock(&usb
->usb_ctrl_mtx
);
190 __mt76u_wr(dev
, base
+ data
->reg
, data
->value
);
194 mutex_unlock(&usb
->usb_ctrl_mtx
);
200 mt76u_wr_rp(struct mt76_dev
*dev
, u32 base
,
201 const struct mt76_reg_pair
*data
, int n
)
203 if (test_bit(MT76_STATE_MCU_RUNNING
, &dev
->state
))
204 return dev
->mcu_ops
->mcu_wr_rp(dev
, base
, data
, n
);
206 return mt76u_req_wr_rp(dev
, base
, data
, n
);
210 mt76u_req_rd_rp(struct mt76_dev
*dev
, u32 base
, struct mt76_reg_pair
*data
,
213 struct mt76_usb
*usb
= &dev
->usb
;
215 mutex_lock(&usb
->usb_ctrl_mtx
);
217 data
->value
= __mt76u_rr(dev
, base
+ data
->reg
);
221 mutex_unlock(&usb
->usb_ctrl_mtx
);
227 mt76u_rd_rp(struct mt76_dev
*dev
, u32 base
,
228 struct mt76_reg_pair
*data
, int n
)
230 if (test_bit(MT76_STATE_MCU_RUNNING
, &dev
->state
))
231 return dev
->mcu_ops
->mcu_rd_rp(dev
, base
, data
, n
);
233 return mt76u_req_rd_rp(dev
, base
, data
, n
);
236 static bool mt76u_check_sg(struct mt76_dev
*dev
)
238 struct usb_interface
*uintf
= to_usb_interface(dev
->dev
);
239 struct usb_device
*udev
= interface_to_usbdev(uintf
);
241 return (!disable_usb_sg
&& udev
->bus
->sg_tablesize
> 0 &&
242 (udev
->bus
->no_sg_constraint
||
243 udev
->speed
== USB_SPEED_WIRELESS
));
247 mt76u_set_endpoints(struct usb_interface
*intf
,
248 struct mt76_usb
*usb
)
250 struct usb_host_interface
*intf_desc
= intf
->cur_altsetting
;
251 struct usb_endpoint_descriptor
*ep_desc
;
252 int i
, in_ep
= 0, out_ep
= 0;
254 for (i
= 0; i
< intf_desc
->desc
.bNumEndpoints
; i
++) {
255 ep_desc
= &intf_desc
->endpoint
[i
].desc
;
257 if (usb_endpoint_is_bulk_in(ep_desc
) &&
258 in_ep
< __MT_EP_IN_MAX
) {
259 usb
->in_ep
[in_ep
] = usb_endpoint_num(ep_desc
);
261 } else if (usb_endpoint_is_bulk_out(ep_desc
) &&
262 out_ep
< __MT_EP_OUT_MAX
) {
263 usb
->out_ep
[out_ep
] = usb_endpoint_num(ep_desc
);
268 if (in_ep
!= __MT_EP_IN_MAX
|| out_ep
!= __MT_EP_OUT_MAX
)
274 mt76u_fill_rx_sg(struct mt76_dev
*dev
, struct mt76_queue
*q
, struct urb
*urb
,
279 for (i
= 0; i
< nsgs
; i
++) {
284 data
= page_frag_alloc(&q
->rx_page
, q
->buf_size
, gfp
);
288 page
= virt_to_head_page(data
);
289 offset
= data
- page_address(page
);
290 sg_set_page(&urb
->sg
[i
], page
, q
->buf_size
, offset
);
296 for (j
= nsgs
; j
< urb
->num_sgs
; j
++)
297 skb_free_frag(sg_virt(&urb
->sg
[j
]));
301 urb
->num_sgs
= max_t(int, i
, urb
->num_sgs
);
302 urb
->transfer_buffer_length
= urb
->num_sgs
* q
->buf_size
;
303 sg_init_marker(urb
->sg
, urb
->num_sgs
);
305 return i
? : -ENOMEM
;
309 mt76u_refill_rx(struct mt76_dev
*dev
, struct urb
*urb
, int nsgs
, gfp_t gfp
)
311 struct mt76_queue
*q
= &dev
->q_rx
[MT_RXQ_MAIN
];
314 return mt76u_fill_rx_sg(dev
, q
, urb
, nsgs
, gfp
);
316 urb
->transfer_buffer_length
= q
->buf_size
;
317 urb
->transfer_buffer
= page_frag_alloc(&q
->rx_page
, q
->buf_size
, gfp
);
319 return urb
->transfer_buffer
? 0 : -ENOMEM
;
323 mt76u_urb_alloc(struct mt76_dev
*dev
, struct mt76_queue_entry
*e
,
326 unsigned int size
= sizeof(struct urb
);
329 size
+= sg_max_size
* sizeof(struct scatterlist
);
331 e
->urb
= kzalloc(size
, GFP_KERNEL
);
335 usb_init_urb(e
->urb
);
338 e
->urb
->sg
= (struct scatterlist
*)(e
->urb
+ 1);
344 mt76u_rx_urb_alloc(struct mt76_dev
*dev
, struct mt76_queue_entry
*e
)
348 err
= mt76u_urb_alloc(dev
, e
, MT_RX_SG_MAX_SIZE
);
352 return mt76u_refill_rx(dev
, e
->urb
, MT_RX_SG_MAX_SIZE
,
356 static void mt76u_urb_free(struct urb
*urb
)
360 for (i
= 0; i
< urb
->num_sgs
; i
++)
361 skb_free_frag(sg_virt(&urb
->sg
[i
]));
363 if (urb
->transfer_buffer
)
364 skb_free_frag(urb
->transfer_buffer
);
370 mt76u_fill_bulk_urb(struct mt76_dev
*dev
, int dir
, int index
,
371 struct urb
*urb
, usb_complete_t complete_fn
,
374 struct usb_interface
*uintf
= to_usb_interface(dev
->dev
);
375 struct usb_device
*udev
= interface_to_usbdev(uintf
);
378 if (dir
== USB_DIR_IN
)
379 pipe
= usb_rcvbulkpipe(udev
, dev
->usb
.in_ep
[index
]);
381 pipe
= usb_sndbulkpipe(udev
, dev
->usb
.out_ep
[index
]);
385 urb
->complete
= complete_fn
;
386 urb
->context
= context
;
389 static inline struct urb
*
390 mt76u_get_next_rx_entry(struct mt76_dev
*dev
)
392 struct mt76_queue
*q
= &dev
->q_rx
[MT_RXQ_MAIN
];
393 struct urb
*urb
= NULL
;
396 spin_lock_irqsave(&q
->lock
, flags
);
398 urb
= q
->entry
[q
->head
].urb
;
399 q
->head
= (q
->head
+ 1) % q
->ndesc
;
402 spin_unlock_irqrestore(&q
->lock
, flags
);
407 static int mt76u_get_rx_entry_len(u8
*data
, u32 data_len
)
409 u16 dma_len
, min_len
;
411 dma_len
= get_unaligned_le16(data
);
412 min_len
= MT_DMA_HDR_LEN
+ MT_RX_RXWI_LEN
+
415 if (data_len
< min_len
|| !dma_len
||
416 dma_len
+ MT_DMA_HDR_LEN
> data_len
||
422 static struct sk_buff
*
423 mt76u_build_rx_skb(void *data
, int len
, int buf_size
)
427 if (SKB_WITH_OVERHEAD(buf_size
) < MT_DMA_HDR_LEN
+ len
) {
430 /* slow path, not enough space for data and
433 skb
= alloc_skb(MT_SKB_HEAD_LEN
, GFP_ATOMIC
);
437 skb_put_data(skb
, data
+ MT_DMA_HDR_LEN
, MT_SKB_HEAD_LEN
);
438 data
+= (MT_DMA_HDR_LEN
+ MT_SKB_HEAD_LEN
);
439 page
= virt_to_head_page(data
);
440 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
441 page
, data
- page_address(page
),
442 len
- MT_SKB_HEAD_LEN
, buf_size
);
448 skb
= build_skb(data
, buf_size
);
452 skb_reserve(skb
, MT_DMA_HDR_LEN
);
459 mt76u_process_rx_entry(struct mt76_dev
*dev
, struct urb
*urb
)
461 struct mt76_queue
*q
= &dev
->q_rx
[MT_RXQ_MAIN
];
462 u8
*data
= urb
->num_sgs
? sg_virt(&urb
->sg
[0]) : urb
->transfer_buffer
;
463 int data_len
= urb
->num_sgs
? urb
->sg
[0].length
: urb
->actual_length
;
467 if (!test_bit(MT76_STATE_INITIALIZED
, &dev
->state
))
470 len
= mt76u_get_rx_entry_len(data
, urb
->actual_length
);
474 data_len
= min_t(int, len
, data_len
- MT_DMA_HDR_LEN
);
475 skb
= mt76u_build_rx_skb(data
, data_len
, q
->buf_size
);
480 while (len
> 0 && nsgs
< urb
->num_sgs
) {
481 data_len
= min_t(int, len
, urb
->sg
[nsgs
].length
);
482 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
483 sg_page(&urb
->sg
[nsgs
]),
484 urb
->sg
[nsgs
].offset
,
485 data_len
, q
->buf_size
);
489 dev
->drv
->rx_skb(dev
, MT_RXQ_MAIN
, skb
);
494 static void mt76u_complete_rx(struct urb
*urb
)
496 struct mt76_dev
*dev
= urb
->context
;
497 struct mt76_queue
*q
= &dev
->q_rx
[MT_RXQ_MAIN
];
500 trace_rx_urb(dev
, urb
);
502 switch (urb
->status
) {
508 dev_err_ratelimited(dev
->dev
, "rx urb failed: %d\n",
515 spin_lock_irqsave(&q
->lock
, flags
);
516 if (WARN_ONCE(q
->entry
[q
->tail
].urb
!= urb
, "rx urb mismatch"))
519 q
->tail
= (q
->tail
+ 1) % q
->ndesc
;
521 tasklet_schedule(&dev
->usb
.rx_tasklet
);
523 spin_unlock_irqrestore(&q
->lock
, flags
);
527 mt76u_submit_rx_buf(struct mt76_dev
*dev
, struct urb
*urb
)
529 mt76u_fill_bulk_urb(dev
, USB_DIR_IN
, MT_EP_IN_PKT_RX
, urb
,
530 mt76u_complete_rx
, dev
);
531 trace_submit_urb(dev
, urb
);
533 return usb_submit_urb(urb
, GFP_ATOMIC
);
536 static void mt76u_rx_tasklet(unsigned long data
)
538 struct mt76_dev
*dev
= (struct mt76_dev
*)data
;
545 urb
= mt76u_get_next_rx_entry(dev
);
549 count
= mt76u_process_rx_entry(dev
, urb
);
551 err
= mt76u_refill_rx(dev
, urb
, count
, GFP_ATOMIC
);
555 mt76u_submit_rx_buf(dev
, urb
);
557 mt76_rx_poll_complete(dev
, MT_RXQ_MAIN
, NULL
);
562 static int mt76u_submit_rx_buffers(struct mt76_dev
*dev
)
564 struct mt76_queue
*q
= &dev
->q_rx
[MT_RXQ_MAIN
];
568 spin_lock_irqsave(&q
->lock
, flags
);
569 for (i
= 0; i
< q
->ndesc
; i
++) {
570 err
= mt76u_submit_rx_buf(dev
, q
->entry
[i
].urb
);
574 q
->head
= q
->tail
= 0;
576 spin_unlock_irqrestore(&q
->lock
, flags
);
581 static int mt76u_alloc_rx(struct mt76_dev
*dev
)
583 struct mt76_usb
*usb
= &dev
->usb
;
584 struct mt76_queue
*q
= &dev
->q_rx
[MT_RXQ_MAIN
];
587 usb
->mcu
.data
= devm_kmalloc(dev
->dev
, MCU_RESP_URB_SIZE
, GFP_KERNEL
);
591 spin_lock_init(&q
->lock
);
592 q
->entry
= devm_kcalloc(dev
->dev
,
593 MT_NUM_RX_ENTRIES
, sizeof(*q
->entry
),
598 q
->ndesc
= MT_NUM_RX_ENTRIES
;
599 q
->buf_size
= PAGE_SIZE
;
601 for (i
= 0; i
< q
->ndesc
; i
++) {
602 err
= mt76u_rx_urb_alloc(dev
, &q
->entry
[i
]);
607 return mt76u_submit_rx_buffers(dev
);
610 static void mt76u_free_rx(struct mt76_dev
*dev
)
612 struct mt76_queue
*q
= &dev
->q_rx
[MT_RXQ_MAIN
];
616 for (i
= 0; i
< q
->ndesc
; i
++)
617 mt76u_urb_free(q
->entry
[i
].urb
);
622 page
= virt_to_page(q
->rx_page
.va
);
623 __page_frag_cache_drain(page
, q
->rx_page
.pagecnt_bias
);
624 memset(&q
->rx_page
, 0, sizeof(q
->rx_page
));
627 void mt76u_stop_rx(struct mt76_dev
*dev
)
629 struct mt76_queue
*q
= &dev
->q_rx
[MT_RXQ_MAIN
];
632 for (i
= 0; i
< q
->ndesc
; i
++)
633 usb_poison_urb(q
->entry
[i
].urb
);
635 tasklet_kill(&dev
->usb
.rx_tasklet
);
637 EXPORT_SYMBOL_GPL(mt76u_stop_rx
);
639 int mt76u_resume_rx(struct mt76_dev
*dev
)
641 struct mt76_queue
*q
= &dev
->q_rx
[MT_RXQ_MAIN
];
644 for (i
= 0; i
< q
->ndesc
; i
++)
645 usb_unpoison_urb(q
->entry
[i
].urb
);
647 return mt76u_submit_rx_buffers(dev
);
649 EXPORT_SYMBOL_GPL(mt76u_resume_rx
);
651 static void mt76u_tx_tasklet(unsigned long data
)
653 struct mt76_dev
*dev
= (struct mt76_dev
*)data
;
654 struct mt76_queue_entry entry
;
655 struct mt76_sw_queue
*sq
;
656 struct mt76_queue
*q
;
660 for (i
= 0; i
< IEEE80211_NUM_ACS
; i
++) {
661 u32 n_dequeued
= 0, n_sw_dequeued
= 0;
666 while (q
->queued
> n_dequeued
) {
667 if (!q
->entry
[q
->head
].done
)
670 if (q
->entry
[q
->head
].schedule
) {
671 q
->entry
[q
->head
].schedule
= false;
675 entry
= q
->entry
[q
->head
];
676 q
->entry
[q
->head
].done
= false;
677 q
->head
= (q
->head
+ 1) % q
->ndesc
;
680 dev
->drv
->tx_complete_skb(dev
, i
, &entry
);
683 spin_lock_bh(&q
->lock
);
685 sq
->swq_queued
-= n_sw_dequeued
;
686 q
->queued
-= n_dequeued
;
688 wake
= q
->stopped
&& q
->queued
< q
->ndesc
- 8;
693 wake_up(&dev
->tx_wait
);
695 spin_unlock_bh(&q
->lock
);
697 mt76_txq_schedule(dev
, i
);
699 if (!test_and_set_bit(MT76_READING_STATS
, &dev
->state
))
700 queue_work(dev
->usb
.stat_wq
, &dev
->usb
.stat_work
);
702 ieee80211_wake_queue(dev
->hw
, i
);
706 static void mt76u_tx_status_data(struct work_struct
*work
)
708 struct mt76_usb
*usb
;
709 struct mt76_dev
*dev
;
713 usb
= container_of(work
, struct mt76_usb
, stat_work
);
714 dev
= container_of(usb
, struct mt76_dev
, usb
);
717 if (test_bit(MT76_REMOVED
, &dev
->state
))
720 if (!dev
->drv
->tx_status_data(dev
, &update
))
725 if (count
&& test_bit(MT76_STATE_RUNNING
, &dev
->state
))
726 queue_work(usb
->stat_wq
, &usb
->stat_work
);
728 clear_bit(MT76_READING_STATS
, &dev
->state
);
731 static void mt76u_complete_tx(struct urb
*urb
)
733 struct mt76_dev
*dev
= dev_get_drvdata(&urb
->dev
->dev
);
734 struct mt76_queue_entry
*e
= urb
->context
;
736 if (mt76u_urb_error(urb
))
737 dev_err(dev
->dev
, "tx urb failed: %d\n", urb
->status
);
740 tasklet_schedule(&dev
->tx_tasklet
);
744 mt76u_tx_setup_buffers(struct mt76_dev
*dev
, struct sk_buff
*skb
,
747 urb
->transfer_buffer_length
= skb
->len
;
749 if (!dev
->usb
.sg_en
) {
750 urb
->transfer_buffer
= skb
->data
;
754 sg_init_table(urb
->sg
, MT_TX_SG_MAX_SIZE
);
755 urb
->num_sgs
= skb_to_sgvec(skb
, urb
->sg
, 0, skb
->len
);
763 mt76u_tx_queue_skb(struct mt76_dev
*dev
, enum mt76_txq_id qid
,
764 struct sk_buff
*skb
, struct mt76_wcid
*wcid
,
765 struct ieee80211_sta
*sta
)
767 struct mt76_queue
*q
= dev
->q_tx
[qid
].q
;
768 struct mt76_tx_info tx_info
= {
774 if (q
->queued
== q
->ndesc
)
777 skb
->prev
= skb
->next
= NULL
;
778 err
= dev
->drv
->tx_prepare_skb(dev
, NULL
, qid
, wcid
, sta
, &tx_info
);
782 err
= mt76u_tx_setup_buffers(dev
, tx_info
.skb
, q
->entry
[idx
].urb
);
786 mt76u_fill_bulk_urb(dev
, USB_DIR_OUT
, q2ep(q
->hw_idx
),
787 q
->entry
[idx
].urb
, mt76u_complete_tx
,
790 q
->tail
= (q
->tail
+ 1) % q
->ndesc
;
791 q
->entry
[idx
].skb
= tx_info
.skb
;
797 static void mt76u_tx_kick(struct mt76_dev
*dev
, struct mt76_queue
*q
)
802 while (q
->first
!= q
->tail
) {
803 urb
= q
->entry
[q
->first
].urb
;
805 trace_submit_urb(dev
, urb
);
806 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
809 set_bit(MT76_REMOVED
, &dev
->state
);
811 dev_err(dev
->dev
, "tx urb submit failed:%d\n",
815 q
->first
= (q
->first
+ 1) % q
->ndesc
;
819 static int mt76u_alloc_tx(struct mt76_dev
*dev
)
821 struct mt76_queue
*q
;
824 for (i
= 0; i
<= MT_TXQ_PSD
; i
++) {
825 INIT_LIST_HEAD(&dev
->q_tx
[i
].swq
);
827 if (i
>= IEEE80211_NUM_ACS
) {
828 dev
->q_tx
[i
].q
= dev
->q_tx
[0].q
;
832 q
= devm_kzalloc(dev
->dev
, sizeof(*q
), GFP_KERNEL
);
836 spin_lock_init(&q
->lock
);
837 q
->hw_idx
= mt76_ac_to_hwq(i
);
840 q
->entry
= devm_kcalloc(dev
->dev
,
841 MT_NUM_TX_ENTRIES
, sizeof(*q
->entry
),
846 q
->ndesc
= MT_NUM_TX_ENTRIES
;
847 for (j
= 0; j
< q
->ndesc
; j
++) {
848 err
= mt76u_urb_alloc(dev
, &q
->entry
[j
],
857 static void mt76u_free_tx(struct mt76_dev
*dev
)
859 struct mt76_queue
*q
;
862 for (i
= 0; i
< IEEE80211_NUM_ACS
; i
++) {
864 for (j
= 0; j
< q
->ndesc
; j
++)
865 usb_free_urb(q
->entry
[j
].urb
);
869 void mt76u_stop_tx(struct mt76_dev
*dev
)
871 struct mt76_queue_entry entry
;
872 struct mt76_queue
*q
;
875 ret
= wait_event_timeout(dev
->tx_wait
, !mt76_has_tx_pending(dev
),
878 dev_err(dev
->dev
, "timed out waiting for pending tx\n");
880 for (i
= 0; i
< IEEE80211_NUM_ACS
; i
++) {
882 for (j
= 0; j
< q
->ndesc
; j
++)
883 usb_kill_urb(q
->entry
[j
].urb
);
886 tasklet_kill(&dev
->tx_tasklet
);
888 /* On device removal we maight queue skb's, but mt76u_tx_kick()
889 * will fail to submit urb, cleanup those skb's manually.
891 for (i
= 0; i
< IEEE80211_NUM_ACS
; i
++) {
894 /* Assure we are in sync with killed tasklet. */
895 spin_lock_bh(&q
->lock
);
897 entry
= q
->entry
[q
->head
];
898 q
->head
= (q
->head
+ 1) % q
->ndesc
;
901 dev
->drv
->tx_complete_skb(dev
, i
, &entry
);
903 spin_unlock_bh(&q
->lock
);
907 cancel_work_sync(&dev
->usb
.stat_work
);
908 clear_bit(MT76_READING_STATS
, &dev
->state
);
910 mt76_tx_status_check(dev
, NULL
, true);
912 EXPORT_SYMBOL_GPL(mt76u_stop_tx
);
914 void mt76u_queues_deinit(struct mt76_dev
*dev
)
922 EXPORT_SYMBOL_GPL(mt76u_queues_deinit
);
924 int mt76u_alloc_queues(struct mt76_dev
*dev
)
928 err
= mt76u_alloc_rx(dev
);
932 return mt76u_alloc_tx(dev
);
934 EXPORT_SYMBOL_GPL(mt76u_alloc_queues
);
936 static const struct mt76_queue_ops usb_queue_ops
= {
937 .tx_queue_skb
= mt76u_tx_queue_skb
,
938 .kick
= mt76u_tx_kick
,
941 int mt76u_init(struct mt76_dev
*dev
,
942 struct usb_interface
*intf
)
944 static const struct mt76_bus_ops mt76u_ops
= {
948 .write_copy
= mt76u_copy
,
949 .wr_rp
= mt76u_wr_rp
,
950 .rd_rp
= mt76u_rd_rp
,
951 .type
= MT76_BUS_USB
,
953 struct usb_device
*udev
= interface_to_usbdev(intf
);
954 struct mt76_usb
*usb
= &dev
->usb
;
956 tasklet_init(&usb
->rx_tasklet
, mt76u_rx_tasklet
, (unsigned long)dev
);
957 tasklet_init(&dev
->tx_tasklet
, mt76u_tx_tasklet
, (unsigned long)dev
);
958 INIT_WORK(&usb
->stat_work
, mt76u_tx_status_data
);
959 skb_queue_head_init(&dev
->rx_skb
[MT_RXQ_MAIN
]);
961 usb
->stat_wq
= alloc_workqueue("mt76u", WQ_UNBOUND
, 0);
965 mutex_init(&usb
->mcu
.mutex
);
967 mutex_init(&usb
->usb_ctrl_mtx
);
968 dev
->bus
= &mt76u_ops
;
969 dev
->queue_ops
= &usb_queue_ops
;
971 dev_set_drvdata(&udev
->dev
, dev
);
973 usb
->sg_en
= mt76u_check_sg(dev
);
975 return mt76u_set_endpoints(intf
, usb
);
977 EXPORT_SYMBOL_GPL(mt76u_init
);
979 void mt76u_deinit(struct mt76_dev
*dev
)
981 if (dev
->usb
.stat_wq
) {
982 destroy_workqueue(dev
->usb
.stat_wq
);
983 dev
->usb
.stat_wq
= NULL
;
986 EXPORT_SYMBOL_GPL(mt76u_deinit
);
988 MODULE_AUTHOR("Lorenzo Bianconi <lorenzo.bianconi83@gmail.com>");
989 MODULE_LICENSE("Dual BSD/GPL");