rt2x00usb.c 14 KB

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