hif_usb.c 30 KB

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