Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 1 | /* Copyright (c) 2012-2013, The Linux Foundation. All rights reserved. |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #ifdef pr_fmt |
| 14 | #undef pr_fmt |
| 15 | #endif |
| 16 | #define pr_fmt(fmt) "%s: " fmt, __func__ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/device.h> |
| 21 | #include <linux/bitops.h> |
| 22 | #include <linux/usb/gadget.h> |
| 23 | |
| 24 | #include <mach/bam_dmux.h> |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 25 | #include <mach/usb_bam.h> |
| 26 | |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 27 | #include "u_bam_data.h" |
| 28 | |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 29 | #define BAM2BAM_DATA_N_PORTS 1 |
| 30 | |
| 31 | static struct workqueue_struct *bam_data_wq; |
| 32 | static int n_bam2bam_data_ports; |
| 33 | |
| 34 | #define SPS_PARAMS_SPS_MODE BIT(5) |
| 35 | #define SPS_PARAMS_TBE BIT(6) |
| 36 | #define MSM_VENDOR_ID BIT(16) |
| 37 | |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 38 | struct bam_data_ch_info { |
| 39 | unsigned long flags; |
| 40 | unsigned id; |
| 41 | |
| 42 | struct bam_data_port *port; |
| 43 | struct work_struct write_tobam_w; |
| 44 | |
| 45 | struct usb_request *rx_req; |
| 46 | struct usb_request *tx_req; |
| 47 | |
Shimrit Malichi | 255b534 | 2012-08-02 21:01:43 +0300 | [diff] [blame] | 48 | u32 src_pipe_idx; |
| 49 | u32 dst_pipe_idx; |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 50 | u8 src_connection_idx; |
| 51 | u8 dst_connection_idx; |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 52 | |
| 53 | enum function_type func_type; |
| 54 | enum transport_type trans; |
| 55 | struct usb_bam_connect_ipa_params ipa_params; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | struct bam_data_port { |
| 59 | unsigned port_num; |
| 60 | struct data_port *port_usb; |
| 61 | struct bam_data_ch_info data_ch; |
| 62 | |
| 63 | struct work_struct connect_w; |
| 64 | struct work_struct disconnect_w; |
Bar Weiner | 84e1ba0 | 2013-07-18 09:08:03 +0300 | [diff] [blame] | 65 | struct work_struct suspend_w; |
| 66 | struct work_struct resume_w; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | struct bam_data_port *bam2bam_data_ports[BAM2BAM_DATA_N_PORTS]; |
| 70 | |
Bar Weiner | 84e1ba0 | 2013-07-18 09:08:03 +0300 | [diff] [blame] | 71 | static void bam2bam_data_suspend_work(struct work_struct *w); |
| 72 | static void bam2bam_data_resume_work(struct work_struct *w); |
| 73 | |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 74 | /*------------data_path----------------------------*/ |
| 75 | |
| 76 | static void bam_data_endless_rx_complete(struct usb_ep *ep, |
| 77 | struct usb_request *req) |
| 78 | { |
| 79 | int status = req->status; |
| 80 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 81 | pr_debug("%s: status: %d\n", __func__, status); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void bam_data_endless_tx_complete(struct usb_ep *ep, |
| 85 | struct usb_request *req) |
| 86 | { |
| 87 | int status = req->status; |
| 88 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 89 | pr_debug("%s: status: %d\n", __func__, status); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static void bam_data_start_endless_rx(struct bam_data_port *port) |
| 93 | { |
| 94 | struct bam_data_ch_info *d = &port->data_ch; |
| 95 | int status; |
| 96 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 97 | if (!port->port_usb) |
| 98 | return; |
| 99 | |
Lena Salman | 05b544f | 2013-05-13 15:49:10 +0300 | [diff] [blame] | 100 | pr_debug("%s: enqueue\n", __func__); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 101 | status = usb_ep_queue(port->port_usb->out, d->rx_req, GFP_ATOMIC); |
| 102 | if (status) |
| 103 | pr_err("error enqueuing transfer, %d\n", status); |
| 104 | } |
| 105 | |
| 106 | static void bam_data_start_endless_tx(struct bam_data_port *port) |
| 107 | { |
| 108 | struct bam_data_ch_info *d = &port->data_ch; |
| 109 | int status; |
| 110 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 111 | if (!port->port_usb) |
| 112 | return; |
| 113 | |
Lena Salman | 05b544f | 2013-05-13 15:49:10 +0300 | [diff] [blame] | 114 | pr_debug("%s: enqueue\n", __func__); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 115 | status = usb_ep_queue(port->port_usb->in, d->tx_req, GFP_ATOMIC); |
| 116 | if (status) |
| 117 | pr_err("error enqueuing transfer, %d\n", status); |
| 118 | } |
| 119 | |
Lena Salman | 05b544f | 2013-05-13 15:49:10 +0300 | [diff] [blame] | 120 | static void bam_data_stop_endless_rx(struct bam_data_port *port) |
| 121 | { |
| 122 | struct bam_data_ch_info *d = &port->data_ch; |
| 123 | int status; |
| 124 | |
| 125 | if (!port->port_usb) |
| 126 | return; |
| 127 | |
| 128 | pr_debug("%s: dequeue\n", __func__); |
| 129 | status = usb_ep_dequeue(port->port_usb->out, d->rx_req); |
| 130 | if (status) |
| 131 | pr_err("%s: error dequeuing transfer, %d\n", __func__, status); |
| 132 | |
| 133 | } |
| 134 | static void bam_data_stop_endless_tx(struct bam_data_port *port) |
| 135 | { |
| 136 | struct bam_data_ch_info *d = &port->data_ch; |
| 137 | int status; |
| 138 | |
| 139 | if (!port->port_usb) |
| 140 | return; |
| 141 | |
| 142 | pr_debug("%s: dequeue\n", __func__); |
| 143 | status = usb_ep_dequeue(port->port_usb->in, d->tx_req); |
| 144 | if (status) |
| 145 | pr_err("%s: error dequeuing transfer, %d\n", __func__, status); |
| 146 | } |
| 147 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 148 | static int bam_data_peer_reset_cb(void *param) |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 149 | { |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 150 | struct bam_data_port *port = (struct bam_data_port *)param; |
| 151 | struct bam_data_ch_info *d; |
| 152 | int ret; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 153 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 154 | d = &port->data_ch; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 155 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 156 | pr_debug("%s: reset by peer\n", __func__); |
| 157 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 158 | /* Disable BAM */ |
| 159 | msm_hw_bam_disable(1); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 160 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 161 | /* Reset BAM */ |
Bar Weiner | 189bb3c | 2013-06-09 14:24:56 +0300 | [diff] [blame] | 162 | ret = usb_bam_a2_reset(0); |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 163 | if (ret) { |
| 164 | pr_err("%s: BAM reset failed %d\n", __func__, ret); |
Bar Weiner | 189bb3c | 2013-06-09 14:24:56 +0300 | [diff] [blame] | 165 | return ret; |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 166 | } |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 167 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 168 | /* Enable BAM */ |
| 169 | msm_hw_bam_disable(0); |
| 170 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 171 | /* Unregister the peer reset callback */ |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 172 | usb_bam_register_peer_reset_cb(NULL, NULL); |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 173 | |
| 174 | return 0; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 175 | } |
| 176 | |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 177 | static void bam2bam_data_disconnect_work(struct work_struct *w) |
| 178 | { |
| 179 | struct bam_data_port *port = |
| 180 | container_of(w, struct bam_data_port, disconnect_w); |
| 181 | struct bam_data_ch_info *d = &port->data_ch; |
| 182 | int ret; |
| 183 | |
| 184 | if (d->trans == USB_GADGET_XPORT_BAM2BAM_IPA) { |
| 185 | if (d->func_type == USB_FUNC_ECM) |
| 186 | ecm_ipa_disconnect(d->ipa_params.priv); |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 187 | ret = usb_bam_disconnect_ipa(&d->ipa_params); |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 188 | if (ret) |
| 189 | pr_err("usb_bam_disconnect_ipa failed: err:%d\n", ret); |
Lena Salman | 7c0e979 | 2013-06-20 14:36:54 +0300 | [diff] [blame] | 190 | if (d->func_type == USB_FUNC_MBIM) |
| 191 | teth_bridge_disconnect(); |
| 192 | |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 196 | static void bam2bam_data_connect_work(struct work_struct *w) |
| 197 | { |
| 198 | struct bam_data_port *port = container_of(w, struct bam_data_port, |
| 199 | connect_w); |
Lena Salman | df7e799 | 2013-03-15 09:46:27 +0200 | [diff] [blame] | 200 | struct teth_bridge_connect_params connect_params; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 201 | struct bam_data_ch_info *d = &port->data_ch; |
Lena Salman | df7e799 | 2013-03-15 09:46:27 +0200 | [diff] [blame] | 202 | ipa_notify_cb usb_notify_cb; |
| 203 | void *priv; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 204 | u32 sps_params; |
| 205 | int ret; |
| 206 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 207 | pr_debug("%s: Connect workqueue started", __func__); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 208 | |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 209 | if (d->trans == USB_GADGET_XPORT_BAM2BAM_IPA) { |
Lena Salman | df7e799 | 2013-03-15 09:46:27 +0200 | [diff] [blame] | 210 | if (d->func_type == USB_FUNC_MBIM) { |
| 211 | ret = teth_bridge_init(&usb_notify_cb, &priv); |
| 212 | if (ret) { |
| 213 | pr_err("%s:teth_bridge_init() failed\n", |
| 214 | __func__); |
| 215 | return; |
| 216 | } |
| 217 | d->ipa_params.notify = usb_notify_cb; |
| 218 | d->ipa_params.priv = priv; |
| 219 | d->ipa_params.ipa_ep_cfg.mode.mode = IPA_BASIC; |
| 220 | } |
| 221 | |
Lena Salman | abde35d | 2013-04-25 15:29:43 +0300 | [diff] [blame] | 222 | d->ipa_params.client = IPA_CLIENT_USB_PROD; |
| 223 | d->ipa_params.dir = USB_TO_PEER_PERIPHERAL; |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 224 | if (d->func_type == USB_FUNC_ECM) { |
Lena Salman | abde35d | 2013-04-25 15:29:43 +0300 | [diff] [blame] | 225 | d->ipa_params.notify = ecm_qc_get_ipa_rx_cb(); |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 226 | d->ipa_params.priv = ecm_qc_get_ipa_priv(); |
| 227 | } |
| 228 | ret = usb_bam_connect_ipa(&d->ipa_params); |
| 229 | if (ret) { |
| 230 | pr_err("%s: usb_bam_connect_ipa failed: err:%d\n", |
| 231 | __func__, ret); |
| 232 | return; |
| 233 | } |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 234 | |
Lena Salman | abde35d | 2013-04-25 15:29:43 +0300 | [diff] [blame] | 235 | d->ipa_params.client = IPA_CLIENT_USB_CONS; |
| 236 | d->ipa_params.dir = PEER_PERIPHERAL_TO_USB; |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 237 | if (d->func_type == USB_FUNC_ECM) { |
Lena Salman | abde35d | 2013-04-25 15:29:43 +0300 | [diff] [blame] | 238 | d->ipa_params.notify = ecm_qc_get_ipa_tx_cb(); |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 239 | d->ipa_params.priv = ecm_qc_get_ipa_priv(); |
| 240 | } |
| 241 | ret = usb_bam_connect_ipa(&d->ipa_params); |
| 242 | if (ret) { |
| 243 | pr_err("%s: usb_bam_connect_ipa failed: err:%d\n", |
| 244 | __func__, ret); |
| 245 | return; |
| 246 | } |
Lena Salman | df7e799 | 2013-03-15 09:46:27 +0200 | [diff] [blame] | 247 | |
| 248 | if (d->func_type == USB_FUNC_MBIM) { |
| 249 | connect_params.ipa_usb_pipe_hdl = |
| 250 | d->ipa_params.prod_clnt_hdl; |
| 251 | connect_params.usb_ipa_pipe_hdl = |
| 252 | d->ipa_params.cons_clnt_hdl; |
| 253 | connect_params.tethering_mode = |
| 254 | TETH_TETHERING_MODE_MBIM; |
| 255 | ret = teth_bridge_connect(&connect_params); |
| 256 | if (ret) { |
| 257 | pr_err("%s:teth_bridge_connect() failed\n", |
| 258 | __func__); |
| 259 | return; |
| 260 | } |
| 261 | mbim_configure_params(); |
| 262 | } |
| 263 | |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 264 | if (d->func_type == USB_FUNC_ECM) { |
| 265 | ret = ecm_ipa_connect(d->ipa_params.cons_clnt_hdl, |
| 266 | d->ipa_params.prod_clnt_hdl, |
| 267 | d->ipa_params.priv); |
| 268 | if (ret) { |
| 269 | pr_err("%s: failed to connect IPA: err:%d\n", |
| 270 | __func__, ret); |
| 271 | return; |
| 272 | } |
| 273 | } |
| 274 | } else { /* transport type is USB_GADGET_XPORT_BAM2BAM */ |
Bar Weiner | d94bcfa | 2013-06-25 03:37:24 +0300 | [diff] [blame] | 275 | usb_bam_reset_complete(); |
| 276 | ret = usb_bam_connect(d->src_connection_idx, &d->src_pipe_idx); |
| 277 | if (ret) { |
| 278 | pr_err("usb_bam_connect (src) failed: err:%d\n", ret); |
| 279 | return; |
| 280 | } |
| 281 | ret = usb_bam_connect(d->dst_connection_idx, &d->dst_pipe_idx); |
| 282 | if (ret) { |
| 283 | pr_err("usb_bam_connect (dst) failed: err:%d\n", ret); |
| 284 | return; |
| 285 | } |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | if (!port->port_usb) { |
| 289 | pr_err("port_usb is NULL"); |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | if (!port->port_usb->out) { |
| 294 | pr_err("port_usb->out (bulk out ep) is NULL"); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | d->rx_req = usb_ep_alloc_request(port->port_usb->out, GFP_KERNEL); |
| 299 | if (!d->rx_req) |
| 300 | return; |
| 301 | |
| 302 | d->rx_req->context = port; |
| 303 | d->rx_req->complete = bam_data_endless_rx_complete; |
| 304 | d->rx_req->length = 0; |
Bar Weiner | 8475d63 | 2013-06-13 11:29:49 +0300 | [diff] [blame] | 305 | d->rx_req->no_interrupt = 1; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 306 | sps_params = (SPS_PARAMS_SPS_MODE | d->src_pipe_idx | |
| 307 | MSM_VENDOR_ID) & ~SPS_PARAMS_TBE; |
| 308 | d->rx_req->udc_priv = sps_params; |
| 309 | d->tx_req = usb_ep_alloc_request(port->port_usb->in, GFP_KERNEL); |
| 310 | if (!d->tx_req) |
| 311 | return; |
| 312 | |
| 313 | d->tx_req->context = port; |
| 314 | d->tx_req->complete = bam_data_endless_tx_complete; |
| 315 | d->tx_req->length = 0; |
Bar Weiner | 8475d63 | 2013-06-13 11:29:49 +0300 | [diff] [blame] | 316 | d->tx_req->no_interrupt = 1; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 317 | sps_params = (SPS_PARAMS_SPS_MODE | d->dst_pipe_idx | |
| 318 | MSM_VENDOR_ID) & ~SPS_PARAMS_TBE; |
| 319 | d->tx_req->udc_priv = sps_params; |
| 320 | |
| 321 | /* queue in & out requests */ |
| 322 | bam_data_start_endless_rx(port); |
| 323 | bam_data_start_endless_tx(port); |
| 324 | |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 325 | /* Register for peer reset callback if USB_GADGET_XPORT_BAM2BAM */ |
| 326 | if (d->trans != USB_GADGET_XPORT_BAM2BAM_IPA) { |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 327 | usb_bam_register_peer_reset_cb(bam_data_peer_reset_cb, port); |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 328 | |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 329 | ret = usb_bam_client_ready(true); |
| 330 | if (ret) { |
| 331 | pr_err("%s: usb_bam_client_ready failed: err:%d\n", |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 332 | __func__, ret); |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 333 | return; |
| 334 | } |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | pr_debug("%s: Connect workqueue done", __func__); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | static void bam2bam_data_port_free(int portno) |
| 341 | { |
| 342 | kfree(bam2bam_data_ports[portno]); |
| 343 | bam2bam_data_ports[portno] = NULL; |
| 344 | } |
| 345 | |
| 346 | static int bam2bam_data_port_alloc(int portno) |
| 347 | { |
| 348 | struct bam_data_port *port = NULL; |
| 349 | struct bam_data_ch_info *d = NULL; |
| 350 | |
| 351 | port = kzalloc(sizeof(struct bam_data_port), GFP_KERNEL); |
| 352 | if (!port) |
| 353 | return -ENOMEM; |
| 354 | |
| 355 | port->port_num = portno; |
| 356 | |
| 357 | INIT_WORK(&port->connect_w, bam2bam_data_connect_work); |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 358 | INIT_WORK(&port->disconnect_w, bam2bam_data_disconnect_work); |
Bar Weiner | 84e1ba0 | 2013-07-18 09:08:03 +0300 | [diff] [blame] | 359 | INIT_WORK(&port->suspend_w, bam2bam_data_suspend_work); |
| 360 | INIT_WORK(&port->resume_w, bam2bam_data_resume_work); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 361 | |
| 362 | /* data ch */ |
| 363 | d = &port->data_ch; |
| 364 | d->port = port; |
| 365 | bam2bam_data_ports[portno] = port; |
| 366 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 367 | pr_debug("port:%p portno:%d\n", port, portno); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 368 | |
| 369 | return 0; |
| 370 | } |
| 371 | |
| 372 | void bam_data_disconnect(struct data_port *gr, u8 port_num) |
| 373 | { |
| 374 | struct bam_data_port *port; |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 375 | struct bam_data_ch_info *d; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 376 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 377 | pr_debug("dev:%p port#%d\n", gr, port_num); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 378 | |
| 379 | if (port_num >= n_bam2bam_data_ports) { |
| 380 | pr_err("invalid bam2bam portno#%d\n", port_num); |
| 381 | return; |
| 382 | } |
| 383 | |
| 384 | if (!gr) { |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 385 | pr_err("data port is null\n"); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 386 | return; |
| 387 | } |
| 388 | |
| 389 | port = bam2bam_data_ports[port_num]; |
| 390 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 391 | if (port->port_usb && port->port_usb->in && |
| 392 | port->port_usb->in->driver_data) { |
| 393 | /* disable endpoints */ |
| 394 | usb_ep_disable(port->port_usb->out); |
| 395 | usb_ep_disable(port->port_usb->in); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 396 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 397 | port->port_usb->in->driver_data = NULL; |
| 398 | port->port_usb->out->driver_data = NULL; |
| 399 | |
| 400 | port->port_usb = 0; |
| 401 | } |
| 402 | |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 403 | d = &port->data_ch; |
| 404 | if (d->trans == USB_GADGET_XPORT_BAM2BAM_IPA) |
Lena Salman | 27b085c | 2013-03-13 08:38:25 +0200 | [diff] [blame] | 405 | queue_work(bam_data_wq, &port->disconnect_w); |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 406 | else { |
| 407 | if (usb_bam_client_ready(false)) { |
| 408 | pr_err("%s: usb_bam_client_ready failed\n", |
| 409 | __func__); |
| 410 | } |
| 411 | } |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | int bam_data_connect(struct data_port *gr, u8 port_num, |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 415 | enum transport_type trans, u8 src_connection_idx, |
| 416 | u8 dst_connection_idx, enum function_type func) |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 417 | { |
| 418 | struct bam_data_port *port; |
| 419 | struct bam_data_ch_info *d; |
| 420 | int ret; |
| 421 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 422 | pr_debug("dev:%p port#%d\n", gr, port_num); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 423 | |
| 424 | if (port_num >= n_bam2bam_data_ports) { |
| 425 | pr_err("invalid portno#%d\n", port_num); |
| 426 | return -ENODEV; |
| 427 | } |
| 428 | |
| 429 | if (!gr) { |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 430 | pr_err("data port is null\n"); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 431 | return -ENODEV; |
| 432 | } |
| 433 | |
| 434 | port = bam2bam_data_ports[port_num]; |
| 435 | |
| 436 | d = &port->data_ch; |
| 437 | |
| 438 | ret = usb_ep_enable(gr->in); |
| 439 | if (ret) { |
| 440 | pr_err("usb_ep_enable failed eptype:IN ep:%p", gr->in); |
| 441 | return ret; |
| 442 | } |
| 443 | gr->in->driver_data = port; |
| 444 | |
| 445 | ret = usb_ep_enable(gr->out); |
| 446 | if (ret) { |
| 447 | pr_err("usb_ep_enable failed eptype:OUT ep:%p", gr->out); |
| 448 | gr->in->driver_data = 0; |
| 449 | return ret; |
| 450 | } |
| 451 | gr->out->driver_data = port; |
| 452 | |
| 453 | port->port_usb = gr; |
| 454 | |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 455 | d->src_connection_idx = src_connection_idx; |
| 456 | d->dst_connection_idx = dst_connection_idx; |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 457 | |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 458 | d->trans = trans; |
Lena Salman | df7e799 | 2013-03-15 09:46:27 +0200 | [diff] [blame] | 459 | d->func_type = func; |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 460 | |
| 461 | if (trans == USB_GADGET_XPORT_BAM2BAM_IPA) { |
| 462 | d->ipa_params.src_pipe = &(d->src_pipe_idx); |
| 463 | d->ipa_params.dst_pipe = &(d->dst_pipe_idx); |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 464 | d->ipa_params.src_idx = src_connection_idx; |
| 465 | d->ipa_params.dst_idx = dst_connection_idx; |
Amit Blay | f9b352b | 2013-03-04 15:01:40 +0200 | [diff] [blame] | 466 | } |
| 467 | |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 468 | queue_work(bam_data_wq, &port->connect_w); |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
Bar Weiner | 189bb3c | 2013-06-09 14:24:56 +0300 | [diff] [blame] | 473 | int bam_data_destroy(unsigned int no_bam2bam_port) |
| 474 | { |
| 475 | struct bam_data_ch_info *d; |
| 476 | struct bam_data_port *port; |
| 477 | |
| 478 | port = bam2bam_data_ports[no_bam2bam_port]; |
| 479 | d = &port->data_ch; |
| 480 | |
| 481 | pr_debug("bam_data_destroy: Freeing ports\n"); |
| 482 | bam2bam_data_port_free(no_bam2bam_port); |
| 483 | if (bam_data_wq) |
| 484 | destroy_workqueue(bam_data_wq); |
| 485 | bam_data_wq = NULL; |
| 486 | |
| 487 | return 0; |
| 488 | } |
| 489 | |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 490 | int bam_data_setup(unsigned int no_bam2bam_port) |
| 491 | { |
| 492 | int i; |
| 493 | int ret; |
| 494 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 495 | pr_debug("requested %d BAM2BAM ports", no_bam2bam_port); |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 496 | |
| 497 | if (!no_bam2bam_port || no_bam2bam_port > BAM2BAM_DATA_N_PORTS) { |
| 498 | pr_err("Invalid num of ports count:%d\n", no_bam2bam_port); |
| 499 | return -EINVAL; |
| 500 | } |
| 501 | |
Bar Weiner | 189bb3c | 2013-06-09 14:24:56 +0300 | [diff] [blame] | 502 | if (bam_data_wq) { |
| 503 | pr_debug("bam_data is already setup"); |
| 504 | return 0; |
| 505 | } |
| 506 | |
Anna Perel | a8c991d | 2012-04-09 16:44:46 +0300 | [diff] [blame] | 507 | bam_data_wq = alloc_workqueue("k_bam_data", |
| 508 | WQ_UNBOUND | WQ_MEM_RECLAIM, 1); |
| 509 | if (!bam_data_wq) { |
| 510 | pr_err("Failed to create workqueue\n"); |
| 511 | return -ENOMEM; |
| 512 | } |
| 513 | |
| 514 | for (i = 0; i < no_bam2bam_port; i++) { |
| 515 | n_bam2bam_data_ports++; |
| 516 | ret = bam2bam_data_port_alloc(i); |
| 517 | if (ret) { |
| 518 | n_bam2bam_data_ports--; |
| 519 | pr_err("Failed to alloc port:%d\n", i); |
| 520 | goto free_bam_ports; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return 0; |
| 525 | |
| 526 | free_bam_ports: |
| 527 | for (i = 0; i < n_bam2bam_data_ports; i++) |
| 528 | bam2bam_data_port_free(i); |
| 529 | destroy_workqueue(bam_data_wq); |
| 530 | |
| 531 | return ret; |
| 532 | } |
| 533 | |
Anna Perel | 557bf72 | 2012-09-20 11:16:35 +0300 | [diff] [blame] | 534 | static int bam_data_wake_cb(void *param) |
| 535 | { |
| 536 | struct bam_data_port *port = (struct bam_data_port *)param; |
| 537 | struct data_port *d_port = port->port_usb; |
| 538 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 539 | pr_debug("%s: woken up by peer\n", __func__); |
Anna Perel | 557bf72 | 2012-09-20 11:16:35 +0300 | [diff] [blame] | 540 | |
| 541 | if (!d_port) { |
| 542 | pr_err("FAILED: d_port == NULL"); |
| 543 | return -ENODEV; |
| 544 | } |
| 545 | |
| 546 | if (!d_port->cdev) { |
| 547 | pr_err("FAILED: d_port->cdev == NULL"); |
| 548 | return -ENODEV; |
| 549 | } |
| 550 | |
| 551 | if (!d_port->cdev->gadget) { |
| 552 | pr_err("FAILED: d_port->cdev->gadget == NULL"); |
| 553 | return -ENODEV; |
| 554 | } |
| 555 | |
| 556 | return usb_gadget_wakeup(d_port->cdev->gadget); |
| 557 | } |
| 558 | |
Lena Salman | 05b544f | 2013-05-13 15:49:10 +0300 | [diff] [blame] | 559 | static void bam_data_start(void *param, enum usb_bam_pipe_dir dir) |
| 560 | { |
| 561 | struct bam_data_port *port = param; |
| 562 | |
| 563 | if (dir == USB_TO_PEER_PERIPHERAL) |
| 564 | bam_data_start_endless_rx(port); |
| 565 | else |
| 566 | bam_data_start_endless_tx(port); |
| 567 | } |
| 568 | |
| 569 | static void bam_data_stop(void *param, enum usb_bam_pipe_dir dir) |
| 570 | { |
| 571 | struct bam_data_port *port = param; |
| 572 | |
| 573 | if (dir == USB_TO_PEER_PERIPHERAL) |
| 574 | bam_data_stop_endless_rx(port); |
| 575 | else |
| 576 | bam_data_stop_endless_tx(port); |
| 577 | } |
| 578 | |
Anna Perel | 557bf72 | 2012-09-20 11:16:35 +0300 | [diff] [blame] | 579 | void bam_data_suspend(u8 port_num) |
| 580 | { |
Anna Perel | 557bf72 | 2012-09-20 11:16:35 +0300 | [diff] [blame] | 581 | struct bam_data_port *port; |
| 582 | struct bam_data_ch_info *d; |
| 583 | |
| 584 | port = bam2bam_data_ports[port_num]; |
| 585 | d = &port->data_ch; |
| 586 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 587 | pr_debug("%s: suspended port %d\n", __func__, port_num); |
Bar Weiner | 84e1ba0 | 2013-07-18 09:08:03 +0300 | [diff] [blame] | 588 | |
| 589 | queue_work(bam_data_wq, &port->suspend_w); |
Anna Perel | 557bf72 | 2012-09-20 11:16:35 +0300 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void bam_data_resume(u8 port_num) |
| 593 | { |
| 594 | |
| 595 | struct bam_data_port *port; |
| 596 | struct bam_data_ch_info *d; |
| 597 | |
| 598 | port = bam2bam_data_ports[port_num]; |
| 599 | d = &port->data_ch; |
| 600 | |
Amit Blay | 51bebe9 | 2012-12-25 18:48:10 +0200 | [diff] [blame] | 601 | pr_debug("%s: resumed port %d\n", __func__, port_num); |
Bar Weiner | 84e1ba0 | 2013-07-18 09:08:03 +0300 | [diff] [blame] | 602 | |
| 603 | queue_work(bam_data_wq, &port->resume_w); |
| 604 | } |
| 605 | |
| 606 | static void bam2bam_data_suspend_work(struct work_struct *w) |
| 607 | { |
| 608 | struct bam_data_port *port = |
| 609 | container_of(w, struct bam_data_port, suspend_w); |
| 610 | struct bam_data_ch_info *d = &port->data_ch; |
| 611 | |
| 612 | pr_debug("%s: suspend work started\n", __func__); |
| 613 | |
| 614 | usb_bam_register_wake_cb(d->dst_connection_idx, bam_data_wake_cb, port); |
| 615 | if (d->trans == USB_GADGET_XPORT_BAM2BAM_IPA) { |
| 616 | usb_bam_register_start_stop_cbs(bam_data_start, bam_data_stop, |
| 617 | port); |
| 618 | usb_bam_suspend(&d->ipa_params); |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | static void bam2bam_data_resume_work(struct work_struct *w) |
| 623 | { |
| 624 | struct bam_data_port *port = |
| 625 | container_of(w, struct bam_data_port, resume_w); |
| 626 | struct bam_data_ch_info *d = &port->data_ch; |
| 627 | |
| 628 | pr_debug("%s: resume work started\n", __func__); |
| 629 | |
Shimrit Malichi | dbf43d7 | 2013-03-16 03:32:27 +0200 | [diff] [blame] | 630 | usb_bam_register_wake_cb(d->dst_connection_idx, NULL, NULL); |
Lena Salman | 05b544f | 2013-05-13 15:49:10 +0300 | [diff] [blame] | 631 | if (d->trans == USB_GADGET_XPORT_BAM2BAM_IPA) |
| 632 | usb_bam_resume(&d->ipa_params); |
Anna Perel | 557bf72 | 2012-09-20 11:16:35 +0300 | [diff] [blame] | 633 | } |