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
 * message.c - synchronous message handling
3
 */
4
 
5
#include <linuxcomp.h>
6
 
7
#include <linux/config.h>
8
 
9
#ifdef CONFIG_USB_DEBUG
10
        #define DEBUG
11
#else
12
        #undef DEBUG
13
#endif
14
 
15
#include <linux/pci.h>  /* for scatterlist macros */
16
#include <linux/usb.h>
17
#include <linux/module.h>
18
#include <linux/slab.h>
19
#include <linux/init.h>
20
#include <linux/mm.h>
21
#include <linux/timer.h>
22
#include <asm/byteorder.h>
23
 
24
#include "hcd.h"        /* for usbcore internals */
25
#include "usb.h"
26
 
27
static void usb_api_blocking_completion(struct urb *urb, struct pt_regs *regs)
28
{
29
//printk(KERN_INFO "api\n");
30
        complete((struct completion *)urb->context);
31
}
32
 
33
 
34
static void timeout_kill(unsigned long data)
35
{
36
        struct urb      *urb = (struct urb *) data;
37
 
38
        dev_warn(&urb->dev->dev, "%s timeout on ep%d%s\n",
39
                usb_pipecontrol(urb->pipe) ? "control" : "bulk",
40
                usb_pipeendpoint(urb->pipe),
41
                usb_pipein(urb->pipe) ? "in" : "out");
42
        usb_unlink_urb(urb);
43
}                
44
 
45
// Starts urb and waits for completion or timeout
46
// note that this call is NOT interruptible, while
47
// many device driver i/o requests should be interruptible
48
static int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length)
49
{
50
        struct completion       done;
51
        struct timer_list       timer;
52
        int                     status;
53
        struct pt_regs *regs;
54
 
55
 
56
        init_completion(&done);        
57
        urb->context = &done;
58
        urb->transfer_flags |= URB_ASYNC_UNLINK;
59
        urb->actual_length = 0;
60
 
61
        status = usb_submit_urb(urb, GFP_NOIO);
62
 
63
        if (status == 0) {
64
                if (timeout > 0) {
65
                        init_timer(&timer);
66
                        timer.expires = jiffies26 + timeout;
67
                        timer.data = (unsigned long)urb;
68
                        timer.function = timeout_kill;
69
                        /* grr.  timeout _should_ include submit delays. */
70
                        add_timer(&timer);
71
                }
72
                wait_for_completion(&done);
73
                status = urb->status;
74
                /* note:  HCDs return ETIMEDOUT for other reasons too */
75
                if (status == -ECONNRESET)
76
                        status = -ETIMEDOUT;
77
                if (timeout > 0)
78
                        del_timer_sync(&timer);
79
        }
80
 
81
        if (actual_length)
82
                *actual_length = urb->actual_length;
83
        usb_free_urb(urb);
84
        return status;
85
}
86
 
87
/*-------------------------------------------------------------------*/
88
// returns status (negative) or length (positive)
89
int usb_internal_control_msg(struct usb_device *usb_dev, unsigned int pipe,
90
                            struct usb_ctrlrequest *cmd,  void *data, int len, int timeout)
91
{
92
        struct urb *urb;
93
        int retv;
94
        int length;
95
 
96
        urb = usb_alloc_urb(0, GFP_NOIO);
97
        if (!urb)
98
                return -ENOMEM;
99
 
100
        usb_fill_control_urb(urb, usb_dev, pipe, (unsigned char*)cmd, data, len,
101
                   usb_api_blocking_completion, 0);
102
        retv = usb_start_wait_urb(urb, timeout, &length);
103
        if (retv < 0)
104
                return retv;
105
        else
106
                return length;
107
}
108
 
109
/**
110
 *      usb_control_msg - Builds a control urb, sends it off and waits for completion
111
 *      @dev: pointer to the usb device to send the message to
112
 *      @pipe: endpoint "pipe" to send the message to
113
 *      @request: USB message request value
114
 *      @requesttype: USB message request type value
115
 *      @value: USB message value
116
 *      @index: USB message index value
117
 *      @data: pointer to the data to send
118
 *      @size: length in bytes of the data to send
119
 *      @timeout: time in jiffies26 to wait for the message to complete before
120
 *              timing out (if 0 the wait is forever)
121
 *      Context: !in_interrupt ()
122
 *
123
 *      This function sends a simple control message to a specified endpoint
124
 *      and waits for the message to complete, or timeout.
125
 *     
126
 *      If successful, it returns the number of bytes transferred, otherwise a negative error number.
127
 *
128
 *      Don't use this function from within an interrupt context, like a
129
 *      bottom half handler.  If you need an asynchronous message, or need to send
130
 *      a message from within interrupt context, use usb_submit_urb()
131
 *      If a thread in your driver uses this call, make sure your disconnect()
132
 *      method can wait for it to complete.  Since you don't have a handle on
133
 *      the URB used, you can't cancel the request.
134
 */
135
int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,
136
                         __u16 value, __u16 index, void *data, __u16 size, int timeout)
137
{
138
        struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_NOIO);
139
        int ret;
140
 
141
        if (!dr)
142
                return -ENOMEM;
143
 
144
        dr->bRequestType= requesttype;
145
        dr->bRequest = request;
146
        dr->wValue = cpu_to_le16p(&value);
147
        dr->wIndex = cpu_to_le16p(&index);
148
        dr->wLength = cpu_to_le16p(&size);
149
 
150
        //dbg("usb_control_msg");       
151
 
152
        ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);
153
 
154
        kfree(dr);
155
 
156
        return ret;
157
}
158
 
159
 
160
/**
161
 *      usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion
162
 *      @usb_dev: pointer to the usb device to send the message to
163
 *      @pipe: endpoint "pipe" to send the message to
164
 *      @data: pointer to the data to send
165
 *      @len: length in bytes of the data to send
166
 *      @actual_length: pointer to a location to put the actual length transferred in bytes
167
 *      @timeout: time in jiffies26 to wait for the message to complete before
168
 *              timing out (if 0 the wait is forever)
169
 *      Context: !in_interrupt ()
170
 *
171
 *      This function sends a simple bulk message to a specified endpoint
172
 *      and waits for the message to complete, or timeout.
173
 *     
174
 *      If successful, it returns 0, otherwise a negative error number.
175
 *      The number of actual bytes transferred will be stored in the
176
 *      actual_length paramater.
177
 *
178
 *      Don't use this function from within an interrupt context, like a
179
 *      bottom half handler.  If you need an asynchronous message, or need to
180
 *      send a message from within interrupt context, use usb_submit_urb()
181
 *      If a thread in your driver uses this call, make sure your disconnect()
182
 *      method can wait for it to complete.  Since you don't have a handle on
183
 *      the URB used, you can't cancel the request.
184
 */
