4 * Copyright (C) 2005, 2006 Princeton Instruments
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation version 2 of the License
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/vmalloc.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/init.h>
24 #include <linux/slab.h>
25 #include <linux/module.h>
26 #include <linux/smp_lock.h>
27 #include <linux/completion.h>
28 #include <linux/scatterlist.h>
29 #include <linux/usb.h>
31 #include <linux/pagemap.h>
32 #include <linux/ioctl.h>
35 #ifdef CONFIG_USB_DEBUG
40 /* Use our own dbg macro */
42 #define dbg(format, arg...) do { if (debug) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg); } while (0)
44 /* Version Information */
45 #define DRIVER_VERSION "V1.0.1"
46 #define DRIVER_AUTHOR "Princeton Instruments"
47 #define DRIVER_DESC "PI USB2.0 Device Driver for Linux"
49 /* Define these values to match your devices */
50 #define VENDOR_ID 0x0BD7
51 #define ST133_PID 0xA010
52 #define PIXIS_PID 0xA026
54 /* Get a minor range for your devices from the usb maintainer */
55 #ifdef CONFIG_USB_DYNAMIC_MINORS
56 #define PIUSB_MINOR_BASE 0
58 #define PIUSB_MINOR_BASE 192
61 /* prevent races between open() and disconnect() */
62 static DECLARE_MUTEX(disconnect_sem);
64 /* Structure to hold all of our device specific stuff */
65 struct device_extension {
66 struct usb_device *udev; /* save off the usb device pointer */
67 struct usb_interface *interface; /* the interface for this device */
68 unsigned char minor; /* the starting minor number for this device */
69 size_t bulk_in_size_returned;
71 struct urb ***PixelUrb;
74 unsigned int *maplist_numPagesMapped;
75 int open; /* if the port is open or not */
76 int present; /* if the device is not disconnected */
77 int userBufMapped; /* has the user buffer been mapped? */
78 struct scatterlist **sgl; /* scatter-gather list for user buffer */
79 unsigned int *sgEntries;
83 char **pendedPixelUrbs;
84 int iama; /*PIXIS or ST133 */
85 int num_frames; /* the number of frames that will fit in the user buffer */
87 unsigned long frameSize;
89 //FX2 specific endpoints
92 #define to_pi_dev(d) container_of( d, struct device_extension, kref )
94 static int MapUserBuffer(struct ioctl_struct *, struct device_extension *);
95 static int UnMapUserBuffer(struct device_extension *);
96 static int piusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
98 static int piusb_output(struct ioctl_struct *, unsigned char *, int, struct device_extension *);
99 static struct usb_driver piusb_driver;
101 /* table of devices that work with this driver */
102 static struct usb_device_id pi_device_table[] = {
103 {USB_DEVICE(VENDOR_ID, ST133_PID)},
104 {USB_DEVICE(VENDOR_ID, PIXIS_PID)},
105 {0, } /* Terminating entry */
108 MODULE_DEVICE_TABLE(usb, pi_device_table);
110 static int lastErr = 0;
111 static int errCnt = 0;
113 static void piusb_delete(struct kref *kref)
115 struct device_extension *pdx = to_pi_dev(kref);
117 dev_dbg(&pdx->udev->dev, "%s\n", __func__);
118 usb_put_dev(pdx->udev);
122 static int piusb_open(struct inode *inode, struct file *file)
124 struct device_extension *pdx = NULL;
125 struct usb_interface *interface;
130 subminor = iminor(inode);
131 interface = usb_find_interface(&piusb_driver, subminor);
133 printk(KERN_ERR "%s - error, can't find device for minor %d\n",
139 pdx = usb_get_intfdata(interface);
144 dbg("Alternate Setting = %d", interface->num_altsetting);
146 pdx->frameIdx = pdx->urbIdx = 0;
147 pdx->gotPixelData = 0;
148 pdx->pendingWrite = 0;
151 pdx->active_frame = 0;
152 pdx->bulk_in_byte_trk = 0;
153 pdx->userBufMapped = 0;
154 pdx->pendedPixelUrbs = NULL;
155 pdx->sgEntries = NULL;
157 pdx->maplist_numPagesMapped = NULL;
158 pdx->PixelUrb = NULL;
159 pdx->bulk_in_size_returned = 0;
160 /* increment our usage count for the device */
161 kref_get(&pdx->kref);
162 /* save our object in the file's private structure */
163 file->private_data = pdx;
168 static int piusb_release(struct inode *inode, struct file *file)
170 struct device_extension *pdx;
173 dbg("Piusb_Release()");
174 pdx = (struct device_extension *)file->private_data;
176 dbg("%s - object is NULL", __func__);
179 /* decrement the count on our device */
180 kref_put(&pdx->kref, piusb_delete);
187 static int piusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
190 struct device_extension *pdx;
191 char dummyCtlBuf[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
192 unsigned long devRB = 0;
196 struct ioctl_struct ctrl;
199 unsigned short controlData = 0;
201 pdx = (struct device_extension *)file->private_data;
202 /* verify that the device wasn't unplugged */
204 dbg("No Device Present\n");
207 /* fill in your device specific stuff here */
208 if (_IOC_DIR(cmd) & _IOC_READ)
209 err = !access_ok(VERIFY_WRITE, (void __user *)arg, _IOC_SIZE(cmd));
210 else if (_IOC_DIR(cmd) & _IOC_WRITE)
211 err = !access_ok(VERIFY_READ, (void __user *)arg, _IOC_SIZE(cmd));
213 dev_err(&pdx->udev->dev, "return with error = %d\n", err);
217 case PIUSB_GETVNDCMD:
219 (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct)))
220 info("copy_from_user failed\n");
221 dbg("%s %x\n", "Get Vendor Command = ", ctrl.cmd);
223 usb_control_msg(pdx->udev, usb_rcvctrlpipe(pdx->udev, 0),
224 ctrl.cmd, USB_DIR_IN, 0, 0, &devRB,
225 ctrl.numbytes, HZ * 10);
226 if (ctrl.cmd == 0xF1) {
227 dbg("FW Version returned from HW = %ld.%ld",
228 (devRB >> 8), (devRB & 0xFF));
231 case PIUSB_SETVNDCMD:
233 (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct)))
234 info("copy_from_user failed\n");
235 // dbg( "%s %x", "Set Vendor Command = ",ctrl.cmd );
236 controlData = ctrl.pData[0];
237 controlData |= (ctrl.pData[1] << 8);
238 // dbg( "%s %d", "Vendor Data =",controlData );
239 retval = usb_control_msg(pdx->udev, usb_sndctrlpipe(pdx->udev, 0), ctrl.cmd, (USB_DIR_OUT | USB_TYPE_VENDOR), /* | USB_RECIP_ENDPOINT), */
242 &dummyCtlBuf, ctrl.numbytes, HZ * 10);
245 case PIUSB_ISHIGHSPEED:
246 return ((pdx->udev->speed == USB_SPEED_HIGH) ? 1 : 0);
248 case PIUSB_WRITEPIPE:
249 if (copy_from_user(&ctrl, (void __user *)arg, _IOC_SIZE(cmd)))
250 info("copy_from_user WRITE_DUMMY failed\n");
251 if (!access_ok(VERIFY_READ, ctrl.pData, ctrl.numbytes)) {
252 dbg("can't access pData");
255 piusb_output(&ctrl, ctrl.pData /*uBuf */ , ctrl.numbytes, pdx);
256 return ctrl.numbytes;
258 case PIUSB_USERBUFFER:
260 (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct)))
261 info("copy_from_user failed\n");
262 return MapUserBuffer((struct ioctl_struct *) & ctrl, pdx);
264 case PIUSB_UNMAP_USERBUFFER:
265 UnMapUserBuffer(pdx);
270 (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct)))
271 info("copy_from_user failed\n");
272 switch (ctrl.endpoint) {
273 case 0: //ST133 Pixel Data or PIXIS IO
274 if (pdx->iama == PIXIS_PID) {
275 unsigned int numToRead = 0;
276 unsigned int totalRead = 0;
277 uBuf = kmalloc(ctrl.numbytes, GFP_KERNEL);
279 dbg("Alloc for uBuf failed");
282 numbytes = ctrl.numbytes;
283 numToRead = numbytes;
284 dbg("numbytes to read = %d", numbytes);
285 dbg("endpoint # %d", ctrl.endpoint);
286 if (copy_from_user(uBuf, ctrl.pData, numbytes))
287 dbg("copying ctrl.pData to dummyBuf failed");
289 i = usb_bulk_msg(pdx->udev, pdx->hEP[ctrl.endpoint], (uBuf + totalRead), (numToRead > 64) ? 64 : numToRead, &numbytes, HZ * 10); //EP0 can only handle 64 bytes at a time
291 dbg("CMD = %s, Address = 0x%02X", ((uBuf[3] == 0x02) ? "WRITE" : "READ"), uBuf[1]);
292 dbg("Number of bytes Attempted to read = %d", (int)ctrl.numbytes);
293 dbg("Blocking ReadI/O Failed with status %d", i);
297 dbg("Pixis EP0 Read %d bytes",
299 totalRead += numbytes;
300 numToRead -= numbytes;
304 memcpy(ctrl.pData, uBuf, totalRead);
305 dbg("Total Bytes Read from PIXIS EP0 = %d",
307 ctrl.numbytes = totalRead;
309 ((struct ioctl_struct *) arg, &ctrl,
310 sizeof(struct ioctl_struct)))
311 dbg("copy_to_user failed in IORB");
313 return ctrl.numbytes;
314 } else //ST133 Pixel Data
316 if (!pdx->gotPixelData)
319 pdx->gotPixelData = 0;
321 pdx->bulk_in_size_returned;
322 pdx->bulk_in_size_returned -=
324 for (i = 0; i < pdx->maplist_numPagesMapped[pdx->active_frame]; i++)
325 SetPageDirty(pdx->sgl[pdx->active_frame][i].page_link);
327 ((pdx->active_frame +
328 1) % pdx->num_frames);
329 return ctrl.numbytes;
335 uBuf = kmalloc(ctrl.numbytes, GFP_KERNEL);
337 dbg("Alloc for uBuf failed");
340 numbytes = ctrl.numbytes;
341 // dbg( "numbytes to read = %d", numbytes );
342 if (copy_from_user(uBuf, ctrl.pData, numbytes))
343 dbg("copying ctrl.pData to dummyBuf failed");
344 i = usb_bulk_msg(pdx->udev, pdx->hEP[ctrl.endpoint],
345 uBuf, numbytes, &numbytes, HZ * 10);
347 dbg("Blocking ReadI/O Failed with status %d",
352 ctrl.numbytes = numbytes;
353 memcpy(ctrl.pData, uBuf, numbytes);
355 ((struct ioctl_struct *) arg, &ctrl,
356 sizeof(struct ioctl_struct)))
357 dbg("copy_to_user failed in IORB");
359 return ctrl.numbytes;
365 if (!pdx->gotPixelData)
368 pdx->gotPixelData = 0;
369 ctrl.numbytes = pdx->bulk_in_size_returned;
370 pdx->bulk_in_size_returned -= pdx->frameSize;
373 pdx->maplist_numPagesMapped[pdx->
376 SetPageDirty(pdx->sgl[pdx->active_frame][i].page_link);
378 ((pdx->active_frame + 1) % pdx->num_frames);
379 return ctrl.numbytes;
384 case PIUSB_WHATCAMERA:
386 case PIUSB_SETFRAMESIZE:
387 dbg("PIUSB_SETFRAMESIZE");
389 (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct)))
390 info("copy_from_user failed\n");
391 pdx->frameSize = ctrl.numbytes;
392 pdx->num_frames = ctrl.numFrames;
395 kmalloc(sizeof(struct scatterlist *) *
396 pdx->num_frames, GFP_KERNEL);
399 kmalloc(sizeof(unsigned int) * pdx->num_frames,
403 kmalloc(sizeof(struct urb **) * pdx->num_frames,
405 if (!pdx->maplist_numPagesMapped)
406 pdx->maplist_numPagesMapped =
407 vmalloc(sizeof(unsigned int) * pdx->num_frames);
408 if (!pdx->pendedPixelUrbs)
409 pdx->pendedPixelUrbs =
410 kmalloc(sizeof(char *) * pdx->num_frames,
414 dbg("%s\n", "No IOCTL found");
418 /* return that we did not understand this ioctl call */
419 dbg("Returning -ENOTTY");
423 static void piusb_write_bulk_callback(struct urb *urb)
425 struct device_extension *pdx = urb->context;
426 int status = urb->status;
428 /* sync/async unlink faults aren't errors */
429 if (status && !(status == -ENOENT || status == -ECONNRESET))
430 dev_dbg(&urb->dev->dev,
431 "%s - nonzero write bulk status received: %d",
434 pdx->pendingWrite = 0;
435 usb_buffer_free(urb->dev, urb->transfer_buffer_length,
436 urb->transfer_buffer, urb->transfer_dma);
439 int piusb_output(struct ioctl_struct * io, unsigned char *uBuf, int len,
440 struct device_extension *pdx)
442 struct urb *urb = NULL;
444 unsigned char *kbuf = NULL;
446 urb = usb_alloc_urb(0, GFP_KERNEL);
449 usb_buffer_alloc(pdx->udev, len, GFP_KERNEL,
452 info("buffer_alloc failed\n");
455 memcpy(kbuf, uBuf, len);
456 usb_fill_bulk_urb(urb, pdx->udev, pdx->hEP[io->endpoint], kbuf,
457 len, piusb_write_bulk_callback, pdx);
458 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
459 err = usb_submit_urb(urb, GFP_KERNEL);
461 dev_err(&pdx->udev->dev,
462 "WRITE ERROR:submit urb error = %d\n", err);
464 pdx->pendingWrite = 1;
470 static int UnMapUserBuffer(struct device_extension *pdx)
475 for (k = 0; k < pdx->num_frames; k++) {
476 dbg("Killing Urbs for Frame %d", k);
477 for (i = 0; i < pdx->sgEntries[k]; i++) {
478 usb_kill_urb(pdx->PixelUrb[k][i]);
479 usb_free_urb(pdx->PixelUrb[k][i]);
480 pdx->pendedPixelUrbs[k][i] = 0;
482 dbg("Urb error count = %d", errCnt);
484 dbg("Urbs free'd and Killed for Frame %d", k);
487 for (k = 0; k < pdx->num_frames; k++) {
488 if (pdx->iama == PIXIS_PID) //if so, which EP should we map this frame to
490 if (k % 2) //check to see if this should use EP4(PONG)
492 epAddr = pdx->hEP[3]; //PONG, odd frames
494 epAddr = pdx->hEP[2]; //PING, even frames and zero
496 } else //ST133 only has 1 endpoint for Pixel data transfer
498 epAddr = pdx->hEP[0];
500 usb_buffer_unmap_sg(pdx->udev, epAddr, pdx->sgl[k],
501 pdx->maplist_numPagesMapped[k]);
502 for (i = 0; i < pdx->maplist_numPagesMapped[k]; i++) {
503 page_cache_release(pdx->sgl[k][i].page_link);
506 kfree(pdx->PixelUrb[k]);
507 kfree(pdx->pendedPixelUrbs[k]);
509 pdx->PixelUrb[k] = NULL;
510 pdx->pendedPixelUrbs[k] = NULL;
512 kfree(pdx->sgEntries);
513 vfree(pdx->maplist_numPagesMapped);
514 pdx->sgEntries = NULL;
515 pdx->maplist_numPagesMapped = NULL;
517 kfree(pdx->pendedPixelUrbs);
518 kfree(pdx->PixelUrb);
520 pdx->pendedPixelUrbs = NULL;
521 pdx->PixelUrb = NULL;
525 static void piusb_readPIXEL_callback(struct urb *urb)
528 struct device_extension *pdx = urb->context;
529 int status = urb->status;
531 if (status && !(status == -ENOENT || status == -ECONNRESET)) {
532 dbg("%s - nonzero read bulk status received: %d", __func__,
534 dbg("Error in read EP2 callback");
535 dbg("FrameIndex = %d", pdx->frameIdx);
536 dbg("Bytes received before problem occurred = %d",
537 pdx->bulk_in_byte_trk);
538 dbg("Urb Idx = %d", pdx->urbIdx);
539 pdx->pendedPixelUrbs[pdx->frameIdx][pdx->urbIdx] = 0;
541 pdx->bulk_in_byte_trk += urb->actual_length;
543 i = usb_submit_urb(urb, GFP_ATOMIC); //resubmit the URB
547 dbg("submit urb in callback failed with error code %d", i);
551 pdx->urbIdx++; //point to next URB when we callback
552 if (pdx->bulk_in_byte_trk >= pdx->frameSize) {
553 pdx->bulk_in_size_returned =
554 pdx->bulk_in_byte_trk;
555 pdx->bulk_in_byte_trk = 0;
556 pdx->gotPixelData = 1;
559 1) % pdx->num_frames);
569 struct ioctl_struct *io - structure containing user address, frame #, and size
570 struct device_extension *pdx - the PIUSB device extension
572 int - status of the task
574 MapUserBuffer maps a buffer passed down through an ioctl. The user buffer is Page Aligned by the app
575 and then passed down. The function get_free_pages(...) does the actual mapping of the buffer from user space to
576 kernel space. From there a scatterlist is created from all the pages. The next function called is to usb_buffer_map_sg
577 which allocated DMA addresses for each page, even coalescing them if possible. The DMA address is placed in the scatterlist
578 structure. The function returns the number of DMA addresses. This may or may not be equal to the number of pages that
579 the user buffer uses. We then build an URB for each DMA address and then submit them.
581 //int MapUserBuffer( unsigned long uaddr, unsigned long numbytes, unsigned long frameInfo, struct device_extension *pdx )
582 static int MapUserBuffer(struct ioctl_struct *io, struct device_extension *pdx)
585 unsigned long numbytes;
586 int frameInfo; //which frame we're mapping
587 unsigned int epAddr = 0;
588 unsigned long count = 0;
592 struct page **maplist_p;
593 int numPagesRequired;
594 frameInfo = io->numFrames;
595 uaddr = (unsigned long)io->pData;
596 numbytes = io->numbytes;
598 if (pdx->iama == PIXIS_PID) //if so, which EP should we map this frame to
600 if (frameInfo % 2) //check to see if this should use EP4(PONG)
602 epAddr = pdx->hEP[3]; //PONG, odd frames
604 epAddr = pdx->hEP[2]; //PING, even frames and zero
606 dbg("Pixis Frame #%d: EP=%d", frameInfo,
607 (epAddr == pdx->hEP[2]) ? 2 : 4);
608 } else //ST133 only has 1 endpoint for Pixel data transfer
610 epAddr = pdx->hEP[0];
611 dbg("ST133 Frame #%d: EP=2", frameInfo);
614 dbg("UserAddress = 0x%08lX", uaddr);
615 dbg("numbytes = %d", (int)numbytes);
616 //number of pages to map the entire user space DMA buffer
618 ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT;
619 dbg("Number of pages needed = %d", numPagesRequired);
620 maplist_p = vmalloc(numPagesRequired * sizeof(struct page)); //, GFP_ATOMIC);
622 dbg("Can't Allocate Memory for maplist_p");
625 //map the user buffer to kernel memory
626 down_write(¤t->mm->mmap_sem);
627 pdx->maplist_numPagesMapped[frameInfo] = get_user_pages(current, current->mm, (uaddr & PAGE_MASK), numPagesRequired, WRITE, 0, //Don't Force
630 up_write(¤t->mm->mmap_sem);
631 dbg("Number of pages mapped = %d",
632 pdx->maplist_numPagesMapped[frameInfo]);
633 for (i = 0; i < pdx->maplist_numPagesMapped[frameInfo]; i++)
634 flush_dcache_page(maplist_p[i]);
635 if (!pdx->maplist_numPagesMapped[frameInfo]) {
636 dbg("get_user_pages() failed");
640 //need to create a scatterlist that spans each frame that can fit into the mapped buffer
641 pdx->sgl[frameInfo] =
642 kmalloc((pdx->maplist_numPagesMapped[frameInfo] *
643 sizeof(struct scatterlist)), GFP_ATOMIC);
644 if (!pdx->sgl[frameInfo]) {
646 dbg("can't allocate mem for sgl");
649 pdx->sgl[frameInfo][0].page_link = maplist_p[0];
650 pdx->sgl[frameInfo][0].offset = uaddr & ~PAGE_MASK;
651 if (pdx->maplist_numPagesMapped[frameInfo] > 1) {
652 pdx->sgl[frameInfo][0].length =
653 PAGE_SIZE - pdx->sgl[frameInfo][0].offset;
654 count -= pdx->sgl[frameInfo][0].length;
655 for (k = 1; k < pdx->maplist_numPagesMapped[frameInfo]; k++) {
656 pdx->sgl[frameInfo][k].offset = 0;
657 pdx->sgl[frameInfo][k].page_link = maplist_p[k];
658 pdx->sgl[frameInfo][k].length =
659 (count < PAGE_SIZE) ? count : PAGE_SIZE;
660 count -= PAGE_SIZE; //example had PAGE_SIZE here;
663 pdx->sgl[frameInfo][0].length = count;
665 pdx->sgEntries[frameInfo] =
666 usb_buffer_map_sg(pdx->udev, epAddr, pdx->sgl[frameInfo],
667 pdx->maplist_numPagesMapped[frameInfo]);
668 dbg("number of sgEntries = %d", pdx->sgEntries[frameInfo]);
669 pdx->userBufMapped = 1;
671 //Create and Send the URB's for each s/g entry
672 pdx->PixelUrb[frameInfo] =
673 kmalloc(pdx->sgEntries[frameInfo] * sizeof(struct urb *),
675 if (!pdx->PixelUrb[frameInfo]) {
676 dbg("Can't Allocate Memory for Urb");
679 for (i = 0; i < pdx->sgEntries[frameInfo]; i++) {
680 pdx->PixelUrb[frameInfo][i] = usb_alloc_urb(0, GFP_KERNEL); //0 because we're using BULK transfers
681 usb_fill_bulk_urb(pdx->PixelUrb[frameInfo][i],
684 (dma_addr_t *) sg_dma_address(&pdx->
687 sg_dma_len(&pdx->sgl[frameInfo][i]),
688 piusb_readPIXEL_callback, (void *)pdx);
689 pdx->PixelUrb[frameInfo][i]->transfer_dma =
690 sg_dma_address(&pdx->sgl[frameInfo][i]);
691 pdx->PixelUrb[frameInfo][i]->transfer_flags =
692 URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT;
694 pdx->PixelUrb[frameInfo][--i]->transfer_flags &= ~URB_NO_INTERRUPT; //only interrupt when last URB completes
695 pdx->pendedPixelUrbs[frameInfo] =
696 kmalloc((pdx->sgEntries[frameInfo] * sizeof(char)), GFP_KERNEL);
697 if (!pdx->pendedPixelUrbs[frameInfo])
698 dbg("Can't allocate Memory for pendedPixelUrbs");
699 for (i = 0; i < pdx->sgEntries[frameInfo]; i++) {
700 err = usb_submit_urb(pdx->PixelUrb[frameInfo][i], GFP_ATOMIC);
702 dbg("%s %d\n", "submit urb error =", err);
703 pdx->pendedPixelUrbs[frameInfo][i] = 0;
706 pdx->pendedPixelUrbs[frameInfo][i] = 1;;
711 static struct file_operations piusb_fops = {
712 .owner = THIS_MODULE,
713 .ioctl = piusb_ioctl,
715 .release = piusb_release,
718 static struct usb_class_driver piusb_class = {
719 .name = "usb/rspiusb%d",
721 .minor_base = PIUSB_MINOR_BASE,
727 * Called by the usb core when a new device is connected that it thinks
728 * this driver might be interested in.
730 static int piusb_probe(struct usb_interface *interface,
731 const struct usb_device_id *id)
733 struct device_extension *pdx = NULL;
734 struct usb_host_interface *iface_desc;
735 struct usb_endpoint_descriptor *endpoint;
737 int retval = -ENOMEM;
739 dev_dbg(&interface->dev, "%s - Looking for PI USB Hardware", __func__);
741 pdx = kzalloc(sizeof(struct device_extension), GFP_KERNEL);
743 dev_err(&interface->dev, "Out of memory\n");
746 kref_init(&pdx->kref);
747 pdx->udev = usb_get_dev(interface_to_usbdev(interface));
748 pdx->interface = interface;
749 iface_desc = interface->cur_altsetting;
751 /* See if the device offered us matches what we can accept */
752 if ((pdx->udev->descriptor.idVendor != VENDOR_ID)
753 || ((pdx->udev->descriptor.idProduct != PIXIS_PID)
754 && (pdx->udev->descriptor.idProduct != ST133_PID))) {
757 pdx->iama = pdx->udev->descriptor.idProduct;
760 if (pdx->udev->descriptor.idProduct == PIXIS_PID)
761 dbg("PIUSB:Pixis Camera Found");
763 dbg("PIUSB:ST133 USB Controller Found");
764 if (pdx->udev->speed == USB_SPEED_HIGH)
765 dbg("Highspeed(USB2.0) Device Attached");
767 dbg("Lowspeed (USB1.1) Device Attached");
769 dbg("NumEndpoints in Configuration: %d",
770 iface_desc->desc.bNumEndpoints);
772 for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
773 endpoint = &iface_desc->endpoint[i].desc;
775 dbg("Endpoint[%d]->bDescriptorType = %d", i,
776 endpoint->bDescriptorType);
777 dbg("Endpoint[%d]->bEndpointAddress = 0x%02X", i,
778 endpoint->bEndpointAddress);
779 dbg("Endpoint[%d]->bbmAttributes = %d", i,
780 endpoint->bmAttributes);
781 dbg("Endpoint[%d]->MaxPacketSize = %d\n", i,
782 endpoint->wMaxPacketSize);
784 if (usb_endpoint_xfer_bulk(endpoint)) {
785 if (usb_endpoint_dir_in(endpoint))
787 usb_rcvbulkpipe(pdx->udev,
788 endpoint->bEndpointAddress);
791 usb_sndbulkpipe(pdx->udev,
792 endpoint->bEndpointAddress);
795 usb_set_intfdata(interface, pdx);
796 retval = usb_register_dev(interface, &piusb_class);
798 err("Not able to get a minor for this device.");
799 usb_set_intfdata(interface, NULL);
804 /* we can register the device now, as it is ready */
805 pdx->minor = interface->minor;
806 /* let the user know what node this device is now attached to */
807 dbg("PI USB2.0 device now attached to piusb-%d", pdx->minor);
812 kref_put(&pdx->kref, piusb_delete);
819 * Called by the usb core when the device is removed from the system.
821 * This routine guarantees that the driver will not submit any more urbs
822 * by clearing pdx->udev. It is also supposed to terminate any currently
823 * active urbs. Unfortunately, usb_bulk_msg(), used in piusb_read(), does
824 * not provide any way to do this. But at least we can cancel an active
827 static void piusb_disconnect(struct usb_interface *interface)
829 struct device_extension *pdx;
830 int minor = interface->minor;
832 pdx = usb_get_intfdata(interface);
833 usb_set_intfdata(interface, NULL);
834 /* give back our minor */
835 usb_deregister_dev(interface, &piusb_class);
837 /* prevent device read, write and ioctl */
839 kref_put(&pdx->kref, piusb_delete);
840 dbg("PI USB2.0 device #%d now disconnected\n", minor);
843 static struct usb_driver piusb_driver = {
845 .probe = piusb_probe,
846 .disconnect = piusb_disconnect,
847 .id_table = pi_device_table,
853 static int __init piusb_init(void)
856 /* register this driver with the USB subsystem */
857 result = usb_register(&piusb_driver);
859 printk(KERN_ERR KBUILD_MODNAME
860 ": usb_register failed. Error number %d\n", result);
863 printk(KERN_INFO KBUILD_MODNAME ":%s: %s\n", DRIVER_DESC,
871 static void __exit piusb_exit(void)
873 /* deregister this driver with the USB subsystem */
874 usb_deregister(&piusb_driver);
877 module_init(piusb_init);
878 module_exit(piusb_exit);
880 /* Module parameters */
881 module_param(debug, int, 0);
882 MODULE_PARM_DESC(debug, "Debug enabled or not");
884 MODULE_AUTHOR(DRIVER_AUTHOR);
885 MODULE_DESCRIPTION(DRIVER_DESC);
886 MODULE_LICENSE("GPL v2");