~ubuntu-branches/ubuntu/vivid/linux-fsl-imx51/vivid

« back to all changes in this revision

Viewing changes to drivers/usb/gadget/stmp_updater.c

  • Committer: Bazaar Package Importer
  • Author(s): Andy Whitcroft, Amit Kucheria, Andy Whitcroft, Bryan Wu, Upstream Kernel Changes
  • Date: 2010-01-11 16:26:27 UTC
  • Revision ID: james.westby@ubuntu.com-20100111162627-1q2fl9tcuwcywt1e
Tags: 2.6.31-602.4
[ Amit Kucheria ]

* Update to official 2.6.31 BSP release from Freescale

[ Andy Whitcroft ]

* drop a number of modules no longer built

[ Bryan Wu ]

* [Config] Update configs after applying .31 patchset from Freescale
* [Config] Sync with imx51_defconfig from Freescale BSP

[ Upstream Kernel Changes ]

* Update to official 2.6.31 BSP release from Freescale.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Freescale STMP378X UUT driver
 
3
 *
 
4
 * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
 
5
 * Copyright 2008-2009 Embedded Alley Solutions, Inc All Rights Reserved.
 
6
 */
 
7
 
 
8
/*
 
9
 * The code contained herein is licensed under the GNU General Public
 
10
 * License. You may obtain a copy of the GNU General Public License
 
11
 * Version 2 or later at the following locations:
 
12
 *
 
13
 * http://www.opensource.org/licenses/gpl-license.html
 
14
 * http://www.gnu.org/copyleft/gpl.html
 
15
 */
 
16
 
 
17
static u64 get_be64(u8 *buf)
 
18
{
 
19
        return ((u64)get_unaligned_be32(buf) << 32) | get_unaligned_be32(buf + 4);
 
20
}
 
21
 
 
22
static int utp_init(struct fsg_dev *fsg)
 
23
{
 
24
        init_waitqueue_head(&utp_context.wq);
 
25
        INIT_LIST_HEAD(&utp_context.read);
 
26
        INIT_LIST_HEAD(&utp_context.write);
 
27
        mutex_init(&utp_context.lock);
 
28
        utp_context.buffer = vmalloc(0x10000);
 
29
        if (!utp_context.buffer)
 
30
                return -EIO;
 
31
        utp_context.utp_version = 0x1ull;
 
32
        fsg->utp = &utp_context;
 
33
        return misc_register(&utp_dev);
 
34
}
 
35
 
 
36
static void utp_exit(struct fsg_dev *fsg)
 
37
{
 
38
        vfree(utp_context.buffer);
 
39
        misc_deregister(&utp_dev);
 
40
}
 
41
 
 
42
static struct utp_user_data *utp_user_data_alloc(size_t size)
 
43
{
 
44
        struct utp_user_data *uud;
 
45
 
 
46
        uud = kzalloc(size + sizeof(*uud), GFP_KERNEL);
 
47
        if (!uud)
 
48
                return uud;
 
49
        uud->data.size = size + sizeof(uud->data);
 
50
        INIT_LIST_HEAD(&uud->link);
 
51
        return uud;
 
52
}
 
53
 
 
54
static void utp_user_data_free(struct utp_user_data *uud)
 
55
{
 
56
        list_del(&uud->link);
 
57
        kfree(uud);
 
58
}
 
59
 
 
60
#define WAIT_ACTIVITY(queue) \
 
61
 wait_event_interruptible(utp_context.wq, !list_empty(&utp_context.queue))
 
62
 
 
63
static ssize_t utp_file_read(struct file *file,
 
64
                             char __user *buf,
 
65
                             size_t size,
 
66
                             loff_t *off)
 
67
{
 
68
        struct utp_user_data *uud;
 
69
        size_t size_to_put;
 
70
        int free = 0;
 
71
 
 
72
        WAIT_ACTIVITY(read);
 
73
 
 
74
        mutex_lock(&utp_context.lock);
 
75
        uud = list_first_entry(&utp_context.read, struct utp_user_data, link);
 
76
        mutex_unlock(&utp_context.lock);
 
77
        size_to_put = uud->data.size;
 
78
        if (size >= size_to_put)
 
79
                free = !0;
 
80
        if (copy_to_user(buf, &uud->data, size_to_put))
 
81
                return -EACCES;
 
82
        if (free)
 
83
                utp_user_data_free(uud);
 
84
        else {
 
85
                pr_info("sizeof = %d, size = %d\n",
 
86
                        sizeof(uud->data),
 
87
                        uud->data.size);
 
88
 
 
89
                pr_err("Will not free utp_user_data, because buffer size = %d,"
 
90
                        "need to put %d\n", size, size_to_put);
 
91
        }
 
92
 
 
93
        return size_to_put;
 
94
}
 
