rt2x00usb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. * Note that when the status is 0 it does not mean the
  122. * frame was send out correctly. It only means the frame
  123. * was succesfully pushed to the hardware, we have no
  124. * way to determine the transmission status right now.
  125. * (Only indirectly by looking at the failed TX counters
  126. * in the register).
  127. */
  128. if (!urb->status)
  129. __set_bit(TXDONE_UNKNOWN, &txdesc.flags);
  130. else
  131. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  132. txdesc.retry = 0;
  133. txdesc.control = &priv_tx->control;
  134. rt2x00lib_txdone(entry, &txdesc);
  135. /*
  136. * Make this entry available for reuse.
  137. */
  138. entry->flags = 0;
  139. rt2x00queue_index_inc(entry->queue, Q_INDEX_DONE);
  140. /*
  141. * If the data queue was full before the txdone handler
  142. * we must make sure the packet queue in the mac80211 stack
  143. * is reenabled when the txdone handler has finished.
  144. */
  145. if (!rt2x00queue_full(entry->queue))
  146. ieee80211_wake_queue(rt2x00dev->hw, priv_tx->control.queue);
  147. }
  148. int rt2x00usb_write_tx_data(struct rt2x00_dev *rt2x00dev,
  149. struct data_queue *queue, struct sk_buff *skb,
  150. struct ieee80211_tx_control *control)
  151. {
  152. struct usb_device *usb_dev = rt2x00dev_usb_dev(rt2x00dev);
  153. struct queue_entry *entry = rt2x00queue_get_entry(queue, Q_INDEX);
  154. struct queue_entry_priv_usb_tx *priv_tx = entry->priv_data;
  155. struct skb_frame_desc *skbdesc;
  156. struct txentry_desc txdesc;
  157. u32 length;
  158. if (rt2x00queue_full(queue))
  159. return -EINVAL;
  160. if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags)) {
  161. ERROR(rt2x00dev,
  162. "Arrived at non-free entry in the non-full queue %d.\n"
  163. "Please file bug report to %s.\n",
  164. entry->queue->qid, DRV_PROJECT);
  165. return -EINVAL;
  166. }
  167. /*
  168. * Copy all TX descriptor information into txdesc,
  169. * after that we are free to use the skb->cb array
  170. * for our information.
  171. */
  172. entry->skb = skb;
  173. rt2x00queue_create_tx_descriptor(entry, &txdesc, control);
  174. /*
  175. * Add the descriptor in front of the skb.
  176. */
  177. skb_push(skb, queue->desc_size);
  178. memset(skb->data, 0, queue->desc_size);
  179. /*
  180. * Fill in skb descriptor
  181. */
  182. skbdesc = get_skb_frame_desc(skb);
  183. skbdesc->data = skb->data + queue->desc_size;
  184. skbdesc->data_len = skb->len - queue->desc_size;
  185. skbdesc->desc = skb->data;
  186. skbdesc->desc_len = queue->desc_size;
  187. skbdesc->entry = entry;
  188. memcpy(&priv_tx->control, control, sizeof(priv_tx->control));
  189. rt2x00queue_write_tx_descriptor(entry, &txdesc);
  190. /*
  191. * USB devices cannot blindly pass the skb->len as the
  192. * length of the data to usb_fill_bulk_urb. Pass the skb
  193. * to the driver to determine what the length should be.
  194. */
  195. length = rt2x00dev->ops->lib->get_tx_data_len(rt2x00dev, skb);
  196. /*
  197. * Initialize URB and send the frame to the device.
  198. */
  199. __set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  200. usb_fill_bulk_urb(priv_tx->urb, usb_dev, usb_sndbulkpipe(usb_dev, 1),
  201. skb->data, length, rt2x00usb_interrupt_txdone, entry);
  202. usb_submit_urb(priv_tx->urb, GFP_ATOMIC);
  203. rt2x00queue_index_inc(queue, Q_INDEX);
  204. return 0;
  205. }
  206. EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data);
  207. /*
  208. * RX data handlers.
  209. */
  210. static struct sk_buff* rt2x00usb_alloc_rxskb(struct data_queue *queue)
  211. {
  212. struct sk_buff *skb;
  213. unsigned int frame_size;
  214. unsigned int reserved_size;
  215. /*
  216. * The frame size includes descriptor size, because the
  217. * hardware directly receive the frame into the skbuffer.
  218. */
  219. frame_size = queue->data_size + queue->desc_size;
  220. /*
  221. * For the allocation we should keep a few things in mind:
  222. * 1) 4byte alignment of 802.11 payload
  223. *
  224. * For (1) we need at most 4 bytes to guarentee the correct
  225. * alignment. We are going to optimize the fact that the chance
  226. * that the 802.11 header_size % 4 == 2 is much bigger then
  227. * anything else. However since we need to move the frame up
  228. * to 3 bytes to the front, which means we need to preallocate
  229. * 6 bytes.
  230. */
  231. reserved_size = 6;
  232. /*
  233. * Allocate skbuffer.
  234. */
  235. skb = dev_alloc_skb(frame_size + reserved_size);
  236. if (!skb)
  237. return NULL;
  238. skb_reserve(skb, reserved_size);
  239. skb_put(skb, frame_size);
  240. return skb;
  241. }
  242. static void rt2x00usb_interrupt_rxdone(struct urb *urb)
  243. {
  244. struct queue_entry *entry = (struct queue_entry *)urb->context;
  245. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  246. struct sk_buff *skb;
  247. struct skb_frame_desc *skbdesc;
  248. struct rxdone_entry_desc rxdesc;
  249. unsigned int header_size;
  250. unsigned int align;
  251. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
  252. !test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  253. return;
  254. /*
  255. * Check if the received data is simply too small
  256. * to be actually valid, or if the urb is signaling
  257. * a problem.
  258. */
  259. if (urb->actual_length < entry->queue->desc_size || urb->status)
  260. goto skip_entry;
  261. /*
  262. * Fill in skb descriptor
  263. */
  264. skbdesc = get_skb_frame_desc(entry->skb);
  265. memset(skbdesc, 0, sizeof(*skbdesc));
  266. skbdesc->entry = entry;
  267. memset(&rxdesc, 0, sizeof(rxdesc));
  268. rt2x00dev->ops->lib->fill_rxdone(entry, &rxdesc);
  269. header_size = ieee80211_get_hdrlen_from_skb(entry->skb);
  270. /*
  271. * The data behind the ieee80211 header must be
  272. * aligned on a 4 byte boundary. We already reserved
  273. * 2 bytes for header_size % 4 == 2 optimization.
  274. * To determine the number of bytes which the data
  275. * should be moved to the left, we must add these
  276. * 2 bytes to the header_size.
  277. */
  278. align = (header_size + 2) % 4;
  279. if (align) {
  280. skb_push(entry->skb, align);
  281. /* Move entire frame in 1 command */
  282. memmove(entry->skb->data, entry->skb->data + align,
  283. rxdesc.size);
  284. }
  285. /* Update data pointers, trim buffer to correct size */
  286. skbdesc->data = entry->skb->data;
  287. skb_trim(entry->skb, rxdesc.size);
  288. /*
  289. * Allocate a new sk buffer to replace the current one.
  290. * If allocation fails, we should drop the current frame
  291. * so we can recycle the existing sk buffer for the new frame.
  292. */
  293. skb = rt2x00usb_alloc_rxskb(entry->queue);
  294. if (!skb)
  295. goto skip_entry;
  296. /*
  297. * Send the frame to rt2x00lib for further processing.
  298. */
  299. rt2x00lib_rxdone(entry, &rxdesc);
  300. /*
  301. * Replace current entry's skb with the newly allocated one,
  302. * and reinitialize the urb.
  303. */
  304. entry->skb = skb;
  305. urb->transfer_buffer = entry->skb->data;
  306. urb->transfer_buffer_length = entry->skb->len;
  307. skip_entry:
  308. if (test_bit(DEVICE_ENABLED_RADIO, &entry->queue->rt2x00dev->flags)) {
  309. __set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  310. usb_submit_urb(urb, GFP_ATOMIC);
  311. }
  312. rt2x00queue_index_inc(entry->queue, Q_INDEX);
  313. }
  314. /*
  315. * Radio handlers
  316. */
  317. void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
  318. {
  319. struct queue_entry_priv_usb_rx *priv_rx;
  320. struct queue_entry_priv_usb_tx *priv_tx;
  321. struct queue_entry_priv_usb_bcn *priv_bcn;
  322. struct data_queue *queue;
  323. unsigned int i;
  324. rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
  325. REGISTER_TIMEOUT);
  326. /*
  327. * Cancel all queues.
  328. */
  329. for (i = 0; i < rt2x00dev->rx->limit; i++) {
  330. priv_rx = rt2x00dev->rx->entries[i].priv_data;
  331. usb_kill_urb(priv_rx->urb);
  332. }
  333. tx_queue_for_each(rt2x00dev, queue) {
  334. for (i = 0; i < queue->limit; i++) {
  335. priv_tx = queue->entries[i].priv_data;
  336. usb_kill_urb(priv_tx->urb);
  337. }
  338. }
  339. for (i = 0; i < rt2x00dev->bcn->limit; i++) {
  340. priv_bcn = rt2x00dev->bcn->entries[i].priv_data;
  341. usb_kill_urb(priv_bcn->urb);
  342. if (priv_bcn->guardian_urb)
  343. usb_kill_urb(priv_bcn->guardian_urb);
  344. }
  345. if (!test_bit(DRIVER_REQUIRE_ATIM_QUEUE, &rt2x00dev->flags))
  346. return;
  347. for (i = 0; i < rt2x00dev->bcn[1].limit; i++) {
  348. priv_tx = rt2x00dev->bcn[1].entries[i].priv_data;
  349. usb_kill_urb(priv_tx->urb);
  350. }
  351. }
  352. EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
  353. /*
  354. * Device initialization handlers.
  355. */
  356. void rt2x00usb_init_rxentry(struct rt2x00_dev *rt2x00dev,
  357. struct queue_entry *entry)
  358. {
  359. struct usb_device *usb_dev = rt2x00dev_usb_dev(rt2x00dev);
  360. struct queue_entry_priv_usb_rx *priv_rx = entry->priv_data;
  361. usb_fill_bulk_urb(priv_rx->urb, usb_dev,
  362. usb_rcvbulkpipe(usb_dev, 1),
  363. entry->skb->data, entry->skb->len,
  364. rt2x00usb_interrupt_rxdone, entry);
  365. __set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  366. usb_submit_urb(priv_rx->urb, GFP_ATOMIC);
  367. }
  368. EXPORT_SYMBOL_GPL(rt2x00usb_init_rxentry);
  369. void rt2x00usb_init_txentry(struct rt2x00_dev *rt2x00dev,
  370. struct queue_entry *entry)
  371. {
  372. entry->flags = 0;
  373. }
  374. EXPORT_SYMBOL_GPL(rt2x00usb_init_txentry);
  375. static int rt2x00usb_alloc_urb(struct rt2x00_dev *rt2x00dev,
  376. struct data_queue *queue)
  377. {
  378. struct queue_entry_priv_usb_rx *priv_rx;
  379. struct queue_entry_priv_usb_tx *priv_tx;
  380. struct queue_entry_priv_usb_bcn *priv_bcn;
  381. struct urb *urb;
  382. unsigned int guardian =
  383. test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags);
  384. unsigned int i;
  385. /*
  386. * Allocate the URB's
  387. */
  388. for (i = 0; i < queue->limit; i++) {
  389. urb = usb_alloc_urb(0, GFP_KERNEL);
  390. if (!urb)
  391. return -ENOMEM;
  392. if (queue->qid == QID_RX) {
  393. priv_rx = queue->entries[i].priv_data;
  394. priv_rx->urb = urb;
  395. } else if (queue->qid == QID_MGMT && guardian) {
  396. priv_bcn = queue->entries[i].priv_data;
  397. priv_bcn->urb = urb;
  398. urb = usb_alloc_urb(0, GFP_KERNEL);
  399. if (!urb)
  400. return -ENOMEM;
  401. priv_bcn->guardian_urb = urb;
  402. } else {
  403. priv_tx = queue->entries[i].priv_data;
  404. priv_tx->urb = urb;
  405. }
  406. }
  407. return 0;
  408. }
  409. static void rt2x00usb_free_urb(struct rt2x00_dev *rt2x00dev,
  410. struct data_queue *queue)
  411. {
  412. struct queue_entry_priv_usb_rx *priv_rx;
  413. struct queue_entry_priv_usb_tx *priv_tx;
  414. struct queue_entry_priv_usb_bcn *priv_bcn;
  415. struct urb *urb;
  416. unsigned int guardian =
  417. test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags);
  418. unsigned int i;
  419. if (!queue->entries)
  420. return;
  421. for (i = 0; i < queue->limit; i++) {
  422. if (queue->qid == QID_RX) {
  423. priv_rx = queue->entries[i].priv_data;
  424. urb = priv_rx->urb;
  425. } else if (queue->qid == QID_MGMT && guardian) {
  426. priv_bcn = queue->entries[i].priv_data;
  427. usb_kill_urb(priv_bcn->guardian_urb);
  428. usb_free_urb(priv_bcn->guardian_urb);
  429. urb = priv_bcn->urb;
  430. } else {
  431. priv_tx = queue->entries[i].priv_data;
  432. urb = priv_tx->urb;
  433. }
  434. usb_kill_urb(urb);
  435. usb_free_urb(urb);
  436. if (queue->entries[i].skb)
  437. kfree_skb(queue->entries[i].skb);
  438. }
  439. }
  440. int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
  441. {
  442. struct data_queue *queue;
  443. struct sk_buff *skb;
  444. unsigned int entry_size;
  445. unsigned int i;
  446. int uninitialized_var(status);
  447. /*
  448. * Allocate DMA
  449. */
  450. queue_for_each(rt2x00dev, queue) {
  451. status = rt2x00usb_alloc_urb(rt2x00dev, queue);
  452. if (status)
  453. goto exit;
  454. }
  455. /*
  456. * For the RX queue, skb's should be allocated.
  457. */
  458. entry_size = rt2x00dev->rx->data_size + rt2x00dev->rx->desc_size;
  459. for (i = 0; i < rt2x00dev->rx->limit; i++) {
  460. skb = rt2x00usb_alloc_rxskb(rt2x00dev->rx);
  461. if (!skb)
  462. goto exit;
  463. rt2x00dev->rx->entries[i].skb = skb;
  464. }
  465. return 0;
  466. exit:
  467. rt2x00usb_uninitialize(rt2x00dev);
  468. return status;
  469. }
  470. EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
  471. void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
  472. {
  473. struct data_queue *queue;
  474. queue_for_each(rt2x00dev, queue)
  475. rt2x00usb_free_urb(rt2x00dev, queue);
  476. }
  477. EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
  478. /*
  479. * USB driver handlers.
  480. */
  481. static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
  482. {
  483. kfree(rt2x00dev->rf);
  484. rt2x00dev->rf = NULL;
  485. kfree(rt2x00dev->eeprom);
  486. rt2x00dev->eeprom = NULL;
  487. kfree(rt2x00dev->csr.cache);
  488. rt2x00dev->csr.cache = NULL;
  489. }
  490. static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
  491. {
  492. rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
  493. if (!rt2x00dev->csr.cache)
  494. goto exit;
  495. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  496. if (!rt2x00dev->eeprom)
  497. goto exit;
  498. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  499. if (!rt2x00dev->rf)
  500. goto exit;
  501. return 0;
  502. exit:
  503. ERROR_PROBE("Failed to allocate registers.\n");
  504. rt2x00usb_free_reg(rt2x00dev);
  505. return -ENOMEM;
  506. }
  507. int rt2x00usb_probe(struct usb_interface *usb_intf,
  508. const struct usb_device_id *id)
  509. {
  510. struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
  511. struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_info;
  512. struct ieee80211_hw *hw;
  513. struct rt2x00_dev *rt2x00dev;
  514. int retval;
  515. usb_dev = usb_get_dev(usb_dev);
  516. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  517. if (!hw) {
  518. ERROR_PROBE("Failed to allocate hardware.\n");
  519. retval = -ENOMEM;
  520. goto exit_put_device;
  521. }
  522. usb_set_intfdata(usb_intf, hw);
  523. rt2x00dev = hw->priv;
  524. rt2x00dev->dev = usb_intf;
  525. rt2x00dev->ops = ops;
  526. rt2x00dev->hw = hw;
  527. mutex_init(&rt2x00dev->usb_cache_mutex);
  528. rt2x00dev->usb_maxpacket =
  529. usb_maxpacket(usb_dev, usb_sndbulkpipe(usb_dev, 1), 1);
  530. if (!rt2x00dev->usb_maxpacket)
  531. rt2x00dev->usb_maxpacket = 1;
  532. retval = rt2x00usb_alloc_reg(rt2x00dev);
  533. if (retval)
  534. goto exit_free_device;
  535. retval = rt2x00lib_probe_dev(rt2x00dev);
  536. if (retval)
  537. goto exit_free_reg;
  538. return 0;
  539. exit_free_reg:
  540. rt2x00usb_free_reg(rt2x00dev);
  541. exit_free_device:
  542. ieee80211_free_hw(hw);
  543. exit_put_device:
  544. usb_put_dev(usb_dev);
  545. usb_set_intfdata(usb_intf, NULL);
  546. return retval;
  547. }
  548. EXPORT_SYMBOL_GPL(rt2x00usb_probe);
  549. void rt2x00usb_disconnect(struct usb_interface *usb_intf)
  550. {
  551. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  552. struct rt2x00_dev *rt2x00dev = hw->priv;
  553. /*
  554. * Free all allocated data.
  555. */
  556. rt2x00lib_remove_dev(rt2x00dev);
  557. rt2x00usb_free_reg(rt2x00dev);
  558. ieee80211_free_hw(hw);
  559. /*
  560. * Free the USB device data.
  561. */
  562. usb_set_intfdata(usb_intf, NULL);
  563. usb_put_dev(interface_to_usbdev(usb_intf));
  564. }
  565. EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
  566. #ifdef CONFIG_PM
  567. int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
  568. {
  569. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  570. struct rt2x00_dev *rt2x00dev = hw->priv;
  571. int retval;
  572. retval = rt2x00lib_suspend(rt2x00dev, state);
  573. if (retval)
  574. return retval;
  575. rt2x00usb_free_reg(rt2x00dev);
  576. /*
  577. * Decrease usbdev refcount.
  578. */
  579. usb_put_dev(interface_to_usbdev(usb_intf));
  580. return 0;
  581. }
  582. EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
  583. int rt2x00usb_resume(struct usb_interface *usb_intf)
  584. {
  585. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  586. struct rt2x00_dev *rt2x00dev = hw->priv;
  587. int retval;
  588. usb_get_dev(interface_to_usbdev(usb_intf));
  589. retval = rt2x00usb_alloc_reg(rt2x00dev);
  590. if (retval)
  591. return retval;
  592. retval = rt2x00lib_resume(rt2x00dev);
  593. if (retval)
  594. goto exit_free_reg;
  595. return 0;
  596. exit_free_reg:
  597. rt2x00usb_free_reg(rt2x00dev);
  598. return retval;
  599. }
  600. EXPORT_SYMBOL_GPL(rt2x00usb_resume);
  601. #endif /* CONFIG_PM */
  602. /*
  603. * rt2x00usb module information.
  604. */
  605. MODULE_AUTHOR(DRV_PROJECT);
  606. MODULE_VERSION(DRV_VERSION);
  607. MODULE_DESCRIPTION("rt2x00 usb library");
  608. MODULE_LICENSE("GPL");