usb.c 37 KB

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