hif_usb.c 22 KB

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