rt2x00usb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00usb
  19. Abstract: rt2x00 generic usb device routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/usb.h>
  24. #include <linux/bug.h>
  25. #include "rt2x00.h"
  26. #include "rt2x00usb.h"
  27. /*
  28. * Interfacing with the HW.
  29. */
  30. int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
  31. const u8 request, const u8 requesttype,
  32. const u16 offset, const u16 value,
  33. void *buffer, const u16 buffer_length,
  34. const int timeout)
  35. {
  36. struct usb_device *usb_dev = rt2x00dev_usb_dev(rt2x00dev);
  37. int status;
  38. unsigned int i;
  39. unsigned int pipe =
  40. (requesttype == USB_VENDOR_REQUEST_IN) ?
  41. usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
  42. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  43. status = usb_control_msg(usb_dev, pipe, request, requesttype,
  44. value, offset, buffer, buffer_length,
  45. timeout);
  46. if (status >= 0)
  47. return 0;
  48. /*
  49. * Check for errors
  50. * -ENODEV: Device has disappeared, no point continuing.
  51. * All other errors: Try again.
  52. */
  53. else if (status == -ENODEV)
  54. break;
  55. }
  56. ERROR(rt2x00dev,
  57. "Vendor Request 0x%02x failed for offset 0x%04x with error %d.\n",
  58. request, offset, status);
  59. return status;
  60. }
  61. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request);
  62. int rt2x00usb_vendor_req_buff_lock(struct rt2x00_dev *rt2x00dev,
  63. const u8 request, const u8 requesttype,
  64. const u16 offset, void *buffer,
  65. const u16 buffer_length, const int timeout)
  66. {
  67. int status;
  68. BUG_ON(!mutex_is_locked(&rt2x00dev->usb_cache_mutex));
  69. /*
  70. * Check for Cache availability.
  71. */
  72. if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
  73. ERROR(rt2x00dev, "CSR cache not available.\n");
  74. return -ENOMEM;
  75. }
  76. if (requesttype == USB_VENDOR_REQUEST_OUT)
  77. memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
  78. status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
  79. offset, 0, rt2x00dev->csr.cache,
  80. buffer_length, timeout);
  81. if (!status && requesttype == USB_VENDOR_REQUEST_IN)
  82. memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
  83. return status;
  84. }
  85. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
  86. int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
  87. const u8 request, const u8 requesttype,
  88. const u16 offset, void *buffer,
  89. const u16 buffer_length, const int timeout)
  90. {
  91. int status;
  92. mutex_lock(&rt2x00dev->usb_cache_mutex);
  93. status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
  94. requesttype, offset, buffer,
  95. buffer_length, timeout);
  96. mutex_unlock(&rt2x00dev->usb_cache_mutex);
  97. return status;
  98. }
  99. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
  100. /*
  101. * TX data handlers.
  102. */
  103. static void rt2x00usb_interrupt_txdone(struct urb *urb)
  104. {
  105. struct queue_entry *entry = (struct queue_entry *)urb->context;
  106. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  107. struct queue_entry_priv_usb_tx *priv_tx = entry->priv_data;
  108. struct txdone_entry_desc txdesc;
  109. __le32 *txd = (__le32 *)entry->skb->data;
  110. u32 word;
  111. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
  112. !__test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  113. return;
  114. rt2x00_desc_read(txd, 0, &word);
  115. /*
  116. * Remove the descriptor data from the buffer.
  117. */
  118. skb_pull(entry->skb, entry->queue->desc_size);
  119. /*
  120. * Obtain the status about this packet.
  121. */
  122. txdesc.status = !urb->status ? TX_SUCCESS : TX_FAIL_RETRY;
  123. txdesc.retry = 0;
  124. txdesc.control = &priv_tx->control;
  125. rt2x00lib_txdone(entry, &txdesc);
  126. /*
  127. * Make this entry available for reuse.
  128. */
  129. entry->flags = 0;
  130. rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
  131. /*
  132. * If the data queue was full before the txdone handler
  133. * we must make sure the packet queue in the mac80211 stack
  134. * is reenabled when the txdone handler has finished.
  135. */
  136. if (!rt2x00queue_full(entry->queue))
  137. ieee80211_wake_queue(rt2x00dev->hw, priv_tx->control.queue);
  138. }
  139. int rt2x00usb_write_tx_data(struct rt2x00_dev *rt2x00dev,
  140. struct data_queue *queue, struct sk_buff *skb,
  141. struct ieee80211_tx_control *control)
  142. {
  143. struct usb_device *usb_dev = rt2x00dev_usb_dev(rt2x00dev);
  144. struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
  145. struct queue_entry_priv_usb_tx *priv_tx = entry->priv_data;
  146. struct skb_frame_desc *skbdesc;
  147. u32 length;
  148. if (rt2x00queue_full(queue))
  149. return -EINVAL;
  150. if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) {
  151. ERROR(rt2x00dev,
  152. "Arrived at non-free entry in the non-full queue %d.\n"
  153. "Please file bug report to %s.\n",
  154. control->queue, DRV_PROJECT);
  155. return -EINVAL;
  156. }
  157. /*
  158. * Add the descriptor in front of the skb.
  159. */
  160. skb_push(skb, queue->desc_size);
  161. memset(skb->data, 0, queue->desc_size);
  162. /*
  163. * Fill in skb descriptor
  164. */
  165. skbdesc = get_skb_frame_desc(skb);
  166. skbdesc->data = skb->data + queue->desc_size;
  167. skbdesc->data_len = skb->len - queue->desc_size;
  168. skbdesc->desc = skb->data;
  169. skbdesc->desc_len = queue->desc_size;
  170. skbdesc->entry = entry;
  171. memcpy(&priv_tx->control, control, sizeof(priv_tx->control));
  172. rt2x00lib_write_tx_desc(rt2x00dev, skb, control);
  173. /*
  174. * USB devices cannot blindly pass the skb->len as the
  175. * length of the data to usb_fill_bulk_urb. Pass the skb
  176. * to the driver to determine what the length should be.
  177. */
  178. length = rt2x00dev->ops->lib->get_tx_data_len(rt2x00dev, skb);
  179. /*
  180. * Initialize URB and send the frame to the device.
  181. */
  182. __set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  183. usb_fill_bulk_urb(priv_tx->urb, usb_dev, usb_sndbulkpipe(usb_dev, 1),
  184. skb->data, length, rt2x00usb_interrupt_txdone, entry);
  185. usb_submit_urb(priv_tx->urb, GFP_ATOMIC);
  186. rt2x00queue_index_inc(queue, Q_INDEX);
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data);
  190. /*
  191. * RX data handlers.
  192. */
  193. static struct sk_buff* rt2x00usb_alloc_rxskb(struct data_queue *queue)
  194. {
  195. struct sk_buff *skb;
  196. unsigned int frame_size;
  197. /*
  198. * As alignment we use 2 and not NET_IP_ALIGN because we need
  199. * to be sure we have 2 bytes room in the head. (NET_IP_ALIGN
  200. * can be 0 on some hardware). We use these 2 bytes for frame
  201. * alignment later, we assume that the chance that
  202. * header_size % 4 == 2 is bigger then header_size % 2 == 0
  203. * and thus optimize alignment by reserving the 2 bytes in
  204. * advance.
  205. */
  206. frame_size = queue->data_size + queue->desc_size;
  207. skb = dev_alloc_skb(queue->desc_size + frame_size + 2);
  208. if (!skb)
  209. return NULL;
  210. skb_reserve(skb, queue->desc_size + 2);
  211. skb_put(skb, frame_size);
  212. return skb;
  213. }
  214. static void rt2x00usb_interrupt_rxdone(struct urb *urb)
  215. {
  216. struct queue_entry *entry = (struct queue_entry *)urb->context;
  217. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  218. struct sk_buff *skb;
  219. struct skb_frame_desc *skbdesc;
  220. struct rxdone_entry_desc rxdesc;
  221. int header_size;
  222. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
  223. !test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  224. return;
  225. /*
  226. * Check if the received data is simply too small
  227. * to be actually valid, or if the urb is signaling
  228. * a problem.
  229. */
  230. if (urb->actual_length < entry->queue->desc_size || urb->status)
  231. goto skip_entry;
  232. /*
  233. * Fill in skb descriptor
  234. */
  235. skbdesc = get_skb_frame_desc(entry->skb);
  236. memset(skbdesc, 0, sizeof(*skbdesc));
  237. skbdesc->entry = entry;
  238. memset(&rxdesc, 0, sizeof(rxdesc));
  239. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  240. /*
  241. * The data behind the ieee80211 header must be
  242. * aligned on a 4 byte boundary.
  243. */
  244. header_size = ieee80211_get_hdrlen_from_skb(entry->skb);
  245. if (header_size % 4 == 0) {
  246. skb_push(entry->skb, 2);
  247. memmove(entry->skb->data, entry->skb->data + 2,
  248. entry->skb->len - 2);
  249. skbdesc->data = entry->skb->data;
  250. }
  251. /*
  252. * Allocate a new sk buffer to replace the current one.
  253. * If allocation fails, we should drop the current frame
  254. * so we can recycle the existing sk buffer for the new frame.
  255. */
  256. skb = rt2x00usb_alloc_rxskb(entry->queue);
  257. if (!skb)
  258. goto skip_entry;
  259. /*
  260. * Send the frame to rt2x00lib for further processing.
  261. */
  262. rt2x00lib_rxdone(entry, &rxdesc);
  263. /*
  264. * Replace current entry's skb with the newly allocated one,
  265. * and reinitialize the urb.
  266. */
  267. entry->skb = skb;
  268. urb->transfer_buffer = entry->skb->data;
  269. urb->transfer_buffer_length = entry->skb->len;
  270. skip_entry:
  271. if (test_bit(DEVICE_ENABLED_RADIO, &entry->queue->rt2x00dev->flags)) {
  272. __set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  273. usb_submit_urb(urb, GFP_ATOMIC);
  274. }
  275. rt2x00queue_index_inc(entry->queue, Q_INDEX);
  276. }
  277. /*
  278. * Radio handlers
  279. */
  280. void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
  281. {
  282. struct queue_entry_priv_usb_rx *priv_rx;
  283. struct queue_entry_priv_usb_tx *priv_tx;
  284. struct queue_entry_priv_usb_bcn *priv_bcn;
  285. struct data_queue *queue;
  286. unsigned int i;
  287. rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0x0000, 0x0000,
  288. REGISTER_TIMEOUT);
  289. /*
  290. * Cancel all queues.
  291. */
  292. for (i = 0; i < rt2x00dev->rx->limit; i++) {
  293. priv_rx = rt2x00dev->rx->entries[i].priv_data;
  294. usb_kill_urb(priv_rx->urb);
  295. }
  296. tx_queue_for_each(rt2x00dev, queue) {
  297. for (i = 0; i < queue->limit; i++) {
  298. priv_tx = queue->entries[i].priv_data;
  299. usb_kill_urb(priv_tx->urb);
  300. }
  301. }
  302. for (i = 0; i < rt2x00dev->bcn->limit; i++) {
  303. priv_bcn = rt2x00dev->bcn->entries[i].priv_data;
  304. usb_kill_urb(priv_bcn->urb);
  305. if (priv_bcn->guardian_urb)
  306. usb_kill_urb(priv_bcn->guardian_urb);
  307. }
  308. if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
  309. return;
  310. for (i = 0; i < rt2x00dev->bcn[1].limit; i++) {
  311. priv_tx = rt2x00dev->bcn[1].entries[i].priv_data;
  312. usb_kill_urb(priv_tx->urb);
  313. }
  314. }
  315. EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
  316. /*
  317. * Device initialization handlers.
  318. */
  319. void rt2x00usb_init_rxentry(struct rt2x00_dev *rt2x00dev,
  320. struct queue_entry *entry)
  321. {
  322. struct usb_device *usb_dev = rt2x00dev_usb_dev(rt2x00dev);
  323. struct queue_entry_priv_usb_rx *priv_rx = entry->priv_data;
  324. usb_fill_bulk_urb(priv_rx->urb, usb_dev,
  325. usb_rcvbulkpipe(usb_dev, 1),
  326. entry->skb->data, entry->skb->len,
  327. rt2x00usb_interrupt_rxdone, entry);
  328. __set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  329. usb_submit_urb(priv_rx->urb, GFP_ATOMIC);
  330. }
  331. EXPORT_SYMBOL_GPL(rt2x00usb_init_rxentry);
  332. void rt2x00usb_init_txentry(struct rt2x00_dev *rt2x00dev,
  333. struct queue_entry *entry)
  334. {
  335. entry->flags = 0;
  336. }
  337. EXPORT_SYMBOL_GPL(rt2x00usb_init_txentry);
  338. static int rt2x00usb_alloc_urb(struct rt2x00_dev *rt2x00dev,
  339. struct data_queue *queue)
  340. {
  341. struct queue_entry_priv_usb_rx *priv_rx;
  342. struct queue_entry_priv_usb_tx *priv_tx;
  343. struct queue_entry_priv_usb_bcn *priv_bcn;
  344. struct urb *urb;
  345. unsigned int guardian =
  346. test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags);
  347. unsigned int i;
  348. /*
  349. * Allocate the URB's
  350. */
  351. for (i = 0; i < queue->limit; i++) {
  352. urb = usb_alloc_urb(0, GFP_KERNEL);
  353. if (!urb)
  354. return -ENOMEM;
  355. if (queue->qid == QID_RX) {
  356. priv_rx = queue->entries[i].priv_data;
  357. priv_rx->urb = urb;
  358. } else if (queue->qid == QID_MGMT && guardian) {
  359. priv_bcn = queue->entries[i].priv_data;
  360. priv_bcn->urb = urb;
  361. urb = usb_alloc_urb(0, GFP_KERNEL);
  362. if (!urb)
  363. return -ENOMEM;
  364. priv_bcn->guardian_urb = urb;
  365. } else {
  366. priv_tx = queue->entries[i].priv_data;
  367. priv_tx->urb = urb;
  368. }
  369. }
  370. return 0;
  371. }
  372. static void rt2x00usb_free_urb(struct rt2x00_dev *rt2x00dev,
  373. struct data_queue *queue)
  374. {
  375. struct queue_entry_priv_usb_rx *priv_rx;
  376. struct queue_entry_priv_usb_tx *priv_tx;
  377. struct queue_entry_priv_usb_bcn *priv_bcn;
  378. struct urb *urb;
  379. unsigned int guardian =
  380. test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags);
  381. unsigned int i;
  382. if (!queue->entries)
  383. return;
  384. for (i = 0; i < queue->limit; i++) {
  385. if (queue->qid == QID_RX) {
  386. priv_rx = queue->entries[i].priv_data;
  387. urb = priv_rx->urb;
  388. } else if (queue->qid == QID_MGMT && guardian) {
  389. priv_bcn = queue->entries[i].priv_data;
  390. usb_kill_urb(priv_bcn->guardian_urb);
  391. usb_free_urb(priv_bcn->guardian_urb);
  392. urb = priv_bcn->urb;
  393. } else {
  394. priv_tx = queue->entries[i].priv_data;
  395. urb = priv_tx->urb;
  396. }
  397. usb_kill_urb(urb);
  398. usb_free_urb(urb);
  399. if (queue->entries[i].skb)
  400. kfree_skb(queue->entries[i].skb);
  401. }
  402. }
  403. int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
  404. {
  405. struct data_queue *queue;
  406. struct sk_buff *skb;
  407. unsigned int entry_size;
  408. unsigned int i;
  409. int uninitialized_var(status);
  410. /*
  411. * Allocate DMA
  412. */
  413. queue_for_each(rt2x00dev, queue) {
  414. status = rt2x00usb_alloc_urb(rt2x00dev, queue);
  415. if (status)
  416. goto exit;
  417. }
  418. /*
  419. * For the RX queue, skb's should be allocated.
  420. */
  421. entry_size = rt2x00dev->rx->data_size + rt2x00dev->rx->desc_size;
  422. for (i = 0; i < rt2x00dev->rx->limit; i++) {
  423. skb = rt2x00usb_alloc_rxskb(rt2x00dev->rx);
  424. if (!skb)
  425. goto exit;
  426. rt2x00dev->rx->entries[i].skb = skb;
  427. }
  428. return 0;
  429. exit:
  430. rt2x00usb_uninitialize(rt2x00dev);
  431. return status;
  432. }
  433. EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
  434. void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
  435. {
  436. struct data_queue *queue;
  437. queue_for_each(rt2x00dev, queue)
  438. rt2x00usb_free_urb(rt2x00dev, queue);
  439. }
  440. EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
  441. /*
  442. * USB driver handlers.
  443. */
  444. static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
  445. {
  446. kfree(rt2x00dev->rf);
  447. rt2x00dev->rf = NULL;
  448. kfree(rt2x00dev->eeprom);
  449. rt2x00dev->eeprom = NULL;
  450. kfree(rt2x00dev->csr.cache);
  451. rt2x00dev->csr.cache = NULL;
  452. }
  453. static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
  454. {
  455. rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
  456. if (!rt2x00dev->csr.cache)
  457. goto exit;
  458. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  459. if (!rt2x00dev->eeprom)
  460. goto exit;
  461. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  462. if (!rt2x00dev->rf)
  463. goto exit;
  464. return 0;
  465. exit:
  466. ERROR_PROBE("Failed to allocate registers.\n");
  467. rt2x00usb_free_reg(rt2x00dev);
  468. return -ENOMEM;
  469. }
  470. int rt2x00usb_probe(struct usb_interface *usb_intf,
  471. const struct usb_device_id *id)
  472. {
  473. struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
  474. struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_info;
  475. struct ieee80211_hw *hw;
  476. struct rt2x00_dev *rt2x00dev;
  477. int retval;
  478. usb_dev = usb_get_dev(usb_dev);
  479. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  480. if (!hw) {
  481. ERROR_PROBE("Failed to allocate hardware.\n");
  482. retval = -ENOMEM;
  483. goto exit_put_device;
  484. }
  485. usb_set_intfdata(usb_intf, hw);
  486. rt2x00dev = hw->priv;
  487. rt2x00dev->dev = usb_intf;
  488. rt2x00dev->ops = ops;
  489. rt2x00dev->hw = hw;
  490. mutex_init(&rt2x00dev->usb_cache_mutex);
  491. rt2x00dev->usb_maxpacket =
  492. usb_maxpacket(usb_dev, usb_sndbulkpipe(usb_dev, 1), 1);
  493. if (!rt2x00dev->usb_maxpacket)
  494. rt2x00dev->usb_maxpacket = 1;
  495. retval = rt2x00usb_alloc_reg(rt2x00dev);
  496. if (retval)
  497. goto exit_free_device;
  498. retval = rt2x00lib_probe_dev(rt2x00dev);
  499. if (retval)
  500. goto exit_free_reg;
  501. return 0;
  502. exit_free_reg:
  503. rt2x00usb_free_reg(rt2x00dev);
  504. exit_free_device:
  505. ieee80211_free_hw(hw);
  506. exit_put_device:
  507. usb_put_dev(usb_dev);
  508. usb_set_intfdata(usb_intf, NULL);
  509. return retval;
  510. }
  511. EXPORT_SYMBOL_GPL(rt2x00usb_probe);
  512. void rt2x00usb_disconnect(struct usb_interface *usb_intf)
  513. {
  514. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  515. struct rt2x00_dev *rt2x00dev = hw->priv;
  516. /*
  517. * Free all allocated data.
  518. */
  519. rt2x00lib_remove_dev(rt2x00dev);
  520. rt2x00usb_free_reg(rt2x00dev);
  521. ieee80211_free_hw(hw);
  522. /*
  523. * Free the USB device data.
  524. */
  525. usb_set_intfdata(usb_intf, NULL);
  526. usb_put_dev(interface_to_usbdev(usb_intf));
  527. }
  528. EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
  529. #ifdef CONFIG_PM
  530. int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
  531. {
  532. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  533. struct rt2x00_dev *rt2x00dev = hw->priv;
  534. int retval;
  535. retval = rt2x00lib_suspend(rt2x00dev, state);
  536. if (retval)
  537. return retval;
  538. rt2x00usb_free_reg(rt2x00dev);
  539. /*
  540. * Decrease usbdev refcount.
  541. */
  542. usb_put_dev(interface_to_usbdev(usb_intf));
  543. return 0;
  544. }
  545. EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
  546. int rt2x00usb_resume(struct usb_interface *usb_intf)
  547. {
  548. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  549. struct rt2x00_dev *rt2x00dev = hw->priv;
  550. int retval;
  551. usb_get_dev(interface_to_usbdev(usb_intf));
  552. retval = rt2x00usb_alloc_reg(rt2x00dev);
  553. if (retval)
  554. return retval;
  555. retval = rt2x00lib_resume(rt2x00dev);
  556. if (retval)
  557. goto exit_free_reg;
  558. return 0;
  559. exit_free_reg:
  560. rt2x00usb_free_reg(rt2x00dev);
  561. return retval;
  562. }
  563. EXPORT_SYMBOL_GPL(rt2x00usb_resume);
  564. #endif /* CONFIG_PM */
  565. /*
  566. * rt2x00usb module information.
  567. */
  568. MODULE_AUTHOR(DRV_PROJECT);
  569. MODULE_VERSION(DRV_VERSION);
  570. MODULE_DESCRIPTION("rt2x00 usb library");
  571. MODULE_LICENSE("GPL");