Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame^] | 1 | /* |
| 2 | * Fujifilm Finepix subdriver |
| 3 | * |
| 4 | * Copyright (C) 2008 Frank Zago |
| 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; either version 2 of the License, or |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License 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 |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
| 20 | |
| 21 | #define MODULE_NAME "finepix" |
| 22 | |
| 23 | #include "gspca.h" |
| 24 | |
| 25 | MODULE_AUTHOR("Frank Zago <frank@zago.net>"); |
| 26 | MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver"); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | |
| 29 | /* Default timeout, in ms */ |
| 30 | #define FPIX_TIMEOUT (HZ / 10) |
| 31 | |
| 32 | /* Maximum transfer size to use. The windows driver reads by chunks of |
| 33 | * 0x2000 bytes, so do the same. Note: reading more seems to work |
| 34 | * too. */ |
| 35 | #define FPIX_MAX_TRANSFER 0x2000 |
| 36 | |
| 37 | /* Structure to hold all of our device specific stuff */ |
| 38 | struct usb_fpix { |
| 39 | struct gspca_dev gspca_dev; /* !! must be the first item */ |
| 40 | |
| 41 | /* |
| 42 | * USB stuff |
| 43 | */ |
| 44 | struct usb_ctrlrequest ctrlreq; |
| 45 | struct urb *control_urb; |
| 46 | struct timer_list bulk_timer; |
| 47 | |
| 48 | enum { |
| 49 | FPIX_NOP, /* inactive, else streaming */ |
| 50 | FPIX_RESET, /* must reset */ |
| 51 | FPIX_REQ_FRAME, /* requesting a frame */ |
| 52 | FPIX_READ_FRAME, /* reading frame */ |
| 53 | } state; |
| 54 | |
| 55 | /* |
| 56 | * Driver stuff |
| 57 | */ |
| 58 | struct delayed_work wqe; |
| 59 | struct completion can_close; |
| 60 | int streaming; |
| 61 | }; |
| 62 | |
| 63 | /* Delay after which claim the next frame. If the delay is too small, |
| 64 | * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms |
| 65 | * will fail every 4 or 5 frames, but 30ms is perfect. */ |
| 66 | #define NEXT_FRAME_DELAY (((HZ * 30) + 999) / 1000) |
| 67 | |
| 68 | #define dev_new_state(new_state) { \ |
| 69 | PDEBUG(D_STREAM, "new state from %d to %d at %s:%d", \ |
| 70 | dev->state, new_state, __func__, __LINE__); \ |
| 71 | dev->state = new_state; \ |
| 72 | } |
| 73 | |
| 74 | /* These cameras only support 320x200. */ |
| 75 | static struct v4l2_pix_format fpix_mode[1] = { |
| 76 | { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, |
| 77 | .bytesperline = 320, |
| 78 | .sizeimage = 320 * 240 * 3 / 8 + 590, |
| 79 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 80 | .priv = 0} |
| 81 | }; |
| 82 | |
| 83 | /* Reads part of a frame */ |
| 84 | static void read_frame_part(struct usb_fpix *dev) |
| 85 | { |
| 86 | int ret; |
| 87 | |
| 88 | PDEBUG(D_STREAM, "read_frame_part"); |
| 89 | |
| 90 | /* Reads part of a frame */ |
| 91 | ret = usb_submit_urb(dev->gspca_dev.urb[0], GFP_ATOMIC); |
| 92 | if (ret) { |
| 93 | dev_new_state(FPIX_RESET); |
| 94 | schedule_delayed_work(&dev->wqe, 1); |
| 95 | PDEBUG(D_STREAM, "usb_submit_urb failed with %d", |
| 96 | ret); |
| 97 | } else { |
| 98 | /* Sometimes we never get a callback, so use a timer. |
| 99 | * Is this masking a bug somewhere else? */ |
| 100 | dev->bulk_timer.expires = jiffies + msecs_to_jiffies(150); |
| 101 | add_timer(&dev->bulk_timer); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /* Callback for URBs. */ |
| 106 | static void urb_callback(struct urb *urb) |
| 107 | { |
| 108 | struct gspca_dev *gspca_dev = urb->context; |
| 109 | struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; |
| 110 | |
| 111 | PDEBUG(D_PACK, |
| 112 | "enter urb_callback - status=%d, length=%d", |
| 113 | urb->status, urb->actual_length); |
| 114 | |
| 115 | if (dev->state == FPIX_READ_FRAME) |
| 116 | del_timer(&dev->bulk_timer); |
| 117 | |
| 118 | if (urb->status != 0) { |
| 119 | /* We kill a stuck urb every 50 frames on average, so don't |
| 120 | * display a log message for that. */ |
| 121 | if (urb->status != -ECONNRESET) |
| 122 | PDEBUG(D_STREAM, "bad URB status %d", urb->status); |
| 123 | dev_new_state(FPIX_RESET); |
| 124 | schedule_delayed_work(&dev->wqe, 1); |
| 125 | } |
| 126 | |
| 127 | switch (dev->state) { |
| 128 | case FPIX_REQ_FRAME: |
| 129 | dev_new_state(FPIX_READ_FRAME); |
| 130 | read_frame_part(dev); |
| 131 | break; |
| 132 | |
| 133 | case FPIX_READ_FRAME: { |
| 134 | unsigned char *data = urb->transfer_buffer; |
| 135 | struct gspca_frame *frame; |
| 136 | |
| 137 | frame = gspca_get_i_frame(&dev->gspca_dev); |
| 138 | if (frame == NULL) { |
| 139 | gspca_dev->last_packet_type = DISCARD_PACKET; |
| 140 | break; |
| 141 | } |
| 142 | if (urb->actual_length < FPIX_MAX_TRANSFER || |
| 143 | (data[urb->actual_length-2] == 0xff && |
| 144 | data[urb->actual_length-1] == 0xd9)) { |
| 145 | |
| 146 | /* If the result is less than what was asked |
| 147 | * for, then it's the end of the |
| 148 | * frame. Sometime the jpeg is not complete, |
| 149 | * but there's nothing we can do. We also end |
| 150 | * here if the the jpeg ends right at the end |
| 151 | * of the frame. */ |
| 152 | gspca_frame_add(gspca_dev, LAST_PACKET, |
| 153 | frame, |
| 154 | data, urb->actual_length); |
| 155 | dev_new_state(FPIX_REQ_FRAME); |
| 156 | schedule_delayed_work(&dev->wqe, NEXT_FRAME_DELAY); |
| 157 | } else { |
| 158 | |
| 159 | /* got a partial image */ |
| 160 | gspca_frame_add(gspca_dev, |
| 161 | gspca_dev->last_packet_type == LAST_PACKET |
| 162 | ? FIRST_PACKET : INTER_PACKET, |
| 163 | frame, |
| 164 | data, urb->actual_length); |
| 165 | read_frame_part(dev); |
| 166 | } |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | case FPIX_NOP: |
| 171 | case FPIX_RESET: |
| 172 | PDEBUG(D_STREAM, "invalid state %d", dev->state); |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* Request a new frame */ |
| 178 | static void request_frame(struct usb_fpix *dev) |
| 179 | { |
| 180 | int ret; |
| 181 | struct gspca_dev *gspca_dev = &dev->gspca_dev; |
| 182 | |
| 183 | /* Setup command packet */ |
| 184 | memset(gspca_dev->usb_buf, 0, 12); |
| 185 | gspca_dev->usb_buf[0] = 0xd3; |
| 186 | gspca_dev->usb_buf[7] = 0x01; |
| 187 | |
| 188 | /* Request a frame */ |
| 189 | dev->ctrlreq.bRequestType = |
| 190 | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE; |
| 191 | dev->ctrlreq.bRequest = USB_REQ_GET_STATUS; |
| 192 | dev->ctrlreq.wValue = 0; |
| 193 | dev->ctrlreq.wIndex = 0; |
| 194 | dev->ctrlreq.wLength = cpu_to_le16(12); |
| 195 | |
| 196 | usb_fill_control_urb(dev->control_urb, |
| 197 | gspca_dev->dev, |
| 198 | usb_sndctrlpipe(gspca_dev->dev, 0), |
| 199 | (unsigned char *) &dev->ctrlreq, |
| 200 | gspca_dev->usb_buf, |
| 201 | 12, urb_callback, gspca_dev); |
| 202 | |
| 203 | ret = usb_submit_urb(dev->control_urb, GFP_ATOMIC); |
| 204 | if (ret) { |
| 205 | dev_new_state(FPIX_RESET); |
| 206 | schedule_delayed_work(&dev->wqe, 1); |
| 207 | PDEBUG(D_STREAM, "usb_submit_urb failed with %d", ret); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /*--------------------------------------------------------------------------*/ |
| 212 | |
| 213 | /* State machine. */ |
| 214 | static void fpix_sm(struct work_struct *work) |
| 215 | { |
| 216 | struct usb_fpix *dev = container_of(work, struct usb_fpix, wqe.work); |
| 217 | |
| 218 | PDEBUG(D_STREAM, "fpix_sm state %d", dev->state); |
| 219 | |
| 220 | /* verify that the device wasn't unplugged */ |
| 221 | if (!dev->gspca_dev.present) { |
| 222 | PDEBUG(D_STREAM, "device is gone"); |
| 223 | dev_new_state(FPIX_NOP); |
| 224 | complete(&dev->can_close); |
| 225 | return; |
| 226 | } |
| 227 | |
| 228 | if (!dev->streaming) { |
| 229 | PDEBUG(D_STREAM, "stopping state machine"); |
| 230 | dev_new_state(FPIX_NOP); |
| 231 | complete(&dev->can_close); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | switch (dev->state) { |
| 236 | case FPIX_RESET: |
| 237 | dev_new_state(FPIX_REQ_FRAME); |
| 238 | schedule_delayed_work(&dev->wqe, HZ / 10); |
| 239 | break; |
| 240 | |
| 241 | case FPIX_REQ_FRAME: |
| 242 | /* get an image */ |
| 243 | request_frame(dev); |
| 244 | break; |
| 245 | |
| 246 | case FPIX_NOP: |
| 247 | case FPIX_READ_FRAME: |
| 248 | PDEBUG(D_STREAM, "invalid state %d", dev->state); |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | /* this function is called at probe time */ |
| 254 | static int sd_config(struct gspca_dev *gspca_dev, |
| 255 | const struct usb_device_id *id) |
| 256 | { |
| 257 | struct cam *cam = &gspca_dev->cam; |
| 258 | |
| 259 | cam->cam_mode = fpix_mode; |
| 260 | cam->nmodes = 1; |
| 261 | cam->epaddr = 0x01; /* todo: correct for all cams? */ |
| 262 | cam->bulk_size = FPIX_MAX_TRANSFER; |
| 263 | |
| 264 | /* gspca_dev->nbalt = 1; * use bulk transfer */ |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | /* Stop streaming and free the ressources allocated by sd_start. */ |
| 269 | static void sd_stopN(struct gspca_dev *gspca_dev) |
| 270 | { |
| 271 | struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; |
| 272 | |
| 273 | dev->streaming = 0; |
| 274 | |
| 275 | /* Stop the state machine */ |
| 276 | if (dev->state != FPIX_NOP) |
| 277 | wait_for_completion(&dev->can_close); |
| 278 | |
| 279 | usb_free_urb(dev->control_urb); |
| 280 | dev->control_urb = NULL; |
| 281 | } |
| 282 | |
| 283 | /* Kill an URB that hasn't completed. */ |
| 284 | static void timeout_kill(unsigned long data) |
| 285 | { |
| 286 | struct urb *urb = (struct urb *) data; |
| 287 | |
| 288 | usb_unlink_urb(urb); |
| 289 | } |
| 290 | |
| 291 | /* this function is called at probe and resume time */ |
| 292 | static int sd_init(struct gspca_dev *gspca_dev) |
| 293 | { |
| 294 | struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; |
| 295 | |
| 296 | INIT_DELAYED_WORK(&dev->wqe, fpix_sm); |
| 297 | |
| 298 | init_timer(&dev->bulk_timer); |
| 299 | dev->bulk_timer.function = timeout_kill; |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int sd_start(struct gspca_dev *gspca_dev) |
| 305 | { |
| 306 | struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; |
| 307 | int ret; |
| 308 | int size_ret; |
| 309 | |
| 310 | /* Reset bulk in endpoint */ |
| 311 | usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr); |
| 312 | |
| 313 | /* Init the device */ |
| 314 | memset(gspca_dev->usb_buf, 0, 12); |
| 315 | gspca_dev->usb_buf[0] = 0xc6; |
| 316 | gspca_dev->usb_buf[8] = 0x20; |
| 317 | |
| 318 | ret = usb_control_msg(gspca_dev->dev, |
| 319 | usb_sndctrlpipe(gspca_dev->dev, 0), |
| 320 | USB_REQ_GET_STATUS, |
| 321 | USB_DIR_OUT | USB_TYPE_CLASS | |
| 322 | USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf, |
| 323 | 12, FPIX_TIMEOUT); |
| 324 | |
| 325 | if (ret != 12) { |
| 326 | PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret); |
| 327 | ret = -EIO; |
| 328 | goto error; |
| 329 | } |
| 330 | |
| 331 | /* Read the result of the command. Ignore the result, for it |
| 332 | * varies with the device. */ |
| 333 | ret = usb_bulk_msg(gspca_dev->dev, |
| 334 | usb_rcvbulkpipe(gspca_dev->dev, |
| 335 | gspca_dev->cam.epaddr), |
| 336 | gspca_dev->usb_buf, FPIX_MAX_TRANSFER, &size_ret, |
| 337 | FPIX_TIMEOUT); |
| 338 | if (ret != 0) { |
| 339 | PDEBUG(D_STREAM, "usb_bulk_msg failed (%d)", ret); |
| 340 | ret = -EIO; |
| 341 | goto error; |
| 342 | } |
| 343 | |
| 344 | /* Request a frame, but don't read it */ |
| 345 | memset(gspca_dev->usb_buf, 0, 12); |
| 346 | gspca_dev->usb_buf[0] = 0xd3; |
| 347 | gspca_dev->usb_buf[7] = 0x01; |
| 348 | |
| 349 | ret = usb_control_msg(gspca_dev->dev, |
| 350 | usb_sndctrlpipe(gspca_dev->dev, 0), |
| 351 | USB_REQ_GET_STATUS, |
| 352 | USB_DIR_OUT | USB_TYPE_CLASS | |
| 353 | USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf, |
| 354 | 12, FPIX_TIMEOUT); |
| 355 | if (ret != 12) { |
| 356 | PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret); |
| 357 | ret = -EIO; |
| 358 | goto error; |
| 359 | } |
| 360 | |
| 361 | /* Again, reset bulk in endpoint */ |
| 362 | usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr); |
| 363 | |
| 364 | /* Allocate a control URB */ |
| 365 | dev->control_urb = usb_alloc_urb(0, GFP_KERNEL); |
| 366 | if (!dev->control_urb) { |
| 367 | PDEBUG(D_STREAM, "No free urbs available"); |
| 368 | ret = -EIO; |
| 369 | goto error; |
| 370 | } |
| 371 | |
| 372 | /* Various initializations. */ |
| 373 | init_completion(&dev->can_close); |
| 374 | dev->bulk_timer.data = (unsigned long)dev->gspca_dev.urb[0]; |
| 375 | dev->gspca_dev.urb[0]->complete = urb_callback; |
| 376 | dev->streaming = 1; |
| 377 | |
| 378 | /* Schedule a frame request. */ |
| 379 | dev_new_state(FPIX_REQ_FRAME); |
| 380 | schedule_delayed_work(&dev->wqe, 1); |
| 381 | |
| 382 | return 0; |
| 383 | |
| 384 | error: |
| 385 | /* Free the ressources */ |
| 386 | sd_stopN(gspca_dev); |
| 387 | return ret; |
| 388 | } |
| 389 | |
| 390 | /* Table of supported USB devices */ |
| 391 | static const __devinitdata struct usb_device_id device_table[] = { |
| 392 | {USB_DEVICE(0x04cb, 0x0104)}, |
| 393 | {USB_DEVICE(0x04cb, 0x0109)}, |
| 394 | {USB_DEVICE(0x04cb, 0x010b)}, |
| 395 | {USB_DEVICE(0x04cb, 0x010f)}, |
| 396 | {USB_DEVICE(0x04cb, 0x0111)}, |
| 397 | {USB_DEVICE(0x04cb, 0x0113)}, |
| 398 | {USB_DEVICE(0x04cb, 0x0115)}, |
| 399 | {USB_DEVICE(0x04cb, 0x0117)}, |
| 400 | {USB_DEVICE(0x04cb, 0x0119)}, |
| 401 | {USB_DEVICE(0x04cb, 0x011b)}, |
| 402 | {USB_DEVICE(0x04cb, 0x011d)}, |
| 403 | {USB_DEVICE(0x04cb, 0x0121)}, |
| 404 | {USB_DEVICE(0x04cb, 0x0123)}, |
| 405 | {USB_DEVICE(0x04cb, 0x0125)}, |
| 406 | {USB_DEVICE(0x04cb, 0x0127)}, |
| 407 | {USB_DEVICE(0x04cb, 0x0129)}, |
| 408 | {USB_DEVICE(0x04cb, 0x012b)}, |
| 409 | {USB_DEVICE(0x04cb, 0x012d)}, |
| 410 | {USB_DEVICE(0x04cb, 0x012f)}, |
| 411 | {USB_DEVICE(0x04cb, 0x0131)}, |
| 412 | {USB_DEVICE(0x04cb, 0x013b)}, |
| 413 | {USB_DEVICE(0x04cb, 0x013d)}, |
| 414 | {USB_DEVICE(0x04cb, 0x013f)}, |
| 415 | {} |
| 416 | }; |
| 417 | |
| 418 | MODULE_DEVICE_TABLE(usb, device_table); |
| 419 | |
| 420 | /* sub-driver description */ |
| 421 | static const struct sd_desc sd_desc = { |
| 422 | .name = MODULE_NAME, |
| 423 | .config = sd_config, |
| 424 | .init = sd_init, |
| 425 | .start = sd_start, |
| 426 | .stopN = sd_stopN, |
| 427 | }; |
| 428 | |
| 429 | /* -- device connect -- */ |
| 430 | static int sd_probe(struct usb_interface *intf, |
| 431 | const struct usb_device_id *id) |
| 432 | { |
| 433 | return gspca_dev_probe(intf, id, |
| 434 | &sd_desc, |
| 435 | sizeof(struct usb_fpix), |
| 436 | THIS_MODULE); |
| 437 | } |
| 438 | |
| 439 | static struct usb_driver sd_driver = { |
| 440 | .name = MODULE_NAME, |
| 441 | .id_table = device_table, |
| 442 | .probe = sd_probe, |
| 443 | .disconnect = gspca_disconnect, |
| 444 | #ifdef CONFIG_PM |
| 445 | .suspend = gspca_suspend, |
| 446 | .resume = gspca_resume, |
| 447 | #endif |
| 448 | }; |
| 449 | |
| 450 | /* -- module insert / remove -- */ |
| 451 | static int __init sd_mod_init(void) |
| 452 | { |
| 453 | if (usb_register(&sd_driver) < 0) |
| 454 | return -1; |
| 455 | PDEBUG(D_PROBE, "registered"); |
| 456 | return 0; |
| 457 | } |
| 458 | static void __exit sd_mod_exit(void) |
| 459 | { |
| 460 | usb_deregister(&sd_driver); |
| 461 | PDEBUG(D_PROBE, "deregistered"); |
| 462 | } |
| 463 | |
| 464 | module_init(sd_mod_init); |
| 465 | module_exit(sd_mod_exit); |