185
int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe,
186
                        void *data, int len, int *actual_length, int timeout)
187
{
188
        struct urb *urb;
189
 
190
        if (len < 0)
191
                return -EINVAL;
192
 
193
        urb=usb_alloc_urb(0, GFP_KERNEL);
194
        if (!urb)
195
                return -ENOMEM;
196
 
197
        usb_fill_bulk_urb(urb, usb_dev, pipe, data, len,
198
                    usb_api_blocking_completion, 0);
199
 
200
        return usb_start_wait_urb(urb,timeout,actual_length);
201
}
202
 
203
/*-------------------------------------------------------------------*/
204
 
205
static void sg_clean (struct usb_sg_request *io)
206
{
207
        if (io->urbs) {
208
                while (io->entries--)
209
                        usb_free_urb (io->urbs [io->entries]);
210
                kfree (io->urbs);
211
                io->urbs = 0;
212
        }
213
        if (io->dev->dev.dma_mask != 0)
214
                usb_buffer_unmap_sg (io->dev, io->pipe, io->sg, io->nents);
215
        io->dev = 0;
216
}
217
 
218
static void sg_complete (struct urb *urb, struct pt_regs *regs)
219
{
220
        struct usb_sg_request   *io = (struct usb_sg_request *) urb->context;
221
        unsigned long           flags;
222
 
223
        spin_lock_irqsave (&io->lock, flags);
224
 
225
        /* In 2.5 we require hcds' endpoint queues not to progress after fault
226
         * reports, until the completion callback (this!) returns.  That lets
227
         * device driver code (like this routine) unlink queued urbs first,
228
         * if it needs to, since the HC won't work on them at all.  So it's
229
         * not possible for page N+1 to overwrite page N, and so on.
230
         *
231
         * That's only for "hard" faults; "soft" faults (unlinks) sometimes
232
         * complete before the HCD can get requests away from hardware,
233
         * though never during cleanup after a hard fault.
234
         */
235
        if (io->status
236
                        && (io->status != -ECONNRESET
237
                                || urb->status != -ECONNRESET)
238
                        && urb->actual_length) {
239
                dev_err (io->dev->bus->controller,
240
                        "dev %s ep%d%s scatterlist error %d/%d\n",
241
                        io->dev->devpath,
242
                        usb_pipeendpoint (urb->pipe),
243
                        usb_pipein (urb->pipe) ? "in" : "out",
244
                        urb->status, io->status);
245
                // BUG ();
246
        }
247
 
248
        if (urb->status && urb->status != -ECONNRESET) {
249
                int             i, found, status;
250
 
251
                io->status = urb->status;
252
 
253
                /* the previous urbs, and this one, completed already.
254
                 * unlink pending urbs so they won't rx/tx bad data.
255
                 */
256
                for (i = 0, found = 0; i < io->entries; i++) {
257
                        if (!io->urbs [i])
258
                                continue;
259
                        if (found) {
260
                                status = usb_unlink_urb (io->urbs [i]);
261
                                if (status != -EINPROGRESS && status != -EBUSY)
262
                                        dev_err (&io->dev->dev,
263
                                                "%s, unlink --> %d\n",
264
                                                __FUNCTION__, status);
265
                        } else if (urb == io->urbs [i])
266
                                found = 1;
267
                }
268
        }
269
        urb->dev = 0;
270
 
271
        /* on the last completion, signal usb_sg_wait() */
272
        io->bytes += urb->actual_length;
273
        io->count--;
274
        if (!io->count)
275
                complete (&io->complete);
276
 
277
        spin_unlock_irqrestore (&io->lock, flags);
278
}
279
 
280
 
281
/**
282
 * usb_sg_init - initializes scatterlist-based bulk/interrupt I/O request
283
 * @io: request block being initialized.  until usb_sg_wait() returns,
284
 *      treat this as a pointer to an opaque block of memory,
285
 * @dev: the usb device that will send or receive the data
286
 * @pipe: endpoint "pipe" used to transfer the data
287
 * @period: polling rate for interrupt endpoints, in frames or
288
 *      (for high speed endpoints) microframes; ignored for bulk
289
 * @sg: scatterlist entries
290
 * @nents: how many entries in the scatterlist
291
 * @length: how many bytes to send from the scatterlist, or zero to
292
 *      send every byte identified in the list.
293
 * @mem_flags: SLAB_* flags affecting memory allocations in this call
294
 *
295
 * Returns zero for success, else a negative errno value.  This initializes a
296
 * scatter/gather request, allocating resources such as I/O mappings and urb
297
 * memory (except maybe memory used by USB controller drivers).
298
 *
299
 * The request must be issued using usb_sg_wait(), which waits for the I/O to
300
 * complete (or to be canceled) and then cleans up all resources allocated by
301
 * usb_sg_init().
302
 *
303
 * The request may be canceled with usb_sg_cancel(), either before or after
304
 * usb_sg_wait() is called.
305
 */
