rt2x00usb.c 18 KB

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