4 * Copyright 2008-2013 Freescale Semiconductor, Inc.
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) |
20 get_unaligned_be32(buf + 4);
23 static int utp_init(struct fsg_dev *fsg)
25 init_waitqueue_head(&utp_context.wq);
26 init_waitqueue_head(&utp_context.list_full_wq);
28 INIT_LIST_HEAD(&utp_context.read);
29 INIT_LIST_HEAD(&utp_context.write);
30 mutex_init(&utp_context.lock);
32 /* the max message is 64KB */
33 utp_context.buffer = vmalloc(0x10000);
34 if (!utp_context.buffer)
36 utp_context.utp_version = 0x1ull;
37 fsg->utp = &utp_context;
38 return misc_register(&utp_dev);
41 static void utp_exit(struct fsg_dev *fsg)
43 vfree(utp_context.buffer);
44 misc_deregister(&utp_dev);
47 static struct utp_user_data *utp_user_data_alloc(size_t size)
49 struct utp_user_data *uud;
51 uud = vmalloc(size + sizeof(*uud));
54 memset(uud, 0, size + sizeof(*uud));
55 uud->data.size = size + sizeof(uud->data);
56 INIT_LIST_HEAD(&uud->link);
60 static void utp_user_data_free(struct utp_user_data *uud)
62 mutex_lock(&utp_context.lock);
64 mutex_unlock(&utp_context.lock);
68 /* Get the number of element for list */
69 static u32 count_list(struct list_head *l)
72 struct list_head *tmp;
74 mutex_lock(&utp_context.lock);
75 list_for_each(tmp, l) {
78 mutex_unlock(&utp_context.lock);
82 /* The routine will not go on if utp_context.queue is empty */
83 #define WAIT_ACTIVITY(queue) \
84 wait_event_interruptible(utp_context.wq, !list_empty(&utp_context.queue))
86 /* Called by userspace program (uuc) */
87 static ssize_t utp_file_read(struct file *file,
92 struct utp_user_data *uud;
98 mutex_lock(&utp_context.lock);
99 uud = list_first_entry(&utp_context.read, struct utp_user_data, link);
100 mutex_unlock(&utp_context.lock);
101 size_to_put = uud->data.size;
103 if (size >= size_to_put)
105 if (copy_to_user(buf, &uud->data, size_to_put)) {
106 printk(KERN_INFO "[ %s ] copy error\n", __func__);
110 utp_user_data_free(uud);
112 pr_info("sizeof = %d, size = %d\n",
116 pr_err("Will not free utp_user_data, because buffer size = %d,"
117 "need to put %d\n", size, size_to_put);
121 * The user program has already finished data process,
122 * go on getting data from the host
124 wake_up(&utp_context.list_full_wq);
129 static ssize_t utp_file_write(struct file *file, const char __user *buf,
130 size_t size, loff_t *off)
132 struct utp_user_data *uud;
134 if (size < sizeof(uud->data))
136 uud = utp_user_data_alloc(size);
139 if (copy_from_user(&uud->data, buf, size)) {
140 printk(KERN_INFO "[ %s ] copy error!\n", __func__);
144 mutex_lock(&utp_context.lock);
145 list_add_tail(&uud->link, &utp_context.write);
146 /* Go on EXEC routine process */
147 wake_up(&utp_context.wq);
148 mutex_unlock(&utp_context.lock);
153 * uuc should change to use soc bus infrastructure to soc information
154 * /sys/devices/soc0/soc_id
155 * this function can be removed.
158 utp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
164 return put_user(cpu_id, (int __user *)arg);
170 /* Will be called when the host wants to get the sense data */
171 static int utp_get_sense(struct fsg_dev *fsg)
173 if (UTP_CTX(fsg)->processed == 0)
176 UTP_CTX(fsg)->processed = 0;
180 static int utp_do_read(struct fsg_dev *fsg, void *data, size_t size)
182 struct fsg_buffhd *bh;
187 /* Get the starting Logical Block Address and check that it's
191 if (unlikely(amount_left == 0))
192 return -EIO; /* No default reply*/
194 pr_debug("%s: sending %d\n", __func__, size);
196 /* Figure out how much we need to read:
197 * Try to read the remaining amount.
198 * But don't read more than the buffer size.
199 * And don't try to read past the end of the file.
200 * Finally, if we're not at a page boundary, don't read past
202 * If this means reading 0 then we were asked to read past
203 * the end of file. */
204 amount = min((unsigned int) amount_left, FSG_BUFLEN);
206 /* Wait for the next buffer to become available */
207 bh = fsg->common->next_buffhd_to_fill;
208 while (bh->state != BUF_STATE_EMPTY) {
209 rc = sleep_thread(fsg->common);
214 /* If we were asked to read past the end of file,
215 * end with an empty buffer. */
217 bh->inreq->length = 0;
218 bh->state = BUF_STATE_FULL;
222 /* Perform the read */
223 pr_info("Copied to %p, %d bytes started from %d\n",
224 bh->buf, amount, size - amount_left);
225 /* from upt buffer to file_storeage buffer */
226 memcpy(bh->buf, data + size - amount_left, amount);
227 amount_left -= amount;
228 fsg->common->residue -= amount;
230 bh->inreq->length = amount;
231 bh->state = BUF_STATE_FULL;
233 /* Send this buffer and go read some more */
236 /* USB Physical transfer: Data from device to host */
237 start_transfer(fsg, fsg->bulk_in, bh->inreq,
238 &bh->inreq_busy, &bh->state);
240 fsg->common->next_buffhd_to_fill = bh->next;
242 if (amount_left <= 0)
246 return size - amount_left;
249 static int utp_do_write(struct fsg_dev *fsg, void *data, size_t size)
251 struct fsg_buffhd *bh;
253 u32 amount_left_to_req, amount_left_to_write;
258 /* Carry out the file writes */
260 amount_left_to_req = amount_left_to_write = size;
262 if (unlikely(amount_left_to_write == 0))
266 while (amount_left_to_write > 0) {
268 /* Queue a request for more data from the host */
269 bh = fsg->common->next_buffhd_to_fill;
270 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
272 /* Figure out how much we want to get:
273 * Try to get the remaining amount.
274 * But don't get more than the buffer size.
275 * And don't try to go past the end of the file.
276 * If we're not at a page boundary,
277 * don't go past the next page.
278 * If this means getting 0, then we were asked
279 * to write past the end of file.
280 * Finally, round down to a block boundary. */
281 amount = min(amount_left_to_req, FSG_BUFLEN);
289 /* Get the next buffer */
290 amount_left_to_req -= amount;
291 if (amount_left_to_req == 0)
294 /* amount is always divisible by 512, hence by
295 * the bulk-out maxpacket size */
296 bh->outreq->length = bh->bulk_out_intended_length =
298 bh->outreq->short_not_ok = 1;
299 start_transfer(fsg, fsg->bulk_out, bh->outreq,
300 &bh->outreq_busy, &bh->state);
301 fsg->common->next_buffhd_to_fill = bh->next;
305 /* Write the received data to the backing file */
306 bh = fsg->common->next_buffhd_to_drain;
307 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
308 break; /* We stopped early */
309 if (bh->state == BUF_STATE_FULL) {
311 fsg->common->next_buffhd_to_drain = bh->next;
312 bh->state = BUF_STATE_EMPTY;
314 /* Did something go wrong with the transfer? */
315 if (bh->outreq->status != 0)
316 /* cry again, COMMUNICATION_FAILURE */
319 amount = bh->outreq->actual;
321 /* Perform the write */
322 memcpy(data + offset, bh->buf, amount);
325 if (signal_pending(current))
326 return -EINTR; /* Interrupted!*/
327 amount_left_to_write -= amount;
328 fsg->common->residue -= amount;
330 /* Did the host decide to stop early? */
331 if (bh->outreq->actual != bh->outreq->length) {
332 fsg->common->short_packet_received = 1;
338 /* Wait for something to happen */
339 rc = sleep_thread(fsg->common);
347 static inline void utp_set_sense(struct fsg_dev *fsg, u16 code, u64 reply)
349 UTP_CTX(fsg)->processed = true;
350 UTP_CTX(fsg)->sdinfo = reply & 0xFFFFFFFF;
351 UTP_CTX(fsg)->sdinfo_h = (reply >> 32) & 0xFFFFFFFF;
352 UTP_CTX(fsg)->sd = (UTP_SENSE_KEY << 16) | code;
355 static void utp_poll(struct fsg_dev *fsg)
357 struct utp_context *ctx = UTP_CTX(fsg);
358 struct utp_user_data *uud = NULL;
360 mutex_lock(&ctx->lock);
361 if (!list_empty(&ctx->write))
362 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
363 mutex_unlock(&ctx->lock);
366 if (uud->data.flags & UTP_FLAG_STATUS) {
367 printk(KERN_WARNING "%s: exit with status %d\n",
368 __func__, uud->data.status);
369 UTP_SS_EXIT(fsg, uud->data.status);
370 } else if (uud->data.flags & UTP_FLAG_REPORT_BUSY) {
371 UTP_SS_BUSY(fsg, --ctx->counter);
373 printk("%s: pass returned.\n", __func__);
376 utp_user_data_free(uud);
378 if (utp_context.cur_state & UTP_FLAG_DATA) {
379 if (count_list(&ctx->read) < 7) {
380 pr_debug("%s: pass returned in POLL stage. \n", __func__);
382 utp_context.cur_state = 0;
386 UTP_SS_BUSY(fsg, --ctx->counter);
390 static int utp_exec(struct fsg_dev *fsg,
393 unsigned long long payload)
395 struct utp_user_data *uud = NULL, *uud2r;
396 struct utp_context *ctx = UTP_CTX(fsg);
398 ctx->counter = 0xFFFF;
399 uud2r = utp_user_data_alloc(cmdsize + 1);
402 uud2r->data.flags = UTP_FLAG_COMMAND;
403 uud2r->data.payload = payload;
404 strncpy(uud2r->data.command, command, cmdsize);
406 mutex_lock(&ctx->lock);
407 list_add_tail(&uud2r->link, &ctx->read);
408 mutex_unlock(&ctx->lock);
409 /* wake up the read routine */
412 if (command[0] == '!') /* there will be no response */
416 * the user program (uuc) will return utp_message
417 * and add list to write list
419 WAIT_ACTIVITY(write);
421 mutex_lock(&ctx->lock);
422 if (!list_empty(&ctx->write)) {
423 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
425 pr_info("UUD:\n\tFlags = %02X\n", uud->data.flags);
426 if (uud->data.flags & UTP_FLAG_DATA) {
427 pr_info("\tbufsize = %d\n", uud->data.bufsize);
428 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_NONE,
429 16, 2, uud->data.data, uud->data.bufsize, true);
431 if (uud->data.flags & UTP_FLAG_REPORT_BUSY)
435 mutex_unlock(&ctx->lock);
437 if (uud->data.flags & UTP_FLAG_DATA) {
438 memcpy(ctx->buffer, uud->data.data, uud->data.bufsize);
439 UTP_SS_SIZE(fsg, uud->data.bufsize);
440 } else if (uud->data.flags & UTP_FLAG_REPORT_BUSY) {
441 UTP_SS_BUSY(fsg, ctx->counter);
442 } else if (uud->data.flags & UTP_FLAG_STATUS) {
443 printk(KERN_WARNING "%s: exit with status %d\n", __func__,
445 UTP_SS_EXIT(fsg, uud->data.status);
447 pr_debug("%s: pass returned in EXEC stage. \n", __func__);
450 utp_user_data_free(uud);
454 static int utp_send_status(struct fsg_dev *fsg)
456 struct fsg_buffhd *bh;
457 u8 status = US_BULK_STAT_OK;
458 struct bulk_cs_wrap *csw;
461 /* Wait for the next buffer to become available */
462 bh = fsg->common->next_buffhd_to_fill;
463 while (bh->state != BUF_STATE_EMPTY) {
464 rc = sleep_thread(fsg->common);
469 if (fsg->common->phase_error) {
470 DBG(fsg, "sending phase-error status\n");
471 status = US_BULK_STAT_PHASE;
473 } else if ((UTP_CTX(fsg)->sd & 0xFFFF) != UTP_REPLY_PASS) {
474 status = US_BULK_STAT_FAIL;
479 /* Store and send the Bulk-only CSW */
480 csw->Signature = __constant_cpu_to_le32(US_BULK_CS_SIGN);
481 csw->Tag = fsg->common->tag;
482 csw->Residue = cpu_to_le32(fsg->common->residue);
483 csw->Status = status;
485 bh->inreq->length = US_BULK_CS_WRAP_LEN;
487 start_transfer(fsg, fsg->bulk_in, bh->inreq,
488 &bh->inreq_busy, &bh->state);
489 fsg->common->next_buffhd_to_fill = bh->next;
493 static int utp_handle_message(struct fsg_dev *fsg,
497 struct utp_msg *m = (struct utp_msg *)cdb_data;
500 struct utp_user_data *uud2r;
501 unsigned long long param;
505 return default_reply;
507 tag = get_unaligned_be32((void *)&m->utp_msg_tag);
508 param = get_be64((void *)&m->param);
509 pr_debug("Type 0x%x, tag 0x%08lx, param %llx\n",
510 m->utp_msg_type, tag, param);
512 switch ((enum utp_msg_type)m->utp_msg_type) {
515 if (get_be64((void *)&m->param) == 1) {
516 pr_debug("%s: version request\n", __func__);
517 UTP_SS_EXIT(fsg, UTP_CTX(fsg)->utp_version);
523 pr_debug("%s: EXEC\n", __func__);
524 data = vmalloc(fsg->common->data_size);
525 memset(data, 0, fsg->common->data_size);
526 /* copy data from usb buffer to utp buffer */
527 utp_do_write(fsg, data, fsg->common->data_size);
528 utp_exec(fsg, data, fsg->common->data_size, param);
531 case UTP_GET: /* data from device to host */
532 pr_debug("%s: GET, %d bytes\n", __func__,
533 fsg->common->data_size);
534 r = utp_do_read(fsg, UTP_CTX(fsg)->buffer,
535 fsg->common->data_size);
539 utp_context.cur_state = UTP_FLAG_DATA;
540 pr_debug("%s: PUT, Received %d bytes\n", __func__, fsg->common->data_size);/* data from host to device */
541 uud2r = utp_user_data_alloc(fsg->common->data_size);
544 uud2r->data.bufsize = fsg->common->data_size;
545 uud2r->data.flags = UTP_FLAG_DATA;
546 utp_do_write(fsg, uud2r->data.data, fsg->common->data_size);
547 /* don't know what will be written */
548 mutex_lock(&UTP_CTX(fsg)->lock);
549 list_add_tail(&uud2r->link, &UTP_CTX(fsg)->read);
550 mutex_unlock(&UTP_CTX(fsg)->lock);
551 wake_up(&UTP_CTX(fsg)->wq);
553 * Return PASS or FAIL according to uuc's status
554 * Please open it if need to check uuc's status
555 * and use another version uuc
558 struct utp_user_data *uud = NULL;
559 struct utp_context *ctx;
560 WAIT_ACTIVITY(write);
562 mutex_lock(&ctx->lock);
564 if (!list_empty(&ctx->write))
565 uud = list_first_entry(&ctx->write,
566 struct utp_user_data, link);
568 mutex_unlock(&ctx->lock);
570 if (uud->data.flags & UTP_FLAG_STATUS) {
571 printk(KERN_WARNING "%s: exit with status %d\n",
572 __func__, uud->data.status);
573 UTP_SS_EXIT(fsg, uud->data.status);
575 pr_debug("%s: pass\n", __func__);
578 utp_user_data_free(uud);
583 if (count_list(&UTP_CTX(fsg)->read) < 7) {
584 utp_context.cur_state = 0;
587 UTP_SS_BUSY(fsg, UTP_CTX(fsg)->counter);
592 utp_send_status(fsg);