rt2x00usb.c 19 KB

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