2
* Freescale STMP378X UUT driver
4
* Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
5
* Copyright 2008-2009 Embedded Alley Solutions, Inc All Rights Reserved.
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:
13
* http://www.opensource.org/licenses/gpl-license.html
14
* http://www.gnu.org/copyleft/gpl.html
17
static u64 get_be64(u8 *buf)
19
return ((u64)get_unaligned_be32(buf) << 32) | get_unaligned_be32(buf + 4);
22
static int utp_init(struct fsg_dev *fsg)
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)
31
utp_context.utp_version = 0x1ull;
32
fsg->utp = &utp_context;
33
return misc_register(&utp_dev);
36
static void utp_exit(struct fsg_dev *fsg)
38
vfree(utp_context.buffer);
39
misc_deregister(&utp_dev);
42
static struct utp_user_data *utp_user_data_alloc(size_t size)
44
struct utp_user_data *uud;
46
uud = kzalloc(size + sizeof(*uud), GFP_KERNEL);
49
uud->data.size = size + sizeof(uud->data);
50
INIT_LIST_HEAD(&uud->link);
54
static void utp_user_data_free(struct utp_user_data *uud)
60
#define WAIT_ACTIVITY(queue) \
61
wait_event_interruptible(utp_context.wq, !list_empty(&utp_context.queue))
63
static ssize_t utp_file_read(struct file *file,
68
struct utp_user_data *uud;
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)
80
if (copy_to_user(buf, &uud->data, size_to_put))
83
utp_user_data_free(uud);
85
pr_info("sizeof = %d, size = %d\n",
89
pr_err("Will not free utp_user_data, because buffer size = %d,"
90
"need to put %d\n", size, size_to_put);
97
static ssize_t utp_file_write(struct file *file, const char __user *buf,
98
size_t size, loff_t *off)
100
struct utp_user_data *uud;
102
if (size < sizeof(uud->data))
104
uud = utp_user_data_alloc(size);
105
if (copy_from_user(&uud->data, buf, size))
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);
114
static int utp_get_sense(struct fsg_dev *fsg)
116
if (UTP_CTX(fsg)->processed == 0)
119
UTP_CTX(fsg)->processed = 0;
123
static int utp_do_read(struct fsg_dev *fsg, void *data, size_t size)
125
struct fsg_buffhd *bh;
130
/* Get the starting Logical Block Address and check that it's
134
if (unlikely(amount_left == 0))
135
return -EIO; /* No default reply*/
137
pr_debug("%s: sending %d\n", __func__, size);
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
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);
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);
157
/* If we were asked to read past the end of file,
158
* end with an empty buffer. */
160
bh->inreq->length = 0;
161
bh->state = BUF_STATE_FULL;
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;
172
bh->inreq->length = amount;
173
bh->state = BUF_STATE_FULL;
175
/* Send this buffer and go read some more */
178
start_transfer(fsg, fsg->bulk_in, bh->inreq,
179
&bh->inreq_busy, &bh->state);
181
fsg->next_buffhd_to_fill = bh->next;
183
if (amount_left <= 0)
187
return size - amount_left;
190
static int utp_do_write(struct fsg_dev *fsg, void *data, size_t size)
192
struct fsg_buffhd *bh;
194
u32 amount_left_to_req, amount_left_to_write;
199
/* Carry out the file writes */
201
amount_left_to_req = amount_left_to_write = size;
203
if (unlikely(amount_left_to_write == 0))
207
while (amount_left_to_write > 0) {
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) {
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);
230
/* Get the next buffer */
231
amount_left_to_req -= amount;
232
if (amount_left_to_req == 0)
235
/* amount is always divisible by 512, hence by
236
* the bulk-out maxpacket size */
237
bh->outreq->length = bh->bulk_out_intended_length =
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;
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) {
252
fsg->next_buffhd_to_drain = bh->next;
253
bh->state = BUF_STATE_EMPTY;
255
/* Did something go wrong with the transfer? */
256
if (bh->outreq->status != 0)
257
/* cry again, COMMUNICATION_FAILURE */
260
amount = bh->outreq->actual;
262
/* Perform the write */
263
memcpy(data + offset, bh->buf, amount);
266
if (signal_pending(current))
267
return -EINTR; /* Interrupted!*/
268
amount_left_to_write -= amount;
269
fsg->residue -= amount;
271
/* Did the host decide to stop early? */
272
if (bh->outreq->actual != bh->outreq->length) {
273
fsg->short_packet_received = 1;
279
/* Wait for something to happen */
280
rc = sleep_thread(fsg);
288
static inline void utp_set_sense(struct fsg_dev *fsg, u16 code, u64 reply)
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;
296
static void utp_poll(struct fsg_dev *fsg)
298
struct utp_context *ctx = UTP_CTX(fsg);
299
struct utp_user_data *uud = NULL;
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);
307
if (uud->data.flags & UTP_FLAG_STATUS) {
308
pr_debug("%s: exit with status %d\n", __func__,
310
UTP_SS_EXIT(fsg, uud->data.status);
312
pr_debug("%s: pass\n", __func__);
315
utp_user_data_free(uud);
317
pr_debug("%s: still busy...\n", __func__);
318
UTP_SS_BUSY(fsg, --ctx->counter);
322
static int utp_exec(struct fsg_dev *fsg,
325
unsigned long long payload)
327
struct utp_user_data *uud = NULL, *uud2r;
328
struct utp_context *ctx = UTP_CTX(fsg);
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);
335
mutex_lock(&ctx->lock);
336
list_add_tail(&uud2r->link, &ctx->read);
337
mutex_unlock(&ctx->lock);
340
if (command[0] == '!') /* there will be no response */
343
WAIT_ACTIVITY(write);
345
mutex_lock(&ctx->lock);
346
if (!list_empty(&ctx->write)) {
347
uud = list_first_entry(&ctx->write, struct utp_user_data, link);
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);
355
if (uud->data.flags & UTP_FLAG_REPORT_BUSY)
359
mutex_unlock(&ctx->lock);
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);
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);
375
utp_user_data_free(uud);
381
static int utp_send_status(struct fsg_dev *fsg)
383
struct fsg_buffhd *bh;
384
u8 status = USB_STATUS_PASS;
385
struct bulk_cs_wrap *csw;
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);
396
if (fsg->phase_error) {
397
DBG(fsg, "sending phase-error status\n");
398
status = USB_STATUS_PHASE_ERROR;
400
} else if ((UTP_CTX(fsg)->sd & 0xFFFF) != UTP_REPLY_PASS) {
401
status = USB_STATUS_FAIL;
406
/* Store and send the Bulk-only CSW */
407
csw->Signature = __constant_cpu_to_le32(USB_BULK_CS_SIG);
409
csw->Residue = cpu_to_le32(fsg->residue);
410
csw->Status = status;
412
bh->inreq->length = USB_BULK_CS_WRAP_LEN;
414
start_transfer(fsg, fsg->bulk_in, bh->inreq,
415
&bh->inreq_busy, &bh->state);
416
fsg->next_buffhd_to_fill = bh->next;
420
static int utp_handle_message(struct fsg_dev *fsg,
424
struct utp_msg *m = (struct utp_msg *)cdb_data;
427
struct utp_user_data *uud2r;
428
unsigned long long param;
432
return default_reply;
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);
439
switch ((enum utp_msg_type)m->utp_msg_type) {
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);
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);
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);
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);
476
utp_send_status(fsg);