hif_usb.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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, 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,
  813. USB_MSG_TIMEOUT);
  814. if (err < 0) {
  815. kfree(buf);
  816. return err;
  817. }
  818. len -= transfer;
  819. data += transfer;
  820. addr += transfer;
  821. }
  822. kfree(buf);
  823. if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
  824. firm_offset = AR7010_FIRMWARE_TEXT;
  825. else
  826. firm_offset = AR9271_FIRMWARE_TEXT;
  827. /*
  828. * Issue FW download complete command to firmware.
  829. */
  830. err = usb_control_msg(hif_dev->udev, usb_sndctrlpipe(hif_dev->udev, 0),
  831. FIRMWARE_DOWNLOAD_COMP,
  832. 0x40 | USB_DIR_OUT,
  833. firm_offset >> 8, 0, NULL, 0, USB_MSG_TIMEOUT);
  834. if (err)
  835. return -EIO;
  836. dev_info(&hif_dev->udev->dev, "ath9k_htc: Transferred FW: %s, size: %ld\n",
  837. hif_dev->fw_name, (unsigned long) hif_dev->fw_size);
  838. return 0;
  839. }
  840. static int ath9k_hif_usb_dev_init(struct hif_device_usb *hif_dev)
  841. {
  842. int ret;
  843. ret = ath9k_hif_usb_download_fw(hif_dev);
  844. if (ret) {
  845. dev_err(&hif_dev->udev->dev,
  846. "ath9k_htc: Firmware - %s download failed\n",
  847. hif_dev->fw_name);
  848. return ret;
  849. }
  850. /* Alloc URBs */
  851. ret = ath9k_hif_usb_alloc_urbs(hif_dev);
  852. if (ret) {
  853. dev_err(&hif_dev->udev->dev,
  854. "ath9k_htc: Unable to allocate URBs\n");
  855. return ret;
  856. }
  857. return 0;
  858. }
  859. static void ath9k_hif_usb_dev_deinit(struct hif_device_usb *hif_dev)
  860. {
  861. ath9k_hif_usb_dealloc_urbs(hif_dev);
  862. }
  863. /*
  864. * If initialization fails or the FW cannot be retrieved,
  865. * detach the device.
  866. */
  867. static void ath9k_hif_usb_firmware_fail(struct hif_device_usb *hif_dev)
  868. {
  869. struct device *dev = &hif_dev->udev->dev;
  870. struct device *parent = dev->parent;
  871. complete_all(&hif_dev->fw_done);
  872. if (parent)
  873. device_lock(parent);
  874. device_release_driver(dev);
  875. if (parent)
  876. device_unlock(parent);
  877. }
  878. static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context);
  879. /* taken from iwlwifi */
  880. static int ath9k_hif_request_firmware(struct hif_device_usb *hif_dev,
  881. bool first)
  882. {
  883. char index[8], *chip;
  884. int ret;
  885. if (first) {
  886. if (htc_use_dev_fw) {
  887. hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX + 1;
  888. sprintf(index, "%s", "dev");
  889. } else {
  890. hif_dev->fw_minor_index = FIRMWARE_MINOR_IDX_MAX;
  891. sprintf(index, "%d", hif_dev->fw_minor_index);
  892. }
  893. } else {
  894. hif_dev->fw_minor_index--;
  895. sprintf(index, "%d", hif_dev->fw_minor_index);
  896. }
  897. /* test for FW 1.3 */
  898. if (MAJOR_VERSION_REQ == 1 && hif_dev->fw_minor_index == 3) {
  899. const char *filename;
  900. if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
  901. filename = FIRMWARE_AR7010_1_1;
  902. else
  903. filename = FIRMWARE_AR9271;
  904. /* expected fw locations:
  905. * - htc_9271.fw (stable version 1.3, depricated)
  906. */
  907. snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
  908. "%s", filename);
  909. } else if (hif_dev->fw_minor_index < FIRMWARE_MINOR_IDX_MIN) {
  910. dev_err(&hif_dev->udev->dev, "no suitable firmware found!\n");
  911. return -ENOENT;
  912. } else {
  913. if (IS_AR7010_DEVICE(hif_dev->usb_device_id->driver_info))
  914. chip = "7010";
  915. else
  916. chip = "9271";
  917. /* expected fw locations:
  918. * - ath9k_htc/htc_9271-1.dev.0.fw (development version)
  919. * - ath9k_htc/htc_9271-1.4.0.fw (stable version)
  920. */
  921. snprintf(hif_dev->fw_name, sizeof(hif_dev->fw_name),
  922. "%s/htc_%s-%d.%s.0.fw", HTC_FW_PATH,
  923. chip, MAJOR_VERSION_REQ, index);
  924. }
  925. ret = request_firmware_nowait(THIS_MODULE, true, hif_dev->fw_name,
  926. &hif_dev->udev->dev, GFP_KERNEL,
  927. hif_dev, ath9k_hif_usb_firmware_cb);
  928. if (ret) {
  929. dev_err(&hif_dev->udev->dev,
  930. "ath9k_htc: Async request for firmware %s failed\n",
  931. hif_dev->fw_name);
  932. return ret;
  933. }
  934. dev_info(&hif_dev->udev->dev, "ath9k_htc: Firmware %s requested\n",
  935. hif_dev->fw_name);
  936. return ret;
  937. }
  938. static void ath9k_hif_usb_firmware_cb(const struct firmware *fw, void *context)
  939. {
  940. struct hif_device_usb *hif_dev = context;
  941. int ret;
  942. if (!fw) {
  943. ret = ath9k_hif_request_firmware(hif_dev, false);
  944. if (!ret)
  945. return;
  946. dev_err(&hif_dev->udev->dev,
  947. "ath9k_htc: Failed to get firmware %s\n",
  948. hif_dev->fw_name);
  949. goto err_fw;
  950. }
  951. hif_dev->htc_handle = ath9k_htc_hw_alloc(hif_dev, &hif_usb,
  952. &hif_dev->udev->dev);
  953. if (hif_dev->htc_handle == NULL)
  954. goto err_dev_alloc;
  955. hif_dev->fw_data = fw->data;
  956. hif_dev->fw_size = fw->size;
  957. /* Proceed with initialization */
  958. ret = ath9k_hif_usb_dev_init(hif_dev);
  959. if (ret)
  960. goto err_dev_init;
  961. ret = ath9k_htc_hw_init(hif_dev->htc_handle,
  962. &hif_dev->interface->dev,
  963. hif_dev->usb_device_id->idProduct,
  964. hif_dev->udev->product,
  965. hif_dev->usb_device_id->driver_info);
  966. if (ret) {
  967. ret = -EINVAL;
  968. goto err_htc_hw_init;
  969. }
  970. release_firmware(fw);
  971. hif_dev->flags |= HIF_USB_READY;
  972. complete_all(&hif_dev->fw_done);
  973. return;
  974. err_htc_hw_init:
  975. ath9k_hif_usb_dev_deinit(hif_dev);
  976. err_dev_init:
  977. ath9k_htc_hw_free(hif_dev->htc_handle);
  978. err_dev_alloc:
  979. release_firmware(fw);
  980. err_fw:
  981. ath9k_hif_usb_firmware_fail(hif_dev);
  982. }
  983. /*
  984. * An exact copy of the function from zd1211rw.
  985. */
  986. static int send_eject_command(struct usb_interface *interface)
  987. {
  988. struct usb_device *udev = interface_to_usbdev(interface);
  989. struct usb_host_interface *iface_desc = &interface->altsetting[0];
  990. struct usb_endpoint_descriptor *endpoint;
  991. unsigned char *cmd;
  992. u8 bulk_out_ep;
  993. int r;
  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, 2 * USB_MSG_TIMEOUT);
  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, USB_MSG_TIMEOUT);
  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. }