hif_usb.c 30 KB

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