Subversion Repositories shark

Rev

Rev 846 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
846 giacomo 1
/*
2
 * USB hub driver.
3
 *
4
 * (C) Copyright 1999 Linus Torvalds
5
 * (C) Copyright 1999 Johannes Erdfelt
6
 * (C) Copyright 1999 Gregory P. Smith
7
 * (C) Copyright 2001 Brad Hards (bhards@bigpond.net.au)
8
 *
9
 */
10
 
11
#include <linuxcomp.h>
12
 
13
#include <linux/config.h>
14
#include <linux/kernel.h>
15
#include <linux/errno.h>
16
#include <linux/module.h>
17
#include <linux/completion.h>
18
#include <linux/sched.h>
19
#include <linux/list.h>
20
#include <linux/slab.h>
21
#include <linux/smp_lock.h>
22
#include <linux/ioctl.h>
23
#ifdef CONFIG_USB_DEBUG
24
        #define DEBUG
25
#else
26
        #undef DEBUG
27
#endif
28
#include <linux/usb.h>
29
#include <linux/usbdevice_fs.h>
30
#include <linux/suspend.h>
31
 
32
#include <asm/semaphore.h>
33
#include <asm/uaccess.h>
34
#include <asm/byteorder.h>
35
 
36
#include "hcd.h"
37
#include "hub.h"
38
 
39
/* Wakes up khubd */
40
static spinlock_t hub_event_lock = SPIN_LOCK_UNLOCKED;
41
static DECLARE_MUTEX(usb_address0_sem);
42
 
43
static LIST_HEAD(hub_event_list);       /* List of hubs needing servicing */
44
static LIST_HEAD(hub_list);             /* List of all hubs (for cleanup) */
45
 
46
static DECLARE_WAIT_QUEUE_HEAD(khubd_wait);
47
static pid_t khubd_pid = 0;                     /* PID of khubd */
48
static DECLARE_COMPLETION(khubd_exited);
49
 
50
//#ifdef        DEBUG
51
static inline char *portspeed (int portstatus)
52
{
53
        if (portstatus & (1 << USB_PORT_FEAT_HIGHSPEED))
54
                return "480 Mb/s";
55
        else if (portstatus & (1 << USB_PORT_FEAT_LOWSPEED))
56
                return "1.5 Mb/s";
57
        else
58
                return "12 Mb/s";
59
}
60
//#endif
61
 
62
/* for dev_info, dev_dbg, etc */
63
static inline struct device *hubdev (struct usb_device *dev)
64
{
65
        return &dev->actconfig->interface[0]->dev;
66
}
67
 
68
/* USB 2.0 spec Section 11.24.4.5 */
69
static int get_hub_descriptor(struct usb_device *dev, void *data, int size)
70
{
71
        return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
72
                USB_REQ_GET_DESCRIPTOR, USB_DIR_IN | USB_RT_HUB,
73
                USB_DT_HUB << 8, 0, data, size, HZ * USB_CTRL_GET_TIMEOUT);
74
}
75
 
76
/*
77
 * USB 2.0 spec Section 11.24.2.1
78
 */
79
static int clear_hub_feature(struct usb_device *dev, int feature)
80
{
81
        return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
82
                USB_REQ_CLEAR_FEATURE, USB_RT_HUB, feature, 0, NULL, 0, HZ);
83
}
84
 
85
/*
86
 * USB 2.0 spec Section 11.24.2.2
87
 * BUG: doesn't handle port indicator selector in high byte of wIndex
88
 */
89
static int clear_port_feature(struct usb_device *dev, int port, int feature)
90
{
91
        return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
92
                USB_REQ_CLEAR_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
93
}
94
 
95
/*
96
 * USB 2.0 spec Section 11.24.2.13
97
 * BUG: doesn't handle port indicator selector in high byte of wIndex
98
 */
99
static int set_port_feature(struct usb_device *dev, int port, int feature)
100
{
101
        return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
102
                USB_REQ_SET_FEATURE, USB_RT_PORT, feature, port, NULL, 0, HZ);
103
}
104
 
105
/*
106
 * USB 2.0 spec Section 11.24.2.6
107
 */
108
static int get_hub_status(struct usb_device *dev,
109
                struct usb_hub_status *data)
110
{
111
        return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
112
                USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_HUB, 0, 0,
113
                data, sizeof(*data), HZ * USB_CTRL_GET_TIMEOUT);
114
}
115
 
116
/*
117
 * USB 2.0 spec Section 11.24.2.7
118
 */
119
static int get_port_status(struct usb_device *dev, int port,
120
                struct usb_port_status *data)
121
{
122
        return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
123
                USB_REQ_GET_STATUS, USB_DIR_IN | USB_RT_PORT, 0, port,
124
                data, sizeof(*data), HZ * USB_CTRL_GET_TIMEOUT);
125
}
126
 
127
/* completion function, fires on port status changes and various faults */
128
static void hub_irq(struct urb *urb, struct pt_regs *regs)
129
{
130
        struct usb_hub *hub = (struct usb_hub *)urb->context;
1049 mauro 131
//**    unsigned long flags; 2.6.1
846 giacomo 132
        int status;
133
 
1049 mauro 134
        spin_lock(&hub_event_lock);
135
        hub->urb_active = 0;
136
        if (hub->urb_complete) {        /* disconnect or rmmod */
137
                complete(hub->urb_complete);
138
                goto done;
139
        }
140
 
846 giacomo 141
        switch (urb->status) {
142
        case -ENOENT:           /* synchronous unlink */
143
        case -ECONNRESET:       /* async unlink */
144
        case -ESHUTDOWN:        /* hardware going away */
1049 mauro 145
                goto done;
146
//**            return; 2.6.1
147
 
846 giacomo 148
        default:                /* presumably an error */
149
                /* Cause a hub reset after 10 consecutive errors */
150
                dev_dbg (&hub->intf->dev, "transfer --> %d\n", urb->status);
151
                if ((++hub->nerrors < 10) || hub->error)
152
                        goto resubmit;
153
                hub->error = urb->status;
154
                /* FALL THROUGH */
155
 
156
        /* let khubd handle things */
157
        case 0:                 /* we got data:  port status changed */
158
                break;
159
        }
160
 
161
        hub->nerrors = 0;
162
 
163
        /* Something happened, let khubd figure it out */
1049 mauro 164
//**    spin_lock_irqsave(&hub_event_lock, flags); 2.6.1
846 giacomo 165
        if (list_empty(&hub->event_list)) {
166
                list_add(&hub->event_list, &hub_event_list);
167
                wake_up(&khubd_wait);
168
        }
1049 mauro 169
//**    spin_unlock_irqrestore(&hub_event_lock, flags); 2.6.1
846 giacomo 170
 
171
resubmit:
172
        if ((status = usb_submit_urb (hub->urb, GFP_ATOMIC)) != 0
173
                        /* ENODEV means we raced disconnect() */
174
                        && status != -ENODEV)
175
                dev_err (&hub->intf->dev, "resubmit --> %d\n", urb->status);
1049 mauro 176
 
177
        if (status == 0)
178
                hub->urb_active = 1;
179
done:
180
        spin_unlock(&hub_event_lock);
846 giacomo 181
}
182
 
