Greg Kroah-Hartman | eb50fd3 | 2017-11-07 14:58:41 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Greybus "AP" USB driver for "ES2" controller chips |
| 4 | * |
Alex Elder | 142f8dd | 2015-03-26 21:25:06 -0500 | [diff] [blame] | 5 | * Copyright 2014-2015 Google Inc. |
| 6 | * Copyright 2014-2015 Linaro Ltd. |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 7 | */ |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 8 | #include <linux/kthread.h> |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 9 | #include <linux/sizes.h> |
| 10 | #include <linux/usb.h> |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 11 | #include <linux/kfifo.h> |
| 12 | #include <linux/debugfs.h> |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 13 | #include <linux/list.h> |
Greg Kroah-Hartman | ec0ad86 | 2019-08-25 07:54:27 +0200 | [diff] [blame] | 14 | #include <linux/greybus.h> |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 15 | #include <asm/unaligned.h> |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 16 | |
Johan Hovold | 7d0e76d | 2016-08-10 12:58:41 +0200 | [diff] [blame] | 17 | #include "arpc.h" |
Greg Kroah-Hartman | b81beec | 2019-08-25 07:54:29 +0200 | [diff] [blame] | 18 | #include "greybus_trace.h" |
Johan Hovold | 0506150 | 2016-05-11 10:18:01 +0200 | [diff] [blame] | 19 | |
Johan Hovold | 54f34e1 | 2016-08-03 14:09:34 +0200 | [diff] [blame] | 20 | |
Johan Hovold | 60793c9 | 2016-08-03 14:09:35 +0200 | [diff] [blame] | 21 | /* Default timeout for USB vendor requests. */ |
| 22 | #define ES2_USB_CTRL_TIMEOUT 500 |
| 23 | |
Johan Hovold | 54f34e1 | 2016-08-03 14:09:34 +0200 | [diff] [blame] | 24 | /* Default timeout for ARPC CPort requests */ |
| 25 | #define ES2_ARPC_CPORT_TIMEOUT 500 |
| 26 | |
Johan Hovold | 0506150 | 2016-05-11 10:18:01 +0200 | [diff] [blame] | 27 | /* Fixed CPort numbers */ |
| 28 | #define ES2_CPORT_CDSI0 16 |
| 29 | #define ES2_CPORT_CDSI1 17 |
| 30 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 31 | /* Memory sizes for the buffers sent to/from the ES2 controller */ |
| 32 | #define ES2_GBUF_MSG_SIZE_MAX 2048 |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 33 | |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 34 | /* Memory sizes for the ARPC buffers */ |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 35 | #define ARPC_OUT_SIZE_MAX U16_MAX |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 36 | #define ARPC_IN_SIZE_MAX 128 |
| 37 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 38 | static const struct usb_device_id id_table[] = { |
Greg Kroah-Hartman | 8f0a654 | 2015-10-22 16:40:41 -0700 | [diff] [blame] | 39 | { USB_DEVICE(0x18d1, 0x1eaf) }, |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 40 | { }, |
| 41 | }; |
| 42 | MODULE_DEVICE_TABLE(usb, id_table); |
| 43 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 44 | #define APB1_LOG_SIZE SZ_16K |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 45 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 46 | /* |
| 47 | * Number of CPort IN urbs in flight at any point in time. |
| 48 | * Adjust if we are having stalls in the USB buffer due to not enough urbs in |
| 49 | * flight. |
| 50 | */ |
| 51 | #define NUM_CPORT_IN_URB 4 |
| 52 | |
| 53 | /* Number of CPort OUT urbs in flight at any point in time. |
| 54 | * Adjust if we get messages saying we are out of urbs in the system log. |
| 55 | */ |
Greg Kroah-Hartman | fc994f0 | 2016-08-17 13:21:51 +0200 | [diff] [blame] | 56 | #define NUM_CPORT_OUT_URB 8 |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 57 | |
Alexandre Bailon | ddc09ac | 2015-06-15 18:08:12 +0200 | [diff] [blame] | 58 | /* |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 59 | * Number of ARPC in urbs in flight at any point in time. |
| 60 | */ |
| 61 | #define NUM_ARPC_IN_URB 2 |
| 62 | |
| 63 | /* |
Alexandre Bailon | ddc09ac | 2015-06-15 18:08:12 +0200 | [diff] [blame] | 64 | * @endpoint: bulk in endpoint for CPort data |
| 65 | * @urb: array of urbs for the CPort in messages |
| 66 | * @buffer: array of buffers for the @cport_in_urb urbs |
| 67 | */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 68 | struct es2_cport_in { |
Alexandre Bailon | ddc09ac | 2015-06-15 18:08:12 +0200 | [diff] [blame] | 69 | __u8 endpoint; |
| 70 | struct urb *urb[NUM_CPORT_IN_URB]; |
| 71 | u8 *buffer[NUM_CPORT_IN_URB]; |
| 72 | }; |
| 73 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 74 | /** |
Randy Dunlap | 39d2a78 | 2021-04-14 22:43:38 -0700 | [diff] [blame] | 75 | * struct es2_ap_dev - ES2 USB Bridge to AP structure |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 76 | * @usb_dev: pointer to the USB device we are. |
| 77 | * @usb_intf: pointer to the USB interface we are bound to. |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 78 | * @hd: pointer to our gb_host_device structure |
Randy Dunlap | 39d2a78 | 2021-04-14 22:43:38 -0700 | [diff] [blame] | 79 | * |
Alexandre Bailon | ddc09ac | 2015-06-15 18:08:12 +0200 | [diff] [blame] | 80 | * @cport_in: endpoint, urbs and buffer for cport in messages |
Greg Kroah-Hartman | 403074b | 2016-08-17 16:27:49 +0200 | [diff] [blame] | 81 | * @cport_out_endpoint: endpoint for for cport out messages |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 82 | * @cport_out_urb: array of urbs for the CPort out messages |
| 83 | * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or |
| 84 | * not. |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 85 | * @cport_out_urb_cancelled: array of flags indicating whether the |
| 86 | * corresponding @cport_out_urb is being cancelled |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 87 | * @cport_out_urb_lock: locks the @cport_out_urb_busy "list" |
Randy Dunlap | 39d2a78 | 2021-04-14 22:43:38 -0700 | [diff] [blame] | 88 | * @cdsi1_in_use: true if cport CDSI1 is in use |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 89 | * @apb_log_task: task pointer for logging thread |
| 90 | * @apb_log_dentry: file system entry for the log file interface |
| 91 | * @apb_log_enable_dentry: file system entry for enabling logging |
| 92 | * @apb_log_fifo: kernel FIFO to carry logged data |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 93 | * @arpc_urb: array of urbs for the ARPC in messages |
| 94 | * @arpc_buffer: array of buffers for the @arpc_urb urbs |
| 95 | * @arpc_endpoint_in: bulk in endpoint for APBridgeA RPC |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 96 | * @arpc_id_cycle: gives an unique id to ARPC |
| 97 | * @arpc_lock: locks ARPC list |
| 98 | * @arpcs: list of in progress ARPCs |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 99 | */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 100 | struct es2_ap_dev { |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 101 | struct usb_device *usb_dev; |
| 102 | struct usb_interface *usb_intf; |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 103 | struct gb_host_device *hd; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 104 | |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 105 | struct es2_cport_in cport_in; |
Greg Kroah-Hartman | 403074b | 2016-08-17 16:27:49 +0200 | [diff] [blame] | 106 | __u8 cport_out_endpoint; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 107 | struct urb *cport_out_urb[NUM_CPORT_OUT_URB]; |
| 108 | bool cport_out_urb_busy[NUM_CPORT_OUT_URB]; |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 109 | bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB]; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 110 | spinlock_t cport_out_urb_lock; |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 111 | |
Johan Hovold | 045d356 | 2016-05-11 10:18:04 +0200 | [diff] [blame] | 112 | bool cdsi1_in_use; |
| 113 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 114 | struct task_struct *apb_log_task; |
| 115 | struct dentry *apb_log_dentry; |
| 116 | struct dentry *apb_log_enable_dentry; |
| 117 | DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE); |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 118 | |
| 119 | __u8 arpc_endpoint_in; |
| 120 | struct urb *arpc_urb[NUM_ARPC_IN_URB]; |
| 121 | u8 *arpc_buffer[NUM_ARPC_IN_URB]; |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 122 | |
| 123 | int arpc_id_cycle; |
| 124 | spinlock_t arpc_lock; |
| 125 | struct list_head arpcs; |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 128 | struct arpc { |
| 129 | struct list_head list; |
| 130 | struct arpc_request_message *req; |
| 131 | struct arpc_response_message *resp; |
| 132 | struct completion response_received; |
| 133 | bool active; |
| 134 | }; |
| 135 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 136 | static inline struct es2_ap_dev *hd_to_es2(struct gb_host_device *hd) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 137 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 138 | return (struct es2_ap_dev *)&hd->hd_priv; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | static void cport_out_callback(struct urb *urb); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 142 | static void usb_log_enable(struct es2_ap_dev *es2); |
| 143 | static void usb_log_disable(struct es2_ap_dev *es2); |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 144 | static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload, |
| 145 | size_t size, int *result, unsigned int timeout); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 146 | |
Greg Kroah-Hartman | ed4596e9 | 2015-12-22 18:21:51 -0800 | [diff] [blame] | 147 | static int output_sync(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd) |
Laurent Pinchart | 8e2b7da | 2015-12-18 21:23:24 +0200 | [diff] [blame] | 148 | { |
Laurent Pinchart | 8e2b7da | 2015-12-18 21:23:24 +0200 | [diff] [blame] | 149 | struct usb_device *udev = es2->usb_dev; |
Greg Kroah-Hartman | ed4596e9 | 2015-12-22 18:21:51 -0800 | [diff] [blame] | 150 | u8 *data; |
Laurent Pinchart | 8e2b7da | 2015-12-18 21:23:24 +0200 | [diff] [blame] | 151 | int retval; |
| 152 | |
sayli karnik | 9edae49 | 2016-10-16 14:19:48 +0530 | [diff] [blame] | 153 | data = kmemdup(req, size, GFP_KERNEL); |
Greg Kroah-Hartman | ed4596e9 | 2015-12-22 18:21:51 -0800 | [diff] [blame] | 154 | if (!data) |
Laurent Pinchart | ccb5803 | 2015-12-22 03:00:37 +0200 | [diff] [blame] | 155 | return -ENOMEM; |
Laurent Pinchart | 8e2b7da | 2015-12-18 21:23:24 +0200 | [diff] [blame] | 156 | |
| 157 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
Greg Kroah-Hartman | ed4596e9 | 2015-12-22 18:21:51 -0800 | [diff] [blame] | 158 | cmd, |
Laurent Pinchart | 8e2b7da | 2015-12-18 21:23:24 +0200 | [diff] [blame] | 159 | USB_DIR_OUT | USB_TYPE_VENDOR | |
Greg Kroah-Hartman | ed4596e9 | 2015-12-22 18:21:51 -0800 | [diff] [blame] | 160 | USB_RECIP_INTERFACE, |
Johan Hovold | 60793c9 | 2016-08-03 14:09:35 +0200 | [diff] [blame] | 161 | 0, 0, data, size, ES2_USB_CTRL_TIMEOUT); |
Laurent Pinchart | ccb5803 | 2015-12-22 03:00:37 +0200 | [diff] [blame] | 162 | if (retval < 0) |
Greg Kroah-Hartman | ed4596e9 | 2015-12-22 18:21:51 -0800 | [diff] [blame] | 163 | dev_err(&udev->dev, "%s: return error %d\n", __func__, retval); |
| 164 | else |
| 165 | retval = 0; |
Laurent Pinchart | 8e2b7da | 2015-12-18 21:23:24 +0200 | [diff] [blame] | 166 | |
Greg Kroah-Hartman | ed4596e9 | 2015-12-22 18:21:51 -0800 | [diff] [blame] | 167 | kfree(data); |
Laurent Pinchart | ccb5803 | 2015-12-22 03:00:37 +0200 | [diff] [blame] | 168 | return retval; |
Laurent Pinchart | 8e2b7da | 2015-12-18 21:23:24 +0200 | [diff] [blame] | 169 | } |
Greg Kroah-Hartman | ed4596e9 | 2015-12-22 18:21:51 -0800 | [diff] [blame] | 170 | |
| 171 | static void ap_urb_complete(struct urb *urb) |
| 172 | { |
| 173 | struct usb_ctrlrequest *dr = urb->context; |
| 174 | |
| 175 | kfree(dr); |
| 176 | usb_free_urb(urb); |
| 177 | } |
| 178 | |
| 179 | static int output_async(struct es2_ap_dev *es2, void *req, u16 size, u8 cmd) |
| 180 | { |
| 181 | struct usb_device *udev = es2->usb_dev; |
| 182 | struct urb *urb; |
| 183 | struct usb_ctrlrequest *dr; |
| 184 | u8 *buf; |
| 185 | int retval; |
| 186 | |
| 187 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 188 | if (!urb) |
| 189 | return -ENOMEM; |
| 190 | |
| 191 | dr = kmalloc(sizeof(*dr) + size, GFP_ATOMIC); |
| 192 | if (!dr) { |
| 193 | usb_free_urb(urb); |
| 194 | return -ENOMEM; |
| 195 | } |
| 196 | |
| 197 | buf = (u8 *)dr + sizeof(*dr); |
| 198 | memcpy(buf, req, size); |
| 199 | |
| 200 | dr->bRequest = cmd; |
| 201 | dr->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE; |
| 202 | dr->wValue = 0; |
| 203 | dr->wIndex = 0; |
| 204 | dr->wLength = cpu_to_le16(size); |
| 205 | |
| 206 | usb_fill_control_urb(urb, udev, usb_sndctrlpipe(udev, 0), |
| 207 | (unsigned char *)dr, buf, size, |
| 208 | ap_urb_complete, dr); |
| 209 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 210 | if (retval) { |
| 211 | usb_free_urb(urb); |
| 212 | kfree(dr); |
| 213 | } |
| 214 | return retval; |
| 215 | } |
| 216 | |
| 217 | static int output(struct gb_host_device *hd, void *req, u16 size, u8 cmd, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 218 | bool async) |
Greg Kroah-Hartman | ed4596e9 | 2015-12-22 18:21:51 -0800 | [diff] [blame] | 219 | { |
| 220 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 221 | |
| 222 | if (async) |
| 223 | return output_async(es2, req, size, cmd); |
| 224 | |
| 225 | return output_sync(es2, req, size, cmd); |
| 226 | } |
Laurent Pinchart | 8e2b7da | 2015-12-18 21:23:24 +0200 | [diff] [blame] | 227 | |
Johan Hovold | 0ce68ce4 | 2015-11-04 18:55:14 +0100 | [diff] [blame] | 228 | static int es2_cport_in_enable(struct es2_ap_dev *es2, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 229 | struct es2_cport_in *cport_in) |
Johan Hovold | 0ce68ce4 | 2015-11-04 18:55:14 +0100 | [diff] [blame] | 230 | { |
| 231 | struct urb *urb; |
| 232 | int ret; |
| 233 | int i; |
| 234 | |
| 235 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
| 236 | urb = cport_in->urb[i]; |
| 237 | |
| 238 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 239 | if (ret) { |
| 240 | dev_err(&es2->usb_dev->dev, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 241 | "failed to submit in-urb: %d\n", ret); |
Johan Hovold | 0ce68ce4 | 2015-11-04 18:55:14 +0100 | [diff] [blame] | 242 | goto err_kill_urbs; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return 0; |
| 247 | |
| 248 | err_kill_urbs: |
| 249 | for (--i; i >= 0; --i) { |
| 250 | urb = cport_in->urb[i]; |
| 251 | usb_kill_urb(urb); |
| 252 | } |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
Johan Hovold | f6624ca | 2015-11-04 18:55:16 +0100 | [diff] [blame] | 257 | static void es2_cport_in_disable(struct es2_ap_dev *es2, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 258 | struct es2_cport_in *cport_in) |
Johan Hovold | f6624ca | 2015-11-04 18:55:16 +0100 | [diff] [blame] | 259 | { |
| 260 | struct urb *urb; |
| 261 | int i; |
| 262 | |
| 263 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
| 264 | urb = cport_in->urb[i]; |
| 265 | usb_kill_urb(urb); |
| 266 | } |
| 267 | } |
| 268 | |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 269 | static int es2_arpc_in_enable(struct es2_ap_dev *es2) |
| 270 | { |
| 271 | struct urb *urb; |
| 272 | int ret; |
| 273 | int i; |
| 274 | |
| 275 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { |
| 276 | urb = es2->arpc_urb[i]; |
| 277 | |
| 278 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 279 | if (ret) { |
| 280 | dev_err(&es2->usb_dev->dev, |
| 281 | "failed to submit arpc in-urb: %d\n", ret); |
| 282 | goto err_kill_urbs; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | |
| 288 | err_kill_urbs: |
| 289 | for (--i; i >= 0; --i) { |
| 290 | urb = es2->arpc_urb[i]; |
| 291 | usb_kill_urb(urb); |
| 292 | } |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static void es2_arpc_in_disable(struct es2_ap_dev *es2) |
| 298 | { |
| 299 | struct urb *urb; |
| 300 | int i; |
| 301 | |
| 302 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { |
| 303 | urb = es2->arpc_urb[i]; |
| 304 | usb_kill_urb(urb); |
| 305 | } |
| 306 | } |
| 307 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 308 | static struct urb *next_free_urb(struct es2_ap_dev *es2, gfp_t gfp_mask) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 309 | { |
| 310 | struct urb *urb = NULL; |
| 311 | unsigned long flags; |
| 312 | int i; |
| 313 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 314 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 315 | |
| 316 | /* Look in our pool of allocated urbs first, as that's the "fastest" */ |
| 317 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
Ioannis Valasakis | 2eece0a | 2018-11-09 13:54:57 +0000 | [diff] [blame] | 318 | if (!es2->cport_out_urb_busy[i] && |
| 319 | !es2->cport_out_urb_cancelled[i]) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 320 | es2->cport_out_urb_busy[i] = true; |
| 321 | urb = es2->cport_out_urb[i]; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 322 | break; |
| 323 | } |
| 324 | } |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 325 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 326 | if (urb) |
| 327 | return urb; |
| 328 | |
| 329 | /* |
| 330 | * Crap, pool is empty, complain to the syslog and go allocate one |
| 331 | * dynamically as we have to succeed. |
| 332 | */ |
Johan Hovold | ed972e3 | 2015-12-15 15:51:48 +0100 | [diff] [blame] | 333 | dev_dbg(&es2->usb_dev->dev, |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 334 | "No free CPort OUT urbs, having to dynamically allocate one!\n"); |
| 335 | return usb_alloc_urb(0, gfp_mask); |
| 336 | } |
| 337 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 338 | static void free_urb(struct es2_ap_dev *es2, struct urb *urb) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 339 | { |
| 340 | unsigned long flags; |
| 341 | int i; |
| 342 | /* |
| 343 | * See if this was an urb in our pool, if so mark it "free", otherwise |
| 344 | * we need to free it ourselves. |
| 345 | */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 346 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 347 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 348 | if (urb == es2->cport_out_urb[i]) { |
| 349 | es2->cport_out_urb_busy[i] = false; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 350 | urb = NULL; |
| 351 | break; |
| 352 | } |
| 353 | } |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 354 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 355 | |
| 356 | /* If urb is not NULL, then we need to free this urb */ |
| 357 | usb_free_urb(urb); |
| 358 | } |
| 359 | |
| 360 | /* |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 361 | * We (ab)use the operation-message header pad bytes to transfer the |
| 362 | * cport id in order to minimise overhead. |
| 363 | */ |
| 364 | static void |
| 365 | gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id) |
| 366 | { |
Alex Elder | 4bc1389 | 2015-06-13 11:02:11 -0500 | [diff] [blame] | 367 | header->pad[0] = cport_id; |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* Clear the pad bytes used for the CPort id */ |
| 371 | static void gb_message_cport_clear(struct gb_operation_msg_hdr *header) |
| 372 | { |
Alex Elder | 4bc1389 | 2015-06-13 11:02:11 -0500 | [diff] [blame] | 373 | header->pad[0] = 0; |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | /* Extract the CPort id packed into the header, and clear it */ |
| 377 | static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header) |
| 378 | { |
Alex Elder | 4bc1389 | 2015-06-13 11:02:11 -0500 | [diff] [blame] | 379 | u16 cport_id = header->pad[0]; |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 380 | |
| 381 | gb_message_cport_clear(header); |
| 382 | |
| 383 | return cport_id; |
| 384 | } |
| 385 | |
| 386 | /* |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 387 | * Returns zero if the message was successfully queued, or a negative errno |
| 388 | * otherwise. |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 389 | */ |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 390 | static int message_send(struct gb_host_device *hd, u16 cport_id, |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 391 | struct gb_message *message, gfp_t gfp_mask) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 392 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 393 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 394 | struct usb_device *udev = es2->usb_dev; |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 395 | size_t buffer_size; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 396 | int retval; |
| 397 | struct urb *urb; |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 398 | unsigned long flags; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 399 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 400 | /* |
| 401 | * The data actually transferred will include an indication |
| 402 | * of where the data should be sent. Do one last check of |
| 403 | * the target CPort id before filling it in. |
| 404 | */ |
Fabien Parent | 144670c | 2015-09-02 15:50:35 +0200 | [diff] [blame] | 405 | if (!cport_id_valid(hd, cport_id)) { |
Johan Hovold | 100e900 | 2015-12-07 15:05:38 +0100 | [diff] [blame] | 406 | dev_err(&udev->dev, "invalid cport %u\n", cport_id); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 407 | return -EINVAL; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 408 | } |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 409 | |
| 410 | /* Find a free urb */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 411 | urb = next_free_urb(es2, gfp_mask); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 412 | if (!urb) |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 413 | return -ENOMEM; |
| 414 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 415 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 416 | message->hcpriv = urb; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 417 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 418 | |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 419 | /* Pack the cport id into the message header */ |
| 420 | gb_message_cport_pack(message->header, cport_id); |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 421 | |
Alex Elder | 821c620 | 2015-06-13 11:02:07 -0500 | [diff] [blame] | 422 | buffer_size = sizeof(*message->header) + message->payload_size; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 423 | |
| 424 | usb_fill_bulk_urb(urb, udev, |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 425 | usb_sndbulkpipe(udev, |
Greg Kroah-Hartman | 403074b | 2016-08-17 16:27:49 +0200 | [diff] [blame] | 426 | es2->cport_out_endpoint), |
Alex Elder | 821c620 | 2015-06-13 11:02:07 -0500 | [diff] [blame] | 427 | message->buffer, buffer_size, |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 428 | cport_out_callback, message); |
Alexandre Bailon | 977e209 | 2015-08-31 09:00:16 +0200 | [diff] [blame] | 429 | urb->transfer_flags |= URB_ZERO_PACKET; |
Alex Elder | 495787a | 2016-06-03 15:55:38 -0500 | [diff] [blame] | 430 | |
| 431 | trace_gb_message_submit(message); |
| 432 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 433 | retval = usb_submit_urb(urb, gfp_mask); |
| 434 | if (retval) { |
Viresh Kumar | 05e3095 | 2016-06-23 23:22:15 +0530 | [diff] [blame] | 435 | dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 436 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 437 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 438 | message->hcpriv = NULL; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 439 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 440 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 441 | free_urb(es2, urb); |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 442 | gb_message_cport_clear(message->header); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 443 | |
| 444 | return retval; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 445 | } |
| 446 | |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 447 | return 0; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 451 | * Can not be called in atomic context. |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 452 | */ |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 453 | static void message_cancel(struct gb_message *message) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 454 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 455 | struct gb_host_device *hd = message->operation->connection->hd; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 456 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 457 | struct urb *urb; |
| 458 | int i; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 459 | |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 460 | might_sleep(); |
| 461 | |
Johan Hovold | 07af934 | 2016-06-27 20:07:11 +0200 | [diff] [blame] | 462 | spin_lock_irq(&es2->cport_out_urb_lock); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 463 | urb = message->hcpriv; |
| 464 | |
| 465 | /* Prevent dynamically allocated urb from being deallocated. */ |
| 466 | usb_get_urb(urb); |
| 467 | |
| 468 | /* Prevent pre-allocated urb from being reused. */ |
| 469 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 470 | if (urb == es2->cport_out_urb[i]) { |
| 471 | es2->cport_out_urb_cancelled[i] = true; |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 472 | break; |
| 473 | } |
| 474 | } |
Johan Hovold | 07af934 | 2016-06-27 20:07:11 +0200 | [diff] [blame] | 475 | spin_unlock_irq(&es2->cport_out_urb_lock); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 476 | |
| 477 | usb_kill_urb(urb); |
| 478 | |
| 479 | if (i < NUM_CPORT_OUT_URB) { |
Johan Hovold | 07af934 | 2016-06-27 20:07:11 +0200 | [diff] [blame] | 480 | spin_lock_irq(&es2->cport_out_urb_lock); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 481 | es2->cport_out_urb_cancelled[i] = false; |
Johan Hovold | 07af934 | 2016-06-27 20:07:11 +0200 | [diff] [blame] | 482 | spin_unlock_irq(&es2->cport_out_urb_lock); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | usb_free_urb(urb); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 486 | } |
| 487 | |
Johan Hovold | 045d356 | 2016-05-11 10:18:04 +0200 | [diff] [blame] | 488 | static int es2_cport_allocate(struct gb_host_device *hd, int cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 489 | unsigned long flags) |
Johan Hovold | 045d356 | 2016-05-11 10:18:04 +0200 | [diff] [blame] | 490 | { |
| 491 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 492 | struct ida *id_map = &hd->cport_id_map; |
| 493 | int ida_start, ida_end; |
| 494 | |
| 495 | switch (cport_id) { |
| 496 | case ES2_CPORT_CDSI0: |
| 497 | case ES2_CPORT_CDSI1: |
| 498 | dev_err(&hd->dev, "cport %d not available\n", cport_id); |
| 499 | return -EBUSY; |
| 500 | } |
| 501 | |
| 502 | if (flags & GB_CONNECTION_FLAG_OFFLOADED && |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 503 | flags & GB_CONNECTION_FLAG_CDSI1) { |
Johan Hovold | 045d356 | 2016-05-11 10:18:04 +0200 | [diff] [blame] | 504 | if (es2->cdsi1_in_use) { |
| 505 | dev_err(&hd->dev, "CDSI1 already in use\n"); |
| 506 | return -EBUSY; |
| 507 | } |
| 508 | |
| 509 | es2->cdsi1_in_use = true; |
| 510 | |
| 511 | return ES2_CPORT_CDSI1; |
| 512 | } |
| 513 | |
| 514 | if (cport_id < 0) { |
| 515 | ida_start = 0; |
| 516 | ida_end = hd->num_cports; |
| 517 | } else if (cport_id < hd->num_cports) { |
| 518 | ida_start = cport_id; |
| 519 | ida_end = cport_id + 1; |
| 520 | } else { |
| 521 | dev_err(&hd->dev, "cport %d not available\n", cport_id); |
| 522 | return -EINVAL; |
| 523 | } |
| 524 | |
| 525 | return ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL); |
| 526 | } |
| 527 | |
| 528 | static void es2_cport_release(struct gb_host_device *hd, u16 cport_id) |
| 529 | { |
| 530 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 531 | |
| 532 | switch (cport_id) { |
| 533 | case ES2_CPORT_CDSI1: |
| 534 | es2->cdsi1_in_use = false; |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | ida_simple_remove(&hd->cport_id_map, cport_id); |
| 539 | } |
| 540 | |
Johan Hovold | 74ec759 | 2016-06-22 11:42:05 +0200 | [diff] [blame] | 541 | static int cport_enable(struct gb_host_device *hd, u16 cport_id, |
| 542 | unsigned long flags) |
| 543 | { |
| 544 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 545 | struct usb_device *udev = es2->usb_dev; |
| 546 | struct gb_apb_request_cport_flags *req; |
Bryan O'Donoghue | 6a1d295 | 2016-07-18 14:59:36 +0100 | [diff] [blame] | 547 | u32 connection_flags; |
Johan Hovold | 74ec759 | 2016-06-22 11:42:05 +0200 | [diff] [blame] | 548 | int ret; |
| 549 | |
| 550 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 551 | if (!req) |
| 552 | return -ENOMEM; |
| 553 | |
Bryan O'Donoghue | 6a1d295 | 2016-07-18 14:59:36 +0100 | [diff] [blame] | 554 | connection_flags = 0; |
Johan Hovold | 74ec759 | 2016-06-22 11:42:05 +0200 | [diff] [blame] | 555 | if (flags & GB_CONNECTION_FLAG_CONTROL) |
Bryan O'Donoghue | 6a1d295 | 2016-07-18 14:59:36 +0100 | [diff] [blame] | 556 | connection_flags |= GB_APB_CPORT_FLAG_CONTROL; |
Johan Hovold | 74ec759 | 2016-06-22 11:42:05 +0200 | [diff] [blame] | 557 | if (flags & GB_CONNECTION_FLAG_HIGH_PRIO) |
Bryan O'Donoghue | 6a1d295 | 2016-07-18 14:59:36 +0100 | [diff] [blame] | 558 | connection_flags |= GB_APB_CPORT_FLAG_HIGH_PRIO; |
| 559 | |
| 560 | req->flags = cpu_to_le32(connection_flags); |
Johan Hovold | 74ec759 | 2016-06-22 11:42:05 +0200 | [diff] [blame] | 561 | |
| 562 | dev_dbg(&hd->dev, "%s - cport = %u, flags = %02x\n", __func__, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 563 | cport_id, connection_flags); |
Johan Hovold | 74ec759 | 2016-06-22 11:42:05 +0200 | [diff] [blame] | 564 | |
| 565 | ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 566 | GB_APB_REQUEST_CPORT_FLAGS, |
| 567 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 568 | USB_RECIP_INTERFACE, cport_id, 0, |
| 569 | req, sizeof(*req), ES2_USB_CTRL_TIMEOUT); |
Johan Hovold | e424025 | 2021-01-18 15:46:29 +0100 | [diff] [blame] | 570 | if (ret < 0) { |
Johan Hovold | 74ec759 | 2016-06-22 11:42:05 +0200 | [diff] [blame] | 571 | dev_err(&udev->dev, "failed to set cport flags for port %d\n", |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 572 | cport_id); |
Johan Hovold | 74ec759 | 2016-06-22 11:42:05 +0200 | [diff] [blame] | 573 | goto out; |
| 574 | } |
| 575 | |
| 576 | ret = 0; |
| 577 | out: |
| 578 | kfree(req); |
| 579 | |
| 580 | return ret; |
| 581 | } |
| 582 | |
Johan Hovold | a9dc1cf | 2016-08-10 12:58:42 +0200 | [diff] [blame] | 583 | static int es2_cport_connected(struct gb_host_device *hd, u16 cport_id) |
| 584 | { |
| 585 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 586 | struct device *dev = &es2->usb_dev->dev; |
| 587 | struct arpc_cport_connected_req req; |
| 588 | int ret; |
| 589 | |
| 590 | req.cport_id = cpu_to_le16(cport_id); |
| 591 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_CONNECTED, &req, sizeof(req), |
| 592 | NULL, ES2_ARPC_CPORT_TIMEOUT); |
| 593 | if (ret) { |
| 594 | dev_err(dev, "failed to set connected state for cport %u: %d\n", |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 595 | cport_id, ret); |
Johan Hovold | a9dc1cf | 2016-08-10 12:58:42 +0200 | [diff] [blame] | 596 | return ret; |
| 597 | } |
| 598 | |
| 599 | return 0; |
| 600 | } |
| 601 | |
Johan Hovold | d2dee94 | 2016-08-26 12:55:47 +0200 | [diff] [blame] | 602 | static int es2_cport_flush(struct gb_host_device *hd, u16 cport_id) |
| 603 | { |
| 604 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 605 | struct device *dev = &es2->usb_dev->dev; |
| 606 | struct arpc_cport_flush_req req; |
| 607 | int ret; |
| 608 | |
| 609 | req.cport_id = cpu_to_le16(cport_id); |
| 610 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_FLUSH, &req, sizeof(req), |
| 611 | NULL, ES2_ARPC_CPORT_TIMEOUT); |
| 612 | if (ret) { |
| 613 | dev_err(dev, "failed to flush cport %u: %d\n", cport_id, ret); |
| 614 | return ret; |
| 615 | } |
| 616 | |
| 617 | return 0; |
| 618 | } |
| 619 | |
Johan Hovold | 77e52b3 | 2016-08-26 12:55:48 +0200 | [diff] [blame] | 620 | static int es2_cport_shutdown(struct gb_host_device *hd, u16 cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 621 | u8 phase, unsigned int timeout) |
Johan Hovold | 77e52b3 | 2016-08-26 12:55:48 +0200 | [diff] [blame] | 622 | { |
| 623 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 624 | struct device *dev = &es2->usb_dev->dev; |
| 625 | struct arpc_cport_shutdown_req req; |
| 626 | int result; |
| 627 | int ret; |
| 628 | |
| 629 | if (timeout > U16_MAX) |
| 630 | return -EINVAL; |
| 631 | |
| 632 | req.cport_id = cpu_to_le16(cport_id); |
| 633 | req.timeout = cpu_to_le16(timeout); |
| 634 | req.phase = phase; |
| 635 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_SHUTDOWN, &req, sizeof(req), |
| 636 | &result, ES2_ARPC_CPORT_TIMEOUT + timeout); |
| 637 | if (ret) { |
| 638 | dev_err(dev, "failed to send shutdown over cport %u: %d (%d)\n", |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 639 | cport_id, ret, result); |
Johan Hovold | 77e52b3 | 2016-08-26 12:55:48 +0200 | [diff] [blame] | 640 | return ret; |
| 641 | } |
| 642 | |
| 643 | return 0; |
| 644 | } |
| 645 | |
Johan Hovold | 0e7cd0d | 2016-08-10 12:58:43 +0200 | [diff] [blame] | 646 | static int es2_cport_quiesce(struct gb_host_device *hd, u16 cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 647 | size_t peer_space, unsigned int timeout) |
Johan Hovold | 0e7cd0d | 2016-08-10 12:58:43 +0200 | [diff] [blame] | 648 | { |
| 649 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 650 | struct device *dev = &es2->usb_dev->dev; |
| 651 | struct arpc_cport_quiesce_req req; |
| 652 | int result; |
| 653 | int ret; |
| 654 | |
| 655 | if (peer_space > U16_MAX) |
| 656 | return -EINVAL; |
| 657 | |
| 658 | if (timeout > U16_MAX) |
| 659 | return -EINVAL; |
| 660 | |
| 661 | req.cport_id = cpu_to_le16(cport_id); |
| 662 | req.peer_space = cpu_to_le16(peer_space); |
| 663 | req.timeout = cpu_to_le16(timeout); |
| 664 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_QUIESCE, &req, sizeof(req), |
| 665 | &result, ES2_ARPC_CPORT_TIMEOUT + timeout); |
| 666 | if (ret) { |
| 667 | dev_err(dev, "failed to quiesce cport %u: %d (%d)\n", |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 668 | cport_id, ret, result); |
Johan Hovold | 0e7cd0d | 2016-08-10 12:58:43 +0200 | [diff] [blame] | 669 | return ret; |
| 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
Johan Hovold | 6471c00 | 2016-08-10 12:58:44 +0200 | [diff] [blame] | 675 | static int es2_cport_clear(struct gb_host_device *hd, u16 cport_id) |
| 676 | { |
| 677 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 678 | struct device *dev = &es2->usb_dev->dev; |
| 679 | struct arpc_cport_clear_req req; |
| 680 | int ret; |
| 681 | |
| 682 | req.cport_id = cpu_to_le16(cport_id); |
| 683 | ret = arpc_sync(es2, ARPC_TYPE_CPORT_CLEAR, &req, sizeof(req), |
| 684 | NULL, ES2_ARPC_CPORT_TIMEOUT); |
| 685 | if (ret) { |
| 686 | dev_err(dev, "failed to clear cport %u: %d\n", cport_id, ret); |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 693 | static int latency_tag_enable(struct gb_host_device *hd, u16 cport_id) |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 694 | { |
| 695 | int retval; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 696 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 697 | struct usb_device *udev = es2->usb_dev; |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 698 | |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 699 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
Greg Kroah-Hartman | e527338 | 2015-12-31 11:14:33 -0800 | [diff] [blame] | 700 | GB_APB_REQUEST_LATENCY_TAG_EN, |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 701 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 702 | USB_RECIP_INTERFACE, cport_id, 0, NULL, |
Johan Hovold | 60793c9 | 2016-08-03 14:09:35 +0200 | [diff] [blame] | 703 | 0, ES2_USB_CTRL_TIMEOUT); |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 704 | |
| 705 | if (retval < 0) |
| 706 | dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n", |
| 707 | cport_id); |
| 708 | return retval; |
| 709 | } |
| 710 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 711 | static int latency_tag_disable(struct gb_host_device *hd, u16 cport_id) |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 712 | { |
| 713 | int retval; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 714 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 715 | struct usb_device *udev = es2->usb_dev; |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 716 | |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 717 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
Greg Kroah-Hartman | e527338 | 2015-12-31 11:14:33 -0800 | [diff] [blame] | 718 | GB_APB_REQUEST_LATENCY_TAG_DIS, |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 719 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 720 | USB_RECIP_INTERFACE, cport_id, 0, NULL, |
Johan Hovold | 60793c9 | 2016-08-03 14:09:35 +0200 | [diff] [blame] | 721 | 0, ES2_USB_CTRL_TIMEOUT); |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 722 | |
| 723 | if (retval < 0) |
| 724 | dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n", |
| 725 | cport_id); |
| 726 | return retval; |
| 727 | } |
| 728 | |
Johan Hovold | a8cc020 | 2015-11-03 18:03:24 +0100 | [diff] [blame] | 729 | static struct gb_hd_driver es2_driver = { |
Bryan O'Donoghue | c8a657b | 2016-05-15 19:37:48 +0100 | [diff] [blame] | 730 | .hd_priv_size = sizeof(struct es2_ap_dev), |
| 731 | .message_send = message_send, |
| 732 | .message_cancel = message_cancel, |
| 733 | .cport_allocate = es2_cport_allocate, |
| 734 | .cport_release = es2_cport_release, |
Johan Hovold | 74ec759 | 2016-06-22 11:42:05 +0200 | [diff] [blame] | 735 | .cport_enable = cport_enable, |
Johan Hovold | a9dc1cf | 2016-08-10 12:58:42 +0200 | [diff] [blame] | 736 | .cport_connected = es2_cport_connected, |
Johan Hovold | d2dee94 | 2016-08-26 12:55:47 +0200 | [diff] [blame] | 737 | .cport_flush = es2_cport_flush, |
Johan Hovold | 77e52b3 | 2016-08-26 12:55:48 +0200 | [diff] [blame] | 738 | .cport_shutdown = es2_cport_shutdown, |
Johan Hovold | 0e7cd0d | 2016-08-10 12:58:43 +0200 | [diff] [blame] | 739 | .cport_quiesce = es2_cport_quiesce, |
Johan Hovold | 6471c00 | 2016-08-10 12:58:44 +0200 | [diff] [blame] | 740 | .cport_clear = es2_cport_clear, |
Bryan O'Donoghue | c8a657b | 2016-05-15 19:37:48 +0100 | [diff] [blame] | 741 | .latency_tag_enable = latency_tag_enable, |
| 742 | .latency_tag_disable = latency_tag_disable, |
| 743 | .output = output, |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 744 | }; |
| 745 | |
| 746 | /* Common function to report consistent warnings based on URB status */ |
| 747 | static int check_urb_status(struct urb *urb) |
| 748 | { |
| 749 | struct device *dev = &urb->dev->dev; |
| 750 | int status = urb->status; |
| 751 | |
| 752 | switch (status) { |
| 753 | case 0: |
| 754 | return 0; |
| 755 | |
| 756 | case -EOVERFLOW: |
| 757 | dev_err(dev, "%s: overflow actual length is %d\n", |
| 758 | __func__, urb->actual_length); |
Gustavo A. R. Silva | 37b8b73 | 2020-07-27 13:32:58 -0500 | [diff] [blame] | 759 | fallthrough; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 760 | case -ECONNRESET: |
| 761 | case -ENOENT: |
| 762 | case -ESHUTDOWN: |
| 763 | case -EILSEQ: |
| 764 | case -EPROTO: |
| 765 | /* device is gone, stop sending */ |
| 766 | return status; |
| 767 | } |
| 768 | dev_err(dev, "%s: unknown status %d\n", __func__, status); |
| 769 | |
| 770 | return -EAGAIN; |
| 771 | } |
| 772 | |
Johan Hovold | 57bc17f | 2015-11-04 18:55:20 +0100 | [diff] [blame] | 773 | static void es2_destroy(struct es2_ap_dev *es2) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 774 | { |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 775 | struct usb_device *udev; |
Greg Kroah-Hartman | 1521eb6 | 2016-08-17 16:44:11 +0200 | [diff] [blame] | 776 | struct urb *urb; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 777 | int i; |
| 778 | |
Johan Hovold | 74cd650 | 2015-11-04 18:55:18 +0100 | [diff] [blame] | 779 | debugfs_remove(es2->apb_log_enable_dentry); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 780 | usb_log_disable(es2); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 781 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 782 | /* Tear down everything! */ |
| 783 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
Greg Kroah-Hartman | 1521eb6 | 2016-08-17 16:44:11 +0200 | [diff] [blame] | 784 | urb = es2->cport_out_urb[i]; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 785 | usb_kill_urb(urb); |
| 786 | usb_free_urb(urb); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 787 | es2->cport_out_urb[i] = NULL; |
| 788 | es2->cport_out_urb_busy[i] = false; /* just to be anal */ |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 789 | } |
| 790 | |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 791 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { |
Greg Kroah-Hartman | 1521eb6 | 2016-08-17 16:44:11 +0200 | [diff] [blame] | 792 | usb_free_urb(es2->arpc_urb[i]); |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 793 | kfree(es2->arpc_buffer[i]); |
| 794 | es2->arpc_buffer[i] = NULL; |
| 795 | } |
| 796 | |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 797 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
| 798 | usb_free_urb(es2->cport_in.urb[i]); |
| 799 | kfree(es2->cport_in.buffer[i]); |
| 800 | es2->cport_in.buffer[i] = NULL; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 801 | } |
| 802 | |
Vaibhav Agarwal | 52033fd | 2016-05-27 10:49:24 +0530 | [diff] [blame] | 803 | /* release reserved CDSI0 and CDSI1 cports */ |
| 804 | gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI1); |
| 805 | gb_hd_cport_release_reserved(es2->hd, ES2_CPORT_CDSI0); |
| 806 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 807 | udev = es2->usb_dev; |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 808 | gb_hd_put(es2->hd); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 809 | |
| 810 | usb_put_dev(udev); |
| 811 | } |
| 812 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 813 | static void cport_in_callback(struct urb *urb) |
| 814 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 815 | struct gb_host_device *hd = urb->context; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 816 | struct device *dev = &urb->dev->dev; |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 817 | struct gb_operation_msg_hdr *header; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 818 | int status = check_urb_status(urb); |
| 819 | int retval; |
| 820 | u16 cport_id; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 821 | |
| 822 | if (status) { |
| 823 | if ((status == -EAGAIN) || (status == -EPROTO)) |
| 824 | goto exit; |
Viresh Kumar | 62de6e0 | 2016-04-21 11:31:15 +0530 | [diff] [blame] | 825 | |
| 826 | /* The urb is being unlinked */ |
| 827 | if (status == -ENOENT || status == -ESHUTDOWN) |
| 828 | return; |
| 829 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 830 | dev_err(dev, "urb cport in error %d (dropped)\n", status); |
| 831 | return; |
| 832 | } |
| 833 | |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 834 | if (urb->actual_length < sizeof(*header)) { |
Johan Hovold | 305a031 | 2015-10-13 19:10:20 +0200 | [diff] [blame] | 835 | dev_err(dev, "short message received\n"); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 836 | goto exit; |
| 837 | } |
| 838 | |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 839 | /* Extract the CPort id, which is packed in the message header */ |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 840 | header = urb->transfer_buffer; |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 841 | cport_id = gb_message_cport_unpack(header); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 842 | |
Bryan O'Donoghue | 6872c46 | 2015-09-22 18:06:39 -0700 | [diff] [blame] | 843 | if (cport_id_valid(hd, cport_id)) { |
Alex Elder | 821c620 | 2015-06-13 11:02:07 -0500 | [diff] [blame] | 844 | greybus_data_rcvd(hd, cport_id, urb->transfer_buffer, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 845 | urb->actual_length); |
Bryan O'Donoghue | 6872c46 | 2015-09-22 18:06:39 -0700 | [diff] [blame] | 846 | } else { |
Johan Hovold | 100e900 | 2015-12-07 15:05:38 +0100 | [diff] [blame] | 847 | dev_err(dev, "invalid cport id %u received\n", cport_id); |
Bryan O'Donoghue | 6872c46 | 2015-09-22 18:06:39 -0700 | [diff] [blame] | 848 | } |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 849 | exit: |
| 850 | /* put our urb back in the request pool */ |
| 851 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 852 | if (retval) |
Johan Hovold | 305a031 | 2015-10-13 19:10:20 +0200 | [diff] [blame] | 853 | dev_err(dev, "failed to resubmit in-urb: %d\n", retval); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | static void cport_out_callback(struct urb *urb) |
| 857 | { |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 858 | struct gb_message *message = urb->context; |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 859 | struct gb_host_device *hd = message->operation->connection->hd; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 860 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 861 | int status = check_urb_status(urb); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 862 | unsigned long flags; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 863 | |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 864 | gb_message_cport_clear(message->header); |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 865 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 866 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
Johan Hovold | 58c8512 | 2015-09-26 14:37:59 -0700 | [diff] [blame] | 867 | message->hcpriv = NULL; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 868 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
Johan Hovold | 58c8512 | 2015-09-26 14:37:59 -0700 | [diff] [blame] | 869 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 870 | /* |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 871 | * Tell the submitter that the message send (attempt) is |
| 872 | * complete, and report the status. |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 873 | */ |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 874 | greybus_message_sent(hd, message, status); |
| 875 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 876 | free_urb(es2, urb); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 877 | } |
| 878 | |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 879 | static struct arpc *arpc_alloc(void *payload, u16 size, u8 type) |
| 880 | { |
| 881 | struct arpc *rpc; |
| 882 | |
| 883 | if (size + sizeof(*rpc->req) > ARPC_OUT_SIZE_MAX) |
| 884 | return NULL; |
| 885 | |
| 886 | rpc = kzalloc(sizeof(*rpc), GFP_KERNEL); |
| 887 | if (!rpc) |
| 888 | return NULL; |
| 889 | |
| 890 | INIT_LIST_HEAD(&rpc->list); |
| 891 | rpc->req = kzalloc(sizeof(*rpc->req) + size, GFP_KERNEL); |
| 892 | if (!rpc->req) |
| 893 | goto err_free_rpc; |
| 894 | |
| 895 | rpc->resp = kzalloc(sizeof(*rpc->resp), GFP_KERNEL); |
Johan Hovold | fcba5d0 | 2016-07-27 16:37:20 +0200 | [diff] [blame] | 896 | if (!rpc->resp) |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 897 | goto err_free_req; |
| 898 | |
| 899 | rpc->req->type = type; |
Rui Miguel Silva | 7fec2bc | 2016-10-19 13:44:25 +0100 | [diff] [blame] | 900 | rpc->req->size = cpu_to_le16(sizeof(*rpc->req) + size); |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 901 | memcpy(rpc->req->data, payload, size); |
| 902 | |
| 903 | init_completion(&rpc->response_received); |
| 904 | |
| 905 | return rpc; |
| 906 | |
| 907 | err_free_req: |
| 908 | kfree(rpc->req); |
| 909 | err_free_rpc: |
| 910 | kfree(rpc); |
| 911 | |
| 912 | return NULL; |
| 913 | } |
| 914 | |
| 915 | static void arpc_free(struct arpc *rpc) |
| 916 | { |
| 917 | kfree(rpc->req); |
| 918 | kfree(rpc->resp); |
| 919 | kfree(rpc); |
| 920 | } |
| 921 | |
Johan Hovold | 165a74a | 2016-07-14 12:55:00 -0500 | [diff] [blame] | 922 | static struct arpc *arpc_find(struct es2_ap_dev *es2, __le16 id) |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 923 | { |
| 924 | struct arpc *rpc; |
| 925 | |
| 926 | list_for_each_entry(rpc, &es2->arpcs, list) { |
Johan Hovold | 165a74a | 2016-07-14 12:55:00 -0500 | [diff] [blame] | 927 | if (rpc->req->id == id) |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 928 | return rpc; |
| 929 | } |
| 930 | |
| 931 | return NULL; |
| 932 | } |
| 933 | |
| 934 | static void arpc_add(struct es2_ap_dev *es2, struct arpc *rpc) |
| 935 | { |
| 936 | rpc->active = true; |
Viresh Kumar | e818027 | 2016-07-14 12:18:00 -0500 | [diff] [blame] | 937 | rpc->req->id = cpu_to_le16(es2->arpc_id_cycle++); |
Johan Hovold | c0219cb | 2016-07-27 16:37:22 +0200 | [diff] [blame] | 938 | list_add_tail(&rpc->list, &es2->arpcs); |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | static void arpc_del(struct es2_ap_dev *es2, struct arpc *rpc) |
| 942 | { |
| 943 | if (rpc->active) { |
| 944 | rpc->active = false; |
| 945 | list_del(&rpc->list); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | static int arpc_send(struct es2_ap_dev *es2, struct arpc *rpc, int timeout) |
| 950 | { |
| 951 | struct usb_device *udev = es2->usb_dev; |
| 952 | int retval; |
| 953 | |
| 954 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
Johan Hovold | 83d474f | 2016-08-10 12:58:40 +0200 | [diff] [blame] | 955 | GB_APB_REQUEST_ARPC_RUN, |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 956 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 957 | USB_RECIP_INTERFACE, |
| 958 | 0, 0, |
Viresh Kumar | e818027 | 2016-07-14 12:18:00 -0500 | [diff] [blame] | 959 | rpc->req, le16_to_cpu(rpc->req->size), |
Johan Hovold | 60793c9 | 2016-08-03 14:09:35 +0200 | [diff] [blame] | 960 | ES2_USB_CTRL_TIMEOUT); |
Johan Hovold | e424025 | 2021-01-18 15:46:29 +0100 | [diff] [blame] | 961 | if (retval < 0) { |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 962 | dev_err(&udev->dev, |
| 963 | "failed to send ARPC request %d: %d\n", |
| 964 | rpc->req->type, retval); |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 965 | return retval; |
| 966 | } |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | static int arpc_sync(struct es2_ap_dev *es2, u8 type, void *payload, |
| 972 | size_t size, int *result, unsigned int timeout) |
| 973 | { |
| 974 | struct arpc *rpc; |
| 975 | unsigned long flags; |
| 976 | int retval; |
| 977 | |
Johan Hovold | 121bae2 | 2016-08-03 14:09:33 +0200 | [diff] [blame] | 978 | if (result) |
| 979 | *result = 0; |
Johan Hovold | 4ae2d96 | 2016-08-03 14:09:32 +0200 | [diff] [blame] | 980 | |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 981 | rpc = arpc_alloc(payload, size, type); |
| 982 | if (!rpc) |
| 983 | return -ENOMEM; |
| 984 | |
| 985 | spin_lock_irqsave(&es2->arpc_lock, flags); |
| 986 | arpc_add(es2, rpc); |
| 987 | spin_unlock_irqrestore(&es2->arpc_lock, flags); |
| 988 | |
| 989 | retval = arpc_send(es2, rpc, timeout); |
| 990 | if (retval) |
| 991 | goto out_arpc_del; |
| 992 | |
| 993 | retval = wait_for_completion_interruptible_timeout( |
| 994 | &rpc->response_received, |
| 995 | msecs_to_jiffies(timeout)); |
| 996 | if (retval <= 0) { |
| 997 | if (!retval) |
| 998 | retval = -ETIMEDOUT; |
| 999 | goto out_arpc_del; |
| 1000 | } |
| 1001 | |
Johan Hovold | 4ae2d96 | 2016-08-03 14:09:32 +0200 | [diff] [blame] | 1002 | if (rpc->resp->result) { |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1003 | retval = -EREMOTEIO; |
Johan Hovold | 121bae2 | 2016-08-03 14:09:33 +0200 | [diff] [blame] | 1004 | if (result) |
| 1005 | *result = rpc->resp->result; |
Johan Hovold | 4ae2d96 | 2016-08-03 14:09:32 +0200 | [diff] [blame] | 1006 | } else { |
Johan Hovold | 3487394 | 2016-07-13 09:34:00 -0500 | [diff] [blame] | 1007 | retval = 0; |
Johan Hovold | 4ae2d96 | 2016-08-03 14:09:32 +0200 | [diff] [blame] | 1008 | } |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1009 | |
| 1010 | out_arpc_del: |
| 1011 | spin_lock_irqsave(&es2->arpc_lock, flags); |
| 1012 | arpc_del(es2, rpc); |
| 1013 | spin_unlock_irqrestore(&es2->arpc_lock, flags); |
| 1014 | arpc_free(rpc); |
| 1015 | |
| 1016 | if (retval < 0 && retval != -EREMOTEIO) { |
| 1017 | dev_err(&es2->usb_dev->dev, |
| 1018 | "failed to execute ARPC: %d\n", retval); |
| 1019 | } |
| 1020 | |
| 1021 | return retval; |
| 1022 | } |
| 1023 | |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1024 | static void arpc_in_callback(struct urb *urb) |
| 1025 | { |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1026 | struct es2_ap_dev *es2 = urb->context; |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1027 | struct device *dev = &urb->dev->dev; |
| 1028 | int status = check_urb_status(urb); |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1029 | struct arpc *rpc; |
| 1030 | struct arpc_response_message *resp; |
| 1031 | unsigned long flags; |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1032 | int retval; |
| 1033 | |
| 1034 | if (status) { |
| 1035 | if ((status == -EAGAIN) || (status == -EPROTO)) |
| 1036 | goto exit; |
| 1037 | |
| 1038 | /* The urb is being unlinked */ |
| 1039 | if (status == -ENOENT || status == -ESHUTDOWN) |
| 1040 | return; |
| 1041 | |
| 1042 | dev_err(dev, "arpc in-urb error %d (dropped)\n", status); |
| 1043 | return; |
| 1044 | } |
| 1045 | |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1046 | if (urb->actual_length < sizeof(*resp)) { |
| 1047 | dev_err(dev, "short aprc response received\n"); |
| 1048 | goto exit; |
| 1049 | } |
| 1050 | |
| 1051 | resp = urb->transfer_buffer; |
| 1052 | spin_lock_irqsave(&es2->arpc_lock, flags); |
Johan Hovold | 165a74a | 2016-07-14 12:55:00 -0500 | [diff] [blame] | 1053 | rpc = arpc_find(es2, resp->id); |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1054 | if (!rpc) { |
Johan Hovold | 178457e | 2016-07-27 16:37:21 +0200 | [diff] [blame] | 1055 | dev_err(dev, "invalid arpc response id received: %u\n", |
| 1056 | le16_to_cpu(resp->id)); |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1057 | spin_unlock_irqrestore(&es2->arpc_lock, flags); |
| 1058 | goto exit; |
| 1059 | } |
| 1060 | |
| 1061 | arpc_del(es2, rpc); |
| 1062 | memcpy(rpc->resp, resp, sizeof(*resp)); |
| 1063 | complete(&rpc->response_received); |
| 1064 | spin_unlock_irqrestore(&es2->arpc_lock, flags); |
| 1065 | |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1066 | exit: |
| 1067 | /* put our urb back in the request pool */ |
| 1068 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 1069 | if (retval) |
| 1070 | dev_err(dev, "failed to resubmit arpc in-urb: %d\n", retval); |
| 1071 | } |
| 1072 | |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 1073 | #define APB1_LOG_MSG_SIZE 64 |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1074 | static void apb_log_get(struct es2_ap_dev *es2, char *buf) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1075 | { |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1076 | int retval; |
| 1077 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1078 | do { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1079 | retval = usb_control_msg(es2->usb_dev, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 1080 | usb_rcvctrlpipe(es2->usb_dev, 0), |
| 1081 | GB_APB_REQUEST_LOG, |
| 1082 | USB_DIR_IN | USB_TYPE_VENDOR | |
| 1083 | USB_RECIP_INTERFACE, |
| 1084 | 0x00, 0x00, |
| 1085 | buf, |
| 1086 | APB1_LOG_MSG_SIZE, |
| 1087 | ES2_USB_CTRL_TIMEOUT); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1088 | if (retval > 0) |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1089 | kfifo_in(&es2->apb_log_fifo, buf, retval); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1090 | } while (retval > 0); |
| 1091 | } |
| 1092 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1093 | static int apb_log_poll(void *data) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1094 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1095 | struct es2_ap_dev *es2 = data; |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 1096 | char *buf; |
| 1097 | |
| 1098 | buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL); |
| 1099 | if (!buf) |
| 1100 | return -ENOMEM; |
| 1101 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1102 | while (!kthread_should_stop()) { |
| 1103 | msleep(1000); |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1104 | apb_log_get(es2, buf); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1105 | } |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 1106 | |
| 1107 | kfree(buf); |
| 1108 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1109 | return 0; |
| 1110 | } |
| 1111 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1112 | static ssize_t apb_log_read(struct file *f, char __user *buf, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 1113 | size_t count, loff_t *ppos) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1114 | { |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 1115 | struct es2_ap_dev *es2 = file_inode(f)->i_private; |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1116 | ssize_t ret; |
| 1117 | size_t copied; |
| 1118 | char *tmp_buf; |
| 1119 | |
| 1120 | if (count > APB1_LOG_SIZE) |
| 1121 | count = APB1_LOG_SIZE; |
| 1122 | |
| 1123 | tmp_buf = kmalloc(count, GFP_KERNEL); |
| 1124 | if (!tmp_buf) |
| 1125 | return -ENOMEM; |
| 1126 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1127 | copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1128 | ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied); |
| 1129 | |
| 1130 | kfree(tmp_buf); |
| 1131 | |
| 1132 | return ret; |
| 1133 | } |
| 1134 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1135 | static const struct file_operations apb_log_fops = { |
| 1136 | .read = apb_log_read, |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1137 | }; |
| 1138 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1139 | static void usb_log_enable(struct es2_ap_dev *es2) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1140 | { |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1141 | if (!IS_ERR_OR_NULL(es2->apb_log_task)) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1142 | return; |
| 1143 | |
| 1144 | /* get log from APB1 */ |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1145 | es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log"); |
| 1146 | if (IS_ERR(es2->apb_log_task)) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1147 | return; |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1148 | /* XXX We will need to rename this per APB */ |
Gioh Kim | 563a841 | 2017-02-09 17:30:12 +0100 | [diff] [blame] | 1149 | es2->apb_log_dentry = debugfs_create_file("apb_log", 0444, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 1150 | gb_debugfs_get(), es2, |
| 1151 | &apb_log_fops); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1152 | } |
| 1153 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1154 | static void usb_log_disable(struct es2_ap_dev *es2) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1155 | { |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1156 | if (IS_ERR_OR_NULL(es2->apb_log_task)) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1157 | return; |
| 1158 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1159 | debugfs_remove(es2->apb_log_dentry); |
| 1160 | es2->apb_log_dentry = NULL; |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1161 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1162 | kthread_stop(es2->apb_log_task); |
| 1163 | es2->apb_log_task = NULL; |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1164 | } |
| 1165 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1166 | static ssize_t apb_log_enable_read(struct file *f, char __user *buf, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 1167 | size_t count, loff_t *ppos) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1168 | { |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 1169 | struct es2_ap_dev *es2 = file_inode(f)->i_private; |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1170 | int enable = !IS_ERR_OR_NULL(es2->apb_log_task); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1171 | char tmp_buf[3]; |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1172 | |
| 1173 | sprintf(tmp_buf, "%d\n", enable); |
Rasmus Villemoes | 012ac58 | 2021-03-26 16:22:54 +0100 | [diff] [blame] | 1174 | return simple_read_from_buffer(buf, count, ppos, tmp_buf, 2); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1175 | } |
| 1176 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1177 | static ssize_t apb_log_enable_write(struct file *f, const char __user *buf, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 1178 | size_t count, loff_t *ppos) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1179 | { |
| 1180 | int enable; |
| 1181 | ssize_t retval; |
Al Viro | 4506309 | 2016-12-04 18:24:56 -0500 | [diff] [blame] | 1182 | struct es2_ap_dev *es2 = file_inode(f)->i_private; |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1183 | |
| 1184 | retval = kstrtoint_from_user(buf, count, 10, &enable); |
| 1185 | if (retval) |
| 1186 | return retval; |
| 1187 | |
| 1188 | if (enable) |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1189 | usb_log_enable(es2); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1190 | else |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1191 | usb_log_disable(es2); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1192 | |
| 1193 | return count; |
| 1194 | } |
| 1195 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1196 | static const struct file_operations apb_log_enable_fops = { |
| 1197 | .read = apb_log_enable_read, |
| 1198 | .write = apb_log_enable_write, |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 1199 | }; |
| 1200 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1201 | static int apb_get_cport_count(struct usb_device *udev) |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 1202 | { |
| 1203 | int retval; |
| 1204 | __le16 *cport_count; |
| 1205 | |
Johan Hovold | 478ce72 | 2016-02-17 19:30:40 +0100 | [diff] [blame] | 1206 | cport_count = kzalloc(sizeof(*cport_count), GFP_KERNEL); |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 1207 | if (!cport_count) |
| 1208 | return -ENOMEM; |
| 1209 | |
| 1210 | retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
Greg Kroah-Hartman | e527338 | 2015-12-31 11:14:33 -0800 | [diff] [blame] | 1211 | GB_APB_REQUEST_CPORT_COUNT, |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 1212 | USB_DIR_IN | USB_TYPE_VENDOR | |
| 1213 | USB_RECIP_INTERFACE, 0, 0, cport_count, |
Johan Hovold | 60793c9 | 2016-08-03 14:09:35 +0200 | [diff] [blame] | 1214 | sizeof(*cport_count), ES2_USB_CTRL_TIMEOUT); |
Johan Hovold | 478ce72 | 2016-02-17 19:30:40 +0100 | [diff] [blame] | 1215 | if (retval != sizeof(*cport_count)) { |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 1216 | dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n", |
| 1217 | retval); |
Johan Hovold | 478ce72 | 2016-02-17 19:30:40 +0100 | [diff] [blame] | 1218 | |
| 1219 | if (retval >= 0) |
| 1220 | retval = -EIO; |
| 1221 | |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 1222 | goto out; |
| 1223 | } |
| 1224 | |
| 1225 | retval = le16_to_cpu(*cport_count); |
| 1226 | |
| 1227 | /* We need to fit a CPort ID in one byte of a message header */ |
| 1228 | if (retval > U8_MAX) { |
| 1229 | retval = U8_MAX; |
| 1230 | dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n"); |
| 1231 | } |
| 1232 | |
| 1233 | out: |
| 1234 | kfree(cport_count); |
| 1235 | return retval; |
| 1236 | } |
| 1237 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1238 | /* |
Johan Hovold | 4d5c446 | 2015-11-02 11:56:57 +0100 | [diff] [blame] | 1239 | * The ES2 USB Bridge device has 15 endpoints |
| 1240 | * 1 Control - usual USB stuff + AP -> APBridgeA messages |
| 1241 | * 7 Bulk IN - CPort data in |
| 1242 | * 7 Bulk OUT - CPort data out |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1243 | */ |
| 1244 | static int ap_probe(struct usb_interface *interface, |
| 1245 | const struct usb_device_id *id) |
| 1246 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1247 | struct es2_ap_dev *es2; |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 1248 | struct gb_host_device *hd; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1249 | struct usb_device *udev; |
| 1250 | struct usb_host_interface *iface_desc; |
| 1251 | struct usb_endpoint_descriptor *endpoint; |
Greg Kroah-Hartman | 2722910 | 2016-08-17 17:11:04 +0200 | [diff] [blame] | 1252 | __u8 ep_addr; |
Johan Hovold | be5d01f | 2016-05-11 10:18:00 +0200 | [diff] [blame] | 1253 | int retval; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1254 | int i; |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 1255 | int num_cports; |
Greg Kroah-Hartman | 403074b | 2016-08-17 16:27:49 +0200 | [diff] [blame] | 1256 | bool bulk_out_found = false; |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1257 | bool bulk_in_found = false; |
| 1258 | bool arpc_in_found = false; |
Alex Elder | 4bc1389 | 2015-06-13 11:02:11 -0500 | [diff] [blame] | 1259 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1260 | udev = usb_get_dev(interface_to_usbdev(interface)); |
| 1261 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1262 | num_cports = apb_get_cport_count(udev); |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 1263 | if (num_cports < 0) { |
| 1264 | usb_put_dev(udev); |
| 1265 | dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n", |
| 1266 | num_cports); |
| 1267 | return num_cports; |
| 1268 | } |
| 1269 | |
Johan Hovold | d6e139b | 2015-11-03 18:03:25 +0100 | [diff] [blame] | 1270 | hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 1271 | num_cports); |
Alex Elder | 8ea70fe | 2015-05-22 09:52:45 -0500 | [diff] [blame] | 1272 | if (IS_ERR(hd)) { |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1273 | usb_put_dev(udev); |
Alex Elder | 8ea70fe | 2015-05-22 09:52:45 -0500 | [diff] [blame] | 1274 | return PTR_ERR(hd); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1275 | } |
| 1276 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1277 | es2 = hd_to_es2(hd); |
| 1278 | es2->hd = hd; |
| 1279 | es2->usb_intf = interface; |
| 1280 | es2->usb_dev = udev; |
| 1281 | spin_lock_init(&es2->cport_out_urb_lock); |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1282 | INIT_KFIFO(es2->apb_log_fifo); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1283 | usb_set_intfdata(interface, es2); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1284 | |
Johan Hovold | 0506150 | 2016-05-11 10:18:01 +0200 | [diff] [blame] | 1285 | /* |
| 1286 | * Reserve the CDSI0 and CDSI1 CPorts so they won't be allocated |
| 1287 | * dynamically. |
| 1288 | */ |
| 1289 | retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI0); |
| 1290 | if (retval) |
| 1291 | goto error; |
| 1292 | retval = gb_hd_cport_reserve(hd, ES2_CPORT_CDSI1); |
| 1293 | if (retval) |
| 1294 | goto error; |
| 1295 | |
Johan Hovold | 4d5c446 | 2015-11-02 11:56:57 +0100 | [diff] [blame] | 1296 | /* find all bulk endpoints */ |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1297 | iface_desc = interface->cur_altsetting; |
| 1298 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 1299 | endpoint = &iface_desc->endpoint[i].desc; |
Greg Kroah-Hartman | 2722910 | 2016-08-17 17:11:04 +0200 | [diff] [blame] | 1300 | ep_addr = endpoint->bEndpointAddress; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1301 | |
Greg Kroah-Hartman | b767ee4 | 2015-07-24 17:09:48 -0700 | [diff] [blame] | 1302 | if (usb_endpoint_is_bulk_in(endpoint)) { |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1303 | if (!bulk_in_found) { |
Greg Kroah-Hartman | 2722910 | 2016-08-17 17:11:04 +0200 | [diff] [blame] | 1304 | es2->cport_in.endpoint = ep_addr; |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1305 | bulk_in_found = true; |
| 1306 | } else if (!arpc_in_found) { |
Greg Kroah-Hartman | 2722910 | 2016-08-17 17:11:04 +0200 | [diff] [blame] | 1307 | es2->arpc_endpoint_in = ep_addr; |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1308 | arpc_in_found = true; |
Greg Kroah-Hartman | 2722910 | 2016-08-17 17:11:04 +0200 | [diff] [blame] | 1309 | } else { |
| 1310 | dev_warn(&udev->dev, |
| 1311 | "Unused bulk IN endpoint found: 0x%02x\n", |
| 1312 | ep_addr); |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1313 | } |
Greg Kroah-Hartman | 2722910 | 2016-08-17 17:11:04 +0200 | [diff] [blame] | 1314 | continue; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1315 | } |
Greg Kroah-Hartman | 2722910 | 2016-08-17 17:11:04 +0200 | [diff] [blame] | 1316 | if (usb_endpoint_is_bulk_out(endpoint)) { |
| 1317 | if (!bulk_out_found) { |
| 1318 | es2->cport_out_endpoint = ep_addr; |
| 1319 | bulk_out_found = true; |
| 1320 | } else { |
| 1321 | dev_warn(&udev->dev, |
| 1322 | "Unused bulk OUT endpoint found: 0x%02x\n", |
| 1323 | ep_addr); |
| 1324 | } |
| 1325 | continue; |
| 1326 | } |
| 1327 | dev_warn(&udev->dev, |
| 1328 | "Unknown endpoint type found, address 0x%02x\n", |
| 1329 | ep_addr); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1330 | } |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1331 | if (!bulk_in_found || !arpc_in_found || !bulk_out_found) { |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1332 | dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n"); |
Johan Hovold | be5d01f | 2016-05-11 10:18:00 +0200 | [diff] [blame] | 1333 | retval = -ENODEV; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1334 | goto error; |
| 1335 | } |
| 1336 | |
Johan Hovold | 0ce68ce4 | 2015-11-04 18:55:14 +0100 | [diff] [blame] | 1337 | /* Allocate buffers for our cport in messages */ |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1338 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
| 1339 | struct urb *urb; |
| 1340 | u8 *buffer; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1341 | |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1342 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1343 | if (!urb) { |
| 1344 | retval = -ENOMEM; |
| 1345 | goto error; |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 1346 | } |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1347 | es2->cport_in.urb[i] = urb; |
| 1348 | |
| 1349 | buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL); |
| 1350 | if (!buffer) { |
| 1351 | retval = -ENOMEM; |
| 1352 | goto error; |
| 1353 | } |
| 1354 | |
| 1355 | usb_fill_bulk_urb(urb, udev, |
| 1356 | usb_rcvbulkpipe(udev, es2->cport_in.endpoint), |
| 1357 | buffer, ES2_GBUF_MSG_SIZE_MAX, |
| 1358 | cport_in_callback, hd); |
| 1359 | |
| 1360 | es2->cport_in.buffer[i] = buffer; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1361 | } |
| 1362 | |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1363 | /* Allocate buffers for ARPC in messages */ |
| 1364 | for (i = 0; i < NUM_ARPC_IN_URB; ++i) { |
| 1365 | struct urb *urb; |
| 1366 | u8 *buffer; |
| 1367 | |
| 1368 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 1369 | if (!urb) { |
| 1370 | retval = -ENOMEM; |
| 1371 | goto error; |
| 1372 | } |
Johan Hovold | 5f62eab | 2016-08-17 15:43:32 +0200 | [diff] [blame] | 1373 | es2->arpc_urb[i] = urb; |
| 1374 | |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1375 | buffer = kmalloc(ARPC_IN_SIZE_MAX, GFP_KERNEL); |
| 1376 | if (!buffer) { |
| 1377 | retval = -ENOMEM; |
| 1378 | goto error; |
| 1379 | } |
| 1380 | |
| 1381 | usb_fill_bulk_urb(urb, udev, |
| 1382 | usb_rcvbulkpipe(udev, |
| 1383 | es2->arpc_endpoint_in), |
| 1384 | buffer, ARPC_IN_SIZE_MAX, |
| 1385 | arpc_in_callback, es2); |
| 1386 | |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1387 | es2->arpc_buffer[i] = buffer; |
| 1388 | } |
| 1389 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1390 | /* Allocate urbs for our CPort OUT messages */ |
| 1391 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
| 1392 | struct urb *urb; |
| 1393 | |
| 1394 | urb = usb_alloc_urb(0, GFP_KERNEL); |
Johan Hovold | be5d01f | 2016-05-11 10:18:00 +0200 | [diff] [blame] | 1395 | if (!urb) { |
| 1396 | retval = -ENOMEM; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1397 | goto error; |
Johan Hovold | be5d01f | 2016-05-11 10:18:00 +0200 | [diff] [blame] | 1398 | } |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1399 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1400 | es2->cport_out_urb[i] = urb; |
| 1401 | es2->cport_out_urb_busy[i] = false; /* just to be anal */ |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1402 | } |
| 1403 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 1404 | /* XXX We will need to rename this per APB */ |
| 1405 | es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable", |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 1406 | 0644, |
| 1407 | gb_debugfs_get(), es2, |
| 1408 | &apb_log_enable_fops); |
Johan Hovold | 0ce68ce4 | 2015-11-04 18:55:14 +0100 | [diff] [blame] | 1409 | |
Alexandre Bailon | c14118a | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1410 | INIT_LIST_HEAD(&es2->arpcs); |
| 1411 | spin_lock_init(&es2->arpc_lock); |
| 1412 | |
Wei Yongjun | 1305f2b | 2016-10-19 13:17:53 +0000 | [diff] [blame] | 1413 | retval = es2_arpc_in_enable(es2); |
| 1414 | if (retval) |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1415 | goto error; |
| 1416 | |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 1417 | retval = gb_hd_add(hd); |
| 1418 | if (retval) |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1419 | goto err_disable_arpc_in; |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 1420 | |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1421 | retval = es2_cport_in_enable(es2, &es2->cport_in); |
| 1422 | if (retval) |
| 1423 | goto err_hd_del; |
Johan Hovold | 0ce68ce4 | 2015-11-04 18:55:14 +0100 | [diff] [blame] | 1424 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1425 | return 0; |
Johan Hovold | 57bc17f | 2015-11-04 18:55:20 +0100 | [diff] [blame] | 1426 | |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1427 | err_hd_del: |
Johan Hovold | c170047 | 2015-11-04 18:55:22 +0100 | [diff] [blame] | 1428 | gb_hd_del(hd); |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1429 | err_disable_arpc_in: |
| 1430 | es2_arpc_in_disable(es2); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1431 | error: |
Johan Hovold | 57bc17f | 2015-11-04 18:55:20 +0100 | [diff] [blame] | 1432 | es2_destroy(es2); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1433 | |
| 1434 | return retval; |
| 1435 | } |
| 1436 | |
Viresh Kumar | 2437f1c | 2016-04-21 11:31:16 +0530 | [diff] [blame] | 1437 | static void ap_disconnect(struct usb_interface *interface) |
| 1438 | { |
| 1439 | struct es2_ap_dev *es2 = usb_get_intfdata(interface); |
Viresh Kumar | 2437f1c | 2016-04-21 11:31:16 +0530 | [diff] [blame] | 1440 | |
| 1441 | gb_hd_del(es2->hd); |
| 1442 | |
Greg Kroah-Hartman | 7330c48 | 2016-08-17 16:37:39 +0200 | [diff] [blame] | 1443 | es2_cport_in_disable(es2, &es2->cport_in); |
Alexandre Bailon | 9d9d377 | 2016-07-07 07:41:00 -0500 | [diff] [blame] | 1444 | es2_arpc_in_disable(es2); |
Viresh Kumar | 2437f1c | 2016-04-21 11:31:16 +0530 | [diff] [blame] | 1445 | |
| 1446 | es2_destroy(es2); |
| 1447 | } |
| 1448 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1449 | static struct usb_driver es2_ap_driver = { |
Rob Herring | c13c8bf | 2015-05-05 11:04:22 -0500 | [diff] [blame] | 1450 | .name = "es2_ap_driver", |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1451 | .probe = ap_probe, |
| 1452 | .disconnect = ap_disconnect, |
| 1453 | .id_table = id_table, |
Viresh Kumar | a0f997b | 2016-04-21 11:31:13 +0530 | [diff] [blame] | 1454 | .soft_unbind = 1, |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1455 | }; |
| 1456 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 1457 | module_usb_driver(es2_ap_driver); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1458 | |
Greg Kroah-Hartman | 6cf42a4 | 2015-04-13 19:51:33 +0200 | [diff] [blame] | 1459 | MODULE_LICENSE("GPL v2"); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1460 | MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>"); |