finepix.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. };
  37. /* Delay after which claim the next frame. If the delay is too small,
  38. * the camera will return old frames. On the 4800Z, 20ms is bad, 25ms
  39. * will fail every 4 or 5 frames, but 30ms is perfect. On the A210,
  40. * 30ms is bad while 35ms is perfect. */
  41. #define NEXT_FRAME_DELAY 35
  42. /* These cameras only support 320x200. */
  43. static const struct v4l2_pix_format fpix_mode[1] = {
  44. { 320, 240, V4L2_PIX_FMT_JPEG, V4L2_FIELD_NONE,
  45. .bytesperline = 320,
  46. .sizeimage = 320 * 240 * 3 / 8 + 590,
  47. .colorspace = V4L2_COLORSPACE_SRGB,
  48. .priv = 0}
  49. };
  50. /* send a command to the webcam */
  51. static int command(struct gspca_dev *gspca_dev,
  52. int order) /* 0: reset, 1: frame request */
  53. {
  54. static u8 order_values[2][12] = {
  55. {0xc6, 0, 0, 0, 0, 0, 0, 0, 0x20, 0, 0, 0}, /* reset */
  56. {0xd3, 0, 0, 0, 0, 0, 0, 0x01, 0, 0, 0, 0}, /* fr req */
  57. };
  58. memcpy(gspca_dev->usb_buf, order_values[order], 12);
  59. return usb_control_msg(gspca_dev->dev,
  60. usb_sndctrlpipe(gspca_dev->dev, 0),
  61. USB_REQ_GET_STATUS,
  62. USB_DIR_OUT | USB_TYPE_CLASS |
  63. USB_RECIP_INTERFACE, 0, 0, gspca_dev->usb_buf,
  64. 12, FPIX_TIMEOUT);
  65. }
  66. /*
  67. * This function is called as a workqueue function and runs whenever the camera
  68. * is streaming data. Because it is a workqueue function it is allowed to sleep
  69. * so we can use synchronous USB calls. To avoid possible collisions with other
  70. * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
  71. * performing USB operations using it. In practice we don't really need this
  72. * as the camera doesn't provide any controls.
  73. */
  74. static void dostream(struct work_struct *work)
  75. {
  76. struct usb_fpix *dev = container_of(work, struct usb_fpix, work_struct);
  77. struct gspca_dev *gspca_dev = &dev->gspca_dev;
  78. struct urb *urb = gspca_dev->urb[0];
  79. u8 *data = urb->transfer_buffer;
  80. int ret = 0;
  81. int len;
  82. PDEBUG(D_STREAM, "dostream started");
  83. /* loop reading a frame */
  84. again:
  85. while (gspca_dev->present && gspca_dev->streaming) {
  86. #ifdef CONFIG_PM
  87. if (gspca_dev->frozen)
  88. break;
  89. #endif
  90. /* request a frame */
  91. mutex_lock(&gspca_dev->usb_lock);
  92. ret = command(gspca_dev, 1);
  93. mutex_unlock(&gspca_dev->usb_lock);
  94. if (ret < 0)
  95. break;
  96. #ifdef CONFIG_PM
  97. if (gspca_dev->frozen)
  98. break;
  99. #endif
  100. if (!gspca_dev->present || !gspca_dev->streaming)
  101. break;
  102. /* the frame comes in parts */
  103. for (;;) {
  104. ret = usb_bulk_msg(gspca_dev->dev,
  105. urb->pipe,
  106. data,
  107. FPIX_MAX_TRANSFER,
  108. &len, FPIX_TIMEOUT);
  109. if (ret < 0) {
  110. /* Most of the time we get a timeout
  111. * error. Just restart. */
  112. goto again;
  113. }
  114. #ifdef CONFIG_PM
  115. if (gspca_dev->frozen)
  116. goto out;
  117. #endif
  118. if (!gspca_dev->present || !gspca_dev->streaming)
  119. goto out;
  120. if (len < FPIX_MAX_TRANSFER ||
  121. (data[len - 2] == 0xff &&
  122. data[len - 1] == 0xd9)) {
  123. /* If the result is less than what was asked
  124. * for, then it's the end of the
  125. * frame. Sometimes 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. data, len);
  131. break;
  132. }
  133. /* got a partial image */
  134. gspca_frame_add(gspca_dev,
  135. gspca_dev->last_packet_type
  136. == LAST_PACKET
  137. ? FIRST_PACKET : INTER_PACKET,
  138. data, len);
  139. }
  140. /* We must wait before trying reading the next
  141. * frame. If we don't, or if the delay is too short,
  142. * the camera will disconnect. */
  143. msleep(NEXT_FRAME_DELAY);
  144. }
  145. out:
  146. PDEBUG(D_STREAM, "dostream stopped");
  147. }
  148. /* this function is called at probe time */
  149. static int sd_config(struct gspca_dev *gspca_dev,
  150. const struct usb_device_id *id)
  151. {
  152. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  153. struct cam *cam = &gspca_dev->cam;
  154. cam->cam_mode = fpix_mode;
  155. cam->nmodes = 1;
  156. cam->bulk = 1;
  157. cam->bulk_size = FPIX_MAX_TRANSFER;
  158. INIT_WORK(&dev->work_struct, dostream);
  159. return 0;
  160. }
  161. /* this function is called at probe and resume time */
  162. static int sd_init(struct gspca_dev *gspca_dev)
  163. {
  164. return 0;
  165. }
  166. /* start the camera */
  167. static int sd_start(struct gspca_dev *gspca_dev)
  168. {
  169. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  170. int ret, len;
  171. /* Init the device */
  172. ret = command(gspca_dev, 0);
  173. if (ret < 0) {
  174. pr_err("init failed %d\n", ret);
  175. return ret;
  176. }
  177. /* Read the result of the command. Ignore the result, for it
  178. * varies with the device. */
  179. ret = usb_bulk_msg(gspca_dev->dev,
  180. gspca_dev->urb[0]->pipe,
  181. gspca_dev->urb[0]->transfer_buffer,
  182. FPIX_MAX_TRANSFER, &len,
  183. FPIX_TIMEOUT);
  184. if (ret < 0) {
  185. pr_err("usb_bulk_msg failed %d\n", ret);
  186. return ret;
  187. }
  188. /* Request a frame, but don't read it */
  189. ret = command(gspca_dev, 1);
  190. if (ret < 0) {
  191. pr_err("frame request failed %d\n", ret);
  192. return ret;
  193. }
  194. /* Again, reset bulk in endpoint */
  195. usb_clear_halt(gspca_dev->dev, gspca_dev->urb[0]->pipe);
  196. schedule_work(&dev->work_struct);
  197. return 0;
  198. }
  199. /* called on streamoff with alt==0 and on disconnect */
  200. /* the usb_lock is held at entry - restore on exit */
  201. static void sd_stop0(struct gspca_dev *gspca_dev)
  202. {
  203. struct usb_fpix *dev = (struct usb_fpix *) gspca_dev;
  204. /* wait for the work queue to terminate */
  205. mutex_unlock(&gspca_dev->usb_lock);
  206. flush_work(&dev->work_struct);
  207. mutex_lock(&gspca_dev->usb_lock);
  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);