rt2x00usb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. /*
  2. Copyright (C) 2004 - 2007 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. /*
  22. * Set enviroment defines for rt2x00.h
  23. */
  24. #define DRV_NAME "rt2x00usb"
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/usb.h>
  28. #include <linux/bug.h>
  29. #include "rt2x00.h"
  30. #include "rt2x00usb.h"
  31. /*
  32. * Interfacing with the HW.
  33. */
  34. int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
  35. const u8 request, const u8 requesttype,
  36. const u16 offset, const u16 value,
  37. void *buffer, const u16 buffer_length,
  38. const int timeout)
  39. {
  40. struct usb_device *usb_dev =
  41. interface_to_usbdev(rt2x00dev_usb(rt2x00dev));
  42. int status;
  43. unsigned int i;
  44. unsigned int pipe =
  45. (requesttype == USB_VENDOR_REQUEST_IN) ?
  46. usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
  47. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  48. status = usb_control_msg(usb_dev, pipe, request, requesttype,
  49. value, offset, buffer, buffer_length,
  50. timeout);
  51. if (status >= 0)
  52. return 0;
  53. /*
  54. * Check for errors
  55. * -ENODEV: Device has disappeared, no point continuing.
  56. * All other errors: Try again.
  57. */
  58. else if (status == -ENODEV)
  59. break;
  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->usb_cache_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->usb_cache_mutex);
  98. status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
  99. requesttype, offset, buffer,
  100. buffer_length, timeout);
  101. mutex_unlock(&rt2x00dev->usb_cache_mutex);
  102. return status;
  103. }
  104. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
  105. /*
  106. * TX data handlers.
  107. */
  108. static void rt2x00usb_interrupt_txdone(struct urb *urb)
  109. {
  110. struct data_entry *entry = (struct data_entry *)urb->context;
  111. struct data_ring *ring = entry->ring;
  112. struct rt2x00_dev *rt2x00dev = ring->rt2x00dev;
  113. __le32 *txd = (__le32 *)entry->skb->data;
  114. u32 word;
  115. int tx_status;
  116. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
  117. !__test_and_clear_bit(ENTRY_OWNER_NIC, &entry->flags))
  118. return;
  119. rt2x00_desc_read(txd, 0, &word);
  120. /*
  121. * Remove the descriptor data from the buffer.
  122. */
  123. skb_pull(entry->skb, ring->desc_size);
  124. /*
  125. * Obtain the status about this packet.
  126. */
  127. tx_status = !urb->status ? TX_SUCCESS : TX_FAIL_RETRY;
  128. rt2x00lib_txdone(entry, tx_status, 0);
  129. /*
  130. * Make this entry available for reuse.
  131. */
  132. entry->flags = 0;
  133. rt2x00_ring_index_done_inc(entry->ring);
  134. /*
  135. * If the data ring was full before the txdone handler
  136. * we must make sure the packet queue in the mac80211 stack
  137. * is reenabled when the txdone handler has finished.
  138. */
  139. if (!rt2x00_ring_full(ring))
  140. ieee80211_wake_queue(rt2x00dev->hw,
  141. entry->tx_status.control.queue);
  142. }
  143. int rt2x00usb_write_tx_data(struct rt2x00_dev *rt2x00dev,
  144. struct data_ring *ring, struct sk_buff *skb,
  145. struct ieee80211_tx_control *control)
  146. {
  147. struct usb_device *usb_dev =
  148. interface_to_usbdev(rt2x00dev_usb(rt2x00dev));
  149. struct data_entry *entry = rt2x00_get_data_entry(ring);
  150. int pipe = usb_sndbulkpipe(usb_dev, 1);
  151. u32 length;
  152. if (rt2x00_ring_full(ring)) {
  153. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  154. return -EINVAL;
  155. }
  156. if (test_bit(ENTRY_OWNER_NIC, &entry->flags)) {
  157. ERROR(rt2x00dev,
  158. "Arrived at non-free entry in the non-full queue %d.\n"
  159. "Please file bug report to %s.\n",
  160. control->queue, DRV_PROJECT);
  161. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  162. return -EINVAL;
  163. }
  164. /*
  165. * Add the descriptor in front of the skb.
  166. */
  167. skb_push(skb, ring->desc_size);
  168. memset(skb->data, 0, ring->desc_size);
  169. rt2x00lib_write_tx_desc(rt2x00dev, (__le32 *)skb->data,
  170. (struct ieee80211_hdr *)(skb->data +
  171. ring->desc_size),
  172. skb->len - ring->desc_size, control);
  173. memcpy(&entry->tx_status.control, control, sizeof(*control));
  174. entry->skb = skb;
  175. /*
  176. * USB devices cannot blindly pass the skb->len as the
  177. * length of the data to usb_fill_bulk_urb. Pass the skb
  178. * to the driver to determine what the length should be.
  179. */
  180. length = rt2x00dev->ops->lib->get_tx_data_len(rt2x00dev, skb);
  181. /*
  182. * Initialize URB and send the frame to the device.
  183. */
  184. __set_bit(ENTRY_OWNER_NIC, &entry->flags);
  185. usb_fill_bulk_urb(entry->priv, usb_dev, pipe,
  186. skb->data, length, rt2x00usb_interrupt_txdone, entry);
  187. usb_submit_urb(entry->priv, GFP_ATOMIC);
  188. rt2x00_ring_index_inc(ring);
  189. if (rt2x00_ring_full(ring))
  190. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(rt2x00usb_write_tx_data);
  194. /*
  195. * RX data handlers.
  196. */
  197. static void rt2x00usb_interrupt_rxdone(struct urb *urb)
  198. {
  199. struct data_entry *entry = (struct data_entry *)urb->context;
  200. struct data_ring *ring = entry->ring;
  201. struct rt2x00_dev *rt2x00dev = ring->rt2x00dev;
  202. struct sk_buff *skb;
  203. struct ieee80211_hdr *hdr;
  204. struct rxdata_entry_desc desc;
  205. int header_size;
  206. int frame_size;
  207. if (!test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags) ||
  208. !test_and_clear_bit(ENTRY_OWNER_NIC, &entry->flags))
  209. return;
  210. /*
  211. * Check if the received data is simply too small
  212. * to be actually valid, or if the urb is signaling
  213. * a problem.
  214. */
  215. if (urb->actual_length < entry->ring->desc_size || urb->status)
  216. goto skip_entry;
  217. memset(&desc, 0x00, sizeof(desc));
  218. rt2x00dev->ops->lib->fill_rxdone(entry, &desc);
  219. /*
  220. * Allocate a new sk buffer to replace the current one.
  221. * If allocation fails, we should drop the current frame
  222. * so we can recycle the existing sk buffer for the new frame.
  223. * As alignment we use 2 and not NET_IP_ALIGN because we need
  224. * to be sure we have 2 bytes room in the head. (NET_IP_ALIGN
  225. * can be 0 on some hardware). We use these 2 bytes for frame
  226. * alignment later, we assume that the chance that
  227. * header_size % 4 == 2 is bigger then header_size % 2 == 0
  228. * and thus optimize alignment by reserving the 2 bytes in
  229. * advance.
  230. */
  231. frame_size = entry->ring->data_size + entry->ring->desc_size;
  232. skb = dev_alloc_skb(frame_size + 2);
  233. if (!skb)
  234. goto skip_entry;
  235. skb_reserve(skb, 2);
  236. skb_put(skb, frame_size);
  237. /*
  238. * The data behind the ieee80211 header must be
  239. * aligned on a 4 byte boundary.
  240. * After that trim the entire buffer down to only
  241. * contain the valid frame data excluding the device
  242. * descriptor.
  243. */
  244. hdr = (struct ieee80211_hdr *)entry->skb->data;
  245. header_size =
  246. ieee80211_get_hdrlen(le16_to_cpu(hdr->frame_control));
  247. if (header_size % 4 == 0) {
  248. skb_push(entry->skb, 2);
  249. memmove(entry->skb->data, entry->skb->data + 2, skb->len - 2);
  250. }
  251. skb_trim(entry->skb, desc.size);
  252. /*
  253. * Send the frame to rt2x00lib for further processing.
  254. */
  255. rt2x00lib_rxdone(entry, entry->skb, &desc);
  256. /*
  257. * Replace current entry's skb with the newly allocated one,
  258. * and reinitialize the urb.
  259. */
  260. entry->skb = skb;
  261. urb->transfer_buffer = entry->skb->data;
  262. urb->transfer_buffer_length = entry->skb->len;
  263. skip_entry:
  264. if (test_bit(DEVICE_ENABLED_RADIO, &ring->rt2x00dev->flags)) {
  265. __set_bit(ENTRY_OWNER_NIC, &entry->flags);
  266. usb_submit_urb(urb, GFP_ATOMIC);
  267. }
  268. rt2x00_ring_index_inc(ring);
  269. }
  270. /*
  271. * Radio handlers
  272. */
  273. void rt2x00usb_enable_radio(struct rt2x00_dev *rt2x00dev)
  274. {
  275. struct usb_device *usb_dev =
  276. interface_to_usbdev(rt2x00dev_usb(rt2x00dev));
  277. struct data_ring *ring;
  278. struct data_entry *entry;
  279. unsigned int i;
  280. /*
  281. * Initialize the TX rings
  282. */
  283. txringall_for_each(rt2x00dev, ring) {
  284. for (i = 0; i < ring->stats.limit; i++)
  285. ring->entry[i].flags = 0;
  286. rt2x00_ring_index_clear(ring);
  287. }
  288. /*
  289. * Initialize and start the RX ring.
  290. */
  291. rt2x00_ring_index_clear(rt2x00dev->rx);
  292. for (i = 0; i < rt2x00dev->rx->stats.limit; i++) {
  293. entry = &rt2x00dev->rx->entry[i];
  294. usb_fill_bulk_urb(entry->priv, usb_dev,
  295. usb_rcvbulkpipe(usb_dev, 1),
  296. entry->skb->data, entry->skb->len,
  297. rt2x00usb_interrupt_rxdone, entry);
  298. __set_bit(ENTRY_OWNER_NIC, &entry->flags);
  299. usb_submit_urb(entry->priv, GFP_ATOMIC);
  300. }
  301. }
  302. EXPORT_SYMBOL_GPL(rt2x00usb_enable_radio);
  303. void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
  304. {
  305. struct data_ring *ring;
  306. unsigned int i;
  307. rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0x0000, 0x0000,
  308. REGISTER_TIMEOUT);
  309. /*
  310. * Cancel all rings.
  311. */
  312. ring_for_each(rt2x00dev, ring) {
  313. for (i = 0; i < ring->stats.limit; i++)
  314. usb_kill_urb(ring->entry[i].priv);
  315. }
  316. }
  317. EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
  318. /*
  319. * Device initialization handlers.
  320. */
  321. static int rt2x00usb_alloc_urb(struct rt2x00_dev *rt2x00dev,
  322. struct data_ring *ring)
  323. {
  324. unsigned int i;
  325. /*
  326. * Allocate the URB's
  327. */
  328. for (i = 0; i < ring->stats.limit; i++) {
  329. ring->entry[i].priv = usb_alloc_urb(0, GFP_KERNEL);
  330. if (!ring->entry[i].priv)
  331. return -ENOMEM;
  332. }
  333. return 0;
  334. }
  335. static void rt2x00usb_free_urb(struct rt2x00_dev *rt2x00dev,
  336. struct data_ring *ring)
  337. {
  338. unsigned int i;
  339. if (!ring->entry)
  340. return;
  341. for (i = 0; i < ring->stats.limit; i++) {
  342. usb_kill_urb(ring->entry[i].priv);
  343. usb_free_urb(ring->entry[i].priv);
  344. if (ring->entry[i].skb)
  345. kfree_skb(ring->entry[i].skb);
  346. }
  347. }
  348. int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
  349. {
  350. struct data_ring *ring;
  351. struct sk_buff *skb;
  352. unsigned int entry_size;
  353. unsigned int i;
  354. int status;
  355. /*
  356. * Allocate DMA
  357. */
  358. ring_for_each(rt2x00dev, ring) {
  359. status = rt2x00usb_alloc_urb(rt2x00dev, ring);
  360. if (status)
  361. goto exit;
  362. }
  363. /*
  364. * For the RX ring, skb's should be allocated.
  365. */
  366. entry_size = rt2x00dev->rx->data_size + rt2x00dev->rx->desc_size;
  367. for (i = 0; i < rt2x00dev->rx->stats.limit; i++) {
  368. skb = dev_alloc_skb(NET_IP_ALIGN + entry_size);
  369. if (!skb)
  370. goto exit;
  371. skb_reserve(skb, NET_IP_ALIGN);
  372. skb_put(skb, entry_size);
  373. rt2x00dev->rx->entry[i].skb = skb;
  374. }
  375. return 0;
  376. exit:
  377. rt2x00usb_uninitialize(rt2x00dev);
  378. return status;
  379. }
  380. EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
  381. void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
  382. {
  383. struct data_ring *ring;
  384. ring_for_each(rt2x00dev, ring)
  385. rt2x00usb_free_urb(rt2x00dev, ring);
  386. }
  387. EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
  388. /*
  389. * USB driver handlers.
  390. */
  391. static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
  392. {
  393. kfree(rt2x00dev->rf);
  394. rt2x00dev->rf = NULL;
  395. kfree(rt2x00dev->eeprom);
  396. rt2x00dev->eeprom = NULL;
  397. kfree(rt2x00dev->csr_cache);
  398. rt2x00dev->csr_cache = NULL;
  399. }
  400. static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
  401. {
  402. rt2x00dev->csr_cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
  403. if (!rt2x00dev->csr_cache)
  404. goto exit;
  405. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  406. if (!rt2x00dev->eeprom)
  407. goto exit;
  408. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  409. if (!rt2x00dev->rf)
  410. goto exit;
  411. return 0;
  412. exit:
  413. ERROR_PROBE("Failed to allocate registers.\n");
  414. rt2x00usb_free_reg(rt2x00dev);
  415. return -ENOMEM;
  416. }
  417. int rt2x00usb_probe(struct usb_interface *usb_intf,
  418. const struct usb_device_id *id)
  419. {
  420. struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
  421. struct rt2x00_ops *ops = (struct rt2x00_ops *)id->driver_info;
  422. struct ieee80211_hw *hw;
  423. struct rt2x00_dev *rt2x00dev;
  424. int retval;
  425. usb_dev = usb_get_dev(usb_dev);
  426. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  427. if (!hw) {
  428. ERROR_PROBE("Failed to allocate hardware.\n");
  429. retval = -ENOMEM;
  430. goto exit_put_device;
  431. }
  432. usb_set_intfdata(usb_intf, hw);
  433. rt2x00dev = hw->priv;
  434. rt2x00dev->dev = usb_intf;
  435. rt2x00dev->ops = ops;
  436. rt2x00dev->hw = hw;
  437. mutex_init(&rt2x00dev->usb_cache_mutex);
  438. rt2x00dev->usb_maxpacket =
  439. usb_maxpacket(usb_dev, usb_sndbulkpipe(usb_dev, 1), 1);
  440. if (!rt2x00dev->usb_maxpacket)
  441. rt2x00dev->usb_maxpacket = 1;
  442. retval = rt2x00usb_alloc_reg(rt2x00dev);
  443. if (retval)
  444. goto exit_free_device;
  445. retval = rt2x00lib_probe_dev(rt2x00dev);
  446. if (retval)
  447. goto exit_free_reg;
  448. return 0;
  449. exit_free_reg:
  450. rt2x00usb_free_reg(rt2x00dev);
  451. exit_free_device:
  452. ieee80211_free_hw(hw);
  453. exit_put_device:
  454. usb_put_dev(usb_dev);
  455. usb_set_intfdata(usb_intf, NULL);
  456. return retval;
  457. }
  458. EXPORT_SYMBOL_GPL(rt2x00usb_probe);
  459. void rt2x00usb_disconnect(struct usb_interface *usb_intf)
  460. {
  461. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  462. struct rt2x00_dev *rt2x00dev = hw->priv;
  463. /*
  464. * Free all allocated data.
  465. */
  466. rt2x00lib_remove_dev(rt2x00dev);
  467. rt2x00usb_free_reg(rt2x00dev);
  468. ieee80211_free_hw(hw);
  469. /*
  470. * Free the USB device data.
  471. */
  472. usb_set_intfdata(usb_intf, NULL);
  473. usb_put_dev(interface_to_usbdev(usb_intf));
  474. }
  475. EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
  476. #ifdef CONFIG_PM
  477. int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
  478. {
  479. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  480. struct rt2x00_dev *rt2x00dev = hw->priv;
  481. int retval;
  482. retval = rt2x00lib_suspend(rt2x00dev, state);
  483. if (retval)
  484. return retval;
  485. rt2x00usb_free_reg(rt2x00dev);
  486. /*
  487. * Decrease usbdev refcount.
  488. */
  489. usb_put_dev(interface_to_usbdev(usb_intf));
  490. return 0;
  491. }
  492. EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
  493. int rt2x00usb_resume(struct usb_interface *usb_intf)
  494. {
  495. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  496. struct rt2x00_dev *rt2x00dev = hw->priv;
  497. int retval;
  498. usb_get_dev(interface_to_usbdev(usb_intf));
  499. retval = rt2x00usb_alloc_reg(rt2x00dev);
  500. if (retval)
  501. return retval;
  502. retval = rt2x00lib_resume(rt2x00dev);
  503. if (retval)
  504. goto exit_free_reg;
  505. return 0;
  506. exit_free_reg:
  507. rt2x00usb_free_reg(rt2x00dev);
  508. return retval;
  509. }
  510. EXPORT_SYMBOL_GPL(rt2x00usb_resume);
  511. #endif /* CONFIG_PM */
  512. /*
  513. * rt2x00pci module information.
  514. */
  515. MODULE_AUTHOR(DRV_PROJECT);
  516. MODULE_VERSION(DRV_VERSION);
  517. MODULE_DESCRIPTION("rt2x00 library");
  518. MODULE_LICENSE("GPL");