rt2x00usb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the
  15. Free Software Foundation, Inc.,
  16. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. /*
  19. Module: rt2x00usb
  20. Abstract: rt2x00 generic usb device routines.
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/usb.h>
  26. #include <linux/bug.h>
  27. #include "rt2x00.h"
  28. #include "rt2x00usb.h"
  29. /*
  30. * Interfacing with the HW.
  31. */
  32. int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
  33. const u8 request, const u8 requesttype,
  34. const u16 offset, const u16 value,
  35. void *buffer, const u16 buffer_length,
  36. const int timeout)
  37. {
  38. struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
  39. int status;
  40. unsigned int i;
  41. unsigned int pipe =
  42. (requesttype == USB_VENDOR_REQUEST_IN) ?
  43. usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
  44. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  45. return -ENODEV;
  46. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  47. status = usb_control_msg(usb_dev, pipe, request, requesttype,
  48. value, offset, buffer, buffer_length,
  49. timeout);
  50. if (status >= 0)
  51. return 0;
  52. /*
  53. * Check for errors
  54. * -ENODEV: Device has disappeared, no point continuing.
  55. * All other errors: Try again.
  56. */
  57. else if (status == -ENODEV) {
  58. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  59. break;
  60. }
  61. }
  62. ERROR(rt2x00dev,
  63. "Vendor Request 0x%02x failed for offset 0x%04x with error %d.\n",
  64. request, offset, status);
  65. return status;
  66. }
  67. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
  68. int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
  69. const u8 request, const u8 requesttype,
  70. const u16 offset, void *buffer,
  71. const u16 buffer_length, const int timeout)
  72. {
  73. int status;
  74. BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex));
  75. /*
  76. * Check for Cache availability.
  77. */
  78. if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
  79. ERROR(rt2x00dev, "CSR cache not available.\n");
  80. return -ENOMEM;
  81. }
  82. if (requesttype == USB_VENDOR_REQUEST_OUT)
  83. memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
  84. status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
  85. offset, 0, rt2x00dev->csr.cache,
  86. buffer_length, timeout);
  87. if (!status && requesttype == USB_VENDOR_REQUEST_IN)
  88. memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
  89. return status;
  90. }
  91. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
  92. int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
  93. const u8 request, const u8 requesttype,
  94. const u16 offset, void *buffer,
  95. const u16 buffer_length, const int timeout)
  96. {
  97. int status = 0;
  98. unsigned char *tb;
  99. u16 off, len, bsize;
  100. mutex_lock(&rt2x00dev->csr_mutex);
  101. tb = (char *)buffer;
  102. off = offset;
  103. len = buffer_length;
  104. while (len && !status) {
  105. bsize = min_t(u16, CSR_CACHE_SIZE, len);
  106. status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
  107. requesttype, off, tb,
  108. bsize, timeout);
  109. tb += bsize;
  110. len -= bsize;
  111. off += bsize;
  112. }
  113. mutex_unlock(&rt2x00dev->csr_mutex);
  114. return status;
  115. }
  116. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
  117. int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
  118. const unsigned int offset,
  119. const struct rt2x00_field32 field,
  120. u32 *reg)
  121. {
  122. unsigned int i;
  123. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  124. return -ENODEV;
  125. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  126. rt2x00usb_register_read_lock(rt2x00dev, offset, reg);
  127. if (!rt2x00_get_field32(*reg, field))
  128. return 1;
  129. udelay(REGISTER_BUSY_DELAY);
  130. }
  131. ERROR(rt2x00dev, "Indirect register access failed: "
  132. "offset=0x%.08x, value=0x%.08x\n", offset, *reg);
  133. *reg = ~0;
  134. return 0;
  135. }
  136. EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
  137. /*
  138. * TX data handlers.
  139. */
  140. static void rt2x00usb_work_txdone_entry(struct queue_entry *entry)
  141. {
  142. /*
  143. * If the transfer to hardware succeeded, it does not mean the
  144. * frame was send out correctly. It only means the frame
  145. * was succesfully pushed to the hardware, we have no
  146. * way to determine the transmission status right now.
  147. * (Only indirectly by looking at the failed TX counters
  148. * in the register).
  149. */
  150. if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
  151. rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
  152. else
  153. rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
  154. }
  155. static void rt2x00usb_work_txdone(struct work_struct *work)
  156. {
  157. struct rt2x00_dev *rt2x00dev =
  158. container_of(work, struct rt2x00_dev, txdone_work);
  159. struct data_queue *queue;
  160. struct queue_entry *entry;
  161. tx_queue_for_each(rt2x00dev, queue) {
  162. while (!rt2x00queue_empty(queue)) {
  163. entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  164. if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  165. break;
  166. rt2x00usb_work_txdone_entry(entry);
  167. }
  168. }
  169. }
  170. static void rt2x00usb_interrupt_txdone(struct urb *urb)
  171. {
  172. struct queue_entry *entry = (struct queue_entry *)urb->context;
  173. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  174. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
  175. !__test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  176. return;
  177. /*
  178. * Check if the frame was correctly uploaded
  179. */
  180. if (urb->status)
  181. __set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
  182. /*
  183. * Schedule the delayed work for reading the TX status
  184. * from the device.
  185. */
  186. ieee80211_queue_work(rt2x00dev->hw, &rt2x00dev->txdone_work);
  187. }
  188. static inline void rt2x00usb_kick_tx_entry(struct queue_entry *entry)
  189. {
  190. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  191. struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
  192. struct queue_entry_priv_usb *entry_priv = entry->priv_data;
  193. u32 length;
  194. if (test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags)) {
  195. /*
  196. * USB devices cannot blindly pass the skb->len as the
  197. * length of the data to usb_fill_bulk_urb. Pass the skb
  198. * to the driver to determine what the length should be.
  199. */
  200. length = rt2x00dev->ops->lib->get_tx_data_len(entry);
  201. usb_fill_bulk_urb(entry_priv->urb, usb_dev,
  202. usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
  203. entry->skb->data, length,
  204. rt2x00usb_interrupt_txdone, entry);
  205. usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
  206. }
  207. }
  208. void rt2x00usb_kick_tx_queue(struct data_queue *queue)
  209. {
  210. unsigned long irqflags;
  211. unsigned int index;
  212. unsigned int index_done;
  213. unsigned int i;
  214. /*
  215. * Only protect the range we are going to loop over,
  216. * if during our loop a extra entry is set to pending
  217. * it should not be kicked during this run, since it
  218. * is part of another TX operation.
  219. */
  220. spin_lock_irqsave(&queue->lock, irqflags);
  221. index = queue->index[Q_INDEX];
  222. index_done = queue->index[Q_INDEX_DONE];
  223. spin_unlock_irqrestore(&queue->lock, irqflags);
  224. /*
  225. * Start from the TX done pointer, this guarentees that we will
  226. * send out all frames in the correct order.
  227. */
  228. if (index_done < index) {
  229. for (i = index_done; i < index; i++)
  230. rt2x00usb_kick_tx_entry(&queue->entries[i]);
  231. } else {
  232. for (i = index_done; i < queue->limit; i++)
  233. rt2x00usb_kick_tx_entry(&queue->entries[i]);
  234. for (i = 0; i < index; i++)
  235. rt2x00usb_kick_tx_entry(&queue->entries[i]);
  236. }
  237. }
  238. EXPORT_SYMBOL_GPL(rt2x00usb_kick_tx_queue);
  239. void rt2x00usb_kill_tx_queue(struct data_queue *queue)
  240. {
  241. struct queue_entry_priv_usb *entry_priv;
  242. struct queue_entry_priv_usb_bcn *bcn_priv;
  243. unsigned int i;
  244. bool kill_guard;
  245. /*
  246. * When killing the beacon queue, we must also kill
  247. * the beacon guard byte.
  248. */
  249. kill_guard =
  250. (queue->qid == QID_BEACON) &&
  251. (test_bit(DRIVER_REQUIRE_BEACON_GUARD, &queue->rt2x00dev->flags));
  252. /*
  253. * Cancel all entries.
  254. */
  255. for (i = 0; i < queue->limit; i++) {
  256. entry_priv = queue->entries[i].priv_data;
  257. usb_kill_urb(entry_priv->urb);
  258. /*
  259. * Kill guardian urb (if required by driver).
  260. */
  261. if (kill_guard) {
  262. bcn_priv = queue->entries[i].priv_data;
  263. usb_kill_urb(bcn_priv->guardian_urb);
  264. }
  265. }
  266. }
  267. EXPORT_SYMBOL_GPL(rt2x00usb_kill_tx_queue);
  268. static void rt2x00usb_watchdog_reset_tx(struct data_queue *queue)
  269. {
  270. struct queue_entry *entry;
  271. struct queue_entry_priv_usb *entry_priv;
  272. unsigned short threshold = queue->threshold;
  273. WARNING(queue->rt2x00dev, "TX queue %d timed out, invoke reset", queue->qid);
  274. /*
  275. * Temporarily disable the TX queue, this will force mac80211
  276. * to use the other queues until this queue has been restored.
  277. *
  278. * Set the queue threshold to the queue limit. This prevents the
  279. * queue from being enabled during the txdone handler.
  280. */
  281. queue->threshold = queue->limit;
  282. ieee80211_stop_queue(queue->rt2x00dev->hw, queue->qid);
  283. /*
  284. * Reset all currently uploaded TX frames.
  285. */
  286. while (!rt2x00queue_empty(queue)) {
  287. entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  288. entry_priv = entry->priv_data;
  289. usb_kill_urb(entry_priv->urb);
  290. /*
  291. * We need a short delay here to wait for
  292. * the URB to be canceled
  293. */
  294. do {
  295. udelay(100);
  296. } while (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags));
  297. /*
  298. * Invoke the TX done handler
  299. */
  300. rt2x00usb_work_txdone_entry(entry);
  301. }
  302. /*
  303. * The queue has been reset, and mac80211 is allowed to use the
  304. * queue again.
  305. */
  306. queue->threshold = threshold;
  307. ieee80211_wake_queue(queue->rt2x00dev->hw, queue->qid);
  308. }
  309. void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev)
  310. {
  311. struct data_queue *queue;
  312. tx_queue_for_each(rt2x00dev, queue) {
  313. if (rt2x00queue_timeout(queue))
  314. rt2x00usb_watchdog_reset_tx(queue);
  315. }
  316. }
  317. EXPORT_SYMBOL_GPL(rt2x00usb_watchdog);
  318. /*
  319. * RX data handlers.
  320. */
  321. static void rt2x00usb_work_rxdone(struct work_struct *work)
  322. {
  323. struct rt2x00_dev *rt2x00dev =
  324. container_of(work, struct rt2x00_dev, rxdone_work);
  325. struct queue_entry *entry;
  326. struct skb_frame_desc *skbdesc;
  327. u8 rxd[32];
  328. while (!rt2x00queue_empty(rt2x00dev->rx)) {
  329. entry = rt2x00queue_get_entry(rt2x00dev->rx, Q_INDEX_DONE);
  330. if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  331. break;
  332. /*
  333. * Fill in desc fields of the skb descriptor
  334. */
  335. skbdesc = get_skb_frame_desc(entry->skb);
  336. skbdesc->desc = rxd;
  337. skbdesc->desc_len = entry->queue->desc_size;
  338. /*
  339. * Send the frame to rt2x00lib for further processing.
  340. */
  341. rt2x00lib_rxdone(rt2x00dev, entry);
  342. }
  343. }
  344. static void rt2x00usb_interrupt_rxdone(struct urb *urb)
  345. {
  346. struct queue_entry *entry = (struct queue_entry *)urb->context;
  347. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  348. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
  349. !__test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  350. return;
  351. /*
  352. * Check if the received data is simply too small
  353. * to be actually valid, or if the urb is signaling
  354. * a problem.
  355. */
  356. if (urb->actual_length < entry->queue->desc_size || urb->status)
  357. __set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
  358. /*
  359. * Schedule the delayed work for reading the RX status
  360. * from the device.
  361. */
  362. ieee80211_queue_work(rt2x00dev->hw, &rt2x00dev->rxdone_work);
  363. }
  364. /*
  365. * Radio handlers
  366. */
  367. void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
  368. {
  369. rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
  370. REGISTER_TIMEOUT);
  371. /*
  372. * The USB version of kill_tx_queue also works
  373. * on the RX queue.
  374. */
  375. rt2x00dev->ops->lib->kill_tx_queue(rt2x00dev->rx);
  376. }
  377. EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
  378. /*
  379. * Device initialization handlers.
  380. */
  381. void rt2x00usb_clear_entry(struct queue_entry *entry)
  382. {
  383. struct usb_device *usb_dev =
  384. to_usb_device_intf(entry->queue->rt2x00dev->dev);
  385. struct queue_entry_priv_usb *entry_priv = entry->priv_data;
  386. int pipe;
  387. entry->flags = 0;
  388. if (entry->queue->qid == QID_RX) {
  389. pipe = usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint);
  390. usb_fill_bulk_urb(entry_priv->urb, usb_dev, pipe,
  391. entry->skb->data, entry->skb->len,
  392. rt2x00usb_interrupt_rxdone, entry);
  393. set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  394. usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
  395. }
  396. }
  397. EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
  398. static void rt2x00usb_assign_endpoint(struct data_queue *queue,
  399. struct usb_endpoint_descriptor *ep_desc)
  400. {
  401. struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
  402. int pipe;
  403. queue->usb_endpoint = usb_endpoint_num(ep_desc);
  404. if (queue->qid == QID_RX) {
  405. pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
  406. queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 0);
  407. } else {
  408. pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
  409. queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 1);
  410. }
  411. if (!queue->usb_maxpacket)
  412. queue->usb_maxpacket = 1;
  413. }
  414. static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
  415. {
  416. struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
  417. struct usb_host_interface *intf_desc = intf->cur_altsetting;
  418. struct usb_endpoint_descriptor *ep_desc;
  419. struct data_queue *queue = rt2x00dev->tx;
  420. struct usb_endpoint_descriptor *tx_ep_desc = NULL;
  421. unsigned int i;
  422. /*
  423. * Walk through all available endpoints to search for "bulk in"
  424. * and "bulk out" endpoints. When we find such endpoints collect
  425. * the information we need from the descriptor and assign it
  426. * to the queue.
  427. */
  428. for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
  429. ep_desc = &intf_desc->endpoint[i].desc;
  430. if (usb_endpoint_is_bulk_in(ep_desc)) {
  431. rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
  432. } else if (usb_endpoint_is_bulk_out(ep_desc) &&
  433. (queue != queue_end(rt2x00dev))) {
  434. rt2x00usb_assign_endpoint(queue, ep_desc);
  435. queue = queue_next(queue);
  436. tx_ep_desc = ep_desc;
  437. }
  438. }
  439. /*
  440. * At least 1 endpoint for RX and 1 endpoint for TX must be available.
  441. */
  442. if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
  443. ERROR(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
  444. return -EPIPE;
  445. }
  446. /*
  447. * It might be possible not all queues have a dedicated endpoint.
  448. * Loop through all TX queues and copy the endpoint information
  449. * which we have gathered from already assigned endpoints.
  450. */
  451. txall_queue_for_each(rt2x00dev, queue) {
  452. if (!queue->usb_endpoint)
  453. rt2x00usb_assign_endpoint(queue, tx_ep_desc);
  454. }
  455. return 0;
  456. }
  457. static int rt2x00usb_alloc_urb(struct rt2x00_dev *rt2x00dev,
  458. struct data_queue *queue)
  459. {
  460. struct queue_entry_priv_usb *entry_priv;
  461. struct queue_entry_priv_usb_bcn *bcn_priv;
  462. unsigned int i;
  463. for (i = 0; i < queue->limit; i++) {
  464. entry_priv = queue->entries[i].priv_data;
  465. entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
  466. if (!entry_priv->urb)
  467. return -ENOMEM;
  468. }
  469. /*
  470. * If this is not the beacon queue or
  471. * no guardian byte was required for the beacon,
  472. * then we are done.
  473. */
  474. if (rt2x00dev->bcn != queue ||
  475. !test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
  476. return 0;
  477. for (i = 0; i < queue->limit; i++) {
  478. bcn_priv = queue->entries[i].priv_data;
  479. bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
  480. if (!bcn_priv->guardian_urb)
  481. return -ENOMEM;
  482. }
  483. return 0;
  484. }
  485. static void rt2x00usb_free_urb(struct rt2x00_dev *rt2x00dev,
  486. struct data_queue *queue)
  487. {
  488. struct queue_entry_priv_usb *entry_priv;
  489. struct queue_entry_priv_usb_bcn *bcn_priv;
  490. unsigned int i;
  491. if (!queue->entries)
  492. return;
  493. for (i = 0; i < queue->limit; i++) {
  494. entry_priv = queue->entries[i].priv_data;
  495. usb_kill_urb(entry_priv->urb);
  496. usb_free_urb(entry_priv->urb);
  497. }
  498. /*
  499. * If this is not the beacon queue or
  500. * no guardian byte was required for the beacon,
  501. * then we are done.
  502. */
  503. if (rt2x00dev->bcn != queue ||
  504. !test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
  505. return;
  506. for (i = 0; i < queue->limit; i++) {
  507. bcn_priv = queue->entries[i].priv_data;
  508. usb_kill_urb(bcn_priv->guardian_urb);
  509. usb_free_urb(bcn_priv->guardian_urb);
  510. }
  511. }
  512. int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
  513. {
  514. struct data_queue *queue;
  515. int status;
  516. /*
  517. * Find endpoints for each queue
  518. */
  519. status = rt2x00usb_find_endpoints(rt2x00dev);
  520. if (status)
  521. goto exit;
  522. /*
  523. * Allocate DMA
  524. */
  525. queue_for_each(rt2x00dev, queue) {
  526. status = rt2x00usb_alloc_urb(rt2x00dev, queue);
  527. if (status)
  528. goto exit;
  529. }
  530. return 0;
  531. exit:
  532. rt2x00usb_uninitialize(rt2x00dev);
  533. return status;
  534. }
  535. EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
  536. void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
  537. {
  538. struct data_queue *queue;
  539. queue_for_each(rt2x00dev, queue)
  540. rt2x00usb_free_urb(rt2x00dev, queue);
  541. }
  542. EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
  543. /*
  544. * USB driver handlers.
  545. */
  546. static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
  547. {
  548. kfree(rt2x00dev->rf);
  549. rt2x00dev->rf = NULL;
  550. kfree(rt2x00dev->eeprom);
  551. rt2x00dev->eeprom = NULL;
  552. kfree(rt2x00dev->csr.cache);
  553. rt2x00dev->csr.cache = NULL;
  554. }
  555. static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
  556. {
  557. rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
  558. if (!rt2x00dev->csr.cache)
  559. goto exit;
  560. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  561. if (!rt2x00dev->eeprom)
  562. goto exit;
  563. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  564. if (!rt2x00dev->rf)
  565. goto exit;
  566. return 0;
  567. exit:
  568. ERROR_PROBE("Failed to allocate registers.\n");
  569. rt2x00usb_free_reg(rt2x00dev);
  570. return -ENOMEM;
  571. }
  572. int rt2x00usb_probe(struct usb_interface *usb_intf,
  573. const struct usb_device_id *id)
  574. {
  575. struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
  576. struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_info;
  577. struct ieee80211_hw *hw;
  578. struct rt2x00_dev *rt2x00dev;
  579. int retval;
  580. usb_dev = usb_get_dev(usb_dev);
  581. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  582. if (!hw) {
  583. ERROR_PROBE("Failed to allocate hardware.\n");
  584. retval = -ENOMEM;
  585. goto exit_put_device;
  586. }
  587. usb_set_intfdata(usb_intf, hw);
  588. rt2x00dev = hw->priv;
  589. rt2x00dev->dev = &usb_intf->dev;
  590. rt2x00dev->ops = ops;
  591. rt2x00dev->hw = hw;
  592. rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB);
  593. INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone);
  594. INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone);
  595. retval = rt2x00usb_alloc_reg(rt2x00dev);
  596. if (retval)
  597. goto exit_free_device;
  598. retval = rt2x00lib_probe_dev(rt2x00dev);
  599. if (retval)
  600. goto exit_free_reg;
  601. return 0;
  602. exit_free_reg:
  603. rt2x00usb_free_reg(rt2x00dev);
  604. exit_free_device:
  605. ieee80211_free_hw(hw);
  606. exit_put_device:
  607. usb_put_dev(usb_dev);
  608. usb_set_intfdata(usb_intf, NULL);
  609. return retval;
  610. }
  611. EXPORT_SYMBOL_GPL(rt2x00usb_probe);
  612. void rt2x00usb_disconnect(struct usb_interface *usb_intf)
  613. {
  614. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  615. struct rt2x00_dev *rt2x00dev = hw->priv;
  616. /*
  617. * Free all allocated data.
  618. */
  619. rt2x00lib_remove_dev(rt2x00dev);
  620. rt2x00usb_free_reg(rt2x00dev);
  621. ieee80211_free_hw(hw);
  622. /*
  623. * Free the USB device data.
  624. */
  625. usb_set_intfdata(usb_intf, NULL);
  626. usb_put_dev(interface_to_usbdev(usb_intf));
  627. }
  628. EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
  629. #ifdef CONFIG_PM
  630. int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
  631. {
  632. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  633. struct rt2x00_dev *rt2x00dev = hw->priv;
  634. int retval;
  635. retval = rt2x00lib_suspend(rt2x00dev, state);
  636. if (retval)
  637. return retval;
  638. /*
  639. * Decrease usbdev refcount.
  640. */
  641. usb_put_dev(interface_to_usbdev(usb_intf));
  642. return 0;
  643. }
  644. EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
  645. int rt2x00usb_resume(struct usb_interface *usb_intf)
  646. {
  647. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  648. struct rt2x00_dev *rt2x00dev = hw->priv;
  649. usb_get_dev(interface_to_usbdev(usb_intf));
  650. return rt2x00lib_resume(rt2x00dev);
  651. }
  652. EXPORT_SYMBOL_GPL(rt2x00usb_resume);
  653. #endif /* CONFIG_PM */
  654. /*
  655. * rt2x00usb module information.
  656. */
  657. MODULE_AUTHOR(DRV_PROJECT);
  658. MODULE_VERSION(DRV_VERSION);
  659. MODULE_DESCRIPTION("rt2x00 usb library");
  660. MODULE_LICENSE("GPL");