hif_usb.c 31 KB

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