hif_usb.c 30 KB

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