finepix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 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. usb_free_urb(dev->control_urb);
  236. dev->control_urb = NULL;
  237. }
  238. /* Kill an URB that hasn't completed. */
  239. static void timeout_kill(unsigned long data)
  240. {
  241. struct urb *urb = (struct urb *) data;
  242. usb_unlink_urb(urb);
  243. }
  244. /* this function is called at probe and resume time */
  245. static int sd_init(struct gspca_dev *gspca_dev)
  246. {
  247. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  248. INIT_DELAYED_WORK(&dev->wqe, fpix_sm);
  249. init_timer(&dev->bulk_timer);
  250. dev->bulk_timer.function = timeout_kill;
  251. return 0;
  252. }
  253. static int sd_start(struct gspca_dev *gspca_dev)
  254. {
  255. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  256. int ret;
  257. int size_ret;
  258. /* Reset bulk in endpoint */
  259. usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr);
  260. /* Init the device */
  261. memset(gspca_dev->usb_buf, 0, 12);
  262. gspca_dev->usb_buf[0] = 0xc6;
  263. gspca_dev->usb_buf[8] = 0x20;
  264. ret = usb_control_msg(gspca_dev->dev,
  265. usb_sndctrlpipe(gspca_dev->dev, 0),
  266. USB_REQ_GET_STATUS,
  267. USB_DIR_OUT | USB_TYPE_CLASS |
  268. USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
  269. 12, FPIX_TIMEOUT);
  270. if (ret != 12) {
  271. PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);
  272. ret = -EIO;
  273. goto error;
  274. }
  275. /* Read the result of the command. Ignore the result, for it
  276. * varies with the device. */
  277. ret = usb_bulk_msg(gspca_dev->dev,
  278. usb_rcvbulkpipe(gspca_dev->dev,
  279. gspca_dev->cam.epaddr),
  280. gspca_dev->usb_buf, FPIX_MAX_TRANSFER, &size_ret,
  281. FPIX_TIMEOUT);
  282. if (ret != 0) {
  283. PDEBUG(D_STREAM, "usb_bulk_msg failed (%d)", ret);
  284. ret = -EIO;
  285. goto error;
  286. }
  287. /* Request a frame, but don't read it */
  288. memset(gspca_dev->usb_buf, 0, 12);
  289. gspca_dev->usb_buf[0] = 0xd3;
  290. gspca_dev->usb_buf[7] = 0x01;
  291. ret = usb_control_msg(gspca_dev->dev,
  292. usb_sndctrlpipe(gspca_dev->dev, 0),
  293. USB_REQ_GET_STATUS,
  294. USB_DIR_OUT | USB_TYPE_CLASS |
  295. USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
  296. 12, FPIX_TIMEOUT);
  297. if (ret != 12) {
  298. PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);
  299. ret = -EIO;
  300. goto error;
  301. }
  302. /* Again, reset bulk in endpoint */
  303. usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr);
  304. /* Allocate a control URB */
  305. dev->control_urb = usb_alloc_urb(0, GFP_KERNEL);
  306. if (!dev->control_urb) {
  307. PDEBUG(D_STREAM, "No free urbs available");
  308. ret = -EIO;
  309. goto error;
  310. }
  311. /* Various initializations. */
  312. init_completion(&dev->can_close);
  313. dev->bulk_timer.data = (unsigned long)dev->gspca_dev.urb[0];
  314. dev->gspca_dev.urb[0]->complete = urb_callback;
  315. dev->streaming = 1;
  316. /* Schedule a frame request. */
  317. dev_new_state(FPIX_REQ_FRAME);
  318. schedule_delayed_work(&dev->wqe, 1);
  319. return 0;
  320. error:
  321. /* Free the ressources */
  322. sd_stopN(gspca_dev);
  323. return ret;
  324. }
  325. /* Table of supported USB devices */
  326. static const __devinitdata struct usb_device_id device_table[] = {
  327. {USB_DEVICE(0x04cb, 0x0104)},
  328. {USB_DEVICE(0x04cb, 0x0109)},
  329. {USB_DEVICE(0x04cb, 0x010b)},
  330. {USB_DEVICE(0x04cb, 0x010f)},
  331. {USB_DEVICE(0x04cb, 0x0111)},
  332. {USB_DEVICE(0x04cb, 0x0113)},
  333. {USB_DEVICE(0x04cb, 0x0115)},
  334. {USB_DEVICE(0x04cb, 0x0117)},
  335. {USB_DEVICE(0x04cb, 0x0119)},
  336. {USB_DEVICE(0x04cb, 0x011b)},
  337. {USB_DEVICE(0x04cb, 0x011d)},
  338. {USB_DEVICE(0x04cb, 0x0121)},
  339. {USB_DEVICE(0x04cb, 0x0123)},
  340. {USB_DEVICE(0x04cb, 0x0125)},
  341. {USB_DEVICE(0x04cb, 0x0127)},
  342. {USB_DEVICE(0x04cb, 0x0129)},
  343. {USB_DEVICE(0x04cb, 0x012b)},
  344. {USB_DEVICE(0x04cb, 0x012d)},
  345. {USB_DEVICE(0x04cb, 0x012f)},
  346. {USB_DEVICE(0x04cb, 0x0131)},
  347. {USB_DEVICE(0x04cb, 0x013b)},
  348. {USB_DEVICE(0x04cb, 0x013d)},
  349. {USB_DEVICE(0x04cb, 0x013f)},
  350. {}
  351. };
  352. MODULE_DEVICE_TABLE(usb, device_table);
  353. /* sub-driver description */
  354. static const struct sd_desc sd_desc = {
  355. .name = MODULE_NAME,
  356. .config = sd_config,
  357. .init = sd_init,
  358. .start = sd_start,
  359. .stopN = sd_stopN,
  360. };
  361. /* -- device connect -- */
  362. static int sd_probe(struct usb_interface *intf,
  363. const struct usb_device_id *id)
  364. {
  365. return gspca_dev_probe(intf, id,
  366. &sd_desc,
  367. sizeof(struct usb_fpix),
  368. THIS_MODULE);
  369. }
  370. static struct usb_driver sd_driver = {
  371. .name = MODULE_NAME,
  372. .id_table = device_table,
  373. .probe = sd_probe,
  374. .disconnect = gspca_disconnect,
  375. #ifdef CONFIG_PM
  376. .suspend = gspca_suspend,
  377. .resume = gspca_resume,
  378. #endif
  379. };
  380. /* -- module insert / remove -- */
  381. static int __init sd_mod_init(void)
  382. {
  383. if (usb_register(&sd_driver) < 0)
  384. return -1;
  385. PDEBUG(D_PROBE, "registered");
  386. return 0;
  387. }
  388. static void __exit sd_mod_exit(void)
  389. {
  390. usb_deregister(&sd_driver);
  391. PDEBUG(D_PROBE, "deregistered");
  392. }
  393. module_init(sd_mod_init);
  394. module_exit(sd_mod_exit);