finepix.c 12 KB

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