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 | */ |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 11 | #include <linux/kthread.h> |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 12 | #include <linux/slab.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <linux/sizes.h> |
| 15 | #include <linux/usb.h> |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 16 | #include <linux/kfifo.h> |
| 17 | #include <linux/debugfs.h> |
| 18 | #include <linux/uaccess.h> |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 19 | #include <asm/unaligned.h> |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 20 | |
| 21 | #include "greybus.h" |
| 22 | #include "svc_msg.h" |
| 23 | #include "kernel_ver.h" |
| 24 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 25 | /* Memory sizes for the buffers sent to/from the ES1 controller */ |
| 26 | #define ES1_SVC_MSG_SIZE (sizeof(struct svc_msg) + SZ_64K) |
| 27 | #define ES1_GBUF_MSG_SIZE_MAX PAGE_SIZE |
| 28 | |
| 29 | static const struct usb_device_id id_table[] = { |
Greg Kroah-Hartman | 2bf4c87 | 2015-03-02 08:52:07 -0800 | [diff] [blame] | 30 | /* Made up numbers for the SVC USB Bridge in ES2 */ |
| 31 | { USB_DEVICE(0xffff, 0x0002) }, |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 32 | { }, |
| 33 | }; |
| 34 | MODULE_DEVICE_TABLE(usb, id_table); |
| 35 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 36 | #define APB1_LOG_SIZE SZ_16K |
| 37 | static struct dentry *apb1_log_dentry; |
| 38 | static struct dentry *apb1_log_enable_dentry; |
| 39 | static struct task_struct *apb1_log_task; |
| 40 | static DEFINE_KFIFO(apb1_log_fifo, char, APB1_LOG_SIZE); |
| 41 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 42 | /* |
| 43 | * Number of CPort IN urbs in flight at any point in time. |
| 44 | * Adjust if we are having stalls in the USB buffer due to not enough urbs in |
| 45 | * flight. |
| 46 | */ |
| 47 | #define NUM_CPORT_IN_URB 4 |
| 48 | |
| 49 | /* Number of CPort OUT urbs in flight at any point in time. |
| 50 | * Adjust if we get messages saying we are out of urbs in the system log. |
| 51 | */ |
| 52 | #define NUM_CPORT_OUT_URB 8 |
| 53 | |
| 54 | /** |
| 55 | * es1_ap_dev - ES1 USB Bridge to AP structure |
| 56 | * @usb_dev: pointer to the USB device we are. |
| 57 | * @usb_intf: pointer to the USB interface we are bound to. |
| 58 | * @hd: pointer to our greybus_host_device structure |
| 59 | * @control_endpoint: endpoint to send data to SVC |
| 60 | * @svc_endpoint: endpoint for SVC data in |
| 61 | * @cport_in_endpoint: bulk in endpoint for CPort data |
| 62 | * @cport-out_endpoint: bulk out endpoint for CPort data |
| 63 | * @svc_buffer: buffer for SVC messages coming in on @svc_endpoint |
| 64 | * @svc_urb: urb for SVC messages coming in on @svc_endpoint |
| 65 | * @cport_in_urb: array of urbs for the CPort in messages |
| 66 | * @cport_in_buffer: array of buffers for the @cport_in_urb urbs |
| 67 | * @cport_out_urb: array of urbs for the CPort out messages |
| 68 | * @cport_out_urb_busy: array of flags to see if the @cport_out_urb is busy or |
| 69 | * not. |
| 70 | * @cport_out_urb_lock: locks the @cport_out_urb_busy "list" |
| 71 | */ |
| 72 | struct es1_ap_dev { |
| 73 | struct usb_device *usb_dev; |
| 74 | struct usb_interface *usb_intf; |
| 75 | struct greybus_host_device *hd; |
| 76 | |
| 77 | __u8 control_endpoint; |
| 78 | __u8 svc_endpoint; |
| 79 | __u8 cport_in_endpoint; |
| 80 | __u8 cport_out_endpoint; |
| 81 | |
| 82 | u8 *svc_buffer; |
| 83 | struct urb *svc_urb; |
| 84 | |
| 85 | struct urb *cport_in_urb[NUM_CPORT_IN_URB]; |
| 86 | u8 *cport_in_buffer[NUM_CPORT_IN_URB]; |
| 87 | struct urb *cport_out_urb[NUM_CPORT_OUT_URB]; |
| 88 | bool cport_out_urb_busy[NUM_CPORT_OUT_URB]; |
| 89 | spinlock_t cport_out_urb_lock; |
| 90 | }; |
| 91 | |
| 92 | static inline struct es1_ap_dev *hd_to_es1(struct greybus_host_device *hd) |
| 93 | { |
| 94 | return (struct es1_ap_dev *)&hd->hd_priv; |
| 95 | } |
| 96 | |
| 97 | static void cport_out_callback(struct urb *urb); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 98 | static void usb_log_enable(struct es1_ap_dev *es1); |
| 99 | static void usb_log_disable(struct es1_ap_dev *es1); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 100 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 101 | #define ES1_TIMEOUT 500 /* 500 ms for the SVC to do something */ |
| 102 | static int submit_svc(struct svc_msg *svc_msg, struct greybus_host_device *hd) |
| 103 | { |
| 104 | struct es1_ap_dev *es1 = hd_to_es1(hd); |
| 105 | int retval; |
| 106 | |
| 107 | /* SVC messages go down our control pipe */ |
| 108 | retval = usb_control_msg(es1->usb_dev, |
| 109 | usb_sndctrlpipe(es1->usb_dev, |
| 110 | es1->control_endpoint), |
| 111 | 0x01, /* vendor request AP message */ |
| 112 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 113 | 0x00, 0x00, |
| 114 | (char *)svc_msg, |
| 115 | sizeof(*svc_msg), |
| 116 | ES1_TIMEOUT); |
| 117 | if (retval != sizeof(*svc_msg)) |
| 118 | return retval; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static struct urb *next_free_urb(struct es1_ap_dev *es1, gfp_t gfp_mask) |
| 124 | { |
| 125 | struct urb *urb = NULL; |
| 126 | unsigned long flags; |
| 127 | int i; |
| 128 | |
| 129 | spin_lock_irqsave(&es1->cport_out_urb_lock, flags); |
| 130 | |
| 131 | /* Look in our pool of allocated urbs first, as that's the "fastest" */ |
| 132 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
| 133 | if (es1->cport_out_urb_busy[i] == false) { |
| 134 | es1->cport_out_urb_busy[i] = true; |
| 135 | urb = es1->cport_out_urb[i]; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags); |
| 140 | if (urb) |
| 141 | return urb; |
| 142 | |
| 143 | /* |
| 144 | * Crap, pool is empty, complain to the syslog and go allocate one |
| 145 | * dynamically as we have to succeed. |
| 146 | */ |
| 147 | dev_err(&es1->usb_dev->dev, |
| 148 | "No free CPort OUT urbs, having to dynamically allocate one!\n"); |
| 149 | return usb_alloc_urb(0, gfp_mask); |
| 150 | } |
| 151 | |
| 152 | static void free_urb(struct es1_ap_dev *es1, struct urb *urb) |
| 153 | { |
| 154 | unsigned long flags; |
| 155 | int i; |
| 156 | /* |
| 157 | * See if this was an urb in our pool, if so mark it "free", otherwise |
| 158 | * we need to free it ourselves. |
| 159 | */ |
| 160 | spin_lock_irqsave(&es1->cport_out_urb_lock, flags); |
| 161 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
| 162 | if (urb == es1->cport_out_urb[i]) { |
| 163 | es1->cport_out_urb_busy[i] = false; |
| 164 | urb = NULL; |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | spin_unlock_irqrestore(&es1->cport_out_urb_lock, flags); |
| 169 | |
| 170 | /* If urb is not NULL, then we need to free this urb */ |
| 171 | usb_free_urb(urb); |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Returns an opaque cookie value if successful, or a pointer coded |
| 176 | * error otherwise. If the caller wishes to cancel the in-flight |
| 177 | * buffer, it must supply the returned cookie to the cancel routine. |
| 178 | */ |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 179 | static void *message_send(struct greybus_host_device *hd, u16 cport_id, |
| 180 | struct gb_message *message, gfp_t gfp_mask) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 181 | { |
| 182 | struct es1_ap_dev *es1 = hd_to_es1(hd); |
| 183 | struct usb_device *udev = es1->usb_dev; |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 184 | void *buffer; |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 185 | size_t buffer_size; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 186 | int retval; |
| 187 | struct urb *urb; |
| 188 | |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 189 | buffer = message->buffer; |
| 190 | buffer_size = sizeof(*message->header) + message->payload_size; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * The data actually transferred will include an indication |
| 194 | * of where the data should be sent. Do one last check of |
| 195 | * the target CPort id before filling it in. |
| 196 | */ |
| 197 | if (cport_id == CPORT_ID_BAD) { |
| 198 | pr_err("request to send inbound data buffer\n"); |
| 199 | return ERR_PTR(-EINVAL); |
| 200 | } |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 201 | |
| 202 | /* Find a free urb */ |
| 203 | urb = next_free_urb(es1, gfp_mask); |
| 204 | if (!urb) |
| 205 | return ERR_PTR(-ENOMEM); |
| 206 | |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 207 | /* |
| 208 | * We (ab)use the operation-message header pad bytes to transfer the |
| 209 | * cport id in order to minimise overhead. |
| 210 | */ |
| 211 | put_unaligned_le16(cport_id, message->header->pad); |
| 212 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 213 | usb_fill_bulk_urb(urb, udev, |
| 214 | usb_sndbulkpipe(udev, es1->cport_out_endpoint), |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 215 | buffer, buffer_size, |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 216 | cport_out_callback, message); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 217 | retval = usb_submit_urb(urb, gfp_mask); |
| 218 | if (retval) { |
| 219 | pr_err("error %d submitting URB\n", retval); |
| 220 | free_urb(es1, urb); |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 221 | put_unaligned_le16(0, message->header->pad); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 222 | return ERR_PTR(retval); |
| 223 | } |
| 224 | |
Alex Elder | 142f8dd | 2015-03-26 21:25:06 -0500 | [diff] [blame] | 225 | return urb; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 229 | * The cookie value supplied is the value that message_send() |
| 230 | * returned to its caller. It identifies the message that should be |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 231 | * canceled. This function must also handle (which is to say, |
| 232 | * ignore) a null cookie value. |
| 233 | */ |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 234 | static void message_cancel(void *cookie) |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 235 | { |
| 236 | |
| 237 | /* |
| 238 | * We really should be defensive and track all outstanding |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 239 | * (sent) messages rather than trusting the cookie provided |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 240 | * is valid. For the time being, this will do. |
| 241 | */ |
| 242 | if (cookie) |
Alex Elder | 142f8dd | 2015-03-26 21:25:06 -0500 | [diff] [blame] | 243 | usb_kill_urb(cookie); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static struct greybus_host_driver es1_driver = { |
| 247 | .hd_priv_size = sizeof(struct es1_ap_dev), |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 248 | .message_send = message_send, |
| 249 | .message_cancel = message_cancel, |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 250 | .submit_svc = submit_svc, |
| 251 | }; |
| 252 | |
| 253 | /* Common function to report consistent warnings based on URB status */ |
| 254 | static int check_urb_status(struct urb *urb) |
| 255 | { |
| 256 | struct device *dev = &urb->dev->dev; |
| 257 | int status = urb->status; |
| 258 | |
| 259 | switch (status) { |
| 260 | case 0: |
| 261 | return 0; |
| 262 | |
| 263 | case -EOVERFLOW: |
| 264 | dev_err(dev, "%s: overflow actual length is %d\n", |
| 265 | __func__, urb->actual_length); |
| 266 | case -ECONNRESET: |
| 267 | case -ENOENT: |
| 268 | case -ESHUTDOWN: |
| 269 | case -EILSEQ: |
| 270 | case -EPROTO: |
| 271 | /* device is gone, stop sending */ |
| 272 | return status; |
| 273 | } |
| 274 | dev_err(dev, "%s: unknown status %d\n", __func__, status); |
| 275 | |
| 276 | return -EAGAIN; |
| 277 | } |
| 278 | |
| 279 | static void ap_disconnect(struct usb_interface *interface) |
| 280 | { |
| 281 | struct es1_ap_dev *es1; |
| 282 | struct usb_device *udev; |
| 283 | int i; |
| 284 | |
| 285 | es1 = usb_get_intfdata(interface); |
| 286 | if (!es1) |
| 287 | return; |
| 288 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 289 | usb_log_disable(es1); |
| 290 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 291 | /* Tear down everything! */ |
| 292 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
| 293 | struct urb *urb = es1->cport_out_urb[i]; |
| 294 | |
| 295 | if (!urb) |
| 296 | break; |
| 297 | usb_kill_urb(urb); |
| 298 | usb_free_urb(urb); |
| 299 | es1->cport_out_urb[i] = NULL; |
| 300 | es1->cport_out_urb_busy[i] = false; /* just to be anal */ |
| 301 | } |
| 302 | |
| 303 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
| 304 | struct urb *urb = es1->cport_in_urb[i]; |
| 305 | |
| 306 | if (!urb) |
| 307 | break; |
| 308 | usb_kill_urb(urb); |
| 309 | usb_free_urb(urb); |
| 310 | kfree(es1->cport_in_buffer[i]); |
| 311 | es1->cport_in_buffer[i] = NULL; |
| 312 | } |
| 313 | |
| 314 | usb_kill_urb(es1->svc_urb); |
| 315 | usb_free_urb(es1->svc_urb); |
| 316 | es1->svc_urb = NULL; |
| 317 | kfree(es1->svc_buffer); |
| 318 | es1->svc_buffer = NULL; |
| 319 | |
| 320 | usb_set_intfdata(interface, NULL); |
| 321 | udev = es1->usb_dev; |
| 322 | greybus_remove_hd(es1->hd); |
| 323 | |
| 324 | usb_put_dev(udev); |
| 325 | } |
| 326 | |
| 327 | /* Callback for when we get a SVC message */ |
| 328 | static void svc_in_callback(struct urb *urb) |
| 329 | { |
| 330 | struct greybus_host_device *hd = urb->context; |
| 331 | struct device *dev = &urb->dev->dev; |
| 332 | int status = check_urb_status(urb); |
| 333 | int retval; |
| 334 | |
| 335 | if (status) { |
| 336 | if ((status == -EAGAIN) || (status == -EPROTO)) |
| 337 | goto exit; |
| 338 | dev_err(dev, "urb svc in error %d (dropped)\n", status); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | /* We have a message, create a new message structure, add it to the |
| 343 | * list, and wake up our thread that will process the messages. |
| 344 | */ |
| 345 | greybus_svc_in(hd, urb->transfer_buffer, urb->actual_length); |
| 346 | |
| 347 | exit: |
| 348 | /* resubmit the urb to get more messages */ |
| 349 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 350 | if (retval) |
| 351 | dev_err(dev, "Can not submit urb for AP data: %d\n", retval); |
| 352 | } |
| 353 | |
| 354 | static void cport_in_callback(struct urb *urb) |
| 355 | { |
| 356 | struct greybus_host_device *hd = urb->context; |
| 357 | struct device *dev = &urb->dev->dev; |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 358 | struct gb_operation_msg_hdr *header; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 359 | int status = check_urb_status(urb); |
| 360 | int retval; |
| 361 | u16 cport_id; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 362 | |
| 363 | if (status) { |
| 364 | if ((status == -EAGAIN) || (status == -EPROTO)) |
| 365 | goto exit; |
| 366 | dev_err(dev, "urb cport in error %d (dropped)\n", status); |
| 367 | return; |
| 368 | } |
| 369 | |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 370 | if (urb->actual_length < sizeof(*header)) { |
| 371 | dev_err(dev, "%s: short message received\n", __func__); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 372 | goto exit; |
| 373 | } |
| 374 | |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 375 | header = urb->transfer_buffer; |
| 376 | cport_id = get_unaligned_le16(header->pad); |
| 377 | put_unaligned_le16(0, header->pad); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 378 | |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 379 | greybus_data_rcvd(hd, cport_id, urb->transfer_buffer, |
| 380 | urb->actual_length); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 381 | exit: |
| 382 | /* put our urb back in the request pool */ |
| 383 | retval = usb_submit_urb(urb, GFP_ATOMIC); |
| 384 | if (retval) |
| 385 | dev_err(dev, "%s: error %d in submitting urb.\n", |
| 386 | __func__, retval); |
| 387 | } |
| 388 | |
| 389 | static void cport_out_callback(struct urb *urb) |
| 390 | { |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 391 | struct gb_message *message = urb->context; |
| 392 | struct greybus_host_device *hd = message->operation->connection->hd; |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 393 | struct es1_ap_dev *es1 = hd_to_es1(hd); |
| 394 | int status = check_urb_status(urb); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 395 | |
Johan Hovold | 491e60d | 2015-04-07 11:27:20 +0200 | [diff] [blame] | 396 | /* Clear the pad bytes used for the cport id */ |
| 397 | put_unaligned_le16(0, message->header->pad); |
| 398 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 399 | /* |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 400 | * Tell the submitter that the message send (attempt) is |
| 401 | * complete, and report the status. |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 402 | */ |
Johan Hovold | 7cf7bca | 2015-04-07 11:27:16 +0200 | [diff] [blame] | 403 | greybus_message_sent(hd, message, status); |
| 404 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 405 | free_urb(es1, urb); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 406 | } |
| 407 | |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 408 | #define APB1_LOG_MSG_SIZE 64 |
| 409 | static void apb1_log_get(struct es1_ap_dev *es1, char *buf) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 410 | { |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 411 | int retval; |
| 412 | |
| 413 | /* SVC messages go down our control pipe */ |
| 414 | do { |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 415 | retval = usb_control_msg(es1->usb_dev, |
| 416 | usb_rcvctrlpipe(es1->usb_dev, |
| 417 | es1->control_endpoint), |
| 418 | 0x02, /* vendor request APB1 log */ |
| 419 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_INTERFACE, |
| 420 | 0x00, 0x00, |
| 421 | buf, |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 422 | APB1_LOG_MSG_SIZE, |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 423 | ES1_TIMEOUT); |
| 424 | if (retval > 0) |
| 425 | kfifo_in(&apb1_log_fifo, buf, retval); |
| 426 | } while (retval > 0); |
| 427 | } |
| 428 | |
| 429 | static int apb1_log_poll(void *data) |
| 430 | { |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 431 | struct es1_ap_dev *es1 = data; |
| 432 | char *buf; |
| 433 | |
| 434 | buf = kmalloc(APB1_LOG_MSG_SIZE, GFP_KERNEL); |
| 435 | if (!buf) |
| 436 | return -ENOMEM; |
| 437 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 438 | while (!kthread_should_stop()) { |
| 439 | msleep(1000); |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 440 | apb1_log_get(es1, buf); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 441 | } |
Johan Hovold | c15ccab | 2015-04-07 11:27:12 +0200 | [diff] [blame] | 442 | |
| 443 | kfree(buf); |
| 444 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static ssize_t apb1_log_read(struct file *f, char __user *buf, |
| 449 | size_t count, loff_t *ppos) |
| 450 | { |
| 451 | ssize_t ret; |
| 452 | size_t copied; |
| 453 | char *tmp_buf; |
| 454 | |
| 455 | if (count > APB1_LOG_SIZE) |
| 456 | count = APB1_LOG_SIZE; |
| 457 | |
| 458 | tmp_buf = kmalloc(count, GFP_KERNEL); |
| 459 | if (!tmp_buf) |
| 460 | return -ENOMEM; |
| 461 | |
| 462 | copied = kfifo_out(&apb1_log_fifo, tmp_buf, count); |
| 463 | ret = simple_read_from_buffer(buf, count, ppos, tmp_buf, copied); |
| 464 | |
| 465 | kfree(tmp_buf); |
| 466 | |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | static const struct file_operations apb1_log_fops = { |
| 471 | .read = apb1_log_read, |
| 472 | }; |
| 473 | |
| 474 | static void usb_log_enable(struct es1_ap_dev *es1) |
| 475 | { |
Alex Elder | e0feaf1 | 2015-03-27 15:20:49 -0500 | [diff] [blame] | 476 | if (!IS_ERR_OR_NULL(apb1_log_task)) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 477 | return; |
| 478 | |
| 479 | /* get log from APB1 */ |
| 480 | apb1_log_task = kthread_run(apb1_log_poll, es1, "apb1_log"); |
Alex Elder | e0feaf1 | 2015-03-27 15:20:49 -0500 | [diff] [blame] | 481 | if (IS_ERR(apb1_log_task)) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 482 | return; |
| 483 | apb1_log_dentry = debugfs_create_file("apb1_log", S_IRUGO, |
| 484 | gb_debugfs_get(), NULL, |
| 485 | &apb1_log_fops); |
| 486 | } |
| 487 | |
| 488 | static void usb_log_disable(struct es1_ap_dev *es1) |
| 489 | { |
Alex Elder | e0feaf1 | 2015-03-27 15:20:49 -0500 | [diff] [blame] | 490 | if (IS_ERR_OR_NULL(apb1_log_task)) |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 491 | return; |
| 492 | |
| 493 | debugfs_remove(apb1_log_dentry); |
| 494 | apb1_log_dentry = NULL; |
| 495 | |
| 496 | kthread_stop(apb1_log_task); |
| 497 | apb1_log_task = NULL; |
| 498 | } |
| 499 | |
| 500 | static ssize_t apb1_log_enable_read(struct file *f, char __user *buf, |
| 501 | size_t count, loff_t *ppos) |
| 502 | { |
| 503 | char tmp_buf[3]; |
Alex Elder | e0feaf1 | 2015-03-27 15:20:49 -0500 | [diff] [blame] | 504 | int enable = !IS_ERR_OR_NULL(apb1_log_task); |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 505 | |
| 506 | sprintf(tmp_buf, "%d\n", enable); |
| 507 | return simple_read_from_buffer(buf, count, ppos, tmp_buf, 3); |
| 508 | } |
| 509 | |
| 510 | static ssize_t apb1_log_enable_write(struct file *f, const char __user *buf, |
| 511 | size_t count, loff_t *ppos) |
| 512 | { |
| 513 | int enable; |
| 514 | ssize_t retval; |
| 515 | struct es1_ap_dev *es1 = (struct es1_ap_dev *)f->f_inode->i_private; |
| 516 | |
| 517 | retval = kstrtoint_from_user(buf, count, 10, &enable); |
| 518 | if (retval) |
| 519 | return retval; |
| 520 | |
| 521 | if (enable) |
| 522 | usb_log_enable(es1); |
| 523 | else |
| 524 | usb_log_disable(es1); |
| 525 | |
| 526 | return count; |
| 527 | } |
| 528 | |
| 529 | static const struct file_operations apb1_log_enable_fops = { |
| 530 | .read = apb1_log_enable_read, |
| 531 | .write = apb1_log_enable_write, |
| 532 | }; |
| 533 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 534 | /* |
| 535 | * The ES1 USB Bridge device contains 4 endpoints |
| 536 | * 1 Control - usual USB stuff + AP -> SVC messages |
| 537 | * 1 Interrupt IN - SVC -> AP messages |
| 538 | * 1 Bulk IN - CPort data in |
| 539 | * 1 Bulk OUT - CPort data out |
| 540 | */ |
| 541 | static int ap_probe(struct usb_interface *interface, |
| 542 | const struct usb_device_id *id) |
| 543 | { |
| 544 | struct es1_ap_dev *es1; |
| 545 | struct greybus_host_device *hd; |
| 546 | struct usb_device *udev; |
| 547 | struct usb_host_interface *iface_desc; |
| 548 | struct usb_endpoint_descriptor *endpoint; |
| 549 | bool int_in_found = false; |
| 550 | bool bulk_in_found = false; |
| 551 | bool bulk_out_found = false; |
| 552 | int retval = -ENOMEM; |
| 553 | int i; |
Alex Elder | eb765e4 | 2015-05-22 12:56:49 -0500 | [diff] [blame] | 554 | u16 endo_id = 0x4755; // FIXME - get endo "ID" from the SVC |
| 555 | u8 ap_intf_id = 0x01; // FIXME - get endo "ID" from the SVC |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 556 | u8 svc_interval = 0; |
| 557 | |
| 558 | udev = usb_get_dev(interface_to_usbdev(interface)); |
| 559 | |
Johan Hovold | d933667 | 2015-05-19 11:22:43 +0200 | [diff] [blame] | 560 | hd = greybus_create_hd(&es1_driver, &udev->dev, ES1_GBUF_MSG_SIZE_MAX); |
Alex Elder | 8ea70fe | 2015-05-22 09:52:45 -0500 | [diff] [blame] | 561 | if (IS_ERR(hd)) { |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 562 | usb_put_dev(udev); |
Alex Elder | 8ea70fe | 2015-05-22 09:52:45 -0500 | [diff] [blame] | 563 | return PTR_ERR(hd); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 564 | } |
| 565 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 566 | es1 = hd_to_es1(hd); |
| 567 | es1->hd = hd; |
| 568 | es1->usb_intf = interface; |
| 569 | es1->usb_dev = udev; |
| 570 | spin_lock_init(&es1->cport_out_urb_lock); |
| 571 | usb_set_intfdata(interface, es1); |
| 572 | |
| 573 | /* Control endpoint is the pipe to talk to this AP, so save it off */ |
| 574 | endpoint = &udev->ep0.desc; |
| 575 | es1->control_endpoint = endpoint->bEndpointAddress; |
| 576 | |
| 577 | /* find all 3 of our endpoints */ |
| 578 | iface_desc = interface->cur_altsetting; |
| 579 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 580 | endpoint = &iface_desc->endpoint[i].desc; |
| 581 | |
| 582 | if (usb_endpoint_is_int_in(endpoint)) { |
| 583 | es1->svc_endpoint = endpoint->bEndpointAddress; |
| 584 | svc_interval = endpoint->bInterval; |
| 585 | int_in_found = true; |
| 586 | } else if (usb_endpoint_is_bulk_in(endpoint)) { |
| 587 | es1->cport_in_endpoint = endpoint->bEndpointAddress; |
| 588 | bulk_in_found = true; |
| 589 | } else if (usb_endpoint_is_bulk_out(endpoint)) { |
| 590 | es1->cport_out_endpoint = endpoint->bEndpointAddress; |
| 591 | bulk_out_found = true; |
| 592 | } else { |
| 593 | dev_err(&udev->dev, |
| 594 | "Unknown endpoint type found, address %x\n", |
| 595 | endpoint->bEndpointAddress); |
| 596 | } |
| 597 | } |
| 598 | if ((int_in_found == false) || |
| 599 | (bulk_in_found == false) || |
| 600 | (bulk_out_found == false)) { |
| 601 | dev_err(&udev->dev, "Not enough endpoints found in device, aborting!\n"); |
| 602 | goto error; |
| 603 | } |
| 604 | |
| 605 | /* Create our buffer and URB to get SVC messages, and start it up */ |
| 606 | es1->svc_buffer = kmalloc(ES1_SVC_MSG_SIZE, GFP_KERNEL); |
| 607 | if (!es1->svc_buffer) |
| 608 | goto error; |
| 609 | |
| 610 | es1->svc_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 611 | if (!es1->svc_urb) |
| 612 | goto error; |
| 613 | |
| 614 | usb_fill_int_urb(es1->svc_urb, udev, |
| 615 | usb_rcvintpipe(udev, es1->svc_endpoint), |
| 616 | es1->svc_buffer, ES1_SVC_MSG_SIZE, svc_in_callback, |
| 617 | hd, svc_interval); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 618 | |
| 619 | /* Allocate buffers for our cport in messages and start them up */ |
| 620 | for (i = 0; i < NUM_CPORT_IN_URB; ++i) { |
| 621 | struct urb *urb; |
| 622 | u8 *buffer; |
| 623 | |
| 624 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 625 | if (!urb) |
| 626 | goto error; |
| 627 | buffer = kmalloc(ES1_GBUF_MSG_SIZE_MAX, GFP_KERNEL); |
| 628 | if (!buffer) |
| 629 | goto error; |
| 630 | |
| 631 | usb_fill_bulk_urb(urb, udev, |
| 632 | usb_rcvbulkpipe(udev, es1->cport_in_endpoint), |
| 633 | buffer, ES1_GBUF_MSG_SIZE_MAX, |
| 634 | cport_in_callback, hd); |
| 635 | es1->cport_in_urb[i] = urb; |
| 636 | es1->cport_in_buffer[i] = buffer; |
| 637 | retval = usb_submit_urb(urb, GFP_KERNEL); |
| 638 | if (retval) |
| 639 | goto error; |
| 640 | } |
| 641 | |
| 642 | /* Allocate urbs for our CPort OUT messages */ |
| 643 | for (i = 0; i < NUM_CPORT_OUT_URB; ++i) { |
| 644 | struct urb *urb; |
| 645 | |
| 646 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 647 | if (!urb) |
| 648 | goto error; |
| 649 | |
| 650 | es1->cport_out_urb[i] = urb; |
| 651 | es1->cport_out_urb_busy[i] = false; /* just to be anal */ |
| 652 | } |
| 653 | |
Greg Kroah-Hartman | ca3ec29 | 2015-03-27 11:38:07 +0100 | [diff] [blame] | 654 | /* Start up our svc urb, which allows events to start flowing */ |
| 655 | retval = usb_submit_urb(es1->svc_urb, GFP_KERNEL); |
| 656 | if (retval) |
| 657 | goto error; |
| 658 | |
| 659 | apb1_log_enable_dentry = debugfs_create_file("apb1_log_enable", |
| 660 | (S_IWUSR | S_IRUGO), |
| 661 | gb_debugfs_get(), es1, |
| 662 | &apb1_log_enable_fops); |
| 663 | |
Alex Elder | eb765e4 | 2015-05-22 12:56:49 -0500 | [diff] [blame] | 664 | /* |
| 665 | * XXX Soon this will be initiated later, with a combination |
| 666 | * XXX of a Control protocol probe operation and a |
| 667 | * XXX subsequent Control protocol connected operation for |
| 668 | * XXX the SVC connection. At that point we know we're |
| 669 | * XXX properly connected to an Endo. |
| 670 | */ |
| 671 | retval = greybus_endo_setup(hd, endo_id, ap_intf_id); |
| 672 | if (retval) |
| 673 | goto error; |
| 674 | |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 675 | return 0; |
| 676 | error: |
| 677 | ap_disconnect(interface); |
| 678 | |
| 679 | return retval; |
| 680 | } |
| 681 | |
| 682 | static struct usb_driver es1_ap_driver = { |
Rob Herring | c13c8bf | 2015-05-05 11:04:22 -0500 | [diff] [blame] | 683 | .name = "es2_ap_driver", |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 684 | .probe = ap_probe, |
| 685 | .disconnect = ap_disconnect, |
| 686 | .id_table = id_table, |
| 687 | }; |
| 688 | |
| 689 | module_usb_driver(es1_ap_driver); |
| 690 | |
Greg Kroah-Hartman | 6cf42a4 | 2015-04-13 19:51:33 +0200 | [diff] [blame] | 691 | MODULE_LICENSE("GPL v2"); |
Greg Kroah-Hartman | f587027 | 2015-01-21 10:24:15 +0800 | [diff] [blame] | 692 | MODULE_AUTHOR("Greg Kroah-Hartman <gregkh@linuxfoundation.org>"); |