hif_usb.c 35 KB

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