hif_usb.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446
  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. 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(0x07D1, 0x3A10) }, /* Dlink Wireless 150 */
  25. { USB_DEVICE(0x13D3, 0x3327) }, /* Azurewave */
  26. { USB_DEVICE(0x13D3, 0x3328) }, /* Azurewave */
  27. { USB_DEVICE(0x13D3, 0x3346) }, /* IMC Networks */
  28. { USB_DEVICE(0x13D3, 0x3348) }, /* Azurewave */
  29. { USB_DEVICE(0x13D3, 0x3349) }, /* Azurewave */
  30. { USB_DEVICE(0x13D3, 0x3350) }, /* Azurewave */
  31. { USB_DEVICE(0x04CA, 0x4605) }, /* Liteon */
  32. { USB_DEVICE(0x040D, 0x3801) }, /* VIA */
  33. { USB_DEVICE(0x0cf3, 0xb003) }, /* Ubiquiti WifiStation Ext */
  34. { USB_DEVICE(0x0cf3, 0xb002) }, /* Ubiquiti WifiStation */
  35. { USB_DEVICE(0x057c, 0x8403) }, /* AVM FRITZ!WLAN 11N v2 USB */
  36. { USB_DEVICE(0x0471, 0x209e) }, /* Philips (or NXP) PTA01 */
  37. { USB_DEVICE(0x1eda, 0x2315) }, /* AirTies */
  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(0x0411, 0x0197),
  51. .driver_info = AR9280_USB }, /* Buffalo WLI-UV-AG300P */
  52. { USB_DEVICE(0x04da, 0x3904),
  53. .driver_info = AR9280_USB },
  54. { USB_DEVICE(0x0930, 0x0a08),
  55. .driver_info = AR9280_USB }, /* Toshiba WLM-20U2 and GN-1080 */
  56. { USB_DEVICE(0x0cf3, 0x20ff),
  57. .driver_info = STORAGE_DEVICE },
  58. { },
  59. };
  60. MODULE_DEVICE_TABLE(usb, ath9k_hif_usb_ids);
  61. static int __hif_usb_tx(struct hif_device_usb *hif_dev);
  62. static void hif_usb_regout_cb(struct urb *urb)
  63. {
  64. struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
  65. switch (urb->status) {
  66. case 0:
  67. break;
  68. case -ENOENT:
  69. case -ECONNRESET:
  70. case -ENODEV:
  71. case -ESHUTDOWN:
  72. goto free;
  73. default:
  74. break;
  75. }
  76. if (cmd) {
  77. ath9k_htc_txcompletion_cb(cmd->hif_dev->htc_handle,
  78. cmd->skb, true);
  79. kfree(cmd);
  80. }
  81. return;
  82. free:
  83. kfree_skb(cmd->skb);
  84. kfree(cmd);
  85. }
  86. static int hif_usb_send_regout(struct hif_device_usb *hif_dev,
  87. struct sk_buff *skb)
  88. {
  89. struct urb *urb;
  90. struct cmd_buf *cmd;
  91. int ret = 0;
  92. urb = usb_alloc_urb(0, GFP_KERNEL);
  93. if (urb == NULL)
  94. return -ENOMEM;
  95. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  96. if (cmd == NULL) {
  97. usb_free_urb(urb);
  98. return -ENOMEM;
  99. }
  100. cmd->skb = skb;
  101. cmd->hif_dev = hif_dev;
  102. usb_fill_int_urb(urb, hif_dev->udev,
  103. usb_sndintpipe(hif_dev->udev, USB_REG_OUT_PIPE),
  104. skb->data, skb->len,
  105. hif_usb_regout_cb, cmd, 1);
  106. usb_anchor_urb(urb, &hif_dev->regout_submitted);
  107. ret = usb_submit_urb(urb, GFP_KERNEL);
  108. if (ret) {
  109. usb_unanchor_urb(urb);
  110. kfree(cmd);
  111. }
  112. usb_free_urb(urb);
  113. return ret;
  114. }
  115. static void hif_usb_mgmt_cb(struct urb *urb)
  116. {
  117. struct cmd_buf *cmd = (struct cmd_buf *)urb->context;
  118. struct hif_device_usb *hif_dev;
  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(&hif_dev->tx.tx_lock);
  136. if (hif_dev->tx.flags & HIF_USB_TX_FLUSH) {
  137. spin_unlock(&hif_dev->tx.tx_lock);
  138. dev_kfree_skb_any(cmd->skb);
  139. kfree(cmd);
  140. return;
  141. }
  142. spin_unlock(&hif_dev->tx.tx_lock);
  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(sizeof(*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(skb_success);
  207. TX_STAT_ADD(skb_success_bytes, ln);
  208. }
  209. else
  210. TX_STAT_INC(skb_failed);
  211. }
  212. }
  213. static void hif_usb_tx_cb(struct urb *urb)
  214. {
  215. struct tx_buf *tx_buf = (struct 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(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(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. }
  308. if (!ret)
  309. TX_STAT_INC(buf_queued);
  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 = (struct hif_device_usb *)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 = (struct hif_device_usb *)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. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  369. &hif_dev->tx.tx_pending, list) {
  370. usb_kill_urb(tx_buf->urb);
  371. }
  372. usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
  373. }
  374. static int hif_usb_send(void *hif_handle, u8 pipe_id, struct sk_buff *skb)
  375. {
  376. struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
  377. int ret = 0;
  378. switch (pipe_id) {
  379. case USB_WLAN_TX_PIPE:
  380. ret = hif_usb_send_tx(hif_dev, skb);
  381. break;
  382. case USB_REG_OUT_PIPE:
  383. ret = hif_usb_send_regout(hif_dev, skb);
  384. break;
  385. default:
  386. dev_err(&hif_dev->udev->dev,
  387. "ath9k_htc: Invalid TX pipe: %d\n", pipe_id);
  388. ret = -EINVAL;
  389. break;
  390. }
  391. return ret;
  392. }
  393. static inline bool check_index(struct sk_buff *skb, u8 idx)
  394. {
  395. struct ath9k_htc_tx_ctl *tx_ctl;
  396. tx_ctl = HTC_SKB_CB(skb);
  397. if ((tx_ctl->type == ATH9K_HTC_AMPDU) &&
  398. (tx_ctl->sta_idx == idx))
  399. return true;
  400. return false;
  401. }
  402. static void hif_usb_sta_drain(void *hif_handle, u8 idx)
  403. {
  404. struct hif_device_usb *hif_dev = (struct hif_device_usb *)hif_handle;
  405. struct sk_buff *skb, *tmp;
  406. unsigned long flags;
  407. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  408. skb_queue_walk_safe(&hif_dev->tx.tx_skb_queue, skb, tmp) {
  409. if (check_index(skb, idx)) {
  410. __skb_unlink(skb, &hif_dev->tx.tx_skb_queue);
  411. ath9k_htc_txcompletion_cb(hif_dev->htc_handle,
  412. skb, false);
  413. hif_dev->tx.tx_skb_cnt--;
  414. TX_STAT_INC(skb_failed);
  415. }
  416. }
  417. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  418. }
  419. static struct ath9k_htc_hif hif_usb = {
  420. .transport = ATH9K_HIF_USB,
  421. .name = "ath9k_hif_usb",
  422. .control_ul_pipe = USB_REG_OUT_PIPE,
  423. .control_dl_pipe = USB_REG_IN_PIPE,
  424. .start = hif_usb_start,
  425. .stop = hif_usb_stop,
  426. .sta_drain = hif_usb_sta_drain,
  427. .send = hif_usb_send,
  428. };
  429. static void ath9k_hif_usb_rx_stream(struct hif_device_usb *hif_dev,
  430. struct sk_buff *skb)
  431. {
  432. struct sk_buff *nskb, *skb_pool[MAX_PKT_NUM_IN_TRANSFER];
  433. int index = 0, i, len = skb->len;
  434. int rx_remain_len, rx_pkt_len;
  435. u16 pool_index = 0;
  436. u8 *ptr;
  437. spin_lock(&hif_dev->rx_lock);
  438. rx_remain_len = hif_dev->rx_remain_len;
  439. rx_pkt_len = hif_dev->rx_transfer_len;
  440. if (rx_remain_len != 0) {
  441. struct sk_buff *remain_skb = hif_dev->remain_skb;
  442. if (remain_skb) {
  443. ptr = (u8 *) remain_skb->data;
  444. index = rx_remain_len;
  445. rx_remain_len -= hif_dev->rx_pad_len;
  446. ptr += rx_pkt_len;
  447. memcpy(ptr, skb->data, rx_remain_len);
  448. rx_pkt_len += rx_remain_len;
  449. hif_dev->rx_remain_len = 0;
  450. skb_put(remain_skb, rx_pkt_len);
  451. skb_pool[pool_index++] = remain_skb;
  452. } else {
  453. index = rx_remain_len;
  454. }
  455. }
  456. spin_unlock(&hif_dev->rx_lock);
  457. while (index < len) {
  458. u16 pkt_len;
  459. u16 pkt_tag;
  460. u16 pad_len;
  461. int chk_idx;
  462. ptr = (u8 *) skb->data;
  463. pkt_len = get_unaligned_le16(ptr + index);
  464. pkt_tag = get_unaligned_le16(ptr + index + 2);
  465. if (pkt_tag != ATH_USB_RX_STREAM_MODE_TAG) {
  466. RX_STAT_INC(skb_dropped);
  467. return;
  468. }
  469. pad_len = 4 - (pkt_len & 0x3);
  470. if (pad_len == 4)
  471. pad_len = 0;
  472. chk_idx = index;
  473. index = index + 4 + pkt_len + pad_len;
  474. if (index > MAX_RX_BUF_SIZE) {
  475. spin_lock(&hif_dev->rx_lock);
  476. hif_dev->rx_remain_len = index - MAX_RX_BUF_SIZE;
  477. hif_dev->rx_transfer_len =
  478. MAX_RX_BUF_SIZE - chk_idx - 4;
  479. hif_dev->rx_pad_len = pad_len;
  480. nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
  481. if (!nskb) {
  482. dev_err(&hif_dev->udev->dev,
  483. "ath9k_htc: RX memory allocation error\n");
  484. spin_unlock(&hif_dev->rx_lock);
  485. goto err;
  486. }
  487. skb_reserve(nskb, 32);
  488. RX_STAT_INC(skb_allocated);
  489. memcpy(nskb->data, &(skb->data[chk_idx+4]),
  490. hif_dev->rx_transfer_len);
  491. /* Record the buffer pointer */
  492. hif_dev->remain_skb = nskb;
  493. spin_unlock(&hif_dev->rx_lock);
  494. } else {
  495. nskb = __dev_alloc_skb(pkt_len + 32, GFP_ATOMIC);
  496. if (!nskb) {
  497. dev_err(&hif_dev->udev->dev,
  498. "ath9k_htc: RX memory allocation error\n");
  499. goto err;
  500. }
  501. skb_reserve(nskb, 32);
  502. RX_STAT_INC(skb_allocated);
  503. memcpy(nskb->data, &(skb->data[chk_idx+4]), pkt_len);
  504. skb_put(nskb, pkt_len);
  505. skb_pool[pool_index++] = nskb;
  506. }
  507. }
  508. err:
  509. for (i = 0; i < pool_index; i++) {
  510. RX_STAT_ADD(skb_completed_bytes, skb_pool[i]->len);
  511. ath9k_htc_rx_msg(hif_dev->htc_handle, skb_pool[i],
  512. skb_pool[i]->len, USB_WLAN_RX_PIPE);
  513. RX_STAT_INC(skb_completed);
  514. }
  515. }
  516. static void ath9k_hif_usb_rx_cb(struct urb *urb)
  517. {
  518. struct sk_buff *skb = (struct sk_buff *) urb->context;
  519. struct hif_device_usb *hif_dev =
  520. usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  521. int ret;
  522. if (!skb)
  523. return;
  524. if (!hif_dev)
  525. goto free;
  526. switch (urb->status) {
  527. case 0:
  528. break;
  529. case -ENOENT:
  530. case -ECONNRESET:
  531. case -ENODEV:
  532. case -ESHUTDOWN:
  533. goto free;
  534. default:
  535. goto resubmit;
  536. }
  537. if (likely(urb->actual_length != 0)) {
  538. skb_put(skb, urb->actual_length);
  539. ath9k_hif_usb_rx_stream(hif_dev, skb);
  540. }
  541. resubmit:
  542. skb_reset_tail_pointer(skb);
  543. skb_trim(skb, 0);
  544. usb_anchor_urb(urb, &hif_dev->rx_submitted);
  545. ret = usb_submit_urb(urb, GFP_ATOMIC);
  546. if (ret) {
  547. usb_unanchor_urb(urb);
  548. goto free;
  549. }
  550. return;
  551. free:
  552. kfree_skb(skb);
  553. }
  554. static void ath9k_hif_usb_reg_in_cb(struct urb *urb)
  555. {
  556. struct sk_buff *skb = (struct sk_buff *) urb->context;
  557. struct sk_buff *nskb;
  558. struct hif_device_usb *hif_dev =
  559. usb_get_intfdata(usb_ifnum_to_if(urb->dev, 0));
  560. int ret;
  561. if (!skb)
  562. return;
  563. if (!hif_dev)
  564. goto free;
  565. switch (urb->status) {
  566. case 0:
  567. break;
  568. case -ENOENT:
  569. case -ECONNRESET:
  570. case -ENODEV:
  571. case -ESHUTDOWN:
  572. goto free;
  573. default:
  574. skb_reset_tail_pointer(skb);
  575. skb_trim(skb, 0);
  576. goto resubmit;
  577. }
  578. if (likely(urb->actual_length != 0)) {
  579. skb_put(skb, urb->actual_length);
  580. /* Process the command first */
  581. ath9k_htc_rx_msg(hif_dev->htc_handle, skb,
  582. skb->len, USB_REG_IN_PIPE);
  583. nskb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_ATOMIC);
  584. if (!nskb) {
  585. dev_err(&hif_dev->udev->dev,
  586. "ath9k_htc: REG_IN memory allocation failure\n");
  587. urb->context = NULL;
  588. return;
  589. }
  590. usb_fill_int_urb(urb, hif_dev->udev,
  591. usb_rcvintpipe(hif_dev->udev,
  592. USB_REG_IN_PIPE),
  593. nskb->data, MAX_REG_IN_BUF_SIZE,
  594. ath9k_hif_usb_reg_in_cb, nskb, 1);
  595. }
  596. resubmit:
  597. usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
  598. ret = usb_submit_urb(urb, GFP_ATOMIC);
  599. if (ret) {
  600. usb_unanchor_urb(urb);
  601. goto free;
  602. }
  603. return;
  604. free:
  605. kfree_skb(skb);
  606. urb->context = NULL;
  607. }
  608. static void ath9k_hif_usb_dealloc_tx_urbs(struct hif_device_usb *hif_dev)
  609. {
  610. struct tx_buf *tx_buf = NULL, *tx_buf_tmp = NULL;
  611. unsigned long flags;
  612. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  613. &hif_dev->tx.tx_buf, list) {
  614. usb_kill_urb(tx_buf->urb);
  615. list_del(&tx_buf->list);
  616. usb_free_urb(tx_buf->urb);
  617. kfree(tx_buf->buf);
  618. kfree(tx_buf);
  619. }
  620. spin_lock_irqsave(&hif_dev->tx.tx_lock, flags);
  621. hif_dev->tx.flags |= HIF_USB_TX_FLUSH;
  622. spin_unlock_irqrestore(&hif_dev->tx.tx_lock, flags);
  623. list_for_each_entry_safe(tx_buf, tx_buf_tmp,
  624. &hif_dev->tx.tx_pending, list) {
  625. usb_kill_urb(tx_buf->urb);
  626. list_del(&tx_buf->list);
  627. usb_free_urb(tx_buf->urb);
  628. kfree(tx_buf->buf);
  629. kfree(tx_buf);
  630. }
  631. usb_kill_anchored_urbs(&hif_dev->mgmt_submitted);
  632. }
  633. static int ath9k_hif_usb_alloc_tx_urbs(struct hif_device_usb *hif_dev)
  634. {
  635. struct tx_buf *tx_buf;
  636. int i;
  637. INIT_LIST_HEAD(&hif_dev->tx.tx_buf);
  638. INIT_LIST_HEAD(&hif_dev->tx.tx_pending);
  639. spin_lock_init(&hif_dev->tx.tx_lock);
  640. __skb_queue_head_init(&hif_dev->tx.tx_skb_queue);
  641. init_usb_anchor(&hif_dev->mgmt_submitted);
  642. for (i = 0; i < MAX_TX_URB_NUM; i++) {
  643. tx_buf = kzalloc(sizeof(struct tx_buf), GFP_KERNEL);
  644. if (!tx_buf)
  645. goto err;
  646. tx_buf->buf = kzalloc(MAX_TX_BUF_SIZE, GFP_KERNEL);
  647. if (!tx_buf->buf)
  648. goto err;
  649. tx_buf->urb = usb_alloc_urb(0, GFP_KERNEL);
  650. if (!tx_buf->urb)
  651. goto err;
  652. tx_buf->hif_dev = hif_dev;
  653. __skb_queue_head_init(&tx_buf->skb_queue);
  654. list_add_tail(&tx_buf->list, &hif_dev->tx.tx_buf);
  655. }
  656. hif_dev->tx.tx_buf_cnt = MAX_TX_URB_NUM;
  657. return 0;
  658. err:
  659. if (tx_buf) {
  660. kfree(tx_buf->buf);
  661. kfree(tx_buf);
  662. }
  663. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  664. return -ENOMEM;
  665. }
  666. static void ath9k_hif_usb_dealloc_rx_urbs(struct hif_device_usb *hif_dev)
  667. {
  668. usb_kill_anchored_urbs(&hif_dev->rx_submitted);
  669. }
  670. static int ath9k_hif_usb_alloc_rx_urbs(struct hif_device_usb *hif_dev)
  671. {
  672. struct urb *urb = NULL;
  673. struct sk_buff *skb = NULL;
  674. int i, ret;
  675. init_usb_anchor(&hif_dev->rx_submitted);
  676. spin_lock_init(&hif_dev->rx_lock);
  677. for (i = 0; i < MAX_RX_URB_NUM; i++) {
  678. /* Allocate URB */
  679. urb = usb_alloc_urb(0, GFP_KERNEL);
  680. if (urb == NULL) {
  681. ret = -ENOMEM;
  682. goto err_urb;
  683. }
  684. /* Allocate buffer */
  685. skb = alloc_skb(MAX_RX_BUF_SIZE, GFP_KERNEL);
  686. if (!skb) {
  687. ret = -ENOMEM;
  688. goto err_skb;
  689. }
  690. usb_fill_bulk_urb(urb, hif_dev->udev,
  691. usb_rcvbulkpipe(hif_dev->udev,
  692. USB_WLAN_RX_PIPE),
  693. skb->data, MAX_RX_BUF_SIZE,
  694. ath9k_hif_usb_rx_cb, skb);
  695. /* Anchor URB */
  696. usb_anchor_urb(urb, &hif_dev->rx_submitted);
  697. /* Submit URB */
  698. ret = usb_submit_urb(urb, GFP_KERNEL);
  699. if (ret) {
  700. usb_unanchor_urb(urb);
  701. goto err_submit;
  702. }
  703. /*
  704. * Drop reference count.
  705. * This ensures that the URB is freed when killing them.
  706. */
  707. usb_free_urb(urb);
  708. }
  709. return 0;
  710. err_submit:
  711. kfree_skb(skb);
  712. err_skb:
  713. usb_free_urb(urb);
  714. err_urb:
  715. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  716. return ret;
  717. }
  718. static void ath9k_hif_usb_dealloc_reg_in_urbs(struct hif_device_usb *hif_dev)
  719. {
  720. usb_kill_anchored_urbs(&hif_dev->reg_in_submitted);
  721. }
  722. static int ath9k_hif_usb_alloc_reg_in_urbs(struct hif_device_usb *hif_dev)
  723. {
  724. struct urb *urb = NULL;
  725. struct sk_buff *skb = NULL;
  726. int i, ret;
  727. init_usb_anchor(&hif_dev->reg_in_submitted);
  728. for (i = 0; i < MAX_REG_IN_URB_NUM; i++) {
  729. /* Allocate URB */
  730. urb = usb_alloc_urb(0, GFP_KERNEL);
  731. if (urb == NULL) {
  732. ret = -ENOMEM;
  733. goto err_urb;
  734. }
  735. /* Allocate buffer */
  736. skb = alloc_skb(MAX_REG_IN_BUF_SIZE, GFP_KERNEL);
  737. if (!skb) {
  738. ret = -ENOMEM;
  739. goto err_skb;
  740. }
  741. usb_fill_int_urb(urb, hif_dev->udev,
  742. usb_rcvintpipe(hif_dev->udev,
  743. USB_REG_IN_PIPE),
  744. skb->data, MAX_REG_IN_BUF_SIZE,
  745. ath9k_hif_usb_reg_in_cb, skb, 1);
  746. /* Anchor URB */
  747. usb_anchor_urb(urb, &hif_dev->reg_in_submitted);
  748. /* Submit URB */
  749. ret = usb_submit_urb(urb, GFP_KERNEL);
  750. if (ret) {
  751. usb_unanchor_urb(urb);
  752. goto err_submit;
  753. }
  754. /*
  755. * Drop reference count.
  756. * This ensures that the URB is freed when killing them.
  757. */
  758. usb_free_urb(urb);
  759. }
  760. return 0;
  761. err_submit:
  762. kfree_skb(skb);
  763. err_skb:
  764. usb_free_urb(urb);
  765. err_urb:
  766. ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
  767. return ret;
  768. }
  769. static int ath9k_hif_usb_alloc_urbs(struct hif_device_usb *hif_dev)
  770. {
  771. /* Register Write */
  772. init_usb_anchor(&hif_dev->regout_submitted);
  773. /* TX */
  774. if (ath9k_hif_usb_alloc_tx_urbs(hif_dev) < 0)
  775. goto err;
  776. /* RX */
  777. if (ath9k_hif_usb_alloc_rx_urbs(hif_dev) < 0)
  778. goto err_rx;
  779. /* Register Read */
  780. if (ath9k_hif_usb_alloc_reg_in_urbs(hif_dev) < 0)
  781. goto err_reg;
  782. return 0;
  783. err_reg:
  784. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  785. err_rx:
  786. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  787. err:
  788. return -ENOMEM;
  789. }
  790. static void ath9k_hif_usb_dealloc_urbs(struct hif_device_usb *hif_dev)
  791. {
  792. usb_kill_anchored_urbs(&hif_dev->regout_submitted);
  793. ath9k_hif_usb_dealloc_reg_in_urbs(hif_dev);
  794. ath9k_hif_usb_dealloc_tx_urbs(hif_dev);
  795. ath9k_hif_usb_dealloc_rx_urbs(hif_dev);
  796. }
  797. static int ath9k_hif_usb_download_fw(struct hif_device_usb *hif_dev)
  798. {
  799. int transfer, err;
  800. const void *data = hif_dev->fw_data;
  801. size_t len = hif_dev->fw_size;
  802. u32 addr = AR9271_FIRMWARE;
  803. u8 *buf = kzalloc(4096, GFP_KERNEL);
  804. u32 firm_offset;
  805. if (!buf)
  806. return -ENOMEM;
  807. while (len) {
  808. transfer = min_t(size_t, len, 4096);
  809. memcpy(buf, data, transfer);
  810. err = usb_control_msg(hif_dev->udev,
  811. usb_sndctrlpipe(hif_dev->udev, 0),
  812. FIRMWARE_DOWNLOAD, 0x40 | USB_DIR_OUT,
  813. addr >> 8, 0, buf, transfer,
  814. USB_MSG_TIMEOUT);
  815. if (err < 0) {
  816. kfree(buf);
  817. return err;
  818. }
  819. len -= transfer;
  820. data += transfer;
  821. addr += transfer;
  822. }
  823. kfree(buf);
  824. if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
  825. firm_offset = AR7010_FIRMWARE_TEXT;
  826. else
  827. firm_offset = AR9271_FIRMWARE_TEXT;
  828. /*
  829. * Issue FW download complete command to firmware.
  830. */
  831. err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
  832. FIRMWARE_DOWNLOAD_COMP,
  833. 0x40 | USB_DIR_OUT,
  834. firm_offset >> 8, 0, NULL, 0, USB_MSG_TIMEOUT);
  835. if (err)
  836. return -EIO;
  837. dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
  838. hif_dev->fw_name, (unsigned long) hif_dev->fw_size);
  839. return 0;
  840. }
  841. static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
  842. {
  843. int ret;
  844. ret = ath9k_hif_usb_download_fw(hif_dev);
  845. if (ret) {
  846. dev_err(&hif_dev->udev->dev,
  847. "ath9k_htc: Firmware - %s download failed\n",
  848. hif_dev->fw_name);
  849. return ret;
  850. }
  851. /* Alloc URBs */
  852. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  853. if (ret) {
  854. dev_err(&hif_dev->udev->dev,
  855. "ath9k_htc: Unable to allocate URBs\n");
  856. return ret;
  857. }
  858. return 0;
  859. }
  860. static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
  861. {
  862. ath9k_hif_usb_dealloc_urbs(hif_dev);
  863. }
  864. /*
  865. * If initialization fails or the FW cannot be retrieved,
  866. * detach the device.
  867. */
  868. static void ath9k_hif_usb_firmware_fail(struct hif_device_usb *hif_dev)
  869. {
  870. struct device *dev = &hif_dev->udev->dev;
  871. struct device *parent = dev->parent;
  872. complete_all(&hif_dev->fw_done);
  873. if (parent)
  874. device_lock(parent);
  875. device_release_driver(dev);
  876. if (parent)
  877. device_unlock(parent);
  878. }
  879. static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context);
  880. /* taken from iwlwifi */
  881. static int ath9k_hif_request_firmware(struct hif_device_usb *hif_dev,
  882. bool first)
  883. {
  884. char index[8], *chip;
  885. int ret;
  886. if (first) {
  887. if (htc_use_dev_fw) {
  888. hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX + 1;
  889. sprintf(index, "%s", "dev");
  890. } else {
  891. hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX;
  892. sprintf(index, "%d", hif_dev->fw_minor_index);
  893. }
  894. } else {
  895. hif_dev->fw_minor_index--;
  896. sprintf(index, "%d", hif_dev->fw_minor_index);
  897. }
  898. /* test for FW 1.3 */
  899. if (MAJOR_VERSION_REQ == 1 && hif_dev->fw_minor_index == 3) {
  900. const char *filename;
  901. if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
  902. filename = FIRMWARE_AR7010_1_1;
  903. else
  904. filename = FIRMWARE_AR9271;
  905. /* expected fw locations:
  906. * - htc_9271.fw (stable version 1.3, depricated)
  907. */
  908. snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
  909. "%s", filename);
  910. } else if (hif_dev->fw_minor_index < FIRMWARE_MINOR_IDX_MIN) {
  911. dev_err(&hif_dev->udev->dev, "no suitable firmware found!\n");
  912. return -ENOENT;
  913. } else {
  914. if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
  915. chip = "7010";
  916. else
  917. chip = "9271";
  918. /* expected fw locations:
  919. * - ath9k_htc/htc_9271-1.dev.0.fw (development version)
  920. * - ath9k_htc/htc_9271-1.4.0.fw (stable version)
  921. */
  922. snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
  923. "%s/htc_%s-%d.%s.0.fw", HTC_FW_PATH,
  924. chip, MAJOR_VERSION_REQ, index);
  925. }
  926. ret = request_firmware_nowait(THIS_MODULE, true, hif_dev->fw_name,
  927. &hif_dev->udev->dev, GFP_KERNEL,
  928. hif_dev, ath9k_hif_usb_firmware_cb);
  929. if (ret) {
  930. dev_err(&hif_dev->udev->dev,
  931. "ath9k_htc: Async request for firmware %s failed\n",
  932. hif_dev->fw_name);
  933. return ret;
  934. }
  935. dev_info(&hif_dev->udev->dev, "ath9k_htc: Firmware %s requested\n",
  936. hif_dev->fw_name);
  937. return ret;
  938. }
  939. static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context)
  940. {
  941. struct hif_device_usb *hif_dev = context;
  942. int ret;
  943. if (!fw) {
  944. ret = ath9k_hif_request_firmware(hif_dev, false);
  945. if (!ret)
  946. return;
  947. dev_err(&hif_dev->udev->dev,
  948. "ath9k_htc: Failed to get firmware %s\n",
  949. hif_dev->fw_name);
  950. goto err_fw;
  951. }
  952. hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
  953. &hif_dev->udev->dev);
  954. if (hif_dev->htc_handle == NULL)
  955. goto err_dev_alloc;
  956. hif_dev->fw_data = fw->data;
  957. hif_dev->fw_size = fw->size;
  958. /* Proceed with initialization */
  959. ret = ath9k_hif_usb_dev_init(hif_dev);
  960. if (ret)
  961. goto err_dev_init;
  962. ret = ath9k_htc_hw_init(hif_dev->htc_handle,
  963. &hif_dev->interface->dev,
  964. hif_dev->usb_device_id->idProduct,
  965. hif_dev->udev->product,
  966. hif_dev->usb_device_id->driver_info);
  967. if (ret) {
  968. ret = -EINVAL;
  969. goto err_htc_hw_init;
  970. }
  971. release_firmware(fw);
  972. hif_dev->flags |= HIF_USB_READY;
  973. complete_all(&hif_dev->fw_done);
  974. return;
  975. err_htc_hw_init:
  976. ath9k_hif_usb_dev_deinit(hif_dev);
  977. err_dev_init:
  978. ath9k_htc_hw_free(hif_dev->htc_handle);
  979. err_dev_alloc:
  980. release_firmware(fw);
  981. err_fw:
  982. ath9k_hif_usb_firmware_fail(hif_dev);
  983. }
  984. /*
  985. * An exact copy of the function from zd1211rw.
  986. */
  987. static int send_eject_command(struct usb_interface *interface)
  988. {
  989. struct usb_device *udev = interface_to_usbdev(interface);
  990. struct usb_host_interface *iface_desc = &interface->altsetting[0];
  991. struct usb_endpoint_descriptor *endpoint;
  992. unsigned char *cmd;
  993. u8 bulk_out_ep;
  994. int r;
  995. if (iface_desc->desc.bNumEndpoints < 2)
  996. return -ENODEV;
  997. /* Find bulk out endpoint */
  998. for (r = 1; r >= 0; r--) {
  999. endpoint = &iface_desc->endpoint[r].desc;
  1000. if (usb_endpoint_dir_out(endpoint) &&
  1001. usb_endpoint_xfer_bulk(endpoint)) {
  1002. bulk_out_ep = endpoint->bEndpointAddress;
  1003. break;
  1004. }
  1005. }
  1006. if (r == -1) {
  1007. dev_err(&udev->dev,
  1008. "ath9k_htc: Could not find bulk out endpoint\n");
  1009. return -ENODEV;
  1010. }
  1011. cmd = kzalloc(31, GFP_KERNEL);
  1012. if (cmd == NULL)
  1013. return -ENODEV;
  1014. /* USB bulk command block */
  1015. cmd[0] = 0x55; /* bulk command signature */
  1016. cmd[1] = 0x53; /* bulk command signature */
  1017. cmd[2] = 0x42; /* bulk command signature */
  1018. cmd[3] = 0x43; /* bulk command signature */
  1019. cmd[14] = 6; /* command length */
  1020. cmd[15] = 0x1b; /* SCSI command: START STOP UNIT */
  1021. cmd[19] = 0x2; /* eject disc */
  1022. dev_info(&udev->dev, "Ejecting storage device...\n");
  1023. r = usb_bulk_msg(udev, usb_sndbulkpipe(udev, bulk_out_ep),
  1024. cmd, 31, NULL, 2 * USB_MSG_TIMEOUT);
  1025. kfree(cmd);
  1026. if (r)
  1027. return r;
  1028. /* At this point, the device disconnects and reconnects with the real
  1029. * ID numbers. */
  1030. usb_set_intfdata(interface, NULL);
  1031. return 0;
  1032. }
  1033. static int ath9k_hif_usb_probe(struct usb_interface *interface,
  1034. const struct usb_device_id *id)
  1035. {
  1036. struct usb_device *udev = interface_to_usbdev(interface);
  1037. struct hif_device_usb *hif_dev;
  1038. int ret = 0;
  1039. if (id->driver_info == STORAGE_DEVICE)
  1040. return send_eject_command(interface);
  1041. hif_dev = kzalloc(sizeof(struct hif_device_usb), GFP_KERNEL);
  1042. if (!hif_dev) {
  1043. ret = -ENOMEM;
  1044. goto err_alloc;
  1045. }
  1046. usb_get_dev(udev);
  1047. hif_dev->udev = udev;
  1048. hif_dev->interface = interface;
  1049. hif_dev->usb_device_id = id;
  1050. #ifdef CONFIG_PM
  1051. udev->reset_resume = 1;
  1052. #endif
  1053. usb_set_intfdata(interface, hif_dev);
  1054. init_completion(&hif_dev->fw_done);
  1055. ret = ath9k_hif_request_firmware(hif_dev, true);
  1056. if (ret)
  1057. goto err_fw_req;
  1058. return ret;
  1059. err_fw_req:
  1060. usb_set_intfdata(interface, NULL);
  1061. kfree(hif_dev);
  1062. usb_put_dev(udev);
  1063. err_alloc:
  1064. return ret;
  1065. }
  1066. static void ath9k_hif_usb_reboot(struct usb_device *udev)
  1067. {
  1068. u32 reboot_cmd = 0xffffffff;
  1069. void *buf;
  1070. int ret;
  1071. buf = kmemdup(&reboot_cmd, 4, GFP_KERNEL);
  1072. if (!buf)
  1073. return;
  1074. ret = usb_interrupt_msg(udev, usb_sndintpipe(udev, USB_REG_OUT_PIPE),
  1075. buf, 4, NULL, USB_MSG_TIMEOUT);
  1076. if (ret)
  1077. dev_err(&udev->dev, "ath9k_htc: USB reboot failed\n");
  1078. kfree(buf);
  1079. }
  1080. static void ath9k_hif_usb_disconnect(struct usb_interface *interface)
  1081. {
  1082. struct usb_device *udev = interface_to_usbdev(interface);
  1083. struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
  1084. bool unplugged = (udev->state == USB_STATE_NOTATTACHED) ? true : false;
  1085. if (!hif_dev)
  1086. return;
  1087. wait_for_completion(&hif_dev->fw_done);
  1088. if (hif_dev->flags & HIF_USB_READY) {
  1089. ath9k_htc_hw_deinit(hif_dev->htc_handle, unplugged);
  1090. ath9k_htc_hw_free(hif_dev->htc_handle);
  1091. ath9k_hif_usb_dev_deinit(hif_dev);
  1092. }
  1093. usb_set_intfdata(interface, NULL);
  1094. /* If firmware was loaded we should drop it
  1095. * go back to first stage bootloader. */
  1096. if (!unplugged && (hif_dev->flags & HIF_USB_READY))
  1097. ath9k_hif_usb_reboot(udev);
  1098. kfree(hif_dev);
  1099. dev_info(&udev->dev, "ath9k_htc: USB layer deinitialized\n");
  1100. usb_put_dev(udev);
  1101. }
  1102. #ifdef CONFIG_PM
  1103. static int ath9k_hif_usb_suspend(struct usb_interface *interface,
  1104. pm_message_t message)
  1105. {
  1106. struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
  1107. /*
  1108. * The device has to be set to FULLSLEEP mode in case no
  1109. * interface is up.
  1110. */
  1111. if (!(hif_dev->flags & HIF_USB_START))
  1112. ath9k_htc_suspend(hif_dev->htc_handle);
  1113. wait_for_completion(&hif_dev->fw_done);
  1114. if (hif_dev->flags & HIF_USB_READY)
  1115. ath9k_hif_usb_dealloc_urbs(hif_dev);
  1116. return 0;
  1117. }
  1118. static int ath9k_hif_usb_resume(struct usb_interface *interface)
  1119. {
  1120. struct hif_device_usb *hif_dev = usb_get_intfdata(interface);
  1121. struct htc_target *htc_handle = hif_dev->htc_handle;
  1122. int ret;
  1123. const struct firmware *fw;
  1124. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  1125. if (ret)
  1126. return ret;
  1127. if (hif_dev->flags & HIF_USB_READY) {
  1128. /* request cached firmware during suspend/resume cycle */
  1129. ret = request_firmware(&fw, hif_dev->fw_name,
  1130. &hif_dev->udev->dev);
  1131. if (ret)
  1132. goto fail_resume;
  1133. hif_dev->fw_data = fw->data;
  1134. hif_dev->fw_size = fw->size;
  1135. ret = ath9k_hif_usb_download_fw(hif_dev);
  1136. release_firmware(fw);
  1137. if (ret)
  1138. goto fail_resume;
  1139. } else {
  1140. ath9k_hif_usb_dealloc_urbs(hif_dev);
  1141. return -EIO;
  1142. }
  1143. mdelay(100);
  1144. ret = ath9k_htc_resume(htc_handle);
  1145. if (ret)
  1146. goto fail_resume;
  1147. return 0;
  1148. fail_resume:
  1149. ath9k_hif_usb_dealloc_urbs(hif_dev);
  1150. return ret;
  1151. }
  1152. #endif
  1153. static struct usb_driver ath9k_hif_usb_driver = {
  1154. .name = KBUILD_MODNAME,
  1155. .probe = ath9k_hif_usb_probe,
  1156. .disconnect = ath9k_hif_usb_disconnect,
  1157. #ifdef CONFIG_PM
  1158. .suspend = ath9k_hif_usb_suspend,
  1159. .resume = ath9k_hif_usb_resume,
  1160. .reset_resume = ath9k_hif_usb_resume,
  1161. #endif
  1162. .id_table = ath9k_hif_usb_ids,
  1163. .soft_unbind = 1,
  1164. .disable_hub_initiated_lpm = 1,
  1165. };
  1166. int ath9k_hif_usb_init(void)
  1167. {
  1168. return usb_register(&ath9k_hif_usb_driver);
  1169. }
  1170. void ath9k_hif_usb_exit(void)
  1171. {
  1172. usb_deregister(&ath9k_hif_usb_driver);
  1173. }