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);
138 if (copy_from_user(&uud->data, buf, size)) {
139 printk(KERN_INFO "[ %s ] copy error!\n", __func__);
143 mutex_lock(&utp_context.lock);
144 list_add_tail(&uud->link, &utp_context.write);
145 /* Go on EXEC routine process */
146 wake_up(&utp_context.wq);
147 mutex_unlock(&utp_context.lock);
152 * uuc should change to use soc bus infrastructure to soc information
153 * /sys/devices/soc0/soc_id
154 * this function can be removed.
157 utp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
163 return put_user(cpu_id, (int __user *)arg);
169 /* Will be called when the host wants to get the sense data */
170 static int utp_get_sense(struct fsg_dev *fsg)
172 if (UTP_CTX(fsg)->processed == 0)
175 UTP_CTX(fsg)->processed = 0;
179 static int utp_do_read(struct fsg_dev *fsg, void *data, size_t size)
181 struct fsg_buffhd *bh;
186 /* Get the starting Logical Block Address and check that it's
190 if (unlikely(amount_left == 0))
191 return -EIO; /* No default reply*/
193 pr_debug("%s: sending %d\n", __func__, size);
195 /* Figure out how much we need to read:
196 * Try to read the remaining amount.
197 * But don't read more than the buffer size.
198 * And don't try to read past the end of the file.
199 * Finally, if we're not at a page boundary, don't read past
201 * If this means reading 0 then we were asked to read past
202 * the end of file. */
203 amount = min((unsigned int) amount_left, FSG_BUFLEN);
205 /* Wait for the next buffer to become available */
206 bh = fsg->common->next_buffhd_to_fill;
207 while (bh->state != BUF_STATE_EMPTY) {
208 rc = sleep_thread(fsg->common);
213 /* If we were asked to read past the end of file,
214 * end with an empty buffer. */
216 bh->inreq->length = 0;
217 bh->state = BUF_STATE_FULL;
221 /* Perform the read */
222 pr_info("Copied to %p, %d bytes started from %d\n",
223 bh->buf, amount, size - amount_left);
224 /* from upt buffer to file_storeage buffer */
225 memcpy(bh->buf, data + size - amount_left, amount);
226 amount_left -= amount;
227 fsg->common->residue -= amount;
229 bh->inreq->length = amount;
230 bh->state = BUF_STATE_FULL;
232 /* Send this buffer and go read some more */
235 /* USB Physical transfer: Data from device to host */
236 start_transfer(fsg, fsg->bulk_in, bh->inreq,
237 &bh->inreq_busy, &bh->state);
239 fsg->common->next_buffhd_to_fill = bh->next;
241 if (amount_left <= 0)
245 return size - amount_left;
248 static int utp_do_write(struct fsg_dev *fsg, void *data, size_t size)
250 struct fsg_buffhd *bh;
252 u32 amount_left_to_req, amount_left_to_write;
257 /* Carry out the file writes */
259 amount_left_to_req = amount_left_to_write = size;
261 if (unlikely(amount_left_to_write == 0))
265 while (amount_left_to_write > 0) {
267 /* Queue a request for more data from the host */
268 bh = fsg->common->next_buffhd_to_fill;
269 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
271 /* Figure out how much we want to get:
272 * Try to get the remaining amount.
273 * But don't get more than the buffer size.
274 * And don't try to go past the end of the file.
275 * If we're not at a page boundary,
276 * don't go past the next page.
277 * If this means getting 0, then we were asked
278 * to write past the end of file.
279 * Finally, round down to a block boundary. */
280 amount = min(amount_left_to_req, FSG_BUFLEN);
288 /* Get the next buffer */
289 amount_left_to_req -= amount;
290 if (amount_left_to_req == 0)
293 /* amount is always divisible by 512, hence by
294 * the bulk-out maxpacket size */
295 bh->outreq->length = bh->bulk_out_intended_length =
297 bh->outreq->short_not_ok = 1;
298 start_transfer(fsg, fsg->bulk_out, bh->outreq,
299 &bh->outreq_busy, &bh->state);
300 fsg->common->next_buffhd_to_fill = bh->next;
304 /* Write the received data to the backing file */
305 bh = fsg->common->next_buffhd_to_drain;
306 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
307 break; /* We stopped early */
308 if (bh->state == BUF_STATE_FULL) {
310 fsg->common->next_buffhd_to_drain = bh->next;
311 bh->state = BUF_STATE_EMPTY;
313 /* Did something go wrong with the transfer? */
314 if (bh->outreq->status != 0)
315 /* cry again, COMMUNICATION_FAILURE */
318 amount = bh->outreq->actual;
320 /* Perform the write */
321 memcpy(data + offset, bh->buf, amount);
324 if (signal_pending(current))
325 return -EINTR; /* Interrupted!*/
326 amount_left_to_write -= amount;
327 fsg->common->residue -= amount;
329 /* Did the host decide to stop early? */
330 if (bh->outreq->actual != bh->outreq->length) {
331 fsg->common->short_packet_received = 1;
337 /* Wait for something to happen */
338 rc = sleep_thread(fsg->common);
346 static inline void utp_set_sense(struct fsg_dev *fsg, u16 code, u64 reply)
348 UTP_CTX(fsg)->processed = true;
349 UTP_CTX(fsg)->sdinfo = reply & 0xFFFFFFFF;
350 UTP_CTX(fsg)->sdinfo_h = (reply >> 32) & 0xFFFFFFFF;
351 UTP_CTX(fsg)->sd = (UTP_SENSE_KEY << 16) | code;
354 static void utp_poll(struct fsg_dev *fsg)
356 struct utp_context *ctx = UTP_CTX(fsg);
357 struct utp_user_data *uud = NULL;
359 mutex_lock(&ctx->lock);
360 if (!list_empty(&ctx->write))
361 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
362 mutex_unlock(&ctx->lock);
365 if (uud->data.flags & UTP_FLAG_STATUS) {
366 printk(KERN_WARNING "%s: exit with status %d\n",
367 __func__, uud->data.status);
368 UTP_SS_EXIT(fsg, uud->data.status);
369 } else if (uud->data.flags & UTP_FLAG_REPORT_BUSY) {
370 UTP_SS_BUSY(fsg, --ctx->counter);
372 printk("%s: pass returned.\n", __func__);
375 utp_user_data_free(uud);
377 if (utp_context.cur_state & UTP_FLAG_DATA) {
378 if (count_list(&ctx->read) < 7) {
379 pr_debug("%s: pass returned in POLL stage. \n", __func__);
381 utp_context.cur_state = 0;
385 UTP_SS_BUSY(fsg, --ctx->counter);
389 static int utp_exec(struct fsg_dev *fsg,
392 unsigned long long payload)
394 struct utp_user_data *uud = NULL, *uud2r;
395 struct utp_context *ctx = UTP_CTX(fsg);
397 ctx->counter = 0xFFFF;
398 uud2r = utp_user_data_alloc(cmdsize + 1);
401 uud2r->data.flags = UTP_FLAG_COMMAND;
402 uud2r->data.payload = payload;
403 strncpy(uud2r->data.command, command, cmdsize);
405 mutex_lock(&ctx->lock);
406 list_add_tail(&uud2r->link, &ctx->read);
407 mutex_unlock(&ctx->lock);
408 /* wake up the read routine */
411 if (command[0] == '!') /* there will be no response */
415 * the user program (uuc) will return utp_message
416 * and add list to write list
418 WAIT_ACTIVITY(write);
420 mutex_lock(&ctx->lock);
421 if (!list_empty(&ctx->write)) {
422 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
424 pr_info("UUD:\n\tFlags = %02X\n", uud->data.flags);
425 if (uud->data.flags & UTP_FLAG_DATA) {
426 pr_info("\tbufsize = %d\n", uud->data.bufsize);
427 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_NONE,
428 16, 2, uud->data.data, uud->data.bufsize, true);
430 if (uud->data.flags & UTP_FLAG_REPORT_BUSY)
434 mutex_unlock(&ctx->lock);
436 if (uud->data.flags & UTP_FLAG_DATA) {
437 memcpy(ctx->buffer, uud->data.data, uud->data.bufsize);
438 UTP_SS_SIZE(fsg, uud->data.bufsize);
439 } else if (uud->data.flags & UTP_FLAG_REPORT_BUSY) {
440 UTP_SS_BUSY(fsg, ctx->counter);
441 } else if (uud->data.flags & UTP_FLAG_STATUS) {
442 printk(KERN_WARNING "%s: exit with status %d\n", __func__,
444 UTP_SS_EXIT(fsg, uud->data.status);
446 pr_debug("%s: pass returned in EXEC stage. \n", __func__);
449 utp_user_data_free(uud);
453 static int utp_send_status(struct fsg_dev *fsg)
455 struct fsg_buffhd *bh;
456 u8 status = US_BULK_STAT_OK;
457 struct bulk_cs_wrap *csw;
460 /* Wait for the next buffer to become available */
461 bh = fsg->common->next_buffhd_to_fill;
462 while (bh->state != BUF_STATE_EMPTY) {
463 rc = sleep_thread(fsg->common);
468 if (fsg->common->phase_error) {
469 DBG(fsg, "sending phase-error status\n");
470 status = US_BULK_STAT_PHASE;
472 } else if ((UTP_CTX(fsg)->sd & 0xFFFF) != UTP_REPLY_PASS) {
473 status = US_BULK_STAT_FAIL;
478 /* Store and send the Bulk-only CSW */
479 csw->Signature = __constant_cpu_to_le32(US_BULK_CS_SIGN);
480 csw->Tag = fsg->common->tag;
481 csw->Residue = cpu_to_le32(fsg->common->residue);
482 csw->Status = status;
484 bh->inreq->length = US_BULK_CS_WRAP_LEN;
486 start_transfer(fsg, fsg->bulk_in, bh->inreq,
487 &bh->inreq_busy, &bh->state);
488 fsg->common->next_buffhd_to_fill = bh->next;
492 static int utp_handle_message(struct fsg_dev *fsg,
496 struct utp_msg *m = (struct utp_msg *)cdb_data;
499 struct utp_user_data *uud2r;
500 unsigned long long param;
504 return default_reply;
506 tag = get_unaligned_be32((void *)&m->utp_msg_tag);
507 param = get_be64((void *)&m->param);
508 pr_debug("Type 0x%x, tag 0x%08lx, param %llx\n",
509 m->utp_msg_type, tag, param);
511 switch ((enum utp_msg_type)m->utp_msg_type) {
514 if (get_be64((void *)&m->param) == 1) {
515 pr_debug("%s: version request\n", __func__);
516 UTP_SS_EXIT(fsg, UTP_CTX(fsg)->utp_version);
522 pr_debug("%s: EXEC\n", __func__);
523 data = vmalloc(fsg->common->data_size);
524 memset(data, 0, fsg->common->data_size);
525 /* copy data from usb buffer to utp buffer */
526 utp_do_write(fsg, data, fsg->common->data_size);
527 utp_exec(fsg, data, fsg->common->data_size, param);
530 case UTP_GET: /* data from device to host */
531 pr_debug("%s: GET, %d bytes\n", __func__,
532 fsg->common->data_size);
533 r = utp_do_read(fsg, UTP_CTX(fsg)->buffer,
534 fsg->common->data_size);
538 utp_context.cur_state = UTP_FLAG_DATA;
539 pr_debug("%s: PUT, Received %d bytes\n", __func__, fsg->common->data_size);/* data from host to device */
540 uud2r = utp_user_data_alloc(fsg->common->data_size);
543 uud2r->data.bufsize = fsg->common->data_size;
544 uud2r->data.flags = UTP_FLAG_DATA;
545 utp_do_write(fsg, uud2r->data.data, fsg->common->data_size);
546 /* don't know what will be written */
547 mutex_lock(&UTP_CTX(fsg)->lock);
548 list_add_tail(&uud2r->link, &UTP_CTX(fsg)->read);
549 mutex_unlock(&UTP_CTX(fsg)->lock);
550 wake_up(&UTP_CTX(fsg)->wq);
552 * Return PASS or FAIL according to uuc's status
553 * Please open it if need to check uuc's status
554 * and use another version uuc
557 struct utp_user_data *uud = NULL;
558 struct utp_context *ctx;
559 WAIT_ACTIVITY(write);
561 mutex_lock(&ctx->lock);
563 if (!list_empty(&ctx->write))
564 uud = list_first_entry(&ctx->write,
565 struct utp_user_data, link);
567 mutex_unlock(&ctx->lock);
569 if (uud->data.flags & UTP_FLAG_STATUS) {
570 printk(KERN_WARNING "%s: exit with status %d\n",
571 __func__, uud->data.status);
572 UTP_SS_EXIT(fsg, uud->data.status);
574 pr_debug("%s: pass\n", __func__);
577 utp_user_data_free(uud);
582 if (count_list(&UTP_CTX(fsg)->read) < 7) {
583 utp_context.cur_state = 0;
586 UTP_SS_BUSY(fsg, UTP_CTX(fsg)->counter);
591 utp_send_status(fsg);