]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/function/fsl_updater.c
ENGR00161643-3 UTP : bugfix
[karo-tx-linux.git] / drivers / usb / gadget / function / fsl_updater.c
1 /*
2  * Freescale UUT driver
3  *
4  * Copyright 2008-2013 Freescale Semiconductor, Inc.
5  * Copyright 2008-2009 Embedded Alley Solutions, Inc All Rights Reserved.
6  */
7
8 /*
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:
12  *
13  * http://www.opensource.org/licenses/gpl-license.html
14  * http://www.gnu.org/copyleft/gpl.html
15  */
16
17 static u64 get_be64(u8 *buf)
18 {
19         return ((u64)get_unaligned_be32(buf) << 32) |
20                 get_unaligned_be32(buf + 4);
21 }
22
23 static int utp_init(struct fsg_dev *fsg)
24 {
25         init_waitqueue_head(&utp_context.wq);
26         init_waitqueue_head(&utp_context.list_full_wq);
27
28         INIT_LIST_HEAD(&utp_context.read);
29         INIT_LIST_HEAD(&utp_context.write);
30         mutex_init(&utp_context.lock);
31
32         /* the max message is 64KB */
33         utp_context.buffer = vmalloc(0x10000);
34         if (!utp_context.buffer)
35                 return -EIO;
36         utp_context.utp_version = 0x1ull;
37         fsg->utp = &utp_context;
38         return misc_register(&utp_dev);
39 }
40
41 static void utp_exit(struct fsg_dev *fsg)
42 {
43         vfree(utp_context.buffer);
44         misc_deregister(&utp_dev);
45 }
46
47 static struct utp_user_data *utp_user_data_alloc(size_t size)
48 {
49         struct utp_user_data *uud;
50
51         uud = vmalloc(size + sizeof(*uud));
52         if (!uud)
53                 return uud;
54         memset(uud, 0, size + sizeof(*uud));
55         uud->data.size = size + sizeof(uud->data);
56         INIT_LIST_HEAD(&uud->link);
57         return uud;
58 }
59
60 static void utp_user_data_free(struct utp_user_data *uud)
61 {
62         mutex_lock(&utp_context.lock);
63         list_del(&uud->link);
64         mutex_unlock(&utp_context.lock);
65         vfree(uud);
66 }
67
68 /* Get the number of element for list */
69 static u32 count_list(struct list_head *l)
70 {
71         u32 count = 0;
72         struct list_head *tmp;
73
74         mutex_lock(&utp_context.lock);
75         list_for_each(tmp, l) {
76                 count++;
77         }
78         mutex_unlock(&utp_context.lock);
79
80         return count;
81 }
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))
85
86 /* Called by userspace program (uuc) */
87 static ssize_t utp_file_read(struct file *file,
88                              char __user *buf,
89                              size_t size,
90                              loff_t *off)
91 {
92         struct utp_user_data *uud;
93         size_t size_to_put;
94         int free = 0;
95
96         WAIT_ACTIVITY(read);
97
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;
102
103         if (size >= size_to_put)
104                 free = !0;
105         if (copy_to_user(buf, &uud->data, size_to_put)) {
106                 printk(KERN_INFO "[ %s ] copy error\n", __func__);
107                 return -EACCES;
108         }
109         if (free)
110                 utp_user_data_free(uud);
111         else {
112                 pr_info("sizeof = %d, size = %d\n",
113                         sizeof(uud->data),
114                         uud->data.size);
115
116                 pr_err("Will not free utp_user_data, because buffer size = %d,"
117                         "need to put %d\n", size, size_to_put);
118         }
119
120         /*
121          * The user program has already finished data process,
122          * go on getting data from the host
123          */
124         wake_up(&utp_context.list_full_wq);
125
126         return size_to_put;
127 }
128
129 static ssize_t utp_file_write(struct file *file, const char __user *buf,
130                                 size_t size, loff_t *off)
131 {
132         struct utp_user_data *uud;
133
134         if (size < sizeof(uud->data))
135                 return -EINVAL;
136         uud = utp_user_data_alloc(size);
137         if (uud == NULL)
138                 return -ENOMEM;
139         if (copy_from_user(&uud->data, buf, size)) {
140                 printk(KERN_INFO "[ %s ] copy error!\n", __func__);
141                 vfree(uud);
142                 return -EACCES;
143         }
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);
149         return size;
150 }
151
152 /*
153  * uuc should change to use soc bus infrastructure to soc information
154  * /sys/devices/soc0/soc_id
155  * this function can be removed.
156  */
157 static long
158 utp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
159 {
160         int cpu_id = 0;
161
162         switch (cmd) {
163         case UTP_GET_CPU_ID:
164                 return put_user(cpu_id, (int __user *)arg);
165         default:
166                 return -ENOIOCTLCMD;
167         }
168 }
169
170 /* Will be called when the host wants to get the sense data */
171 static int utp_get_sense(struct fsg_dev *fsg)
172 {
173         if (UTP_CTX(fsg)->processed == 0)
174                 return -1;
175
176         UTP_CTX(fsg)->processed = 0;
177         return 0;
178 }
179
180 static int utp_do_read(struct fsg_dev *fsg, void *data, size_t size)
181 {
182         struct fsg_buffhd       *bh;
183         int                     rc;
184         u32                     amount_left;
185         unsigned int            amount;
186
187         /* Get the starting Logical Block Address and check that it's
188          * not too big */
189
190         amount_left = size;
191         if (unlikely(amount_left == 0))
192                 return -EIO;            /* No default reply*/
193
194         pr_debug("%s: sending %d\n", __func__, size);
195         for (;;) {
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
201                  *      the next page.
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);
205
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);
210                         if (rc)
211                                 return rc;
212                 }
213
214                 /* If we were asked to read past the end of file,
215                  * end with an empty buffer. */
216                 if (amount == 0) {
217                         bh->inreq->length = 0;
218                         bh->state = BUF_STATE_FULL;
219                         break;
220                 }
221
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;
229
230                 bh->inreq->length = amount;
231                 bh->state = BUF_STATE_FULL;
232
233                 /* Send this buffer and go read some more */
234                 bh->inreq->zero = 0;
235
236                 /* USB Physical transfer: Data from device to host */
237                 start_transfer(fsg, fsg->bulk_in, bh->inreq,
238                                 &bh->inreq_busy, &bh->state);
239
240                 fsg->common->next_buffhd_to_fill = bh->next;
241
242                 if (amount_left <= 0)
243                         break;
244         }
245
246         return size - amount_left;
247 }
248
249 static int utp_do_write(struct fsg_dev *fsg, void *data, size_t size)
250 {
251         struct fsg_buffhd       *bh;
252         int                     get_some_more;
253         u32                     amount_left_to_req, amount_left_to_write;
254         unsigned int            amount;
255         int                     rc;
256         loff_t                  offset;
257
258         /* Carry out the file writes */
259         get_some_more = 1;
260         amount_left_to_req = amount_left_to_write = size;
261
262         if (unlikely(amount_left_to_write == 0))
263                 return -EIO;
264
265         offset = 0;
266         while (amount_left_to_write > 0) {
267
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) {
271
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);
282
283                         if (amount == 0) {
284                                 get_some_more = 0;
285                                 /* cry now */
286                                 continue;
287                         }
288
289                         /* Get the next buffer */
290                         amount_left_to_req -= amount;
291                         if (amount_left_to_req == 0)
292                                 get_some_more = 0;
293
294                         /* amount is always divisible by 512, hence by
295                          * the bulk-out maxpacket size */
296                         bh->outreq->length = bh->bulk_out_intended_length =
297                                         amount;
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;
302                         continue;
303                 }
304
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) {
310                         smp_rmb();
311                         fsg->common->next_buffhd_to_drain = bh->next;
312                         bh->state = BUF_STATE_EMPTY;
313
314                         /* Did something go wrong with the transfer? */
315                         if (bh->outreq->status != 0)
316                                 /* cry again, COMMUNICATION_FAILURE */
317                                 break;
318
319                         amount = bh->outreq->actual;
320
321                         /* Perform the write */
322                         memcpy(data + offset, bh->buf, amount);
323
324                         offset += amount;
325                         if (signal_pending(current))
326                                 return -EINTR;          /* Interrupted!*/
327                         amount_left_to_write -= amount;
328                         fsg->common->residue -= amount;
329
330                         /* Did the host decide to stop early? */
331                         if (bh->outreq->actual != bh->outreq->length) {
332                                 fsg->common->short_packet_received = 1;
333                                 break;
334                         }
335                         continue;
336                 }
337
338                 /* Wait for something to happen */
339                 rc = sleep_thread(fsg->common);
340                 if (rc)
341                         return rc;
342         }
343
344         return -EIO;
345 }
346
347 static inline void utp_set_sense(struct fsg_dev *fsg, u16 code, u64 reply)
348 {
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;
353 }
354
355 static void utp_poll(struct fsg_dev *fsg)
356 {
357         struct utp_context *ctx = UTP_CTX(fsg);
358         struct utp_user_data *uud = NULL;
359
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);
364
365         if (uud) {
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);
372                 } else {
373                         printk("%s: pass returned.\n", __func__);
374                         UTP_SS_PASS(fsg);
375                 }
376                 utp_user_data_free(uud);
377         } else {
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__);
381                                 UTP_SS_PASS(fsg);
382                                 utp_context.cur_state = 0;
383                                 return;
384                         }
385                 }
386                 UTP_SS_BUSY(fsg, --ctx->counter);
387         }
388 }
389
390 static int utp_exec(struct fsg_dev *fsg,
391                     char *command,
392                     int cmdsize,
393                     unsigned long long payload)
394 {
395         struct utp_user_data *uud = NULL, *uud2r;
396         struct utp_context *ctx = UTP_CTX(fsg);
397
398         ctx->counter = 0xFFFF;
399         uud2r = utp_user_data_alloc(cmdsize + 1);
400         if (!uud2r)
401                 return -ENOMEM;
402         uud2r->data.flags = UTP_FLAG_COMMAND;
403         uud2r->data.payload = payload;
404         strncpy(uud2r->data.command, command, cmdsize);
405
406         mutex_lock(&ctx->lock);
407         list_add_tail(&uud2r->link, &ctx->read);
408         mutex_unlock(&ctx->lock);
409         /* wake up the read routine */
410         wake_up(&ctx->wq);
411
412         if (command[0] == '!')  /* there will be no response */
413                 return 0;
414
415         /*
416          * the user program (uuc) will return utp_message
417          * and add list to write list
418          */
419         WAIT_ACTIVITY(write);
420
421         mutex_lock(&ctx->lock);
422         if (!list_empty(&ctx->write)) {
423                 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
424 #ifdef DEBUG
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);
430                 }
431                 if (uud->data.flags & UTP_FLAG_REPORT_BUSY)
432                         pr_info("\tBUSY\n");
433 #endif
434         }
435         mutex_unlock(&ctx->lock);
436
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__,
444                                 uud->data.status);
445                 UTP_SS_EXIT(fsg, uud->data.status);
446         } else {
447                 pr_debug("%s: pass returned in EXEC stage. \n", __func__);
448                 UTP_SS_PASS(fsg);
449         }
450         utp_user_data_free(uud);
451         return 0;
452 }
453
454 static int utp_send_status(struct fsg_dev *fsg)
455 {
456         struct fsg_buffhd       *bh;
457         u8                      status = US_BULK_STAT_OK;
458         struct bulk_cs_wrap     *csw;
459         int                     rc;
460
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);
465                 if (rc)
466                         return rc;
467         }
468
469         if (fsg->common->phase_error) {
470                 DBG(fsg, "sending phase-error status\n");
471                 status = US_BULK_STAT_PHASE;
472
473         } else if ((UTP_CTX(fsg)->sd & 0xFFFF) != UTP_REPLY_PASS) {
474                 status = US_BULK_STAT_FAIL;
475         }
476
477         csw = bh->buf;
478
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;
484
485         bh->inreq->length = US_BULK_CS_WRAP_LEN;
486         bh->inreq->zero = 0;
487         start_transfer(fsg, fsg->bulk_in, bh->inreq,
488                         &bh->inreq_busy, &bh->state);
489         fsg->common->next_buffhd_to_fill = bh->next;
490         return 0;
491 }
492
493 static int utp_handle_message(struct fsg_dev *fsg,
494                               char *cdb_data,
495                               int default_reply)
496 {
497         struct utp_msg *m = (struct utp_msg *)cdb_data;
498         void *data = NULL;
499         int r;
500         struct utp_user_data *uud2r;
501         unsigned long long param;
502         unsigned long tag;
503
504         if (m->f0 != 0xF0)
505                 return default_reply;
506
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);
511
512         switch ((enum utp_msg_type)m->utp_msg_type) {
513
514         case UTP_POLL:
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);
518                         break;
519                 }
520                 utp_poll(fsg);
521                 break;
522         case UTP_EXEC:
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);
529                 vfree(data);
530                 break;
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);
536                 UTP_SS_PASS(fsg);
537                 break;
538         case UTP_PUT:
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);
542                 if (!uud2r)
543                         return -ENOMEM;
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);
552                 /*
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
556                  */
557 #if 0
558                 struct utp_user_data *uud = NULL;
559                 struct utp_context *ctx;
560                 WAIT_ACTIVITY(write);
561                 ctx = UTP_CTX(fsg);
562                 mutex_lock(&ctx->lock);
563
564                 if (!list_empty(&ctx->write))
565                         uud = list_first_entry(&ctx->write,
566                                         struct utp_user_data, link);
567
568                 mutex_unlock(&ctx->lock);
569                 if (uud) {
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);
574                         } else {
575                                 pr_debug("%s: pass\n", __func__);
576                                 UTP_SS_PASS(fsg);
577                         }
578                         utp_user_data_free(uud);
579                 } else{
580                         UTP_SS_PASS(fsg);
581                 }
582 #endif
583                 if (count_list(&UTP_CTX(fsg)->read) < 7) {
584                         utp_context.cur_state = 0;
585                         UTP_SS_PASS(fsg);
586                 } else
587                         UTP_SS_BUSY(fsg, UTP_CTX(fsg)->counter);
588
589                 break;
590         }
591
592         utp_send_status(fsg);
593         return -1;
594 }