rt2x00usb.c 22 KB

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