usb.c 39 KB

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