rt2x00usb.c 18 KB

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