forked from ni/usb3vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
u3v_stream.c
1613 lines (1401 loc) · 45.3 KB
/
u3v_stream.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* u3v_stream.c: Driver for USB3 Vision(TM) class devices
*
* (C) Copyright 2014 National Instruments Corp.
* Authors: Katie Ensign <[email protected]>,
* Jared Jenson <[email protected]>
*
* The "USB3 Vision" name and logo are trademarks of the AIA and may not
* be used without the authorization of the AIA <www.visiononline.org>
*
* Anyone wishing to develop, manufacture, or sell private labeled
* compliant product for commercial purposes or to develop compliant
* software for distribution must obtain a license to use the USB3
* Vision standard and the USB3 Vision name and logo from the AIA.
* All products (including software) must be registered with the AIA and
* must be tested for compliancy with the standard.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "u3v.h"
#include "u3v_shared.h"
#include "u3v_stream.h"
#include "u3v_control.h"
#include <linux/atomic.h>
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/highmem.h>
#include <linux/kernel.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/pagemap.h>
#include <linux/scatterlist.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/vmalloc.h>
#include <asm/unaligned.h>
#ifdef VERSION_COMPATIBILITY
#include <linux/version.h>
#else
#include <generated/uapi/linux/version.h>
#endif
/* Internal stream structs and enums */
struct urb_info {
u32 urb_index;
struct u3v_stream *stream;
struct buffer_entry *entry;
struct urb *purb;
bool kernel_allocated;
struct pg_list *pglist;
u8 *buffer;
size_t buffer_size;
size_t min_expected_size;
};
enum buffer_state {
u3v_idle, u3v_queued, u3v_complete, u3v_cancelled
};
struct buffer_entry {
atomic_t callbacks_received;
enum buffer_state state;
struct urb_info *urb_info_array;
int urb_info_count;
u32 incomplete_callbacks_received;
u32 leader_received_size;
u32 payload_received_size;
u32 trailer_received_size;
void *transfer2_addr;
u32 transfer2_received_size;
struct completion buffer_complete;
u64 buffer_id;
struct rb_node node;
s32 status;
};
struct pg_list {
size_t num_bytes;
size_t num_pages;
size_t offset;
struct page **pages;
struct sg_table *sgt;
};
/*
* Inline helper function that returns the number of pages in a num_bytes
* sized chunk of memory starting at addr.
*/
static inline int get_num_pages(uintptr_t addr, size_t num_bytes)
{
unsigned long end = (addr + num_bytes + PAGE_SIZE - 1) >> PAGE_SHIFT;
unsigned long start = addr >> PAGE_SHIFT;
return end - start;
}
/* Forward declaration for internal functions */
/* Helper function for u3v_create_stream */
static int set_buffer_sizes(struct u3v_stream *stream, u64 image_buffer_size,
u64 chunk_data_buffer_size, u32 max_leader_size,
u32 max_trailer_size, u32 payload_size, u32 payload_count,
u32 transfer1_size, u32 transfer2_size);
/* Helper functions for u3v_configure/unconfigure_buffer */
static int create_buffer_entry(struct u3v_stream *stream,
void *user_image_buffer,
void *user_chunk_data_buffer,
__u64 *buffer_id);
static int allocate_urb_buffer(struct u3v_stream *stream,
struct urb_info *urb_info, size_t size,
size_t min_expected_size);
static int map_urb_buffer(struct u3v_stream *stream,
struct urb_info *urb_info, void *user_buffer,
size_t buffer_size, size_t offset,
size_t urb_buffer_size, size_t min_expected_size);
static int destroy_buffer(struct u3v_stream *stream, __u64 buffer_id);
static void destroy_urb(struct urb_info *urb_info);
static struct pg_list *create_pg_list(struct u3v_stream *stream,
void *user_buffer, size_t num_bytes);
static void destroy_pg_list(struct pg_list *pglist, bool dirty);
static struct page **lock_user_pages(struct u3v_stream *stream,
uintptr_t uaddr, size_t count, int num_pages);
static void unlock_user_pages(struct pg_list *pglist, int dirty);
static void create_sg_list(struct u3v_stream *stream, struct pg_list *pglist);
static void destroy_sg_list(struct pg_list *pglist);
/* Helper functions for u3v_queue_buffer */
static void reset_counters(struct buffer_entry *entry);
static int submit_stream_urb(struct u3v_stream *stream,
struct urb_info *urb_info);
/* Stream URB callback function */
static void stream_urb_completion(struct urb *purb);
/* Cancels buffers and resets the stream bulk in endpoint */
static int reset_stream(struct u3v_stream *stream);
/* Helper functions for accessing the rb tree of buffer entries */
static struct buffer_entry *search_buffer_entries(struct u3v_stream *stream,
__u64 value);
static void buffer_entry_insert(struct u3v_stream *stream,
struct buffer_entry *new);
/*
* u3v_create_stream - Initializes the stream interface.
* @u3v: pointer to the u3v_device struct
* @intf: pointer to the stream interface struct
* @image_buffer_size: the size of the buffers that will hold the image data.
* Could include size of both the image data and chunk data if they
* are to be acquired into the same buffer. Otherwise, it is just
* the size of the image data.
* @chunk_data_buffer_size: the size of the buffers that will hold only
* chunk data. If the chunk data is acquired into the same buffer
* as the image data, this size should be 0.
* @max_leader_size: the max required size + alignment for the leader buffer
* @max_trailer_size: the max required size + alignment for the trailer buffer
* @payload_size: size of each payload buffer
* @payload_count: number of payload buffers
* @transfer1_size: size of transfer1 payload buffer
* @transfer2_size: size of transfer2 payload buffer
*/
int u3v_create_stream(struct u3v_device *u3v, struct usb_interface *intf,
u64 image_buffer_size, u64 chunk_data_buffer_size,
u32 max_leader_size, u32 max_trailer_size, u32 payload_size,
u32 payload_count, u32 transfer1_size, u32 transfer2_size)
{
struct u3v_stream *stream = NULL;
int ret = 0;
if (u3v == NULL)
return -EINVAL;
if (u3v->stream_info.bulk_in == NULL) {
dev_err(u3v->device,
"%s: Did not detect bulk in endpoint in the stream interface.\n",
__func__);
return U3V_ERR_NO_STREAM_INTERFACE;
}
if (u3v->u3v_info->sirm_addr == 0) {
dev_err(u3v->device,
"%s: the SIRM address is 0 - streaming is not supported\n",
__func__);
return U3V_ERR_NO_STREAM_INTERFACE;
}
if (u3v->stream_info.interface_ptr != NULL)
return U3V_ERR_STREAM_ALREADY_CONFIGURED;
stream = kzalloc(sizeof(*stream), GFP_KERNEL);
if (stream == NULL)
return -ENOMEM;
mutex_init(&stream->stream_lock);
stream->u3v_dev = u3v;
stream->wait_in_progress = false;
init_usb_anchor(&stream->stream_anchor);
stream->next_buffer_id = 0;
stream->root = RB_ROOT;
reset_stream(stream);
/* set buffer configurations */
ret = set_buffer_sizes(stream, image_buffer_size,
chunk_data_buffer_size, max_leader_size,
max_trailer_size, payload_size, payload_count,
transfer1_size, transfer2_size);
if (ret != 0)
kfree(stream);
else
u3v->stream_info.interface_ptr = stream;
return ret;
}
/*
* u3v_destroy_stream - destroys the stream interface
* @u3v: pointer to the u3v_device struct
*/
int u3v_destroy_stream(struct u3v_device *u3v)
{
struct u3v_stream *stream;
struct rb_node *node;
struct buffer_entry *entry;
int ret;
if (u3v == NULL)
return -EINVAL;
stream = (struct u3v_stream *)(u3v->stream_info.interface_ptr);
if (stream == NULL)
return U3V_ERR_STREAM_NOT_CONFIGURED;
mutex_lock(&stream->stream_lock);
ret = reset_stream(stream);
if (ret != 0)
goto error;
mutex_unlock(&stream->stream_lock);
wait_for_completion(&u3v->stream_info.ioctl_complete);
mutex_lock(&stream->stream_lock);
for (node = rb_first(&stream->root); node; node = rb_next(node)) {
entry = rb_entry(node, struct buffer_entry, node);
destroy_buffer(stream, entry->buffer_id);
}
mutex_unlock(&stream->stream_lock);
kfree(stream);
u3v->stream_info.interface_ptr = NULL;
return 0;
error:
mutex_unlock(&stream->stream_lock);
return ret;
}
/*
* set_buffer_sizes - helper function for configure_stream that initializes
* buffer size information and sets the device up for streaming
* precondition: must be called from u3v_create_stream
* @stream: pointer to the u3v_stream interface struct
* @image_buffer_size: the size of the buffers that will hold the image data.
* Could include size of both the image data and chunk data if they
* are to be acquired into the same buffer. Otherwise, it is just
* the size of the image data.
* @chunk_data_buffer_size: the size of the buffers that will hold only
* chunk data. If the chunk data is acquired into the same buffer
* as the image data, this size should be 0.
* @max_leader_size: the max required size + alignment for the leader buffer
* @max_trailer_size: the max required size + alignment for the trailer buffer
* @payload_size: size of each payload buffer
* @payload_count: number of payload buffers
* @transfer1_size: size of transfer1 payload buffer
* @transfer2_size: size of transfer2 payload buffer
*/
static int set_buffer_sizes(struct u3v_stream *stream,
u64 image_buffer_size,
u64 chunk_data_buffer_size, u32 max_leader_size,
u32 max_trailer_size, u32 payload_size, u32 payload_count,
u32 transfer1_size, u32 transfer2_size)
{
struct u3v_device *u3v;
struct device *dev;
u32 transfer2_data_size;
if (stream == NULL)
return -EINVAL;
u3v = stream->u3v_dev;
dev = u3v->device;
dev_dbg(dev, "%s: image buffer size = %llu\n",
__func__, image_buffer_size);
dev_dbg(dev, "%s: chunk data buffer size = %llu\n",
__func__, chunk_data_buffer_size);
dev_dbg(dev, "%s: payload size = %u\n",
__func__, payload_size);
dev_dbg(dev, "%s: payload count = %u\n",
__func__, payload_count);
dev_dbg(dev, "%s: transfer1 = %u\n",
__func__, transfer1_size);
dev_dbg(dev, "%s: transfer2 = %u\n",
__func__, transfer2_size);
if (image_buffer_size == 0 || max_leader_size == 0 ||
max_trailer_size == 0 || payload_size == 0) {
dev_err(dev,
"%s: leader, trailer, and image buffer sizes cannot be 0\n",
__func__);
dev_err(dev, "\timage buffer size is %llu\n",
image_buffer_size);
dev_err(dev, "\tmax leader buffer size is %u\n",
max_leader_size);
dev_err(dev, "\tmax trailer buffer size is %u\n",
max_trailer_size);
dev_err(dev, "\tpayload transfer buffer size is %u\n",
payload_size);
return -EINVAL;
}
if ((image_buffer_size + chunk_data_buffer_size) <
((payload_size * payload_count) + transfer1_size)) {
dev_err(dev,
"%s: buffer sizes are too small to hold all of the requested DMA payload data. Total buffer size = %llu, calculated DMA size = %u\n",
__func__, image_buffer_size + chunk_data_buffer_size,
((payload_size * payload_count) + transfer1_size));
return -EINVAL;
}
/*
* Calculate how much data we actually expect to be valid in the
* final transfer 2 buffer
*/
transfer2_data_size = (image_buffer_size + chunk_data_buffer_size -
(payload_size * payload_count) - transfer1_size);
dev_dbg(dev, "%s: transfer2_data = %u\n",
__func__, transfer2_data_size);
if (transfer2_data_size > transfer2_size) {
dev_err(dev,
"%s: final transfer 2 data size (%u) exceeds the size of the buffer (%u)\n",
__func__, transfer2_data_size, transfer2_size);
return -EINVAL;
}
/* The buffer sizes are valid, so now we store them */
stream->config.image_buffer_size = image_buffer_size;
stream->config.chunk_data_buffer_size = chunk_data_buffer_size;
stream->config.max_leader_size = max_leader_size;
stream->config.max_trailer_size = max_trailer_size;
stream->config.payload_size = payload_size;
stream->config.payload_count = payload_count;
stream->config.transfer1_size = transfer1_size;
stream->config.transfer2_size = transfer2_size;
stream->config.transfer2_data_size = transfer2_data_size;
return 0;
}
/*
* u3v_configure_buffer - creates the buffer context data, page locks the
* user mode image and chunk data buffers, and adds the buffer to the
* rbtree of available buffers.
* @stream: pointer to the stream interface struct
* @user_image_buffer: user mode memory address of the image buffer to lock
* @user_chunk_data_buffer: user mode memory address of the chunk data buffer
* to lock
*/
int u3v_configure_buffer(struct u3v_stream *stream, void *user_image_buffer,
void *user_chunk_data_buffer, __u64 *buffer_id)
{
struct device *dev;
int ret;
if (stream == NULL || user_image_buffer == NULL ||
buffer_id == NULL)
return -EINVAL;
dev = stream->u3v_dev->device;
if ((user_chunk_data_buffer == NULL &&
stream->config.chunk_data_buffer_size != 0) ||
(user_chunk_data_buffer != NULL &&
stream->config.chunk_data_buffer_size == 0)) {
dev_err(dev,
"%s: Invalid chunk data buffer address and size combination\n",
__func__);
return -EINVAL;
}
ret = create_buffer_entry(stream, user_image_buffer,
user_chunk_data_buffer, buffer_id);
if (ret != 0)
return ret;
return 0;
}
/*
* create_buffer_entry - this function allocates and initializes the buffer
* context data, page locks the image buffer and chunk data buffer,
* allocates buffers for the leader and trailer URBs, and allocates
* a scratch buffer for the final transfer 2 URB if necessary
*
* @stream: pointer to the stream interface struct
* @user_image_buffer: memory address of the user image buffer to be locked
* @user_chunk_data_buffer: memory address of the user chunk data buffer to
* be locked
* @buffer_id: on return this user pointer points to valid buffer id
*/
static int create_buffer_entry(struct u3v_stream *stream,
void *user_image_buffer,
void *user_chunk_data_buffer,
__u64 *buffer_id)
{
int i;
int ret;
size_t image_offset;
size_t chunk_data_offset;
struct buffer_entry *entry;
u8 *user_transfer2_addr;
entry = kzalloc(sizeof(*entry), GFP_KERNEL);
if (entry == NULL)
return -ENOMEM;
/*
* We will need URBs for each of the payloads as well as the
* leader, trailer, final transfer 1 (if applicable) and final
* transfer 2 (if applicable)
*/
entry->urb_info_count = stream->config.payload_count + 2;
if (stream->config.transfer1_size != 0)
entry->urb_info_count++;
if (stream->config.transfer2_size != 0)
entry->urb_info_count++;
entry->urb_info_array = kzalloc(sizeof(struct urb_info) *
entry->urb_info_count, GFP_KERNEL);
if (entry->urb_info_array == NULL) {
kfree(entry);
return -ENOMEM;
}
/* Initialize the common fields of all of the urb_info structs */
for (i = 0; i < entry->urb_info_count; i++) {
entry->urb_info_array[i].urb_index = i;
entry->urb_info_array[i].stream = stream;
entry->urb_info_array[i].entry = entry;
entry->urb_info_array[i].purb = usb_alloc_urb(0, GFP_KERNEL);
if (entry->urb_info_array[i].purb == NULL) {
ret = -ENOMEM;
goto error;
}
}
/* Initialize the leader */
i = 0;
ret = allocate_urb_buffer(stream, &entry->urb_info_array[i++],
stream->config.max_leader_size, sizeof(struct leader_header));
if (ret != 0)
goto error;
/* Initialize the buffer offsets for configuring the URBs */
image_offset = 0;
chunk_data_offset = 0;
while ((i < stream->config.payload_count + 1) &&
(image_offset < stream->config.image_buffer_size)) {
ret = map_urb_buffer(stream, &entry->urb_info_array[i++],
user_image_buffer, stream->config.image_buffer_size,
image_offset, stream->config.payload_size,
stream->config.payload_size);
if (ret != 0)
goto error;
image_offset += stream->config.payload_size;
}
/*
* If we account for the total image size before getting to
* the last payload transfer then switch to the chunk data
* buffer for the remaining payload transfer requests
*/
while (i < stream->config.payload_count + 1) {
ret = map_urb_buffer(stream, &entry->urb_info_array[i++],
user_chunk_data_buffer,
stream->config.chunk_data_buffer_size,
chunk_data_offset, stream->config.payload_size,
stream->config.payload_size);
if (ret != 0)
goto error;
chunk_data_offset += stream->config.payload_size;
}
/*
* Configure the final transfer 1 buffer if applicable. If we
* haven't already finished requesting all of the image data
* then use the image buffer. Otherwise, use the chunk data buffer.
*/
if (stream->config.transfer1_size != 0) {
if (image_offset < stream->config.image_buffer_size) {
ret = map_urb_buffer(stream,
&entry->urb_info_array[i++],
user_image_buffer,
stream->config.image_buffer_size,
image_offset, stream->config.transfer1_size,
stream->config.transfer1_size);
if (ret != 0)
goto error;
image_offset += stream->config.transfer1_size;
} else {
ret = map_urb_buffer(stream,
&entry->urb_info_array[i++],
user_chunk_data_buffer,
stream->config.chunk_data_buffer_size,
chunk_data_offset,
stream->config.transfer1_size,
stream->config.transfer1_size);
if (ret != 0)
goto error;
chunk_data_offset += stream->config.transfer1_size;
}
}
entry->transfer2_addr = NULL;
user_transfer2_addr = NULL;
/*
* Configure the final transfer 2 buffer if applicable. If the
* transfer 2 size is not equal to the transfer 2 data size, then
* use a scratch buffer. We will copy from the scratch buffer
* to the specified address or just transfer the data straight
* to the image or chunk data buffer
*/
if (stream->config.transfer2_size != 0) {
if (stream->config.transfer2_size !=
stream->config.transfer2_data_size) {
ret = allocate_urb_buffer(stream,
&entry->urb_info_array[i++],
stream->config.transfer2_size,
stream->config.transfer2_data_size);
if (ret != 0)
goto error;
if (image_offset < stream->config.image_buffer_size) {
user_transfer2_addr =
(u8 *)(user_image_buffer) +
image_offset;
image_offset +=
stream->config.transfer2_data_size;
} else {
user_transfer2_addr =
(u8 *)(user_chunk_data_buffer) +
chunk_data_offset;
chunk_data_offset +=
stream->config.transfer2_data_size;
}
/* lock user memory */
entry->urb_info_array[i - 1].pglist =
create_pg_list(stream,
user_transfer2_addr,
stream->config.transfer2_size);
entry->transfer2_addr = user_transfer2_addr;
} else {
if (image_offset < stream->config.image_buffer_size) {
ret = map_urb_buffer(stream,
&entry->urb_info_array[i++],
user_image_buffer,
stream->config.image_buffer_size,
image_offset,
stream->config.transfer2_size,
stream->config.transfer2_data_size);
if (ret != 0)
goto error;
image_offset +=
stream->config.transfer2_data_size;
} else {
ret = map_urb_buffer(stream,
&entry->urb_info_array[i++],
user_chunk_data_buffer,
stream->config.chunk_data_buffer_size,
chunk_data_offset,
stream->config.transfer2_size,
stream->config.transfer2_data_size);
if (ret != 0)
goto error;
chunk_data_offset +=
stream->config.transfer2_data_size;
}
}
}
/* Initialize the trailer */
ret = allocate_urb_buffer(stream, &entry->urb_info_array[i++],
stream->config.max_trailer_size,
sizeof(struct trailer_header));
if (ret != 0)
goto error;
if ((i != entry->urb_info_count) ||
(image_offset != stream->config.image_buffer_size) ||
(chunk_data_offset != stream->config.chunk_data_buffer_size)) {
dev_err(stream->u3v_dev->device,
"%s: error configuring buffer sizes\n", __func__);
ret = U3V_ERR_INTERNAL;
goto error;
}
init_completion(&entry->buffer_complete);
complete_all(&entry->buffer_complete);
atomic_set(&entry->callbacks_received, 0);
entry->state = u3v_idle;
entry->buffer_id = stream->next_buffer_id++;
put_user(entry->buffer_id, buffer_id);
entry->leader_received_size = 0;
entry->payload_received_size = 0;
entry->trailer_received_size = 0;
entry->status = 0;
mutex_lock(&stream->stream_lock);
buffer_entry_insert(stream, entry);
mutex_unlock(&stream->stream_lock);
return 0;
error:
for (i = 0; i < entry->urb_info_count; i++)
usb_free_urb(entry->urb_info_array[i].purb);
kfree(entry->urb_info_array);
kfree(entry);
return ret;
}
/*
* allocate_urb_buffer - this function populates a urb_info struct by
* allocating a kernel buffer. If a user buffer exists and needs to
* be mapped to the kernel, map_urb_buffer is used instead of this
* function
* @stream: pointer to the stream interface struct
* @urb_info: on return this struct will be initialized
* @size: size of the buffer to allocate
* @min_expected_size: minimum amount of data we expect to receive
*/
static int allocate_urb_buffer(struct u3v_stream *stream,
struct urb_info *urb_info, size_t size,
size_t min_expected_size)
{
if (urb_info == NULL)
return -EINVAL;
urb_info->buffer = kzalloc(size, GFP_KERNEL);
if (urb_info->buffer == NULL)
return -ENOMEM;
urb_info->kernel_allocated = true;
urb_info->pglist = NULL;
urb_info->buffer_size = size;
urb_info->min_expected_size = min_expected_size;
return 0;
}
/*
* map_urb_buffer - this function locks a segment of user memory, creates
* a corresponding scatter-gather list, and populates the urb_info
* struct accordingly. This function is an alternative to
* allocate_urb_buffer and is used to DMA directly into the user
* buffer without making a copy.
* @stream: pointer to the stream interface struct
* @urb_info: on return this struct will be initialized
* @user_buffer: user mode memory address of the buffer to lock
* @buffer_size: size of the user buffer
* @offset: offset into the user buffer
* @urb_buffer_size: size of the URB buffer
* @min_expected_size: minimum size for valid data
*/
static int map_urb_buffer(struct u3v_stream *stream,
struct urb_info *urb_info, void *user_buffer, size_t buffer_size,
size_t offset, size_t urb_buffer_size, size_t min_expected_size)
{
if (urb_info == NULL || user_buffer == NULL || buffer_size == 0)
return -EINVAL;
urb_info->kernel_allocated = false;
urb_info->pglist = create_pg_list(stream, user_buffer,
buffer_size);
if (urb_info->pglist == NULL)
return -ENOMEM;
urb_info->buffer = NULL;
urb_info->buffer_size = urb_buffer_size;
urb_info->min_expected_size = min_expected_size;
return 0;
}
/*
* u3v_unconfigure_buffer - finds the buffer with buffer_id,
* unlocks corresponding pagelists, and frees all resources associated
* with the buffer entry.
* @stream: pointer to the u3v_stream interface struct
* @buffer_id: id of the buffer to be destroyed
*/
int u3v_unconfigure_buffer(struct u3v_stream *stream, __u64 buffer_id)
{
int ret;
if (stream == NULL)
return -EINVAL;
mutex_lock(&stream->stream_lock);
ret = destroy_buffer(stream, buffer_id);
mutex_unlock(&stream->stream_lock);
return ret;
}
/*
* destroy_buffer - helper function that finds a buffer entry by id,
* removes it from the list of buffers, and frees its memory
* precondition: caller must hold stream_lock
* @stream: pointer to the u3v_stream interface struct
* @buffer_id: id of the buffer to be destroyed
*/
static int destroy_buffer(struct u3v_stream *stream, __u64 buffer_id)
{
struct buffer_entry *entry;
struct device *dev;
int i;
if (stream == NULL)
return -EINVAL;
dev = stream->u3v_dev->device;
entry = search_buffer_entries(stream, buffer_id);
if (entry == NULL) {
dev_err(dev, "%s: Failed to find entry with buffer id %llu\n",
__func__, buffer_id);
return -EINVAL;
}
if (entry->state == u3v_queued) {
dev_err(dev, "%s: Buffer is in use, cannot be unconfigured\n",
__func__);
return U3V_ERR_BUFFER_STILL_IN_USE;
}
rb_erase(&entry->node, &stream->root);
for (i = 0; i < entry->urb_info_count; i++)
destroy_urb(&entry->urb_info_array[i]);
kfree(entry->urb_info_array);
kfree(entry);
return 0;
}
/*
* destroy_urb - helper function that handles the destruction of a urb.
* If the urb was mapped from user memory, we destroy the page list
* and unlock the memory. If it was allocated from kernel memory,
* we just free that buffer
* precondition: caller must hold stream_lock
* @urb_info: pointer to struct containing a urb pointer and its metadata
*/
static void destroy_urb(struct urb_info *urb_info)
{
if (urb_info == NULL)
return;
kfree(urb_info->purb);
if (urb_info->kernel_allocated)
kfree(urb_info->buffer);
if (urb_info->pglist != NULL)
destroy_pg_list(urb_info->pglist, true);
}
/*
* u3v_queue_buffer - queues the buffer to the camera for it to be filled
* with image data
* @stream: pointer to the stream interface struct
* @buffer_id: on return, buffer with this id is queued
*/
int u3v_queue_buffer(struct u3v_stream *stream, __u64 buffer_id)
{
struct buffer_entry *entry;
struct device *dev = stream->u3v_dev->device;
int ret = 0;
int i = 0;
if (stream == NULL)
return -EINVAL;
dev_vdbg(dev, "%s: buffer id = %llu\n", __func__, buffer_id);
mutex_lock(&stream->stream_lock);
entry = search_buffer_entries(stream, buffer_id);
if (entry == NULL) {
dev_err(dev, "%s: Failed to find entry with id %llu\n",
__func__, buffer_id);
ret = U3V_ERR_INTERNAL;
goto exit;
}
if (entry->state == u3v_queued) {
dev_err(dev, "%s: Error, buffer %llu is already queued\n",
__func__, buffer_id);
ret = U3V_ERR_INTERNAL;
goto exit;
}
reset_counters(entry);
entry->state = u3v_queued;
INIT_COMPLETION(entry->buffer_complete);
for (i = 0; i < entry->urb_info_count; i++) {
ret = submit_stream_urb(stream, &entry->urb_info_array[i]);
if (ret != 0)
goto reset_stream_exit;
}
exit:
mutex_unlock(&stream->stream_lock);
return ret;
reset_stream_exit:
entry->state = u3v_cancelled;
complete_all(&entry->buffer_complete);
reset_stream(stream);
mutex_unlock(&stream->stream_lock);
return ret;
}
/*
* reset_counters - helper function that resets values in a buffer
* entry so that it can be resubmitted
* precondition: caller must hold stream_lock
* @entry: buffer to be reset
*/
static void reset_counters(struct buffer_entry *entry)
{
entry->state = u3v_idle;
atomic_set(&entry->callbacks_received, 0);
entry->incomplete_callbacks_received = 0;
entry->leader_received_size = 0;
entry->payload_received_size = 0;
entry->trailer_received_size = 0;
entry->status = 0;
}
/*
* submit_stream_urb - queues a buffer to the stream endpoint and
* increments the number of outstanding URBS
* precondition: caller must hold stream_lock
* @stream: pointer to the stream interface struct
* @urb_info: pointer to the urb_info struct that contains the
* urb to be submitted
*/
static int submit_stream_urb(struct u3v_stream *stream,
struct urb_info *urb_info)
{
struct device *dev;
struct usb_device *udev;
int ret;
if (stream == NULL || urb_info == NULL || urb_info->purb == NULL)
return -EINVAL;
dev = stream->u3v_dev->device;
udev = stream->u3v_dev->udev;
if (urb_info->kernel_allocated && (urb_info->buffer == NULL)) {
dev_err(dev, "%s: NULL buffer for kernel URB\n", __func__);
return -EINVAL;
}
if (!urb_info->kernel_allocated && (urb_info->pglist == NULL)) {
dev_err(dev, "%s: NULL page list for user buffer URB\n",
__func__);
return -EINVAL;
}
usb_fill_bulk_urb(urb_info->purb, udev,
usb_rcvbulkpipe(udev,
usb_endpoint_num(stream->u3v_dev->stream_info.bulk_in)),
urb_info->buffer, urb_info->buffer_size,
stream_urb_completion, urb_info);
/*
* If we are setting up zero-copy DMA for user memory,
* we want to use the sg list we created
*/
if (!urb_info->kernel_allocated) {
urb_info->purb->sg = urb_info->pglist->sgt->sgl;
urb_info->purb->num_sgs = urb_info->pglist->sgt->nents;
}
usb_anchor_urb(urb_info->purb, &stream->stream_anchor);
ret = usb_submit_urb(urb_info->purb, GFP_KERNEL);
if (ret != 0) {
usb_unanchor_urb(urb_info->purb);
dev_err(dev, "%s: Error %d submitting urb\n", __func__, ret);
}
return ret;
}
/*
* stream_urb_completion - This function executes when the transfer
* finishes, fails, or is cancelled. It runs in interrupt context,
* and keeps track of when a buffer entry has received all of its
* expected callbacks. Once the callbacks have all been received, it
* signals that it is complete so that wait_for_buffer can finish.
* @purb: pointer to the urb that has been completed
*/
static void stream_urb_completion(struct urb *purb)
{
struct urb_info *urb_info;
struct buffer_entry *entry;
struct device *dev;
int idx;
size_t len;
int urb_count;
/* check pointers */
if (purb == NULL || purb->context == NULL) {
pr_err("%s: Received an invalid urb pointer\n", __func__);
return;
}
dev = &purb->dev->dev;
/* check status */
if (purb->status &&
!(purb->status == -ENOENT ||
purb->status == -ECONNRESET ||
purb->status == -ESHUTDOWN ||
purb->status == -EPROTO ||
purb->status == -EPIPE)) {
dev_err(dev, "%s: Received nonzero urb completion status: %d",
__func__, purb->status);
}
urb_info = (struct urb_info *)(purb->context);
if (urb_info == NULL) {
dev_err(dev, "%s: Received a NULL context pointer\n",
__func__);
return;
}
entry = urb_info->entry;
if (entry == NULL) {
dev_err(dev, "%s: Received a NULL buffer entry pointer\n",
__func__);
return;
}
if (entry->state == u3v_cancelled)
return;
/* If status is already bad, don't overwrite */
if (purb->status != 0)
entry->status = purb->status;
len = purb->actual_length;
dev_vdbg(dev, "%s: urb %d: length = %zu, expected >=%zu\n",