306
int usb_sg_init (
307
        struct usb_sg_request   *io,
308
        struct usb_device       *dev,
309
        unsigned                pipe,
310
        unsigned                period,
311
        struct scatterlist      *sg,
312
        int                     nents,
313
        size_t                  length,
314
        int                     mem_flags
315
)
316
{
317
        int                     i;
318
        int                     urb_flags;
319
        int                     dma;
320
 
321
        {
322
                int i;
323
                for (i=0; i<20; i++)
324
                {
325
                        wait_ms26(300);
326
                        printk(KERN_INFO "usb_sg_init!!!!!!!\n");
327
                }
328
        }
329
 
330
        if (!io || !dev || !sg
331
                        || usb_pipecontrol (pipe)
332
                        || usb_pipeisoc (pipe)
333
                        || nents <= 0)
334
                return -EINVAL;
335
 
336
        spin_lock_init (&io->lock);
337
        io->dev = dev;
338
        io->pipe = pipe;
339
        io->sg = sg;
340
        io->nents = nents;
341
 
342
        /* not all host controllers use DMA (like the mainstream pci ones);
343
         * they can use PIO (sl811) or be software over another transport.
344
         */
345
        dma = (dev->dev.dma_mask != 0);
346
        if (dma)
347
                io->entries = usb_buffer_map_sg (dev, pipe, sg, nents);
348
        else
349
                io->entries = nents;
350
 
351
        /* initialize all the urbs we'll use */
352
        if (io->entries <= 0)
353
                return io->entries;
354
 
355
        io->count = 0;
356
        io->urbs = kmalloc (io->entries * sizeof *io->urbs, mem_flags);
357
        if (!io->urbs)
358
                goto nomem;
359
 
360
        urb_flags = URB_ASYNC_UNLINK | URB_NO_TRANSFER_DMA_MAP
361
                        | URB_NO_INTERRUPT;
362
        if (usb_pipein (pipe))
363
                urb_flags |= URB_SHORT_NOT_OK;
364
 
365
        for (i = 0; i < io->entries; i++, io->count = i) {
366
                unsigned                len;
367
 
368
                io->urbs [i] = usb_alloc_urb (0, mem_flags);
369
                if (!io->urbs [i]) {
370
                        io->entries = i;
371
                        goto nomem;
372
                }
373
 
374
                io->urbs [i]->dev = 0;
375
                io->urbs [i]->pipe = pipe;
376
                io->urbs [i]->interval = period;
377
                io->urbs [i]->transfer_flags = urb_flags;
378
 
379
                io->urbs [i]->complete = sg_complete;
380
                io->urbs [i]->context = io;
381
                io->urbs [i]->status = -EINPROGRESS;
382
                io->urbs [i]->actual_length = 0;
383
 
384
                if (dma) {
385
                        /* hc may use _only_ transfer_dma */
386
                        io->urbs [i]->transfer_dma = sg_dma_address (sg + i);
387
                        len = sg_dma_len (sg + i);
388
                } else {
389
                        /* hc may use _only_ transfer_buffer */
390
                        io->urbs [i]->transfer_buffer =
391
                                page_address (sg [i].page) + sg [i].offset;
392
                        len = sg [i].length;
393
                }
394
 
395
                if (length) {
396
                        len = min_t (unsigned, len, length);
397
                        length -= len;
398
                        if (length == 0)
399
                                io->entries = i + 1;
400
                }
401
                io->urbs [i]->transfer_buffer_length = len;
402
        }
403
        io->urbs [--i]->transfer_flags &= ~URB_NO_INTERRUPT;
404
 
405
        /* transaction state */
406
        io->status = 0;
407
        io->bytes = 0;
408
        init_completion (&io->complete);
409
        return 0;
410
 
411
nomem:
412
        sg_clean (io);
413
        return -ENOMEM;
414
}
415
 
416
 
417
/**
418
 * usb_sg_wait - synchronously execute scatter/gather request
419
 * @io: request block handle, as initialized with usb_sg_init().
420
 *      some fields become accessible when this call returns.
421
 * Context: !in_interrupt ()
422
 *
423
 * This function blocks until the specified I/O operation completes.  It
424
 * leverages the grouping of the related I/O requests to get good transfer
425
 * rates, by queueing the requests.  At higher speeds, such queuing can
426
 * significantly improve USB throughput.
427
 *
428
 * There are three kinds of completion for this function.
429
 * (1) success, where io->status is zero.  The number of io->bytes
430
 *     transferred is as requested.
431
 * (2) error, where io->status is a negative errno value.  The number
432
 *     of io->bytes transferred before the error is usually less
433
 *     than requested, and can be nonzero.
434
 * (3) cancelation, a type of error with status -ECONNRESET that
435
 *     is initiated by usb_sg_cancel().
436
 *
437
 * When this function returns, all memory allocated through usb_sg_init() or
438
 * this call will have been freed.  The request block parameter may still be
439
 * passed to usb_sg_cancel(), or it may be freed.  It could also be
440
 * reinitialized and then reused.
441
 *
442
 * Data Transfer Rates:
443
 *
444
 * Bulk transfers are valid for full or high speed endpoints.
445
 * The best full speed data rate is 19 packets of 64 bytes each
446
 * per frame, or 1216 bytes per millisecond.
447
 * The best high speed data rate is 13 packets of 512 bytes each
448
 * per microframe, or 52 KBytes per millisecond.
449
 *
450
 * The reason to use interrupt transfers through this API would most likely
451
 * be to reserve high speed bandwidth, where up to 24 KBytes per millisecond
452
 * could be transferred.  That capability is less useful for low or full
453
 * speed interrupt endpoints, which allow at most one packet per millisecond,
454
 * of at most 8 or 64 bytes (respectively).
455
 */
456
void usb_sg_wait (struct usb_sg_request *io)
457
{
458
        int             i;
459
        unsigned long   flags;
460
 
461
        /* queue the urbs.  */
462
        spin_lock_irqsave (&io->lock, flags);
463
        for (i = 0; i < io->entries && !io->status; i++) {
464
                int     retval;
465
 
466
                io->urbs [i]->dev = io->dev;
467
                retval = usb_submit_urb (io->urbs [i], SLAB_ATOMIC);
468
 
469
                /* after we submit, let completions or cancelations fire;
470
                 * we handshake using io->status.
471
                 */
472
                spin_unlock_irqrestore (&io->lock, flags);
473
                switch (retval) {
474
                        /* maybe we retrying will recover */
475
                case -ENXIO:    // hc didn't queue this one
476
                case -EAGAIN:
477
                case -ENOMEM:
478
                        io->urbs [i]->dev = 0;
479
                        retval = 0;
480
                        i--;
481
                        yield ();
482
                        break;
483
 
484
                        /* no error? continue immediately.
485
                         *
486
                         * NOTE: to work better with UHCI (4K I/O buffer may
487
                         * need 3K of TDs) it may be good to limit how many
488
                         * URBs are queued at once; N milliseconds?
489
                         */
490
                case 0:
491
                        cpu_relax ();
492
                        break;
493
 
494
                        /* fail any uncompleted urbs */
495
                default:
496
                        io->urbs [i]->dev = 0;
497
                        io->urbs [i]->status = retval;
498
                        dev_dbg (&io->dev->dev, "%s, submit --> %d\n",
499
                                __FUNCTION__, retval);
500
                        usb_sg_cancel (io);
501
                }
502
                spin_lock_irqsave (&io->lock, flags);
503
                if (retval && io->status == -ECONNRESET)
504
                        io->status = retval;
505
        }
506
        spin_unlock_irqrestore (&io->lock, flags);
507
 
508
        /* OK, yes, this could be packaged as non-blocking.
509
         * So could the submit loop above ... but it's easier to
510
         * solve neither problem than to solve both!
511
         */
512
        wait_for_completion (&io->complete);
513
 
514
        sg_clean (io);
515
}
516
 
