1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * aspeed-vhub -- Driver for Aspeed SoC "vHub" USB gadget
4 *
5 * hub.c - virtual hub handling
6 *
7 * Copyright 2017 IBM Corporation
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/delay.h>
14 #include <linux/ioport.h>
15 #include <linux/slab.h>
16 #include <linux/errno.h>
17 #include <linux/list.h>
18 #include <linux/interrupt.h>
19 #include <linux/proc_fs.h>
20 #include <linux/prefetch.h>
21 #include <linux/clk.h>
22 #include <linux/usb/gadget.h>
23 #include <linux/of.h>
24 #include <linux/regmap.h>
25 #include <linux/string_choices.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/bcd.h>
28 #include <linux/version.h>
29 #include <linux/usb.h>
30 #include <linux/usb/hcd.h>
31
32 #include "vhub.h"
33
34 /* usb 2.0 hub device descriptor
35 *
36 * A few things we may want to improve here:
37 *
38 * - We may need to indicate TT support
39 * - We may need a device qualifier descriptor
40 * as devices can pretend to be usb1 or 2
41 * - Make vid/did overridable
42 * - make it look like usb1 if usb1 mode forced
43 */
44 #define KERNEL_REL bin2bcd(LINUX_VERSION_MAJOR)
45 #define KERNEL_VER bin2bcd(LINUX_VERSION_PATCHLEVEL)
46
47 enum {
48 AST_VHUB_STR_INDEX_MAX = 4,
49 AST_VHUB_STR_MANUF = 3,
50 AST_VHUB_STR_PRODUCT = 2,
51 AST_VHUB_STR_SERIAL = 1,
52 };
53
54 static const struct usb_device_descriptor ast_vhub_dev_desc = {
55 .bLength = USB_DT_DEVICE_SIZE,
56 .bDescriptorType = USB_DT_DEVICE,
57 .bcdUSB = cpu_to_le16(0x0200),
58 .bDeviceClass = USB_CLASS_HUB,
59 .bDeviceSubClass = 0,
60 .bDeviceProtocol = 1,
61 .bMaxPacketSize0 = 64,
62 .idVendor = cpu_to_le16(0x1d6b),
63 .idProduct = cpu_to_le16(0x0107),
64 .bcdDevice = cpu_to_le16(0x0100),
65 .iManufacturer = AST_VHUB_STR_MANUF,
66 .iProduct = AST_VHUB_STR_PRODUCT,
67 .iSerialNumber = AST_VHUB_STR_SERIAL,
68 .bNumConfigurations = 1,
69 };
70
71 static const struct usb_qualifier_descriptor ast_vhub_qual_desc = {
72 .bLength = 0xA,
73 .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
74 .bcdUSB = cpu_to_le16(0x0200),
75 .bDeviceClass = USB_CLASS_HUB,
76 .bDeviceSubClass = 0,
77 .bDeviceProtocol = 0,
78 .bMaxPacketSize0 = 64,
79 .bNumConfigurations = 1,
80 .bRESERVED = 0,
81 };
82
83 /*
84 * Configuration descriptor: same comments as above
85 * regarding handling USB1 mode.
86 */
87
88 /*
89 * We don't use sizeof() as Linux definition of
90 * struct usb_endpoint_descriptor contains 2
91 * extra bytes
92 */
93 #define AST_VHUB_CONF_DESC_SIZE (USB_DT_CONFIG_SIZE + \
94 USB_DT_INTERFACE_SIZE + \
95 USB_DT_ENDPOINT_SIZE)
96
97 static const struct ast_vhub_full_cdesc ast_vhub_conf_desc = {
98 .cfg = {
99 .bLength = USB_DT_CONFIG_SIZE,
100 .bDescriptorType = USB_DT_CONFIG,
101 .wTotalLength = cpu_to_le16(AST_VHUB_CONF_DESC_SIZE),
102 .bNumInterfaces = 1,
103 .bConfigurationValue = 1,
104 .iConfiguration = 0,
105 .bmAttributes = USB_CONFIG_ATT_ONE |
106 USB_CONFIG_ATT_SELFPOWER |
107 USB_CONFIG_ATT_WAKEUP,
108 .bMaxPower = 0,
109 },
110 .intf = {
111 .bLength = USB_DT_INTERFACE_SIZE,
112 .bDescriptorType = USB_DT_INTERFACE,
113 .bInterfaceNumber = 0,
114 .bAlternateSetting = 0,
115 .bNumEndpoints = 1,
116 .bInterfaceClass = USB_CLASS_HUB,
117 .bInterfaceSubClass = 0,
118 .bInterfaceProtocol = 0,
119 .iInterface = 0,
120 },
121 .ep = {
122 .bLength = USB_DT_ENDPOINT_SIZE,
123 .bDescriptorType = USB_DT_ENDPOINT,
124 .bEndpointAddress = 0x81,
125 .bmAttributes = USB_ENDPOINT_XFER_INT,
126 .wMaxPacketSize = cpu_to_le16(1),
127 .bInterval = 0x0c,
128 },
129 };
130
131 #define AST_VHUB_HUB_DESC_SIZE (USB_DT_HUB_NONVAR_SIZE + 2)
132
133 static const struct usb_hub_descriptor ast_vhub_hub_desc = {
134 .bDescLength = AST_VHUB_HUB_DESC_SIZE,
135 .bDescriptorType = USB_DT_HUB,
136 .bNbrPorts = AST_VHUB_NUM_PORTS,
137 .wHubCharacteristics = cpu_to_le16(HUB_CHAR_NO_LPSM),
138 .bPwrOn2PwrGood = 10,
139 .bHubContrCurrent = 0,
140 .u.hs.DeviceRemovable[0] = 0,
141 .u.hs.DeviceRemovable[1] = 0xff,
142 };
143
144 /*
145 * These strings converted to UTF-16 must be smaller than
146 * our EP0 buffer.
147 */
148 static const struct usb_string ast_vhub_str_array[] = {
149 {
150 .id = AST_VHUB_STR_SERIAL,
151 .s = "00000000"
152 },
153 {
154 .id = AST_VHUB_STR_PRODUCT,
155 .s = "USB Virtual Hub"
156 },
157 {
158 .id = AST_VHUB_STR_MANUF,
159 .s = "Aspeed"
160 },
161 { }
162 };
163
164 static const struct usb_gadget_strings ast_vhub_strings = {
165 .language = 0x0409,
166 .strings = (struct usb_string *)ast_vhub_str_array
167 };
168
ast_vhub_hub_dev_status(struct ast_vhub_ep * ep,u16 wIndex,u16 wValue)169 static int ast_vhub_hub_dev_status(struct ast_vhub_ep *ep,
170 u16 wIndex, u16 wValue)
171 {
172 u8 st0;
173
174 EPDBG(ep, "GET_STATUS(dev)\n");
175
176 /*
177 * Mark it as self-powered, I doubt the BMC is powered off
178 * the USB bus ...
179 */
180 st0 = 1 << USB_DEVICE_SELF_POWERED;
181
182 /*
183 * Need to double check how remote wakeup actually works
184 * on that chip and what triggers it.
185 */
186 if (ep->vhub->wakeup_en)
187 st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP;
188
189 return ast_vhub_simple_reply(ep, st0, 0);
190 }
191
ast_vhub_hub_ep_status(struct ast_vhub_ep * ep,u16 wIndex,u16 wValue)192 static int ast_vhub_hub_ep_status(struct ast_vhub_ep *ep,
193 u16 wIndex, u16 wValue)
194 {
195 int ep_num;
196 u8 st0 = 0;
197
198 ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
199 EPDBG(ep, "GET_STATUS(ep%d)\n", ep_num);
200
201 /* On the hub we have only EP 0 and 1 */
202 if (ep_num == 1) {
203 if (ep->vhub->ep1_stalled)
204 st0 |= 1 << USB_ENDPOINT_HALT;
205 } else if (ep_num != 0)
206 return std_req_stall;
207
208 return ast_vhub_simple_reply(ep, st0, 0);
209 }
210
ast_vhub_hub_dev_feature(struct ast_vhub_ep * ep,u16 wIndex,u16 wValue,bool is_set)211 static int ast_vhub_hub_dev_feature(struct ast_vhub_ep *ep,
212 u16 wIndex, u16 wValue,
213 bool is_set)
214 {
215 u32 val;
216
217 EPDBG(ep, "%s_FEATURE(dev val=%02x)\n",
218 is_set ? "SET" : "CLEAR", wValue);
219
220 if (wValue == USB_DEVICE_REMOTE_WAKEUP) {
221 ep->vhub->wakeup_en = is_set;
222 EPDBG(ep, "Hub remote wakeup %s\n",
223 str_enabled_disabled(is_set));
224 return std_req_complete;
225 }
226
227 if (wValue == USB_DEVICE_TEST_MODE) {
228 val = readl(ep->vhub->regs + AST_VHUB_CTRL);
229 val &= ~GENMASK(10, 8);
230 val |= VHUB_CTRL_SET_TEST_MODE((wIndex >> 8) & 0x7);
231 writel(val, ep->vhub->regs + AST_VHUB_CTRL);
232
233 return std_req_complete;
234 }
235
236 return std_req_stall;
237 }
238
ast_vhub_hub_ep_feature(struct ast_vhub_ep * ep,u16 wIndex,u16 wValue,bool is_set)239 static int ast_vhub_hub_ep_feature(struct ast_vhub_ep *ep,
240 u16 wIndex, u16 wValue,
241 bool is_set)
242 {
243 int ep_num;
244 u32 reg;
245
246 ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
247 EPDBG(ep, "%s_FEATURE(ep%d val=%02x)\n",
248 is_set ? "SET" : "CLEAR", ep_num, wValue);
249
250 if (ep_num > 1)
251 return std_req_stall;
252 if (wValue != USB_ENDPOINT_HALT)
253 return std_req_stall;
254 if (ep_num == 0)
255 return std_req_complete;
256
257 EPDBG(ep, "%s stall on EP 1\n",
258 is_set ? "setting" : "clearing");
259
260 ep->vhub->ep1_stalled = is_set;
261 reg = readl(ep->vhub->regs + AST_VHUB_EP1_CTRL);
262 if (is_set) {
263 reg |= VHUB_EP1_CTRL_STALL;
264 } else {
265 reg &= ~VHUB_EP1_CTRL_STALL;
266 reg |= VHUB_EP1_CTRL_RESET_TOGGLE;
267 }
268 writel(reg, ep->vhub->regs + AST_VHUB_EP1_CTRL);
269
270 return std_req_complete;
271 }
272
ast_vhub_rep_desc(struct ast_vhub_ep * ep,u8 desc_type,u16 len)273 static int ast_vhub_rep_desc(struct ast_vhub_ep *ep,
274 u8 desc_type, u16 len)
275 {
276 size_t dsize;
277 struct ast_vhub *vhub = ep->vhub;
278
279 EPDBG(ep, "GET_DESCRIPTOR(type:%d)\n", desc_type);
280
281 /*
282 * Copy first to EP buffer and send from there, so
283 * we can do some in-place patching if needed. We know
284 * the EP buffer is big enough but ensure that doesn't
285 * change. We do that now rather than later after we
286 * have checked sizes etc... to avoid a gcc bug where
287 * it thinks len is constant and barfs about read
288 * overflows in memcpy.
289 */
290 switch(desc_type) {
291 case USB_DT_DEVICE:
292 dsize = USB_DT_DEVICE_SIZE;
293 memcpy(ep->buf, &vhub->vhub_dev_desc, dsize);
294 BUILD_BUG_ON(dsize > sizeof(vhub->vhub_dev_desc));
295 BUILD_BUG_ON(USB_DT_DEVICE_SIZE >= AST_VHUB_EP0_MAX_PACKET);
296 break;
297 case USB_DT_OTHER_SPEED_CONFIG:
298 case USB_DT_CONFIG:
299 dsize = AST_VHUB_CONF_DESC_SIZE;
300 memcpy(ep->buf, &vhub->vhub_conf_desc, dsize);
301 ((u8 *)ep->buf)[1] = desc_type;
302 BUILD_BUG_ON(dsize > sizeof(vhub->vhub_conf_desc));
303 BUILD_BUG_ON(AST_VHUB_CONF_DESC_SIZE >= AST_VHUB_EP0_MAX_PACKET);
304 break;
305 case USB_DT_HUB:
306 dsize = AST_VHUB_HUB_DESC_SIZE;
307 memcpy(ep->buf, &vhub->vhub_hub_desc, dsize);
308 BUILD_BUG_ON(dsize > sizeof(vhub->vhub_hub_desc));
309 BUILD_BUG_ON(AST_VHUB_HUB_DESC_SIZE >= AST_VHUB_EP0_MAX_PACKET);
310 break;
311 case USB_DT_DEVICE_QUALIFIER:
312 dsize = sizeof(vhub->vhub_qual_desc);
313 memcpy(ep->buf, &vhub->vhub_qual_desc, dsize);
314 break;
315 default:
316 return std_req_stall;
317 }
318
319 /* Crop requested length */
320 if (len > dsize)
321 len = dsize;
322
323 /* Shoot it from the EP buffer */
324 return ast_vhub_reply(ep, NULL, len);
325 }
326
327 static struct usb_gadget_strings*
ast_vhub_str_of_container(struct usb_gadget_string_container * container)328 ast_vhub_str_of_container(struct usb_gadget_string_container *container)
329 {
330 return (struct usb_gadget_strings *)container->stash;
331 }
332
ast_vhub_collect_languages(struct ast_vhub * vhub,void * buf,size_t size)333 static int ast_vhub_collect_languages(struct ast_vhub *vhub, void *buf,
334 size_t size)
335 {
336 int rc, hdr_len, nlangs, max_langs;
337 struct usb_gadget_strings *lang_str;
338 struct usb_gadget_string_container *container;
339 struct usb_string_descriptor *sdesc = buf;
340
341 nlangs = 0;
342 hdr_len = sizeof(struct usb_descriptor_header);
343 max_langs = (size - hdr_len) / sizeof(sdesc->wData[0]);
344 list_for_each_entry(container, &vhub->vhub_str_desc, list) {
345 if (nlangs >= max_langs)
346 break;
347
348 lang_str = ast_vhub_str_of_container(container);
349 sdesc->wData[nlangs++] = cpu_to_le16(lang_str->language);
350 }
351
352 rc = hdr_len + nlangs * sizeof(sdesc->wData[0]);
353 sdesc->bLength = rc;
354 sdesc->bDescriptorType = USB_DT_STRING;
355
356 return rc;
357 }
358
ast_vhub_lookup_string(struct ast_vhub * vhub,u16 lang_id)359 static struct usb_gadget_strings *ast_vhub_lookup_string(struct ast_vhub *vhub,
360 u16 lang_id)
361 {
362 struct usb_gadget_strings *lang_str;
363 struct usb_gadget_string_container *container;
364
365 list_for_each_entry(container, &vhub->vhub_str_desc, list) {
366 lang_str = ast_vhub_str_of_container(container);
367 if (lang_str->language == lang_id)
368 return lang_str;
369 }
370
371 return NULL;
372 }
373
ast_vhub_rep_string(struct ast_vhub_ep * ep,u8 string_id,u16 lang_id,u16 len)374 static int ast_vhub_rep_string(struct ast_vhub_ep *ep,
375 u8 string_id, u16 lang_id,
376 u16 len)
377 {
378 int rc;
379 u8 buf[256];
380 struct ast_vhub *vhub = ep->vhub;
381 struct usb_gadget_strings *lang_str;
382
383 if (string_id == 0) {
384 rc = ast_vhub_collect_languages(vhub, buf, sizeof(buf));
385 } else {
386 lang_str = ast_vhub_lookup_string(vhub, lang_id);
387 if (!lang_str)
388 return std_req_stall;
389
390 rc = usb_gadget_get_string(lang_str, string_id, buf);
391 }
392
393 if (rc < 0 || rc >= AST_VHUB_EP0_MAX_PACKET)
394 return std_req_stall;
395
396 /* Shoot it from the EP buffer */
397 memcpy(ep->buf, buf, rc);
398 return ast_vhub_reply(ep, NULL, min_t(u16, rc, len));
399 }
400
ast_vhub_std_hub_request(struct ast_vhub_ep * ep,struct usb_ctrlrequest * crq)401 enum std_req_rc ast_vhub_std_hub_request(struct ast_vhub_ep *ep,
402 struct usb_ctrlrequest *crq)
403 {
404 struct ast_vhub *vhub = ep->vhub;
405 u16 wValue, wIndex, wLength;
406
407 wValue = le16_to_cpu(crq->wValue);
408 wIndex = le16_to_cpu(crq->wIndex);
409 wLength = le16_to_cpu(crq->wLength);
410
411 /* First packet, grab speed */
412 if (vhub->speed == USB_SPEED_UNKNOWN) {
413 u32 ustat = readl(vhub->regs + AST_VHUB_USBSTS);
414 if (ustat & VHUB_USBSTS_HISPEED)
415 vhub->speed = USB_SPEED_HIGH;
416 else
417 vhub->speed = USB_SPEED_FULL;
418 UDCDBG(vhub, "USB status=%08x speed=%s\n", ustat,
419 vhub->speed == USB_SPEED_HIGH ? "high" : "full");
420 }
421
422 switch ((crq->bRequestType << 8) | crq->bRequest) {
423 /* SET_ADDRESS */
424 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
425 EPDBG(ep, "SET_ADDRESS: Got address %x\n", wValue);
426 writel(wValue, vhub->regs + AST_VHUB_CONF);
427 return std_req_complete;
428
429 /* GET_STATUS */
430 case DeviceRequest | USB_REQ_GET_STATUS:
431 return ast_vhub_hub_dev_status(ep, wIndex, wValue);
432 case InterfaceRequest | USB_REQ_GET_STATUS:
433 return ast_vhub_simple_reply(ep, 0, 0);
434 case EndpointRequest | USB_REQ_GET_STATUS:
435 return ast_vhub_hub_ep_status(ep, wIndex, wValue);
436
437 /* SET/CLEAR_FEATURE */
438 case DeviceOutRequest | USB_REQ_SET_FEATURE:
439 return ast_vhub_hub_dev_feature(ep, wIndex, wValue, true);
440 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
441 return ast_vhub_hub_dev_feature(ep, wIndex, wValue, false);
442 case EndpointOutRequest | USB_REQ_SET_FEATURE:
443 return ast_vhub_hub_ep_feature(ep, wIndex, wValue, true);
444 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
445 return ast_vhub_hub_ep_feature(ep, wIndex, wValue, false);
446
447 /* GET/SET_CONFIGURATION */
448 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
449 return ast_vhub_simple_reply(ep, 1);
450 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
451 if (wValue != 1)
452 return std_req_stall;
453 return std_req_complete;
454
455 /* GET_DESCRIPTOR */
456 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
457 switch (wValue >> 8) {
458 case USB_DT_DEVICE:
459 case USB_DT_CONFIG:
460 case USB_DT_DEVICE_QUALIFIER:
461 case USB_DT_OTHER_SPEED_CONFIG:
462 return ast_vhub_rep_desc(ep, wValue >> 8,
463 wLength);
464 case USB_DT_STRING:
465 return ast_vhub_rep_string(ep, wValue & 0xff,
466 wIndex, wLength);
467 }
468 return std_req_stall;
469
470 /* GET/SET_INTERFACE */
471 case DeviceRequest | USB_REQ_GET_INTERFACE:
472 return ast_vhub_simple_reply(ep, 0);
473 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
474 if (wValue != 0 || wIndex != 0)
475 return std_req_stall;
476 return std_req_complete;
477 }
478 return std_req_stall;
479 }
480
ast_vhub_update_hub_ep1(struct ast_vhub * vhub,unsigned int port)481 static void ast_vhub_update_hub_ep1(struct ast_vhub *vhub,
482 unsigned int port)
483 {
484 /* Update HW EP1 response */
485 u32 reg = readl(vhub->regs + AST_VHUB_EP1_STS_CHG);
486 u32 pmask = (1 << (port + 1));
487 if (vhub->ports[port].change)
488 reg |= pmask;
489 else
490 reg &= ~pmask;
491 writel(reg, vhub->regs + AST_VHUB_EP1_STS_CHG);
492 }
493
ast_vhub_change_port_stat(struct ast_vhub * vhub,unsigned int port,u16 clr_flags,u16 set_flags,bool set_c)494 static void ast_vhub_change_port_stat(struct ast_vhub *vhub,
495 unsigned int port,
496 u16 clr_flags,
497 u16 set_flags,
498 bool set_c)
499 {
500 struct ast_vhub_port *p = &vhub->ports[port];
501 u16 prev;
502
503 /* Update port status */
504 prev = p->status;
505 p->status = (prev & ~clr_flags) | set_flags;
506 DDBG(&p->dev, "port %d status %04x -> %04x (C=%d)\n",
507 port + 1, prev, p->status, set_c);
508
509 /* Update change bits if needed */
510 if (set_c) {
511 u16 chg = p->status ^ prev;
512
513 /* Only these are relevant for change */
514 chg &= USB_PORT_STAT_C_CONNECTION |
515 USB_PORT_STAT_C_ENABLE |
516 USB_PORT_STAT_C_SUSPEND |
517 USB_PORT_STAT_C_OVERCURRENT |
518 USB_PORT_STAT_C_RESET |
519 USB_PORT_STAT_C_L1;
520
521 /*
522 * We only set USB_PORT_STAT_C_ENABLE if we are disabling
523 * the port as per USB spec, otherwise MacOS gets upset
524 */
525 if (p->status & USB_PORT_STAT_ENABLE)
526 chg &= ~USB_PORT_STAT_C_ENABLE;
527
528 p->change = chg;
529 ast_vhub_update_hub_ep1(vhub, port);
530 }
531 }
532
ast_vhub_send_host_wakeup(struct ast_vhub * vhub)533 static void ast_vhub_send_host_wakeup(struct ast_vhub *vhub)
534 {
535 u32 reg = readl(vhub->regs + AST_VHUB_CTRL);
536 UDCDBG(vhub, "Waking up host !\n");
537 reg |= VHUB_CTRL_MANUAL_REMOTE_WAKEUP;
538 writel(reg, vhub->regs + AST_VHUB_CTRL);
539 }
540
ast_vhub_device_connect(struct ast_vhub * vhub,unsigned int port,bool on)541 void ast_vhub_device_connect(struct ast_vhub *vhub,
542 unsigned int port, bool on)
543 {
544 if (on)
545 ast_vhub_change_port_stat(vhub, port, 0,
546 USB_PORT_STAT_CONNECTION, true);
547 else
548 ast_vhub_change_port_stat(vhub, port,
549 USB_PORT_STAT_CONNECTION |
550 USB_PORT_STAT_ENABLE,
551 0, true);
552
553 /*
554 * If the hub is set to wakup the host on connection events
555 * then send a wakeup.
556 */
557 if (vhub->wakeup_en)
558 ast_vhub_send_host_wakeup(vhub);
559 }
560
ast_vhub_wake_work(struct work_struct * work)561 void ast_vhub_wake_work(struct work_struct *work)
562 {
563 struct ast_vhub *vhub = container_of(work,
564 struct ast_vhub,
565 wake_work);
566 unsigned long flags;
567 unsigned int i;
568
569 /*
570 * Wake all sleeping ports. If a port is suspended by
571 * the host suspend (without explicit state suspend),
572 * we let the normal host wake path deal with it later.
573 */
574 spin_lock_irqsave(&vhub->lock, flags);
575 for (i = 0; i < vhub->max_ports; i++) {
576 struct ast_vhub_port *p = &vhub->ports[i];
577
578 if (!(p->status & USB_PORT_STAT_SUSPEND))
579 continue;
580 ast_vhub_change_port_stat(vhub, i,
581 USB_PORT_STAT_SUSPEND,
582 0, true);
583 ast_vhub_dev_resume(&p->dev);
584 }
585 ast_vhub_send_host_wakeup(vhub);
586 spin_unlock_irqrestore(&vhub->lock, flags);
587 }
588
ast_vhub_hub_wake_all(struct ast_vhub * vhub)589 void ast_vhub_hub_wake_all(struct ast_vhub *vhub)
590 {
591 lockdep_assert_held(&vhub->lock);
592
593 /*
594 * A device is trying to wake the world, because this
595 * can recurse into the device, we break the call chain
596 * using a work queue
597 */
598 schedule_work(&vhub->wake_work);
599 }
600
ast_vhub_port_reset(struct ast_vhub * vhub,u8 port)601 static void ast_vhub_port_reset(struct ast_vhub *vhub, u8 port)
602 {
603 struct ast_vhub_port *p = &vhub->ports[port];
604 u16 set, clr, speed;
605
606 /* First mark disabled */
607 ast_vhub_change_port_stat(vhub, port,
608 USB_PORT_STAT_ENABLE |
609 USB_PORT_STAT_SUSPEND,
610 USB_PORT_STAT_RESET,
611 false);
612
613 if (!p->dev.driver)
614 return;
615
616 /*
617 * This will either "start" the port or reset the
618 * device if already started...
619 */
620 ast_vhub_dev_reset(&p->dev);
621
622 /* Grab the right speed */
623 speed = p->dev.driver->max_speed;
624 if (speed == USB_SPEED_UNKNOWN || speed > vhub->speed)
625 speed = vhub->speed;
626
627 switch (speed) {
628 case USB_SPEED_LOW:
629 set = USB_PORT_STAT_LOW_SPEED;
630 clr = USB_PORT_STAT_HIGH_SPEED;
631 break;
632 case USB_SPEED_FULL:
633 set = 0;
634 clr = USB_PORT_STAT_LOW_SPEED |
635 USB_PORT_STAT_HIGH_SPEED;
636 break;
637 case USB_SPEED_HIGH:
638 set = USB_PORT_STAT_HIGH_SPEED;
639 clr = USB_PORT_STAT_LOW_SPEED;
640 break;
641 default:
642 UDCDBG(vhub, "Unsupported speed %d when"
643 " connecting device\n",
644 speed);
645 return;
646 }
647 clr |= USB_PORT_STAT_RESET;
648 set |= USB_PORT_STAT_ENABLE;
649
650 /* This should ideally be delayed ... */
651 ast_vhub_change_port_stat(vhub, port, clr, set, true);
652 }
653
ast_vhub_set_port_feature(struct ast_vhub_ep * ep,u8 port,u16 feat)654 static enum std_req_rc ast_vhub_set_port_feature(struct ast_vhub_ep *ep,
655 u8 port, u16 feat)
656 {
657 struct ast_vhub *vhub = ep->vhub;
658 struct ast_vhub_port *p;
659
660 if (port == 0 || port > vhub->max_ports)
661 return std_req_stall;
662 port--;
663 p = &vhub->ports[port];
664
665 switch(feat) {
666 case USB_PORT_FEAT_SUSPEND:
667 if (!(p->status & USB_PORT_STAT_ENABLE))
668 return std_req_complete;
669 ast_vhub_change_port_stat(vhub, port,
670 0, USB_PORT_STAT_SUSPEND,
671 false);
672 ast_vhub_dev_suspend(&p->dev);
673 return std_req_complete;
674 case USB_PORT_FEAT_RESET:
675 EPDBG(ep, "Port reset !\n");
676 ast_vhub_port_reset(vhub, port);
677 return std_req_complete;
678 case USB_PORT_FEAT_POWER:
679 /*
680 * On Power-on, we mark the connected flag changed,
681 * if there's a connected device, some hosts will
682 * otherwise fail to detect it.
683 */
684 if (p->status & USB_PORT_STAT_CONNECTION) {
685 p->change |= USB_PORT_STAT_C_CONNECTION;
686 ast_vhub_update_hub_ep1(vhub, port);
687 }
688 return std_req_complete;
689 case USB_PORT_FEAT_TEST:
690 case USB_PORT_FEAT_INDICATOR:
691 /* We don't do anything with these */
692 return std_req_complete;
693 }
694 return std_req_stall;
695 }
696
ast_vhub_clr_port_feature(struct ast_vhub_ep * ep,u8 port,u16 feat)697 static enum std_req_rc ast_vhub_clr_port_feature(struct ast_vhub_ep *ep,
698 u8 port, u16 feat)
699 {
700 struct ast_vhub *vhub = ep->vhub;
701 struct ast_vhub_port *p;
702
703 if (port == 0 || port > vhub->max_ports)
704 return std_req_stall;
705 port--;
706 p = &vhub->ports[port];
707
708 switch(feat) {
709 case USB_PORT_FEAT_ENABLE:
710 ast_vhub_change_port_stat(vhub, port,
711 USB_PORT_STAT_ENABLE |
712 USB_PORT_STAT_SUSPEND, 0,
713 false);
714 ast_vhub_dev_suspend(&p->dev);
715 return std_req_complete;
716 case USB_PORT_FEAT_SUSPEND:
717 if (!(p->status & USB_PORT_STAT_SUSPEND))
718 return std_req_complete;
719 ast_vhub_change_port_stat(vhub, port,
720 USB_PORT_STAT_SUSPEND, 0,
721 false);
722 ast_vhub_dev_resume(&p->dev);
723 return std_req_complete;
724 case USB_PORT_FEAT_POWER:
725 /* We don't do power control */
726 return std_req_complete;
727 case USB_PORT_FEAT_INDICATOR:
728 /* We don't have indicators */
729 return std_req_complete;
730 case USB_PORT_FEAT_C_CONNECTION:
731 case USB_PORT_FEAT_C_ENABLE:
732 case USB_PORT_FEAT_C_SUSPEND:
733 case USB_PORT_FEAT_C_OVER_CURRENT:
734 case USB_PORT_FEAT_C_RESET:
735 /* Clear state-change feature */
736 p->change &= ~(1u << (feat - 16));
737 ast_vhub_update_hub_ep1(vhub, port);
738 return std_req_complete;
739 }
740 return std_req_stall;
741 }
742
ast_vhub_get_port_stat(struct ast_vhub_ep * ep,u8 port)743 static enum std_req_rc ast_vhub_get_port_stat(struct ast_vhub_ep *ep,
744 u8 port)
745 {
746 struct ast_vhub *vhub = ep->vhub;
747 u16 stat, chg;
748
749 if (port == 0 || port > vhub->max_ports)
750 return std_req_stall;
751 port--;
752
753 stat = vhub->ports[port].status;
754 chg = vhub->ports[port].change;
755
756 /* We always have power */
757 stat |= USB_PORT_STAT_POWER;
758
759 EPDBG(ep, " port status=%04x change=%04x\n", stat, chg);
760
761 return ast_vhub_simple_reply(ep,
762 stat & 0xff,
763 stat >> 8,
764 chg & 0xff,
765 chg >> 8);
766 }
767
ast_vhub_class_hub_request(struct ast_vhub_ep * ep,struct usb_ctrlrequest * crq)768 enum std_req_rc ast_vhub_class_hub_request(struct ast_vhub_ep *ep,
769 struct usb_ctrlrequest *crq)
770 {
771 u16 wValue, wIndex, wLength;
772
773 wValue = le16_to_cpu(crq->wValue);
774 wIndex = le16_to_cpu(crq->wIndex);
775 wLength = le16_to_cpu(crq->wLength);
776
777 switch ((crq->bRequestType << 8) | crq->bRequest) {
778 case GetHubStatus:
779 EPDBG(ep, "GetHubStatus\n");
780 return ast_vhub_simple_reply(ep, 0, 0, 0, 0);
781 case GetPortStatus:
782 EPDBG(ep, "GetPortStatus(%d)\n", wIndex & 0xff);
783 return ast_vhub_get_port_stat(ep, wIndex & 0xf);
784 case GetHubDescriptor:
785 if (wValue != (USB_DT_HUB << 8))
786 return std_req_stall;
787 EPDBG(ep, "GetHubDescriptor(%d)\n", wIndex & 0xff);
788 return ast_vhub_rep_desc(ep, USB_DT_HUB, wLength);
789 case SetHubFeature:
790 case ClearHubFeature:
791 EPDBG(ep, "Get/SetHubFeature(%d)\n", wValue);
792 /* No feature, just complete the requests */
793 if (wValue == C_HUB_LOCAL_POWER ||
794 wValue == C_HUB_OVER_CURRENT)
795 return std_req_complete;
796 return std_req_stall;
797 case SetPortFeature:
798 EPDBG(ep, "SetPortFeature(%d,%d)\n", wIndex & 0xf, wValue);
799 return ast_vhub_set_port_feature(ep, wIndex & 0xf, wValue);
800 case ClearPortFeature:
801 EPDBG(ep, "ClearPortFeature(%d,%d)\n", wIndex & 0xf, wValue);
802 return ast_vhub_clr_port_feature(ep, wIndex & 0xf, wValue);
803 case ClearTTBuffer:
804 case ResetTT:
805 case StopTT:
806 return std_req_complete;
807 case GetTTState:
808 return ast_vhub_simple_reply(ep, 0, 0, 0, 0);
809 default:
810 EPDBG(ep, "Unknown class request\n");
811 }
812 return std_req_stall;
813 }
814
ast_vhub_hub_suspend(struct ast_vhub * vhub)815 void ast_vhub_hub_suspend(struct ast_vhub *vhub)
816 {
817 unsigned int i;
818
819 UDCDBG(vhub, "USB bus suspend\n");
820
821 if (vhub->suspended)
822 return;
823
824 vhub->suspended = true;
825
826 /*
827 * Forward to unsuspended ports without changing
828 * their connection status.
829 */
830 for (i = 0; i < vhub->max_ports; i++) {
831 struct ast_vhub_port *p = &vhub->ports[i];
832
833 if (!(p->status & USB_PORT_STAT_SUSPEND))
834 ast_vhub_dev_suspend(&p->dev);
835 }
836 }
837
ast_vhub_hub_resume(struct ast_vhub * vhub)838 void ast_vhub_hub_resume(struct ast_vhub *vhub)
839 {
840 unsigned int i;
841
842 UDCDBG(vhub, "USB bus resume\n");
843
844 if (!vhub->suspended)
845 return;
846
847 vhub->suspended = false;
848
849 /*
850 * Forward to unsuspended ports without changing
851 * their connection status.
852 */
853 for (i = 0; i < vhub->max_ports; i++) {
854 struct ast_vhub_port *p = &vhub->ports[i];
855
856 if (!(p->status & USB_PORT_STAT_SUSPEND))
857 ast_vhub_dev_resume(&p->dev);
858 }
859 }
860
ast_vhub_hub_reset(struct ast_vhub * vhub)861 void ast_vhub_hub_reset(struct ast_vhub *vhub)
862 {
863 unsigned int i;
864
865 UDCDBG(vhub, "USB bus reset\n");
866
867 /*
868 * Is the speed known ? If not we don't care, we aren't
869 * initialized yet and ports haven't been enabled.
870 */
871 if (vhub->speed == USB_SPEED_UNKNOWN)
872 return;
873
874 /* We aren't suspended anymore obviously */
875 vhub->suspended = false;
876
877 /* No speed set */
878 vhub->speed = USB_SPEED_UNKNOWN;
879
880 /* Wakeup not enabled anymore */
881 vhub->wakeup_en = false;
882
883 /*
884 * Clear all port status, disable gadgets and "suspend"
885 * them. They will be woken up by a port reset.
886 */
887 for (i = 0; i < vhub->max_ports; i++) {
888 struct ast_vhub_port *p = &vhub->ports[i];
889
890 /* Only keep the connected flag */
891 p->status &= USB_PORT_STAT_CONNECTION;
892 p->change = 0;
893
894 /* Suspend the gadget if any */
895 ast_vhub_dev_suspend(&p->dev);
896 }
897
898 /* Cleanup HW */
899 writel(0, vhub->regs + AST_VHUB_CONF);
900 writel(0, vhub->regs + AST_VHUB_EP0_CTRL);
901 writel(VHUB_EP1_CTRL_RESET_TOGGLE |
902 VHUB_EP1_CTRL_ENABLE,
903 vhub->regs + AST_VHUB_EP1_CTRL);
904 writel(0, vhub->regs + AST_VHUB_EP1_STS_CHG);
905 }
906
ast_vhub_of_parse_dev_desc(struct ast_vhub * vhub,const struct device_node * vhub_np)907 static void ast_vhub_of_parse_dev_desc(struct ast_vhub *vhub,
908 const struct device_node *vhub_np)
909 {
910 u16 id;
911 u32 data;
912
913 if (!of_property_read_u32(vhub_np, "vhub-vendor-id", &data)) {
914 id = (u16)data;
915 vhub->vhub_dev_desc.idVendor = cpu_to_le16(id);
916 }
917 if (!of_property_read_u32(vhub_np, "vhub-product-id", &data)) {
918 id = (u16)data;
919 vhub->vhub_dev_desc.idProduct = cpu_to_le16(id);
920 }
921 if (!of_property_read_u32(vhub_np, "vhub-device-revision", &data)) {
922 id = (u16)data;
923 vhub->vhub_dev_desc.bcdDevice = cpu_to_le16(id);
924 }
925 }
926
ast_vhub_fixup_usb1_dev_desc(struct ast_vhub * vhub)927 static void ast_vhub_fixup_usb1_dev_desc(struct ast_vhub *vhub)
928 {
929 vhub->vhub_dev_desc.bcdUSB = cpu_to_le16(0x0100);
930 vhub->vhub_dev_desc.bDeviceProtocol = 0;
931 }
932
933 static struct usb_gadget_string_container*
ast_vhub_str_container_alloc(struct ast_vhub * vhub)934 ast_vhub_str_container_alloc(struct ast_vhub *vhub)
935 {
936 unsigned int size;
937 struct usb_string *str_array;
938 struct usb_gadget_strings *lang_str;
939 struct usb_gadget_string_container *container;
940
941 size = sizeof(*container);
942 size += sizeof(struct usb_gadget_strings);
943 size += sizeof(struct usb_string) * AST_VHUB_STR_INDEX_MAX;
944 container = devm_kzalloc(&vhub->pdev->dev, size, GFP_KERNEL);
945 if (!container)
946 return ERR_PTR(-ENOMEM);
947
948 lang_str = ast_vhub_str_of_container(container);
949 str_array = (struct usb_string *)(lang_str + 1);
950 lang_str->strings = str_array;
951 return container;
952 }
953
ast_vhub_str_deep_copy(struct usb_gadget_strings * dest,const struct usb_gadget_strings * src)954 static void ast_vhub_str_deep_copy(struct usb_gadget_strings *dest,
955 const struct usb_gadget_strings *src)
956 {
957 struct usb_string *src_array = src->strings;
958 struct usb_string *dest_array = dest->strings;
959
960 dest->language = src->language;
961 if (src_array && dest_array) {
962 do {
963 *dest_array = *src_array;
964 dest_array++;
965 src_array++;
966 } while (src_array->s);
967 }
968 }
969
ast_vhub_str_alloc_add(struct ast_vhub * vhub,const struct usb_gadget_strings * src_str)970 static int ast_vhub_str_alloc_add(struct ast_vhub *vhub,
971 const struct usb_gadget_strings *src_str)
972 {
973 struct usb_gadget_strings *dest_str;
974 struct usb_gadget_string_container *container;
975
976 container = ast_vhub_str_container_alloc(vhub);
977 if (IS_ERR(container))
978 return PTR_ERR(container);
979
980 dest_str = ast_vhub_str_of_container(container);
981 ast_vhub_str_deep_copy(dest_str, src_str);
982 list_add_tail(&container->list, &vhub->vhub_str_desc);
983
984 return 0;
985 }
986
987 static const struct {
988 const char *name;
989 u8 id;
990 } str_id_map[] = {
991 {"manufacturer", AST_VHUB_STR_MANUF},
992 {"product", AST_VHUB_STR_PRODUCT},
993 {"serial-number", AST_VHUB_STR_SERIAL},
994 {},
995 };
996
ast_vhub_of_parse_str_desc(struct ast_vhub * vhub,const struct device_node * desc_np)997 static int ast_vhub_of_parse_str_desc(struct ast_vhub *vhub,
998 const struct device_node *desc_np)
999 {
1000 u32 langid;
1001 int ret = 0;
1002 int i, offset;
1003 const char *str;
1004 struct device_node *child;
1005 struct usb_string str_array[AST_VHUB_STR_INDEX_MAX];
1006 struct usb_gadget_strings lang_str = {
1007 .strings = (struct usb_string *)str_array,
1008 };
1009
1010 for_each_child_of_node(desc_np, child) {
1011 if (of_property_read_u32(child, "reg", &langid))
1012 continue; /* no language identifier specified */
1013
1014 if (!usb_validate_langid(langid))
1015 continue; /* invalid language identifier */
1016
1017 lang_str.language = langid;
1018 for (i = offset = 0; str_id_map[i].name; i++) {
1019 str = of_get_property(child, str_id_map[i].name, NULL);
1020 if (str) {
1021 str_array[offset].s = str;
1022 str_array[offset].id = str_id_map[i].id;
1023 offset++;
1024 }
1025 }
1026 str_array[offset].id = 0;
1027 str_array[offset].s = NULL;
1028
1029 ret = ast_vhub_str_alloc_add(vhub, &lang_str);
1030 if (ret) {
1031 of_node_put(child);
1032 break;
1033 }
1034 }
1035
1036 return ret;
1037 }
1038
ast_vhub_init_desc(struct ast_vhub * vhub)1039 static int ast_vhub_init_desc(struct ast_vhub *vhub)
1040 {
1041 int ret;
1042 struct device_node *desc_np;
1043 const struct device_node *vhub_np = vhub->pdev->dev.of_node;
1044
1045 /* Initialize vhub Device Descriptor. */
1046 memcpy(&vhub->vhub_dev_desc, &ast_vhub_dev_desc,
1047 sizeof(vhub->vhub_dev_desc));
1048 ast_vhub_of_parse_dev_desc(vhub, vhub_np);
1049 if (vhub->force_usb1)
1050 ast_vhub_fixup_usb1_dev_desc(vhub);
1051
1052 /* Initialize vhub Configuration Descriptor. */
1053 memcpy(&vhub->vhub_conf_desc, &ast_vhub_conf_desc,
1054 sizeof(vhub->vhub_conf_desc));
1055
1056 /* Initialize vhub Hub Descriptor. */
1057 memcpy(&vhub->vhub_hub_desc, &ast_vhub_hub_desc,
1058 sizeof(vhub->vhub_hub_desc));
1059 vhub->vhub_hub_desc.bNbrPorts = vhub->max_ports;
1060
1061 /* Initialize vhub String Descriptors. */
1062 INIT_LIST_HEAD(&vhub->vhub_str_desc);
1063 desc_np = of_get_child_by_name(vhub_np, "vhub-strings");
1064 if (desc_np) {
1065 ret = ast_vhub_of_parse_str_desc(vhub, desc_np);
1066 of_node_put(desc_np);
1067 }
1068 else
1069 ret = ast_vhub_str_alloc_add(vhub, &ast_vhub_strings);
1070
1071 /* Initialize vhub Qualifier Descriptor. */
1072 memcpy(&vhub->vhub_qual_desc, &ast_vhub_qual_desc,
1073 sizeof(vhub->vhub_qual_desc));
1074
1075 return ret;
1076 }
1077
ast_vhub_init_hub(struct ast_vhub * vhub)1078 int ast_vhub_init_hub(struct ast_vhub *vhub)
1079 {
1080 vhub->speed = USB_SPEED_UNKNOWN;
1081
1082 return ast_vhub_init_desc(vhub);
1083 }
1084