hif_usb.c 21 KB

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