hif_usb.c 25 KB

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