hif_usb.c 24 KB

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