hif_usb.c 32 KB

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