517
/**
518
 * usb_sg_cancel - stop scatter/gather i/o issued by usb_sg_wait()
519
 * @io: request block, initialized with usb_sg_init()
520
 *
521
 * This stops a request after it has been started by usb_sg_wait().
522
 * It can also prevents one initialized by usb_sg_init() from starting,
523
 * so that call just frees resources allocated to the request.
524
 */
525
void usb_sg_cancel (struct usb_sg_request *io)
526
{
527
        unsigned long   flags;
528
 
529
        spin_lock_irqsave (&io->lock, flags);
530
 
531
        /* shut everything down, if it didn't already */
532
        if (!io->status) {
533
                int     i;
534
 
535
                io->status = -ECONNRESET;
536
                for (i = 0; i < io->entries; i++) {
537
                        int     retval;
538
 
539
                        if (!io->urbs [i]->dev)
540
                                continue;
541
                        retval = usb_unlink_urb (io->urbs [i]);
542
                        if (retval != -EINPROGRESS && retval != -EBUSY)
543
                                dev_warn (&io->dev->dev, "%s, unlink --> %d\n",
544
                                        __FUNCTION__, retval);
545
                }
546
        }
547
        spin_unlock_irqrestore (&io->lock, flags);
548
}
549
 
550
/*-------------------------------------------------------------------*/
551
 
552
/**
553
 * usb_get_descriptor - issues a generic GET_DESCRIPTOR request
554
 * @dev: the device whose descriptor is being retrieved
555
 * @type: the descriptor type (USB_DT_*)
556
 * @index: the number of the descriptor
557
 * @buf: where to put the descriptor
558
 * @size: how big is "buf"?
559
 * Context: !in_interrupt ()
560
 *
561
 * Gets a USB descriptor.  Convenience functions exist to simplify
562
 * getting some types of descriptors.  Use
563
 * usb_get_device_descriptor() for USB_DT_DEVICE,
564
 * and usb_get_string() or usb_string() for USB_DT_STRING.
565
 * Configuration descriptors (USB_DT_CONFIG) are part of the device
566
 * structure, at least for the current configuration.
567
 * In addition to a number of USB-standard descriptors, some
568
 * devices also use class-specific or vendor-specific descriptors.
569
 *
570
 * This call is synchronous, and may not be used in an interrupt context.
571
 *
572
 * Returns the number of bytes received on success, or else the status code
573
 * returned by the underlying usb_control_msg() call.
574
 */
575
int usb_get_descriptor(struct usb_device *dev, unsigned char type, unsigned char index, void *buf, int size)
576
{
577
        int i = 5;
578
        int result = 0;
579
 
580
        memset(buf,0,size);     // Make sure we parse really received data
581
 
582
        while (i--) {
583
                /* retries if the returned length was 0; flakey device */
584
                if ((result = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
585
                                    USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
586
                                    (type << 8) + index, 0, buf, size,
587
                                    HZ * USB_CTRL_GET_TIMEOUT)) > 0
588
                                || result == -EPIPE)
589
                        break;
590
        }
591
        return result;
592
}
593
 
594
/**
595
 * usb_get_string - gets a string descriptor
596
 * @dev: the device whose string descriptor is being retrieved
597
 * @langid: code for language chosen (from string descriptor zero)
598
 * @index: the number of the descriptor
599
 * @buf: where to put the string
600
 * @size: how big is "buf"?
601
 * Context: !in_interrupt ()
602
 *
603
 * Retrieves a string, encoded using UTF-16LE (Unicode, 16 bits per character,
604
 * in little-endian byte order).
605
 * The usb_string() function will often be a convenient way to turn
606
 * these strings into kernel-printable form.
607
 *
608
 * Strings may be referenced in device, configuration, interface, or other
609
 * descriptors, and could also be used in vendor-specific ways.
610
 *
611
 * This call is synchronous, and may not be used in an interrupt context.
612
 *
613
 * Returns the number of bytes received on success, or else the status code
614
 * returned by the underlying usb_control_msg() call.
615
 */
616
int usb_get_string(struct usb_device *dev, unsigned short langid, unsigned char index, void *buf, int size)
617
{
618
        return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
619
                USB_REQ_GET_DESCRIPTOR, USB_DIR_IN,
620
                (USB_DT_STRING << 8) + index, langid, buf, size,
621
                HZ * USB_CTRL_GET_TIMEOUT);
622
}
623
 
624
/**
625
 * usb_get_device_descriptor - (re)reads the device descriptor
626
 * @dev: the device whose device descriptor is being updated
627
 * Context: !in_interrupt ()
628
 *
629
 * Updates the copy of the device descriptor stored in the device structure,
630
 * which dedicates space for this purpose.  Note that several fields are
631
 * converted to the host CPU's byte order:  the USB version (bcdUSB), and
632
 * vendors product and version fields (idVendor, idProduct, and bcdDevice).
633
 * That lets device drivers compare against non-byteswapped constants.
634
 *
635
 * There's normally no need to use this call, although some devices
636
 * will change their descriptors after events like updating firmware.
637
 *
638
 * This call is synchronous, and may not be used in an interrupt context.
639
 *
640
 * Returns the number of bytes received on success, or else the status code
641
 * returned by the underlying usb_control_msg() call.
642
 */
643
int usb_get_device_descriptor(struct usb_device *dev)
644
{
645
        int ret = usb_get_descriptor(dev, USB_DT_DEVICE, 0, &dev->descriptor,
646
                                     sizeof(dev->descriptor));
647
        if (ret >= 0) {
648
                le16_to_cpus(&dev->descriptor.bcdUSB);
649
                le16_to_cpus(&dev->descriptor.idVendor);
650
                le16_to_cpus(&dev->descriptor.idProduct);
651
                le16_to_cpus(&dev->descriptor.bcdDevice);
652
        }
653
        return ret;
654
}
655
 
