Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3	Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
  4	Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
  5	<http://rt2x00.serialmonkey.com>
  6
  7 */
  8
  9/*
 10	Module: rt2x00usb
 11	Abstract: rt2x00 generic usb device routines.
 12 */
 13
 14#include <linux/kernel.h>
 15#include <linux/module.h>
 16#include <linux/slab.h>
 17#include <linux/usb.h>
 18#include <linux/bug.h>
 19
 20#include "rt2x00.h"
 21#include "rt2x00usb.h"
 22
 23static bool rt2x00usb_check_usb_error(struct rt2x00_dev *rt2x00dev, int status)
 24{
 25	if (status == -ENODEV || status == -ENOENT)
 26		return true;
 27
 28	if (status == -EPROTO || status == -ETIMEDOUT)
 29		rt2x00dev->num_proto_errs++;
 30	else
 31		rt2x00dev->num_proto_errs = 0;
 32
 33	if (rt2x00dev->num_proto_errs > 3)
 34		return true;
 35
 36	return false;
 37}
 38
 39/*
 40 * Interfacing with the HW.
 41 */
 42int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
 43			     const u8 request, const u8 requesttype,
 44			     const u16 offset, const u16 value,
 45			     void *buffer, const u16 buffer_length,
 46			     const int timeout)
 47{
 48	struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
 49	int status;
 50	unsigned int pipe =
 51	    (requesttype == USB_VENDOR_REQUEST_IN) ?
 52	    usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
 53	unsigned long expire = jiffies + msecs_to_jiffies(timeout);
 54
 55	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
 56		return -ENODEV;
 57
 58	do {
 59		status = usb_control_msg(usb_dev, pipe, request, requesttype,
 60					 value, offset, buffer, buffer_length,
 61					 timeout / 2);
 62		if (status >= 0)
 63			return 0;
 64
 65		if (rt2x00usb_check_usb_error(rt2x00dev, status)) {
 66			/* Device has disappeared. */
 67			clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
 68			break;
 69		}
 70	} while (time_before(jiffies, expire));
 71
 72	rt2x00_err(rt2x00dev,
 73		   "Vendor Request 0x%02x failed for offset 0x%04x with error %d\n",
 74		   request, offset, status);
 75
 76	return status;
 77}
 78EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
 79
 80int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
 81				   const u8 request, const u8 requesttype,
 82				   const u16 offset, void *buffer,
 83				   const u16 buffer_length, const int timeout)
 84{
 85	int status;
 86
 87	BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex));
 88
 89	/*
 90	 * Check for Cache availability.
 91	 */
 92	if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
 93		rt2x00_err(rt2x00dev, "CSR cache not available\n");
 94		return -ENOMEM;
 95	}
 96
 97	if (requesttype == USB_VENDOR_REQUEST_OUT)
 98		memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
 99
