summaryrefslogtreecommitdiff
path: root/drivers/staging/rspiusb/rspiusb.c
blob: 2f8155c1968b31652f63f781c0e15071fd25afe6 (plain)
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
/*
 * rspiusb.c
 *
 * Copyright (C) 2005, 2006 Princeton Instruments
 *
 * 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
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/vmalloc.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <linux/smp_lock.h>
#include <linux/completion.h>
#include <linux/scatterlist.h>
#include <linux/usb.h>
#include <linux/mm.h>
#include <linux/pagemap.h>
#include <linux/ioctl.h>
#include "rspiusb.h"

#ifdef CONFIG_USB_DEBUG
static int debug = 1;
#else
static int debug;
#endif
/* Use our own dbg macro */
#undef dbg
#define dbg(format, arg...) \
	do { \
		if (debug) \
			printk(KERN_DEBUG __FILE__ ": " format "\n" , ##arg); \
	} while (0)

/* Version Information */
#define DRIVER_VERSION "V1.0.1"
#define DRIVER_AUTHOR  "Princeton Instruments"
#define DRIVER_DESC    "PI USB2.0 Device Driver for Linux"

/* Define these values to match your devices */
#define VENDOR_ID   0x0BD7
#define ST133_PID   0xA010
#define PIXIS_PID   0xA026

/* Get a minor range for your devices from the usb maintainer */
#ifdef CONFIG_USB_DYNAMIC_MINORS
#define PIUSB_MINOR_BASE    0
#else
#define PIUSB_MINOR_BASE    192
#endif

/* prevent races between open() and disconnect() */
static DECLARE_MUTEX(disconnect_sem);

/* Structure to hold all of our device specific stuff */
struct device_extension {
	struct usb_device *udev;	 /* save off the usb device pointer */
	struct usb_interface *interface; /* the interface for this device */
	unsigned char minor;		 /* the starting minor number
					  * for this device
					  */
	size_t bulk_in_size_returned;
	int bulk_in_byte_trk;
	struct urb ***PixelUrb;
	int frameIdx;
	int urbIdx;
	unsigned int *maplist_numPagesMapped;
	int open;		  /* if the port is open or not */
	int present;		  /* if the device is not disconnected */
	int userBufMapped;	  /* has the user buffer been mapped ? */
	struct scatterlist **sgl; /* scatter-gather list for user buffer */
	unsigned int *sgEntries;
	struct kref kref;
	int gotPixelData;
	int pendingWrite;
	char **pendedPixelUrbs;
	int iama;		 /* PIXIS or ST133 */
	int num_frames;		 /* the number of frames that will fit
				  * in the user buffer
				  */
	int active_frame;
	unsigned long frameSize;
	struct semaphore sem;
	unsigned int hEP[8];	 /* FX2 specific endpoints */
};

#define to_pi_dev(d) container_of(d, struct device_extension, kref)

/* Prototypes */
static int MapUserBuffer(struct ioctl_struct *, struct device_extension *);
static int UnMapUserBuffer(struct device_extension *);
static int piusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
		       unsigned long arg);
static int piusb_output(struct ioctl_struct *, unsigned char *, int,
		struct device_extension *);
static struct usb_driver piusb_driver;

/* table of devices that work with this driver */
static struct usb_device_id pi_device_table[] = {
	{USB_DEVICE(VENDOR_ID, ST133_PID)},
	{USB_DEVICE(VENDOR_ID, PIXIS_PID)},
	{0, } /* Terminating entry */
};

MODULE_DEVICE_TABLE(usb, pi_device_table);

static int lastErr;
static int errCnt;

static void piusb_delete(struct kref *kref)
{
	struct device_extension *pdx = to_pi_dev(kref);

	dev_dbg(&pdx->udev->dev, "%s\n", __func__);
	usb_put_dev(pdx->udev);
	kfree(pdx);
}