656
/**
657
 * usb_get_status - issues a GET_STATUS call
658
 * @dev: the device whose status is being checked
659
 * @type: USB_RECIP_*; for device, interface, or endpoint
660
 * @target: zero (for device), else interface or endpoint number
661
 * @data: pointer to two bytes of bitmap data
662
 * Context: !in_interrupt ()
663
 *
664
 * Returns device, interface, or endpoint status.  Normally only of
665
 * interest to see if the device is self powered, or has enabled the
666
 * remote wakeup facility; or whether a bulk or interrupt endpoint
667
 * is halted ("stalled").
668
 *
669
 * Bits in these status bitmaps are set using the SET_FEATURE request,
670
 * and cleared using the CLEAR_FEATURE request.  The usb_clear_halt()
671
 * function should be used to clear halt ("stall") status.
672
 *
673
 * This call is synchronous, and may not be used in an interrupt context.
674
 *
675
 * Returns the number of bytes received on success, or else the status code
676
 * returned by the underlying usb_control_msg() call.
677
 */
678
int usb_get_status(struct usb_device *dev, int type, int target, void *data)
679
{
680
        return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),
681
                USB_REQ_GET_STATUS, USB_DIR_IN | type, 0, target, data, 2,
682
                HZ * USB_CTRL_GET_TIMEOUT);
683
}
684
 
685
/**
686
 * usb_clear_halt - tells device to clear endpoint halt/stall condition
687
 * @dev: device whose endpoint is halted
688
 * @pipe: endpoint "pipe" being cleared
689
 * Context: !in_interrupt ()
690
 *
691
 * This is used to clear halt conditions for bulk and interrupt endpoints,
692
 * as reported by URB completion status.  Endpoints that are halted are
693
 * sometimes referred to as being "stalled".  Such endpoints are unable
694
 * to transmit or receive data until the halt status is cleared.  Any URBs
695
 * queued for such an endpoint should normally be unlinked by the driver
696
 * before clearing the halt condition, as described in sections 5.7.5
697
 * and 5.8.5 of the USB 2.0 spec.
698
 *
699
 * Note that control and isochronous endpoints don't halt, although control
700
 * endpoints report "protocol stall" (for unsupported requests) using the
701
 * same status code used to report a true stall.
702
 *
703
 * This call is synchronous, and may not be used in an interrupt context.
704
 *
705
 * Returns zero on success, or else the status code returned by the
706
 * underlying usb_control_msg() call.
707
 */
708
int usb_clear_halt(struct usb_device *dev, int pipe)
709
{
710
        int result;
711
        int endp = usb_pipeendpoint(pipe);
712
 
713
        if (usb_pipein (pipe))
714
                endp |= USB_DIR_IN;
715
 
716
        /* we don't care if it wasn't halted first. in fact some devices
717
         * (like some ibmcam model 1 units) seem to expect hosts to make
718
         * this request for iso endpoints, which can't halt!
719
         */
720
        result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
721
                USB_REQ_CLEAR_FEATURE, USB_RECIP_ENDPOINT, 0, endp, NULL, 0,
722
                HZ * USB_CTRL_SET_TIMEOUT);
723
 
724
        /* don't un-halt or force to DATA0 except on success */
725
        if (result < 0)
726
                return result;
727
 
728
        /* NOTE:  seems like Microsoft and Apple don't bother verifying
729
         * the clear "took", so some devices could lock up if you check...
730
         * such as the Hagiwara FlashGate DUAL.  So we won't bother.
731
         *
732
         * NOTE:  make sure the logic here doesn't diverge much from
733
         * the copy in usb-storage, for as long as we need two copies.
734
         */
735
 
736
        /* toggle was reset by the clear, then ep was reactivated */
737
        usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), 0);
738
        usb_endpoint_running(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe));
739
 
740
        return 0;
741
}
742
 
743
/**
744
 * usb_disable_endpoint -- Disable an endpoint by address
745
 * @dev: the device whose endpoint is being disabled
746
 * @epaddr: the endpoint's address.  Endpoint number for output,
747
 *      endpoint number + USB_DIR_IN for input
748
 *
749
 * Deallocates hcd/hardware state for this endpoint ... and nukes all
750
 * pending urbs.
751
 *
752
 * If the HCD hasn't registered a disable() function, this marks the
753
 * endpoint as halted and sets its maxpacket size to 0 to prevent
754
 * further submissions.
755
 */
756
void usb_disable_endpoint(struct usb_device *dev, unsigned int epaddr)
757
{
758
        if (dev && dev->bus && dev->bus->op && dev->bus->op->disable)
759
                dev->bus->op->disable(dev, epaddr);
760
        else {
761
                unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
762
 
763
                if (usb_endpoint_out(epaddr)) {
764
                        usb_endpoint_halt(dev, epnum, 1);
765
                        dev->epmaxpacketout[epnum] = 0;
766
                } else {
767
                        usb_endpoint_halt(dev, epnum, 0);
768
                        dev->epmaxpacketin[epnum] = 0;
769
                }
770
        }
771
}
772
 
773
/**
774
 * usb_disable_interface -- Disable all endpoints for an interface
775
 * @dev: the device whose interface is being disabled
776
 * @intf: pointer to the interface descriptor
777
 *
778
 * Disables all the endpoints for the interface's current altsetting.
779
 */
780
void usb_disable_interface(struct usb_device *dev, struct usb_interface *intf)
781
{
782
        struct usb_host_interface *hintf =
783
                        &intf->altsetting[intf->act_altsetting];
784
        int i;
785
 
786
        for (i = 0; i < hintf->desc.bNumEndpoints; ++i) {
787
                usb_disable_endpoint(dev,
788
                                hintf->endpoint[i].desc.bEndpointAddress);
789
        }
790
}
791
 
792
/*
793
 * usb_disable_device - Disable all the endpoints for a USB device
794
 * @dev: the device whose endpoints are being disabled
795
 * @skip_ep0: 0 to disable endpoint 0, 1 to skip it.
796
 *
797
 * Disables all the device's endpoints, potentially including endpoint 0.
798
 * Deallocates hcd/hardware state for the endpoints (nuking all or most
799
 * pending urbs) and usbcore state for the interfaces, so that usbcore
800
 * must usb_set_configuration() before any interfaces could be used.
801
 */
