Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Greybus "AP" USB driver for "ES2" controller chips |
| 3 | * |
Alex Elder | 142f8dd | 2015-03-26 21:25:06 -0500 | [diff] [blame] | 4 | * Copyright 2014-2015 Google Inc. |
| 5 | * Copyright 2014-2015 Linaro Ltd. |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 6 | * |
| 7 | * Released under the GPLv2 only. |
| 8 | */ |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 9 | #include <linux/kthread.h> |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 10 | #include <linux/sizes.h> |
| 11 | #include <linux/usb.h> |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 12 | #include <linux/kfifo.h> |
| 13 | #include <linux/debugfs.h> |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 14 | #include <asm/unaligned.h> |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 15 | |
| 16 | #include "greybus.h" |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 17 | #include "kernel_ver.h" |
Bryan O'Donoghue | 3f2a809 | 2015-08-11 13:50:52 +0100 | [diff] [blame] | 18 | #include "connection.h" |
Bryan O'Donoghue | 6872c46 | 2015-09-22 18:06:39 -0700 | [diff] [blame] | 19 | #include "greybus_trace.h" |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 20 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 21 | /* Memory sizes for the buffers sent to/from the ES2 controller */ |
| 22 | #define ES2_GBUF_MSG_SIZE_MAX 2048 |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 23 | |
| 24 | static const struct usb_device_id id_table[] = { |
Greg Kroah-Hartman | 8f0a654 | 2015-10-22 16:40:41 -0700 | [diff] [blame] | 25 | { USB_DEVICE(0xffff, 0x0002) }, /* Made up number, delete once firmware is fixed to use real number */ |
| 26 | { USB_DEVICE(0x18d1, 0x1eaf) }, |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 27 | { }, |
| 28 | }; |
| 29 | MODULE_DEVICE_TABLE(usb, id_table); |
| 30 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 31 | #define APB1_LOG_SIZE SZ_16K |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 32 | |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 33 | /* Number of bulk in and bulk out couple */ |
| 34 | #define NUM_BULKS 7 |
| 35 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 36 | /* |
| 37 | * Number of CPort IN urbs in flight at any point in time. |
| 38 | * Adjust if we are having stalls in the USB buffer due to not enough urbs in |
| 39 | * flight. |
| 40 | */ |
| 41 | #define NUM_CPORT_IN_URB 4 |
| 42 | |
| 43 | /* Number of CPort OUT urbs in flight at any point in time. |
| 44 | * Adjust if we get messages saying we are out of urbs in the system log. |
| 45 | */ |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 46 | #define NUM_CPORT_OUT_URB (8 * NUM_BULKS) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 47 | |
Alexandre Bailon | 611c1739 | 2015-06-15 18:08:11 +0200 | [diff] [blame] | 48 | /* vendor request APB1 log */ |
| 49 | #define REQUEST_LOG 0x02 |
| 50 | |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 51 | /* vendor request to map a cport to bulk in and bulk out endpoints */ |
| 52 | #define REQUEST_EP_MAPPING 0x03 |
| 53 | |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 54 | /* vendor request to get the number of cports available */ |
| 55 | #define REQUEST_CPORT_COUNT 0x04 |
| 56 | |
Fabien Parent | 82ee1e6 | 2015-10-13 17:34:50 +0200 | [diff] [blame] | 57 | /* vendor request to reset a cport state */ |
| 58 | #define REQUEST_RESET_CPORT 0x05 |
| 59 | |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 60 | /* vendor request to time the latency of messages on a given cport */ |
| 61 | #define REQUEST_LATENCY_TAG_EN 0x06 |
| 62 | #define REQUEST_LATENCY_TAG_DIS 0x07 |
| 63 | |
Alexandre Bailon | ddc09ac | 2015-06-15 18:08:12 +0200 | [diff] [blame] | 64 | /* |
| 65 | * @endpoint: bulk in endpoint for CPort data |
| 66 | * @urb: array of urbs for the CPort in messages |
| 67 | * @buffer: array of buffers for the @cport_in_urb urbs |
| 68 | */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 69 | struct es2_cport_in { |
Alexandre Bailon | ddc09ac | 2015-06-15 18:08:12 +0200 | [diff] [blame] | 70 | __u8 endpoint; |
| 71 | struct urb *urb[NUM_CPORT_IN_URB]; |
| 72 | u8 *buffer[NUM_CPORT_IN_URB]; |
| 73 | }; |
| 74 | |
| 75 | /* |
| 76 | * @endpoint: bulk out endpoint for CPort data |
| 77 | */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 78 | struct es2_cport_out { |
Alexandre Bailon | ddc09ac | 2015-06-15 18:08:12 +0200 | [diff] [blame] | 79 | __u8 endpoint; |
| 80 | }; |
| 81 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 82 | /** |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 83 | * es2_ap_dev - ES2 USB Bridge to AP structure |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 84 | * @usb_dev: pointer to the USB device we are. |
| 85 | * @usb_intf: pointer to the USB interface we are bound to. |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 86 | * @hd: pointer to our gb_host_device structure |
Alexandre Bailon | ddc09ac | 2015-06-15 18:08:12 +0200 | [diff] [blame] | 87 | |
Alexandre Bailon | ddc09ac | 2015-06-15 18:08:12 +0200 | [diff] [blame] | 88 | * @cport_in: endpoint, urbs and buffer for cport in messages |
| 89 | * @cport_out: endpoint for for cport out messages |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 90 | * @cport_out_urb: array of urbs for the CPort out messages |
| 91 | * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or |
| 92 | * not. |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 93 | * @cport_out_urb_cancelled: array of flags indicating whether the |
| 94 | * corresponding @cport_out_urb is being cancelled |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 95 | * @cport_out_urb_lock: locks the @cport_out_urb_busy "list" |
Alex Elder | 1482b3e | 2015-10-27 22:18:38 -0500 | [diff] [blame] | 96 | * |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 97 | * @apb_log_task: task pointer for logging thread |
| 98 | * @apb_log_dentry: file system entry for the log file interface |
| 99 | * @apb_log_enable_dentry: file system entry for enabling logging |
| 100 | * @apb_log_fifo: kernel FIFO to carry logged data |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 101 | */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 102 | struct es2_ap_dev { |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 103 | struct usb_device *usb_dev; |
| 104 | struct usb_interface *usb_intf; |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 105 | struct gb_host_device *hd; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 106 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 107 | struct es2_cport_in cport_in[NUM_BULKS]; |
| 108 | struct es2_cport_out cport_out[NUM_BULKS]; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 109 | struct urb *cport_out_urb[NUM_CPORT_OUT_URB]; |
| 110 | bool cport_out_urb_busy[NUM_CPORT_OUT_URB]; |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 111 | bool cport_out_urb_cancelled[NUM_CPORT_OUT_URB]; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 112 | spinlock_t cport_out_urb_lock; |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 113 | |
Fabien Parent | c011d55 | 2015-09-02 15:50:36 +0200 | [diff] [blame] | 114 | int *cport_to_ep; |
Alex Elder | 1482b3e | 2015-10-27 22:18:38 -0500 | [diff] [blame] | 115 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 116 | struct task_struct *apb_log_task; |
| 117 | struct dentry *apb_log_dentry; |
| 118 | struct dentry *apb_log_enable_dentry; |
| 119 | DECLARE_KFIFO(apb_log_fifo, char, APB1_LOG_SIZE); |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 120 | }; |
| 121 | |
Alexandre Bailon | e5acf73 | 2015-09-14 18:20:44 +0200 | [diff] [blame] | 122 | /** |
| 123 | * cport_to_ep - information about cport to endpoints mapping |
| 124 | * @cport_id: the id of cport to map to endpoints |
| 125 | * @endpoint_in: the endpoint number to use for in transfer |
| 126 | * @endpoint_out: he endpoint number to use for out transfer |
| 127 | */ |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 128 | struct cport_to_ep { |
| 129 | __le16 cport_id; |
| 130 | __u8 endpoint_in; |
| 131 | __u8 endpoint_out; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 132 | }; |
| 133 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 134 | 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] | 135 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 136 | return (struct es2_ap_dev *)&hd->hd_priv; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static void cport_out_callback(struct urb *urb); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 140 | static void usb_log_enable(struct es2_ap_dev *es2); |
| 141 | static void usb_log_disable(struct es2_ap_dev *es2); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 142 | |
Alexandre Bailon | e5acf73 | 2015-09-14 18:20:44 +0200 | [diff] [blame] | 143 | /* Get the endpoints pair mapped to the cport */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 144 | static int cport_to_ep_pair(struct es2_ap_dev *es2, u16 cport_id) |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 145 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 146 | if (cport_id >= es2->hd->num_cports) |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 147 | return 0; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 148 | return es2->cport_to_ep[cport_id]; |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 149 | } |
| 150 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 151 | #define ES2_TIMEOUT 500 /* 500 ms for the SVC to do something */ |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 152 | |
Greg Kroah-Hartman | 608f661 | 2015-09-22 09:42:52 -0700 | [diff] [blame] | 153 | /* Disable for now until we work all of this out to keep a warning-free build */ |
| 154 | #if 0 |
Alexandre Bailon | e5acf73 | 2015-09-14 18:20:44 +0200 | [diff] [blame] | 155 | /* Test if the endpoints pair is already mapped to a cport */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 156 | static int ep_pair_in_use(struct es2_ap_dev *es2, int ep_pair) |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 157 | { |
| 158 | int i; |
| 159 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 160 | for (i = 0; i < es2->hd->num_cports; i++) { |
| 161 | if (es2->cport_to_ep[i] == ep_pair) |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 162 | return 1; |
| 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
Alexandre Bailon | e5acf73 | 2015-09-14 18:20:44 +0200 | [diff] [blame] | 167 | /* Configure the endpoint mapping and send the request to APBridge */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 168 | static int map_cport_to_ep(struct es2_ap_dev *es2, |
Alexandre Bailon | 1ff3dc9 | 2015-09-14 18:20:42 +0200 | [diff] [blame] | 169 | u16 cport_id, int ep_pair) |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 170 | { |
| 171 | int retval; |
| 172 | struct cport_to_ep *cport_to_ep; |
| 173 | |
Alexandre Bailon | 1ff3dc9 | 2015-09-14 18:20:42 +0200 | [diff] [blame] | 174 | if (ep_pair < 0 || ep_pair >= NUM_BULKS) |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 175 | return -EINVAL; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 176 | if (cport_id >= es2->hd->num_cports) |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 177 | return -EINVAL; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 178 | if (ep_pair && ep_pair_in_use(es2, ep_pair)) |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 179 | return -EINVAL; |
| 180 | |
| 181 | cport_to_ep = kmalloc(sizeof(*cport_to_ep), GFP_KERNEL); |
| 182 | if (!cport_to_ep) |
| 183 | return -ENOMEM; |
| 184 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 185 | es2->cport_to_ep[cport_id] = ep_pair; |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 186 | cport_to_ep->cport_id = cpu_to_le16(cport_id); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 187 | cport_to_ep->endpoint_in = es2->cport_in[ep_pair].endpoint; |
| 188 | cport_to_ep->endpoint_out = es2->cport_out[ep_pair].endpoint; |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 189 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 190 | retval = usb_control_msg(es2->usb_dev, |
| 191 | usb_sndctrlpipe(es2->usb_dev, 0), |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 192 | REQUEST_EP_MAPPING, |
| 193 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 194 | 0x00, 0x00, |
| 195 | (char *)cport_to_ep, |
| 196 | sizeof(*cport_to_ep), |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 197 | ES2_TIMEOUT); |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 198 | if (retval == sizeof(*cport_to_ep)) |
| 199 | retval = 0; |
| 200 | kfree(cport_to_ep); |
| 201 | |
| 202 | return retval; |
| 203 | } |
| 204 | |
Alexandre Bailon | e5acf73 | 2015-09-14 18:20:44 +0200 | [diff] [blame] | 205 | /* Unmap a cport: use the muxed endpoints pair */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 206 | static int unmap_cport(struct es2_ap_dev *es2, u16 cport_id) |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 207 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 208 | return map_cport_to_ep(es2, cport_id, 0); |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 209 | } |
Greg Kroah-Hartman | 608f661 | 2015-09-22 09:42:52 -0700 | [diff] [blame] | 210 | #endif |
Alexandre Bailon | fc1a536 | 2015-06-15 18:08:14 +0200 | [diff] [blame] | 211 | |
Johan Hovold | 0ce68ce4 | 2015-11-04 18:55:14 +0100 | [diff] [blame] | 212 | static int es2_cport_in_enable(struct es2_ap_dev *es2, |
| 213 | struct es2_cport_in *cport_in) |
| 214 | { |
| 215 | struct urb *urb; |
| 216 | int ret; |
| 217 | int i; |
| 218 | |
| 219 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
| 220 | urb = cport_in->urb[i]; |
| 221 | |
| 222 | ret = usb_submit_urb(urb, GFP_KERNEL); |
| 223 | if (ret) { |
| 224 | dev_err(&es2->usb_dev->dev, |
| 225 | "failed to submit in-urb: %d\n", ret); |
| 226 | goto err_kill_urbs; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | |
| 232 | err_kill_urbs: |
| 233 | for (--i; i >= 0; --i) { |
| 234 | urb = cport_in->urb[i]; |
| 235 | usb_kill_urb(urb); |
| 236 | } |
| 237 | |
| 238 | return ret; |
| 239 | } |
| 240 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 241 | 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] | 242 | { |
| 243 | struct urb *urb = NULL; |
| 244 | unsigned long flags; |
| 245 | int i; |
| 246 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 247 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 248 | |
| 249 | /* Look in our pool of allocated urbs first, as that's the "fastest" */ |
| 250 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 251 | if (es2->cport_out_urb_busy[i] == false && |
| 252 | es2->cport_out_urb_cancelled[i] == false) { |
| 253 | es2->cport_out_urb_busy[i] = true; |
| 254 | urb = es2->cport_out_urb[i]; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 255 | break; |
| 256 | } |
| 257 | } |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 258 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 259 | if (urb) |
| 260 | return urb; |
| 261 | |
| 262 | /* |
| 263 | * Crap, pool is empty, complain to the syslog and go allocate one |
| 264 | * dynamically as we have to succeed. |
| 265 | */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 266 | dev_err(&es2->usb_dev->dev, |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 267 | "No free CPort OUT urbs, having to dynamically allocate one!\n"); |
| 268 | return usb_alloc_urb(0, gfp_mask); |
| 269 | } |
| 270 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 271 | 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] | 272 | { |
| 273 | unsigned long flags; |
| 274 | int i; |
| 275 | /* |
| 276 | * See if this was an urb in our pool, if so mark it "free", otherwise |
| 277 | * we need to free it ourselves. |
| 278 | */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 279 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 280 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 281 | if (urb == es2->cport_out_urb[i]) { |
| 282 | es2->cport_out_urb_busy[i] = false; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 283 | urb = NULL; |
| 284 | break; |
| 285 | } |
| 286 | } |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 287 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 288 | |
| 289 | /* If urb is not NULL, then we need to free this urb */ |
| 290 | usb_free_urb(urb); |
| 291 | } |
| 292 | |
| 293 | /* |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 294 | * We (ab)use the operation-message header pad bytes to transfer the |
| 295 | * cport id in order to minimise overhead. |
| 296 | */ |
| 297 | static void |
| 298 | gb_message_cport_pack(struct gb_operation_msg_hdr *header, u16 cport_id) |
| 299 | { |
Alex Elder | 4bc1389 | 2015-06-13 11:02:11 -0500 | [diff] [blame] | 300 | header->pad[0] = cport_id; |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /* Clear the pad bytes used for the CPort id */ |
| 304 | static void gb_message_cport_clear(struct gb_operation_msg_hdr *header) |
| 305 | { |
Alex Elder | 4bc1389 | 2015-06-13 11:02:11 -0500 | [diff] [blame] | 306 | header->pad[0] = 0; |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /* Extract the CPort id packed into the header, and clear it */ |
| 310 | static u16 gb_message_cport_unpack(struct gb_operation_msg_hdr *header) |
| 311 | { |
Alex Elder | 4bc1389 | 2015-06-13 11:02:11 -0500 | [diff] [blame] | 312 | u16 cport_id = header->pad[0]; |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 313 | |
| 314 | gb_message_cport_clear(header); |
| 315 | |
| 316 | return cport_id; |
| 317 | } |
| 318 | |
| 319 | /* |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 320 | * Returns zero if the message was successfully queued, or a negative errno |
| 321 | * otherwise. |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 322 | */ |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 323 | static int message_send(struct gb_host_device *hd, u16 cport_id, |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 324 | struct gb_message *message, gfp_t gfp_mask) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 325 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 326 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 327 | struct usb_device *udev = es2->usb_dev; |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 328 | size_t buffer_size; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 329 | int retval; |
| 330 | struct urb *urb; |
Alexandre Bailon | 1ff3dc9 | 2015-09-14 18:20:42 +0200 | [diff] [blame] | 331 | int ep_pair; |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 332 | unsigned long flags; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 333 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 334 | /* |
| 335 | * The data actually transferred will include an indication |
| 336 | * of where the data should be sent. Do one last check of |
| 337 | * the target CPort id before filling it in. |
| 338 | */ |
Fabien Parent | 144670c | 2015-09-02 15:50:35 +0200 | [diff] [blame] | 339 | if (!cport_id_valid(hd, cport_id)) { |
Johan Hovold | 305a031 | 2015-10-13 19:10:20 +0200 | [diff] [blame] | 340 | dev_err(&udev->dev, "invalid destination cport 0x%02x\n", |
| 341 | cport_id); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 342 | return -EINVAL; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 343 | } |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 344 | |
| 345 | /* Find a free urb */ |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 346 | urb = next_free_urb(es2, gfp_mask); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 347 | if (!urb) |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 348 | return -ENOMEM; |
| 349 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 350 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 351 | message->hcpriv = urb; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 352 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 353 | |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 354 | /* Pack the cport id into the message header */ |
| 355 | gb_message_cport_pack(message->header, cport_id); |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 356 | |
Alex Elder | 821c620 | 2015-06-13 11:02:07 -0500 | [diff] [blame] | 357 | buffer_size = sizeof(*message->header) + message->payload_size; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 358 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 359 | ep_pair = cport_to_ep_pair(es2, cport_id); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 360 | usb_fill_bulk_urb(urb, udev, |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 361 | usb_sndbulkpipe(udev, |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 362 | es2->cport_out[ep_pair].endpoint), |
Alex Elder | 821c620 | 2015-06-13 11:02:07 -0500 | [diff] [blame] | 363 | message->buffer, buffer_size, |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 364 | cport_out_callback, message); |
Alexandre Bailon | 977e209 | 2015-08-31 09:00:16 +0200 | [diff] [blame] | 365 | urb->transfer_flags |= URB_ZERO_PACKET; |
Bryan O'Donoghue | 6872c46 | 2015-09-22 18:06:39 -0700 | [diff] [blame] | 366 | trace_gb_host_device_send(hd, cport_id, buffer_size); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 367 | retval = usb_submit_urb(urb, gfp_mask); |
| 368 | if (retval) { |
Johan Hovold | 305a031 | 2015-10-13 19:10:20 +0200 | [diff] [blame] | 369 | dev_err(&udev->dev, "failed to submit out-urb: %d\n", retval); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 370 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 371 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 372 | message->hcpriv = NULL; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 373 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 374 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 375 | free_urb(es2, urb); |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 376 | gb_message_cport_clear(message->header); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 377 | |
| 378 | return retval; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 379 | } |
| 380 | |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 381 | return 0; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 385 | * Can not be called in atomic context. |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 386 | */ |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 387 | static void message_cancel(struct gb_message *message) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 388 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 389 | struct gb_host_device *hd = message->operation->connection->hd; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 390 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 391 | struct urb *urb; |
| 392 | int i; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 393 | |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 394 | might_sleep(); |
| 395 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 396 | spin_lock_irq(&es2->cport_out_urb_lock); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 397 | urb = message->hcpriv; |
| 398 | |
| 399 | /* Prevent dynamically allocated urb from being deallocated. */ |
| 400 | usb_get_urb(urb); |
| 401 | |
| 402 | /* Prevent pre-allocated urb from being reused. */ |
| 403 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 404 | if (urb == es2->cport_out_urb[i]) { |
| 405 | es2->cport_out_urb_cancelled[i] = true; |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 406 | break; |
| 407 | } |
| 408 | } |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 409 | spin_unlock_irq(&es2->cport_out_urb_lock); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 410 | |
| 411 | usb_kill_urb(urb); |
| 412 | |
| 413 | if (i < NUM_CPORT_OUT_URB) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 414 | spin_lock_irq(&es2->cport_out_urb_lock); |
| 415 | es2->cport_out_urb_cancelled[i] = false; |
| 416 | spin_unlock_irq(&es2->cport_out_urb_lock); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | usb_free_urb(urb); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 420 | } |
| 421 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 422 | static int cport_reset(struct gb_host_device *hd, u16 cport_id) |
Fabien Parent | 82ee1e6 | 2015-10-13 17:34:50 +0200 | [diff] [blame] | 423 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 424 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 425 | struct usb_device *udev = es2->usb_dev; |
Fabien Parent | 82ee1e6 | 2015-10-13 17:34:50 +0200 | [diff] [blame] | 426 | int retval; |
| 427 | |
| 428 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 429 | REQUEST_RESET_CPORT, |
| 430 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 431 | USB_RECIP_INTERFACE, 0, cport_id, |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 432 | NULL, 0, ES2_TIMEOUT); |
Fabien Parent | 82ee1e6 | 2015-10-13 17:34:50 +0200 | [diff] [blame] | 433 | if (retval < 0) { |
| 434 | dev_err(&udev->dev, "failed to reset cport %hu: %d\n", cport_id, |
| 435 | retval); |
| 436 | return retval; |
| 437 | } |
| 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 442 | static int cport_enable(struct gb_host_device *hd, u16 cport_id) |
Fabien Parent | 82ee1e6 | 2015-10-13 17:34:50 +0200 | [diff] [blame] | 443 | { |
| 444 | int retval; |
| 445 | |
| 446 | if (cport_id != GB_SVC_CPORT_ID) { |
| 447 | retval = cport_reset(hd, cport_id); |
| 448 | if (retval) |
| 449 | return retval; |
| 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |
| 454 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 455 | 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] | 456 | { |
| 457 | int retval; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 458 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 459 | struct usb_device *udev = es2->usb_dev; |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 460 | |
| 461 | if (!cport_id_valid(hd, cport_id)) { |
| 462 | dev_err(&udev->dev, "invalid destination cport 0x%02x\n", |
| 463 | cport_id); |
| 464 | return -EINVAL; |
| 465 | } |
| 466 | |
| 467 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 468 | REQUEST_LATENCY_TAG_EN, |
| 469 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 470 | USB_RECIP_INTERFACE, cport_id, 0, NULL, |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 471 | 0, ES2_TIMEOUT); |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 472 | |
| 473 | if (retval < 0) |
| 474 | dev_err(&udev->dev, "Cannot enable latency tag for cport %d\n", |
| 475 | cport_id); |
| 476 | return retval; |
| 477 | } |
| 478 | |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 479 | 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] | 480 | { |
| 481 | int retval; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 482 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
| 483 | struct usb_device *udev = es2->usb_dev; |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 484 | |
| 485 | if (!cport_id_valid(hd, cport_id)) { |
| 486 | dev_err(&udev->dev, "invalid destination cport 0x%02x\n", |
| 487 | cport_id); |
| 488 | return -EINVAL; |
| 489 | } |
| 490 | |
| 491 | retval = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), |
| 492 | REQUEST_LATENCY_TAG_DIS, |
| 493 | USB_DIR_OUT | USB_TYPE_VENDOR | |
| 494 | USB_RECIP_INTERFACE, cport_id, 0, NULL, |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 495 | 0, ES2_TIMEOUT); |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 496 | |
| 497 | if (retval < 0) |
| 498 | dev_err(&udev->dev, "Cannot disable latency tag for cport %d\n", |
| 499 | cport_id); |
| 500 | return retval; |
| 501 | } |
| 502 | |
Johan Hovold | a8cc020 | 2015-11-03 18:03:24 +0100 | [diff] [blame] | 503 | static struct gb_hd_driver es2_driver = { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 504 | .hd_priv_size = sizeof(struct es2_ap_dev), |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 505 | .message_send = message_send, |
| 506 | .message_cancel = message_cancel, |
Fabien Parent | 82ee1e6 | 2015-10-13 17:34:50 +0200 | [diff] [blame] | 507 | .cport_enable = cport_enable, |
Bryan O'Donoghue | 608ab2f | 2015-10-15 16:10:41 +0100 | [diff] [blame] | 508 | .latency_tag_enable = latency_tag_enable, |
| 509 | .latency_tag_disable = latency_tag_disable, |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 510 | }; |
| 511 | |
| 512 | /* Common function to report consistent warnings based on URB status */ |
| 513 | static int check_urb_status(struct urb *urb) |
| 514 | { |
| 515 | struct device *dev = &urb->dev->dev; |
| 516 | int status = urb->status; |
| 517 | |
| 518 | switch (status) { |
| 519 | case 0: |
| 520 | return 0; |
| 521 | |
| 522 | case -EOVERFLOW: |
| 523 | dev_err(dev, "%s: overflow actual length is %d\n", |
| 524 | __func__, urb->actual_length); |
| 525 | case -ECONNRESET: |
| 526 | case -ENOENT: |
| 527 | case -ESHUTDOWN: |
| 528 | case -EILSEQ: |
| 529 | case -EPROTO: |
| 530 | /* device is gone, stop sending */ |
| 531 | return status; |
| 532 | } |
| 533 | dev_err(dev, "%s: unknown status %d\n", __func__, status); |
| 534 | |
| 535 | return -EAGAIN; |
| 536 | } |
| 537 | |
| 538 | static void ap_disconnect(struct usb_interface *interface) |
| 539 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 540 | struct es2_ap_dev *es2; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 541 | struct usb_device *udev; |
Johan Hovold | a51e8551 | 2015-11-04 18:55:12 +0100 | [diff] [blame] | 542 | int *cport_to_ep; |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 543 | int bulk_in; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 544 | int i; |
| 545 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 546 | es2 = usb_get_intfdata(interface); |
| 547 | if (!es2) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 548 | return; |
| 549 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 550 | usb_log_disable(es2); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 551 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 552 | /* Tear down everything! */ |
| 553 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 554 | struct urb *urb = es2->cport_out_urb[i]; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 555 | |
| 556 | if (!urb) |
| 557 | break; |
| 558 | usb_kill_urb(urb); |
| 559 | usb_free_urb(urb); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 560 | es2->cport_out_urb[i] = NULL; |
| 561 | es2->cport_out_urb_busy[i] = false; /* just to be anal */ |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 562 | } |
| 563 | |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 564 | for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 565 | struct es2_cport_in *cport_in = &es2->cport_in[bulk_in]; |
| 566 | |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 567 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
| 568 | struct urb *urb = cport_in->urb[i]; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 569 | |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 570 | if (!urb) |
| 571 | break; |
| 572 | usb_kill_urb(urb); |
| 573 | usb_free_urb(urb); |
| 574 | kfree(cport_in->buffer[i]); |
| 575 | cport_in->buffer[i] = NULL; |
| 576 | } |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 577 | } |
| 578 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 579 | usb_set_intfdata(interface, NULL); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 580 | udev = es2->usb_dev; |
Johan Hovold | a51e8551 | 2015-11-04 18:55:12 +0100 | [diff] [blame] | 581 | cport_to_ep = es2->cport_to_ep; |
Johan Hovold | d6e139b | 2015-11-03 18:03:25 +0100 | [diff] [blame] | 582 | gb_hd_remove(es2->hd); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 583 | |
Johan Hovold | a51e8551 | 2015-11-04 18:55:12 +0100 | [diff] [blame] | 584 | kfree(cport_to_ep); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 585 | usb_put_dev(udev); |
| 586 | } |
| 587 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 588 | static void cport_in_callback(struct urb *urb) |
| 589 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 590 | struct gb_host_device *hd = urb->context; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 591 | struct device *dev = &urb->dev->dev; |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 592 | struct gb_operation_msg_hdr *header; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 593 | int status = check_urb_status(urb); |
| 594 | int retval; |
| 595 | u16 cport_id; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 596 | |
| 597 | if (status) { |
| 598 | if ((status == -EAGAIN) || (status == -EPROTO)) |
| 599 | goto exit; |
| 600 | dev_err(dev, "urb cport in error %d (dropped)\n", status); |
| 601 | return; |
| 602 | } |
| 603 | |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 604 | if (urb->actual_length < sizeof(*header)) { |
Johan Hovold | 305a031 | 2015-10-13 19:10:20 +0200 | [diff] [blame] | 605 | dev_err(dev, "short message received\n"); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 606 | goto exit; |
| 607 | } |
| 608 | |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 609 | /* Extract the CPort id, which is packed in the message header */ |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 610 | header = urb->transfer_buffer; |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 611 | cport_id = gb_message_cport_unpack(header); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 612 | |
Bryan O'Donoghue | 6872c46 | 2015-09-22 18:06:39 -0700 | [diff] [blame] | 613 | if (cport_id_valid(hd, cport_id)) { |
| 614 | trace_gb_host_device_recv(hd, cport_id, urb->actual_length); |
Alex Elder | 821c620 | 2015-06-13 11:02:07 -0500 | [diff] [blame] | 615 | greybus_data_rcvd(hd, cport_id, urb->transfer_buffer, |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 616 | urb->actual_length); |
Bryan O'Donoghue | 6872c46 | 2015-09-22 18:06:39 -0700 | [diff] [blame] | 617 | } else { |
Johan Hovold | 305a031 | 2015-10-13 19:10:20 +0200 | [diff] [blame] | 618 | dev_err(dev, "invalid cport id 0x%02x received\n", cport_id); |
Bryan O'Donoghue | 6872c46 | 2015-09-22 18:06:39 -0700 | [diff] [blame] | 619 | } |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 620 | exit: |
| 621 | /* put our urb back in the request pool */ |
| 622 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 623 | if (retval) |
Johan Hovold | 305a031 | 2015-10-13 19:10:20 +0200 | [diff] [blame] | 624 | dev_err(dev, "failed to resubmit in-urb: %d\n", retval); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | static void cport_out_callback(struct urb *urb) |
| 628 | { |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 629 | struct gb_message *message = urb->context; |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 630 | struct gb_host_device *hd = message->operation->connection->hd; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 631 | struct es2_ap_dev *es2 = hd_to_es2(hd); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 632 | int status = check_urb_status(urb); |
Johan Hovold | 3e136cc | 2015-07-01 12:37:21 +0200 | [diff] [blame] | 633 | unsigned long flags; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 634 | |
Alex Elder | d29b3d6 | 2015-06-13 11:02:08 -0500 | [diff] [blame] | 635 | gb_message_cport_clear(message->header); |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 636 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 637 | spin_lock_irqsave(&es2->cport_out_urb_lock, flags); |
Johan Hovold | 58c8512 | 2015-09-26 14:37:59 -0700 | [diff] [blame] | 638 | message->hcpriv = NULL; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 639 | spin_unlock_irqrestore(&es2->cport_out_urb_lock, flags); |
Johan Hovold | 58c8512 | 2015-09-26 14:37:59 -0700 | [diff] [blame] | 640 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 641 | /* |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 642 | * Tell the submitter that the message send (attempt) is |
| 643 | * complete, and report the status. |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 644 | */ |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 645 | greybus_message_sent(hd, message, status); |
| 646 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 647 | free_urb(es2, urb); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 648 | } |
| 649 | |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 650 | #define APB1_LOG_MSG_SIZE 64 |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 651 | 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] | 652 | { |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 653 | int retval; |
| 654 | |
| 655 | /* SVC messages go down our control pipe */ |
| 656 | do { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 657 | retval = usb_control_msg(es2->usb_dev, |
| 658 | usb_rcvctrlpipe(es2->usb_dev, 0), |
Alexandre Bailon | 611c1739 | 2015-06-15 18:08:11 +0200 | [diff] [blame] | 659 | REQUEST_LOG, |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 660 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 661 | 0x00, 0x00, |
| 662 | buf, |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 663 | APB1_LOG_MSG_SIZE, |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 664 | ES2_TIMEOUT); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 665 | if (retval > 0) |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 666 | kfifo_in(&es2->apb_log_fifo, buf, retval); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 667 | } while (retval > 0); |
| 668 | } |
| 669 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 670 | static int apb_log_poll(void *data) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 671 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 672 | struct es2_ap_dev *es2 = data; |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 673 | char *buf; |
| 674 | |
| 675 | buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL); |
| 676 | if (!buf) |
| 677 | return -ENOMEM; |
| 678 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 679 | while (!kthread_should_stop()) { |
| 680 | msleep(1000); |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 681 | apb_log_get(es2, buf); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 682 | } |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 683 | |
| 684 | kfree(buf); |
| 685 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 686 | return 0; |
| 687 | } |
| 688 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 689 | static ssize_t apb_log_read(struct file *f, char __user *buf, |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 690 | size_t count, loff_t *ppos) |
| 691 | { |
Alex Elder | 8995a39 | 2015-10-27 22:18:39 -0500 | [diff] [blame] | 692 | struct es2_ap_dev *es2 = f->f_inode->i_private; |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 693 | ssize_t ret; |
| 694 | size_t copied; |
| 695 | char *tmp_buf; |
| 696 | |
| 697 | if (count > APB1_LOG_SIZE) |
| 698 | count = APB1_LOG_SIZE; |
| 699 | |
| 700 | tmp_buf = kmalloc(count, GFP_KERNEL); |
| 701 | if (!tmp_buf) |
| 702 | return -ENOMEM; |
| 703 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 704 | copied = kfifo_out(&es2->apb_log_fifo, tmp_buf, count); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 705 | ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied); |
| 706 | |
| 707 | kfree(tmp_buf); |
| 708 | |
| 709 | return ret; |
| 710 | } |
| 711 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 712 | static const struct file_operations apb_log_fops = { |
| 713 | .read = apb_log_read, |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 714 | }; |
| 715 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 716 | static void usb_log_enable(struct es2_ap_dev *es2) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 717 | { |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 718 | if (!IS_ERR_OR_NULL(es2->apb_log_task)) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 719 | return; |
| 720 | |
| 721 | /* get log from APB1 */ |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 722 | es2->apb_log_task = kthread_run(apb_log_poll, es2, "apb_log"); |
| 723 | if (IS_ERR(es2->apb_log_task)) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 724 | return; |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 725 | /* XXX We will need to rename this per APB */ |
| 726 | es2->apb_log_dentry = debugfs_create_file("apb_log", S_IRUGO, |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 727 | gb_debugfs_get(), NULL, |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 728 | &apb_log_fops); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 729 | } |
| 730 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 731 | static void usb_log_disable(struct es2_ap_dev *es2) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 732 | { |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 733 | if (IS_ERR_OR_NULL(es2->apb_log_task)) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 734 | return; |
| 735 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 736 | debugfs_remove(es2->apb_log_dentry); |
| 737 | es2->apb_log_dentry = NULL; |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 738 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 739 | kthread_stop(es2->apb_log_task); |
| 740 | es2->apb_log_task = NULL; |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 743 | static ssize_t apb_log_enable_read(struct file *f, char __user *buf, |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 744 | size_t count, loff_t *ppos) |
| 745 | { |
Alex Elder | 1482b3e | 2015-10-27 22:18:38 -0500 | [diff] [blame] | 746 | struct es2_ap_dev *es2 = f->f_inode->i_private; |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 747 | int enable = !IS_ERR_OR_NULL(es2->apb_log_task); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 748 | char tmp_buf[3]; |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 749 | |
| 750 | sprintf(tmp_buf, "%d\n", enable); |
| 751 | return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3); |
| 752 | } |
| 753 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 754 | static ssize_t apb_log_enable_write(struct file *f, const char __user *buf, |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 755 | size_t count, loff_t *ppos) |
| 756 | { |
| 757 | int enable; |
| 758 | ssize_t retval; |
Alex Elder | 1482b3e | 2015-10-27 22:18:38 -0500 | [diff] [blame] | 759 | struct es2_ap_dev *es2 = f->f_inode->i_private; |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 760 | |
| 761 | retval = kstrtoint_from_user(buf, count, 10, &enable); |
| 762 | if (retval) |
| 763 | return retval; |
| 764 | |
| 765 | if (enable) |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 766 | usb_log_enable(es2); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 767 | else |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 768 | usb_log_disable(es2); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 769 | |
| 770 | return count; |
| 771 | } |
| 772 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 773 | static const struct file_operations apb_log_enable_fops = { |
| 774 | .read = apb_log_enable_read, |
| 775 | .write = apb_log_enable_write, |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 776 | }; |
| 777 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 778 | static int apb_get_cport_count(struct usb_device *udev) |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 779 | { |
| 780 | int retval; |
| 781 | __le16 *cport_count; |
| 782 | |
| 783 | cport_count = kmalloc(sizeof(*cport_count), GFP_KERNEL); |
| 784 | if (!cport_count) |
| 785 | return -ENOMEM; |
| 786 | |
| 787 | retval = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), |
| 788 | REQUEST_CPORT_COUNT, |
| 789 | USB_DIR_IN | USB_TYPE_VENDOR | |
| 790 | USB_RECIP_INTERFACE, 0, 0, cport_count, |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 791 | sizeof(*cport_count), ES2_TIMEOUT); |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 792 | if (retval < 0) { |
| 793 | dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n", |
| 794 | retval); |
| 795 | goto out; |
| 796 | } |
| 797 | |
| 798 | retval = le16_to_cpu(*cport_count); |
| 799 | |
| 800 | /* We need to fit a CPort ID in one byte of a message header */ |
| 801 | if (retval > U8_MAX) { |
| 802 | retval = U8_MAX; |
| 803 | dev_warn(&udev->dev, "Limiting number of CPorts to U8_MAX\n"); |
| 804 | } |
| 805 | |
| 806 | out: |
| 807 | kfree(cport_count); |
| 808 | return retval; |
| 809 | } |
| 810 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 811 | /* |
Johan Hovold | 4d5c446 | 2015-11-02 11:56:57 +0100 | [diff] [blame] | 812 | * The ES2 USB Bridge device has 15 endpoints |
| 813 | * 1 Control - usual USB stuff + AP -> APBridgeA messages |
| 814 | * 7 Bulk IN - CPort data in |
| 815 | * 7 Bulk OUT - CPort data out |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 816 | */ |
| 817 | static int ap_probe(struct usb_interface *interface, |
| 818 | const struct usb_device_id *id) |
| 819 | { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 820 | struct es2_ap_dev *es2; |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 821 | struct gb_host_device *hd; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 822 | struct usb_device *udev; |
| 823 | struct usb_host_interface *iface_desc; |
| 824 | struct usb_endpoint_descriptor *endpoint; |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 825 | int bulk_in = 0; |
| 826 | int bulk_out = 0; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 827 | int retval = -ENOMEM; |
| 828 | int i; |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 829 | int num_cports; |
Alex Elder | 4bc1389 | 2015-06-13 11:02:11 -0500 | [diff] [blame] | 830 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 831 | udev = usb_get_dev(interface_to_usbdev(interface)); |
| 832 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 833 | num_cports = apb_get_cport_count(udev); |
Fabien Parent | 24a6112 | 2015-09-02 15:50:37 +0200 | [diff] [blame] | 834 | if (num_cports < 0) { |
| 835 | usb_put_dev(udev); |
| 836 | dev_err(&udev->dev, "Cannot retrieve CPort count: %d\n", |
| 837 | num_cports); |
| 838 | return num_cports; |
| 839 | } |
| 840 | |
Johan Hovold | d6e139b | 2015-11-03 18:03:25 +0100 | [diff] [blame] | 841 | hd = gb_hd_create(&es2_driver, &udev->dev, ES2_GBUF_MSG_SIZE_MAX, |
| 842 | num_cports); |
Alex Elder | 8ea70fe | 2015-05-22 09:52:45 -0500 | [diff] [blame] | 843 | if (IS_ERR(hd)) { |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 844 | usb_put_dev(udev); |
Alex Elder | 8ea70fe | 2015-05-22 09:52:45 -0500 | [diff] [blame] | 845 | return PTR_ERR(hd); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 846 | } |
| 847 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 848 | es2 = hd_to_es2(hd); |
| 849 | es2->hd = hd; |
| 850 | es2->usb_intf = interface; |
| 851 | es2->usb_dev = udev; |
| 852 | spin_lock_init(&es2->cport_out_urb_lock); |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 853 | INIT_KFIFO(es2->apb_log_fifo); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 854 | usb_set_intfdata(interface, es2); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 855 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 856 | es2->cport_to_ep = kcalloc(hd->num_cports, sizeof(*es2->cport_to_ep), |
Fabien Parent | c011d55 | 2015-09-02 15:50:36 +0200 | [diff] [blame] | 857 | GFP_KERNEL); |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 858 | if (!es2->cport_to_ep) { |
Fabien Parent | c011d55 | 2015-09-02 15:50:36 +0200 | [diff] [blame] | 859 | retval = -ENOMEM; |
| 860 | goto error; |
| 861 | } |
| 862 | |
Johan Hovold | 4d5c446 | 2015-11-02 11:56:57 +0100 | [diff] [blame] | 863 | /* find all bulk endpoints */ |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 864 | iface_desc = interface->cur_altsetting; |
| 865 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 866 | endpoint = &iface_desc->endpoint[i].desc; |
| 867 | |
Greg Kroah-Hartman | b767ee4 | 2015-07-24 17:09:48 -0700 | [diff] [blame] | 868 | if (usb_endpoint_is_bulk_in(endpoint)) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 869 | es2->cport_in[bulk_in++].endpoint = |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 870 | endpoint->bEndpointAddress; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 871 | } else if (usb_endpoint_is_bulk_out(endpoint)) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 872 | es2->cport_out[bulk_out++].endpoint = |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 873 | endpoint->bEndpointAddress; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 874 | } else { |
| 875 | dev_err(&udev->dev, |
| 876 | "Unknown endpoint type found, address %x\n", |
| 877 | endpoint->bEndpointAddress); |
| 878 | } |
| 879 | } |
Johan Hovold | b6d8085 | 2015-11-04 18:55:13 +0100 | [diff] [blame] | 880 | if (bulk_in != NUM_BULKS || bulk_out != NUM_BULKS) { |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 881 | dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n"); |
| 882 | goto error; |
| 883 | } |
| 884 | |
Johan Hovold | 0ce68ce4 | 2015-11-04 18:55:14 +0100 | [diff] [blame] | 885 | /* Allocate buffers for our cport in messages */ |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 886 | for (bulk_in = 0; bulk_in < NUM_BULKS; bulk_in++) { |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 887 | struct es2_cport_in *cport_in = &es2->cport_in[bulk_in]; |
| 888 | |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 889 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
| 890 | struct urb *urb; |
| 891 | u8 *buffer; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 892 | |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 893 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 894 | if (!urb) |
| 895 | goto error; |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 896 | buffer = kmalloc(ES2_GBUF_MSG_SIZE_MAX, GFP_KERNEL); |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 897 | if (!buffer) |
| 898 | goto error; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 899 | |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 900 | usb_fill_bulk_urb(urb, udev, |
| 901 | usb_rcvbulkpipe(udev, |
| 902 | cport_in->endpoint), |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 903 | buffer, ES2_GBUF_MSG_SIZE_MAX, |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 904 | cport_in_callback, hd); |
| 905 | cport_in->urb[i] = urb; |
| 906 | cport_in->buffer[i] = buffer; |
Alexandre Bailon | 606addd | 2015-06-15 18:08:13 +0200 | [diff] [blame] | 907 | } |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /* Allocate urbs for our CPort OUT messages */ |
| 911 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
| 912 | struct urb *urb; |
| 913 | |
| 914 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 915 | if (!urb) |
| 916 | goto error; |
| 917 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 918 | es2->cport_out_urb[i] = urb; |
| 919 | es2->cport_out_urb_busy[i] = false; /* just to be anal */ |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 920 | } |
| 921 | |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 922 | /* XXX We will need to rename this per APB */ |
| 923 | es2->apb_log_enable_dentry = debugfs_create_file("apb_log_enable", |
Johan Hovold | 86f918e | 2015-06-23 14:17:41 +0200 | [diff] [blame] | 924 | (S_IWUSR | S_IRUGO), |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 925 | gb_debugfs_get(), es2, |
Alex Elder | 3be0e17 | 2015-10-27 22:18:41 -0500 | [diff] [blame] | 926 | &apb_log_enable_fops); |
Johan Hovold | 0ce68ce4 | 2015-11-04 18:55:14 +0100 | [diff] [blame] | 927 | |
| 928 | for (i = 0; i < NUM_BULKS; ++i) { |
| 929 | retval = es2_cport_in_enable(es2, &es2->cport_in[i]); |
| 930 | if (retval) |
| 931 | goto error; |
| 932 | } |
| 933 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 934 | return 0; |
| 935 | error: |
| 936 | ap_disconnect(interface); |
| 937 | |
| 938 | return retval; |
| 939 | } |
| 940 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 941 | static struct usb_driver es2_ap_driver = { |
Rob Herring | c13c8bf | 2015-05-05 11:04:22 -0500 | [diff] [blame] | 942 | .name = "es2_ap_driver", |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 943 | .probe = ap_probe, |
| 944 | .disconnect = ap_disconnect, |
| 945 | .id_table = id_table, |
| 946 | }; |
| 947 | |
Alex Elder | 4b1d820 | 2015-10-27 22:18:37 -0500 | [diff] [blame] | 948 | module_usb_driver(es2_ap_driver); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 949 | |
Greg Kroah-Hartman | 6cf42a4 | 2015-04-13 19:51:33 +0200 | [diff] [blame] | 950 | MODULE_LICENSE("GPL v2"); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 951 | MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>"); |