···11+/*22+ * Fujifilm Finepix subdriver33+ *44+ * Copyright (C) 2008 Frank Zago55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License as published by88+ * the Free Software Foundation; either version 2 of the License, or99+ * any later version.1010+ *1111+ * This program is distributed in the hope that it will be useful,1212+ * but WITHOUT ANY WARRANTY; without even the implied warranty of1313+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the1414+ * GNU General Public License for more details.1515+ *1616+ * You should have received a copy of the GNU General Public License1717+ * along with this program; if not, write to the Free Software1818+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA1919+ */2020+2121+#define MODULE_NAME "finepix"2222+2323+#include "gspca.h"2424+2525+MODULE_AUTHOR("Frank Zago <frank@zago.net>");2626+MODULE_DESCRIPTION("Fujifilm FinePix USB V4L2 driver");2727+MODULE_LICENSE("GPL");2828+2929+/* Default timeout, in ms */3030+#define FPIX_TIMEOUT (HZ / 10)3131+3232+/* Maximum transfer size to use. The windows driver reads by chunks of3333+ * 0x2000 bytes, so do the same. Note: reading more seems to work3434+ * too. */3535+#define FPIX_MAX_TRANSFER 0x20003636+3737+/* Structure to hold all of our device specific stuff */3838+struct usb_fpix {3939+ struct gspca_dev gspca_dev; /* !! must be the first item */4040+4141+ /*4242+ * USB stuff4343+ */4444+ struct usb_ctrlrequest ctrlreq;4545+ struct urb *control_urb;4646+ struct timer_list bulk_timer;4747+4848+ enum {4949+ FPIX_NOP, /* inactive, else streaming */5050+ FPIX_RESET, /* must reset */5151+ FPIX_REQ_FRAME, /* requesting a frame */5252+ FPIX_READ_FRAME, /* reading frame */5353+ } state;5454+5555+ /*5656+ * Driver stuff5757+ */5858+ struct delayed_work wqe;5959+ struct completion can_close;6060+ int streaming;6161+};6262+6363+/* Delay after which claim the next frame. If the delay is too small,6464+ * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms6565+ * will fail every 4 or 5 frames, but 30ms is perfect. */6666+#define NEXT_FRAME_DELAY (((HZ * 30) + 999) / 1000)6767+6868+#define dev_new_state(new_state) { \6969+ PDEBUG(D_STREAM, "new state from %d to %d at %s:%d", \7070+ dev->state, new_state, __func__, __LINE__); \7171+ dev->state = new_state; \7272+}7373+7474+/* These cameras only support 320x200. */7575+static struct v4l2_pix_format fpix_mode[1] = {7676+ { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,7777+ .bytesperline = 320,7878+ .sizeimage = 320 * 240 * 3 / 8 + 590,7979+ .colorspace = V4L2_COLORSPACE_SRGB,8080+ .priv = 0}8181+};8282+8383+/* Reads part of a frame */8484+static void read_frame_part(struct usb_fpix *dev)8585+{8686+ int ret;8787+8888+ PDEBUG(D_STREAM, "read_frame_part");8989+9090+ /* Reads part of a frame */9191+ ret = usb_submit_urb(dev->gspca_dev.urb[0], GFP_ATOMIC);9292+ if (ret) {9393+ dev_new_state(FPIX_RESET);9494+ schedule_delayed_work(&dev->wqe, 1);9595+ PDEBUG(D_STREAM, "usb_submit_urb failed with %d",9696+ ret);9797+ } else {9898+ /* Sometimes we never get a callback, so use a timer.9999+ * Is this masking a bug somewhere else? */100100+ dev->bulk_timer.expires = jiffies + msecs_to_jiffies(150);101101+ add_timer(&dev->bulk_timer);102102+ }103103+}104104+105105+/* Callback for URBs. */106106+static void urb_callback(struct urb *urb)107107+{108108+ struct gspca_dev *gspca_dev = urb->context;109109+ struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;110110+111111+ PDEBUG(D_PACK,112112+ "enter urb_callback - status=%d, length=%d",113113+ urb->status, urb->actual_length);114114+115115+ if (dev->state == FPIX_READ_FRAME)116116+ del_timer(&dev->bulk_timer);117117+118118+ if (urb->status != 0) {119119+ /* We kill a stuck urb every 50 frames on average, so don't120120+ * display a log message for that. */121121+ if (urb->status != -ECONNRESET)122122+ PDEBUG(D_STREAM, "bad URB status %d", urb->status);123123+ dev_new_state(FPIX_RESET);124124+ schedule_delayed_work(&dev->wqe, 1);125125+ }126126+127127+ switch (dev->state) {128128+ case FPIX_REQ_FRAME:129129+ dev_new_state(FPIX_READ_FRAME);130130+ read_frame_part(dev);131131+ break;132132+133133+ case FPIX_READ_FRAME: {134134+ unsigned char *data = urb->transfer_buffer;135135+ struct gspca_frame *frame;136136+137137+ frame = gspca_get_i_frame(&dev->gspca_dev);138138+ if (frame == NULL) {139139+ gspca_dev->last_packet_type = DISCARD_PACKET;140140+ break;141141+ }142142+ if (urb->actual_length < FPIX_MAX_TRANSFER ||143143+ (data[urb->actual_length-2] == 0xff &&144144+ data[urb->actual_length-1] == 0xd9)) {145145+146146+ /* If the result is less than what was asked147147+ * for, then it's the end of the148148+ * frame. Sometime the jpeg is not complete,149149+ * but there's nothing we can do. We also end150150+ * here if the the jpeg ends right at the end151151+ * of the frame. */152152+ gspca_frame_add(gspca_dev, LAST_PACKET,153153+ frame,154154+ data, urb->actual_length);155155+ dev_new_state(FPIX_REQ_FRAME);156156+ schedule_delayed_work(&dev->wqe, NEXT_FRAME_DELAY);157157+ } else {158158+159159+ /* got a partial image */160160+ gspca_frame_add(gspca_dev,161161+ gspca_dev->last_packet_type == LAST_PACKET162162+ ? FIRST_PACKET : INTER_PACKET,163163+ frame,164164+ data, urb->actual_length);165165+ read_frame_part(dev);166166+ }167167+ break;168168+ }169169+170170+ case FPIX_NOP:171171+ case FPIX_RESET:172172+ PDEBUG(D_STREAM, "invalid state %d", dev->state);173173+ break;174174+ }175175+}176176+177177+/* Request a new frame */178178+static void request_frame(struct usb_fpix *dev)179179+{180180+ int ret;181181+ struct gspca_dev *gspca_dev = &dev->gspca_dev;182182+183183+ /* Setup command packet */184184+ memset(gspca_dev->usb_buf, 0, 12);185185+ gspca_dev->usb_buf[0] = 0xd3;186186+ gspca_dev->usb_buf[7] = 0x01;187187+188188+ /* Request a frame */189189+ dev->ctrlreq.bRequestType =190190+ USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;191191+ dev->ctrlreq.bRequest = USB_REQ_GET_STATUS;192192+ dev->ctrlreq.wValue = 0;193193+ dev->ctrlreq.wIndex = 0;194194+ dev->ctrlreq.wLength = cpu_to_le16(12);195195+196196+ usb_fill_control_urb(dev->control_urb,197197+ gspca_dev->dev,198198+ usb_sndctrlpipe(gspca_dev->dev, 0),199199+ (unsigned char *) &dev->ctrlreq,200200+ gspca_dev->usb_buf,201201+ 12, urb_callback, gspca_dev);202202+203203+ ret = usb_submit_urb(dev->control_urb, GFP_ATOMIC);204204+ if (ret) {205205+ dev_new_state(FPIX_RESET);206206+ schedule_delayed_work(&dev->wqe, 1);207207+ PDEBUG(D_STREAM, "usb_submit_urb failed with %d", ret);208208+ }209209+}210210+211211+/*--------------------------------------------------------------------------*/212212+213213+/* State machine. */214214+static void fpix_sm(struct work_struct *work)215215+{216216+ struct usb_fpix *dev = container_of(work, struct usb_fpix, wqe.work);217217+218218+ PDEBUG(D_STREAM, "fpix_sm state %d", dev->state);219219+220220+ /* verify that the device wasn't unplugged */221221+ if (!dev->gspca_dev.present) {222222+ PDEBUG(D_STREAM, "device is gone");223223+ dev_new_state(FPIX_NOP);224224+ complete(&dev->can_close);225225+ return;226226+ }227227+228228+ if (!dev->streaming) {229229+ PDEBUG(D_STREAM, "stopping state machine");230230+ dev_new_state(FPIX_NOP);231231+ complete(&dev->can_close);232232+ return;233233+ }234234+235235+ switch (dev->state) {236236+ case FPIX_RESET:237237+ dev_new_state(FPIX_REQ_FRAME);238238+ schedule_delayed_work(&dev->wqe, HZ / 10);239239+ break;240240+241241+ case FPIX_REQ_FRAME:242242+ /* get an image */243243+ request_frame(dev);244244+ break;245245+246246+ case FPIX_NOP:247247+ case FPIX_READ_FRAME:248248+ PDEBUG(D_STREAM, "invalid state %d", dev->state);249249+ break;250250+ }251251+}252252+253253+/* this function is called at probe time */254254+static int sd_config(struct gspca_dev *gspca_dev,255255+ const struct usb_device_id *id)256256+{257257+ struct cam *cam = &gspca_dev->cam;258258+259259+ cam->cam_mode = fpix_mode;260260+ cam->nmodes = 1;261261+ cam->epaddr = 0x01; /* todo: correct for all cams? */262262+ cam->bulk_size = FPIX_MAX_TRANSFER;263263+264264+/* gspca_dev->nbalt = 1; * use bulk transfer */265265+ return 0;266266+}267267+268268+/* Stop streaming and free the ressources allocated by sd_start. */269269+static void sd_stopN(struct gspca_dev *gspca_dev)270270+{271271+ struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;272272+273273+ dev->streaming = 0;274274+275275+ /* Stop the state machine */276276+ if (dev->state != FPIX_NOP)277277+ wait_for_completion(&dev->can_close);278278+279279+ usb_free_urb(dev->control_urb);280280+ dev->control_urb = NULL;281281+}282282+283283+/* Kill an URB that hasn't completed. */284284+static void timeout_kill(unsigned long data)285285+{286286+ struct urb *urb = (struct urb *) data;287287+288288+ usb_unlink_urb(urb);289289+}290290+291291+/* this function is called at probe and resume time */292292+static int sd_init(struct gspca_dev *gspca_dev)293293+{294294+ struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;295295+296296+ INIT_DELAYED_WORK(&dev->wqe, fpix_sm);297297+298298+ init_timer(&dev->bulk_timer);299299+ dev->bulk_timer.function = timeout_kill;300300+301301+ return 0;302302+}303303+304304+static int sd_start(struct gspca_dev *gspca_dev)305305+{306306+ struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;307307+ int ret;308308+ int size_ret;309309+310310+ /* Reset bulk in endpoint */311311+ usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr);312312+313313+ /* Init the device */314314+ memset(gspca_dev->usb_buf, 0, 12);315315+ gspca_dev->usb_buf[0] = 0xc6;316316+ gspca_dev->usb_buf[8] = 0x20;317317+318318+ ret = usb_control_msg(gspca_dev->dev,319319+ usb_sndctrlpipe(gspca_dev->dev, 0),320320+ USB_REQ_GET_STATUS,321321+ USB_DIR_OUT | USB_TYPE_CLASS |322322+ USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,323323+ 12, FPIX_TIMEOUT);324324+325325+ if (ret != 12) {326326+ PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);327327+ ret = -EIO;328328+ goto error;329329+ }330330+331331+ /* Read the result of the command. Ignore the result, for it332332+ * varies with the device. */333333+ ret = usb_bulk_msg(gspca_dev->dev,334334+ usb_rcvbulkpipe(gspca_dev->dev,335335+ gspca_dev->cam.epaddr),336336+ gspca_dev->usb_buf, FPIX_MAX_TRANSFER, &size_ret,337337+ FPIX_TIMEOUT);338338+ if (ret != 0) {339339+ PDEBUG(D_STREAM, "usb_bulk_msg failed (%d)", ret);340340+ ret = -EIO;341341+ goto error;342342+ }343343+344344+ /* Request a frame, but don't read it */345345+ memset(gspca_dev->usb_buf, 0, 12);346346+ gspca_dev->usb_buf[0] = 0xd3;347347+ gspca_dev->usb_buf[7] = 0x01;348348+349349+ ret = usb_control_msg(gspca_dev->dev,350350+ usb_sndctrlpipe(gspca_dev->dev, 0),351351+ USB_REQ_GET_STATUS,352352+ USB_DIR_OUT | USB_TYPE_CLASS |353353+ USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,354354+ 12, FPIX_TIMEOUT);355355+ if (ret != 12) {356356+ PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);357357+ ret = -EIO;358358+ goto error;359359+ }360360+361361+ /* Again, reset bulk in endpoint */362362+ usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr);363363+364364+ /* Allocate a control URB */365365+ dev->control_urb = usb_alloc_urb(0, GFP_KERNEL);366366+ if (!dev->control_urb) {367367+ PDEBUG(D_STREAM, "No free urbs available");368368+ ret = -EIO;369369+ goto error;370370+ }371371+372372+ /* Various initializations. */373373+ init_completion(&dev->can_close);374374+ dev->bulk_timer.data = (unsigned long)dev->gspca_dev.urb[0];375375+ dev->gspca_dev.urb[0]->complete = urb_callback;376376+ dev->streaming = 1;377377+378378+ /* Schedule a frame request. */379379+ dev_new_state(FPIX_REQ_FRAME);380380+ schedule_delayed_work(&dev->wqe, 1);381381+382382+ return 0;383383+384384+error:385385+ /* Free the ressources */386386+ sd_stopN(gspca_dev);387387+ return ret;388388+}389389+390390+/* Table of supported USB devices */391391+static const __devinitdata struct usb_device_id device_table[] = {392392+ {USB_DEVICE(0x04cb, 0x0104)},393393+ {USB_DEVICE(0x04cb, 0x0109)},394394+ {USB_DEVICE(0x04cb, 0x010b)},395395+ {USB_DEVICE(0x04cb, 0x010f)},396396+ {USB_DEVICE(0x04cb, 0x0111)},397397+ {USB_DEVICE(0x04cb, 0x0113)},398398+ {USB_DEVICE(0x04cb, 0x0115)},399399+ {USB_DEVICE(0x04cb, 0x0117)},400400+ {USB_DEVICE(0x04cb, 0x0119)},401401+ {USB_DEVICE(0x04cb, 0x011b)},402402+ {USB_DEVICE(0x04cb, 0x011d)},403403+ {USB_DEVICE(0x04cb, 0x0121)},404404+ {USB_DEVICE(0x04cb, 0x0123)},405405+ {USB_DEVICE(0x04cb, 0x0125)},406406+ {USB_DEVICE(0x04cb, 0x0127)},407407+ {USB_DEVICE(0x04cb, 0x0129)},408408+ {USB_DEVICE(0x04cb, 0x012b)},409409+ {USB_DEVICE(0x04cb, 0x012d)},410410+ {USB_DEVICE(0x04cb, 0x012f)},411411+ {USB_DEVICE(0x04cb, 0x0131)},412412+ {USB_DEVICE(0x04cb, 0x013b)},413413+ {USB_DEVICE(0x04cb, 0x013d)},414414+ {USB_DEVICE(0x04cb, 0x013f)},415415+ {}416416+};417417+418418+MODULE_DEVICE_TABLE(usb, device_table);419419+420420+/* sub-driver description */421421+static const struct sd_desc sd_desc = {422422+ .name = MODULE_NAME,423423+ .config = sd_config,424424+ .init = sd_init,425425+ .start = sd_start,426426+ .stopN = sd_stopN,427427+};428428+429429+/* -- device connect -- */430430+static int sd_probe(struct usb_interface *intf,431431+ const struct usb_device_id *id)432432+{433433+ return gspca_dev_probe(intf, id,434434+ &sd_desc,435435+ sizeof(struct usb_fpix),436436+ THIS_MODULE);437437+}438438+439439+static struct usb_driver sd_driver = {440440+ .name = MODULE_NAME,441441+ .id_table = device_table,442442+ .probe = sd_probe,443443+ .disconnect = gspca_disconnect,444444+#ifdef CONFIG_PM445445+ .suspend = gspca_suspend,446446+ .resume = gspca_resume,447447+#endif448448+};449449+450450+/* -- module insert / remove -- */451451+static int __init sd_mod_init(void)452452+{453453+ if (usb_register(&sd_driver) < 0)454454+ return -1;455455+ PDEBUG(D_PROBE, "registered");456456+ return 0;457457+}458458+static void __exit sd_mod_exit(void)459459+{460460+ usb_deregister(&sd_driver);461461+ PDEBUG(D_PROBE, "deregistered");462462+}463463+464464+module_init(sd_mod_init);465465+module_exit(sd_mod_exit);