802
void usb_disable_device(struct usb_device *dev, int skip_ep0)
803
{
804
        int i;
805
 
806
        dev_dbg(&dev->dev, "%s nuking %s URBs\n", __FUNCTION__,
807
                        skip_ep0 ? "non-ep0" : "all");
808
        for (i = skip_ep0; i < 16; ++i) {
809
                usb_disable_endpoint(dev, i);
810
                usb_disable_endpoint(dev, i + USB_DIR_IN);
811
        }
812
        dev->toggle[0] = dev->toggle[1] = 0;
813
        dev->halted[0] = dev->halted[1] = 0;
814
 
815
        /* getting rid of interfaces will disconnect
816
         * any drivers bound to them (a key side effect)
817
         */
818
        if (dev->actconfig) {
819
                for (i = 0; i < dev->actconfig->desc.bNumInterfaces; i++) {
820
                        struct usb_interface    *interface;
821
 
822
                        /* remove this interface */
823
                        interface = dev->actconfig->interface[i];
824
                        dev_dbg (&dev->dev, "unregistering interface %s\n",
825
                                interface->dev.bus_id);
826
                        device_del(&interface->dev);
827
                }
828
                dev->actconfig = 0;
829
                if (dev->state == USB_STATE_CONFIGURED)
830
                        dev->state = USB_STATE_ADDRESS;
831
        }
832
}
833
 
834
 
835
/*
836
 * usb_enable_endpoint - Enable an endpoint for USB communications
837
 * @dev: the device whose interface is being enabled
838
 * @epd: pointer to the endpoint descriptor
839
 *
840
 * Marks the endpoint as running, resets its toggle, and stores
841
 * its maxpacket value.  For control endpoints, both the input
842
 * and output sides are handled.
843
 */
844
void usb_enable_endpoint(struct usb_device *dev,
845
                struct usb_endpoint_descriptor *epd)
846
{
847
        int maxsize = epd->wMaxPacketSize;
848
        unsigned int epaddr = epd->bEndpointAddress;
849
        unsigned int epnum = epaddr & USB_ENDPOINT_NUMBER_MASK;
850
        int is_control = ((epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
851
                                USB_ENDPOINT_XFER_CONTROL);
852
 
853
        if (usb_endpoint_out(epaddr) || is_control) {
854
                usb_endpoint_running(dev, epnum, 1);
855
                usb_settoggle(dev, epnum, 1, 0);
856
                dev->epmaxpacketout[epnum] = maxsize;
857
        }
858
        if (!usb_endpoint_out(epaddr) || is_control) {
859
                usb_endpoint_running(dev, epnum, 0);
860
                usb_settoggle(dev, epnum, 0, 0);
861
                dev->epmaxpacketin[epnum] = maxsize;
862
        }
863
}
864
 
865
/*
866
 * usb_enable_interface - Enable all the endpoints for an interface
867
 * @dev: the device whose interface is being enabled
868
 * @intf: pointer to the interface descriptor
869
 *
870
 * Enables all the endpoints for the interface's current altsetting.
871
 */
872
void usb_enable_interface(struct usb_device *dev,
873
                struct usb_interface *intf)
874
{
875
        struct usb_host_interface *hintf =
876
                        &intf->altsetting[intf->act_altsetting];
877
        int i;
878
 
879
        for (i = 0; i < hintf->desc.bNumEndpoints; ++i)
880
                usb_enable_endpoint(dev, &hintf->endpoint[i].desc);
881
}
882
 
883
/**
884
 * usb_set_interface - Makes a particular alternate setting be current
885
 * @dev: the device whose interface is being updated
886
 * @interface: the interface being updated
887
 * @alternate: the setting being chosen.
888
 * Context: !in_interrupt ()
889
 *
890
 * This is used to enable data transfers on interfaces that may not
891
 * be enabled by default.  Not all devices support such configurability.
892
 * Only the driver bound to an interface may change its setting.
893
 *
894
 * Within any given configuration, each interface may have several
895
 * alternative settings.  These are often used to control levels of
896
 * bandwidth consumption.  For example, the default setting for a high
897
 * speed interrupt endpoint may not send more than 64 bytes per microframe,
898
 * while interrupt transfers of up to 3KBytes per microframe are legal.
899
 * Also, isochronous endpoints may never be part of an
900
 * interface's default setting.  To access such bandwidth, alternate
901
 * interface settings must be made current.
902
 *
903
 * Note that in the Linux USB subsystem, bandwidth associated with
904
 * an endpoint in a given alternate setting is not reserved until an URB
905
 * is submitted that needs that bandwidth.  Some other operating systems
906
 * allocate bandwidth early, when a configuration is chosen.
907
 *
908
 * This call is synchronous, and may not be used in an interrupt context.
909
 * Also, drivers must not change altsettings while urbs are scheduled for
910
 * endpoints in that interface; all such urbs must first be completed
911
 * (perhaps forced by unlinking).
912
 *
913
 * Returns zero on success, or else the status code returned by the
914
 * underlying usb_control_msg() call.
915
 */
916
int usb_set_interface(struct usb_device *dev, int interface, int alternate)
917
{
918
        struct usb_interface *iface;
919
        int ret;
920
        int manual = 0;
921
 
922
        iface = usb_ifnum_to_if(dev, interface);
923
        if (!iface) {
924
                warn("selecting invalid interface %d", interface);
925
                return -EINVAL;
926
        }
927
 
928
        if (alternate < 0 || alternate >= iface->num_altsetting)
929
                return -EINVAL;
930
 
931
        ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
932
                                   USB_REQ_SET_INTERFACE, USB_RECIP_INTERFACE,
933
                                   iface->altsetting[alternate]
934
                                        .desc.bAlternateSetting,
935
                                   interface, NULL, 0, HZ * 5);
936
 
937
        /* 9.4.10 says devices don't need this and are free to STALL the
938
         * request if the interface only has one alternate setting.
939
         */
940
        if (ret == -EPIPE && iface->num_altsetting == 1) {
941
                dbg("manual set_interface for dev %d, iface %d, alt %d",
942
                        dev->devnum, interface, alternate);
943
                manual = 1;
944
        } else if (ret < 0)
945
                return ret;
946
 
947
        /* FIXME drivers shouldn't need to replicate/bugfix the logic here
948
         * when they implement async or easily-killable versions of this or
949
         * other "should-be-internal" functions (like clear_halt).
950
         * should hcd+usbcore postprocess control requests?
951
         */
952
 
953
        /* prevent submissions using previous endpoint settings */
954
        usb_disable_interface(dev, iface);
955
 
956
        iface->act_altsetting = alternate;
957
 
958
        /* If the interface only has one altsetting and the device didn't
959
         * accept the request, we attempt to carry out the equivalent action
960
         * by manually clearing the HALT feature for each endpoint in the
961
         * new altsetting.
962
         */
963
        if (manual) {
964
                struct usb_host_interface *iface_as =
965
                                &iface->altsetting[alternate];
966
                int i;
967
 
968
                for (i = 0; i < iface_as->desc.bNumEndpoints; i++) {
969
                        unsigned int epaddr =
970
                                iface_as->endpoint[i].desc.bEndpointAddress;
971
                        unsigned int pipe =
972
        __create_pipe(dev, USB_ENDPOINT_NUMBER_MASK & epaddr)
973
        | (usb_endpoint_out(epaddr) ? USB_DIR_OUT : USB_DIR_IN);
974
 
975
                        usb_clear_halt(dev, pipe);
976
                }
977
        }
978
 
979
        /* 9.1.1.5: reset toggles for all endpoints in the new altsetting
980
         *
981
         * Note:
982
         * Despite EP0 is always present in all interfaces/AS, the list of
983
         * endpoints from the descriptor does not contain EP0. Due to its
984
         * omnipresence one might expect EP0 being considered "affected" by
985
         * any SetInterface request and hence assume toggles need to be reset.
986
         * However, EP0 toggles are re-synced for every individual transfer
987
         * during the SETUP stage - hence EP0 toggles are "don't care" here.
988
         * (Likewise, EP0 never "halts" on well designed devices.)
989
         */
990
        usb_enable_interface(dev, iface);
991
 
992
        return 0;
993
}
994
 
