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 = kzalloc(size + sizeof(*uud), GFP_KERNEL);
54 uud->data.size = size + sizeof(uud->data);
55 INIT_LIST_HEAD(&uud->link);
59 static void utp_user_data_free(struct utp_user_data *uud)
61 mutex_lock(&utp_context.lock);
63 mutex_unlock(&utp_context.lock);
67 /* Get the number of element for list */
68 static u32 count_list(struct list_head *l)
71 struct list_head *tmp;
73 mutex_lock(&utp_context.lock);
74 list_for_each(tmp, l) {
77 mutex_unlock(&utp_context.lock);
81 /* The routine will not go on if utp_context.queue is empty */
82 #define WAIT_ACTIVITY(queue) \
83 wait_event_interruptible(utp_context.wq, !list_empty(&utp_context.queue))
85 /* Called by userspace program (uuc) */
86 static ssize_t utp_file_read(struct file *file,
91 struct utp_user_data *uud;
97 mutex_lock(&utp_context.lock);
98 uud = list_first_entry(&utp_context.read, struct utp_user_data, link);
99 mutex_unlock(&utp_context.lock);
100 size_to_put = uud->data.size;
102 if (size >= size_to_put)
104 if (copy_to_user(buf, &uud->data, size_to_put))
107 utp_user_data_free(uud);
109 pr_info("sizeof = %d, size = %d\n",
113 pr_err("Will not free utp_user_data, because buffer size = %d,"
114 "need to put %d\n", size, size_to_put);
118 * The user program has already finished data process,
119 * go on getting data from the host
121 wake_up(&utp_context.list_full_wq);
126 static ssize_t utp_file_write(struct file *file, const char __user *buf,
127 size_t size, loff_t *off)
129 struct utp_user_data *uud;
131 if (size < sizeof(uud->data))
133 uud = utp_user_data_alloc(size);
134 if (copy_from_user(&uud->data, buf, size))
136 mutex_lock(&utp_context.lock);
137 list_add_tail(&uud->link, &utp_context.write);
138 /* Go on EXEC routine process */
139 wake_up(&utp_context.wq);
140 mutex_unlock(&utp_context.lock);
145 * uuc should change to use soc bus infrastructure to soc information
146 * /sys/devices/soc0/soc_id
147 * this function can be removed.
150 utp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
156 return put_user(cpu_id, (int __user *)arg);
162 /* Will be called when the host wants to get the sense data */
163 static int utp_get_sense(struct fsg_dev *fsg)
165 if (UTP_CTX(fsg)->processed == 0)
168 UTP_CTX(fsg)->processed = 0;
172 static int utp_do_read(struct fsg_dev *fsg, void *data, size_t size)
174 struct fsg_buffhd *bh;
179 /* Get the starting Logical Block Address and check that it's
183 if (unlikely(amount_left == 0))
184 return -EIO; /* No default reply*/
186 pr_debug("%s: sending %d\n", __func__, size);
188 /* Figure out how much we need to read:
189 * Try to read the remaining amount.
190 * But don't read more than the buffer size.
191 * And don't try to read past the end of the file.
192 * Finally, if we're not at a page boundary, don't read past
194 * If this means reading 0 then we were asked to read past
195 * the end of file. */
196 amount = min((unsigned int) amount_left, FSG_BUFLEN);
198 /* Wait for the next buffer to become available */
199 bh = fsg->common->next_buffhd_to_fill;
200 while (bh->state != BUF_STATE_EMPTY) {
201 rc = sleep_thread(fsg->common);
206 /* If we were asked to read past the end of file,
207 * end with an empty buffer. */
209 bh->inreq->length = 0;
210 bh->state = BUF_STATE_FULL;
214 /* Perform the read */
215 pr_info("Copied to %p, %d bytes started from %d\n",
216 bh->buf, amount, size - amount_left);
217 /* from upt buffer to file_storeage buffer */
218 memcpy(bh->buf, data + size - amount_left, amount);
219 amount_left -= amount;
220 fsg->common->residue -= amount;
222 bh->inreq->length = amount;
223 bh->state = BUF_STATE_FULL;
225 /* Send this buffer and go read some more */
228 /* USB Physical transfer: Data from device to host */
229 start_transfer(fsg, fsg->bulk_in, bh->inreq,
230 &bh->inreq_busy, &bh->state);
232 fsg->common->next_buffhd_to_fill = bh->next;
234 if (amount_left <= 0)
238 return size - amount_left;
241 static int utp_do_write(struct fsg_dev *fsg, void *data, size_t size)
243 struct fsg_buffhd *bh;
245 u32 amount_left_to_req, amount_left_to_write;
250 /* Carry out the file writes */
252 amount_left_to_req = amount_left_to_write = size;
254 if (unlikely(amount_left_to_write == 0))
258 while (amount_left_to_write > 0) {
260 /* Queue a request for more data from the host */
261 bh = fsg->common->next_buffhd_to_fill;
262 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
264 /* Figure out how much we want to get:
265 * Try to get the remaining amount.
266 * But don't get more than the buffer size.
267 * And don't try to go past the end of the file.
268 * If we're not at a page boundary,
269 * don't go past the next page.
270 * If this means getting 0, then we were asked
271 * to write past the end of file.
272 * Finally, round down to a block boundary. */
273 amount = min(amount_left_to_req, FSG_BUFLEN);
281 /* Get the next buffer */
282 amount_left_to_req -= amount;
283 if (amount_left_to_req == 0)
286 /* amount is always divisible by 512, hence by
287 * the bulk-out maxpacket size */
288 bh->outreq->length = bh->bulk_out_intended_length =
290 bh->outreq->short_not_ok = 1;
291 start_transfer(fsg, fsg->bulk_out, bh->outreq,
292 &bh->outreq_busy, &bh->state);
293 fsg->common->next_buffhd_to_fill = bh->next;
297 /* Write the received data to the backing file */
298 bh = fsg->common->next_buffhd_to_drain;
299 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
300 break; /* We stopped early */
301 if (bh->state == BUF_STATE_FULL) {
303 fsg->common->next_buffhd_to_drain = bh->next;
304 bh->state = BUF_STATE_EMPTY;
306 /* Did something go wrong with the transfer? */
307 if (bh->outreq->status != 0)
308 /* cry again, COMMUNICATION_FAILURE */
311 amount = bh->outreq->actual;
313 /* Perform the write */
314 memcpy(data + offset, bh->buf, amount);
317 if (signal_pending(current))
318 return -EINTR; /* Interrupted!*/
319 amount_left_to_write -= amount;
320 fsg->common->residue -= amount;
322 /* Did the host decide to stop early? */
323 if (bh->outreq->actual != bh->outreq->length) {
324 fsg->common->short_packet_received = 1;
330 /* Wait for something to happen */
331 rc = sleep_thread(fsg->common);
339 static inline void utp_set_sense(struct fsg_dev *fsg, u16 code, u64 reply)
341 UTP_CTX(fsg)->processed = true;
342 UTP_CTX(fsg)->sdinfo = reply & 0xFFFFFFFF;
343 UTP_CTX(fsg)->sdinfo_h = (reply >> 32) & 0xFFFFFFFF;
344 UTP_CTX(fsg)->sd = (UTP_SENSE_KEY << 16) | code;
347 static void utp_poll(struct fsg_dev *fsg)
349 struct utp_context *ctx = UTP_CTX(fsg);
350 struct utp_user_data *uud = NULL;
352 mutex_lock(&ctx->lock);
353 if (!list_empty(&ctx->write))
354 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
355 mutex_unlock(&ctx->lock);
358 if (uud->data.flags & UTP_FLAG_STATUS) {
359 printk(KERN_WARNING "%s: exit with status %d\n",
360 __func__, uud->data.status);
361 UTP_SS_EXIT(fsg, uud->data.status);
362 } else if (uud->data.flags & UTP_FLAG_REPORT_BUSY) {
363 UTP_SS_BUSY(fsg, --ctx->counter);
365 printk("%s: pass returned.\n", __func__);
368 utp_user_data_free(uud);
370 if (utp_context.cur_state & UTP_FLAG_DATA) {
371 if (count_list(&ctx->read) < 7) {
372 pr_debug("%s: pass returned in POLL stage. \n", __func__);
374 utp_context.cur_state = 0;
378 UTP_SS_BUSY(fsg, --ctx->counter);
382 static int utp_exec(struct fsg_dev *fsg,
385 unsigned long long payload)
387 struct utp_user_data *uud = NULL, *uud2r;
388 struct utp_context *ctx = UTP_CTX(fsg);
390 ctx->counter = 0xFFFF;
391 uud2r = utp_user_data_alloc(cmdsize + 1);
392 uud2r->data.flags = UTP_FLAG_COMMAND;
393 uud2r->data.payload = payload;
394 strncpy(uud2r->data.command, command, cmdsize);
396 mutex_lock(&ctx->lock);
397 list_add_tail(&uud2r->link, &ctx->read);
398 mutex_unlock(&ctx->lock);
399 /* wake up the read routine */
402 if (command[0] == '!') /* there will be no response */
406 * the user program (uuc) will return utp_message
407 * and add list to write list
409 WAIT_ACTIVITY(write);
411 mutex_lock(&ctx->lock);
412 if (!list_empty(&ctx->write)) {
413 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
415 pr_info("UUD:\n\tFlags = %02X\n", uud->data.flags);
416 if (uud->data.flags & UTP_FLAG_DATA) {
417 pr_info("\tbufsize = %d\n", uud->data.bufsize);
418 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_NONE,
419 16, 2, uud->data.data, uud->data.bufsize, true);
421 if (uud->data.flags & UTP_FLAG_REPORT_BUSY)
425 mutex_unlock(&ctx->lock);
427 if (uud->data.flags & UTP_FLAG_DATA) {
428 memcpy(ctx->buffer, uud->data.data, uud->data.bufsize);
429 UTP_SS_SIZE(fsg, uud->data.bufsize);
430 } else if (uud->data.flags & UTP_FLAG_REPORT_BUSY) {
431 UTP_SS_BUSY(fsg, ctx->counter);
432 } else if (uud->data.flags & UTP_FLAG_STATUS) {
433 printk(KERN_WARNING "%s: exit with status %d\n", __func__,
435 UTP_SS_EXIT(fsg, uud->data.status);
437 pr_debug("%s: pass returned in EXEC stage. \n", __func__);
440 utp_user_data_free(uud);
444 static int utp_send_status(struct fsg_dev *fsg)
446 struct fsg_buffhd *bh;
447 u8 status = US_BULK_STAT_OK;
448 struct bulk_cs_wrap *csw;
451 /* Wait for the next buffer to become available */
452 bh = fsg->common->next_buffhd_to_fill;
453 while (bh->state != BUF_STATE_EMPTY) {
454 rc = sleep_thread(fsg->common);
459 if (fsg->common->phase_error) {
460 DBG(fsg, "sending phase-error status\n");
461 status = US_BULK_STAT_PHASE;
463 } else if ((UTP_CTX(fsg)->sd & 0xFFFF) != UTP_REPLY_PASS) {
464 status = US_BULK_STAT_FAIL;
469 /* Store and send the Bulk-only CSW */
470 csw->Signature = __constant_cpu_to_le32(US_BULK_CS_SIGN);
471 csw->Tag = fsg->common->tag;
472 csw->Residue = cpu_to_le32(fsg->common->residue);
473 csw->Status = status;
475 bh->inreq->length = US_BULK_CS_WRAP_LEN;
477 start_transfer(fsg, fsg->bulk_in, bh->inreq,
478 &bh->inreq_busy, &bh->state);
479 fsg->common->next_buffhd_to_fill = bh->next;
483 static int utp_handle_message(struct fsg_dev *fsg,
487 struct utp_msg *m = (struct utp_msg *)cdb_data;
490 struct utp_user_data *uud2r;
491 unsigned long long param;
495 return default_reply;
497 tag = get_unaligned_be32((void *)&m->utp_msg_tag);
498 param = get_be64((void *)&m->param);
499 pr_debug("Type 0x%x, tag 0x%08lx, param %llx\n",
500 m->utp_msg_type, tag, param);
502 switch ((enum utp_msg_type)m->utp_msg_type) {
505 if (get_be64((void *)&m->param) == 1) {
506 pr_debug("%s: version request\n", __func__);
507 UTP_SS_EXIT(fsg, UTP_CTX(fsg)->utp_version);
513 pr_debug("%s: EXEC\n", __func__);
514 data = kzalloc(fsg->common->data_size, GFP_KERNEL);
515 /* copy data from usb buffer to utp buffer */
516 utp_do_write(fsg, data, fsg->common->data_size);
517 utp_exec(fsg, data, fsg->common->data_size, param);
520 case UTP_GET: /* data from device to host */
521 pr_debug("%s: GET, %d bytes\n", __func__,
522 fsg->common->data_size);
523 r = utp_do_read(fsg, UTP_CTX(fsg)->buffer,
524 fsg->common->data_size);
528 utp_context.cur_state = UTP_FLAG_DATA;
529 pr_debug("%s: PUT, Received %d bytes\n", __func__, fsg->common->data_size);/* data from host to device */
530 uud2r = utp_user_data_alloc(fsg->common->data_size);
533 uud2r->data.bufsize = fsg->common->data_size;
534 uud2r->data.flags = UTP_FLAG_DATA;
535 utp_do_write(fsg, uud2r->data.data, fsg->common->data_size);
536 /* don't know what will be written */
537 mutex_lock(&UTP_CTX(fsg)->lock);
538 list_add_tail(&uud2r->link, &UTP_CTX(fsg)->read);
539 mutex_unlock(&UTP_CTX(fsg)->lock);
540 wake_up(&UTP_CTX(fsg)->wq);
542 * Return PASS or FAIL according to uuc's status
543 * Please open it if need to check uuc's status
544 * and use another version uuc
547 struct utp_user_data *uud = NULL;
548 struct utp_context *ctx;
549 WAIT_ACTIVITY(write);
551 mutex_lock(&ctx->lock);
553 if (!list_empty(&ctx->write))
554 uud = list_first_entry(&ctx->write,
555 struct utp_user_data, link);
557 mutex_unlock(&ctx->lock);
559 if (uud->data.flags & UTP_FLAG_STATUS) {
560 printk(KERN_WARNING "%s: exit with status %d\n",
561 __func__, uud->data.status);
562 UTP_SS_EXIT(fsg, uud->data.status);
564 pr_debug("%s: pass\n", __func__);
567 utp_user_data_free(uud);
572 if (count_list(&UTP_CTX(fsg)->read) < 7) {
573 utp_context.cur_state = 0;
576 UTP_SS_BUSY(fsg, UTP_CTX(fsg)->counter);
581 utp_send_status(fsg);