95
 
 
96
 
 
97
static ssize_t utp_file_write(struct file *file, const char __user *buf,
 
98
                                size_t size, loff_t *off)
 
99
{
 
100
        struct utp_user_data *uud;
 
101
 
 
102
        if (size < sizeof(uud->data))
 
103
                return -EINVAL;
 
104
        uud = utp_user_data_alloc(size);
 
105
        if (copy_from_user(&uud->data, buf, size))
 
106
                return -EACCES;
 
107
        mutex_lock(&utp_context.lock);
 
108
        list_add_tail(&uud->link, &utp_context.write);
 
109
        wake_up(&utp_context.wq);
 
110
        mutex_unlock(&utp_context.lock);
 
111
        return size;
 
112
}
 
113
 
 
114
static int utp_get_sense(struct fsg_dev *fsg)
 
115
{
 
116
        if (UTP_CTX(fsg)->processed == 0)
 
117
                return -1;
 
118
 
 
119
        UTP_CTX(fsg)->processed = 0;
 
120
        return 0;
 
121
}
 
122
 
 
123
static int utp_do_read(struct fsg_dev *fsg, void *data, size_t size)
 
124
{
 
125
        struct fsg_buffhd       *bh;
 
126
        int                     rc;
 
127
        u32                     amount_left;
 
128
        unsigned int            amount;
 
129
 
 
130
        /* Get the starting Logical Block Address and check that it's
 
131
         * not too big */
 
132
 
 
133
        amount_left = size;
 
134
        if (unlikely(amount_left == 0))
 
135
                return -EIO;            /* No default reply*/
 
136
 
 
137
        pr_debug("%s: sending %d\n", __func__, size);
 
138
        for (;;) {
 
139
                /* Figure out how much we need to read:
 
140
                 * Try to read the remaining amount.
 
141
                 * But don't read more than the buffer size.
 
142
                 * And don't try to read past the end of the file.
 
143
                 * Finally, if we're not at a page boundary, don't read past
 
144
                 *      the next page.
 
145
                 * If this means reading 0 then we were asked to read past
 
146
                 *      the end of file. */
 
147
                amount = min((unsigned int) amount_left, mod_data.buflen);
 
148
 
 
149
                /* Wait for the next buffer to become available */
 
150
                bh = fsg->next_buffhd_to_fill;
 
151
                while (bh->state != BUF_STATE_EMPTY) {
 
152
                        rc = sleep_thread(fsg);
 
153
                        if (rc)
 
154
                                return rc;
 
155
                }
 
156
 
 
157
                /* If we were asked to read past the end of file,
 
158
                 * end with an empty buffer. */
 
159
                if (amount == 0) {
 
160
                        bh->inreq->length = 0;
 
161
                        bh->state = BUF_STATE_FULL;
 
162
                        break;
 
163
                }
 
164
 
 
165
                /* Perform the read */
 
166
                pr_info("Copied to %p, %d bytes started from %d\n",
 
167
                                bh->buf, amount, size - amount_left);
 
168
                memcpy(bh->buf, data + size - amount_left, amount);
 
169
                amount_left  -= amount;
 
170
                fsg->residue -= amount;
 
171
 
 
172
                bh->inreq->length = amount;
 
173
                bh->state = BUF_STATE_FULL;
 
174
 
 
175
                /* Send this buffer and go read some more */
 
176
                bh->inreq->zero = 0;
 
177
 
 
178
                start_transfer(fsg, fsg->bulk_in, bh->inreq,
 
179
                                &bh->inreq_busy, &bh->state);
 
180
 
 
181
                fsg->next_buffhd_to_fill = bh->next;
 
182
 
 
183
                if (amount_left <= 0)
 
184
                        break;
 
185
        }
 
186
 
 
187
        return size - amount_left;
 
188
}
 
189
 
 
190
static int utp_do_write(struct fsg_dev *fsg, void *data, size_t size)
 