183
/* USB 2.0 spec Section 11.24.2.3 */
184
static inline int
185
hub_clear_tt_buffer (struct usb_device *hub, u16 devinfo, u16 tt)
186
{
187
        return usb_control_msg (hub, usb_rcvctrlpipe (hub, 0),
188
                HUB_CLEAR_TT_BUFFER, USB_DIR_IN | USB_RECIP_OTHER,
189
                devinfo, tt, 0, 0, HZ);
190
}
191
 
192
/*
193
 * enumeration blocks khubd for a long time. we use keventd instead, since
194
 * long blocking there is the exception, not the rule.  accordingly, HCDs
195
 * talking to TTs must queue control transfers (not just bulk and iso), so
196
 * both can talk to the same hub concurrently.
197
 */
198
static void hub_tt_kevent (void *arg)
199
{
200
        struct usb_hub          *hub = arg;
201
        unsigned long           flags;
202
 
203
        spin_lock_irqsave (&hub->tt.lock, flags);
204
        while (!list_empty (&hub->tt.clear_list)) {
205
                struct list_head        *temp;
206
                struct usb_tt_clear     *clear;
207
                struct usb_device       *dev;
208
                int                     status;
209
 
210
                temp = hub->tt.clear_list.next;
211
                clear = list_entry (temp, struct usb_tt_clear, clear_list);
212
                list_del (&clear->clear_list);
213
 
214
                /* drop lock so HCD can concurrently report other TT errors */
215
                spin_unlock_irqrestore (&hub->tt.lock, flags);
216
                dev = interface_to_usbdev (hub->intf);
217
                status = hub_clear_tt_buffer (dev, clear->devinfo, clear->tt);
218
                spin_lock_irqsave (&hub->tt.lock, flags);
219
 
220
                if (status)
221
                        err ("usb-%s-%s clear tt %d (%04x) error %d",
222
                                dev->bus->bus_name, dev->devpath,
223
                                clear->tt, clear->devinfo, status);
224
                kfree (clear);
225
        }
226
        spin_unlock_irqrestore (&hub->tt.lock, flags);
227
}
228
 
229
/**
230
 * usb_hub_tt_clear_buffer - clear control/bulk TT state in high speed hub
231
 * @dev: the device whose split transaction failed
232
 * @pipe: identifies the endpoint of the failed transaction
233
 *
234
 * High speed HCDs use this to tell the hub driver that some split control or
235
 * bulk transaction failed in a way that requires clearing internal state of
236
 * a transaction translator.  This is normally detected (and reported) from
237
 * interrupt context.
238
 *
239
 * It may not be possible for that hub to handle additional full (or low)
240
 * speed transactions until that state is fully cleared out.
241
 */
242
void usb_hub_tt_clear_buffer (struct usb_device *dev, int pipe)
243
{
244
        struct usb_tt           *tt = dev->tt;
245
        unsigned long           flags;
246
        struct usb_tt_clear     *clear;
247
 
248
        /* we've got to cope with an arbitrary number of pending TT clears,
249
         * since each TT has "at least two" buffers that can need it (and
250
         * there can be many TTs per hub).  even if they're uncommon.
251
         */
252
        if ((clear = kmalloc (sizeof *clear, SLAB_ATOMIC)) == 0) {
253
                err ("can't save CLEAR_TT_BUFFER state for hub at usb-%s-%s",
254
                        dev->bus->bus_name, tt->hub->devpath);
255
                /* FIXME recover somehow ... RESET_TT? */
256
                return;
257
        }
258
 
259
        /* info that CLEAR_TT_BUFFER needs */
260
        clear->tt = tt->multi ? dev->ttport : 1;
261
        clear->devinfo = usb_pipeendpoint (pipe);
262
        clear->devinfo |= dev->devnum << 4;
263
        clear->devinfo |= usb_pipecontrol (pipe)
264
                        ? (USB_ENDPOINT_XFER_CONTROL << 11)
265
                        : (USB_ENDPOINT_XFER_BULK << 11);
266
        if (usb_pipein (pipe))
267
                clear->devinfo |= 1 << 15;
268
 
269
        /* tell keventd to clear state for this TT */
270
        spin_lock_irqsave (&tt->lock, flags);
271
        list_add_tail (&clear->clear_list, &tt->clear_list);
272
        schedule_work (&tt->kevent);
273
        spin_unlock_irqrestore (&tt->lock, flags);
274
}
275
 
276
static void hub_power_on(struct usb_hub *hub)
277
{
278
        struct usb_device *dev;
279
        int i;
280
 
281
        /* Enable power to the ports */
282
        dev_dbg(hubdev(interface_to_usbdev(hub->intf)),
283
                "enabling power on all ports\n");
284
        dev = interface_to_usbdev(hub->intf);
285
        for (i = 0; i < hub->descriptor->bNbrPorts; i++)
286
                set_port_feature(dev, i + 1, USB_PORT_FEAT_POWER);
287
 
288
        /* Wait for power to be enabled */
289
        wait_ms(hub->descriptor->bPwrOn2PwrGood * 2);
290
}
291
 
292
static int hub_hub_status(struct usb_hub *hub,
293
                u16 *status, u16 *change)
294
{
295
        struct usb_device *dev = interface_to_usbdev (hub->intf);
296
        int ret;
297
 
298
        ret = get_hub_status(dev, &hub->status->hub);
299
        if (ret < 0)
300
                dev_err (hubdev (dev),
301
                        "%s failed (err = %d)\n", __FUNCTION__, ret);
302
        else {
303
                *status = le16_to_cpu(hub->status->hub.wHubStatus);
304
                *change = le16_to_cpu(hub->status->hub.wHubChange);
305
                ret = 0;
306
        }
307
        return ret;
308
}
309
 
310
static int hub_configure(struct usb_hub *hub,
311
        struct usb_endpoint_descriptor *endpoint)
