import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / misc / mediatek / xhci_test / mtk-usb-hcd.c
1 //#include "mtk-usb-hcd.h"
2 //#include <linux/usb.h>
3 //#include <linux/usb/hcd.h>
4 #include <linux/slab.h>
5 #include <linux/dma-mapping.h>
6 #include <asm/unaligned.h>
7 #include "xhci.h"
8 #include "mtk-test.h"
9
10 #if 0
11 /* Device for a quirk */
12 #define PCI_VENDOR_ID_FRESCO_LOGIC 0x1b73
13 #define PCI_DEVICE_ID_FRESCO_LOGIC_PDK 0x1000
14 #endif
15
16 /* FIXME tune these based on pool statistics ... */
17 static const size_t pool_max [HCD_BUFFER_POOLS] = {
18 /* platforms without dma-friendly caches might need to
19 * prevent cacheline sharing...
20 */
21 32,
22 128,
23 512,
24 PAGE_SIZE / 2
25 /* bigger --> allocate pages */
26 };
27
28 #if 0
29 /**
30 * hcd_buffer_create - initialize buffer pools
31 * @hcd: the bus whose buffer pools are to be initialized
32 * Context: !in_interrupt()
33 *
34 * Call this as part of initializing a host controller that uses the dma
35 * memory allocators. It initializes some pools of dma-coherent memory that
36 * will be shared by all drivers using that controller, or returns a negative
37 * errno value on error.
38 *
39 * Call hcd_buffer_destroy() to clean up after using those pools.
40 */
41 int hcd_buffer_create(struct usb_hcd *hcd)
42 {
43 char name[16];
44 int i, size;
45
46 if (!hcd->self.controller->dma_mask &&
47 !(hcd->driver->flags & HCD_LOCAL_MEM))
48 return 0;
49
50 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
51 size = pool_max[i];
52 if (!size)
53 continue;
54 snprintf(name, sizeof name, "buffer-%d", size);
55 hcd->pool[i] = dma_pool_create(name, hcd->self.controller,
56 size, size, 0);
57 if (!hcd->pool [i]) {
58 hcd_buffer_destroy(hcd);
59 return -ENOMEM;
60 }
61 }
62 return 0;
63 }
64 #endif
65 #if 0
66 /**
67 * hcd_buffer_destroy - deallocate buffer pools
68 * @hcd: the bus whose buffer pools are to be destroyed
69 * Context: !in_interrupt()
70 *
71 * This frees the buffer pools created by hcd_buffer_create().
72 */
73 void hcd_buffer_destroy(struct usb_hcd *hcd)
74 {
75 int i;
76
77 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
78 struct dma_pool *pool = hcd->pool[i];
79 if (pool) {
80 dma_pool_destroy(pool);
81 hcd->pool[i] = NULL;
82 }
83 }
84 }
85 #endif
86 #if 0
87 /**
88 * usb_hcd_irq - hook IRQs to HCD framework (bus glue)
89 * @irq: the IRQ being raised
90 * @__hcd: pointer to the HCD whose IRQ is being signaled
91 *
92 * If the controller isn't HALTed, calls the driver's irq handler.
93 * Checks whether the controller is now dead.
94 */
95 irqreturn_t usb_hcd_irq (int irq, void *__hcd)
96 {
97 struct usb_hcd *hcd = __hcd;
98 unsigned long flags;
99 irqreturn_t rc;
100
101 /* IRQF_DISABLED doesn't work correctly with shared IRQs
102 * when the first handler doesn't use it. So let's just
103 * assume it's never used.
104 */
105 local_irq_save(flags);
106 if (unlikely(hcd->state == HC_STATE_HALT ||
107 !test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))) {
108 rc = IRQ_NONE;
109 } else if (hcd->driver->irq(hcd) == IRQ_NONE) {
110 rc = IRQ_NONE;
111 } else {
112 set_bit(HCD_FLAG_SAW_IRQ, &hcd->flags);
113
114 if (unlikely(hcd->state == HC_STATE_HALT))
115 usb_hc_died(hcd);
116 rc = IRQ_HANDLED;
117 }
118
119 local_irq_restore(flags);
120 return rc;
121 }
122 #endif
123 #if 0
124 /* Returns 1 if @usb_bus is WUSB, 0 otherwise */
125 static unsigned usb_bus_is_wusb(struct usb_bus *bus)
126 {
127 struct usb_hcd *hcd = container_of(bus, struct usb_hcd, self);
128 return hcd->wireless;
129 }
130 #endif
131
132 struct usb_device *mtk_usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1){
133 struct usb_device *dev;
134 struct usb_hcd *usb_hcd = container_of(bus, struct usb_hcd, self);
135 unsigned root_hub = 0;
136
137 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
138 if (!dev)
139 return NULL;
140
141 if (!usb_get_hcd(bus_to_hcd(bus))) {
142 kfree(dev);
143 return NULL;
144 }
145 device_initialize(&dev->dev);
146 dev->children = kzalloc(31 * sizeof(struct usb_device *), GFP_KERNEL);
147
148 /* Save readable and stable topology id, distinguishing devices
149 * by location for diagnostics, tools, driver model, etc. The
150 * string is a path along hub ports, from the root. Each device's
151 * dev->devpath will be stable until USB is re-cabled, and hubs
152 * are often labeled with these port numbers. The name isn't
153 * as stable: bus->busnum changes easily from modprobe order,
154 * cardbus or pci hotplugging, and so on.
155 */
156 if (unlikely(!parent)) {
157 dev->devpath[0] = '0';
158 dev->route = 0;
159
160 dev->dev.parent = bus->controller;
161 dev_set_name(&dev->dev, "usb%d", bus->busnum);
162 root_hub = 1;
163 } else {
164 /* match any labeling on the hubs; it's one-based */
165 if (parent->devpath[0] == '0') {
166 snprintf(dev->devpath, sizeof dev->devpath, "%d", port1);
167 /* Root ports are not counted in route string */
168 dev->route = 0;
169 printk(KERN_DEBUG "device attached on roothub\n");
170 } else {
171 snprintf(dev->devpath, sizeof dev->devpath, "%s.%d", parent->devpath, port1);
172 /* Route string assumes hubs have less than 16 ports */
173 if (port1 < 15)
174 dev->route = parent->route +
175 (port1 << ((parent->level - 1)*4));
176 else
177 dev->route = parent->route +
178 (15 << ((parent->level - 1)*4));
179 printk(KERN_DEBUG "device route string %d\n", dev->route);
180 printk(KERN_DEBUG "parent level %d\n", parent->level);
181 printk(KERN_DEBUG "parent route string %d\n", parent->route);
182 }
183
184 dev->dev.parent = &parent->dev;
185 dev_set_name(&dev->dev, "%d-%s", bus->busnum, dev->devpath);
186
187 /* hub driver sets up TT records */
188 }
189 dev->portnum = port1;
190 dev->bus = bus;
191 dev->parent = parent;
192 if (root_hub) /* Root hub always ok [and always wired] */
193 dev->authorized = 1;
194 else {
195 dev->authorized = usb_hcd->authorized_default;
196 // dev->wusb = usb_bus_is_wusb(bus)? 1 : 0;
197 }
198
199 dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;
200 dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;
201 dev->ep0.enabled = 1;
202 dev->ep_in[0] = &dev->ep0;
203 dev->ep_out[0] = &dev->ep0;
204 #if 0
205 /* ep0 maxpacket comes later, from device descriptor */
206 usb_enable_endpoint(dev, &dev->ep0, false);
207 #endif
208 dev->can_submit = 1;
209
210 return dev;
211 }
212 #if 0
213 void *hcd_buffer_alloc(
214 struct usb_bus *bus,
215 size_t size,
216 gfp_t mem_flags,
217 dma_addr_t *dma
218 )
219 {
220 struct usb_hcd *hcd = bus_to_hcd(bus);
221 int i;
222
223 /* some USB hosts just use PIO */
224 if (!bus->controller->dma_mask &&
225 !(hcd->driver->flags & HCD_LOCAL_MEM)) {
226 *dma = ~(dma_addr_t) 0;
227 return kmalloc(size, mem_flags);
228 }
229
230 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
231 if (size <= pool_max [i])
232 return dma_pool_alloc(hcd->pool [i], mem_flags, dma);
233 }
234 return dma_alloc_coherent(hcd->self.controller, size, dma, mem_flags);
235 }
236
237 void hcd_buffer_free(
238 struct usb_bus *bus,
239 size_t size,
240 void *addr,
241 dma_addr_t dma
242 )
243 {
244 struct usb_hcd *hcd = bus_to_hcd(bus);
245 int i;
246
247 if (!addr)
248 return;
249
250 if (!bus->controller->dma_mask &&
251 !(hcd->driver->flags & HCD_LOCAL_MEM)) {
252 kfree(addr);
253 return;
254 }
255
256 for (i = 0; i < HCD_BUFFER_POOLS; i++) {
257 if (size <= pool_max [i]) {
258 dma_pool_free(hcd->pool [i], addr, dma);
259 return;
260 }
261 }
262 dma_free_coherent(hcd->self.controller, size, addr, dma);
263 }
264 #endif
265
266 /*
267 * Some usb host controllers can only perform dma using a small SRAM area.
268 * The usb core itself is however optimized for host controllers that can dma
269 * using regular system memory - like pci devices doing bus mastering.
270 *
271 * To support host controllers with limited dma capabilites we provide dma
272 * bounce buffers. This feature can be enabled using the HCD_LOCAL_MEM flag.
273 * For this to work properly the host controller code must first use the
274 * function dma_declare_coherent_memory() to point out which memory area
275 * that should be used for dma allocations.
276 *
277 * The HCD_LOCAL_MEM flag then tells the usb code to allocate all data for
278 * dma using dma_alloc_coherent() which in turn allocates from the memory
279 * area pointed out with dma_declare_coherent_memory().
280 *
281 * So, to summarize...
282 *
283 * - We need "local" memory, canonical example being
284 * a small SRAM on a discrete controller being the
285 * only memory that the controller can read ...
286 * (a) "normal" kernel memory is no good, and
287 * (b) there's not enough to share
288 *
289 * - The only *portable* hook for such stuff in the
290 * DMA framework is dma_declare_coherent_memory()
291 *
292 * - So we use that, even though the primary requirement
293 * is that the memory be "local" (hence addressible
294 * by that device), not "coherent".
295 *
296 */
297
298 static int hcd_alloc_coherent(struct usb_bus *bus,
299 gfp_t mem_flags, dma_addr_t *dma_handle,
300 void **vaddr_handle, size_t size,
301 enum dma_data_direction dir)
302 {
303 unsigned char *vaddr;
304
305 vaddr = hcd_buffer_alloc(bus, size + sizeof(vaddr),
306 mem_flags, dma_handle);
307 if (!vaddr)
308 return -ENOMEM;
309
310 /*
311 * Store the virtual address of the buffer at the end
312 * of the allocated dma buffer. The size of the buffer
313 * may be uneven so use unaligned functions instead
314 * of just rounding up. It makes sense to optimize for
315 * memory footprint over access speed since the amount
316 * of memory available for dma may be limited.
317 */
318 put_unaligned((unsigned long)*vaddr_handle,
319 (unsigned long *)(vaddr + size));
320
321 if (dir == DMA_TO_DEVICE)
322 memcpy(vaddr, *vaddr_handle, size);
323
324 *vaddr_handle = vaddr;
325 return 0;
326 }
327
328 void hcd_free_coherent(struct usb_bus *bus, dma_addr_t *dma_handle,
329 void **vaddr_handle, size_t size,
330 enum dma_data_direction dir)
331 {
332 unsigned char *vaddr = *vaddr_handle;
333
334 vaddr = (void *)get_unaligned((unsigned long *)(vaddr + size));
335
336 if (dir == DMA_FROM_DEVICE)
337 memcpy(vaddr, *vaddr_handle, size);
338
339 hcd_buffer_free(bus, size + sizeof(vaddr), *vaddr_handle, *dma_handle);
340
341 *vaddr_handle = vaddr;
342 *dma_handle = 0;
343 }
344
345 #if 0
346 void unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
347 {
348 enum dma_data_direction dir;
349
350 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
351 hcd_free_coherent(urb->dev->bus,
352 &urb->transfer_dma,
353 &urb->transfer_buffer,
354 urb->transfer_buffer_length,
355 dir);
356
357 }
358
359
360 int map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
361 gfp_t mem_flags)
362 {
363 enum dma_data_direction dir;
364 int ret = 0;
365 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
366 /* Map the URB's buffers for DMA access.
367 * Lower level HCD code should use *_dma exclusively,
368 * unless it uses pio or talks to another transport,
369 * or uses the provided scatter gather list for bulk.
370 */
371 ret = hcd_alloc_coherent(
372 urb->dev->bus, mem_flags,
373 &urb->transfer_dma,
374 &urb->transfer_buffer,
375 urb->transfer_buffer_length,
376 dir);
377
378 if (ret == 0)
379 urb->transfer_flags |= URB_MAP_LOCAL;
380 return ret;
381 }
382
383 void rh_port_clear_change(struct xhci_hcd *xhci, int port_id){
384 u32 temp,status;
385 u32 __iomem *addr;
386 port_id--;
387 status = 0;
388
389 addr = &xhci->op_regs->port_status_base + NUM_PORT_REGS*(port_id & 0xff);
390 temp = xhci_readl(xhci, addr);
391 xhci_dbg(xhci, "to clear port change, actual port %d status = 0x%x\n", port_id, temp);
392 temp = mtktest_xhci_port_state_to_clear_change(temp);
393 xhci_writel(xhci, temp, addr);
394 temp = xhci_readl(xhci, addr);
395 xhci_dbg(xhci, "clear port change, actual port %d status = 0x%x\n", port_id, temp);
396 }
397
398 int rh_get_port_status(struct xhci_hcd *xhci, int port_id){
399 u32 temp,status;
400 u32 __iomem *addr;
401
402 port_id--;
403 status = 0;
404
405 addr = &xhci->op_regs->port_status_base + NUM_PORT_REGS*(port_id & 0xff);
406 temp = xhci_readl(xhci, addr);
407 xhci_dbg(xhci, "get port status, actual port %d status = 0x%x\n", port_id, temp);
408
409 /* wPortChange bits */
410 if (temp & PORT_CSC)
411 status |= USB_PORT_STAT_C_CONNECTION << 16;
412 if (temp & PORT_PEC)
413 status |= USB_PORT_STAT_C_ENABLE << 16;
414 if ((temp & PORT_OCC))
415 status |= USB_PORT_STAT_C_OVERCURRENT << 16;
416 if ((temp & PORT_RC))
417 status |= USB_PORT_STAT_C_RESET << 16;
418 if ((temp & PORT_PLC))
419 status |= USB_PORT_STAT_C_SUSPEND << 16;
420 /*
421 * FIXME ignoring suspend, reset, and USB 2.1/3.0 specific
422 * changes
423 */
424 if (temp & PORT_CONNECT) {
425 status |= USB_PORT_STAT_CONNECTION;
426 status |= mtktest_xhci_port_speed(temp);
427 }
428 if (temp & PORT_PE)
429 status |= USB_PORT_STAT_ENABLE;
430 if (temp & PORT_OC)
431 status |= USB_PORT_STAT_OVERCURRENT;
432 if (temp & PORT_RESET)
433 status |= USB_PORT_STAT_RESET;
434 if (temp & PORT_POWER)
435 status |= USB_PORT_STAT_POWER;
436 xhci_dbg(xhci, "Get port status returned 0x%x\n", status);
437 temp = mtktest_xhci_port_state_to_neutral(temp);
438 xhci_writel(xhci, temp, addr);
439 temp = xhci_readl(xhci, addr);
440 xhci_dbg(xhci, "clear port change, actual port %d status = 0x%x\n", port_id, temp);
441 #if 0
442 put_unaligned(cpu_to_le32(status), (__le32 *) buf);
443 #endif
444 return status;
445 }
446 #endif
447
448 int mtk_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags){
449 enum dma_data_direction dir;
450 int ret = 0;
451 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
452 /* Map the URB's buffers for DMA access.
453 * Lower level HCD code should use *_dma exclusively,
454 * unless it uses pio or talks to another transport,
455 * or uses the provided scatter gather list for bulk.
456 */
457 ret = hcd_alloc_coherent(
458 urb->dev->bus, mem_flags,
459 &urb->transfer_dma,
460 &urb->transfer_buffer,
461 urb->transfer_buffer_length,
462 dir);
463
464 if (ret == 0)
465 urb->transfer_flags |= URB_MAP_LOCAL;
466 return ret;
467 }
468 void mtk_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb){
469 enum dma_data_direction dir;
470
471 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
472 hcd_free_coherent(urb->dev->bus,
473 &urb->transfer_dma,
474 &urb->transfer_buffer,
475 urb->transfer_buffer_length,
476 dir);
477 }
478
479 /**
480 * usb_alloc_dev - usb device constructor (usbcore-internal)
481 * @parent: hub to which device is connected; null to allocate a root hub
482 * @bus: bus used to access the device
483 * @port1: one-based index of port; ignored for root hubs
484 * Context: !in_interrupt()
485 *
486 * Only hub drivers (including virtual root hub drivers for host
487 * controllers) should ever call this.
488 *
489 * This call may not be used in a non-sleeping context.
490 */
491 struct usb_device *mtk_usb_alloc_rhdev(struct usb_device *parent,
492 struct usb_bus *bus, unsigned port1)
493 {
494 struct usb_device *dev;
495 struct usb_hcd *usb_hcd = container_of(bus, struct usb_hcd, self);
496 unsigned root_hub = 0;
497
498 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
499 if (!dev)
500 return NULL;
501
502 if (!usb_get_hcd(bus_to_hcd(bus))) {
503 kfree(dev);
504 return NULL;
505 }
506 device_initialize(&dev->dev);
507 dev->children = kzalloc(31 * sizeof(struct usb_device *), GFP_KERNEL);
508 dev->dev.dma_mask = bus->controller->dma_mask;
509 atomic_set(&dev->urbnum, 0);
510 dev->can_submit = 1;
511
512 /* Save readable and stable topology id, distinguishing devices
513 * by location for diagnostics, tools, driver model, etc. The
514 * string is a path along hub ports, from the root. Each device's
515 * dev->devpath will be stable until USB is re-cabled, and hubs
516 * are often labeled with these port numbers. The name isn't
517 * as stable: bus->busnum changes easily from modprobe order,
518 * cardbus or pci hotplugging, and so on.
519 */
520 if (unlikely(!parent)) {
521 dev->devpath[0] = '0';
522 dev->route = 0;
523 dev->dev.parent = bus->controller;
524 dev_set_name(&dev->dev, "usb%d", bus->busnum);
525 root_hub = 1;
526 }
527
528 dev->portnum = port1;
529 dev->bus = bus;
530 dev->parent = parent;
531
532 dev->authorized = 1;
533 return dev;
534 }
535
536
537 /**
538 * usb_add_hcd - finish generic HCD structure initialization and register
539 * @hcd: the usb_hcd structure to initialize
540 * @irqnum: Interrupt line to allocate
541 * @irqflags: Interrupt type flags
542 *
543 * Finish the remaining parts of generic HCD initialization: allocate the
544 * buffers of consistent memory, register the bus, request the IRQ line,
545 * and call the driver's reset() and start() routines.
546 */
547 int mtk_usb_add_hcd(struct usb_hcd *hcd,
548 unsigned int irqnum, unsigned long irqflags)
549 {
550 int retval;
551 struct usb_device *rhdev;
552 dev_info(hcd->self.controller, "%s\n", hcd->product_desc);
553
554 hcd->authorized_default = hcd->wireless? 0 : 1;
555 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
556
557 /* HC is in reset state, but accessible. Now do the one-time init,
558 * bottom up so that hcds can customize the root hubs before khubd
559 * starts talking to them. (Note, bus id is assigned early too.)
560 */
561 if ((retval = hcd_buffer_create(hcd)) != 0) {
562 dev_dbg(hcd->self.controller, "pool alloc failed\n");
563 return retval;
564 }
565
566 if ((rhdev = mtk_usb_alloc_rhdev(NULL, &hcd->self, 0)) == NULL) {
567 dev_err(hcd->self.controller, "unable to allocate root hub\n");
568 retval = -ENOMEM;
569 goto err_allocate_root_hub;
570 }
571 hcd->self.root_hub = rhdev;
572 #if 0
573 if ((retval = usb_register_bus(&hcd->self)) < 0)
574 goto err_register_bus;
575
576 if ((rhdev = usb_alloc_dev(NULL, &hcd->self, 0)) == NULL) {
577 dev_err(hcd->self.controller, "unable to allocate root hub\n");
578 retval = -ENOMEM;
579 goto err_allocate_root_hub;
580 }
581
582 switch (hcd->driver->flags & HCD_MASK) {
583 case HCD_USB11:
584 rhdev->speed = USB_SPEED_FULL;
585 break;
586 case HCD_USB2:
587 rhdev->speed = USB_SPEED_HIGH;
588 break;
589 case HCD_USB3:
590 rhdev->speed = USB_SPEED_SUPER;
591 break;
592 default:
593 goto err_allocate_root_hub;
594 }
595 hcd->self.root_hub = rhdev;
596
597 /* wakeup flag init defaults to "everything works" for root hubs,
598 * but drivers can override it in reset() if needed, along with
599 * recording the overall controller's system wakeup capability.
600 */
601 device_init_wakeup(&rhdev->dev, 1);
602 #endif
603
604 /* "reset" is misnamed; its role is now one-time init. the controller
605 * should already have been reset (and boot firmware kicked off etc).
606 */
607 printk(KERN_DEBUG "call xhci_mtk_setup\n");
608 if (hcd->driver->reset && (retval = hcd->driver->reset(hcd)) < 0) {
609 dev_err(hcd->self.controller, "can't setup\n");
610 goto err_hcd_driver_setup;
611 }
612 #if 0
613 /* NOTE: root hub and controller capabilities may not be the same */
614 if (device_can_wakeup(hcd->self.controller)
615 && device_can_wakeup(&hcd->self.root_hub->dev))
616 dev_dbg(hcd->self.controller, "supports USB remote wakeup\n");
617 #endif
618 /* enable irqs just before we start the controller */
619 if (hcd->driver->irq) {
620
621 /* IRQF_DISABLED doesn't work as advertised when used together
622 * with IRQF_SHARED. As usb_hcd_irq() will always disable
623 * interrupts we can remove it here.
624 */
625 if (irqflags & IRQF_SHARED)
626 irqflags &= ~IRQF_DISABLED;
627
628 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
629 hcd->driver->description, hcd->self.busnum);
630 if ((retval = request_irq(irqnum, &usb_hcd_irq, irqflags,
631 hcd->irq_descr, hcd)) != 0) {
632 dev_err(hcd->self.controller,
633 "request interrupt %d failed\n", irqnum);
634 goto err_request_irq;
635 }
636 hcd->irq = irqnum;
637 dev_info(hcd->self.controller, "irq %d, %s 0x%08llx\n", irqnum,
638 (hcd->driver->flags & HCD_MEMORY) ?
639 "io mem" : "io base",
640 (unsigned long long)hcd->rsrc_start);
641 } else {
642 hcd->irq = -1;
643 if (hcd->rsrc_start)
644 dev_info(hcd->self.controller, "%s 0x%08llx\n",
645 (hcd->driver->flags & HCD_MEMORY) ?
646 "io mem" : "io base",
647 (unsigned long long)hcd->rsrc_start);
648 }
649
650 if ((retval = hcd->driver->start(hcd)) < 0) {
651 dev_err(hcd->self.controller, "startup error %d\n", retval);
652 goto err_hcd_driver_start;
653 }
654 #if 0
655 /* starting here, usbcore will pay attention to this root hub */
656 rhdev->bus_mA = min(500u, hcd->power_budget);
657 if ((retval = register_root_hub(hcd)) != 0)
658 goto err_register_root_hub;
659
660 retval = sysfs_create_group(&rhdev->dev.kobj, &usb_bus_attr_group);
661 if (retval < 0) {
662 printk(KERN_ERR "Cannot register USB bus sysfs attributes: %d\n",
663 retval);
664 goto error_create_attr_group;
665 }
666 if (hcd->uses_new_polling && hcd->poll_rh)
667 usb_hcd_poll_rh_status(hcd);
668 #endif
669 return retval;
670
671 error_create_attr_group:
672 mutex_lock(&usb_bus_list_lock);
673 usb_disconnect(&hcd->self.root_hub);
674 mutex_unlock(&usb_bus_list_lock);
675 err_register_root_hub:
676 hcd->driver->stop(hcd);
677 err_hcd_driver_start:
678 if (hcd->irq >= 0)
679 free_irq(irqnum, hcd);
680 err_request_irq:
681 err_hcd_driver_setup:
682 hcd->self.root_hub = NULL;
683 usb_put_dev(rhdev);
684 #if 1
685 err_allocate_root_hub:
686 hcd->driver->stop(hcd);
687 #endif
688 err_register_bus:
689 hcd_buffer_destroy(hcd);
690 return retval;
691 }
692
693
694 /**
695 * usb_remove_hcd - shutdown processing for generic HCDs
696 * @hcd: the usb_hcd structure to remove
697 * Context: !in_interrupt()
698 *
699 * Disconnects the root hub, then reverses the effects of usb_add_hcd(),
700 * invoking the HCD's stop() method.
701 */
702 void mtk_usb_remove_hcd(struct usb_hcd *hcd)
703 {
704 dev_info(hcd->self.controller, "remove, state %x\n", hcd->state);
705
706 if (HC_IS_RUNNING (hcd->state))
707 hcd->state = HC_STATE_QUIESCING;
708
709 dev_dbg(hcd->self.controller, "roothub graceful disconnect\n");
710 #if 0
711 spin_lock_irq (&hcd_root_hub_lock);
712 hcd->rh_registered = 0;
713 spin_unlock_irq (&hcd_root_hub_lock);
714 #endif
715 #if 0 //#ifdef CONFIG_USB_SUSPEND
716 cancel_work_sync(&hcd->wakeup_work);
717 #endif
718 #if 0
719 sysfs_remove_group(&hcd->self.root_hub->dev.kobj, &usb_bus_attr_group);
720 mutex_lock(&usb_bus_list_lock);
721 usb_disconnect(&hcd->self.root_hub);
722 mutex_unlock(&usb_bus_list_lock);
723 #endif
724 hcd->driver->stop(hcd);
725 hcd->state = HC_STATE_HALT;
726 #if 0
727 hcd->poll_rh = 0;
728 del_timer_sync(&hcd->rh_timer);
729 #endif
730 if (hcd->irq >= 0)
731 free_irq(hcd->irq, hcd);
732 #if 0
733 usb_deregister_bus(&hcd->self);
734 #endif
735 hcd_buffer_destroy(hcd);
736 }
737 /**
738 * usb_bus_init - shared initialization code
739 * @bus: the bus structure being initialized
740 *
741 * This code is used to initialize a usb_bus structure, memory for which is
742 * separately managed.
743 */
744 static void mtk_usb_bus_init (struct usb_bus *bus)
745 {
746 memset (&bus->devmap, 0, sizeof(struct usb_devmap));
747
748 bus->devnum_next = 1;
749
750 bus->root_hub = NULL;
751 bus->busnum = -1;
752 bus->bandwidth_allocated = 0;
753 bus->bandwidth_int_reqs = 0;
754 bus->bandwidth_isoc_reqs = 0;
755
756 INIT_LIST_HEAD (&bus->bus_list);
757 }
758
759 /*-------------------------------------------------------------------------*/
760
761 /**
762 * usb_create_hcd - create and initialize an HCD structure
763 * @driver: HC driver that will use this hcd
764 * @dev: device for this HC, stored in hcd->self.controller
765 * @bus_name: value to store in hcd->self.bus_name
766 * Context: !in_interrupt()
767 *
768 * Allocate a struct usb_hcd, with extra space at the end for the
769 * HC driver's private data. Initialize the generic members of the
770 * hcd structure.
771 *
772 * If memory is unavailable, returns NULL.
773 */
774 struct usb_hcd *mtk_usb_create_hcd (const struct hc_driver *driver,
775 struct device *dev, const char *bus_name)
776 {
777 struct usb_hcd *hcd;
778
779 hcd = kzalloc(sizeof(*hcd) + driver->hcd_priv_size, GFP_KERNEL);
780 if (!hcd) {
781 dev_dbg (dev, "hcd alloc failed\n");
782 return NULL;
783 }
784
785 hcd->bandwidth_mutex = kmalloc(sizeof(*hcd->bandwidth_mutex), GFP_KERNEL);
786 if (!hcd->bandwidth_mutex) {
787 kfree(hcd);
788 dev_dbg(dev, "hcd bandwidth mutex alloc failed\n");
789 return NULL;
790 }
791 mutex_init(hcd->bandwidth_mutex);
792
793 printk(KERN_ERR "====%s(%d)==== hcd->pool[0] = 0x%p!\n", __func__, __LINE__, hcd->pool[0]);
794 dev_set_drvdata(dev, hcd);
795 kref_init(&hcd->kref);
796
797 printk(KERN_ERR "====%s(%d)==== hcd->pool[0] = 0x%p!\n", __func__, __LINE__, hcd->pool[0]);
798
799 mtk_usb_bus_init(&hcd->self);
800 hcd->self.controller = dev;
801 hcd->self.bus_name = bus_name;
802 hcd->self.uses_dma = (dev->dma_mask != NULL);
803
804 printk(KERN_ERR "====%s(%d)==== hcd->pool[0] = 0x%p!\n", __func__, __LINE__, hcd->pool[0]);
805
806 // init_timer(&hcd->rh_timer);
807 // hcd->rh_timer.function = rh_timer_func;
808 // hcd->rh_timer.data = (unsigned long) hcd;
809 #if 0 //#ifdef CONFIG_USB_SUSPEND
810 INIT_WORK(&hcd->wakeup_work, hcd_resume_work);
811 #endif
812 printk(KERN_ERR "====%s(%d)==== hcd->pool[0] = 0x%p!\n", __func__, __LINE__, hcd->pool[0]);
813 hcd->driver = driver;
814 hcd->product_desc = (driver->product_desc) ? driver->product_desc :
815 "USB Host Controller";
816 return hcd;
817 }
818
819
820 /* Find the flag for this endpoint (for use in the control context). Use the
821 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
822 * bit 1, etc.
823 */
824 unsigned int mtktest_xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
825 {
826 return 1 << (mtktest_xhci_get_endpoint_index(desc) + 1);
827 }
828