usb.c 34 KB

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