312
{
313
        struct usb_device *dev = interface_to_usbdev (hub->intf);
314
        struct device *hub_dev;
315
        u16 hubstatus, hubchange;
316
        unsigned int pipe;
317
        int maxp, ret;
318
        char *message;
319
 
320
        hub->buffer = usb_buffer_alloc(dev, sizeof(*hub->buffer), GFP_KERNEL,
321
                        &hub->buffer_dma);
322
        if (!hub->buffer) {
323
                message = "can't allocate hub irq buffer";
324
                ret = -ENOMEM;
325
                goto fail;
326
        }
327
 
328
        hub->status = kmalloc(sizeof(*hub->status), GFP_KERNEL);
329
        if (!hub->status) {
330
                message = "can't kmalloc hub status buffer";
331
                ret = -ENOMEM;
332
                goto fail;
333
        }
334
 
335
        hub->descriptor = kmalloc(sizeof(*hub->descriptor), GFP_KERNEL);
336
        if (!hub->descriptor) {
337
                message = "can't kmalloc hub descriptor";
338
                ret = -ENOMEM;
339
                goto fail;
340
        }
341
 
342
        /* Request the entire hub descriptor.
343
         * hub->descriptor can handle USB_MAXCHILDREN ports,
344
         * but the hub can/will return fewer bytes here.
345
         */
346
        ret = get_hub_descriptor(dev, hub->descriptor,
347
                        sizeof(*hub->descriptor));
348
        if (ret < 0) {
349
                message = "can't read hub descriptor";
350
                goto fail;
351
        } else if (hub->descriptor->bNbrPorts > USB_MAXCHILDREN) {
352
                message = "hub has too many ports!";
353
                ret = -ENODEV;
354
                goto fail;
355
        }
356
 
357
        hub_dev = hubdev(dev);
358
        dev->maxchild = hub->descriptor->bNbrPorts;
359
        dev_info (hub_dev, "%d port%s detected\n", dev->maxchild,
360
                (dev->maxchild == 1) ? "" : "s");
361
 
362
        le16_to_cpus(&hub->descriptor->wHubCharacteristics);
363
 
364
        if (hub->descriptor->wHubCharacteristics & HUB_CHAR_COMPOUND) {
365
                int     i;
366
                char    portstr [USB_MAXCHILDREN + 1];
367
 
368
                for (i = 0; i < dev->maxchild; i++)
369
                        portstr[i] = hub->descriptor->DeviceRemovable
370
                                    [((i + 1) / 8)] & (1 << ((i + 1) % 8))
371
                                ? 'F' : 'R';
372
                portstr[dev->maxchild] = 0;
373
                dev_dbg(hub_dev, "compound device; port removable status: %s\n", portstr);
374
        } else
375
                dev_dbg(hub_dev, "standalone hub\n");
376
 
377
        switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_LPSM) {
378
                case 0x00:
379
                        dev_dbg(hub_dev, "ganged power switching\n");
380
                        break;
381
                case 0x01:
382
                        dev_dbg(hub_dev, "individual port power switching\n");
383
                        break;
384
                case 0x02:
385
                case 0x03:
386
                        dev_dbg(hub_dev, "unknown reserved power switching mode\n");
387
                        break;
388
        }
389
 
390
        switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_OCPM) {
391
                case 0x00:
392
                        dev_dbg(hub_dev, "global over-current protection\n");
393
                        break;
394
                case 0x08:
395
                        dev_dbg(hub_dev, "individual port over-current protection\n");
396
                        break;
397
                case 0x10:
398
                case 0x18:
399
                        dev_dbg(hub_dev, "no over-current protection\n");
400
                        break;
401
        }
402
 
403
        spin_lock_init (&hub->tt.lock);
404
        INIT_LIST_HEAD (&hub->tt.clear_list);
405
        INIT_WORK (&hub->tt.kevent, hub_tt_kevent, hub);
406
        switch (dev->descriptor.bDeviceProtocol) {
407
                case 0:
408
                        break;
409
                case 1:
410
                        dev_dbg(hub_dev, "Single TT\n");
411
                        hub->tt.hub = dev;
412
                        break;
413
                case 2:
414
                        dev_dbg(hub_dev, "TT per port\n");
415
                        hub->tt.hub = dev;
416
                        hub->tt.multi = 1;
417
                        break;
418
                default:
419
                        dev_dbg(hub_dev, "Unrecognized hub protocol %d\n",
420
                                dev->descriptor.bDeviceProtocol);
421
                        break;
422
        }
423
 
424
        switch (hub->descriptor->wHubCharacteristics & HUB_CHAR_TTTT) {
425
                case 0x00:
426
                        if (dev->descriptor.bDeviceProtocol != 0)
427
                                dev_dbg(hub_dev, "TT requires at most 8 FS bit times\n");
428
                        break;
429
                case 0x20:
430
                        dev_dbg(hub_dev, "TT requires at most 16 FS bit times\n");
431
                        break;
432
                case 0x40:
433
                        dev_dbg(hub_dev, "TT requires at most 24 FS bit times\n");
434
                        break;
435
                case 0x60:
436
                        dev_dbg(hub_dev, "TT requires at most 32 FS bit times\n");
437
                        break;
438
        }
439
 
440
        dev_dbg(hub_dev, "Port indicators are %s supported\n",
441
            (hub->descriptor->wHubCharacteristics & HUB_CHAR_PORTIND)
442
                ? "" : "not");
443
 
444
        dev_dbg(hub_dev, "power on to power good time: %dms\n",
445
                hub->descriptor->bPwrOn2PwrGood * 2);
446
        dev_dbg(hub_dev, "hub controller current requirement: %dmA\n",
447
                hub->descriptor->bHubContrCurrent);
448
 
449
        ret = hub_hub_status(hub, &hubstatus, &hubchange);
450
        if (ret < 0) {
451
                message = "can't get hub status";
452
                goto fail;
453
        }
454
 
455
        dev_dbg(hub_dev, "local power source is %s\n",
456
                (hubstatus & HUB_STATUS_LOCAL_POWER)
457
                ? "lost (inactive)" : "good");
458
 
459
        dev_dbg(hub_dev, "%sover-current condition exists\n",
460
                (hubstatus & HUB_STATUS_OVERCURRENT) ? "" : "no ");
461
 
462
        /* Start the interrupt endpoint */
463
        pipe = usb_rcvintpipe(dev, endpoint->bEndpointAddress);
464
        maxp = usb_maxpacket(dev, pipe, usb_pipeout(pipe));
465
 
466
        if (maxp > sizeof(*hub->buffer))
467
                maxp = sizeof(*hub->buffer);
468
 
469
        hub->urb = usb_alloc_urb(0, GFP_KERNEL);
470
        if (!hub->urb) {
471
                message = "couldn't allocate interrupt urb";
472
                ret = -ENOMEM;
473
                goto fail;
474
        }
475
 
476
        usb_fill_int_urb(hub->urb, dev, pipe, *hub->buffer, maxp, hub_irq,
477
                hub, endpoint->bInterval);
478
        hub->urb->transfer_dma = hub->buffer_dma;
479
        hub->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
480
        ret = usb_submit_urb(hub->urb, GFP_KERNEL);
481
        if (ret) {
482
                message = "couldn't submit status urb";
483
                goto fail;
484
        }
1049 mauro 485
        hub->urb_active = 1;
846 giacomo 486
 
487
        /* Wake up khubd */
488
        wake_up(&khubd_wait);
489
 
490
        hub_power_on(hub);
491
 
492
        return 0;
493
 
494
fail:
495
        dev_err (&hub->intf->dev, "config failed, %s (err %d)\n",
496
                        message, ret);
497
        /* hub_disconnect() frees urb and descriptor */
