usb.c 38 KB

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