-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVenusDrv.c
2249 lines (1922 loc) · 69.4 KB
/
VenusDrv.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
/* NITGEN USB Fingkey Hamster (hfdu04) driver - 1.0.4
* Copyright(C) 2012~2015, NITGEN&COMPANY Co., Ltd.
* History:
* 24/10/2007 : first release
* 01/12/2010 : Modified for NorthStar OEM
* 22/03/2012 : Modified i/o control macro
* 07/08/2012 : Modified time out
* 22/01/2015 : supports for Ubuntu-14.04(Kernel v3.13.0) by jphwang
* 23/03/2015 : supports for Ubuntu-14.04(Kernel v3.16.0) by jphwang
* deprecated function 'interruptible_sleep_on' replaced
* 'wait_event_interruptible'.
* 14/02/2018 : supports for Ubuntu-17.10(Kernel v4.13.0) by jphwang
* 06/08/2018 : supports for Ubuntu-18.04(Kernel v4.15.0) by jphwang
* 22/10/2023 : support for linux kernel higher or equal than v.5.19 by Lucas Andreatta
* 01/02/2024 : support for linux kernel higher or equal than v.6.4 by Lucas Andreatta
*
*/
/* driver include files ***********************************************************/
#include <linux/module.h> /* dynamic loading of modules */
#include <linux/kernel.h> /* dynamic loading of modules */
#include <linux/init.h> /*_init _exit */
#include <linux/slab.h> /* kmalloc () .. */
#include <linux/errno.h> /* err numer */
#include <linux/version.h>
#include <linux/list.h> /* link list impl */
#include <linux/wait.h> /* interruptible_sleep_on */
#include <linux/workqueue.h>
#include <linux/delay.h>
#include <linux/completion.h>
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 35)
#include <linux/semaphore.h>
#elif LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 16)
#include <linux/mutex.h>
#endif
#include <linux/sched.h> /* v1.0.4-5.1 */
#include <linux/vmalloc.h>
#include <asm/uaccess.h> /* user access from kernel space copy_to_user */
#include <asm/atomic.h> /* atomic_inc() and .. */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 10)
#include <linux/smp_lock.h> /* spin_lock_.. */
#else
#include <linux/spinlock.h>
#endif
#include <linux/usb.h> /* USB APIs */
#include <linux/sched.h>
#include <linux/err.h>
#include <linux/slab.h>
#include <linux/fs.h> /* file system functions */
/* v1.0.4-5 */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0) /* for signal_pending() api */
#include <linux/uaccess.h> /* for copy_to_user/copy_from_user() api */
#endif
#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) /* for signal_pending() api */
#include <linux/sched/signal.h> /* for signal_pending() api */
#endif
#include "VenusDrv.h"
/* driver define ******************************************************************/
#ifndef __VERSION_INCLUDED
#define __VERSION_INCLUDED
#define MODULE_MAJOR_VERSION 0x01
#define MODULE_MINOR_VERSION 0x00
#define MODULE_RELEASE_VERSION 0x03
#define MODULE_BUILD_VERSION 0x0A
#define MODULE_RELEASE_STRING "1.0.4-5.1"
#endif
#define CONFIG_USB_DEBUG
#ifdef CONFIG_USB_DEBUG
#define DEBUG
int debug = 1;
#else
int debug = 0;
#endif
#define MSG_TIME_OUT 10 * 1000 // 10 sec
/* Use our own dbg macro */
#undef dbg
#define dbg(format, arg...) \
do \
{ \
if (debug) \
printk(KERN_DEBUG __FILE__ ": " format "\n", ##arg); \
} while (0)
/* Driver Information */
#define DRIVER_VERSION "v1.0.4-5.1"
#define DRIVER_AUTHOR "NITGEN Hardware Development Team"
#define DRIVER_DESC "NITGEN USB FDU01/04/06 Fingkey Hamster Driver"
/* Define these values to match your device */
#define USB_HFDU04_VENDOR_ID 0x0A86
#define USB_HFDU04_PRODUCT_ID 0x0100
/* We can have up to this number of device plugged in at once */
#define HFDU04_MAX_DEVICES 8
#define _ISOPIPESIZE 4200
#define ANCHOR_LOAD_INTERNAL 0xA0 /* Vender specific request code for Anchor upload/Download */
#define VENDOR_REQUEST_OUT 0x40
#define CPUCS_REG 0x7F92 /* EZ-USB Control and Status Register. Bit 0 controls 8051 reset */
#define E2PROM_SIZE 15 * 1024
#define HFDU06_MAX_PACKET_SIZE 512 * 782
#define MAX_READ_DATA (240) * 512
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 16)
static struct workqueue_struct *comm_queue;
#endif
// O1 Ver //////////////////
#define MAX_INTEL_HEX_RECORD_LENGTH 16
/* Get a minor range for your devices from the usb maintainer
* still we need free minor numbers
*/
#define USB_HFDU04_MINOR_BASE 230
/* prevent races between open() disconnect() read() */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
static DECLARE_MUTEX(disconnect_sem);
#elif LINUX_VERSION_CODE < KERNEL_VERSION(6, 4, 0)
static DEFINE_SEMAPHORE(disconnect_sem);
#else
static DEFINE_SEMAPHORE(disconnect_sem, 1);
#endif
/* driver structure ***************************************************************/
/* table of devices that work with this driver */
static struct usb_device_id hfdu04_device_ids[] = {
{USB_DEVICE(USB_HFDU04_VENDOR_ID, USB_HFDU04_PRODUCT_ID)},
{},
/* Terminating entry */
};
typedef enum
{
_stopped = 0,
_started
} driver_state_t;
MODULE_DEVICE_TABLE(usb, hfdu04_device_ids);
struct hfdu04_data
{
struct usb_device *udev;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 16)
struct usb_interface *interface;
#endif
driver_state_t state; /* State of the device */
unsigned char minor; /* Scanner minor - used in disconnect() */
__u8 present; /* Not zero if device is present */
__u8 opencount; /* Not zero if device is opened */
unsigned char *bulk_in_buffer; /* the buffer to receive data */
unsigned char *bulk_out_buffer; /* the buffer to send data */
size_t bulk_out_size; /* the maximum bulk packet size */
size_t bulk_in_size; /* the maximum bulk packet size */
size_t orig_bi_size; /* same as above, but reported by the device */
__u8 bulk_in_endpointAddr; /* Address of bulkin endpoint */
__u8 bulk_in_endpointAddr_image; /* Address of bulkin endpoint for image*/
__u8 bulk_in_endpointAddr_fw; /* Address of bulkin endpoint for firmware*/
size_t bulk_out_buffer_used; /* the quantity of buffered data to send */
__u8 bulk_out_endpointAddr; /* Address of bulkout endpoint */
__u8 bulk_out_endpointAddr_normal; /* Address of bulkout endpoint */
__u8 bulk_out_endpointAddr_fw; /* Address of bulkout endpoint */
__u8 iso_in_endpointAddr; /* Address of iso in endpoint */
atomic_t pending_io; /* Pending i/o transaction */
unsigned int readptr; /* Read pointer */
wait_queue_head_t wait; /* urb wait */
wait_queue_head_t wait1; /* urb wait */
spinlock_t lock; /* lock list */
struct semaphore sem; /* Lock to prevent concurrent reads or writes */
struct list_head free_buff_list; /* Isoc free buffer list */
struct list_head rec_buff_list; /* Isoc rec buffer list */
unsigned int got_mem;
unsigned int overruns;
unsigned int bcdDevice;
unsigned int maxbulktransfersize;
bool IsOpened;
bool IsConnected;
unsigned int idVendor;
unsigned int idProduct;
};
/* driver specific functions ******************************************************/
static int hfdu04_freequeue(struct list_head *i);
static int hfdu04_freebuffers(struct hfdu04_data *);
static void hfdu04_disconnect(struct usb_interface *);
static int hfdu04_open(struct inode *, struct file *);
static int hfdu04_allocbuffers(struct hfdu04_data *);
static int hfdu04_release(struct inode *, struct file *);
static int hfdu04_stopgrabbingdata(struct hfdu04_data *);
static int hfdu04_startgrabbingdata(struct hfdu04_data *);
static void hfdu04_isocomplete(struct urb *, struct pt_regs *);
static ssize_t hfdu04_read(struct file *, char *, size_t, loff_t *);
static ssize_t hfdu04_write(struct file *, const char *, size_t, loff_t *);
static int hfdu04_cancelqueue(struct hfdu04_data *, struct list_head *);
static int hfdu04_probe(struct usb_interface *, const struct usb_device_id *);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
static int hfdu04_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
#else
#if 1 /* jphwang++(2015.01.21) */
static long hfdu04_ioctl(struct file *, unsigned int, unsigned long);
#else
static int hfdu04_ioctl(struct file *, unsigned int, unsigned long);
#endif
#endif
static int hfdu04_addbuffertail(struct hfdu04_data *, struct list_head *, struct list_head *);
static int hfdu04_init_device(struct usb_device *udev);
int hfdu04_user_command_in(struct usb_device *udev, unsigned char *value);
int hfdu04_user_command_out(struct usb_device *udev, unsigned char *value);
/* driver structure ***************************************************************/
/* intel hex format */
typedef struct _INTEL_HEX_RECORD
{
__u32 length;
__u32 address;
__u32 type;
__u8 data[MAX_INTEL_HEX_RECORD_LENGTH];
} intel_hex_record, *pintel_hex_record;
/* Iso transfer */
typedef struct
{
struct hfdu04_data *dev; /* Pointer hfdu04_data structure */
struct urb *purb; /* Pointer to urb */
struct list_head buff_list; /* Isoc buffer list */
} buff_t, *pbuff_t;
// #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
/* Array of pointers to our devices that are currently connected */
// static struct hfdu04_data *hfdu04_table[HFDU04_MAX_DEVICES] =
//{ NULL, /* ... */ };
// #endif
static struct
file_operations hfdu04_fops = {
.owner = THIS_MODULE,
.read = hfdu04_read,
.write = hfdu04_write,
.open = hfdu04_open,
.release = hfdu04_release,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 36)
.ioctl = hfdu04_ioctl,
#else
.unlocked_ioctl = hfdu04_ioctl,
#endif
};
/* USB class driver information in order to get a minor number
* from the usb core the driver core and file operations .
*/
static struct usb_class_driver hfdu04_class = {
.name = "nitgen%d",
.fops = &hfdu04_fops,
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
.mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH,
#endif
.minor_base = USB_HFDU04_MINOR_BASE,
};
/* USB specific object needed to register this driver with the usb sub-system */
static struct usb_driver hfdu04_driver = {
.name = "hfdu04",
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
.owner = THIS_MODULE,
#endif
.id_table = hfdu04_device_ids,
.probe = hfdu04_probe,
.disconnect = hfdu04_disconnect,
};
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
#else
/* prevent races between open() and disconnect() */
static DEFINE_MUTEX(disconnect_mutex);
#endif
/* driver implemented function ****************************************************/
/* Iso Transfers */
/* adds buffer to list */
static int hfdu04_addbuffertail(struct hfdu04_data *dev,
struct list_head *dst,
struct list_head *src)
{
unsigned long flags;
struct list_head *tmp;
int ret = 0;
spin_lock_irqsave(&dev->lock, flags);
if (list_empty(src))
{
/* no elements in source buffer */
ret = -1;
goto err;
}
tmp = src->next;
list_move_tail(tmp, dst);
err:
spin_unlock_irqrestore(&dev->lock, flags);
return ret;
}
/* dump urb information if needed */
#ifdef DEBUG
static void dump_urb(struct urb *urb)
{
dbg("urb :%p", urb);
dbg("dev :%p", urb->dev);
dbg("pipe :%08X", urb->pipe);
dbg("status :%d", urb->status);
dbg("transfer_flags :%08X", urb->transfer_flags);
dbg("transfer_buffer :%p", urb->transfer_buffer);
dbg("transfer_buffer_length:%d", urb->transfer_buffer_length);
dbg("actual_length :%d", urb->actual_length);
dbg("setup_packet :%p", urb->setup_packet);
dbg("start_frame :%d", urb->start_frame);
dbg("number_of_packets :%d", urb->number_of_packets);
dbg("interval :%d", urb->interval);
dbg("error_count :%d", urb->error_count);
dbg("context :%p", urb->context);
dbg("complete :%p", urb->complete);
}
#endif
/*unlink the queue of urbs */
static int hfdu04_cancelqueue(struct hfdu04_data *dev, struct list_head *q)
{
if (dev->bcdDevice < 0x2000)
{
unsigned long flags;
struct list_head *p;
pbuff_t b;
// dbg("hfdu04_cancelqueue");
spin_lock_irqsave(&dev->lock, flags);
for (p = q->next; p != q; p = p->next)
{
b = list_entry(p, buff_t, buff_list);
#ifdef DEBUG
dump_urb(b->purb);
#endif
usb_unlink_urb(b->purb);
}
spin_unlock_irqrestore(&dev->lock, flags);
}
return 0;
}
/* free usb list */
static int hfdu04_freequeue(struct list_head *q)
{
// if ( bcdDevice < 0x2000)
{
struct list_head *tmp;
struct list_head *p;
pbuff_t b;
// dbg("hfdu04_freequeue");
for (p = q->next; p != q;)
{
b = list_entry(p, buff_t, buff_list);
#ifdef DEBUG
dump_urb(b->purb);
#endif
if (b->purb->transfer_buffer)
kfree(b->purb->transfer_buffer);
usb_free_urb(b->purb);
tmp = p->next;
list_del(p);
kfree(b);
p = tmp;
}
}
return 0;
}
/* free buffers which are allocated */
static int hfdu04_freebuffers(struct hfdu04_data *dev)
{
if (dev->bcdDevice < 0x2000)
{
unsigned long flags;
// dbg("hfdu04_freebuffers");
spin_lock_irqsave(&dev->lock, flags);
hfdu04_freequeue(&dev->free_buff_list);
hfdu04_freequeue(&dev->rec_buff_list);
spin_unlock_irqrestore(&dev->lock, flags);
dev->got_mem = 0;
}
return 0;
}
/* urb filling call back function for iso read */
// #define EILSEQ 84 /* Illegal byte sequence */
static void hfdu04_isocomplete(struct urb *purb, struct pt_regs *regs)
{
pbuff_t b = purb->context;
struct hfdu04_data *dev = b->dev;
int i;
int len;
int dst = 0;
unsigned int pipe;
int pipesize;
void *buf = purb->transfer_buffer;
// dbg("hfdu04_isocomplete");
// printk("#1. hfdu04_isocomplete\n");
/* process if URB was not killed */
if (purb->status != -ENOENT)
{
pipe = usb_rcvisocpipe(purb->dev, dev->iso_in_endpointAddr);
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 20, 0)
pipesize = usb_maxpacket(purb->dev, pipe);
#else
pipesize = usb_maxpacket(purb->dev, pipe, usb_pipeout(pipe));
#endif
for (i = 0; i < purb->number_of_packets; i++)
{
if (!purb->iso_frame_desc[i].status)
{
len = purb->iso_frame_desc[i].actual_length;
if (len <= pipesize)
{
memcpy(buf + dst, buf + purb->iso_frame_desc[i].offset, len);
dst += len;
}
else
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
printk("%s: invalid len %d", __FUNCTION__, len);
#else
err("%s: invalid len %d", __FUNCTION__, len);
#endif
}
}
else
{
// warn("%s: corrupted packet status: %d", __FUNCTION__, purb->iso_frame_desc[i].status);
}
}
if (dst != purb->actual_length)
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
printk("dst != purb->actual_length: %d! = %d", dst, purb->actual_length);
#else
err("dst != purb->actual_length: %d! = %d", dst, purb->actual_length);
#endif
return;
}
}
if (atomic_dec_and_test(&dev->pending_io) && (dev->state != _stopped))
{
dev->overruns++;
// err("overrun (%d)", dev->overruns);
}
// printk("#2. hfdu04_isocomplete\n");
wake_up(&dev->wait);
// wake_up(&dev->wait1);
}
/* allocate memory for buffers Only 01 Ver */
static int hfdu04_allocbuffers(struct hfdu04_data *dev)
{
if (dev->bcdDevice < 0x2000)
{
unsigned int buffers = 0;
pbuff_t b = NULL;
unsigned int pipe;
int pipesize, packets;
unsigned int transfer_buffer_length, totalbuffersize;
int i;
pipe = usb_rcvisocpipe(dev->udev, dev->iso_in_endpointAddr);
#if LINUX_VERSION_CODE > KERNEL_VERSION(5, 19, 0)
pipesize = usb_maxpacket(dev->udev, pipe);
#else
pipesize = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe));
#endif
packets = _ISOPIPESIZE / pipesize;
transfer_buffer_length = packets * pipesize;
totalbuffersize = 840 * 280;
while (buffers < totalbuffersize)
{
b = (pbuff_t)kmalloc(sizeof(buff_t), GFP_KERNEL);
if (!b)
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
printk("kmalloc( sizeof( buff_t ) ) == NULL");
#else
err("kmalloc( sizeof( buff_t ) ) == NULL");
#endif
goto err;
}
memset(b, 0, sizeof(buff_t));
b->dev = dev;
b->purb = usb_alloc_urb(packets, GFP_KERNEL);
if (!b->purb)
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
printk("usb_alloc_urb == NULL");
#else
err("usb_alloc_urb == NULL");
#endif
kfree(b);
goto err;
}
b->purb->transfer_buffer = kmalloc(transfer_buffer_length, GFP_KERNEL);
if (!b->purb->transfer_buffer)
{
// usb_free_urb(b->purb->transfer_buffer);
kfree(b->purb);
kfree(b);
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
printk("transfre_buffer_length kmalloc( %d ) == NULL", transfer_buffer_length);
#else
err("transfre_buffer_length kmalloc( %d ) == NULL", transfer_buffer_length);
#endif
goto err;
}
b->purb->transfer_buffer_length = transfer_buffer_length;
b->purb->number_of_packets = packets;
b->purb->complete = (usb_complete_t)hfdu04_isocomplete;
b->purb->context = b;
b->purb->interval = 1;
b->purb->dev = dev->udev;
b->purb->pipe = pipe;
b->purb->transfer_flags = URB_ISO_ASAP;
for (i = 0; i < packets; i++)
{
b->purb->iso_frame_desc[i].offset = i * pipesize;
b->purb->iso_frame_desc[i].length = pipesize;
}
buffers += transfer_buffer_length;
list_add(&b->buff_list, &dev->free_buff_list);
}
dev->got_mem = buffers;
return 0;
err:
hfdu04_freebuffers(dev);
return -ENOMEM;
}
else
{
return 1;
}
}
/* stops catching data */
static int hfdu04_stopgrabbingdata(struct hfdu04_data *dev)
{
dev->state = _stopped;
hfdu04_cancelqueue(dev, &dev->rec_buff_list);
dev->pending_io.counter = 0;
return 0;
}
/* starts catching data */
static int hfdu04_startgrabbingdata(struct hfdu04_data *dev)
{
if (!dev->got_mem && dev->state != _started)
{
if (hfdu04_allocbuffers(dev) < 0)
return -ENOMEM;
hfdu04_stopgrabbingdata(dev);
dev->state = _started;
dev->readptr = 0;
}
if (!list_empty(&dev->free_buff_list))
{
pbuff_t end;
int ret;
while (!hfdu04_addbuffertail(dev, &dev->rec_buff_list, &dev->free_buff_list))
{
end = list_entry(dev->rec_buff_list.prev, buff_t, buff_list);
ret = usb_submit_urb(end->purb, GFP_KERNEL);
// msleep(1);
if (ret != 0)
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
printk("usb_submit_urb returned: %d", ret);
#else
err("usb_submit_urb returned: %d", ret);
#endif
if (hfdu04_addbuffertail(dev, &dev->free_buff_list, &dev->rec_buff_list))
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
printk("startgrabbing: hfdu04_addbuffer tail failed");
#else
err("startgrabbing: hfdu04_addbuffer tail failed");
#endif
break;
}
else
{
atomic_inc(&dev->pending_io);
}
}
// udelay(50000);
// msleep(5);
}
return 0;
}
/* read call we implemented ISOC read here */
static ssize_t hfdu04_read(struct file *file, char *buf, size_t count, loff_t *ppos)
{
struct hfdu04_data *dev = (struct hfdu04_data *)file->private_data;
// unsigned ret = 0;
int ret = 0;
int rem;
int cnt;
struct urb *purb = NULL;
int user_result;
int actual_length;
// int while_count = 0;
int remain_count = 0;
char *u_buf = buf;
int bulk_ret;
int bulk_size;
int bulk_read_size;
// int incount = 0;
// int loopcount = 0;
// printk("%s : %d\n", __FUNCTION__, __LINE__);
// Only 01 Ver bcdDevice < 0x2000
if (dev->IsOpened == false)
{
// printk(" %s : %d - disconnected or not opened \n", __FUNCTION__, __LINE__);
return -EIO;
}
if (*ppos)
return -ESPIPE;
if (!dev->udev)
return -EIO;
// printk("%s : %d\n", __FUNCTION__, __LINE__);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
/* prevent disconnections */
down(&disconnect_sem);
#else
/* prevent disconnects */
mutex_lock(&disconnect_mutex);
#endif
down(&dev->sem);
// printk("%s : %d\n", __FUNCTION__, __LINE__);
if (dev->bcdDevice >= 0x4000)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)
// ret = -EFAULT;
// goto err;
remain_count = count;
bulk_read_size = 16 * PAGE_SIZE;
#else
remain_count = count;
bulk_read_size = count / 2; //(dev->maxbulktransfersize)/8;
#endif
if (dev->bulk_in_endpointAddr == dev->bulk_in_endpointAddr_fw)
bulk_read_size = dev->maxbulktransfersize;
while (remain_count > 0)
{
bulk_size = min_t(uint, remain_count, bulk_read_size /*dev->maxbulktransfersize*/);
// ��ũ����� �̿��Ͽ� ������ �д´�.
bulk_ret = usb_bulk_msg(dev->udev,
usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
dev->bulk_in_buffer,
bulk_size, // test: 512, 256, 128, 64
&actual_length,
MSG_TIME_OUT);
remain_count = remain_count - actual_length;
// ���� ������ copy_to_user �Լ��� �̿��Ͽ� ���ø����̼����� �ѱ��.
user_result = copy_to_user(u_buf, dev->bulk_in_buffer, actual_length);
u_buf += actual_length;
if (bulk_ret != 0)
{
printk("bulk error!!! : %d\n", bulk_ret);
break;
}
}
ret = u_buf - buf;
// printk("%s : %d ret: %d\n", __FUNCTION__, __LINE__, ret);
}
else if (dev->bcdDevice >= 0x2000)
{
remain_count = count;
// printk("%s : %d - remain_count: %d \n", __FUNCTION__, __LINE__, remain_count);
while (remain_count > 0)
{
bulk_size = min_t(uint, remain_count, dev->maxbulktransfersize);
// ��ũ����� �̿��Ͽ� ������ �д´�.
bulk_ret = usb_bulk_msg(dev->udev,
usb_rcvbulkpipe(dev->udev, dev->bulk_in_endpointAddr),
dev->bulk_in_buffer,
bulk_size, // test: 512, 256, 128, 64
&actual_length,
MSG_TIME_OUT);
remain_count = remain_count - actual_length;
// printk("%s : %d remain_count - %ld, actual_length - %d\n", __FUNCTION__, __LINE__, remain_count, actual_length);
// ���� ������ copy_to_user �Լ��� �̿��Ͽ� ���ø����̼����� �ѱ��.
user_result = copy_to_user(u_buf, dev->bulk_in_buffer, actual_length);
u_buf += actual_length;
if (bulk_ret != 0)
{
printk("bulk error!!! : %d\n", bulk_ret);
break;
}
}
ret = u_buf - buf;
// printk("%s : %d ret: %d\n", __FUNCTION__, __LINE__, ret);
}
else if (dev->bcdDevice < 0x2000)
{
unsigned long flags;
pbuff_t b;
while (count > 0)
{
// 01 �������� ����ϴ� ISO ����� �̿��� ������ ���� ���.
// ISO ����� ��� hfdu04_startgrabbingdata �Լ��� �ִ� hfdu04_allocbuffers�� �̿��Ͽ�, URB �ʱ�ȭ �ϰ�,
// �ʱ�ȭ�� URB�� usb_submit_urb �Լ��� �̿��Ͽ� URB�� USB �ھ������ �Ѱ��ְ� �ȴ�.
// usb_submit_urb �� ���������� ������ �Ǹ�, URB�� �ʱ�ȭ �Ҷ� ����� callback �Լ� ( hfdu04_isocomplete )�� ȣ���ϰ� �ȴ�.
// �� hfdu04_isocomplete �Լ����� ������ copy_to_user �Լ��� �̿��Ͽ� ���ø����̼����� �ѱ��.
hfdu04_startgrabbingdata(dev);
spin_lock_irqsave(&dev->lock, flags);
if (list_empty(&dev->rec_buff_list))
{
spin_unlock_irqrestore(&dev->lock, flags);
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
printk("%s error: rec_buf_list is empty %d ", __FUNCTION__, __LINE__);
#else
err("%s error: rec_buf_list is empty %d ", __FUNCTION__, __LINE__);
#endif
goto err;
}
b = list_entry(dev->rec_buff_list.next, buff_t, buff_list);
purb = b->purb;
spin_unlock_irqrestore(&dev->lock, flags);
if (purb->status == -EINPROGRESS)
{
if (file->f_flags & O_NONBLOCK) // return nonblocking
{
if (!ret)
{
ret = -EAGAIN;
}
goto err;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 13, 0)
/* jphwang++(2015.03.23) 'interruptible_sleep_on()' is deprecated. replace to 'wait_event_interruptible()' */
// waits for the data
wait_event_interruptible(dev->wait, 0 /* force condition is false */);
#else /* org */
// waits for the data
interruptible_sleep_on(&dev->wait);
#endif
if (signal_pending(current))
{
if (!ret)
{
ret = -ERESTARTSYS;
}
goto err;
}
spin_lock_irqsave(&dev->lock, flags);
if (list_empty(&dev->rec_buff_list))
{
spin_unlock_irqrestore(&dev->lock, flags);
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
printk("%s error: still no buffer available.", __FUNCTION__);
#else
err("%s error: still no buffer available.", __FUNCTION__);
#endif
goto err;
}
spin_unlock_irqrestore(&dev->lock, flags);
dev->readptr = 0;
}
rem = purb->actual_length - dev->readptr; // set remaining bytes to copy
if (count >= rem)
{
cnt = rem;
}
else
{
cnt = count;
}
// dbg("copy_to_user:%p %p %d", buf, purb->transfer_buffer + dev->readptr, cnt);
// printk("copy_to_user: buf:%p (purb->transfer_buffer + dev->readptr):%p cnt:%d\n", buf, purb->transfer_buffer + dev->readptr, cnt);
if (copy_to_user(buf, (purb->transfer_buffer + dev->readptr), cnt))
{
if (!ret)
ret = -EFAULT;
goto err;
}
dev->readptr += cnt;
count -= cnt;
buf += cnt;
ret += cnt;
if (dev->readptr == purb->actual_length)
{
// finished, take next buffer
if (hfdu04_addbuffertail(dev, &dev->free_buff_list, &dev->rec_buff_list))
{
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 3, 0)
printk("read: hfdu04_addbuffertail failed");
#else
err("read: hfdu04_addbuffertail failed");
#endif
}
dev->readptr = 0;
}
}
}
err:
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
up(&disconnect_sem);
#else
mutex_unlock(&disconnect_mutex);
#endif
up(&dev->sem);
return ret;
}
/* write call we implemented ISOC write here */
static ssize_t hfdu04_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
{
struct hfdu04_data *dev = (struct hfdu04_data *)file->private_data;
int ret = 0;
int user_result;
int actual_length;
int remain_count = 0;
const char *u_buf = buf;
int bulk_ret;
int bulk_size;
int bulk_write_size;
size_t bulk_write_total;
bulk_write_total = 0;
// printk("%s : %d\n", __FUNCTION__, __LINE__);
if (dev->IsOpened == false)
{
// printk(" %s : %d - disconnected or not opened \n", __FUNCTION__, __LINE__);
return -EIO;
}
if (*ppos)
return -ESPIPE;
if (!dev->udev)
return -EIO;
// printk("%s : %d\n", __FUNCTION__, __LINE__);
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
/* prevent disconnections */
down(&disconnect_sem);
#else
/* prevent disconnects */
mutex_lock(&disconnect_mutex);
#endif
down(&dev->sem);
// printk("%s : %d\n", __FUNCTION__, __LINE__);
if (dev->bcdDevice >= 0x4000)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23)
remain_count = count;
bulk_write_size = 32 * PAGE_SIZE;
#else
remain_count = count;
bulk_write_size = count;
#endif
// printk("%s : %d - r %d, b %d\n", __FUNCTION__, __LINE__, remain_count, bulk_write_size);
while (remain_count > 0)
{
bulk_size = min_t(uint, remain_count, dev->bulk_out_size);
// printk("%s : %d - bulk_out_endpoint %d, bulk_size %d\n", __FUNCTION__, __LINE__, dev->bulk_out_endpointAddr, bulk_size);
// �� ������ copy_from_user �Լ��� �̿��Ͽ� kernel buffer�� �ű��.
user_result = copy_from_user(dev->bulk_out_buffer, buf + bulk_write_total, bulk_size);
// ��ũ����� �̿��Ͽ� ������ ����.
bulk_ret = usb_bulk_msg(dev->udev,
usb_sndbulkpipe(dev->udev, dev->bulk_out_endpointAddr),
dev->bulk_out_buffer,
bulk_size,
&actual_length,
MSG_TIME_OUT);
remain_count = remain_count - actual_length;
bulk_write_total += actual_length;
// u_buf += actual_length;
if (bulk_ret != 0)
{
printk("bulk error!!! : %d\n", bulk_ret);
break;
}
}
// ret = u_buf - buf;
ret = bulk_write_total;
// printk("%s : %d ret: %d\n", __FUNCTION__, __LINE__, ret);
dev->bulk_out_endpointAddr = dev->bulk_out_endpointAddr_normal;
}