usbtv-video.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. /*
  2. * Copyright (c) 2013 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. return 0;
  236. }
  237. /* Copy data from chunk into a frame buffer, deinterlacing the data
  238. * into every second line. Unfortunately, they don't align nicely into
  239. * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
  240. * Therefore, we break down the chunk into two halves before copying,
  241. * so that we can interleave a line if needed.
  242. *
  243. * Each "chunk" is 240 words; a word in this context equals 4 bytes.
  244. * Image format is YUYV/YUV 4:2:2, consisting of Y Cr Y Cb, defining two
  245. * pixels, the Cr and Cb shared between the two pixels, but each having
  246. * separate Y values. Thus, the 240 words equal 480 pixels. It therefore,
  247. * takes 1.5 chunks to make a 720 pixel-wide line for the frame.
  248. * The image is interlaced, so there is a "scan" of odd lines, followed
  249. * by "scan" of even numbered lines.
  250. *
  251. * Following code is writing the chunks in correct sequence, skipping
  252. * the rows based on "odd" value.
  253. * line 1: chunk[0][ 0..479] chunk[0][480..959] chunk[1][ 0..479]
  254. * line 3: chunk[1][480..959] chunk[2][ 0..479] chunk[2][480..959]
  255. * ...etc.
  256. */
  257. static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd)
  258. {
  259. int half;
  260. for (half = 0; half < 2; half++) {
  261. int part_no = chunk_no * 2 + half;
  262. int line = part_no / 3;
  263. int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
  264. u32 *dst = &frame[part_index * USBTV_CHUNK/2];
  265. memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
  266. src += USBTV_CHUNK/2;
  267. }
  268. }
  269. /* Called for each 256-byte image chunk.
  270. * First word identifies the chunk, followed by 240 words of image
  271. * data and padding. */
  272. static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk)
  273. {
  274. int frame_id, odd, chunk_no;
  275. u32 *frame;
  276. struct usbtv_buf *buf;
  277. unsigned long flags;
  278. /* Ignore corrupted lines. */
  279. if (!USBTV_MAGIC_OK(chunk))
  280. return;
  281. frame_id = USBTV_FRAME_ID(chunk);
  282. odd = USBTV_ODD(chunk);
  283. chunk_no = USBTV_CHUNK_NO(chunk);
  284. if (chunk_no >= usbtv->n_chunks)
  285. return;
  286. /* Beginning of a frame. */
  287. if (chunk_no == 0) {
  288. usbtv->frame_id = frame_id;
  289. usbtv->chunks_done = 0;
  290. }
  291. if (usbtv->frame_id != frame_id)
  292. return;
  293. spin_lock_irqsave(&usbtv->buflock, flags);
  294. if (list_empty(&usbtv->bufs)) {
  295. /* No free buffers. Userspace likely too slow. */
  296. spin_unlock_irqrestore(&usbtv->buflock, flags);
  297. return;
  298. }
  299. /* First available buffer. */
  300. buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
  301. frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  302. /* Copy the chunk data. */
  303. usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
  304. usbtv->chunks_done++;
  305. /* Last chunk in a field */
  306. if (chunk_no == usbtv->n_chunks-1) {
  307. /* Last chunk in a frame, signalling an end */
  308. if (odd && !usbtv->last_odd) {
  309. int size = vb2_plane_size(&buf->vb.vb2_buf, 0);
  310. enum vb2_buffer_state state = usbtv->chunks_done ==
  311. usbtv->n_chunks ?
  312. VB2_BUF_STATE_DONE :
  313. VB2_BUF_STATE_ERROR;
  314. buf->vb.field = V4L2_FIELD_INTERLACED;
  315. buf->vb.sequence = usbtv->sequence++;
  316. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  317. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
  318. vb2_buffer_done(&buf->vb.vb2_buf, state);
  319. list_del(&buf->list);
  320. }
  321. usbtv->last_odd = odd;
  322. }
  323. spin_unlock_irqrestore(&usbtv->buflock, flags);
  324. }
  325. /* Got image data. Each packet contains a number of 256-word chunks we
  326. * compose the image from. */
  327. static void usbtv_iso_cb(struct urb *ip)
  328. {
  329. int ret;
  330. int i;
  331. struct usbtv *usbtv = (struct usbtv *)ip->context;
  332. switch (ip->status) {
  333. /* All fine. */
  334. case 0:
  335. break;
  336. /* Device disconnected or capture stopped? */
  337. case -ENODEV:
  338. case -ENOENT:
  339. case -ECONNRESET:
  340. case -ESHUTDOWN:
  341. return;
  342. /* Unknown error. Retry. */
  343. default:
  344. dev_warn(usbtv->dev, "Bad response for ISO request.\n");
  345. goto resubmit;
  346. }
  347. for (i = 0; i < ip->number_of_packets; i++) {
  348. int size = ip->iso_frame_desc[i].actual_length;
  349. unsigned char *data = ip->transfer_buffer +
  350. ip->iso_frame_desc[i].offset;
  351. int offset;
  352. for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
  353. usbtv_image_chunk(usbtv,
  354. (__be32 *)&data[USBTV_CHUNK_SIZE * offset]);
  355. }
  356. resubmit:
  357. ret = usb_submit_urb(ip, GFP_ATOMIC);
  358. if (ret < 0)
  359. dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
  360. }
  361. static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
  362. {
  363. struct urb *ip;
  364. int size = usbtv->iso_size;
  365. int i;
  366. ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
  367. if (ip == NULL)
  368. return NULL;
  369. ip->dev = usbtv->udev;
  370. ip->context = usbtv;
  371. ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
  372. ip->interval = 1;
  373. ip->transfer_flags = URB_ISO_ASAP;
  374. ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS,
  375. GFP_KERNEL);
  376. if (!ip->transfer_buffer) {
  377. usb_free_urb(ip);
  378. return NULL;
  379. }
  380. ip->complete = usbtv_iso_cb;
  381. ip->number_of_packets = USBTV_ISOC_PACKETS;
  382. ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
  383. for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
  384. ip->iso_frame_desc[i].offset = size * i;
  385. ip->iso_frame_desc[i].length = size;
  386. }
  387. return ip;
  388. }
  389. static void usbtv_stop(struct usbtv *usbtv)
  390. {
  391. int i;
  392. unsigned long flags;
  393. /* Cancel running transfers. */
  394. for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
  395. struct urb *ip = usbtv->isoc_urbs[i];
  396. if (ip == NULL)
  397. continue;
  398. usb_kill_urb(ip);
  399. kfree(ip->transfer_buffer);
  400. usb_free_urb(ip);
  401. usbtv->isoc_urbs[i] = NULL;
  402. }
  403. /* Return buffers to userspace. */
  404. spin_lock_irqsave(&usbtv->buflock, flags);
  405. while (!list_empty(&usbtv->bufs)) {
  406. struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
  407. struct usbtv_buf, list);
  408. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  409. list_del(&buf->list);
  410. }
  411. spin_unlock_irqrestore(&usbtv->buflock, flags);
  412. }
  413. static int usbtv_start(struct usbtv *usbtv)
  414. {
  415. int i;
  416. int ret;
  417. usbtv_audio_suspend(usbtv);
  418. ret = usb_set_interface(usbtv->udev, 0, 0);
  419. if (ret < 0)
  420. return ret;
  421. ret = usbtv_setup_capture(usbtv);
  422. if (ret < 0)
  423. return ret;
  424. ret = usb_set_interface(usbtv->udev, 0, 1);
  425. if (ret < 0)
  426. return ret;
  427. usbtv_audio_resume(usbtv);
  428. for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
  429. struct urb *ip;
  430. ip = usbtv_setup_iso_transfer(usbtv);
  431. if (ip == NULL) {
  432. ret = -ENOMEM;
  433. goto start_fail;
  434. }
  435. usbtv->isoc_urbs[i] = ip;
  436. ret = usb_submit_urb(ip, GFP_KERNEL);
  437. if (ret < 0)
  438. goto start_fail;
  439. }
  440. return 0;
  441. start_fail:
  442. usbtv_stop(usbtv);
  443. return ret;
  444. }
  445. static int usbtv_querycap(struct file *file, void *priv,
  446. struct v4l2_capability *cap)
  447. {
  448. struct usbtv *dev = video_drvdata(file);
  449. strlcpy(cap->driver, "usbtv", sizeof(cap->driver));
  450. strlcpy(cap->card, "usbtv", sizeof(cap->card));
  451. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  452. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
  453. cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  454. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  455. return 0;
  456. }
  457. static int usbtv_enum_input(struct file *file, void *priv,
  458. struct v4l2_input *i)
  459. {
  460. struct usbtv *dev = video_drvdata(file);
  461. switch (i->index) {
  462. case USBTV_COMPOSITE_INPUT:
  463. strlcpy(i->name, "Composite", sizeof(i->name));
  464. break;
  465. case USBTV_SVIDEO_INPUT:
  466. strlcpy(i->name, "S-Video", sizeof(i->name));
  467. break;
  468. default:
  469. return -EINVAL;
  470. }
  471. i->type = V4L2_INPUT_TYPE_CAMERA;
  472. i->std = dev->vdev.tvnorms;
  473. return 0;
  474. }
  475. static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv,
  476. struct v4l2_fmtdesc *f)
  477. {
  478. if (f->index > 0)
  479. return -EINVAL;
  480. strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
  481. sizeof(f->description));
  482. f->pixelformat = V4L2_PIX_FMT_YUYV;
  483. return 0;
  484. }
  485. static int usbtv_fmt_vid_cap(struct file *file, void *priv,
  486. struct v4l2_format *f)
  487. {
  488. struct usbtv *usbtv = video_drvdata(file);
  489. f->fmt.pix.width = usbtv->width;
  490. f->fmt.pix.height = usbtv->height;
  491. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  492. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  493. f->fmt.pix.bytesperline = usbtv->width * 2;
  494. f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
  495. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  496. return 0;
  497. }
  498. static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  499. {
  500. struct usbtv *usbtv = video_drvdata(file);
  501. *norm = usbtv->norm;
  502. return 0;
  503. }
  504. static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
  505. {
  506. int ret = -EINVAL;
  507. struct usbtv *usbtv = video_drvdata(file);
  508. if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL))
  509. ret = usbtv_select_norm(usbtv, norm);
  510. return ret;
  511. }
  512. static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
  513. {
  514. struct usbtv *usbtv = video_drvdata(file);
  515. *i = usbtv->input;
  516. return 0;
  517. }
  518. static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
  519. {
  520. struct usbtv *usbtv = video_drvdata(file);
  521. return usbtv_select_input(usbtv, i);
  522. }
  523. static struct v4l2_ioctl_ops usbtv_ioctl_ops = {
  524. .vidioc_querycap = usbtv_querycap,
  525. .vidioc_enum_input = usbtv_enum_input,
  526. .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
  527. .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
  528. .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
  529. .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
  530. .vidioc_g_std = usbtv_g_std,
  531. .vidioc_s_std = usbtv_s_std,
  532. .vidioc_g_input = usbtv_g_input,
  533. .vidioc_s_input = usbtv_s_input,
  534. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  535. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  536. .vidioc_querybuf = vb2_ioctl_querybuf,
  537. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  538. .vidioc_qbuf = vb2_ioctl_qbuf,
  539. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  540. .vidioc_streamon = vb2_ioctl_streamon,
  541. .vidioc_streamoff = vb2_ioctl_streamoff,
  542. };
  543. static struct v4l2_file_operations usbtv_fops = {
  544. .owner = THIS_MODULE,
  545. .unlocked_ioctl = video_ioctl2,
  546. .mmap = vb2_fop_mmap,
  547. .open = v4l2_fh_open,
  548. .release = vb2_fop_release,
  549. .read = vb2_fop_read,
  550. .poll = vb2_fop_poll,
  551. };
  552. static int usbtv_queue_setup(struct vb2_queue *vq,
  553. unsigned int *nbuffers,
  554. unsigned int *nplanes, unsigned int sizes[], struct device *alloc_devs[])
  555. {
  556. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  557. unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
  558. if (vq->num_buffers + *nbuffers < 2)
  559. *nbuffers = 2 - vq->num_buffers;
  560. if (*nplanes)
  561. return sizes[0] < size ? -EINVAL : 0;
  562. *nplanes = 1;
  563. sizes[0] = size;
  564. return 0;
  565. }
  566. static void usbtv_buf_queue(struct vb2_buffer *vb)
  567. {
  568. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  569. struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
  570. struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb);
  571. unsigned long flags;
  572. if (usbtv->udev == NULL) {
  573. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  574. return;
  575. }
  576. spin_lock_irqsave(&usbtv->buflock, flags);
  577. list_add_tail(&buf->list, &usbtv->bufs);
  578. spin_unlock_irqrestore(&usbtv->buflock, flags);
  579. }
  580. static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
  581. {
  582. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  583. if (usbtv->udev == NULL)
  584. return -ENODEV;
  585. usbtv->last_odd = 1;
  586. usbtv->sequence = 0;
  587. return usbtv_start(usbtv);
  588. }
  589. static void usbtv_stop_streaming(struct vb2_queue *vq)
  590. {
  591. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  592. if (usbtv->udev)
  593. usbtv_stop(usbtv);
  594. }
  595. static const struct vb2_ops usbtv_vb2_ops = {
  596. .queue_setup = usbtv_queue_setup,
  597. .buf_queue = usbtv_buf_queue,
  598. .start_streaming = usbtv_start_streaming,
  599. .stop_streaming = usbtv_stop_streaming,
  600. };
  601. static void usbtv_release(struct v4l2_device *v4l2_dev)
  602. {
  603. struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
  604. v4l2_device_unregister(&usbtv->v4l2_dev);
  605. vb2_queue_release(&usbtv->vb2q);
  606. kfree(usbtv);
  607. }
  608. int usbtv_video_init(struct usbtv *usbtv)
  609. {
  610. int ret;
  611. (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60);
  612. spin_lock_init(&usbtv->buflock);
  613. mutex_init(&usbtv->v4l2_lock);
  614. mutex_init(&usbtv->vb2q_lock);
  615. INIT_LIST_HEAD(&usbtv->bufs);
  616. /* videobuf2 structure */
  617. usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  618. usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  619. usbtv->vb2q.drv_priv = usbtv;
  620. usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
  621. usbtv->vb2q.ops = &usbtv_vb2_ops;
  622. usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
  623. usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  624. usbtv->vb2q.lock = &usbtv->vb2q_lock;
  625. ret = vb2_queue_init(&usbtv->vb2q);
  626. if (ret < 0) {
  627. dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n");
  628. return ret;
  629. }
  630. /* v4l2 structure */
  631. usbtv->v4l2_dev.release = usbtv_release;
  632. ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev);
  633. if (ret < 0) {
  634. dev_warn(usbtv->dev, "Could not register v4l2 device\n");
  635. goto v4l2_fail;
  636. }
  637. /* Video structure */
  638. strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
  639. usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
  640. usbtv->vdev.release = video_device_release_empty;
  641. usbtv->vdev.fops = &usbtv_fops;
  642. usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
  643. usbtv->vdev.tvnorms = USBTV_TV_STD;
  644. usbtv->vdev.queue = &usbtv->vb2q;
  645. usbtv->vdev.lock = &usbtv->v4l2_lock;
  646. video_set_drvdata(&usbtv->vdev, usbtv);
  647. ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1);
  648. if (ret < 0) {
  649. dev_warn(usbtv->dev, "Could not register video device\n");
  650. goto vdev_fail;
  651. }
  652. return 0;
  653. vdev_fail:
  654. v4l2_device_unregister(&usbtv->v4l2_dev);
  655. v4l2_fail:
  656. vb2_queue_release(&usbtv->vb2q);
  657. return ret;
  658. }
  659. void usbtv_video_free(struct usbtv *usbtv)
  660. {
  661. mutex_lock(&usbtv->vb2q_lock);
  662. mutex_lock(&usbtv->v4l2_lock);
  663. usbtv_stop(usbtv);
  664. video_unregister_device(&usbtv->vdev);
  665. v4l2_device_disconnect(&usbtv->v4l2_dev);
  666. mutex_unlock(&usbtv->v4l2_lock);
  667. mutex_unlock(&usbtv->vb2q_lock);
  668. v4l2_device_put(&usbtv->v4l2_dev);
  669. }