]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/function/fsl_updater.c
83333d1c16db495cf7a4064d654d443ce8be9373
[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                 return -ENOMEM;
138         if (copy_from_user(&uud->data, buf, size)) {
139                 printk(KERN_INFO "[ %s ] copy error!\n", __func__);
140                 vfree(uud);
141                 return -EACCES;
142         }
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);
148         return size;
149 }
150
151 /*
152  * uuc should change to use soc bus infrastructure to soc information
153  * /sys/devices/soc0/soc_id
154  * this function can be removed.
155  */
156 static long
157 utp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
158 {
159         int cpu_id = 0;
160
161         switch (cmd) {
162         case UTP_GET_CPU_ID:
163                 return put_user(cpu_id, (int __user *)arg);
164         default:
165                 return -ENOIOCTLCMD;
166         }
167 }
168
169 /* Will be called when the host wants to get the sense data */
170 static int utp_get_sense(struct fsg_dev *fsg)
171 {
172         if (UTP_CTX(fsg)->processed == 0)
173                 return -1;
174
175         UTP_CTX(fsg)->processed = 0;
176         return 0;
177 }
178
179 static int utp_do_read(struct fsg_dev *fsg, void *data, size_t size)
180 {
181         struct fsg_buffhd       *bh;
182         int                     rc;
183         u32                     amount_left;
184         unsigned int            amount;
185
186         /* Get the starting Logical Block Address and check that it's
187          * not too big */
188
189         amount_left = size;
190         if (unlikely(amount_left == 0))
191                 return -EIO;            /* No default reply*/
192
193         pr_debug("%s: sending %d\n", __func__, size);
194         for (;;) {
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
200                  *      the next page.
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);
204
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);
209                         if (rc)
210                                 return rc;
211                 }
212
213                 /* If we were asked to read past the end of file,
214                  * end with an empty buffer. */
215                 if (amount == 0) {
216                         bh->inreq->length = 0;
217                         bh->state = BUF_STATE_FULL;
218                         break;
219                 }
220
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;
228
229                 bh->inreq->length = amount;
230                 bh->state = BUF_STATE_FULL;
231
232                 /* Send this buffer and go read some more */
233                 bh->inreq->zero = 0;
234
235                 /* USB Physical transfer: Data from device to host */
236                 start_transfer(fsg, fsg->bulk_in, bh->inreq,
237                                 &bh->inreq_busy, &bh->state);
238
239                 fsg->common->next_buffhd_to_fill = bh->next;
240
241                 if (amount_left <= 0)
242                         break;
243         }
244
245         return size - amount_left;
246 }
247
248 static int utp_do_write(struct fsg_dev *fsg, void *data, size_t size)
249 {
250         struct fsg_buffhd       *bh;
251         int                     get_some_more;
252         u32                     amount_left_to_req, amount_left_to_write;
253         unsigned int            amount;
254         int                     rc;
255         loff_t                  offset;
256
257         /* Carry out the file writes */
258         get_some_more = 1;
259         amount_left_to_req = amount_left_to_write = size;
260
261         if (unlikely(amount_left_to_write == 0))
262                 return -EIO;
263
264         offset = 0;
265         while (amount_left_to_write > 0) {
266
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) {
270
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);
281
282                         if (amount == 0) {
283                                 get_some_more = 0;
284                                 /* cry now */
285                                 continue;
286                         }
287
288                         /* Get the next buffer */
289                         amount_left_to_req -= amount;
290                         if (amount_left_to_req == 0)
291                                 get_some_more = 0;
292
293                         /* amount is always divisible by 512, hence by
294                          * the bulk-out maxpacket size */
295                         bh->outreq->length = bh->bulk_out_intended_length =
296                                         amount;
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;
301                         continue;
302                 }
303
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) {
309                         smp_rmb();
310                         fsg->common->next_buffhd_to_drain = bh->next;
311                         bh->state = BUF_STATE_EMPTY;
312
313                         /* Did something go wrong with the transfer? */
314                         if (bh->outreq->status != 0)
315                                 /* cry again, COMMUNICATION_FAILURE */
316                                 break;
317
318                         amount = bh->outreq->actual;
319
320                         /* Perform the write */
321                         memcpy(data + offset, bh->buf, amount);
322
323                         offset += amount;
324                         if (signal_pending(current))
325                                 return -EINTR;          /* Interrupted!*/
326                         amount_left_to_write -= amount;
327                         fsg->common->residue -= amount;
328
329                         /* Did the host decide to stop early? */
330                         if (bh->outreq->actual != bh->outreq->length) {
331                                 fsg->common->short_packet_received = 1;
332                                 break;
333                         }
334                         continue;
335                 }
336
337                 /* Wait for something to happen */
338                 rc = sleep_thread(fsg->common);
339                 if (rc)
340                         return rc;
341         }
342
343         return -EIO;
344 }
345
346 static inline void utp_set_sense(struct fsg_dev *fsg, u16 code, u64 reply)
347 {
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;
352 }
353
354 static void utp_poll(struct fsg_dev *fsg)
355 {
356         struct utp_context *ctx = UTP_CTX(fsg);
357         struct utp_user_data *uud = NULL;
358
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);
363
364         if (uud) {
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);
371                 } else {
372                         printk("%s: pass returned.\n", __func__);
373                         UTP_SS_PASS(fsg);
374                 }
375                 utp_user_data_free(uud);
376         } else {
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__);
380                                 UTP_SS_PASS(fsg);
381                                 utp_context.cur_state = 0;
382                                 return;
383                         }
384                 }
385                 UTP_SS_BUSY(fsg, --ctx->counter);
386         }
387 }
388
389 static int utp_exec(struct fsg_dev *fsg,
390                     char *command,
391                     int cmdsize,
392                     unsigned long long payload)
393 {
394         struct utp_user_data *uud = NULL, *uud2r;
395         struct utp_context *ctx = UTP_CTX(fsg);
396
397         ctx->counter = 0xFFFF;
398         uud2r = utp_user_data_alloc(cmdsize + 1);
399         if (!uud2r)
400                 return -ENOMEM;
401         uud2r->data.flags = UTP_FLAG_COMMAND;
402         uud2r->data.payload = payload;
403         strncpy(uud2r->data.command, command, cmdsize);
404
405         mutex_lock(&ctx->lock);
406         list_add_tail(&uud2r->link, &ctx->read);
407         mutex_unlock(&ctx->lock);
408         /* wake up the read routine */
409         wake_up(&ctx->wq);
410
411         if (command[0] == '!')  /* there will be no response */
412                 return 0;
413
414         /*
415          * the user program (uuc) will return utp_message
416          * and add list to write list
417          */
418         WAIT_ACTIVITY(write);
419
420         mutex_lock(&ctx->lock);
421         if (!list_empty(&ctx->write)) {
422                 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
423 #ifdef DEBUG
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);
429                 }
430                 if (uud->data.flags & UTP_FLAG_REPORT_BUSY)
431                         pr_info("\tBUSY\n");
432 #endif
433         }
434         mutex_unlock(&ctx->lock);
435
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__,
443                                 uud->data.status);
444                 UTP_SS_EXIT(fsg, uud->data.status);
445         } else {
446                 pr_debug("%s: pass returned in EXEC stage. \n", __func__);
447                 UTP_SS_PASS(fsg);
448         }
449         utp_user_data_free(uud);
450         return 0;
451 }
452
453 static int utp_send_status(struct fsg_dev *fsg)
454 {
455         struct fsg_buffhd       *bh;
456         u8                      status = US_BULK_STAT_OK;
457         struct bulk_cs_wrap     *csw;
458         int                     rc;
459
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);
464                 if (rc)
465                         return rc;
466         }
467
468         if (fsg->common->phase_error) {
469                 DBG(fsg, "sending phase-error status\n");
470                 status = US_BULK_STAT_PHASE;
471
472         } else if ((UTP_CTX(fsg)->sd & 0xFFFF) != UTP_REPLY_PASS) {
473                 status = US_BULK_STAT_FAIL;
474         }
475
476         csw = bh->buf;
477
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;
483
484         bh->inreq->length = US_BULK_CS_WRAP_LEN;
485         bh->inreq->zero = 0;
486         start_transfer(fsg, fsg->bulk_in, bh->inreq,
487                         &bh->inreq_busy, &bh->state);
488         fsg->common->next_buffhd_to_fill = bh->next;
489         return 0;
490 }
491
492 static int utp_handle_message(struct fsg_dev *fsg,
493                               char *cdb_data,
494                               int default_reply)
495 {
496         struct utp_msg *m = (struct utp_msg *)cdb_data;
497         void *data = NULL;
498         int r;
499         struct utp_user_data *uud2r;
500         unsigned long long param;
501         unsigned long tag;
502
503         if (m->f0 != 0xF0)
504                 return default_reply;
505
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);
510
511         switch ((enum utp_msg_type)m->utp_msg_type) {
512
513         case UTP_POLL:
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);
517                         break;
518                 }
519                 utp_poll(fsg);
520                 break;
521         case UTP_EXEC:
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);
528                 vfree(data);
529                 break;
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);
535                 UTP_SS_PASS(fsg);
536                 break;
537         case UTP_PUT:
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);
541                 if (!uud2r)
542                         return -ENOMEM;
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);
551                 /*
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
555                  */
556 #if 0
557                 struct utp_user_data *uud = NULL;
558                 struct utp_context *ctx;
559                 WAIT_ACTIVITY(write);
560                 ctx = UTP_CTX(fsg);
561                 mutex_lock(&ctx->lock);
562
563                 if (!list_empty(&ctx->write))
564                         uud = list_first_entry(&ctx->write,
565                                         struct utp_user_data, link);
566
567                 mutex_unlock(&ctx->lock);
568                 if (uud) {
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);
573                         } else {
574                                 pr_debug("%s: pass\n", __func__);
575                                 UTP_SS_PASS(fsg);
576                         }
577                         utp_user_data_free(uud);
578                 } else{
579                         UTP_SS_PASS(fsg);
580                 }
581 #endif
582                 if (count_list(&UTP_CTX(fsg)->read) < 7) {
583                         utp_context.cur_state = 0;
584                         UTP_SS_PASS(fsg);
585                 } else
586                         UTP_SS_BUSY(fsg, UTP_CTX(fsg)->counter);
587
588                 break;
589         }
590
591         utp_send_status(fsg);
592         return -1;
593 }