hif_usb.c 22 KB

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