hif_usb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. rx_remain_len = hif_dev->rx_remain_len;
  272. rx_pkt_len = hif_dev->rx_transfer_len;
  273. if (rx_remain_len != 0) {
  274. struct sk_buff *remain_skb = hif_dev->remain_skb;
  275. if (remain_skb) {
  276. ptr = (u8 *) remain_skb->data;
  277. index = rx_remain_len;
  278. rx_remain_len -= hif_dev->rx_pad_len;
  279. ptr += rx_pkt_len;
  280. memcpy(ptr, skb->data, rx_remain_len);
  281. rx_pkt_len += rx_remain_len;
  282. hif_dev->rx_remain_len = 0;
  283. skb_put(remain_skb, rx_pkt_len);
  284. skb_pool[pool_index++] = remain_skb;
  285. } else {
  286. index = rx_remain_len;
  287. }
  288. }
  289. while (index < len) {
  290. ptr = (u8 *) skb->data;
  291. pkt_len = ptr[index] + (ptr[index+1] << 8);
  292. pkt_tag = ptr[index+2] + (ptr[index+3] << 8);
  293. if (pkt_tag == ATH_USB_RX_STREAM_MODE_TAG) {
  294. u16 pad_len;
  295. pad_len = 4 - (pkt_len & 0x3);
  296. if (pad_len == 4)
  297. pad_len = 0;
  298. chk_idx = index;
  299. index = index + 4 + pkt_len + pad_len;
  300. if (index > MAX_RX_BUF_SIZE) {
  301. hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
  302. hif_dev->rx_transfer_len =
  303. MAX_RX_BUF_SIZE - chk_idx - 4;
  304. hif_dev->rx_pad_len = pad_len;
  305. nskb = __dev_alloc_skb(pkt_len + 32,
  306. GFP_ATOMIC);
  307. if (!nskb) {
  308. dev_err(&hif_dev->udev->dev,
  309. "ath9k_htc: RX memory allocation"
  310. " error\n");
  311. goto err;
  312. }
  313. skb_reserve(nskb, 32);
  314. RX_STAT_INC(skb_allocated);
  315. memcpy(nskb->data, &(skb->data[chk_idx+4]),
  316. hif_dev->rx_transfer_len);
  317. /* Record the buffer pointer */
  318. hif_dev->remain_skb = nskb;
  319. } else {
  320. nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
  321. if (!nskb) {
  322. dev_err(&hif_dev->udev->dev,
  323. "ath9k_htc: RX memory allocation"
  324. " error\n");
  325. goto err;
  326. }
  327. skb_reserve(nskb, 32);
  328. RX_STAT_INC(skb_allocated);
  329. memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
  330. skb_put(nskb, pkt_len);
  331. skb_pool[pool_index++] = nskb;
  332. }
  333. } else {
  334. RX_STAT_INC(skb_dropped);
  335. return;
  336. }
  337. }
  338. err:
  339. for (i = 0; i < pool_index; i++) {
  340. ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
  341. skb_pool[i]->len, USB_WLAN_RX_PIPE);
  342. RX_STAT_INC(skb_completed);
  343. }
  344. }
  345. static void ath9k_hif_usb_rx_cb(struct urb *urb)
  346. {
  347. struct sk_buff *skb = (struct sk_buff *) urb->context;
  348. struct hif_device_usb *hif_dev = (struct hif_device_usb *)
  349. usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  350. int ret;
  351. if (!skb)
  352. return;
  353. if (!hif_dev)
  354. goto free;
  355. switch (urb->status) {
  356. case 0:
  357. break;
  358. case -ENOENT:
  359. case -ECONNRESET:
  360. case -ENODEV:
  361. case -ESHUTDOWN:
  362. goto free;
  363. default:
  364. goto resubmit;
  365. }
  366. if (likely(urb->actual_length != 0)) {
  367. skb_put(skb, urb->actual_length);
  368. ath9k_hif_usb_rx_stream(hif_dev, skb);
  369. }
  370. resubmit:
  371. skb_reset_tail_pointer(skb);
  372. skb_trim(skb, 0);
  373. usb_anchor_urb(urb, &hif_dev->rx_submitted);
  374. ret = usb_submit_urb(urb, GFP_ATOMIC);
  375. if (ret) {
  376. usb_unanchor_urb(urb);
  377. goto free;
  378. }
  379. return;
  380. free:
  381. dev_kfree_skb_any(skb);
  382. }
  383. static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
  384. {
  385. struct sk_buff *skb = (struct sk_buff *) urb->context;
  386. struct sk_buff *nskb;
  387. struct hif_device_usb *hif_dev = (struct hif_device_usb *)
  388. usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  389. int ret;
  390. if (!skb)
  391. return;
  392. if (!hif_dev)
  393. goto free;
  394. switch (urb->status) {
  395. case 0:
  396. break;
  397. case -ENOENT:
  398. case -ECONNRESET:
  399. case -ENODEV:
  400. case -ESHUTDOWN:
  401. goto free;
  402. default:
  403. goto resubmit;
  404. }
  405. if (likely(urb->actual_length != 0)) {
  406. skb_put(skb, urb->actual_length);
  407. nskb = __dev_alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
  408. if (!nskb)
  409. goto resubmit;
  410. usb_fill_int_urb(urb, hif_dev->udev,
  411. usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
  412. nskb->data, MAX_REG_IN_BUF_SIZE,
  413. ath9k_hif_usb_reg_in_cb, nskb, 1);
  414. ret = usb_submit_urb(urb, GFP_ATOMIC);
  415. if (ret) {
  416. dev_kfree_skb_any(nskb);
  417. goto free;
  418. }
  419. ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
  420. skb->len, USB_REG_IN_PIPE);
  421. return;
  422. }
  423. resubmit:
  424. skb_reset_tail_pointer(skb);
  425. skb_trim(skb, 0);
  426. ret = usb_submit_urb(urb, GFP_ATOMIC);
  427. if (ret)
  428. goto free;
  429. return;
  430. free:
  431. dev_kfree_skb_any(skb);
  432. urb->context = NULL;
  433. }
  434. static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
  435. {
  436. unsigned long flags;
  437. struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
  438. list_for_each_entry_safe(tx_buf, tx_buf_tmp, &hif_dev->tx.tx_buf, list) {
  439. list_del(&tx_buf->list);
  440. usb_free_urb(tx_buf->urb);
  441. kfree(tx_buf->buf);
  442. kfree(tx_buf);
  443. }
  444. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  445. hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
  446. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  447. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  448. &hif_dev->tx.tx_pending, list) {
  449. usb_kill_urb(tx_buf->urb);
  450. list_del(&tx_buf->list);
  451. usb_free_urb(tx_buf->urb);
  452. kfree(tx_buf->buf);
  453. kfree(tx_buf);
  454. }
  455. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  456. hif_dev->tx.flags &= ~HIF_USB_TX_FLUSH;
  457. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  458. }
  459. static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
  460. {
  461. struct tx_buf *tx_buf;
  462. int i;
  463. INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
  464. INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
  465. spin_lock_init(&hif_dev->tx.tx_lock);
  466. __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
  467. for (i = 0; i < MAX_TX_URB_NUM; i++) {
  468. tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
  469. if (!tx_buf)
  470. goto err;
  471. tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
  472. if (!tx_buf->buf)
  473. goto err;
  474. tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
  475. if (!tx_buf->urb)
  476. goto err;
  477. tx_buf->hif_dev = hif_dev;
  478. __skb_queue_head_init(&tx_buf->skb_queue);
  479. list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
  480. }
  481. hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
  482. return 0;
  483. err:
  484. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  485. return -ENOMEM;
  486. }
  487. static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
  488. {
  489. usb_kill_anchored_urbs(&hif_dev->rx_submitted);
  490. }
  491. static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
  492. {
  493. struct urb *urb = NULL;
  494. struct sk_buff *skb = NULL;
  495. int i, ret;
  496. init_usb_anchor(&hif_dev->rx_submitted);
  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 = __dev_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. return 0;
  525. err_submit:
  526. dev_kfree_skb_any(skb);
  527. err_skb:
  528. usb_free_urb(urb);
  529. err_urb:
  530. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  531. return ret;
  532. }
  533. static void ath9k_hif_usb_dealloc_reg_in_urb(struct hif_device_usb *hif_dev)
  534. {
  535. if (hif_dev->reg_in_urb) {
  536. usb_kill_urb(hif_dev->reg_in_urb);
  537. if (hif_dev->reg_in_urb->context)
  538. dev_kfree_skb_any((void *)hif_dev->reg_in_urb->context);
  539. usb_free_urb(hif_dev->reg_in_urb);
  540. hif_dev->reg_in_urb = NULL;
  541. }
  542. }
  543. static int ath9k_hif_usb_alloc_reg_in_urb(struct hif_device_usb *hif_dev)
  544. {
  545. struct sk_buff *skb;
  546. hif_dev->reg_in_urb = usb_alloc_urb(0, GFP_KERNEL);
  547. if (hif_dev->reg_in_urb == NULL)
  548. return -ENOMEM;
  549. skb = __dev_alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
  550. if (!skb)
  551. goto err;
  552. usb_fill_int_urb(hif_dev->reg_in_urb, hif_dev->udev,
  553. usb_rcvintpipe(hif_dev->udev, USB_REG_IN_PIPE),
  554. skb->data, MAX_REG_IN_BUF_SIZE,
  555. ath9k_hif_usb_reg_in_cb, skb, 1);
  556. if (usb_submit_urb(hif_dev->reg_in_urb, GFP_KERNEL) != 0)
  557. goto err;
  558. return 0;
  559. err:
  560. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  561. return -ENOMEM;
  562. }
  563. static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
  564. {
  565. /* TX */
  566. if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
  567. goto err;
  568. /* RX */
  569. if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
  570. goto err;
  571. /* Register Read/Write */
  572. if (ath9k_hif_usb_alloc_reg_in_urb(hif_dev) < 0)
  573. goto err;
  574. return 0;
  575. err:
  576. return -ENOMEM;
  577. }
  578. static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
  579. {
  580. int transfer, err;
  581. const void *data = hif_dev->firmware->data;
  582. size_t len = hif_dev->firmware->size;
  583. u32 addr = AR9271_FIRMWARE;
  584. u8 *buf = kzalloc(4096, GFP_KERNEL);
  585. if (!buf)
  586. return -ENOMEM;
  587. while (len) {
  588. transfer = min_t(int, len, 4096);
  589. memcpy(buf, data, transfer);
  590. err = usb_control_msg(hif_dev->udev,
  591. usb_sndctrlpipe(hif_dev->udev, 0),
  592. FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
  593. addr >> 8, 0, buf, transfer, HZ);
  594. if (err < 0) {
  595. kfree(buf);
  596. return err;
  597. }
  598. len -= transfer;
  599. data += transfer;
  600. addr += transfer;
  601. }
  602. kfree(buf);
  603. /*
  604. * Issue FW download complete command to firmware.
  605. */
  606. err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
  607. FIRMWARE_DOWNLOAD_COMP,
  608. 0x40 | USB_DIR_OUT,
  609. AR9271_FIRMWARE_TEXT >> 8, 0, NULL, 0, HZ);
  610. if (err)
  611. return -EIO;
  612. dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
  613. "ar9271.fw", (unsigned long) hif_dev->firmware->size);
  614. return 0;
  615. }
  616. static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev,
  617. const char *fw_name)
  618. {
  619. int ret;
  620. /* Request firmware */
  621. ret = request_firmware(&hif_dev->firmware, fw_name, &hif_dev->udev->dev);
  622. if (ret) {
  623. dev_err(&hif_dev->udev->dev,
  624. "ath9k_htc: Firmware - %s not found\n", fw_name);
  625. goto err_fw_req;
  626. }
  627. /* Download firmware */
  628. ret = ath9k_hif_usb_download_fw(hif_dev);
  629. if (ret) {
  630. dev_err(&hif_dev->udev->dev,
  631. "ath9k_htc: Firmware - %s download failed\n", fw_name);
  632. goto err_fw_download;
  633. }
  634. /* Alloc URBs */
  635. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  636. if (ret) {
  637. dev_err(&hif_dev->udev->dev,
  638. "ath9k_htc: Unable to allocate URBs\n");
  639. goto err_urb;
  640. }
  641. return 0;
  642. err_urb:
  643. /* Nothing */
  644. err_fw_download:
  645. release_firmware(hif_dev->firmware);
  646. err_fw_req:
  647. hif_dev->firmware = NULL;
  648. return ret;
  649. }
  650. static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
  651. {
  652. ath9k_hif_usb_dealloc_reg_in_urb(hif_dev);
  653. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  654. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  655. }
  656. static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
  657. {
  658. ath9k_hif_usb_dealloc_urbs(hif_dev);
  659. if (hif_dev->firmware)
  660. release_firmware(hif_dev->firmware);
  661. }
  662. static int ath9k_hif_usb_probe(struct usb_interface *interface,
  663. const struct usb_device_id *id)
  664. {
  665. struct usb_device *udev = interface_to_usbdev(interface);
  666. struct hif_device_usb *hif_dev;
  667. const char *fw_name = (const char *) id->driver_info;
  668. int ret = 0;
  669. hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
  670. if (!hif_dev) {
  671. ret = -ENOMEM;
  672. goto err_alloc;
  673. }
  674. usb_get_dev(udev);
  675. hif_dev->udev = udev;
  676. hif_dev->interface = interface;
  677. hif_dev->device_id = id->idProduct;
  678. #ifdef CONFIG_PM
  679. udev->reset_resume = 1;
  680. #endif
  681. usb_set_intfdata(interface, hif_dev);
  682. ret = ath9k_hif_usb_dev_init(hif_dev, fw_name);
  683. if (ret) {
  684. ret = -EINVAL;
  685. goto err_hif_init_usb;
  686. }
  687. hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev);
  688. if (hif_dev->htc_handle == NULL) {
  689. ret = -ENOMEM;
  690. goto err_htc_hw_alloc;
  691. }
  692. ret = ath9k_htc_hw_init(&hif_usb, hif_dev->htc_handle, hif_dev,
  693. &hif_dev->udev->dev, hif_dev->device_id,
  694. ATH9K_HIF_USB);
  695. if (ret) {
  696. ret = -EINVAL;
  697. goto err_htc_hw_init;
  698. }
  699. dev_info(&hif_dev->udev->dev, "ath9k_htc: USB layer initialized\n");
  700. return 0;
  701. err_htc_hw_init:
  702. ath9k_htc_hw_free(hif_dev->htc_handle);
  703. err_htc_hw_alloc:
  704. ath9k_hif_usb_dev_deinit(hif_dev);
  705. err_hif_init_usb:
  706. usb_set_intfdata(interface, NULL);
  707. kfree(hif_dev);
  708. usb_put_dev(udev);
  709. err_alloc:
  710. return ret;
  711. }
  712. static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
  713. {
  714. struct usb_device *udev = interface_to_usbdev(interface);
  715. struct hif_device_usb *hif_dev =
  716. (struct hif_device_usb *) usb_get_intfdata(interface);
  717. if (hif_dev) {
  718. ath9k_htc_hw_deinit(hif_dev->htc_handle, true);
  719. ath9k_htc_hw_free(hif_dev->htc_handle);
  720. ath9k_hif_usb_dev_deinit(hif_dev);
  721. usb_set_intfdata(interface, NULL);
  722. }
  723. if (hif_dev->flags & HIF_USB_START)
  724. usb_reset_device(udev);
  725. kfree(hif_dev);
  726. dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
  727. usb_put_dev(udev);
  728. }
  729. #ifdef CONFIG_PM
  730. static int ath9k_hif_usb_suspend(struct usb_interface *interface,
  731. pm_message_t message)
  732. {
  733. struct hif_device_usb *hif_dev =
  734. (struct hif_device_usb *) usb_get_intfdata(interface);
  735. ath9k_hif_usb_dealloc_urbs(hif_dev);
  736. return 0;
  737. }
  738. static int ath9k_hif_usb_resume(struct usb_interface *interface)
  739. {
  740. struct hif_device_usb *hif_dev =
  741. (struct hif_device_usb *) usb_get_intfdata(interface);
  742. int ret;
  743. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  744. if (ret)
  745. return ret;
  746. if (hif_dev->firmware) {
  747. ret = ath9k_hif_usb_download_fw(hif_dev);
  748. if (ret)
  749. goto fail_resume;
  750. } else {
  751. ath9k_hif_usb_dealloc_urbs(hif_dev);
  752. return -EIO;
  753. }
  754. mdelay(100);
  755. ret = ath9k_htc_resume(hif_dev->htc_handle);
  756. if (ret)
  757. goto fail_resume;
  758. return 0;
  759. fail_resume:
  760. ath9k_hif_usb_dealloc_urbs(hif_dev);
  761. return ret;
  762. }
  763. #endif
  764. static struct usb_driver ath9k_hif_usb_driver = {
  765. .name = "ath9k_hif_usb",
  766. .probe = ath9k_hif_usb_probe,
  767. .disconnect = ath9k_hif_usb_disconnect,
  768. #ifdef CONFIG_PM
  769. .suspend = ath9k_hif_usb_suspend,
  770. .resume = ath9k_hif_usb_resume,
  771. .reset_resume = ath9k_hif_usb_resume,
  772. #endif
  773. .id_table = ath9k_hif_usb_ids,
  774. .soft_unbind = 1,
  775. };
  776. int ath9k_hif_usb_init(void)
  777. {
  778. return usb_register(&ath9k_hif_usb_driver);
  779. }
  780. void ath9k_hif_usb_exit(void)
  781. {
  782. usb_deregister(&ath9k_hif_usb_driver);
  783. }