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