static int piusb_open(struct inode *inode, struct file *file)
{
	struct device_extension *pdx = NULL;
	struct usb_interface *interface;
	int subminor;
	int retval = 0;

	dbg("Piusb_Open()");
	subminor = iminor(inode);
	interface = usb_find_interface(&piusb_driver, subminor);
	if (!interface) {
		printk(KERN_ERR "%s - error, can't find device for minor %d\n",
		       __func__, subminor);
		retval = -ENODEV;
		goto exit_no_device;
	}

	pdx = usb_get_intfdata(interface);
	if (!pdx) {
		retval = -ENODEV;
		goto exit_no_device;
	}
	dbg("Alternate Setting = %d", interface->num_altsetting);

	pdx->bulk_in_size_returned = 0;
	pdx->bulk_in_byte_trk = 0;
	pdx->PixelUrb = NULL;
	pdx->frameIdx = 0;
	pdx->urbIdx = 0;
	pdx->maplist_numPagesMapped = NULL;
	pdx->userBufMapped = 0;
	pdx->sgl = NULL;
	pdx->sgEntries = NULL;
	pdx->gotPixelData = 0;
	pdx->pendingWrite = 0;
	pdx->pendedPixelUrbs = NULL;
	pdx->num_frames = 0;
	pdx->active_frame = 0;
	pdx->frameSize = 0;

	/* increment our usage count for the device */
	kref_get(&pdx->kref);

	/* save our object in the file's private structure */
	file->private_data = pdx;

exit_no_device:
	return retval;
}

static int piusb_release(struct inode *inode, struct file *file)
{
	struct device_extension *pdx;
	int retval = 0;

	dbg("Piusb_Release()");
	pdx = (struct device_extension *)file->private_data;
	if (pdx == NULL) {
		dbg("%s - object is NULL", __func__);
		retval = -ENODEV;
		goto object_null;
	}
	/* decrement the count on our device */
	kref_put(&pdx->kref, piusb_delete);

object_null:
	return retval;
}

static int pixis_io(struct ioctl_struct *ctrl, struct device_extension *pdx,
		struct ioctl_struct *arg)
{
	unsigned int numToRead = 0;
	unsigned int totalRead = 0;
	unsigned char *uBuf;
	int numbytes;
	int i;

	uBuf = kmalloc(ctrl->numbytes, GFP_KERNEL);
	if (!uBuf) {
		dbg("Alloc for uBuf failed");
		return 0;
	}
	numbytes = (int) ctrl->numbytes;
	numToRead = (unsigned int) ctrl->numbytes;
	dbg("numbytes to read = %d", numbytes);
	dbg("endpoint # %d", ctrl->endpoint);

	if (copy_from_user(uBuf, ctrl->pData, numbytes)) {
		dbg("copying ctrl->pData to dummyBuf failed");
		return -EFAULT;
	}

	do {
		i = usb_bulk_msg(pdx->udev, pdx->hEP[ctrl->endpoint],
				(uBuf + totalRead),
				/* EP0 can only handle 64 bytes at a time */
				(numToRead > 64) ? 64 : numToRead,
				&numbytes, HZ * 10);
		if (i) {
			dbg("CMD = %s, Address = 0x%02X",
					((uBuf[3] == 0x02) ? "WRITE" : "READ"),
					uBuf[1]);
			dbg("Number of bytes Attempted to read = %d",
					(int)ctrl->numbytes);
			dbg("Blocking ReadI/O Failed with status %d", i);
			kfree(uBuf);
			return -1;
		}
		dbg("Pixis EP0 Read %d bytes", numbytes);
		totalRead += numbytes;
		numToRead -= numbytes;
	} while (numToRead);

	memcpy(ctrl->pData, uBuf, totalRead);
	dbg("Total Bytes Read from PIXIS EP0 = %d", totalRead);
	ctrl->numbytes = totalRead;

	if (copy_to_user(arg, ctrl, sizeof(struct ioctl_struct)))
		dbg("copy_to_user failed in IORB");

	kfree(uBuf);
	return ctrl->numbytes;
}