100	status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
101					  offset, 0, rt2x00dev->csr.cache,
102					  buffer_length, timeout);
103
104	if (!status && requesttype == USB_VENDOR_REQUEST_IN)
105		memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
106
107	return status;
108}
109EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
110
111int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
112				  const u8 request, const u8 requesttype,
113				  const u16 offset, void *buffer,
114				  const u16 buffer_length)
115{
116	int status = 0;
117	unsigned char *tb;
118	u16 off, len, bsize;
119
120	mutex_lock(&rt2x00dev->csr_mutex);
121
122	tb  = (char *)buffer;
123	off = offset;
124	len = buffer_length;
125	while (len && !status) {
126		bsize = min_t(u16, CSR_CACHE_SIZE, len);
127		status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
128							requesttype, off, tb,
129							bsize, REGISTER_TIMEOUT);
130
131		tb  += bsize;
132		len -= bsize;
133		off += bsize;
134	}
135
136	mutex_unlock(&rt2x00dev->csr_mutex);
137
138	return status;
139}
140EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
141
142int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
143			   const unsigned int offset,
144			   const struct rt2x00_field32 field,
145			   u32 *reg)
146{
147	unsigned int i;
148
149	if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
150		return -ENODEV;
151
152	for (i = 0; i < REGISTER_USB_BUSY_COUNT; i++) {
153		*reg = rt2x00usb_register_read_lock(rt2x00dev, offset);
154		if (!rt2x00_get_field32(*reg, field))
155			return 1;
156		udelay(REGISTER_BUSY_DELAY);
157	}
158
159	rt2x00_err(rt2x00dev, "Indirect register access failed: offset=0x%.08x, value=0x%.08x\n",
160		   offset, *reg);
161	*reg = ~0;
162
163	return 0;
164}
165EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
166
167
168struct rt2x00_async_read_data {
169	__le32 reg;
170	struct usb_ctrlrequest cr;
171	struct rt2x00_dev *rt2x00dev;
172	bool (*callback)(struct rt2x00_dev *, int, u32);
173};
174
175static void rt2x00usb_register_read_async_cb(struct urb *urb)
176{
177	struct rt2x00_async_read_data *rd = urb->context;
178	if (rd->callback(rd->rt2x00dev, urb->status, le32_to_cpu(rd->reg))) {
179		usb_anchor_urb(urb, rd->rt2x00dev->anchor);
180		if (usb_submit_urb(urb, GFP_ATOMIC) < 0) {
181			usb_unanchor_urb(urb);
182			kfree(rd);
183		}
184	} else
185		kfree(rd);
186}
187
188void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
189				   const unsigned int offset,
190				   bool (*callback)(struct rt2x00_dev*, int, u32))
191{
192	struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
193	struct urb *urb;
194	struct rt2x00_async_read_data *rd;
195
196	rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
197	if (!rd)
198		return;
199
200	urb = usb_alloc_urb(0, GFP_ATOMIC);
201	if (!urb) {
202		kfree(rd);
203		return;
204	}
205
206	rd->rt2x00dev = rt2x00dev;
207	rd->callback = callback;
208	rd->cr.bRequestType = USB_VENDOR_REQUEST_IN;
209	rd->cr.bRequest = USB_MULTI_READ;
210	rd->cr.wValue = 0;
211	rd->cr.wIndex = cpu_to_le16(offset);
212	rd->cr.wLength = cpu_to_le16(sizeof(u32));
213
214	usb_fill_control_urb(urb, usb_dev, usb_rcvctrlpipe(usb_dev, 0),
215			     (unsigned char *)(&rd->cr), &rd->reg, sizeof(rd->reg),
216			     rt2x00usb_register_read_async_cb, rd);
217	usb_anchor_urb(urb, rt2x00dev->anchor);
218	if (usb_submit_urb(urb, GFP_ATOMIC) < 0) {
219		usb_unanchor_urb(urb);
220		kfree(rd);
221	}
222	usb_free_urb(urb);
223}
224EXPORT_SYMBOL_GPL(rt2x00usb_register_read_async);
225
226/*
227 * TX data handlers.
228 */
229static void rt2x00usb_work_txdone_entry(struct queue_entry *entry)
230{
231	/*
232	 * If the transfer to hardware succeeded, it does not mean the
233	 * frame was send out correctly. It only means the frame
234	 * was successfully pushed to the hardware, we have no
235	 * way to determine the transmission status right now.
236	 * (Only indirectly by looking at the failed TX counters
237	 * in the register).
238	 */
239	if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
240		rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
241	else
242		rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
243}
244
245static void rt2x00usb_work_txdone(struct work_struct *work)
246{
247	struct rt2x00_dev *rt2x00dev =
248	    container_of(work, struct rt2x00_dev, txdone_work);
249	struct data_queue *queue;
250	struct queue_entry *entry;
251
252	tx_queue_for_each(rt2x00dev, queue) {
253		while (!rt2x00queue_empty(queue)) {
254			entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
255
256			if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
257			    !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
258				break;
259
260			rt2x00usb_work_txdone_entry(entry);
261		}
262	}
263}
264
265static void rt2x00usb_interrupt_txdone(struct urb *urb)
266{
267	struct queue_entry *entry = (struct queue_entry *)urb->context;
268	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
269
270	if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
271		return;
272	/*
273	 * Check if the frame was correctly uploaded
274	 */
275	if (urb->status)
276		set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
277	/*
278	 * Report the frame as DMA done
279	 */
280	rt2x00lib_dmadone(entry);
281
282	if (rt2x00dev->ops->lib->tx_dma_done)
283		rt2x00dev->ops->lib->tx_dma_done(entry);
284	/*
285	 * Schedule the delayed work for reading the TX status
286	 * from the device.
287	 */
288	if (!rt2x00_has_cap_flag(rt2x00dev, REQUIRE_TXSTATUS_FIFO) ||
289	    !kfifo_is_empty(&rt2x00dev->txstatus_fifo))
290		queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);
291}
292
293static bool rt2x00usb_kick_tx_entry(struct queue_entry *entry, void *data)
294{
295	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
296	struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
297	struct queue_entry_priv_usb *entry_priv = entry->priv_data;
298	u32 length;
299	int status;
300
301	if (!test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags) ||
302	    test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
303		return false;
304
305	/*
306	 * USB devices require certain padding at the end of each frame
307	 * and urb. Those paddings are not included in skbs. Pass entry
308	 * to the driver to determine what the overall length should be.
309	 */
310	length = rt2x00dev->ops->lib->get_tx_data_len(entry);
311
312	status = skb_padto(entry->skb, length);
313	if (unlikely(status)) {
314		/* TODO: report something more appropriate than IO_FAILED. */
315		rt2x00_warn(rt2x00dev, "TX SKB padding error, out of memory\n");
316		set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
317		rt2x00lib_dmadone(entry);
318
319		return false;
320	}
321
322	usb_fill_bulk_urb(entry_priv->urb, usb_dev,
323			  usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
324			  entry->skb->data, length,
325			  rt2x00usb_interrupt_txdone, entry);
326
327	status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
328	if (status) {
329		if (rt2x00usb_check_usb_error(rt2x00dev, status))
330			clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
331		set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
332		rt2x00lib_dmadone(entry);
333	}
334
335	return false;
336}
337
338/*
339 * RX data handlers.
340 */
341static void rt2x00usb_work_rxdone(struct work_struct *work)
342{
343	struct rt2x00_dev *rt2x00dev =
344	    container_of(work, struct rt2x00_dev, rxdone_work);
345	struct queue_entry *entry;
346	struct skb_frame_desc *skbdesc;
347	u8 rxd[32];
348
349	while (!rt2x00queue_empty(rt2x00dev->rx)) {
350		entry = rt2x00queue_get_entry(rt2x00dev->rx, Q_INDEX_DONE);
351
352		if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
353			break;
354
355		/*
356		 * Fill in desc fields of the skb descriptor
357		 */
358		skbdesc = get_skb_frame_desc(entry->skb);
359		skbdesc->desc = rxd;
360		skbdesc->desc_len = entry->queue->desc_size;
361
362		/*
363		 * Send the frame to rt2x00lib for further processing.
364		 */
365		rt2x00lib_rxdone(entry, GFP_KERNEL);
366	}
367}
368
369static void rt2x00usb_interrupt_rxdone(struct urb *urb)
370{
371	struct queue_entry *entry = (struct queue_entry *)urb->context;
372	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
373
374	if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
375		return;
376
377	/*
378	 * Check if the received data is simply too small
379	 * to be actually valid, or if the urb is signaling
380	 * a problem.
381	 */
382	if (urb->actual_length < entry->queue->desc_size || urb->status)
383		set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
384
385	/*
386	 * Report the frame as DMA done
387	 */
388	rt2x00lib_dmadone(entry);
389
390	/*
391	 * Schedule the delayed work for processing RX data
392	 */
393	queue_work(rt2x00dev->workqueue, &rt2x00dev->rxdone_work);
394}
395
396static bool rt2x00usb_kick_rx_entry(struct queue_entry *entry, void *data)
397{
398	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
399	struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
400	struct queue_entry_priv_usb *entry_priv = entry->priv_data;
401	int status;
402
403	if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
404		return false;
405
406	rt2x00lib_dmastart(entry);
407
408	usb_fill_bulk_urb(entry_priv->urb, usb_dev,
409			  usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint),
410			  entry->skb->data, entry->skb->len,
411			  rt2x00usb_interrupt_rxdone, entry);
412
413	status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
414	if (status) {
415		if (rt2x00usb_check_usb_error(rt2x00dev, status))
416			clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
417		set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
418		rt2x00lib_dmadone(entry);
419	}
420
421	return false;
422}
423
424void rt2x00usb_kick_queue(struct data_queue *queue)
425{
426	switch (queue->qid) {
427	case QID_AC_VO:
428	case QID_AC_VI:
429	case QID_AC_BE:
430	case QID_AC_BK:
431		if (!rt2x00queue_empty(queue))
432			rt2x00queue_for_each_entry(queue,
433						   Q_INDEX_DONE,
434						   Q_INDEX,
435						   NULL,
436						   rt2x00usb_kick_tx_entry);
437		break;
438	case QID_RX:
439		if (!rt2x00queue_full(queue))
440			rt2x00queue_for_each_entry(queue,
441						   Q_INDEX,
442						   Q_INDEX_DONE,
443						   NULL,
444						   rt2x00usb_kick_rx_entry);
445		break;
446	default:
447		break;
448	}
449}
450EXPORT_SYMBOL_GPL(rt2x00usb_kick_queue);
451
452static bool rt2x00usb_flush_entry(struct queue_entry *entry, void *data)
453{
454	struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
455	struct queue_entry_priv_usb *entry_priv = entry->priv_data;
456	struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
457
458	if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
459		return false;
460
461	usb_kill_urb(entry_priv->urb);
462
463	/*
464	 * Kill guardian urb (if required by driver).
465	 */
466	if ((entry->queue->qid == QID_BEACON) &&
467	    (rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD)))
468		usb_kill_urb(bcn_priv->guardian_urb);
469
470	return false;
471}
472
473void rt2x00usb_flush_queue(struct data_queue *queue, bool drop)
474{
475	struct work_struct *completion;
476	unsigned int i;
477
478	if (drop)
479		rt2x00queue_for_each_entry(queue, Q_INDEX_DONE, Q_INDEX, NULL,
480					   rt2x00usb_flush_entry);
481
482	/*
483	 * Obtain the queue completion handler
484	 */
485	switch (queue->qid) {
486	case QID_AC_VO:
487	case QID_AC_VI:
488	case QID_AC_BE:
489	case QID_AC_BK:
490		completion = &queue->rt2x00dev->txdone_work;
491		break;
492	case QID_RX:
493		completion = &queue->rt2x00dev->rxdone_work;
494		break;
495	default:
496		return;
497	}
498
499	for (i = 0; i < 10; i++) {
500		/*
501		 * Check if the driver is already done, otherwise we
502		 * have to sleep a little while to give the driver/hw
503		 * the oppurtunity to complete interrupt process itself.
504		 */
505		if (rt2x00queue_empty(queue))
506			break;
507
508		/*
509		 * Schedule the completion handler manually, when this
510		 * worker function runs, it should cleanup the queue.
511		 */
512		queue_work(queue->rt2x00dev->workqueue, completion);
513
514		/*
515		 * Wait for a little while to give the driver
516		 * the oppurtunity to recover itself.
517		 */
518		msleep(50);
519	}
520}
521EXPORT_SYMBOL_GPL(rt2x00usb_flush_queue);
522
523static void rt2x00usb_watchdog_tx_dma(struct data_queue *queue)
524{
525	rt2x00_warn(queue->rt2x00dev, "TX queue %d DMA timed out, invoke forced forced reset\n",
526		    queue->qid);
527
528	rt2x00queue_stop_queue(queue);
529	rt2x00queue_flush_queue(queue, true);
530	rt2x00queue_start_queue(queue);
531}
532
533static int rt2x00usb_dma_timeout(struct data_queue *queue)
534{
535	struct queue_entry *entry;
536
537	entry = rt2x00queue_get_entry(queue, Q_INDEX_DMA_DONE);
538	return rt2x00queue_dma_timeout(entry);
539}
540
541void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev)
542{
543	struct data_queue *queue;
544
545	tx_queue_for_each(rt2x00dev, queue) {
546		if (!rt2x00queue_empty(queue)) {
547			if (rt2x00usb_dma_timeout(queue))
548				rt2x00usb_watchdog_tx_dma(queue);
549		}
550	}
551}
552EXPORT_SYMBOL_GPL(rt2x00usb_watchdog);
553
554/*
555 * Radio handlers
556 */
557void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
558{
559	rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
560				    REGISTER_TIMEOUT);
561}
562EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
563
564/*
565 * Device initialization handlers.
566 */
567void rt2x00usb_clear_entry(struct queue_entry *entry)
568{
569	entry->flags = 0;
570
571	if (entry->queue->qid == QID_RX)
572		rt2x00usb_kick_rx_entry(entry, NULL);
573}
574EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
575
576static void rt2x00usb_assign_endpoint(struct data_queue *queue,
577				      struct usb_endpoint_descriptor *ep_desc)
578{
579	struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
580	int pipe;
581
582	queue->usb_endpoint = usb_endpoint_num(ep_desc);
583
584	if (queue->qid == QID_RX) {
585		pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
586		queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 0);
587	} else {
588		pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
589		queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 1);
590	}
591
592	if (!queue->usb_maxpacket)
593		queue->usb_maxpacket = 1;
594}
595
596static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
597{
598	struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
599	struct usb_host_interface *intf_desc = intf->cur_altsetting;
600	struct usb_endpoint_descriptor *ep_desc;
601	struct data_queue *queue = rt2x00dev->tx;
602	struct usb_endpoint_descriptor *tx_ep_desc = NULL;
603	unsigned int i;
604
605	/*
606	 * Walk through all available endpoints to search for "bulk in"
607	 * and "bulk out" endpoints. When we find such endpoints collect
608	 * the information we need from the descriptor and assign it
609	 * to the queue.
610	 */
611	for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
612		ep_desc = &intf_desc->endpoint[i].desc;
613
614		if (usb_endpoint_is_bulk_in(ep_desc)) {
615			rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
616		} else if (usb_endpoint_is_bulk_out(ep_desc) &&
617			   (queue != queue_end(rt2x00dev))) {
618			rt2x00usb_assign_endpoint(queue, ep_desc);
619			queue = queue_next(queue);
620
621			tx_ep_desc = ep_desc;
622		}
623	}
624
625	/*
626	 * At least 1 endpoint for RX and 1 endpoint for TX must be available.
627	 */
628	if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
629		rt2x00_err(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
630		return -EPIPE;
631	}
632
633	/*
634	 * It might be possible not all queues have a dedicated endpoint.
635	 * Loop through all TX queues and copy the endpoint information
636	 * which we have gathered from already assigned endpoints.
637	 */
638	txall_queue_for_each(rt2x00dev, queue) {
639		if (!queue->usb_endpoint)
640			rt2x00usb_assign_endpoint(queue, tx_ep_desc);
641	}
642
643	return 0;
644}
645
646static int rt2x00usb_alloc_entries(struct data_queue *queue)
647{
648	struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
649	struct queue_entry_priv_usb *entry_priv;
650	struct queue_entry_priv_usb_bcn *bcn_priv;
651	unsigned int i;
652
653	for (i = 0; i < queue->limit; i++) {
654		entry_priv = queue->entries[i].priv_data;
655		entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
656		if (!entry_priv->urb)
657			return -ENOMEM;
658	}
659
660	/*
661	 * If this is not the beacon queue or
662	 * no guardian byte was required for the beacon,
663	 * then we are done.
664	 */
665	if (queue->qid != QID_BEACON ||
666	    !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
667		return 0;
668
669	for (i = 0; i < queue->limit; i++) {
670		bcn_priv = queue->entries[i].priv_data;
671		bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
672		if (!bcn_priv->guardian_urb)
673			return -ENOMEM;
674	}
675
676	return 0;
677}
678
679static void rt2x00usb_free_entries(struct data_queue *queue)
680{
681	struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
682	struct queue_entry_priv_usb *entry_priv;
683	struct queue_entry_priv_usb_bcn *bcn_priv;
684	unsigned int i;
685
686	if (!queue->entries)
687		return;
688
689	for (i = 0; i < queue->limit; i++) {
690		entry_priv = queue->entries[i].priv_data;
691		usb_kill_urb(entry_priv->urb);
692		usb_free_urb(entry_priv->urb);
693	}
694
695	/*
696	 * If this is not the beacon queue or
697	 * no guardian byte was required for the beacon,
698	 * then we are done.
699	 */
700	if (queue->qid != QID_BEACON ||
701	    !rt2x00_has_cap_flag(rt2x00dev, REQUIRE_BEACON_GUARD))
702		return;
703
704	for (i = 0; i < queue->limit; i++) {
705		bcn_priv = queue->entries[i].priv_data;
706		usb_kill_urb(bcn_priv->guardian_urb);
707		usb_free_urb(bcn_priv->guardian_urb);
708	}
709}
710
711int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
712{
713	struct data_queue *queue;
714	int status;
715
716	/*
717	 * Find endpoints for each queue
718	 */
719	status = rt2x00usb_find_endpoints(rt2x00dev);
720	if (status)
721		goto exit;
722
723	/*
724	 * Allocate DMA
725	 */
726	queue_for_each(rt2x00dev, queue) {
727		status = rt2x00usb_alloc_entries(queue);
728		if (status)
729			goto exit;
730	}
731
732	return 0;
733
734exit:
735	rt2x00usb_uninitialize(rt2x00dev);
736
737	return status;
738}
739EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
740
741void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
742{
743	struct data_queue *queue;
744
745	usb_kill_anchored_urbs(rt2x00dev->anchor);
746	hrtimer_cancel(&rt2x00dev->txstatus_timer);
747	cancel_work_sync(&rt2x00dev->rxdone_work);
748	cancel_work_sync(&rt2x00dev->txdone_work);
749
750	queue_for_each(rt2x00dev, queue)
751		rt2x00usb_free_entries(queue);
752}
753EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
754
755/*
756 * USB driver handlers.
757 */
758static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
759{
760	kfree(rt2x00dev->rf);
761	rt2x00dev->rf = NULL;
762
763	kfree(rt2x00dev->eeprom);
764	rt2x00dev->eeprom = NULL;
765
766	kfree(rt2x00dev->csr.cache);
767	rt2x00dev->csr.cache = NULL;
768}
769
770static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
771{
772	rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
773	if (!rt2x00dev->csr.cache)
774		goto exit;
775
776	rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
777	if (!rt2x00dev->eeprom)
778		goto exit;
779
780	rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
781	if (!rt2x00dev->rf)
782		goto exit;
783
784	return 0;
785
786exit:
787	rt2x00_probe_err("Failed to allocate registers\n");
788
789	rt2x00usb_free_reg(rt2x00dev);
790
791	return -ENOMEM;
792}
793
794int rt2x00usb_probe(struct usb_interface *usb_intf,
795		    const struct rt2x00_ops *ops)
796{
797	struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
798	struct ieee80211_hw *hw;
799	struct rt2x00_dev *rt2x00dev;
800	int retval;
801
802	usb_dev = usb_get_dev(usb_dev);
803	usb_reset_device(usb_dev);
804
805	hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
806	if (!hw) {
807		rt2x00_probe_err("Failed to allocate hardware\n");
808		retval = -ENOMEM;
809		goto exit_put_device;
810	}
811
812	usb_set_intfdata(usb_intf, hw);
813
814	rt2x00dev = hw->priv;
815	rt2x00dev->dev = &usb_intf->dev;
816	rt2x00dev->ops = ops;
817	rt2x00dev->hw = hw;
818
819	rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB);
820
821	INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone);
822	INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone);
823	hrtimer_init(&rt2x00dev->txstatus_timer, CLOCK_MONOTONIC,
824		     HRTIMER_MODE_REL);
825
826	retval = rt2x00usb_alloc_reg(rt2x00dev);
827	if (retval)
828		goto exit_free_device;
829
830	rt2x00dev->anchor = devm_kmalloc(&usb_dev->dev,
831					sizeof(struct usb_anchor),
832					GFP_KERNEL);
833	if (!rt2x00dev->anchor) {
834		retval = -ENOMEM;
835		goto exit_free_reg;
836	}
837	init_usb_anchor(rt2x00dev->anchor);
838
839	retval = rt2x00lib_probe_dev(rt2x00dev);
840	if (retval)
841		goto exit_free_anchor;
842
843	return 0;
844
845exit_free_anchor:
846	usb_kill_anchored_urbs(rt2x00dev->anchor);
847
848exit_free_reg:
849	rt2x00usb_free_reg(rt2x00dev);
850
851exit_free_device:
852	ieee80211_free_hw(hw);
853
854exit_put_device:
855	usb_put_dev(usb_dev);
856
857	usb_set_intfdata(usb_intf, NULL);
858
859	return retval;
860}
861EXPORT_SYMBOL_GPL(rt2x00usb_probe);
862
863void rt2x00usb_disconnect(struct usb_interface *usb_intf)
864{
865	struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
866	struct rt2x00_dev *rt2x00dev = hw->priv;
867
868	/*
869	 * Free all allocated data.
870	 */
871	rt2x00lib_remove_dev(rt2x00dev);
872	rt2x00usb_free_reg(rt2x00dev);
873	ieee80211_free_hw(hw);
874
875	/*
876	 * Free the USB device data.
877	 */
878	usb_set_intfdata(usb_intf, NULL);
879	usb_put_dev(interface_to_usbdev(usb_intf));
880}
881EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
882
883#ifdef CONFIG_PM
884int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
885{
886	struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
887	struct rt2x00_dev *rt2x00dev = hw->priv;
888
889	return rt2x00lib_suspend(rt2x00dev, state);
890}
891EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
892
893int rt2x00usb_resume(struct usb_interface *usb_intf)
894{
895	struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
896	struct rt2x00_dev *rt2x00dev = hw->priv;
897
898	return rt2x00lib_resume(rt2x00dev);
899}
900EXPORT_SYMBOL_GPL(rt2x00usb_resume);
901#endif /* CONFIG_PM */
902
903/*
904 * rt2x00usb module information.
905 */
906MODULE_AUTHOR(DRV_PROJECT);
907MODULE_VERSION(DRV_VERSION);
908MODULE_DESCRIPTION("rt2x00 usb library");
909MODULE_LICENSE("GPL");