hif_usb.c 32 KB

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