hif_usb.c 31 KB

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