1
2
3
4
5
6
7
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/of_gpio.h>
25#include <linux/regmap.h>
26#include <linux/dma-mapping.h>
27#include <linux/usb.h>
28#include <linux/usb/hcd.h>
29
30#include "vhub.h"
31
32void ast_vhub_dev_irq(struct ast_vhub_dev *d)
33{
34 u32 istat = readl(d->regs + AST_VHUB_DEV_ISR);
35
36 writel(istat, d->regs + AST_VHUB_DEV_ISR);
37
38 if (istat & VHUV_DEV_IRQ_EP0_IN_ACK_STALL)
39 ast_vhub_ep0_handle_ack(&d->ep0, true);
40 if (istat & VHUV_DEV_IRQ_EP0_OUT_ACK_STALL)
41 ast_vhub_ep0_handle_ack(&d->ep0, false);
42 if (istat & VHUV_DEV_IRQ_EP0_SETUP)
43 ast_vhub_ep0_handle_setup(&d->ep0);
44}
45
46static void ast_vhub_dev_enable(struct ast_vhub_dev *d)
47{
48 u32 reg, hmsk, i;
49
50 if (d->enabled)
51 return;
52
53
54 ast_vhub_reset_ep0(d);
55
56
57 reg = VHUB_DEV_EN_ENABLE_PORT |
58 VHUB_DEV_EN_EP0_IN_ACK_IRQEN |
59 VHUB_DEV_EN_EP0_OUT_ACK_IRQEN |
60 VHUB_DEV_EN_EP0_SETUP_IRQEN;
61 if (d->gadget.speed == USB_SPEED_HIGH)
62 reg |= VHUB_DEV_EN_SPEED_SEL_HIGH;
63 writel(reg, d->regs + AST_VHUB_DEV_EN_CTRL);
64
65
66 hmsk = VHUB_IRQ_DEVICE1 << d->index;
67 reg = readl(d->vhub->regs + AST_VHUB_IER);
68 reg |= hmsk;
69 writel(reg, d->vhub->regs + AST_VHUB_IER);
70
71
72 writel(d->ep0.buf_dma, d->regs + AST_VHUB_DEV_EP0_DATA);
73
74
75 for (i = 0; i < d->max_epns; i++) {
76 struct ast_vhub_ep *ep = d->epns[i];
77
78 if (ep && (ep->epn.stalled || ep->epn.wedged)) {
79 ep->epn.stalled = false;
80 ep->epn.wedged = false;
81 ast_vhub_update_epn_stall(ep);
82 }
83 }
84
85
86 d->wakeup_en = false;
87 d->enabled = true;
88}
89
90static void ast_vhub_dev_disable(struct ast_vhub_dev *d)
91{
92 u32 reg, hmsk;
93
94 if (!d->enabled)
95 return;
96
97
98 hmsk = VHUB_IRQ_DEVICE1 << d->index;
99 reg = readl(d->vhub->regs + AST_VHUB_IER);
100 reg &= ~hmsk;
101 writel(reg, d->vhub->regs + AST_VHUB_IER);
102
103
104 writel(0, d->regs + AST_VHUB_DEV_EN_CTRL);
105 d->gadget.speed = USB_SPEED_UNKNOWN;
106 d->enabled = false;
107}
108
109static int ast_vhub_dev_feature(struct ast_vhub_dev *d,
110 u16 wIndex, u16 wValue,
111 bool is_set)
112{
113 DDBG(d, "%s_FEATURE(dev val=%02x)\n",
114 is_set ? "SET" : "CLEAR", wValue);
115
116 if (wValue != USB_DEVICE_REMOTE_WAKEUP)
117 return std_req_driver;
118
119 d->wakeup_en = is_set;
120
121 return std_req_complete;
122}
123
124static int ast_vhub_ep_feature(struct ast_vhub_dev *d,
125 u16 wIndex, u16 wValue, bool is_set)
126{
127 struct ast_vhub_ep *ep;
128 int ep_num;
129
130 ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
131 DDBG(d, "%s_FEATURE(ep%d val=%02x)\n",
132 is_set ? "SET" : "CLEAR", ep_num, wValue);
133 if (ep_num == 0)
134 return std_req_complete;
135 if (ep_num >= d->max_epns || !d->epns[ep_num - 1])
136 return std_req_stall;
137 if (wValue != USB_ENDPOINT_HALT)
138 return std_req_driver;
139
140 ep = d->epns[ep_num - 1];
141 if (WARN_ON(!ep))
142 return std_req_stall;
143
144 if (!ep->epn.enabled || !ep->ep.desc || ep->epn.is_iso ||
145 ep->epn.is_in != !!(wIndex & USB_DIR_IN))
146 return std_req_stall;
147
148 DDBG(d, "%s stall on EP %d\n",
149 is_set ? "setting" : "clearing", ep_num);
150 ep->epn.stalled = is_set;
151 ast_vhub_update_epn_stall(ep);
152
153 return std_req_complete;
154}
155
156static int ast_vhub_dev_status(struct ast_vhub_dev *d,
157 u16 wIndex, u16 wValue)
158{
159 u8 st0;
160
161 DDBG(d, "GET_STATUS(dev)\n");
162
163 st0 = d->gadget.is_selfpowered << USB_DEVICE_SELF_POWERED;
164 if (d->wakeup_en)
165 st0 |= 1 << USB_DEVICE_REMOTE_WAKEUP;
166
167 return ast_vhub_simple_reply(&d->ep0, st0, 0);
168}
169
170static int ast_vhub_ep_status(struct ast_vhub_dev *d,
171 u16 wIndex, u16 wValue)
172{
173 int ep_num = wIndex & USB_ENDPOINT_NUMBER_MASK;
174 struct ast_vhub_ep *ep;
175 u8 st0 = 0;
176
177 DDBG(d, "GET_STATUS(ep%d)\n", ep_num);
178
179 if (ep_num >= d->max_epns)
180 return std_req_stall;
181 if (ep_num != 0) {
182 ep = d->epns[ep_num - 1];
183 if (!ep)
184 return std_req_stall;
185 if (!ep->epn.enabled || !ep->ep.desc || ep->epn.is_iso ||
186 ep->epn.is_in != !!(wIndex & USB_DIR_IN))
187 return std_req_stall;
188 if (ep->epn.stalled)
189 st0 |= 1 << USB_ENDPOINT_HALT;
190 }
191
192 return ast_vhub_simple_reply(&d->ep0, st0, 0);
193}
194
195static void ast_vhub_dev_set_address(struct ast_vhub_dev *d, u8 addr)
196{
197 u32 reg;
198
199 DDBG(d, "SET_ADDRESS: Got address %x\n", addr);
200
201 reg = readl(d->regs + AST_VHUB_DEV_EN_CTRL);
202 reg &= ~VHUB_DEV_EN_ADDR_MASK;
203 reg |= VHUB_DEV_EN_SET_ADDR(addr);
204 writel(reg, d->regs + AST_VHUB_DEV_EN_CTRL);
205}
206
207int ast_vhub_std_dev_request(struct ast_vhub_ep *ep,
208 struct usb_ctrlrequest *crq)
209{
210 struct ast_vhub_dev *d = ep->dev;
211 u16 wValue, wIndex;
212
213
214 if (!d->driver || !d->enabled) {
215 EPDBG(ep,
216 "Device is wrong state driver=%p enabled=%d\n",
217 d->driver, d->enabled);
218 return std_req_stall;
219 }
220
221
222
223
224
225
226
227
228 if (d->gadget.speed == USB_SPEED_UNKNOWN) {
229 d->gadget.speed = ep->vhub->speed;
230 if (d->gadget.speed > d->driver->max_speed)
231 d->gadget.speed = d->driver->max_speed;
232 DDBG(d, "fist packet, captured speed %d\n",
233 d->gadget.speed);
234 }
235
236 wValue = le16_to_cpu(crq->wValue);
237 wIndex = le16_to_cpu(crq->wIndex);
238
239 switch ((crq->bRequestType << 8) | crq->bRequest) {
240
241 case DeviceOutRequest | USB_REQ_SET_ADDRESS:
242 ast_vhub_dev_set_address(d, wValue);
243 return std_req_complete;
244
245
246 case DeviceRequest | USB_REQ_GET_STATUS:
247 return ast_vhub_dev_status(d, wIndex, wValue);
248 case InterfaceRequest | USB_REQ_GET_STATUS:
249 return ast_vhub_simple_reply(ep, 0, 0);
250 case EndpointRequest | USB_REQ_GET_STATUS:
251 return ast_vhub_ep_status(d, wIndex, wValue);
252
253
254 case DeviceOutRequest | USB_REQ_SET_FEATURE:
255 return ast_vhub_dev_feature(d, wIndex, wValue, true);
256 case DeviceOutRequest | USB_REQ_CLEAR_FEATURE:
257 return ast_vhub_dev_feature(d, wIndex, wValue, false);
258 case EndpointOutRequest | USB_REQ_SET_FEATURE:
259 return ast_vhub_ep_feature(d, wIndex, wValue, true);
260 case EndpointOutRequest | USB_REQ_CLEAR_FEATURE:
261 return ast_vhub_ep_feature(d, wIndex, wValue, false);
262 }
263 return std_req_driver;
264}
265
266static int ast_vhub_udc_wakeup(struct usb_gadget* gadget)
267{
268 struct ast_vhub_dev *d = to_ast_dev(gadget);
269 unsigned long flags;
270 int rc = -EINVAL;
271
272 spin_lock_irqsave(&d->vhub->lock, flags);
273 if (!d->wakeup_en)
274 goto err;
275
276 DDBG(d, "Device initiated wakeup\n");
277
278
279 ast_vhub_hub_wake_all(d->vhub);
280 rc = 0;
281 err:
282 spin_unlock_irqrestore(&d->vhub->lock, flags);
283 return rc;
284}
285
286static int ast_vhub_udc_get_frame(struct usb_gadget* gadget)
287{
288 struct ast_vhub_dev *d = to_ast_dev(gadget);
289
290 return (readl(d->vhub->regs + AST_VHUB_USBSTS) >> 16) & 0x7ff;
291}
292
293static void ast_vhub_dev_nuke(struct ast_vhub_dev *d)
294{
295 unsigned int i;
296
297 for (i = 0; i < d->max_epns; i++) {
298 if (!d->epns[i])
299 continue;
300 ast_vhub_nuke(d->epns[i], -ESHUTDOWN);
301 }
302}
303
304static int ast_vhub_udc_pullup(struct usb_gadget* gadget, int on)
305{
306 struct ast_vhub_dev *d = to_ast_dev(gadget);
307 unsigned long flags;
308
309 spin_lock_irqsave(&d->vhub->lock, flags);
310
311 DDBG(d, "pullup(%d)\n", on);
312
313
314 ast_vhub_device_connect(d->vhub, d->index, on);
315
316
317
318
319
320 if (d->enabled) {
321 ast_vhub_dev_nuke(d);
322 ast_vhub_dev_disable(d);
323 }
324
325 spin_unlock_irqrestore(&d->vhub->lock, flags);
326
327 return 0;
328}
329
330static int ast_vhub_udc_start(struct usb_gadget *gadget,
331 struct usb_gadget_driver *driver)
332{
333 struct ast_vhub_dev *d = to_ast_dev(gadget);
334 unsigned long flags;
335
336 spin_lock_irqsave(&d->vhub->lock, flags);
337
338 DDBG(d, "start\n");
339
340
341 d->driver = driver;
342 d->gadget.is_selfpowered = 1;
343
344 spin_unlock_irqrestore(&d->vhub->lock, flags);
345
346 return 0;
347}
348
349static struct usb_ep *ast_vhub_udc_match_ep(struct usb_gadget *gadget,
350 struct usb_endpoint_descriptor *desc,
351 struct usb_ss_ep_comp_descriptor *ss)
352{
353 struct ast_vhub_dev *d = to_ast_dev(gadget);
354 struct ast_vhub_ep *ep;
355 struct usb_ep *u_ep;
356 unsigned int max, addr, i;
357
358 DDBG(d, "Match EP type %d\n", usb_endpoint_type(desc));
359
360
361
362
363
364
365
366 list_for_each_entry(u_ep, &gadget->ep_list, ep_list) {
367 if (usb_gadget_ep_match_desc(gadget, u_ep, desc, ss)) {
368 DDBG(d, " -> using existing EP%d\n",
369 to_ast_ep(u_ep)->d_idx);
370 return u_ep;
371 }
372 }
373
374
375
376
377
378
379 switch(usb_endpoint_type(desc)) {
380 case USB_ENDPOINT_XFER_CONTROL:
381
382 return NULL;
383 case USB_ENDPOINT_XFER_ISOC:
384
385 if (gadget_is_dualspeed(gadget))
386 max = 1024;
387 else
388 max = 1023;
389 break;
390 case USB_ENDPOINT_XFER_BULK:
391 if (gadget_is_dualspeed(gadget))
392 max = 512;
393 else
394 max = 64;
395 break;
396 case USB_ENDPOINT_XFER_INT:
397 if (gadget_is_dualspeed(gadget))
398 max = 1024;
399 else
400 max = 64;
401 break;
402 }
403 if (usb_endpoint_maxp(desc) > max)
404 return NULL;
405
406
407
408
409
410
411
412
413
414 for (i = 0; i < d->max_epns; i++)
415 if (d->epns[i] == NULL)
416 break;
417 if (i >= d->max_epns)
418 return NULL;
419 addr = i + 1;
420
421
422
423
424
425 ep = ast_vhub_alloc_epn(d, addr);
426 if (!ep)
427 return NULL;
428 DDBG(d, "Allocated epn#%d for port EP%d\n",
429 ep->epn.g_idx, addr);
430
431 return &ep->ep;
432}
433
434static int ast_vhub_udc_stop(struct usb_gadget *gadget)
435{
436 struct ast_vhub_dev *d = to_ast_dev(gadget);
437 unsigned long flags;
438
439 spin_lock_irqsave(&d->vhub->lock, flags);
440
441 DDBG(d, "stop\n");
442
443 d->driver = NULL;
444 d->gadget.speed = USB_SPEED_UNKNOWN;
445
446 ast_vhub_dev_nuke(d);
447
448 if (d->enabled)
449 ast_vhub_dev_disable(d);
450
451 spin_unlock_irqrestore(&d->vhub->lock, flags);
452
453 return 0;
454}
455
456static const struct usb_gadget_ops ast_vhub_udc_ops = {
457 .get_frame = ast_vhub_udc_get_frame,
458 .wakeup = ast_vhub_udc_wakeup,
459 .pullup = ast_vhub_udc_pullup,
460 .udc_start = ast_vhub_udc_start,
461 .udc_stop = ast_vhub_udc_stop,
462 .match_ep = ast_vhub_udc_match_ep,
463};
464
465void ast_vhub_dev_suspend(struct ast_vhub_dev *d)
466{
467 if (d->driver && d->driver->suspend) {
468 spin_unlock(&d->vhub->lock);
469 d->driver->suspend(&d->gadget);
470 spin_lock(&d->vhub->lock);
471 }
472}
473
474void ast_vhub_dev_resume(struct ast_vhub_dev *d)
475{
476 if (d->driver && d->driver->resume) {
477 spin_unlock(&d->vhub->lock);
478 d->driver->resume(&d->gadget);
479 spin_lock(&d->vhub->lock);
480 }
481}
482
483void ast_vhub_dev_reset(struct ast_vhub_dev *d)
484{
485
486 if (!d->driver) {
487 ast_vhub_dev_disable(d);
488 return;
489 }
490
491
492 if (!d->enabled) {
493 DDBG(d, "Reset of disabled device, enabling...\n");
494 ast_vhub_dev_enable(d);
495 } else {
496 DDBG(d, "Reset of enabled device, resetting...\n");
497 spin_unlock(&d->vhub->lock);
498 usb_gadget_udc_reset(&d->gadget, d->driver);
499 spin_lock(&d->vhub->lock);
500
501
502
503
504
505 ast_vhub_dev_disable(d);
506 ast_vhub_dev_enable(d);
507 }
508}
509
510void ast_vhub_del_dev(struct ast_vhub_dev *d)
511{
512 unsigned long flags;
513
514 spin_lock_irqsave(&d->vhub->lock, flags);
515 if (!d->registered) {
516 spin_unlock_irqrestore(&d->vhub->lock, flags);
517 return;
518 }
519 d->registered = false;
520 spin_unlock_irqrestore(&d->vhub->lock, flags);
521
522 usb_del_gadget_udc(&d->gadget);
523 device_unregister(d->port_dev);
524 kfree(d->epns);
525}
526
527static void ast_vhub_dev_release(struct device *dev)
528{
529 kfree(dev);
530}
531
532int ast_vhub_init_dev(struct ast_vhub *vhub, unsigned int idx)
533{
534 struct ast_vhub_dev *d = &vhub->ports[idx].dev;
535 struct device *parent = &vhub->pdev->dev;
536 int rc;
537
538 d->vhub = vhub;
539 d->index = idx;
540 d->name = devm_kasprintf(parent, GFP_KERNEL, "port%d", idx+1);
541 d->regs = vhub->regs + 0x100 + 0x10 * idx;
542
543 ast_vhub_init_ep0(vhub, &d->ep0, d);
544
545
546
547
548
549 d->max_epns = min_t(u32, vhub->max_epns, 30);
550 d->epns = kcalloc(d->max_epns, sizeof(*d->epns), GFP_KERNEL);
551 if (!d->epns)
552 return -ENOMEM;
553
554
555
556
557
558
559 d->port_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
560 if (!d->port_dev) {
561 rc = -ENOMEM;
562 goto fail_alloc;
563 }
564 device_initialize(d->port_dev);
565 d->port_dev->release = ast_vhub_dev_release;
566 d->port_dev->parent = parent;
567 dev_set_name(d->port_dev, "%s:p%d", dev_name(parent), idx + 1);
568 rc = device_add(d->port_dev);
569 if (rc)
570 goto fail_add;
571
572
573 INIT_LIST_HEAD(&d->gadget.ep_list);
574 d->gadget.ops = &ast_vhub_udc_ops;
575 d->gadget.ep0 = &d->ep0.ep;
576 d->gadget.name = KBUILD_MODNAME;
577 if (vhub->force_usb1)
578 d->gadget.max_speed = USB_SPEED_FULL;
579 else
580 d->gadget.max_speed = USB_SPEED_HIGH;
581 d->gadget.speed = USB_SPEED_UNKNOWN;
582 d->gadget.dev.of_node = vhub->pdev->dev.of_node;
583
584 rc = usb_add_gadget_udc(d->port_dev, &d->gadget);
585 if (rc != 0)
586 goto fail_udc;
587 d->registered = true;
588
589 return 0;
590 fail_udc:
591 device_del(d->port_dev);
592 fail_add:
593 put_device(d->port_dev);
594 fail_alloc:
595 kfree(d->epns);
596
597 return rc;
598}
599