usb.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. /*
  2. * Copyright (c) 2011 Broadcom Corporation
  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 ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/firmware.h>
  19. #include <linux/usb.h>
  20. #include <linux/vmalloc.h>
  21. #include <brcmu_utils.h>
  22. #include <brcmu_wifi.h>
  23. #include <dhd_bus.h>
  24. #include <dhd_dbg.h>
  25. #include "usb_rdl.h"
  26. #include "usb.h"
  27. #define IOCTL_RESP_TIMEOUT 2000
  28. #define BRCMF_USB_RESET_GETVER_SPINWAIT 100 /* in unit of ms */
  29. #define BRCMF_USB_RESET_GETVER_LOOP_CNT 10
  30. #define BRCMF_POSTBOOT_ID 0xA123 /* ID to detect if dongle
  31. has boot up */
  32. #define BRCMF_USB_NRXQ 50
  33. #define BRCMF_USB_NTXQ 50
  34. #define CONFIGDESC(usb) (&((usb)->actconfig)->desc)
  35. #define IFPTR(usb, idx) ((usb)->actconfig->interface[(idx)])
  36. #define IFALTS(usb, idx) (IFPTR((usb), (idx))->altsetting[0])
  37. #define IFDESC(usb, idx) IFALTS((usb), (idx)).desc
  38. #define IFEPDESC(usb, idx, ep) (IFALTS((usb), (idx)).endpoint[(ep)]).desc
  39. #define CONTROL_IF 0
  40. #define BULK_IF 0
  41. #define BRCMF_USB_CBCTL_WRITE 0
  42. #define BRCMF_USB_CBCTL_READ 1
  43. #define BRCMF_USB_MAX_PKT_SIZE 1600
  44. #define BRCMF_USB_43143_FW_NAME "brcm/brcmfmac43143.bin"
  45. #define BRCMF_USB_43236_FW_NAME "brcm/brcmfmac43236b.bin"
  46. #define BRCMF_USB_43242_FW_NAME "brcm/brcmfmac43242a.bin"
  47. struct brcmf_usb_image {
  48. struct list_head list;
  49. s8 *fwname;
  50. u8 *image;
  51. int image_len;
  52. };
  53. static struct list_head fw_image_list;
  54. struct intr_transfer_buf {
  55. u32 notification;
  56. u32 reserved;
  57. };
  58. struct brcmf_usbdev_info {
  59. struct brcmf_usbdev bus_pub; /* MUST BE FIRST */
  60. spinlock_t qlock;
  61. struct list_head rx_freeq;
  62. struct list_head rx_postq;
  63. struct list_head tx_freeq;
  64. struct list_head tx_postq;
  65. uint rx_pipe, tx_pipe, intr_pipe, rx_pipe2;
  66. int rx_low_watermark;
  67. int tx_low_watermark;
  68. int tx_high_watermark;
  69. int tx_freecount;
  70. bool tx_flowblock;
  71. struct brcmf_usbreq *tx_reqs;
  72. struct brcmf_usbreq *rx_reqs;
  73. u8 *image; /* buffer for combine fw and nvram */
  74. int image_len;
  75. struct usb_device *usbdev;
  76. struct device *dev;
  77. int ctl_in_pipe, ctl_out_pipe;
  78. struct urb *ctl_urb; /* URB for control endpoint */
  79. struct usb_ctrlrequest ctl_write;
  80. struct usb_ctrlrequest ctl_read;
  81. u32 ctl_urb_actual_length;
  82. int ctl_urb_status;
  83. int ctl_completed;
  84. wait_queue_head_t ioctl_resp_wait;
  85. ulong ctl_op;
  86. struct urb *bulk_urb; /* used for FW download */
  87. struct urb *intr_urb; /* URB for interrupt endpoint */
  88. int intr_size; /* Size of interrupt message */
  89. int interval; /* Interrupt polling interval */
  90. struct intr_transfer_buf intr; /* Data buffer for interrupt endpoint */
  91. };
  92. static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
  93. struct brcmf_usbreq *req);
  94. MODULE_AUTHOR("Broadcom Corporation");
  95. MODULE_DESCRIPTION("Broadcom 802.11n wireless LAN fullmac usb driver.");
  96. MODULE_SUPPORTED_DEVICE("Broadcom 802.11n WLAN fullmac usb cards");
  97. MODULE_LICENSE("Dual BSD/GPL");
  98. static struct brcmf_usbdev *brcmf_usb_get_buspub(struct device *dev)
  99. {
  100. struct brcmf_bus *bus_if = dev_get_drvdata(dev);
  101. return bus_if->bus_priv.usb;
  102. }
  103. static struct brcmf_usbdev_info *brcmf_usb_get_businfo(struct device *dev)
  104. {
  105. return brcmf_usb_get_buspub(dev)->devinfo;
  106. }
  107. static int brcmf_usb_ioctl_resp_wait(struct brcmf_usbdev_info *devinfo)
  108. {
  109. return wait_event_timeout(devinfo->ioctl_resp_wait,
  110. devinfo->ctl_completed,
  111. msecs_to_jiffies(IOCTL_RESP_TIMEOUT));
  112. }
  113. static void brcmf_usb_ioctl_resp_wake(struct brcmf_usbdev_info *devinfo)
  114. {
  115. if (waitqueue_active(&devinfo->ioctl_resp_wait))
  116. wake_up(&devinfo->ioctl_resp_wait);
  117. }
  118. static void
  119. brcmf_usb_ctl_complete(struct brcmf_usbdev_info *devinfo, int type, int status)
  120. {
  121. brcmf_dbg(USB, "Enter, status=%d\n", status);
  122. if (unlikely(devinfo == NULL))
  123. return;
  124. if (type == BRCMF_USB_CBCTL_READ) {
  125. if (status == 0)
  126. devinfo->bus_pub.stats.rx_ctlpkts++;
  127. else
  128. devinfo->bus_pub.stats.rx_ctlerrs++;
  129. } else if (type == BRCMF_USB_CBCTL_WRITE) {
  130. if (status == 0)
  131. devinfo->bus_pub.stats.tx_ctlpkts++;
  132. else
  133. devinfo->bus_pub.stats.tx_ctlerrs++;
  134. }
  135. devinfo->ctl_urb_status = status;
  136. devinfo->ctl_completed = true;
  137. brcmf_usb_ioctl_resp_wake(devinfo);
  138. }
  139. static void
  140. brcmf_usb_ctlread_complete(struct urb *urb)
  141. {
  142. struct brcmf_usbdev_info *devinfo =
  143. (struct brcmf_usbdev_info *)urb->context;
  144. brcmf_dbg(USB, "Enter\n");
  145. devinfo->ctl_urb_actual_length = urb->actual_length;
  146. brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_READ,
  147. urb->status);
  148. }
  149. static void
  150. brcmf_usb_ctlwrite_complete(struct urb *urb)
  151. {
  152. struct brcmf_usbdev_info *devinfo =
  153. (struct brcmf_usbdev_info *)urb->context;
  154. brcmf_dbg(USB, "Enter\n");
  155. brcmf_usb_ctl_complete(devinfo, BRCMF_USB_CBCTL_WRITE,
  156. urb->status);
  157. }
  158. static int
  159. brcmf_usb_send_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
  160. {
  161. int ret;
  162. u16 size;
  163. brcmf_dbg(USB, "Enter\n");
  164. if (devinfo == NULL || buf == NULL ||
  165. len == 0 || devinfo->ctl_urb == NULL)
  166. return -EINVAL;
  167. size = len;
  168. devinfo->ctl_write.wLength = cpu_to_le16p(&size);
  169. devinfo->ctl_urb->transfer_buffer_length = size;
  170. devinfo->ctl_urb_status = 0;
  171. devinfo->ctl_urb_actual_length = 0;
  172. usb_fill_control_urb(devinfo->ctl_urb,
  173. devinfo->usbdev,
  174. devinfo->ctl_out_pipe,
  175. (unsigned char *) &devinfo->ctl_write,
  176. buf, size,
  177. (usb_complete_t)brcmf_usb_ctlwrite_complete,
  178. devinfo);
  179. ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
  180. if (ret < 0)
  181. brcmf_err("usb_submit_urb failed %d\n", ret);
  182. return ret;
  183. }
  184. static int
  185. brcmf_usb_recv_ctl(struct brcmf_usbdev_info *devinfo, u8 *buf, int len)
  186. {
  187. int ret;
  188. u16 size;
  189. brcmf_dbg(USB, "Enter\n");
  190. if ((devinfo == NULL) || (buf == NULL) || (len == 0)
  191. || (devinfo->ctl_urb == NULL))
  192. return -EINVAL;
  193. size = len;
  194. devinfo->ctl_read.wLength = cpu_to_le16p(&size);
  195. devinfo->ctl_urb->transfer_buffer_length = size;
  196. devinfo->ctl_read.bRequestType = USB_DIR_IN
  197. | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  198. devinfo->ctl_read.bRequest = 1;
  199. usb_fill_control_urb(devinfo->ctl_urb,
  200. devinfo->usbdev,
  201. devinfo->ctl_in_pipe,
  202. (unsigned char *) &devinfo->ctl_read,
  203. buf, size,
  204. (usb_complete_t)brcmf_usb_ctlread_complete,
  205. devinfo);
  206. ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
  207. if (ret < 0)
  208. brcmf_err("usb_submit_urb failed %d\n", ret);
  209. return ret;
  210. }
  211. static int brcmf_usb_tx_ctlpkt(struct device *dev, u8 *buf, u32 len)
  212. {
  213. int err = 0;
  214. int timeout = 0;
  215. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  216. brcmf_dbg(USB, "Enter\n");
  217. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
  218. return -EIO;
  219. if (test_and_set_bit(0, &devinfo->ctl_op))
  220. return -EIO;
  221. devinfo->ctl_completed = false;
  222. err = brcmf_usb_send_ctl(devinfo, buf, len);
  223. if (err) {
  224. brcmf_err("fail %d bytes: %d\n", err, len);
  225. clear_bit(0, &devinfo->ctl_op);
  226. return err;
  227. }
  228. timeout = brcmf_usb_ioctl_resp_wait(devinfo);
  229. clear_bit(0, &devinfo->ctl_op);
  230. if (!timeout) {
  231. brcmf_err("Txctl wait timed out\n");
  232. err = -EIO;
  233. }
  234. return err;
  235. }
  236. static int brcmf_usb_rx_ctlpkt(struct device *dev, u8 *buf, u32 len)
  237. {
  238. int err = 0;
  239. int timeout = 0;
  240. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  241. brcmf_dbg(USB, "Enter\n");
  242. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
  243. return -EIO;
  244. if (test_and_set_bit(0, &devinfo->ctl_op))
  245. return -EIO;
  246. devinfo->ctl_completed = false;
  247. err = brcmf_usb_recv_ctl(devinfo, buf, len);
  248. if (err) {
  249. brcmf_err("fail %d bytes: %d\n", err, len);
  250. clear_bit(0, &devinfo->ctl_op);
  251. return err;
  252. }
  253. timeout = brcmf_usb_ioctl_resp_wait(devinfo);
  254. err = devinfo->ctl_urb_status;
  255. clear_bit(0, &devinfo->ctl_op);
  256. if (!timeout) {
  257. brcmf_err("rxctl wait timed out\n");
  258. err = -EIO;
  259. }
  260. if (!err)
  261. return devinfo->ctl_urb_actual_length;
  262. else
  263. return err;
  264. }
  265. static struct brcmf_usbreq *brcmf_usb_deq(struct brcmf_usbdev_info *devinfo,
  266. struct list_head *q, int *counter)
  267. {
  268. unsigned long flags;
  269. struct brcmf_usbreq *req;
  270. spin_lock_irqsave(&devinfo->qlock, flags);
  271. if (list_empty(q)) {
  272. spin_unlock_irqrestore(&devinfo->qlock, flags);
  273. return NULL;
  274. }
  275. req = list_entry(q->next, struct brcmf_usbreq, list);
  276. list_del_init(q->next);
  277. if (counter)
  278. (*counter)--;
  279. spin_unlock_irqrestore(&devinfo->qlock, flags);
  280. return req;
  281. }
  282. static void brcmf_usb_enq(struct brcmf_usbdev_info *devinfo,
  283. struct list_head *q, struct brcmf_usbreq *req,
  284. int *counter)
  285. {
  286. unsigned long flags;
  287. spin_lock_irqsave(&devinfo->qlock, flags);
  288. list_add_tail(&req->list, q);
  289. if (counter)
  290. (*counter)++;
  291. spin_unlock_irqrestore(&devinfo->qlock, flags);
  292. }
  293. static struct brcmf_usbreq *
  294. brcmf_usbdev_qinit(struct list_head *q, int qsize)
  295. {
  296. int i;
  297. struct brcmf_usbreq *req, *reqs;
  298. reqs = kzalloc(sizeof(struct brcmf_usbreq) * qsize, GFP_ATOMIC);
  299. if (reqs == NULL) {
  300. brcmf_err("fail to allocate memory!\n");
  301. return NULL;
  302. }
  303. req = reqs;
  304. for (i = 0; i < qsize; i++) {
  305. req->urb = usb_alloc_urb(0, GFP_ATOMIC);
  306. if (!req->urb)
  307. goto fail;
  308. INIT_LIST_HEAD(&req->list);
  309. list_add_tail(&req->list, q);
  310. req++;
  311. }
  312. return reqs;
  313. fail:
  314. brcmf_err("fail!\n");
  315. while (!list_empty(q)) {
  316. req = list_entry(q->next, struct brcmf_usbreq, list);
  317. if (req && req->urb)
  318. usb_free_urb(req->urb);
  319. list_del(q->next);
  320. }
  321. return NULL;
  322. }
  323. static void brcmf_usb_free_q(struct list_head *q, bool pending)
  324. {
  325. struct brcmf_usbreq *req, *next;
  326. int i = 0;
  327. list_for_each_entry_safe(req, next, q, list) {
  328. if (!req->urb) {
  329. brcmf_err("bad req\n");
  330. break;
  331. }
  332. i++;
  333. if (pending) {
  334. usb_kill_urb(req->urb);
  335. } else {
  336. usb_free_urb(req->urb);
  337. list_del_init(&req->list);
  338. }
  339. }
  340. }
  341. static void brcmf_usb_del_fromq(struct brcmf_usbdev_info *devinfo,
  342. struct brcmf_usbreq *req)
  343. {
  344. unsigned long flags;
  345. spin_lock_irqsave(&devinfo->qlock, flags);
  346. list_del_init(&req->list);
  347. spin_unlock_irqrestore(&devinfo->qlock, flags);
  348. }
  349. static void brcmf_usb_tx_complete(struct urb *urb)
  350. {
  351. struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
  352. struct brcmf_usbdev_info *devinfo = req->devinfo;
  353. brcmf_dbg(USB, "Enter, urb->status=%d, skb=%p\n", urb->status,
  354. req->skb);
  355. brcmf_usb_del_fromq(devinfo, req);
  356. if (urb->status == 0)
  357. devinfo->bus_pub.bus->dstats.tx_packets++;
  358. else
  359. devinfo->bus_pub.bus->dstats.tx_errors++;
  360. brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
  361. brcmu_pkt_buf_free_skb(req->skb);
  362. req->skb = NULL;
  363. brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
  364. if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
  365. devinfo->tx_flowblock) {
  366. brcmf_txflowblock(devinfo->dev, false);
  367. devinfo->tx_flowblock = false;
  368. }
  369. }
  370. static void brcmf_usb_rx_complete(struct urb *urb)
  371. {
  372. struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
  373. struct brcmf_usbdev_info *devinfo = req->devinfo;
  374. struct sk_buff *skb;
  375. int ifidx = 0;
  376. brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
  377. brcmf_usb_del_fromq(devinfo, req);
  378. skb = req->skb;
  379. req->skb = NULL;
  380. if (urb->status == 0) {
  381. devinfo->bus_pub.bus->dstats.rx_packets++;
  382. } else {
  383. devinfo->bus_pub.bus->dstats.rx_errors++;
  384. brcmu_pkt_buf_free_skb(skb);
  385. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  386. return;
  387. }
  388. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
  389. skb_put(skb, urb->actual_length);
  390. if (brcmf_proto_hdrpull(devinfo->dev, &ifidx, skb) != 0) {
  391. brcmf_err("rx protocol error\n");
  392. brcmu_pkt_buf_free_skb(skb);
  393. devinfo->bus_pub.bus->dstats.rx_errors++;
  394. } else
  395. brcmf_rx_packet(devinfo->dev, ifidx, skb);
  396. brcmf_usb_rx_refill(devinfo, req);
  397. } else {
  398. brcmu_pkt_buf_free_skb(skb);
  399. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  400. }
  401. return;
  402. }
  403. static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
  404. struct brcmf_usbreq *req)
  405. {
  406. struct sk_buff *skb;
  407. int ret;
  408. if (!req || !devinfo)
  409. return;
  410. skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
  411. if (!skb) {
  412. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  413. return;
  414. }
  415. req->skb = skb;
  416. usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
  417. skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
  418. req);
  419. req->devinfo = devinfo;
  420. brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
  421. ret = usb_submit_urb(req->urb, GFP_ATOMIC);
  422. if (ret) {
  423. brcmf_usb_del_fromq(devinfo, req);
  424. brcmu_pkt_buf_free_skb(req->skb);
  425. req->skb = NULL;
  426. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  427. }
  428. return;
  429. }
  430. static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
  431. {
  432. struct brcmf_usbreq *req;
  433. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
  434. brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
  435. return;
  436. }
  437. while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
  438. brcmf_usb_rx_refill(devinfo, req);
  439. }
  440. static void
  441. brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
  442. {
  443. struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
  444. int old_state;
  445. brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
  446. devinfo->bus_pub.state, state);
  447. if (devinfo->bus_pub.state == state)
  448. return;
  449. old_state = devinfo->bus_pub.state;
  450. devinfo->bus_pub.state = state;
  451. /* update state of upper layer */
  452. if (state == BRCMFMAC_USB_STATE_DOWN) {
  453. brcmf_dbg(USB, "DBUS is down\n");
  454. bcmf_bus->state = BRCMF_BUS_DOWN;
  455. } else if (state == BRCMFMAC_USB_STATE_UP) {
  456. brcmf_dbg(USB, "DBUS is up\n");
  457. bcmf_bus->state = BRCMF_BUS_DATA;
  458. } else {
  459. brcmf_dbg(USB, "DBUS current state=%d\n", state);
  460. }
  461. }
  462. static void
  463. brcmf_usb_intr_complete(struct urb *urb)
  464. {
  465. struct brcmf_usbdev_info *devinfo =
  466. (struct brcmf_usbdev_info *)urb->context;
  467. int err;
  468. brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
  469. if (devinfo == NULL)
  470. return;
  471. if (unlikely(urb->status)) {
  472. if (urb->status == -ENOENT ||
  473. urb->status == -ESHUTDOWN ||
  474. urb->status == -ENODEV) {
  475. brcmf_usb_state_change(devinfo,
  476. BRCMFMAC_USB_STATE_DOWN);
  477. }
  478. }
  479. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN) {
  480. brcmf_err("intr cb when DBUS down, ignoring\n");
  481. return;
  482. }
  483. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
  484. err = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
  485. if (err)
  486. brcmf_err("usb_submit_urb, err=%d\n", err);
  487. }
  488. }
  489. static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
  490. {
  491. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  492. struct brcmf_usbreq *req;
  493. int ret;
  494. brcmf_dbg(USB, "Enter, skb=%p\n", skb);
  495. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
  496. return -EIO;
  497. req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
  498. &devinfo->tx_freecount);
  499. if (!req) {
  500. brcmu_pkt_buf_free_skb(skb);
  501. brcmf_err("no req to send\n");
  502. return -ENOMEM;
  503. }
  504. req->skb = skb;
  505. req->devinfo = devinfo;
  506. usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
  507. skb->data, skb->len, brcmf_usb_tx_complete, req);
  508. req->urb->transfer_flags |= URB_ZERO_PACKET;
  509. brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
  510. ret = usb_submit_urb(req->urb, GFP_ATOMIC);
  511. if (ret) {
  512. brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
  513. brcmf_usb_del_fromq(devinfo, req);
  514. brcmu_pkt_buf_free_skb(req->skb);
  515. req->skb = NULL;
  516. brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
  517. &devinfo->tx_freecount);
  518. } else {
  519. if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
  520. !devinfo->tx_flowblock) {
  521. brcmf_txflowblock(dev, true);
  522. devinfo->tx_flowblock = true;
  523. }
  524. }
  525. return ret;
  526. }
  527. static int brcmf_usb_up(struct device *dev)
  528. {
  529. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  530. u16 ifnum;
  531. int ret;
  532. brcmf_dbg(USB, "Enter\n");
  533. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
  534. return 0;
  535. /* Success, indicate devinfo is fully up */
  536. brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
  537. if (devinfo->intr_urb) {
  538. usb_fill_int_urb(devinfo->intr_urb, devinfo->usbdev,
  539. devinfo->intr_pipe,
  540. &devinfo->intr,
  541. devinfo->intr_size,
  542. (usb_complete_t)brcmf_usb_intr_complete,
  543. devinfo,
  544. devinfo->interval);
  545. ret = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
  546. if (ret) {
  547. brcmf_err("USB_SUBMIT_URB failed with status %d\n",
  548. ret);
  549. return -EINVAL;
  550. }
  551. }
  552. if (devinfo->ctl_urb) {
  553. devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
  554. devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
  555. ifnum = IFDESC(devinfo->usbdev, CONTROL_IF).bInterfaceNumber;
  556. /* CTL Write */
  557. devinfo->ctl_write.bRequestType =
  558. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  559. devinfo->ctl_write.bRequest = 0;
  560. devinfo->ctl_write.wValue = cpu_to_le16(0);
  561. devinfo->ctl_write.wIndex = cpu_to_le16p(&ifnum);
  562. /* CTL Read */
  563. devinfo->ctl_read.bRequestType =
  564. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  565. devinfo->ctl_read.bRequest = 1;
  566. devinfo->ctl_read.wValue = cpu_to_le16(0);
  567. devinfo->ctl_read.wIndex = cpu_to_le16p(&ifnum);
  568. }
  569. brcmf_usb_rx_fill_all(devinfo);
  570. return 0;
  571. }
  572. static void brcmf_usb_down(struct device *dev)
  573. {
  574. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  575. brcmf_dbg(USB, "Enter\n");
  576. if (devinfo == NULL)
  577. return;
  578. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
  579. return;
  580. brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
  581. if (devinfo->intr_urb)
  582. usb_kill_urb(devinfo->intr_urb);
  583. if (devinfo->ctl_urb)
  584. usb_kill_urb(devinfo->ctl_urb);
  585. if (devinfo->bulk_urb)
  586. usb_kill_urb(devinfo->bulk_urb);
  587. brcmf_usb_free_q(&devinfo->tx_postq, true);
  588. brcmf_usb_free_q(&devinfo->rx_postq, true);
  589. }
  590. static void
  591. brcmf_usb_sync_complete(struct urb *urb)
  592. {
  593. struct brcmf_usbdev_info *devinfo =
  594. (struct brcmf_usbdev_info *)urb->context;
  595. devinfo->ctl_completed = true;
  596. brcmf_usb_ioctl_resp_wake(devinfo);
  597. }
  598. static bool brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
  599. void *buffer, int buflen)
  600. {
  601. int ret = 0;
  602. char *tmpbuf;
  603. u16 size;
  604. if ((!devinfo) || (devinfo->ctl_urb == NULL))
  605. return false;
  606. tmpbuf = kmalloc(buflen, GFP_ATOMIC);
  607. if (!tmpbuf)
  608. return false;
  609. size = buflen;
  610. devinfo->ctl_urb->transfer_buffer_length = size;
  611. devinfo->ctl_read.wLength = cpu_to_le16p(&size);
  612. devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
  613. USB_RECIP_INTERFACE;
  614. devinfo->ctl_read.bRequest = cmd;
  615. usb_fill_control_urb(devinfo->ctl_urb,
  616. devinfo->usbdev,
  617. usb_rcvctrlpipe(devinfo->usbdev, 0),
  618. (unsigned char *) &devinfo->ctl_read,
  619. (void *) tmpbuf, size,
  620. (usb_complete_t)brcmf_usb_sync_complete, devinfo);
  621. devinfo->ctl_completed = false;
  622. ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
  623. if (ret < 0) {
  624. brcmf_err("usb_submit_urb failed %d\n", ret);
  625. kfree(tmpbuf);
  626. return false;
  627. }
  628. ret = brcmf_usb_ioctl_resp_wait(devinfo);
  629. memcpy(buffer, tmpbuf, buflen);
  630. kfree(tmpbuf);
  631. return ret;
  632. }
  633. static bool
  634. brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
  635. {
  636. struct bootrom_id_le id;
  637. u32 chipid, chiprev;
  638. brcmf_dbg(USB, "Enter\n");
  639. if (devinfo == NULL)
  640. return false;
  641. /* Check if firmware downloaded already by querying runtime ID */
  642. id.chip = cpu_to_le32(0xDEAD);
  643. brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
  644. chipid = le32_to_cpu(id.chip);
  645. chiprev = le32_to_cpu(id.chiprev);
  646. if ((chipid & 0x4300) == 0x4300)
  647. brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
  648. else
  649. brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
  650. if (chipid == BRCMF_POSTBOOT_ID) {
  651. brcmf_dbg(USB, "firmware already downloaded\n");
  652. brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
  653. return false;
  654. } else {
  655. devinfo->bus_pub.devid = chipid;
  656. devinfo->bus_pub.chiprev = chiprev;
  657. }
  658. return true;
  659. }
  660. static int
  661. brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
  662. {
  663. struct bootrom_id_le id;
  664. u32 loop_cnt;
  665. brcmf_dbg(USB, "Enter\n");
  666. loop_cnt = 0;
  667. do {
  668. mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
  669. loop_cnt++;
  670. id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
  671. brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
  672. if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
  673. break;
  674. } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
  675. if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
  676. brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
  677. le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
  678. brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
  679. return 0;
  680. } else {
  681. brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
  682. BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
  683. return -EINVAL;
  684. }
  685. }
  686. static int
  687. brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
  688. {
  689. int ret;
  690. if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
  691. return -EINVAL;
  692. /* Prepare the URB */
  693. usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
  694. devinfo->tx_pipe, buffer, len,
  695. (usb_complete_t)brcmf_usb_sync_complete, devinfo);
  696. devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
  697. devinfo->ctl_completed = false;
  698. ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
  699. if (ret) {
  700. brcmf_err("usb_submit_urb failed %d\n", ret);
  701. return ret;
  702. }
  703. ret = brcmf_usb_ioctl_resp_wait(devinfo);
  704. return (ret == 0);
  705. }
  706. static int
  707. brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
  708. {
  709. unsigned int sendlen, sent, dllen;
  710. char *bulkchunk = NULL, *dlpos;
  711. struct rdl_state_le state;
  712. u32 rdlstate, rdlbytes;
  713. int err = 0;
  714. brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
  715. bulkchunk = kmalloc(RDL_CHUNK, GFP_ATOMIC);
  716. if (bulkchunk == NULL) {
  717. err = -ENOMEM;
  718. goto fail;
  719. }
  720. /* 1) Prepare USB boot loader for runtime image */
  721. brcmf_usb_dl_cmd(devinfo, DL_START, &state,
  722. sizeof(struct rdl_state_le));
  723. rdlstate = le32_to_cpu(state.state);
  724. rdlbytes = le32_to_cpu(state.bytes);
  725. /* 2) Check we are in the Waiting state */
  726. if (rdlstate != DL_WAITING) {
  727. brcmf_err("Failed to DL_START\n");
  728. err = -EINVAL;
  729. goto fail;
  730. }
  731. sent = 0;
  732. dlpos = fw;
  733. dllen = fwlen;
  734. /* Get chip id and rev */
  735. while (rdlbytes != dllen) {
  736. /* Wait until the usb device reports it received all
  737. * the bytes we sent */
  738. if ((rdlbytes == sent) && (rdlbytes != dllen)) {
  739. if ((dllen-sent) < RDL_CHUNK)
  740. sendlen = dllen-sent;
  741. else
  742. sendlen = RDL_CHUNK;
  743. /* simply avoid having to send a ZLP by ensuring we
  744. * never have an even
  745. * multiple of 64
  746. */
  747. if (!(sendlen % 64))
  748. sendlen -= 4;
  749. /* send data */
  750. memcpy(bulkchunk, dlpos, sendlen);
  751. if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
  752. sendlen)) {
  753. brcmf_err("send_bulk failed\n");
  754. err = -EINVAL;
  755. goto fail;
  756. }
  757. dlpos += sendlen;
  758. sent += sendlen;
  759. }
  760. if (!brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
  761. sizeof(struct rdl_state_le))) {
  762. brcmf_err("DL_GETSTATE Failed xxxx\n");
  763. err = -EINVAL;
  764. goto fail;
  765. }
  766. rdlstate = le32_to_cpu(state.state);
  767. rdlbytes = le32_to_cpu(state.bytes);
  768. /* restart if an error is reported */
  769. if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
  770. brcmf_err("Bad Hdr or Bad CRC state %d\n",
  771. rdlstate);
  772. err = -EINVAL;
  773. goto fail;
  774. }
  775. }
  776. fail:
  777. kfree(bulkchunk);
  778. brcmf_dbg(USB, "Exit, err=%d\n", err);
  779. return err;
  780. }
  781. static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
  782. {
  783. int err;
  784. brcmf_dbg(USB, "Enter\n");
  785. if (devinfo == NULL)
  786. return -EINVAL;
  787. if (devinfo->bus_pub.devid == 0xDEAD)
  788. return -EINVAL;
  789. err = brcmf_usb_dl_writeimage(devinfo, fw, len);
  790. if (err == 0)
  791. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
  792. else
  793. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
  794. brcmf_dbg(USB, "Exit, err=%d\n", err);
  795. return err;
  796. }
  797. static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
  798. {
  799. struct rdl_state_le state;
  800. brcmf_dbg(USB, "Enter\n");
  801. if (!devinfo)
  802. return -EINVAL;
  803. if (devinfo->bus_pub.devid == 0xDEAD)
  804. return -EINVAL;
  805. /* Check we are runnable */
  806. brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
  807. sizeof(struct rdl_state_le));
  808. /* Start the image */
  809. if (state.state == cpu_to_le32(DL_RUNNABLE)) {
  810. if (!brcmf_usb_dl_cmd(devinfo, DL_GO, &state,
  811. sizeof(struct rdl_state_le)))
  812. return -ENODEV;
  813. if (brcmf_usb_resetcfg(devinfo))
  814. return -ENODEV;
  815. /* The Dongle may go for re-enumeration. */
  816. } else {
  817. brcmf_err("Dongle not runnable\n");
  818. return -EINVAL;
  819. }
  820. brcmf_dbg(USB, "Exit\n");
  821. return 0;
  822. }
  823. static bool brcmf_usb_chip_support(int chipid, int chiprev)
  824. {
  825. switch(chipid) {
  826. case 43143:
  827. return true;
  828. case 43235:
  829. case 43236:
  830. case 43238:
  831. return (chiprev == 3);
  832. case 43242:
  833. return true;
  834. default:
  835. break;
  836. }
  837. return false;
  838. }
  839. static int
  840. brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
  841. {
  842. int devid, chiprev;
  843. int err;
  844. brcmf_dbg(USB, "Enter\n");
  845. if (devinfo == NULL)
  846. return -ENODEV;
  847. devid = devinfo->bus_pub.devid;
  848. chiprev = devinfo->bus_pub.chiprev;
  849. if (!brcmf_usb_chip_support(devid, chiprev)) {
  850. brcmf_err("unsupported chip %d rev %d\n",
  851. devid, chiprev);
  852. return -EINVAL;
  853. }
  854. if (!devinfo->image) {
  855. brcmf_err("No firmware!\n");
  856. return -ENOENT;
  857. }
  858. err = brcmf_usb_dlstart(devinfo,
  859. devinfo->image, devinfo->image_len);
  860. if (err == 0)
  861. err = brcmf_usb_dlrun(devinfo);
  862. return err;
  863. }
  864. static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
  865. {
  866. brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
  867. /* free the URBS */
  868. brcmf_usb_free_q(&devinfo->rx_freeq, false);
  869. brcmf_usb_free_q(&devinfo->tx_freeq, false);
  870. usb_free_urb(devinfo->intr_urb);
  871. usb_free_urb(devinfo->ctl_urb);
  872. usb_free_urb(devinfo->bulk_urb);
  873. kfree(devinfo->tx_reqs);
  874. kfree(devinfo->rx_reqs);
  875. }
  876. #define TRX_MAGIC 0x30524448 /* "HDR0" */
  877. #define TRX_VERSION 1 /* Version 1 */
  878. #define TRX_MAX_LEN 0x3B0000 /* Max length */
  879. #define TRX_NO_HEADER 1 /* Do not write TRX header */
  880. #define TRX_MAX_OFFSET 3 /* Max number of individual files */
  881. #define TRX_UNCOMP_IMAGE 0x20 /* Trx contains uncompressed image */
  882. struct trx_header_le {
  883. __le32 magic; /* "HDR0" */
  884. __le32 len; /* Length of file including header */
  885. __le32 crc32; /* CRC from flag_version to end of file */
  886. __le32 flag_version; /* 0:15 flags, 16:31 version */
  887. __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
  888. * header */
  889. };
  890. static int check_file(const u8 *headers)
  891. {
  892. struct trx_header_le *trx;
  893. int actual_len = -1;
  894. brcmf_dbg(USB, "Enter\n");
  895. /* Extract trx header */
  896. trx = (struct trx_header_le *) headers;
  897. if (trx->magic != cpu_to_le32(TRX_MAGIC))
  898. return -1;
  899. headers += sizeof(struct trx_header_le);
  900. if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
  901. actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
  902. return actual_len + sizeof(struct trx_header_le);
  903. }
  904. return -1;
  905. }
  906. static int brcmf_usb_get_fw(struct brcmf_usbdev_info *devinfo)
  907. {
  908. s8 *fwname;
  909. const struct firmware *fw;
  910. struct brcmf_usb_image *fw_image;
  911. int err;
  912. brcmf_dbg(USB, "Enter\n");
  913. switch (devinfo->bus_pub.devid) {
  914. case 43143:
  915. fwname = BRCMF_USB_43143_FW_NAME;
  916. break;
  917. case 43235:
  918. case 43236:
  919. case 43238:
  920. fwname = BRCMF_USB_43236_FW_NAME;
  921. break;
  922. case 43242:
  923. fwname = BRCMF_USB_43242_FW_NAME;
  924. break;
  925. default:
  926. return -EINVAL;
  927. break;
  928. }
  929. brcmf_dbg(USB, "Loading FW %s\n", fwname);
  930. list_for_each_entry(fw_image, &fw_image_list, list) {
  931. if (fw_image->fwname == fwname) {
  932. devinfo->image = fw_image->image;
  933. devinfo->image_len = fw_image->image_len;
  934. return 0;
  935. }
  936. }
  937. /* fw image not yet loaded. Load it now and add to list */
  938. err = request_firmware(&fw, fwname, devinfo->dev);
  939. if (!fw) {
  940. brcmf_err("fail to request firmware %s\n", fwname);
  941. return err;
  942. }
  943. if (check_file(fw->data) < 0) {
  944. brcmf_err("invalid firmware %s\n", fwname);
  945. return -EINVAL;
  946. }
  947. fw_image = kzalloc(sizeof(*fw_image), GFP_ATOMIC);
  948. if (!fw_image)
  949. return -ENOMEM;
  950. INIT_LIST_HEAD(&fw_image->list);
  951. list_add_tail(&fw_image->list, &fw_image_list);
  952. fw_image->fwname = fwname;
  953. fw_image->image = vmalloc(fw->size);
  954. if (!fw_image->image)
  955. return -ENOMEM;
  956. memcpy(fw_image->image, fw->data, fw->size);
  957. fw_image->image_len = fw->size;
  958. release_firmware(fw);
  959. devinfo->image = fw_image->image;
  960. devinfo->image_len = fw_image->image_len;
  961. return 0;
  962. }
  963. static
  964. struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
  965. int nrxq, int ntxq)
  966. {
  967. brcmf_dbg(USB, "Enter\n");
  968. devinfo->bus_pub.nrxq = nrxq;
  969. devinfo->rx_low_watermark = nrxq / 2;
  970. devinfo->bus_pub.devinfo = devinfo;
  971. devinfo->bus_pub.ntxq = ntxq;
  972. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
  973. /* flow control when too many tx urbs posted */
  974. devinfo->tx_low_watermark = ntxq / 4;
  975. devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
  976. devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
  977. /* Initialize other structure content */
  978. init_waitqueue_head(&devinfo->ioctl_resp_wait);
  979. /* Initialize the spinlocks */
  980. spin_lock_init(&devinfo->qlock);
  981. INIT_LIST_HEAD(&devinfo->rx_freeq);
  982. INIT_LIST_HEAD(&devinfo->rx_postq);
  983. INIT_LIST_HEAD(&devinfo->tx_freeq);
  984. INIT_LIST_HEAD(&devinfo->tx_postq);
  985. devinfo->tx_flowblock = false;
  986. devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
  987. if (!devinfo->rx_reqs)
  988. goto error;
  989. devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
  990. if (!devinfo->tx_reqs)
  991. goto error;
  992. devinfo->tx_freecount = ntxq;
  993. devinfo->intr_urb = usb_alloc_urb(0, GFP_ATOMIC);
  994. if (!devinfo->intr_urb) {
  995. brcmf_err("usb_alloc_urb (intr) failed\n");
  996. goto error;
  997. }
  998. devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
  999. if (!devinfo->ctl_urb) {
  1000. brcmf_err("usb_alloc_urb (ctl) failed\n");
  1001. goto error;
  1002. }
  1003. devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
  1004. if (!devinfo->bulk_urb) {
  1005. brcmf_err("usb_alloc_urb (bulk) failed\n");
  1006. goto error;
  1007. }
  1008. if (!brcmf_usb_dlneeded(devinfo))
  1009. return &devinfo->bus_pub;
  1010. brcmf_dbg(USB, "Start fw downloading\n");
  1011. if (brcmf_usb_get_fw(devinfo))
  1012. goto error;
  1013. if (brcmf_usb_fw_download(devinfo))
  1014. goto error;
  1015. return &devinfo->bus_pub;
  1016. error:
  1017. brcmf_err("failed!\n");
  1018. brcmf_usb_detach(devinfo);
  1019. return NULL;
  1020. }
  1021. static struct brcmf_bus_ops brcmf_usb_bus_ops = {
  1022. .txdata = brcmf_usb_tx,
  1023. .init = brcmf_usb_up,
  1024. .stop = brcmf_usb_down,
  1025. .txctl = brcmf_usb_tx_ctlpkt,
  1026. .rxctl = brcmf_usb_rx_ctlpkt,
  1027. };
  1028. static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
  1029. {
  1030. struct brcmf_bus *bus = NULL;
  1031. struct brcmf_usbdev *bus_pub = NULL;
  1032. int ret;
  1033. struct device *dev = devinfo->dev;
  1034. brcmf_dbg(USB, "Enter\n");
  1035. bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
  1036. if (!bus_pub)
  1037. return -ENODEV;
  1038. bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
  1039. if (!bus) {
  1040. ret = -ENOMEM;
  1041. goto fail;
  1042. }
  1043. bus->dev = dev;
  1044. bus_pub->bus = bus;
  1045. bus->bus_priv.usb = bus_pub;
  1046. dev_set_drvdata(dev, bus);
  1047. bus->ops = &brcmf_usb_bus_ops;
  1048. /* Attach to the common driver interface */
  1049. ret = brcmf_attach(0, dev);
  1050. if (ret) {
  1051. brcmf_err("brcmf_attach failed\n");
  1052. goto fail;
  1053. }
  1054. ret = brcmf_bus_start(dev);
  1055. if (ret) {
  1056. brcmf_err("dongle is not responding\n");
  1057. brcmf_detach(dev);
  1058. goto fail;
  1059. }
  1060. return 0;
  1061. fail:
  1062. /* Release resources in reverse order */
  1063. kfree(bus);
  1064. brcmf_usb_detach(devinfo);
  1065. return ret;
  1066. }
  1067. static void
  1068. brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
  1069. {
  1070. if (!devinfo)
  1071. return;
  1072. brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
  1073. brcmf_detach(devinfo->dev);
  1074. kfree(devinfo->bus_pub.bus);
  1075. brcmf_usb_detach(devinfo);
  1076. }
  1077. static int
  1078. brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1079. {
  1080. int ep;
  1081. struct usb_endpoint_descriptor *endpoint;
  1082. int ret = 0;
  1083. struct usb_device *usb = interface_to_usbdev(intf);
  1084. int num_of_eps;
  1085. u8 endpoint_num;
  1086. struct brcmf_usbdev_info *devinfo;
  1087. brcmf_dbg(USB, "Enter\n");
  1088. devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
  1089. if (devinfo == NULL)
  1090. return -ENOMEM;
  1091. devinfo->usbdev = usb;
  1092. devinfo->dev = &usb->dev;
  1093. usb_set_intfdata(intf, devinfo);
  1094. /* Check that the device supports only one configuration */
  1095. if (usb->descriptor.bNumConfigurations != 1) {
  1096. ret = -1;
  1097. goto fail;
  1098. }
  1099. if (usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
  1100. ret = -1;
  1101. goto fail;
  1102. }
  1103. /*
  1104. * Only the BDC interface configuration is supported:
  1105. * Device class: USB_CLASS_VENDOR_SPEC
  1106. * if0 class: USB_CLASS_VENDOR_SPEC
  1107. * if0/ep0: control
  1108. * if0/ep1: bulk in
  1109. * if0/ep2: bulk out (ok if swapped with bulk in)
  1110. */
  1111. if (CONFIGDESC(usb)->bNumInterfaces != 1) {
  1112. ret = -1;
  1113. goto fail;
  1114. }
  1115. /* Check interface */
  1116. if (IFDESC(usb, CONTROL_IF).bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
  1117. IFDESC(usb, CONTROL_IF).bInterfaceSubClass != 2 ||
  1118. IFDESC(usb, CONTROL_IF).bInterfaceProtocol != 0xff) {
  1119. brcmf_err("invalid control interface: class %d, subclass %d, proto %d\n",
  1120. IFDESC(usb, CONTROL_IF).bInterfaceClass,
  1121. IFDESC(usb, CONTROL_IF).bInterfaceSubClass,
  1122. IFDESC(usb, CONTROL_IF).bInterfaceProtocol);
  1123. ret = -1;
  1124. goto fail;
  1125. }
  1126. /* Check control endpoint */
  1127. endpoint = &IFEPDESC(usb, CONTROL_IF, 0);
  1128. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1129. != USB_ENDPOINT_XFER_INT) {
  1130. brcmf_err("invalid control endpoint %d\n",
  1131. endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
  1132. ret = -1;
  1133. goto fail;
  1134. }
  1135. endpoint_num = endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1136. devinfo->intr_pipe = usb_rcvintpipe(usb, endpoint_num);
  1137. devinfo->rx_pipe = 0;
  1138. devinfo->rx_pipe2 = 0;
  1139. devinfo->tx_pipe = 0;
  1140. num_of_eps = IFDESC(usb, BULK_IF).bNumEndpoints - 1;
  1141. /* Check data endpoints and get pipes */
  1142. for (ep = 1; ep <= num_of_eps; ep++) {
  1143. endpoint = &IFEPDESC(usb, BULK_IF, ep);
  1144. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1145. USB_ENDPOINT_XFER_BULK) {
  1146. brcmf_err("invalid data endpoint %d\n", ep);
  1147. ret = -1;
  1148. goto fail;
  1149. }
  1150. endpoint_num = endpoint->bEndpointAddress &
  1151. USB_ENDPOINT_NUMBER_MASK;
  1152. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1153. == USB_DIR_IN) {
  1154. if (!devinfo->rx_pipe) {
  1155. devinfo->rx_pipe =
  1156. usb_rcvbulkpipe(usb, endpoint_num);
  1157. } else {
  1158. devinfo->rx_pipe2 =
  1159. usb_rcvbulkpipe(usb, endpoint_num);
  1160. }
  1161. } else {
  1162. devinfo->tx_pipe = usb_sndbulkpipe(usb, endpoint_num);
  1163. }
  1164. }
  1165. /* Allocate interrupt URB and data buffer */
  1166. /* RNDIS says 8-byte intr, our old drivers used 4-byte */
  1167. if (IFEPDESC(usb, CONTROL_IF, 0).wMaxPacketSize == cpu_to_le16(16))
  1168. devinfo->intr_size = 8;
  1169. else
  1170. devinfo->intr_size = 4;
  1171. devinfo->interval = IFEPDESC(usb, CONTROL_IF, 0).bInterval;
  1172. if (usb->speed == USB_SPEED_HIGH)
  1173. brcmf_dbg(USB, "Broadcom high speed USB wireless device detected\n");
  1174. else
  1175. brcmf_dbg(USB, "Broadcom full speed USB wireless device detected\n");
  1176. ret = brcmf_usb_probe_cb(devinfo);
  1177. if (ret)
  1178. goto fail;
  1179. /* Success */
  1180. return 0;
  1181. fail:
  1182. brcmf_err("failed with errno %d\n", ret);
  1183. kfree(devinfo);
  1184. usb_set_intfdata(intf, NULL);
  1185. return ret;
  1186. }
  1187. static void
  1188. brcmf_usb_disconnect(struct usb_interface *intf)
  1189. {
  1190. struct brcmf_usbdev_info *devinfo;
  1191. brcmf_dbg(USB, "Enter\n");
  1192. devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
  1193. brcmf_usb_disconnect_cb(devinfo);
  1194. kfree(devinfo);
  1195. brcmf_dbg(USB, "Exit\n");
  1196. }
  1197. /*
  1198. * only need to signal the bus being down and update the state.
  1199. */
  1200. static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
  1201. {
  1202. struct usb_device *usb = interface_to_usbdev(intf);
  1203. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1204. brcmf_dbg(USB, "Enter\n");
  1205. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
  1206. brcmf_detach(&usb->dev);
  1207. return 0;
  1208. }
  1209. /*
  1210. * (re-) start the bus.
  1211. */
  1212. static int brcmf_usb_resume(struct usb_interface *intf)
  1213. {
  1214. struct usb_device *usb = interface_to_usbdev(intf);
  1215. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1216. brcmf_dbg(USB, "Enter\n");
  1217. if (!brcmf_attach(0, devinfo->dev))
  1218. return brcmf_bus_start(&usb->dev);
  1219. return 0;
  1220. }
  1221. static int brcmf_usb_reset_resume(struct usb_interface *intf)
  1222. {
  1223. struct usb_device *usb = interface_to_usbdev(intf);
  1224. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1225. brcmf_dbg(USB, "Enter\n");
  1226. if (!brcmf_usb_fw_download(devinfo))
  1227. return brcmf_usb_resume(intf);
  1228. return -EIO;
  1229. }
  1230. #define BRCMF_USB_VENDOR_ID_BROADCOM 0x0a5c
  1231. #define BRCMF_USB_DEVICE_ID_43143 0xbd1e
  1232. #define BRCMF_USB_DEVICE_ID_43236 0xbd17
  1233. #define BRCMF_USB_DEVICE_ID_43242 0xbd1f
  1234. #define BRCMF_USB_DEVICE_ID_BCMFW 0x0bdc
  1235. static struct usb_device_id brcmf_usb_devid_table[] = {
  1236. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43143) },
  1237. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43236) },
  1238. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43242) },
  1239. /* special entry for device with firmware loaded and running */
  1240. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_BCMFW) },
  1241. { }
  1242. };
  1243. MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
  1244. MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME);
  1245. MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME);
  1246. MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME);
  1247. static struct usb_driver brcmf_usbdrvr = {
  1248. .name = KBUILD_MODNAME,
  1249. .probe = brcmf_usb_probe,
  1250. .disconnect = brcmf_usb_disconnect,
  1251. .id_table = brcmf_usb_devid_table,
  1252. .suspend = brcmf_usb_suspend,
  1253. .resume = brcmf_usb_resume,
  1254. .reset_resume = brcmf_usb_reset_resume,
  1255. .supports_autosuspend = 1,
  1256. .disable_hub_initiated_lpm = 1,
  1257. };
  1258. static void brcmf_release_fw(struct list_head *q)
  1259. {
  1260. struct brcmf_usb_image *fw_image, *next;
  1261. list_for_each_entry_safe(fw_image, next, q, list) {
  1262. vfree(fw_image->image);
  1263. list_del_init(&fw_image->list);
  1264. }
  1265. }
  1266. void brcmf_usb_exit(void)
  1267. {
  1268. brcmf_dbg(USB, "Enter\n");
  1269. usb_deregister(&brcmf_usbdrvr);
  1270. brcmf_release_fw(&fw_image_list);
  1271. }
  1272. void brcmf_usb_init(void)
  1273. {
  1274. brcmf_dbg(USB, "Enter\n");
  1275. INIT_LIST_HEAD(&fw_image_list);
  1276. usb_register(&brcmf_usbdrvr);
  1277. }