-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmi2021_main.c
1133 lines (936 loc) · 26.5 KB
/
smi2021_main.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
/************************************************************************
* smi2021_main.c *
* *
* USB Driver for SMI2021 - EasyCAP *
* **********************************************************************
*
* Copyright 2011-2013 Jon Arne Jørgensen
* <jonjon.arnearne--a.t--gmail.com>
*
* Copyright 2011, 2012 Tony Brown, Michal Demin, Jeffry Johnston
*
* 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, either 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, see <http://www.gnu.org/licenses/>.
*
* This driver is heavily influensed by the STK1160 driver.
* Copyright (C) 2012 Ezequiel Garcia
* <elezegarcia--a.t--gmail.com>
*
*/
#include "smi2021.h"
#define VENDOR_ID 0x1c88
#define BOOTLOADER_ID 0x0007
#define SMI2021_MODE_CTRL_HEAD 0x01
#define SMI2021_MODE_CAPTURE 0x05
#define SMI2021_MODE_STANDBY 0x03
#define SMI2021_REG_CTRL_HEAD 0x0b
static int smi2021_set_mode(struct smi2021 *smi2021, u8 mode)
{
int pipe, rc;
struct mode_ctrl_transfer {
u8 head;
u8 mode;
} *transfer_buf;
transfer_buf = kzalloc(sizeof(*transfer_buf), GFP_KERNEL);
if (!transfer_buf)
return -ENOMEM;
if (!smi2021->udev) {
rc = -ENODEV;
goto out;
}
transfer_buf->head = SMI2021_MODE_CTRL_HEAD;
transfer_buf->mode = mode;
pipe = usb_sndctrlpipe(smi2021->udev, SMI2021_USB_SNDPIPE);
rc = usb_control_msg(smi2021->udev, pipe, SMI2021_USB_REQUEST,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
transfer_buf->head, SMI2021_USB_INDEX,
transfer_buf, sizeof(*transfer_buf), 1000);
out:
kfree(transfer_buf);
return rc;
}
/*
* The smi2021 chip will handle two different types of register settings.
* Settings for the gm7113c chip via i2c or settings for the smi2021 chip.
* All settings are passed with the following struct.
* Some bits in data_offset and data_cntl parameters tells the device what
* kind of setting it's receiving and if it's a read or write request.
*/
struct smi2021_reg_ctrl_transfer {
u8 head;
u8 i2c_addr;
u8 data_cntl;
u8 data_offset;
u8 data_size;
union data {
u8 val;
struct i2c_data {
u8 reg;
u8 val;
} __packed i2c_data;
struct smi_data {
__be16 reg;
u8 val;
} __packed smi_data;
u8 reserved[8];
} __packed data;
} __packed;
static int smi2021_set_reg(struct smi2021 *smi2021, u8 i2c_addr,
u16 reg, u8 val)
{
struct smi2021_reg_ctrl_transfer *transfer_buf;
int rc, pipe;
static const struct smi2021_reg_ctrl_transfer smi_data = {
.head = SMI2021_REG_CTRL_HEAD,
.i2c_addr = 0x00,
.data_cntl = 0x00,
.data_offset = 0x82,
.data_size = sizeof(u8),
};
static const struct smi2021_reg_ctrl_transfer i2c_data = {
.head = SMI2021_REG_CTRL_HEAD,
.i2c_addr = 0x00,
.data_cntl = 0xc0,
.data_offset = 0x01,
.data_size = sizeof(u8)
};
if (!smi2021->udev) {
rc = -ENODEV;
goto out;
}
transfer_buf = kzalloc(sizeof(*transfer_buf), GFP_KERNEL);
if (!transfer_buf) {
rc = -ENOMEM;
goto out;
}
if (i2c_addr) {
memcpy(transfer_buf, &i2c_data, sizeof(*transfer_buf));
transfer_buf->i2c_addr = i2c_addr;
transfer_buf->data.i2c_data.reg = reg;
transfer_buf->data.i2c_data.val = val;
} else {
memcpy(transfer_buf, &smi_data, sizeof(*transfer_buf));
transfer_buf->data.smi_data.reg = cpu_to_be16(reg);
transfer_buf->data.smi_data.val = val;
}
pipe = usb_sndctrlpipe(smi2021->udev, SMI2021_USB_SNDPIPE);
rc = usb_control_msg(smi2021->udev, pipe, SMI2021_USB_REQUEST,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
transfer_buf->head, SMI2021_USB_INDEX,
transfer_buf, sizeof(*transfer_buf), 1000);
kfree(transfer_buf);
out:
return rc;
}
static int smi2021_get_reg(struct smi2021 *smi2021, u8 i2c_addr,
u16 reg, u8 *val)
{
int rc, pipe;
struct smi2021_reg_ctrl_transfer *transfer_buf;
static const struct smi2021_reg_ctrl_transfer i2c_prepare_read = {
.head = SMI2021_REG_CTRL_HEAD,
.i2c_addr = 0x00,
.data_cntl = 0x84,
.data_offset = 0x00,
.data_size = sizeof(u8)
};
static const struct smi2021_reg_ctrl_transfer smi_read = {
.head = SMI2021_REG_CTRL_HEAD,
.i2c_addr = 0x00,
.data_cntl = 0x20,
.data_offset = 0x82,
.data_size = sizeof(u8)
};
*val = 0;
if (!smi2021->udev) {
rc = -ENODEV;
goto out;
}
transfer_buf = kzalloc(sizeof(*transfer_buf), GFP_KERNEL);
if (!transfer_buf) {
rc = -ENOMEM;
goto out;
}
pipe = usb_sndctrlpipe(smi2021->udev, SMI2021_USB_SNDPIPE);
if (i2c_addr) {
memcpy(transfer_buf, &i2c_prepare_read, sizeof(*transfer_buf));
transfer_buf->i2c_addr = i2c_addr;
transfer_buf->data.i2c_data.reg = reg;
rc = usb_control_msg(smi2021->udev, pipe, SMI2021_USB_REQUEST,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
transfer_buf->head, SMI2021_USB_INDEX,
transfer_buf, sizeof(*transfer_buf), 1000);
if (rc < 0)
goto free_out;
transfer_buf->data_cntl = 0xa0;
} else {
memcpy(transfer_buf, &smi_read, sizeof(*transfer_buf));
transfer_buf->data.smi_data.reg = cpu_to_be16(reg);
}
rc = usb_control_msg(smi2021->udev, pipe, SMI2021_USB_REQUEST,
USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
transfer_buf->head, SMI2021_USB_INDEX,
transfer_buf, sizeof(*transfer_buf), 1000);
if (rc < 0)
goto free_out;
pipe = usb_rcvctrlpipe(smi2021->udev, SMI2021_USB_RCVPIPE);
rc = usb_control_msg(smi2021->udev, pipe, SMI2021_USB_REQUEST,
USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
transfer_buf->head, SMI2021_USB_INDEX,
transfer_buf, sizeof(*transfer_buf), 1000);
if (rc < 0)
goto free_out;
*val = transfer_buf->data.val;
free_out:
kfree(transfer_buf);
out:
return rc;
}
static int smi2021_i2c_xfer(struct i2c_adapter *i2c_adap,
struct i2c_msg msgs[], int num)
{
struct smi2021 *smi2021 = i2c_adap->algo_data;
switch (num) {
case 2: /* Read reg */
if (msgs[0].len != 1 || msgs[1].len != 1)
goto err_out;
if ((msgs[1].flags & I2C_M_RD) != I2C_M_RD)
goto err_out;
smi2021_get_reg(smi2021, msgs[0].addr, msgs[0].buf[0],
msgs[1].buf);
break;
case 1: /* Write reg */
if (msgs[0].len == 0)
break;
else if (msgs[0].len != 2)
goto err_out;
if (msgs[0].buf[0] == 0)
break;
smi2021_set_reg(smi2021, msgs[0].addr, msgs[0].buf[0],
msgs[0].buf[1]);
break;
default:
goto err_out;
}
return num;
err_out:
return -EOPNOTSUPP;
}
static u32 smi2021_i2c_functionality(struct i2c_adapter *adap)
{
return I2C_FUNC_SMBUS_EMUL;
}
/*
static int smi2021_initialize(struct smi2021 *smi2021)
{
int i, rc;
static u8 init[][2] = {
{ 0x3a, 0x80 },
{ 0x3b, 0x00 },
{ 0x34, 0x01 },
{ 0x35, 0x00 },
{ 0x34, 0x11 },
{ 0x35, 0x11 },
{ 0x3b, 0x80 },
{ 0x3b, 0x00 },
};
for (i = 0; i < ARRAY_SIZE(init); i++) {
rc = smi2021_set_reg(smi2021, 0x00, init[i][0], init[i][1]);
if (rc < 0)
return rc;
}
return 0;
}
*/
static struct smi2021_buf *smi2021_get_buf(struct smi2021 *smi2021)
{
unsigned long flags;
struct smi2021_buf *buf = NULL;
WARN_ON(smi2021->cur_buf);
spin_lock_irqsave(&smi2021->buf_lock, flags);
if (!list_empty(&smi2021->avail_bufs)) {
buf = list_first_entry(&smi2021->avail_bufs,
struct smi2021_buf, list);
list_del(&buf->list);
}
spin_unlock_irqrestore(&smi2021->buf_lock, flags);
return buf;
}
static void smi2021_buf_done(struct smi2021 *smi2021)
{
struct smi2021_buf *buf = smi2021->cur_buf;
v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
buf->vb.v4l2_buf.sequence = smi2021->sequence++;
buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
if (buf->pos < (SMI2021_BYTES_PER_LINE * smi2021->cur_height)) {
vb2_set_plane_payload(&buf->vb, 0, 0);
vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
} else {
vb2_set_plane_payload(&buf->vb, 0, buf->pos);
vb2_buffer_done(&buf->vb, VB2_BUF_STATE_DONE);
}
smi2021->cur_buf = NULL;
}
#define is_sav(trc) \
((trc & SMI2021_TRC_EAV) == 0x00)
#define is_field2(trc) \
((trc & SMI2021_TRC_FIELD_2) == SMI2021_TRC_FIELD_2)
#define is_active_video(trc) \
((trc & SMI2021_TRC_VBI) == 0x00)
/*
* Parse the TRC.
* Grab a new buffer from the queue if don't have one
* and we are recieving the start of a video frame.
*
* Mark video buffers as done if we have one full frame.
*/
static void parse_trc(struct smi2021 *smi2021, u8 trc)
{
struct smi2021_buf *buf = smi2021->cur_buf;
int lines_per_field = smi2021->cur_height / 2;
int line = 0;
if (!buf) {
if (!is_sav(trc))
return;
if (!is_active_video(trc))
return;
if (is_field2(trc))
return;
buf = smi2021_get_buf(smi2021);
if (!buf)
return;
smi2021->cur_buf = buf;
}
if (is_sav(trc)) {
/* Start of VBI or ACTIVE VIDEO */
if (is_active_video(trc)) {
buf->in_blank = false;
buf->trc_av++;
} else {
/* VBI */
buf->in_blank = true;
}
if (!buf->second_field && is_field2(trc)) {
line = buf->pos / SMI2021_BYTES_PER_LINE;
if (line < lines_per_field)
goto buf_done;
buf->second_field = true;
buf->trc_av = 0;
}
if (buf->second_field && !is_field2(trc))
goto buf_done;
} else {
/* End of VBI or ACTIVE VIDEO */
buf->in_blank = true;
}
return;
buf_done:
smi2021_buf_done(smi2021);
}
static void copy_video(struct smi2021 *smi2021, u8 p)
{
struct smi2021_buf *buf = smi2021->cur_buf;
int lines_per_field = smi2021->cur_height / 2;
int line = 0;
int pos_in_line = 0;
unsigned int offset = 0;
u8 *dst;
if (!buf)
return;
if (buf->in_blank)
return;
if (buf->pos >= buf->length) {
smi2021_buf_done(smi2021);
return;
}
//dev_info(smi2021->dev, "kerpz haxx hit\n");
pos_in_line = buf->pos % SMI2021_BYTES_PER_LINE;
line = buf->pos / SMI2021_BYTES_PER_LINE;
if (line >= lines_per_field)
line -= lines_per_field;
if (line != buf->trc_av - 1) {
/* Keep video synchronized.
* The device will sometimes give us too many bytes
* for a line, before we get a new TRC.
* We just drop these bytes */
return;
}
if (buf->second_field)
offset += SMI2021_BYTES_PER_LINE;
offset += (SMI2021_BYTES_PER_LINE * line * 2) + pos_in_line;
/* Will this ever happen? */
if (offset >= buf->length)
return;
dst = buf->mem + offset;
*dst = p;
buf->pos++;
}
/*
* Scan the saa7113 Active video data.
* This data is:
* 4 bytes header (0xff 0x00 0x00 [TRC/SAV])
* 1440 bytes of UYUV Video data
* 4 bytes footer (0xff 0x00 0x00 [TRC/EAV])
*
* TRC = Time Reference Code.
* SAV = Start Active Video.
* EAV = End Active Video.
* This is described in the saa7113 datasheet.
*/
static void parse_video(struct smi2021 *smi2021, u8 *p, int size)
{
int i;
for (i = 0; i < size; i++) {
switch (smi2021->sync_state) {
case HSYNC:
if (p[i] == 0xff) {
smi2021->sync_state = SYNCZ1;
}
else {
copy_video(smi2021, p[i]);
}
break;
case SYNCZ1:
if (p[i] == 0x00) {
smi2021->sync_state = SYNCZ2;
} else {
smi2021->sync_state = HSYNC;
copy_video(smi2021, 0xff);
copy_video(smi2021, p[i]);
}
break;
case SYNCZ2:
if (p[i] == 0x00) {
smi2021->sync_state = TRC;
} else {
smi2021->sync_state = HSYNC;
copy_video(smi2021, 0xff);
copy_video(smi2021, 0x00);
copy_video(smi2021, p[i]);
}
break;
case TRC:
smi2021->sync_state = HSYNC;
parse_trc(smi2021, p[i]);
break;
}
}
}
/*
* The device delivers data in chunks of 0x400 bytes.
* The four first bytes is a magic header to identify the chunks.
* 0xaa 0xaa 0x00 0x00 = saa7113 Active Video Data
* 0xaa 0xaa 0x00 0x01 = PCM - 24Bit 2 Channel audio data
*/
static void process_packet(struct smi2021 *smi2021, u8 *p, int size)
{
int i;
u32 *header;
if (size % 0x400 != 0) {
printk_ratelimited(KERN_INFO "smi2021::%s: size: %d\n",
__func__, size);
return;
}
for (i = 0; i < size; i += 0x400) {
header = (u32 *)(p + i);
switch (*header) {
case cpu_to_be32(0xaaaa0000):
parse_video(smi2021, p+i+4, 0x400-4);
break;
case cpu_to_be32(0xaaaa0001):
smi2021_audio(smi2021, p+i+4, 0x400-4);
break;
}
}
}
static void smi2021_iso_cb(struct urb *ip)
{
struct smi2021 *smi2021 = ip->context;
int i;
switch (ip->status) {
case 0:
/* All fine */
break;
/* Device disconnected or capture stopped? */
case -ENODEV:
case -ENOENT:
case -ECONNRESET:
case -ESHUTDOWN:
return;
/* Unknown error, retry */
default:
dev_warn(smi2021->dev, "urb error! status %d\n", ip->status);
return;
}
for (i = 0; i < ip->number_of_packets; i++) {
int size = ip->iso_frame_desc[i].actual_length;
unsigned char *data = ip->transfer_buffer +
ip->iso_frame_desc[i].offset;
process_packet(smi2021, data, size);
ip->iso_frame_desc[i].status = 0;
ip->iso_frame_desc[i].actual_length = 0;
}
ip->status = 0;
ip->status = usb_submit_urb(ip, GFP_ATOMIC);
if (ip->status)
dev_warn(smi2021->dev, "urb re-submit failed (%d)\n", ip->status);
}
static void smi2021_cancel_isoc(struct smi2021 *smi2021)
{
int i, num_bufs = smi2021->isoc_ctl.num_bufs;
dev_notice(smi2021->dev, "killing %d urbs...\n", num_bufs);
for (i = 0; i < num_bufs; i++) {
/*
* To kill urbs, we can't be in atomic context.
* We don't care for NULL pointers since
* usb_kill_urb allows it.
*/
usb_kill_urb(smi2021->isoc_ctl.urb[i]);
}
dev_notice(smi2021->dev, "all urbs killed\n");
}
/*
* Releases all urb and transfer buffers.
* All associated urbs must be killed before calling this function.
*/
static void smi2021_free_isoc(struct smi2021 *smi2021)
{
struct urb *urb;
int i, num_bufs = smi2021->isoc_ctl.num_bufs;
dev_info(smi2021->dev, "freeing %d urb buffers...\n", num_bufs);
for (i = 0; i < num_bufs; i++) {
urb = smi2021->isoc_ctl.urb[i];
if (unlikely(!urb))
continue;
if (smi2021->isoc_ctl.transfer_buffer[i])
#ifndef CONFIG_DMA_NONCOHERENT
usb_free_coherent(smi2021->udev,
urb->transfer_buffer_length,
smi2021->isoc_ctl.transfer_buffer[i],
urb->transfer_dma);
#else
kfree(smi2021->isoc_ctl.transfer_buffer[i]);
#endif
usb_free_urb(urb);
smi2021->isoc_ctl.urb[i] = NULL;
smi2021->isoc_ctl.transfer_buffer[i] = NULL;
}
kfree(smi2021->isoc_ctl.urb);
kfree(smi2021->isoc_ctl.transfer_buffer);
smi2021->isoc_ctl.urb = NULL;
smi2021->isoc_ctl.transfer_buffer = NULL;
smi2021->isoc_ctl.num_bufs = 0;
dev_info(smi2021->dev, "all urb buffers freed\n");
}
static void smi2021_uninit_isoc(struct smi2021 *smi2021)
{
smi2021_cancel_isoc(smi2021);
smi2021_free_isoc(smi2021);
}
static int smi2021_alloc_isoc(struct smi2021 *smi2021)
{
struct urb *urb;
int i, j, sb_size, max_packets, num_bufs;
/*
* It may be necessary to release isoc here,
* since isocs are only released on disconnect.
*/
if (smi2021->isoc_ctl.num_bufs)
smi2021_uninit_isoc(smi2021);
dev_info(smi2021->dev, "allocating urbs...\n");
num_bufs = SMI2021_ISOC_TRANSFERS;
max_packets = SMI2021_ISOC_PACKETS;
sb_size = max_packets * smi2021->iso_size;
smi2021->cur_buf = NULL;
smi2021->isoc_ctl.max_pkt_size = smi2021->iso_size;
smi2021->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs, GFP_KERNEL);
if (!smi2021->isoc_ctl.urb) {
dev_err(smi2021->dev, "out of memory for urb array");
return -ENOMEM;
}
smi2021->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
GFP_KERNEL);
if (!smi2021->isoc_ctl.transfer_buffer) {
dev_err(smi2021->dev, "out of memory for usb transfers");
kfree(smi2021->isoc_ctl.urb);
return -ENOMEM;
}
/* allocate urbs and transfer buffers */
for (i = 0; i < num_bufs; i++) {
urb = usb_alloc_urb(max_packets, GFP_KERNEL);
if (!urb) {
dev_err(smi2021->dev, "cannot allocate urb[%d]\n", i);
goto err_out;
}
smi2021->isoc_ctl.urb[i] = urb;
#ifndef CONFIG_DMA_NONCOHERENT
smi2021->isoc_ctl.transfer_buffer[i] = usb_alloc_coherent(smi2021->udev,
sb_size, GFP_KERNEL, &urb->transfer_dma);
#else
smi2021->isoc_ctl.transfer_buffer[i] = kmalloc(sb_size, GFP_KERNEL);
#endif
if (!smi2021->isoc_ctl.transfer_buffer[i]) {
dev_err(smi2021->dev,
"cannot alloc %d bytes for tx[%d] buffer\n",
sb_size, i);
goto err_out;
}
urb->dev = smi2021->udev;
urb->pipe = usb_rcvisocpipe(smi2021->udev, SMI2021_ISOC_EP);
urb->transfer_buffer = smi2021->isoc_ctl.transfer_buffer[i];
urb->transfer_buffer_length = sb_size;
urb->complete = smi2021_iso_cb;
urb->context = smi2021;
urb->interval = 1;
urb->start_frame = 0;
urb->number_of_packets = max_packets;
#ifndef CONFIG_DMA_NONCOHERENT
urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
#else
urb->transfer_flags = URB_ISO_ASAP;
#endif
for (j = 0; j < max_packets; j++) {
urb->iso_frame_desc[j].offset = smi2021->iso_size * j;
urb->iso_frame_desc[j].length = smi2021->iso_size;
}
}
dev_info(smi2021->dev, "%d urbs of %d bytes, allocated\n", num_bufs,
sb_size);
smi2021->isoc_ctl.num_bufs = num_bufs;
return 0;
err_out:
/* Save the allocataed buffers so far, so we can properly free them */
smi2021->isoc_ctl.num_bufs = i+1;
smi2021_free_isoc(smi2021);
return -ENOMEM;
}
void smi2021_toggle_audio(struct smi2021 *smi2021, bool enable)
{
/*
* I know that setting this register enables and disables
* the transfer of audio data over usb.
* I have no idea about what the number 0x1d really represents.
*/
if (enable)
smi2021_set_reg(smi2021, 0, 0x1740, 0x1d);
else
smi2021_set_reg(smi2021, 0, 0x1740, 0x00);
}
int smi2021_start(struct smi2021 *smi2021)
{
int i, rc;
u8 reg;
smi2021->sync_state = HSYNC;
/* Check device presence */
if (!smi2021->udev)
return -ENODEV;
if (mutex_lock_interruptible(&smi2021->v4l2_lock))
return -ERESTARTSYS;
v4l2_subdev_call(smi2021->gm7113c_subdev, video, s_stream, 1);
/*
* Enble automatic field detection on gm7113c (Bit 7)
* It seems the device needs this to not fail when receiving bad video
* i.e. from an old VHS tape.
*/
smi2021_get_reg(smi2021, 0x4a, 0x08, ®);
smi2021_set_reg(smi2021, 0x4a, 0x08, reg | 0x80);
/*
* Reset RTSO0 6 Times (Bit 7)
* The Windows driver does this, not sure if it's really needed.
*/
smi2021_get_reg(smi2021, 0x4a, 0x0e, ®);
reg |= 0x80;
for (i = 0; i < 6; i++)
smi2021_set_reg(smi2021, 0x4a, 0x0e, reg);
rc = smi2021_set_mode(smi2021, SMI2021_MODE_CAPTURE);
if (rc < 0)
goto start_fail;
rc = usb_set_interface(smi2021->udev, 0, 2);
if (rc < 0)
goto start_fail;
smi2021_toggle_audio(smi2021, false);
if (!smi2021->isoc_ctl.num_bufs) {
rc = smi2021_alloc_isoc(smi2021);
if (rc < 0)
goto err_stop_hw;
}
for (i = 0; i < smi2021->isoc_ctl.num_bufs; i++) {
rc = usb_submit_urb(smi2021->isoc_ctl.urb[i], GFP_KERNEL);
if (rc) {
dev_err(smi2021->dev, "cannot submit urb[%d] (%d)\n",
i, rc);
goto err_uninit;
}
}
/* I have no idea about what this register does with this value. */
smi2021_set_reg(smi2021, 0, 0x1800, 0x0d);
mutex_unlock(&smi2021->v4l2_lock);
return 0;
err_uninit:
smi2021_uninit_isoc(smi2021);
err_stop_hw:
usb_set_interface(smi2021->udev, 0, 0);
smi2021_clear_queue(smi2021);
start_fail:
mutex_unlock(&smi2021->v4l2_lock);
return rc;
}
/* Must be called with v4l2_lock held */
static void smi2021_stop_hw(struct smi2021 *smi2021)
{
if (!smi2021->udev)
return;
v4l2_subdev_call(smi2021->gm7113c_subdev, video, s_stream, 0);
smi2021_set_mode(smi2021, SMI2021_MODE_STANDBY);
usb_set_interface(smi2021->udev, 0, 0);
}
int smi2021_stop(struct smi2021 *smi2021)
{
if (!smi2021->udev)
return -ENODEV;
if (mutex_lock_interruptible(&smi2021->v4l2_lock))
return -ERESTARTSYS;
/* This is split into two functions because I've
* copied this from the stk1160 driver.
*
* That should probably not be neccessary.
*/
smi2021_cancel_isoc(smi2021);
smi2021_free_isoc(smi2021);
smi2021_stop_hw(smi2021);
smi2021_clear_queue(smi2021);
dev_notice(smi2021->dev, "streaming stopped\n");
mutex_unlock(&smi2021->v4l2_lock);
return 0;
}
static void smi2021_release(struct v4l2_device *v4l2_dev)
{
struct smi2021 *smi2021 = container_of(v4l2_dev, struct smi2021,
v4l2_dev);
dev_info(smi2021->dev, "releasing all resources\n");
i2c_del_adapter(&smi2021->i2c_adap);
v4l2_ctrl_handler_free(&smi2021->ctrl_handler);
v4l2_device_unregister(&smi2021->v4l2_dev);
/* vb2_queue_release(&smi2021->vb_vidq); */
dev_info(smi2021->dev, "freeing smi2021-struct\n");
kfree(smi2021);
}
/*
* DEVICE - PROBE & DISCONNECT
*/
static const struct usb_device_id smi2021_usb_device_id_table[] = {
{ USB_DEVICE(VENDOR_ID, BOOTLOADER_ID) },
{ USB_DEVICE(VENDOR_ID, 0x003c) },
{ USB_DEVICE(VENDOR_ID, 0x003d) },
{ USB_DEVICE(VENDOR_ID, 0x003e) },
{ USB_DEVICE(VENDOR_ID, 0x003f) },
{ }
};
MODULE_DEVICE_TABLE(usb, smi2021_usb_device_id_table);
static const struct smi2021_vid_input dual_input[] = {
{
.name = "Composite",
.type = SAA7115_COMPOSITE0,
},
{
.name = "S-Video",
.type = SAA7115_SVIDEO1,
}
};
static const struct smi2021_vid_input quad_input[] = {
{
.name = "Composite 0",
.type = SAA7115_COMPOSITE0,
},
{
.name = "Composite 1",
.type = SAA7115_COMPOSITE1,
},
{
.name = "Composite 2",
.type = SAA7115_COMPOSITE2,
},
{
.name = "Composite 3",
.type = SAA7115_COMPOSITE3,
},
};
const static struct i2c_algorithm smi2021_algo = {
.master_xfer = smi2021_i2c_xfer,
.functionality = smi2021_i2c_functionality,
};
static struct i2c_adapter adap_template = {
.owner = THIS_MODULE,
.name = "smi2021",
.algo = &smi2021_algo,
};
static struct i2c_client client_template = {
.name = "smi2021 internal",
};
/* gm7113c I2C address */
static unsigned short gm7113c_addrs[] = {
0x94 >> 1,
I2C_CLIENT_END
};
static int smi2021_usb_probe(struct usb_interface *intf,
const struct usb_device_id *devid)
{
int rc, size, input_count;
const struct smi2021_vid_input *vid_inputs;
struct device *dev = &intf->dev;
struct usb_device *udev = interface_to_usbdev(intf);
struct smi2021 *smi2021;
if (udev->descriptor.idProduct == BOOTLOADER_ID)
return smi2021_bootloader_probe(intf, devid);
if (intf->num_altsetting != 3)
return -ENODEV;
if (intf->altsetting[2].desc.bNumEndpoints != 1)
return -ENODEV;
size = usb_endpoint_maxp(&intf->altsetting[2].endpoint[0].desc);
size = (size & 0x07ff) * (((size & 0x1800) >> 11) + 1);
switch (udev->descriptor.idProduct) {
case 0x3e:
case 0x3f:
input_count = ARRAY_SIZE(quad_input);
vid_inputs = quad_input;
break;
case 0x3c:
case 0x3d:
default:
input_count = ARRAY_SIZE(dual_input);
vid_inputs = dual_input;
}
smi2021 = kzalloc(sizeof(struct smi2021), GFP_KERNEL);
if (!smi2021)
return -ENOMEM;
smi2021->dev = dev;
smi2021->udev = udev;
smi2021->vid_input_count = input_count;
smi2021->vid_inputs = vid_inputs;
smi2021->iso_size = size;
/* videobuf2 struct and locks */
spin_lock_init(&smi2021->buf_lock);
mutex_init(&smi2021->v4l2_lock);
mutex_init(&smi2021->vb_queue_lock);
rc = smi2021_vb2_setup(smi2021);
if (rc < 0) {
dev_err(dev, "Could not initialize videobuf2 queue\n");
goto free_err;
}
rc = v4l2_ctrl_handler_init(&smi2021->ctrl_handler, 0);
if (rc < 0) {
dev_err(dev, "Could not initialize v4l2 ctrl handler\n");
goto free_err;
}
/* v4l2 struct */