static int pixel_data(struct ioctl_struct *ctrl, struct device_extension *pdx)
{
	int i;

	if (!pdx->gotPixelData)
		return 0;

	pdx->gotPixelData = 0;
	ctrl->numbytes = pdx->bulk_in_size_returned;
	pdx->bulk_in_size_returned -= pdx->frameSize;

	for (i = 0; i < pdx->maplist_numPagesMapped[pdx->active_frame]; i++)
		SetPageDirty(sg_page(&pdx->sgl[pdx->active_frame][i]));

	pdx->active_frame = ((pdx->active_frame + 1) % pdx->num_frames);

	return ctrl->numbytes;
}

/**
 *	piusb_ioctl
 */
static int piusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
		       unsigned long arg)
{
	struct device_extension *pdx;
	char dummyCtlBuf[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	unsigned long devRB = 0;
	int err = 0;
	int retval = 0;
	struct ioctl_struct ctrl;
	unsigned short controlData = 0;

	pdx = (struct device_extension *)file->private_data;
	/* verify that the device wasn't unplugged */
	if (!pdx->present) {
		dbg("No Device Present\n");
		return -ENODEV;
	}
	/* fill in your device specific stuff here */
	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(&pdx->udev->dev, "return with error = %d\n", err);
		return -EFAULT;
	}
	switch (cmd) {
	case PIUSB_GETVNDCMD:
		if (__copy_from_user
		    (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct))) {
			dev_err(&pdx->udev->dev, "copy_from_user failed\n");
			return -EFAULT;
		}
		dbg("%s %x\n", "Get Vendor Command = ", ctrl.cmd);
		retval =
		    usb_control_msg(pdx->udev, usb_rcvctrlpipe(pdx->udev, 0),
				    ctrl.cmd, USB_DIR_IN, 0, 0, &devRB,
				    ctrl.numbytes, HZ * 10);
		if (ctrl.cmd == 0xF1) {
			dbg("FW Version returned from HW = %ld.%ld",
			    (devRB >> 8), (devRB & 0xFF));
		}
		if (retval >= 0)
			retval = (int)devRB;
		return retval;

	case PIUSB_SETVNDCMD:
		if (__copy_from_user
		    (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct))) {
			dev_err(&pdx->udev->dev, "copy_from_user failed\n");
			return -EFAULT;
		}
		/* dbg( "%s %x", "Set Vendor Command = ",ctrl.cmd ); */
		controlData = ctrl.pData[0];
		controlData |= (ctrl.pData[1] << 8);
		/* dbg( "%s %d", "Vendor Data =",controlData ); */
		retval = usb_control_msg(pdx->udev,
				usb_sndctrlpipe(pdx->udev, 0),
				ctrl.cmd,
				(USB_DIR_OUT | USB_TYPE_VENDOR
				 /* | USB_RECIP_ENDPOINT */),
				controlData, 0,
				&dummyCtlBuf, ctrl.numbytes, HZ * 10);
		return retval;

	case PIUSB_ISHIGHSPEED:
		return ((pdx->udev->speed == USB_SPEED_HIGH) ? 1 : 0);

	case PIUSB_WRITEPIPE:
		if (__copy_from_user(&ctrl, (void __user *)arg, _IOC_SIZE(cmd))) {
			dev_err(&pdx->udev->dev,
					"copy_from_user WRITE_DUMMY failed\n");
			return -EFAULT;
		}
		if (!access_ok(VERIFY_READ, ctrl.pData, ctrl.numbytes)) {
			dbg("can't access pData");
			return 0;
		}
		piusb_output(&ctrl, ctrl.pData /* uBuf */, ctrl.numbytes, pdx);
		return ctrl.numbytes;

	case PIUSB_USERBUFFER:
		if (__copy_from_user
		    (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct))) {
			dev_err(&pdx->udev->dev, "copy_from_user failed\n");
			return -EFAULT;
		}
		return MapUserBuffer((struct ioctl_struct *) &ctrl, pdx);

	case PIUSB_UNMAP_USERBUFFER:
		retval = UnMapUserBuffer(pdx);
		return retval;

	case PIUSB_READPIPE:
		if (__copy_from_user(&ctrl, (void __user *)arg,
					sizeof(struct ioctl_struct))) {
			dev_err(&pdx->udev->dev, "copy_from_user failed\n");
			return -EFAULT;
		}
		if (((0 == ctrl.endpoint) && (PIXIS_PID == pdx->iama)) ||
				(1 == ctrl.endpoint) ||	/* ST133IO */
				(4 == ctrl.endpoint))	/* PIXIS IO */
			return pixis_io(&ctrl, pdx,
					(struct ioctl_struct *)arg);
		else if ((0 == ctrl.endpoint) || /* ST133 Pixel Data */
				(2 == ctrl.endpoint) || /* PIXIS Ping */
				(3 == ctrl.endpoint))	/* PIXIS Pong */
			return pixel_data(&ctrl, pdx);

		break;

	case PIUSB_WHATCAMERA:
		return pdx->iama;

	case PIUSB_SETFRAMESIZE:
		dbg("PIUSB_SETFRAMESIZE");
		if (__copy_from_user
		    (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct))) {
			dev_err(&pdx->udev->dev, "copy_from_user failed\n");
			return -EFAULT;
		}
		pdx->frameSize = ctrl.numbytes;
		pdx->num_frames = ctrl.numFrames;
		if (!pdx->sgl)
			pdx->sgl =
			    kmalloc(sizeof(struct scatterlist *) *
				    pdx->num_frames, GFP_KERNEL);
		if (!pdx->sgEntries)
			pdx->sgEntries =
			    kmalloc(sizeof(unsigned int) * pdx->num_frames,
				    GFP_KERNEL);
		if (!pdx->PixelUrb)
			pdx->PixelUrb =
			    kmalloc(sizeof(struct urb **) * pdx->num_frames,
				    GFP_KERNEL);
		if (!pdx->maplist_numPagesMapped)
			pdx->maplist_numPagesMapped =
			    vmalloc(sizeof(unsigned int) * pdx->num_frames);
		if (!pdx->pendedPixelUrbs)
			pdx->pendedPixelUrbs =
			    kmalloc(sizeof(char *) * pdx->num_frames,
				    GFP_KERNEL);
		return 0;

	default:
		dbg("%s\n", "No IOCTL found");
		break;

	}
	/* return that we did not understand this ioctl call */
	dbg("Returning -ENOTTY");
	return -ENOTTY;
}

