rt2x00usb.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 = to_usb_device_intf(rt2x00dev->dev);
  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->csr_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->csr_mutex);
  93. status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
  94. requesttype, offset, buffer,
  95. buffer_length, timeout);
  96. mutex_unlock(&rt2x00dev->csr_mutex);
  97. return status;
  98. }
  99. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
  100. int rt2x00usb_vendor_request_large_buff(struct rt2x00_dev *rt2x00dev,
  101. const u8 request, const u8 requesttype,
  102. const u16 offset, const void *buffer,
  103. const u16 buffer_length,
  104. const int timeout)
  105. {
  106. int status = 0;
  107. unsigned char *tb;
  108. u16 off, len, bsize;
  109. mutex_lock(&rt2x00dev->csr_mutex);
  110. tb = (char *)buffer;
  111. off = offset;
  112. len = buffer_length;
  113. while (len && !status) {
  114. bsize = min_t(u16, CSR_CACHE_SIZE, len);
  115. status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
  116. requesttype, off, tb,
  117. bsize, timeout);
  118. tb += bsize;
  119. len -= bsize;
  120. off += bsize;
  121. }
  122. mutex_unlock(&rt2x00dev->csr_mutex);
  123. return status;
  124. }
  125. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_large_buff);
  126. int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
  127. const unsigned int offset,
  128. struct rt2x00_field32 field,
  129. u32 *reg)
  130. {
  131. unsigned int i;
  132. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  133. rt2x00usb_register_read_lock(rt2x00dev, offset, reg);
  134. if (!rt2x00_get_field32(*reg, field))
  135. return 1;
  136. udelay(REGISTER_BUSY_DELAY);
  137. }
  138. ERROR(rt2x00dev, "Indirect register access failed: "
  139. "offset=0x%.08x, value=0x%.08x\n", offset, *reg);
  140. *reg = ~0;
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
  144. /*
  145. * TX data handlers.
  146. */
  147. static void rt2x00usb_interrupt_txdone(struct urb *urb)
  148. {
  149. struct queue_entry *entry = (struct queue_entry *)urb->context;
  150. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  151. struct txdone_entry_desc txdesc;
  152. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
  153. !test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  154. return;
  155. /*
  156. * Obtain the status about this packet.
  157. * Note that when the status is 0 it does not mean the
  158. * frame was send out correctly. It only means the frame
  159. * was succesfully pushed to the hardware, we have no
  160. * way to determine the transmission status right now.
  161. * (Only indirectly by looking at the failed TX counters
  162. * in the register).
  163. */
  164. txdesc.flags = 0;
  165. if (!urb->status)
  166. __set_bit(TXDONE_UNKNOWN, &txdesc.flags);
  167. else
  168. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  169. txdesc.retry = 0;
  170. rt2x00lib_txdone(entry, &txdesc);
  171. }
  172. int rt2x00usb_write_tx_data(struct queue_entry *entry)
  173. {
  174. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  175. struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
  176. struct queue_entry_priv_usb *entry_priv = entry->priv_data;
  177. struct skb_frame_desc *skbdesc;
  178. u32 length;
  179. /*
  180. * Add the descriptor in front of the skb.
  181. */
  182. skb_push(entry->skb, entry->queue->desc_size);
  183. memset(entry->skb->data, 0, entry->queue->desc_size);
  184. /*
  185. * Fill in skb descriptor
  186. */
  187. skbdesc = get_skb_frame_desc(entry->skb);
  188. skbdesc->desc = entry->skb->data;
  189. skbdesc->desc_len = entry->queue->desc_size;
  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(entry);
  196. usb_fill_bulk_urb(entry_priv->urb, usb_dev,
  197. usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
  198. entry->skb->data, length,
  199. rt2x00usb_interrupt_txdone, entry);
  200. /*
  201. * Make sure the skb->data pointer points to the frame, not the
  202. * descriptor.
  203. */
  204. skb_pull(entry->skb, entry->queue->desc_size);
  205. return 0;
  206. }
  207. EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data);
  208. static inline void rt2x00usb_kick_tx_entry(struct queue_entry *entry)
  209. {
  210. struct queue_entry_priv_usb *entry_priv = entry->priv_data;
  211. if (test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags))
  212. usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
  213. }
  214. void rt2x00usb_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
  215. const enum data_queue_qid qid)
  216. {
  217. struct data_queue *queue = rt2x00queue_get_queue(rt2x00dev, qid);
  218. unsigned long irqflags;
  219. unsigned int index;
  220. unsigned int index_done;
  221. unsigned int i;
  222. /*
  223. * Only protect the range we are going to loop over,
  224. * if during our loop a extra entry is set to pending
  225. * it should not be kicked during this run, since it
  226. * is part of another TX operation.
  227. */
  228. spin_lock_irqsave(&queue->lock, irqflags);
  229. index = queue->index[Q_INDEX];
  230. index_done = queue->index[Q_INDEX_DONE];
  231. spin_unlock_irqrestore(&queue->lock, irqflags);
  232. /*
  233. * Start from the TX done pointer, this guarentees that we will
  234. * send out all frames in the correct order.
  235. */
  236. if (index_done < index) {
  237. for (i = index_done; i < index; i++)
  238. rt2x00usb_kick_tx_entry(&queue->entries[i]);
  239. } else {
  240. for (i = index_done; i < queue->limit; i++)
  241. rt2x00usb_kick_tx_entry(&queue->entries[i]);
  242. for (i = 0; i < index; i++)
  243. rt2x00usb_kick_tx_entry(&queue->entries[i]);
  244. }
  245. }
  246. EXPORT_SYMBOL_GPL(rt2x00usb_kick_tx_queue);
  247. /*
  248. * RX data handlers.
  249. */
  250. static void rt2x00usb_interrupt_rxdone(struct urb *urb)
  251. {
  252. struct queue_entry *entry = (struct queue_entry *)urb->context;
  253. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  254. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  255. u8 rxd[32];
  256. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags) ||
  257. !test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  258. return;
  259. /*
  260. * Check if the received data is simply too small
  261. * to be actually valid, or if the urb is signaling
  262. * a problem.
  263. */
  264. if (urb->actual_length < entry->queue->desc_size || urb->status) {
  265. set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  266. usb_submit_urb(urb, GFP_ATOMIC);
  267. return;
  268. }
  269. /*
  270. * Fill in desc fields of the skb descriptor
  271. */
  272. skbdesc->desc = rxd;
  273. skbdesc->desc_len = entry->queue->desc_size;
  274. /*
  275. * Send the frame to rt2x00lib for further processing.
  276. */
  277. rt2x00lib_rxdone(rt2x00dev, entry);
  278. }
  279. /*
  280. * Radio handlers
  281. */
  282. void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
  283. {
  284. struct queue_entry_priv_usb *entry_priv;
  285. struct queue_entry_priv_usb_bcn *bcn_priv;
  286. struct data_queue *queue;
  287. unsigned int i;
  288. rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
  289. REGISTER_TIMEOUT);
  290. /*
  291. * Cancel all queues.
  292. */
  293. queue_for_each(rt2x00dev, queue) {
  294. for (i = 0; i < queue->limit; i++) {
  295. entry_priv = queue->entries[i].priv_data;
  296. usb_kill_urb(entry_priv->urb);
  297. }
  298. }
  299. /*
  300. * Kill guardian urb (if required by driver).
  301. */
  302. if (!test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
  303. return;
  304. for (i = 0; i < rt2x00dev->bcn->limit; i++) {
  305. bcn_priv = rt2x00dev->bcn->entries[i].priv_data;
  306. if (bcn_priv->guardian_urb)
  307. usb_kill_urb(bcn_priv->guardian_urb);
  308. }
  309. }
  310. EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
  311. /*
  312. * Device initialization handlers.
  313. */
  314. void rt2x00usb_clear_entry(struct queue_entry *entry)
  315. {
  316. struct usb_device *usb_dev =
  317. to_usb_device_intf(entry->queue->rt2x00dev->dev);
  318. struct queue_entry_priv_usb *entry_priv = entry->priv_data;
  319. int pipe;
  320. if (entry->queue->qid == QID_RX) {
  321. pipe = usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint);
  322. usb_fill_bulk_urb(entry_priv->urb, usb_dev, pipe,
  323. entry->skb->data, entry->skb->len,
  324. rt2x00usb_interrupt_rxdone, entry);
  325. set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags);
  326. usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
  327. } else {
  328. entry->flags = 0;
  329. }
  330. }
  331. EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
  332. static void rt2x00usb_assign_endpoint(struct data_queue *queue,
  333. struct usb_endpoint_descriptor *ep_desc)
  334. {
  335. struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
  336. int pipe;
  337. queue->usb_endpoint = usb_endpoint_num(ep_desc);
  338. if (queue->qid == QID_RX) {
  339. pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
  340. queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 0);
  341. } else {
  342. pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
  343. queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 1);
  344. }
  345. if (!queue->usb_maxpacket)
  346. queue->usb_maxpacket = 1;
  347. }
  348. static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
  349. {
  350. struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
  351. struct usb_host_interface *intf_desc = intf->cur_altsetting;
  352. struct usb_endpoint_descriptor *ep_desc;
  353. struct data_queue *queue = rt2x00dev->tx;
  354. struct usb_endpoint_descriptor *tx_ep_desc = NULL;
  355. unsigned int i;
  356. /*
  357. * Walk through all available endpoints to search for "bulk in"
  358. * and "bulk out" endpoints. When we find such endpoints collect
  359. * the information we need from the descriptor and assign it
  360. * to the queue.
  361. */
  362. for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
  363. ep_desc = &intf_desc->endpoint[i].desc;
  364. if (usb_endpoint_is_bulk_in(ep_desc)) {
  365. rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
  366. } else if (usb_endpoint_is_bulk_out(ep_desc) &&
  367. (queue != queue_end(rt2x00dev))) {
  368. rt2x00usb_assign_endpoint(queue, ep_desc);
  369. queue = queue_next(queue);
  370. tx_ep_desc = ep_desc;
  371. }
  372. }
  373. /*
  374. * At least 1 endpoint for RX and 1 endpoint for TX must be available.
  375. */
  376. if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
  377. ERROR(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
  378. return -EPIPE;
  379. }
  380. /*
  381. * It might be possible not all queues have a dedicated endpoint.
  382. * Loop through all TX queues and copy the endpoint information
  383. * which we have gathered from already assigned endpoints.
  384. */
  385. txall_queue_for_each(rt2x00dev, queue) {
  386. if (!queue->usb_endpoint)
  387. rt2x00usb_assign_endpoint(queue, tx_ep_desc);
  388. }
  389. return 0;
  390. }
  391. static int rt2x00usb_alloc_urb(struct rt2x00_dev *rt2x00dev,
  392. struct data_queue *queue)
  393. {
  394. struct queue_entry_priv_usb *entry_priv;
  395. struct queue_entry_priv_usb_bcn *bcn_priv;
  396. unsigned int i;
  397. for (i = 0; i < queue->limit; i++) {
  398. entry_priv = queue->entries[i].priv_data;
  399. entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
  400. if (!entry_priv->urb)
  401. return -ENOMEM;
  402. }
  403. /*
  404. * If this is not the beacon queue or
  405. * no guardian byte was required for the beacon,
  406. * then we are done.
  407. */
  408. if (rt2x00dev->bcn != queue ||
  409. !test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
  410. return 0;
  411. for (i = 0; i < queue->limit; i++) {
  412. bcn_priv = queue->entries[i].priv_data;
  413. bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
  414. if (!bcn_priv->guardian_urb)
  415. return -ENOMEM;
  416. }
  417. return 0;
  418. }
  419. static void rt2x00usb_free_urb(struct rt2x00_dev *rt2x00dev,
  420. struct data_queue *queue)
  421. {
  422. struct queue_entry_priv_usb *entry_priv;
  423. struct queue_entry_priv_usb_bcn *bcn_priv;
  424. unsigned int i;
  425. if (!queue->entries)
  426. return;
  427. for (i = 0; i < queue->limit; i++) {
  428. entry_priv = queue->entries[i].priv_data;
  429. usb_kill_urb(entry_priv->urb);
  430. usb_free_urb(entry_priv->urb);
  431. }
  432. /*
  433. * If this is not the beacon queue or
  434. * no guardian byte was required for the beacon,
  435. * then we are done.
  436. */
  437. if (rt2x00dev->bcn != queue ||
  438. !test_bit(DRIVER_REQUIRE_BEACON_GUARD, &rt2x00dev->flags))
  439. return;
  440. for (i = 0; i < queue->limit; i++) {
  441. bcn_priv = queue->entries[i].priv_data;
  442. usb_kill_urb(bcn_priv->guardian_urb);
  443. usb_free_urb(bcn_priv->guardian_urb);
  444. }
  445. }
  446. int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
  447. {
  448. struct data_queue *queue;
  449. int status;
  450. /*
  451. * Find endpoints for each queue
  452. */
  453. status = rt2x00usb_find_endpoints(rt2x00dev);
  454. if (status)
  455. goto exit;
  456. /*
  457. * Allocate DMA
  458. */
  459. queue_for_each(rt2x00dev, queue) {
  460. status = rt2x00usb_alloc_urb(rt2x00dev, queue);
  461. if (status)
  462. goto exit;
  463. }
  464. return 0;
  465. exit:
  466. rt2x00usb_uninitialize(rt2x00dev);
  467. return status;
  468. }
  469. EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
  470. void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
  471. {
  472. struct data_queue *queue;
  473. queue_for_each(rt2x00dev, queue)
  474. rt2x00usb_free_urb(rt2x00dev, queue);
  475. }
  476. EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
  477. /*
  478. * USB driver handlers.
  479. */
  480. static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
  481. {
  482. kfree(rt2x00dev->rf);
  483. rt2x00dev->rf = NULL;
  484. kfree(rt2x00dev->eeprom);
  485. rt2x00dev->eeprom = NULL;
  486. kfree(rt2x00dev->csr.cache);
  487. rt2x00dev->csr.cache = NULL;
  488. }
  489. static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
  490. {
  491. rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
  492. if (!rt2x00dev->csr.cache)
  493. goto exit;
  494. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  495. if (!rt2x00dev->eeprom)
  496. goto exit;
  497. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  498. if (!rt2x00dev->rf)
  499. goto exit;
  500. return 0;
  501. exit:
  502. ERROR_PROBE("Failed to allocate registers.\n");
  503. rt2x00usb_free_reg(rt2x00dev);
  504. return -ENOMEM;
  505. }
  506. int rt2x00usb_probe(struct usb_interface *usb_intf,
  507. const struct usb_device_id *id)
  508. {
  509. struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
  510. struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_info;
  511. struct ieee80211_hw *hw;
  512. struct rt2x00_dev *rt2x00dev;
  513. int retval;
  514. usb_dev = usb_get_dev(usb_dev);
  515. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  516. if (!hw) {
  517. ERROR_PROBE("Failed to allocate hardware.\n");
  518. retval = -ENOMEM;
  519. goto exit_put_device;
  520. }
  521. usb_set_intfdata(usb_intf, hw);
  522. rt2x00dev = hw->priv;
  523. rt2x00dev->dev = &usb_intf->dev;
  524. rt2x00dev->ops = ops;
  525. rt2x00dev->hw = hw;
  526. retval = rt2x00usb_alloc_reg(rt2x00dev);
  527. if (retval)
  528. goto exit_free_device;
  529. retval = rt2x00lib_probe_dev(rt2x00dev);
  530. if (retval)
  531. goto exit_free_reg;
  532. return 0;
  533. exit_free_reg:
  534. rt2x00usb_free_reg(rt2x00dev);
  535. exit_free_device:
  536. ieee80211_free_hw(hw);
  537. exit_put_device:
  538. usb_put_dev(usb_dev);
  539. usb_set_intfdata(usb_intf, NULL);
  540. return retval;
  541. }
  542. EXPORT_SYMBOL_GPL(rt2x00usb_probe);
  543. void rt2x00usb_disconnect(struct usb_interface *usb_intf)
  544. {
  545. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  546. struct rt2x00_dev *rt2x00dev = hw->priv;
  547. /*
  548. * Free all allocated data.
  549. */
  550. rt2x00lib_remove_dev(rt2x00dev);
  551. rt2x00usb_free_reg(rt2x00dev);
  552. ieee80211_free_hw(hw);
  553. /*
  554. * Free the USB device data.
  555. */
  556. usb_set_intfdata(usb_intf, NULL);
  557. usb_put_dev(interface_to_usbdev(usb_intf));
  558. }
  559. EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
  560. #ifdef CONFIG_PM
  561. int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
  562. {
  563. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  564. struct rt2x00_dev *rt2x00dev = hw->priv;
  565. int retval;
  566. retval = rt2x00lib_suspend(rt2x00dev, state);
  567. if (retval)
  568. return retval;
  569. rt2x00usb_free_reg(rt2x00dev);
  570. /*
  571. * Decrease usbdev refcount.
  572. */
  573. usb_put_dev(interface_to_usbdev(usb_intf));
  574. return 0;
  575. }
  576. EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
  577. int rt2x00usb_resume(struct usb_interface *usb_intf)
  578. {
  579. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  580. struct rt2x00_dev *rt2x00dev = hw->priv;
  581. int retval;
  582. usb_get_dev(interface_to_usbdev(usb_intf));
  583. retval = rt2x00usb_alloc_reg(rt2x00dev);
  584. if (retval)
  585. return retval;
  586. retval = rt2x00lib_resume(rt2x00dev);
  587. if (retval)
  588. goto exit_free_reg;
  589. return 0;
  590. exit_free_reg:
  591. rt2x00usb_free_reg(rt2x00dev);
  592. return retval;
  593. }
  594. EXPORT_SYMBOL_GPL(rt2x00usb_resume);
  595. #endif /* CONFIG_PM */
  596. /*
  597. * rt2x00usb module information.
  598. */
  599. MODULE_AUTHOR(DRV_PROJECT);
  600. MODULE_VERSION(DRV_VERSION);
  601. MODULE_DESCRIPTION("rt2x00 usb library");
  602. MODULE_LICENSE("GPL");