linux/drivers/staging/ozwpan/ozhcd.c
<<
>>
Prefs
   1/* -----------------------------------------------------------------------------
   2 * Copyright (c) 2011 Ozmo Inc
   3 * Released under the GNU General Public License Version 2 (GPLv2).
   4 *
   5 * This file provides the implementation of a USB host controller device that
   6 * does not have any associated hardware. Instead the virtual device is
   7 * connected to the WiFi network and emulates the operation of a USB hcd by
   8 * receiving and sending network frames.
   9 * Note:
  10 * We take great pains to reduce the amount of code where interrupts need to be
  11 * disabled and in this respect we are different from standard HCD's. In
  12 * particular we don't want in_irq() code bleeding over to the protocol side of
  13 * the driver.
  14 * The troublesome functions are the urb enqueue and dequeue functions both of
  15 * which can be called in_irq(). So for these functions we put the urbs into a
  16 * queue and request a tasklet to process them. This means that a spinlock with
  17 * interrupts disabled must be held for insertion and removal but most code is
  18 * is in tasklet or soft irq context. The lock that protects this list is called
  19 * the tasklet lock and serves the purpose of the 'HCD lock' which must be held
  20 * when calling the following functions.
  21 *   usb_hcd_link_urb_to_ep()
  22 *   usb_hcd_unlink_urb_from_ep()
  23 *   usb_hcd_flush_endpoint()
  24 *   usb_hcd_check_unlink_urb()
  25 * -----------------------------------------------------------------------------
  26 */
  27#include <linux/platform_device.h>
  28#include <linux/usb.h>
  29#include <linux/jiffies.h>
  30#include <linux/slab.h>
  31#include <linux/export.h>
  32#include "linux/usb/hcd.h"
  33#include <asm/unaligned.h>
  34#include "ozconfig.h"
  35#include "ozusbif.h"
  36#include "oztrace.h"
  37#include "ozurbparanoia.h"
  38#include "ozevent.h"
  39#include "ozhcd.h"
  40/*------------------------------------------------------------------------------
  41 * Number of units of buffering to capture for an isochronous IN endpoint before
  42 * allowing data to be indicated up.
  43 */
  44#define OZ_IN_BUFFERING_UNITS   50
  45/* Name of our platform device.
  46 */
  47#define OZ_PLAT_DEV_NAME        "ozwpan"
  48/* Maximum number of free urb links that can be kept in the pool.
  49 */
  50#define OZ_MAX_LINK_POOL_SIZE   16
  51/* Get endpoint object from the containing link.
  52 */
  53#define ep_from_link(__e) container_of((__e), struct oz_endpoint, link)
  54/*------------------------------------------------------------------------------
  55 * Used to link urbs together and also store some status information for each
  56 * urb.
  57 * A cache of these are kept in a pool to reduce number of calls to kmalloc.
  58 */
  59struct oz_urb_link {
  60        struct list_head link;
  61        struct urb *urb;
  62        struct oz_port *port;
  63        u8 req_id;
  64        u8 ep_num;
  65        unsigned long submit_jiffies;
  66};
  67
  68/* Holds state information about a USB endpoint.
  69 */
  70struct oz_endpoint {
  71        struct list_head urb_list;      /* List of oz_urb_link items. */
  72        struct list_head link;          /* For isoc ep, links in to isoc
  73                                           lists of oz_port. */
  74        unsigned long last_jiffies;
  75        int credit;
  76        int credit_ceiling;
  77        u8 ep_num;
  78        u8 attrib;
  79        u8 *buffer;
  80        int buffer_size;
  81        int in_ix;
  82        int out_ix;
  83        int buffered_units;
  84        unsigned flags;
  85        int start_frame;
  86};
  87/* Bits in the flags field. */
  88#define OZ_F_EP_BUFFERING       0x1
  89#define OZ_F_EP_HAVE_STREAM     0x2
  90
  91/* Holds state information about a USB interface.
  92 */
  93struct oz_interface {
  94        unsigned ep_mask;
  95        u8 alt;
  96};
  97
  98/* Holds state information about an hcd port.
  99 */
 100#define OZ_NB_ENDPOINTS 16
 101struct oz_port {
 102        unsigned flags;
 103        unsigned status;
 104        void *hpd;
 105        struct oz_hcd *ozhcd;
 106        spinlock_t port_lock;
 107        u8 bus_addr;
 108        u8 next_req_id;
 109        u8 config_num;
 110        int num_iface;
 111        struct oz_interface *iface;
 112        struct oz_endpoint *out_ep[OZ_NB_ENDPOINTS];
 113        struct oz_endpoint *in_ep[OZ_NB_ENDPOINTS];
 114        struct list_head isoc_out_ep;
 115        struct list_head isoc_in_ep;
 116};
 117#define OZ_PORT_F_PRESENT       0x1
 118#define OZ_PORT_F_CHANGED       0x2
 119#define OZ_PORT_F_DYING         0x4
 120
 121/* Data structure in the private context area of struct usb_hcd.
 122 */
 123#define OZ_NB_PORTS     8
 124struct oz_hcd {
 125        spinlock_t hcd_lock;
 126        struct list_head urb_pending_list;
 127        struct list_head urb_cancel_list;
 128        struct list_head orphanage;
 129        int conn_port; /* Port that is currently connecting, -1 if none.*/
 130        struct oz_port ports[OZ_NB_PORTS];
 131        uint flags;
 132        struct usb_hcd *hcd;
 133};
 134/* Bits in flags field.
 135 */
 136#define OZ_HDC_F_SUSPENDED      0x1
 137
 138/*------------------------------------------------------------------------------
 139 * Static function prototypes.
 140 */
 141static int oz_hcd_start(struct usb_hcd *hcd);
 142static void oz_hcd_stop(struct usb_hcd *hcd);
 143static void oz_hcd_shutdown(struct usb_hcd *hcd);
 144static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
 145                                gfp_t mem_flags);
 146static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
 147static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
 148                                struct usb_host_endpoint *ep);
 149static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
 150                                struct usb_host_endpoint *ep);
 151static int oz_hcd_get_frame_number(struct usb_hcd *hcd);
 152static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf);
 153static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
 154                                u16 windex, char *buf, u16 wlength);
 155static int oz_hcd_bus_suspend(struct usb_hcd *hcd);
 156static int oz_hcd_bus_resume(struct usb_hcd *hcd);
 157static int oz_plat_probe(struct platform_device *dev);
 158static int oz_plat_remove(struct platform_device *dev);
 159static void oz_plat_shutdown(struct platform_device *dev);
 160static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg);
 161static int oz_plat_resume(struct platform_device *dev);
 162static void oz_urb_process_tasklet(unsigned long unused);
 163static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
 164                struct oz_port *port, struct usb_host_config *config,
 165                gfp_t mem_flags);
 166static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
 167                                struct oz_port *port);
 168static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
 169                        struct oz_port *port,
 170                        struct usb_host_interface *intf, gfp_t mem_flags);
 171static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
 172                        struct oz_port *port, int if_ix);
 173static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
 174                gfp_t mem_flags);
 175static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
 176                struct urb *urb);
 177static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status);
 178/*------------------------------------------------------------------------------
 179 * Static external variables.
 180 */
 181static struct platform_device *g_plat_dev;
 182static struct oz_hcd *g_ozhcd;
 183static DEFINE_SPINLOCK(g_hcdlock);      /* Guards g_ozhcd. */
 184static const char g_hcd_name[] = "Ozmo WPAN";
 185static struct list_head *g_link_pool;
 186static int g_link_pool_size;
 187static DEFINE_SPINLOCK(g_link_lock);
 188static DEFINE_SPINLOCK(g_tasklet_lock);
 189static struct tasklet_struct g_urb_process_tasklet;
 190static struct tasklet_struct g_urb_cancel_tasklet;
 191static atomic_t g_pending_urbs = ATOMIC_INIT(0);
 192static const struct hc_driver g_oz_hc_drv = {
 193        .description =          g_hcd_name,
 194        .product_desc =         "Ozmo Devices WPAN",
 195        .hcd_priv_size =        sizeof(struct oz_hcd),
 196        .flags =                HCD_USB11,
 197        .start =                oz_hcd_start,
 198        .stop =                 oz_hcd_stop,
 199        .shutdown =             oz_hcd_shutdown,
 200        .urb_enqueue =          oz_hcd_urb_enqueue,
 201        .urb_dequeue =          oz_hcd_urb_dequeue,
 202        .endpoint_disable =     oz_hcd_endpoint_disable,
 203        .endpoint_reset =       oz_hcd_endpoint_reset,
 204        .get_frame_number =     oz_hcd_get_frame_number,
 205        .hub_status_data =      oz_hcd_hub_status_data,
 206        .hub_control =          oz_hcd_hub_control,
 207        .bus_suspend =          oz_hcd_bus_suspend,
 208        .bus_resume =           oz_hcd_bus_resume,
 209};
 210
 211static struct platform_driver g_oz_plat_drv = {
 212        .probe = oz_plat_probe,
 213        .remove = oz_plat_remove,
 214        .shutdown = oz_plat_shutdown,
 215        .suspend = oz_plat_suspend,
 216        .resume = oz_plat_resume,
 217        .driver = {
 218                .name = OZ_PLAT_DEV_NAME,
 219                .owner = THIS_MODULE,
 220        },
 221};
 222/*------------------------------------------------------------------------------
 223 * Gets our private context area (which is of type struct oz_hcd) from the
 224 * usb_hcd structure.
 225 * Context: any
 226 */
 227static inline struct oz_hcd *oz_hcd_private(struct usb_hcd *hcd)
 228{
 229        return (struct oz_hcd *)hcd->hcd_priv;
 230}
 231/*------------------------------------------------------------------------------
 232 * Searches list of ports to find the index of the one with a specified  USB
 233 * bus address. If none of the ports has the bus address then the connection
 234 * port is returned, if there is one or -1 otherwise.
 235 * Context: any
 236 */
 237static int oz_get_port_from_addr(struct oz_hcd *ozhcd, u8 bus_addr)
 238{
 239        int i;
 240        for (i = 0; i < OZ_NB_PORTS; i++) {
 241                if (ozhcd->ports[i].bus_addr == bus_addr)
 242                        return i;
 243        }
 244        return ozhcd->conn_port;
 245}
 246/*------------------------------------------------------------------------------
 247 * Allocates an urb link, first trying the pool but going to heap if empty.
 248 * Context: any
 249 */
 250static struct oz_urb_link *oz_alloc_urb_link(void)
 251{
 252        struct oz_urb_link *urbl = NULL;
 253        unsigned long irq_state;
 254        spin_lock_irqsave(&g_link_lock, irq_state);
 255        if (g_link_pool) {
 256                urbl = container_of(g_link_pool, struct oz_urb_link, link);
 257                g_link_pool = urbl->link.next;
 258                --g_link_pool_size;
 259        }
 260        spin_unlock_irqrestore(&g_link_lock, irq_state);
 261        if (urbl == NULL)
 262                urbl = kmalloc(sizeof(struct oz_urb_link), GFP_ATOMIC);
 263        return urbl;
 264}
 265/*------------------------------------------------------------------------------
 266 * Frees an urb link by putting it in the pool if there is enough space or
 267 * deallocating it to heap otherwise.
 268 * Context: any
 269 */
 270static void oz_free_urb_link(struct oz_urb_link *urbl)
 271{
 272        if (urbl) {
 273                unsigned long irq_state;
 274                spin_lock_irqsave(&g_link_lock, irq_state);
 275                if (g_link_pool_size < OZ_MAX_LINK_POOL_SIZE) {
 276                        urbl->link.next = g_link_pool;
 277                        g_link_pool = &urbl->link;
 278                        urbl = NULL;
 279                        g_link_pool_size++;
 280                }
 281                spin_unlock_irqrestore(&g_link_lock, irq_state);
 282                kfree(urbl);
 283        }
 284}
 285/*------------------------------------------------------------------------------
 286 * Deallocates all the urb links in the pool.
 287 * Context: unknown
 288 */
 289static void oz_empty_link_pool(void)
 290{
 291        struct list_head *e;
 292        unsigned long irq_state;
 293        spin_lock_irqsave(&g_link_lock, irq_state);
 294        e = g_link_pool;
 295        g_link_pool = NULL;
 296        g_link_pool_size = 0;
 297        spin_unlock_irqrestore(&g_link_lock, irq_state);
 298        while (e) {
 299                struct oz_urb_link *urbl =
 300                        container_of(e, struct oz_urb_link, link);
 301                e = e->next;
 302                kfree(urbl);
 303        }
 304}
 305/*------------------------------------------------------------------------------
 306 * Allocates endpoint structure and optionally a buffer. If a buffer is
 307 * allocated it immediately follows the endpoint structure.
 308 * Context: softirq
 309 */
 310static struct oz_endpoint *oz_ep_alloc(gfp_t mem_flags, int buffer_size)
 311{
 312        struct oz_endpoint *ep =
 313                kzalloc(sizeof(struct oz_endpoint)+buffer_size, mem_flags);
 314        if (ep) {
 315                INIT_LIST_HEAD(&ep->urb_list);
 316                INIT_LIST_HEAD(&ep->link);
 317                ep->credit = -1;
 318                if (buffer_size) {
 319                        ep->buffer_size = buffer_size;
 320                        ep->buffer = (u8 *)(ep+1);
 321                }
 322        }
 323        return ep;
 324}
 325/*------------------------------------------------------------------------------
 326 * Pre-condition: Must be called with g_tasklet_lock held and interrupts
 327 * disabled.
 328 * Context: softirq or process
 329 */
 330static struct oz_urb_link *oz_uncancel_urb(struct oz_hcd *ozhcd, struct urb *urb)
 331{
 332        struct oz_urb_link *urbl;
 333        struct list_head *e;
 334        list_for_each(e, &ozhcd->urb_cancel_list) {
 335                urbl = container_of(e, struct oz_urb_link, link);
 336                if (urb == urbl->urb) {
 337                        list_del_init(e);
 338                        return urbl;
 339                }
 340        }
 341        return NULL;
 342}
 343/*------------------------------------------------------------------------------
 344 * This is called when we have finished processing an urb. It unlinks it from
 345 * the ep and returns it to the core.
 346 * Context: softirq or process
 347 */
 348static void oz_complete_urb(struct usb_hcd *hcd, struct urb *urb,
 349                int status, unsigned long submit_jiffies)
 350{
 351        struct oz_hcd *ozhcd = oz_hcd_private(hcd);
 352        unsigned long irq_state;
 353        struct oz_urb_link *cancel_urbl = NULL;
 354        spin_lock_irqsave(&g_tasklet_lock, irq_state);
 355        usb_hcd_unlink_urb_from_ep(hcd, urb);
 356        /* Clear hcpriv which will prevent it being put in the cancel list
 357         * in the event that an attempt is made to cancel it.
 358         */
 359        urb->hcpriv = NULL;
 360        /* Walk the cancel list in case the urb is already sitting there.
 361         * Since we process the cancel list in a tasklet rather than in
 362         * the dequeue function this could happen.
 363         */
 364        cancel_urbl = oz_uncancel_urb(ozhcd, urb);
 365        /* Note: we release lock but do not enable local irqs.
 366         * It appears that usb_hcd_giveback_urb() expects irqs to be disabled,
 367         * or at least other host controllers disable interrupts at this point
 368         * so we do the same. We must, however, release the lock otherwise a
 369         * deadlock will occur if an urb is submitted to our driver in the urb
 370         * completion function. Because we disable interrupts it is possible
 371         * that the urb_enqueue function can be called with them disabled.
 372         */
 373        spin_unlock(&g_tasklet_lock);
 374        if (oz_forget_urb(urb)) {
 375                oz_trace("OZWPAN: ERROR Unknown URB %p\n", urb);
 376        } else {
 377                static unsigned long last_time;
 378                atomic_dec(&g_pending_urbs);
 379                oz_trace2(OZ_TRACE_URB,
 380                        "%lu: giveback_urb(%p,%x) %lu %lu pending:%d\n",
 381                        jiffies, urb, status, jiffies-submit_jiffies,
 382                        jiffies-last_time, atomic_read(&g_pending_urbs));
 383                last_time = jiffies;
 384                oz_event_log(OZ_EVT_URB_DONE, 0, 0, urb, status);
 385                usb_hcd_giveback_urb(hcd, urb, status);
 386        }
 387        spin_lock(&g_tasklet_lock);
 388        spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
 389        if (cancel_urbl)
 390                oz_free_urb_link(cancel_urbl);
 391}
 392/*------------------------------------------------------------------------------
 393 * Deallocates an endpoint including deallocating any associated stream and
 394 * returning any queued urbs to the core.
 395 * Context: softirq
 396 */
 397static void oz_ep_free(struct oz_port *port, struct oz_endpoint *ep)
 398{
 399        oz_trace("oz_ep_free()\n");
 400        if (port) {
 401                struct list_head list;
 402                struct oz_hcd *ozhcd = port->ozhcd;
 403                INIT_LIST_HEAD(&list);
 404                if (ep->flags & OZ_F_EP_HAVE_STREAM)
 405                        oz_usb_stream_delete(port->hpd, ep->ep_num);
 406                /* Transfer URBs to the orphanage while we hold the lock. */
 407                spin_lock_bh(&ozhcd->hcd_lock);
 408                /* Note: this works even if ep->urb_list is empty.*/
 409                list_replace_init(&ep->urb_list, &list);
 410                /* Put the URBs in the orphanage. */
 411                list_splice_tail(&list, &ozhcd->orphanage);
 412                spin_unlock_bh(&ozhcd->hcd_lock);
 413        }
 414        oz_trace("Freeing endpoint memory\n");
 415        kfree(ep);
 416}
 417/*------------------------------------------------------------------------------
 418 * Context: softirq
 419 */
 420static void oz_complete_buffered_urb(struct oz_port *port,
 421                        struct oz_endpoint *ep,
 422                        struct urb *urb)
 423{
 424        u8 data_len, available_space, copy_len;
 425
 426        memcpy(&data_len, &ep->buffer[ep->out_ix], sizeof(u8));
 427        if (data_len <= urb->transfer_buffer_length)
 428                available_space = data_len;
 429        else
 430                available_space = urb->transfer_buffer_length;
 431
 432        if (++ep->out_ix == ep->buffer_size)
 433                ep->out_ix = 0;
 434        copy_len = ep->buffer_size - ep->out_ix;
 435        if (copy_len >= available_space)
 436                copy_len = available_space;
 437        memcpy(urb->transfer_buffer, &ep->buffer[ep->out_ix], copy_len);
 438
 439        if (copy_len < available_space) {
 440                memcpy((urb->transfer_buffer + copy_len), ep->buffer,
 441                                                (available_space - copy_len));
 442                ep->out_ix = available_space - copy_len;
 443        } else {
 444                ep->out_ix += copy_len;
 445        }
 446        urb->actual_length = available_space;
 447        if (ep->out_ix == ep->buffer_size)
 448                ep->out_ix = 0;
 449
 450        ep->buffered_units--;
 451        oz_trace("Trying to give back buffered frame of size=%d\n",
 452                                                available_space);
 453        oz_complete_urb(port->ozhcd->hcd, urb, 0, 0);
 454}
 455
 456/*------------------------------------------------------------------------------
 457 * Context: softirq
 458 */
 459static int oz_enqueue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
 460                        struct urb *urb, u8 req_id)
 461{
 462        struct oz_urb_link *urbl;
 463        struct oz_endpoint *ep;
 464        int err = 0;
 465        if (ep_addr >= OZ_NB_ENDPOINTS) {
 466                oz_trace("Invalid endpoint number in oz_enqueue_ep_urb().\n");
 467                return -EINVAL;
 468        }
 469        urbl = oz_alloc_urb_link();
 470        if (!urbl)
 471                return -ENOMEM;
 472        urbl->submit_jiffies = jiffies;
 473        urbl->urb = urb;
 474        urbl->req_id = req_id;
 475        urbl->ep_num = ep_addr;
 476        /* Hold lock while we insert the URB into the list within the
 477         * endpoint structure.
 478         */
 479        spin_lock_bh(&port->ozhcd->hcd_lock);
 480        /* If the urb has been unlinked while out of any list then
 481         * complete it now.
 482         */
 483        if (urb->unlinked) {
 484                spin_unlock_bh(&port->ozhcd->hcd_lock);
 485                oz_trace("urb %p unlinked so complete immediately\n", urb);
 486                oz_complete_urb(port->ozhcd->hcd, urb, 0, 0);
 487                oz_free_urb_link(urbl);
 488                return 0;
 489        }
 490        if (in_dir)
 491                ep = port->in_ep[ep_addr];
 492        else
 493                ep = port->out_ep[ep_addr];
 494
 495        /*For interrupt endpoint check for buffered data
 496        * & complete urb
 497        */
 498        if (((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
 499                                                 && ep->buffered_units > 0) {
 500                oz_free_urb_link(urbl);
 501                spin_unlock_bh(&port->ozhcd->hcd_lock);
 502                oz_complete_buffered_urb(port, ep, urb);
 503                return 0;
 504        }
 505
 506        if (ep && port->hpd) {
 507                list_add_tail(&urbl->link, &ep->urb_list);
 508                if (!in_dir && ep_addr && (ep->credit < 0)) {
 509                        ep->last_jiffies = jiffies;
 510                        ep->credit = 0;
 511                        oz_event_log(OZ_EVT_EP_CREDIT, ep->ep_num,
 512                                        0, NULL, ep->credit);
 513                }
 514        } else {
 515                err = -EPIPE;
 516        }
 517        spin_unlock_bh(&port->ozhcd->hcd_lock);
 518        if (err)
 519                oz_free_urb_link(urbl);
 520        return err;
 521}
 522/*------------------------------------------------------------------------------
 523 * Removes an urb from the queue in the endpoint.
 524 * Returns 0 if it is found and -EIDRM otherwise.
 525 * Context: softirq
 526 */
 527static int oz_dequeue_ep_urb(struct oz_port *port, u8 ep_addr, int in_dir,
 528                        struct urb *urb)
 529{
 530        struct oz_urb_link *urbl = NULL;
 531        struct oz_endpoint *ep;
 532        spin_lock_bh(&port->ozhcd->hcd_lock);
 533        if (in_dir)
 534                ep = port->in_ep[ep_addr];
 535        else
 536                ep = port->out_ep[ep_addr];
 537        if (ep) {
 538                struct list_head *e;
 539                list_for_each(e, &ep->urb_list) {
 540                        urbl = container_of(e, struct oz_urb_link, link);
 541                        if (urbl->urb == urb) {
 542                                list_del_init(e);
 543                                break;
 544                        }
 545                        urbl = NULL;
 546                }
 547        }
 548        spin_unlock_bh(&port->ozhcd->hcd_lock);
 549        if (urbl)
 550                oz_free_urb_link(urbl);
 551        return urbl ? 0 : -EIDRM;
 552}
 553/*------------------------------------------------------------------------------
 554 * Finds an urb given its request id.
 555 * Context: softirq
 556 */
 557static struct urb *oz_find_urb_by_id(struct oz_port *port, int ep_ix,
 558                u8 req_id)
 559{
 560        struct oz_hcd *ozhcd = port->ozhcd;
 561        struct urb *urb = NULL;
 562        struct oz_urb_link *urbl = NULL;
 563        struct oz_endpoint *ep;
 564
 565        spin_lock_bh(&ozhcd->hcd_lock);
 566        ep = port->out_ep[ep_ix];
 567        if (ep) {
 568                struct list_head *e;
 569                list_for_each(e, &ep->urb_list) {
 570                        urbl = container_of(e, struct oz_urb_link, link);
 571                        if (urbl->req_id == req_id) {
 572                                urb = urbl->urb;
 573                                list_del_init(e);
 574                                break;
 575                        }
 576                }
 577        }
 578        spin_unlock_bh(&ozhcd->hcd_lock);
 579        /* If urb is non-zero then we we must have an urb link to delete.
 580         */
 581        if (urb)
 582                oz_free_urb_link(urbl);
 583        return urb;
 584}
 585/*------------------------------------------------------------------------------
 586 * Pre-condition: Port lock must be held.
 587 * Context: softirq
 588 */
 589static void oz_acquire_port(struct oz_port *port, void *hpd)
 590{
 591        INIT_LIST_HEAD(&port->isoc_out_ep);
 592        INIT_LIST_HEAD(&port->isoc_in_ep);
 593        port->flags |= OZ_PORT_F_PRESENT | OZ_PORT_F_CHANGED;
 594        port->status |= USB_PORT_STAT_CONNECTION |
 595                        (USB_PORT_STAT_C_CONNECTION << 16);
 596        oz_usb_get(hpd);
 597        port->hpd = hpd;
 598}
 599/*------------------------------------------------------------------------------
 600 * Context: softirq
 601 */
 602static struct oz_hcd *oz_hcd_claim(void)
 603{
 604        struct oz_hcd *ozhcd;
 605        spin_lock_bh(&g_hcdlock);
 606        ozhcd = g_ozhcd;
 607        if (ozhcd)
 608                usb_get_hcd(ozhcd->hcd);
 609        spin_unlock_bh(&g_hcdlock);
 610        return ozhcd;
 611}
 612/*------------------------------------------------------------------------------
 613 * Context: softirq
 614 */
 615static inline void oz_hcd_put(struct oz_hcd *ozhcd)
 616{
 617        if (ozhcd)
 618                usb_put_hcd(ozhcd->hcd);
 619}
 620/*------------------------------------------------------------------------------
 621 * This is called by the protocol handler to notify that a PD has arrived.
 622 * We allocate a port to associate with the PD and create a structure for
 623 * endpoint 0. This port is made the connection port.
 624 * In the event that one of the other port is already a connection port then
 625 * we fail.
 626 * TODO We should be able to do better than fail and should be able remember
 627 * that this port needs configuring and make it the connection port once the
 628 * current connection port has been assigned an address. Collisions here are
 629 * probably very rare indeed.
 630 * Context: softirq
 631 */
 632void *oz_hcd_pd_arrived(void *hpd)
 633{
 634        int i;
 635        void *hport = NULL;
 636        struct oz_hcd *ozhcd = NULL;
 637        struct oz_endpoint *ep;
 638        oz_trace("oz_hcd_pd_arrived()\n");
 639        ozhcd = oz_hcd_claim();
 640        if (ozhcd == NULL)
 641                return NULL;
 642        /* Allocate an endpoint object in advance (before holding hcd lock) to
 643         * use for out endpoint 0.
 644         */
 645        ep = oz_ep_alloc(GFP_ATOMIC, 0);
 646        spin_lock_bh(&ozhcd->hcd_lock);
 647        if (ozhcd->conn_port >= 0) {
 648                spin_unlock_bh(&ozhcd->hcd_lock);
 649                oz_trace("conn_port >= 0\n");
 650                goto out;
 651        }
 652        for (i = 0; i < OZ_NB_PORTS; i++) {
 653                struct oz_port *port = &ozhcd->ports[i];
 654                spin_lock(&port->port_lock);
 655                if ((port->flags & OZ_PORT_F_PRESENT) == 0) {
 656                        oz_acquire_port(port, hpd);
 657                        spin_unlock(&port->port_lock);
 658                        break;
 659                }
 660                spin_unlock(&port->port_lock);
 661        }
 662        if (i < OZ_NB_PORTS) {
 663                oz_trace("Setting conn_port = %d\n", i);
 664                ozhcd->conn_port = i;
 665                /* Attach out endpoint 0.
 666                 */
 667                ozhcd->ports[i].out_ep[0] = ep;
 668                ep = NULL;
 669                hport = &ozhcd->ports[i];
 670                spin_unlock_bh(&ozhcd->hcd_lock);
 671                if (ozhcd->flags & OZ_HDC_F_SUSPENDED) {
 672                        oz_trace("Resuming root hub\n");
 673                        usb_hcd_resume_root_hub(ozhcd->hcd);
 674                }
 675                usb_hcd_poll_rh_status(ozhcd->hcd);
 676        } else {
 677                spin_unlock_bh(&ozhcd->hcd_lock);
 678        }
 679out:
 680        if (ep) /* ep is non-null if not used. */
 681                oz_ep_free(NULL, ep);
 682        oz_hcd_put(ozhcd);
 683        return hport;
 684}
 685/*------------------------------------------------------------------------------
 686 * This is called by the protocol handler to notify that the PD has gone away.
 687 * We need to deallocate all resources and then request that the root hub is
 688 * polled. We release the reference we hold on the PD.
 689 * Context: softirq
 690 */
 691void oz_hcd_pd_departed(void *hport)
 692{
 693        struct oz_port *port = (struct oz_port *)hport;
 694        struct oz_hcd *ozhcd;
 695        void *hpd;
 696        struct oz_endpoint *ep = NULL;
 697
 698        oz_trace("oz_hcd_pd_departed()\n");
 699        if (port == NULL) {
 700                oz_trace("oz_hcd_pd_departed() port = 0\n");
 701                return;
 702        }
 703        ozhcd = port->ozhcd;
 704        if (ozhcd == NULL)
 705                return;
 706        /* Check if this is the connection port - if so clear it.
 707         */
 708        spin_lock_bh(&ozhcd->hcd_lock);
 709        if ((ozhcd->conn_port >= 0) &&
 710                (port == &ozhcd->ports[ozhcd->conn_port])) {
 711                oz_trace("Clearing conn_port\n");
 712                ozhcd->conn_port = -1;
 713        }
 714        spin_lock(&port->port_lock);
 715        port->flags |= OZ_PORT_F_DYING;
 716        spin_unlock(&port->port_lock);
 717        spin_unlock_bh(&ozhcd->hcd_lock);
 718
 719        oz_clean_endpoints_for_config(ozhcd->hcd, port);
 720        spin_lock_bh(&port->port_lock);
 721        hpd = port->hpd;
 722        port->hpd = NULL;
 723        port->bus_addr = 0xff;
 724        port->flags &= ~(OZ_PORT_F_PRESENT | OZ_PORT_F_DYING);
 725        port->flags |= OZ_PORT_F_CHANGED;
 726        port->status &= ~USB_PORT_STAT_CONNECTION;
 727        port->status |= (USB_PORT_STAT_C_CONNECTION << 16);
 728        /* If there is an endpont 0 then clear the pointer while we hold
 729         * the spinlock be we deallocate it after releasing the lock.
 730         */
 731        if (port->out_ep[0]) {
 732                ep = port->out_ep[0];
 733                port->out_ep[0] = NULL;
 734        }
 735        spin_unlock_bh(&port->port_lock);
 736        if (ep)
 737                oz_ep_free(port, ep);
 738        usb_hcd_poll_rh_status(ozhcd->hcd);
 739        oz_usb_put(hpd);
 740}
 741/*------------------------------------------------------------------------------
 742 * Context: softirq
 743 */
 744void oz_hcd_pd_reset(void *hpd, void *hport)
 745{
 746        /* Cleanup the current configuration and report reset to the core.
 747         */
 748        struct oz_port *port = (struct oz_port *)hport;
 749        struct oz_hcd *ozhcd = port->ozhcd;
 750        oz_trace("PD Reset\n");
 751        spin_lock_bh(&port->port_lock);
 752        port->flags |= OZ_PORT_F_CHANGED;
 753        port->status |= USB_PORT_STAT_RESET;
 754        port->status |= (USB_PORT_STAT_C_RESET << 16);
 755        spin_unlock_bh(&port->port_lock);
 756        oz_clean_endpoints_for_config(ozhcd->hcd, port);
 757        usb_hcd_poll_rh_status(ozhcd->hcd);
 758}
 759/*------------------------------------------------------------------------------
 760 * Context: softirq
 761 */
 762void oz_hcd_get_desc_cnf(void *hport, u8 req_id, int status, const u8 *desc,
 763                        int length, int offset, int total_size)
 764{
 765        struct oz_port *port = (struct oz_port *)hport;
 766        struct urb *urb;
 767        int err = 0;
 768
 769        oz_event_log(OZ_EVT_CTRL_CNF, 0, req_id, NULL, status);
 770        oz_trace("oz_hcd_get_desc_cnf length = %d offs = %d tot_size = %d\n",
 771                        length, offset, total_size);
 772        urb = oz_find_urb_by_id(port, 0, req_id);
 773        if (!urb)
 774                return;
 775        if (status == 0) {
 776                int copy_len;
 777                int required_size = urb->transfer_buffer_length;
 778                if (required_size > total_size)
 779                        required_size = total_size;
 780                copy_len = required_size-offset;
 781                if (length <= copy_len)
 782                        copy_len = length;
 783                memcpy(urb->transfer_buffer+offset, desc, copy_len);
 784                offset += copy_len;
 785                if (offset < required_size) {
 786                        struct usb_ctrlrequest *setup =
 787                                (struct usb_ctrlrequest *)urb->setup_packet;
 788                        unsigned wvalue = le16_to_cpu(setup->wValue);
 789                        if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
 790                                err = -ENOMEM;
 791                        else if (oz_usb_get_desc_req(port->hpd, req_id,
 792                                        setup->bRequestType, (u8)(wvalue>>8),
 793                                        (u8)wvalue, setup->wIndex, offset,
 794                                        required_size-offset)) {
 795                                oz_dequeue_ep_urb(port, 0, 0, urb);
 796                                err = -ENOMEM;
 797                        }
 798                        if (err == 0)
 799                                return;
 800                }
 801        }
 802        urb->actual_length = total_size;
 803        oz_complete_urb(port->ozhcd->hcd, urb, 0, 0);
 804}
 805/*------------------------------------------------------------------------------
 806 * Context: softirq
 807 */
 808#ifdef WANT_TRACE
 809static void oz_display_conf_type(u8 t)
 810{
 811        switch (t) {
 812        case USB_REQ_GET_STATUS:
 813                oz_trace("USB_REQ_GET_STATUS - cnf\n");
 814                break;
 815        case USB_REQ_CLEAR_FEATURE:
 816                oz_trace("USB_REQ_CLEAR_FEATURE - cnf\n");
 817                break;
 818        case USB_REQ_SET_FEATURE:
 819                oz_trace("USB_REQ_SET_FEATURE - cnf\n");
 820                break;
 821        case USB_REQ_SET_ADDRESS:
 822                oz_trace("USB_REQ_SET_ADDRESS - cnf\n");
 823                break;
 824        case USB_REQ_GET_DESCRIPTOR:
 825                oz_trace("USB_REQ_GET_DESCRIPTOR - cnf\n");
 826                break;
 827        case USB_REQ_SET_DESCRIPTOR:
 828                oz_trace("USB_REQ_SET_DESCRIPTOR - cnf\n");
 829                break;
 830        case USB_REQ_GET_CONFIGURATION:
 831                oz_trace("USB_REQ_GET_CONFIGURATION - cnf\n");
 832                break;
 833        case USB_REQ_SET_CONFIGURATION:
 834                oz_trace("USB_REQ_SET_CONFIGURATION - cnf\n");
 835                break;
 836        case USB_REQ_GET_INTERFACE:
 837                oz_trace("USB_REQ_GET_INTERFACE - cnf\n");
 838                break;
 839        case USB_REQ_SET_INTERFACE:
 840                oz_trace("USB_REQ_SET_INTERFACE - cnf\n");
 841                break;
 842        case USB_REQ_SYNCH_FRAME:
 843                oz_trace("USB_REQ_SYNCH_FRAME - cnf\n");
 844                break;
 845        }
 846}
 847#else
 848#define oz_display_conf_type(__x)
 849#endif /* WANT_TRACE */
 850/*------------------------------------------------------------------------------
 851 * Context: softirq
 852 */
 853static void oz_hcd_complete_set_config(struct oz_port *port, struct urb *urb,
 854                u8 rcode, u8 config_num)
 855{
 856        int rc = 0;
 857        struct usb_hcd *hcd = port->ozhcd->hcd;
 858        if (rcode == 0) {
 859                port->config_num = config_num;
 860                oz_clean_endpoints_for_config(hcd, port);
 861                if (oz_build_endpoints_for_config(hcd, port,
 862                        &urb->dev->config[port->config_num-1], GFP_ATOMIC)) {
 863                        rc = -ENOMEM;
 864                }
 865        } else {
 866                rc = -ENOMEM;
 867        }
 868        oz_complete_urb(hcd, urb, rc, 0);
 869}
 870/*------------------------------------------------------------------------------
 871 * Context: softirq
 872 */
 873static void oz_hcd_complete_set_interface(struct oz_port *port, struct urb *urb,
 874                u8 rcode, u8 if_num, u8 alt)
 875{
 876        struct usb_hcd *hcd = port->ozhcd->hcd;
 877        int rc = 0;
 878        if (rcode == 0) {
 879                struct usb_host_config *config;
 880                struct usb_host_interface *intf;
 881                oz_trace("Set interface %d alt %d\n", if_num, alt);
 882                oz_clean_endpoints_for_interface(hcd, port, if_num);
 883                config = &urb->dev->config[port->config_num-1];
 884                intf = &config->intf_cache[if_num]->altsetting[alt];
 885                if (oz_build_endpoints_for_interface(hcd, port, intf,
 886                        GFP_ATOMIC))
 887                        rc = -ENOMEM;
 888                else
 889                        port->iface[if_num].alt = alt;
 890        } else {
 891                rc = -ENOMEM;
 892        }
 893        oz_complete_urb(hcd, urb, rc, 0);
 894}
 895/*------------------------------------------------------------------------------
 896 * Context: softirq
 897 */
 898void oz_hcd_control_cnf(void *hport, u8 req_id, u8 rcode, const u8 *data,
 899        int data_len)
 900{
 901        struct oz_port *port = (struct oz_port *)hport;
 902        struct urb *urb;
 903        struct usb_ctrlrequest *setup;
 904        struct usb_hcd *hcd = port->ozhcd->hcd;
 905        unsigned windex;
 906        unsigned wvalue;
 907
 908        oz_event_log(OZ_EVT_CTRL_CNF, 0, req_id, NULL, rcode);
 909        oz_trace("oz_hcd_control_cnf rcode=%u len=%d\n", rcode, data_len);
 910        urb = oz_find_urb_by_id(port, 0, req_id);
 911        if (!urb) {
 912                oz_trace("URB not found\n");
 913                return;
 914        }
 915        setup = (struct usb_ctrlrequest *)urb->setup_packet;
 916        windex = le16_to_cpu(setup->wIndex);
 917        wvalue = le16_to_cpu(setup->wValue);
 918        if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
 919                /* Standard requests */
 920                oz_display_conf_type(setup->bRequest);
 921                switch (setup->bRequest) {
 922                case USB_REQ_SET_CONFIGURATION:
 923                        oz_hcd_complete_set_config(port, urb, rcode,
 924                                (u8)wvalue);
 925                        break;
 926                case USB_REQ_SET_INTERFACE:
 927                        oz_hcd_complete_set_interface(port, urb, rcode,
 928                                (u8)windex, (u8)wvalue);
 929                        break;
 930                default:
 931                        oz_complete_urb(hcd, urb, 0, 0);
 932                }
 933
 934        } else {
 935                int copy_len;
 936                oz_trace("VENDOR-CLASS - cnf\n");
 937                if (data_len) {
 938                        if (data_len <= urb->transfer_buffer_length)
 939                                copy_len = data_len;
 940                        else
 941                                copy_len = urb->transfer_buffer_length;
 942                        memcpy(urb->transfer_buffer, data, copy_len);
 943                        urb->actual_length = copy_len;
 944                }
 945                oz_complete_urb(hcd, urb, 0, 0);
 946        }
 947}
 948/*------------------------------------------------------------------------------
 949 * Context: softirq-serialized
 950 */
 951static int oz_hcd_buffer_data(struct oz_endpoint *ep, const u8 *data,
 952                              int data_len)
 953{
 954        int space;
 955        int copy_len;
 956        if (!ep->buffer)
 957                return -1;
 958        space = ep->out_ix-ep->in_ix-1;
 959        if (space < 0)
 960                space += ep->buffer_size;
 961        if (space < (data_len+1)) {
 962                oz_trace("Buffer full\n");
 963                return -1;
 964        }
 965        ep->buffer[ep->in_ix] = (u8)data_len;
 966        if (++ep->in_ix == ep->buffer_size)
 967                ep->in_ix = 0;
 968        copy_len = ep->buffer_size - ep->in_ix;
 969        if (copy_len > data_len)
 970                copy_len = data_len;
 971        memcpy(&ep->buffer[ep->in_ix], data, copy_len);
 972
 973        if (copy_len < data_len) {
 974                memcpy(ep->buffer, data+copy_len, data_len-copy_len);
 975                ep->in_ix = data_len-copy_len;
 976        } else {
 977                ep->in_ix += copy_len;
 978        }
 979        if (ep->in_ix == ep->buffer_size)
 980                ep->in_ix = 0;
 981        ep->buffered_units++;
 982        return 0;
 983}
 984/*------------------------------------------------------------------------------
 985 * Context: softirq-serialized
 986 */
 987void oz_hcd_data_ind(void *hport, u8 endpoint, const u8 *data, int data_len)
 988{
 989        struct oz_port *port = (struct oz_port *)hport;
 990        struct oz_endpoint *ep;
 991        struct oz_hcd *ozhcd = port->ozhcd;
 992        spin_lock_bh(&ozhcd->hcd_lock);
 993        ep = port->in_ep[endpoint & USB_ENDPOINT_NUMBER_MASK];
 994        if (ep == NULL)
 995                goto done;
 996        switch (ep->attrib & USB_ENDPOINT_XFERTYPE_MASK) {
 997        case USB_ENDPOINT_XFER_INT:
 998        case USB_ENDPOINT_XFER_BULK:
 999                if (!list_empty(&ep->urb_list)) {
1000                        struct oz_urb_link *urbl =
1001                                list_first_entry(&ep->urb_list,
1002                                        struct oz_urb_link, link);
1003                        struct urb *urb;
1004                        int copy_len;
1005                        list_del_init(&urbl->link);
1006                        spin_unlock_bh(&ozhcd->hcd_lock);
1007                        urb = urbl->urb;
1008                        oz_free_urb_link(urbl);
1009                        if (data_len <= urb->transfer_buffer_length)
1010                                copy_len = data_len;
1011                        else
1012                                copy_len = urb->transfer_buffer_length;
1013                        memcpy(urb->transfer_buffer, data, copy_len);
1014                        urb->actual_length = copy_len;
1015                        oz_complete_urb(port->ozhcd->hcd, urb, 0, 0);
1016                        return;
1017                } else {
1018                        oz_trace("buffering frame as URB is not available\n");
1019                        oz_hcd_buffer_data(ep, data, data_len);
1020                }
1021                break;
1022        case USB_ENDPOINT_XFER_ISOC:
1023                oz_hcd_buffer_data(ep, data, data_len);
1024                break;
1025        }
1026done:
1027        spin_unlock_bh(&ozhcd->hcd_lock);
1028}
1029/*------------------------------------------------------------------------------
1030 * Context: unknown
1031 */
1032static inline int oz_usb_get_frame_number(void)
1033{
1034        return jiffies_to_msecs(get_jiffies_64());
1035}
1036/*------------------------------------------------------------------------------
1037 * Context: softirq
1038 */
1039int oz_hcd_heartbeat(void *hport)
1040{
1041        int rc = 0;
1042        struct oz_port *port = (struct oz_port *)hport;
1043        struct oz_hcd *ozhcd = port->ozhcd;
1044        struct oz_urb_link *urbl;
1045        struct list_head xfr_list;
1046        struct list_head *e;
1047        struct list_head *n;
1048        struct urb *urb;
1049        struct oz_endpoint *ep;
1050        unsigned long now = jiffies;
1051        INIT_LIST_HEAD(&xfr_list);
1052        /* Check the OUT isoc endpoints to see if any URB data can be sent.
1053         */
1054        spin_lock_bh(&ozhcd->hcd_lock);
1055        list_for_each(e, &port->isoc_out_ep) {
1056                ep = ep_from_link(e);
1057                if (ep->credit < 0)
1058                        continue;
1059                ep->credit += jiffies_to_msecs(now - ep->last_jiffies);
1060                if (ep->credit > ep->credit_ceiling)
1061                        ep->credit = ep->credit_ceiling;
1062                oz_event_log(OZ_EVT_EP_CREDIT, ep->ep_num, 0, NULL,
1063                             ep->credit);
1064                ep->last_jiffies = now;
1065                while (ep->credit && !list_empty(&ep->urb_list)) {
1066                        urbl = list_first_entry(&ep->urb_list,
1067                                struct oz_urb_link, link);
1068                        urb = urbl->urb;
1069                        if ((ep->credit + 1) < urb->number_of_packets)
1070                                break;
1071                        ep->credit -= urb->number_of_packets;
1072                        oz_event_log(OZ_EVT_EP_CREDIT, ep->ep_num, 0, NULL,
1073                                     ep->credit);
1074                        list_move_tail(&urbl->link, &xfr_list);
1075                }
1076        }
1077        spin_unlock_bh(&ozhcd->hcd_lock);
1078        /* Send to PD and complete URBs.
1079         */
1080        list_for_each_safe(e, n, &xfr_list) {
1081                unsigned long t;
1082                urbl = container_of(e, struct oz_urb_link, link);
1083                urb = urbl->urb;
1084                t = urbl->submit_jiffies;
1085                list_del_init(e);
1086                urb->error_count = 0;
1087                urb->start_frame = oz_usb_get_frame_number();
1088                oz_usb_send_isoc(port->hpd, urbl->ep_num, urb);
1089                oz_free_urb_link(urbl);
1090                oz_complete_urb(port->ozhcd->hcd, urb, 0, t);
1091        }
1092        /* Check the IN isoc endpoints to see if any URBs can be completed.
1093         */
1094        spin_lock_bh(&ozhcd->hcd_lock);
1095        list_for_each(e, &port->isoc_in_ep) {
1096                struct oz_endpoint *ep = ep_from_link(e);
1097                if (ep->flags & OZ_F_EP_BUFFERING) {
1098                        if (ep->buffered_units >= OZ_IN_BUFFERING_UNITS) {
1099                                ep->flags &= ~OZ_F_EP_BUFFERING;
1100                                ep->credit = 0;
1101                                oz_event_log(OZ_EVT_EP_CREDIT,
1102                                        ep->ep_num | USB_DIR_IN,
1103                                        0, NULL, ep->credit);
1104                                ep->last_jiffies = now;
1105                                ep->start_frame = 0;
1106                                oz_event_log(OZ_EVT_EP_BUFFERING,
1107                                        ep->ep_num | USB_DIR_IN, 0, NULL, 0);
1108                        }
1109                        continue;
1110                }
1111                ep->credit += jiffies_to_msecs(now - ep->last_jiffies);
1112                oz_event_log(OZ_EVT_EP_CREDIT, ep->ep_num | USB_DIR_IN,
1113                        0, NULL, ep->credit);
1114                ep->last_jiffies = now;
1115                while (!list_empty(&ep->urb_list)) {
1116                        struct oz_urb_link *urbl =
1117                                list_first_entry(&ep->urb_list,
1118                                        struct oz_urb_link, link);
1119                        struct urb *urb = urbl->urb;
1120                        int len = 0;
1121                        int copy_len;
1122                        int i;
1123                        if ((ep->credit + 1) < urb->number_of_packets)
1124                                break;
1125                        if (ep->buffered_units < urb->number_of_packets)
1126                                break;
1127                        urb->actual_length = 0;
1128                        for (i = 0; i < urb->number_of_packets; i++) {
1129                                len = ep->buffer[ep->out_ix];
1130                                if (++ep->out_ix == ep->buffer_size)
1131                                        ep->out_ix = 0;
1132                                copy_len = ep->buffer_size - ep->out_ix;
1133                                if (copy_len > len)
1134                                        copy_len = len;
1135                                memcpy(urb->transfer_buffer,
1136                                        &ep->buffer[ep->out_ix], copy_len);
1137                                if (copy_len < len) {
1138                                        memcpy(urb->transfer_buffer+copy_len,
1139                                                ep->buffer, len-copy_len);
1140                                        ep->out_ix = len-copy_len;
1141                                } else
1142                                        ep->out_ix += copy_len;
1143                                if (ep->out_ix == ep->buffer_size)
1144                                        ep->out_ix = 0;
1145                                urb->iso_frame_desc[i].offset =
1146                                        urb->actual_length;
1147                                urb->actual_length += len;
1148                                urb->iso_frame_desc[i].actual_length = len;
1149                                urb->iso_frame_desc[i].status = 0;
1150                        }
1151                        ep->buffered_units -= urb->number_of_packets;
1152                        urb->error_count = 0;
1153                        urb->start_frame = ep->start_frame;
1154                        ep->start_frame += urb->number_of_packets;
1155                        list_move_tail(&urbl->link, &xfr_list);
1156                        ep->credit -= urb->number_of_packets;
1157                        oz_event_log(OZ_EVT_EP_CREDIT, ep->ep_num | USB_DIR_IN,
1158                                0, NULL, ep->credit);
1159                }
1160        }
1161        if (!list_empty(&port->isoc_out_ep) || !list_empty(&port->isoc_in_ep))
1162                rc = 1;
1163        spin_unlock_bh(&ozhcd->hcd_lock);
1164        /* Complete the filled URBs.
1165         */
1166        list_for_each_safe(e, n, &xfr_list) {
1167                urbl = container_of(e, struct oz_urb_link, link);
1168                urb = urbl->urb;
1169                list_del_init(e);
1170                oz_free_urb_link(urbl);
1171                oz_complete_urb(port->ozhcd->hcd, urb, 0, 0);
1172        }
1173        /* Check if there are any ep0 requests that have timed out.
1174         * If so resent to PD.
1175         */
1176        ep = port->out_ep[0];
1177        if (ep) {
1178                struct list_head *e;
1179                struct list_head *n;
1180                spin_lock_bh(&ozhcd->hcd_lock);
1181                list_for_each_safe(e, n, &ep->urb_list) {
1182                        urbl = container_of(e, struct oz_urb_link, link);
1183                        if (time_after(now, urbl->submit_jiffies+HZ/2)) {
1184                                oz_trace("%ld: Request 0x%p timeout\n",
1185                                                now, urbl->urb);
1186                                urbl->submit_jiffies = now;
1187                                list_move_tail(e, &xfr_list);
1188                        }
1189                }
1190                if (!list_empty(&ep->urb_list))
1191                        rc = 1;
1192                spin_unlock_bh(&ozhcd->hcd_lock);
1193                e = xfr_list.next;
1194                while (e != &xfr_list) {
1195                        urbl = container_of(e, struct oz_urb_link, link);
1196                        e = e->next;
1197                        oz_trace("Resending request to PD.\n");
1198                        oz_process_ep0_urb(ozhcd, urbl->urb, GFP_ATOMIC);
1199                        oz_free_urb_link(urbl);
1200                }
1201        }
1202        return rc;
1203}
1204/*------------------------------------------------------------------------------
1205 * Context: softirq
1206 */
1207static int oz_build_endpoints_for_interface(struct usb_hcd *hcd,
1208                struct oz_port *port,
1209                struct usb_host_interface *intf, gfp_t mem_flags)
1210{
1211        struct oz_hcd *ozhcd = port->ozhcd;
1212        int i;
1213        int if_ix = intf->desc.bInterfaceNumber;
1214        int request_heartbeat = 0;
1215        oz_trace("interface[%d] = %p\n", if_ix, intf);
1216        for (i = 0; i < intf->desc.bNumEndpoints; i++) {
1217                struct usb_host_endpoint *hep = &intf->endpoint[i];
1218                u8 ep_addr = hep->desc.bEndpointAddress;
1219                u8 ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1220                struct oz_endpoint *ep;
1221                int buffer_size = 0;
1222
1223                oz_trace("%d bEndpointAddress = %x\n", i, ep_addr);
1224                if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1225                        switch (hep->desc.bmAttributes &
1226                                                USB_ENDPOINT_XFERTYPE_MASK) {
1227                        case USB_ENDPOINT_XFER_ISOC:
1228                                buffer_size = 24*1024;
1229                                break;
1230                        case USB_ENDPOINT_XFER_INT:
1231                                buffer_size = 128;
1232                                break;
1233                        }
1234                }
1235
1236                ep = oz_ep_alloc(mem_flags, buffer_size);
1237                if (!ep) {
1238                        oz_clean_endpoints_for_interface(hcd, port, if_ix);
1239                        return -ENOMEM;
1240                }
1241                ep->attrib = hep->desc.bmAttributes;
1242                ep->ep_num = ep_num;
1243                if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1244                        == USB_ENDPOINT_XFER_ISOC) {
1245                        oz_trace("wMaxPacketSize = %d\n",
1246                                hep->desc.wMaxPacketSize);
1247                        ep->credit_ceiling = 200;
1248                        if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1249                                ep->flags |= OZ_F_EP_BUFFERING;
1250                                oz_event_log(OZ_EVT_EP_BUFFERING,
1251                                        ep->ep_num | USB_DIR_IN, 1, NULL, 0);
1252                        } else {
1253                                ep->flags |= OZ_F_EP_HAVE_STREAM;
1254                                if (oz_usb_stream_create(port->hpd, ep_num))
1255                                        ep->flags &= ~OZ_F_EP_HAVE_STREAM;
1256                        }
1257                }
1258                spin_lock_bh(&ozhcd->hcd_lock);
1259                if (ep_addr & USB_ENDPOINT_DIR_MASK) {
1260                        port->in_ep[ep_num] = ep;
1261                        port->iface[if_ix].ep_mask |=
1262                                (1<<(ep_num+OZ_NB_ENDPOINTS));
1263                        if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1264                                 == USB_ENDPOINT_XFER_ISOC) {
1265                                list_add_tail(&ep->link, &port->isoc_in_ep);
1266                                request_heartbeat = 1;
1267                        }
1268                } else {
1269                        port->out_ep[ep_num] = ep;
1270                        port->iface[if_ix].ep_mask |= (1<<ep_num);
1271                        if ((ep->attrib & USB_ENDPOINT_XFERTYPE_MASK)
1272                                == USB_ENDPOINT_XFER_ISOC) {
1273                                list_add_tail(&ep->link, &port->isoc_out_ep);
1274                                request_heartbeat = 1;
1275                        }
1276                }
1277                spin_unlock_bh(&ozhcd->hcd_lock);
1278                if (request_heartbeat && port->hpd)
1279                        oz_usb_request_heartbeat(port->hpd);
1280        }
1281        return 0;
1282}
1283/*------------------------------------------------------------------------------
1284 * Context: softirq
1285 */
1286static void oz_clean_endpoints_for_interface(struct usb_hcd *hcd,
1287                        struct oz_port *port, int if_ix)
1288{
1289        struct oz_hcd *ozhcd = port->ozhcd;
1290        unsigned mask;
1291        int i;
1292        struct list_head ep_list;
1293
1294        oz_trace("Deleting endpoints for interface %d\n", if_ix);
1295        if (if_ix >= port->num_iface)
1296                return;
1297        INIT_LIST_HEAD(&ep_list);
1298        spin_lock_bh(&ozhcd->hcd_lock);
1299        mask = port->iface[if_ix].ep_mask;
1300        port->iface[if_ix].ep_mask = 0;
1301        for (i = 0; i < OZ_NB_ENDPOINTS; i++) {
1302                struct list_head *e;
1303                /* Gather OUT endpoints.
1304                 */
1305                if ((mask & (1<<i)) && port->out_ep[i]) {
1306                        e = &port->out_ep[i]->link;
1307                        port->out_ep[i] = NULL;
1308                        /* Remove from isoc list if present.
1309                         */
1310                        list_move_tail(e, &ep_list);
1311                }
1312                /* Gather IN endpoints.
1313                 */
1314                if ((mask & (1<<(i+OZ_NB_ENDPOINTS))) && port->in_ep[i]) {
1315                        e = &port->in_ep[i]->link;
1316                        port->in_ep[i] = NULL;
1317                        list_move_tail(e, &ep_list);
1318                }
1319        }
1320        spin_unlock_bh(&ozhcd->hcd_lock);
1321        while (!list_empty(&ep_list)) {
1322                struct oz_endpoint *ep =
1323                        list_first_entry(&ep_list, struct oz_endpoint, link);
1324                list_del_init(&ep->link);
1325                oz_ep_free(port, ep);
1326        }
1327}
1328/*------------------------------------------------------------------------------
1329 * Context: softirq
1330 */
1331static int oz_build_endpoints_for_config(struct usb_hcd *hcd,
1332                struct oz_port *port, struct usb_host_config *config,
1333                gfp_t mem_flags)
1334{
1335        struct oz_hcd *ozhcd = port->ozhcd;
1336        int i;
1337        int num_iface = config->desc.bNumInterfaces;
1338        if (num_iface) {
1339                struct oz_interface *iface;
1340
1341                iface = kmalloc(num_iface*sizeof(struct oz_interface),
1342                                mem_flags | __GFP_ZERO);
1343                if (!iface)
1344                        return -ENOMEM;
1345                spin_lock_bh(&ozhcd->hcd_lock);
1346                port->iface = iface;
1347                port->num_iface = num_iface;
1348                spin_unlock_bh(&ozhcd->hcd_lock);
1349        }
1350        for (i = 0; i < num_iface; i++) {
1351                struct usb_host_interface *intf =
1352                        &config->intf_cache[i]->altsetting[0];
1353                if (oz_build_endpoints_for_interface(hcd, port, intf,
1354                        mem_flags))
1355                        goto fail;
1356        }
1357        return 0;
1358fail:
1359        oz_clean_endpoints_for_config(hcd, port);
1360        return -1;
1361}
1362/*------------------------------------------------------------------------------
1363 * Context: softirq
1364 */
1365static void oz_clean_endpoints_for_config(struct usb_hcd *hcd,
1366                        struct oz_port *port)
1367{
1368        struct oz_hcd *ozhcd = port->ozhcd;
1369        int i;
1370        oz_trace("Deleting endpoints for configuration.\n");
1371        for (i = 0; i < port->num_iface; i++)
1372                oz_clean_endpoints_for_interface(hcd, port, i);
1373        spin_lock_bh(&ozhcd->hcd_lock);
1374        if (port->iface) {
1375                oz_trace("Freeing interfaces object.\n");
1376                kfree(port->iface);
1377                port->iface = NULL;
1378        }
1379        port->num_iface = 0;
1380        spin_unlock_bh(&ozhcd->hcd_lock);
1381}
1382/*------------------------------------------------------------------------------
1383 * Context: tasklet
1384 */
1385static void *oz_claim_hpd(struct oz_port *port)
1386{
1387        void *hpd = NULL;
1388        struct oz_hcd *ozhcd = port->ozhcd;
1389        spin_lock_bh(&ozhcd->hcd_lock);
1390        hpd = port->hpd;
1391        if (hpd)
1392                oz_usb_get(hpd);
1393        spin_unlock_bh(&ozhcd->hcd_lock);
1394        return hpd;
1395}
1396/*------------------------------------------------------------------------------
1397 * Context: tasklet
1398 */
1399static void oz_process_ep0_urb(struct oz_hcd *ozhcd, struct urb *urb,
1400                gfp_t mem_flags)
1401{
1402        struct usb_ctrlrequest *setup;
1403        unsigned windex;
1404        unsigned wvalue;
1405        unsigned wlength;
1406        void *hpd = NULL;
1407        u8 req_id;
1408        int rc = 0;
1409        unsigned complete = 0;
1410
1411        int port_ix = -1;
1412        struct oz_port *port = NULL;
1413
1414        oz_trace2(OZ_TRACE_URB, "%lu: oz_process_ep0_urb(%p)\n", jiffies, urb);
1415        port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1416        if (port_ix < 0) {
1417                rc = -EPIPE;
1418                goto out;
1419        }
1420        port =  &ozhcd->ports[port_ix];
1421        if (((port->flags & OZ_PORT_F_PRESENT) == 0)
1422                || (port->flags & OZ_PORT_F_DYING)) {
1423                oz_trace("Refusing URB port_ix = %d devnum = %d\n",
1424                        port_ix, urb->dev->devnum);
1425                rc = -EPIPE;
1426                goto out;
1427        }
1428        /* Store port in private context data.
1429         */
1430        urb->hcpriv = port;
1431        setup = (struct usb_ctrlrequest *)urb->setup_packet;
1432        windex = le16_to_cpu(setup->wIndex);
1433        wvalue = le16_to_cpu(setup->wValue);
1434        wlength = le16_to_cpu(setup->wLength);
1435        oz_trace2(OZ_TRACE_CTRL_DETAIL, "bRequestType = %x\n",
1436                setup->bRequestType);
1437        oz_trace2(OZ_TRACE_CTRL_DETAIL, "bRequest = %x\n", setup->bRequest);
1438        oz_trace2(OZ_TRACE_CTRL_DETAIL, "wValue = %x\n", wvalue);
1439        oz_trace2(OZ_TRACE_CTRL_DETAIL, "wIndex = %x\n", windex);
1440        oz_trace2(OZ_TRACE_CTRL_DETAIL, "wLength = %x\n", wlength);
1441
1442        req_id = port->next_req_id++;
1443        hpd = oz_claim_hpd(port);
1444        if (hpd == NULL) {
1445                oz_trace("Cannot claim port\n");
1446                rc = -EPIPE;
1447                goto out;
1448        }
1449
1450        if ((setup->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1451                /* Standard requests
1452                 */
1453                switch (setup->bRequest) {
1454                case USB_REQ_GET_DESCRIPTOR:
1455                        oz_trace("USB_REQ_GET_DESCRIPTOR - req\n");
1456                        break;
1457                case USB_REQ_SET_ADDRESS:
1458                        oz_event_log(OZ_EVT_CTRL_LOCAL, setup->bRequest,
1459                                0, NULL, setup->bRequestType);
1460                        oz_trace("USB_REQ_SET_ADDRESS - req\n");
1461                        oz_trace("Port %d address is 0x%x\n", ozhcd->conn_port,
1462                                (u8)le16_to_cpu(setup->wValue));
1463                        spin_lock_bh(&ozhcd->hcd_lock);
1464                        if (ozhcd->conn_port >= 0) {
1465                                ozhcd->ports[ozhcd->conn_port].bus_addr =
1466                                        (u8)le16_to_cpu(setup->wValue);
1467                                oz_trace("Clearing conn_port\n");
1468                                ozhcd->conn_port = -1;
1469                        }
1470                        spin_unlock_bh(&ozhcd->hcd_lock);
1471                        complete = 1;
1472                        break;
1473                case USB_REQ_SET_CONFIGURATION:
1474                        oz_trace("USB_REQ_SET_CONFIGURATION - req\n");
1475                        break;
1476                case USB_REQ_GET_CONFIGURATION:
1477                        /* We short circuit this case and reply directly since
1478                         * we have the selected configuration number cached.
1479                         */
1480                        oz_event_log(OZ_EVT_CTRL_LOCAL, setup->bRequest, 0,
1481                                     NULL, setup->bRequestType);
1482                        oz_trace("USB_REQ_GET_CONFIGURATION - reply now\n");
1483                        if (urb->transfer_buffer_length >= 1) {
1484                                urb->actual_length = 1;
1485                                *((u8 *)urb->transfer_buffer) =
1486                                        port->config_num;
1487                                complete = 1;
1488                        } else {
1489                                rc = -EPIPE;
1490                        }
1491                        break;
1492                case USB_REQ_GET_INTERFACE:
1493                        /* We short circuit this case and reply directly since
1494                         * we have the selected interface alternative cached.
1495                         */
1496                        oz_event_log(OZ_EVT_CTRL_LOCAL, setup->bRequest, 0,
1497                                     NULL, setup->bRequestType);
1498                        oz_trace("USB_REQ_GET_INTERFACE - reply now\n");
1499                        if (urb->transfer_buffer_length >= 1) {
1500                                urb->actual_length = 1;
1501                                *((u8 *)urb->transfer_buffer) =
1502                                        port->iface[(u8)windex].alt;
1503                                oz_trace("interface = %d alt = %d\n",
1504                                        windex, port->iface[(u8)windex].alt);
1505                                complete = 1;
1506                        } else {
1507                                rc = -EPIPE;
1508                        }
1509                        break;
1510                case USB_REQ_SET_INTERFACE:
1511                        oz_trace("USB_REQ_SET_INTERFACE - req\n");
1512                        break;
1513                }
1514        }
1515        if (!rc && !complete) {
1516                int data_len = 0;
1517                if ((setup->bRequestType & USB_DIR_IN) == 0)
1518                        data_len = wlength;
1519                urb->actual_length = data_len;
1520                if (oz_usb_control_req(port->hpd, req_id, setup,
1521                                urb->transfer_buffer, data_len)) {
1522                        rc = -ENOMEM;
1523                } else {
1524                        /* Note: we are queuing the request after we have
1525                         * submitted it to be transmitted. If the request were
1526                         * to complete before we queued it then it would not
1527                         * be found in the queue. It seems impossible for
1528                         * this to happen but if it did the request would
1529                         * be resubmitted so the problem would hopefully
1530                         * resolve itself. Putting the request into the
1531                         * queue before it has been sent is worse since the
1532                         * urb could be cancelled while we are using it
1533                         * to build the request.
1534                         */
1535                        if (oz_enqueue_ep_urb(port, 0, 0, urb, req_id))
1536                                rc = -ENOMEM;
1537                }
1538        }
1539        oz_usb_put(hpd);
1540out:
1541        if (rc || complete) {
1542                oz_trace("Completing request locally\n");
1543                oz_complete_urb(ozhcd->hcd, urb, rc, 0);
1544        } else {
1545                oz_usb_request_heartbeat(port->hpd);
1546        }
1547}
1548/*------------------------------------------------------------------------------
1549 * Context: tasklet
1550 */
1551static int oz_urb_process(struct oz_hcd *ozhcd, struct urb *urb)
1552{
1553        int rc = 0;
1554        struct oz_port *port = urb->hcpriv;
1555        u8 ep_addr;
1556        /* When we are paranoid we keep a list of urbs which we check against
1557         * before handing one back. This is just for debugging during
1558         * development and should be turned off in the released driver.
1559         */
1560        oz_remember_urb(urb);
1561        /* Check buffer is valid.
1562         */
1563        if (!urb->transfer_buffer && urb->transfer_buffer_length)
1564                return -EINVAL;
1565        /* Check if there is a device at the port - refuse if not.
1566         */
1567        if ((port->flags & OZ_PORT_F_PRESENT) == 0)
1568                return -EPIPE;
1569        ep_addr = usb_pipeendpoint(urb->pipe);
1570        if (ep_addr) {
1571                /* If the request is not for EP0 then queue it.
1572                 */
1573                if (oz_enqueue_ep_urb(port, ep_addr, usb_pipein(urb->pipe),
1574                        urb, 0))
1575                        rc = -EPIPE;
1576        } else {
1577                oz_process_ep0_urb(ozhcd, urb, GFP_ATOMIC);
1578        }
1579        return rc;
1580}
1581/*------------------------------------------------------------------------------
1582 * Context: tasklet
1583 */
1584static void oz_urb_process_tasklet(unsigned long unused)
1585{
1586        unsigned long irq_state;
1587        struct urb *urb;
1588        struct oz_hcd *ozhcd = oz_hcd_claim();
1589        int rc = 0;
1590        if (ozhcd == NULL)
1591                return;
1592        /* This is called from a tasklet so is in softirq context but the urb
1593         * list is filled from any context so we need to lock
1594         * appropriately while removing urbs.
1595         */
1596        spin_lock_irqsave(&g_tasklet_lock, irq_state);
1597        while (!list_empty(&ozhcd->urb_pending_list)) {
1598                struct oz_urb_link *urbl =
1599                        list_first_entry(&ozhcd->urb_pending_list,
1600                                struct oz_urb_link, link);
1601                list_del_init(&urbl->link);
1602                spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1603                urb = urbl->urb;
1604                oz_free_urb_link(urbl);
1605                rc = oz_urb_process(ozhcd, urb);
1606                if (rc)
1607                        oz_complete_urb(ozhcd->hcd, urb, rc, 0);
1608                spin_lock_irqsave(&g_tasklet_lock, irq_state);
1609        }
1610        spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1611        oz_hcd_put(ozhcd);
1612}
1613/*------------------------------------------------------------------------------
1614 * This function searches for the urb in any of the lists it could be in.
1615 * If it is found it is removed from the list and completed. If the urb is
1616 * being processed then it won't be in a list so won't be found. However, the
1617 * call to usb_hcd_check_unlink_urb() will set the value of the unlinked field
1618 * to a non-zero value. When an attempt is made to put the urb back in a list
1619 * the unlinked field will be checked and the urb will then be completed.
1620 * Context: tasklet
1621 */
1622static void oz_urb_cancel(struct oz_port *port, u8 ep_num, struct urb *urb)
1623{
1624        struct oz_urb_link *urbl = NULL;
1625        struct list_head *e;
1626        struct oz_hcd *ozhcd;
1627        unsigned long irq_state;
1628        u8 ix;
1629        if (port == NULL) {
1630                oz_trace("ERRORERROR: oz_urb_cancel(%p) port is null\n", urb);
1631                return;
1632        }
1633        ozhcd = port->ozhcd;
1634        if (ozhcd == NULL) {
1635                oz_trace("ERRORERROR: oz_urb_cancel(%p) ozhcd is null\n", urb);
1636                return;
1637        }
1638
1639        /* Look in the tasklet queue.
1640         */
1641        spin_lock_irqsave(&g_tasklet_lock, irq_state);
1642        list_for_each(e, &ozhcd->urb_cancel_list) {
1643                urbl = container_of(e, struct oz_urb_link, link);
1644                if (urb == urbl->urb) {
1645                        list_del_init(e);
1646                        spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1647                        goto out2;
1648                }
1649        }
1650        spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1651        urbl = NULL;
1652
1653        /* Look in the orphanage.
1654         */
1655        spin_lock_irqsave(&ozhcd->hcd_lock, irq_state);
1656        list_for_each(e, &ozhcd->orphanage) {
1657                urbl = container_of(e, struct oz_urb_link, link);
1658                if (urbl->urb == urb) {
1659                        list_del(e);
1660                        oz_trace("Found urb in orphanage\n");
1661                        goto out;
1662                }
1663        }
1664        ix = (ep_num & 0xf);
1665        urbl = NULL;
1666        if ((ep_num & USB_DIR_IN) && ix)
1667                urbl = oz_remove_urb(port->in_ep[ix], urb);
1668        else
1669                urbl = oz_remove_urb(port->out_ep[ix], urb);
1670out:
1671        spin_unlock_irqrestore(&ozhcd->hcd_lock, irq_state);
1672out2:
1673        if (urbl) {
1674                urb->actual_length = 0;
1675                oz_free_urb_link(urbl);
1676                oz_complete_urb(ozhcd->hcd, urb, -EPIPE, 0);
1677        }
1678}
1679/*------------------------------------------------------------------------------
1680 * Context: tasklet
1681 */
1682static void oz_urb_cancel_tasklet(unsigned long unused)
1683{
1684        unsigned long irq_state;
1685        struct urb *urb;
1686        struct oz_hcd *ozhcd = oz_hcd_claim();
1687        if (ozhcd == NULL)
1688                return;
1689        spin_lock_irqsave(&g_tasklet_lock, irq_state);
1690        while (!list_empty(&ozhcd->urb_cancel_list)) {
1691                struct oz_urb_link *urbl =
1692                        list_first_entry(&ozhcd->urb_cancel_list,
1693                                struct oz_urb_link, link);
1694                list_del_init(&urbl->link);
1695                spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1696                urb = urbl->urb;
1697                if (urb->unlinked)
1698                        oz_urb_cancel(urbl->port, urbl->ep_num, urb);
1699                oz_free_urb_link(urbl);
1700                spin_lock_irqsave(&g_tasklet_lock, irq_state);
1701        }
1702        spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1703        oz_hcd_put(ozhcd);
1704}
1705/*------------------------------------------------------------------------------
1706 * Context: unknown
1707 */
1708static void oz_hcd_clear_orphanage(struct oz_hcd *ozhcd, int status)
1709{
1710        if (ozhcd) {
1711                struct oz_urb_link *urbl;
1712                while (!list_empty(&ozhcd->orphanage)) {
1713                        urbl = list_first_entry(&ozhcd->orphanage,
1714                                struct oz_urb_link, link);
1715                        list_del(&urbl->link);
1716                        oz_complete_urb(ozhcd->hcd, urbl->urb, status, 0);
1717                        oz_free_urb_link(urbl);
1718                }
1719        }
1720}
1721/*------------------------------------------------------------------------------
1722 * Context: unknown
1723 */
1724static int oz_hcd_start(struct usb_hcd *hcd)
1725{
1726        oz_trace("oz_hcd_start()\n");
1727        hcd->power_budget = 200;
1728        hcd->state = HC_STATE_RUNNING;
1729        hcd->uses_new_polling = 1;
1730        return 0;
1731}
1732/*------------------------------------------------------------------------------
1733 * Context: unknown
1734 */
1735static void oz_hcd_stop(struct usb_hcd *hcd)
1736{
1737        oz_trace("oz_hcd_stop()\n");
1738}
1739/*------------------------------------------------------------------------------
1740 * Context: unknown
1741 */
1742static void oz_hcd_shutdown(struct usb_hcd *hcd)
1743{
1744        oz_trace("oz_hcd_shutdown()\n");
1745}
1746/*------------------------------------------------------------------------------
1747 * Context: any
1748 */
1749#ifdef WANT_EVENT_TRACE
1750static u8 oz_get_irq_ctx(void)
1751{
1752        u8 irq_info = 0;
1753        if (in_interrupt())
1754                irq_info |= 1;
1755        if (in_irq())
1756                irq_info |= 2;
1757        return irq_info;
1758}
1759#endif /* WANT_EVENT_TRACE */
1760/*------------------------------------------------------------------------------
1761 * Called to queue an urb for the device.
1762 * This function should return a non-zero error code if it fails the urb but
1763 * should not call usb_hcd_giveback_urb().
1764 * Context: any
1765 */
1766static int oz_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1767                                gfp_t mem_flags)
1768{
1769        struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1770        int rc = 0;
1771        int port_ix;
1772        struct oz_port *port;
1773        unsigned long irq_state;
1774        struct oz_urb_link *urbl;
1775        oz_trace2(OZ_TRACE_URB, "%lu: oz_hcd_urb_enqueue(%p)\n",
1776                jiffies, urb);
1777        oz_event_log(OZ_EVT_URB_SUBMIT, oz_get_irq_ctx(),
1778                (u16)urb->number_of_packets, urb, urb->pipe);
1779        if (unlikely(ozhcd == NULL)) {
1780                oz_trace2(OZ_TRACE_URB, "%lu: Refused urb(%p) not ozhcd.\n",
1781                        jiffies, urb);
1782                return -EPIPE;
1783        }
1784        if (unlikely(hcd->state != HC_STATE_RUNNING)) {
1785                oz_trace2(OZ_TRACE_URB, "%lu: Refused urb(%p) not running.\n",
1786                        jiffies, urb);
1787                return -EPIPE;
1788        }
1789        port_ix = oz_get_port_from_addr(ozhcd, urb->dev->devnum);
1790        if (port_ix < 0)
1791                return -EPIPE;
1792        port =  &ozhcd->ports[port_ix];
1793        if (port == NULL)
1794                return -EPIPE;
1795        if ((port->flags & OZ_PORT_F_PRESENT) == 0) {
1796                oz_trace("Refusing URB port_ix = %d devnum = %d\n",
1797                        port_ix, urb->dev->devnum);
1798                return -EPIPE;
1799        }
1800        urb->hcpriv = port;
1801        /* Put request in queue for processing by tasklet.
1802         */
1803        urbl = oz_alloc_urb_link();
1804        if (unlikely(urbl == NULL))
1805                return -ENOMEM;
1806        urbl->urb = urb;
1807        spin_lock_irqsave(&g_tasklet_lock, irq_state);
1808        rc = usb_hcd_link_urb_to_ep(hcd, urb);
1809        if (unlikely(rc)) {
1810                spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1811                oz_free_urb_link(urbl);
1812                return rc;
1813        }
1814        list_add_tail(&urbl->link, &ozhcd->urb_pending_list);
1815        spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1816        tasklet_schedule(&g_urb_process_tasklet);
1817        atomic_inc(&g_pending_urbs);
1818        return 0;
1819}
1820/*------------------------------------------------------------------------------
1821 * Context: tasklet
1822 */
1823static struct oz_urb_link *oz_remove_urb(struct oz_endpoint *ep,
1824                                struct urb *urb)
1825{
1826        struct oz_urb_link *urbl = NULL;
1827        struct list_head *e;
1828        if (unlikely(ep == NULL))
1829                return NULL;
1830        list_for_each(e, &ep->urb_list) {
1831                urbl = container_of(e, struct oz_urb_link, link);
1832                if (urbl->urb == urb) {
1833                        list_del_init(e);
1834                        if (usb_pipeisoc(urb->pipe)) {
1835                                ep->credit -= urb->number_of_packets;
1836                                if (ep->credit < 0)
1837                                        ep->credit = 0;
1838                                oz_event_log(OZ_EVT_EP_CREDIT,
1839                                        usb_pipein(urb->pipe) ?
1840                                        (ep->ep_num | USB_DIR_IN) : ep->ep_num,
1841                                        0, NULL, ep->credit);
1842                        }
1843                        return urbl;
1844                }
1845        }
1846        return NULL;
1847}
1848/*------------------------------------------------------------------------------
1849 * Called to dequeue a previously submitted urb for the device.
1850 * Context: any
1851 */
1852static int oz_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1853{
1854        struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1855        struct oz_urb_link *urbl = NULL;
1856        int rc;
1857        unsigned long irq_state;
1858        oz_trace2(OZ_TRACE_URB, "%lu: oz_hcd_urb_dequeue(%p)\n", jiffies, urb);
1859        urbl = oz_alloc_urb_link();
1860        if (unlikely(urbl == NULL))
1861                return -ENOMEM;
1862        spin_lock_irqsave(&g_tasklet_lock, irq_state);
1863        /* The following function checks the urb is still in the queue
1864         * maintained by the core and that the unlinked field is zero.
1865         * If both are true the function sets the unlinked field and returns
1866         * zero. Otherwise it returns an error.
1867         */
1868        rc = usb_hcd_check_unlink_urb(hcd, urb, status);
1869        /* We have to check we haven't completed the urb or are about
1870         * to complete it. When we do we set hcpriv to 0 so if this has
1871         * already happened we don't put the urb in the cancel queue.
1872         */
1873        if ((rc == 0) && urb->hcpriv) {
1874                urbl->urb = urb;
1875                urbl->port = (struct oz_port *)urb->hcpriv;
1876                urbl->ep_num = usb_pipeendpoint(urb->pipe);
1877                if (usb_pipein(urb->pipe))
1878                        urbl->ep_num |= USB_DIR_IN;
1879                list_add_tail(&urbl->link, &ozhcd->urb_cancel_list);
1880                spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1881                tasklet_schedule(&g_urb_cancel_tasklet);
1882        } else {
1883                spin_unlock_irqrestore(&g_tasklet_lock, irq_state);
1884                oz_free_urb_link(urbl);
1885        }
1886        return rc;
1887}
1888/*------------------------------------------------------------------------------
1889 * Context: unknown
1890 */
1891static void oz_hcd_endpoint_disable(struct usb_hcd *hcd,
1892                                struct usb_host_endpoint *ep)
1893{
1894        oz_trace("oz_hcd_endpoint_disable\n");
1895}
1896/*------------------------------------------------------------------------------
1897 * Context: unknown
1898 */
1899static void oz_hcd_endpoint_reset(struct usb_hcd *hcd,
1900                                struct usb_host_endpoint *ep)
1901{
1902        oz_trace("oz_hcd_endpoint_reset\n");
1903}
1904/*------------------------------------------------------------------------------
1905 * Context: unknown
1906 */
1907static int oz_hcd_get_frame_number(struct usb_hcd *hcd)
1908{
1909        oz_trace("oz_hcd_get_frame_number\n");
1910        return oz_usb_get_frame_number();
1911}
1912/*------------------------------------------------------------------------------
1913 * Context: softirq
1914 * This is called as a consquence of us calling usb_hcd_poll_rh_status() and we
1915 * always do that in softirq context.
1916 */
1917static int oz_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
1918{
1919        struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1920        int i;
1921
1922        oz_trace2(OZ_TRACE_HUB, "oz_hcd_hub_status_data()\n");
1923        buf[0] = 0;
1924
1925        spin_lock_bh(&ozhcd->hcd_lock);
1926        for (i = 0; i < OZ_NB_PORTS; i++) {
1927                if (ozhcd->ports[i].flags & OZ_PORT_F_CHANGED) {
1928                        oz_trace2(OZ_TRACE_HUB, "Port %d changed\n", i);
1929                        ozhcd->ports[i].flags &= ~OZ_PORT_F_CHANGED;
1930                        buf[0] |= 1<<(i+1);
1931                }
1932        }
1933        spin_unlock_bh(&ozhcd->hcd_lock);
1934        return buf[0] ? 1 : 0;
1935}
1936/*------------------------------------------------------------------------------
1937 * Context: process
1938 */
1939static void oz_get_hub_descriptor(struct usb_hcd *hcd,
1940                                struct usb_hub_descriptor *desc)
1941{
1942        oz_trace2(OZ_TRACE_HUB, "GetHubDescriptor\n");
1943        memset(desc, 0, sizeof(*desc));
1944        desc->bDescriptorType = 0x29;
1945        desc->bDescLength = 9;
1946        desc->wHubCharacteristics = (__force __u16)
1947                        __constant_cpu_to_le16(0x0001);
1948        desc->bNbrPorts = OZ_NB_PORTS;
1949}
1950/*------------------------------------------------------------------------------
1951 * Context: process
1952 */
1953static int oz_set_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
1954{
1955        struct oz_port *port;
1956        int err = 0;
1957        u8 port_id = (u8)windex;
1958        struct oz_hcd *ozhcd = oz_hcd_private(hcd);
1959        unsigned set_bits = 0;
1960        unsigned clear_bits = 0;
1961        oz_trace2(OZ_TRACE_HUB, "SetPortFeature\n");
1962        if ((port_id < 1) || (port_id > OZ_NB_PORTS))
1963                return -EPIPE;
1964        port = &ozhcd->ports[port_id-1];
1965        switch (wvalue) {
1966        case USB_PORT_FEAT_CONNECTION:
1967                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_CONNECTION\n");
1968                break;
1969        case USB_PORT_FEAT_ENABLE:
1970                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_ENABLE\n");
1971                break;
1972        case USB_PORT_FEAT_SUSPEND:
1973                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_SUSPEND\n");
1974                break;
1975        case USB_PORT_FEAT_OVER_CURRENT:
1976                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
1977                break;
1978        case USB_PORT_FEAT_RESET:
1979                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_RESET\n");
1980                set_bits = USB_PORT_STAT_ENABLE | (USB_PORT_STAT_C_RESET<<16);
1981                clear_bits = USB_PORT_STAT_RESET;
1982                ozhcd->ports[port_id-1].bus_addr = 0;
1983                break;
1984        case USB_PORT_FEAT_POWER:
1985                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_POWER\n");
1986                set_bits |= USB_PORT_STAT_POWER;
1987                break;
1988        case USB_PORT_FEAT_LOWSPEED:
1989                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_LOWSPEED\n");
1990                break;
1991        case USB_PORT_FEAT_C_CONNECTION:
1992                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_C_CONNECTION\n");
1993                break;
1994        case USB_PORT_FEAT_C_ENABLE:
1995                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_C_ENABLE\n");
1996                break;
1997        case USB_PORT_FEAT_C_SUSPEND:
1998                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_C_SUSPEND\n");
1999                break;
2000        case USB_PORT_FEAT_C_OVER_CURRENT:
2001                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2002                break;
2003        case USB_PORT_FEAT_C_RESET:
2004                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_C_RESET\n");
2005                break;
2006        case USB_PORT_FEAT_TEST:
2007                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_TEST\n");
2008                break;
2009        case USB_PORT_FEAT_INDICATOR:
2010                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_INDICATOR\n");
2011                break;
2012        default:
2013                oz_trace2(OZ_TRACE_HUB, "Other %d\n", wvalue);
2014                break;
2015        }
2016        if (set_bits || clear_bits) {
2017                spin_lock_bh(&port->port_lock);
2018                port->status &= ~clear_bits;
2019                port->status |= set_bits;
2020                spin_unlock_bh(&port->port_lock);
2021        }
2022        oz_trace2(OZ_TRACE_HUB, "Port[%d] status = 0x%x\n", port_id,
2023                port->status);
2024        return err;
2025}
2026/*------------------------------------------------------------------------------
2027 * Context: process
2028 */
2029static int oz_clear_port_feature(struct usb_hcd *hcd, u16 wvalue, u16 windex)
2030{
2031        struct oz_port *port;
2032        int err = 0;
2033        u8 port_id = (u8)windex;
2034        struct oz_hcd *ozhcd = oz_hcd_private(hcd);
2035        unsigned clear_bits = 0;
2036        oz_trace2(OZ_TRACE_HUB, "ClearPortFeature\n");
2037        if ((port_id < 1) || (port_id > OZ_NB_PORTS))
2038                return -EPIPE;
2039        port = &ozhcd->ports[port_id-1];
2040        switch (wvalue) {
2041        case USB_PORT_FEAT_CONNECTION:
2042                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_CONNECTION\n");
2043                break;
2044        case USB_PORT_FEAT_ENABLE:
2045                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_ENABLE\n");
2046                clear_bits = USB_PORT_STAT_ENABLE;
2047                break;
2048        case USB_PORT_FEAT_SUSPEND:
2049                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_SUSPEND\n");
2050                break;
2051        case USB_PORT_FEAT_OVER_CURRENT:
2052                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_OVER_CURRENT\n");
2053                break;
2054        case USB_PORT_FEAT_RESET:
2055                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_RESET\n");
2056                break;
2057        case USB_PORT_FEAT_POWER:
2058                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_POWER\n");
2059                clear_bits |= USB_PORT_STAT_POWER;
2060                break;
2061        case USB_PORT_FEAT_LOWSPEED:
2062                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_LOWSPEED\n");
2063                break;
2064        case USB_PORT_FEAT_C_CONNECTION:
2065                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_C_CONNECTION\n");
2066                clear_bits = (USB_PORT_STAT_C_CONNECTION << 16);
2067                break;
2068        case USB_PORT_FEAT_C_ENABLE:
2069                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_C_ENABLE\n");
2070                clear_bits = (USB_PORT_STAT_C_ENABLE << 16);
2071                break;
2072        case USB_PORT_FEAT_C_SUSPEND:
2073                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_C_SUSPEND\n");
2074                break;
2075        case USB_PORT_FEAT_C_OVER_CURRENT:
2076                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_C_OVER_CURRENT\n");
2077                break;
2078        case USB_PORT_FEAT_C_RESET:
2079                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_C_RESET\n");
2080                clear_bits = (USB_PORT_FEAT_C_RESET << 16);
2081                break;
2082        case USB_PORT_FEAT_TEST:
2083                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_TEST\n");
2084                break;
2085        case USB_PORT_FEAT_INDICATOR:
2086                oz_trace2(OZ_TRACE_HUB, "USB_PORT_FEAT_INDICATOR\n");
2087                break;
2088        default:
2089                oz_trace2(OZ_TRACE_HUB, "Other %d\n", wvalue);
2090                break;
2091        }
2092        if (clear_bits) {
2093                spin_lock_bh(&port->port_lock);
2094                port->status &= ~clear_bits;
2095                spin_unlock_bh(&port->port_lock);
2096        }
2097        oz_trace2(OZ_TRACE_HUB, "Port[%d] status = 0x%x\n", port_id,
2098                ozhcd->ports[port_id-1].status);
2099        return err;
2100}
2101/*------------------------------------------------------------------------------
2102 * Context: process
2103 */
2104static int oz_get_port_status(struct usb_hcd *hcd, u16 windex, char *buf)
2105{
2106        struct oz_hcd *ozhcd;
2107        u32 status = 0;
2108        if ((windex < 1) || (windex > OZ_NB_PORTS))
2109                return -EPIPE;
2110        ozhcd = oz_hcd_private(hcd);
2111        oz_trace2(OZ_TRACE_HUB, "GetPortStatus windex = %d\n", windex);
2112        status = ozhcd->ports[windex-1].status;
2113        put_unaligned(cpu_to_le32(status), (__le32 *)buf);
2114        oz_trace2(OZ_TRACE_HUB, "Port[%d] status = %x\n", windex, status);
2115        return 0;
2116}
2117/*------------------------------------------------------------------------------
2118 * Context: process
2119 */
2120static int oz_hcd_hub_control(struct usb_hcd *hcd, u16 req_type, u16 wvalue,
2121                                u16 windex, char *buf, u16 wlength)
2122{
2123        int err = 0;
2124        oz_trace2(OZ_TRACE_HUB, "oz_hcd_hub_control()\n");
2125        switch (req_type) {
2126        case ClearHubFeature:
2127                oz_trace2(OZ_TRACE_HUB, "ClearHubFeature: %d\n", req_type);
2128                break;
2129        case ClearPortFeature:
2130                err = oz_clear_port_feature(hcd, wvalue, windex);
2131                break;
2132        case GetHubDescriptor:
2133                oz_get_hub_descriptor(hcd, (struct usb_hub_descriptor *)buf);
2134                break;
2135        case GetHubStatus:
2136                oz_trace2(OZ_TRACE_HUB, "GetHubStatus: req_type = 0x%x\n",
2137                        req_type);
2138                put_unaligned(__constant_cpu_to_le32(0), (__le32 *)buf);
2139                break;
2140        case GetPortStatus:
2141                err = oz_get_port_status(hcd, windex, buf);
2142                break;
2143        case SetHubFeature:
2144                oz_trace2(OZ_TRACE_HUB, "SetHubFeature: %d\n", req_type);
2145                break;
2146        case SetPortFeature:
2147                err = oz_set_port_feature(hcd, wvalue, windex);
2148                break;
2149        default:
2150                oz_trace2(OZ_TRACE_HUB, "Other: %d\n", req_type);
2151                break;
2152        }
2153        return err;
2154}
2155/*------------------------------------------------------------------------------
2156 * Context: process
2157 */
2158static int oz_hcd_bus_suspend(struct usb_hcd *hcd)
2159{
2160        struct oz_hcd *ozhcd;
2161        oz_trace2(OZ_TRACE_HUB, "oz_hcd_hub_suspend()\n");
2162        ozhcd = oz_hcd_private(hcd);
2163        spin_lock_bh(&ozhcd->hcd_lock);
2164        hcd->state = HC_STATE_SUSPENDED;
2165        ozhcd->flags |= OZ_HDC_F_SUSPENDED;
2166        spin_unlock_bh(&ozhcd->hcd_lock);
2167        return 0;
2168}
2169/*------------------------------------------------------------------------------
2170 * Context: process
2171 */
2172static int oz_hcd_bus_resume(struct usb_hcd *hcd)
2173{
2174        struct oz_hcd *ozhcd;
2175        oz_trace2(OZ_TRACE_HUB, "oz_hcd_hub_resume()\n");
2176        ozhcd = oz_hcd_private(hcd);
2177        spin_lock_bh(&ozhcd->hcd_lock);
2178        ozhcd->flags &= ~OZ_HDC_F_SUSPENDED;
2179        hcd->state = HC_STATE_RUNNING;
2180        spin_unlock_bh(&ozhcd->hcd_lock);
2181        return 0;
2182}
2183/*------------------------------------------------------------------------------
2184 */
2185static void oz_plat_shutdown(struct platform_device *dev)
2186{
2187        oz_trace("oz_plat_shutdown()\n");
2188}
2189/*------------------------------------------------------------------------------
2190 * Context: process
2191 */
2192static int oz_plat_probe(struct platform_device *dev)
2193{
2194        int i;
2195        int err;
2196        struct usb_hcd *hcd;
2197        struct oz_hcd *ozhcd;
2198        oz_trace("oz_plat_probe()\n");
2199        hcd = usb_create_hcd(&g_oz_hc_drv, &dev->dev, dev_name(&dev->dev));
2200        if (hcd == NULL) {
2201                oz_trace("Failed to created hcd object OK\n");
2202                return -ENOMEM;
2203        }
2204        ozhcd = oz_hcd_private(hcd);
2205        memset(ozhcd, 0, sizeof(*ozhcd));
2206        INIT_LIST_HEAD(&ozhcd->urb_pending_list);
2207        INIT_LIST_HEAD(&ozhcd->urb_cancel_list);
2208        INIT_LIST_HEAD(&ozhcd->orphanage);
2209        ozhcd->hcd = hcd;
2210        ozhcd->conn_port = -1;
2211        spin_lock_init(&ozhcd->hcd_lock);
2212        for (i = 0; i < OZ_NB_PORTS; i++) {
2213                struct oz_port *port = &ozhcd->ports[i];
2214                port->ozhcd = ozhcd;
2215                port->flags = 0;
2216                port->status = 0;
2217                port->bus_addr = 0xff;
2218                spin_lock_init(&port->port_lock);
2219        }
2220        err = usb_add_hcd(hcd, 0, 0);
2221        if (err) {
2222                oz_trace("Failed to add hcd object OK\n");
2223                usb_put_hcd(hcd);
2224                return -1;
2225        }
2226        spin_lock_bh(&g_hcdlock);
2227        g_ozhcd = ozhcd;
2228        spin_unlock_bh(&g_hcdlock);
2229        return 0;
2230}
2231/*------------------------------------------------------------------------------
2232 * Context: unknown
2233 */
2234static int oz_plat_remove(struct platform_device *dev)
2235{
2236        struct usb_hcd *hcd = platform_get_drvdata(dev);
2237        struct oz_hcd *ozhcd;
2238        oz_trace("oz_plat_remove()\n");
2239        if (hcd == NULL)
2240                return -1;
2241        ozhcd = oz_hcd_private(hcd);
2242        spin_lock_bh(&g_hcdlock);
2243        if (ozhcd == g_ozhcd)
2244                g_ozhcd = NULL;
2245        spin_unlock_bh(&g_hcdlock);
2246        oz_trace("Clearing orphanage\n");
2247        oz_hcd_clear_orphanage(ozhcd, -EPIPE);
2248        oz_trace("Removing hcd\n");
2249        usb_remove_hcd(hcd);
2250        usb_put_hcd(hcd);
2251        oz_empty_link_pool();
2252        return 0;
2253}
2254/*------------------------------------------------------------------------------
2255 * Context: unknown
2256 */
2257static int oz_plat_suspend(struct platform_device *dev, pm_message_t msg)
2258{
2259        oz_trace("oz_plat_suspend()\n");
2260        return 0;
2261}
2262/*------------------------------------------------------------------------------
2263 * Context: unknown
2264 */
2265static int oz_plat_resume(struct platform_device *dev)
2266{
2267        oz_trace("oz_plat_resume()\n");
2268        return 0;
2269}
2270/*------------------------------------------------------------------------------
2271 * Context: process
2272 */
2273int oz_hcd_init(void)
2274{
2275        int err;
2276        if (usb_disabled())
2277                return -ENODEV;
2278        tasklet_init(&g_urb_process_tasklet, oz_urb_process_tasklet, 0);
2279        tasklet_init(&g_urb_cancel_tasklet, oz_urb_cancel_tasklet, 0);
2280        err = platform_driver_register(&g_oz_plat_drv);
2281        oz_trace("platform_driver_register() returned %d\n", err);
2282        if (err)
2283                goto error;
2284        g_plat_dev = platform_device_alloc(OZ_PLAT_DEV_NAME, -1);
2285        if (g_plat_dev == NULL) {
2286                err = -ENOMEM;
2287                goto error1;
2288        }
2289        oz_trace("platform_device_alloc() succeeded\n");
2290        err = platform_device_add(g_plat_dev);
2291        if (err)
2292                goto error2;
2293        oz_trace("platform_device_add() succeeded\n");
2294        return 0;
2295error2:
2296        platform_device_put(g_plat_dev);
2297error1:
2298        platform_driver_unregister(&g_oz_plat_drv);
2299error:
2300        tasklet_disable(&g_urb_process_tasklet);
2301        tasklet_disable(&g_urb_cancel_tasklet);
2302        oz_trace("oz_hcd_init() failed %d\n", err);
2303        return err;
2304}
2305/*------------------------------------------------------------------------------
2306 * Context: process
2307 */
2308void oz_hcd_term(void)
2309{
2310        tasklet_kill(&g_urb_process_tasklet);
2311        tasklet_kill(&g_urb_cancel_tasklet);
2312        platform_device_unregister(g_plat_dev);
2313        platform_driver_unregister(&g_oz_plat_drv);
2314        oz_trace("Pending urbs:%d\n", atomic_read(&g_pending_urbs));
2315}
2316