rt2x00usb.c 15 KB

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