finepix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. break;
  119. }
  120. if (urb->actual_length < FPIX_MAX_TRANSFER ||
  121. (data[urb->actual_length-2] == 0xff &&
  122. data[urb->actual_length-1] == 0xd9)) {
  123. /* If the result is less than what was asked
  124. * for, then it's the end of the
  125. * frame. Sometime the jpeg is not complete,
  126. * but there's nothing we can do. We also end
  127. * here if the the jpeg ends right at the end
  128. * of the frame. */
  129. gspca_frame_add(gspca_dev, LAST_PACKET,
  130. frame,
  131. data, urb->actual_length);
  132. dev_new_state(FPIX_REQ_FRAME);
  133. schedule_delayed_work(&dev->wqe, NEXT_FRAME_DELAY);
  134. } else {
  135. /* got a partial image */
  136. gspca_frame_add(gspca_dev,
  137. gspca_dev->last_packet_type == LAST_PACKET
  138. ? FIRST_PACKET : INTER_PACKET,
  139. frame,
  140. data, urb->actual_length);
  141. read_frame_part(dev);
  142. }
  143. break;
  144. }
  145. case FPIX_NOP:
  146. case FPIX_RESET:
  147. PDEBUG(D_STREAM, "invalid state %d", dev->state);
  148. break;
  149. }
  150. }
  151. /* Request a new frame */
  152. static void request_frame(struct usb_fpix *dev)
  153. {
  154. int ret;
  155. struct gspca_dev *gspca_dev = &dev->gspca_dev;
  156. /* Setup command packet */
  157. memset(gspca_dev->usb_buf, 0, 12);
  158. gspca_dev->usb_buf[0] = 0xd3;
  159. gspca_dev->usb_buf[7] = 0x01;
  160. /* Request a frame */
  161. dev->ctrlreq.bRequestType =
  162. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE;
  163. dev->ctrlreq.bRequest = USB_REQ_GET_STATUS;
  164. dev->ctrlreq.wValue = 0;
  165. dev->ctrlreq.wIndex = 0;
  166. dev->ctrlreq.wLength = cpu_to_le16(12);
  167. usb_fill_control_urb(dev->control_urb,
  168. gspca_dev->dev,
  169. usb_sndctrlpipe(gspca_dev->dev, 0),
  170. (unsigned char *) &dev->ctrlreq,
  171. gspca_dev->usb_buf,
  172. 12, urb_callback, gspca_dev);
  173. ret = usb_submit_urb(dev->control_urb, GFP_ATOMIC);
  174. if (ret) {
  175. dev_new_state(FPIX_RESET);
  176. schedule_delayed_work(&dev->wqe, 1);
  177. PDEBUG(D_STREAM, "usb_submit_urb failed with %d", ret);
  178. }
  179. }
  180. /*--------------------------------------------------------------------------*/
  181. /* State machine. */
  182. static void fpix_sm(struct work_struct *work)
  183. {
  184. struct usb_fpix *dev = container_of(work, struct usb_fpix, wqe.work);
  185. PDEBUG(D_STREAM, "fpix_sm state %d", dev->state);
  186. /* verify that the device wasn't unplugged */
  187. if (!dev->gspca_dev.present) {
  188. PDEBUG(D_STREAM, "device is gone");
  189. dev_new_state(FPIX_NOP);
  190. complete(&dev->can_close);
  191. return;
  192. }
  193. if (!dev->streaming) {
  194. PDEBUG(D_STREAM, "stopping state machine");
  195. dev_new_state(FPIX_NOP);
  196. complete(&dev->can_close);
  197. return;
  198. }
  199. switch (dev->state) {
  200. case FPIX_RESET:
  201. dev_new_state(FPIX_REQ_FRAME);
  202. schedule_delayed_work(&dev->wqe, HZ / 10);
  203. break;
  204. case FPIX_REQ_FRAME:
  205. /* get an image */
  206. request_frame(dev);
  207. break;
  208. case FPIX_NOP:
  209. case FPIX_READ_FRAME:
  210. PDEBUG(D_STREAM, "invalid state %d", dev->state);
  211. break;
  212. }
  213. }
  214. /* this function is called at probe time */
  215. static int sd_config(struct gspca_dev *gspca_dev,
  216. const struct usb_device_id *id)
  217. {
  218. struct cam *cam = &gspca_dev->cam;
  219. cam->cam_mode = fpix_mode;
  220. cam->nmodes = 1;
  221. cam->epaddr = 0x01; /* todo: correct for all cams? */
  222. cam->bulk_size = FPIX_MAX_TRANSFER;
  223. /* gspca_dev->nbalt = 1; * use bulk transfer */
  224. return 0;
  225. }
  226. /* Stop streaming and free the ressources allocated by sd_start. */
  227. static void sd_stopN(struct gspca_dev *gspca_dev)
  228. {
  229. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  230. dev->streaming = 0;
  231. /* Stop the state machine */
  232. if (dev->state != FPIX_NOP)
  233. wait_for_completion(&dev->can_close);
  234. usb_free_urb(dev->control_urb);
  235. dev->control_urb = NULL;
  236. }
  237. /* Kill an URB that hasn't completed. */
  238. static void timeout_kill(unsigned long data)
  239. {
  240. struct urb *urb = (struct urb *) data;
  241. usb_unlink_urb(urb);
  242. }
  243. /* this function is called at probe and resume time */
  244. static int sd_init(struct gspca_dev *gspca_dev)
  245. {
  246. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  247. INIT_DELAYED_WORK(&dev->wqe, fpix_sm);
  248. init_timer(&dev->bulk_timer);
  249. dev->bulk_timer.function = timeout_kill;
  250. return 0;
  251. }
  252. static int sd_start(struct gspca_dev *gspca_dev)
  253. {
  254. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  255. int ret;
  256. int size_ret;
  257. /* Reset bulk in endpoint */
  258. usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr);
  259. /* Init the device */
  260. memset(gspca_dev->usb_buf, 0, 12);
  261. gspca_dev->usb_buf[0] = 0xc6;
  262. gspca_dev->usb_buf[8] = 0x20;
  263. ret = usb_control_msg(gspca_dev->dev,
  264. usb_sndctrlpipe(gspca_dev->dev, 0),
  265. USB_REQ_GET_STATUS,
  266. USB_DIR_OUT | USB_TYPE_CLASS |
  267. USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
  268. 12, FPIX_TIMEOUT);
  269. if (ret != 12) {
  270. PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);
  271. ret = -EIO;
  272. goto error;
  273. }
  274. /* Read the result of the command. Ignore the result, for it
  275. * varies with the device. */
  276. ret = usb_bulk_msg(gspca_dev->dev,
  277. usb_rcvbulkpipe(gspca_dev->dev,
  278. gspca_dev->cam.epaddr),
  279. gspca_dev->usb_buf, FPIX_MAX_TRANSFER, &size_ret,
  280. FPIX_TIMEOUT);
  281. if (ret != 0) {
  282. PDEBUG(D_STREAM, "usb_bulk_msg failed (%d)", ret);
  283. ret = -EIO;
  284. goto error;
  285. }
  286. /* Request a frame, but don't read it */
  287. memset(gspca_dev->usb_buf, 0, 12);
  288. gspca_dev->usb_buf[0] = 0xd3;
  289. gspca_dev->usb_buf[7] = 0x01;
  290. ret = usb_control_msg(gspca_dev->dev,
  291. usb_sndctrlpipe(gspca_dev->dev, 0),
  292. USB_REQ_GET_STATUS,
  293. USB_DIR_OUT | USB_TYPE_CLASS |
  294. USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
  295. 12, FPIX_TIMEOUT);
  296. if (ret != 12) {
  297. PDEBUG(D_STREAM, "usb_control_msg failed (%d)", ret);
  298. ret = -EIO;
  299. goto error;
  300. }
  301. /* Again, reset bulk in endpoint */
  302. usb_clear_halt(gspca_dev->dev, gspca_dev->cam.epaddr);
  303. /* Allocate a control URB */
  304. dev->control_urb = usb_alloc_urb(0, GFP_KERNEL);
  305. if (!dev->control_urb) {
  306. PDEBUG(D_STREAM, "No free urbs available");
  307. ret = -EIO;
  308. goto error;
  309. }
  310. /* Various initializations. */
  311. init_completion(&dev->can_close);
  312. dev->bulk_timer.data = (unsigned long)dev->gspca_dev.urb[0];
  313. dev->gspca_dev.urb[0]->complete = urb_callback;
  314. dev->streaming = 1;
  315. /* Schedule a frame request. */
  316. dev_new_state(FPIX_REQ_FRAME);
  317. schedule_delayed_work(&dev->wqe, 1);
  318. return 0;
  319. error:
  320. /* Free the ressources */
  321. sd_stopN(gspca_dev);
  322. return ret;
  323. }
  324. /* Table of supported USB devices */
  325. static const __devinitdata struct usb_device_id device_table[] = {
  326. {USB_DEVICE(0x04cb, 0x0104)},
  327. {USB_DEVICE(0x04cb, 0x0109)},
  328. {USB_DEVICE(0x04cb, 0x010b)},
  329. {USB_DEVICE(0x04cb, 0x010f)},
  330. {USB_DEVICE(0x04cb, 0x0111)},
  331. {USB_DEVICE(0x04cb, 0x0113)},
  332. {USB_DEVICE(0x04cb, 0x0115)},
  333. {USB_DEVICE(0x04cb, 0x0117)},
  334. {USB_DEVICE(0x04cb, 0x0119)},
  335. {USB_DEVICE(0x04cb, 0x011b)},
  336. {USB_DEVICE(0x04cb, 0x011d)},
  337. {USB_DEVICE(0x04cb, 0x0121)},
  338. {USB_DEVICE(0x04cb, 0x0123)},
  339. {USB_DEVICE(0x04cb, 0x0125)},
  340. {USB_DEVICE(0x04cb, 0x0127)},
  341. {USB_DEVICE(0x04cb, 0x0129)},
  342. {USB_DEVICE(0x04cb, 0x012b)},
  343. {USB_DEVICE(0x04cb, 0x012d)},
  344. {USB_DEVICE(0x04cb, 0x012f)},
  345. {USB_DEVICE(0x04cb, 0x0131)},
  346. {USB_DEVICE(0x04cb, 0x013b)},
  347. {USB_DEVICE(0x04cb, 0x013d)},
  348. {USB_DEVICE(0x04cb, 0x013f)},
  349. {}
  350. };
  351. MODULE_DEVICE_TABLE(usb, device_table);
  352. /* sub-driver description */
  353. static const struct sd_desc sd_desc = {
  354. .name = MODULE_NAME,
  355. .config = sd_config,
  356. .init = sd_init,
  357. .start = sd_start,
  358. .stopN = sd_stopN,
  359. };
  360. /* -- device connect -- */
  361. static int sd_probe(struct usb_interface *intf,
  362. const struct usb_device_id *id)
  363. {
  364. return gspca_dev_probe(intf, id,
  365. &sd_desc,
  366. sizeof(struct usb_fpix),
  367. THIS_MODULE);
  368. }
  369. static struct usb_driver sd_driver = {
  370. .name = MODULE_NAME,
  371. .id_table = device_table,
  372. .probe = sd_probe,
  373. .disconnect = gspca_disconnect,
  374. #ifdef CONFIG_PM
  375. .suspend = gspca_suspend,
  376. .resume = gspca_resume,
  377. #endif
  378. };
  379. /* -- module insert / remove -- */
  380. static int __init sd_mod_init(void)
  381. {
  382. if (usb_register(&sd_driver) < 0)
  383. return -1;
  384. PDEBUG(D_PROBE, "registered");
  385. return 0;
  386. }
  387. static void __exit sd_mod_exit(void)
  388. {
  389. usb_deregister(&sd_driver);
  390. PDEBUG(D_PROBE, "deregistered");
  391. }
  392. module_init(sd_mod_init);
  393. module_exit(sd_mod_exit);