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