Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * rspiusb.c |
| 3 | * |
| 4 | * Copyright (C) 2005, 2006 Princeton Instruments |
| 5 | * |
| 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 |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 18 | */ |
| 19 | |
| 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> |
| 30 | #include <linux/mm.h> |
| 31 | #include <linux/pagemap.h> |
| 32 | #include <linux/ioctl.h> |
| 33 | #include "rspiusb.h" |
| 34 | |
| 35 | #ifdef CONFIG_USB_DEBUG |
| 36 | static int debug = 1; |
| 37 | #else |
| 38 | static int debug; |
| 39 | #endif |
| 40 | /* Use our own dbg macro */ |
| 41 | #undef dbg |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 42 | #define dbg(format, arg...) \ |
| 43 | do { \ |
| 44 | if (debug) \ |
| 45 | printk(KERN_DEBUG __FILE__ ": " format "\n" , ##arg); \ |
| 46 | } while (0) |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 47 | |
| 48 | /* Version Information */ |
| 49 | #define DRIVER_VERSION "V1.0.1" |
| 50 | #define DRIVER_AUTHOR "Princeton Instruments" |
| 51 | #define DRIVER_DESC "PI USB2.0 Device Driver for Linux" |
| 52 | |
| 53 | /* Define these values to match your devices */ |
| 54 | #define VENDOR_ID 0x0BD7 |
| 55 | #define ST133_PID 0xA010 |
| 56 | #define PIXIS_PID 0xA026 |
| 57 | |
| 58 | /* Get a minor range for your devices from the usb maintainer */ |
| 59 | #ifdef CONFIG_USB_DYNAMIC_MINORS |
| 60 | #define PIUSB_MINOR_BASE 0 |
| 61 | #else |
| 62 | #define PIUSB_MINOR_BASE 192 |
| 63 | #endif |
| 64 | |
| 65 | /* prevent races between open() and disconnect() */ |
| 66 | static DECLARE_MUTEX(disconnect_sem); |
| 67 | |
| 68 | /* Structure to hold all of our device specific stuff */ |
| 69 | struct device_extension { |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 70 | struct usb_device *udev; /* save off the usb device pointer */ |
| 71 | struct usb_interface *interface; /* the interface for this device */ |
| 72 | unsigned char minor; /* the starting minor number |
| 73 | * for this device |
| 74 | */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 75 | size_t bulk_in_size_returned; |
| 76 | int bulk_in_byte_trk; |
| 77 | struct urb ***PixelUrb; |
| 78 | int frameIdx; |
| 79 | int urbIdx; |
| 80 | unsigned int *maplist_numPagesMapped; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 81 | int open; /* if the port is open or not */ |
| 82 | int present; /* if the device is not disconnected */ |
| 83 | int userBufMapped; /* has the user buffer been mapped ? */ |
| 84 | struct scatterlist **sgl; /* scatter-gather list for user buffer */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 85 | unsigned int *sgEntries; |
| 86 | struct kref kref; |
| 87 | int gotPixelData; |
| 88 | int pendingWrite; |
| 89 | char **pendedPixelUrbs; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 90 | int iama; /* PIXIS or ST133 */ |
| 91 | int num_frames; /* the number of frames that will fit |
| 92 | * in the user buffer |
| 93 | */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 94 | int active_frame; |
| 95 | unsigned long frameSize; |
| 96 | struct semaphore sem; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 97 | unsigned int hEP[8]; /* FX2 specific endpoints */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 98 | }; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 99 | |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 100 | #define to_pi_dev(d) container_of(d, struct device_extension, kref) |
| 101 | |
| 102 | /* Prototypes */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 103 | static int MapUserBuffer(struct ioctl_struct *, struct device_extension *); |
| 104 | static int UnMapUserBuffer(struct device_extension *); |
| 105 | static int piusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd, |
| 106 | unsigned long arg); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 107 | static int piusb_output(struct ioctl_struct *, unsigned char *, int, |
| 108 | struct device_extension *); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 109 | static struct usb_driver piusb_driver; |
| 110 | |
| 111 | /* table of devices that work with this driver */ |
| 112 | static struct usb_device_id pi_device_table[] = { |
| 113 | {USB_DEVICE(VENDOR_ID, ST133_PID)}, |
| 114 | {USB_DEVICE(VENDOR_ID, PIXIS_PID)}, |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 115 | {0, } /* Terminating entry */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | MODULE_DEVICE_TABLE(usb, pi_device_table); |
| 119 | |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 120 | static int lastErr; |
| 121 | static int errCnt; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 122 | |
| 123 | static void piusb_delete(struct kref *kref) |
| 124 | { |
| 125 | struct device_extension *pdx = to_pi_dev(kref); |
| 126 | |
| 127 | dev_dbg(&pdx->udev->dev, "%s\n", __func__); |
| 128 | usb_put_dev(pdx->udev); |
| 129 | kfree(pdx); |
| 130 | } |
| 131 | |
| 132 | static int piusb_open(struct inode *inode, struct file *file) |
| 133 | { |
| 134 | struct device_extension *pdx = NULL; |
| 135 | struct usb_interface *interface; |
| 136 | int subminor; |
| 137 | int retval = 0; |
| 138 | |
| 139 | dbg("Piusb_Open()"); |
| 140 | subminor = iminor(inode); |
| 141 | interface = usb_find_interface(&piusb_driver, subminor); |
| 142 | if (!interface) { |
| 143 | printk(KERN_ERR "%s - error, can't find device for minor %d\n", |
| 144 | __func__, subminor); |
| 145 | retval = -ENODEV; |
| 146 | goto exit_no_device; |
| 147 | } |
| 148 | |
| 149 | pdx = usb_get_intfdata(interface); |
| 150 | if (!pdx) { |
| 151 | retval = -ENODEV; |
| 152 | goto exit_no_device; |
| 153 | } |
| 154 | dbg("Alternate Setting = %d", interface->num_altsetting); |
| 155 | |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 156 | pdx->bulk_in_size_returned = 0; |
| 157 | pdx->bulk_in_byte_trk = 0; |
| 158 | pdx->PixelUrb = NULL; |
| 159 | pdx->frameIdx = 0; |
| 160 | pdx->urbIdx = 0; |
| 161 | pdx->maplist_numPagesMapped = NULL; |
| 162 | pdx->userBufMapped = 0; |
| 163 | pdx->sgl = NULL; |
| 164 | pdx->sgEntries = NULL; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 165 | pdx->gotPixelData = 0; |
| 166 | pdx->pendingWrite = 0; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 167 | pdx->pendedPixelUrbs = NULL; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 168 | pdx->num_frames = 0; |
| 169 | pdx->active_frame = 0; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 170 | pdx->frameSize = 0; |
| 171 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 172 | /* increment our usage count for the device */ |
| 173 | kref_get(&pdx->kref); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 174 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 175 | /* save our object in the file's private structure */ |
| 176 | file->private_data = pdx; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 177 | |
| 178 | exit_no_device: |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 179 | return retval; |
| 180 | } |
| 181 | |
| 182 | static int piusb_release(struct inode *inode, struct file *file) |
| 183 | { |
| 184 | struct device_extension *pdx; |
| 185 | int retval = 0; |
| 186 | |
| 187 | dbg("Piusb_Release()"); |
| 188 | pdx = (struct device_extension *)file->private_data; |
| 189 | if (pdx == NULL) { |
| 190 | dbg("%s - object is NULL", __func__); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 191 | retval = -ENODEV; |
| 192 | goto object_null; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 193 | } |
| 194 | /* decrement the count on our device */ |
| 195 | kref_put(&pdx->kref, piusb_delete); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 196 | |
| 197 | object_null: |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 198 | return retval; |
| 199 | } |
| 200 | |
Richard Genoud | c854b5e | 2009-05-17 13:06:31 +0200 | [diff] [blame] | 201 | static int pixis_io(struct ioctl_struct *ctrl, struct device_extension *pdx, |
| 202 | struct ioctl_struct *arg) |
| 203 | { |
| 204 | unsigned int numToRead = 0; |
| 205 | unsigned int totalRead = 0; |
| 206 | unsigned char *uBuf; |
| 207 | int numbytes; |
| 208 | int i; |
| 209 | |
| 210 | uBuf = kmalloc(ctrl->numbytes, GFP_KERNEL); |
| 211 | if (!uBuf) { |
| 212 | dbg("Alloc for uBuf failed"); |
| 213 | return 0; |
| 214 | } |
| 215 | numbytes = (int) ctrl->numbytes; |
| 216 | numToRead = (unsigned int) ctrl->numbytes; |
| 217 | dbg("numbytes to read = %d", numbytes); |
| 218 | dbg("endpoint # %d", ctrl->endpoint); |
| 219 | |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 220 | if (copy_from_user(uBuf, ctrl->pData, numbytes)) { |
Richard Genoud | c854b5e | 2009-05-17 13:06:31 +0200 | [diff] [blame] | 221 | dbg("copying ctrl->pData to dummyBuf failed"); |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 222 | return -EFAULT; |
| 223 | } |
Richard Genoud | c854b5e | 2009-05-17 13:06:31 +0200 | [diff] [blame] | 224 | |
| 225 | do { |
| 226 | i = usb_bulk_msg(pdx->udev, pdx->hEP[ctrl->endpoint], |
| 227 | (uBuf + totalRead), |
| 228 | /* EP0 can only handle 64 bytes at a time */ |
| 229 | (numToRead > 64) ? 64 : numToRead, |
| 230 | &numbytes, HZ * 10); |
| 231 | if (i) { |
| 232 | dbg("CMD = %s, Address = 0x%02X", |
| 233 | ((uBuf[3] == 0x02) ? "WRITE" : "READ"), |
| 234 | uBuf[1]); |
| 235 | dbg("Number of bytes Attempted to read = %d", |
| 236 | (int)ctrl->numbytes); |
| 237 | dbg("Blocking ReadI/O Failed with status %d", i); |
| 238 | kfree(uBuf); |
| 239 | return -1; |
| 240 | } |
| 241 | dbg("Pixis EP0 Read %d bytes", numbytes); |
| 242 | totalRead += numbytes; |
| 243 | numToRead -= numbytes; |
| 244 | } while (numToRead); |
| 245 | |
| 246 | memcpy(ctrl->pData, uBuf, totalRead); |
| 247 | dbg("Total Bytes Read from PIXIS EP0 = %d", totalRead); |
| 248 | ctrl->numbytes = totalRead; |
| 249 | |
| 250 | if (copy_to_user(arg, ctrl, sizeof(struct ioctl_struct))) |
| 251 | dbg("copy_to_user failed in IORB"); |
| 252 | |
| 253 | kfree(uBuf); |
| 254 | return ctrl->numbytes; |
| 255 | } |
| 256 | |
Richard Genoud | c854b5e | 2009-05-17 13:06:31 +0200 | [diff] [blame] | 257 | static int pixel_data(struct ioctl_struct *ctrl, struct device_extension *pdx) |
| 258 | { |
| 259 | int i; |
| 260 | |
| 261 | if (!pdx->gotPixelData) |
| 262 | return 0; |
| 263 | |
| 264 | pdx->gotPixelData = 0; |
| 265 | ctrl->numbytes = pdx->bulk_in_size_returned; |
| 266 | pdx->bulk_in_size_returned -= pdx->frameSize; |
| 267 | |
| 268 | for (i = 0; i < pdx->maplist_numPagesMapped[pdx->active_frame]; i++) |
vibi sreenivasan | 8d2db51 | 2009-06-04 20:56:45 +0530 | [diff] [blame] | 269 | SetPageDirty(sg_page(&pdx->sgl[pdx->active_frame][i])); |
Richard Genoud | c854b5e | 2009-05-17 13:06:31 +0200 | [diff] [blame] | 270 | |
| 271 | pdx->active_frame = ((pdx->active_frame + 1) % pdx->num_frames); |
| 272 | |
| 273 | return ctrl->numbytes; |
| 274 | } |
| 275 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 276 | /** |
| 277 | * piusb_ioctl |
| 278 | */ |
| 279 | static int piusb_ioctl(struct inode *inode, struct file *file, unsigned int cmd, |
| 280 | unsigned long arg) |
| 281 | { |
| 282 | struct device_extension *pdx; |
| 283 | char dummyCtlBuf[] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 284 | unsigned long devRB = 0; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 285 | int err = 0; |
| 286 | int retval = 0; |
| 287 | struct ioctl_struct ctrl; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 288 | unsigned short controlData = 0; |
| 289 | |
| 290 | pdx = (struct device_extension *)file->private_data; |
| 291 | /* verify that the device wasn't unplugged */ |
| 292 | if (!pdx->present) { |
| 293 | dbg("No Device Present\n"); |
| 294 | return -ENODEV; |
| 295 | } |
| 296 | /* fill in your device specific stuff here */ |
| 297 | if (_IOC_DIR(cmd) & _IOC_READ) |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 298 | err = !access_ok(VERIFY_WRITE, (void __user *)arg, |
| 299 | _IOC_SIZE(cmd)); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 300 | else if (_IOC_DIR(cmd) & _IOC_WRITE) |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 301 | err = !access_ok(VERIFY_READ, (void __user *)arg, |
| 302 | _IOC_SIZE(cmd)); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 303 | if (err) { |
| 304 | dev_err(&pdx->udev->dev, "return with error = %d\n", err); |
| 305 | return -EFAULT; |
| 306 | } |
| 307 | switch (cmd) { |
| 308 | case PIUSB_GETVNDCMD: |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 309 | if (__copy_from_user |
| 310 | (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct))) { |
J.R. Mauro | 6546f08 | 2009-03-28 00:10:35 -0400 | [diff] [blame] | 311 | dev_err(&pdx->udev->dev, "copy_from_user failed\n"); |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 312 | return -EFAULT; |
| 313 | } |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 314 | dbg("%s %x\n", "Get Vendor Command = ", ctrl.cmd); |
| 315 | retval = |
| 316 | usb_control_msg(pdx->udev, usb_rcvctrlpipe(pdx->udev, 0), |
| 317 | ctrl.cmd, USB_DIR_IN, 0, 0, &devRB, |
| 318 | ctrl.numbytes, HZ * 10); |
| 319 | if (ctrl.cmd == 0xF1) { |
| 320 | dbg("FW Version returned from HW = %ld.%ld", |
| 321 | (devRB >> 8), (devRB & 0xFF)); |
| 322 | } |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 323 | if (retval >= 0) |
| 324 | retval = (int)devRB; |
| 325 | return retval; |
| 326 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 327 | case PIUSB_SETVNDCMD: |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 328 | if (__copy_from_user |
| 329 | (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct))) { |
J.R. Mauro | 6546f08 | 2009-03-28 00:10:35 -0400 | [diff] [blame] | 330 | dev_err(&pdx->udev->dev, "copy_from_user failed\n"); |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 331 | return -EFAULT; |
| 332 | } |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 333 | /* dbg( "%s %x", "Set Vendor Command = ",ctrl.cmd ); */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 334 | controlData = ctrl.pData[0]; |
| 335 | controlData |= (ctrl.pData[1] << 8); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 336 | /* dbg( "%s %d", "Vendor Data =",controlData ); */ |
| 337 | retval = usb_control_msg(pdx->udev, |
| 338 | usb_sndctrlpipe(pdx->udev, 0), |
| 339 | ctrl.cmd, |
| 340 | (USB_DIR_OUT | USB_TYPE_VENDOR |
| 341 | /* | USB_RECIP_ENDPOINT */), |
| 342 | controlData, 0, |
| 343 | &dummyCtlBuf, ctrl.numbytes, HZ * 10); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 344 | return retval; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 345 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 346 | case PIUSB_ISHIGHSPEED: |
| 347 | return ((pdx->udev->speed == USB_SPEED_HIGH) ? 1 : 0); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 348 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 349 | case PIUSB_WRITEPIPE: |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 350 | if (__copy_from_user(&ctrl, (void __user *)arg, _IOC_SIZE(cmd))) { |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 351 | dev_err(&pdx->udev->dev, |
| 352 | "copy_from_user WRITE_DUMMY failed\n"); |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 353 | return -EFAULT; |
| 354 | } |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 355 | if (!access_ok(VERIFY_READ, ctrl.pData, ctrl.numbytes)) { |
| 356 | dbg("can't access pData"); |
| 357 | return 0; |
| 358 | } |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 359 | piusb_output(&ctrl, ctrl.pData /* uBuf */, ctrl.numbytes, pdx); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 360 | return ctrl.numbytes; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 361 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 362 | case PIUSB_USERBUFFER: |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 363 | if (__copy_from_user |
| 364 | (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct))) { |
J.R. Mauro | 6546f08 | 2009-03-28 00:10:35 -0400 | [diff] [blame] | 365 | dev_err(&pdx->udev->dev, "copy_from_user failed\n"); |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 366 | return -EFAULT; |
| 367 | } |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 368 | return MapUserBuffer((struct ioctl_struct *) &ctrl, pdx); |
| 369 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 370 | case PIUSB_UNMAP_USERBUFFER: |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 371 | retval = UnMapUserBuffer(pdx); |
| 372 | return retval; |
| 373 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 374 | case PIUSB_READPIPE: |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 375 | if (__copy_from_user(&ctrl, (void __user *)arg, |
| 376 | sizeof(struct ioctl_struct))) { |
J.R. Mauro | 6546f08 | 2009-03-28 00:10:35 -0400 | [diff] [blame] | 377 | dev_err(&pdx->udev->dev, "copy_from_user failed\n"); |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 378 | return -EFAULT; |
| 379 | } |
Richard Genoud | 14e8bcd | 2009-05-17 13:06:32 +0200 | [diff] [blame] | 380 | if (((0 == ctrl.endpoint) && (PIXIS_PID == pdx->iama)) || |
| 381 | (1 == ctrl.endpoint) || /* ST133IO */ |
| 382 | (4 == ctrl.endpoint)) /* PIXIS IO */ |
| 383 | return pixis_io(&ctrl, pdx, |
| 384 | (struct ioctl_struct *)arg); |
| 385 | else if ((0 == ctrl.endpoint) || /* ST133 Pixel Data */ |
| 386 | (2 == ctrl.endpoint) || /* PIXIS Ping */ |
| 387 | (3 == ctrl.endpoint)) /* PIXIS Pong */ |
Richard Genoud | c854b5e | 2009-05-17 13:06:31 +0200 | [diff] [blame] | 388 | return pixel_data(&ctrl, pdx); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 389 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 390 | break; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 391 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 392 | case PIUSB_WHATCAMERA: |
| 393 | return pdx->iama; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 394 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 395 | case PIUSB_SETFRAMESIZE: |
| 396 | dbg("PIUSB_SETFRAMESIZE"); |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 397 | if (__copy_from_user |
| 398 | (&ctrl, (void __user *)arg, sizeof(struct ioctl_struct))) { |
J.R. Mauro | 6546f08 | 2009-03-28 00:10:35 -0400 | [diff] [blame] | 399 | dev_err(&pdx->udev->dev, "copy_from_user failed\n"); |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 400 | return -EFAULT; |
| 401 | } |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 402 | pdx->frameSize = ctrl.numbytes; |
| 403 | pdx->num_frames = ctrl.numFrames; |
| 404 | if (!pdx->sgl) |
| 405 | pdx->sgl = |
| 406 | kmalloc(sizeof(struct scatterlist *) * |
| 407 | pdx->num_frames, GFP_KERNEL); |
| 408 | if (!pdx->sgEntries) |
| 409 | pdx->sgEntries = |
| 410 | kmalloc(sizeof(unsigned int) * pdx->num_frames, |
| 411 | GFP_KERNEL); |
| 412 | if (!pdx->PixelUrb) |
| 413 | pdx->PixelUrb = |
| 414 | kmalloc(sizeof(struct urb **) * pdx->num_frames, |
| 415 | GFP_KERNEL); |
| 416 | if (!pdx->maplist_numPagesMapped) |
| 417 | pdx->maplist_numPagesMapped = |
| 418 | vmalloc(sizeof(unsigned int) * pdx->num_frames); |
| 419 | if (!pdx->pendedPixelUrbs) |
| 420 | pdx->pendedPixelUrbs = |
| 421 | kmalloc(sizeof(char *) * pdx->num_frames, |
| 422 | GFP_KERNEL); |
| 423 | return 0; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 424 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 425 | default: |
| 426 | dbg("%s\n", "No IOCTL found"); |
| 427 | break; |
| 428 | |
| 429 | } |
| 430 | /* return that we did not understand this ioctl call */ |
| 431 | dbg("Returning -ENOTTY"); |
| 432 | return -ENOTTY; |
| 433 | } |
| 434 | |
| 435 | static void piusb_write_bulk_callback(struct urb *urb) |
| 436 | { |
| 437 | struct device_extension *pdx = urb->context; |
| 438 | int status = urb->status; |
| 439 | |
| 440 | /* sync/async unlink faults aren't errors */ |
| 441 | if (status && !(status == -ENOENT || status == -ECONNRESET)) |
| 442 | dev_dbg(&urb->dev->dev, |
| 443 | "%s - nonzero write bulk status received: %d", |
| 444 | __func__, status); |
| 445 | |
| 446 | pdx->pendingWrite = 0; |
Pete Zaitcev | aea0d43b | 2009-06-10 14:44:14 -0600 | [diff] [blame] | 447 | kfree(urb->transfer_buffer); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 450 | int piusb_output(struct ioctl_struct *io, unsigned char *uBuf, int len, |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 451 | struct device_extension *pdx) |
| 452 | { |
| 453 | struct urb *urb = NULL; |
| 454 | int err = 0; |
| 455 | unsigned char *kbuf = NULL; |
| 456 | |
| 457 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 458 | if (urb != NULL) { |
Pete Zaitcev | aea0d43b | 2009-06-10 14:44:14 -0600 | [diff] [blame] | 459 | kbuf = kmalloc(len, GFP_KERNEL); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 460 | if (!kbuf) { |
J.R. Mauro | 6546f08 | 2009-03-28 00:10:35 -0400 | [diff] [blame] | 461 | dev_err(&pdx->udev->dev, "buffer_alloc failed\n"); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 462 | return -ENOMEM; |
| 463 | } |
vibi sreenivasan | 7a80bfc | 2009-06-04 20:59:17 +0530 | [diff] [blame] | 464 | if(__copy_from_user(kbuf, uBuf, len)) { |
| 465 | dev_err(&pdx->udev->dev, "__copy_from_user failed\n"); |
| 466 | return -EFAULT; |
| 467 | } |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 468 | usb_fill_bulk_urb(urb, pdx->udev, pdx->hEP[io->endpoint], kbuf, |
| 469 | len, piusb_write_bulk_callback, pdx); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 470 | err = usb_submit_urb(urb, GFP_KERNEL); |
| 471 | if (err) { |
| 472 | dev_err(&pdx->udev->dev, |
| 473 | "WRITE ERROR:submit urb error = %d\n", err); |
| 474 | } |
| 475 | pdx->pendingWrite = 1; |
| 476 | usb_free_urb(urb); |
| 477 | } |
| 478 | return -EINPROGRESS; |
| 479 | } |
| 480 | |
| 481 | static int UnMapUserBuffer(struct device_extension *pdx) |
| 482 | { |
| 483 | int i = 0; |
| 484 | int k = 0; |
| 485 | unsigned int epAddr; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 486 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 487 | for (k = 0; k < pdx->num_frames; k++) { |
| 488 | dbg("Killing Urbs for Frame %d", k); |
| 489 | for (i = 0; i < pdx->sgEntries[k]; i++) { |
| 490 | usb_kill_urb(pdx->PixelUrb[k][i]); |
| 491 | usb_free_urb(pdx->PixelUrb[k][i]); |
| 492 | pdx->pendedPixelUrbs[k][i] = 0; |
| 493 | } |
| 494 | dbg("Urb error count = %d", errCnt); |
| 495 | errCnt = 0; |
| 496 | dbg("Urbs free'd and Killed for Frame %d", k); |
| 497 | } |
| 498 | |
| 499 | for (k = 0; k < pdx->num_frames; k++) { |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 500 | if (pdx->iama == PIXIS_PID) |
| 501 | /* which EP should we map this frame to ? */ |
| 502 | /* PONG, odd frames: hEP[3] */ |
| 503 | /* PING, even frames and zero hEP[2] */ |
| 504 | epAddr = (k % 2) ? pdx->hEP[3] : pdx->hEP[2]; |
| 505 | else |
| 506 | /* ST133 only has 1 endpoint for Pixel data transfer */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 507 | epAddr = pdx->hEP[0]; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 508 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 509 | usb_buffer_unmap_sg(pdx->udev, epAddr, pdx->sgl[k], |
| 510 | pdx->maplist_numPagesMapped[k]); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 511 | for (i = 0; i < pdx->maplist_numPagesMapped[k]; i++) |
vibi sreenivasan | 8d2db51 | 2009-06-04 20:56:45 +0530 | [diff] [blame] | 512 | page_cache_release(sg_page(&pdx->sgl[k][i])); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 513 | kfree(pdx->sgl[k]); |
| 514 | kfree(pdx->PixelUrb[k]); |
| 515 | kfree(pdx->pendedPixelUrbs[k]); |
| 516 | pdx->sgl[k] = NULL; |
| 517 | pdx->PixelUrb[k] = NULL; |
| 518 | pdx->pendedPixelUrbs[k] = NULL; |
| 519 | } |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 520 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 521 | kfree(pdx->sgEntries); |
| 522 | vfree(pdx->maplist_numPagesMapped); |
| 523 | pdx->sgEntries = NULL; |
| 524 | pdx->maplist_numPagesMapped = NULL; |
| 525 | kfree(pdx->sgl); |
| 526 | kfree(pdx->pendedPixelUrbs); |
| 527 | kfree(pdx->PixelUrb); |
| 528 | pdx->sgl = NULL; |
| 529 | pdx->pendedPixelUrbs = NULL; |
| 530 | pdx->PixelUrb = NULL; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 531 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static void piusb_readPIXEL_callback(struct urb *urb) |
| 536 | { |
| 537 | int i = 0; |
| 538 | struct device_extension *pdx = urb->context; |
| 539 | int status = urb->status; |
| 540 | |
| 541 | if (status && !(status == -ENOENT || status == -ECONNRESET)) { |
| 542 | dbg("%s - nonzero read bulk status received: %d", __func__, |
| 543 | status); |
| 544 | dbg("Error in read EP2 callback"); |
| 545 | dbg("FrameIndex = %d", pdx->frameIdx); |
| 546 | dbg("Bytes received before problem occurred = %d", |
| 547 | pdx->bulk_in_byte_trk); |
| 548 | dbg("Urb Idx = %d", pdx->urbIdx); |
| 549 | pdx->pendedPixelUrbs[pdx->frameIdx][pdx->urbIdx] = 0; |
| 550 | } else { |
| 551 | pdx->bulk_in_byte_trk += urb->actual_length; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 552 | i = usb_submit_urb(urb, GFP_ATOMIC); /* resubmit the URB */ |
| 553 | if (i) { |
| 554 | errCnt++; |
| 555 | if (i != lastErr) { |
| 556 | dbg("submit urb in callback failed " |
| 557 | "with error code %d", i); |
| 558 | lastErr = i; |
| 559 | } |
| 560 | } else { |
| 561 | pdx->urbIdx++; /* point to next URB when we callback */ |
| 562 | if (pdx->bulk_in_byte_trk >= pdx->frameSize) { |
| 563 | pdx->bulk_in_size_returned = |
| 564 | pdx->bulk_in_byte_trk; |
| 565 | pdx->bulk_in_byte_trk = 0; |
| 566 | pdx->gotPixelData = 1; |
| 567 | pdx->frameIdx = |
| 568 | ((pdx->frameIdx + |
| 569 | 1) % pdx->num_frames); |
| 570 | pdx->urbIdx = 0; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 571 | } |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | /* MapUserBuffer( |
| 577 | inputs: |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 578 | struct ioctl_struct *io - structure containing user address, |
| 579 | frame #, and size |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 580 | struct device_extension *pdx - the PIUSB device extension |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 581 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 582 | returns: |
| 583 | int - status of the task |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 584 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 585 | Notes: |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 586 | MapUserBuffer maps a buffer passed down through an ioctl. |
| 587 | The user buffer is Page Aligned by the app and then passed down. |
| 588 | The function get_free_pages(...) does the actual mapping of the buffer |
| 589 | from user space to kernel space. |
| 590 | From there a scatterlist is created from all the pages. |
| 591 | The next function called is to usb_buffer_map_sg which allocated |
| 592 | DMA addresses for each page, even coalescing them if possible. |
| 593 | The DMA address is placed in the scatterlist structure. |
| 594 | The function returns the number of DMA addresses. |
| 595 | This may or may not be equal to the number of pages that |
| 596 | the user buffer uses. |
| 597 | We then build an URB for each DMA address and then submit them. |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 598 | */ |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 599 | |
| 600 | /* |
| 601 | int MapUserBuffer(unsigned long uaddr, unsigned long numbytes, |
| 602 | unsigned long frameInfo, struct device_extension *pdx) |
| 603 | */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 604 | static int MapUserBuffer(struct ioctl_struct *io, struct device_extension *pdx) |
| 605 | { |
| 606 | unsigned long uaddr; |
| 607 | unsigned long numbytes; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 608 | int frameInfo; /* which frame we're mapping */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 609 | unsigned int epAddr = 0; |
| 610 | unsigned long count = 0; |
| 611 | int i = 0; |
| 612 | int k = 0; |
| 613 | int err = 0; |
| 614 | struct page **maplist_p; |
| 615 | int numPagesRequired; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 616 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 617 | frameInfo = io->numFrames; |
| 618 | uaddr = (unsigned long)io->pData; |
| 619 | numbytes = io->numbytes; |
| 620 | |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 621 | if (pdx->iama == PIXIS_PID) { |
| 622 | /* which EP should we map this frame to ? */ |
| 623 | /* PONG, odd frames: hEP[3] */ |
| 624 | /* PING, even frames and zero hEP[2] */ |
| 625 | epAddr = (frameInfo % 2) ? pdx->hEP[3] : pdx->hEP[2]; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 626 | dbg("Pixis Frame #%d: EP=%d", frameInfo, |
| 627 | (epAddr == pdx->hEP[2]) ? 2 : 4); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 628 | } else { /* ST133 only has 1 endpoint for Pixel data transfer */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 629 | epAddr = pdx->hEP[0]; |
| 630 | dbg("ST133 Frame #%d: EP=2", frameInfo); |
| 631 | } |
| 632 | count = numbytes; |
| 633 | dbg("UserAddress = 0x%08lX", uaddr); |
| 634 | dbg("numbytes = %d", (int)numbytes); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 635 | |
| 636 | /* number of pages to map the entire user space DMA buffer */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 637 | numPagesRequired = |
| 638 | ((uaddr & ~PAGE_MASK) + count + ~PAGE_MASK) >> PAGE_SHIFT; |
| 639 | dbg("Number of pages needed = %d", numPagesRequired); |
Pete Zaitcev | aea0d43b | 2009-06-10 14:44:14 -0600 | [diff] [blame] | 640 | maplist_p = vmalloc(numPagesRequired * sizeof(struct page *)); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 641 | if (!maplist_p) { |
| 642 | dbg("Can't Allocate Memory for maplist_p"); |
| 643 | return -ENOMEM; |
| 644 | } |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 645 | |
| 646 | /* map the user buffer to kernel memory */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 647 | down_write(¤t->mm->mmap_sem); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 648 | pdx->maplist_numPagesMapped[frameInfo] = get_user_pages(current, |
| 649 | current->mm, (uaddr & PAGE_MASK), numPagesRequired, |
| 650 | WRITE, 0 /* Don't Force*/, maplist_p, NULL); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 651 | up_write(¤t->mm->mmap_sem); |
| 652 | dbg("Number of pages mapped = %d", |
| 653 | pdx->maplist_numPagesMapped[frameInfo]); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 654 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 655 | for (i = 0; i < pdx->maplist_numPagesMapped[frameInfo]; i++) |
| 656 | flush_dcache_page(maplist_p[i]); |
| 657 | if (!pdx->maplist_numPagesMapped[frameInfo]) { |
| 658 | dbg("get_user_pages() failed"); |
| 659 | vfree(maplist_p); |
| 660 | return -ENOMEM; |
| 661 | } |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 662 | |
| 663 | /* need to create a scatterlist that spans each frame |
| 664 | * that can fit into the mapped buffer |
| 665 | */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 666 | pdx->sgl[frameInfo] = |
| 667 | kmalloc((pdx->maplist_numPagesMapped[frameInfo] * |
| 668 | sizeof(struct scatterlist)), GFP_ATOMIC); |
| 669 | if (!pdx->sgl[frameInfo]) { |
| 670 | vfree(maplist_p); |
| 671 | dbg("can't allocate mem for sgl"); |
| 672 | return -ENOMEM; |
| 673 | } |
vibi sreenivasan | 8d2db51 | 2009-06-04 20:56:45 +0530 | [diff] [blame] | 674 | sg_assign_page(&pdx->sgl[frameInfo][0], maplist_p[0]); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 675 | pdx->sgl[frameInfo][0].offset = uaddr & ~PAGE_MASK; |
| 676 | if (pdx->maplist_numPagesMapped[frameInfo] > 1) { |
| 677 | pdx->sgl[frameInfo][0].length = |
| 678 | PAGE_SIZE - pdx->sgl[frameInfo][0].offset; |
| 679 | count -= pdx->sgl[frameInfo][0].length; |
| 680 | for (k = 1; k < pdx->maplist_numPagesMapped[frameInfo]; k++) { |
| 681 | pdx->sgl[frameInfo][k].offset = 0; |
vibi sreenivasan | 8d2db51 | 2009-06-04 20:56:45 +0530 | [diff] [blame] | 682 | sg_assign_page(&pdx->sgl[frameInfo][k], maplist_p[k]); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 683 | pdx->sgl[frameInfo][k].length = |
| 684 | (count < PAGE_SIZE) ? count : PAGE_SIZE; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 685 | count -= PAGE_SIZE; /* example had PAGE_SIZE here */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 686 | } |
| 687 | } else { |
| 688 | pdx->sgl[frameInfo][0].length = count; |
| 689 | } |
| 690 | pdx->sgEntries[frameInfo] = |
| 691 | usb_buffer_map_sg(pdx->udev, epAddr, pdx->sgl[frameInfo], |
| 692 | pdx->maplist_numPagesMapped[frameInfo]); |
| 693 | dbg("number of sgEntries = %d", pdx->sgEntries[frameInfo]); |
| 694 | pdx->userBufMapped = 1; |
| 695 | vfree(maplist_p); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 696 | |
| 697 | /* Create and Send the URB's for each s/g entry */ |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 698 | pdx->PixelUrb[frameInfo] = |
| 699 | kmalloc(pdx->sgEntries[frameInfo] * sizeof(struct urb *), |
| 700 | GFP_KERNEL); |
| 701 | if (!pdx->PixelUrb[frameInfo]) { |
| 702 | dbg("Can't Allocate Memory for Urb"); |
| 703 | return -ENOMEM; |
| 704 | } |
| 705 | for (i = 0; i < pdx->sgEntries[frameInfo]; i++) { |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 706 | /* 0 iso packets because we're using BULK transfers */ |
| 707 | pdx->PixelUrb[frameInfo][i] = usb_alloc_urb(0, GFP_KERNEL); |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 708 | usb_fill_bulk_urb(pdx->PixelUrb[frameInfo][i], |
| 709 | pdx->udev, |
| 710 | epAddr, |
Pete Zaitcev | aea0d43b | 2009-06-10 14:44:14 -0600 | [diff] [blame] | 711 | NULL, // non-DMA HC? buy a better hardware |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 712 | sg_dma_len(&pdx->sgl[frameInfo][i]), |
| 713 | piusb_readPIXEL_callback, (void *)pdx); |
| 714 | pdx->PixelUrb[frameInfo][i]->transfer_dma = |
| 715 | sg_dma_address(&pdx->sgl[frameInfo][i]); |
| 716 | pdx->PixelUrb[frameInfo][i]->transfer_flags = |
| 717 | URB_NO_TRANSFER_DMA_MAP | URB_NO_INTERRUPT; |
| 718 | } |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 719 | /* only interrupt when last URB completes */ |
| 720 | pdx->PixelUrb[frameInfo][--i]->transfer_flags &= ~URB_NO_INTERRUPT; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 721 | pdx->pendedPixelUrbs[frameInfo] = |
| 722 | kmalloc((pdx->sgEntries[frameInfo] * sizeof(char)), GFP_KERNEL); |
| 723 | if (!pdx->pendedPixelUrbs[frameInfo]) |
| 724 | dbg("Can't allocate Memory for pendedPixelUrbs"); |
| 725 | for (i = 0; i < pdx->sgEntries[frameInfo]; i++) { |
| 726 | err = usb_submit_urb(pdx->PixelUrb[frameInfo][i], GFP_ATOMIC); |
| 727 | if (err) { |
| 728 | dbg("%s %d\n", "submit urb error =", err); |
| 729 | pdx->pendedPixelUrbs[frameInfo][i] = 0; |
| 730 | return err; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 731 | } |
| 732 | pdx->pendedPixelUrbs[frameInfo][i] = 1; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 733 | } |
| 734 | return 0; |
| 735 | } |
| 736 | |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 737 | static const struct file_operations piusb_fops = { |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 738 | .owner = THIS_MODULE, |
| 739 | .ioctl = piusb_ioctl, |
| 740 | .open = piusb_open, |
| 741 | .release = piusb_release, |
| 742 | }; |
| 743 | |
| 744 | static struct usb_class_driver piusb_class = { |
| 745 | .name = "usb/rspiusb%d", |
| 746 | .fops = &piusb_fops, |
| 747 | .minor_base = PIUSB_MINOR_BASE, |
| 748 | }; |
| 749 | |
| 750 | /** |
| 751 | * piusb_probe |
| 752 | * |
| 753 | * Called by the usb core when a new device is connected that it thinks |
| 754 | * this driver might be interested in. |
| 755 | */ |
| 756 | static int piusb_probe(struct usb_interface *interface, |
| 757 | const struct usb_device_id *id) |
| 758 | { |
| 759 | struct device_extension *pdx = NULL; |
| 760 | struct usb_host_interface *iface_desc; |
| 761 | struct usb_endpoint_descriptor *endpoint; |
| 762 | int i; |
| 763 | int retval = -ENOMEM; |
| 764 | |
| 765 | dev_dbg(&interface->dev, "%s - Looking for PI USB Hardware", __func__); |
| 766 | |
| 767 | pdx = kzalloc(sizeof(struct device_extension), GFP_KERNEL); |
| 768 | if (pdx == NULL) { |
| 769 | dev_err(&interface->dev, "Out of memory\n"); |
| 770 | goto error; |
| 771 | } |
| 772 | kref_init(&pdx->kref); |
| 773 | pdx->udev = usb_get_dev(interface_to_usbdev(interface)); |
| 774 | pdx->interface = interface; |
| 775 | iface_desc = interface->cur_altsetting; |
| 776 | |
| 777 | /* See if the device offered us matches what we can accept */ |
| 778 | if ((pdx->udev->descriptor.idVendor != VENDOR_ID) |
| 779 | || ((pdx->udev->descriptor.idProduct != PIXIS_PID) |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 780 | && (pdx->udev->descriptor.idProduct != ST133_PID))) |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 781 | return -ENODEV; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 782 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 783 | pdx->iama = pdx->udev->descriptor.idProduct; |
| 784 | |
| 785 | if (debug) { |
| 786 | if (pdx->udev->descriptor.idProduct == PIXIS_PID) |
| 787 | dbg("PIUSB:Pixis Camera Found"); |
| 788 | else |
| 789 | dbg("PIUSB:ST133 USB Controller Found"); |
| 790 | if (pdx->udev->speed == USB_SPEED_HIGH) |
| 791 | dbg("Highspeed(USB2.0) Device Attached"); |
| 792 | else |
| 793 | dbg("Lowspeed (USB1.1) Device Attached"); |
| 794 | |
| 795 | dbg("NumEndpoints in Configuration: %d", |
| 796 | iface_desc->desc.bNumEndpoints); |
| 797 | } |
| 798 | for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) { |
| 799 | endpoint = &iface_desc->endpoint[i].desc; |
| 800 | if (debug) { |
| 801 | dbg("Endpoint[%d]->bDescriptorType = %d", i, |
| 802 | endpoint->bDescriptorType); |
| 803 | dbg("Endpoint[%d]->bEndpointAddress = 0x%02X", i, |
| 804 | endpoint->bEndpointAddress); |
| 805 | dbg("Endpoint[%d]->bbmAttributes = %d", i, |
| 806 | endpoint->bmAttributes); |
| 807 | dbg("Endpoint[%d]->MaxPacketSize = %d\n", i, |
| 808 | endpoint->wMaxPacketSize); |
| 809 | } |
Julia Lawall | c62d843 | 2008-12-29 11:21:52 +0100 | [diff] [blame] | 810 | if (usb_endpoint_xfer_bulk(endpoint)) { |
| 811 | if (usb_endpoint_dir_in(endpoint)) |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 812 | pdx->hEP[i] = |
| 813 | usb_rcvbulkpipe(pdx->udev, |
| 814 | endpoint->bEndpointAddress); |
| 815 | else |
| 816 | pdx->hEP[i] = |
| 817 | usb_sndbulkpipe(pdx->udev, |
| 818 | endpoint->bEndpointAddress); |
| 819 | } |
| 820 | } |
| 821 | usb_set_intfdata(interface, pdx); |
| 822 | retval = usb_register_dev(interface, &piusb_class); |
| 823 | if (retval) { |
| 824 | err("Not able to get a minor for this device."); |
| 825 | usb_set_intfdata(interface, NULL); |
| 826 | goto error; |
| 827 | } |
| 828 | pdx->present = 1; |
| 829 | |
| 830 | /* we can register the device now, as it is ready */ |
| 831 | pdx->minor = interface->minor; |
| 832 | /* let the user know what node this device is now attached to */ |
| 833 | dbg("PI USB2.0 device now attached to piusb-%d", pdx->minor); |
| 834 | return 0; |
| 835 | |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 836 | error: |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 837 | if (pdx) |
| 838 | kref_put(&pdx->kref, piusb_delete); |
| 839 | return retval; |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * piusb_disconnect |
| 844 | * |
| 845 | * Called by the usb core when the device is removed from the system. |
| 846 | * |
| 847 | * This routine guarantees that the driver will not submit any more urbs |
| 848 | * by clearing pdx->udev. It is also supposed to terminate any currently |
| 849 | * active urbs. Unfortunately, usb_bulk_msg(), used in piusb_read(), does |
| 850 | * not provide any way to do this. But at least we can cancel an active |
| 851 | * write. |
| 852 | */ |
| 853 | static void piusb_disconnect(struct usb_interface *interface) |
| 854 | { |
| 855 | struct device_extension *pdx; |
| 856 | int minor = interface->minor; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 857 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 858 | lock_kernel(); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 859 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 860 | pdx = usb_get_intfdata(interface); |
| 861 | usb_set_intfdata(interface, NULL); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 862 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 863 | /* give back our minor */ |
| 864 | usb_deregister_dev(interface, &piusb_class); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 865 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 866 | unlock_kernel(); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 867 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 868 | /* prevent device read, write and ioctl */ |
| 869 | pdx->present = 0; |
| 870 | kref_put(&pdx->kref, piusb_delete); |
| 871 | dbg("PI USB2.0 device #%d now disconnected\n", minor); |
| 872 | } |
| 873 | |
| 874 | static struct usb_driver piusb_driver = { |
| 875 | .name = "sub", |
| 876 | .probe = piusb_probe, |
| 877 | .disconnect = piusb_disconnect, |
| 878 | .id_table = pi_device_table, |
| 879 | }; |
| 880 | |
| 881 | /** |
| 882 | * piusb_init |
| 883 | */ |
| 884 | static int __init piusb_init(void) |
| 885 | { |
| 886 | int result; |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 887 | |
| 888 | lastErr = 0; |
| 889 | errCnt = 0; |
| 890 | |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 891 | /* register this driver with the USB subsystem */ |
| 892 | result = usb_register(&piusb_driver); |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 893 | if (result) |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 894 | printk(KERN_ERR KBUILD_MODNAME |
Richard Genoud | f2d46e2 | 2009-05-17 13:06:30 +0200 | [diff] [blame] | 895 | ": usb_register failed. Error number %d\n", |
| 896 | result); |
| 897 | else |
| 898 | printk(KERN_INFO KBUILD_MODNAME ":%s: %s\n", DRIVER_DESC, |
| 899 | DRIVER_VERSION); |
| 900 | return result; |
Greg Kroah-Hartman | 28397ff | 2008-08-21 14:04:55 -0700 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | /** |
| 904 | * piusb_exit |
| 905 | */ |
| 906 | static void __exit piusb_exit(void) |
| 907 | { |
| 908 | /* deregister this driver with the USB subsystem */ |
| 909 | usb_deregister(&piusb_driver); |
| 910 | } |
| 911 | |
| 912 | module_init(piusb_init); |
| 913 | module_exit(piusb_exit); |
| 914 | |
| 915 | /* Module parameters */ |
| 916 | module_param(debug, int, 0); |
| 917 | MODULE_PARM_DESC(debug, "Debug enabled or not"); |
| 918 | |
| 919 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 920 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 921 | MODULE_LICENSE("GPL v2"); |