1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/platform_device.h>
18#include <linux/delay.h>
19#include <linux/ioport.h>
20#include <linux/slab.h>
21#include <linux/errno.h>
22#include <linux/list.h>
23#include <linux/interrupt.h>
24#include <linux/proc_fs.h>
25#include <linux/prefetch.h>
26#include <linux/clk.h>
27#include <linux/usb/gadget.h>
28#include <linux/of.h>
29#include <linux/of_gpio.h>
30#include <linux/regmap.h>
31#include <linux/dma-mapping.h>
32#include <linux/bcd.h>
33#include <linux/version.h>
34#include <linux/usb.h>
35#include <linux/usb/hcd.h>
36
37#include "vhub.h"
38
39
40
41
42
43
44
45
46
47
48
49#define KERNEL_REL bin2bcd(LINUX_VERSION_MAJOR)
50#define KERNEL_VER bin2bcd(LINUX_VERSION_PATCHLEVEL)
51
52enum {
53 AST_VHUB_STR_INDEX_MAX = 4,
54 AST_VHUB_STR_MANUF = 3,
55 AST_VHUB_STR_PRODUCT = 2,
56 AST_VHUB_STR_SERIAL = 1,
57};
58
59static const struct usb_device_descriptor ast_vhub_dev_desc = {
60 .bLength = USB_DT_DEVICE_SIZE,
61 .bDescriptorType = USB_DT_DEVICE,
62 .bcdUSB = cpu_to_le16(0x0200),
63 .bDeviceClass = USB_CLASS_HUB,
64 .bDeviceSubClass = 0,
65 .bDeviceProtocol = 1,
66 .bMaxPacketSize0 = 64,
67 .idVendor = cpu_to_le16(0x1d6b),
68 .idProduct = cpu_to_le16(0x0107),
69 .bcdDevice = cpu_to_le16(0x0100),
70 .iManufacturer = AST_VHUB_STR_MANUF,
71 .iProduct = AST_VHUB_STR_PRODUCT,
72 .iSerialNumber = AST_VHUB_STR_SERIAL,
73 .bNumConfigurations = 1,
74};
75
76
77
78
79
80
81
82
83
84
85
86#define AST_VHUB_CONF_DESC_SIZE (USB_DT_CONFIG_SIZE + \
87 USB_DT_INTERFACE_SIZE + \
88 USB_DT_ENDPOINT_SIZE)
89
90static const struct ast_vhub_full_cdesc ast_vhub_conf_desc = {
91 .cfg = {
92 .bLength = USB_DT_CONFIG_SIZE,
93 .bDescriptorType = USB_DT_CONFIG,
94 .wTotalLength = cpu_to_le16(AST_VHUB_CONF_DESC_SIZE),
95 .bNumInterfaces = 1,
96 .bConfigurationValue = 1,
97 .iConfiguration = 0,
98 .bmAttributes = USB_CONFIG_ATT_ONE |
99 USB_CONFIG_ATT_SELFPOWER |
100 USB_CONFIG_ATT_WAKEUP,
101 .bMaxPower = 0,
102 },
103 .intf = {
104 .bLength = USB_DT_INTERFACE_SIZE,
105 .bDescriptorType = USB_DT_INTERFACE,
106 .bInterfaceNumber = 0,
107 .bAlternateSetting = 0,
108 .bNumEndpoints = 1,
109 .bInterfaceClass = USB_CLASS_HUB,
110 .bInterfaceSubClass = 0,
111 .bInterfaceProtocol = 0,
112 .iInterface = 0,
113 },
114 .ep = {
115 .bLength = USB_DT_ENDPOINT_SIZE,
116 .bDescriptorType = USB_DT_ENDPOINT,
117 .bEndpointAddress = 0x81,
118 .bmAttributes = USB_ENDPOINT_XFER_INT,
119 .wMaxPacketSize = cpu_to_le16(1),
120 .bInterval = 0x0c,
121 },
122};
123
124#define AST_VHUB_HUB_DESC_SIZE (USB_DT_HUB_NONVAR_SIZE + 2)
125
126static const struct usb_hub_descriptor ast_vhub_hub_desc = {
127 .bDescLength = AST_VHUB_HUB_DESC_SIZE,
128 .bDescriptorType = USB_DT_HUB,
129 .bNbrPorts = AST_VHUB_NUM_PORTS,
130 .wHubCharacteristics = cpu_to_le16(HUB_CHAR_NO_LPSM),
131 .bPwrOn2PwrGood = 10,
132 .bHubContrCurrent = 0,
133 .u.hs.DeviceRemovable[0] = 0,
134 .u.hs.DeviceRemovable[1] = 0xff,
135};
136
137
138
139
140
141static const struct usb_string ast_vhub_str_array[] = {
142 {
143 .id = AST_VHUB_STR_SERIAL,
144 .s = "00000000"
145 },
146 {
147 .id = AST_VHUB_STR_PRODUCT,
148 .s = "USB Virtual Hub"
149 },
150 {
151 .id = AST_VHUB_STR_MANUF,
152 .s = "Aspeed"
153 },
154 { }
155};
156
157static const struct usb_gadget_strings ast_vhub_strings = {
158 .language = 0x0409,
159 .strings = (struct usb_string *)ast_vhub_str_array
160};
161
162static int ast_vhub_hub_dev_status(struct ast_vhub_ep *ep,
163 u16 wIndex, u16 wValue)
164{
165 u8 st0;
166
167 EPDBG(ep, "GET_STATUS(dev)\n");
168
169
170
171
172
173 st0 = 1 << USB_DEVICE_SELF_POWERED;
174
175
176
177
178
179 if (ep->vhub->wakeup_en)
180 st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP;
181
182 return ast_vhub_simple_reply(ep, st0, 0);
183}
184
185static int ast_vhub_hub_ep_status(struct ast_vhub_ep *ep,
186 u16 wIndex, u16 wValue)
187{
188 int ep_num;
189 u8 st0 = 0;
190
191 ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
192 EPDBG(ep, "GET_STATUS(ep%d)\n", ep_num);
193
194
195 if (ep_num == 1) {
196 if (ep->vhub->ep1_stalled)
197 st0 |= 1 << USB_ENDPOINT_HALT;
198 } else if (ep_num != 0)
199 return std_req_stall;
200
201 return ast_vhub_simple_reply(ep, st0, 0);
202}
203
204static int ast_vhub_hub_dev_feature(struct ast_vhub_ep *ep,
205 u16 wIndex, u16 wValue,
206 bool is_set)
207{
208 EPDBG(ep, "%s_FEATURE(dev val=%02x)\n",
209 is_set ? "SET" : "CLEAR", wValue);
210
211 if (wValue != USB_DEVICE_REMOTE_WAKEUP)
212 return std_req_stall;
213
214 ep->vhub->wakeup_en = is_set;
215 EPDBG(ep, "Hub remote wakeup %s\n",
216 is_set ? "enabled" : "disabled");
217
218 return std_req_complete;
219}
220
221static int ast_vhub_hub_ep_feature(struct ast_vhub_ep *ep,
222 u16 wIndex, u16 wValue,
223 bool is_set)
224{
225 int ep_num;
226 u32 reg;
227
228 ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
229 EPDBG(ep, "%s_FEATURE(ep%d val=%02x)\n",
230 is_set ? "SET" : "CLEAR", ep_num, wValue);
231
232 if (ep_num > 1)
233 return std_req_stall;
234 if (wValue != USB_ENDPOINT_HALT)
235 return std_req_stall;
236 if (ep_num == 0)
237 return std_req_complete;
238
239 EPDBG(ep, "%s stall on EP 1\n",
240 is_set ? "setting" : "clearing");
241
242 ep->vhub->ep1_stalled = is_set;
243 reg = readl(ep->vhub->regs + AST_VHUB_EP1_CTRL);
244 if (is_set) {
245 reg |= VHUB_EP1_CTRL_STALL;
246 } else {
247 reg &= ~VHUB_EP1_CTRL_STALL;
248 reg |= VHUB_EP1_CTRL_RESET_TOGGLE;
249 }
250 writel(reg, ep->vhub->regs + AST_VHUB_EP1_CTRL);
251
252 return std_req_complete;
253}
254
255static int ast_vhub_rep_desc(struct ast_vhub_ep *ep,
256 u8 desc_type, u16 len)
257{
258 size_t dsize;
259 struct ast_vhub *vhub = ep->vhub;
260
261 EPDBG(ep, "GET_DESCRIPTOR(type:%d)\n", desc_type);
262
263
264
265
266
267
268
269
270
271
272 switch(desc_type) {
273 case USB_DT_DEVICE:
274 dsize = USB_DT_DEVICE_SIZE;
275 memcpy(ep->buf, &vhub->vhub_dev_desc, dsize);
276 BUILD_BUG_ON(dsize > sizeof(vhub->vhub_dev_desc));
277 BUILD_BUG_ON(USB_DT_DEVICE_SIZE >= AST_VHUB_EP0_MAX_PACKET);
278 break;
279 case USB_DT_CONFIG:
280 dsize = AST_VHUB_CONF_DESC_SIZE;
281 memcpy(ep->buf, &vhub->vhub_conf_desc, dsize);
282 BUILD_BUG_ON(dsize > sizeof(vhub->vhub_conf_desc));
283 BUILD_BUG_ON(AST_VHUB_CONF_DESC_SIZE >= AST_VHUB_EP0_MAX_PACKET);
284 break;
285 case USB_DT_HUB:
286 dsize = AST_VHUB_HUB_DESC_SIZE;
287 memcpy(ep->buf, &vhub->vhub_hub_desc, dsize);
288 BUILD_BUG_ON(dsize > sizeof(vhub->vhub_hub_desc));
289 BUILD_BUG_ON(AST_VHUB_HUB_DESC_SIZE >= AST_VHUB_EP0_MAX_PACKET);
290 break;
291 default:
292 return std_req_stall;
293 }
294
295
296 if (len > dsize)
297 len = dsize;
298
299
300 return ast_vhub_reply(ep, NULL, len);
301}
302
303static struct usb_gadget_strings*
304ast_vhub_str_of_container(struct usb_gadget_string_container *container)
305{
306 return (struct usb_gadget_strings *)container->stash;
307}
308
309static int ast_vhub_collect_languages(struct ast_vhub *vhub, void *buf,
310 size_t size)
311{
312 int rc, hdr_len, nlangs, max_langs;
313 struct usb_gadget_strings *lang_str;
314 struct usb_gadget_string_container *container;
315 struct usb_string_descriptor *sdesc = buf;
316
317 nlangs = 0;
318 hdr_len = sizeof(struct usb_descriptor_header);
319 max_langs = (size - hdr_len) / sizeof(sdesc->wData[0]);
320 list_for_each_entry(container, &vhub->vhub_str_desc, list) {
321 if (nlangs >= max_langs)
322 break;
323
324 lang_str = ast_vhub_str_of_container(container);
325 sdesc->wData[nlangs++] = cpu_to_le16(lang_str->language);
326 }
327
328 rc = hdr_len + nlangs * sizeof(sdesc->wData[0]);
329 sdesc->bLength = rc;
330 sdesc->bDescriptorType = USB_DT_STRING;
331
332 return rc;
333}
334
335static struct usb_gadget_strings *ast_vhub_lookup_string(struct ast_vhub *vhub,
336 u16 lang_id)
337{
338 struct usb_gadget_strings *lang_str;
339 struct usb_gadget_string_container *container;
340
341 list_for_each_entry(container, &vhub->vhub_str_desc, list) {
342 lang_str = ast_vhub_str_of_container(container);
343 if (lang_str->language == lang_id)
344 return lang_str;
345 }
346
347 return NULL;
348}
349
350static int ast_vhub_rep_string(struct ast_vhub_ep *ep,
351 u8 string_id, u16 lang_id,
352 u16 len)
353{
354 int rc;
355 u8 buf[256];
356 struct ast_vhub *vhub = ep->vhub;
357 struct usb_gadget_strings *lang_str;
358
359 if (string_id == 0) {
360 rc = ast_vhub_collect_languages(vhub, buf, sizeof(buf));
361 } else {
362 lang_str = ast_vhub_lookup_string(vhub, lang_id);
363 if (!lang_str)
364 return std_req_stall;
365
366 rc = usb_gadget_get_string(lang_str, string_id, buf);
367 }
368
369 if (rc < 0 || rc >= AST_VHUB_EP0_MAX_PACKET)
370 return std_req_stall;
371
372
373 memcpy(ep->buf, buf, rc);
374 return ast_vhub_reply(ep, NULL, min_t(u16, rc, len));
375}
376
377enum std_req_rc ast_vhub_std_hub_request(struct ast_vhub_ep *ep,
378 struct usb_ctrlrequest *crq)
379{
380 struct ast_vhub *vhub = ep->vhub;
381 u16 wValue, wIndex, wLength;
382
383 wValue = le16_to_cpu(crq->wValue);
384 wIndex = le16_to_cpu(crq->wIndex);
385 wLength = le16_to_cpu(crq->wLength);
386
387
388 if (vhub->speed == USB_SPEED_UNKNOWN) {
389 u32 ustat = readl(vhub->regs + AST_VHUB_USBSTS);
390 if (ustat & VHUB_USBSTS_HISPEED)
391 vhub->speed = USB_SPEED_HIGH;
392 else
393 vhub->speed = USB_SPEED_FULL;
394 UDCDBG(vhub, "USB status=%08x speed=%s\n", ustat,
395 vhub->speed == USB_SPEED_HIGH ? "high" : "full");
396 }
397
398 switch ((crq->bRequestType << 8) | crq->bRequest) {
399
400 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
401 EPDBG(ep, "SET_ADDRESS: Got address %x\n", wValue);
402 writel(wValue, vhub->regs + AST_VHUB_CONF);
403 return std_req_complete;
404
405
406 case DeviceRequest | USB_REQ_GET_STATUS:
407 return ast_vhub_hub_dev_status(ep, wIndex, wValue);
408 case InterfaceRequest | USB_REQ_GET_STATUS:
409 return ast_vhub_simple_reply(ep, 0, 0);
410 case EndpointRequest | USB_REQ_GET_STATUS:
411 return ast_vhub_hub_ep_status(ep, wIndex, wValue);
412
413
414 case DeviceOutRequest | USB_REQ_SET_FEATURE:
415 return ast_vhub_hub_dev_feature(ep, wIndex, wValue, true);
416 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
417 return ast_vhub_hub_dev_feature(ep, wIndex, wValue, false);
418 case EndpointOutRequest | USB_REQ_SET_FEATURE:
419 return ast_vhub_hub_ep_feature(ep, wIndex, wValue, true);
420 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
421 return ast_vhub_hub_ep_feature(ep, wIndex, wValue, false);
422
423
424 case DeviceRequest | USB_REQ_GET_CONFIGURATION:
425 return ast_vhub_simple_reply(ep, 1);
426 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
427 if (wValue != 1)
428 return std_req_stall;
429 return std_req_complete;
430
431
432 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
433 switch (wValue >> 8) {
434 case USB_DT_DEVICE:
435 case USB_DT_CONFIG:
436 return ast_vhub_rep_desc(ep, wValue >> 8,
437 wLength);
438 case USB_DT_STRING:
439 return ast_vhub_rep_string(ep, wValue & 0xff,
440 wIndex, wLength);
441 }
442 return std_req_stall;
443
444
445 case DeviceRequest | USB_REQ_GET_INTERFACE:
446 return ast_vhub_simple_reply(ep, 0);
447 case DeviceOutRequest | USB_REQ_SET_INTERFACE:
448 if (wValue != 0 || wIndex != 0)
449 return std_req_stall;
450 return std_req_complete;
451 }
452 return std_req_stall;
453}
454
455static void ast_vhub_update_hub_ep1(struct ast_vhub *vhub,
456 unsigned int port)
457{
458
459 u32 reg = readl(vhub->regs + AST_VHUB_EP1_STS_CHG);
460 u32 pmask = (1 << (port + 1));
461 if (vhub->ports[port].change)
462 reg |= pmask;
463 else
464 reg &= ~pmask;
465 writel(reg, vhub->regs + AST_VHUB_EP1_STS_CHG);
466}
467
468static void ast_vhub_change_port_stat(struct ast_vhub *vhub,
469 unsigned int port,
470 u16 clr_flags,
471 u16 set_flags,
472 bool set_c)
473{
474 struct ast_vhub_port *p = &vhub->ports[port];
475 u16 prev;
476
477
478 prev = p->status;
479 p->status = (prev & ~clr_flags) | set_flags;
480 DDBG(&p->dev, "port %d status %04x -> %04x (C=%d)\n",
481 port + 1, prev, p->status, set_c);
482
483
484 if (set_c) {
485 u16 chg = p->status ^ prev;
486
487
488 chg &= USB_PORT_STAT_C_CONNECTION |
489 USB_PORT_STAT_C_ENABLE |
490 USB_PORT_STAT_C_SUSPEND |
491 USB_PORT_STAT_C_OVERCURRENT |
492 USB_PORT_STAT_C_RESET |
493 USB_PORT_STAT_C_L1;
494
495
496
497
498
499 if (p->status & USB_PORT_STAT_ENABLE)
500 chg &= ~USB_PORT_STAT_C_ENABLE;
501
502 p->change = chg;
503 ast_vhub_update_hub_ep1(vhub, port);
504 }
505}
506
507static void ast_vhub_send_host_wakeup(struct ast_vhub *vhub)
508{
509 u32 reg = readl(vhub->regs + AST_VHUB_CTRL);