forked from ni/usb3vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu3v_core.c
1542 lines (1333 loc) · 43.6 KB
/
u3v_core.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_core.c: Driver for USB3 Vision(TM) class devices
*
* (C) Copyright 2014 National Instruments Corp.
* Authors: Katie Ensign <katie.ensign@ni.com>,
* Jared Jenson <jared.jenson@ni.com>
*
* 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_control.h"
#include "u3v_stream.h"
#include "u3v_event.h"
#include "u3v_shared.h"
#include <linux/atomic.h>
#include <linux/device.h>
#include <linux/kernel.h>
#include <linux/lcm.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/sysfs.h>
#include <linux/types.h>
#include <linux/uaccess.h>
#include <linux/usb.h>
#include <linux/usb/hcd.h>
#include <linux/version.h>
#include <asm/unaligned.h>
enum direction_t {
dir_in,
dir_out
};
/* probe/disconnect and file operations */
static int u3v_probe(struct usb_interface *interface,
const struct usb_device_id *id);
static void u3v_disconnect(struct usb_interface *interface);
static int u3v_open(struct inode *inode, struct file *filp);
static int u3v_release(struct inode *inode, struct file *filp);
static long u3v_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
static void u3v_delete(struct kref *kref);
/* helper functions for probe */
static int enumerate_u3v_interfaces(struct u3v_device *u3v);
static int populate_u3v_properties(struct u3v_device *u3v);
static struct usb_endpoint_descriptor *find_bulk_endpoint(
struct usb_host_interface *iface_desc, enum direction_t dir);
/* device initialization */
static int initialize(struct u3v_device *u3v);
static int get_stream_capabilities(struct u3v_device *u3v);
/* device level ioctls and helper functions*/
static int u3v_get_os_max_transfer_size(struct u3v_device *u3v, __u32 *os_max);
static int u3v_get_stream_alignment(struct u3v_device *u3v, __u32 *alignment);
static int u3v_configure_stream(struct u3v_device *u3v,
__u64 image_buffer_size, __u64 chunk_data_buffer_size,
__u32 max_urb_size, __u32 *max_leader_size, __u32 *max_trailer_size);
static int read_stream_registers(struct u3v_device *u3v,
u32 *req_leader_size, u32 *req_trailer_size);
static int write_stream_registers(struct u3v_device *u3v,
u32 max_leader_size, u32 max_trailer_size, u32 payload_size,
u32 payload_count, u32 transfer1_size, u32 transfer2_size);
static u64 compatibility_div(u64 dividend, u32 divisor);
static u32 compatibility_mod(u64 dividend, u32 divisor);
/* helper function for reset_pipe */
static int reset_endpoint(struct u3v_device *u3v,
struct usb_endpoint_descriptor *ep);
#define GET_INTERFACE(ptr_type, ptr, interface_info) \
do { \
mutex_lock(&interface_info.interface_lock); \
mutex_lock(&interface_info.ioctl_count_lock); \
ptr = (ptr_type)(interface_info.interface_ptr); \
if (interface_info.ioctl_count++ == 0) \
INIT_COMPLETION(interface_info.ioctl_complete); \
mutex_unlock(&interface_info.ioctl_count_lock); \
mutex_unlock(&interface_info.interface_lock); \
} while (0)
#define PUT_INTERFACE(interface_info) \
do { \
mutex_lock(&interface_info.ioctl_count_lock); \
if (--interface_info.ioctl_count == 0) \
complete_all(&interface_info.ioctl_complete); \
mutex_unlock(&interface_info.ioctl_count_lock); \
} while (0)
/* sysfs attribute configuration */
#define u3v_attribute_num(name) \
static ssize_t name##_show(struct device *dev, \
struct device_attribute *attr, char *buf) \
{ \
struct usb_interface *intf = to_usb_interface(dev); \
struct u3v_device *u3v = usb_get_intfdata(intf); \
\
if (u3v == NULL || u3v->u3v_info == NULL) \
return 0; \
\
return scnprintf(buf, PAGE_SIZE, "%u\n", u3v->u3v_info->name); \
} \
static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL);
#define u3v_attribute_str(name) \
static ssize_t name##_show(struct device *dev, \
struct device_attribute *attr, char *buf) \
{ \
struct usb_interface *intf = to_usb_interface(dev); \
struct u3v_device *u3v = usb_get_intfdata(intf); \
\
if (u3v == NULL || u3v->u3v_info == NULL) \
return 0; \
\
return scnprintf(buf, PAGE_SIZE, "%s\n", u3v->u3v_info->name); \
} \
static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL);
#define udev_attribute_str(name) \
static ssize_t name##_show(struct device *dev, \
struct device_attribute *attr, char *buf) \
{ \
struct usb_interface *intf = to_usb_interface(dev); \
struct u3v_device *u3v = usb_get_intfdata(intf); \
struct usb_device *udev = u3v->udev; \
\
if (udev == NULL) \
return 0; \
\
return scnprintf(buf, PAGE_SIZE, "%s\n", udev->name); \
} \
static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL);
#define udev_desc_attribute(name) \
static ssize_t name##_show(struct device *dev, \
struct device_attribute *attr, char *buf) \
{ \
struct usb_interface *intf = to_usb_interface(dev); \
struct u3v_device *u3v = usb_get_intfdata(intf); \
struct usb_device *udev = u3v->udev; \
\
if (udev == NULL) \
return 0; \
\
return scnprintf(buf, PAGE_SIZE, "%u\n", \
le16_to_cpup(&udev->descriptor.name)); \
} \
static DEVICE_ATTR(name, S_IRUGO, name##_show, NULL);
udev_desc_attribute(idVendor);
udev_desc_attribute(idProduct);
udev_attribute_str(manufacturer);
udev_attribute_str(product);
udev_attribute_str(serial);
u3v_attribute_num(gen_cp_version);
u3v_attribute_num(u3v_version);
u3v_attribute_str(device_guid);
u3v_attribute_str(vendor_name);
u3v_attribute_str(model_name);
u3v_attribute_str(family_name);
u3v_attribute_str(device_version);
u3v_attribute_str(manufacturer_info);
u3v_attribute_str(serial_number_u3v);
u3v_attribute_str(user_defined_name);
u3v_attribute_num(speed_support);
u3v_attribute_num(previously_initialized);
u3v_attribute_num(host_byte_alignment);
u3v_attribute_num(os_max_transfer_size);
static struct attribute *u3v_attrs[] = {
&dev_attr_idVendor.attr,
&dev_attr_idProduct.attr,
&dev_attr_manufacturer.attr,
&dev_attr_product.attr,
&dev_attr_serial.attr,
&dev_attr_gen_cp_version.attr,
&dev_attr_u3v_version.attr,
&dev_attr_device_guid.attr,
&dev_attr_vendor_name.attr,
&dev_attr_model_name.attr,
&dev_attr_family_name.attr,
&dev_attr_device_version.attr,
&dev_attr_manufacturer_info.attr,
&dev_attr_serial_number_u3v.attr,
&dev_attr_user_defined_name.attr,
&dev_attr_speed_support.attr,
&dev_attr_previously_initialized.attr,
&dev_attr_host_byte_alignment.attr,
&dev_attr_os_max_transfer_size.attr,
NULL,
};
static struct attribute_group u3v_attr_grp = {
.attrs = u3v_attrs,
};
/* end of sysfs attributes */
/* Static structs */
static const struct file_operations fops = {
.owner = THIS_MODULE,
/* .read not implemented */
/* .write not implemented */
.open = u3v_open,
.release = u3v_release,
.unlocked_ioctl = u3v_ioctl,
.llseek = default_llseek,
};
static struct usb_class_driver u3v_class = {
.name = "u3v%d",
.fops = &fops,
/*
* This base is used when usb_register_dev is called in order to
* determine which minor number should be given out by the function.
* If CONFIG_USB_DYNAMIC_MINORS is set by the kernel (you can check
* in /proc/config.gz), then you can dynamically allocate the minor
* number out of the list of available ones. Ideally this would be
* set so that we could have more than 16 of a single type of
* device. However, it is not currently set, so we use this minor
* base to provide this minor number and the 15 subsequent numbers
* to be allocated.
*/
.minor_base = U3V_MINOR_BASE,
};
static struct usb_device_id u3v_table[] = {
{ USB_INTERFACE_INFO(U3V_INTERFACE_CLASS, U3V_INTERFACE_SUBCLASS,
U3V_INTERFACE_PROTOCOL_CONTROL) },
{} /* Terminating entry */
};
MODULE_DEVICE_TABLE(usb, u3v_table);
static struct usb_driver u3v_driver = {
.name = "u3v",
.id_table = u3v_table,
.probe = u3v_probe,
.disconnect = u3v_disconnect,
};
/* Functions */
/*
* u3v_ioctl - ioctl commands for the u3v driver
* @filp: pointer to the open device file
* @cmd: value indicating which ioctl command to execute
* @arg: pointer to a struct with additional data for ioctl, can be NULL
*/
static long u3v_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct u3v_read_memory *read_req;
struct u3v_write_memory *write_req;
struct u3v_get_stream_alignment *align;
struct u3v_get_os_max_transfer *os_max;
struct u3v_configure_stream *config_stream;
struct u3v_configure_buffer *config_req;
struct u3v_unconfigure_buffer *unconfig_req;
struct u3v_queue_buffer *queue_req;
struct u3v_wait_for_buffer *wait_req;
struct u3v_control_msg *ctrl_msg;
struct u3v_start_events *start_events;
struct u3v_wait_for_event *wait_event;
struct u3v_device *u3v;
struct u3v_control *control;
struct u3v_event *event;
struct u3v_stream *stream;
struct device *dev;
int ret = 0;
int err = 0;
if (!filp || !filp->private_data)
return -EINVAL;
u3v = filp->private_data;
dev = u3v->device;
if (!u3v->device_connected)
return -ENODEV;
dev_vdbg(dev, "%s: cmd = %X, filp = %p, arg = %p\n",
__func__, cmd, filp, (void *)(arg));
if (cmd != U3V_IOCTL_CTRL_MSG) {
ret = initialize(u3v);
if (ret != 0)
return ret;
}
if ((_IOC_TYPE(cmd) != U3V_MAGIC) || (_IOC_NR(cmd) > U3V_MAX_NR)) {
dev_err(dev, "%s: Received invalid command %X\n",
__func__, cmd);
return U3V_ERR_NOT_SUPPORTED;
}
/* Check safety of transfer between user and kernel space */
if (_IOC_DIR(cmd) & _IOC_READ)
err = !access_ok(VERIFY_WRITE, (void __user *)arg,
_IOC_SIZE(cmd));
else if (_IOC_DIR(cmd) & _IOC_WRITE)
err = !access_ok(VERIFY_READ, (void __user *)arg,
_IOC_SIZE(cmd));
if (err) {
dev_err(dev, "%s: Failure on access check for pointer %p",
__func__, (void *)(arg));
return -EFAULT;
}
switch (cmd) {
case U3V_IOCTL_READ:
read_req = (struct u3v_read_memory *)(arg);
if (!read_req)
goto error;
GET_INTERFACE(struct u3v_control *, control,
u3v->control_info);
ret = u3v_read_memory(control,
read_req->transfer_size,
read_req->bytes_read,
read_req->address, NULL,
read_req->user_buffer);
PUT_INTERFACE(u3v->control_info);
break;
case U3V_IOCTL_WRITE:
write_req = (struct u3v_write_memory *)(arg);
if (!write_req)
goto error;
GET_INTERFACE(struct u3v_control *, control,
u3v->control_info);
ret = u3v_write_memory(control,
write_req->transfer_size,
write_req->bytes_written,
write_req->address, NULL,
write_req->user_buffer);
PUT_INTERFACE(u3v->control_info);
break;
case U3V_IOCTL_GET_STREAM_ALIGNMENT:
align = (struct u3v_get_stream_alignment *)(arg);
if (!align)
goto error;
ret = u3v_get_stream_alignment(u3v, align->stream_alignment);
break;
case U3V_IOCTL_GET_OS_MAX_TRANSFER:
os_max = (struct u3v_get_os_max_transfer *)(arg);
if (!os_max)
goto error;
ret = u3v_get_os_max_transfer_size(u3v,
os_max->os_max_transfer_size);
break;
case U3V_IOCTL_CONFIGURE_STREAM:
config_stream = (struct u3v_configure_stream *)(arg);
if (!config_stream)
goto error;
mutex_lock(&u3v->stream_info.interface_lock);
ret = u3v_configure_stream(u3v,
config_stream->image_buffer_size,
config_stream->chunk_data_buffer_size,
config_stream->max_urb_size,
config_stream->max_leader_size,
config_stream->max_trailer_size);
mutex_unlock(&u3v->stream_info.interface_lock);
break;
case U3V_IOCTL_UNCONFIGURE_STREAM:
mutex_lock(&u3v->stream_info.interface_lock);
ret = u3v_destroy_stream(u3v);
mutex_unlock(&u3v->stream_info.interface_lock);
break;
case U3V_IOCTL_CONFIGURE_BUFFER:
config_req = (struct u3v_configure_buffer *)(arg);
if (!config_req)
goto error;
GET_INTERFACE(struct u3v_stream *, stream, u3v->stream_info);
ret = u3v_configure_buffer(stream,
config_req->user_image_buffer,
config_req->user_chunk_data_buffer,
config_req->buffer_handle);
PUT_INTERFACE(u3v->stream_info);
break;
case U3V_IOCTL_UNCONFIGURE_BUFFER:
unconfig_req = (struct u3v_unconfigure_buffer *)(arg);
if (!unconfig_req)
goto error;
GET_INTERFACE(struct u3v_stream *, stream, u3v->stream_info);
ret = u3v_unconfigure_buffer(stream,
unconfig_req->buffer_handle);
PUT_INTERFACE(u3v->stream_info);
break;
case U3V_IOCTL_QUEUE_BUFFER:
queue_req = (struct u3v_queue_buffer *)(arg);
if (!queue_req)
goto error;
GET_INTERFACE(struct u3v_stream *, stream, u3v->stream_info);
ret = u3v_queue_buffer(stream, queue_req->buffer_handle);
PUT_INTERFACE(u3v->stream_info);
break;
case U3V_IOCTL_WAIT_FOR_BUFFER:
wait_req = (struct u3v_wait_for_buffer *)(arg);
if (!wait_req)
goto error;
GET_INTERFACE(struct u3v_stream *, stream, u3v->stream_info);
ret = u3v_wait_for_buffer(stream,
wait_req->buffer_handle,
wait_req->user_leader_buffer,
wait_req->leader_size,
wait_req->user_trailer_buffer,
wait_req->trailer_size,
wait_req->buffer_complete_data);
PUT_INTERFACE(u3v->stream_info);
break;
case U3V_IOCTL_CANCEL_ALL_BUFFERS:
GET_INTERFACE(struct u3v_stream *, stream, u3v->stream_info);
ret = u3v_cancel_all_buffers(stream);
PUT_INTERFACE(u3v->stream_info);
break;
case U3V_IOCTL_CTRL_MSG:
ctrl_msg = (struct u3v_control_msg *)(arg);
if (!ctrl_msg)
goto error;
ret = u3v_control_msg(u3v->udev,
ctrl_msg->request, ctrl_msg->request_type,
ctrl_msg->value, ctrl_msg->index,
ctrl_msg->data, ctrl_msg->size);
break;
case U3V_IOCTL_START_EVENTS:
start_events = (struct u3v_start_events *)(arg);
if (!start_events)
goto error;
mutex_lock(&u3v->event_info.interface_lock);
ret = u3v_create_events(u3v, usb_ifnum_to_if(u3v->udev,
u3v->event_info.idx),
start_events->event_transfer_max_size,
start_events->event_queue_depth);
if (ret == 0) {
event = (struct u3v_event *)
(u3v->event_info.interface_ptr);
ret = u3v_start_events(event);
if (ret != 0)
u3v_destroy_events(u3v);
}
mutex_unlock(&u3v->event_info.interface_lock);
break;
case U3V_IOCTL_WAIT_FOR_EVENT:
wait_event = (struct u3v_wait_for_event *)(arg);
if (!wait_event)
goto error;
GET_INTERFACE(struct u3v_event *, event, u3v->event_info);
ret = u3v_wait_for_event(event,
wait_event->user_buffer,
wait_event->buffer_size,
wait_event->event_complete_buffer);
PUT_INTERFACE(u3v->event_info);
break;
case U3V_IOCTL_STOP_EVENTS:
mutex_lock(&u3v->event_info.interface_lock);
event = (struct u3v_event *)(u3v->event_info.interface_ptr);
u3v_stop_events(event);
u3v_destroy_events(u3v);
mutex_unlock(&u3v->event_info.interface_lock);
break;
}
return ret;
error:
dev_err(dev, "%s: Received a NULL pointer as an argument\n", __func__);
return -EINVAL;
}
/*
* u3v_open - called when a process tries to open the device file
*/
static int u3v_open(struct inode *inode, struct file *filp)
{
struct usb_interface *intf;
struct u3v_device *u3v;
intf = usb_find_interface(&u3v_driver, iminor(inode));
if (!intf) {
pr_err("%s: cannot find device for minor %d\n",
__func__, iminor(inode));
return -ENODEV;
}
u3v = usb_get_intfdata(intf);
/*
* Ensuring that device is being used by only one process
* at a time.
*/
if (!atomic_dec_and_test(&u3v->device_available)) {
atomic_inc(&u3v->device_available);
dev_err(u3v->device, "%s: device is not available\n",
__func__);
return -EBUSY;
}
kref_get(&u3v->kref);
filp->private_data = u3v;
return 0;
}
/*
* initialize - enumerates and initializes interfaces for this device
*/
static int initialize(struct u3v_device *u3v)
{
int ret = 0;
mutex_lock(&u3v->control_info.interface_lock);
if (u3v->control_info.interface_ptr != NULL) {
mutex_unlock(&u3v->control_info.interface_lock);
return 0;
}
ret = u3v_create_control(u3v);
mutex_unlock(&u3v->control_info.interface_lock);
if (ret != 0)
return ret;
ret = get_stream_capabilities(u3v);
/*
* get_stream_capabilities only fails if the reads fail.
* If streaming is actually not supported, it will still
* return 0 and we will not destroy the control interface.
* Initialization succeeds, but any calls to create or use
* the stream interface will fail.
*/
if (ret != 0) {
mutex_lock(&u3v->control_info.interface_lock);
u3v_destroy_control(u3v);
mutex_unlock(&u3v->control_info.interface_lock);
} else {
u3v->u3v_info->previously_initialized = 1;
}
return ret;
}
/*
* get_stream_capabilities - helper function for initialize that reads
* relevant stream registers to store configuration information
* @u3v: pointer to the u3v_device struct. If stream is supported,
* on return sirm_addr and transfer_alignment are set in
* u3v_device_info
*/
static int get_stream_capabilities(struct u3v_device *u3v)
{
struct u3v_control *control;
struct device *dev;
u64 sbrm_address;
u64 u3v_capability;
u32 si_info;
u32 device_byte_alignment;
int bytes_read;
int ret;
/*
* Control interface must already be set up so we can read
* the stream interface registers
*/
if (u3v == NULL || u3v->u3v_info == NULL)
return -EINVAL;
GET_INTERFACE(struct u3v_control *, control, u3v->control_info);
dev = u3v->device;
/* First get stream interface information */
ret = u3v_read_memory(control, sizeof(sbrm_address), &bytes_read,
ABRM_SBRM_ADDRESS, &sbrm_address, NULL);
if (ret != 0) {
dev_err(dev, "%s: Error reading SBRM address\n", __func__);
goto exit;
}
/* Check capabilities to see if SIRM is available */
ret = u3v_read_memory(control, sizeof(u3v_capability),
&bytes_read, sbrm_address + SBRM_U3VCP_CAPABILITY,
&u3v_capability, NULL);
if (ret != 0) {
dev_err(dev, "%s: Error reading U3VCP capability\n", __func__);
goto exit;
}
if (u3v_capability & SIRM_AVAILABLE_MASK) {
ret = u3v_read_memory(control,
sizeof(u3v->u3v_info->sirm_addr),
&bytes_read, sbrm_address + SBRM_SIRM_ADDRESS,
&u3v->u3v_info->sirm_addr, NULL);
if (ret != 0) {
dev_err(dev, "%s: Error reading SIRM address\n",
__func__);
goto exit;
}
ret = u3v_read_memory(control, sizeof(si_info),
&bytes_read, u3v->u3v_info->sirm_addr + SI_INFO,
&si_info, NULL);
if (ret != 0) {
dev_err(dev, "%s: Error reading SI info\n", __func__);
goto exit;
}
device_byte_alignment = 1 <<
((si_info & SI_INFO_ALIGNMENT_MASK) >>
SI_INFO_ALIGNMENT_SHIFT);
u3v->u3v_info->transfer_alignment = lcm(device_byte_alignment,
u3v->u3v_info->host_byte_alignment);
dev_vdbg(dev, "%s: transfer alignment is %u\n", __func__,
u3v->u3v_info->transfer_alignment);
}
exit:
PUT_INTERFACE(u3v->control_info);
return ret;
}
/*
* u3v_configure_stream - sets up the stream interface for the device
* @u3v: pointer to the u3v_device 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_urb_size: this parameter can be used to limit the size of the
* URBs if necessary. If you want to use the max size supported
* by the OS, set this value to uint max
* @max_leader_size: on return this will contain the size of the
* buffer that the caller must allocate to guarantee enough
* space to hold all of the leader data. This size will account
* for any alignment restrictions
* @max_trailer_size: on return this will contain the size of the
* buffer that the caller must allocate to guarantee enough space
* to hold all of the trailer data. This size will account for
* any alignment restrictions.
*/
static int u3v_configure_stream(struct u3v_device *u3v,
__u64 image_buffer_size, __u64 chunk_data_buffer_size,
__u32 max_urb_size, __u32 *max_leader_size, __u32 *max_trailer_size)
{
struct device *dev;
u32 req_leader_size = 0;
u32 req_trailer_size = 0;
u32 byte_alignment = 0;
u32 payload_size = 0;
u32 payload_count = 0;
u32 transfer1_size = 0;
u32 transfer2_size = 0;
u32 aligned_max_transfer_size = 0;
u32 aligned_cd_buffer_size = 0;
u32 k_max_leader_size = 0;
u32 k_max_trailer_size = 0;
int ret = 0;
if (u3v == NULL)
return -EINVAL;
dev = u3v->device;
ret = read_stream_registers(u3v, &req_leader_size, &req_trailer_size);
if (ret != 0)
return ret;
byte_alignment = u3v->u3v_info->transfer_alignment;
aligned_max_transfer_size = (max_urb_size / byte_alignment) *
byte_alignment;
k_max_leader_size = ((req_leader_size + byte_alignment - 1) /
byte_alignment) * byte_alignment;
k_max_leader_size = min(k_max_leader_size,
aligned_max_transfer_size);
if (max_leader_size != NULL) {
ret = put_user(k_max_leader_size, max_leader_size);
if (ret != 0) {
dev_err(dev,
"%s: Error copying max leader size to user\n",
__func__);
return ret;
}
}
payload_size = aligned_max_transfer_size;
payload_count = compatibility_div(image_buffer_size, payload_size);
transfer1_size = (compatibility_mod(image_buffer_size, payload_size) /
byte_alignment) * byte_alignment;
transfer2_size =
compatibility_mod(image_buffer_size, byte_alignment) == 0 ?
0 : byte_alignment;
k_max_trailer_size = ((req_trailer_size + byte_alignment - 1) /
byte_alignment) * byte_alignment;
k_max_trailer_size = min(k_max_trailer_size,
aligned_max_transfer_size);
if (max_trailer_size != NULL) {
ret = put_user(k_max_trailer_size, max_trailer_size);
if (ret != 0) {
dev_err(dev,
"%s: Error copying max trailer size to user\n",
__func__);
return ret;
}
}
/*
* If there is a separate chunk data buffer, then 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
* alignmed 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 =
compatibility_div((chunk_data_buffer_size +
byte_alignment - 1), byte_alignment) * byte_alignment;
dev_dbg(dev, "%s: aligned chunk data buffer size = %u\n",
__func__, aligned_cd_buffer_size);
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 (transfer1_size == 0) {
payload_count +=
compatibility_div(chunk_data_buffer_size,
payload_size);
transfer1_size =
(compatibility_mod(chunk_data_buffer_size,
payload_size) / byte_alignment) *
byte_alignment;
transfer2_size =
compatibility_mod(chunk_data_buffer_size,
byte_alignment) == 0 ? 0 : byte_alignment;
} else {
transfer2_size = aligned_cd_buffer_size;
}
}
/* now that the buffer sizes are configured, create the stream */
ret = u3v_create_stream(u3v, usb_ifnum_to_if(u3v->udev,
u3v->stream_info.idx), image_buffer_size,
chunk_data_buffer_size, k_max_leader_size,
k_max_trailer_size, payload_size, payload_count,
transfer1_size, transfer2_size);
if (ret != 0)
return ret;
ret = write_stream_registers(u3v, k_max_leader_size,
k_max_trailer_size, payload_size, payload_count,
transfer1_size, transfer2_size);
if (ret != 0) {
/* caller already holds stream_info->interface_lock */
u3v_destroy_stream(u3v);
}
return ret;
}
/*
* read_stream_registers - helper function for configure_stream that
* reads the required leader and trailer sizes from the device
* @u3v: pointer to the struct u3v_device
* @req_leader_size: on successful return, this will point to the
* value for the device's required leader size
* @req_trailer_size: on successful return, this will point to the
* value for the device's required trailer size
*/
static int read_stream_registers(struct u3v_device *u3v,
u32 *req_leader_size, u32 *req_trailer_size)
{
struct u3v_control *control;
struct device *dev;
u32 leader_size = 0;
u32 trailer_size = 0;
int bytes_read;
int ret;
if (u3v == NULL)
return -EINVAL;
dev = u3v->device;
GET_INTERFACE(struct u3v_control *, control, u3v->control_info);
/*
* Read the required size information. If device reports 0
* for the required leader and trailer size, we set it to
* at least 1024 bytes
*/
ret = u3v_read_memory(control, sizeof(leader_size),
&bytes_read, u3v->u3v_info->sirm_addr + SI_REQ_LEADER_SIZE,
req_leader_size, NULL);
if (ret != 0) {
dev_err(dev, "%s: Error reading required leader size\n",
__func__);
goto exit;
}
leader_size = max(leader_size, (u32)(1024));
ret = u3v_read_memory(control, sizeof(trailer_size),
&bytes_read, u3v->u3v_info->sirm_addr + SI_REQ_TRAILER_SIZE,
&trailer_size, NULL);
if (ret != 0) {
dev_err(dev, "%s: Error reading required trailer size\n",
__func__);
}
trailer_size = max(trailer_size, (u32)(1024));
*req_leader_size = leader_size;
*req_trailer_size = trailer_size;
exit:
PUT_INTERFACE(u3v->control_info);
return ret;
}
/*
* write_stream_registers - helper function for configure_stream that
* writes the buffer configuration information out to the device's
* stream registers. These sizes were validated previously when
* we created the stream interface.
* @u3v: pointer to the u3v_device struct
* @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 write_stream_registers(struct u3v_device *u3v,
u32 max_leader_size, u32 max_trailer_size, u32 payload_size,
u32 payload_count, u32 transfer1_size, u32 transfer2_size)
{
struct u3v_control *control;
struct device *dev;
u32 bytes_written;
int ret;
if (u3v == NULL)
return -EINVAL;
dev = u3v->device;
GET_INTERFACE(struct u3v_control *, control, u3v->control_info);
ret = u3v_write_memory(control, sizeof(max_leader_size),
&bytes_written, u3v->u3v_info->sirm_addr + SI_MAX_LEADER_SIZE,
&max_leader_size, NULL);
if (ret != 0)
goto error;
ret = u3v_write_memory(control, sizeof(payload_size),
&bytes_written, u3v->u3v_info->sirm_addr + SI_PAYLOAD_SIZE,
&payload_size, NULL);
if (ret != 0)
goto error;
ret = u3v_write_memory(control, sizeof(payload_count),
&bytes_written, u3v->u3v_info->sirm_addr + SI_PAYLOAD_COUNT,
&payload_count, NULL);
if (ret != 0)
goto error;
ret = u3v_write_memory(control, sizeof(transfer1_size),
&bytes_written, u3v->u3v_info->sirm_addr + SI_TRANSFER1_SIZE,
&transfer1_size, NULL);
if (ret != 0)
goto error;
ret = u3v_write_memory(control, sizeof(transfer2_size),
&bytes_written, u3v->u3v_info->sirm_addr + SI_TRANSFER2_SIZE,
&transfer2_size, NULL);
if (ret != 0)
goto error;
ret = u3v_write_memory(control, sizeof(max_trailer_size),
&bytes_written, u3v->u3v_info->sirm_addr + SI_MAX_TRAILER_SIZE,
&max_trailer_size, NULL);
if (ret != 0)
goto error;
PUT_INTERFACE(u3v->control_info);
return 0;
error:
dev_err(dev, "%s: Error setting stream interface registers\n",
__func__);
PUT_INTERFACE(u3v->control_info);
return ret;
}
/*
* u3v_control_msg - ioctl using control endpoint 0.
* Does not require the control interface.
* @udev: pointer to the usb device
* @request: request code value
* @requesttype: request type code value
* @value: message value
* @index: message index
* @data: user buffer
* @size: size of user buffer
*/
int u3v_control_msg(struct usb_device *udev, __u8 request, __u8 requesttype,
__u16 value, __u16 index, void *data, __u16 size)
{
int ret;
void *kdata = kzalloc(size, GFP_KERNEL);
if (!kdata)
return -ENOMEM;
/*
* usb_control_msg returns number of bytes transferred if successful,
* or a negative error code upon failure
*/
ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, U3V_CTRL_ENDPOINT),
request, requesttype, value, index, kdata, size, U3V_TIMEOUT);
if (ret != size)
return ret;
copy_to_user(data, kdata, size);
return 0;
}
/*
* compatibility_div - returns quotient. Created to use do_div without
* altering the dividend. Using do_div for 64 bit / 32 bit division
* to avoid __aebi_uldivmod unresolved symbol for 32 bit OSes.
*/
static u64 compatibility_div(u64 dividend, u32 divisor)
{
u64 dividend_copy = dividend;
do_div(dividend_copy, divisor);
return dividend_copy;
}
/*
* compatibility_mod - returns remainder. Created to use do_div without
* altering the dividend. Using do_div for 64 bit / 32 bit division
* to avoid __aebi_uldivmod unresolved symbol for 32 bit OSes.
*/