usbtv-video.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. * Copyright (c) 2013,2016 Lubomir Rintel
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions, and the following disclaimer,
  10. * without modification.
  11. * 2. The name of the author may not be used to endorse or promote products
  12. * derived from this software without specific prior written permission.
  13. *
  14. * Alternatively, this software may be distributed under the terms of the
  15. * GNU General Public License ("GPL").
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. * Fushicai USBTV007 Audio-Video Grabber Driver
  31. *
  32. * Product web site:
  33. * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
  34. *
  35. * Following LWN articles were very useful in construction of this driver:
  36. * Video4Linux2 API series: http://lwn.net/Articles/203924/
  37. * videobuf2 API explanation: http://lwn.net/Articles/447435/
  38. * Thanks go to Jonathan Corbet for providing this quality documentation.
  39. * He is awesome.
  40. *
  41. * No physical hardware was harmed running Windows during the
  42. * reverse-engineering activity
  43. */
  44. #include <media/v4l2-ioctl.h>
  45. #include <media/videobuf2-v4l2.h>
  46. #include "usbtv.h"
  47. static struct usbtv_norm_params norm_params[] = {
  48. {
  49. .norm = V4L2_STD_525_60,
  50. .cap_width = 720,
  51. .cap_height = 480,
  52. },
  53. {
  54. .norm = V4L2_STD_625_50,
  55. .cap_width = 720,
  56. .cap_height = 576,
  57. }
  58. };
  59. static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm)
  60. {
  61. int i, ret = 0;
  62. struct usbtv_norm_params *params = NULL;
  63. for (i = 0; i < ARRAY_SIZE(norm_params); i++) {
  64. if (norm_params[i].norm & norm) {
  65. params = &norm_params[i];
  66. break;
  67. }
  68. }
  69. if (params) {
  70. usbtv->width = params->cap_width;
  71. usbtv->height = params->cap_height;
  72. usbtv->n_chunks = usbtv->width * usbtv->height
  73. / 4 / USBTV_CHUNK;
  74. usbtv->norm = norm;
  75. } else
  76. ret = -EINVAL;
  77. return ret;
  78. }
  79. static int usbtv_select_input(struct usbtv *usbtv, int input)
  80. {
  81. int ret;
  82. static const u16 composite[][2] = {
  83. { USBTV_BASE + 0x0105, 0x0060 },
  84. { USBTV_BASE + 0x011f, 0x00f2 },
  85. { USBTV_BASE + 0x0127, 0x0060 },
  86. { USBTV_BASE + 0x00ae, 0x0010 },
  87. { USBTV_BASE + 0x0239, 0x0060 },
  88. };
  89. static const u16 svideo[][2] = {
  90. { USBTV_BASE + 0x0105, 0x0010 },
  91. { USBTV_BASE + 0x011f, 0x00ff },
  92. { USBTV_BASE + 0x0127, 0x0060 },
  93. { USBTV_BASE + 0x00ae, 0x0030 },
  94. { USBTV_BASE + 0x0239, 0x0060 },
  95. };
  96. switch (input) {
  97. case USBTV_COMPOSITE_INPUT:
  98. ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite));
  99. break;
  100. case USBTV_SVIDEO_INPUT:
  101. ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo));
  102. break;
  103. default:
  104. ret = -EINVAL;
  105. }
  106. if (!ret)
  107. usbtv->input = input;
  108. return ret;
  109. }
  110. static uint16_t usbtv_norm_to_16f_reg(v4l2_std_id norm)
  111. {
  112. /* NTSC M/M-JP/M-KR */
  113. if (norm & V4L2_STD_NTSC)
  114. return 0x00b8;
  115. /* PAL BG/DK/H/I */
  116. if (norm & V4L2_STD_PAL)
  117. return 0x00ee;
  118. /* SECAM B/D/G/H/K/K1/L/Lc */
  119. if (norm & V4L2_STD_SECAM)
  120. return 0x00ff;
  121. if (norm & V4L2_STD_NTSC_443)
  122. return 0x00a8;
  123. if (norm & (V4L2_STD_PAL_M | V4L2_STD_PAL_60))
  124. return 0x00bc;
  125. /* Fallback to automatic detection for other standards */
  126. return 0x0000;
  127. }
  128. static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm)
  129. {
  130. int ret;
  131. /* These are the series of register values used to configure the
  132. * decoder for a specific standard.
  133. * The first 21 register writes are copied from the
  134. * Settings\DecoderDefaults registry keys present in the Windows driver
  135. * .INF file, and control various image tuning parameters (color
  136. * correction, sharpness, ...).
  137. */
  138. static const u16 pal[][2] = {
  139. /* "AVPAL" tuning sequence from .INF file */
  140. { USBTV_BASE + 0x0003, 0x0004 },
  141. { USBTV_BASE + 0x001a, 0x0068 },
  142. { USBTV_BASE + 0x0100, 0x00d3 },
  143. { USBTV_BASE + 0x010e, 0x0072 },
  144. { USBTV_BASE + 0x010f, 0x00a2 },
  145. { USBTV_BASE + 0x0112, 0x00b0 },
  146. { USBTV_BASE + 0x0115, 0x0015 },
  147. { USBTV_BASE + 0x0117, 0x0001 },
  148. { USBTV_BASE + 0x0118, 0x002c },
  149. { USBTV_BASE + 0x012d, 0x0010 },
  150. { USBTV_BASE + 0x012f, 0x0020 },
  151. { USBTV_BASE + 0x0220, 0x002e },
  152. { USBTV_BASE + 0x0225, 0x0008 },
  153. { USBTV_BASE + 0x024e, 0x0002 },
  154. { USBTV_BASE + 0x024f, 0x0002 },
  155. { USBTV_BASE + 0x0254, 0x0059 },
  156. { USBTV_BASE + 0x025a, 0x0016 },
  157. { USBTV_BASE + 0x025b, 0x0035 },
  158. { USBTV_BASE + 0x0263, 0x0017 },
  159. { USBTV_BASE + 0x0266, 0x0016 },
  160. { USBTV_BASE + 0x0267, 0x0036 },
  161. /* End image tuning */
  162. { USBTV_BASE + 0x024e, 0x0002 },
  163. { USBTV_BASE + 0x024f, 0x0002 },
  164. };
  165. static const u16 ntsc[][2] = {
  166. /* "AVNTSC" tuning sequence from .INF file */
  167. { USBTV_BASE + 0x0003, 0x0004 },
  168. { USBTV_BASE + 0x001a, 0x0079 },
  169. { USBTV_BASE + 0x0100, 0x00d3 },
  170. { USBTV_BASE + 0x010e, 0x0068 },
  171. { USBTV_BASE + 0x010f, 0x009c },
  172. { USBTV_BASE + 0x0112, 0x00f0 },
  173. { USBTV_BASE + 0x0115, 0x0015 },
  174. { USBTV_BASE + 0x0117, 0x0000 },
  175. { USBTV_BASE + 0x0118, 0x00fc },
  176. { USBTV_BASE + 0x012d, 0x0004 },
  177. { USBTV_BASE + 0x012f, 0x0008 },
  178. { USBTV_BASE + 0x0220, 0x002e },
  179. { USBTV_BASE + 0x0225, 0x0008 },
  180. { USBTV_BASE + 0x024e, 0x0002 },
  181. { USBTV_BASE + 0x024f, 0x0001 },
  182. { USBTV_BASE + 0x0254, 0x005f },
  183. { USBTV_BASE + 0x025a, 0x0012 },
  184. { USBTV_BASE + 0x025b, 0x0001 },
  185. { USBTV_BASE + 0x0263, 0x001c },
  186. { USBTV_BASE + 0x0266, 0x0011 },
  187. { USBTV_BASE + 0x0267, 0x0005 },
  188. /* End image tuning */
  189. { USBTV_BASE + 0x024e, 0x0002 },
  190. { USBTV_BASE + 0x024f, 0x0002 },
  191. };
  192. static const u16 secam[][2] = {
  193. /* "AVSECAM" tuning sequence from .INF file */
  194. { USBTV_BASE + 0x0003, 0x0004 },
  195. { USBTV_BASE + 0x001a, 0x0073 },
  196. { USBTV_BASE + 0x0100, 0x00dc },
  197. { USBTV_BASE + 0x010e, 0x0072 },
  198. { USBTV_BASE + 0x010f, 0x00a2 },
  199. { USBTV_BASE + 0x0112, 0x0090 },
  200. { USBTV_BASE + 0x0115, 0x0035 },
  201. { USBTV_BASE + 0x0117, 0x0001 },
  202. { USBTV_BASE + 0x0118, 0x0030 },
  203. { USBTV_BASE + 0x012d, 0x0004 },
  204. { USBTV_BASE + 0x012f, 0x0008 },
  205. { USBTV_BASE + 0x0220, 0x002d },
  206. { USBTV_BASE + 0x0225, 0x0028 },
  207. { USBTV_BASE + 0x024e, 0x0008 },
  208. { USBTV_BASE + 0x024f, 0x0002 },
  209. { USBTV_BASE + 0x0254, 0x0069 },
  210. { USBTV_BASE + 0x025a, 0x0016 },
  211. { USBTV_BASE + 0x025b, 0x0035 },
  212. { USBTV_BASE + 0x0263, 0x0021 },
  213. { USBTV_BASE + 0x0266, 0x0016 },
  214. { USBTV_BASE + 0x0267, 0x0036 },
  215. /* End image tuning */
  216. { USBTV_BASE + 0x024e, 0x0002 },
  217. { USBTV_BASE + 0x024f, 0x0002 },
  218. };
  219. ret = usbtv_configure_for_norm(usbtv, norm);
  220. if (!ret) {
  221. /* Masks for norms using a NTSC or PAL color encoding. */
  222. static const v4l2_std_id ntsc_mask =
  223. V4L2_STD_NTSC | V4L2_STD_NTSC_443;
  224. static const v4l2_std_id pal_mask =
  225. V4L2_STD_PAL | V4L2_STD_PAL_60 | V4L2_STD_PAL_M;
  226. if (norm & ntsc_mask)
  227. ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc));
  228. else if (norm & pal_mask)
  229. ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal));
  230. else if (norm & V4L2_STD_SECAM)
  231. ret = usbtv_set_regs(usbtv, secam, ARRAY_SIZE(secam));
  232. else
  233. ret = -EINVAL;
  234. }
  235. if (!ret) {
  236. /* Configure the decoder for the color standard */
  237. const u16 cfg[][2] = {
  238. { USBTV_BASE + 0x016f, usbtv_norm_to_16f_reg(norm) }
  239. };
  240. ret = usbtv_set_regs(usbtv, cfg, ARRAY_SIZE(cfg));
  241. }
  242. return ret;
  243. }
  244. static int usbtv_setup_capture(struct usbtv *usbtv)
  245. {
  246. int ret;
  247. static const u16 setup[][2] = {
  248. /* These seem to enable the device. */
  249. { USBTV_BASE + 0x0008, 0x0001 },
  250. { USBTV_BASE + 0x01d0, 0x00ff },
  251. { USBTV_BASE + 0x01d9, 0x0002 },
  252. /* These seem to influence color parameters, such as
  253. * brightness, etc. */
  254. { USBTV_BASE + 0x0239, 0x0040 },
  255. { USBTV_BASE + 0x0240, 0x0000 },
  256. { USBTV_BASE + 0x0241, 0x0000 },
  257. { USBTV_BASE + 0x0242, 0x0002 },
  258. { USBTV_BASE + 0x0243, 0x0080 },
  259. { USBTV_BASE + 0x0244, 0x0012 },
  260. { USBTV_BASE + 0x0245, 0x0090 },
  261. { USBTV_BASE + 0x0246, 0x0000 },
  262. { USBTV_BASE + 0x0278, 0x002d },
  263. { USBTV_BASE + 0x0279, 0x000a },
  264. { USBTV_BASE + 0x027a, 0x0032 },
  265. { 0xf890, 0x000c },
  266. { 0xf894, 0x0086 },
  267. { USBTV_BASE + 0x00ac, 0x00c0 },
  268. { USBTV_BASE + 0x00ad, 0x0000 },
  269. { USBTV_BASE + 0x00a2, 0x0012 },
  270. { USBTV_BASE + 0x00a3, 0x00e0 },
  271. { USBTV_BASE + 0x00a4, 0x0028 },
  272. { USBTV_BASE + 0x00a5, 0x0082 },
  273. { USBTV_BASE + 0x00a7, 0x0080 },
  274. { USBTV_BASE + 0x0000, 0x0014 },
  275. { USBTV_BASE + 0x0006, 0x0003 },
  276. { USBTV_BASE + 0x0090, 0x0099 },
  277. { USBTV_BASE + 0x0091, 0x0090 },
  278. { USBTV_BASE + 0x0094, 0x0068 },
  279. { USBTV_BASE + 0x0095, 0x0070 },
  280. { USBTV_BASE + 0x009c, 0x0030 },
  281. { USBTV_BASE + 0x009d, 0x00c0 },
  282. { USBTV_BASE + 0x009e, 0x00e0 },
  283. { USBTV_BASE + 0x0019, 0x0006 },
  284. { USBTV_BASE + 0x008c, 0x00ba },
  285. { USBTV_BASE + 0x0101, 0x00ff },
  286. { USBTV_BASE + 0x010c, 0x00b3 },
  287. { USBTV_BASE + 0x01b2, 0x0080 },
  288. { USBTV_BASE + 0x01b4, 0x00a0 },
  289. { USBTV_BASE + 0x014c, 0x00ff },
  290. { USBTV_BASE + 0x014d, 0x00ca },
  291. { USBTV_BASE + 0x0113, 0x0053 },
  292. { USBTV_BASE + 0x0119, 0x008a },
  293. { USBTV_BASE + 0x013c, 0x0003 },
  294. { USBTV_BASE + 0x0150, 0x009c },
  295. { USBTV_BASE + 0x0151, 0x0071 },
  296. { USBTV_BASE + 0x0152, 0x00c6 },
  297. { USBTV_BASE + 0x0153, 0x0084 },
  298. { USBTV_BASE + 0x0154, 0x00bc },
  299. { USBTV_BASE + 0x0155, 0x00a0 },
  300. { USBTV_BASE + 0x0156, 0x00a0 },
  301. { USBTV_BASE + 0x0157, 0x009c },
  302. { USBTV_BASE + 0x0158, 0x001f },
  303. { USBTV_BASE + 0x0159, 0x0006 },
  304. { USBTV_BASE + 0x015d, 0x0000 },
  305. };
  306. ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup));
  307. if (ret)
  308. return ret;
  309. ret = usbtv_select_norm(usbtv, usbtv->norm);
  310. if (ret)
  311. return ret;
  312. ret = usbtv_select_input(usbtv, usbtv->input);
  313. if (ret)
  314. return ret;
  315. ret = v4l2_ctrl_handler_setup(&usbtv->ctrl);
  316. if (ret)
  317. return ret;
  318. return 0;
  319. }
  320. /* Copy data from chunk into a frame buffer, deinterlacing the data
  321. * into every second line. Unfortunately, they don't align nicely into
  322. * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
  323. * Therefore, we break down the chunk into two halves before copying,
  324. * so that we can interleave a line if needed.
  325. *
  326. * Each "chunk" is 240 words; a word in this context equals 4 bytes.
  327. * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
  328. * pixels, the Cr and Cb shared between the two pixels, but each having
  329. * separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
  330. * takes 1.5 chunks to make a 720 pixel-wide line for the frame.
  331. * The image is interlaced, so there is a "scan" of odd lines, followed
  332. * by "scan" of even numbered lines.
  333. *
  334. * Following code is writing the chunks in correct sequence, skipping
  335. * the rows based on "odd" value.
  336. * line 1: chunk[0][ 0..479] chunk[0][480..959] chunk[1][ 0..479]
  337. * line 3: chunk[1][480..959] chunk[2][ 0..479] chunk[2][480..959]
  338. * ...etc.
  339. */
  340. static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd)
  341. {
  342. int half;
  343. for (half = 0; half < 2; half++) {
  344. int part_no = chunk_no * 2 + half;
  345. int line = part_no / 3;
  346. int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
  347. u32 *dst = &frame[part_index * USBTV_CHUNK/2];
  348. memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
  349. src += USBTV_CHUNK/2;
  350. }
  351. }
  352. /* Called for each 256-byte image chunk.
  353. * First word identifies the chunk, followed by 240 words of image
  354. * data and padding. */
  355. static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk)
  356. {
  357. int frame_id, odd, chunk_no;
  358. u32 *frame;
  359. struct usbtv_buf *buf;
  360. unsigned long flags;
  361. /* Ignore corrupted lines. */
  362. if (!USBTV_MAGIC_OK(chunk))
  363. return;
  364. frame_id = USBTV_FRAME_ID(chunk);
  365. odd = USBTV_ODD(chunk);
  366. chunk_no = USBTV_CHUNK_NO(chunk);
  367. if (chunk_no >= usbtv->n_chunks)
  368. return;
  369. /* Beginning of a frame. */
  370. if (chunk_no == 0) {
  371. usbtv->frame_id = frame_id;
  372. usbtv->chunks_done = 0;
  373. }
  374. if (usbtv->frame_id != frame_id)
  375. return;
  376. spin_lock_irqsave(&usbtv->buflock, flags);
  377. if (list_empty(&usbtv->bufs)) {
  378. /* No free buffers. Userspace likely too slow. */
  379. spin_unlock_irqrestore(&usbtv->buflock, flags);
  380. return;
  381. }
  382. /* First available buffer. */
  383. buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
  384. frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  385. /* Copy the chunk data. */
  386. usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
  387. usbtv->chunks_done++;
  388. /* Last chunk in a field */
  389. if (chunk_no == usbtv->n_chunks-1) {
  390. /* Last chunk in a frame, signalling an end */
  391. if (odd && !usbtv->last_odd) {
  392. int size = vb2_plane_size(&buf->vb.vb2_buf, 0);
  393. enum vb2_buffer_state state = usbtv->chunks_done ==
  394. usbtv->n_chunks ?
  395. VB2_BUF_STATE_DONE :
  396. VB2_BUF_STATE_ERROR;
  397. buf->vb.field = V4L2_FIELD_INTERLACED;
  398. buf->vb.sequence = usbtv->sequence++;
  399. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  400. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
  401. vb2_buffer_done(&buf->vb.vb2_buf, state);
  402. list_del(&buf->list);
  403. }
  404. usbtv->last_odd = odd;
  405. }
  406. spin_unlock_irqrestore(&usbtv->buflock, flags);
  407. }
  408. /* Got image data. Each packet contains a number of 256-word chunks we
  409. * compose the image from. */
  410. static void usbtv_iso_cb(struct urb *ip)
  411. {
  412. int ret;
  413. int i;
  414. struct usbtv *usbtv = (struct usbtv *)ip->context;
  415. switch (ip->status) {
  416. /* All fine. */
  417. case 0:
  418. break;
  419. /* Device disconnected or capture stopped? */
  420. case -ENODEV:
  421. case -ENOENT:
  422. case -ECONNRESET:
  423. case -ESHUTDOWN:
  424. return;
  425. /* Unknown error. Retry. */
  426. default:
  427. dev_warn(usbtv->dev, "Bad response for ISO request.\n");
  428. goto resubmit;
  429. }
  430. for (i = 0; i < ip->number_of_packets; i++) {
  431. int size = ip->iso_frame_desc[i].actual_length;
  432. unsigned char *data = ip->transfer_buffer +
  433. ip->iso_frame_desc[i].offset;
  434. int offset;
  435. for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
  436. usbtv_image_chunk(usbtv,
  437. (__be32 *)&data[USBTV_CHUNK_SIZE * offset]);
  438. }
  439. resubmit:
  440. ret = usb_submit_urb(ip, GFP_ATOMIC);
  441. if (ret < 0)
  442. dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
  443. }
  444. static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
  445. {
  446. struct urb *ip;
  447. int size = usbtv->iso_size;
  448. int i;
  449. ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
  450. if (ip == NULL)
  451. return NULL;
  452. ip->dev = usbtv->udev;
  453. ip->context = usbtv;
  454. ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
  455. ip->interval = 1;
  456. ip->transfer_flags = URB_ISO_ASAP;
  457. ip->transfer_buffer = kcalloc(USBTV_ISOC_PACKETS, size,
  458. GFP_KERNEL);
  459. if (!ip->transfer_buffer) {
  460. usb_free_urb(ip);
  461. return NULL;
  462. }
  463. ip->complete = usbtv_iso_cb;
  464. ip->number_of_packets = USBTV_ISOC_PACKETS;
  465. ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
  466. for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
  467. ip->iso_frame_desc[i].offset = size * i;
  468. ip->iso_frame_desc[i].length = size;
  469. }
  470. return ip;
  471. }
  472. static void usbtv_stop(struct usbtv *usbtv)
  473. {
  474. int i;
  475. unsigned long flags;
  476. /* Cancel running transfers. */
  477. for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
  478. struct urb *ip = usbtv->isoc_urbs[i];
  479. if (ip == NULL)
  480. continue;
  481. usb_kill_urb(ip);
  482. kfree(ip->transfer_buffer);
  483. usb_free_urb(ip);
  484. usbtv->isoc_urbs[i] = NULL;
  485. }
  486. /* Return buffers to userspace. */
  487. spin_lock_irqsave(&usbtv->buflock, flags);
  488. while (!list_empty(&usbtv->bufs)) {
  489. struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
  490. struct usbtv_buf, list);
  491. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  492. list_del(&buf->list);
  493. }
  494. spin_unlock_irqrestore(&usbtv->buflock, flags);
  495. }
  496. static int usbtv_start(struct usbtv *usbtv)
  497. {
  498. int i;
  499. int ret;
  500. usbtv_audio_suspend(usbtv);
  501. ret = usb_set_interface(usbtv->udev, 0, 0);
  502. if (ret < 0)
  503. return ret;
  504. ret = usbtv_setup_capture(usbtv);
  505. if (ret < 0)
  506. return ret;
  507. ret = usb_set_interface(usbtv->udev, 0, 1);
  508. if (ret < 0)
  509. return ret;
  510. usbtv_audio_resume(usbtv);
  511. for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
  512. struct urb *ip;
  513. ip = usbtv_setup_iso_transfer(usbtv);
  514. if (ip == NULL) {
  515. ret = -ENOMEM;
  516. goto start_fail;
  517. }
  518. usbtv->isoc_urbs[i] = ip;
  519. ret = usb_submit_urb(ip, GFP_KERNEL);
  520. if (ret < 0)
  521. goto start_fail;
  522. }
  523. return 0;
  524. start_fail:
  525. usbtv_stop(usbtv);
  526. return ret;
  527. }
  528. static int usbtv_querycap(struct file *file, void *priv,
  529. struct v4l2_capability *cap)
  530. {
  531. struct usbtv *dev = video_drvdata(file);
  532. strlcpy(cap->driver, "usbtv", sizeof(cap->driver));
  533. strlcpy(cap->card, "usbtv", sizeof(cap->card));
  534. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  535. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
  536. cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  537. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  538. return 0;
  539. }
  540. static int usbtv_enum_input(struct file *file, void *priv,
  541. struct v4l2_input *i)
  542. {
  543. struct usbtv *dev = video_drvdata(file);
  544. switch (i->index) {
  545. case USBTV_COMPOSITE_INPUT:
  546. strlcpy(i->name, "Composite", sizeof(i->name));
  547. break;
  548. case USBTV_SVIDEO_INPUT:
  549. strlcpy(i->name, "S-Video", sizeof(i->name));
  550. break;
  551. default:
  552. return -EINVAL;
  553. }
  554. i->type = V4L2_INPUT_TYPE_CAMERA;
  555. i->std = dev->vdev.tvnorms;
  556. return 0;
  557. }
  558. static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv,
  559. struct v4l2_fmtdesc *f)
  560. {
  561. if (f->index > 0)
  562. return -EINVAL;
  563. strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
  564. sizeof(f->description));
  565. f->pixelformat = V4L2_PIX_FMT_YUYV;
  566. return 0;
  567. }
  568. static int usbtv_fmt_vid_cap(struct file *file, void *priv,
  569. struct v4l2_format *f)
  570. {
  571. struct usbtv *usbtv = video_drvdata(file);
  572. f->fmt.pix.width = usbtv->width;
  573. f->fmt.pix.height = usbtv->height;
  574. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  575. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  576. f->fmt.pix.bytesperline = usbtv->width * 2;
  577. f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
  578. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  579. return 0;
  580. }
  581. static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  582. {
  583. struct usbtv *usbtv = video_drvdata(file);
  584. *norm = usbtv->norm;
  585. return 0;
  586. }
  587. static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
  588. {
  589. int ret = -EINVAL;
  590. struct usbtv *usbtv = video_drvdata(file);
  591. if (norm & USBTV_TV_STD)
  592. ret = usbtv_select_norm(usbtv, norm);
  593. return ret;
  594. }
  595. static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
  596. {
  597. struct usbtv *usbtv = video_drvdata(file);
  598. *i = usbtv->input;
  599. return 0;
  600. }
  601. static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
  602. {
  603. struct usbtv *usbtv = video_drvdata(file);
  604. return usbtv_select_input(usbtv, i);
  605. }
  606. static struct v4l2_ioctl_ops usbtv_ioctl_ops = {
  607. .vidioc_querycap = usbtv_querycap,
  608. .vidioc_enum_input = usbtv_enum_input,
  609. .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
  610. .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
  611. .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
  612. .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
  613. .vidioc_g_std = usbtv_g_std,
  614. .vidioc_s_std = usbtv_s_std,
  615. .vidioc_g_input = usbtv_g_input,
  616. .vidioc_s_input = usbtv_s_input,
  617. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  618. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  619. .vidioc_querybuf = vb2_ioctl_querybuf,
  620. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  621. .vidioc_qbuf = vb2_ioctl_qbuf,
  622. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  623. .vidioc_streamon = vb2_ioctl_streamon,
  624. .vidioc_streamoff = vb2_ioctl_streamoff,
  625. };
  626. static const struct v4l2_file_operations usbtv_fops = {
  627. .owner = THIS_MODULE,
  628. .unlocked_ioctl = video_ioctl2,
  629. .mmap = vb2_fop_mmap,
  630. .open = v4l2_fh_open,
  631. .release = vb2_fop_release,
  632. .read = vb2_fop_read,
  633. .poll = vb2_fop_poll,
  634. };
  635. static int usbtv_queue_setup(struct vb2_queue *vq,
  636. unsigned int *nbuffers,
  637. unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
  638. {
  639. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  640. unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
  641. if (vq->num_buffers + *nbuffers < 2)
  642. *nbuffers = 2 - vq->num_buffers;
  643. if (*nplanes)
  644. return sizes[0] < size ? -EINVAL : 0;
  645. *nplanes = 1;
  646. sizes[0] = size;
  647. return 0;
  648. }
  649. static void usbtv_buf_queue(struct vb2_buffer *vb)
  650. {
  651. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  652. struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
  653. struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb);
  654. unsigned long flags;
  655. if (usbtv->udev == NULL) {
  656. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  657. return;
  658. }
  659. spin_lock_irqsave(&usbtv->buflock, flags);
  660. list_add_tail(&buf->list, &usbtv->bufs);
  661. spin_unlock_irqrestore(&usbtv->buflock, flags);
  662. }
  663. static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
  664. {
  665. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  666. if (usbtv->udev == NULL)
  667. return -ENODEV;
  668. usbtv->last_odd = 1;
  669. usbtv->sequence = 0;
  670. return usbtv_start(usbtv);
  671. }
  672. static void usbtv_stop_streaming(struct vb2_queue *vq)
  673. {
  674. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  675. if (usbtv->udev)
  676. usbtv_stop(usbtv);
  677. }
  678. static const struct vb2_ops usbtv_vb2_ops = {
  679. .queue_setup = usbtv_queue_setup,
  680. .buf_queue = usbtv_buf_queue,
  681. .start_streaming = usbtv_start_streaming,
  682. .stop_streaming = usbtv_stop_streaming,
  683. .wait_prepare = vb2_ops_wait_prepare,
  684. .wait_finish = vb2_ops_wait_finish,
  685. };
  686. static int usbtv_s_ctrl(struct v4l2_ctrl *ctrl)
  687. {
  688. struct usbtv *usbtv = container_of(ctrl->handler, struct usbtv,
  689. ctrl);
  690. u8 *data;
  691. u16 index, size;
  692. int ret;
  693. data = kmalloc(3, GFP_KERNEL);
  694. if (!data)
  695. return -ENOMEM;
  696. /*
  697. * Read in the current brightness/contrast registers. We need them
  698. * both, because the values are for some reason interleaved.
  699. */
  700. if (ctrl->id == V4L2_CID_BRIGHTNESS || ctrl->id == V4L2_CID_CONTRAST) {
  701. ret = usb_control_msg(usbtv->udev,
  702. usb_rcvctrlpipe(usbtv->udev, 0), USBTV_CONTROL_REG,
  703. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  704. 0, USBTV_BASE + 0x0244, (void *)data, 3,
  705. USB_CTRL_GET_TIMEOUT);
  706. if (ret < 0)
  707. goto error;
  708. }
  709. switch (ctrl->id) {
  710. case V4L2_CID_BRIGHTNESS:
  711. index = USBTV_BASE + 0x0244;
  712. size = 3;
  713. data[0] &= 0xf0;
  714. data[0] |= (ctrl->val >> 8) & 0xf;
  715. data[2] = ctrl->val & 0xff;
  716. break;
  717. case V4L2_CID_CONTRAST:
  718. index = USBTV_BASE + 0x0244;
  719. size = 3;
  720. data[0] &= 0x0f;
  721. data[0] |= (ctrl->val >> 4) & 0xf0;
  722. data[1] = ctrl->val & 0xff;
  723. break;
  724. case V4L2_CID_SATURATION:
  725. index = USBTV_BASE + 0x0242;
  726. data[0] = ctrl->val >> 8;
  727. data[1] = ctrl->val & 0xff;
  728. size = 2;
  729. break;
  730. case V4L2_CID_HUE:
  731. index = USBTV_BASE + 0x0240;
  732. size = 2;
  733. if (ctrl->val > 0) {
  734. data[0] = 0x92 + (ctrl->val >> 8);
  735. data[1] = ctrl->val & 0xff;
  736. } else {
  737. data[0] = 0x82 + (-ctrl->val >> 8);
  738. data[1] = -ctrl->val & 0xff;
  739. }
  740. break;
  741. case V4L2_CID_SHARPNESS:
  742. index = USBTV_BASE + 0x0239;
  743. data[0] = 0;
  744. data[1] = ctrl->val;
  745. size = 2;
  746. break;
  747. default:
  748. kfree(data);
  749. return -EINVAL;
  750. }
  751. ret = usb_control_msg(usbtv->udev, usb_sndctrlpipe(usbtv->udev, 0),
  752. USBTV_CONTROL_REG,
  753. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  754. 0, index, (void *)data, size, USB_CTRL_SET_TIMEOUT);
  755. error:
  756. if (ret < 0)
  757. dev_warn(usbtv->dev, "Failed to submit a control request.\n");
  758. kfree(data);
  759. return ret;
  760. }
  761. static const struct v4l2_ctrl_ops usbtv_ctrl_ops = {
  762. .s_ctrl = usbtv_s_ctrl,
  763. };
  764. static void usbtv_release(struct v4l2_device *v4l2_dev)
  765. {
  766. struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
  767. v4l2_device_unregister(&usbtv->v4l2_dev);
  768. v4l2_ctrl_handler_free(&usbtv->ctrl);
  769. vb2_queue_release(&usbtv->vb2q);
  770. kfree(usbtv);
  771. }
  772. int usbtv_video_init(struct usbtv *usbtv)
  773. {
  774. int ret;
  775. (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60);
  776. spin_lock_init(&usbtv->buflock);
  777. mutex_init(&usbtv->v4l2_lock);
  778. mutex_init(&usbtv->vb2q_lock);
  779. INIT_LIST_HEAD(&usbtv->bufs);
  780. /* videobuf2 structure */
  781. usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  782. usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  783. usbtv->vb2q.drv_priv = usbtv;
  784. usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
  785. usbtv->vb2q.ops = &usbtv_vb2_ops;
  786. usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
  787. usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  788. usbtv->vb2q.lock = &usbtv->vb2q_lock;
  789. ret = vb2_queue_init(&usbtv->vb2q);
  790. if (ret < 0) {
  791. dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n");
  792. return ret;
  793. }
  794. /* controls */
  795. v4l2_ctrl_handler_init(&usbtv->ctrl, 4);
  796. v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
  797. V4L2_CID_CONTRAST, 0, 0x3ff, 1, 0x1d0);
  798. v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
  799. V4L2_CID_BRIGHTNESS, 0, 0x3ff, 1, 0x1c0);
  800. v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
  801. V4L2_CID_SATURATION, 0, 0x3ff, 1, 0x200);
  802. v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
  803. V4L2_CID_HUE, -0xdff, 0xdff, 1, 0x000);
  804. v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
  805. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x60);
  806. ret = usbtv->ctrl.error;
  807. if (ret < 0) {
  808. dev_warn(usbtv->dev, "Could not initialize controls\n");
  809. goto ctrl_fail;
  810. }
  811. /* v4l2 structure */
  812. usbtv->v4l2_dev.ctrl_handler = &usbtv->ctrl;
  813. usbtv->v4l2_dev.release = usbtv_release;
  814. ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev);
  815. if (ret < 0) {
  816. dev_warn(usbtv->dev, "Could not register v4l2 device\n");
  817. goto v4l2_fail;
  818. }
  819. /* Video structure */
  820. strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
  821. usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
  822. usbtv->vdev.release = video_device_release_empty;
  823. usbtv->vdev.fops = &usbtv_fops;
  824. usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
  825. usbtv->vdev.tvnorms = USBTV_TV_STD;
  826. usbtv->vdev.queue = &usbtv->vb2q;
  827. usbtv->vdev.lock = &usbtv->v4l2_lock;
  828. video_set_drvdata(&usbtv->vdev, usbtv);
  829. ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1);
  830. if (ret < 0) {
  831. dev_warn(usbtv->dev, "Could not register video device\n");
  832. goto vdev_fail;
  833. }
  834. return 0;
  835. vdev_fail:
  836. v4l2_device_unregister(&usbtv->v4l2_dev);
  837. v4l2_fail:
  838. ctrl_fail:
  839. v4l2_ctrl_handler_free(&usbtv->ctrl);
  840. vb2_queue_release(&usbtv->vb2q);
  841. return ret;
  842. }
  843. void usbtv_video_free(struct usbtv *usbtv)
  844. {
  845. mutex_lock(&usbtv->vb2q_lock);
  846. mutex_lock(&usbtv->v4l2_lock);
  847. usbtv_stop(usbtv);
  848. video_unregister_device(&usbtv->vdev);
  849. v4l2_device_disconnect(&usbtv->v4l2_dev);
  850. mutex_unlock(&usbtv->v4l2_lock);
  851. mutex_unlock(&usbtv->vb2q_lock);
  852. v4l2_device_put(&usbtv->v4l2_dev);
  853. }