static void piusb_write_bulk_callback(struct urb *urb)
{
	struct device_extension *pdx = urb->context;
	int status = urb->status;

	/* sync/async unlink faults aren't errors */
	if (status && !(status == -ENOENT || status == -ECONNRESET))
		dev_dbg(&urb->dev->dev,
			"%s - nonzero write bulk status received: %d",
			__func__, status);

	pdx->pendingWrite = 0;
	kfree(urb->transfer_buffer);
}

int piusb_output(struct ioctl_struct *io, unsigned char *uBuf, int len,
		 struct device_extension *pdx)
{
	struct urb *urb = NULL;
	int err = 0;
	unsigned char *kbuf = NULL;

	urb = usb_alloc_urb(0, GFP_KERNEL);
	if (urb != NULL) {
		kbuf = kmalloc(len, GFP_KERNEL);
		if (!kbuf) {
			dev_err(&pdx->udev->dev, "buffer_alloc failed\n");
			return -ENOMEM;
		}
		if(__copy_from_user(kbuf, uBuf, len)) {
			dev_err(&pdx->udev->dev, "__copy_from_user failed\n");
			return -EFAULT;
		}
		usb_fill_bulk_urb(urb, pdx->udev, pdx->hEP[io->endpoint], kbuf,
				  len, piusb_write_bulk_callback, pdx);
		err = usb_submit_urb(urb, GFP_KERNEL);
		if (err) {
			dev_err(&pdx->udev->dev,
				"WRITE ERROR:submit urb error = %d\n", err);
		}
		pdx->pendingWrite = 1;
		usb_free_urb(urb);
	}
	return -EINPROGRESS;
}

