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 */ |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 30 | #define FPIX_TIMEOUT 250 |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 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 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 41 | struct work_struct work_struct; |
| 42 | struct workqueue_struct *work_thread; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /* Delay after which claim the next frame. If the delay is too small, |
| 46 | * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 47 | * will fail every 4 or 5 frames, but 30ms is perfect. On the A210, |
| 48 | * 30ms is bad while 35ms is perfect. */ |
| 49 | #define NEXT_FRAME_DELAY 35 |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 50 | |
| 51 | /* These cameras only support 320x200. */ |
Jean-Francois Moine | cc611b8 | 2008-12-29 07:49:41 -0300 | [diff] [blame] | 52 | static const struct v4l2_pix_format fpix_mode[1] = { |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 53 | { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE, |
| 54 | .bytesperline = 320, |
| 55 | .sizeimage = 320 * 240 * 3 / 8 + 590, |
| 56 | .colorspace = V4L2_COLORSPACE_SRGB, |
| 57 | .priv = 0} |
| 58 | }; |
| 59 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 60 | /* send a command to the webcam */ |
| 61 | static int command(struct gspca_dev *gspca_dev, |
| 62 | int order) /* 0: reset, 1: frame request */ |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 63 | { |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 64 | static u8 order_values[2][12] = { |
| 65 | {0xc6, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0}, /* reset */ |
| 66 | {0xd3, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0}, /* fr req */ |
| 67 | }; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 68 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 69 | memcpy(gspca_dev->usb_buf, order_values[order], 12); |
| 70 | return usb_control_msg(gspca_dev->dev, |
| 71 | usb_sndctrlpipe(gspca_dev->dev, 0), |
| 72 | USB_REQ_GET_STATUS, |
| 73 | USB_DIR_OUT | USB_TYPE_CLASS | |
| 74 | USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf, |
| 75 | 12, FPIX_TIMEOUT); |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 76 | } |
| 77 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 78 | /* workqueue */ |
| 79 | static void dostream(struct work_struct *work) |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 80 | { |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 81 | struct usb_fpix *dev = container_of(work, struct usb_fpix, work_struct); |
| 82 | struct gspca_dev *gspca_dev = &dev->gspca_dev; |
| 83 | struct urb *urb = gspca_dev->urb[0]; |
| 84 | u8 *data = urb->transfer_buffer; |
| 85 | struct gspca_frame *frame; |
| 86 | int ret = 0; |
| 87 | int len; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 88 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 89 | /* synchronize with the main driver */ |
| 90 | mutex_lock(&gspca_dev->usb_lock); |
| 91 | mutex_unlock(&gspca_dev->usb_lock); |
| 92 | PDEBUG(D_STREAM, "dostream started"); |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 93 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 94 | /* loop reading a frame */ |
| 95 | again: |
| 96 | while (gspca_dev->present && gspca_dev->streaming) { |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 97 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 98 | /* request a frame */ |
| 99 | mutex_lock(&gspca_dev->usb_lock); |
| 100 | ret = command(gspca_dev, 1); |
| 101 | mutex_unlock(&gspca_dev->usb_lock); |
| 102 | if (ret < 0) |
| 103 | break; |
| 104 | if (!gspca_dev->present || !gspca_dev->streaming) |
| 105 | break; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 106 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 107 | /* the frame comes in parts */ |
| 108 | for (;;) { |
| 109 | ret = usb_bulk_msg(gspca_dev->dev, |
| 110 | urb->pipe, |
| 111 | data, |
| 112 | FPIX_MAX_TRANSFER, |
| 113 | &len, FPIX_TIMEOUT); |
| 114 | if (ret < 0) { |
| 115 | /* Most of the time we get a timeout |
| 116 | * error. Just restart. */ |
| 117 | goto again; |
| 118 | } |
| 119 | if (!gspca_dev->present || !gspca_dev->streaming) |
| 120 | goto out; |
| 121 | frame = gspca_get_i_frame(&dev->gspca_dev); |
| 122 | if (frame == NULL) |
| 123 | gspca_dev->last_packet_type = DISCARD_PACKET; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 124 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 125 | if (len < FPIX_MAX_TRANSFER || |
| 126 | (data[len - 2] == 0xff && |
| 127 | data[len - 1] == 0xd9)) { |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 128 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 129 | /* If the result is less than what was asked |
| 130 | * for, then it's the end of the |
| 131 | * frame. Sometimes the jpeg is not complete, |
| 132 | * but there's nothing we can do. We also end |
| 133 | * here if the the jpeg ends right at the end |
| 134 | * of the frame. */ |
| 135 | if (frame) |
| 136 | frame = gspca_frame_add(gspca_dev, |
| 137 | LAST_PACKET, |
| 138 | frame, |
| 139 | data, len); |
| 140 | break; |
| 141 | } |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 142 | |
| 143 | /* got a partial image */ |
Frank Zago | a3a5846 | 2008-09-30 03:55:33 -0300 | [diff] [blame] | 144 | if (frame) |
| 145 | gspca_frame_add(gspca_dev, |
| 146 | gspca_dev->last_packet_type |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 147 | == LAST_PACKET |
Frank Zago | a3a5846 | 2008-09-30 03:55:33 -0300 | [diff] [blame] | 148 | ? FIRST_PACKET : INTER_PACKET, |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 149 | frame, data, len); |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 150 | } |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 151 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 152 | /* We must wait before trying reading the next |
| 153 | * frame. If we don't, or if the delay is too short, |
| 154 | * the camera will disconnect. */ |
| 155 | msleep(NEXT_FRAME_DELAY); |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 156 | } |
| 157 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 158 | out: |
| 159 | PDEBUG(D_STREAM, "dostream stopped"); |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* this function is called at probe time */ |
| 163 | static int sd_config(struct gspca_dev *gspca_dev, |
| 164 | const struct usb_device_id *id) |
| 165 | { |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 166 | struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 167 | struct cam *cam = &gspca_dev->cam; |
| 168 | |
| 169 | cam->cam_mode = fpix_mode; |
| 170 | cam->nmodes = 1; |
Jean-Francois Moine | 6929dc6 | 2009-04-21 13:45:56 -0300 | [diff] [blame] | 171 | cam->bulk = 1; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 172 | cam->bulk_size = FPIX_MAX_TRANSFER; |
| 173 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 174 | INIT_WORK(&dev->work_struct, dostream); |
| 175 | |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 179 | /* this function is called at probe and resume time */ |
| 180 | static int sd_init(struct gspca_dev *gspca_dev) |
| 181 | { |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 185 | /* start the camera */ |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 186 | static int sd_start(struct gspca_dev *gspca_dev) |
| 187 | { |
| 188 | struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 189 | int ret, len; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 190 | |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 191 | /* Init the device */ |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 192 | ret = command(gspca_dev, 0); |
| 193 | if (ret < 0) { |
| 194 | PDEBUG(D_STREAM, "init failed %d", ret); |
| 195 | return ret; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* Read the result of the command. Ignore the result, for it |
| 199 | * varies with the device. */ |
| 200 | ret = usb_bulk_msg(gspca_dev->dev, |
Jean-Francois Moine | 50e06de | 2008-12-31 08:13:46 -0300 | [diff] [blame] | 201 | gspca_dev->urb[0]->pipe, |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 202 | gspca_dev->urb[0]->transfer_buffer, |
| 203 | FPIX_MAX_TRANSFER, &len, |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 204 | FPIX_TIMEOUT); |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 205 | if (ret < 0) { |
| 206 | PDEBUG(D_STREAM, "usb_bulk_msg failed %d", ret); |
| 207 | return ret; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* Request a frame, but don't read it */ |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 211 | ret = command(gspca_dev, 1); |
| 212 | if (ret < 0) { |
| 213 | PDEBUG(D_STREAM, "frame request failed %d", ret); |
| 214 | return ret; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* Again, reset bulk in endpoint */ |
Jean-Francois Moine | 50e06de | 2008-12-31 08:13:46 -0300 | [diff] [blame] | 218 | usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe); |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 219 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 220 | /* Start the workqueue function to do the streaming */ |
| 221 | dev->work_thread = create_singlethread_workqueue(MODULE_NAME); |
| 222 | queue_work(dev->work_thread, &dev->work_struct); |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 223 | |
| 224 | return 0; |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 225 | } |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 226 | |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 227 | /* called on streamoff with alt==0 and on disconnect */ |
| 228 | /* the usb_lock is held at entry - restore on exit */ |
| 229 | static void sd_stop0(struct gspca_dev *gspca_dev) |
| 230 | { |
| 231 | struct usb_fpix *dev = (struct usb_fpix *) gspca_dev; |
| 232 | |
| 233 | /* wait for the work queue to terminate */ |
| 234 | mutex_unlock(&gspca_dev->usb_lock); |
| 235 | destroy_workqueue(dev->work_thread); |
| 236 | mutex_lock(&gspca_dev->usb_lock); |
| 237 | dev->work_thread = NULL; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /* Table of supported USB devices */ |
| 241 | static const __devinitdata struct usb_device_id device_table[] = { |
| 242 | {USB_DEVICE(0x04cb, 0x0104)}, |
| 243 | {USB_DEVICE(0x04cb, 0x0109)}, |
| 244 | {USB_DEVICE(0x04cb, 0x010b)}, |
| 245 | {USB_DEVICE(0x04cb, 0x010f)}, |
| 246 | {USB_DEVICE(0x04cb, 0x0111)}, |
| 247 | {USB_DEVICE(0x04cb, 0x0113)}, |
| 248 | {USB_DEVICE(0x04cb, 0x0115)}, |
| 249 | {USB_DEVICE(0x04cb, 0x0117)}, |
| 250 | {USB_DEVICE(0x04cb, 0x0119)}, |
| 251 | {USB_DEVICE(0x04cb, 0x011b)}, |
| 252 | {USB_DEVICE(0x04cb, 0x011d)}, |
| 253 | {USB_DEVICE(0x04cb, 0x0121)}, |
| 254 | {USB_DEVICE(0x04cb, 0x0123)}, |
| 255 | {USB_DEVICE(0x04cb, 0x0125)}, |
| 256 | {USB_DEVICE(0x04cb, 0x0127)}, |
| 257 | {USB_DEVICE(0x04cb, 0x0129)}, |
| 258 | {USB_DEVICE(0x04cb, 0x012b)}, |
| 259 | {USB_DEVICE(0x04cb, 0x012d)}, |
| 260 | {USB_DEVICE(0x04cb, 0x012f)}, |
| 261 | {USB_DEVICE(0x04cb, 0x0131)}, |
| 262 | {USB_DEVICE(0x04cb, 0x013b)}, |
| 263 | {USB_DEVICE(0x04cb, 0x013d)}, |
| 264 | {USB_DEVICE(0x04cb, 0x013f)}, |
| 265 | {} |
| 266 | }; |
| 267 | |
| 268 | MODULE_DEVICE_TABLE(usb, device_table); |
| 269 | |
| 270 | /* sub-driver description */ |
| 271 | static const struct sd_desc sd_desc = { |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 272 | .name = MODULE_NAME, |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 273 | .config = sd_config, |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 274 | .init = sd_init, |
| 275 | .start = sd_start, |
| 276 | .stop0 = sd_stop0, |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | /* -- device connect -- */ |
| 280 | static int sd_probe(struct usb_interface *intf, |
| 281 | const struct usb_device_id *id) |
| 282 | { |
| 283 | return gspca_dev_probe(intf, id, |
| 284 | &sd_desc, |
| 285 | sizeof(struct usb_fpix), |
| 286 | THIS_MODULE); |
| 287 | } |
| 288 | |
| 289 | static struct usb_driver sd_driver = { |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 290 | .name = MODULE_NAME, |
| 291 | .id_table = device_table, |
| 292 | .probe = sd_probe, |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 293 | .disconnect = gspca_disconnect, |
| 294 | #ifdef CONFIG_PM |
| 295 | .suspend = gspca_suspend, |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 296 | .resume = gspca_resume, |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 297 | #endif |
| 298 | }; |
| 299 | |
| 300 | /* -- module insert / remove -- */ |
| 301 | static int __init sd_mod_init(void) |
| 302 | { |
Alexey Klimov | f69e952 | 2009-01-01 13:02:07 -0300 | [diff] [blame] | 303 | int ret; |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 304 | |
Alexey Klimov | f69e952 | 2009-01-01 13:02:07 -0300 | [diff] [blame] | 305 | ret = usb_register(&sd_driver); |
| 306 | if (ret < 0) |
Alexey Klimov | e6b1484 | 2009-01-01 13:04:58 -0300 | [diff] [blame] | 307 | return ret; |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 308 | PDEBUG(D_PROBE, "registered"); |
| 309 | return 0; |
| 310 | } |
Jean-Francois Moine | c0c4c89 | 2009-03-26 05:06:50 -0300 | [diff] [blame] | 311 | |
Frank Zago | 9707685 | 2008-09-29 06:59:36 -0300 | [diff] [blame] | 312 | static void __exit sd_mod_exit(void) |
| 313 | { |
| 314 | usb_deregister(&sd_driver); |
| 315 | PDEBUG(D_PROBE, "deregistered"); |
| 316 | } |
| 317 | |
| 318 | module_init(sd_mod_init); |
| 319 | module_exit(sd_mod_exit); |