498
        return ret;
499
}
500
 
501
static void hub_disconnect(struct usb_interface *intf)
502
{
503
        struct usb_hub *hub = usb_get_intfdata (intf);
1049 mauro 504
        DECLARE_COMPLETION(urb_complete);
846 giacomo 505
        unsigned long flags;
506
 
507
        if (!hub)
508
                return;
509
 
510
        usb_set_intfdata (intf, NULL);
511
        spin_lock_irqsave(&hub_event_lock, flags);
1049 mauro 512
        hub->urb_complete = &urb_complete;
846 giacomo 513
 
514
        /* Delete it and then reset it */
1049 mauro 515
        list_del_init(&hub->event_list);
516
        list_del_init(&hub->hub_list);
846 giacomo 517
 
518
        spin_unlock_irqrestore(&hub_event_lock, flags);
519
 
520
        down(&hub->khubd_sem); /* Wait for khubd to leave this hub alone. */
521
        up(&hub->khubd_sem);
522
 
523
        /* assuming we used keventd, it must quiesce too */
524
        if (hub->tt.hub)
525
                flush_scheduled_work ();
526
 
527
        if (hub->urb) {
528
                usb_unlink_urb(hub->urb);
1049 mauro 529
                if (hub->urb_active)
530
                        wait_for_completion(&urb_complete);
846 giacomo 531
                usb_free_urb(hub->urb);
532
                hub->urb = NULL;
533
        }
534
 
535
        if (hub->descriptor) {
536
                kfree(hub->descriptor);
537
                hub->descriptor = NULL;
538
        }
539
 
540
        if (hub->status) {
541
                kfree(hub->status);
542
                hub->status = NULL;
543
        }
544
 
545
        if (hub->buffer) {
546
                usb_buffer_free(interface_to_usbdev(intf),
547
                                sizeof(*hub->buffer), hub->buffer,
548
                                hub->buffer_dma);
549
                hub->buffer = NULL;
550
        }
551
 
552
        /* Free the memory */
553
        kfree(hub);
554
}
555
 
556
static int hub_probe(struct usb_interface *intf, const struct usb_device_id *id)
557
{
558
        struct usb_host_interface *desc;
559
        struct usb_endpoint_descriptor *endpoint;
560
        struct usb_device *dev;
561
        struct usb_hub *hub;
562
        unsigned long flags;
563
 
564
        desc = intf->altsetting + intf->act_altsetting;
565
        dev = interface_to_usbdev(intf);
566
 
567
        /* Some hubs have a subclass of 1, which AFAICT according to the */
568
        /*  specs is not defined, but it works */
569
        if ((desc->desc.bInterfaceSubClass != 0) &&
570
            (desc->desc.bInterfaceSubClass != 1)) {
571
descriptor_error:
572
                dev_err (&intf->dev, "bad descriptor, ignoring hub\n");
573
                return -EIO;
574
        }
575
 
576
        /* Multiple endpoints? What kind of mutant ninja-hub is this? */
577
        if (desc->desc.bNumEndpoints != 1) {
578
                goto descriptor_error;
579
        }
580
 
581
        endpoint = &desc->endpoint[0].desc;
582
 
583
        /* Output endpoint? Curiouser and curiouser.. */
584
        if (!(endpoint->bEndpointAddress & USB_DIR_IN)) {
585
                goto descriptor_error;
586
        }
587
 
588
        /* If it's not an interrupt endpoint, we'd better punt! */
589
        if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK)
590
                        != USB_ENDPOINT_XFER_INT) {
591
                goto descriptor_error;
592
                return -EIO;
593
        }
594
 
595
        /* We found a hub */
596
        dev_info (hubdev (dev), "USB hub found\n");
597
 
598
        hub = kmalloc(sizeof(*hub), GFP_KERNEL);
599
        if (!hub) {
600
                err("couldn't kmalloc hub struct");
601
                return -ENOMEM;
602
        }
603
 
604
        memset(hub, 0, sizeof(*hub));
605
 
606
        INIT_LIST_HEAD(&hub->event_list);
607
        hub->intf = intf;
608
        init_MUTEX(&hub->khubd_sem);
609
 
610
        /* Record the new hub's existence */
611
        spin_lock_irqsave(&hub_event_lock, flags);
612
        INIT_LIST_HEAD(&hub->hub_list);
613
        list_add(&hub->hub_list, &hub_list);
614
        spin_unlock_irqrestore(&hub_event_lock, flags);
615
 
616
        usb_set_intfdata (intf, hub);
617
 
618
        if (hub_configure(hub, endpoint) >= 0)
619
                return 0;
620
 
621
        hub_disconnect (intf);
622
        return -ENODEV;
623
}
624
 
625
static int
626
hub_ioctl(struct usb_interface *intf, unsigned int code, void *user_data)
627
{
628
        struct usb_device *hub = interface_to_usbdev (intf);
629
 
630
        /* assert ifno == 0 (part of hub spec) */
631
        switch (code) {
632
        case USBDEVFS_HUB_PORTINFO: {
633
                struct usbdevfs_hub_portinfo *info = user_data;
634
                unsigned long flags;
635
                int i;
636
 
637
                spin_lock_irqsave(&hub_event_lock, flags);
638
                if (hub->devnum <= 0)
639
                        info->nports = 0;
640
                else {
641
                        info->nports = hub->maxchild;
642
                        for (i = 0; i < info->nports; i++) {
643
                                if (hub->children[i] == NULL)
644
                                        info->port[i] = 0;
645
                                else
646
                                        info->port[i] =
647
                                                hub->children[i]->devnum;
648
                        }
649
                }
650
                spin_unlock_irqrestore(&hub_event_lock, flags);
651
 
652
                return info->nports + 1;
653
                }
654
 
655
        default:
656
                return -ENOSYS;
657
        }
658
}
659
 
660
static int hub_reset(struct usb_hub *hub)
661
{
662
        struct usb_device *dev = interface_to_usbdev(hub->intf);
663
        int i;
664
 
665
        /* Disconnect any attached devices */
666
        for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
667
                if (dev->children[i])
668
                        usb_disconnect(&dev->children[i]);
669
        }
670
 
671
        /* Attempt to reset the hub */
672
        if (hub->urb)
673
                usb_unlink_urb(hub->urb);
674
        else
675
                return -1;
676
 
677
        if (usb_reset_device(dev))
678
                return -1;
679
 
680
        hub->urb->dev = dev;                                                    
681
        if (usb_submit_urb(hub->urb, GFP_KERNEL))
682
                return -1;
683
 
684
        hub_power_on(hub);
685
 
686
        return 0;
687
}
688
 
689
static void hub_start_disconnect(struct usb_device *dev)
690
{
691
        struct usb_device *parent = dev->parent;
692
        int i;
693
 
694
        /* Find the device pointer to disconnect */
695
        if (parent) {
696
                for (i = 0; i < parent->maxchild; i++) {
697
                        if (parent->children[i] == dev) {
698
                                usb_disconnect(&parent->children[i]);
699
                                return;
700
                        }
701
                }
702
        }
703
 
704
        err("cannot disconnect hub %s", dev->devpath);
705
}
706
 