static int UnMapUserBuffer(struct device_extension *pdx)
{
	int i = 0;
	int k = 0;
	unsigned int epAddr;

	for (k = 0; k < pdx->num_frames; k++) {
		dbg("Killing Urbs for Frame %d", k);
		for (i = 0; i < pdx->sgEntries[k]; i++) {
			usb_kill_urb(pdx->PixelUrb[k][i]);
			usb_free_urb(pdx->PixelUrb[k][i]);
			pdx->pendedPixelUrbs[k][i] = 0;
		}
		dbg("Urb error count = %d", errCnt);
		errCnt = 0;
		dbg("Urbs free'd and Killed for Frame %d", k);
	}

	for (k = 0; k < pdx->num_frames; k++) {
		if (pdx->iama == PIXIS_PID)
			/* which EP should we map this frame to ? */
			/* PONG, odd frames: hEP[3] */
			/* PING, even frames and zero hEP[2] */
			epAddr = (k % 2) ? pdx->hEP[3] : pdx->hEP[2];
		else
			/* ST133 only has 1 endpoint for Pixel data transfer */
			epAddr = pdx->hEP[0];

		usb_buffer_unmap_sg(pdx->udev, epAddr, pdx->sgl[k],
				    pdx->maplist_numPagesMapped[k]);
		for (i = 0; i < pdx->maplist_numPagesMapped[k]; i++)
			page_cache_release(sg_page(&pdx->sgl[k][i]));
		kfree(pdx->sgl[k]);
		kfree(pdx->PixelUrb[k]);
		kfree(pdx->pendedPixelUrbs[k]);
		pdx->sgl[k] = NULL;
		pdx->PixelUrb[k] = NULL;
		pdx->pendedPixelUrbs[k] = NULL;
	}

	kfree(pdx->sgEntries);
	vfree(pdx->maplist_numPagesMapped);
	pdx->sgEntries = NULL;
	pdx->maplist_numPagesMapped = NULL;
	kfree(pdx->sgl);
	kfree(pdx->pendedPixelUrbs);
	kfree(pdx->PixelUrb);
	pdx->sgl = NULL;
	pdx->pendedPixelUrbs = NULL;
	pdx->PixelUrb = NULL;

	return 0;
}

static void piusb_readPIXEL_callback(struct urb *urb)
{
	int i = 0;
	struct device_extension *pdx = urb->context;
	int status = urb->status;

	if (status && !(status == -ENOENT || status == -ECONNRESET)) {
		dbg("%s - nonzero read bulk status received: %d", __func__,
		    status);
		dbg("Error in read EP2 callback");
		dbg("FrameIndex = %d", pdx->frameIdx);
		dbg("Bytes received before problem occurred = %d",
		    pdx->bulk_in_byte_trk);
		dbg("Urb Idx = %d", pdx->urbIdx);
		pdx->pendedPixelUrbs[pdx->frameIdx][pdx->urbIdx] = 0;
	} else {
		pdx->bulk_in_byte_trk += urb->actual_length;
		i = usb_submit_urb(urb, GFP_ATOMIC);	/* resubmit the URB */
		if (i) {
			errCnt++;
			if (i != lastErr) {
				dbg("submit urb in callback failed "
						"with error code %d", i);
				lastErr = i;
			}
		} else {
			pdx->urbIdx++; /* point to next URB when we callback */
			if (pdx->bulk_in_byte_trk >= pdx->frameSize) {
				pdx->bulk_in_size_returned =
					pdx->bulk_in_byte_trk;
				pdx->bulk_in_byte_trk = 0;
				pdx->gotPixelData = 1;
				pdx->frameIdx =
					((pdx->frameIdx +
					  1) % pdx->num_frames);
				pdx->urbIdx = 0;
			}
		}
	}
}

/* MapUserBuffer(
	inputs:
	struct ioctl_struct *io - structure containing user address,
				frame #, and size
	struct device_extension *pdx - the PIUSB device extension

	returns:
	int - status of the task

	Notes:
	MapUserBuffer maps a buffer passed down through an ioctl.
	The user buffer is Page Aligned by the app and then passed down.
	The function get_free_pages(...) does the actual mapping of the buffer
	from user space to kernel space.
	From there a scatterlist is created from all the pages.
	The next function called is to usb_buffer_map_sg which allocated
	DMA addresses for each page, even coalescing them if possible.
	The DMA address is placed in the scatterlist structure.
	The function returns the number of DMA addresses.
	This may or may not be equal to the number of pages that
	the user buffer uses.
	We then build an URB for each DMA address and then submit them.
*/