191
{
 
192
        struct fsg_buffhd       *bh;
 
193
        int                     get_some_more;
 
194
        u32                     amount_left_to_req, amount_left_to_write;
 
195
        unsigned int            amount;
 
196
        int                     rc;
 
197
        loff_t                  offset;
 
198
 
 
199
        /* Carry out the file writes */
 
200
        get_some_more = 1;
 
201
        amount_left_to_req = amount_left_to_write = size;
 
202
 
 
203
        if (unlikely(amount_left_to_write == 0))
 
204
                return -EIO;
 
205
 
 
206
        offset = 0;
 
207
        while (amount_left_to_write > 0) {
 
208
 
 
209
                /* Queue a request for more data from the host */
 
210
                bh = fsg->next_buffhd_to_fill;
 
211
                if (bh->state == BUF_STATE_EMPTY && get_some_more) {
 
212
 
 
213
                        /* Figure out how much we want to get:
 
214
                         * Try to get the remaining amount.
 
215
                         * But don't get more than the buffer size.
 
216
                         * And don't try to go past the end of the file.
 
217
                         * If we're not at a page boundary,
 
218
                         *      don't go past the next page.
 
219
                         * If this means getting 0, then we were asked
 
220
                         *      to write past the end of file.
 
221
                         * Finally, round down to a block boundary. */
 
222
                        amount = min(amount_left_to_req, mod_data.buflen);
 
223
 
 
224
                        if (amount == 0) {
 
225
                                get_some_more = 0;
 
226
                                /* cry now */
 
227
                                continue;
 
228
                        }
 
229
 
 
230
                        /* Get the next buffer */
 
231
                        amount_left_to_req -= amount;
 
232
                        if (amount_left_to_req == 0)
 
233
                                get_some_more = 0;
 
234
 
 
235
                        /* amount is always divisible by 512, hence by
 
236
                         * the bulk-out maxpacket size */
 
237
                        bh->outreq->length = bh->bulk_out_intended_length =
 
238
                                        amount;
 
239
                        bh->outreq->short_not_ok = 1;
 
240
                        start_transfer(fsg, fsg->bulk_out, bh->outreq,
 
241
                                        &bh->outreq_busy, &bh->state);
 
242
                        fsg->next_buffhd_to_fill = bh->next;
 
243
                        continue;
 
244
                }
 
245
 
 
246
                /* Write the received data to the backing file */
 
247
                bh = fsg->next_buffhd_to_drain;
 
248
                if (bh->state == BUF_STATE_EMPTY && !get_some_more)
 
249
                        break;                  /* We stopped early */
 
250
                if (bh->state == BUF_STATE_FULL) {
 
251
                        smp_rmb();
 
252
                        fsg->next_buffhd_to_drain = bh->next;
 
253
                        bh->state = BUF_STATE_EMPTY;
 
254
 
 
255
                        /* Did something go wrong with the transfer? */
 
256
                        if (bh->outreq->status != 0)
 
257
                                /* cry again, COMMUNICATION_FAILURE */
 
258
                                break;
 
259
 
 
260
                        amount = bh->outreq->actual;
 
261
 
 
262
                        /* Perform the write */
 
263
                        memcpy(data + offset, bh->buf, amount);
 
264
 
 
265
                        offset += amount;
 
266
                        if (signal_pending(current))
 
267
                                return -EINTR;          /* Interrupted!*/
 
268
                        amount_left_to_write -= amount;
 
269
                        fsg->residue -= amount;
 
270
 
 
271
                        /* Did the host decide to stop early? */
 
272
                        if (bh->outreq->actual != bh->outreq->length) {
 
273
                                fsg->short_packet_received = 1;
 
274
                                break;
 
275
                        }
 
276
                        continue;
 
277
                }
 
278
 
 
279
                /* Wait for something to happen */
 
280
                rc = sleep_thread(fsg);
 
281
                if (rc)
 
282
                        return rc;
 
283
        }
 
284
 
 
285
        return -EIO;
 
286
}
 
287
 
 
288
static inline void utp_set_sense(struct fsg_dev *fsg, u16 code, u64 reply)
 
289
{
 
290
        UTP_CTX(fsg)->processed = true;
 
291
        UTP_CTX(fsg)->sdinfo = reply & 0xFFFFFFFF;
 
292
        UTP_CTX(fsg)->sdinfo_h = (reply >> 32) & 0xFFFFFFFF;
 
293
        UTP_CTX(fsg)->sd = (UTP_SENSE_KEY << 16) | code;
 
294
}
 
