rt2x00usb.c 22 KB

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