/*
int MapUserBuffer(unsigned long uaddr, unsigned long numbytes,
		unsigned long frameInfo, struct device_extension *pdx)
*/
static int MapUserBuffer(struct ioctl_struct *io, struct device_extension *pdx)
{
	unsigned long uaddr;
	unsigned long numbytes;
	int frameInfo;	/* which frame we're mapping */
	unsigned int epAddr = 0;
	unsigned long count = 0;
	int i = 0;
	int k = 0;
	int err = 0;
	struct page **maplist_p;
	int numPagesRequired;

	frameInfo = io->numFrames;
	uaddr = (unsigned long)io->pData;
	numbytes = io->numbytes;

	if (pdx->iama == PIXIS_PID) {
		/* which EP should we map this frame to ? */
		/* PONG, odd frames: hEP[3] */
		/* PING, even frames and zero hEP[2] */
		epAddr = (frameInfo % 2) ? pdx->hEP[3] : pdx->hEP[2];
		dbg("Pixis Frame #%d: EP=%d", frameInfo,
		    (epAddr == pdx->hEP[2]) ? 2 : 4);
	} else { /* ST133 only has 1 endpoint for Pixel data transfer */
		epAddr = pdx->hEP[0];
		dbg("ST133 Frame #%d: EP=2", frameInfo);
	}
	count = numbytes;
	dbg("UserAddress = 0x%08lX", uaddr);
	dbg("numbytes = %d", (int)numbytes);

	/* number of pages to map the entire user space DMA buffer */
	numPagesRequired =
	    ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT;
	dbg("Number of pages needed = %d", numPagesRequired);
	maplist_p = vmalloc(numPagesRequired * sizeof(struct page *));
	if (!maplist_p) {
		dbg("Can't Allocate Memory for maplist_p");
		return -ENOMEM;
	}

	/* map the user buffer to kernel memory */
	down_write(&current->mm->mmap_sem);
	pdx->maplist_numPagesMapped[frameInfo] = get_user_pages(current,
			current->mm, (uaddr & PAGE_MASK), numPagesRequired,
			WRITE, 0 /* Don't Force*/, maplist_p, NULL);
	up_write(&current->mm->mmap_sem);
	dbg("Number of pages mapped = %d",
	    pdx->maplist_numPagesMapped[frameInfo]);

	for (i = 0; i < pdx->maplist_numPagesMapped[frameInfo]; i++)
		flush_dcache_page(maplist_p[i]);
	if (!pdx->maplist_numPagesMapped[frameInfo]) {
		dbg("get_user_pages() failed");
		vfree(maplist_p);
		return -ENOMEM;
	}

	/* need to create a scatterlist that spans each frame
	 * that can fit into the mapped buffer
	 */
	pdx->sgl[frameInfo] =
	    kmalloc((pdx->maplist_numPagesMapped[frameInfo] *
		     sizeof(struct scatterlist)), GFP_ATOMIC);
	if (!pdx->sgl[frameInfo]) {
		vfree(maplist_p);
		dbg("can't allocate mem for sgl");
		return -ENOMEM;
	}
	sg_assign_page(&pdx->sgl[frameInfo][0], maplist_p[0]);
	pdx->sgl[frameInfo][0].offset = uaddr & ~PAGE_MASK;
	if (pdx->maplist_numPagesMapped[frameInfo] > 1) {
		pdx->sgl[frameInfo][0].length =
		    PAGE_SIZE - pdx->sgl[frameInfo][0].offset;
		count -= pdx->sgl[frameInfo][0].length;
		for (k = 1; k < pdx->maplist_numPagesMapped[frameInfo]; k++) {
			pdx->sgl[frameInfo][k].offset = 0;
			sg_assign_page(&pdx->sgl[frameInfo][k], maplist_p[k]);
			pdx->sgl[frameInfo][k].length =
			    (count < PAGE_SIZE) ? count : PAGE_SIZE;
			count -= PAGE_SIZE; /* example had PAGE_SIZE here */
		}
	} else {
		pdx->sgl[frameInfo][0].length = count;
	}
	pdx->sgEntries[frameInfo] =
	    usb_buffer_map_sg(pdx->udev, epAddr, pdx->sgl[frameInfo],
			      pdx->maplist_numPagesMapped[frameInfo]);
	dbg("number of sgEntries = %d", pdx->sgEntries[frameInfo]);
	pdx->userBufMapped = 1;
	vfree(maplist_p);

	/* Create and Send the URB's for each s/g entry */
	pdx->PixelUrb[frameInfo] =
	    kmalloc(pdx->sgEntries[frameInfo] * sizeof(struct urb *),
		    GFP_KERNEL);
	if (!pdx->PixelUrb[frameInfo]) {
		dbg("Can't Allocate Memory for Urb");
		return -ENOMEM;
	}
	for (i = 0; i < pdx->sgEntries[frameInfo]; i++) {
		/* 0 iso packets because we're using BULK transfers */
		pdx->PixelUrb[frameInfo][i] = usb_alloc_urb(0, GFP_KERNEL);
		usb_fill_bulk_urb(pdx->PixelUrb[frameInfo][i],
				  pdx->udev,
				  epAddr,
				  NULL, // non-DMA HC? buy a better hardware
				  sg_dma_len(&pdx->sgl[frameInfo][i]),
				  piusb_readPIXEL_callback, (void *)pdx);
		pdx->PixelUrb[frameInfo][i]->transfer_dma =
		    sg_dma_address(&pdx->sgl[frameInfo][i]);
		pdx->PixelUrb[frameInfo][i]->transfer_flags =
		    URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT;
	}
	/* only interrupt when last URB completes */
	pdx->PixelUrb[frameInfo][--i]->transfer_flags &= ~URB_NO_INTERRUPT;
	pdx->pendedPixelUrbs[frameInfo] =
	    kmalloc((pdx->sgEntries[frameInfo] * sizeof(char)), GFP_KERNEL);
	if (!pdx->pendedPixelUrbs[frameInfo])
		dbg("Can't allocate Memory for pendedPixelUrbs");
	for (i = 0; i < pdx->sgEntries[frameInfo]; i++) {
		err = usb_submit_urb(pdx->PixelUrb[frameInfo][i], GFP_ATOMIC);
		if (err) {
			dbg("%s %d\n", "submit urb error =", err);
			pdx->pendedPixelUrbs[frameInfo][i] = 0;
			return err;
		}
		pdx->pendedPixelUrbs[frameInfo][i] = 1;
	}
	return 0;
}