707
static int hub_port_status(struct usb_device *dev, int port,
708
                               u16 *status, u16 *change)
709
{
710
        struct usb_hub *hub = usb_get_intfdata(dev->actconfig->interface[0]);
711
        int ret;
712
 
713
        if (!hub)
714
                return -ENODEV;
715
 
716
        ret = get_port_status(dev, port + 1, &hub->status->port);
717
        if (ret < 0)
718
                dev_err (hubdev (dev),
719
                        "%s failed (err = %d)\n", __FUNCTION__, ret);
720
        else {
721
                *status = le16_to_cpu(hub->status->port.wPortStatus);
722
                *change = le16_to_cpu(hub->status->port.wPortChange);
723
                ret = 0;
724
        }
725
        return ret;
726
}
727
 
728
#define HUB_RESET_TRIES         5
729
#define HUB_PROBE_TRIES         2
730
#define HUB_ROOT_RESET_TIME     50      /* times are in msec */
731
#define HUB_SHORT_RESET_TIME    10
732
#define HUB_LONG_RESET_TIME     200
733
#define HUB_RESET_TIMEOUT       500
734
 
735
/* return: -1 on error, 0 on success, 1 on disconnect.  */
736
static int hub_port_wait_reset(struct usb_device *hub, int port,
737
                                struct usb_device *dev, unsigned int delay)
738
{
739
        int delay_time, ret;
740
        u16 portstatus;
741
        u16 portchange;
742
 
743
        for (delay_time = 0;
744
                        delay_time < HUB_RESET_TIMEOUT;
745
                        delay_time += delay) {
746
                /* wait to give the device a chance to reset */
747
                wait_ms(delay);
748
 
749
                /* read and decode port status */
750
                ret = hub_port_status(hub, port, &portstatus, &portchange);
751
                if (ret < 0) {
752
                        return -1;
753
                }
754
 
755
                /* Device went away? */
756
                if (!(portstatus & USB_PORT_STAT_CONNECTION))
757
                        return 1;
758
 
759
                /* bomb out completely if something weird happened */
760
                if ((portchange & USB_PORT_STAT_C_CONNECTION))
761
                        return -1;
762
 
763
                /* if we`ve finished resetting, then break out of the loop */
764
                if (!(portstatus & USB_PORT_STAT_RESET) &&
765
                    (portstatus & USB_PORT_STAT_ENABLE)) {
766
                        if (portstatus & USB_PORT_STAT_HIGH_SPEED)
767
                                dev->speed = USB_SPEED_HIGH;
768
                        else if (portstatus & USB_PORT_STAT_LOW_SPEED)
769
                                dev->speed = USB_SPEED_LOW;
770
                        else
771
                                dev->speed = USB_SPEED_FULL;
772
                        return 0;
773
                }
774
 
775
                /* switch to the long delay after two short delay failures */
776
                if (delay_time >= 2 * HUB_SHORT_RESET_TIME)
777
                        delay = HUB_LONG_RESET_TIME;
778
 
779
                dev_dbg (hubdev (hub),
780
                        "port %d not reset yet, waiting %dms\n",
781
                        port + 1, delay);
782
        }
783
 
784
        return -1;
785
}
786
 
787
/* return: -1 on error, 0 on success, 1 on disconnect.  */
788
static int hub_port_reset(struct usb_device *hub, int port,
789
                                struct usb_device *dev, unsigned int delay)
790
{
791
        int i, status;
792
 
793
        /* Reset the port */
794
        for (i = 0; i < HUB_RESET_TRIES; i++) {
795
                set_port_feature(hub, port + 1, USB_PORT_FEAT_RESET);
796
 
797
                /* return on disconnect or reset */
798
                status = hub_port_wait_reset(hub, port, dev, delay);
799
                if (status != -1) {
800
                        clear_port_feature(hub,
801
                                port + 1, USB_PORT_FEAT_C_RESET);
802
                        dev->state = status
803
                                        ? USB_STATE_NOTATTACHED
804
                                        : USB_STATE_DEFAULT;
805
                        return status;
806
                }
807
 
808
                dev_dbg (hubdev (hub),
809
                        "port %d not enabled, trying reset again...\n",
810
                        port + 1);
811
                delay = HUB_LONG_RESET_TIME;
812
        }
813
 
814
        dev_err (hubdev (hub),
815
                "Cannot enable port %i.  Maybe the USB cable is bad?\n",
816
                port + 1);
817
 
818
        return -1;
819
}
820
 
821
int hub_port_disable(struct usb_device *hub, int port)
822
{
823
        int ret;
824
 
825
        ret = clear_port_feature(hub, port + 1, USB_PORT_FEAT_ENABLE);
826
        if (ret)
827
                dev_err(hubdev(hub), "cannot disable port %d (err = %d)\n",
828
                        port + 1, ret);
829
 
830
        return ret;
831
}
832
 
833
/* USB 2.0 spec, 7.1.7.3 / fig 7-29:
834
 *
835
 * Between connect detection and reset signaling there must be a delay
836
 * of 100ms at least for debounce and power-settling. The corresponding
837
 * timer shall restart whenever the downstream port detects a disconnect.
838
 *
839
 * Apparently there are some bluetooth and irda-dongles and a number
840
 * of low-speed devices which require longer delays of about 200-400ms.
841
 * Not covered by the spec - but easy to deal with.
842
 *
843
 * This implementation uses 400ms minimum debounce timeout and checks
844
 * every 25ms for transient disconnects to restart the delay.
845
 */
846
 
847
#define HUB_DEBOUNCE_TIMEOUT    400
848
#define HUB_DEBOUNCE_STEP        25
849
#define HUB_DEBOUNCE_STABLE       4
850
 
851
/* return: -1 on error, 0 on success, 1 on disconnect.  */
852
static int hub_port_debounce(struct usb_device *hub, int port)
853
{
854
        int ret;
855
        int delay_time, stable_count;
856
        u16 portchange, portstatus;
857
        unsigned connection;
858
 
859
        connection = 0;
860
        stable_count = 0;
861
        for (delay_time = 0; delay_time < HUB_DEBOUNCE_TIMEOUT; delay_time += HUB_DEBOUNCE_STEP) {
862
                wait_ms(HUB_DEBOUNCE_STEP);
863
 
864
                ret = hub_port_status(hub, port, &portstatus, &portchange);
865
                if (ret < 0)
866
                        return -1;
867
 
868
                if ((portstatus & USB_PORT_STAT_CONNECTION) == connection) {
869
                        if (connection) {
870
                                if (++stable_count == HUB_DEBOUNCE_STABLE)
871
                                        break;
872
                        }
873
                } else {
874
                        stable_count = 0;
875
                }
876
                connection = portstatus & USB_PORT_STAT_CONNECTION;
877
 
878
                if ((portchange & USB_PORT_STAT_C_CONNECTION)) {
879
                        clear_port_feature(hub, port+1, USB_PORT_FEAT_C_CONNECTION);
880
                }
881
        }
882
 
883
        dev_dbg (hubdev (hub),
884
                "debounce: port %d: delay %dms stable %d status 0x%x\n",
885
                port + 1, delay_time, stable_count, portstatus);
886
 
887
        return ((portstatus&USB_PORT_STAT_CONNECTION)) ? 0 : 1;
888
}
889
 
