rt2x00usb.c 18 KB

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