hif_usb.c 21 KB

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