hif_usb.c 31 KB

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