995
/**
996
 * usb_reset_configuration - lightweight device reset
997
 * @dev: the device whose configuration is being reset
998
 *
999
 * This issues a standard SET_CONFIGURATION request to the device using
1000
 * the current configuration.  The effect is to reset most USB-related
1001
 * state in the device, including interface altsettings (reset to zero),
1002
 * endpoint halts (cleared), and data toggle (only for bulk and interrupt
1003
 * endpoints).  Other usbcore state is unchanged, including bindings of
1004
 * usb device drivers to interfaces.
1005
 *
1006
 * Because this affects multiple interfaces, avoid using this with composite
1007
 * (multi-interface) devices.  Instead, the driver for each interface may
1008
 * use usb_set_interface() on the interfaces it claims.  Resetting the whole
1009
 * configuration would affect other drivers' interfaces.
1010
 *
1011
 * Returns zero on success, else a negative error code.
1012
 */
1013
int usb_reset_configuration(struct usb_device *dev)
1014
{
1015
        int                     i, retval;
1016
        struct usb_host_config  *config;
1017
 
1018
        /* caller must own dev->serialize (config won't change)
1019
         * and the usb bus readlock (so driver bindings are stable);
1020
         * so calls during probe() are fine
1021
         */
1022
 
1023
        for (i = 1; i < 16; ++i) {
1024
                usb_disable_endpoint(dev, i);
1025
                usb_disable_endpoint(dev, i + USB_DIR_IN);
1026
        }
1027
 
1028
        config = dev->actconfig;
1029
        retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1030
                        USB_REQ_SET_CONFIGURATION, 0,
1031
                        config->desc.bConfigurationValue, 0,
1032
                        NULL, 0, HZ * USB_CTRL_SET_TIMEOUT);
1033
        if (retval < 0) {
1034
                dev->state = USB_STATE_ADDRESS;
1035
                return retval;
1036
        }
1037
 
1038
        dev->toggle[0] = dev->toggle[1] = 0;
1039
        dev->halted[0] = dev->halted[1] = 0;
1040
 
1041
        /* re-init hc/hcd interface/endpoint state */
1042
        for (i = 0; i < config->desc.bNumInterfaces; i++) {
1043
                struct usb_interface *intf = config->interface[i];
1044
 
1045
                intf->act_altsetting = 0;
1046
                usb_enable_interface(dev, intf);
1047
        }
1048
        return 0;
1049
}
1050
 
1051
/**
1052
 * usb_set_configuration - Makes a particular device setting be current
1053
 * @dev: the device whose configuration is being updated
1054
 * @configuration: the configuration being chosen.
1055
 * Context: !in_interrupt ()
1056
 *
1057
 * This is used to enable non-default device modes.  Not all devices
1058
 * use this kind of configurability; many devices only have one
1059
 * configuration.
1060
 *
1061
 * USB device configurations may affect Linux interoperability,
1062
 * power consumption and the functionality available.  For example,
1063
 * the default configuration is limited to using 100mA of bus power,
1064
 * so that when certain device functionality requires more power,
1065
 * and the device is bus powered, that functionality should be in some
1066
 * non-default device configuration.  Other device modes may also be
1067
 * reflected as configuration options, such as whether two ISDN
1068
 * channels are available independently; and choosing between open
1069
 * standard device protocols (like CDC) or proprietary ones.
1070
 *
1071
 * Note that USB has an additional level of device configurability,
1072
 * associated with interfaces.  That configurability is accessed using
1073
 * usb_set_interface().
1074
 *
1075
 * This call is synchronous. The calling context must be able to sleep,
1076
 * and must not hold the driver model lock for USB; usb device driver
1077
 * probe() methods may not use this routine.
1078
 *
1079
 * Returns zero on success, or else the status code returned by the
1080
 * underlying call that failed.  On succesful completion, each interface
1081
 * in the original device configuration has been destroyed, and each one
1082
 * in the new configuration has been probed by all relevant usb device
1083
 * drivers currently known to the kernel.
1084
 */
1085
int usb_set_configuration(struct usb_device *dev, int configuration)
1086
{
1087
        int i, ret;
1088
        struct usb_host_config *cp = NULL;
1089
 
1090
        /* dev->serialize guards all config changes */
1091
        down(&dev->serialize);
1092
 
1093
        for (i=0; i<dev->descriptor.bNumConfigurations; i++) {
1094
                if (dev->config[i].desc.bConfigurationValue == configuration) {
1095
                        cp = &dev->config[i];
1096
                        break;
1097
                }
1098
        }
1099
        if ((!cp && configuration != 0)) {
1100
                ret = -EINVAL;
1101
                goto out;
1102
        }
1103
        if (cp && configuration == 0)
1104
                dev_warn(&dev->dev, "config 0 descriptor??\n");
1105
 
1106
        /* if it's already configured, clear out old state first.
1107
         * getting rid of old interfaces means unbinding their drivers.
1108
         */
1109
        if (dev->state != USB_STATE_ADDRESS)
1110
                usb_disable_device (dev, 1);    // Skip ep0
1111
 
1112
        if ((ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
1113
                        USB_REQ_SET_CONFIGURATION, 0, configuration, 0,
1114
                        NULL, 0, HZ * USB_CTRL_SET_TIMEOUT)) < 0)
