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 list_for_each(tmp, l) {
79 /* The routine will not go on if utp_context.queue is empty */
80 #define WAIT_ACTIVITY(queue) \
81 wait_event_interruptible(utp_context.wq, !list_empty(&utp_context.queue))
83 /* Called by userspace program (uuc) */
84 static ssize_t utp_file_read(struct file *file,
89 struct utp_user_data *uud;
95 mutex_lock(&utp_context.lock);
96 uud = list_first_entry(&utp_context.read, struct utp_user_data, link);
97 mutex_unlock(&utp_context.lock);
98 size_to_put = uud->data.size;
100 if (size >= size_to_put)
102 if (copy_to_user(buf, &uud->data, size_to_put))
105 utp_user_data_free(uud);
107 pr_info("sizeof = %d, size = %d\n",
111 pr_err("Will not free utp_user_data, because buffer size = %d,"
112 "need to put %d\n", size, size_to_put);
116 * The user program has already finished data process,
117 * go on getting data from the host
119 wake_up(&utp_context.list_full_wq);
124 static ssize_t utp_file_write(struct file *file, const char __user *buf,
125 size_t size, loff_t *off)
127 struct utp_user_data *uud;
129 if (size < sizeof(uud->data))
131 uud = utp_user_data_alloc(size);
132 if (copy_from_user(&uud->data, buf, size))
134 mutex_lock(&utp_context.lock);
135 list_add_tail(&uud->link, &utp_context.write);
136 /* Go on EXEC routine process */
137 wake_up(&utp_context.wq);
138 mutex_unlock(&utp_context.lock);
143 * uuc should change to use soc bus infrastructure to soc information
144 * /sys/devices/soc0/soc_id
145 * this function can be removed.
148 utp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
154 return put_user(cpu_id, (int __user *)arg);
160 /* Will be called when the host wants to get the sense data */
161 static int utp_get_sense(struct fsg_dev *fsg)
163 if (UTP_CTX(fsg)->processed == 0)
166 UTP_CTX(fsg)->processed = 0;
170 static int utp_do_read(struct fsg_dev *fsg, void *data, size_t size)
172 struct fsg_buffhd *bh;
177 /* Get the starting Logical Block Address and check that it's
181 if (unlikely(amount_left == 0))
182 return -EIO; /* No default reply*/
184 pr_debug("%s: sending %d\n", __func__, size);
186 /* Figure out how much we need to read:
187 * Try to read the remaining amount.
188 * But don't read more than the buffer size.
189 * And don't try to read past the end of the file.
190 * Finally, if we're not at a page boundary, don't read past
192 * If this means reading 0 then we were asked to read past
193 * the end of file. */
194 amount = min((unsigned int) amount_left, FSG_BUFLEN);
196 /* Wait for the next buffer to become available */
197 bh = fsg->common->next_buffhd_to_fill;
198 while (bh->state != BUF_STATE_EMPTY) {
199 rc = sleep_thread(fsg->common);
204 /* If we were asked to read past the end of file,
205 * end with an empty buffer. */
207 bh->inreq->length = 0;
208 bh->state = BUF_STATE_FULL;
212 /* Perform the read */
213 pr_info("Copied to %p, %d bytes started from %d\n",
214 bh->buf, amount, size - amount_left);
215 /* from upt buffer to file_storeage buffer */
216 memcpy(bh->buf, data + size - amount_left, amount);
217 amount_left -= amount;
218 fsg->common->residue -= amount;
220 bh->inreq->length = amount;
221 bh->state = BUF_STATE_FULL;
223 /* Send this buffer and go read some more */
226 /* USB Physical transfer: Data from device to host */
227 start_transfer(fsg, fsg->bulk_in, bh->inreq,
228 &bh->inreq_busy, &bh->state);
230 fsg->common->next_buffhd_to_fill = bh->next;
232 if (amount_left <= 0)
236 return size - amount_left;
239 static int utp_do_write(struct fsg_dev *fsg, void *data, size_t size)
241 struct fsg_buffhd *bh;
243 u32 amount_left_to_req, amount_left_to_write;
248 /* Carry out the file writes */
250 amount_left_to_req = amount_left_to_write = size;
252 if (unlikely(amount_left_to_write == 0))
256 while (amount_left_to_write > 0) {
258 /* Queue a request for more data from the host */
259 bh = fsg->common->next_buffhd_to_fill;
260 if (bh->state == BUF_STATE_EMPTY && get_some_more) {
262 /* Figure out how much we want to get:
263 * Try to get the remaining amount.
264 * But don't get more than the buffer size.
265 * And don't try to go past the end of the file.
266 * If we're not at a page boundary,
267 * don't go past the next page.
268 * If this means getting 0, then we were asked
269 * to write past the end of file.
270 * Finally, round down to a block boundary. */
271 amount = min(amount_left_to_req, FSG_BUFLEN);
279 /* Get the next buffer */
280 amount_left_to_req -= amount;
281 if (amount_left_to_req == 0)
284 /* amount is always divisible by 512, hence by
285 * the bulk-out maxpacket size */
286 bh->outreq->length = bh->bulk_out_intended_length =
288 bh->outreq->short_not_ok = 1;
289 start_transfer(fsg, fsg->bulk_out, bh->outreq,
290 &bh->outreq_busy, &bh->state);
291 fsg->common->next_buffhd_to_fill = bh->next;
295 /* Write the received data to the backing file */
296 bh = fsg->common->next_buffhd_to_drain;
297 if (bh->state == BUF_STATE_EMPTY && !get_some_more)
298 break; /* We stopped early */
299 if (bh->state == BUF_STATE_FULL) {
301 fsg->common->next_buffhd_to_drain = bh->next;
302 bh->state = BUF_STATE_EMPTY;
304 /* Did something go wrong with the transfer? */
305 if (bh->outreq->status != 0)
306 /* cry again, COMMUNICATION_FAILURE */
309 amount = bh->outreq->actual;
311 /* Perform the write */
312 memcpy(data + offset, bh->buf, amount);
315 if (signal_pending(current))
316 return -EINTR; /* Interrupted!*/
317 amount_left_to_write -= amount;
318 fsg->common->residue -= amount;
320 /* Did the host decide to stop early? */
321 if (bh->outreq->actual != bh->outreq->length) {
322 fsg->common->short_packet_received = 1;
328 /* Wait for something to happen */
329 rc = sleep_thread(fsg->common);
337 static inline void utp_set_sense(struct fsg_dev *fsg, u16 code, u64 reply)
339 UTP_CTX(fsg)->processed = true;
340 UTP_CTX(fsg)->sdinfo = reply & 0xFFFFFFFF;
341 UTP_CTX(fsg)->sdinfo_h = (reply >> 32) & 0xFFFFFFFF;
342 UTP_CTX(fsg)->sd = (UTP_SENSE_KEY << 16) | code;
345 static void utp_poll(struct fsg_dev *fsg)
347 struct utp_context *ctx = UTP_CTX(fsg);
348 struct utp_user_data *uud = NULL;
350 mutex_lock(&ctx->lock);
351 if (!list_empty(&ctx->write))
352 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
353 mutex_unlock(&ctx->lock);
356 if (uud->data.flags & UTP_FLAG_STATUS) {
357 printk(KERN_WARNING "%s: exit with status %d\n",
358 __func__, uud->data.status);
359 UTP_SS_EXIT(fsg, uud->data.status);
361 pr_debug("%s: pass\n", __func__);
364 utp_user_data_free(uud);
366 pr_debug("%s: still busy...\n", __func__);
367 UTP_SS_BUSY(fsg, --ctx->counter);
371 static int utp_exec(struct fsg_dev *fsg,
374 unsigned long long payload)
376 struct utp_user_data *uud = NULL, *uud2r;
377 struct utp_context *ctx = UTP_CTX(fsg);
379 uud2r = utp_user_data_alloc(cmdsize + 1);
380 uud2r->data.flags = UTP_FLAG_COMMAND;
381 uud2r->data.payload = payload;
382 strncpy(uud2r->data.command, command, cmdsize);
384 mutex_lock(&ctx->lock);
385 list_add_tail(&uud2r->link, &ctx->read);
386 mutex_unlock(&ctx->lock);
387 /* wake up the read routine */
390 if (command[0] == '!') /* there will be no response */
394 * the user program (uuc) will return utp_message
395 * and add list to write list
397 WAIT_ACTIVITY(write);
399 mutex_lock(&ctx->lock);
400 if (!list_empty(&ctx->write)) {
401 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
403 pr_info("UUD:\n\tFlags = %02X\n", uud->data.flags);
404 if (uud->data.flags & UTP_FLAG_DATA) {
405 pr_info("\tbufsize = %d\n", uud->data.bufsize);
406 print_hex_dump(KERN_DEBUG, "\t", DUMP_PREFIX_NONE,
407 16, 2, uud->data.data, uud->data.bufsize, true);
409 if (uud->data.flags & UTP_FLAG_REPORT_BUSY)
413 mutex_unlock(&ctx->lock);
415 if (uud->data.flags & UTP_FLAG_DATA) {
416 memcpy(ctx->buffer, uud->data.data, uud->data.bufsize);
417 UTP_SS_SIZE(fsg, uud->data.bufsize);
418 } else if (uud->data.flags & UTP_FLAG_REPORT_BUSY) {
419 ctx->counter = 0xFFFF;
420 UTP_SS_BUSY(fsg, ctx->counter);
421 } else if (uud->data.flags & UTP_FLAG_STATUS) {
422 printk(KERN_WARNING "%s: exit with status %d\n", __func__,
424 UTP_SS_EXIT(fsg, uud->data.status);
426 pr_debug("%s: pass\n", __func__);
429 utp_user_data_free(uud);
433 static int utp_send_status(struct fsg_dev *fsg)
435 struct fsg_buffhd *bh;
436 u8 status = US_BULK_STAT_OK;
437 struct bulk_cs_wrap *csw;
440 /* Wait for the next buffer to become available */
441 bh = fsg->common->next_buffhd_to_fill;
442 while (bh->state != BUF_STATE_EMPTY) {
443 rc = sleep_thread(fsg->common);
448 if (fsg->common->phase_error) {
449 DBG(fsg, "sending phase-error status\n");
450 status = US_BULK_STAT_PHASE;
452 } else if ((UTP_CTX(fsg)->sd & 0xFFFF) != UTP_REPLY_PASS) {
453 status = US_BULK_STAT_FAIL;
458 /* Store and send the Bulk-only CSW */
459 csw->Signature = __constant_cpu_to_le32(US_BULK_CS_SIGN);
460 csw->Tag = fsg->common->tag;
461 csw->Residue = cpu_to_le32(fsg->common->residue);
462 csw->Status = status;
464 bh->inreq->length = US_BULK_CS_WRAP_LEN;
466 start_transfer(fsg, fsg->bulk_in, bh->inreq,
467 &bh->inreq_busy, &bh->state);
468 fsg->common->next_buffhd_to_fill = bh->next;
472 static int utp_handle_message(struct fsg_dev *fsg,
476 struct utp_msg *m = (struct utp_msg *)cdb_data;
479 struct utp_user_data *uud2r;
480 unsigned long long param;
484 return default_reply;
486 tag = get_unaligned_be32((void *)&m->utp_msg_tag);
487 param = get_be64((void *)&m->param);
488 pr_debug("Type 0x%x, tag 0x%08lx, param %llx\n",
489 m->utp_msg_type, tag, param);
491 switch ((enum utp_msg_type)m->utp_msg_type) {
494 if (get_be64((void *)&m->param) == 1) {
495 pr_debug("%s: version request\n", __func__);
496 UTP_SS_EXIT(fsg, UTP_CTX(fsg)->utp_version);
502 pr_debug("%s: EXEC\n", __func__);
503 data = kzalloc(fsg->common->data_size, GFP_KERNEL);
504 /* copy data from usb buffer to utp buffer */
505 utp_do_write(fsg, data, fsg->common->data_size);
506 utp_exec(fsg, data, fsg->common->data_size, param);
509 case UTP_GET: /* data from device to host */
510 pr_debug("%s: GET, %d bytes\n", __func__,
511 fsg->common->data_size);
512 r = utp_do_read(fsg, UTP_CTX(fsg)->buffer,
513 fsg->common->data_size);
516 case UTP_PUT: /* data from host to device */
517 pr_debug("%s: PUT, %d bytes\n", __func__,
518 fsg->common->data_size);
519 uud2r = utp_user_data_alloc(fsg->common->data_size);
520 uud2r->data.bufsize = fsg->common->data_size;
521 uud2r->data.flags = UTP_FLAG_DATA;
522 utp_do_write(fsg, uud2r->data.data, fsg->common->data_size);
523 /* don't know what will be written */
524 mutex_lock(&UTP_CTX(fsg)->lock);
525 list_add_tail(&uud2r->link, &UTP_CTX(fsg)->read);
526 mutex_unlock(&UTP_CTX(fsg)->lock);
527 wake_up(&UTP_CTX(fsg)->wq);
529 * Return PASS or FAIL according to uuc's status
530 * Please open it if need to check uuc's status
531 * and use another version uuc
534 struct utp_user_data *uud = NULL;
535 struct utp_context *ctx;
536 WAIT_ACTIVITY(write);
538 mutex_lock(&ctx->lock);
540 if (!list_empty(&ctx->write))
541 uud = list_first_entry(&ctx->write,
542 struct utp_user_data, link);
544 mutex_unlock(&ctx->lock);
546 if (uud->data.flags & UTP_FLAG_STATUS) {
547 printk(KERN_WARNING "%s: exit with status %d\n",
548 __func__, uud->data.status);
549 UTP_SS_EXIT(fsg, uud->data.status);
551 pr_debug("%s: pass\n", __func__);
554 utp_user_data_free(uud);
561 wait_event_interruptible(UTP_CTX(fsg)->list_full_wq,
562 count_list(&UTP_CTX(fsg)->read) < 7);
566 utp_send_status(fsg);