rt2x00usb.c 22 KB

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