hif_usb.c 23 KB

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