890
static void hub_port_connect_change(struct usb_hub *hubstate, int port,
891
                                        u16 portstatus, u16 portchange)
892
{
893
        struct usb_device *hub = interface_to_usbdev(hubstate->intf);
894
        struct usb_device *dev;
895
        unsigned int delay = HUB_SHORT_RESET_TIME;
896
        int i;
897
 
898
        dev_dbg (&hubstate->intf->dev,
899
                "port %d, status %x, change %x, %s\n",
900
                port + 1, portstatus, portchange, portspeed (portstatus));
901
 
902
        /* Clear the connection change status */
903
        clear_port_feature(hub, port + 1, USB_PORT_FEAT_C_CONNECTION);
904
 
905
        /* Disconnect any existing devices under this port */
906
        if (hub->children[port])
907
                usb_disconnect(&hub->children[port]);
908
 
909
        /* Return now if nothing is connected */
910
        if (!(portstatus & USB_PORT_STAT_CONNECTION)) {
911
                if (portstatus & USB_PORT_STAT_ENABLE)
912
                        hub_port_disable(hub, port);
913
 
914
                return;
915
        }
916
 
917
        if (hub_port_debounce(hub, port)) {
918
                dev_err (&hubstate->intf->dev,
919
                        "connect-debounce failed, port %d disabled\n",
920
                        port+1);
921
                hub_port_disable(hub, port);
922
                return;
923
        }
924
 
925
        /* root hub ports have a slightly longer reset period
926
         * (from USB 2.0 spec, section 7.1.7.5)
927
         */
928
        if (!hub->parent)
929
                delay = HUB_ROOT_RESET_TIME;
930
 
931
        /* Some low speed devices have problems with the quick delay, so */
932
        /*  be a bit pessimistic with those devices. RHbug #23670 */
933
        if (portstatus & USB_PORT_STAT_LOW_SPEED)
934
                delay = HUB_LONG_RESET_TIME;
935
 
936
        down(&usb_address0_sem);
937
 
938
        for (i = 0; i < HUB_PROBE_TRIES; i++) {
939
                struct usb_device *pdev;
940
                int     len;
941
 
942
                /* Allocate a new device struct */
943
                dev = usb_alloc_dev(hub, hub->bus);
944
                if (!dev) {
945
                        dev_err (&hubstate->intf->dev,
946
                                "couldn't allocate usb_device\n");
947
                        break;
948
                }
949
 
950
                dev->state = USB_STATE_POWERED;
951
 
952
                /* Reset the device, and detect its speed */
953
                if (hub_port_reset(hub, port, dev, delay)) {
954
                        usb_put_dev(dev);
955
                        break;
956
                }
957
 
958
                /* Find a new address for it */
959
                usb_choose_address(dev);
960
 
961
                /* Set up TT records, if needed  */
962
                if (hub->tt) {
963
                        dev->tt = hub->tt;
964
                        dev->ttport = hub->ttport;
965
                } else if (dev->speed != USB_SPEED_HIGH
966
                                && hub->speed == USB_SPEED_HIGH) {
967
                        dev->tt = &hubstate->tt;
968
                        dev->ttport = port + 1;
969
                }
970
 
971
                /* Save readable and stable topology id, distinguishing
972
                 * devices by location for diagnostics, tools, etc.  The
973
                 * string is a path along hub ports, from the root.  Each
974
                 * device's id will be stable until USB is re-cabled, and
975
                 * hubs are often labeled with these port numbers.
976
                 *
977
                 * Initial size: ".NN" times five hubs + NUL = 16 bytes max
978
                 * (quite rare, since most hubs have 4-6 ports).
979
                 */
980
                pdev = dev->parent;
981
                if (pdev->devpath [0] != '0')   /* parent not root? */
982
                        len = snprintf26(dev->devpath, sizeof dev->devpath,
983
                                "%s.%d", pdev->devpath, port + 1);
984
                /* root == "0", root port 2 == "2", port 3 that hub "2.3" */
985
                else
986
                        len = snprintf26(dev->devpath, sizeof dev->devpath,
987
                                "%d", port + 1);
988
                if (len == sizeof dev->devpath)
989
                        dev_err (&hubstate->intf->dev,
990
                                "devpath size! usb/%03d/%03d path %s\n",
991
                                dev->bus->busnum, dev->devnum, dev->devpath);
992
                dev_info (&hubstate->intf->dev,
993
                        "new USB device on port %d, assigned address %d\n",
994
                        port + 1, dev->devnum);
995
 
996
                /* put the device in the global device tree. the hub port
997
                 * is the "bus_id"; hubs show in hierarchy like bridges
998
                 */
999
                dev->dev.parent = dev->parent->dev.parent->parent;
1000
 
1001
                /* Run it through the hoops (find a driver, etc) */
1002
                if (!usb_new_device(dev, &hub->dev)) {
1003
                        hub->children[port] = dev;
1004
                        goto done;
1005
                }
1006
 
1007
                /* Free the configuration if there was an error */
1008
                usb_put_dev(dev);
1009
 
1010
                /* Switch to a long reset time */
1011
                delay = HUB_LONG_RESET_TIME;
1012
        }
1013
 
1014
        hub_port_disable(hub, port);
1015
done:
1016
        up(&usb_address0_sem);
1017
}
1018
 
