usbtv-video.c 23 KB

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