hif_usb.c 27 KB

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