]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/usb/gadget/fsl_updater.c
8b4af624ba8391f53b35a538c1ed023b29ca61b0
[karo-tx-linux.git] / drivers / usb / gadget / 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 = kzalloc(size + sizeof(*uud), GFP_KERNEL);
52         if (!uud)
53                 return uud;
54         uud->data.size = size + sizeof(uud->data);
55         INIT_LIST_HEAD(&uud->link);
56         return uud;
57 }
58
59 static void utp_user_data_free(struct utp_user_data *uud)
60 {
61         mutex_lock(&utp_context.lock);
62         list_del(&uud->link);
63         mutex_unlock(&utp_context.lock);
64         kfree(uud);
65 }
66
67 /* Get the number of element for list */
68 static u32 count_list(struct list_head *l)
69 {
70         u32 count = 0;
71         struct list_head *tmp;
72
73         list_for_each(tmp, l) {
74                 count++;
75         }
76
77         return count;
78 }
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))
82
83 /* Called by userspace program (uuc) */
84 static ssize_t utp_file_read(struct file *file,
85                              char __user *buf,
86                              size_t size,
87                              loff_t *off)
88 {
89         struct utp_user_data *uud;
90         size_t size_to_put;
91         int free = 0;
92
93         WAIT_ACTIVITY(read);
94
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;
99
100         if (size >= size_to_put)
101                 free = !0;
102         if (copy_to_user(buf, &uud->data, size_to_put))
103                 return -EACCES;
104         if (free)
105                 utp_user_data_free(uud);
106         else {
107                 pr_info("sizeof = %d, size = %d\n",
108                         sizeof(uud->data),
109                         uud->data.size);
110
111                 pr_err("Will not free utp_user_data, because buffer size = %d,"
112                         "need to put %d\n", size, size_to_put);
113         }
114
115         /*
116          * The user program has already finished data process,
117          * go on getting data from the host
118          */
119         wake_up(&utp_context.list_full_wq);
120
121         return size_to_put;
122 }
123
124 static ssize_t utp_file_write(struct file *file, const char __user *buf,
125                                 size_t size, loff_t *off)
126 {
127         struct utp_user_data *uud;
128
129         if (size < sizeof(uud->data))
130                 return -EINVAL;
131         uud = utp_user_data_alloc(size);
132         if (copy_from_user(&uud->data, buf, size))
133                 return -EACCES;
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);
139         return size;
140 }
141
142 /*
143  * uuc should change to use soc bus infrastructure to soc information
144  * /sys/devices/soc0/soc_id
145  * this function can be removed.
146  */
147 static long
148 utp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
149 {
150         int cpu_id = 0;
151
152         switch (cmd) {
153         case UTP_GET_CPU_ID:
154                 return put_user(cpu_id, (int __user *)arg);
155         default:
156                 return -ENOIOCTLCMD;
157         }
158 }
159
160 /* Will be called when the host wants to get the sense data */
161 static int utp_get_sense(struct fsg_dev *fsg)
162 {
163         if (UTP_CTX(fsg)->processed == 0)
164                 return -1;
165
166         UTP_CTX(fsg)->processed = 0;
167         return 0;
168 }
169
170 static int utp_do_read(struct fsg_dev *fsg, void *data, size_t size)
171 {
172         struct fsg_buffhd       *bh;
173         int                     rc;
174         u32                     amount_left;
175         unsigned int            amount;
176
177         /* Get the starting Logical Block Address and check that it's
178          * not too big */
179
180         amount_left = size;
181         if (unlikely(amount_left == 0))
182                 return -EIO;            /* No default reply*/
183
184         pr_debug("%s: sending %d\n", __func__, size);
185         for (;;) {
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
191                  *      the next page.
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);
195
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);
200                         if (rc)
201                                 return rc;
202                 }
203
204                 /* If we were asked to read past the end of file,
205                  * end with an empty buffer. */
206                 if (amount == 0) {
207                         bh->inreq->length = 0;
208                         bh->state = BUF_STATE_FULL;
209                         break;
210                 }
211
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;
219
220                 bh->inreq->length = amount;
221                 bh->state = BUF_STATE_FULL;
222
223                 /* Send this buffer and go read some more */
224                 bh->inreq->zero = 0;
225
226                 /* USB Physical transfer: Data from device to host */
227                 start_transfer(fsg, fsg->bulk_in, bh->inreq,
228                                 &bh->inreq_busy, &bh->state);
229
230                 fsg->common->next_buffhd_to_fill = bh->next;
231
232                 if (amount_left <= 0)
233                         break;
234         }
235
236         return size - amount_left;
237 }
238
239 static int utp_do_write(struct fsg_dev *fsg, void *data, size_t size)
240 {
241         struct fsg_buffhd       *bh;
242         int                     get_some_more;
243         u32                     amount_left_to_req, amount_left_to_write;
244         unsigned int            amount;
245         int                     rc;
246         loff_t                  offset;
247
248         /* Carry out the file writes */
249         get_some_more = 1;
250         amount_left_to_req = amount_left_to_write = size;
251
252         if (unlikely(amount_left_to_write == 0))
253                 return -EIO;
254
255         offset = 0;
256         while (amount_left_to_write > 0) {
257
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) {
261
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);
272
273                         if (amount == 0) {
274                                 get_some_more = 0;
275                                 /* cry now */
276                                 continue;
277                         }
278
279                         /* Get the next buffer */
280                         amount_left_to_req -= amount;
281                         if (amount_left_to_req == 0)
282                                 get_some_more = 0;
283
284                         /* amount is always divisible by 512, hence by
285                          * the bulk-out maxpacket size */
286                         bh->outreq->length = bh->bulk_out_intended_length =
287                                         amount;
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;
292                         continue;
293                 }
294
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) {
300                         smp_rmb();
301                         fsg->common->next_buffhd_to_drain = bh->next;
302                         bh->state = BUF_STATE_EMPTY;
303
304                         /* Did something go wrong with the transfer? */
305                         if (bh->outreq->status != 0)
306                                 /* cry again, COMMUNICATION_FAILURE */
307                                 break;
308
309                         amount = bh->outreq->actual;
310
311                         /* Perform the write */
312                         memcpy(data + offset, bh->buf, amount);
313
314                         offset += amount;
315                         if (signal_pending(current))
316                                 return -EINTR;          /* Interrupted!*/
317                         amount_left_to_write -= amount;
318                         fsg->common->residue -= amount;
319
320                         /* Did the host decide to stop early? */
321                         if (bh->outreq->actual != bh->outreq->length) {
322                                 fsg->common->short_packet_received = 1;
323                                 break;
324                         }
325                         continue;
326                 }
327
328                 /* Wait for something to happen */
329                 rc = sleep_thread(fsg->common);
330                 if (rc)
331                         return rc;
332         }
333
334         return -EIO;
335 }
336
337 static inline void utp_set_sense(struct fsg_dev *fsg, u16 code, u64 reply)
338 {
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;
343 }
344
345 static void utp_poll(struct fsg_dev *fsg)
346 {
347         struct utp_context *ctx = UTP_CTX(fsg);
348         struct utp_user_data *uud = NULL;
349
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);
354
355         if (uud) {
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);
360                 } else {
361                         pr_debug("%s: pass\n", __func__);
362                         UTP_SS_PASS(fsg);
363                 }
364                 utp_user_data_free(uud);
365         } else {
366                 pr_debug("%s: still busy...\n", __func__);
367                 UTP_SS_BUSY(fsg, --ctx->counter);
368         }
369 }
370
371 static int utp_exec(struct fsg_dev *fsg,
372                     char *command,
373                     int cmdsize,
374                     unsigned long long payload)
375 {
376         struct utp_user_data *uud = NULL, *uud2r;
377         struct utp_context *ctx = UTP_CTX(fsg);
378
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);
383
384         mutex_lock(&ctx->lock);
385         list_add_tail(&uud2r->link, &ctx->read);
386         mutex_unlock(&ctx->lock);
387         /* wake up the read routine */
388         wake_up(&ctx->wq);
389
390         if (command[0] == '!')  /* there will be no response */
391                 return 0;
392
393         /*
394          * the user program (uuc) will return utp_message
395          * and add list to write list
396          */
397         WAIT_ACTIVITY(write);
398
399         mutex_lock(&ctx->lock);
400         if (!list_empty(&ctx->write)) {
401                 uud = list_first_entry(&ctx->write, struct utp_user_data, link);
402 #ifdef DEBUG
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);
408                 }
409                 if (uud->data.flags & UTP_FLAG_REPORT_BUSY)
410                         pr_info("\tBUSY\n");
411 #endif
412         }
413         mutex_unlock(&ctx->lock);
414
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__,
423                                 uud->data.status);
424                 UTP_SS_EXIT(fsg, uud->data.status);
425         } else {
426                 pr_debug("%s: pass\n", __func__);
427                 UTP_SS_PASS(fsg);
428         }
429         utp_user_data_free(uud);
430         return 0;
431 }
432
433 static int utp_send_status(struct fsg_dev *fsg)
434 {
435         struct fsg_buffhd       *bh;
436         u8                      status = US_BULK_STAT_OK;
437         struct bulk_cs_wrap     *csw;
438         int                     rc;
439
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);
444                 if (rc)
445                         return rc;
446         }
447
448         if (fsg->common->phase_error) {
449                 DBG(fsg, "sending phase-error status\n");
450                 status = US_BULK_STAT_PHASE;
451
452         } else if ((UTP_CTX(fsg)->sd & 0xFFFF) != UTP_REPLY_PASS) {
453                 status = US_BULK_STAT_FAIL;
454         }
455
456         csw = bh->buf;
457
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;
463
464         bh->inreq->length = US_BULK_CS_WRAP_LEN;
465         bh->inreq->zero = 0;
466         start_transfer(fsg, fsg->bulk_in, bh->inreq,
467                         &bh->inreq_busy, &bh->state);
468         fsg->common->next_buffhd_to_fill = bh->next;
469         return 0;
470 }
471
472 static int utp_handle_message(struct fsg_dev *fsg,
473                               char *cdb_data,
474                               int default_reply)
475 {
476         struct utp_msg *m = (struct utp_msg *)cdb_data;
477         void *data = NULL;
478         int r;
479         struct utp_user_data *uud2r;
480         unsigned long long param;
481         unsigned long tag;
482
483         if (m->f0 != 0xF0)
484                 return default_reply;
485
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);
490
491         switch ((enum utp_msg_type)m->utp_msg_type) {
492
493         case UTP_POLL:
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);
497                         break;
498                 }
499                 utp_poll(fsg);
500                 break;
501         case UTP_EXEC:
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);
507                 kfree(data);
508                 break;
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);
514                 UTP_SS_PASS(fsg);
515                 break;
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);
528                 /*
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
532                  */
533 #if 0
534                 struct utp_user_data *uud = NULL;
535                 struct utp_context *ctx;
536                 WAIT_ACTIVITY(write);
537                 ctx = UTP_CTX(fsg);
538                 mutex_lock(&ctx->lock);
539
540                 if (!list_empty(&ctx->write))
541                         uud = list_first_entry(&ctx->write,
542                                         struct utp_user_data, link);
543
544                 mutex_unlock(&ctx->lock);
545                 if (uud) {
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);
550                         } else {
551                                 pr_debug("%s: pass\n", __func__);
552                                 UTP_SS_PASS(fsg);
553                         }
554                         utp_user_data_free(uud);
555                 } else{
556                         UTP_SS_PASS(fsg);
557                 }
558 #endif
559                 UTP_SS_PASS(fsg);
560
561                 wait_event_interruptible(UTP_CTX(fsg)->list_full_wq,
562                         count_list(&UTP_CTX(fsg)->read) < 7);
563                 break;
564         }
565
566         utp_send_status(fsg);
567         return -1;
568 }