rt2x00usb.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  1. /*
  2. Copyright (C) 2010 Willow Garage <http://www.willowgarage.com>
  3. Copyright (C) 2004 - 2010 Ivo van Doorn <IvDoorn@gmail.com>
  4. <http://rt2x00.serialmonkey.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. Module: rt2x00usb
  18. Abstract: rt2x00 generic usb device routines.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/usb.h>
  24. #include <linux/bug.h>
  25. #include "rt2x00.h"
  26. #include "rt2x00usb.h"
  27. /*
  28. * Interfacing with the HW.
  29. */
  30. int rt2x00usb_vendor_request(struct rt2x00_dev *rt2x00dev,
  31. const u8 request, const u8 requesttype,
  32. const u16 offset, const u16 value,
  33. void *buffer, const u16 buffer_length,
  34. const int timeout)
  35. {
  36. struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
  37. int status;
  38. unsigned int i;
  39. unsigned int pipe =
  40. (requesttype == USB_VENDOR_REQUEST_IN) ?
  41. usb_rcvctrlpipe(usb_dev, 0) : usb_sndctrlpipe(usb_dev, 0);
  42. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  43. return -ENODEV;
  44. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  45. status = usb_control_msg(usb_dev, pipe, request, requesttype,
  46. value, offset, buffer, buffer_length,
  47. timeout);
  48. if (status >= 0)
  49. return 0;
  50. /*
  51. * Check for errors
  52. * -ENODEV: Device has disappeared, no point continuing.
  53. * All other errors: Try again.
  54. */
  55. else if (status == -ENODEV) {
  56. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  57. break;
  58. }
  59. }
  60. rt2x00_err(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_req_buff_lock(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. BUG_ON(!mutex_is_locked(&rt2x00dev->csr_mutex));
  73. /*
  74. * Check for Cache availability.
  75. */
  76. if (unlikely(!rt2x00dev->csr.cache || buffer_length > CSR_CACHE_SIZE)) {
  77. rt2x00_err(rt2x00dev, "CSR cache not available\n");
  78. return -ENOMEM;
  79. }
  80. if (requesttype == USB_VENDOR_REQUEST_OUT)
  81. memcpy(rt2x00dev->csr.cache, buffer, buffer_length);
  82. status = rt2x00usb_vendor_request(rt2x00dev, request, requesttype,
  83. offset, 0, rt2x00dev->csr.cache,
  84. buffer_length, timeout);
  85. if (!status && requesttype == USB_VENDOR_REQUEST_IN)
  86. memcpy(buffer, rt2x00dev->csr.cache, buffer_length);
  87. return status;
  88. }
  89. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_req_buff_lock);
  90. int rt2x00usb_vendor_request_buff(struct rt2x00_dev *rt2x00dev,
  91. const u8 request, const u8 requesttype,
  92. const u16 offset, void *buffer,
  93. const u16 buffer_length, const int timeout)
  94. {
  95. int status = 0;
  96. unsigned char *tb;
  97. u16 off, len, bsize;
  98. mutex_lock(&rt2x00dev->csr_mutex);
  99. tb = (char *)buffer;
  100. off = offset;
  101. len = buffer_length;
  102. while (len && !status) {
  103. bsize = min_t(u16, CSR_CACHE_SIZE, len);
  104. status = rt2x00usb_vendor_req_buff_lock(rt2x00dev, request,
  105. requesttype, off, tb,
  106. bsize, timeout);
  107. tb += bsize;
  108. len -= bsize;
  109. off += bsize;
  110. }
  111. mutex_unlock(&rt2x00dev->csr_mutex);
  112. return status;
  113. }
  114. EXPORT_SYMBOL_GPL(rt2x00usb_vendor_request_buff);
  115. int rt2x00usb_regbusy_read(struct rt2x00_dev *rt2x00dev,
  116. const unsigned int offset,
  117. const struct rt2x00_field32 field,
  118. u32 *reg)
  119. {
  120. unsigned int i;
  121. if (!test_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags))
  122. return -ENODEV;
  123. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  124. rt2x00usb_register_read_lock(rt2x00dev, offset, reg);
  125. if (!rt2x00_get_field32(*reg, field))
  126. return 1;
  127. udelay(REGISTER_BUSY_DELAY);
  128. }
  129. rt2x00_err(rt2x00dev, "Indirect register access failed: offset=0x%.08x, value=0x%.08x\n",
  130. offset, *reg);
  131. *reg = ~0;
  132. return 0;
  133. }
  134. EXPORT_SYMBOL_GPL(rt2x00usb_regbusy_read);
  135. struct rt2x00_async_read_data {
  136. __le32 reg;
  137. struct usb_ctrlrequest cr;
  138. struct rt2x00_dev *rt2x00dev;
  139. bool (*callback)(struct rt2x00_dev *, int, u32);
  140. };
  141. static void rt2x00usb_register_read_async_cb(struct urb *urb)
  142. {
  143. struct rt2x00_async_read_data *rd = urb->context;
  144. if (rd->callback(rd->rt2x00dev, urb->status, le32_to_cpu(rd->reg))) {
  145. if (usb_submit_urb(urb, GFP_ATOMIC) < 0)
  146. kfree(rd);
  147. } else
  148. kfree(rd);
  149. }
  150. void rt2x00usb_register_read_async(struct rt2x00_dev *rt2x00dev,
  151. const unsigned int offset,
  152. bool (*callback)(struct rt2x00_dev*, int, u32))
  153. {
  154. struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
  155. struct urb *urb;
  156. struct rt2x00_async_read_data *rd;
  157. rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
  158. if (!rd)
  159. return;
  160. urb = usb_alloc_urb(0, GFP_ATOMIC);
  161. if (!urb) {
  162. kfree(rd);
  163. return;
  164. }
  165. rd->rt2x00dev = rt2x00dev;
  166. rd->callback = callback;
  167. rd->cr.bRequestType = USB_VENDOR_REQUEST_IN;
  168. rd->cr.bRequest = USB_MULTI_READ;
  169. rd->cr.wValue = 0;
  170. rd->cr.wIndex = cpu_to_le16(offset);
  171. rd->cr.wLength = cpu_to_le16(sizeof(u32));
  172. usb_fill_control_urb(urb, usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  173. (unsigned char *)(&rd->cr), &rd->reg, sizeof(rd->reg),
  174. rt2x00usb_register_read_async_cb, rd);
  175. if (usb_submit_urb(urb, GFP_ATOMIC) < 0)
  176. kfree(rd);
  177. usb_free_urb(urb);
  178. }
  179. EXPORT_SYMBOL_GPL(rt2x00usb_register_read_async);
  180. /*
  181. * TX data handlers.
  182. */
  183. static void rt2x00usb_work_txdone_entry(struct queue_entry *entry)
  184. {
  185. /*
  186. * If the transfer to hardware succeeded, it does not mean the
  187. * frame was send out correctly. It only means the frame
  188. * was successfully pushed to the hardware, we have no
  189. * way to determine the transmission status right now.
  190. * (Only indirectly by looking at the failed TX counters
  191. * in the register).
  192. */
  193. if (test_bit(ENTRY_DATA_IO_FAILED, &entry->flags))
  194. rt2x00lib_txdone_noinfo(entry, TXDONE_FAILURE);
  195. else
  196. rt2x00lib_txdone_noinfo(entry, TXDONE_UNKNOWN);
  197. }
  198. static void rt2x00usb_work_txdone(struct work_struct *work)
  199. {
  200. struct rt2x00_dev *rt2x00dev =
  201. container_of(work, struct rt2x00_dev, txdone_work);
  202. struct data_queue *queue;
  203. struct queue_entry *entry;
  204. tx_queue_for_each(rt2x00dev, queue) {
  205. while (!rt2x00queue_empty(queue)) {
  206. entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  207. if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
  208. !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
  209. break;
  210. rt2x00usb_work_txdone_entry(entry);
  211. }
  212. }
  213. }
  214. static void rt2x00usb_interrupt_txdone(struct urb *urb)
  215. {
  216. struct queue_entry *entry = (struct queue_entry *)urb->context;
  217. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  218. if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  219. return;
  220. /*
  221. * Check if the frame was correctly uploaded
  222. */
  223. if (urb->status)
  224. set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
  225. /*
  226. * Report the frame as DMA done
  227. */
  228. rt2x00lib_dmadone(entry);
  229. if (rt2x00dev->ops->lib->tx_dma_done)
  230. rt2x00dev->ops->lib->tx_dma_done(entry);
  231. /*
  232. * Schedule the delayed work for reading the TX status
  233. * from the device.
  234. */
  235. if (!test_bit(REQUIRE_TXSTATUS_FIFO, &rt2x00dev->cap_flags) ||
  236. !kfifo_is_empty(&rt2x00dev->txstatus_fifo))
  237. queue_work(rt2x00dev->workqueue, &rt2x00dev->txdone_work);
  238. }
  239. static bool rt2x00usb_kick_tx_entry(struct queue_entry *entry, void *data)
  240. {
  241. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  242. struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
  243. struct queue_entry_priv_usb *entry_priv = entry->priv_data;
  244. u32 length;
  245. int status;
  246. if (!test_and_clear_bit(ENTRY_DATA_PENDING, &entry->flags) ||
  247. test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
  248. return false;
  249. /*
  250. * USB devices require certain padding at the end of each frame
  251. * and urb. Those paddings are not included in skbs. Pass entry
  252. * to the driver to determine what the overall length should be.
  253. */
  254. length = rt2x00dev->ops->lib->get_tx_data_len(entry);
  255. status = skb_padto(entry->skb, length);
  256. if (unlikely(status)) {
  257. /* TODO: report something more appropriate than IO_FAILED. */
  258. rt2x00_warn(rt2x00dev, "TX SKB padding error, out of memory\n");
  259. set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
  260. rt2x00lib_dmadone(entry);
  261. return false;
  262. }
  263. usb_fill_bulk_urb(entry_priv->urb, usb_dev,
  264. usb_sndbulkpipe(usb_dev, entry->queue->usb_endpoint),
  265. entry->skb->data, length,
  266. rt2x00usb_interrupt_txdone, entry);
  267. status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
  268. if (status) {
  269. if (status == -ENODEV)
  270. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  271. set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
  272. rt2x00lib_dmadone(entry);
  273. }
  274. return false;
  275. }
  276. /*
  277. * RX data handlers.
  278. */
  279. static void rt2x00usb_work_rxdone(struct work_struct *work)
  280. {
  281. struct rt2x00_dev *rt2x00dev =
  282. container_of(work, struct rt2x00_dev, rxdone_work);
  283. struct queue_entry *entry;
  284. struct skb_frame_desc *skbdesc;
  285. u8 rxd[32];
  286. while (!rt2x00queue_empty(rt2x00dev->rx)) {
  287. entry = rt2x00queue_get_entry(rt2x00dev->rx, Q_INDEX_DONE);
  288. if (test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
  289. !test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
  290. break;
  291. /*
  292. * Fill in desc fields of the skb descriptor
  293. */
  294. skbdesc = get_skb_frame_desc(entry->skb);
  295. skbdesc->desc = rxd;
  296. skbdesc->desc_len = entry->queue->desc_size;
  297. /*
  298. * Send the frame to rt2x00lib for further processing.
  299. */
  300. rt2x00lib_rxdone(entry, GFP_KERNEL);
  301. }
  302. }
  303. static void rt2x00usb_interrupt_rxdone(struct urb *urb)
  304. {
  305. struct queue_entry *entry = (struct queue_entry *)urb->context;
  306. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  307. if (!test_and_clear_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  308. return;
  309. /*
  310. * Report the frame as DMA done
  311. */
  312. rt2x00lib_dmadone(entry);
  313. /*
  314. * Check if the received data is simply too small
  315. * to be actually valid, or if the urb is signaling
  316. * a problem.
  317. */
  318. if (urb->actual_length < entry->queue->desc_size || urb->status)
  319. set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
  320. /*
  321. * Schedule the delayed work for reading the RX status
  322. * from the device.
  323. */
  324. queue_work(rt2x00dev->workqueue, &rt2x00dev->rxdone_work);
  325. }
  326. static bool rt2x00usb_kick_rx_entry(struct queue_entry *entry, void *data)
  327. {
  328. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  329. struct usb_device *usb_dev = to_usb_device_intf(rt2x00dev->dev);
  330. struct queue_entry_priv_usb *entry_priv = entry->priv_data;
  331. int status;
  332. if (test_and_set_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags) ||
  333. test_bit(ENTRY_DATA_STATUS_PENDING, &entry->flags))
  334. return false;
  335. rt2x00lib_dmastart(entry);
  336. usb_fill_bulk_urb(entry_priv->urb, usb_dev,
  337. usb_rcvbulkpipe(usb_dev, entry->queue->usb_endpoint),
  338. entry->skb->data, entry->skb->len,
  339. rt2x00usb_interrupt_rxdone, entry);
  340. status = usb_submit_urb(entry_priv->urb, GFP_ATOMIC);
  341. if (status) {
  342. if (status == -ENODEV)
  343. clear_bit(DEVICE_STATE_PRESENT, &rt2x00dev->flags);
  344. set_bit(ENTRY_DATA_IO_FAILED, &entry->flags);
  345. rt2x00lib_dmadone(entry);
  346. }
  347. return false;
  348. }
  349. void rt2x00usb_kick_queue(struct data_queue *queue)
  350. {
  351. switch (queue->qid) {
  352. case QID_AC_VO:
  353. case QID_AC_VI:
  354. case QID_AC_BE:
  355. case QID_AC_BK:
  356. if (!rt2x00queue_empty(queue))
  357. rt2x00queue_for_each_entry(queue,
  358. Q_INDEX_DONE,
  359. Q_INDEX,
  360. NULL,
  361. rt2x00usb_kick_tx_entry);
  362. break;
  363. case QID_RX:
  364. if (!rt2x00queue_full(queue))
  365. rt2x00queue_for_each_entry(queue,
  366. Q_INDEX,
  367. Q_INDEX_DONE,
  368. NULL,
  369. rt2x00usb_kick_rx_entry);
  370. break;
  371. default:
  372. break;
  373. }
  374. }
  375. EXPORT_SYMBOL_GPL(rt2x00usb_kick_queue);
  376. static bool rt2x00usb_flush_entry(struct queue_entry *entry, void *data)
  377. {
  378. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  379. struct queue_entry_priv_usb *entry_priv = entry->priv_data;
  380. struct queue_entry_priv_usb_bcn *bcn_priv = entry->priv_data;
  381. if (!test_bit(ENTRY_OWNER_DEVICE_DATA, &entry->flags))
  382. return false;
  383. usb_kill_urb(entry_priv->urb);
  384. /*
  385. * Kill guardian urb (if required by driver).
  386. */
  387. if ((entry->queue->qid == QID_BEACON) &&
  388. (test_bit(REQUIRE_BEACON_GUARD, &rt2x00dev->cap_flags)))
  389. usb_kill_urb(bcn_priv->guardian_urb);
  390. return false;
  391. }
  392. void rt2x00usb_flush_queue(struct data_queue *queue, bool drop)
  393. {
  394. struct work_struct *completion;
  395. unsigned int i;
  396. if (drop)
  397. rt2x00queue_for_each_entry(queue, Q_INDEX_DONE, Q_INDEX, NULL,
  398. rt2x00usb_flush_entry);
  399. /*
  400. * Obtain the queue completion handler
  401. */
  402. switch (queue->qid) {
  403. case QID_AC_VO:
  404. case QID_AC_VI:
  405. case QID_AC_BE:
  406. case QID_AC_BK:
  407. completion = &queue->rt2x00dev->txdone_work;
  408. break;
  409. case QID_RX:
  410. completion = &queue->rt2x00dev->rxdone_work;
  411. break;
  412. default:
  413. return;
  414. }
  415. for (i = 0; i < 10; i++) {
  416. /*
  417. * Check if the driver is already done, otherwise we
  418. * have to sleep a little while to give the driver/hw
  419. * the oppurtunity to complete interrupt process itself.
  420. */
  421. if (rt2x00queue_empty(queue))
  422. break;
  423. /*
  424. * Schedule the completion handler manually, when this
  425. * worker function runs, it should cleanup the queue.
  426. */
  427. queue_work(queue->rt2x00dev->workqueue, completion);
  428. /*
  429. * Wait for a little while to give the driver
  430. * the oppurtunity to recover itself.
  431. */
  432. msleep(10);
  433. }
  434. }
  435. EXPORT_SYMBOL_GPL(rt2x00usb_flush_queue);
  436. static void rt2x00usb_watchdog_tx_dma(struct data_queue *queue)
  437. {
  438. rt2x00_warn(queue->rt2x00dev, "TX queue %d DMA timed out, invoke forced forced reset\n",
  439. queue->qid);
  440. rt2x00queue_stop_queue(queue);
  441. rt2x00queue_flush_queue(queue, true);
  442. rt2x00queue_start_queue(queue);
  443. }
  444. static int rt2x00usb_dma_timeout(struct data_queue *queue)
  445. {
  446. struct queue_entry *entry;
  447. entry = rt2x00queue_get_entry(queue, Q_INDEX_DMA_DONE);
  448. return rt2x00queue_dma_timeout(entry);
  449. }
  450. void rt2x00usb_watchdog(struct rt2x00_dev *rt2x00dev)
  451. {
  452. struct data_queue *queue;
  453. tx_queue_for_each(rt2x00dev, queue) {
  454. if (!rt2x00queue_empty(queue)) {
  455. if (rt2x00usb_dma_timeout(queue))
  456. rt2x00usb_watchdog_tx_dma(queue);
  457. }
  458. }
  459. }
  460. EXPORT_SYMBOL_GPL(rt2x00usb_watchdog);
  461. /*
  462. * Radio handlers
  463. */
  464. void rt2x00usb_disable_radio(struct rt2x00_dev *rt2x00dev)
  465. {
  466. rt2x00usb_vendor_request_sw(rt2x00dev, USB_RX_CONTROL, 0, 0,
  467. REGISTER_TIMEOUT);
  468. }
  469. EXPORT_SYMBOL_GPL(rt2x00usb_disable_radio);
  470. /*
  471. * Device initialization handlers.
  472. */
  473. void rt2x00usb_clear_entry(struct queue_entry *entry)
  474. {
  475. entry->flags = 0;
  476. if (entry->queue->qid == QID_RX)
  477. rt2x00usb_kick_rx_entry(entry, NULL);
  478. }
  479. EXPORT_SYMBOL_GPL(rt2x00usb_clear_entry);
  480. static void rt2x00usb_assign_endpoint(struct data_queue *queue,
  481. struct usb_endpoint_descriptor *ep_desc)
  482. {
  483. struct usb_device *usb_dev = to_usb_device_intf(queue->rt2x00dev->dev);
  484. int pipe;
  485. queue->usb_endpoint = usb_endpoint_num(ep_desc);
  486. if (queue->qid == QID_RX) {
  487. pipe = usb_rcvbulkpipe(usb_dev, queue->usb_endpoint);
  488. queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 0);
  489. } else {
  490. pipe = usb_sndbulkpipe(usb_dev, queue->usb_endpoint);
  491. queue->usb_maxpacket = usb_maxpacket(usb_dev, pipe, 1);
  492. }
  493. if (!queue->usb_maxpacket)
  494. queue->usb_maxpacket = 1;
  495. }
  496. static int rt2x00usb_find_endpoints(struct rt2x00_dev *rt2x00dev)
  497. {
  498. struct usb_interface *intf = to_usb_interface(rt2x00dev->dev);
  499. struct usb_host_interface *intf_desc = intf->cur_altsetting;
  500. struct usb_endpoint_descriptor *ep_desc;
  501. struct data_queue *queue = rt2x00dev->tx;
  502. struct usb_endpoint_descriptor *tx_ep_desc = NULL;
  503. unsigned int i;
  504. /*
  505. * Walk through all available endpoints to search for "bulk in"
  506. * and "bulk out" endpoints. When we find such endpoints collect
  507. * the information we need from the descriptor and assign it
  508. * to the queue.
  509. */
  510. for (i = 0; i < intf_desc->desc.bNumEndpoints; i++) {
  511. ep_desc = &intf_desc->endpoint[i].desc;
  512. if (usb_endpoint_is_bulk_in(ep_desc)) {
  513. rt2x00usb_assign_endpoint(rt2x00dev->rx, ep_desc);
  514. } else if (usb_endpoint_is_bulk_out(ep_desc) &&
  515. (queue != queue_end(rt2x00dev))) {
  516. rt2x00usb_assign_endpoint(queue, ep_desc);
  517. queue = queue_next(queue);
  518. tx_ep_desc = ep_desc;
  519. }
  520. }
  521. /*
  522. * At least 1 endpoint for RX and 1 endpoint for TX must be available.
  523. */
  524. if (!rt2x00dev->rx->usb_endpoint || !rt2x00dev->tx->usb_endpoint) {
  525. rt2x00_err(rt2x00dev, "Bulk-in/Bulk-out endpoints not found\n");
  526. return -EPIPE;
  527. }
  528. /*
  529. * It might be possible not all queues have a dedicated endpoint.
  530. * Loop through all TX queues and copy the endpoint information
  531. * which we have gathered from already assigned endpoints.
  532. */
  533. txall_queue_for_each(rt2x00dev, queue) {
  534. if (!queue->usb_endpoint)
  535. rt2x00usb_assign_endpoint(queue, tx_ep_desc);
  536. }
  537. return 0;
  538. }
  539. static int rt2x00usb_alloc_entries(struct data_queue *queue)
  540. {
  541. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  542. struct queue_entry_priv_usb *entry_priv;
  543. struct queue_entry_priv_usb_bcn *bcn_priv;
  544. unsigned int i;
  545. for (i = 0; i < queue->limit; i++) {
  546. entry_priv = queue->entries[i].priv_data;
  547. entry_priv->urb = usb_alloc_urb(0, GFP_KERNEL);
  548. if (!entry_priv->urb)
  549. return -ENOMEM;
  550. }
  551. /*
  552. * If this is not the beacon queue or
  553. * no guardian byte was required for the beacon,
  554. * then we are done.
  555. */
  556. if (queue->qid != QID_BEACON ||
  557. !test_bit(REQUIRE_BEACON_GUARD, &rt2x00dev->cap_flags))
  558. return 0;
  559. for (i = 0; i < queue->limit; i++) {
  560. bcn_priv = queue->entries[i].priv_data;
  561. bcn_priv->guardian_urb = usb_alloc_urb(0, GFP_KERNEL);
  562. if (!bcn_priv->guardian_urb)
  563. return -ENOMEM;
  564. }
  565. return 0;
  566. }
  567. static void rt2x00usb_free_entries(struct data_queue *queue)
  568. {
  569. struct rt2x00_dev *rt2x00dev = queue->rt2x00dev;
  570. struct queue_entry_priv_usb *entry_priv;
  571. struct queue_entry_priv_usb_bcn *bcn_priv;
  572. unsigned int i;
  573. if (!queue->entries)
  574. return;
  575. for (i = 0; i < queue->limit; i++) {
  576. entry_priv = queue->entries[i].priv_data;
  577. usb_kill_urb(entry_priv->urb);
  578. usb_free_urb(entry_priv->urb);
  579. }
  580. /*
  581. * If this is not the beacon queue or
  582. * no guardian byte was required for the beacon,
  583. * then we are done.
  584. */
  585. if (queue->qid != QID_BEACON ||
  586. !test_bit(REQUIRE_BEACON_GUARD, &rt2x00dev->cap_flags))
  587. return;
  588. for (i = 0; i < queue->limit; i++) {
  589. bcn_priv = queue->entries[i].priv_data;
  590. usb_kill_urb(bcn_priv->guardian_urb);
  591. usb_free_urb(bcn_priv->guardian_urb);
  592. }
  593. }
  594. int rt2x00usb_initialize(struct rt2x00_dev *rt2x00dev)
  595. {
  596. struct data_queue *queue;
  597. int status;
  598. /*
  599. * Find endpoints for each queue
  600. */
  601. status = rt2x00usb_find_endpoints(rt2x00dev);
  602. if (status)
  603. goto exit;
  604. /*
  605. * Allocate DMA
  606. */
  607. queue_for_each(rt2x00dev, queue) {
  608. status = rt2x00usb_alloc_entries(queue);
  609. if (status)
  610. goto exit;
  611. }
  612. return 0;
  613. exit:
  614. rt2x00usb_uninitialize(rt2x00dev);
  615. return status;
  616. }
  617. EXPORT_SYMBOL_GPL(rt2x00usb_initialize);
  618. void rt2x00usb_uninitialize(struct rt2x00_dev *rt2x00dev)
  619. {
  620. struct data_queue *queue;
  621. queue_for_each(rt2x00dev, queue)
  622. rt2x00usb_free_entries(queue);
  623. }
  624. EXPORT_SYMBOL_GPL(rt2x00usb_uninitialize);
  625. /*
  626. * USB driver handlers.
  627. */
  628. static void rt2x00usb_free_reg(struct rt2x00_dev *rt2x00dev)
  629. {
  630. kfree(rt2x00dev->rf);
  631. rt2x00dev->rf = NULL;
  632. kfree(rt2x00dev->eeprom);
  633. rt2x00dev->eeprom = NULL;
  634. kfree(rt2x00dev->csr.cache);
  635. rt2x00dev->csr.cache = NULL;
  636. }
  637. static int rt2x00usb_alloc_reg(struct rt2x00_dev *rt2x00dev)
  638. {
  639. rt2x00dev->csr.cache = kzalloc(CSR_CACHE_SIZE, GFP_KERNEL);
  640. if (!rt2x00dev->csr.cache)
  641. goto exit;
  642. rt2x00dev->eeprom = kzalloc(rt2x00dev->ops->eeprom_size, GFP_KERNEL);
  643. if (!rt2x00dev->eeprom)
  644. goto exit;
  645. rt2x00dev->rf = kzalloc(rt2x00dev->ops->rf_size, GFP_KERNEL);
  646. if (!rt2x00dev->rf)
  647. goto exit;
  648. return 0;
  649. exit:
  650. rt2x00_probe_err("Failed to allocate registers\n");
  651. rt2x00usb_free_reg(rt2x00dev);
  652. return -ENOMEM;
  653. }
  654. int rt2x00usb_probe(struct usb_interface *usb_intf,
  655. const struct rt2x00_ops *ops)
  656. {
  657. struct usb_device *usb_dev = interface_to_usbdev(usb_intf);
  658. struct ieee80211_hw *hw;
  659. struct rt2x00_dev *rt2x00dev;
  660. int retval;
  661. usb_dev = usb_get_dev(usb_dev);
  662. usb_reset_device(usb_dev);
  663. hw = ieee80211_alloc_hw(sizeof(struct rt2x00_dev), ops->hw);
  664. if (!hw) {
  665. rt2x00_probe_err("Failed to allocate hardware\n");
  666. retval = -ENOMEM;
  667. goto exit_put_device;
  668. }
  669. usb_set_intfdata(usb_intf, hw);
  670. rt2x00dev = hw->priv;
  671. rt2x00dev->dev = &usb_intf->dev;
  672. rt2x00dev->ops = ops;
  673. rt2x00dev->hw = hw;
  674. rt2x00_set_chip_intf(rt2x00dev, RT2X00_CHIP_INTF_USB);
  675. INIT_WORK(&rt2x00dev->rxdone_work, rt2x00usb_work_rxdone);
  676. INIT_WORK(&rt2x00dev->txdone_work, rt2x00usb_work_txdone);
  677. hrtimer_init(&rt2x00dev->txstatus_timer, CLOCK_MONOTONIC,
  678. HRTIMER_MODE_REL);
  679. retval = rt2x00usb_alloc_reg(rt2x00dev);
  680. if (retval)
  681. goto exit_free_device;
  682. retval = rt2x00lib_probe_dev(rt2x00dev);
  683. if (retval)
  684. goto exit_free_reg;
  685. return 0;
  686. exit_free_reg:
  687. rt2x00usb_free_reg(rt2x00dev);
  688. exit_free_device:
  689. ieee80211_free_hw(hw);
  690. exit_put_device:
  691. usb_put_dev(usb_dev);
  692. usb_set_intfdata(usb_intf, NULL);
  693. return retval;
  694. }
  695. EXPORT_SYMBOL_GPL(rt2x00usb_probe);
  696. void rt2x00usb_disconnect(struct usb_interface *usb_intf)
  697. {
  698. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  699. struct rt2x00_dev *rt2x00dev = hw->priv;
  700. /*
  701. * Free all allocated data.
  702. */
  703. rt2x00lib_remove_dev(rt2x00dev);
  704. rt2x00usb_free_reg(rt2x00dev);
  705. ieee80211_free_hw(hw);
  706. /*
  707. * Free the USB device data.
  708. */
  709. usb_set_intfdata(usb_intf, NULL);
  710. usb_put_dev(interface_to_usbdev(usb_intf));
  711. }
  712. EXPORT_SYMBOL_GPL(rt2x00usb_disconnect);
  713. #ifdef CONFIG_PM
  714. int rt2x00usb_suspend(struct usb_interface *usb_intf, pm_message_t state)
  715. {
  716. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  717. struct rt2x00_dev *rt2x00dev = hw->priv;
  718. return rt2x00lib_suspend(rt2x00dev, state);
  719. }
  720. EXPORT_SYMBOL_GPL(rt2x00usb_suspend);
  721. int rt2x00usb_resume(struct usb_interface *usb_intf)
  722. {
  723. struct ieee80211_hw *hw = usb_get_intfdata(usb_intf);
  724. struct rt2x00_dev *rt2x00dev = hw->priv;
  725. return rt2x00lib_resume(rt2x00dev);
  726. }
  727. EXPORT_SYMBOL_GPL(rt2x00usb_resume);
  728. #endif /* CONFIG_PM */
  729. /*
  730. * rt2x00usb module information.
  731. */
  732. MODULE_AUTHOR(DRV_PROJECT);
  733. MODULE_VERSION(DRV_VERSION);
  734. MODULE_DESCRIPTION("rt2x00 usb library");
  735. MODULE_LICENSE("GPL");