hif_usb.c 31 KB

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