usbtv-video.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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 const 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. const 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. if (norm & V4L2_STD_PAL_Nc)
  126. return 0x00fe;
  127. /* Fallback to automatic detection for other standards */
  128. return 0x0000;
  129. }
  130. static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm)
  131. {
  132. int ret;
  133. /* These are the series of register values used to configure the
  134. * decoder for a specific standard.
  135. * The first 21 register writes are copied from the
  136. * Settings\DecoderDefaults registry keys present in the Windows driver
  137. * .INF file, and control various image tuning parameters (color
  138. * correction, sharpness, ...).
  139. */
  140. static const u16 pal[][2] = {
  141. /* "AVPAL" tuning sequence from .INF file */
  142. { USBTV_BASE + 0x0003, 0x0004 },
  143. { USBTV_BASE + 0x001a, 0x0068 },
  144. { USBTV_BASE + 0x0100, 0x00d3 },
  145. { USBTV_BASE + 0x010e, 0x0072 },
  146. { USBTV_BASE + 0x010f, 0x00a2 },
  147. { USBTV_BASE + 0x0112, 0x00b0 },
  148. { USBTV_BASE + 0x0115, 0x0015 },
  149. { USBTV_BASE + 0x0117, 0x0001 },
  150. { USBTV_BASE + 0x0118, 0x002c },
  151. { USBTV_BASE + 0x012d, 0x0010 },
  152. { USBTV_BASE + 0x012f, 0x0020 },
  153. { USBTV_BASE + 0x0220, 0x002e },
  154. { USBTV_BASE + 0x0225, 0x0008 },
  155. { USBTV_BASE + 0x024e, 0x0002 },
  156. { USBTV_BASE + 0x024f, 0x0002 },
  157. { USBTV_BASE + 0x0254, 0x0059 },
  158. { USBTV_BASE + 0x025a, 0x0016 },
  159. { USBTV_BASE + 0x025b, 0x0035 },
  160. { USBTV_BASE + 0x0263, 0x0017 },
  161. { USBTV_BASE + 0x0266, 0x0016 },
  162. { USBTV_BASE + 0x0267, 0x0036 },
  163. /* End image tuning */
  164. { USBTV_BASE + 0x024e, 0x0002 },
  165. { USBTV_BASE + 0x024f, 0x0002 },
  166. };
  167. static const u16 ntsc[][2] = {
  168. /* "AVNTSC" tuning sequence from .INF file */
  169. { USBTV_BASE + 0x0003, 0x0004 },
  170. { USBTV_BASE + 0x001a, 0x0079 },
  171. { USBTV_BASE + 0x0100, 0x00d3 },
  172. { USBTV_BASE + 0x010e, 0x0068 },
  173. { USBTV_BASE + 0x010f, 0x009c },
  174. { USBTV_BASE + 0x0112, 0x00f0 },
  175. { USBTV_BASE + 0x0115, 0x0015 },
  176. { USBTV_BASE + 0x0117, 0x0000 },
  177. { USBTV_BASE + 0x0118, 0x00fc },
  178. { USBTV_BASE + 0x012d, 0x0004 },
  179. { USBTV_BASE + 0x012f, 0x0008 },
  180. { USBTV_BASE + 0x0220, 0x002e },
  181. { USBTV_BASE + 0x0225, 0x0008 },
  182. { USBTV_BASE + 0x024e, 0x0002 },
  183. { USBTV_BASE + 0x024f, 0x0001 },
  184. { USBTV_BASE + 0x0254, 0x005f },
  185. { USBTV_BASE + 0x025a, 0x0012 },
  186. { USBTV_BASE + 0x025b, 0x0001 },
  187. { USBTV_BASE + 0x0263, 0x001c },
  188. { USBTV_BASE + 0x0266, 0x0011 },
  189. { USBTV_BASE + 0x0267, 0x0005 },
  190. /* End image tuning */
  191. { USBTV_BASE + 0x024e, 0x0002 },
  192. { USBTV_BASE + 0x024f, 0x0002 },
  193. };
  194. static const u16 secam[][2] = {
  195. /* "AVSECAM" tuning sequence from .INF file */
  196. { USBTV_BASE + 0x0003, 0x0004 },
  197. { USBTV_BASE + 0x001a, 0x0073 },
  198. { USBTV_BASE + 0x0100, 0x00dc },
  199. { USBTV_BASE + 0x010e, 0x0072 },
  200. { USBTV_BASE + 0x010f, 0x00a2 },
  201. { USBTV_BASE + 0x0112, 0x0090 },
  202. { USBTV_BASE + 0x0115, 0x0035 },
  203. { USBTV_BASE + 0x0117, 0x0001 },
  204. { USBTV_BASE + 0x0118, 0x0030 },
  205. { USBTV_BASE + 0x012d, 0x0004 },
  206. { USBTV_BASE + 0x012f, 0x0008 },
  207. { USBTV_BASE + 0x0220, 0x002d },
  208. { USBTV_BASE + 0x0225, 0x0028 },
  209. { USBTV_BASE + 0x024e, 0x0008 },
  210. { USBTV_BASE + 0x024f, 0x0002 },
  211. { USBTV_BASE + 0x0254, 0x0069 },
  212. { USBTV_BASE + 0x025a, 0x0016 },
  213. { USBTV_BASE + 0x025b, 0x0035 },
  214. { USBTV_BASE + 0x0263, 0x0021 },
  215. { USBTV_BASE + 0x0266, 0x0016 },
  216. { USBTV_BASE + 0x0267, 0x0036 },
  217. /* End image tuning */
  218. { USBTV_BASE + 0x024e, 0x0002 },
  219. { USBTV_BASE + 0x024f, 0x0002 },
  220. };
  221. ret = usbtv_configure_for_norm(usbtv, norm);
  222. if (!ret) {
  223. /* Masks for norms using a NTSC or PAL color encoding. */
  224. static const v4l2_std_id ntsc_mask =
  225. V4L2_STD_NTSC | V4L2_STD_NTSC_443;
  226. static const v4l2_std_id pal_mask =
  227. V4L2_STD_PAL | V4L2_STD_PAL_60 | V4L2_STD_PAL_M |
  228. V4L2_STD_PAL_Nc;
  229. if (norm & ntsc_mask)
  230. ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc));
  231. else if (norm & pal_mask)
  232. ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal));
  233. else if (norm & V4L2_STD_SECAM)
  234. ret = usbtv_set_regs(usbtv, secam, ARRAY_SIZE(secam));
  235. else
  236. ret = -EINVAL;
  237. }
  238. if (!ret) {
  239. /* Configure the decoder for the color standard */
  240. const u16 cfg[][2] = {
  241. { USBTV_BASE + 0x016f, usbtv_norm_to_16f_reg(norm) }
  242. };
  243. ret = usbtv_set_regs(usbtv, cfg, ARRAY_SIZE(cfg));
  244. }
  245. return ret;
  246. }
  247. static int usbtv_setup_capture(struct usbtv *usbtv)
  248. {
  249. int ret;
  250. static const u16 setup[][2] = {
  251. /* These seem to enable the device. */
  252. { USBTV_BASE + 0x0008, 0x0001 },
  253. { USBTV_BASE + 0x01d0, 0x00ff },
  254. { USBTV_BASE + 0x01d9, 0x0002 },
  255. /* These seem to influence color parameters, such as
  256. * brightness, etc. */
  257. { USBTV_BASE + 0x0239, 0x0040 },
  258. { USBTV_BASE + 0x0240, 0x0000 },
  259. { USBTV_BASE + 0x0241, 0x0000 },
  260. { USBTV_BASE + 0x0242, 0x0002 },
  261. { USBTV_BASE + 0x0243, 0x0080 },
  262. { USBTV_BASE + 0x0244, 0x0012 },
  263. { USBTV_BASE + 0x0245, 0x0090 },
  264. { USBTV_BASE + 0x0246, 0x0000 },
  265. { USBTV_BASE + 0x0278, 0x002d },
  266. { USBTV_BASE + 0x0279, 0x000a },
  267. { USBTV_BASE + 0x027a, 0x0032 },
  268. { 0xf890, 0x000c },
  269. { 0xf894, 0x0086 },
  270. { USBTV_BASE + 0x00ac, 0x00c0 },
  271. { USBTV_BASE + 0x00ad, 0x0000 },
  272. { USBTV_BASE + 0x00a2, 0x0012 },
  273. { USBTV_BASE + 0x00a3, 0x00e0 },
  274. { USBTV_BASE + 0x00a4, 0x0028 },
  275. { USBTV_BASE + 0x00a5, 0x0082 },
  276. { USBTV_BASE + 0x00a7, 0x0080 },
  277. { USBTV_BASE + 0x0000, 0x0014 },
  278. { USBTV_BASE + 0x0006, 0x0003 },
  279. { USBTV_BASE + 0x0090, 0x0099 },
  280. { USBTV_BASE + 0x0091, 0x0090 },
  281. { USBTV_BASE + 0x0094, 0x0068 },
  282. { USBTV_BASE + 0x0095, 0x0070 },
  283. { USBTV_BASE + 0x009c, 0x0030 },
  284. { USBTV_BASE + 0x009d, 0x00c0 },
  285. { USBTV_BASE + 0x009e, 0x00e0 },
  286. { USBTV_BASE + 0x0019, 0x0006 },
  287. { USBTV_BASE + 0x008c, 0x00ba },
  288. { USBTV_BASE + 0x0101, 0x00ff },
  289. { USBTV_BASE + 0x010c, 0x00b3 },
  290. { USBTV_BASE + 0x01b2, 0x0080 },
  291. { USBTV_BASE + 0x01b4, 0x00a0 },
  292. { USBTV_BASE + 0x014c, 0x00ff },
  293. { USBTV_BASE + 0x014d, 0x00ca },
  294. { USBTV_BASE + 0x0113, 0x0053 },
  295. { USBTV_BASE + 0x0119, 0x008a },
  296. { USBTV_BASE + 0x013c, 0x0003 },
  297. { USBTV_BASE + 0x0150, 0x009c },
  298. { USBTV_BASE + 0x0151, 0x0071 },
  299. { USBTV_BASE + 0x0152, 0x00c6 },
  300. { USBTV_BASE + 0x0153, 0x0084 },
  301. { USBTV_BASE + 0x0154, 0x00bc },
  302. { USBTV_BASE + 0x0155, 0x00a0 },
  303. { USBTV_BASE + 0x0156, 0x00a0 },
  304. { USBTV_BASE + 0x0157, 0x009c },
  305. { USBTV_BASE + 0x0158, 0x001f },
  306. { USBTV_BASE + 0x0159, 0x0006 },
  307. { USBTV_BASE + 0x015d, 0x0000 },
  308. };
  309. ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup));
  310. if (ret)
  311. return ret;
  312. ret = usbtv_select_norm(usbtv, usbtv->norm);
  313. if (ret)
  314. return ret;
  315. ret = usbtv_select_input(usbtv, usbtv->input);
  316. if (ret)
  317. return ret;
  318. ret = v4l2_ctrl_handler_setup(&usbtv->ctrl);
  319. if (ret)
  320. return ret;
  321. return 0;
  322. }
  323. /* Copy data from chunk into a frame buffer, deinterlacing the data
  324. * into every second line. Unfortunately, they don't align nicely into
  325. * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
  326. * Therefore, we break down the chunk into two halves before copying,
  327. * so that we can interleave a line if needed.
  328. *
  329. * Each "chunk" is 240 words; a word in this context equals 4 bytes.
  330. * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
  331. * pixels, the Cr and Cb shared between the two pixels, but each having
  332. * separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
  333. * takes 1.5 chunks to make a 720 pixel-wide line for the frame.
  334. * The image is interlaced, so there is a "scan" of odd lines, followed
  335. * by "scan" of even numbered lines.
  336. *
  337. * Following code is writing the chunks in correct sequence, skipping
  338. * the rows based on "odd" value.
  339. * line 1: chunk[0][ 0..479] chunk[0][480..959] chunk[1][ 0..479]
  340. * line 3: chunk[1][480..959] chunk[2][ 0..479] chunk[2][480..959]
  341. * ...etc.
  342. */
  343. static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd)
  344. {
  345. int half;
  346. for (half = 0; half < 2; half++) {
  347. int part_no = chunk_no * 2 + half;
  348. int line = part_no / 3;
  349. int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
  350. u32 *dst = &frame[part_index * USBTV_CHUNK/2];
  351. memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
  352. src += USBTV_CHUNK/2;
  353. }
  354. }
  355. /* Called for each 256-byte image chunk.
  356. * First word identifies the chunk, followed by 240 words of image
  357. * data and padding. */
  358. static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk)
  359. {
  360. int frame_id, odd, chunk_no;
  361. u32 *frame;
  362. struct usbtv_buf *buf;
  363. unsigned long flags;
  364. /* Ignore corrupted lines. */
  365. if (!USBTV_MAGIC_OK(chunk))
  366. return;
  367. frame_id = USBTV_FRAME_ID(chunk);
  368. odd = USBTV_ODD(chunk);
  369. chunk_no = USBTV_CHUNK_NO(chunk);
  370. if (chunk_no >= usbtv->n_chunks)
  371. return;
  372. /* Beginning of a frame. */
  373. if (chunk_no == 0) {
  374. usbtv->frame_id = frame_id;
  375. usbtv->chunks_done = 0;
  376. }
  377. if (usbtv->frame_id != frame_id)
  378. return;
  379. spin_lock_irqsave(&usbtv->buflock, flags);
  380. if (list_empty(&usbtv->bufs)) {
  381. /* No free buffers. Userspace likely too slow. */
  382. spin_unlock_irqrestore(&usbtv->buflock, flags);
  383. return;
  384. }
  385. /* First available buffer. */
  386. buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
  387. frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  388. /* Copy the chunk data. */
  389. usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
  390. usbtv->chunks_done++;
  391. /* Last chunk in a field */
  392. if (chunk_no == usbtv->n_chunks-1) {
  393. /* Last chunk in a frame, signalling an end */
  394. if (odd && !usbtv->last_odd) {
  395. int size = vb2_plane_size(&buf->vb.vb2_buf, 0);
  396. enum vb2_buffer_state state = usbtv->chunks_done ==
  397. usbtv->n_chunks ?
  398. VB2_BUF_STATE_DONE :
  399. VB2_BUF_STATE_ERROR;
  400. buf->vb.field = V4L2_FIELD_INTERLACED;
  401. buf->vb.sequence = usbtv->sequence++;
  402. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  403. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
  404. vb2_buffer_done(&buf->vb.vb2_buf, state);
  405. list_del(&buf->list);
  406. }
  407. usbtv->last_odd = odd;
  408. }
  409. spin_unlock_irqrestore(&usbtv->buflock, flags);
  410. }
  411. /* Got image data. Each packet contains a number of 256-word chunks we
  412. * compose the image from. */
  413. static void usbtv_iso_cb(struct urb *ip)
  414. {
  415. int ret;
  416. int i;
  417. struct usbtv *usbtv = (struct usbtv *)ip->context;
  418. switch (ip->status) {
  419. /* All fine. */
  420. case 0:
  421. break;
  422. /* Device disconnected or capture stopped? */
  423. case -ENODEV:
  424. case -ENOENT:
  425. case -ECONNRESET:
  426. case -ESHUTDOWN:
  427. return;
  428. /* Unknown error. Retry. */
  429. default:
  430. dev_warn(usbtv->dev, "Bad response for ISO request.\n");
  431. goto resubmit;
  432. }
  433. for (i = 0; i < ip->number_of_packets; i++) {
  434. int size = ip->iso_frame_desc[i].actual_length;
  435. unsigned char *data = ip->transfer_buffer +
  436. ip->iso_frame_desc[i].offset;
  437. int offset;
  438. for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
  439. usbtv_image_chunk(usbtv,
  440. (__be32 *)&data[USBTV_CHUNK_SIZE * offset]);
  441. }
  442. resubmit:
  443. ret = usb_submit_urb(ip, GFP_ATOMIC);
  444. if (ret < 0)
  445. dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
  446. }
  447. static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
  448. {
  449. struct urb *ip;
  450. int size = usbtv->iso_size;
  451. int i;
  452. ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
  453. if (ip == NULL)
  454. return NULL;
  455. ip->dev = usbtv->udev;
  456. ip->context = usbtv;
  457. ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
  458. ip->interval = 1;
  459. ip->transfer_flags = URB_ISO_ASAP;
  460. ip->transfer_buffer = kcalloc(USBTV_ISOC_PACKETS, size,
  461. GFP_KERNEL);
  462. if (!ip->transfer_buffer) {
  463. usb_free_urb(ip);
  464. return NULL;
  465. }
  466. ip->complete = usbtv_iso_cb;
  467. ip->number_of_packets = USBTV_ISOC_PACKETS;
  468. ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
  469. for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
  470. ip->iso_frame_desc[i].offset = size * i;
  471. ip->iso_frame_desc[i].length = size;
  472. }
  473. return ip;
  474. }
  475. static void usbtv_stop(struct usbtv *usbtv)
  476. {
  477. int i;
  478. unsigned long flags;
  479. /* Cancel running transfers. */
  480. for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
  481. struct urb *ip = usbtv->isoc_urbs[i];
  482. if (ip == NULL)
  483. continue;
  484. usb_kill_urb(ip);
  485. kfree(ip->transfer_buffer);
  486. usb_free_urb(ip);
  487. usbtv->isoc_urbs[i] = NULL;
  488. }
  489. /* Return buffers to userspace. */
  490. spin_lock_irqsave(&usbtv->buflock, flags);
  491. while (!list_empty(&usbtv->bufs)) {
  492. struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
  493. struct usbtv_buf, list);
  494. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  495. list_del(&buf->list);
  496. }
  497. spin_unlock_irqrestore(&usbtv->buflock, flags);
  498. }
  499. static int usbtv_start(struct usbtv *usbtv)
  500. {
  501. int i;
  502. int ret;
  503. usbtv_audio_suspend(usbtv);
  504. ret = usb_set_interface(usbtv->udev, 0, 0);
  505. if (ret < 0)
  506. return ret;
  507. ret = usbtv_setup_capture(usbtv);
  508. if (ret < 0)
  509. return ret;
  510. ret = usb_set_interface(usbtv->udev, 0, 1);
  511. if (ret < 0)
  512. return ret;
  513. usbtv_audio_resume(usbtv);
  514. for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
  515. struct urb *ip;
  516. ip = usbtv_setup_iso_transfer(usbtv);
  517. if (ip == NULL) {
  518. ret = -ENOMEM;
  519. goto start_fail;
  520. }
  521. usbtv->isoc_urbs[i] = ip;
  522. ret = usb_submit_urb(ip, GFP_KERNEL);
  523. if (ret < 0)
  524. goto start_fail;
  525. }
  526. return 0;
  527. start_fail:
  528. usbtv_stop(usbtv);
  529. return ret;
  530. }
  531. static int usbtv_querycap(struct file *file, void *priv,
  532. struct v4l2_capability *cap)
  533. {
  534. struct usbtv *dev = video_drvdata(file);
  535. strscpy(cap->driver, "usbtv", sizeof(cap->driver));
  536. strscpy(cap->card, "usbtv", sizeof(cap->card));
  537. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  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. strscpy(i->name, "Composite", sizeof(i->name));
  547. break;
  548. case USBTV_SVIDEO_INPUT:
  549. strscpy(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. f->pixelformat = V4L2_PIX_FMT_YUYV;
  564. return 0;
  565. }
  566. static int usbtv_fmt_vid_cap(struct file *file, void *priv,
  567. struct v4l2_format *f)
  568. {
  569. struct usbtv *usbtv = video_drvdata(file);
  570. f->fmt.pix.width = usbtv->width;
  571. f->fmt.pix.height = usbtv->height;
  572. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  573. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  574. f->fmt.pix.bytesperline = usbtv->width * 2;
  575. f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
  576. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  577. return 0;
  578. }
  579. static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  580. {
  581. struct usbtv *usbtv = video_drvdata(file);
  582. *norm = usbtv->norm;
  583. return 0;
  584. }
  585. static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
  586. {
  587. int ret = -EINVAL;
  588. struct usbtv *usbtv = video_drvdata(file);
  589. if (norm & USBTV_TV_STD)
  590. ret = usbtv_select_norm(usbtv, norm);
  591. return ret;
  592. }
  593. static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
  594. {
  595. struct usbtv *usbtv = video_drvdata(file);
  596. *i = usbtv->input;
  597. return 0;
  598. }
  599. static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
  600. {
  601. struct usbtv *usbtv = video_drvdata(file);
  602. return usbtv_select_input(usbtv, i);
  603. }
  604. static const struct v4l2_ioctl_ops usbtv_ioctl_ops = {
  605. .vidioc_querycap = usbtv_querycap,
  606. .vidioc_enum_input = usbtv_enum_input,
  607. .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
  608. .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
  609. .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
  610. .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
  611. .vidioc_g_std = usbtv_g_std,
  612. .vidioc_s_std = usbtv_s_std,
  613. .vidioc_g_input = usbtv_g_input,
  614. .vidioc_s_input = usbtv_s_input,
  615. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  616. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  617. .vidioc_querybuf = vb2_ioctl_querybuf,
  618. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  619. .vidioc_qbuf = vb2_ioctl_qbuf,
  620. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  621. .vidioc_streamon = vb2_ioctl_streamon,
  622. .vidioc_streamoff = vb2_ioctl_streamoff,
  623. };
  624. static const struct v4l2_file_operations usbtv_fops = {
  625. .owner = THIS_MODULE,
  626. .unlocked_ioctl = video_ioctl2,
  627. .mmap = vb2_fop_mmap,
  628. .open = v4l2_fh_open,
  629. .release = vb2_fop_release,
  630. .read = vb2_fop_read,
  631. .poll = vb2_fop_poll,
  632. };
  633. static int usbtv_queue_setup(struct vb2_queue *vq,
  634. unsigned int *nbuffers,
  635. unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
  636. {
  637. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  638. unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
  639. if (vq->num_buffers + *nbuffers < 2)
  640. *nbuffers = 2 - vq->num_buffers;
  641. if (*nplanes)
  642. return sizes[0] < size ? -EINVAL : 0;
  643. *nplanes = 1;
  644. sizes[0] = size;
  645. return 0;
  646. }
  647. static void usbtv_buf_queue(struct vb2_buffer *vb)
  648. {
  649. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  650. struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
  651. struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb);
  652. unsigned long flags;
  653. if (usbtv->udev == NULL) {
  654. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  655. return;
  656. }
  657. spin_lock_irqsave(&usbtv->buflock, flags);
  658. list_add_tail(&buf->list, &usbtv->bufs);
  659. spin_unlock_irqrestore(&usbtv->buflock, flags);
  660. }
  661. static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
  662. {
  663. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  664. if (usbtv->udev == NULL)
  665. return -ENODEV;
  666. usbtv->last_odd = 1;
  667. usbtv->sequence = 0;
  668. return usbtv_start(usbtv);
  669. }
  670. static void usbtv_stop_streaming(struct vb2_queue *vq)
  671. {
  672. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  673. if (usbtv->udev)
  674. usbtv_stop(usbtv);
  675. }
  676. static const struct vb2_ops usbtv_vb2_ops = {
  677. .queue_setup = usbtv_queue_setup,
  678. .buf_queue = usbtv_buf_queue,
  679. .start_streaming = usbtv_start_streaming,
  680. .stop_streaming = usbtv_stop_streaming,
  681. .wait_prepare = vb2_ops_wait_prepare,
  682. .wait_finish = vb2_ops_wait_finish,
  683. };
  684. static int usbtv_s_ctrl(struct v4l2_ctrl *ctrl)
  685. {
  686. struct usbtv *usbtv = container_of(ctrl->handler, struct usbtv,
  687. ctrl);
  688. u8 *data;
  689. u16 index, size;
  690. int ret;
  691. data = kmalloc(3, GFP_KERNEL);
  692. if (!data)
  693. return -ENOMEM;
  694. /*
  695. * Read in the current brightness/contrast registers. We need them
  696. * both, because the values are for some reason interleaved.
  697. */
  698. if (ctrl->id == V4L2_CID_BRIGHTNESS || ctrl->id == V4L2_CID_CONTRAST) {
  699. ret = usb_control_msg(usbtv->udev,
  700. usb_rcvctrlpipe(usbtv->udev, 0), USBTV_CONTROL_REG,
  701. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  702. 0, USBTV_BASE + 0x0244, (void *)data, 3,
  703. USB_CTRL_GET_TIMEOUT);
  704. if (ret < 0)
  705. goto error;
  706. }
  707. switch (ctrl->id) {
  708. case V4L2_CID_BRIGHTNESS:
  709. index = USBTV_BASE + 0x0244;
  710. size = 3;
  711. data[0] &= 0xf0;
  712. data[0] |= (ctrl->val >> 8) & 0xf;
  713. data[2] = ctrl->val & 0xff;
  714. break;
  715. case V4L2_CID_CONTRAST:
  716. index = USBTV_BASE + 0x0244;
  717. size = 3;
  718. data[0] &= 0x0f;
  719. data[0] |= (ctrl->val >> 4) & 0xf0;
  720. data[1] = ctrl->val & 0xff;
  721. break;
  722. case V4L2_CID_SATURATION:
  723. index = USBTV_BASE + 0x0242;
  724. data[0] = ctrl->val >> 8;
  725. data[1] = ctrl->val & 0xff;
  726. size = 2;
  727. break;
  728. case V4L2_CID_HUE:
  729. index = USBTV_BASE + 0x0240;
  730. size = 2;
  731. if (ctrl->val > 0) {
  732. data[0] = 0x92 + (ctrl->val >> 8);
  733. data[1] = ctrl->val & 0xff;
  734. } else {
  735. data[0] = 0x82 + (-ctrl->val >> 8);
  736. data[1] = -ctrl->val & 0xff;
  737. }
  738. break;
  739. case V4L2_CID_SHARPNESS:
  740. index = USBTV_BASE + 0x0239;
  741. data[0] = 0;
  742. data[1] = ctrl->val;
  743. size = 2;
  744. break;
  745. default:
  746. kfree(data);
  747. return -EINVAL;
  748. }
  749. ret = usb_control_msg(usbtv->udev, usb_sndctrlpipe(usbtv->udev, 0),
  750. USBTV_CONTROL_REG,
  751. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  752. 0, index, (void *)data, size, USB_CTRL_SET_TIMEOUT);
  753. error:
  754. if (ret < 0)
  755. dev_warn(usbtv->dev, "Failed to submit a control request.\n");
  756. kfree(data);
  757. return ret;
  758. }
  759. static const struct v4l2_ctrl_ops usbtv_ctrl_ops = {
  760. .s_ctrl = usbtv_s_ctrl,
  761. };
  762. static void usbtv_release(struct v4l2_device *v4l2_dev)
  763. {
  764. struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
  765. v4l2_device_unregister(&usbtv->v4l2_dev);
  766. v4l2_ctrl_handler_free(&usbtv->ctrl);
  767. kfree(usbtv);
  768. }
  769. int usbtv_video_init(struct usbtv *usbtv)
  770. {
  771. int ret;
  772. (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60);
  773. spin_lock_init(&usbtv->buflock);
  774. mutex_init(&usbtv->v4l2_lock);
  775. mutex_init(&usbtv->vb2q_lock);
  776. INIT_LIST_HEAD(&usbtv->bufs);
  777. /* videobuf2 structure */
  778. usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  779. usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  780. usbtv->vb2q.drv_priv = usbtv;
  781. usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
  782. usbtv->vb2q.ops = &usbtv_vb2_ops;
  783. usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
  784. usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  785. usbtv->vb2q.lock = &usbtv->vb2q_lock;
  786. ret = vb2_queue_init(&usbtv->vb2q);
  787. if (ret < 0) {
  788. dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n");
  789. return ret;
  790. }
  791. /* controls */
  792. v4l2_ctrl_handler_init(&usbtv->ctrl, 4);
  793. v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
  794. V4L2_CID_CONTRAST, 0, 0x3ff, 1, 0x1d0);
  795. v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
  796. V4L2_CID_BRIGHTNESS, 0, 0x3ff, 1, 0x1c0);
  797. v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
  798. V4L2_CID_SATURATION, 0, 0x3ff, 1, 0x200);
  799. v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
  800. V4L2_CID_HUE, -0xdff, 0xdff, 1, 0x000);
  801. v4l2_ctrl_new_std(&usbtv->ctrl, &usbtv_ctrl_ops,
  802. V4L2_CID_SHARPNESS, 0x0, 0xff, 1, 0x60);
  803. ret = usbtv->ctrl.error;
  804. if (ret < 0) {
  805. dev_warn(usbtv->dev, "Could not initialize controls\n");
  806. goto ctrl_fail;
  807. }
  808. /* v4l2 structure */
  809. usbtv->v4l2_dev.ctrl_handler = &usbtv->ctrl;
  810. usbtv->v4l2_dev.release = usbtv_release;
  811. ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev);
  812. if (ret < 0) {
  813. dev_warn(usbtv->dev, "Could not register v4l2 device\n");
  814. goto v4l2_fail;
  815. }
  816. /* Video structure */
  817. strscpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
  818. usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
  819. usbtv->vdev.release = video_device_release_empty;
  820. usbtv->vdev.fops = &usbtv_fops;
  821. usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
  822. usbtv->vdev.tvnorms = USBTV_TV_STD;
  823. usbtv->vdev.queue = &usbtv->vb2q;
  824. usbtv->vdev.lock = &usbtv->v4l2_lock;
  825. usbtv->vdev.device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  826. V4L2_CAP_STREAMING;
  827. video_set_drvdata(&usbtv->vdev, usbtv);
  828. ret = video_register_device(&usbtv->vdev, VFL_TYPE_VIDEO, -1);
  829. if (ret < 0) {
  830. dev_warn(usbtv->dev, "Could not register video device\n");
  831. goto vdev_fail;
  832. }
  833. return 0;
  834. vdev_fail:
  835. v4l2_device_unregister(&usbtv->v4l2_dev);
  836. v4l2_fail:
  837. ctrl_fail:
  838. v4l2_ctrl_handler_free(&usbtv->ctrl);
  839. return ret;
  840. }
  841. void usbtv_video_free(struct usbtv *usbtv)
  842. {
  843. mutex_lock(&usbtv->vb2q_lock);
  844. mutex_lock(&usbtv->v4l2_lock);
  845. usbtv_stop(usbtv);
  846. vb2_video_unregister_device(&usbtv->vdev);
  847. v4l2_device_disconnect(&usbtv->v4l2_dev);
  848. mutex_unlock(&usbtv->v4l2_lock);
  849. mutex_unlock(&usbtv->vb2q_lock);
  850. v4l2_device_put(&usbtv->v4l2_dev);
  851. }