1019
static void hub_events(void)
1020
{
1021
        unsigned long flags;
1022
        struct list_head *tmp;
1023
        struct usb_device *dev;
1024
        struct usb_hub *hub;
1025
        u16 hubstatus;
1026
        u16 hubchange;
1027
        u16 portstatus;
1028
        u16 portchange;
1029
        int i, ret;
1030
 
1031
        /*
1032
         *  We restart the list every time to avoid a deadlock with
1033
         * deleting hubs downstream from this one. This should be
1034
         * safe since we delete the hub from the event list.
1035
         * Not the most efficient, but avoids deadlocks.
1036
         */
1037
 
1038
       while (1) {
1039
                spin_lock_irqsave(&hub_event_lock, flags);
1040
 
1041
                if (list_empty(&hub_event_list))
1042
                        break;
1043
 
1044
                /* Grab the next entry from the beginning of the list */
1045
                tmp = hub_event_list.next;
1046
 
1047
                hub = list_entry(tmp, struct usb_hub, event_list);
1048
                dev = interface_to_usbdev(hub->intf);
1049
 
1050
                list_del_init(tmp);
1051
 
1052
//**                if (unlikely(down_trylock(&hub->khubd_sem)))
1053
//**                        BUG();  /* never blocks, we were on list */
1054
 
1055
                spin_unlock_irqrestore(&hub_event_lock, flags);
1056
 
1057
                if (hub->error) {
1058
                        dev_dbg (&hub->intf->dev, "resetting for error %d\n",
1059
                                hub->error);
1060
 
1061
                        if (hub_reset(hub)) {
1062
                                dev_dbg (&hub->intf->dev,
1063
                                        "can't reset; disconnecting\n");
1064
                                up(&hub->khubd_sem);
1065
                                hub_start_disconnect(dev);
1066
                                continue;
1067
                        }
1068
 
1069
                        hub->nerrors = 0;
1070
                        hub->error = 0;
1071
                }
1072
 
1073
                for (i = 0; i < hub->descriptor->bNbrPorts; i++) {
1074
                        ret = hub_port_status(dev, i, &portstatus, &portchange);
1075
                        if (ret < 0) {
1076
                                continue;
1077
                        }
1078
 
1079
                        if (portchange & USB_PORT_STAT_C_CONNECTION) {
1080
                                hub_port_connect_change(hub, i, portstatus, portchange);
1081
                        } else if (portchange & USB_PORT_STAT_C_ENABLE) {
1082
                                dev_dbg (hubdev (dev),
1083
                                        "port %d enable change, status %x\n",
1084
                                        i + 1, portstatus);
1085
                                clear_port_feature(dev,
1086
                                        i + 1, USB_PORT_FEAT_C_ENABLE);
1087
 
1088
                                /*
1089
                                 * EM interference sometimes causes badly
1090
                                 * shielded USB devices to be shutdown by
1091
                                 * the hub, this hack enables them again.
1092
                                 * Works at least with mouse driver.
1093
                                 */
1094
                                if (!(portstatus & USB_PORT_STAT_ENABLE)
1095
                                    && (portstatus & USB_PORT_STAT_CONNECTION)
1096
                                    && (dev->children[i])) {
1097
                                        dev_err (&hub->intf->dev,
1098
                                            "port %i "
1099
                                            "disabled by hub (EMI?), "
1100
                                            "re-enabling...",
1101
                                                i + 1);
1102
                                        hub_port_connect_change(hub,
1103
                                                i, portstatus, portchange);
1104
                                }
1105
                        }
1106
 
1107
                        if (portchange & USB_PORT_STAT_C_SUSPEND) {
1108
                                dev_dbg (&hub->intf->dev,
1109
                                        "suspend change on port %d\n",
1110
                                        i + 1);
1111
                                clear_port_feature(dev,
1112
                                        i + 1,  USB_PORT_FEAT_C_SUSPEND);
1113
                        }
1114
 
1115
                        if (portchange & USB_PORT_STAT_C_OVERCURRENT) {
1116
                                dev_err (&hub->intf->dev,
1117
                                        "over-current change on port %d\n",
1118
                                        i + 1);
1119
                                clear_port_feature(dev,
1120
                                        i + 1, USB_PORT_FEAT_C_OVER_CURRENT);
1121
                                hub_power_on(hub);
1122
                        }
1123
 
1124
                        if (portchange & USB_PORT_STAT_C_RESET) {
1125
                                dev_dbg (&hub->intf->dev,
1126
                                        "reset change on port %d\n",
1127
                                        i + 1);
1128
                                clear_port_feature(dev,
1129
                                        i + 1, USB_PORT_FEAT_C_RESET);
1130
                        }
1131
                } /* end for i */
1132
 
1133
                /* deal with hub status changes */
1134
                if (hub_hub_status(hub, &hubstatus, &hubchange) < 0)
1135
                        dev_err (&hub->intf->dev, "get_hub_status failed\n");
1136
                else {
1137
                        if (hubchange & HUB_CHANGE_LOCAL_POWER) {
1138
                                dev_dbg (&hub->intf->dev, "power change\n");
1139
                                clear_hub_feature(dev, C_HUB_LOCAL_POWER);
1140
                        }
1141
                        if (hubchange & HUB_CHANGE_OVERCURRENT) {
1142
                                dev_dbg (&hub->intf->dev, "overcurrent change\n");
1143
                                wait_ms(500);   /* Cool down */
1144
                                clear_hub_feature(dev, C_HUB_OVER_CURRENT);
1145
                                hub_power_on(hub);
1146
                        }
1147
                }
1148
                up(&hub->khubd_sem);
1149
        } /* end while (1) */
1150
 
1151
        spin_unlock_irqrestore(&hub_event_lock, flags);
1152
}
1153
 
1154
static int hub_thread(void *__hub)
1155
{
1156
        /*
1157
         * This thread doesn't need any user-level access,
1158
         * so get rid of all our resources
1159
         */
1160
 
1161
        daemonize("khubd");
1162
        allow_signal(SIGKILL);
1163
 
1164
        /* Send me a signal to get me die (for debugging) */
1165
        do {
1166
                hub_events();
1167
                wait_event_interruptible(khubd_wait, !list_empty(&hub_event_list));
1168
//                if (current->flags & PF_FREEZE)
1169
//                        refrigerator(PF_IOTHREAD);
1170
        } while (!signal_pending(current));
1171
 
1172
        dbg("hub_thread exiting");
1173
        complete_and_exit(&khubd_exited, 0);
1174
        return 0;
1175
}
1176
 
1177
static struct usb_device_id hub_id_table [] = {
1178
    { .match_flags = USB_DEVICE_ID_MATCH_DEV_CLASS,
1179
      .bDeviceClass = USB_CLASS_HUB},
1180
    { .match_flags = USB_DEVICE_ID_MATCH_INT_CLASS,
1181
      .bInterfaceClass = USB_CLASS_HUB},
1182
    { }                                         /* Terminating entry */
1183
};
1184
 
1185
MODULE_DEVICE_TABLE (usb, hub_id_table);
1186
 
1187
static struct usb_driver hub_driver = {
1188
        .owner =        THIS_MODULE,
1189
        .name =         "hub",
1190
        .probe =        hub_probe,
1191
        .disconnect =   hub_disconnect,
1192
        .ioctl =        hub_ioctl,
1193
        .id_table =     hub_id_table,
1194
};
1195
 
1196
/*
1197
 * This should be a separate module.
1198
 */
1199
int usb_hub_init(void)
1200
{
1201
        pid_t pid;
1202
 
1203
        if (usb_register(&hub_driver) < 0) {
1204
                err("Unable to register USB hub driver");
1205
                return -1;
1206
        }
1207
 
1208
        pid = kernel_thread(hub_thread, NULL, CLONE_KERNEL);
1209
        if (pid >= 0) {
1210
                khubd_pid = pid;
1211
 
1212
                return 0;
1213
        }
1214
 
1215
        /* Fall through if kernel_thread failed */
1216
        usb_deregister(&hub_driver);
1217
        err("failed to start hub_thread");
1218
 
1219
        return -1;
1220
}
1221
 
