Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Main USB camera driver |
| 3 | * |
| 4 | * V4L2 by Jean-Francois Moine <http://moinejf.free.fr> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2 of the License, or (at your |
| 9 | * option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software Foundation, |
| 18 | * Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
| 21 | #define MODULE_NAME "gspca" |
| 22 | |
| 23 | #include <linux/init.h> |
| 24 | #include <linux/fs.h> |
| 25 | #include <linux/vmalloc.h> |
| 26 | #include <linux/sched.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/mm.h> |
| 29 | #include <linux/string.h> |
| 30 | #include <linux/pagemap.h> |
| 31 | #include <asm/io.h> |
| 32 | #include <asm/page.h> |
| 33 | #include <asm/uaccess.h> |
| 34 | #include <linux/jiffies.h> |
| 35 | |
| 36 | #include "gspca.h" |
| 37 | |
| 38 | MODULE_AUTHOR("Jean-Francois Moine <http://moinejf.free.fr>"); |
| 39 | MODULE_DESCRIPTION("GSPCA USB Camera Driver"); |
| 40 | MODULE_LICENSE("GPL"); |
| 41 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 42 | #define DRIVER_VERSION_NUMBER KERNEL_VERSION(0, 1, 1) |
| 43 | static const char version[] = "0.1.1"; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 44 | |
| 45 | static int video_nr = -1; |
| 46 | |
| 47 | static int comp_fac = 30; /* Buffer size ratio when compressed in % */ |
| 48 | |
| 49 | #ifdef GSPCA_DEBUG |
| 50 | int gspca_debug = D_ERR | D_PROBE; |
| 51 | EXPORT_SYMBOL(gspca_debug); |
| 52 | |
| 53 | static void PDEBUG_MODE(char *txt, __u32 pixfmt, int w, int h) |
| 54 | { |
| 55 | if ((pixfmt >> 24) >= '0' && (pixfmt >> 24) <= 'z') { |
| 56 | PDEBUG(D_CONF|D_STREAM, "%s %c%c%c%c %dx%d", |
| 57 | txt, |
| 58 | pixfmt & 0xff, |
| 59 | (pixfmt >> 8) & 0xff, |
| 60 | (pixfmt >> 16) & 0xff, |
| 61 | pixfmt >> 24, |
| 62 | w, h); |
| 63 | } else { |
| 64 | PDEBUG(D_CONF|D_STREAM, "%s 0x%08x %dx%d", |
| 65 | txt, |
| 66 | pixfmt, |
| 67 | w, h); |
| 68 | } |
| 69 | } |
| 70 | #else |
| 71 | #define PDEBUG_MODE(txt, pixfmt, w, h) |
| 72 | #endif |
| 73 | |
| 74 | /* |
| 75 | * VMA operations. |
| 76 | */ |
| 77 | static void gspca_vm_open(struct vm_area_struct *vma) |
| 78 | { |
| 79 | struct gspca_frame *frame = vma->vm_private_data; |
| 80 | |
| 81 | frame->vma_use_count++; |
| 82 | frame->v4l2_buf.flags |= V4L2_BUF_FLAG_MAPPED; |
| 83 | } |
| 84 | |
| 85 | static void gspca_vm_close(struct vm_area_struct *vma) |
| 86 | { |
| 87 | struct gspca_frame *frame = vma->vm_private_data; |
| 88 | |
| 89 | if (--frame->vma_use_count <= 0) |
| 90 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_MAPPED; |
| 91 | } |
| 92 | |
| 93 | static struct vm_operations_struct gspca_vm_ops = { |
| 94 | .open = gspca_vm_open, |
| 95 | .close = gspca_vm_close, |
| 96 | }; |
| 97 | |
| 98 | /* |
| 99 | * ISOC message interrupt from the USB device |
| 100 | * |
| 101 | * Analyse each packet and call the subdriver for doing the copy |
| 102 | * to the frame buffer. |
| 103 | */ |
| 104 | static void isoc_irq(struct urb *urb) |
| 105 | { |
| 106 | struct gspca_dev *gspca_dev = (struct gspca_dev *) urb->context; |
| 107 | struct gspca_frame *frame; |
| 108 | unsigned char *data; /* address of data in the iso message */ |
| 109 | int i, j, len, st; |
| 110 | cam_pkt_op pkt_scan; |
| 111 | |
| 112 | PDEBUG(D_PACK, "isoc irq"); |
| 113 | if (!gspca_dev->streaming) |
| 114 | return; |
| 115 | pkt_scan = gspca_dev->sd_desc->pkt_scan; |
| 116 | for (i = 0; i < urb->number_of_packets; i++) { |
| 117 | |
| 118 | /* check the availability of the frame buffer */ |
| 119 | j = gspca_dev->fr_i; |
| 120 | j = gspca_dev->fr_queue[j]; |
| 121 | frame = &gspca_dev->frame[j]; |
| 122 | if ((frame->v4l2_buf.flags |
| 123 | & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)) |
| 124 | != V4L2_BUF_FLAG_QUEUED) { |
| 125 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | /* check the packet status and length */ |
| 130 | len = urb->iso_frame_desc[i].actual_length; |
| 131 | st = urb->iso_frame_desc[i].status; |
| 132 | if (st) { |
| 133 | PDEBUG(D_ERR, "ISOC data error: [%d] len=%d, status=%d", |
| 134 | i, len, st); |
| 135 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 136 | continue; |
| 137 | } |
| 138 | if (len == 0) |
| 139 | continue; |
| 140 | |
| 141 | /* let the packet be analyzed by the subdriver */ |
| 142 | PDEBUG(D_PACK, "packet [%d] o:%d l:%d", |
| 143 | i, urb->iso_frame_desc[i].offset, len); |
| 144 | data = (unsigned char *) urb->transfer_buffer |
| 145 | + urb->iso_frame_desc[i].offset; |
| 146 | pkt_scan(gspca_dev, frame, data, len); |
| 147 | } |
| 148 | |
| 149 | /* resubmit the URB */ |
| 150 | urb->status = 0; |
| 151 | st = usb_submit_urb(urb, GFP_ATOMIC); |
| 152 | if (st < 0) |
| 153 | PDEBUG(D_ERR|D_PACK, "usb_submit_urb() ret %d", st); |
| 154 | } |
| 155 | |
| 156 | /* |
| 157 | * add data to the current frame |
| 158 | * |
| 159 | * This function is called by the subdrivers at interrupt level. |
| 160 | * To build a frame, these ones must add |
| 161 | * - one FIRST_PACKET |
| 162 | * - 0 or many INTER_PACKETs |
| 163 | * - one LAST_PACKET |
| 164 | * DISCARD_PACKET invalidates the whole frame. |
| 165 | * On LAST_PACKET, a new frame is returned. |
| 166 | */ |
| 167 | struct gspca_frame *gspca_frame_add(struct gspca_dev *gspca_dev, |
| 168 | int packet_type, |
| 169 | struct gspca_frame *frame, |
| 170 | unsigned char *data, |
| 171 | int len) |
| 172 | { |
| 173 | int i, j; |
| 174 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 175 | PDEBUG(D_PACK, "add t:%d l:%d", packet_type, len); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 176 | |
| 177 | /* when start of a new frame, if the current frame buffer |
| 178 | * is not queued, discard the whole frame */ |
| 179 | if (packet_type == FIRST_PACKET) { |
| 180 | if ((frame->v4l2_buf.flags |
| 181 | & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)) |
| 182 | != V4L2_BUF_FLAG_QUEUED) { |
| 183 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 184 | return frame; |
| 185 | } |
| 186 | frame->data_end = frame->data; |
| 187 | jiffies_to_timeval(get_jiffies_64(), |
| 188 | &frame->v4l2_buf.timestamp); |
| 189 | frame->v4l2_buf.sequence = ++gspca_dev->sequence; |
| 190 | } else if (gspca_dev->last_packet_type == DISCARD_PACKET) |
| 191 | return frame; |
| 192 | |
| 193 | /* append the packet in the frame buffer */ |
| 194 | if (len > 0) { |
| 195 | if (frame->data_end - frame->data + len |
| 196 | > frame->v4l2_buf.length) { |
| 197 | PDEBUG(D_ERR|D_PACK, "frame overflow %d > %d", |
| 198 | frame->data_end - frame->data + len, |
| 199 | frame->v4l2_buf.length); |
| 200 | packet_type = DISCARD_PACKET; |
| 201 | } else { |
| 202 | if (frame->v4l2_buf.memory != V4L2_MEMORY_USERPTR) |
| 203 | memcpy(frame->data_end, data, len); |
| 204 | else |
| 205 | copy_to_user(frame->data_end, data, len); |
| 206 | frame->data_end += len; |
| 207 | } |
| 208 | } |
| 209 | gspca_dev->last_packet_type = packet_type; |
| 210 | |
| 211 | /* if last packet, wake the application and advance in the queue */ |
| 212 | if (packet_type == LAST_PACKET) { |
| 213 | frame->v4l2_buf.bytesused = frame->data_end - frame->data; |
| 214 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_QUEUED; |
| 215 | frame->v4l2_buf.flags |= V4L2_BUF_FLAG_DONE; |
| 216 | atomic_inc(&gspca_dev->nevent); |
| 217 | wake_up_interruptible(&gspca_dev->wq); /* event = new frame */ |
| 218 | i = gspca_dev->fr_i; |
| 219 | i = (i + 1) % gspca_dev->nframes; |
| 220 | PDEBUG(D_FRAM, "frame complete len:%d q:%d i:%d o:%d", |
| 221 | frame->v4l2_buf.bytesused, |
| 222 | gspca_dev->fr_q, |
| 223 | i, |
| 224 | gspca_dev->fr_o); |
| 225 | j = gspca_dev->fr_queue[i]; |
| 226 | frame = &gspca_dev->frame[j]; |
| 227 | gspca_dev->fr_i = i; |
| 228 | } |
| 229 | return frame; |
| 230 | } |
| 231 | EXPORT_SYMBOL(gspca_frame_add); |
| 232 | |
| 233 | static int gspca_is_compressed(__u32 format) |
| 234 | { |
| 235 | switch (format) { |
| 236 | case V4L2_PIX_FMT_MJPEG: |
| 237 | case V4L2_PIX_FMT_JPEG: |
| 238 | return 1; |
| 239 | } |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | static void *rvmalloc(unsigned long size) |
| 244 | { |
| 245 | void *mem; |
| 246 | unsigned long adr; |
| 247 | |
| 248 | size = PAGE_ALIGN(size); |
| 249 | mem = vmalloc_32(size); |
| 250 | if (mem != 0) { |
| 251 | memset(mem, 0, size); |
| 252 | adr = (unsigned long) mem; |
| 253 | while ((long) size > 0) { |
| 254 | SetPageReserved(vmalloc_to_page((void *) adr)); |
| 255 | adr += PAGE_SIZE; |
| 256 | size -= PAGE_SIZE; |
| 257 | } |
| 258 | } |
| 259 | return mem; |
| 260 | } |
| 261 | |
| 262 | static void rvfree(void *mem, unsigned long size) |
| 263 | { |
| 264 | unsigned long adr; |
| 265 | |
| 266 | if (!mem) |
| 267 | return; |
| 268 | adr = (unsigned long) mem; |
| 269 | while ((long) size > 0) { |
| 270 | ClearPageReserved(vmalloc_to_page((void *) adr)); |
| 271 | adr += PAGE_SIZE; |
| 272 | size -= PAGE_SIZE; |
| 273 | } |
| 274 | vfree(mem); |
| 275 | } |
| 276 | |
| 277 | static int frame_alloc(struct gspca_dev *gspca_dev, |
| 278 | unsigned int count, |
| 279 | unsigned int frsz, |
| 280 | enum v4l2_memory memory) |
| 281 | { |
| 282 | int i, ret = 0; |
| 283 | |
| 284 | PDEBUG(D_STREAM, "frame alloc frsz: %d", frsz); |
| 285 | if (gspca_dev->nframes != 0) { |
| 286 | PDEBUG(D_ERR|D_STREAM, "alloc frame already done"); |
| 287 | return -EBUSY; |
| 288 | } |
| 289 | if (count > GSPCA_MAX_FRAMES) |
| 290 | count = GSPCA_MAX_FRAMES; |
| 291 | /* if compressed, reduce the buffer size */ |
| 292 | if (gspca_is_compressed(gspca_dev->pixfmt)) |
| 293 | frsz = (frsz * comp_fac) / 100; |
| 294 | frsz = PAGE_ALIGN(frsz); |
| 295 | PDEBUG(D_STREAM, "new fr_sz: %d", frsz); |
| 296 | gspca_dev->frsz = frsz; |
| 297 | if (memory == V4L2_MEMORY_MMAP) { |
| 298 | gspca_dev->frbuf = rvmalloc(frsz * count); |
| 299 | if (!gspca_dev->frbuf) { |
| 300 | err("frame alloc failed"); |
| 301 | return -ENOMEM; |
| 302 | } |
| 303 | } |
| 304 | gspca_dev->nframes = count; |
| 305 | for (i = 0; i < count; i++) { |
| 306 | gspca_dev->frame[i].v4l2_buf.index = i; |
| 307 | gspca_dev->frame[i].v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 308 | gspca_dev->frame[i].v4l2_buf.flags = 0; |
| 309 | gspca_dev->frame[i].v4l2_buf.field = V4L2_FIELD_NONE; |
| 310 | gspca_dev->frame[i].v4l2_buf.length = frsz; |
| 311 | gspca_dev->frame[i].v4l2_buf.memory = memory; |
| 312 | if (memory == V4L2_MEMORY_MMAP) { |
| 313 | gspca_dev->frame[i].data |
| 314 | = gspca_dev->frame[i].data_end |
| 315 | = gspca_dev->frbuf + i * frsz; |
| 316 | gspca_dev->frame[i].v4l2_buf.m.offset = i * frsz; |
| 317 | } |
| 318 | gspca_dev->frame[i].v4l2_buf.flags = 0; /* buf in app space */ |
| 319 | } |
| 320 | gspca_dev->fr_i = gspca_dev->fr_o = gspca_dev->fr_q = 0; |
| 321 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 322 | gspca_dev->sequence = 0; |
| 323 | atomic_set(&gspca_dev->nevent, 0); |
| 324 | return ret; |
| 325 | } |
| 326 | |
| 327 | static void frame_free(struct gspca_dev *gspca_dev) |
| 328 | { |
| 329 | int i; |
| 330 | |
| 331 | PDEBUG(D_STREAM, "frame free"); |
| 332 | if (gspca_dev->frbuf != 0) { |
| 333 | rvfree(gspca_dev->frbuf, |
| 334 | gspca_dev->nframes * gspca_dev->frsz); |
| 335 | gspca_dev->frbuf = NULL; |
| 336 | for (i = 0; i < gspca_dev->nframes; i++) |
| 337 | gspca_dev->frame[i].data = NULL; |
| 338 | } |
| 339 | gspca_dev->nframes = 0; |
| 340 | } |
| 341 | |
| 342 | static int gspca_kill_transfer(struct gspca_dev *gspca_dev) |
| 343 | { |
| 344 | struct urb *urb; |
| 345 | unsigned int i; |
| 346 | |
| 347 | PDEBUG(D_STREAM, "kill transfer"); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 348 | for (i = 0; i < NURBS; ++i) { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 349 | urb = gspca_dev->urb[i]; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 350 | if (urb == NULL) |
| 351 | continue; |
| 352 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 353 | gspca_dev->urb[i] = NULL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 354 | usb_kill_urb(urb); |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 355 | if (urb->transfer_buffer != 0) |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 356 | usb_buffer_free(gspca_dev->dev, |
| 357 | urb->transfer_buffer_length, |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 358 | urb->transfer_buffer, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 359 | urb->transfer_dma); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 360 | usb_free_urb(urb); |
| 361 | } |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * search an input isochronous endpoint in an alternate setting |
| 367 | */ |
| 368 | static struct usb_host_endpoint *alt_isoc(struct usb_host_interface *alt, |
| 369 | __u8 epaddr) |
| 370 | { |
| 371 | struct usb_host_endpoint *ep; |
| 372 | int i, attr; |
| 373 | |
| 374 | epaddr |= USB_DIR_IN; |
| 375 | for (i = 0; i < alt->desc.bNumEndpoints; i++) { |
| 376 | ep = &alt->endpoint[i]; |
| 377 | if (ep->desc.bEndpointAddress == epaddr) { |
| 378 | attr = ep->desc.bmAttributes |
| 379 | & USB_ENDPOINT_XFERTYPE_MASK; |
| 380 | if (attr == USB_ENDPOINT_XFER_ISOC) |
| 381 | return ep; |
| 382 | break; |
| 383 | } |
| 384 | } |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | /* |
| 389 | * search an input isochronous endpoint |
| 390 | * |
| 391 | * The endpoint is defined by the subdriver. |
| 392 | * Use only the first isoc (some Zoran - 0x0572:0x0001 - have two such ep). |
| 393 | * This routine may be called many times when the bandwidth is too small |
| 394 | * (the bandwidth is checked on urb submit). |
| 395 | */ |
| 396 | struct usb_host_endpoint *get_isoc_ep(struct gspca_dev *gspca_dev) |
| 397 | { |
| 398 | struct usb_interface *intf; |
| 399 | struct usb_host_endpoint *ep; |
| 400 | int i, ret; |
| 401 | |
| 402 | intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); |
| 403 | i = gspca_dev->alt; /* previous alt setting */ |
| 404 | while (--i > 0) { /* alt 0 is unusable */ |
| 405 | ep = alt_isoc(&intf->altsetting[i], gspca_dev->cam.epaddr); |
| 406 | if (ep) |
| 407 | break; |
| 408 | } |
| 409 | if (i <= 0) { |
| 410 | err("no ISOC endpoint found"); |
| 411 | return NULL; |
| 412 | } |
| 413 | PDEBUG(D_STREAM, "use ISOC alt %d ep 0x%02x", |
| 414 | i, ep->desc.bEndpointAddress); |
| 415 | ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, i); |
| 416 | if (ret < 0) { |
| 417 | err("set interface err %d", ret); |
| 418 | return NULL; |
| 419 | } |
| 420 | gspca_dev->alt = i; |
| 421 | return ep; |
| 422 | } |
| 423 | |
| 424 | /* |
| 425 | * create the isochronous URBs |
| 426 | */ |
| 427 | static int create_urbs(struct gspca_dev *gspca_dev, |
| 428 | struct usb_host_endpoint *ep) |
| 429 | { |
| 430 | struct urb *urb; |
| 431 | int n, i, psize, npkt, bsize; |
| 432 | |
| 433 | /* calculate the packet size and the number of packets */ |
| 434 | /* the URB buffer size must be a power of 2 */ |
| 435 | psize = le16_to_cpu(ep->desc.wMaxPacketSize); |
| 436 | /* See paragraph 5.9 / table 5-11 of the usb 2.0 spec. */ |
| 437 | psize = (psize & 0x07ff) * (1 + ((psize >> 11) & 3)); |
| 438 | npkt = ISO_MAX_SIZE / psize; |
| 439 | if (npkt > ISO_MAX_PKT) |
| 440 | npkt = ISO_MAX_PKT; |
| 441 | bsize = psize * npkt; |
| 442 | for (n = ISO_MAX_SIZE; n > 0; n >>= 1) { |
| 443 | if (n & bsize) /* !! assume ISO_MAX_SIZE is a power of 2 */ |
| 444 | break; |
| 445 | } |
| 446 | if (n != 0) { |
| 447 | npkt = n / psize; |
| 448 | bsize = psize * npkt; |
| 449 | } |
| 450 | PDEBUG(D_STREAM, |
| 451 | "isoc %d pkts size %d (bsize:%d)", npkt, psize, bsize); |
| 452 | for (n = 0; n < NURBS; n++) { |
| 453 | urb = usb_alloc_urb(npkt, GFP_KERNEL); |
| 454 | if (!urb) { |
| 455 | err("usb_alloc_urb failed"); |
| 456 | return -ENOMEM; |
| 457 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 458 | urb->transfer_buffer = usb_buffer_alloc(gspca_dev->dev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 459 | bsize, |
| 460 | GFP_KERNEL, |
| 461 | &urb->transfer_dma); |
| 462 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 463 | if (urb->transfer_buffer == NULL) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 464 | usb_free_urb(urb); |
| 465 | gspca_kill_transfer(gspca_dev); |
| 466 | err("usb_buffer_urb failed"); |
| 467 | return -ENOMEM; |
| 468 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 469 | gspca_dev->urb[n] = urb; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 470 | urb->dev = gspca_dev->dev; |
| 471 | urb->context = gspca_dev; |
| 472 | urb->pipe = usb_rcvisocpipe(gspca_dev->dev, |
| 473 | ep->desc.bEndpointAddress); |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 474 | urb->transfer_flags = URB_ISO_ASAP |
| 475 | | URB_NO_TRANSFER_DMA_MAP; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 476 | urb->interval = ep->desc.bInterval; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 477 | urb->complete = isoc_irq; |
| 478 | urb->number_of_packets = npkt; |
| 479 | urb->transfer_buffer_length = bsize; |
| 480 | for (i = 0; i < npkt; i++) { |
| 481 | urb->iso_frame_desc[i].length = psize; |
| 482 | urb->iso_frame_desc[i].offset = psize * i; |
| 483 | } |
| 484 | } |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /* |
| 489 | * start the USB transfer |
| 490 | */ |
| 491 | static int gspca_init_transfer(struct gspca_dev *gspca_dev) |
| 492 | { |
| 493 | struct usb_interface *intf; |
| 494 | struct usb_host_endpoint *ep; |
| 495 | int n, ret; |
| 496 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 497 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 498 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 499 | |
| 500 | /* set the max alternate setting and loop until urb submit succeeds */ |
| 501 | intf = usb_ifnum_to_if(gspca_dev->dev, gspca_dev->iface); |
| 502 | gspca_dev->alt = intf->num_altsetting; |
| 503 | for (;;) { |
| 504 | PDEBUG(D_STREAM, "init transfer nbalt %d", gspca_dev->alt); |
| 505 | ep = get_isoc_ep(gspca_dev); |
| 506 | if (ep == NULL) { |
| 507 | ret = -EIO; |
| 508 | goto out; |
| 509 | } |
| 510 | ret = create_urbs(gspca_dev, ep); |
| 511 | if (ret < 0) |
| 512 | goto out; |
| 513 | |
| 514 | /* start the cam */ |
| 515 | gspca_dev->sd_desc->start(gspca_dev); |
| 516 | gspca_dev->streaming = 1; |
| 517 | atomic_set(&gspca_dev->nevent, 0); |
| 518 | |
| 519 | /* submit the URBs */ |
| 520 | for (n = 0; n < NURBS; n++) { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 521 | ret = usb_submit_urb(gspca_dev->urb[n], GFP_KERNEL); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 522 | if (ret < 0) { |
| 523 | PDEBUG(D_ERR|D_STREAM, |
| 524 | "usb_submit_urb [%d] err %d", n, ret); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 525 | gspca_dev->streaming = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 526 | gspca_kill_transfer(gspca_dev); |
| 527 | if (ret == -ENOSPC) |
| 528 | break; /* try the previous alt */ |
| 529 | goto out; |
| 530 | } |
| 531 | } |
| 532 | if (ret >= 0) |
| 533 | break; |
| 534 | } |
| 535 | out: |
| 536 | mutex_unlock(&gspca_dev->usb_lock); |
| 537 | return ret; |
| 538 | } |
| 539 | |
| 540 | static int gspca_set_alt0(struct gspca_dev *gspca_dev) |
| 541 | { |
| 542 | int ret; |
| 543 | |
| 544 | ret = usb_set_interface(gspca_dev->dev, gspca_dev->iface, 0); |
| 545 | if (ret < 0) |
| 546 | PDEBUG(D_ERR|D_STREAM, "set interface 0 err %d", ret); |
| 547 | return ret; |
| 548 | } |
| 549 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 550 | /* Note both the queue and the usb lock should be hold when calling this */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 551 | static void gspca_stream_off(struct gspca_dev *gspca_dev) |
| 552 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 553 | gspca_dev->streaming = 0; |
| 554 | if (gspca_dev->present) { |
| 555 | gspca_dev->sd_desc->stopN(gspca_dev); |
| 556 | gspca_kill_transfer(gspca_dev); |
| 557 | gspca_set_alt0(gspca_dev); |
| 558 | gspca_dev->sd_desc->stop0(gspca_dev); |
| 559 | PDEBUG(D_STREAM, "stream off OK"); |
| 560 | } else { |
| 561 | gspca_kill_transfer(gspca_dev); |
| 562 | atomic_inc(&gspca_dev->nevent); |
| 563 | wake_up_interruptible(&gspca_dev->wq); |
| 564 | PDEBUG(D_ERR|D_STREAM, "stream off no device ??"); |
| 565 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | static int gspca_set_default_mode(struct gspca_dev *gspca_dev) |
| 569 | { |
| 570 | int i; |
| 571 | |
| 572 | i = gspca_dev->cam.nmodes - 1; /* take the highest mode */ |
| 573 | gspca_dev->curr_mode = i; |
| 574 | gspca_dev->width = gspca_dev->cam.cam_mode[i].width; |
| 575 | gspca_dev->height = gspca_dev->cam.cam_mode[i].height; |
| 576 | gspca_dev->pixfmt = gspca_dev->cam.cam_mode[i].pixfmt; |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static int wxh_to_mode(struct gspca_dev *gspca_dev, |
| 581 | int width, int height) |
| 582 | { |
| 583 | int i; |
| 584 | |
| 585 | for (i = gspca_dev->cam.nmodes - 1; --i >= 0; ) { |
| 586 | if (width > gspca_dev->cam.cam_mode[i].width) |
| 587 | break; |
| 588 | } |
| 589 | i++; |
| 590 | while (i < gspca_dev->cam.nmodes - 1 |
| 591 | && width == gspca_dev->cam.cam_mode[i + 1].width |
| 592 | && height < gspca_dev->cam.cam_mode[i + 1].height) |
| 593 | i++; |
| 594 | return i; |
| 595 | } |
| 596 | |
| 597 | static __u32 get_v4l2_depth(__u32 pixfmt) |
| 598 | { |
| 599 | switch (pixfmt) { |
| 600 | case V4L2_PIX_FMT_BGR32: |
| 601 | case V4L2_PIX_FMT_RGB32: |
| 602 | return 32; |
| 603 | case V4L2_PIX_FMT_RGB24: |
| 604 | case V4L2_PIX_FMT_BGR24: |
| 605 | return 24; |
| 606 | case V4L2_PIX_FMT_RGB565: |
| 607 | case V4L2_PIX_FMT_YUYV: /* packed 4.2.2 */ |
| 608 | case V4L2_PIX_FMT_YYUV: |
| 609 | return 16; |
| 610 | case V4L2_PIX_FMT_YUV420: /* planar 4.2.0 */ |
| 611 | return 12; |
| 612 | case V4L2_PIX_FMT_MJPEG: |
| 613 | case V4L2_PIX_FMT_JPEG: |
| 614 | case V4L2_PIX_FMT_SBGGR8: /* Bayer */ |
| 615 | return 8; |
| 616 | } |
| 617 | PDEBUG(D_ERR|D_CONF, "Unknown pixel format %c%c%c%c", |
| 618 | pixfmt & 0xff, |
| 619 | (pixfmt >> 8) & 0xff, |
| 620 | (pixfmt >> 16) & 0xff, |
| 621 | pixfmt >> 24); |
| 622 | return -EINVAL; |
| 623 | } |
| 624 | |
| 625 | /* |
| 626 | * search a mode with the right pixel format |
| 627 | */ |
| 628 | static int gspca_get_mode(struct gspca_dev *gspca_dev, |
| 629 | int mode, |
| 630 | int pixfmt) |
| 631 | { |
| 632 | int modeU, modeD; |
| 633 | |
| 634 | modeU = modeD = mode; |
| 635 | while ((modeU < gspca_dev->cam.nmodes) || modeD >= 0) { |
| 636 | if (--modeD >= 0) { |
| 637 | if (gspca_dev->cam.cam_mode[modeD].pixfmt == pixfmt) |
| 638 | return modeD; |
| 639 | } |
| 640 | if (++modeU < gspca_dev->cam.nmodes) { |
| 641 | if (gspca_dev->cam.cam_mode[modeU].pixfmt == pixfmt) |
| 642 | return modeU; |
| 643 | } |
| 644 | } |
| 645 | return -EINVAL; |
| 646 | } |
| 647 | |
| 648 | static int vidioc_enum_fmt_cap(struct file *file, void *priv, |
| 649 | struct v4l2_fmtdesc *fmtdesc) |
| 650 | { |
| 651 | struct gspca_dev *gspca_dev = priv; |
| 652 | int i, j, index; |
| 653 | __u32 fmt_tb[8]; |
| 654 | |
| 655 | PDEBUG(D_CONF, "enum fmt cap"); |
| 656 | |
| 657 | /* give an index to each format */ |
| 658 | index = 0; |
| 659 | j = 0; |
| 660 | for (i = gspca_dev->cam.nmodes; --i >= 0; ) { |
| 661 | fmt_tb[index] = gspca_dev->cam.cam_mode[i].pixfmt; |
| 662 | j = 0; |
| 663 | for (;;) { |
| 664 | if (fmt_tb[j] == fmt_tb[index]) |
| 665 | break; |
| 666 | j++; |
| 667 | } |
| 668 | if (j == index) { |
| 669 | if (fmtdesc->index == index) |
| 670 | break; /* new format */ |
| 671 | index++; |
| 672 | if (index >= sizeof fmt_tb / sizeof fmt_tb[0]) |
| 673 | return -EINVAL; |
| 674 | } |
| 675 | } |
| 676 | if (i < 0) |
| 677 | return -EINVAL; /* no more format */ |
| 678 | |
| 679 | fmtdesc->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 680 | fmtdesc->pixelformat = fmt_tb[index]; |
| 681 | if (gspca_is_compressed(fmt_tb[index])) |
| 682 | fmtdesc->flags = V4L2_FMT_FLAG_COMPRESSED; |
| 683 | fmtdesc->description[0] = fmtdesc->pixelformat & 0xff; |
| 684 | fmtdesc->description[1] = (fmtdesc->pixelformat >> 8) & 0xff; |
| 685 | fmtdesc->description[2] = (fmtdesc->pixelformat >> 16) & 0xff; |
| 686 | fmtdesc->description[3] = fmtdesc->pixelformat >> 24; |
| 687 | fmtdesc->description[4] = '\0'; |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | static int gspca_get_buff_size(struct gspca_dev *gspca_dev) |
| 692 | { |
| 693 | unsigned int size; |
| 694 | |
| 695 | size = gspca_dev->width * gspca_dev->height |
| 696 | * get_v4l2_depth(gspca_dev->pixfmt) / 8; |
| 697 | if (!size) |
| 698 | return -ENOMEM; |
| 699 | return size; |
| 700 | } |
| 701 | |
| 702 | static int vidioc_g_fmt_cap(struct file *file, void *priv, |
| 703 | struct v4l2_format *fmt) |
| 704 | { |
| 705 | struct gspca_dev *gspca_dev = priv; |
| 706 | |
| 707 | fmt->fmt.pix.width = gspca_dev->width; |
| 708 | fmt->fmt.pix.height = gspca_dev->height; |
| 709 | fmt->fmt.pix.pixelformat = gspca_dev->pixfmt; |
| 710 | #ifdef GSPCA_DEBUG |
| 711 | if (gspca_debug & D_CONF) { |
| 712 | PDEBUG_MODE("get fmt cap", |
| 713 | fmt->fmt.pix.pixelformat, |
| 714 | fmt->fmt.pix.width, |
| 715 | fmt->fmt.pix.height); |
| 716 | } |
| 717 | #endif |
| 718 | fmt->fmt.pix.field = V4L2_FIELD_NONE; |
| 719 | fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat) |
| 720 | * fmt->fmt.pix.width / 8; |
| 721 | fmt->fmt.pix.sizeimage = fmt->fmt.pix.bytesperline |
| 722 | * fmt->fmt.pix.height; |
| 723 | /* (should be in the subdriver) */ |
| 724 | fmt->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB; |
| 725 | fmt->fmt.pix.priv = 0; |
| 726 | return 0; |
| 727 | } |
| 728 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 729 | static int try_fmt_cap(struct gspca_dev *gspca_dev, |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 730 | struct v4l2_format *fmt) |
| 731 | { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 732 | int w, h, mode, mode2, frsz; |
| 733 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 734 | w = fmt->fmt.pix.width; |
| 735 | h = fmt->fmt.pix.height; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 736 | #ifdef GSPCA_DEBUG |
| 737 | if (gspca_debug & D_CONF) |
| 738 | PDEBUG_MODE("try fmt cap", fmt->fmt.pix.pixelformat, w, h); |
| 739 | #endif |
| 740 | /* search the closest mode for width and height */ |
| 741 | mode = wxh_to_mode(gspca_dev, w, h); |
| 742 | |
| 743 | /* OK if right palette */ |
| 744 | if (gspca_dev->cam.cam_mode[mode].pixfmt != fmt->fmt.pix.pixelformat) { |
| 745 | |
| 746 | /* else, search the closest mode with the same pixel format */ |
| 747 | mode2 = gspca_get_mode(gspca_dev, mode, |
| 748 | fmt->fmt.pix.pixelformat); |
| 749 | if (mode2 >= 0) |
| 750 | mode = mode2; |
| 751 | else { |
| 752 | |
| 753 | /* no chance, return this mode */ |
| 754 | fmt->fmt.pix.pixelformat |
| 755 | = gspca_dev->cam.cam_mode[mode].pixfmt; |
| 756 | #ifdef GSPCA_DEBUG |
| 757 | if (gspca_debug & D_CONF) { |
| 758 | PDEBUG_MODE("new format", |
| 759 | fmt->fmt.pix.pixelformat, |
| 760 | gspca_dev->cam.cam_mode[mode].width, |
| 761 | gspca_dev->cam.cam_mode[mode].height); |
| 762 | } |
| 763 | #endif |
| 764 | } |
| 765 | } |
| 766 | fmt->fmt.pix.width = gspca_dev->cam.cam_mode[mode].width; |
| 767 | fmt->fmt.pix.height = gspca_dev->cam.cam_mode[mode].height; |
| 768 | fmt->fmt.pix.bytesperline = get_v4l2_depth(fmt->fmt.pix.pixelformat) |
| 769 | * fmt->fmt.pix.width / 8; |
| 770 | frsz = fmt->fmt.pix.bytesperline * fmt->fmt.pix.height; |
| 771 | if (gspca_is_compressed(fmt->fmt.pix.pixelformat)) |
| 772 | frsz = (frsz * comp_fac) / 100; |
| 773 | fmt->fmt.pix.sizeimage = frsz; |
| 774 | return mode; /* used when s_fmt */ |
| 775 | } |
| 776 | |
| 777 | static int vidioc_try_fmt_cap(struct file *file, |
| 778 | void *priv, |
| 779 | struct v4l2_format *fmt) |
| 780 | { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 781 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 782 | int ret; |
| 783 | |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 784 | ret = try_fmt_cap(gspca_dev, fmt); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 785 | if (ret < 0) |
| 786 | return ret; |
| 787 | return 0; |
| 788 | } |
| 789 | |
| 790 | static int vidioc_s_fmt_cap(struct file *file, void *priv, |
| 791 | struct v4l2_format *fmt) |
| 792 | { |
| 793 | struct gspca_dev *gspca_dev = priv; |
| 794 | int ret, was_streaming; |
| 795 | |
| 796 | #ifdef GSPCA_DEBUG |
| 797 | if (gspca_debug & D_CONF) { |
| 798 | PDEBUG_MODE("set fmt cap", |
| 799 | fmt->fmt.pix.pixelformat, |
| 800 | fmt->fmt.pix.width, fmt->fmt.pix.height); |
| 801 | } |
| 802 | #endif |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 803 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 804 | return -ERESTARTSYS; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 805 | ret = try_fmt_cap(gspca_dev, fmt); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 806 | if (ret < 0) |
| 807 | goto out; |
| 808 | |
| 809 | if (ret == gspca_dev->curr_mode) |
| 810 | goto out; /* same mode */ |
| 811 | was_streaming = gspca_dev->streaming; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 812 | if (was_streaming) { |
| 813 | if (gspca_dev->capt_file != 0 |
| 814 | && gspca_dev->capt_file != file) { |
| 815 | ret = -EBUSY; |
| 816 | goto out; |
| 817 | } |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 818 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) { |
| 819 | ret = -ERESTARTSYS; |
| 820 | goto out; |
| 821 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 822 | gspca_stream_off(gspca_dev); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 823 | mutex_unlock(&gspca_dev->usb_lock); |
| 824 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 825 | gspca_dev->width = fmt->fmt.pix.width; |
| 826 | gspca_dev->height = fmt->fmt.pix.height; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 827 | gspca_dev->pixfmt = fmt->fmt.pix.pixelformat; |
| 828 | gspca_dev->curr_mode = ret; |
| 829 | if (was_streaming) |
| 830 | ret = gspca_init_transfer(gspca_dev); |
| 831 | out: |
| 832 | mutex_unlock(&gspca_dev->queue_lock); |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | static int dev_open(struct inode *inode, struct file *file) |
| 837 | { |
| 838 | struct gspca_dev *gspca_dev; |
| 839 | int ret; |
| 840 | |
| 841 | PDEBUG(D_STREAM, "opening"); |
| 842 | gspca_dev = (struct gspca_dev *) video_devdata(file); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 843 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 844 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 845 | if (!gspca_dev->present) { |
| 846 | ret = -ENODEV; |
| 847 | goto out; |
| 848 | } |
| 849 | |
| 850 | /* if not done yet, initialize the sensor */ |
| 851 | if (gspca_dev->users == 0) { |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 852 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) { |
| 853 | ret = -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 854 | goto out; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 855 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 856 | ret = gspca_dev->sd_desc->open(gspca_dev); |
| 857 | mutex_unlock(&gspca_dev->usb_lock); |
| 858 | if (ret != 0) { |
| 859 | PDEBUG(D_ERR|D_CONF, "init device failed %d", ret); |
| 860 | goto out; |
| 861 | } |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 862 | } else if (gspca_dev->users > 4) { /* (arbitrary value) */ |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 863 | ret = -EBUSY; |
| 864 | goto out; |
| 865 | } |
| 866 | gspca_dev->users++; |
| 867 | file->private_data = gspca_dev; |
| 868 | #ifdef GSPCA_DEBUG |
| 869 | /* activate the v4l2 debug */ |
| 870 | if (gspca_debug & D_CONF) |
| 871 | gspca_dev->vdev.debug |= 3; |
| 872 | else |
| 873 | gspca_dev->vdev.debug &= ~3; |
| 874 | #endif |
| 875 | out: |
| 876 | mutex_unlock(&gspca_dev->queue_lock); |
| 877 | if (ret != 0) |
| 878 | PDEBUG(D_ERR|D_STREAM, "open failed err %d", ret); |
| 879 | else |
| 880 | PDEBUG(D_STREAM, "open OK"); |
| 881 | return ret; |
| 882 | } |
| 883 | |
| 884 | static int dev_close(struct inode *inode, struct file *file) |
| 885 | { |
| 886 | struct gspca_dev *gspca_dev = file->private_data; |
| 887 | |
| 888 | PDEBUG(D_STREAM, "closing"); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 889 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 890 | return -ERESTARTSYS; |
| 891 | gspca_dev->users--; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 892 | |
| 893 | /* if the file did capture, free the streaming resources */ |
| 894 | if (gspca_dev->capt_file == file) { |
| 895 | mutex_lock(&gspca_dev->usb_lock); |
| 896 | if (gspca_dev->streaming) |
| 897 | gspca_stream_off(gspca_dev); |
| 898 | gspca_dev->sd_desc->close(gspca_dev); |
| 899 | mutex_unlock(&gspca_dev->usb_lock); |
| 900 | frame_free(gspca_dev); |
| 901 | file->private_data = NULL; |
| 902 | gspca_dev->capt_file = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 903 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 904 | mutex_unlock(&gspca_dev->queue_lock); |
| 905 | PDEBUG(D_STREAM, "closed"); |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | static int vidioc_querycap(struct file *file, void *priv, |
| 910 | struct v4l2_capability *cap) |
| 911 | { |
| 912 | struct gspca_dev *gspca_dev = priv; |
| 913 | |
| 914 | PDEBUG(D_CONF, "querycap"); |
| 915 | memset(cap, 0, sizeof *cap); |
| 916 | strncpy(cap->driver, gspca_dev->sd_desc->name, sizeof cap->driver); |
| 917 | strncpy(cap->card, gspca_dev->cam.dev_name, sizeof cap->card); |
| 918 | strncpy(cap->bus_info, gspca_dev->dev->bus->bus_name, |
| 919 | sizeof cap->bus_info); |
| 920 | cap->version = DRIVER_VERSION_NUMBER; |
| 921 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
| 922 | | V4L2_CAP_STREAMING |
| 923 | | V4L2_CAP_READWRITE; |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 928 | struct v4l2_queryctrl *q_ctrl) |
| 929 | { |
| 930 | struct gspca_dev *gspca_dev = priv; |
| 931 | int i; |
| 932 | |
| 933 | PDEBUG(D_CONF, "queryctrl"); |
| 934 | for (i = 0; i < gspca_dev->sd_desc->nctrls; i++) { |
| 935 | if (q_ctrl->id == gspca_dev->sd_desc->ctrls[i].qctrl.id) { |
| 936 | memcpy(q_ctrl, |
| 937 | &gspca_dev->sd_desc->ctrls[i].qctrl, |
| 938 | sizeof *q_ctrl); |
| 939 | return 0; |
| 940 | } |
| 941 | } |
| 942 | if (q_ctrl->id >= V4L2_CID_BASE |
| 943 | && q_ctrl->id <= V4L2_CID_LASTP1) { |
| 944 | q_ctrl->flags |= V4L2_CTRL_FLAG_DISABLED; |
| 945 | return 0; |
| 946 | } |
| 947 | return -EINVAL; |
| 948 | } |
| 949 | |
| 950 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 951 | struct v4l2_control *ctrl) |
| 952 | { |
| 953 | struct gspca_dev *gspca_dev = priv; |
| 954 | struct ctrl *ctrls; |
| 955 | int i, ret; |
| 956 | |
| 957 | PDEBUG(D_CONF, "set ctrl"); |
| 958 | for (i = 0, ctrls = gspca_dev->sd_desc->ctrls; |
| 959 | i < gspca_dev->sd_desc->nctrls; |
| 960 | i++, ctrls++) { |
| 961 | if (ctrl->id != ctrls->qctrl.id) |
| 962 | continue; |
| 963 | if (ctrl->value < ctrls->qctrl.minimum |
| 964 | && ctrl->value > ctrls->qctrl.maximum) |
| 965 | return -ERANGE; |
| 966 | PDEBUG(D_CONF, "set ctrl [%08x] = %d", ctrl->id, ctrl->value); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 967 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 968 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 969 | ret = ctrls->set(gspca_dev, ctrl->value); |
| 970 | mutex_unlock(&gspca_dev->usb_lock); |
| 971 | return ret; |
| 972 | } |
| 973 | return -EINVAL; |
| 974 | } |
| 975 | |
| 976 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 977 | struct v4l2_control *ctrl) |
| 978 | { |
| 979 | struct gspca_dev *gspca_dev = priv; |
| 980 | |
| 981 | struct ctrl *ctrls; |
| 982 | int i, ret; |
| 983 | |
| 984 | for (i = 0, ctrls = gspca_dev->sd_desc->ctrls; |
| 985 | i < gspca_dev->sd_desc->nctrls; |
| 986 | i++, ctrls++) { |
| 987 | if (ctrl->id != ctrls->qctrl.id) |
| 988 | continue; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 989 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 990 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 991 | ret = ctrls->get(gspca_dev, &ctrl->value); |
| 992 | mutex_unlock(&gspca_dev->usb_lock); |
| 993 | return ret; |
| 994 | } |
| 995 | return -EINVAL; |
| 996 | } |
| 997 | |
| 998 | static int vidioc_querymenu(struct file *file, void *priv, |
| 999 | struct v4l2_querymenu *qmenu) |
| 1000 | { |
| 1001 | struct gspca_dev *gspca_dev = priv; |
| 1002 | |
| 1003 | if (!gspca_dev->sd_desc->querymenu) |
| 1004 | return -EINVAL; |
| 1005 | return gspca_dev->sd_desc->querymenu(gspca_dev, qmenu); |
| 1006 | } |
| 1007 | |
| 1008 | static int vidioc_enum_input(struct file *file, void *priv, |
| 1009 | struct v4l2_input *input) |
| 1010 | { |
| 1011 | struct gspca_dev *gspca_dev = priv; |
| 1012 | |
| 1013 | if (input->index != 0) |
| 1014 | return -EINVAL; |
| 1015 | memset(input, 0, sizeof *input); |
| 1016 | input->type = V4L2_INPUT_TYPE_CAMERA; |
| 1017 | strncpy(input->name, gspca_dev->sd_desc->name, |
| 1018 | sizeof input->name); |
| 1019 | return 0; |
| 1020 | } |
| 1021 | |
| 1022 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
| 1023 | { |
| 1024 | *i = 0; |
| 1025 | return 0; |
| 1026 | } |
| 1027 | |
| 1028 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
| 1029 | { |
| 1030 | if (i > 0) |
| 1031 | return -EINVAL; |
| 1032 | return (0); |
| 1033 | } |
| 1034 | |
| 1035 | static int vidioc_reqbufs(struct file *file, void *priv, |
| 1036 | struct v4l2_requestbuffers *rb) |
| 1037 | { |
| 1038 | struct gspca_dev *gspca_dev = priv; |
| 1039 | int frsz, ret; |
| 1040 | |
| 1041 | PDEBUG(D_STREAM, "reqbufs %d", rb->count); |
| 1042 | if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1043 | return -EINVAL; |
| 1044 | if (rb->memory != V4L2_MEMORY_MMAP |
| 1045 | && rb->memory != V4L2_MEMORY_USERPTR) |
| 1046 | return -EINVAL; |
| 1047 | if (rb->count == 0) |
| 1048 | return -EINVAL; |
| 1049 | frsz = gspca_get_buff_size(gspca_dev); |
| 1050 | if (frsz < 0) |
| 1051 | return frsz; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1052 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1053 | return -ERESTARTSYS; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1054 | if (gspca_dev->capt_file != 0) { /* only one file may do capture */ |
| 1055 | ret = -EBUSY; |
| 1056 | goto out; |
| 1057 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1058 | ret = frame_alloc(gspca_dev, |
| 1059 | rb->count, |
| 1060 | (unsigned int) frsz, |
| 1061 | rb->memory); |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1062 | if (ret == 0) { |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1063 | rb->count = gspca_dev->nframes; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1064 | gspca_dev->capt_file = file; |
| 1065 | } |
| 1066 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1067 | mutex_unlock(&gspca_dev->queue_lock); |
| 1068 | PDEBUG(D_STREAM, "reqbufs st:%d c:%d", ret, rb->count); |
| 1069 | return ret; |
| 1070 | } |
| 1071 | |
| 1072 | static int vidioc_querybuf(struct file *file, void *priv, |
| 1073 | struct v4l2_buffer *v4l2_buf) |
| 1074 | { |
| 1075 | struct gspca_dev *gspca_dev = priv; |
| 1076 | struct gspca_frame *frame; |
| 1077 | |
| 1078 | PDEBUG(D_STREAM, "querybuf"); |
| 1079 | if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE |
| 1080 | || v4l2_buf->index < 0 |
| 1081 | || v4l2_buf->index >= gspca_dev->nframes) |
| 1082 | return -EINVAL; |
| 1083 | |
| 1084 | frame = &gspca_dev->frame[v4l2_buf->index]; |
| 1085 | memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | static int vidioc_streamon(struct file *file, void *priv, |
| 1090 | enum v4l2_buf_type buf_type) |
| 1091 | { |
| 1092 | struct gspca_dev *gspca_dev = priv; |
| 1093 | int ret; |
| 1094 | |
| 1095 | PDEBUG(D_STREAM, "stream on"); |
| 1096 | if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1097 | return -EINVAL; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1098 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1099 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1100 | if (!gspca_dev->present) { |
| 1101 | ret = -ENODEV; |
| 1102 | goto out; |
| 1103 | } |
| 1104 | if (gspca_dev->nframes == 0) { |
| 1105 | ret = -EINVAL; |
| 1106 | goto out; |
| 1107 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1108 | if (gspca_dev->capt_file != file) { |
| 1109 | ret = -EINVAL; |
| 1110 | goto out; |
| 1111 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1112 | if (!gspca_dev->streaming) { |
| 1113 | ret = gspca_init_transfer(gspca_dev); |
| 1114 | if (ret < 0) |
| 1115 | goto out; |
| 1116 | } |
| 1117 | #ifdef GSPCA_DEBUG |
| 1118 | if (gspca_debug & D_STREAM) { |
| 1119 | PDEBUG_MODE("stream on OK", |
| 1120 | gspca_dev->pixfmt, |
| 1121 | gspca_dev->width, |
| 1122 | gspca_dev->height); |
| 1123 | } |
| 1124 | #endif |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1125 | ret = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1126 | out: |
| 1127 | mutex_unlock(&gspca_dev->queue_lock); |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
| 1131 | static int vidioc_streamoff(struct file *file, void *priv, |
| 1132 | enum v4l2_buf_type buf_type) |
| 1133 | { |
| 1134 | struct gspca_dev *gspca_dev = priv; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1135 | int ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1136 | |
| 1137 | PDEBUG(D_STREAM, "stream off"); |
| 1138 | if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1139 | return -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1140 | if (!gspca_dev->streaming) |
| 1141 | return 0; |
| 1142 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1143 | return -ERESTARTSYS; |
| 1144 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) { |
| 1145 | ret = -ERESTARTSYS; |
| 1146 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1147 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1148 | if (gspca_dev->capt_file != file) { |
| 1149 | ret = -EINVAL; |
| 1150 | goto out2; |
| 1151 | } |
| 1152 | gspca_stream_off(gspca_dev); |
| 1153 | ret = 0; |
| 1154 | out2: |
| 1155 | mutex_unlock(&gspca_dev->usb_lock); |
| 1156 | out: |
| 1157 | mutex_unlock(&gspca_dev->queue_lock); |
| 1158 | return ret; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | static int vidioc_g_jpegcomp(struct file *file, void *priv, |
| 1162 | struct v4l2_jpegcompression *jpegcomp) |
| 1163 | { |
| 1164 | struct gspca_dev *gspca_dev = priv; |
| 1165 | int ret; |
| 1166 | |
| 1167 | if (!gspca_dev->sd_desc->get_jcomp) |
| 1168 | return -EINVAL; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1169 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1170 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1171 | ret = gspca_dev->sd_desc->get_jcomp(gspca_dev, jpegcomp); |
| 1172 | mutex_unlock(&gspca_dev->usb_lock); |
| 1173 | return ret; |
| 1174 | } |
| 1175 | |
| 1176 | static int vidioc_s_jpegcomp(struct file *file, void *priv, |
| 1177 | struct v4l2_jpegcompression *jpegcomp) |
| 1178 | { |
| 1179 | struct gspca_dev *gspca_dev = priv; |
| 1180 | int ret; |
| 1181 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1182 | if (mutex_lock_interruptible(&gspca_dev->usb_lock)) |
| 1183 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1184 | if (!gspca_dev->sd_desc->set_jcomp) |
| 1185 | return -EINVAL; |
| 1186 | ret = gspca_dev->sd_desc->set_jcomp(gspca_dev, jpegcomp); |
| 1187 | mutex_unlock(&gspca_dev->usb_lock); |
| 1188 | return ret; |
| 1189 | } |
| 1190 | |
| 1191 | static int vidioc_g_parm(struct file *filp, void *priv, |
| 1192 | struct v4l2_streamparm *parm) |
| 1193 | { |
| 1194 | struct gspca_dev *gspca_dev = priv; |
| 1195 | |
| 1196 | memset(parm, 0, sizeof parm); |
| 1197 | parm->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1198 | parm->parm.capture.readbuffers = gspca_dev->nbufread; |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | static int vidioc_s_parm(struct file *filp, void *priv, |
| 1203 | struct v4l2_streamparm *parm) |
| 1204 | { |
| 1205 | struct gspca_dev *gspca_dev = priv; |
| 1206 | int n; |
| 1207 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1208 | n = parm->parm.capture.readbuffers; |
| 1209 | if (n == 0 || n > GSPCA_MAX_FRAMES) |
| 1210 | parm->parm.capture.readbuffers = gspca_dev->nbufread; |
| 1211 | else |
| 1212 | gspca_dev->nbufread = n; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1213 | return 0; |
| 1214 | } |
| 1215 | |
| 1216 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 1217 | static int vidiocgmbuf(struct file *file, void *priv, |
| 1218 | struct video_mbuf *mbuf) |
| 1219 | { |
| 1220 | struct gspca_dev *gspca_dev = file->private_data; |
| 1221 | int i; |
| 1222 | |
| 1223 | PDEBUG(D_STREAM, "cgmbuf"); |
| 1224 | if (gspca_dev->nframes == 0) { |
| 1225 | struct v4l2_requestbuffers rb; |
| 1226 | int ret; |
| 1227 | |
| 1228 | memset(&rb, 0, sizeof rb); |
| 1229 | rb.count = 4; |
| 1230 | rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1231 | rb.memory = V4L2_MEMORY_MMAP; |
| 1232 | ret = vidioc_reqbufs(file, priv, &rb); |
| 1233 | if (ret != 0) |
| 1234 | return ret; |
| 1235 | } |
| 1236 | mbuf->frames = gspca_dev->nframes; |
| 1237 | mbuf->size = gspca_dev->frsz * gspca_dev->nframes; |
| 1238 | for (i = 0; i < mbuf->frames; i++) |
| 1239 | mbuf->offsets[i] = gspca_dev->frame[i].v4l2_buf.m.offset; |
| 1240 | return 0; |
| 1241 | } |
| 1242 | #endif |
| 1243 | |
| 1244 | static int dev_mmap(struct file *file, struct vm_area_struct *vma) |
| 1245 | { |
| 1246 | struct gspca_dev *gspca_dev = file->private_data; |
| 1247 | struct gspca_frame *frame = 0; |
| 1248 | struct page *page; |
| 1249 | unsigned long addr, start, size; |
| 1250 | int i, ret; |
| 1251 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 1252 | int compat = 0; |
| 1253 | #endif |
| 1254 | |
| 1255 | start = vma->vm_start; |
| 1256 | size = vma->vm_end - vma->vm_start; |
| 1257 | PDEBUG(D_STREAM, "mmap start:%08x size:%d", (int) start, (int) size); |
| 1258 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1259 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1260 | return -ERESTARTSYS; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1261 | if (!gspca_dev->present) { |
| 1262 | ret = -ENODEV; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1263 | goto out; |
| 1264 | } |
| 1265 | if (gspca_dev->capt_file != file) { |
| 1266 | ret = -EINVAL; |
| 1267 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1268 | } |
| 1269 | |
| 1270 | for (i = 0; i < gspca_dev->nframes; ++i) { |
| 1271 | if (gspca_dev->frame[i].v4l2_buf.memory != V4L2_MEMORY_MMAP) { |
| 1272 | PDEBUG(D_STREAM, "mmap bad memory type"); |
| 1273 | break; |
| 1274 | } |
| 1275 | if ((gspca_dev->frame[i].v4l2_buf.m.offset >> PAGE_SHIFT) |
| 1276 | == vma->vm_pgoff) { |
| 1277 | frame = &gspca_dev->frame[i]; |
| 1278 | break; |
| 1279 | } |
| 1280 | } |
| 1281 | if (frame == 0) { |
| 1282 | PDEBUG(D_STREAM, "mmap no frame buffer found"); |
| 1283 | ret = -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1284 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1285 | } |
| 1286 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 1287 | if (i == 0 && size == frame->v4l2_buf.length * gspca_dev->nframes) |
| 1288 | compat = 1; |
| 1289 | else |
| 1290 | #endif |
| 1291 | if (size != frame->v4l2_buf.length) { |
| 1292 | PDEBUG(D_STREAM, "mmap bad size"); |
| 1293 | ret = -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1294 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * - VM_IO marks the area as being a mmaped region for I/O to a |
| 1299 | * device. It also prevents the region from being core dumped. |
| 1300 | */ |
| 1301 | vma->vm_flags |= VM_IO; |
| 1302 | |
| 1303 | addr = (unsigned long) frame->data; |
| 1304 | while (size > 0) { |
| 1305 | page = vmalloc_to_page((void *) addr); |
| 1306 | ret = vm_insert_page(vma, start, page); |
| 1307 | if (ret < 0) |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1308 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1309 | start += PAGE_SIZE; |
| 1310 | addr += PAGE_SIZE; |
| 1311 | size -= PAGE_SIZE; |
| 1312 | } |
| 1313 | |
| 1314 | vma->vm_ops = &gspca_vm_ops; |
| 1315 | vma->vm_private_data = frame; |
| 1316 | gspca_vm_open(vma); |
| 1317 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 1318 | if (compat) { |
| 1319 | /*fixme: ugly*/ |
| 1320 | for (i = 1; i < gspca_dev->nframes; ++i) |
| 1321 | gspca_dev->frame[i].v4l2_buf.flags |= |
| 1322 | V4L2_BUF_FLAG_MAPPED; |
| 1323 | } |
| 1324 | #endif |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1325 | ret = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1326 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1327 | mutex_unlock(&gspca_dev->queue_lock); |
| 1328 | return ret; |
| 1329 | } |
| 1330 | |
| 1331 | static unsigned int dev_poll(struct file *file, poll_table * wait) |
| 1332 | { |
| 1333 | struct gspca_dev *gspca_dev = file->private_data; |
| 1334 | int i, ret; |
| 1335 | |
| 1336 | PDEBUG(D_FRAM, "poll"); |
| 1337 | |
| 1338 | poll_wait(file, &gspca_dev->wq, wait); |
| 1339 | |
| 1340 | if (mutex_lock_interruptible(&gspca_dev->queue_lock) != 0) |
| 1341 | return POLLERR; |
| 1342 | if (gspca_dev->dev == 0 |
| 1343 | || !gspca_dev->streaming) /* if not streaming */ |
| 1344 | ret = POLLERR; |
| 1345 | else { |
| 1346 | i = gspca_dev->fr_o; |
| 1347 | i = gspca_dev->fr_queue[i]; |
| 1348 | if (gspca_dev->frame[i].v4l2_buf.flags & V4L2_BUF_FLAG_DONE) |
| 1349 | ret = POLLIN | POLLRDNORM; /* something to read */ |
| 1350 | else |
| 1351 | ret = 0; |
| 1352 | } |
| 1353 | mutex_unlock(&gspca_dev->queue_lock); |
| 1354 | return ret; |
| 1355 | } |
| 1356 | |
| 1357 | /* |
| 1358 | * wait for a video frame |
| 1359 | * |
| 1360 | * If a frame is ready, its index is returned. |
| 1361 | */ |
| 1362 | static int gspca_frame_wait(struct gspca_dev *gspca_dev, |
| 1363 | int nonblock_ing) |
| 1364 | { |
| 1365 | struct gspca_frame *frame; |
| 1366 | int i, j, ret; |
| 1367 | |
| 1368 | i = gspca_dev->fr_o; |
| 1369 | j = gspca_dev->fr_queue[i]; |
| 1370 | frame = &gspca_dev->frame[j]; |
| 1371 | if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) |
| 1372 | goto ok; |
| 1373 | if (nonblock_ing) /* no frame yet */ |
| 1374 | return -EAGAIN; |
| 1375 | |
| 1376 | /* wait till a frame is ready */ |
| 1377 | for (;;) { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1378 | ret = wait_event_interruptible_timeout(gspca_dev->wq, |
| 1379 | atomic_read(&gspca_dev->nevent) > 0, |
| 1380 | msecs_to_jiffies(3000)); |
| 1381 | if (ret <= 0) { |
| 1382 | if (ret < 0) |
| 1383 | return ret; |
| 1384 | return -EIO; |
| 1385 | } |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1386 | if (!gspca_dev->streaming || !gspca_dev->present) |
| 1387 | return -EIO; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1388 | i = gspca_dev->fr_o; |
| 1389 | j = gspca_dev->fr_queue[i]; |
| 1390 | frame = &gspca_dev->frame[j]; |
| 1391 | if (frame->v4l2_buf.flags & V4L2_BUF_FLAG_DONE) |
| 1392 | break; |
| 1393 | } |
| 1394 | |
| 1395 | ok: |
| 1396 | atomic_dec(&gspca_dev->nevent); |
| 1397 | gspca_dev->fr_o = (i + 1) % gspca_dev->nframes; |
| 1398 | PDEBUG(D_FRAM, "frame wait q:%d i:%d o:%d", |
| 1399 | gspca_dev->fr_q, |
| 1400 | gspca_dev->fr_i, |
| 1401 | gspca_dev->fr_o); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1402 | |
| 1403 | if (gspca_dev->sd_desc->dq_callback) |
| 1404 | gspca_dev->sd_desc->dq_callback(gspca_dev); |
| 1405 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1406 | return j; |
| 1407 | } |
| 1408 | |
| 1409 | /* |
| 1410 | * dequeue a video buffer |
| 1411 | * |
| 1412 | * If nonblock_ing is false, block until a buffer is available. |
| 1413 | */ |
| 1414 | static int vidioc_dqbuf(struct file *file, void *priv, |
| 1415 | struct v4l2_buffer *v4l2_buf) |
| 1416 | { |
| 1417 | struct gspca_dev *gspca_dev = priv; |
| 1418 | struct gspca_frame *frame; |
| 1419 | int i, ret; |
| 1420 | |
| 1421 | PDEBUG(D_FRAM, "dqbuf"); |
| 1422 | if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE |
| 1423 | || (v4l2_buf->memory != V4L2_MEMORY_MMAP |
| 1424 | && v4l2_buf->memory != V4L2_MEMORY_USERPTR)) |
| 1425 | return -EINVAL; |
| 1426 | if (!gspca_dev->streaming) |
| 1427 | return -EINVAL; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1428 | if (gspca_dev->capt_file != file) { |
| 1429 | ret = -EINVAL; |
| 1430 | goto out; |
| 1431 | } |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1432 | |
| 1433 | /* only one read */ |
| 1434 | if (mutex_lock_interruptible(&gspca_dev->read_lock)) |
| 1435 | return -ERESTARTSYS; |
| 1436 | |
| 1437 | ret = gspca_frame_wait(gspca_dev, file->f_flags & O_NONBLOCK); |
| 1438 | if (ret < 0) |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1439 | goto out; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1440 | i = ret; /* frame index */ |
| 1441 | frame = &gspca_dev->frame[i]; |
| 1442 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; |
| 1443 | memcpy(v4l2_buf, &frame->v4l2_buf, sizeof *v4l2_buf); |
| 1444 | PDEBUG(D_FRAM, "dqbuf %d", i); |
| 1445 | ret = 0; |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1446 | out: |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1447 | mutex_unlock(&gspca_dev->read_lock); |
| 1448 | return ret; |
| 1449 | } |
| 1450 | |
| 1451 | /* |
| 1452 | * queue a video buffer |
| 1453 | * |
| 1454 | * Attempting to queue a buffer that has already been |
| 1455 | * queued will return -EINVAL. |
| 1456 | */ |
| 1457 | static int vidioc_qbuf(struct file *file, void *priv, |
| 1458 | struct v4l2_buffer *v4l2_buf) |
| 1459 | { |
| 1460 | struct gspca_dev *gspca_dev = priv; |
| 1461 | struct gspca_frame *frame; |
| 1462 | int i, index, ret; |
| 1463 | |
| 1464 | PDEBUG(D_FRAM, "qbuf %d", v4l2_buf->index); |
| 1465 | if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1466 | return -EINVAL; |
| 1467 | |
| 1468 | index = v4l2_buf->index; |
| 1469 | if ((unsigned) index >= gspca_dev->nframes) { |
| 1470 | PDEBUG(D_STREAM, |
| 1471 | "qbuf idx %d >= %d", index, gspca_dev->nframes); |
| 1472 | return -EINVAL; |
| 1473 | } |
| 1474 | frame = &gspca_dev->frame[index]; |
| 1475 | |
| 1476 | if (v4l2_buf->memory != frame->v4l2_buf.memory) { |
| 1477 | PDEBUG(D_STREAM, "qbuf bad memory type"); |
| 1478 | return -EINVAL; |
| 1479 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1480 | if (gspca_dev->capt_file != file) |
| 1481 | return -EINVAL; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1482 | |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1483 | if (mutex_lock_interruptible(&gspca_dev->queue_lock)) |
| 1484 | return -ERESTARTSYS; |
| 1485 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1486 | if (frame->v4l2_buf.flags |
| 1487 | & (V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE)) { |
| 1488 | PDEBUG(D_STREAM, "qbuf bad state"); |
| 1489 | ret = -EINVAL; |
| 1490 | goto out; |
| 1491 | } |
| 1492 | |
| 1493 | frame->v4l2_buf.flags |= V4L2_BUF_FLAG_QUEUED; |
| 1494 | frame->v4l2_buf.flags &= ~V4L2_BUF_FLAG_DONE; |
| 1495 | |
| 1496 | if (v4l2_buf->memory == V4L2_MEMORY_USERPTR) { |
| 1497 | frame->data = frame->data_end = |
| 1498 | (unsigned char *) v4l2_buf->m.userptr; |
| 1499 | frame->v4l2_buf.m.userptr = v4l2_buf->m.userptr; |
| 1500 | frame->v4l2_buf.length = v4l2_buf->length; |
| 1501 | } |
| 1502 | |
| 1503 | /* put the buffer in the 'queued' queue */ |
| 1504 | i = gspca_dev->fr_q; |
| 1505 | gspca_dev->fr_queue[i] = index; |
| 1506 | gspca_dev->fr_q = (i + 1) % gspca_dev->nframes; |
| 1507 | PDEBUG(D_FRAM, "qbuf q:%d i:%d o:%d", |
| 1508 | gspca_dev->fr_q, |
| 1509 | gspca_dev->fr_i, |
| 1510 | gspca_dev->fr_o); |
| 1511 | |
| 1512 | v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED; |
| 1513 | v4l2_buf->flags &= ~V4L2_BUF_FLAG_DONE; |
| 1514 | ret = 0; |
| 1515 | out: |
| 1516 | mutex_unlock(&gspca_dev->queue_lock); |
| 1517 | return ret; |
| 1518 | } |
| 1519 | |
| 1520 | static ssize_t dev_read(struct file *file, char __user *data, |
| 1521 | size_t count, loff_t *ppos) |
| 1522 | { |
| 1523 | struct gspca_dev *gspca_dev = file->private_data; |
| 1524 | struct gspca_frame *frame; |
| 1525 | struct v4l2_buffer v4l2_buf; |
| 1526 | struct timeval timestamp; |
| 1527 | int i, ret, ret2; |
| 1528 | |
| 1529 | PDEBUG(D_FRAM, "read (%p, %d)", data, count); |
| 1530 | if (gspca_dev->nframes == 0) { |
| 1531 | struct v4l2_requestbuffers rb; |
| 1532 | |
| 1533 | memset(&rb, 0, sizeof rb); |
| 1534 | rb.count = gspca_dev->nbufread; |
| 1535 | rb.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1536 | rb.memory = V4L2_MEMORY_MMAP; |
| 1537 | ret = vidioc_reqbufs(file, gspca_dev, &rb); |
| 1538 | if (ret != 0) { |
| 1539 | PDEBUG(D_STREAM, "read reqbuf err: %d", ret); |
| 1540 | return ret; |
| 1541 | } |
| 1542 | memset(&v4l2_buf, 0, sizeof v4l2_buf); |
| 1543 | v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1544 | v4l2_buf.memory = V4L2_MEMORY_MMAP; |
| 1545 | for (i = 0; i < gspca_dev->nbufread; i++) { |
| 1546 | v4l2_buf.index = i; |
| 1547 | /*fixme: ugly!*/ |
| 1548 | gspca_dev->frame[i].v4l2_buf.flags |= |
| 1549 | V4L2_BUF_FLAG_MAPPED; |
| 1550 | ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 1551 | if (ret != 0) { |
| 1552 | PDEBUG(D_STREAM, "read qbuf err: %d", ret); |
| 1553 | return ret; |
| 1554 | } |
| 1555 | } |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1556 | } else if (gspca_dev->capt_file != file) |
| 1557 | return -EINVAL; |
| 1558 | |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1559 | if (!gspca_dev->streaming) { |
| 1560 | ret = vidioc_streamon(file, gspca_dev, |
| 1561 | V4L2_BUF_TYPE_VIDEO_CAPTURE); |
| 1562 | if (ret != 0) { |
| 1563 | PDEBUG(D_STREAM, "read streamon err %d", ret); |
| 1564 | return ret; |
| 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | /* get a frame */ |
| 1569 | jiffies_to_timeval(get_jiffies_64(), ×tamp); |
| 1570 | timestamp.tv_sec--; |
| 1571 | for (i = 0; i < 2; i++) { |
| 1572 | memset(&v4l2_buf, 0, sizeof v4l2_buf); |
| 1573 | v4l2_buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1574 | v4l2_buf.memory = V4L2_MEMORY_MMAP; |
| 1575 | ret = vidioc_dqbuf(file, gspca_dev, &v4l2_buf); |
| 1576 | if (ret != 0) { |
| 1577 | PDEBUG(D_STREAM, "read dqbuf err %d", ret); |
| 1578 | return ret; |
| 1579 | } |
| 1580 | |
| 1581 | /* if the process slept for more than 1 second, |
| 1582 | * get a brand new frame */ |
| 1583 | frame = &gspca_dev->frame[v4l2_buf.index]; |
| 1584 | if (frame->v4l2_buf.timestamp.tv_sec >= timestamp.tv_sec) |
| 1585 | break; |
| 1586 | ret = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 1587 | if (ret != 0) { |
| 1588 | PDEBUG(D_STREAM, "read qbuf err %d", ret); |
| 1589 | return ret; |
| 1590 | } |
| 1591 | } |
| 1592 | |
| 1593 | /* copy the frame */ |
| 1594 | if (count < frame->v4l2_buf.bytesused) { |
| 1595 | PDEBUG(D_STREAM, "read bad count: %d < %d", |
| 1596 | count, frame->v4l2_buf.bytesused); |
| 1597 | /*fixme: special errno?*/ |
| 1598 | ret = -EINVAL; |
| 1599 | goto out; |
| 1600 | } |
| 1601 | count = frame->v4l2_buf.bytesused; |
| 1602 | ret = copy_to_user(data, frame->data, count); |
| 1603 | if (ret != 0) { |
| 1604 | PDEBUG(D_ERR|D_STREAM, |
| 1605 | "read cp to user lack %d / %d", ret, count); |
| 1606 | ret = -EFAULT; |
| 1607 | goto out; |
| 1608 | } |
| 1609 | ret = count; |
| 1610 | out: |
| 1611 | /* in each case, requeue the buffer */ |
| 1612 | ret2 = vidioc_qbuf(file, gspca_dev, &v4l2_buf); |
| 1613 | if (ret2 != 0) |
| 1614 | return ret2; |
| 1615 | return ret; |
| 1616 | } |
| 1617 | |
| 1618 | static void gspca_dev_release(struct video_device *vfd) |
| 1619 | { |
| 1620 | /* nothing */ |
| 1621 | } |
| 1622 | |
| 1623 | static struct file_operations dev_fops = { |
| 1624 | .owner = THIS_MODULE, |
| 1625 | .open = dev_open, |
| 1626 | .release = dev_close, |
| 1627 | .read = dev_read, |
| 1628 | .mmap = dev_mmap, |
| 1629 | .ioctl = video_ioctl2, |
| 1630 | .llseek = no_llseek, |
| 1631 | .poll = dev_poll, |
| 1632 | }; |
| 1633 | |
| 1634 | static struct video_device gspca_template = { |
| 1635 | .name = "gspca main driver", |
| 1636 | .type = VID_TYPE_CAPTURE, |
| 1637 | .fops = &dev_fops, |
| 1638 | .release = gspca_dev_release, /* mandatory */ |
| 1639 | .minor = -1, |
| 1640 | .vidioc_querycap = vidioc_querycap, |
| 1641 | .vidioc_dqbuf = vidioc_dqbuf, |
| 1642 | .vidioc_qbuf = vidioc_qbuf, |
| 1643 | .vidioc_enum_fmt_cap = vidioc_enum_fmt_cap, |
| 1644 | .vidioc_try_fmt_cap = vidioc_try_fmt_cap, |
| 1645 | .vidioc_g_fmt_cap = vidioc_g_fmt_cap, |
| 1646 | .vidioc_s_fmt_cap = vidioc_s_fmt_cap, |
| 1647 | .vidioc_streamon = vidioc_streamon, |
| 1648 | .vidioc_queryctrl = vidioc_queryctrl, |
| 1649 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 1650 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 1651 | .vidioc_querymenu = vidioc_querymenu, |
| 1652 | .vidioc_enum_input = vidioc_enum_input, |
| 1653 | .vidioc_g_input = vidioc_g_input, |
| 1654 | .vidioc_s_input = vidioc_s_input, |
| 1655 | .vidioc_reqbufs = vidioc_reqbufs, |
| 1656 | .vidioc_querybuf = vidioc_querybuf, |
| 1657 | .vidioc_streamoff = vidioc_streamoff, |
| 1658 | .vidioc_g_jpegcomp = vidioc_g_jpegcomp, |
| 1659 | .vidioc_s_jpegcomp = vidioc_s_jpegcomp, |
| 1660 | .vidioc_g_parm = vidioc_g_parm, |
| 1661 | .vidioc_s_parm = vidioc_s_parm, |
| 1662 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
| 1663 | .vidiocgmbuf = vidiocgmbuf, |
| 1664 | #endif |
| 1665 | }; |
| 1666 | |
| 1667 | /* |
| 1668 | * probe and create a new gspca device |
| 1669 | * |
| 1670 | * This function must be called by the sub-driver when it is |
| 1671 | * called for probing a new device. |
| 1672 | */ |
| 1673 | int gspca_dev_probe(struct usb_interface *intf, |
| 1674 | const struct usb_device_id *id, |
| 1675 | const struct sd_desc *sd_desc, |
| 1676 | int dev_size) |
| 1677 | { |
| 1678 | struct usb_interface_descriptor *interface; |
| 1679 | struct gspca_dev *gspca_dev; |
| 1680 | struct usb_device *dev = interface_to_usbdev(intf); |
| 1681 | int ret; |
| 1682 | __u16 vendor; |
| 1683 | __u16 product; |
| 1684 | |
| 1685 | vendor = le16_to_cpu(dev->descriptor.idVendor); |
| 1686 | product = le16_to_cpu(dev->descriptor.idProduct); |
| 1687 | PDEBUG(D_PROBE, "probing %04x:%04x", vendor, product); |
| 1688 | |
| 1689 | /* we don't handle multi-config cameras */ |
| 1690 | if (dev->descriptor.bNumConfigurations != 1) |
| 1691 | return -ENODEV; |
| 1692 | interface = &intf->cur_altsetting->desc; |
| 1693 | if (interface->bInterfaceNumber > 0) |
| 1694 | return -ENODEV; |
| 1695 | |
| 1696 | /* create the device */ |
| 1697 | if (dev_size < sizeof *gspca_dev) |
| 1698 | dev_size = sizeof *gspca_dev; |
| 1699 | gspca_dev = kzalloc(dev_size, GFP_KERNEL); |
| 1700 | if (gspca_dev == NULL) { |
| 1701 | err("couldn't kzalloc gspca struct"); |
| 1702 | return -EIO; |
| 1703 | } |
| 1704 | gspca_dev->dev = dev; |
| 1705 | gspca_dev->iface = interface->bInterfaceNumber; |
| 1706 | gspca_dev->sd_desc = sd_desc; |
| 1707 | /* gspca_dev->users = 0; (done by kzalloc) */ |
| 1708 | gspca_dev->nbufread = 2; |
| 1709 | |
| 1710 | /* configure the subdriver */ |
| 1711 | ret = gspca_dev->sd_desc->config(gspca_dev, id); |
| 1712 | if (ret < 0) |
| 1713 | goto out; |
| 1714 | ret = gspca_set_alt0(gspca_dev); |
| 1715 | if (ret < 0) |
| 1716 | goto out; |
| 1717 | gspca_set_default_mode(gspca_dev); |
| 1718 | |
| 1719 | mutex_init(&gspca_dev->usb_lock); |
| 1720 | mutex_init(&gspca_dev->read_lock); |
| 1721 | mutex_init(&gspca_dev->queue_lock); |
| 1722 | init_waitqueue_head(&gspca_dev->wq); |
| 1723 | |
| 1724 | /* init video stuff */ |
| 1725 | memcpy(&gspca_dev->vdev, &gspca_template, sizeof gspca_template); |
| 1726 | gspca_dev->vdev.dev = &dev->dev; |
| 1727 | ret = video_register_device(&gspca_dev->vdev, |
| 1728 | VFL_TYPE_GRABBER, |
| 1729 | video_nr); |
| 1730 | if (ret < 0) { |
| 1731 | err("video_register_device err %d", ret); |
| 1732 | goto out; |
| 1733 | } |
| 1734 | |
| 1735 | gspca_dev->present = 1; |
| 1736 | usb_set_intfdata(intf, gspca_dev); |
| 1737 | PDEBUG(D_PROBE, "probe ok"); |
| 1738 | return 0; |
| 1739 | out: |
| 1740 | kfree(gspca_dev); |
| 1741 | return ret; |
| 1742 | } |
| 1743 | EXPORT_SYMBOL(gspca_dev_probe); |
| 1744 | |
| 1745 | /* |
| 1746 | * USB disconnection |
| 1747 | * |
| 1748 | * This function must be called by the sub-driver |
| 1749 | * when the device disconnects, after the specific resources are freed. |
| 1750 | */ |
| 1751 | void gspca_disconnect(struct usb_interface *intf) |
| 1752 | { |
Jean-Francois Moine | 4aa0d03 | 2008-05-04 06:46:21 -0300 | [diff] [blame^] | 1753 | struct gspca_dev *gspca_dev = usb_get_intfdata(intf); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1754 | |
| 1755 | if (!gspca_dev) |
| 1756 | return; |
| 1757 | gspca_dev->present = 0; |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1758 | mutex_lock(&gspca_dev->queue_lock); |
| 1759 | mutex_lock(&gspca_dev->usb_lock); |
| 1760 | gspca_dev->streaming = 0; |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1761 | gspca_kill_transfer(gspca_dev); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1762 | mutex_unlock(&gspca_dev->usb_lock); |
Hans de Goede | e2997a7 | 2008-04-23 08:09:12 -0300 | [diff] [blame] | 1763 | mutex_unlock(&gspca_dev->queue_lock); |
Jean-Francois Moine | 63eb954 | 2008-04-12 09:58:09 -0300 | [diff] [blame] | 1764 | while (gspca_dev->users != 0) { /* wait until fully closed */ |
| 1765 | atomic_inc(&gspca_dev->nevent); |
| 1766 | wake_up_interruptible(&gspca_dev->wq); /* wake processes */ |
| 1767 | schedule(); |
| 1768 | } |
| 1769 | /* We don't want people trying to open up the device */ |
| 1770 | video_unregister_device(&gspca_dev->vdev); |
| 1771 | /* Free the memory */ |
| 1772 | kfree(gspca_dev); |
| 1773 | PDEBUG(D_PROBE, "disconnect complete"); |
| 1774 | } |
| 1775 | EXPORT_SYMBOL(gspca_disconnect); |
| 1776 | |
| 1777 | /* -- module insert / remove -- */ |
| 1778 | static int __init gspca_init(void) |
| 1779 | { |
| 1780 | info("main v%s registered", version); |
| 1781 | return 0; |
| 1782 | } |
| 1783 | static void __exit gspca_exit(void) |
| 1784 | { |
| 1785 | info("main deregistered"); |
| 1786 | } |
| 1787 | |
| 1788 | module_init(gspca_init); |
| 1789 | module_exit(gspca_exit); |
| 1790 | |
| 1791 | module_param_named(debug, gspca_debug, int, 0644); |
| 1792 | MODULE_PARM_DESC(debug, |
| 1793 | "Debug (bit) 0x01:error 0x02:probe 0x04:config" |
| 1794 | " 0x08:stream 0x10:frame 0x20:packet 0x40:USBin 0x80:USBout"); |
| 1795 | |
| 1796 | module_param(comp_fac, int, 0644); |
| 1797 | MODULE_PARM_DESC(comp_fac, |
| 1798 | "Buffer size ratio when compressed in percent"); |