1115
                goto out;
1116
 
1117
        dev->actconfig = cp;
1118
        if (!configuration)
1119
                dev->state = USB_STATE_ADDRESS;
1120
        else {
1121
                dev->state = USB_STATE_CONFIGURED;
1122
 
1123
                /* re-initialize hc/hcd/usbcore interface/endpoint state.
1124
                 * this triggers binding of drivers to interfaces; and
1125
                 * maybe probe() calls will choose different altsettings.
1126
                 */
1127
                for (i = 0; i < cp->desc.bNumInterfaces; ++i) {
1128
                        struct usb_interface *intf = cp->interface[i];
1129
                        struct usb_interface_descriptor *desc;
1130
 
1131
                        intf->act_altsetting = 0;
1132
                        desc = &intf->altsetting [0].desc;
1133
                        usb_enable_interface(dev, intf);
1134
 
1135
                        intf->dev.parent = &dev->dev;
1136
                        intf->dev.driver = NULL;
1137
                        intf->dev.bus = &usb_bus_type;
1138
                        intf->dev.dma_mask = dev->dev.dma_mask;
1139
                        sprintf26 (&intf->dev.bus_id[0], "%d-%s:%d.%d",
1140
                                 dev->bus->busnum, dev->devpath,
1141
                                 configuration,
1142
                                 desc->bInterfaceNumber);
1143
                        dev_dbg (&dev->dev,
1144
                                "registering %s (config #%d, interface %d)\n",
1145
                                intf->dev.bus_id, configuration,
1146
                                desc->bInterfaceNumber);
1147
                        device_add (&intf->dev);
1148
                        usb_create_driverfs_intf_files (intf);
1149
                }
1150
        }
1151
 
1152
out:
1153
        up(&dev->serialize);
1154
        return ret;
1155
}
1156
 
1157
/**
1158
 * usb_string - returns ISO 8859-1 version of a string descriptor
1159
 * @dev: the device whose string descriptor is being retrieved
1160
 * @index: the number of the descriptor
1161
 * @buf: where to put the string
1162
 * @size: how big is "buf"?
1163
 * Context: !in_interrupt ()
1164
 *
1165
 * This converts the UTF-16LE encoded strings returned by devices, from
1166
 * usb_get_string_descriptor(), to null-terminated ISO-8859-1 encoded ones
1167
 * that are more usable in most kernel contexts.  Note that all characters
1168
 * in the chosen descriptor that can't be encoded using ISO-8859-1
1169
 * are converted to the question mark ("?") character, and this function
1170
 * chooses strings in the first language supported by the device.
1171
 *
1172
 * The ASCII (or, redundantly, "US-ASCII") character set is the seven-bit
1173
 * subset of ISO 8859-1. ISO-8859-1 is the eight-bit subset of Unicode,
1174
 * and is appropriate for use many uses of English and several other
1175
 * Western European languages.  (But it doesn't include the "Euro" symbol.)
1176
 *
1177
 * This call is synchronous, and may not be used in an interrupt context.
1178
 *
1179
 * Returns length of the string (>= 0) or usb_control_msg status (< 0).
1180
 */
1181
int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
1182
{
1183
        unsigned char *tbuf;
1184
        int err, len;
1185
        unsigned int u, idx;
1186
 
1187
        if (size <= 0 || !buf || !index)
1188
                return -EINVAL;
1189
        buf[0] = 0;
1190
        tbuf = kmalloc(256, GFP_KERNEL);
1191
        if (!tbuf)
1192
                return -ENOMEM;
1193
 
1194
        /* get langid for strings if it's not yet known */
1195
        if (!dev->have_langid) {
1196
                err = usb_get_string(dev, 0, 0, tbuf, 4);
1197
                if (err < 0) {
1198
                        err("error getting string descriptor 0 (error=%d)", err);
1199
                        goto errout;
1200
                } else if (err < 4 || tbuf[0] < 4) {
1201
                        err("string descriptor 0 too short");
1202
                        err = -EINVAL;
1203
                        goto errout;
1204
                } else {
1205
                        dev->have_langid = -1;
1206
                        dev->string_langid = tbuf[2] | (tbuf[3]<< 8);
1207
                                /* always use the first langid listed */
1208
                        dbg("USB device number %d default language ID 0x%x",
1209
                                dev->devnum, dev->string_langid);
1210
                }
1211
        }
1212
 
1213
        /*
1214
         * ask for the length of the string
1215
         */
1216
 
1217
        err = usb_get_string(dev, dev->string_langid, index, tbuf, 2);
1218
        if(err<2)
1219
                goto errout;
1220
        len=tbuf[0];   
1221
 
1222
        err = usb_get_string(dev, dev->string_langid, index, tbuf, len);
1223
        if (err < 0)
1224
                goto errout;
1225
 
1226
        size--;         /* leave room for trailing NULL char in output buffer */
1227
        for (idx = 0, u = 2; u < err; u += 2) {
1228
                if (idx >= size)
1229
                        break;
1230
                if (tbuf[u+1])                  /* high byte */
1231
                        buf[idx++] = '?';  /* non ISO-8859-1 character */
1232
                else
1233
                        buf[idx++] = tbuf[u];
1234
        }
1235
        buf[idx] = 0;
1236
        err = idx;
1237
 
1238
 errout:
1239
        kfree(tbuf);
1240
        return err;
1241
}
1242
 
1243
// synchronous request completion model
1244
EXPORT_SYMBOL(usb_control_msg);
1245
EXPORT_SYMBOL(usb_bulk_msg);
1246
 
1247
EXPORT_SYMBOL(usb_sg_init);
1248
EXPORT_SYMBOL(usb_sg_cancel);
1249
EXPORT_SYMBOL(usb_sg_wait);
1250
 
1251
// synchronous control message convenience routines
1252
EXPORT_SYMBOL(usb_get_descriptor);
1253
EXPORT_SYMBOL(usb_get_device_descriptor);
1254
EXPORT_SYMBOL(usb_get_status);
1255
EXPORT_SYMBOL(usb_get_string);
1256
EXPORT_SYMBOL(usb_string);
1257
 
1258
// synchronous calls that also maintain usbcore state
1259
EXPORT_SYMBOL(usb_clear_halt);
1260
EXPORT_SYMBOL(usb_reset_configuration);
1261
EXPORT_SYMBOL(usb_set_configuration);
1262
EXPORT_SYMBOL(usb_set_interface);
1263