ar5523.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  1. /*
  2. * Copyright (c) 2006 Damien Bergamini <damien.bergamini@free.fr>
  3. * Copyright (c) 2006 Sam Leffler, Errno Consulting
  4. * Copyright (c) 2007 Christoph Hellwig <hch@lst.de>
  5. * Copyright (c) 2008-2009 Weongyo Jeong <weongyo@freebsd.org>
  6. * Copyright (c) 2012 Pontus Fuchs <pontus.fuchs@gmail.com>
  7. *
  8. * Permission to use, copy, modify, and/or distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. */
  20. /*
  21. * This driver is based on the uath driver written by Damien Bergamini for
  22. * OpenBSD, who did black-box analysis of the Windows binary driver to find
  23. * out how the hardware works. It contains a lot magic numbers because of
  24. * that and only has minimal functionality.
  25. */
  26. #include <linux/compiler.h>
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/list.h>
  30. #include <linux/completion.h>
  31. #include <linux/firmware.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/usb.h>
  34. #include <net/mac80211.h>
  35. #include "ar5523.h"
  36. #include "ar5523_hw.h"
  37. /*
  38. * Various supported device vendors/products.
  39. * UB51: AR5005UG 802.11b/g, UB52: AR5005UX 802.11a/b/g
  40. */
  41. static int ar5523_submit_rx_cmd(struct ar5523 *ar);
  42. static void ar5523_data_tx_pkt_put(struct ar5523 *ar);
  43. static void ar5523_read_reply(struct ar5523 *ar, struct ar5523_cmd_hdr *hdr,
  44. struct ar5523_tx_cmd *cmd)
  45. {
  46. int dlen, olen;
  47. __be32 *rp;
  48. dlen = be32_to_cpu(hdr->len) - sizeof(*hdr);
  49. if (dlen < 0) {
  50. WARN_ON(1);
  51. goto out;
  52. }
  53. ar5523_dbg(ar, "Code = %d len = %d\n", be32_to_cpu(hdr->code) & 0xff,
  54. dlen);
  55. rp = (__be32 *)(hdr + 1);
  56. if (dlen >= sizeof(u32)) {
  57. olen = be32_to_cpu(rp[0]);
  58. dlen -= sizeof(u32);
  59. if (olen == 0) {
  60. /* convention is 0 =>'s one word */
  61. olen = sizeof(u32);
  62. }
  63. } else
  64. olen = 0;
  65. if (cmd->odata) {
  66. if (cmd->olen < olen) {
  67. ar5523_err(ar, "olen to small %d < %d\n",
  68. cmd->olen, olen);
  69. cmd->olen = 0;
  70. cmd->res = -EOVERFLOW;
  71. } else {
  72. cmd->olen = olen;
  73. memcpy(cmd->odata, &rp[1], olen);
  74. cmd->res = 0;
  75. }
  76. }
  77. out:
  78. complete(&cmd->done);
  79. }
  80. static void ar5523_cmd_rx_cb(struct urb *urb)
  81. {
  82. struct ar5523 *ar = urb->context;
  83. struct ar5523_tx_cmd *cmd = &ar->tx_cmd;
  84. struct ar5523_cmd_hdr *hdr = ar->rx_cmd_buf;
  85. int dlen;
  86. u32 code, hdrlen;
  87. if (urb->status) {
  88. if (urb->status != -ESHUTDOWN)
  89. ar5523_err(ar, "RX USB error %d.\n", urb->status);
  90. goto skip;
  91. }
  92. if (urb->actual_length < sizeof(struct ar5523_cmd_hdr)) {
  93. ar5523_err(ar, "RX USB to short.\n");
  94. goto skip;
  95. }
  96. ar5523_dbg(ar, "%s code %02x priv %d\n", __func__,
  97. be32_to_cpu(hdr->code) & 0xff, hdr->priv);
  98. code = be32_to_cpu(hdr->code);
  99. hdrlen = be32_to_cpu(hdr->len);
  100. switch (code & 0xff) {
  101. default:
  102. /* reply to a read command */
  103. if (hdr->priv != AR5523_CMD_ID) {
  104. ar5523_err(ar, "Unexpected command id: %02x\n",
  105. code & 0xff);
  106. goto skip;
  107. }
  108. ar5523_read_reply(ar, hdr, cmd);
  109. break;
  110. case WDCMSG_DEVICE_AVAIL:
  111. ar5523_dbg(ar, "WDCMSG_DEVICE_AVAIL\n");
  112. cmd->res = 0;
  113. cmd->olen = 0;
  114. complete(&cmd->done);
  115. break;
  116. case WDCMSG_SEND_COMPLETE:
  117. ar5523_dbg(ar, "WDCMSG_SEND_COMPLETE: %d pending\n",
  118. atomic_read(&ar->tx_nr_pending));
  119. if (!test_bit(AR5523_HW_UP, &ar->flags))
  120. ar5523_dbg(ar, "Unexpected WDCMSG_SEND_COMPLETE\n");
  121. else {
  122. mod_timer(&ar->tx_wd_timer,
  123. jiffies + AR5523_TX_WD_TIMEOUT);
  124. ar5523_data_tx_pkt_put(ar);
  125. }
  126. break;
  127. case WDCMSG_TARGET_START:
  128. /* This command returns a bogus id so it needs special
  129. handling */
  130. dlen = hdrlen - sizeof(*hdr);
  131. if (dlen != (int)sizeof(u32)) {
  132. ar5523_err(ar, "Invalid reply to WDCMSG_TARGET_START");
  133. return;
  134. }
  135. memcpy(cmd->odata, hdr + 1, sizeof(u32));
  136. cmd->olen = sizeof(u32);
  137. cmd->res = 0;
  138. complete(&cmd->done);
  139. break;
  140. case WDCMSG_STATS_UPDATE:
  141. ar5523_dbg(ar, "WDCMSG_STATS_UPDATE\n");
  142. break;
  143. }
  144. skip:
  145. ar5523_submit_rx_cmd(ar);
  146. }
  147. static int ar5523_alloc_rx_cmd(struct ar5523 *ar)
  148. {
  149. ar->rx_cmd_urb = usb_alloc_urb(0, GFP_KERNEL);
  150. if (!ar->rx_cmd_urb)
  151. return -ENOMEM;
  152. ar->rx_cmd_buf = usb_alloc_coherent(ar->dev, AR5523_MAX_RXCMDSZ,
  153. GFP_KERNEL,
  154. &ar->rx_cmd_urb->transfer_dma);
  155. if (!ar->rx_cmd_buf) {
  156. usb_free_urb(ar->rx_cmd_urb);
  157. return -ENOMEM;
  158. }
  159. return 0;
  160. }
  161. static void ar5523_cancel_rx_cmd(struct ar5523 *ar)
  162. {
  163. usb_kill_urb(ar->rx_cmd_urb);
  164. }
  165. static void ar5523_free_rx_cmd(struct ar5523 *ar)
  166. {
  167. usb_free_coherent(ar->dev, AR5523_MAX_RXCMDSZ,
  168. ar->rx_cmd_buf, ar->rx_cmd_urb->transfer_dma);
  169. usb_free_urb(ar->rx_cmd_urb);
  170. }
  171. static int ar5523_submit_rx_cmd(struct ar5523 *ar)
  172. {
  173. int error;
  174. usb_fill_bulk_urb(ar->rx_cmd_urb, ar->dev,
  175. ar5523_cmd_rx_pipe(ar->dev), ar->rx_cmd_buf,
  176. AR5523_MAX_RXCMDSZ, ar5523_cmd_rx_cb, ar);
  177. ar->rx_cmd_urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  178. error = usb_submit_urb(ar->rx_cmd_urb, GFP_ATOMIC);
  179. if (error) {
  180. if (error != -ENODEV)
  181. ar5523_err(ar, "error %d when submitting rx urb\n",
  182. error);
  183. return error;
  184. }
  185. return 0;
  186. }
  187. /*
  188. * Command submitted cb
  189. */
  190. static void ar5523_cmd_tx_cb(struct urb *urb)
  191. {
  192. struct ar5523_tx_cmd *cmd = urb->context;
  193. struct ar5523 *ar = cmd->ar;
  194. if (urb->status) {
  195. ar5523_err(ar, "Failed to TX command. Status = %d\n",
  196. urb->status);
  197. cmd->res = urb->status;
  198. complete(&cmd->done);
  199. return;
  200. }
  201. if (!(cmd->flags & AR5523_CMD_FLAG_READ)) {
  202. cmd->res = 0;
  203. complete(&cmd->done);
  204. }
  205. }
  206. static int ar5523_cmd(struct ar5523 *ar, u32 code, const void *idata,
  207. int ilen, void *odata, int olen, int flags)
  208. {
  209. struct ar5523_cmd_hdr *hdr;
  210. struct ar5523_tx_cmd *cmd = &ar->tx_cmd;
  211. int xferlen, error;
  212. /* always bulk-out a multiple of 4 bytes */
  213. xferlen = (sizeof(struct ar5523_cmd_hdr) + ilen + 3) & ~3;
  214. hdr = (struct ar5523_cmd_hdr *)cmd->buf_tx;
  215. memset(hdr, 0, sizeof(struct ar5523_cmd_hdr));
  216. hdr->len = cpu_to_be32(xferlen);
  217. hdr->code = cpu_to_be32(code);
  218. hdr->priv = AR5523_CMD_ID;
  219. if (flags & AR5523_CMD_FLAG_MAGIC)
  220. hdr->magic = cpu_to_be32(1 << 24);
  221. memcpy(hdr + 1, idata, ilen);
  222. cmd->odata = odata;
  223. cmd->olen = olen;
  224. cmd->flags = flags;
  225. ar5523_dbg(ar, "do cmd %02x\n", code);
  226. usb_fill_bulk_urb(cmd->urb_tx, ar->dev, ar5523_cmd_tx_pipe(ar->dev),
  227. cmd->buf_tx, xferlen, ar5523_cmd_tx_cb, cmd);
  228. cmd->urb_tx->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
  229. error = usb_submit_urb(cmd->urb_tx, GFP_KERNEL);
  230. if (error) {
  231. ar5523_err(ar, "could not send command 0x%x, error=%d\n",
  232. code, error);
  233. return error;
  234. }
  235. if (!wait_for_completion_timeout(&cmd->done, 2 * HZ)) {
  236. cmd->odata = NULL;
  237. ar5523_err(ar, "timeout waiting for command %02x reply\n",
  238. code);
  239. cmd->res = -ETIMEDOUT;
  240. }
  241. return cmd->res;
  242. }
  243. static int ar5523_cmd_write(struct ar5523 *ar, u32 code, const void *data,
  244. int len, int flags)
  245. {
  246. flags &= ~AR5523_CMD_FLAG_READ;
  247. return ar5523_cmd(ar, code, data, len, NULL, 0, flags);
  248. }
  249. static int ar5523_cmd_read(struct ar5523 *ar, u32 code, const void *idata,
  250. int ilen, void *odata, int olen, int flags)
  251. {
  252. flags |= AR5523_CMD_FLAG_READ;
  253. return ar5523_cmd(ar, code, idata, ilen, odata, olen, flags);
  254. }
  255. static int ar5523_config(struct ar5523 *ar, u32 reg, u32 val)
  256. {
  257. struct ar5523_write_mac write;
  258. int error;
  259. write.reg = cpu_to_be32(reg);
  260. write.len = cpu_to_be32(0); /* 0 = single write */
  261. *(__be32 *)write.data = cpu_to_be32(val);
  262. error = ar5523_cmd_write(ar, WDCMSG_TARGET_SET_CONFIG, &write,
  263. 3 * sizeof(u32), 0);
  264. if (error != 0)
  265. ar5523_err(ar, "could not write register 0x%02x\n", reg);
  266. return error;
  267. }
  268. static int ar5523_config_multi(struct ar5523 *ar, u32 reg, const void *data,
  269. int len)
  270. {
  271. struct ar5523_write_mac write;
  272. int error;
  273. write.reg = cpu_to_be32(reg);
  274. write.len = cpu_to_be32(len);
  275. memcpy(write.data, data, len);
  276. /* properly handle the case where len is zero (reset) */
  277. error = ar5523_cmd_write(ar, WDCMSG_TARGET_SET_CONFIG, &write,
  278. (len == 0) ? sizeof(u32) : 2 * sizeof(u32) + len, 0);
  279. if (error != 0)
  280. ar5523_err(ar, "could not write %d bytes to register 0x%02x\n",
  281. len, reg);
  282. return error;
  283. }
  284. static int ar5523_get_status(struct ar5523 *ar, u32 which, void *odata,
  285. int olen)
  286. {
  287. int error;
  288. __be32 which_be;
  289. which_be = cpu_to_be32(which);
  290. error = ar5523_cmd_read(ar, WDCMSG_TARGET_GET_STATUS,
  291. &which_be, sizeof(which_be), odata, olen, AR5523_CMD_FLAG_MAGIC);
  292. if (error != 0)
  293. ar5523_err(ar, "could not read EEPROM offset 0x%02x\n", which);
  294. return error;
  295. }
  296. static int ar5523_get_capability(struct ar5523 *ar, u32 cap, u32 *val)
  297. {
  298. int error;
  299. __be32 cap_be, val_be;
  300. cap_be = cpu_to_be32(cap);
  301. error = ar5523_cmd_read(ar, WDCMSG_TARGET_GET_CAPABILITY, &cap_be,
  302. sizeof(cap_be), &val_be, sizeof(__be32),
  303. AR5523_CMD_FLAG_MAGIC);
  304. if (error != 0) {
  305. ar5523_err(ar, "could not read capability %u\n", cap);
  306. return error;
  307. }
  308. *val = be32_to_cpu(val_be);
  309. return error;
  310. }
  311. static int ar5523_get_devcap(struct ar5523 *ar)
  312. {
  313. #define GETCAP(x) do { \
  314. error = ar5523_get_capability(ar, x, &cap); \
  315. if (error != 0) \
  316. return error; \
  317. ar5523_info(ar, "Cap: " \
  318. "%s=0x%08x\n", #x, cap); \
  319. } while (0)
  320. int error;
  321. u32 cap;
  322. /* collect device capabilities */
  323. GETCAP(CAP_TARGET_VERSION);
  324. GETCAP(CAP_TARGET_REVISION);
  325. GETCAP(CAP_MAC_VERSION);
  326. GETCAP(CAP_MAC_REVISION);
  327. GETCAP(CAP_PHY_REVISION);
  328. GETCAP(CAP_ANALOG_5GHz_REVISION);
  329. GETCAP(CAP_ANALOG_2GHz_REVISION);
  330. GETCAP(CAP_REG_DOMAIN);
  331. GETCAP(CAP_REG_CAP_BITS);
  332. GETCAP(CAP_WIRELESS_MODES);
  333. GETCAP(CAP_CHAN_SPREAD_SUPPORT);
  334. GETCAP(CAP_COMPRESS_SUPPORT);
  335. GETCAP(CAP_BURST_SUPPORT);
  336. GETCAP(CAP_FAST_FRAMES_SUPPORT);
  337. GETCAP(CAP_CHAP_TUNING_SUPPORT);
  338. GETCAP(CAP_TURBOG_SUPPORT);
  339. GETCAP(CAP_TURBO_PRIME_SUPPORT);
  340. GETCAP(CAP_DEVICE_TYPE);
  341. GETCAP(CAP_WME_SUPPORT);
  342. GETCAP(CAP_TOTAL_QUEUES);
  343. GETCAP(CAP_CONNECTION_ID_MAX);
  344. GETCAP(CAP_LOW_5GHZ_CHAN);
  345. GETCAP(CAP_HIGH_5GHZ_CHAN);
  346. GETCAP(CAP_LOW_2GHZ_CHAN);
  347. GETCAP(CAP_HIGH_2GHZ_CHAN);
  348. GETCAP(CAP_TWICE_ANTENNAGAIN_5G);
  349. GETCAP(CAP_TWICE_ANTENNAGAIN_2G);
  350. GETCAP(CAP_CIPHER_AES_CCM);
  351. GETCAP(CAP_CIPHER_TKIP);
  352. GETCAP(CAP_MIC_TKIP);
  353. return 0;
  354. }
  355. static int ar5523_set_ledsteady(struct ar5523 *ar, int lednum, int ledmode)
  356. {
  357. struct ar5523_cmd_ledsteady led;
  358. led.lednum = cpu_to_be32(lednum);
  359. led.ledmode = cpu_to_be32(ledmode);
  360. ar5523_dbg(ar, "set %s led %s (steady)\n",
  361. (lednum == UATH_LED_LINK) ? "link" : "activity",
  362. ledmode ? "on" : "off");
  363. return ar5523_cmd_write(ar, WDCMSG_SET_LED_STEADY, &led, sizeof(led),
  364. 0);
  365. }
  366. static int ar5523_set_rxfilter(struct ar5523 *ar, u32 bits, u32 op)
  367. {
  368. struct ar5523_cmd_rx_filter rxfilter;
  369. rxfilter.bits = cpu_to_be32(bits);
  370. rxfilter.op = cpu_to_be32(op);
  371. ar5523_dbg(ar, "setting Rx filter=0x%x flags=0x%x\n", bits, op);
  372. return ar5523_cmd_write(ar, WDCMSG_RX_FILTER, &rxfilter,
  373. sizeof(rxfilter), 0);
  374. }
  375. static int ar5523_reset_tx_queues(struct ar5523 *ar)
  376. {
  377. __be32 qid = cpu_to_be32(0);
  378. ar5523_dbg(ar, "resetting Tx queue\n");
  379. return ar5523_cmd_write(ar, WDCMSG_RELEASE_TX_QUEUE,
  380. &qid, sizeof(qid), 0);
  381. }
  382. static int ar5523_set_chan(struct ar5523 *ar)
  383. {
  384. struct ieee80211_conf *conf = &ar->hw->conf;
  385. struct ar5523_cmd_reset reset;
  386. memset(&reset, 0, sizeof(reset));
  387. reset.flags |= cpu_to_be32(UATH_CHAN_2GHZ);
  388. reset.flags |= cpu_to_be32(UATH_CHAN_OFDM);
  389. reset.freq = cpu_to_be32(conf->chandef.chan->center_freq);
  390. reset.maxrdpower = cpu_to_be32(50); /* XXX */
  391. reset.channelchange = cpu_to_be32(1);
  392. reset.keeprccontent = cpu_to_be32(0);
  393. ar5523_dbg(ar, "set chan flags 0x%x freq %d\n",
  394. be32_to_cpu(reset.flags),
  395. conf->chandef.chan->center_freq);
  396. return ar5523_cmd_write(ar, WDCMSG_RESET, &reset, sizeof(reset), 0);
  397. }
  398. static int ar5523_queue_init(struct ar5523 *ar)
  399. {
  400. struct ar5523_cmd_txq_setup qinfo;
  401. ar5523_dbg(ar, "setting up Tx queue\n");
  402. qinfo.qid = cpu_to_be32(0);
  403. qinfo.len = cpu_to_be32(sizeof(qinfo.attr));
  404. qinfo.attr.priority = cpu_to_be32(0); /* XXX */
  405. qinfo.attr.aifs = cpu_to_be32(3);
  406. qinfo.attr.logcwmin = cpu_to_be32(4);
  407. qinfo.attr.logcwmax = cpu_to_be32(10);
  408. qinfo.attr.bursttime = cpu_to_be32(0);
  409. qinfo.attr.mode = cpu_to_be32(0);
  410. qinfo.attr.qflags = cpu_to_be32(1); /* XXX? */
  411. return ar5523_cmd_write(ar, WDCMSG_SETUP_TX_QUEUE, &qinfo,
  412. sizeof(qinfo), 0);
  413. }
  414. static int ar5523_switch_chan(struct ar5523 *ar)
  415. {
  416. int error;
  417. error = ar5523_set_chan(ar);
  418. if (error) {
  419. ar5523_err(ar, "could not set chan, error %d\n", error);
  420. goto out_err;
  421. }
  422. /* reset Tx rings */
  423. error = ar5523_reset_tx_queues(ar);
  424. if (error) {
  425. ar5523_err(ar, "could not reset Tx queues, error %d\n",
  426. error);
  427. goto out_err;
  428. }
  429. /* set Tx rings WME properties */
  430. error = ar5523_queue_init(ar);
  431. if (error)
  432. ar5523_err(ar, "could not init wme, error %d\n", error);
  433. out_err:
  434. return error;
  435. }
  436. static void ar5523_rx_data_put(struct ar5523 *ar,
  437. struct ar5523_rx_data *data)
  438. {
  439. unsigned long flags;
  440. spin_lock_irqsave(&ar->rx_data_list_lock, flags);
  441. list_move(&data->list, &ar->rx_data_free);
  442. spin_unlock_irqrestore(&ar->rx_data_list_lock, flags);
  443. }
  444. static void ar5523_data_rx_cb(struct urb *urb)
  445. {
  446. struct ar5523_rx_data *data = urb->context;
  447. struct ar5523 *ar = data->ar;
  448. struct ar5523_rx_desc *desc;
  449. struct ar5523_chunk *chunk;
  450. struct ieee80211_hw *hw = ar->hw;
  451. struct ieee80211_rx_status *rx_status;
  452. u32 rxlen;
  453. int usblen = urb->actual_length;
  454. int hdrlen, pad;
  455. ar5523_dbg(ar, "%s\n", __func__);
  456. /* sync/async unlink faults aren't errors */
  457. if (urb->status) {
  458. if (urb->status != -ESHUTDOWN)
  459. ar5523_err(ar, "%s: USB err: %d\n", __func__,
  460. urb->status);
  461. goto skip;
  462. }
  463. if (usblen < AR5523_MIN_RXBUFSZ) {
  464. ar5523_err(ar, "RX: wrong xfer size (usblen=%d)\n", usblen);
  465. goto skip;
  466. }
  467. chunk = (struct ar5523_chunk *) data->skb->data;
  468. if (((chunk->flags & UATH_CFLAGS_FINAL) == 0) ||
  469. chunk->seqnum != 0) {
  470. ar5523_dbg(ar, "RX: No final flag. s: %d f: %02x l: %d\n",
  471. chunk->seqnum, chunk->flags,
  472. be16_to_cpu(chunk->length));
  473. goto skip;
  474. }
  475. /* Rx descriptor is located at the end, 32-bit aligned */
  476. desc = (struct ar5523_rx_desc *)
  477. (data->skb->data + usblen - sizeof(struct ar5523_rx_desc));
  478. rxlen = be32_to_cpu(desc->len);
  479. if (rxlen > ar->rxbufsz) {
  480. ar5523_dbg(ar, "RX: Bad descriptor (len=%d)\n",
  481. be32_to_cpu(desc->len));
  482. goto skip;
  483. }
  484. if (!rxlen) {
  485. ar5523_dbg(ar, "RX: rxlen is 0\n");
  486. goto skip;
  487. }
  488. if (be32_to_cpu(desc->status) != 0) {
  489. ar5523_dbg(ar, "Bad RX status (0x%x len = %d). Skip\n",
  490. be32_to_cpu(desc->status), be32_to_cpu(desc->len));
  491. goto skip;
  492. }
  493. skb_reserve(data->skb, sizeof(*chunk));
  494. skb_put(data->skb, rxlen - sizeof(struct ar5523_rx_desc));
  495. hdrlen = ieee80211_get_hdrlen_from_skb(data->skb);
  496. if (!IS_ALIGNED(hdrlen, 4)) {
  497. ar5523_dbg(ar, "eek, alignment workaround activated\n");
  498. pad = ALIGN(hdrlen, 4) - hdrlen;
  499. memmove(data->skb->data + pad, data->skb->data, hdrlen);
  500. skb_pull(data->skb, pad);
  501. skb_put(data->skb, pad);
  502. }
  503. rx_status = IEEE80211_SKB_RXCB(data->skb);
  504. memset(rx_status, 0, sizeof(*rx_status));
  505. rx_status->freq = be32_to_cpu(desc->channel);
  506. rx_status->band = hw->conf.chandef.chan->band;
  507. rx_status->signal = -95 + be32_to_cpu(desc->rssi);
  508. ieee80211_rx_irqsafe(hw, data->skb);
  509. data->skb = NULL;
  510. skip:
  511. if (data->skb) {
  512. dev_kfree_skb_irq(data->skb);
  513. data->skb = NULL;
  514. }
  515. ar5523_rx_data_put(ar, data);
  516. if (atomic_inc_return(&ar->rx_data_free_cnt) >=
  517. AR5523_RX_DATA_REFILL_COUNT &&
  518. test_bit(AR5523_HW_UP, &ar->flags))
  519. queue_work(ar->wq, &ar->rx_refill_work);
  520. }
  521. static void ar5523_rx_refill_work(struct work_struct *work)
  522. {
  523. struct ar5523 *ar = container_of(work, struct ar5523, rx_refill_work);
  524. struct ar5523_rx_data *data;
  525. unsigned long flags;
  526. int error;
  527. ar5523_dbg(ar, "%s\n", __func__);
  528. do {
  529. spin_lock_irqsave(&ar->rx_data_list_lock, flags);
  530. if (!list_empty(&ar->rx_data_free))
  531. data = (struct ar5523_rx_data *) ar->rx_data_free.next;
  532. else
  533. data = NULL;
  534. spin_unlock_irqrestore(&ar->rx_data_list_lock, flags);
  535. if (!data)
  536. goto done;
  537. data->skb = alloc_skb(ar->rxbufsz, GFP_KERNEL);
  538. if (!data->skb) {
  539. ar5523_err(ar, "could not allocate rx skbuff\n");
  540. return;
  541. }
  542. usb_fill_bulk_urb(data->urb, ar->dev,
  543. ar5523_data_rx_pipe(ar->dev), data->skb->data,
  544. ar->rxbufsz, ar5523_data_rx_cb, data);
  545. spin_lock_irqsave(&ar->rx_data_list_lock, flags);
  546. list_move(&data->list, &ar->rx_data_used);
  547. spin_unlock_irqrestore(&ar->rx_data_list_lock, flags);
  548. atomic_dec(&ar->rx_data_free_cnt);
  549. error = usb_submit_urb(data->urb, GFP_KERNEL);
  550. if (error) {
  551. kfree_skb(data->skb);
  552. if (error != -ENODEV)
  553. ar5523_err(ar, "Err sending rx data urb %d\n",
  554. error);
  555. ar5523_rx_data_put(ar, data);
  556. atomic_inc(&ar->rx_data_free_cnt);
  557. return;
  558. }
  559. } while (true);
  560. done:
  561. return;
  562. }
  563. static void ar5523_cancel_rx_bufs(struct ar5523 *ar)
  564. {
  565. struct ar5523_rx_data *data;
  566. unsigned long flags;
  567. do {
  568. spin_lock_irqsave(&ar->rx_data_list_lock, flags);
  569. if (!list_empty(&ar->rx_data_used))
  570. data = (struct ar5523_rx_data *) ar->rx_data_used.next;
  571. else
  572. data = NULL;
  573. spin_unlock_irqrestore(&ar->rx_data_list_lock, flags);
  574. if (!data)
  575. break;
  576. usb_kill_urb(data->urb);
  577. list_move(&data->list, &ar->rx_data_free);
  578. atomic_inc(&ar->rx_data_free_cnt);
  579. } while (data);
  580. }
  581. static void ar5523_free_rx_bufs(struct ar5523 *ar)
  582. {
  583. struct ar5523_rx_data *data;
  584. ar5523_cancel_rx_bufs(ar);
  585. while (!list_empty(&ar->rx_data_free)) {
  586. data = (struct ar5523_rx_data *) ar->rx_data_free.next;
  587. list_del(&data->list);
  588. usb_free_urb(data->urb);
  589. }
  590. }
  591. static int ar5523_alloc_rx_bufs(struct ar5523 *ar)
  592. {
  593. int i;
  594. for (i = 0; i < AR5523_RX_DATA_COUNT; i++) {
  595. struct ar5523_rx_data *data = &ar->rx_data[i];
  596. data->ar = ar;
  597. data->urb = usb_alloc_urb(0, GFP_KERNEL);
  598. if (!data->urb) {
  599. ar5523_err(ar, "could not allocate rx data urb\n");
  600. goto err;
  601. }
  602. list_add_tail(&data->list, &ar->rx_data_free);
  603. atomic_inc(&ar->rx_data_free_cnt);
  604. }
  605. return 0;
  606. err:
  607. ar5523_free_rx_bufs(ar);
  608. return -ENOMEM;
  609. }
  610. static void ar5523_data_tx_pkt_put(struct ar5523 *ar)
  611. {
  612. atomic_dec(&ar->tx_nr_total);
  613. if (!atomic_dec_return(&ar->tx_nr_pending)) {
  614. del_timer(&ar->tx_wd_timer);
  615. wake_up(&ar->tx_flush_waitq);
  616. }
  617. if (atomic_read(&ar->tx_nr_total) < AR5523_TX_DATA_RESTART_COUNT) {
  618. ar5523_dbg(ar, "restart tx queue\n");
  619. ieee80211_wake_queues(ar->hw);
  620. }
  621. }
  622. static void ar5523_data_tx_cb(struct urb *urb)
  623. {
  624. struct sk_buff *skb = urb->context;
  625. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  626. struct ar5523_tx_data *data = (struct ar5523_tx_data *)
  627. txi->driver_data;
  628. struct ar5523 *ar = data->ar;
  629. unsigned long flags;
  630. ar5523_dbg(ar, "data tx urb completed: %d\n", urb->status);
  631. spin_lock_irqsave(&ar->tx_data_list_lock, flags);
  632. list_del(&data->list);
  633. spin_unlock_irqrestore(&ar->tx_data_list_lock, flags);
  634. if (urb->status) {
  635. ar5523_dbg(ar, "%s: urb status: %d\n", __func__, urb->status);
  636. ar5523_data_tx_pkt_put(ar);
  637. ieee80211_free_txskb(ar->hw, skb);
  638. } else {
  639. skb_pull(skb, sizeof(struct ar5523_tx_desc) + sizeof(__be32));
  640. ieee80211_tx_status_irqsafe(ar->hw, skb);
  641. }
  642. usb_free_urb(urb);
  643. }
  644. static void ar5523_tx(struct ieee80211_hw *hw,
  645. struct ieee80211_tx_control *control,
  646. struct sk_buff *skb)
  647. {
  648. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  649. struct ar5523_tx_data *data = (struct ar5523_tx_data *)
  650. txi->driver_data;
  651. struct ar5523 *ar = hw->priv;
  652. unsigned long flags;
  653. ar5523_dbg(ar, "tx called\n");
  654. if (atomic_inc_return(&ar->tx_nr_total) >= AR5523_TX_DATA_COUNT) {
  655. ar5523_dbg(ar, "tx queue full\n");
  656. ar5523_dbg(ar, "stop queues (tot %d pend %d)\n",
  657. atomic_read(&ar->tx_nr_total),
  658. atomic_read(&ar->tx_nr_pending));
  659. ieee80211_stop_queues(hw);
  660. }
  661. spin_lock_irqsave(&ar->tx_data_list_lock, flags);
  662. list_add_tail(&data->list, &ar->tx_queue_pending);
  663. spin_unlock_irqrestore(&ar->tx_data_list_lock, flags);
  664. ieee80211_queue_work(ar->hw, &ar->tx_work);
  665. }
  666. static void ar5523_tx_work_locked(struct ar5523 *ar)
  667. {
  668. struct ar5523_tx_data *data;
  669. struct ar5523_tx_desc *desc;
  670. struct ar5523_chunk *chunk;
  671. struct ieee80211_tx_info *txi;
  672. struct urb *urb;
  673. struct sk_buff *skb;
  674. int error = 0, paylen;
  675. u32 txqid;
  676. unsigned long flags;
  677. BUILD_BUG_ON(sizeof(struct ar5523_tx_data) >
  678. IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
  679. ar5523_dbg(ar, "%s\n", __func__);
  680. do {
  681. spin_lock_irqsave(&ar->tx_data_list_lock, flags);
  682. if (!list_empty(&ar->tx_queue_pending)) {
  683. data = (struct ar5523_tx_data *)
  684. ar->tx_queue_pending.next;
  685. list_del(&data->list);
  686. } else
  687. data = NULL;
  688. spin_unlock_irqrestore(&ar->tx_data_list_lock, flags);
  689. if (!data)
  690. break;
  691. txi = container_of((void *)data, struct ieee80211_tx_info,
  692. driver_data);
  693. txqid = 0;
  694. skb = container_of((void *)txi, struct sk_buff, cb);
  695. paylen = skb->len;
  696. urb = usb_alloc_urb(0, GFP_KERNEL);
  697. if (!urb) {
  698. ar5523_err(ar, "Failed to allocate TX urb\n");
  699. ieee80211_free_txskb(ar->hw, skb);
  700. continue;
  701. }
  702. data->ar = ar;
  703. data->urb = urb;
  704. desc = (struct ar5523_tx_desc *)skb_push(skb, sizeof(*desc));
  705. chunk = (struct ar5523_chunk *)skb_push(skb, sizeof(*chunk));
  706. chunk->seqnum = 0;
  707. chunk->flags = UATH_CFLAGS_FINAL;
  708. chunk->length = cpu_to_be16(skb->len);
  709. desc->msglen = cpu_to_be32(skb->len);
  710. desc->msgid = AR5523_DATA_ID;
  711. desc->buflen = cpu_to_be32(paylen);
  712. desc->type = cpu_to_be32(WDCMSG_SEND);
  713. desc->flags = cpu_to_be32(UATH_TX_NOTIFY);
  714. if (test_bit(AR5523_CONNECTED, &ar->flags))
  715. desc->connid = cpu_to_be32(AR5523_ID_BSS);
  716. else
  717. desc->connid = cpu_to_be32(AR5523_ID_BROADCAST);
  718. if (txi->flags & IEEE80211_TX_CTL_USE_MINRATE)
  719. txqid |= UATH_TXQID_MINRATE;
  720. desc->txqid = cpu_to_be32(txqid);
  721. urb->transfer_flags = URB_ZERO_PACKET;
  722. usb_fill_bulk_urb(urb, ar->dev, ar5523_data_tx_pipe(ar->dev),
  723. skb->data, skb->len, ar5523_data_tx_cb, skb);
  724. spin_lock_irqsave(&ar->tx_data_list_lock, flags);
  725. list_add_tail(&data->list, &ar->tx_queue_submitted);
  726. spin_unlock_irqrestore(&ar->tx_data_list_lock, flags);
  727. mod_timer(&ar->tx_wd_timer, jiffies + AR5523_TX_WD_TIMEOUT);
  728. atomic_inc(&ar->tx_nr_pending);
  729. ar5523_dbg(ar, "TX Frame (%d pending)\n",
  730. atomic_read(&ar->tx_nr_pending));
  731. error = usb_submit_urb(urb, GFP_KERNEL);
  732. if (error) {
  733. ar5523_err(ar, "error %d when submitting tx urb\n",
  734. error);
  735. spin_lock_irqsave(&ar->tx_data_list_lock, flags);
  736. list_del(&data->list);
  737. spin_unlock_irqrestore(&ar->tx_data_list_lock, flags);
  738. atomic_dec(&ar->tx_nr_pending);
  739. ar5523_data_tx_pkt_put(ar);
  740. usb_free_urb(urb);
  741. ieee80211_free_txskb(ar->hw, skb);
  742. }
  743. } while (true);
  744. }
  745. static void ar5523_tx_work(struct work_struct *work)
  746. {
  747. struct ar5523 *ar = container_of(work, struct ar5523, tx_work);
  748. ar5523_dbg(ar, "%s\n", __func__);
  749. mutex_lock(&ar->mutex);
  750. ar5523_tx_work_locked(ar);
  751. mutex_unlock(&ar->mutex);
  752. }
  753. static void ar5523_tx_wd_timer(unsigned long arg)
  754. {
  755. struct ar5523 *ar = (struct ar5523 *) arg;
  756. ar5523_dbg(ar, "TX watchdog timer triggered\n");
  757. ieee80211_queue_work(ar->hw, &ar->tx_wd_work);
  758. }
  759. static void ar5523_tx_wd_work(struct work_struct *work)
  760. {
  761. struct ar5523 *ar = container_of(work, struct ar5523, tx_wd_work);
  762. /* Occasionally the TX queues stop responding. The only way to
  763. * recover seems to be to reset the dongle.
  764. */
  765. mutex_lock(&ar->mutex);
  766. ar5523_err(ar, "TX queue stuck (tot %d pend %d)\n",
  767. atomic_read(&ar->tx_nr_total),
  768. atomic_read(&ar->tx_nr_pending));
  769. ar5523_err(ar, "Will restart dongle.\n");
  770. ar5523_cmd_write(ar, WDCMSG_TARGET_RESET, NULL, 0, 0);
  771. mutex_unlock(&ar->mutex);
  772. }
  773. static void ar5523_flush_tx(struct ar5523 *ar)
  774. {
  775. ar5523_tx_work_locked(ar);
  776. /* Don't waste time trying to flush if USB is disconnected */
  777. if (test_bit(AR5523_USB_DISCONNECTED, &ar->flags))
  778. return;
  779. if (!wait_event_timeout(ar->tx_flush_waitq,
  780. !atomic_read(&ar->tx_nr_pending), AR5523_FLUSH_TIMEOUT))
  781. ar5523_err(ar, "flush timeout (tot %d pend %d)\n",
  782. atomic_read(&ar->tx_nr_total),
  783. atomic_read(&ar->tx_nr_pending));
  784. }
  785. static void ar5523_free_tx_cmd(struct ar5523 *ar)
  786. {
  787. struct ar5523_tx_cmd *cmd = &ar->tx_cmd;
  788. usb_free_coherent(ar->dev, AR5523_MAX_RXCMDSZ, cmd->buf_tx,
  789. cmd->urb_tx->transfer_dma);
  790. usb_free_urb(cmd->urb_tx);
  791. }
  792. static int ar5523_alloc_tx_cmd(struct ar5523 *ar)
  793. {
  794. struct ar5523_tx_cmd *cmd = &ar->tx_cmd;
  795. cmd->ar = ar;
  796. init_completion(&cmd->done);
  797. cmd->urb_tx = usb_alloc_urb(0, GFP_KERNEL);
  798. if (!cmd->urb_tx) {
  799. ar5523_err(ar, "could not allocate urb\n");
  800. return -ENOMEM;
  801. }
  802. cmd->buf_tx = usb_alloc_coherent(ar->dev, AR5523_MAX_TXCMDSZ,
  803. GFP_KERNEL,
  804. &cmd->urb_tx->transfer_dma);
  805. if (!cmd->buf_tx) {
  806. usb_free_urb(cmd->urb_tx);
  807. return -ENOMEM;
  808. }
  809. return 0;
  810. }
  811. /*
  812. * This function is called periodically (every second) when associated to
  813. * query device statistics.
  814. */
  815. static void ar5523_stat_work(struct work_struct *work)
  816. {
  817. struct ar5523 *ar = container_of(work, struct ar5523, stat_work.work);
  818. int error;
  819. ar5523_dbg(ar, "%s\n", __func__);
  820. mutex_lock(&ar->mutex);
  821. /*
  822. * Send request for statistics asynchronously once a second. This
  823. * seems to be important. Throughput is a lot better if this is done.
  824. */
  825. error = ar5523_cmd_write(ar, WDCMSG_TARGET_GET_STATS, NULL, 0, 0);
  826. if (error)
  827. ar5523_err(ar, "could not query stats, error %d\n", error);
  828. mutex_unlock(&ar->mutex);
  829. ieee80211_queue_delayed_work(ar->hw, &ar->stat_work, HZ);
  830. }
  831. /*
  832. * Interface routines to the mac80211 stack.
  833. */
  834. static int ar5523_start(struct ieee80211_hw *hw)
  835. {
  836. struct ar5523 *ar = hw->priv;
  837. int error;
  838. __be32 val;
  839. ar5523_dbg(ar, "start called\n");
  840. mutex_lock(&ar->mutex);
  841. val = cpu_to_be32(0);
  842. ar5523_cmd_write(ar, WDCMSG_BIND, &val, sizeof(val), 0);
  843. /* set MAC address */
  844. ar5523_config_multi(ar, CFG_MAC_ADDR, &ar->hw->wiphy->perm_addr,
  845. ETH_ALEN);
  846. /* XXX honor net80211 state */
  847. ar5523_config(ar, CFG_RATE_CONTROL_ENABLE, 0x00000001);
  848. ar5523_config(ar, CFG_DIVERSITY_CTL, 0x00000001);
  849. ar5523_config(ar, CFG_ABOLT, 0x0000003f);
  850. ar5523_config(ar, CFG_WME_ENABLED, 0x00000000);
  851. ar5523_config(ar, CFG_SERVICE_TYPE, 1);
  852. ar5523_config(ar, CFG_TP_SCALE, 0x00000000);
  853. ar5523_config(ar, CFG_TPC_HALF_DBM5, 0x0000003c);
  854. ar5523_config(ar, CFG_TPC_HALF_DBM2, 0x0000003c);
  855. ar5523_config(ar, CFG_OVERRD_TX_POWER, 0x00000000);
  856. ar5523_config(ar, CFG_GMODE_PROTECTION, 0x00000000);
  857. ar5523_config(ar, CFG_GMODE_PROTECT_RATE_INDEX, 0x00000003);
  858. ar5523_config(ar, CFG_PROTECTION_TYPE, 0x00000000);
  859. ar5523_config(ar, CFG_MODE_CTS, 0x00000002);
  860. error = ar5523_cmd_read(ar, WDCMSG_TARGET_START, NULL, 0,
  861. &val, sizeof(val), AR5523_CMD_FLAG_MAGIC);
  862. if (error) {
  863. ar5523_dbg(ar, "could not start target, error %d\n", error);
  864. goto err;
  865. }
  866. ar5523_dbg(ar, "WDCMSG_TARGET_START returns handle: 0x%x\n",
  867. be32_to_cpu(val));
  868. ar5523_switch_chan(ar);
  869. val = cpu_to_be32(TARGET_DEVICE_AWAKE);
  870. ar5523_cmd_write(ar, WDCMSG_SET_PWR_MODE, &val, sizeof(val), 0);
  871. /* XXX? check */
  872. ar5523_cmd_write(ar, WDCMSG_RESET_KEY_CACHE, NULL, 0, 0);
  873. set_bit(AR5523_HW_UP, &ar->flags);
  874. queue_work(ar->wq, &ar->rx_refill_work);
  875. /* enable Rx */
  876. ar5523_set_rxfilter(ar, 0, UATH_FILTER_OP_INIT);
  877. ar5523_set_rxfilter(ar,
  878. UATH_FILTER_RX_UCAST | UATH_FILTER_RX_MCAST |
  879. UATH_FILTER_RX_BCAST | UATH_FILTER_RX_BEACON,
  880. UATH_FILTER_OP_SET);
  881. ar5523_set_ledsteady(ar, UATH_LED_ACTIVITY, UATH_LED_ON);
  882. ar5523_dbg(ar, "start OK\n");
  883. err:
  884. mutex_unlock(&ar->mutex);
  885. return error;
  886. }
  887. static void ar5523_stop(struct ieee80211_hw *hw)
  888. {
  889. struct ar5523 *ar = hw->priv;
  890. ar5523_dbg(ar, "stop called\n");
  891. cancel_delayed_work_sync(&ar->stat_work);
  892. mutex_lock(&ar->mutex);
  893. clear_bit(AR5523_HW_UP, &ar->flags);
  894. ar5523_set_ledsteady(ar, UATH_LED_LINK, UATH_LED_OFF);
  895. ar5523_set_ledsteady(ar, UATH_LED_ACTIVITY, UATH_LED_OFF);
  896. ar5523_cmd_write(ar, WDCMSG_TARGET_STOP, NULL, 0, 0);
  897. del_timer_sync(&ar->tx_wd_timer);
  898. cancel_work_sync(&ar->tx_wd_work);
  899. cancel_work_sync(&ar->rx_refill_work);
  900. ar5523_cancel_rx_bufs(ar);
  901. mutex_unlock(&ar->mutex);
  902. }
  903. static int ar5523_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  904. {
  905. struct ar5523 *ar = hw->priv;
  906. int ret;
  907. ar5523_dbg(ar, "set_rts_threshold called\n");
  908. mutex_lock(&ar->mutex);
  909. ret = ar5523_config(ar, CFG_USER_RTS_THRESHOLD, value);
  910. mutex_unlock(&ar->mutex);
  911. return ret;
  912. }
  913. static void ar5523_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  914. u32 queues, bool drop)
  915. {
  916. struct ar5523 *ar = hw->priv;
  917. ar5523_dbg(ar, "flush called\n");
  918. ar5523_flush_tx(ar);
  919. }
  920. static int ar5523_add_interface(struct ieee80211_hw *hw,
  921. struct ieee80211_vif *vif)
  922. {
  923. struct ar5523 *ar = hw->priv;
  924. ar5523_dbg(ar, "add interface called\n");
  925. if (ar->vif) {
  926. ar5523_dbg(ar, "invalid add_interface\n");
  927. return -EOPNOTSUPP;
  928. }
  929. switch (vif->type) {
  930. case NL80211_IFTYPE_STATION:
  931. ar->vif = vif;
  932. break;
  933. default:
  934. return -EOPNOTSUPP;
  935. }
  936. return 0;
  937. }
  938. static void ar5523_remove_interface(struct ieee80211_hw *hw,
  939. struct ieee80211_vif *vif)
  940. {
  941. struct ar5523 *ar = hw->priv;
  942. ar5523_dbg(ar, "remove interface called\n");
  943. ar->vif = NULL;
  944. }
  945. static int ar5523_hwconfig(struct ieee80211_hw *hw, u32 changed)
  946. {
  947. struct ar5523 *ar = hw->priv;
  948. ar5523_dbg(ar, "config called\n");
  949. mutex_lock(&ar->mutex);
  950. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  951. ar5523_dbg(ar, "Do channel switch\n");
  952. ar5523_flush_tx(ar);
  953. ar5523_switch_chan(ar);
  954. }
  955. mutex_unlock(&ar->mutex);
  956. return 0;
  957. }
  958. static int ar5523_get_wlan_mode(struct ar5523 *ar,
  959. struct ieee80211_bss_conf *bss_conf)
  960. {
  961. struct ieee80211_supported_band *band;
  962. int bit;
  963. struct ieee80211_sta *sta;
  964. u32 sta_rate_set;
  965. band = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
  966. sta = ieee80211_find_sta(ar->vif, bss_conf->bssid);
  967. if (!sta) {
  968. ar5523_info(ar, "STA not found!\n");
  969. return WLAN_MODE_11b;
  970. }
  971. sta_rate_set = sta->supp_rates[ar->hw->conf.chandef.chan->band];
  972. for (bit = 0; bit < band->n_bitrates; bit++) {
  973. if (sta_rate_set & 1) {
  974. int rate = band->bitrates[bit].bitrate;
  975. switch (rate) {
  976. case 60:
  977. case 90:
  978. case 120:
  979. case 180:
  980. case 240:
  981. case 360:
  982. case 480:
  983. case 540:
  984. return WLAN_MODE_11g;
  985. }
  986. }
  987. sta_rate_set >>= 1;
  988. }
  989. return WLAN_MODE_11b;
  990. }
  991. static void ar5523_create_rateset(struct ar5523 *ar,
  992. struct ieee80211_bss_conf *bss_conf,
  993. struct ar5523_cmd_rateset *rs,
  994. bool basic)
  995. {
  996. struct ieee80211_supported_band *band;
  997. struct ieee80211_sta *sta;
  998. int bit, i = 0;
  999. u32 sta_rate_set, basic_rate_set;
  1000. sta = ieee80211_find_sta(ar->vif, bss_conf->bssid);
  1001. basic_rate_set = bss_conf->basic_rates;
  1002. if (!sta) {
  1003. ar5523_info(ar, "STA not found. Cannot set rates\n");
  1004. sta_rate_set = bss_conf->basic_rates;
  1005. } else
  1006. sta_rate_set = sta->supp_rates[ar->hw->conf.chandef.chan->band];
  1007. ar5523_dbg(ar, "sta rate_set = %08x\n", sta_rate_set);
  1008. band = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
  1009. for (bit = 0; bit < band->n_bitrates; bit++) {
  1010. BUG_ON(i >= AR5523_MAX_NRATES);
  1011. ar5523_dbg(ar, "Considering rate %d : %d\n",
  1012. band->bitrates[bit].hw_value, sta_rate_set & 1);
  1013. if (sta_rate_set & 1) {
  1014. rs->set[i] = band->bitrates[bit].hw_value;
  1015. if (basic_rate_set & 1 && basic)
  1016. rs->set[i] |= 0x80;
  1017. i++;
  1018. }
  1019. sta_rate_set >>= 1;
  1020. basic_rate_set >>= 1;
  1021. }
  1022. rs->length = i;
  1023. }
  1024. static int ar5523_set_basic_rates(struct ar5523 *ar,
  1025. struct ieee80211_bss_conf *bss)
  1026. {
  1027. struct ar5523_cmd_rates rates;
  1028. memset(&rates, 0, sizeof(rates));
  1029. rates.connid = cpu_to_be32(2); /* XXX */
  1030. rates.size = cpu_to_be32(sizeof(struct ar5523_cmd_rateset));
  1031. ar5523_create_rateset(ar, bss, &rates.rateset, true);
  1032. return ar5523_cmd_write(ar, WDCMSG_SET_BASIC_RATE, &rates,
  1033. sizeof(rates), 0);
  1034. }
  1035. static int ar5523_create_connection(struct ar5523 *ar,
  1036. struct ieee80211_vif *vif,
  1037. struct ieee80211_bss_conf *bss)
  1038. {
  1039. struct ar5523_cmd_create_connection create;
  1040. int wlan_mode;
  1041. memset(&create, 0, sizeof(create));
  1042. create.connid = cpu_to_be32(2);
  1043. create.bssid = cpu_to_be32(0);
  1044. /* XXX packed or not? */
  1045. create.size = cpu_to_be32(sizeof(struct ar5523_cmd_rateset));
  1046. ar5523_create_rateset(ar, bss, &create.connattr.rateset, false);
  1047. wlan_mode = ar5523_get_wlan_mode(ar, bss);
  1048. create.connattr.wlanmode = cpu_to_be32(wlan_mode);
  1049. return ar5523_cmd_write(ar, WDCMSG_CREATE_CONNECTION, &create,
  1050. sizeof(create), 0);
  1051. }
  1052. static int ar5523_write_associd(struct ar5523 *ar,
  1053. struct ieee80211_bss_conf *bss)
  1054. {
  1055. struct ar5523_cmd_set_associd associd;
  1056. memset(&associd, 0, sizeof(associd));
  1057. associd.defaultrateix = cpu_to_be32(0); /* XXX */
  1058. associd.associd = cpu_to_be32(bss->aid);
  1059. associd.timoffset = cpu_to_be32(0x3b); /* XXX */
  1060. memcpy(associd.bssid, bss->bssid, ETH_ALEN);
  1061. return ar5523_cmd_write(ar, WDCMSG_WRITE_ASSOCID, &associd,
  1062. sizeof(associd), 0);
  1063. }
  1064. static void ar5523_bss_info_changed(struct ieee80211_hw *hw,
  1065. struct ieee80211_vif *vif,
  1066. struct ieee80211_bss_conf *bss,
  1067. u32 changed)
  1068. {
  1069. struct ar5523 *ar = hw->priv;
  1070. int error;
  1071. ar5523_dbg(ar, "bss_info_changed called\n");
  1072. mutex_lock(&ar->mutex);
  1073. if (!(changed & BSS_CHANGED_ASSOC))
  1074. goto out_unlock;
  1075. if (bss->assoc) {
  1076. error = ar5523_create_connection(ar, vif, bss);
  1077. if (error) {
  1078. ar5523_err(ar, "could not create connection\n");
  1079. goto out_unlock;
  1080. }
  1081. error = ar5523_set_basic_rates(ar, bss);
  1082. if (error) {
  1083. ar5523_err(ar, "could not set negotiated rate set\n");
  1084. goto out_unlock;
  1085. }
  1086. error = ar5523_write_associd(ar, bss);
  1087. if (error) {
  1088. ar5523_err(ar, "could not set association\n");
  1089. goto out_unlock;
  1090. }
  1091. /* turn link LED on */
  1092. ar5523_set_ledsteady(ar, UATH_LED_LINK, UATH_LED_ON);
  1093. set_bit(AR5523_CONNECTED, &ar->flags);
  1094. ieee80211_queue_delayed_work(hw, &ar->stat_work, HZ);
  1095. } else {
  1096. cancel_delayed_work(&ar->stat_work);
  1097. clear_bit(AR5523_CONNECTED, &ar->flags);
  1098. ar5523_set_ledsteady(ar, UATH_LED_LINK, UATH_LED_OFF);
  1099. }
  1100. out_unlock:
  1101. mutex_unlock(&ar->mutex);
  1102. }
  1103. #define AR5523_SUPPORTED_FILTERS (FIF_PROMISC_IN_BSS | \
  1104. FIF_ALLMULTI | \
  1105. FIF_FCSFAIL | \
  1106. FIF_OTHER_BSS)
  1107. static void ar5523_configure_filter(struct ieee80211_hw *hw,
  1108. unsigned int changed_flags,
  1109. unsigned int *total_flags,
  1110. u64 multicast)
  1111. {
  1112. struct ar5523 *ar = hw->priv;
  1113. u32 filter = 0;
  1114. ar5523_dbg(ar, "configure_filter called\n");
  1115. mutex_lock(&ar->mutex);
  1116. ar5523_flush_tx(ar);
  1117. *total_flags &= AR5523_SUPPORTED_FILTERS;
  1118. /* The filters seems strange. UATH_FILTER_RX_BCAST and
  1119. * UATH_FILTER_RX_MCAST does not result in those frames being RXed.
  1120. * The only way I have found to get [mb]cast frames seems to be
  1121. * to set UATH_FILTER_RX_PROM. */
  1122. filter |= UATH_FILTER_RX_UCAST | UATH_FILTER_RX_MCAST |
  1123. UATH_FILTER_RX_BCAST | UATH_FILTER_RX_BEACON |
  1124. UATH_FILTER_RX_PROM;
  1125. ar5523_set_rxfilter(ar, 0, UATH_FILTER_OP_INIT);
  1126. ar5523_set_rxfilter(ar, filter, UATH_FILTER_OP_SET);
  1127. mutex_unlock(&ar->mutex);
  1128. }
  1129. static const struct ieee80211_ops ar5523_ops = {
  1130. .start = ar5523_start,
  1131. .stop = ar5523_stop,
  1132. .tx = ar5523_tx,
  1133. .set_rts_threshold = ar5523_set_rts_threshold,
  1134. .add_interface = ar5523_add_interface,
  1135. .remove_interface = ar5523_remove_interface,
  1136. .config = ar5523_hwconfig,
  1137. .bss_info_changed = ar5523_bss_info_changed,
  1138. .configure_filter = ar5523_configure_filter,
  1139. .flush = ar5523_flush,
  1140. };
  1141. static int ar5523_host_available(struct ar5523 *ar)
  1142. {
  1143. struct ar5523_cmd_host_available setup;
  1144. /* inform target the host is available */
  1145. setup.sw_ver_major = cpu_to_be32(ATH_SW_VER_MAJOR);
  1146. setup.sw_ver_minor = cpu_to_be32(ATH_SW_VER_MINOR);
  1147. setup.sw_ver_patch = cpu_to_be32(ATH_SW_VER_PATCH);
  1148. setup.sw_ver_build = cpu_to_be32(ATH_SW_VER_BUILD);
  1149. return ar5523_cmd_read(ar, WDCMSG_HOST_AVAILABLE,
  1150. &setup, sizeof(setup), NULL, 0, 0);
  1151. }
  1152. static int ar5523_get_devstatus(struct ar5523 *ar)
  1153. {
  1154. u8 macaddr[ETH_ALEN];
  1155. int error;
  1156. /* retrieve MAC address */
  1157. error = ar5523_get_status(ar, ST_MAC_ADDR, macaddr, ETH_ALEN);
  1158. if (error) {
  1159. ar5523_err(ar, "could not read MAC address\n");
  1160. return error;
  1161. }
  1162. SET_IEEE80211_PERM_ADDR(ar->hw, macaddr);
  1163. error = ar5523_get_status(ar, ST_SERIAL_NUMBER,
  1164. &ar->serial[0], sizeof(ar->serial));
  1165. if (error) {
  1166. ar5523_err(ar, "could not read device serial number\n");
  1167. return error;
  1168. }
  1169. return 0;
  1170. }
  1171. #define AR5523_SANE_RXBUFSZ 2000
  1172. static int ar5523_get_max_rxsz(struct ar5523 *ar)
  1173. {
  1174. int error;
  1175. __be32 rxsize;
  1176. /* Get max rx size */
  1177. error = ar5523_get_status(ar, ST_WDC_TRANSPORT_CHUNK_SIZE, &rxsize,
  1178. sizeof(rxsize));
  1179. if (error != 0) {
  1180. ar5523_err(ar, "could not read max RX size\n");
  1181. return error;
  1182. }
  1183. ar->rxbufsz = be32_to_cpu(rxsize);
  1184. if (!ar->rxbufsz || ar->rxbufsz > AR5523_SANE_RXBUFSZ) {
  1185. ar5523_err(ar, "Bad rxbufsz from device. Using %d instead\n",
  1186. AR5523_SANE_RXBUFSZ);
  1187. ar->rxbufsz = AR5523_SANE_RXBUFSZ;
  1188. }
  1189. ar5523_dbg(ar, "Max RX buf size: %d\n", ar->rxbufsz);
  1190. return 0;
  1191. }
  1192. /*
  1193. * This is copied from rtl818x, but we should probably move this
  1194. * to common code as in OpenBSD.
  1195. */
  1196. static const struct ieee80211_rate ar5523_rates[] = {
  1197. { .bitrate = 10, .hw_value = 2, },
  1198. { .bitrate = 20, .hw_value = 4 },
  1199. { .bitrate = 55, .hw_value = 11, },
  1200. { .bitrate = 110, .hw_value = 22, },
  1201. { .bitrate = 60, .hw_value = 12, },
  1202. { .bitrate = 90, .hw_value = 18, },
  1203. { .bitrate = 120, .hw_value = 24, },
  1204. { .bitrate = 180, .hw_value = 36, },
  1205. { .bitrate = 240, .hw_value = 48, },
  1206. { .bitrate = 360, .hw_value = 72, },
  1207. { .bitrate = 480, .hw_value = 96, },
  1208. { .bitrate = 540, .hw_value = 108, },
  1209. };
  1210. static const struct ieee80211_channel ar5523_channels[] = {
  1211. { .center_freq = 2412 },
  1212. { .center_freq = 2417 },
  1213. { .center_freq = 2422 },
  1214. { .center_freq = 2427 },
  1215. { .center_freq = 2432 },
  1216. { .center_freq = 2437 },
  1217. { .center_freq = 2442 },
  1218. { .center_freq = 2447 },
  1219. { .center_freq = 2452 },
  1220. { .center_freq = 2457 },
  1221. { .center_freq = 2462 },
  1222. { .center_freq = 2467 },
  1223. { .center_freq = 2472 },
  1224. { .center_freq = 2484 },
  1225. };
  1226. static int ar5523_init_modes(struct ar5523 *ar)
  1227. {
  1228. BUILD_BUG_ON(sizeof(ar->channels) != sizeof(ar5523_channels));
  1229. BUILD_BUG_ON(sizeof(ar->rates) != sizeof(ar5523_rates));
  1230. memcpy(ar->channels, ar5523_channels, sizeof(ar5523_channels));
  1231. memcpy(ar->rates, ar5523_rates, sizeof(ar5523_rates));
  1232. ar->band.band = IEEE80211_BAND_2GHZ;
  1233. ar->band.channels = ar->channels;
  1234. ar->band.n_channels = ARRAY_SIZE(ar5523_channels);
  1235. ar->band.bitrates = ar->rates;
  1236. ar->band.n_bitrates = ARRAY_SIZE(ar5523_rates);
  1237. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &ar->band;
  1238. return 0;
  1239. }
  1240. /*
  1241. * Load the MIPS R4000 microcode into the device. Once the image is loaded,
  1242. * the device will detach itself from the bus and reattach later with a new
  1243. * product Id (a la ezusb).
  1244. */
  1245. static int ar5523_load_firmware(struct usb_device *dev)
  1246. {
  1247. struct ar5523_fwblock *txblock, *rxblock;
  1248. const struct firmware *fw;
  1249. void *fwbuf;
  1250. int len, offset;
  1251. int foolen; /* XXX(hch): handle short transfers */
  1252. int error = -ENXIO;
  1253. if (request_firmware(&fw, AR5523_FIRMWARE_FILE, &dev->dev)) {
  1254. dev_err(&dev->dev, "no firmware found: %s\n",
  1255. AR5523_FIRMWARE_FILE);
  1256. return -ENOENT;
  1257. }
  1258. txblock = kmalloc(sizeof(*txblock), GFP_KERNEL);
  1259. if (!txblock)
  1260. goto out;
  1261. rxblock = kmalloc(sizeof(*rxblock), GFP_KERNEL);
  1262. if (!rxblock)
  1263. goto out_free_txblock;
  1264. fwbuf = kmalloc(AR5523_MAX_FWBLOCK_SIZE, GFP_KERNEL);
  1265. if (!fwbuf)
  1266. goto out_free_rxblock;
  1267. memset(txblock, 0, sizeof(struct ar5523_fwblock));
  1268. txblock->flags = cpu_to_be32(AR5523_WRITE_BLOCK);
  1269. txblock->total = cpu_to_be32(fw->size);
  1270. offset = 0;
  1271. len = fw->size;
  1272. while (len > 0) {
  1273. int mlen = min(len, AR5523_MAX_FWBLOCK_SIZE);
  1274. txblock->remain = cpu_to_be32(len - mlen);
  1275. txblock->len = cpu_to_be32(mlen);
  1276. /* send firmware block meta-data */
  1277. error = usb_bulk_msg(dev, ar5523_cmd_tx_pipe(dev),
  1278. txblock, sizeof(*txblock), &foolen,
  1279. AR5523_CMD_TIMEOUT);
  1280. if (error) {
  1281. dev_err(&dev->dev,
  1282. "could not send firmware block info\n");
  1283. goto out_free_fwbuf;
  1284. }
  1285. /* send firmware block data */
  1286. memcpy(fwbuf, fw->data + offset, mlen);
  1287. error = usb_bulk_msg(dev, ar5523_data_tx_pipe(dev),
  1288. fwbuf, mlen, &foolen,
  1289. AR5523_DATA_TIMEOUT);
  1290. if (error) {
  1291. dev_err(&dev->dev,
  1292. "could not send firmware block data\n");
  1293. goto out_free_fwbuf;
  1294. }
  1295. /* wait for ack from firmware */
  1296. error = usb_bulk_msg(dev, ar5523_cmd_rx_pipe(dev),
  1297. rxblock, sizeof(*rxblock), &foolen,
  1298. AR5523_CMD_TIMEOUT);
  1299. if (error) {
  1300. dev_err(&dev->dev,
  1301. "could not read firmware answer\n");
  1302. goto out_free_fwbuf;
  1303. }
  1304. len -= mlen;
  1305. offset += mlen;
  1306. }
  1307. /*
  1308. * Set the error to -ENXIO to make sure we continue probing for
  1309. * a driver.
  1310. */
  1311. error = -ENXIO;
  1312. out_free_fwbuf:
  1313. kfree(fwbuf);
  1314. out_free_rxblock:
  1315. kfree(rxblock);
  1316. out_free_txblock:
  1317. kfree(txblock);
  1318. out:
  1319. release_firmware(fw);
  1320. return error;
  1321. }
  1322. static int ar5523_probe(struct usb_interface *intf,
  1323. const struct usb_device_id *id)
  1324. {
  1325. struct usb_device *dev = interface_to_usbdev(intf);
  1326. struct ieee80211_hw *hw;
  1327. struct ar5523 *ar;
  1328. int error = -ENOMEM;
  1329. /*
  1330. * Load firmware if the device requires it. This will return
  1331. * -ENXIO on success and we'll get called back afer the usb
  1332. * id changes to indicate that the firmware is present.
  1333. */
  1334. if (id->driver_info & AR5523_FLAG_PRE_FIRMWARE)
  1335. return ar5523_load_firmware(dev);
  1336. hw = ieee80211_alloc_hw(sizeof(*ar), &ar5523_ops);
  1337. if (!hw)
  1338. goto out;
  1339. SET_IEEE80211_DEV(hw, &intf->dev);
  1340. ar = hw->priv;
  1341. ar->hw = hw;
  1342. ar->dev = dev;
  1343. mutex_init(&ar->mutex);
  1344. INIT_DELAYED_WORK(&ar->stat_work, ar5523_stat_work);
  1345. init_timer(&ar->tx_wd_timer);
  1346. setup_timer(&ar->tx_wd_timer, ar5523_tx_wd_timer, (unsigned long) ar);
  1347. INIT_WORK(&ar->tx_wd_work, ar5523_tx_wd_work);
  1348. INIT_WORK(&ar->tx_work, ar5523_tx_work);
  1349. INIT_LIST_HEAD(&ar->tx_queue_pending);
  1350. INIT_LIST_HEAD(&ar->tx_queue_submitted);
  1351. spin_lock_init(&ar->tx_data_list_lock);
  1352. atomic_set(&ar->tx_nr_total, 0);
  1353. atomic_set(&ar->tx_nr_pending, 0);
  1354. init_waitqueue_head(&ar->tx_flush_waitq);
  1355. atomic_set(&ar->rx_data_free_cnt, 0);
  1356. INIT_WORK(&ar->rx_refill_work, ar5523_rx_refill_work);
  1357. INIT_LIST_HEAD(&ar->rx_data_free);
  1358. INIT_LIST_HEAD(&ar->rx_data_used);
  1359. spin_lock_init(&ar->rx_data_list_lock);
  1360. ar->wq = create_singlethread_workqueue("ar5523");
  1361. if (!ar->wq) {
  1362. ar5523_err(ar, "Could not create wq\n");
  1363. goto out_free_ar;
  1364. }
  1365. error = ar5523_alloc_rx_bufs(ar);
  1366. if (error) {
  1367. ar5523_err(ar, "Could not allocate rx buffers\n");
  1368. goto out_free_wq;
  1369. }
  1370. error = ar5523_alloc_rx_cmd(ar);
  1371. if (error) {
  1372. ar5523_err(ar, "Could not allocate rx command buffers\n");
  1373. goto out_free_rx_bufs;
  1374. }
  1375. error = ar5523_alloc_tx_cmd(ar);
  1376. if (error) {
  1377. ar5523_err(ar, "Could not allocate tx command buffers\n");
  1378. goto out_free_rx_cmd;
  1379. }
  1380. error = ar5523_submit_rx_cmd(ar);
  1381. if (error) {
  1382. ar5523_err(ar, "Failed to submit rx cmd\n");
  1383. goto out_free_tx_cmd;
  1384. }
  1385. /*
  1386. * We're now ready to send/receive firmware commands.
  1387. */
  1388. error = ar5523_host_available(ar);
  1389. if (error) {
  1390. ar5523_err(ar, "could not initialize adapter\n");
  1391. goto out_cancel_rx_cmd;
  1392. }
  1393. error = ar5523_get_max_rxsz(ar);
  1394. if (error) {
  1395. ar5523_err(ar, "could not get caps from adapter\n");
  1396. goto out_cancel_rx_cmd;
  1397. }
  1398. error = ar5523_get_devcap(ar);
  1399. if (error) {
  1400. ar5523_err(ar, "could not get caps from adapter\n");
  1401. goto out_cancel_rx_cmd;
  1402. }
  1403. error = ar5523_get_devstatus(ar);
  1404. if (error != 0) {
  1405. ar5523_err(ar, "could not get device status\n");
  1406. goto out_cancel_rx_cmd;
  1407. }
  1408. ar5523_info(ar, "MAC/BBP AR5523, RF AR%c112\n",
  1409. (id->driver_info & AR5523_FLAG_ABG) ? '5' : '2');
  1410. ar->vif = NULL;
  1411. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1412. IEEE80211_HW_SIGNAL_DBM |
  1413. IEEE80211_HW_HAS_RATE_CONTROL;
  1414. hw->extra_tx_headroom = sizeof(struct ar5523_tx_desc) +
  1415. sizeof(struct ar5523_chunk);
  1416. hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1417. hw->queues = 1;
  1418. error = ar5523_init_modes(ar);
  1419. if (error)
  1420. goto out_cancel_rx_cmd;
  1421. usb_set_intfdata(intf, hw);
  1422. error = ieee80211_register_hw(hw);
  1423. if (error) {
  1424. ar5523_err(ar, "could not register device\n");
  1425. goto out_cancel_rx_cmd;
  1426. }
  1427. ar5523_info(ar, "Found and initialized AR5523 device\n");
  1428. return 0;
  1429. out_cancel_rx_cmd:
  1430. ar5523_cancel_rx_cmd(ar);
  1431. out_free_tx_cmd:
  1432. ar5523_free_tx_cmd(ar);
  1433. out_free_rx_cmd:
  1434. ar5523_free_rx_cmd(ar);
  1435. out_free_rx_bufs:
  1436. ar5523_free_rx_bufs(ar);
  1437. out_free_wq:
  1438. destroy_workqueue(ar->wq);
  1439. out_free_ar:
  1440. ieee80211_free_hw(hw);
  1441. out:
  1442. return error;
  1443. }
  1444. static void ar5523_disconnect(struct usb_interface *intf)
  1445. {
  1446. struct ieee80211_hw *hw = usb_get_intfdata(intf);
  1447. struct ar5523 *ar = hw->priv;
  1448. ar5523_dbg(ar, "detaching\n");
  1449. set_bit(AR5523_USB_DISCONNECTED, &ar->flags);
  1450. ieee80211_unregister_hw(hw);
  1451. ar5523_cancel_rx_cmd(ar);
  1452. ar5523_free_tx_cmd(ar);
  1453. ar5523_free_rx_cmd(ar);
  1454. ar5523_free_rx_bufs(ar);
  1455. destroy_workqueue(ar->wq);
  1456. ieee80211_free_hw(hw);
  1457. usb_set_intfdata(intf, NULL);
  1458. }
  1459. #define AR5523_DEVICE_UG(vendor, device) \
  1460. { USB_DEVICE((vendor), (device)) }, \
  1461. { USB_DEVICE((vendor), (device) + 1), \
  1462. .driver_info = AR5523_FLAG_PRE_FIRMWARE }
  1463. #define AR5523_DEVICE_UX(vendor, device) \
  1464. { USB_DEVICE((vendor), (device)), \
  1465. .driver_info = AR5523_FLAG_ABG }, \
  1466. { USB_DEVICE((vendor), (device) + 1), \
  1467. .driver_info = AR5523_FLAG_ABG|AR5523_FLAG_PRE_FIRMWARE }
  1468. static struct usb_device_id ar5523_id_table[] = {
  1469. AR5523_DEVICE_UG(0x168c, 0x0001), /* Atheros / AR5523 */
  1470. AR5523_DEVICE_UG(0x0cf3, 0x0001), /* Atheros2 / AR5523_1 */
  1471. AR5523_DEVICE_UG(0x0cf3, 0x0003), /* Atheros2 / AR5523_2 */
  1472. AR5523_DEVICE_UX(0x0cf3, 0x0005), /* Atheros2 / AR5523_3 */
  1473. AR5523_DEVICE_UG(0x0d8e, 0x7801), /* Conceptronic / AR5523_1 */
  1474. AR5523_DEVICE_UX(0x0d8e, 0x7811), /* Conceptronic / AR5523_2 */
  1475. AR5523_DEVICE_UX(0x2001, 0x3a00), /* Dlink / DWLAG132 */
  1476. AR5523_DEVICE_UG(0x2001, 0x3a02), /* Dlink / DWLG132 */
  1477. AR5523_DEVICE_UX(0x2001, 0x3a04), /* Dlink / DWLAG122 */
  1478. AR5523_DEVICE_UG(0x07d1, 0x3a07), /* D-Link / WUA-2340 rev A1 */
  1479. AR5523_DEVICE_UG(0x1690, 0x0712), /* Gigaset / AR5523 */
  1480. AR5523_DEVICE_UG(0x1690, 0x0710), /* Gigaset / SMCWUSBTG */
  1481. AR5523_DEVICE_UG(0x129b, 0x160b), /* Gigaset / USB stick 108
  1482. (CyberTAN Technology) */
  1483. AR5523_DEVICE_UG(0x16ab, 0x7801), /* Globalsun / AR5523_1 */
  1484. AR5523_DEVICE_UX(0x16ab, 0x7811), /* Globalsun / AR5523_2 */
  1485. AR5523_DEVICE_UG(0x0d8e, 0x7802), /* Globalsun / AR5523_3 */
  1486. AR5523_DEVICE_UX(0x0846, 0x4300), /* Netgear / WG111U */
  1487. AR5523_DEVICE_UG(0x0846, 0x4250), /* Netgear / WG111T */
  1488. AR5523_DEVICE_UG(0x0846, 0x5f00), /* Netgear / WPN111 */
  1489. AR5523_DEVICE_UG(0x157e, 0x3006), /* Umedia / AR5523_1 */
  1490. AR5523_DEVICE_UX(0x157e, 0x3205), /* Umedia / AR5523_2 */
  1491. AR5523_DEVICE_UG(0x157e, 0x3006), /* Umedia / TEW444UBEU */
  1492. AR5523_DEVICE_UG(0x1435, 0x0826), /* Wistronneweb / AR5523_1 */
  1493. AR5523_DEVICE_UX(0x1435, 0x0828), /* Wistronneweb / AR5523_2 */
  1494. AR5523_DEVICE_UG(0x0cde, 0x0012), /* Zcom / AR5523 */
  1495. AR5523_DEVICE_UG(0x1385, 0x4250), /* Netgear3 / WG111T (2) */
  1496. AR5523_DEVICE_UG(0x1385, 0x5f00), /* Netgear / WPN111 */
  1497. AR5523_DEVICE_UG(0x1385, 0x5f02), /* Netgear / WPN111 */
  1498. { }
  1499. };
  1500. MODULE_DEVICE_TABLE(usb, ar5523_id_table);
  1501. static struct usb_driver ar5523_driver = {
  1502. .name = "ar5523",
  1503. .id_table = ar5523_id_table,
  1504. .probe = ar5523_probe,
  1505. .disconnect = ar5523_disconnect,
  1506. };
  1507. module_usb_driver(ar5523_driver);
  1508. MODULE_LICENSE("Dual BSD/GPL");
  1509. MODULE_FIRMWARE(AR5523_FIRMWARE_FILE);