hif_usb.c 30 KB

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