hif_usb.c 27 KB

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