-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathu3v_stream.c
2412 lines (2129 loc) · 69.2 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/math64.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 */
enum u3v_buffer_state {
u3v_idle, u3v_queued, u3v_complete, u3v_cancelled
};
struct u3v_buffer {
u32 urb_info_count;
u32 incomplete_callbacks_received;
u32 leader_received_size;
u32 payload_received_size;
u32 trailer_received_size;
u32 transfer2_received_size;
void __user *u_transfer2_addr;
u64 buffer_id;
atomic_t callbacks_received;
int status;
enum u3v_buffer_state state;
struct u3v_urb_info *urb_info_array;
struct completion buffer_complete;
struct rb_node node;
};
struct u3v_urb_info {
u32 urb_index;
u32 num_pglists;
u32 buffer_size;
u32 expected_size;
struct u3v_stream *stream;
struct u3v_buffer *entry;
struct urb *purb;
struct u3v_pglist **pglists;
struct sg_table *sgt;
};
struct u3v_pglist {
bool kernel_allocated;
bool segmented_xfer;
u32 num_host_bytes;
u32 num_device_bytes;
u32 num_pages;
u32 page_offset;
u64 host_offset;
u64 device_offset;
struct page **pages;
};
/*
* Inline helper function that returns the number of pages in a num_bytes
* sized chunk of memory starting at addr.
*/
static inline unsigned long get_num_pages(void *ptr, u32 num_bytes)
{
uintptr_t addr = (uintptr_t)(ptr);
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 */
/* Functions related to u3v_create_stream */
static int calculate_urb_sizes(struct u3v_stream *stream,
struct u3v_configure_stream2 *config_stream,
u32 req_leader_size, u32 req_trailer_size);
/* Functions related to u3v_configure_buffer */
static int create_buffer(struct u3v_stream *stream,
void *u_image_buffer,
void *u_chunk_data_buffer,
u64 *buffer_id);
static int configure_urbs(struct u3v_stream *stream,
struct u3v_buffer *entry, void __user *u_image_buffer,
void __user *u_chunk_data_buffer);
static int configure_urbs_sg_constraint(struct u3v_stream *stream,
struct u3v_buffer *entry, void __user *u_image_buffer,
void __user *u_chunk_data_buffer);
static u32 num_host_buffer_bytes(u32 device_bytes, u64 device_image_offset,
u32 segment_padding, u64 segment_size);
static int add_buffer_to_urb(struct u3v_stream *stream,
struct u3v_urb_info *urb_info, void __user *u_buffer, u64 host_offset,
u64 device_offset, u32 buffer_size, u32 expected_size,
bool use_padding);
static struct u3v_pglist *create_pglist(struct u3v_stream *stream,
void __user *u_buffer, u64 host_offset, u64 device_offset,
u32 num_bytes, u32 segment_padding, u64 segment_size);
static struct page **lock_user_pages(struct u3v_stream *stream,
void __user *u_buffer, u32 num_bytes, u32 num_pages);
static int create_sglist(struct u3v_stream *stream,
struct u3v_urb_info *urb_info);
static int calculate_sglist_entries(struct u3v_stream *stream,
struct u3v_urb_info *urb_info, u32 *num_sgs, bool configure);
static int num_contiguous_bytes(struct u3v_pglist *pglist, u32 pg_idx,
u32 offset, u32 limit);
/* Functions related to u3v_unconfigure_buffer */
static int destroy_buffer(struct u3v_stream *stream, u64 buffer_id);
static void destroy_urb(struct u3v_urb_info *urb_info);
static void destroy_pglist(struct u3v_pglist *pglist, bool dirty);
static void unlock_user_pages(struct u3v_pglist *pglist, int dirty);
static void destroy_sglist(struct u3v_urb_info *urb_info);
/* Functions related to u3v_queue_buffer */
static void reset_counters(struct u3v_buffer *entry);
static int submit_stream_urb(struct u3v_stream *stream,
struct u3v_urb_info *urb_info);
/* Callback function for when urb is transferred */
static void stream_urb_completion(struct urb *purb);
/* Function related to u3v_cancel_all_buffers */
static int reset_stream(struct u3v_stream *stream);
/*
* Functions for translating to and from an array of struct page *
* and a kernel address
*/
static struct page **kernel_addr_to_pages(void *addr, u32 num_pages);
static void *pglist_to_kernel_addr(struct u3v_pglist *pglist);
/* Functions for accessing the rb tree of buffer entries */
static struct u3v_buffer *search_buffer_entries(struct u3v_stream *stream,
u64 value);
static void buffer_entry_insert(struct u3v_stream *stream,
struct u3v_buffer *new);
/*
* Math functions to help with 64 bit operations to avoid __aeabi_uldivmod
* unresolved symbol for 32 bit OSes
*/
static u32 u3v_u64_mod(u64 dividend, u32 divisor);
static u64 u3v_u64_roundup(u64 value, u32 alignment);
static u64 u3v_u64_rounddown(u64 value, u32 alignment);
/*
* u3v_create_stream - initializes the stream interface
*
* @u3v: pointer to the u3v_device struct
* @intf: pointer to the stream interface struct
* @config_stream: pointer to the struct with stream configuration data
* from usermode
* @req_leader_size: minimum required leader size, which was previously
* read from a camera register
* @req_trailer_size: minimum required trailer size, which was previously
* read from a camera register
*/
int u3v_create_stream(struct u3v_device *u3v, struct usb_interface *intf,
struct u3v_configure_stream2 *config_stream, u32 req_leader_size,
u32 req_trailer_size)
{
struct u3v_stream *stream = NULL;
struct device *dev = NULL;
int ret = 0;
if (u3v == NULL)
return -EINVAL;
dev = u3v->device;
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(struct u3v_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);
ret = calculate_urb_sizes(stream, config_stream,
req_leader_size, req_trailer_size);
if (ret != 0)
goto error;
u3v->stream_info.interface_ptr = stream;
return 0;
error:
kfree(stream);
return ret;
}
/*
* calculate_urb_sizes - this function determines the stream interface urb
* configuration
*
* @stream: pointer to the stream interface
* @config_stream: pointer to the struct with stream configuration data
* from usermode
* @req_leader_size: minimum required leader size, which was previously
* read from a camera register
* @req_trailer_size: minimum required trailer size, which was previously
* read from a camera register
*/
static int calculate_urb_sizes(struct u3v_stream *stream,
struct u3v_configure_stream2 *config_stream,
u32 req_leader_size, u32 req_trailer_size)
{
struct u3v_device *u3v = stream->u3v_dev;
struct device *dev = u3v->device;
u32 byte_alignment = u3v->u3v_info->transfer_alignment;
u32 payload_size = 0;
u32 payload_count = 0;
u32 chunk_payload_count = 0;
u32 transfer1_size = 0;
u32 max_pglist_count = 0;
u32 transfer2_size = 0;
u32 transfer2_data_size = 0;
u32 max_transfer_size = 0;
u32 aligned_max_transfer_size = 0;
u32 aligned_cd_buffer_size = 0;
u32 max_leader_size = 0;
u32 max_trailer_size = 0;
u32 num_segments = 0;
u32 alignment_padding = 0;
u32 unaligned_bytes = 0;
u32 max_urb_size = config_stream->max_urb_size;
u32 segment_padding = config_stream->segment_padding;
u64 segment_size = config_stream->segment_size;
u64 total_transfer_size = 0;
u64 host_image_buffer_size = config_stream->image_buffer_size;
u64 device_image_buffer_size = 0;
u64 chunk_data_buffer_size = config_stream->chunk_data_buffer_size;
u64 image_bytes_left = 0;
u64 chunk_bytes_left = 0;
u64 total_bytes_left = 0;
bool sg_constraint = false;
int ret = 0;
if (host_image_buffer_size == 0) {
dev_err(dev,
"%s: Error, image buffer size must be greater than 0\n",
__func__);
return -EINVAL;
}
if (segment_size == 0 || segment_size > host_image_buffer_size) {
dev_err(dev,
"%s: Error, segment size is %llu, expected between 1 and %llu\n",
__func__, segment_size, host_image_buffer_size);
return -EINVAL;
}
/*
* Go down the sg constraint path unless sysfs attribute to enable
* segmented transfers is set.
*/
sg_constraint = !(u3v->u3v_info->segmented_xfer_supported &&
stream->u3v_dev->u3v_info->segmented_xfer_enabled);
if (sg_constraint && (segment_padding != 0)) {
dev_err(dev,
"%s: segment padding is %u, but segmented transfers are not supported on this system\n",
__func__, segment_padding);
return U3V_ERR_SEGMENTED_XFER_NOT_AVAIL;
}
num_segments = div64_u64(host_image_buffer_size,
(segment_size + segment_padding));
device_image_buffer_size = num_segments * segment_size;
image_bytes_left = device_image_buffer_size;
max_transfer_size = min(u3v->u3v_info->os_max_transfer_size,
max_urb_size);
aligned_max_transfer_size = rounddown(max_transfer_size,
byte_alignment);
max_leader_size = min(roundup(req_leader_size, byte_alignment),
aligned_max_transfer_size);
max_trailer_size = min(roundup(req_trailer_size, byte_alignment),
aligned_max_transfer_size);
payload_size = aligned_max_transfer_size;
if (sg_constraint) {
payload_count =
div_u64(device_image_buffer_size, payload_size);
image_bytes_left -= payload_count * payload_size;
transfer1_size =
u3v_u64_rounddown(image_bytes_left, byte_alignment);
image_bytes_left -= transfer1_size;
transfer2_size =
u3v_u64_roundup(image_bytes_left, byte_alignment);
/* If there is a separate chunk data buffer, then the chunk
* data size will be > 0. In this case, image data must fit
* into the transfer buffers or the final transfer 1 payload.
* If final transfer 1 is being used for image data, then the
* chunk data must fit into a single URB. Chunk data does not
* have to be aligned because it's a small amount of data
* compared to the image. In that case we are okay using the
* final transfer 2 buffer for the transfer and then copying
* the data into the chunk data buffer.
*/
if (chunk_data_buffer_size != 0) {
if (transfer2_size != 0) {
dev_err(dev,
"%s: Cannot split the image and chunk data into separate buffers because the image size is not a multiple of the required transfer alignment size\n",
__func__);
return U3V_ERR_IMAGE_SIZE_NOT_ALIGNED;
}
aligned_cd_buffer_size = u3v_u64_roundup(
chunk_data_buffer_size, byte_alignment);
if (transfer1_size != 0 &&
(aligned_cd_buffer_size >
aligned_max_transfer_size)) {
dev_err(dev, "%s: Chunk data size too big\n",
__func__);
return U3V_ERR_CHUNK_DATA_SIZE_TOO_BIG;
}
/*
* If all of the image data is in the equally sized
* payload transfers, the chunk data can potentially
* use equally sized transfers as well as transfers
* 1 and 2
*/
if (transfer1_size == 0) {
chunk_bytes_left = chunk_data_buffer_size;
chunk_payload_count = div_u64(chunk_bytes_left,
payload_size);
payload_count += chunk_payload_count;
chunk_bytes_left -=
chunk_payload_count * payload_size;
transfer1_size = u3v_u64_rounddown(
chunk_bytes_left, byte_alignment);
chunk_bytes_left -= transfer1_size;
transfer2_size = u3v_u64_roundup(
chunk_bytes_left, byte_alignment);
} else {
transfer2_size = aligned_cd_buffer_size;
}
}
max_pglist_count = 1;
} else {
/*
* If we don't have restrictions on the size of scatterlist
* entries, we can have multiple buffers in the same urb
*/
total_transfer_size = device_image_buffer_size +
chunk_data_buffer_size;
total_bytes_left = total_transfer_size;
payload_size = aligned_max_transfer_size;
payload_count = div_u64(total_transfer_size,
payload_size);
total_bytes_left -= payload_size * payload_count;
/*
* If we don't meet the device's alignment requirements,
* we will need to allocate some extra kernel memory
* to tack on to the end
*/
unaligned_bytes = u3v_u64_mod(total_bytes_left,
byte_alignment);
alignment_padding = unaligned_bytes ?
byte_alignment - unaligned_bytes : 0;
transfer1_size = total_bytes_left ?
u3v_u64_roundup(total_bytes_left, byte_alignment) : 0;
transfer2_size = 0;
/* There will at least be an image buffer pglist */
max_pglist_count = 1;
if (chunk_data_buffer_size > 0)
max_pglist_count++;
if (alignment_padding != 0)
max_pglist_count++;
}
/*
* If the image size is smaller than the device's alignment
* requirements, update the segment_size to at least be
* as large as that minimum
*/
if (segment_padding == 0 && segment_size < byte_alignment)
segment_size = byte_alignment;
/*
* Calculate how much data we actually expect to be valid in the
* final transfer 2 buffer
*/
if ((device_image_buffer_size + chunk_data_buffer_size +
alignment_padding) < ((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 is at least %u\n",
__func__,
(device_image_buffer_size + chunk_data_buffer_size +
alignment_padding), ((payload_size * payload_count) +
transfer1_size));
return -EINVAL;
}
transfer2_data_size =
((device_image_buffer_size + chunk_data_buffer_size +
alignment_padding) - ((payload_size * payload_count) +
transfer1_size));
/* Validate calculated sizes */
dev_dbg(dev, "%s: host image buffer size = %llu\n",
__func__, host_image_buffer_size);
dev_dbg(dev, "%s: device image buffer size = %llu\n",
__func__, device_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);
dev_dbg(dev, "%s: transfer2_data = %u\n",
__func__, transfer2_data_size);
dev_dbg(dev, "%s: max_pglist_count = %u\n",
__func__, max_pglist_count);
dev_dbg(dev, "%s: alignment_padding = %u\n",
__func__, alignment_padding);
dev_dbg(dev, "%s: segment_padding = %u\n",
__func__, segment_padding);
dev_dbg(dev, "%s: segment_size = %llu\n",
__func__, segment_size);
dev_dbg(dev, "%s: sg_constraint = %s\n",
__func__, (sg_constraint ? "true" : "false"));
if (device_image_buffer_size == 0 || max_leader_size == 0 ||
max_trailer_size == 0) {
dev_err(dev,
"%s: leader, trailer, and image buffer sizes cannot be 0\n",
__func__);
dev_err(dev, "\tdevice image buffer size is %llu\n",
device_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);
return -EINVAL;
}
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;
}
if ((device_image_buffer_size + chunk_data_buffer_size +
alignment_padding) < ((payload_size * payload_count) +
transfer1_size + transfer2_data_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__,
(device_image_buffer_size + chunk_data_buffer_size +
alignment_padding), ((payload_size * payload_count) +
transfer1_size + transfer2_data_size));
return -EINVAL;
}
/* The buffer sizes are valid, so now we store them */
stream->config.host_image_buffer_size = host_image_buffer_size;
stream->config.device_image_buffer_size = device_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.max_pglist_count = max_pglist_count;
stream->config.transfer2_size = transfer2_size;
stream->config.transfer2_data_size = transfer2_data_size;
stream->config.alignment_padding = alignment_padding;
stream->config.segment_padding = segment_padding;
stream->config.segment_size = segment_size;
stream->config.sg_constraint = sg_constraint;
if (config_stream->u_max_leader_size != NULL) {
ret = put_user(stream->config.max_leader_size,
config_stream->u_max_leader_size);
if (ret != 0) {
dev_err(dev,
"%s: Error copying max leader size to user\n",
__func__);
goto error;
}
}
if (config_stream->u_max_trailer_size != NULL) {
ret = put_user(stream->config.max_trailer_size,
config_stream->u_max_trailer_size);
if (ret != 0) {
dev_err(dev,
"%s: Error copying max trailer size to user\n",
__func__);
goto error;
}
}
return ret;
error:
/* caller already holds stream_info->interface_lock */
u3v_destroy_stream(u3v);
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 u3v_buffer *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 u3v_buffer, 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;
}
/*
* 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
* @u_image_buffer: user mode memory address of the image buffer to lock
* @u_chunk_data_buffer: user mode memory address of the chunk data buffer
* to lock
* @u_buffer_id: on return, this user pointer will point to the buffer id
* value for the newly created buffer
*/
int u3v_configure_buffer(struct u3v_stream *stream,
void __user *u_image_buffer, void __user *u_chunk_data_buffer,
__u64 __user *u_buffer_id)
{
struct device *dev = NULL;
int ret = 0;
if (stream == NULL || u_image_buffer == NULL ||
u_buffer_id == NULL)
return -EINVAL;
dev = stream->u3v_dev->device;
if ((u_chunk_data_buffer == NULL &&
stream->config.chunk_data_buffer_size != 0) ||
(u_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(stream, u_image_buffer,
u_chunk_data_buffer, u_buffer_id);
return ret;
}
/*
* create_buffer - 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
* @u_image_buffer: memory address of the user image buffer to be locked
* @u_chunk_data_buffer: memory address of the user chunk data buffer to
* be locked
* @u_buffer_id: on return this user pointer points to valid buffer id
*/
static int create_buffer(struct u3v_stream *stream,
void __user *u_image_buffer,
void __user *u_chunk_data_buffer,
__u64 __user *u_buffer_id)
{
int i = 0;
int ret = 0;
struct u3v_buffer *entry = NULL;
struct device *dev = stream->u3v_dev->device;
entry = kzalloc(sizeof(struct u3v_buffer), 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 u3v_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 u3v_urb structs */
for (i = 0; i < entry->urb_info_count; i++) {
entry->urb_info_array[i].urb_index = i;
entry->urb_info_array[i].num_pglists = 0;
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;
}
entry->urb_info_array[i].pglists =
kzalloc((stream->config.max_pglist_count *
sizeof(struct u3v_pglist *)), GFP_KERNEL);
if (entry->urb_info_array[i].pglists == NULL) {
ret = -ENOMEM;
goto error;
}
entry->urb_info_array[i].sgt = NULL;
entry->urb_info_array[i].buffer_size = 0;
entry->urb_info_array[i].expected_size = 0;
}
if (stream->config.sg_constraint)
ret = configure_urbs_sg_constraint(stream, entry,
u_image_buffer, u_chunk_data_buffer);
else
ret = configure_urbs(stream, entry, u_image_buffer,
u_chunk_data_buffer);
if (ret != 0) {
dev_err(dev, "%s: Configuring urbs failed with error %d\n",
__func__, ret);
goto error;
}
for (i = 0; i < entry->urb_info_count; i++) {
ret = create_sglist(stream, &entry->urb_info_array[i]);
if (ret != 0) {
dev_err(dev,
"%s: Creating sglist failed with error %d\n",
__func__, ret);
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, u_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++)
destroy_urb(&entry->urb_info_array[i]);
kfree(entry->urb_info_array);
kfree(entry);
return ret;
}
/*
* configure_urbs - this function initializes the urb_info structs with
* corresponding pglists and sglists with the provided buffers.
*
* precondition: this function can only be called if sg_constraint is false
* @stream: pointer to the stream interface struct
* @entry: buffer entry in which to configure the urbs
* @u_image_buffer: memory address of the user image buffer to be locked
* @u_chunk_data_buffer: memory address of the user chunk data buffer to
* be locked
*/
static int configure_urbs(struct u3v_stream *stream,
struct u3v_buffer *entry, void __user *u_image_buffer,
void __user *u_chunk_data_buffer)
{
int ret = 0;
u32 urb_idx = 0;
u32 payload_idx = 0;
u32 payload_bytes_remaining = 0;
u32 transfer1_bytes_remaining = 0;
u32 alignment_padding = 0;
u32 buffer_size = 0;
u64 host_image_offset = 0;
u64 chunk_data_offset = 0;
u64 device_image_offset = 0;
u64 device_bytes_remaining = 0;
if (entry == NULL)
return -EINVAL;
alignment_padding = stream->config.alignment_padding;
/* leader */
ret = add_buffer_to_urb(stream, &entry->urb_info_array[urb_idx++],
NULL, 0, 0, stream->config.max_leader_size,
sizeof(struct leader_header), false);
if (ret != 0)
goto exit;
/* payloads */
while (payload_idx < stream->config.payload_count) {
payload_bytes_remaining = stream->config.payload_size;
if (host_image_offset <
stream->config.host_image_buffer_size) {
device_bytes_remaining =
stream->config.device_image_buffer_size -
device_image_offset;
buffer_size = min((device_bytes_remaining),
(u64)(stream->config.payload_size));
ret = add_buffer_to_urb(stream,
&entry->urb_info_array[urb_idx],
u_image_buffer, host_image_offset,
device_image_offset, buffer_size, buffer_size,
true);
if (ret != 0)
goto exit;
device_image_offset += buffer_size;
host_image_offset += num_host_buffer_bytes(
buffer_size, device_image_offset,
stream->config.segment_padding,
stream->config.segment_size);
payload_bytes_remaining -= buffer_size;
}
if (payload_bytes_remaining && chunk_data_offset <
stream->config.chunk_data_buffer_size) {
device_bytes_remaining =
stream->config.chunk_data_buffer_size -
chunk_data_offset;
buffer_size = min((device_bytes_remaining),
(u64)(payload_bytes_remaining));
ret = add_buffer_to_urb(stream,
&entry->urb_info_array[urb_idx],
u_chunk_data_buffer, chunk_data_offset,
chunk_data_offset, buffer_size, buffer_size,
false);
if (ret != 0)
goto exit;
chunk_data_offset += buffer_size;
payload_bytes_remaining -= buffer_size;
}
if (payload_bytes_remaining != 0) {
dev_err(stream->u3v_dev->device,
"%s: Error, unexpected result for calculated payload transfer bytes\n",
__func__);
ret = U3V_ERR_INTERNAL;
goto exit;
}
urb_idx++;
payload_idx++;
}
/* transfer1 */
device_bytes_remaining =
stream->config.device_image_buffer_size -
device_image_offset;
transfer1_bytes_remaining = stream->config.transfer1_size;
if (device_bytes_remaining) {
if (device_bytes_remaining > transfer1_bytes_remaining) {
dev_err(stream->u3v_dev->device,
"%s: Error, transfer1 bytes remaining is %u, but we still have %llu image bytes remaining\n",
__func__, transfer1_bytes_remaining,
device_bytes_remaining);
ret = U3V_ERR_INTERNAL;
goto exit;
}
ret = add_buffer_to_urb(stream,
&entry->urb_info_array[urb_idx],
u_image_buffer, host_image_offset,
device_image_offset, device_bytes_remaining,
device_bytes_remaining, true);
if (ret != 0)
goto exit;
host_image_offset += num_host_buffer_bytes(
device_bytes_remaining,
device_image_offset,
stream->config.segment_padding,
stream->config.segment_size);
device_image_offset += device_bytes_remaining;
transfer1_bytes_remaining -= device_bytes_remaining;
}
device_bytes_remaining =
stream->config.chunk_data_buffer_size - chunk_data_offset;
if (device_bytes_remaining) {
if (u_chunk_data_buffer == NULL) {
ret = -EINVAL;
goto exit;
}
if (device_bytes_remaining > transfer1_bytes_remaining) {
dev_err(stream->u3v_dev->device,
"%s: Error, transfer1 bytes remaining is %u, but we still have %llu chunk data bytes remaining\n",
__func__, transfer1_bytes_remaining,
device_bytes_remaining);
ret = U3V_ERR_INTERNAL;
goto exit;
}
ret = add_buffer_to_urb(stream,
&entry->urb_info_array[urb_idx],
u_chunk_data_buffer, chunk_data_offset,
chunk_data_offset, device_bytes_remaining,
device_bytes_remaining, false);
if (ret != 0)
goto exit;
chunk_data_offset += device_bytes_remaining;
transfer1_bytes_remaining -= device_bytes_remaining;
}
if (alignment_padding > 0) {
ret = add_buffer_to_urb(stream,
&entry->urb_info_array[urb_idx], NULL, 0, 0,
alignment_padding, 0, false);
if (ret != 0)
goto exit;
transfer1_bytes_remaining -= alignment_padding;
}
if (transfer1_bytes_remaining != 0) {
dev_err(stream->u3v_dev->device,
"%s: Error, unexpected result for calculated final transfer 1 bytes\n",
__func__);
ret = U3V_ERR_INTERNAL;
goto exit;
}
if (stream->config.transfer1_size)
urb_idx++;
/* trailer */
ret = add_buffer_to_urb(stream, &entry->urb_info_array[urb_idx++],
NULL, 0, 0, stream->config.max_trailer_size,
sizeof(struct trailer_header), false);
if ((urb_idx != entry->urb_info_count) ||
(host_image_offset != stream->config.host_image_buffer_size) ||
(device_image_offset != stream->config.device_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;
}
exit:
return ret;
}
/*
* configure_urbs_sg_constraint - this function initializes the urb_info
* structs with corresponding pglists and sglists with the provided
* buffers.
*
* precondition: this function can only be called if sg_constraint is true
* @stream: pointer to the stream interface struct
* @entry: buffer entry in which to configure the urbs
* @u_image_buffer: memory address of the user image buffer to be locked
* @u_chunk_data_buffer: memory address of the user chunk data buffer to
* be locked
*/
static int configure_urbs_sg_constraint(struct u3v_stream *stream,
struct u3v_buffer *entry, void __user *u_image_buffer,
void __user *u_chunk_data_buffer)
{
int i = 0;
int ret = 0;
u64 image_offset = 0;