rt2x00usb.c 23 KB

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