usb.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542
  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. brcmf_txcomplete(devinfo->dev, req->skb, urb->status == 0);
  357. brcmu_pkt_buf_free_skb(req->skb);
  358. req->skb = NULL;
  359. brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req, &devinfo->tx_freecount);
  360. if (devinfo->tx_freecount > devinfo->tx_high_watermark &&
  361. devinfo->tx_flowblock) {
  362. brcmf_txflowblock(devinfo->dev, false);
  363. devinfo->tx_flowblock = false;
  364. }
  365. }
  366. static void brcmf_usb_rx_complete(struct urb *urb)
  367. {
  368. struct brcmf_usbreq *req = (struct brcmf_usbreq *)urb->context;
  369. struct brcmf_usbdev_info *devinfo = req->devinfo;
  370. struct sk_buff *skb;
  371. struct sk_buff_head skbq;
  372. brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
  373. brcmf_usb_del_fromq(devinfo, req);
  374. skb = req->skb;
  375. req->skb = NULL;
  376. /* zero lenght packets indicate usb "failure". Do not refill */
  377. if (urb->status != 0 || !urb->actual_length) {
  378. brcmu_pkt_buf_free_skb(skb);
  379. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  380. return;
  381. }
  382. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
  383. skb_queue_head_init(&skbq);
  384. skb_queue_tail(&skbq, skb);
  385. skb_put(skb, urb->actual_length);
  386. brcmf_rx_frames(devinfo->dev, &skbq);
  387. brcmf_usb_rx_refill(devinfo, req);
  388. } else {
  389. brcmu_pkt_buf_free_skb(skb);
  390. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  391. }
  392. return;
  393. }
  394. static void brcmf_usb_rx_refill(struct brcmf_usbdev_info *devinfo,
  395. struct brcmf_usbreq *req)
  396. {
  397. struct sk_buff *skb;
  398. int ret;
  399. if (!req || !devinfo)
  400. return;
  401. skb = dev_alloc_skb(devinfo->bus_pub.bus_mtu);
  402. if (!skb) {
  403. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  404. return;
  405. }
  406. req->skb = skb;
  407. usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->rx_pipe,
  408. skb->data, skb_tailroom(skb), brcmf_usb_rx_complete,
  409. req);
  410. req->devinfo = devinfo;
  411. brcmf_usb_enq(devinfo, &devinfo->rx_postq, req, NULL);
  412. ret = usb_submit_urb(req->urb, GFP_ATOMIC);
  413. if (ret) {
  414. brcmf_usb_del_fromq(devinfo, req);
  415. brcmu_pkt_buf_free_skb(req->skb);
  416. req->skb = NULL;
  417. brcmf_usb_enq(devinfo, &devinfo->rx_freeq, req, NULL);
  418. }
  419. return;
  420. }
  421. static void brcmf_usb_rx_fill_all(struct brcmf_usbdev_info *devinfo)
  422. {
  423. struct brcmf_usbreq *req;
  424. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP) {
  425. brcmf_err("bus is not up=%d\n", devinfo->bus_pub.state);
  426. return;
  427. }
  428. while ((req = brcmf_usb_deq(devinfo, &devinfo->rx_freeq, NULL)) != NULL)
  429. brcmf_usb_rx_refill(devinfo, req);
  430. }
  431. static void
  432. brcmf_usb_state_change(struct brcmf_usbdev_info *devinfo, int state)
  433. {
  434. struct brcmf_bus *bcmf_bus = devinfo->bus_pub.bus;
  435. int old_state;
  436. brcmf_dbg(USB, "Enter, current state=%d, new state=%d\n",
  437. devinfo->bus_pub.state, state);
  438. if (devinfo->bus_pub.state == state)
  439. return;
  440. old_state = devinfo->bus_pub.state;
  441. devinfo->bus_pub.state = state;
  442. /* update state of upper layer */
  443. if (state == BRCMFMAC_USB_STATE_DOWN) {
  444. brcmf_dbg(USB, "DBUS is down\n");
  445. bcmf_bus->state = BRCMF_BUS_DOWN;
  446. } else if (state == BRCMFMAC_USB_STATE_UP) {
  447. brcmf_dbg(USB, "DBUS is up\n");
  448. bcmf_bus->state = BRCMF_BUS_DATA;
  449. } else {
  450. brcmf_dbg(USB, "DBUS current state=%d\n", state);
  451. }
  452. }
  453. static void
  454. brcmf_usb_intr_complete(struct urb *urb)
  455. {
  456. struct brcmf_usbdev_info *devinfo =
  457. (struct brcmf_usbdev_info *)urb->context;
  458. int err;
  459. brcmf_dbg(USB, "Enter, urb->status=%d\n", urb->status);
  460. if (devinfo == NULL)
  461. return;
  462. if (unlikely(urb->status)) {
  463. if (urb->status == -ENOENT ||
  464. urb->status == -ESHUTDOWN ||
  465. urb->status == -ENODEV) {
  466. brcmf_usb_state_change(devinfo,
  467. BRCMFMAC_USB_STATE_DOWN);
  468. }
  469. }
  470. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN) {
  471. brcmf_err("intr cb when DBUS down, ignoring\n");
  472. return;
  473. }
  474. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP) {
  475. err = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
  476. if (err)
  477. brcmf_err("usb_submit_urb, err=%d\n", err);
  478. }
  479. }
  480. static int brcmf_usb_tx(struct device *dev, struct sk_buff *skb)
  481. {
  482. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  483. struct brcmf_usbreq *req;
  484. int ret;
  485. brcmf_dbg(USB, "Enter, skb=%p\n", skb);
  486. if (devinfo->bus_pub.state != BRCMFMAC_USB_STATE_UP)
  487. return -EIO;
  488. req = brcmf_usb_deq(devinfo, &devinfo->tx_freeq,
  489. &devinfo->tx_freecount);
  490. if (!req) {
  491. brcmu_pkt_buf_free_skb(skb);
  492. brcmf_err("no req to send\n");
  493. return -ENOMEM;
  494. }
  495. req->skb = skb;
  496. req->devinfo = devinfo;
  497. usb_fill_bulk_urb(req->urb, devinfo->usbdev, devinfo->tx_pipe,
  498. skb->data, skb->len, brcmf_usb_tx_complete, req);
  499. req->urb->transfer_flags |= URB_ZERO_PACKET;
  500. brcmf_usb_enq(devinfo, &devinfo->tx_postq, req, NULL);
  501. ret = usb_submit_urb(req->urb, GFP_ATOMIC);
  502. if (ret) {
  503. brcmf_err("brcmf_usb_tx usb_submit_urb FAILED\n");
  504. brcmf_usb_del_fromq(devinfo, req);
  505. brcmu_pkt_buf_free_skb(req->skb);
  506. req->skb = NULL;
  507. brcmf_usb_enq(devinfo, &devinfo->tx_freeq, req,
  508. &devinfo->tx_freecount);
  509. } else {
  510. if (devinfo->tx_freecount < devinfo->tx_low_watermark &&
  511. !devinfo->tx_flowblock) {
  512. brcmf_txflowblock(dev, true);
  513. devinfo->tx_flowblock = true;
  514. }
  515. }
  516. return ret;
  517. }
  518. static int brcmf_usb_up(struct device *dev)
  519. {
  520. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  521. u16 ifnum;
  522. int ret;
  523. brcmf_dbg(USB, "Enter\n");
  524. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_UP)
  525. return 0;
  526. /* Success, indicate devinfo is fully up */
  527. brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_UP);
  528. if (devinfo->intr_urb) {
  529. usb_fill_int_urb(devinfo->intr_urb, devinfo->usbdev,
  530. devinfo->intr_pipe,
  531. &devinfo->intr,
  532. devinfo->intr_size,
  533. (usb_complete_t)brcmf_usb_intr_complete,
  534. devinfo,
  535. devinfo->interval);
  536. ret = usb_submit_urb(devinfo->intr_urb, GFP_ATOMIC);
  537. if (ret) {
  538. brcmf_err("USB_SUBMIT_URB failed with status %d\n",
  539. ret);
  540. return -EINVAL;
  541. }
  542. }
  543. if (devinfo->ctl_urb) {
  544. devinfo->ctl_in_pipe = usb_rcvctrlpipe(devinfo->usbdev, 0);
  545. devinfo->ctl_out_pipe = usb_sndctrlpipe(devinfo->usbdev, 0);
  546. ifnum = IFDESC(devinfo->usbdev, CONTROL_IF).bInterfaceNumber;
  547. /* CTL Write */
  548. devinfo->ctl_write.bRequestType =
  549. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  550. devinfo->ctl_write.bRequest = 0;
  551. devinfo->ctl_write.wValue = cpu_to_le16(0);
  552. devinfo->ctl_write.wIndex = cpu_to_le16p(&ifnum);
  553. /* CTL Read */
  554. devinfo->ctl_read.bRequestType =
  555. USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  556. devinfo->ctl_read.bRequest = 1;
  557. devinfo->ctl_read.wValue = cpu_to_le16(0);
  558. devinfo->ctl_read.wIndex = cpu_to_le16p(&ifnum);
  559. }
  560. brcmf_usb_rx_fill_all(devinfo);
  561. return 0;
  562. }
  563. static void brcmf_usb_down(struct device *dev)
  564. {
  565. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(dev);
  566. brcmf_dbg(USB, "Enter\n");
  567. if (devinfo == NULL)
  568. return;
  569. if (devinfo->bus_pub.state == BRCMFMAC_USB_STATE_DOWN)
  570. return;
  571. brcmf_usb_state_change(devinfo, BRCMFMAC_USB_STATE_DOWN);
  572. if (devinfo->intr_urb)
  573. usb_kill_urb(devinfo->intr_urb);
  574. if (devinfo->ctl_urb)
  575. usb_kill_urb(devinfo->ctl_urb);
  576. if (devinfo->bulk_urb)
  577. usb_kill_urb(devinfo->bulk_urb);
  578. brcmf_usb_free_q(&devinfo->tx_postq, true);
  579. brcmf_usb_free_q(&devinfo->rx_postq, true);
  580. }
  581. static void
  582. brcmf_usb_sync_complete(struct urb *urb)
  583. {
  584. struct brcmf_usbdev_info *devinfo =
  585. (struct brcmf_usbdev_info *)urb->context;
  586. devinfo->ctl_completed = true;
  587. brcmf_usb_ioctl_resp_wake(devinfo);
  588. }
  589. static bool brcmf_usb_dl_cmd(struct brcmf_usbdev_info *devinfo, u8 cmd,
  590. void *buffer, int buflen)
  591. {
  592. int ret = 0;
  593. char *tmpbuf;
  594. u16 size;
  595. if ((!devinfo) || (devinfo->ctl_urb == NULL))
  596. return false;
  597. tmpbuf = kmalloc(buflen, GFP_ATOMIC);
  598. if (!tmpbuf)
  599. return false;
  600. size = buflen;
  601. devinfo->ctl_urb->transfer_buffer_length = size;
  602. devinfo->ctl_read.wLength = cpu_to_le16p(&size);
  603. devinfo->ctl_read.bRequestType = USB_DIR_IN | USB_TYPE_VENDOR |
  604. USB_RECIP_INTERFACE;
  605. devinfo->ctl_read.bRequest = cmd;
  606. usb_fill_control_urb(devinfo->ctl_urb,
  607. devinfo->usbdev,
  608. usb_rcvctrlpipe(devinfo->usbdev, 0),
  609. (unsigned char *) &devinfo->ctl_read,
  610. (void *) tmpbuf, size,
  611. (usb_complete_t)brcmf_usb_sync_complete, devinfo);
  612. devinfo->ctl_completed = false;
  613. ret = usb_submit_urb(devinfo->ctl_urb, GFP_ATOMIC);
  614. if (ret < 0) {
  615. brcmf_err("usb_submit_urb failed %d\n", ret);
  616. kfree(tmpbuf);
  617. return false;
  618. }
  619. ret = brcmf_usb_ioctl_resp_wait(devinfo);
  620. memcpy(buffer, tmpbuf, buflen);
  621. kfree(tmpbuf);
  622. return ret;
  623. }
  624. static bool
  625. brcmf_usb_dlneeded(struct brcmf_usbdev_info *devinfo)
  626. {
  627. struct bootrom_id_le id;
  628. u32 chipid, chiprev;
  629. brcmf_dbg(USB, "Enter\n");
  630. if (devinfo == NULL)
  631. return false;
  632. /* Check if firmware downloaded already by querying runtime ID */
  633. id.chip = cpu_to_le32(0xDEAD);
  634. brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
  635. chipid = le32_to_cpu(id.chip);
  636. chiprev = le32_to_cpu(id.chiprev);
  637. if ((chipid & 0x4300) == 0x4300)
  638. brcmf_dbg(USB, "chip %x rev 0x%x\n", chipid, chiprev);
  639. else
  640. brcmf_dbg(USB, "chip %d rev 0x%x\n", chipid, chiprev);
  641. if (chipid == BRCMF_POSTBOOT_ID) {
  642. brcmf_dbg(USB, "firmware already downloaded\n");
  643. brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
  644. return false;
  645. } else {
  646. devinfo->bus_pub.devid = chipid;
  647. devinfo->bus_pub.chiprev = chiprev;
  648. }
  649. return true;
  650. }
  651. static int
  652. brcmf_usb_resetcfg(struct brcmf_usbdev_info *devinfo)
  653. {
  654. struct bootrom_id_le id;
  655. u32 loop_cnt;
  656. brcmf_dbg(USB, "Enter\n");
  657. loop_cnt = 0;
  658. do {
  659. mdelay(BRCMF_USB_RESET_GETVER_SPINWAIT);
  660. loop_cnt++;
  661. id.chip = cpu_to_le32(0xDEAD); /* Get the ID */
  662. brcmf_usb_dl_cmd(devinfo, DL_GETVER, &id, sizeof(id));
  663. if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID))
  664. break;
  665. } while (loop_cnt < BRCMF_USB_RESET_GETVER_LOOP_CNT);
  666. if (id.chip == cpu_to_le32(BRCMF_POSTBOOT_ID)) {
  667. brcmf_dbg(USB, "postboot chip 0x%x/rev 0x%x\n",
  668. le32_to_cpu(id.chip), le32_to_cpu(id.chiprev));
  669. brcmf_usb_dl_cmd(devinfo, DL_RESETCFG, &id, sizeof(id));
  670. return 0;
  671. } else {
  672. brcmf_err("Cannot talk to Dongle. Firmware is not UP, %d ms\n",
  673. BRCMF_USB_RESET_GETVER_SPINWAIT * loop_cnt);
  674. return -EINVAL;
  675. }
  676. }
  677. static int
  678. brcmf_usb_dl_send_bulk(struct brcmf_usbdev_info *devinfo, void *buffer, int len)
  679. {
  680. int ret;
  681. if ((devinfo == NULL) || (devinfo->bulk_urb == NULL))
  682. return -EINVAL;
  683. /* Prepare the URB */
  684. usb_fill_bulk_urb(devinfo->bulk_urb, devinfo->usbdev,
  685. devinfo->tx_pipe, buffer, len,
  686. (usb_complete_t)brcmf_usb_sync_complete, devinfo);
  687. devinfo->bulk_urb->transfer_flags |= URB_ZERO_PACKET;
  688. devinfo->ctl_completed = false;
  689. ret = usb_submit_urb(devinfo->bulk_urb, GFP_ATOMIC);
  690. if (ret) {
  691. brcmf_err("usb_submit_urb failed %d\n", ret);
  692. return ret;
  693. }
  694. ret = brcmf_usb_ioctl_resp_wait(devinfo);
  695. return (ret == 0);
  696. }
  697. static int
  698. brcmf_usb_dl_writeimage(struct brcmf_usbdev_info *devinfo, u8 *fw, int fwlen)
  699. {
  700. unsigned int sendlen, sent, dllen;
  701. char *bulkchunk = NULL, *dlpos;
  702. struct rdl_state_le state;
  703. u32 rdlstate, rdlbytes;
  704. int err = 0;
  705. brcmf_dbg(USB, "Enter, fw %p, len %d\n", fw, fwlen);
  706. bulkchunk = kmalloc(RDL_CHUNK, GFP_ATOMIC);
  707. if (bulkchunk == NULL) {
  708. err = -ENOMEM;
  709. goto fail;
  710. }
  711. /* 1) Prepare USB boot loader for runtime image */
  712. brcmf_usb_dl_cmd(devinfo, DL_START, &state,
  713. sizeof(struct rdl_state_le));
  714. rdlstate = le32_to_cpu(state.state);
  715. rdlbytes = le32_to_cpu(state.bytes);
  716. /* 2) Check we are in the Waiting state */
  717. if (rdlstate != DL_WAITING) {
  718. brcmf_err("Failed to DL_START\n");
  719. err = -EINVAL;
  720. goto fail;
  721. }
  722. sent = 0;
  723. dlpos = fw;
  724. dllen = fwlen;
  725. /* Get chip id and rev */
  726. while (rdlbytes != dllen) {
  727. /* Wait until the usb device reports it received all
  728. * the bytes we sent */
  729. if ((rdlbytes == sent) && (rdlbytes != dllen)) {
  730. if ((dllen-sent) < RDL_CHUNK)
  731. sendlen = dllen-sent;
  732. else
  733. sendlen = RDL_CHUNK;
  734. /* simply avoid having to send a ZLP by ensuring we
  735. * never have an even
  736. * multiple of 64
  737. */
  738. if (!(sendlen % 64))
  739. sendlen -= 4;
  740. /* send data */
  741. memcpy(bulkchunk, dlpos, sendlen);
  742. if (brcmf_usb_dl_send_bulk(devinfo, bulkchunk,
  743. sendlen)) {
  744. brcmf_err("send_bulk failed\n");
  745. err = -EINVAL;
  746. goto fail;
  747. }
  748. dlpos += sendlen;
  749. sent += sendlen;
  750. }
  751. if (!brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
  752. sizeof(struct rdl_state_le))) {
  753. brcmf_err("DL_GETSTATE Failed xxxx\n");
  754. err = -EINVAL;
  755. goto fail;
  756. }
  757. rdlstate = le32_to_cpu(state.state);
  758. rdlbytes = le32_to_cpu(state.bytes);
  759. /* restart if an error is reported */
  760. if (rdlstate == DL_BAD_HDR || rdlstate == DL_BAD_CRC) {
  761. brcmf_err("Bad Hdr or Bad CRC state %d\n",
  762. rdlstate);
  763. err = -EINVAL;
  764. goto fail;
  765. }
  766. }
  767. fail:
  768. kfree(bulkchunk);
  769. brcmf_dbg(USB, "Exit, err=%d\n", err);
  770. return err;
  771. }
  772. static int brcmf_usb_dlstart(struct brcmf_usbdev_info *devinfo, u8 *fw, int len)
  773. {
  774. int err;
  775. brcmf_dbg(USB, "Enter\n");
  776. if (devinfo == NULL)
  777. return -EINVAL;
  778. if (devinfo->bus_pub.devid == 0xDEAD)
  779. return -EINVAL;
  780. err = brcmf_usb_dl_writeimage(devinfo, fw, len);
  781. if (err == 0)
  782. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_DONE;
  783. else
  784. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DL_FAIL;
  785. brcmf_dbg(USB, "Exit, err=%d\n", err);
  786. return err;
  787. }
  788. static int brcmf_usb_dlrun(struct brcmf_usbdev_info *devinfo)
  789. {
  790. struct rdl_state_le state;
  791. brcmf_dbg(USB, "Enter\n");
  792. if (!devinfo)
  793. return -EINVAL;
  794. if (devinfo->bus_pub.devid == 0xDEAD)
  795. return -EINVAL;
  796. /* Check we are runnable */
  797. brcmf_usb_dl_cmd(devinfo, DL_GETSTATE, &state,
  798. sizeof(struct rdl_state_le));
  799. /* Start the image */
  800. if (state.state == cpu_to_le32(DL_RUNNABLE)) {
  801. if (!brcmf_usb_dl_cmd(devinfo, DL_GO, &state,
  802. sizeof(struct rdl_state_le)))
  803. return -ENODEV;
  804. if (brcmf_usb_resetcfg(devinfo))
  805. return -ENODEV;
  806. /* The Dongle may go for re-enumeration. */
  807. } else {
  808. brcmf_err("Dongle not runnable\n");
  809. return -EINVAL;
  810. }
  811. brcmf_dbg(USB, "Exit\n");
  812. return 0;
  813. }
  814. static bool brcmf_usb_chip_support(int chipid, int chiprev)
  815. {
  816. switch(chipid) {
  817. case 43143:
  818. return true;
  819. case 43235:
  820. case 43236:
  821. case 43238:
  822. return (chiprev == 3);
  823. case 43242:
  824. return true;
  825. default:
  826. break;
  827. }
  828. return false;
  829. }
  830. static int
  831. brcmf_usb_fw_download(struct brcmf_usbdev_info *devinfo)
  832. {
  833. int devid, chiprev;
  834. int err;
  835. brcmf_dbg(USB, "Enter\n");
  836. if (devinfo == NULL)
  837. return -ENODEV;
  838. devid = devinfo->bus_pub.devid;
  839. chiprev = devinfo->bus_pub.chiprev;
  840. if (!brcmf_usb_chip_support(devid, chiprev)) {
  841. brcmf_err("unsupported chip %d rev %d\n",
  842. devid, chiprev);
  843. return -EINVAL;
  844. }
  845. if (!devinfo->image) {
  846. brcmf_err("No firmware!\n");
  847. return -ENOENT;
  848. }
  849. err = brcmf_usb_dlstart(devinfo,
  850. devinfo->image, devinfo->image_len);
  851. if (err == 0)
  852. err = brcmf_usb_dlrun(devinfo);
  853. return err;
  854. }
  855. static void brcmf_usb_detach(struct brcmf_usbdev_info *devinfo)
  856. {
  857. brcmf_dbg(USB, "Enter, devinfo %p\n", devinfo);
  858. /* free the URBS */
  859. brcmf_usb_free_q(&devinfo->rx_freeq, false);
  860. brcmf_usb_free_q(&devinfo->tx_freeq, false);
  861. usb_free_urb(devinfo->intr_urb);
  862. usb_free_urb(devinfo->ctl_urb);
  863. usb_free_urb(devinfo->bulk_urb);
  864. kfree(devinfo->tx_reqs);
  865. kfree(devinfo->rx_reqs);
  866. }
  867. #define TRX_MAGIC 0x30524448 /* "HDR0" */
  868. #define TRX_VERSION 1 /* Version 1 */
  869. #define TRX_MAX_LEN 0x3B0000 /* Max length */
  870. #define TRX_NO_HEADER 1 /* Do not write TRX header */
  871. #define TRX_MAX_OFFSET 3 /* Max number of individual files */
  872. #define TRX_UNCOMP_IMAGE 0x20 /* Trx contains uncompressed image */
  873. struct trx_header_le {
  874. __le32 magic; /* "HDR0" */
  875. __le32 len; /* Length of file including header */
  876. __le32 crc32; /* CRC from flag_version to end of file */
  877. __le32 flag_version; /* 0:15 flags, 16:31 version */
  878. __le32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of
  879. * header */
  880. };
  881. static int check_file(const u8 *headers)
  882. {
  883. struct trx_header_le *trx;
  884. int actual_len = -1;
  885. brcmf_dbg(USB, "Enter\n");
  886. /* Extract trx header */
  887. trx = (struct trx_header_le *) headers;
  888. if (trx->magic != cpu_to_le32(TRX_MAGIC))
  889. return -1;
  890. headers += sizeof(struct trx_header_le);
  891. if (le32_to_cpu(trx->flag_version) & TRX_UNCOMP_IMAGE) {
  892. actual_len = le32_to_cpu(trx->offsets[TRX_OFFSETS_DLFWLEN_IDX]);
  893. return actual_len + sizeof(struct trx_header_le);
  894. }
  895. return -1;
  896. }
  897. static int brcmf_usb_get_fw(struct brcmf_usbdev_info *devinfo)
  898. {
  899. s8 *fwname;
  900. const struct firmware *fw;
  901. struct brcmf_usb_image *fw_image;
  902. int err;
  903. brcmf_dbg(USB, "Enter\n");
  904. switch (devinfo->bus_pub.devid) {
  905. case 43143:
  906. fwname = BRCMF_USB_43143_FW_NAME;
  907. break;
  908. case 43235:
  909. case 43236:
  910. case 43238:
  911. fwname = BRCMF_USB_43236_FW_NAME;
  912. break;
  913. case 43242:
  914. fwname = BRCMF_USB_43242_FW_NAME;
  915. break;
  916. default:
  917. return -EINVAL;
  918. break;
  919. }
  920. brcmf_dbg(USB, "Loading FW %s\n", fwname);
  921. list_for_each_entry(fw_image, &fw_image_list, list) {
  922. if (fw_image->fwname == fwname) {
  923. devinfo->image = fw_image->image;
  924. devinfo->image_len = fw_image->image_len;
  925. return 0;
  926. }
  927. }
  928. /* fw image not yet loaded. Load it now and add to list */
  929. err = request_firmware(&fw, fwname, devinfo->dev);
  930. if (!fw) {
  931. brcmf_err("fail to request firmware %s\n", fwname);
  932. return err;
  933. }
  934. if (check_file(fw->data) < 0) {
  935. brcmf_err("invalid firmware %s\n", fwname);
  936. return -EINVAL;
  937. }
  938. fw_image = kzalloc(sizeof(*fw_image), GFP_ATOMIC);
  939. if (!fw_image)
  940. return -ENOMEM;
  941. INIT_LIST_HEAD(&fw_image->list);
  942. list_add_tail(&fw_image->list, &fw_image_list);
  943. fw_image->fwname = fwname;
  944. fw_image->image = vmalloc(fw->size);
  945. if (!fw_image->image)
  946. return -ENOMEM;
  947. memcpy(fw_image->image, fw->data, fw->size);
  948. fw_image->image_len = fw->size;
  949. release_firmware(fw);
  950. devinfo->image = fw_image->image;
  951. devinfo->image_len = fw_image->image_len;
  952. return 0;
  953. }
  954. static
  955. struct brcmf_usbdev *brcmf_usb_attach(struct brcmf_usbdev_info *devinfo,
  956. int nrxq, int ntxq)
  957. {
  958. brcmf_dbg(USB, "Enter\n");
  959. devinfo->bus_pub.nrxq = nrxq;
  960. devinfo->rx_low_watermark = nrxq / 2;
  961. devinfo->bus_pub.devinfo = devinfo;
  962. devinfo->bus_pub.ntxq = ntxq;
  963. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_DOWN;
  964. /* flow control when too many tx urbs posted */
  965. devinfo->tx_low_watermark = ntxq / 4;
  966. devinfo->tx_high_watermark = devinfo->tx_low_watermark * 3;
  967. devinfo->bus_pub.bus_mtu = BRCMF_USB_MAX_PKT_SIZE;
  968. /* Initialize other structure content */
  969. init_waitqueue_head(&devinfo->ioctl_resp_wait);
  970. /* Initialize the spinlocks */
  971. spin_lock_init(&devinfo->qlock);
  972. INIT_LIST_HEAD(&devinfo->rx_freeq);
  973. INIT_LIST_HEAD(&devinfo->rx_postq);
  974. INIT_LIST_HEAD(&devinfo->tx_freeq);
  975. INIT_LIST_HEAD(&devinfo->tx_postq);
  976. devinfo->tx_flowblock = false;
  977. devinfo->rx_reqs = brcmf_usbdev_qinit(&devinfo->rx_freeq, nrxq);
  978. if (!devinfo->rx_reqs)
  979. goto error;
  980. devinfo->tx_reqs = brcmf_usbdev_qinit(&devinfo->tx_freeq, ntxq);
  981. if (!devinfo->tx_reqs)
  982. goto error;
  983. devinfo->tx_freecount = ntxq;
  984. devinfo->intr_urb = usb_alloc_urb(0, GFP_ATOMIC);
  985. if (!devinfo->intr_urb) {
  986. brcmf_err("usb_alloc_urb (intr) failed\n");
  987. goto error;
  988. }
  989. devinfo->ctl_urb = usb_alloc_urb(0, GFP_ATOMIC);
  990. if (!devinfo->ctl_urb) {
  991. brcmf_err("usb_alloc_urb (ctl) failed\n");
  992. goto error;
  993. }
  994. devinfo->bulk_urb = usb_alloc_urb(0, GFP_ATOMIC);
  995. if (!devinfo->bulk_urb) {
  996. brcmf_err("usb_alloc_urb (bulk) failed\n");
  997. goto error;
  998. }
  999. if (!brcmf_usb_dlneeded(devinfo))
  1000. return &devinfo->bus_pub;
  1001. brcmf_dbg(USB, "Start fw downloading\n");
  1002. if (brcmf_usb_get_fw(devinfo))
  1003. goto error;
  1004. if (brcmf_usb_fw_download(devinfo))
  1005. goto error;
  1006. return &devinfo->bus_pub;
  1007. error:
  1008. brcmf_err("failed!\n");
  1009. brcmf_usb_detach(devinfo);
  1010. return NULL;
  1011. }
  1012. static struct brcmf_bus_ops brcmf_usb_bus_ops = {
  1013. .txdata = brcmf_usb_tx,
  1014. .init = brcmf_usb_up,
  1015. .stop = brcmf_usb_down,
  1016. .txctl = brcmf_usb_tx_ctlpkt,
  1017. .rxctl = brcmf_usb_rx_ctlpkt,
  1018. };
  1019. static int brcmf_usb_probe_cb(struct brcmf_usbdev_info *devinfo)
  1020. {
  1021. struct brcmf_bus *bus = NULL;
  1022. struct brcmf_usbdev *bus_pub = NULL;
  1023. int ret;
  1024. struct device *dev = devinfo->dev;
  1025. brcmf_dbg(USB, "Enter\n");
  1026. bus_pub = brcmf_usb_attach(devinfo, BRCMF_USB_NRXQ, BRCMF_USB_NTXQ);
  1027. if (!bus_pub)
  1028. return -ENODEV;
  1029. bus = kzalloc(sizeof(struct brcmf_bus), GFP_ATOMIC);
  1030. if (!bus) {
  1031. ret = -ENOMEM;
  1032. goto fail;
  1033. }
  1034. bus->dev = dev;
  1035. bus_pub->bus = bus;
  1036. bus->bus_priv.usb = bus_pub;
  1037. dev_set_drvdata(dev, bus);
  1038. bus->ops = &brcmf_usb_bus_ops;
  1039. bus->chip = bus_pub->devid;
  1040. bus->chiprev = bus_pub->chiprev;
  1041. /* Attach to the common driver interface */
  1042. ret = brcmf_attach(0, dev);
  1043. if (ret) {
  1044. brcmf_err("brcmf_attach failed\n");
  1045. goto fail;
  1046. }
  1047. ret = brcmf_bus_start(dev);
  1048. if (ret) {
  1049. brcmf_err("dongle is not responding\n");
  1050. brcmf_detach(dev);
  1051. goto fail;
  1052. }
  1053. return 0;
  1054. fail:
  1055. /* Release resources in reverse order */
  1056. kfree(bus);
  1057. brcmf_usb_detach(devinfo);
  1058. return ret;
  1059. }
  1060. static void
  1061. brcmf_usb_disconnect_cb(struct brcmf_usbdev_info *devinfo)
  1062. {
  1063. if (!devinfo)
  1064. return;
  1065. brcmf_dbg(USB, "Enter, bus_pub %p\n", devinfo);
  1066. brcmf_detach(devinfo->dev);
  1067. kfree(devinfo->bus_pub.bus);
  1068. brcmf_usb_detach(devinfo);
  1069. }
  1070. static int
  1071. brcmf_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1072. {
  1073. int ep;
  1074. struct usb_endpoint_descriptor *endpoint;
  1075. int ret = 0;
  1076. struct usb_device *usb = interface_to_usbdev(intf);
  1077. int num_of_eps;
  1078. u8 endpoint_num;
  1079. struct brcmf_usbdev_info *devinfo;
  1080. brcmf_dbg(USB, "Enter\n");
  1081. devinfo = kzalloc(sizeof(*devinfo), GFP_ATOMIC);
  1082. if (devinfo == NULL)
  1083. return -ENOMEM;
  1084. devinfo->usbdev = usb;
  1085. devinfo->dev = &usb->dev;
  1086. usb_set_intfdata(intf, devinfo);
  1087. /* Check that the device supports only one configuration */
  1088. if (usb->descriptor.bNumConfigurations != 1) {
  1089. ret = -1;
  1090. goto fail;
  1091. }
  1092. if (usb->descriptor.bDeviceClass != USB_CLASS_VENDOR_SPEC) {
  1093. ret = -1;
  1094. goto fail;
  1095. }
  1096. /*
  1097. * Only the BDC interface configuration is supported:
  1098. * Device class: USB_CLASS_VENDOR_SPEC
  1099. * if0 class: USB_CLASS_VENDOR_SPEC
  1100. * if0/ep0: control
  1101. * if0/ep1: bulk in
  1102. * if0/ep2: bulk out (ok if swapped with bulk in)
  1103. */
  1104. if (CONFIGDESC(usb)->bNumInterfaces != 1) {
  1105. ret = -1;
  1106. goto fail;
  1107. }
  1108. /* Check interface */
  1109. if (IFDESC(usb, CONTROL_IF).bInterfaceClass != USB_CLASS_VENDOR_SPEC ||
  1110. IFDESC(usb, CONTROL_IF).bInterfaceSubClass != 2 ||
  1111. IFDESC(usb, CONTROL_IF).bInterfaceProtocol != 0xff) {
  1112. brcmf_err("invalid control interface: class %d, subclass %d, proto %d\n",
  1113. IFDESC(usb, CONTROL_IF).bInterfaceClass,
  1114. IFDESC(usb, CONTROL_IF).bInterfaceSubClass,
  1115. IFDESC(usb, CONTROL_IF).bInterfaceProtocol);
  1116. ret = -1;
  1117. goto fail;
  1118. }
  1119. /* Check control endpoint */
  1120. endpoint = &IFEPDESC(usb, CONTROL_IF, 0);
  1121. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
  1122. != USB_ENDPOINT_XFER_INT) {
  1123. brcmf_err("invalid control endpoint %d\n",
  1124. endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
  1125. ret = -1;
  1126. goto fail;
  1127. }
  1128. endpoint_num = endpoint->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
  1129. devinfo->intr_pipe = usb_rcvintpipe(usb, endpoint_num);
  1130. devinfo->rx_pipe = 0;
  1131. devinfo->rx_pipe2 = 0;
  1132. devinfo->tx_pipe = 0;
  1133. num_of_eps = IFDESC(usb, BULK_IF).bNumEndpoints - 1;
  1134. /* Check data endpoints and get pipes */
  1135. for (ep = 1; ep <= num_of_eps; ep++) {
  1136. endpoint = &IFEPDESC(usb, BULK_IF, ep);
  1137. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1138. USB_ENDPOINT_XFER_BULK) {
  1139. brcmf_err("invalid data endpoint %d\n", ep);
  1140. ret = -1;
  1141. goto fail;
  1142. }
  1143. endpoint_num = endpoint->bEndpointAddress &
  1144. USB_ENDPOINT_NUMBER_MASK;
  1145. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
  1146. == USB_DIR_IN) {
  1147. if (!devinfo->rx_pipe) {
  1148. devinfo->rx_pipe =
  1149. usb_rcvbulkpipe(usb, endpoint_num);
  1150. } else {
  1151. devinfo->rx_pipe2 =
  1152. usb_rcvbulkpipe(usb, endpoint_num);
  1153. }
  1154. } else {
  1155. devinfo->tx_pipe = usb_sndbulkpipe(usb, endpoint_num);
  1156. }
  1157. }
  1158. /* Allocate interrupt URB and data buffer */
  1159. /* RNDIS says 8-byte intr, our old drivers used 4-byte */
  1160. if (IFEPDESC(usb, CONTROL_IF, 0).wMaxPacketSize == cpu_to_le16(16))
  1161. devinfo->intr_size = 8;
  1162. else
  1163. devinfo->intr_size = 4;
  1164. devinfo->interval = IFEPDESC(usb, CONTROL_IF, 0).bInterval;
  1165. if (usb->speed == USB_SPEED_HIGH)
  1166. brcmf_dbg(USB, "Broadcom high speed USB wireless device detected\n");
  1167. else
  1168. brcmf_dbg(USB, "Broadcom full speed USB wireless device detected\n");
  1169. ret = brcmf_usb_probe_cb(devinfo);
  1170. if (ret)
  1171. goto fail;
  1172. /* Success */
  1173. return 0;
  1174. fail:
  1175. brcmf_err("failed with errno %d\n", ret);
  1176. kfree(devinfo);
  1177. usb_set_intfdata(intf, NULL);
  1178. return ret;
  1179. }
  1180. static void
  1181. brcmf_usb_disconnect(struct usb_interface *intf)
  1182. {
  1183. struct brcmf_usbdev_info *devinfo;
  1184. brcmf_dbg(USB, "Enter\n");
  1185. devinfo = (struct brcmf_usbdev_info *)usb_get_intfdata(intf);
  1186. brcmf_usb_disconnect_cb(devinfo);
  1187. kfree(devinfo);
  1188. brcmf_dbg(USB, "Exit\n");
  1189. }
  1190. /*
  1191. * only need to signal the bus being down and update the state.
  1192. */
  1193. static int brcmf_usb_suspend(struct usb_interface *intf, pm_message_t state)
  1194. {
  1195. struct usb_device *usb = interface_to_usbdev(intf);
  1196. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1197. brcmf_dbg(USB, "Enter\n");
  1198. devinfo->bus_pub.state = BRCMFMAC_USB_STATE_SLEEP;
  1199. brcmf_detach(&usb->dev);
  1200. return 0;
  1201. }
  1202. /*
  1203. * (re-) start the bus.
  1204. */
  1205. static int brcmf_usb_resume(struct usb_interface *intf)
  1206. {
  1207. struct usb_device *usb = interface_to_usbdev(intf);
  1208. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1209. brcmf_dbg(USB, "Enter\n");
  1210. if (!brcmf_attach(0, devinfo->dev))
  1211. return brcmf_bus_start(&usb->dev);
  1212. return 0;
  1213. }
  1214. static int brcmf_usb_reset_resume(struct usb_interface *intf)
  1215. {
  1216. struct usb_device *usb = interface_to_usbdev(intf);
  1217. struct brcmf_usbdev_info *devinfo = brcmf_usb_get_businfo(&usb->dev);
  1218. brcmf_dbg(USB, "Enter\n");
  1219. if (!brcmf_usb_fw_download(devinfo))
  1220. return brcmf_usb_resume(intf);
  1221. return -EIO;
  1222. }
  1223. #define BRCMF_USB_VENDOR_ID_BROADCOM 0x0a5c
  1224. #define BRCMF_USB_DEVICE_ID_43143 0xbd1e
  1225. #define BRCMF_USB_DEVICE_ID_43236 0xbd17
  1226. #define BRCMF_USB_DEVICE_ID_43242 0xbd1f
  1227. #define BRCMF_USB_DEVICE_ID_BCMFW 0x0bdc
  1228. static struct usb_device_id brcmf_usb_devid_table[] = {
  1229. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43143) },
  1230. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43236) },
  1231. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_43242) },
  1232. /* special entry for device with firmware loaded and running */
  1233. { USB_DEVICE(BRCMF_USB_VENDOR_ID_BROADCOM, BRCMF_USB_DEVICE_ID_BCMFW) },
  1234. { }
  1235. };
  1236. MODULE_DEVICE_TABLE(usb, brcmf_usb_devid_table);
  1237. MODULE_FIRMWARE(BRCMF_USB_43143_FW_NAME);
  1238. MODULE_FIRMWARE(BRCMF_USB_43236_FW_NAME);
  1239. MODULE_FIRMWARE(BRCMF_USB_43242_FW_NAME);
  1240. static struct usb_driver brcmf_usbdrvr = {
  1241. .name = KBUILD_MODNAME,
  1242. .probe = brcmf_usb_probe,
  1243. .disconnect = brcmf_usb_disconnect,
  1244. .id_table = brcmf_usb_devid_table,
  1245. .suspend = brcmf_usb_suspend,
  1246. .resume = brcmf_usb_resume,
  1247. .reset_resume = brcmf_usb_reset_resume,
  1248. .supports_autosuspend = 1,
  1249. .disable_hub_initiated_lpm = 1,
  1250. };
  1251. static void brcmf_release_fw(struct list_head *q)
  1252. {
  1253. struct brcmf_usb_image *fw_image, *next;
  1254. list_for_each_entry_safe(fw_image, next, q, list) {
  1255. vfree(fw_image->image);
  1256. list_del_init(&fw_image->list);
  1257. }
  1258. }
  1259. static int brcmf_usb_reset_device(struct device *dev, void *notused)
  1260. {
  1261. /* device past is the usb interface so we
  1262. * need to use parent here.
  1263. */
  1264. brcmf_dev_reset(dev->parent);
  1265. return 0;
  1266. }
  1267. void brcmf_usb_exit(void)
  1268. {
  1269. struct device_driver *drv = &brcmf_usbdrvr.drvwrap.driver;
  1270. int ret;
  1271. brcmf_dbg(USB, "Enter\n");
  1272. ret = driver_for_each_device(drv, NULL, NULL,
  1273. brcmf_usb_reset_device);
  1274. usb_deregister(&brcmf_usbdrvr);
  1275. brcmf_release_fw(&fw_image_list);
  1276. }
  1277. void brcmf_usb_init(void)
  1278. {
  1279. brcmf_dbg(USB, "Enter\n");
  1280. INIT_LIST_HEAD(&fw_image_list);
  1281. usb_register(&brcmf_usbdrvr);
  1282. }