hif_usb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*
  2. * Copyright (c) 2010 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "htc.h"
  17. #define ATH9K_FW_USB_DEV(devid, fw) \
  18. { USB_DEVICE(0x0cf3, devid), .driver_info = (unsigned long) fw }
  19. static struct usb_device_id ath9k_hif_usb_ids[] = {
  20. ATH9K_FW_USB_DEV(0x9271, "ar9271.fw"),
  21. ATH9K_FW_USB_DEV(0x1006, "ar9271.fw"),
  22. { },
  23. };
  24. MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
  25. static int __hif_usb_tx(struct hif_device_usb *hif_dev);
  26. static void hif_usb_regout_cb(struct urb *urb)
  27. {
  28. struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
  29. switch (urb->status) {
  30. case 0:
  31. break;
  32. case -ENOENT:
  33. case -ECONNRESET:
  34. case -ENODEV:
  35. case -ESHUTDOWN:
  36. goto free;
  37. default:
  38. break;
  39. }
  40. if (cmd) {
  41. ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
  42. cmd->skb, 1);
  43. kfree(cmd);
  44. }
  45. return;
  46. free:
  47. kfree_skb(cmd->skb);
  48. kfree(cmd);
  49. }
  50. static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
  51. struct sk_buff *skb)
  52. {
  53. struct urb *urb;
  54. struct cmd_buf *cmd;
  55. int ret = 0;
  56. urb = usb_alloc_urb(0, GFP_KERNEL);
  57. if (urb == NULL)
  58. return -ENOMEM;
  59. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  60. if (cmd == NULL) {
  61. usb_free_urb(urb);
  62. return -ENOMEM;
  63. }
  64. cmd->skb = skb;
  65. cmd->hif_dev = hif_dev;
  66. usb_fill_int_urb(urb, hif_dev->udev,
  67. usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
  68. skb->data, skb->len,
  69. hif_usb_regout_cb, cmd, 1);
  70. usb_anchor_urb(urb, &hif_dev->regout_submitted);
  71. ret = usb_submit_urb(urb, GFP_KERNEL);
  72. if (ret) {
  73. usb_unanchor_urb(urb);
  74. kfree(cmd);
  75. }
  76. usb_free_urb(urb);
  77. return ret;
  78. }
  79. static void hif_usb_tx_cb(struct urb *urb)
  80. {
  81. struct tx_buf *tx_buf = (struct tx_buf *) urb->context;
  82. struct hif_device_usb *hif_dev = tx_buf->hif_dev;
  83. struct sk_buff *skb;
  84. bool drop, flush;
  85. if (!hif_dev)
  86. return;
  87. switch (urb->status) {
  88. case 0:
  89. break;
  90. case -ENOENT:
  91. case -ECONNRESET:
  92. break;
  93. case -ENODEV:
  94. case -ESHUTDOWN:
  95. return;
  96. default:
  97. break;
  98. }
  99. if (tx_buf) {
  100. spin_lock(&hif_dev->tx.tx_lock);
  101. drop = !!(hif_dev->tx.flags & HIF_USB_TX_STOP);
  102. flush = !!(hif_dev->tx.flags & HIF_USB_TX_FLUSH);
  103. spin_unlock(&hif_dev->tx.tx_lock);
  104. while ((skb = __skb_dequeue(&tx_buf->skb_queue)) != NULL) {
  105. if (!drop && !flush) {
  106. ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
  107. skb, 1);
  108. TX_STAT_INC(skb_completed);
  109. } else {
  110. dev_kfree_skb_any(skb);
  111. }
  112. }
  113. if (flush)
  114. return;
  115. tx_buf->len = tx_buf->offset = 0;
  116. __skb_queue_head_init(&tx_buf->skb_queue);
  117. spin_lock(&hif_dev->tx.tx_lock);
  118. list_del(&tx_buf->list);
  119. list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
  120. hif_dev->tx.tx_buf_cnt++;
  121. if (!drop)
  122. __hif_usb_tx(hif_dev); /* Check for pending SKBs */
  123. TX_STAT_INC(buf_completed);
  124. spin_unlock(&hif_dev->tx.tx_lock);
  125. }
  126. }
  127. /* TX lock has to be taken */
  128. static int __hif_usb_tx(struct hif_device_usb *hif_dev)
  129. {
  130. struct tx_buf *tx_buf = NULL;
  131. struct sk_buff *nskb = NULL;
  132. int ret = 0, i;
  133. u16 *hdr, tx_skb_cnt = 0;
  134. u8 *buf;
  135. if (hif_dev->tx.tx_skb_cnt == 0)
  136. return 0;
  137. /* Check if a free TX buffer is available */
  138. if (list_empty(&hif_dev->tx.tx_buf))
  139. return 0;
  140. tx_buf = list_first_entry(&hif_dev->tx.tx_buf, struct tx_buf, list);
  141. list_del(&tx_buf->list);
  142. list_add_tail(&tx_buf->list, &hif_dev->tx.tx_pending);
  143. hif_dev->tx.tx_buf_cnt--;
  144. tx_skb_cnt = min_t(u16, hif_dev->tx.tx_skb_cnt, MAX_TX_AGGR_NUM);
  145. for (i = 0; i < tx_skb_cnt; i++) {
  146. nskb = __skb_dequeue(&hif_dev->tx.tx_skb_queue);
  147. /* Should never be NULL */
  148. BUG_ON(!nskb);
  149. hif_dev->tx.tx_skb_cnt--;
  150. buf = tx_buf->buf;
  151. buf += tx_buf->offset;
  152. hdr = (u16 *)buf;
  153. *hdr++ = nskb->len;
  154. *hdr++ = ATH_USB_TX_STREAM_MODE_TAG;
  155. buf += 4;
  156. memcpy(buf, nskb->data, nskb->len);
  157. tx_buf->len = nskb->len + 4;
  158. if (i < (tx_skb_cnt - 1))
  159. tx_buf->offset += (((tx_buf->len - 1) / 4) + 1) * 4;
  160. if (i == (tx_skb_cnt - 1))
  161. tx_buf->len += tx_buf->offset;
  162. __skb_queue_tail(&tx_buf->skb_queue, nskb);
  163. TX_STAT_INC(skb_queued);
  164. }
  165. usb_fill_bulk_urb(tx_buf->urb, hif_dev->udev,
  166. usb_sndbulkpipe(hif_dev->udev, USB_WLAN_TX_PIPE),
  167. tx_buf->buf, tx_buf->len,
  168. hif_usb_tx_cb, tx_buf);
  169. ret = usb_submit_urb(tx_buf->urb, GFP_ATOMIC);
  170. if (ret) {
  171. tx_buf->len = tx_buf->offset = 0;
  172. __skb_queue_purge(&tx_buf->skb_queue);
  173. __skb_queue_head_init(&tx_buf->skb_queue);
  174. list_move_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
  175. hif_dev->tx.tx_buf_cnt++;
  176. }
  177. if (!ret)
  178. TX_STAT_INC(buf_queued);
  179. return ret;
  180. }
  181. static int hif_usb_send_tx(struct hif_device_usb *hif_dev, struct sk_buff *skb,
  182. struct ath9k_htc_tx_ctl *tx_ctl)
  183. {
  184. unsigned long flags;
  185. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  186. if (hif_dev->tx.flags & HIF_USB_TX_STOP) {
  187. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  188. return -ENODEV;
  189. }
  190. /* Check if the max queue count has been reached */
  191. if (hif_dev->tx.tx_skb_cnt > MAX_TX_BUF_NUM) {
  192. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  193. return -ENOMEM;
  194. }
  195. __skb_queue_tail(&hif_dev->tx.tx_skb_queue, skb);
  196. hif_dev->tx.tx_skb_cnt++;
  197. /* Send normal frames immediately */
  198. if (!tx_ctl || (tx_ctl && (tx_ctl->type == ATH9K_HTC_NORMAL)))
  199. __hif_usb_tx(hif_dev);
  200. /* Check if AMPDUs have to be sent immediately */
  201. if (tx_ctl && (tx_ctl->type == ATH9K_HTC_AMPDU) &&
  202. (hif_dev->tx.tx_buf_cnt == MAX_TX_URB_NUM) &&
  203. (hif_dev->tx.tx_skb_cnt < 2)) {
  204. __hif_usb_tx(hif_dev);
  205. }
  206. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  207. return 0;
  208. }
  209. static void hif_usb_start(void *hif_handle, u8 pipe_id)
  210. {
  211. struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
  212. unsigned long flags;
  213. hif_dev->flags |= HIF_USB_START;
  214. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  215. hif_dev->tx.flags &= ~HIF_USB_TX_STOP;
  216. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  217. }
  218. static void hif_usb_stop(void *hif_handle, u8 pipe_id)
  219. {
  220. struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
  221. unsigned long flags;
  222. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  223. __skb_queue_purge(&hif_dev->tx.tx_skb_queue);
  224. hif_dev->tx.tx_skb_cnt = 0;
  225. hif_dev->tx.flags |= HIF_USB_TX_STOP;
  226. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  227. }
  228. static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb,
  229. struct ath9k_htc_tx_ctl *tx_ctl)
  230. {
  231. struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
  232. int ret = 0;
  233. switch (pipe_id) {
  234. case USB_WLAN_TX_PIPE:
  235. ret = hif_usb_send_tx(hif_dev, skb, tx_ctl);
  236. break;
  237. case USB_REG_OUT_PIPE:
  238. ret = hif_usb_send_regout(hif_dev, skb);
  239. break;
  240. default:
  241. dev_err(&hif_dev->udev->dev,
  242. "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
  243. ret = -EINVAL;
  244. break;
  245. }
  246. return ret;
  247. }
  248. static struct ath9k_htc_hif hif_usb = {
  249. .transport = ATH9K_HIF_USB,
  250. .name = "ath9k_hif_usb",
  251. .control_ul_pipe = USB_REG_OUT_PIPE,
  252. .control_dl_pipe = USB_REG_IN_PIPE,
  253. .start = hif_usb_start,
  254. .stop = hif_usb_stop,
  255. .send = hif_usb_send,
  256. };
  257. static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
  258. struct sk_buff *skb)
  259. {
  260. struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
  261. int index = 0, i = 0, chk_idx, len = skb->len;
  262. int rx_remain_len = 0, rx_pkt_len = 0;
  263. u16 pkt_len, pkt_tag, pool_index = 0;
  264. u8 *ptr;
  265. spin_lock(&hif_dev->rx_lock);
  266. rx_remain_len = hif_dev->rx_remain_len;
  267. rx_pkt_len = hif_dev->rx_transfer_len;
  268. if (rx_remain_len != 0) {
  269. struct sk_buff *remain_skb = hif_dev->remain_skb;
  270. if (remain_skb) {
  271. ptr = (u8 *) remain_skb->data;
  272. index = rx_remain_len;
  273. rx_remain_len -= hif_dev->rx_pad_len;
  274. ptr += rx_pkt_len;
  275. memcpy(ptr, skb->data, rx_remain_len);
  276. rx_pkt_len += rx_remain_len;
  277. hif_dev->rx_remain_len = 0;
  278. skb_put(remain_skb, rx_pkt_len);
  279. skb_pool[pool_index++] = remain_skb;
  280. } else {
  281. index = rx_remain_len;
  282. }
  283. }
  284. spin_unlock(&hif_dev->rx_lock);
  285. while (index < len) {
  286. ptr = (u8 *) skb->data;
  287. pkt_len = ptr[index] + (ptr[index+1] << 8);
  288. pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
  289. if (pkt_tag == ATH_USB_RX_STREAM_MODE_TAG) {
  290. u16 pad_len;
  291. pad_len = 4 - (pkt_len & 0x3);
  292. if (pad_len == 4)
  293. pad_len = 0;
  294. chk_idx = index;
  295. index = index + 4 + pkt_len + pad_len;
  296. if (index > MAX_RX_BUF_SIZE) {
  297. spin_lock(&hif_dev->rx_lock);
  298. hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
  299. hif_dev->rx_transfer_len =
  300. MAX_RX_BUF_SIZE - chk_idx - 4;
  301. hif_dev->rx_pad_len = pad_len;
  302. nskb = __dev_alloc_skb(pkt_len + 32,
  303. GFP_ATOMIC);
  304. if (!nskb) {
  305. dev_err(&hif_dev->udev->dev,
  306. "ath9k_htc: RX memory allocation"
  307. " error\n");
  308. spin_unlock(&hif_dev->rx_lock);
  309. goto err;
  310. }
  311. skb_reserve(nskb, 32);
  312. RX_STAT_INC(skb_allocated);
  313. memcpy(nskb->data, &(skb->data[chk_idx+4]),
  314. hif_dev->rx_transfer_len);
  315. /* Record the buffer pointer */
  316. hif_dev->remain_skb = nskb;
  317. spin_unlock(&hif_dev->rx_lock);
  318. } else {
  319. nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
  320. if (!nskb) {
  321. dev_err(&hif_dev->udev->dev,
  322. "ath9k_htc: RX memory allocation"
  323. " error\n");
  324. goto err;
  325. }
  326. skb_reserve(nskb, 32);
  327. RX_STAT_INC(skb_allocated);
  328. memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
  329. skb_put(nskb, pkt_len);
  330. skb_pool[pool_index++] = nskb;
  331. }
  332. } else {
  333. RX_STAT_INC(skb_dropped);
  334. return;
  335. }
  336. }
  337. err:
  338. for (i = 0; i < pool_index; i++) {
  339. ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
  340. skb_pool[i]->len, USB_WLAN_RX_PIPE);
  341. RX_STAT_INC(skb_completed);
  342. }
  343. }
  344. static void ath9k_hif_usb_rx_cb(struct urb *urb)
  345. {
  346. struct sk_buff *skb = (struct sk_buff *) urb->context;
  347. struct hif_device_usb *hif_dev = (struct hif_device_usb *)
  348. usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  349. int ret;
  350. if (!skb)
  351. return;
  352. if (!hif_dev)
  353. goto free;
  354. switch (urb->status) {
  355. case 0:
  356. break;
  357. case -ENOENT:
  358. case -ECONNRESET:
  359. case -ENODEV:
  360. case -ESHUTDOWN:
  361. goto free;
  362. default:
  363. goto resubmit;
  364. }
  365. if (likely(urb->actual_length != 0)) {
  366. skb_put(skb, urb->actual_length);
  367. ath9k_hif_usb_rx_stream(hif_dev, skb);
  368. }
  369. resubmit:
  370. skb_reset_tail_pointer(skb);
  371. skb_trim(skb, 0);
  372. usb_anchor_urb(urb, &hif_dev->rx_submitted);
  373. ret = usb_submit_urb(urb, GFP_ATOMIC);
  374. if (ret) {
  375. usb_unanchor_urb(urb);
  376. goto free;
  377. }
  378. return;
  379. free:
  380. kfree_skb(skb);
  381. }
  382. static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
  383. {
  384. struct sk_buff *skb = (struct sk_buff *) urb->context;
  385. struct sk_buff *nskb;
  386. struct hif_device_usb *hif_dev = (struct hif_device_usb *)
  387. usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  388. int ret;
  389. if (!skb)
  390. return;
  391. if (!hif_dev)
  392. goto free;
  393. switch (urb->status) {
  394. case 0:
  395. break;
  396. case -ENOENT:
  397. case -ECONNRESET:
  398. case -ENODEV:
  399. case -ESHUTDOWN:
  400. goto free;
  401. default:
  402. goto resubmit;
  403. }
  404. if (likely(urb->actual_length != 0)) {
  405. skb_put(skb, urb->actual_length);
  406. nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
  407. if (!nskb)
  408. goto resubmit;
  409. usb_fill_int_urb(urb, hif_dev->udev,
  410. usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
  411. nskb->data, MAX_REG_IN_BUF_SIZE,
  412. ath9k_hif_usb_reg_in_cb, nskb, 1);
  413. ret = usb_submit_urb(urb, GFP_ATOMIC);
  414. if (ret) {
  415. kfree_skb(nskb);
  416. goto free;
  417. }
  418. ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
  419. skb->len, USB_REG_IN_PIPE);
  420. return;
  421. }
  422. resubmit:
  423. skb_reset_tail_pointer(skb);
  424. skb_trim(skb, 0);
  425. ret = usb_submit_urb(urb, GFP_ATOMIC);
  426. if (ret)
  427. goto free;
  428. return;
  429. free:
  430. kfree_skb(skb);
  431. urb->context = NULL;
  432. }
  433. static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
  434. {
  435. unsigned long flags;
  436. struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
  437. list_for_each_entry_safe(tx_buf, tx_buf_tmp, &hif_dev->tx.tx_buf, list) {
  438. list_del(&tx_buf->list);
  439. usb_free_urb(tx_buf->urb);
  440. kfree(tx_buf->buf);
  441. kfree(tx_buf);
  442. }
  443. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  444. hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
  445. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  446. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  447. &hif_dev->tx.tx_pending, list) {
  448. usb_kill_urb(tx_buf->urb);
  449. list_del(&tx_buf->list);
  450. usb_free_urb(tx_buf->urb);
  451. kfree(tx_buf->buf);
  452. kfree(tx_buf);
  453. }
  454. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  455. hif_dev->tx.flags &= ~HIF_USB_TX_FLUSH;
  456. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  457. }
  458. static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
  459. {
  460. struct tx_buf *tx_buf;
  461. int i;
  462. INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
  463. INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
  464. spin_lock_init(&hif_dev->tx.tx_lock);
  465. __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
  466. for (i = 0; i < MAX_TX_URB_NUM; i++) {
  467. tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
  468. if (!tx_buf)
  469. goto err;
  470. tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
  471. if (!tx_buf->buf)
  472. goto err;
  473. tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
  474. if (!tx_buf->urb)
  475. goto err;
  476. tx_buf->hif_dev = hif_dev;
  477. __skb_queue_head_init(&tx_buf->skb_queue);
  478. list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
  479. }
  480. hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
  481. return 0;
  482. err:
  483. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  484. return -ENOMEM;
  485. }
  486. static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
  487. {
  488. usb_kill_anchored_urbs(&hif_dev->rx_submitted);
  489. }
  490. static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
  491. {
  492. struct urb *urb = NULL;
  493. struct sk_buff *skb = NULL;
  494. int i, ret;
  495. init_usb_anchor(&hif_dev->rx_submitted);
  496. spin_lock_init(&hif_dev->rx_lock);
  497. for (i = 0; i < MAX_RX_URB_NUM; i++) {
  498. /* Allocate URB */
  499. urb = usb_alloc_urb(0, GFP_KERNEL);
  500. if (urb == NULL) {
  501. ret = -ENOMEM;
  502. goto err_urb;
  503. }
  504. /* Allocate buffer */
  505. skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
  506. if (!skb) {
  507. ret = -ENOMEM;
  508. goto err_skb;
  509. }
  510. usb_fill_bulk_urb(urb, hif_dev->udev,
  511. usb_rcvbulkpipe(hif_dev->udev,
  512. USB_WLAN_RX_PIPE),
  513. skb->data, MAX_RX_BUF_SIZE,
  514. ath9k_hif_usb_rx_cb, skb);
  515. /* Anchor URB */
  516. usb_anchor_urb(urb, &hif_dev->rx_submitted);
  517. /* Submit URB */
  518. ret = usb_submit_urb(urb, GFP_KERNEL);
  519. if (ret) {
  520. usb_unanchor_urb(urb);
  521. goto err_submit;
  522. }
  523. /*
  524. * Drop reference count.
  525. * This ensures that the URB is freed when killing them.
  526. */
  527. usb_free_urb(urb);
  528. }
  529. return 0;
  530. err_submit:
  531. kfree_skb(skb);
  532. err_skb:
  533. usb_free_urb(urb);
  534. err_urb:
  535. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  536. return ret;
  537. }
  538. static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
  539. {
  540. if (hif_dev->reg_in_urb) {
  541. usb_kill_urb(hif_dev->reg_in_urb);
  542. if (hif_dev->reg_in_urb->context)
  543. kfree_skb((void *)hif_dev->reg_in_urb->context);
  544. usb_free_urb(hif_dev->reg_in_urb);
  545. hif_dev->reg_in_urb = NULL;
  546. }
  547. }
  548. static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
  549. {
  550. struct sk_buff *skb;
  551. hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  552. if (hif_dev->reg_in_urb == NULL)
  553. return -ENOMEM;
  554. skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
  555. if (!skb)
  556. goto err;
  557. usb_fill_int_urb(hif_dev->reg_in_urb, hif_dev->udev,
  558. usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
  559. skb->data, MAX_REG_IN_BUF_SIZE,
  560. ath9k_hif_usb_reg_in_cb, skb, 1);
  561. if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
  562. goto err;
  563. return 0;
  564. err:
  565. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  566. return -ENOMEM;
  567. }
  568. static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
  569. {
  570. /* Register Write */
  571. init_usb_anchor(&hif_dev->regout_submitted);
  572. /* TX */
  573. if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
  574. goto err;
  575. /* RX */
  576. if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
  577. goto err;
  578. /* Register Read */
  579. if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
  580. goto err;
  581. return 0;
  582. err:
  583. return -ENOMEM;
  584. }
  585. static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
  586. {
  587. int transfer, err;
  588. const void *data = hif_dev->firmware->data;
  589. size_t len = hif_dev->firmware->size;
  590. u32 addr = AR9271_FIRMWARE;
  591. u8 *buf = kzalloc(4096, GFP_KERNEL);
  592. if (!buf)
  593. return -ENOMEM;
  594. while (len) {
  595. transfer = min_t(int, len, 4096);
  596. memcpy(buf, data, transfer);
  597. err = usb_control_msg(hif_dev->udev,
  598. usb_sndctrlpipe(hif_dev->udev, 0),
  599. FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
  600. addr >> 8, 0, buf, transfer, HZ);
  601. if (err < 0) {
  602. kfree(buf);
  603. return err;
  604. }
  605. len -= transfer;
  606. data += transfer;
  607. addr += transfer;
  608. }
  609. kfree(buf);
  610. /*
  611. * Issue FW download complete command to firmware.
  612. */
  613. err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
  614. FIRMWARE_DOWNLOAD_COMP,
  615. 0x40 | USB_DIR_OUT,
  616. AR9271_FIRMWARE_TEXT >> 8, 0, NULL, 0, HZ);
  617. if (err)
  618. return -EIO;
  619. dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
  620. "ar9271.fw", (unsigned long) hif_dev->firmware->size);
  621. return 0;
  622. }
  623. static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev,
  624. const char *fw_name)
  625. {
  626. int ret;
  627. /* Request firmware */
  628. ret = request_firmware(&hif_dev->firmware, fw_name, &hif_dev->udev->dev);
  629. if (ret) {
  630. dev_err(&hif_dev->udev->dev,
  631. "ath9k_htc: Firmware - %s not found\n", fw_name);
  632. goto err_fw_req;
  633. }
  634. /* Download firmware */
  635. ret = ath9k_hif_usb_download_fw(hif_dev);
  636. if (ret) {
  637. dev_err(&hif_dev->udev->dev,
  638. "ath9k_htc: Firmware - %s download failed\n", fw_name);
  639. goto err_fw_download;
  640. }
  641. /* Alloc URBs */
  642. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  643. if (ret) {
  644. dev_err(&hif_dev->udev->dev,
  645. "ath9k_htc: Unable to allocate URBs\n");
  646. goto err_urb;
  647. }
  648. return 0;
  649. err_urb:
  650. /* Nothing */
  651. err_fw_download:
  652. release_firmware(hif_dev->firmware);
  653. err_fw_req:
  654. hif_dev->firmware = NULL;
  655. return ret;
  656. }
  657. static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
  658. {
  659. usb_kill_anchored_urbs(&hif_dev->regout_submitted);
  660. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  661. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  662. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  663. }
  664. static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
  665. {
  666. ath9k_hif_usb_dealloc_urbs(hif_dev);
  667. if (hif_dev->firmware)
  668. release_firmware(hif_dev->firmware);
  669. }
  670. static int ath9k_hif_usb_probe(struct usb_interface *interface,
  671. const struct usb_device_id *id)
  672. {
  673. struct usb_device *udev = interface_to_usbdev(interface);
  674. struct hif_device_usb *hif_dev;
  675. const char *fw_name = (const char *) id->driver_info;
  676. int ret = 0;
  677. hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
  678. if (!hif_dev) {
  679. ret = -ENOMEM;
  680. goto err_alloc;
  681. }
  682. usb_get_dev(udev);
  683. hif_dev->udev = udev;
  684. hif_dev->interface = interface;
  685. hif_dev->device_id = id->idProduct;
  686. #ifdef CONFIG_PM
  687. udev->reset_resume = 1;
  688. #endif
  689. usb_set_intfdata(interface, hif_dev);
  690. ret = ath9k_hif_usb_dev_init(hif_dev, fw_name);
  691. if (ret) {
  692. ret = -EINVAL;
  693. goto err_hif_init_usb;
  694. }
  695. hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev);
  696. if (hif_dev->htc_handle == NULL) {
  697. ret = -ENOMEM;
  698. goto err_htc_hw_alloc;
  699. }
  700. ret = ath9k_htc_hw_init(&hif_usb, hif_dev->htc_handle, hif_dev,
  701. &hif_dev->udev->dev, hif_dev->device_id,
  702. ATH9K_HIF_USB);
  703. if (ret) {
  704. ret = -EINVAL;
  705. goto err_htc_hw_init;
  706. }
  707. dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
  708. return 0;
  709. err_htc_hw_init:
  710. ath9k_htc_hw_free(hif_dev->htc_handle);
  711. err_htc_hw_alloc:
  712. ath9k_hif_usb_dev_deinit(hif_dev);
  713. err_hif_init_usb:
  714. usb_set_intfdata(interface, NULL);
  715. kfree(hif_dev);
  716. usb_put_dev(udev);
  717. err_alloc:
  718. return ret;
  719. }
  720. static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
  721. {
  722. struct usb_device *udev = interface_to_usbdev(interface);
  723. struct hif_device_usb *hif_dev =
  724. (struct hif_device_usb *) usb_get_intfdata(interface);
  725. if (hif_dev) {
  726. ath9k_htc_hw_deinit(hif_dev->htc_handle, true);
  727. ath9k_htc_hw_free(hif_dev->htc_handle);
  728. ath9k_hif_usb_dev_deinit(hif_dev);
  729. usb_set_intfdata(interface, NULL);
  730. }
  731. if (hif_dev->flags & HIF_USB_START)
  732. usb_reset_device(udev);
  733. kfree(hif_dev);
  734. dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
  735. usb_put_dev(udev);
  736. }
  737. #ifdef CONFIG_PM
  738. static int ath9k_hif_usb_suspend(struct usb_interface *interface,
  739. pm_message_t message)
  740. {
  741. struct hif_device_usb *hif_dev =
  742. (struct hif_device_usb *) usb_get_intfdata(interface);
  743. ath9k_hif_usb_dealloc_urbs(hif_dev);
  744. return 0;
  745. }
  746. static int ath9k_hif_usb_resume(struct usb_interface *interface)
  747. {
  748. struct hif_device_usb *hif_dev =
  749. (struct hif_device_usb *) usb_get_intfdata(interface);
  750. int ret;
  751. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  752. if (ret)
  753. return ret;
  754. if (hif_dev->firmware) {
  755. ret = ath9k_hif_usb_download_fw(hif_dev);
  756. if (ret)
  757. goto fail_resume;
  758. } else {
  759. ath9k_hif_usb_dealloc_urbs(hif_dev);
  760. return -EIO;
  761. }
  762. mdelay(100);
  763. ret = ath9k_htc_resume(hif_dev->htc_handle);
  764. if (ret)
  765. goto fail_resume;
  766. return 0;
  767. fail_resume:
  768. ath9k_hif_usb_dealloc_urbs(hif_dev);
  769. return ret;
  770. }
  771. #endif
  772. static struct usb_driver ath9k_hif_usb_driver = {
  773. .name = "ath9k_hif_usb",
  774. .probe = ath9k_hif_usb_probe,
  775. .disconnect = ath9k_hif_usb_disconnect,
  776. #ifdef CONFIG_PM
  777. .suspend = ath9k_hif_usb_suspend,
  778. .resume = ath9k_hif_usb_resume,
  779. .reset_resume = ath9k_hif_usb_resume,
  780. #endif
  781. .id_table = ath9k_hif_usb_ids,
  782. .soft_unbind = 1,
  783. };
  784. int ath9k_hif_usb_init(void)
  785. {
  786. return usb_register(&ath9k_hif_usb_driver);
  787. }
  788. void ath9k_hif_usb_exit(void)
  789. {
  790. usb_deregister(&ath9k_hif_usb_driver);
  791. }