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