rt2x00usb.c 19 KB

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