Subversion Repositories shark

Rev

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