static const struct file_operations piusb_fops = {
	.owner = THIS_MODULE,
	.ioctl = piusb_ioctl,
	.open = piusb_open,
	.release = piusb_release,
};

static struct usb_class_driver piusb_class = {
	.name = "usb/rspiusb%d",
	.fops = &piusb_fops,
	.minor_base = PIUSB_MINOR_BASE,
};

/**
 *	piusb_probe
 *
 *	Called by the usb core when a new device is connected that it thinks
 *	this driver might be interested in.
 */
static int piusb_probe(struct usb_interface *interface,
		       const struct usb_device_id *id)
{
	struct device_extension *pdx = NULL;
	struct usb_host_interface *iface_desc;
	struct usb_endpoint_descriptor *endpoint;
	int i;
	int retval = -ENOMEM;

	dev_dbg(&interface->dev, "%s - Looking for PI USB Hardware", __func__);

	pdx = kzalloc(sizeof(struct device_extension), GFP_KERNEL);
	if (pdx == NULL) {
		dev_err(&interface->dev, "Out of memory\n");
		goto error;
	}
	kref_init(&pdx->kref);
	pdx->udev = usb_get_dev(interface_to_usbdev(interface));
	pdx->interface = interface;
	iface_desc = interface->cur_altsetting;

	/* See if the device offered us matches what we can accept */
	if ((pdx->udev->descriptor.idVendor != VENDOR_ID)
	    || ((pdx->udev->descriptor.idProduct != PIXIS_PID)
		&& (pdx->udev->descriptor.idProduct != ST133_PID)))
		return -ENODEV;

	pdx->iama = pdx->udev->descriptor.idProduct;

	if (debug) {
		if (pdx->udev->descriptor.idProduct == PIXIS_PID)
			dbg("PIUSB:Pixis Camera Found");
		else
			dbg("PIUSB:ST133 USB Controller Found");
		if (pdx->udev->speed == USB_SPEED_HIGH)
			dbg("Highspeed(USB2.0) Device Attached");
		else
			dbg("Lowspeed (USB1.1) Device Attached");

		dbg("NumEndpoints in Configuration: %d",
		    iface_desc->desc.bNumEndpoints);
	}
	for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
		endpoint = &iface_desc->endpoint[i].desc;
		if (debug) {
			dbg("Endpoint[%d]->bDescriptorType = %d", i,
			    endpoint->bDescriptorType);
			dbg("Endpoint[%d]->bEndpointAddress = 0x%02X", i,
			    endpoint->bEndpointAddress);
			dbg("Endpoint[%d]->bbmAttributes = %d", i,
			    endpoint->bmAttributes);
			dbg("Endpoint[%d]->MaxPacketSize = %d\n", i,
			    endpoint->wMaxPacketSize);
		}
		if (usb_endpoint_xfer_bulk(endpoint)) {
			if (usb_endpoint_dir_in(endpoint))
				pdx->hEP[i] =
				    usb_rcvbulkpipe(pdx->udev,
						    endpoint->bEndpointAddress);
			else
				pdx->hEP[i] =
				    usb_sndbulkpipe(pdx->udev,
						    endpoint->bEndpointAddress);
		}
	}
	usb_set_intfdata(interface, pdx);
	retval = usb_register_dev(interface, &piusb_class);
	if (retval) {
		err("Not able to get a minor for this device.");
		usb_set_intfdata(interface, NULL);
		goto error;
	}
	pdx->present = 1;

	/* we can register the device now, as it is ready */
	pdx->minor = interface->minor;
	/* let the user know what node this device is now attached to */
	dbg("PI USB2.0 device now attached to piusb-%d", pdx->minor);
	return 0;

