finepix.c 7.9 KB

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