hif_usb.c 30 KB

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