295
 
 
296
static void utp_poll(struct fsg_dev *fsg)
 
297
{
 
298
        struct utp_context *ctx = UTP_CTX(fsg);
 
299
        struct utp_user_data *uud = NULL;
 
300
 
 
301
        mutex_lock(&ctx->lock);
 
302
        if (!list_empty(&ctx->write))
 
303
                uud = list_first_entry(&ctx->write, struct utp_user_data, link);
 
304
        mutex_unlock(&ctx->lock);
 
305
 
 
306
        if (uud) {
 
307
                if (uud->data.flags & UTP_FLAG_STATUS) {
 
308
                        pr_debug("%s: exit with status %d\n", __func__,
 
309
                                uud->data.status);
 
310
                        UTP_SS_EXIT(fsg, uud->data.status);
 
311
                } else {
 
312
                        pr_debug("%s: pass\n", __func__);
 
313
                        UTP_SS_PASS(fsg);
 
314
                }
 
315
                utp_user_data_free(uud);
 
316
        } else {
 
317
                pr_debug("%s: still busy...\n", __func__);
 
318
                UTP_SS_BUSY(fsg, --ctx->counter);
 
319
        }
 
320
}
 
321
 
 
322
static int utp_exec(struct fsg_dev *fsg,
 
323
                    char *command,
 
324
                    int cmdsize,
 
325
                    unsigned long long payload)
 
326
{
 
327
        struct utp_user_data *uud = NULL, *uud2r;
 
328
        struct utp_context *ctx = UTP_CTX(fsg);
 
329
 
 
330
        uud2r = utp_user_data_alloc(cmdsize + 1);
 
331
        uud2r->data.flags = UTP_FLAG_COMMAND;
 
332
        uud2r->data.payload = payload;
 
333
        strncpy(uud2r->data.command, command, cmdsize);
 
334
 
 
335
        mutex_lock(&ctx->lock);
 
336
        list_add_tail(&uud2r->link, &ctx->read);
 
337
        mutex_unlock(&ctx->lock);
 
338
        wake_up(&ctx->wq);
 
339
 
 
340
        if (command[0] == '!')  /* there will be no response */
 
341
                return 0;
 
342
 
 
343
        WAIT_ACTIVITY(write);
 
344
 
 
345
        mutex_lock(&ctx->lock);
 
346
        if (!list_empty(&ctx->write)) {
 
347
                uud = list_first_entry(&ctx->write, struct utp_user_data, link);
 
348
#ifdef DEBUG
 
349
                pr_info("UUD:\n\tFlags = %02X\n", uud->data.flags);
 
350
                if (uud->data.flags & UTP_FLAG_DATA) {
 
351
                        pr_info("\tbufsize = %d\n", uud->data.bufsize);
 
352
                        print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_NONE,
 
353
                                16, 2, uud->data.data, uud->data.bufsize, true);
 
354
                }
 
355
                if (uud->data.flags & UTP_FLAG_REPORT_BUSY)
 
356
                        pr_info("\tBUSY\n");
 
357
#endif
 
358
        }
 
359
        mutex_unlock(&ctx->lock);
 
360
 
 
361
        if (uud->data.flags & UTP_FLAG_DATA) {
 
362
                memcpy(ctx->buffer, uud->data.data, uud->data.bufsize);
 
363
                UTP_SS_SIZE(fsg, uud->data.bufsize);
 
364
                utp_user_data_free(uud);
 
365
                return 0;
 
366
        }
 
367
 
 
368
        if (uud->data.flags & UTP_FLAG_REPORT_BUSY) {
 
369
                utp_user_data_free(uud);
 
370
                ctx->counter = 0xFFFF;
 
371
                UTP_SS_BUSY(fsg, ctx->counter);
 
372
                return 0;
 
373
        }
 
374
 
 
375
        utp_user_data_free(uud);
 
376
        UTP_SS_PASS(fsg);
 
377
 
 
378
        return -1;
 
379
}
 
380
 
 
381
static int utp_send_status(struct fsg_dev *fsg)
 
