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