rt2x00usb.c 18 KB

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