382
{
 
383
        struct fsg_buffhd       *bh;
 
384
        u8                      status = USB_STATUS_PASS;
 
385
        struct bulk_cs_wrap     *csw;
 
386
        int                     rc;
 
387
 
 
388
        /* Wait for the next buffer to become available */
 
389
        bh = fsg->next_buffhd_to_fill;
 
390
        while (bh->state != BUF_STATE_EMPTY) {
 
391
                rc = sleep_thread(fsg);
 
392
                if (rc)
 
393
                        return rc;
 
394
        }
 
395
 
 
396
        if (fsg->phase_error) {
 
397
                DBG(fsg, "sending phase-error status\n");
 
398
                status = USB_STATUS_PHASE_ERROR;
 
399
 
 
400
        } else if ((UTP_CTX(fsg)->sd & 0xFFFF) != UTP_REPLY_PASS) {
 
401
                status = USB_STATUS_FAIL;
 
402
        }
 
403
 
 
404
        csw = bh->buf;
 
405
 
 
406
        /* Store and send the Bulk-only CSW */
 
407
        csw->Signature = __constant_cpu_to_le32(USB_BULK_CS_SIG);
 
408
        csw->Tag = fsg->tag;
 
409
        csw->Residue = cpu_to_le32(fsg->residue);
 
410
        csw->Status = status;
 
411
 
 
412
        bh->inreq->length = USB_BULK_CS_WRAP_LEN;
 
413
        bh->inreq->zero = 0;
 
414
        start_transfer(fsg, fsg->bulk_in, bh->inreq,
 
415
                        &bh->inreq_busy, &bh->state);
 
416
        fsg->next_buffhd_to_fill = bh->next;
 
417
        return 0;
 
418
}
 
419
 
 
420
static int utp_handle_message(struct fsg_dev *fsg,
 
421
                              char *cdb_data,
 
422
                              int default_reply)
 
423
{
 
424
        struct utp_msg *m = (struct utp_msg *)cdb_data;
 
425
        void *data = NULL;
 
426
        int r;
 
427
        struct utp_user_data *uud2r;
 
428
        unsigned long long param;
 
429
        unsigned long tag;
 
430
 
 
431
        if (m->f0 != 0xF0)
 
432
                return default_reply;
 
433
 
 
434
        tag = get_unaligned_be32((void *)&m->utp_msg_tag);
 
435
        param = get_be64((void *)&m->param);
 
436
        pr_debug("Type 0x%x, tag 0x%08lx, param %llx\n",
 
437
                        m->utp_msg_type, tag, param);
 
438
 
 
439
        switch ((enum utp_msg_type)m->utp_msg_type) {
 
440
 
 
441
        case UTP_POLL:
 
442
                if (get_be64((void *)&m->param) == 1) {
 
443
                        pr_debug("%s: version request\n", __func__);
 
444
                        UTP_SS_EXIT(fsg, UTP_CTX(fsg)->utp_version);
 
445
                        break;
 
446
                }
 
447
                utp_poll(fsg);
 
448
                break;
 
449
        case UTP_EXEC:
 
450
                pr_debug("%s: EXEC\n", __func__);
 
451
                data = kzalloc(fsg->data_size, GFP_KERNEL);
 
452
                utp_do_write(fsg, data, fsg->data_size);
 
453
                utp_exec(fsg, data, fsg->data_size, param);
 
454
                kfree(data);
 
455
                break;
 
456
        case UTP_GET:
 
457
                pr_debug("%s: GET, %d bytes\n", __func__, fsg->data_size);
 
458
                r = utp_do_read(fsg, UTP_CTX(fsg)->buffer, fsg->data_size);
 
459
                UTP_SS_PASS(fsg);
 
460
                break;
 
461
        case UTP_PUT:
 
462
                pr_debug("%s: PUT, %d bytes\n", __func__, fsg->data_size);
 
463
                uud2r = utp_user_data_alloc(fsg->data_size);
 
464
                uud2r->data.bufsize = fsg->data_size;
 
465
                uud2r->data.flags = UTP_FLAG_DATA;
 
466
                utp_do_write(fsg, uud2r->data.data, fsg->data_size);
 
467
                /* don't know what will be written */
 
468
                mutex_lock(&UTP_CTX(fsg)->lock);
 
469
                list_add_tail(&uud2r->link, &UTP_CTX(fsg)->read);
 
470
                mutex_unlock(&UTP_CTX(fsg)->lock);
 
471
                wake_up(&UTP_CTX(fsg)->wq);
 
472
                UTP_SS_PASS(fsg);
 
473
                break;
 
474
        }
 
475
 
 
476
        utp_send_status(fsg);
 
477
        return -1;
 
478
}
 
479