1222
void usb_hub_cleanup(void)
1223
{
1224
        int ret;
1225
 
1226
        /* Kill the thread */
1227
        ret = kill_proc(khubd_pid, SIGKILL, 1);
1228
 
1229
        wait_for_completion(&khubd_exited);
1230
 
1231
        /*
1232
         * Hub resources are freed for us by usb_deregister. It calls
1233
         * usb_driver_purge on every device which in turn calls that
1234
         * devices disconnect function if it is using this driver.
1235
         * The hub_disconnect function takes care of releasing the
1236
         * individual hub resources. -greg
1237
         */
1238
        usb_deregister(&hub_driver);
1239
} /* usb_hub_cleanup() */
1240
 
1241
/*
1242
 * WARNING - If a driver calls usb_reset_device, you should simulate a
1243
 * disconnect() and probe() for other interfaces you doesn't claim. This
1244
 * is left up to the driver writer right now. This insures other drivers
1245
 * have a chance to re-setup their interface.
1246
 *
1247
 * Take a look at proc_resetdevice in devio.c for some sample code to
1248
 * do this.
1249
 * Use this only from within your probe function, otherwise use
1250
 * usb_reset_device() below, which ensure proper locking
1251
 */
1252
int usb_physical_reset_device(struct usb_device *dev)
1253
{
1254
        struct usb_device *parent = dev->parent;
1255
        struct usb_device_descriptor *descriptor;
1256
        int i, ret, port = -1;
1257
 
1258
        if (!parent) {
1259
                err("attempting to reset root hub!");
1260
                return -EINVAL;
1261
        }
1262
 
1263
        for (i = 0; i < parent->maxchild; i++)
1264
                if (parent->children[i] == dev) {
1265
                        port = i;
1266
                        break;
1267
                }
1268
 
1269
        if (port < 0)
1270
                return -ENOENT;
1271
 
1272
        descriptor = kmalloc(sizeof *descriptor, GFP_NOIO);
1273
        if (!descriptor) {
1274
                return -ENOMEM;
1275
        }
1276
 
1277
        down(&usb_address0_sem);
1278
 
1279
        /* Send a reset to the device */
1280
        if (hub_port_reset(parent, port, dev, HUB_SHORT_RESET_TIME)) {
1281
                hub_port_disable(parent, port);
1282
                up(&usb_address0_sem);
1283
                kfree(descriptor);
1284
                return(-ENODEV);
1285
        }
1286
 
1287
        /* Reprogram the Address */
1288
        ret = usb_set_address(dev);
1289
        if (ret < 0) {
1290
                err("USB device not accepting new address (error=%d)", ret);
1291
                hub_port_disable(parent, port);
1292
                up(&usb_address0_sem);
1293
                kfree(descriptor);
1294
                return ret;
1295
        }
1296
 
1297
        /* Let the SET_ADDRESS settle */
1298
        wait_ms(10);
1299
 
1300
        up(&usb_address0_sem);
1301
 
1302
        /*
1303
         * Now we fetch the configuration descriptors for the device and
1304
         * see if anything has changed. If it has, we dump the current
1305
         * parsed descriptors and reparse from scratch. Then we leave
1306
         * the device alone for the caller to finish setting up.
1307
         *
1308
         * If nothing changed, we reprogram the configuration and then
1309
         * the alternate settings.
1310
         */
1311
 
1312
        ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, descriptor,
1313
                        sizeof(*descriptor));
1314
        if (ret < 0) {
1315
                kfree(descriptor);
1316
                return ret;
1317
        }
1318
 
1319
        le16_to_cpus(&descriptor->bcdUSB);
1320
        le16_to_cpus(&descriptor->idVendor);
1321
        le16_to_cpus(&descriptor->idProduct);
1322
        le16_to_cpus(&descriptor->bcdDevice);
1323
 
1324
        if (memcmp(&dev->descriptor, descriptor, sizeof(*descriptor))) {
1325
                kfree(descriptor);
1326
                usb_destroy_configuration(dev);
1327
 
1328
                ret = usb_get_device_descriptor(dev);
1329
                if (ret < sizeof(dev->descriptor)) {
1330
                        if (ret < 0)
1331
                                err("unable to get device %s descriptor "
1332
                                        "(error=%d)", dev->devpath, ret);
1333
                        else
1334
                                err("USB device %s descriptor short read "
1335
                                        "(expected %Zi, got %i)",
1336
                                        dev->devpath,
1337
                                        sizeof(dev->descriptor), ret);
1338
 
1339
                        clear_bit(dev->devnum, dev->bus->devmap.devicemap);
1340
                        dev->devnum = -1;
1341
                        return -EIO;
1342
                }
1343
 
1344
                ret = usb_get_configuration(dev);
1345
                if (ret < 0) {
1346
                        err("unable to get configuration (error=%d)", ret);
1347
                        usb_destroy_configuration(dev);
1348
                        clear_bit(dev->devnum, dev->bus->devmap.devicemap);
1349
                        dev->devnum = -1;
1350
                        return 1;
1351
                }
1352
 
1353
                usb_set_configuration(dev, dev->config[0].desc.bConfigurationValue);
1354
                return 1;
1355
        }
1356
 
1357
        kfree(descriptor);
1358
 
1359
        ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1360
                        USB_REQ_SET_CONFIGURATION, 0,
1361
                        dev->actconfig->desc.bConfigurationValue, 0,
1362
                        NULL, 0, HZ * USB_CTRL_SET_TIMEOUT);
1363
        if (ret < 0) {
1364
                err("failed to set dev %s active configuration (error=%d)",
1365
                        dev->devpath, ret);
1366
                return ret;
1367
        }
1368
        dev->state = USB_STATE_CONFIGURED;
1369
 
1370
        for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
1371
                struct usb_interface *intf = dev->actconfig->interface[i];
1372
                struct usb_interface_descriptor *as;
1373
 
1374
                as = &intf->altsetting[intf->act_altsetting].desc;
1375
                ret = usb_set_interface(dev, as->bInterfaceNumber,
1376
                        as->bAlternateSetting);
1377
                if (ret < 0) {
1378
                        err("failed to set active alternate setting "
1379
                                "for dev %s interface %d (error=%d)",
1380
                                dev->devpath, i, ret);
1381
                        return ret;
1382
                }
1383
        }
1384
 
1385
        return 0;
1386
}
1387
 
1388
int usb_reset_device(struct usb_device *udev)
1389
{
1390
        struct device *gdev = &udev->dev;
1391
        int r;
1392
 
1393
//      down_read(&gdev->bus->subsys.rwsem);
1394
        r = usb_physical_reset_device(udev);
1395
//      up_read(&gdev->bus->subsys.rwsem);
1396
 
1397
        return r;
1398
}