error:
	if (pdx)
		kref_put(&pdx->kref, piusb_delete);
	return retval;
}

/**
 *	piusb_disconnect
 *
 *	Called by the usb core when the device is removed from the system.
 *
 *	This routine guarantees that the driver will not submit any more urbs
 *	by clearing pdx->udev.  It is also supposed to terminate any currently
 *	active urbs.  Unfortunately, usb_bulk_msg(), used in piusb_read(), does
 *	not provide any way to do this.  But at least we can cancel an active
 *	write.
 */
static void piusb_disconnect(struct usb_interface *interface)
{
	struct device_extension *pdx;
	int minor = interface->minor;

	lock_kernel();

	pdx = usb_get_intfdata(interface);
	usb_set_intfdata(interface, NULL);

	/* give back our minor */
	usb_deregister_dev(interface, &piusb_class);

	unlock_kernel();

	/* prevent device read, write and ioctl */
	pdx->present = 0;
	kref_put(&pdx->kref, piusb_delete);
	dbg("PI USB2.0 device #%d now disconnected\n", minor);
}

static struct usb_driver piusb_driver = {
	.name = "sub",
	.probe = piusb_probe,
	.disconnect = piusb_disconnect,
	.id_table = pi_device_table,
};

/**
 *	piusb_init
 */
static int __init piusb_init(void)
{
	int result;

	lastErr = 0;
	errCnt = 0;

	/* register this driver with the USB subsystem */
	result = usb_register(&piusb_driver);
	if (result)
		printk(KERN_ERR KBUILD_MODNAME
				": usb_register failed. Error number %d\n",
				result);
	else
		printk(KERN_INFO KBUILD_MODNAME ":%s: %s\n", DRIVER_DESC,
				DRIVER_VERSION);
	return result;
}

/**
 *	piusb_exit
 */
static void __exit piusb_exit(void)
{
	/* deregister this driver with the USB subsystem */
	usb_deregister(&piusb_driver);
}

module_init(piusb_init);
module_exit(piusb_exit);

/* Module parameters */
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "Debug enabled or not");

MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL v2");