usbtv-video.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. * Fushicai USBTV007 Audio-Video Grabber Driver
  3. *
  4. * Product web site:
  5. * http://www.fushicai.com/products_detail/&productId=d05449ee-b690-42f9-a661-aa7353894bed.html
  6. *
  7. * Following LWN articles were very useful in construction of this driver:
  8. * Video4Linux2 API series: http://lwn.net/Articles/203924/
  9. * videobuf2 API explanation: http://lwn.net/Articles/447435/
  10. * Thanks go to Jonathan Corbet for providing this quality documentation.
  11. * He is awesome.
  12. *
  13. * Copyright (c) 2013 Lubomir Rintel
  14. * All rights reserved.
  15. * No physical hardware was harmed running Windows during the
  16. * reverse-engineering activity
  17. *
  18. * Redistribution and use in source and binary forms, with or without
  19. * modification, are permitted provided that the following conditions
  20. * are met:
  21. * 1. Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions, and the following disclaimer,
  23. * without modification.
  24. * 2. The name of the author may not be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * Alternatively, this software may be distributed under the terms of the
  28. * GNU General Public License ("GPL").
  29. */
  30. #include <media/v4l2-ioctl.h>
  31. #include <media/videobuf2-v4l2.h>
  32. #include "usbtv.h"
  33. static struct usbtv_norm_params norm_params[] = {
  34. {
  35. .norm = V4L2_STD_525_60,
  36. .cap_width = 720,
  37. .cap_height = 480,
  38. },
  39. {
  40. .norm = V4L2_STD_PAL,
  41. .cap_width = 720,
  42. .cap_height = 576,
  43. }
  44. };
  45. static int usbtv_configure_for_norm(struct usbtv *usbtv, v4l2_std_id norm)
  46. {
  47. int i, ret = 0;
  48. struct usbtv_norm_params *params = NULL;
  49. for (i = 0; i < ARRAY_SIZE(norm_params); i++) {
  50. if (norm_params[i].norm & norm) {
  51. params = &norm_params[i];
  52. break;
  53. }
  54. }
  55. if (params) {
  56. usbtv->width = params->cap_width;
  57. usbtv->height = params->cap_height;
  58. usbtv->n_chunks = usbtv->width * usbtv->height
  59. / 4 / USBTV_CHUNK;
  60. usbtv->norm = params->norm;
  61. } else
  62. ret = -EINVAL;
  63. return ret;
  64. }
  65. static int usbtv_select_input(struct usbtv *usbtv, int input)
  66. {
  67. int ret;
  68. static const u16 composite[][2] = {
  69. { USBTV_BASE + 0x0105, 0x0060 },
  70. { USBTV_BASE + 0x011f, 0x00f2 },
  71. { USBTV_BASE + 0x0127, 0x0060 },
  72. { USBTV_BASE + 0x00ae, 0x0010 },
  73. { USBTV_BASE + 0x0239, 0x0060 },
  74. };
  75. static const u16 svideo[][2] = {
  76. { USBTV_BASE + 0x0105, 0x0010 },
  77. { USBTV_BASE + 0x011f, 0x00ff },
  78. { USBTV_BASE + 0x0127, 0x0060 },
  79. { USBTV_BASE + 0x00ae, 0x0030 },
  80. { USBTV_BASE + 0x0239, 0x0060 },
  81. };
  82. switch (input) {
  83. case USBTV_COMPOSITE_INPUT:
  84. ret = usbtv_set_regs(usbtv, composite, ARRAY_SIZE(composite));
  85. break;
  86. case USBTV_SVIDEO_INPUT:
  87. ret = usbtv_set_regs(usbtv, svideo, ARRAY_SIZE(svideo));
  88. break;
  89. default:
  90. ret = -EINVAL;
  91. }
  92. if (!ret)
  93. usbtv->input = input;
  94. return ret;
  95. }
  96. static int usbtv_select_norm(struct usbtv *usbtv, v4l2_std_id norm)
  97. {
  98. int ret;
  99. static const u16 pal[][2] = {
  100. { USBTV_BASE + 0x001a, 0x0068 },
  101. { USBTV_BASE + 0x010e, 0x0072 },
  102. { USBTV_BASE + 0x010f, 0x00a2 },
  103. { USBTV_BASE + 0x0112, 0x00b0 },
  104. { USBTV_BASE + 0x0117, 0x0001 },
  105. { USBTV_BASE + 0x0118, 0x002c },
  106. { USBTV_BASE + 0x012d, 0x0010 },
  107. { USBTV_BASE + 0x012f, 0x0020 },
  108. { USBTV_BASE + 0x024f, 0x0002 },
  109. { USBTV_BASE + 0x0254, 0x0059 },
  110. { USBTV_BASE + 0x025a, 0x0016 },
  111. { USBTV_BASE + 0x025b, 0x0035 },
  112. { USBTV_BASE + 0x0263, 0x0017 },
  113. { USBTV_BASE + 0x0266, 0x0016 },
  114. { USBTV_BASE + 0x0267, 0x0036 }
  115. };
  116. static const u16 ntsc[][2] = {
  117. { USBTV_BASE + 0x001a, 0x0079 },
  118. { USBTV_BASE + 0x010e, 0x0068 },
  119. { USBTV_BASE + 0x010f, 0x009c },
  120. { USBTV_BASE + 0x0112, 0x00f0 },
  121. { USBTV_BASE + 0x0117, 0x0000 },
  122. { USBTV_BASE + 0x0118, 0x00fc },
  123. { USBTV_BASE + 0x012d, 0x0004 },
  124. { USBTV_BASE + 0x012f, 0x0008 },
  125. { USBTV_BASE + 0x024f, 0x0001 },
  126. { USBTV_BASE + 0x0254, 0x005f },
  127. { USBTV_BASE + 0x025a, 0x0012 },
  128. { USBTV_BASE + 0x025b, 0x0001 },
  129. { USBTV_BASE + 0x0263, 0x001c },
  130. { USBTV_BASE + 0x0266, 0x0011 },
  131. { USBTV_BASE + 0x0267, 0x0005 }
  132. };
  133. ret = usbtv_configure_for_norm(usbtv, norm);
  134. if (!ret) {
  135. if (norm & V4L2_STD_525_60)
  136. ret = usbtv_set_regs(usbtv, ntsc, ARRAY_SIZE(ntsc));
  137. else if (norm & V4L2_STD_PAL)
  138. ret = usbtv_set_regs(usbtv, pal, ARRAY_SIZE(pal));
  139. }
  140. return ret;
  141. }
  142. static int usbtv_setup_capture(struct usbtv *usbtv)
  143. {
  144. int ret;
  145. static const u16 setup[][2] = {
  146. /* These seem to enable the device. */
  147. { USBTV_BASE + 0x0008, 0x0001 },
  148. { USBTV_BASE + 0x01d0, 0x00ff },
  149. { USBTV_BASE + 0x01d9, 0x0002 },
  150. /* These seem to influence color parameters, such as
  151. * brightness, etc. */
  152. { USBTV_BASE + 0x0239, 0x0040 },
  153. { USBTV_BASE + 0x0240, 0x0000 },
  154. { USBTV_BASE + 0x0241, 0x0000 },
  155. { USBTV_BASE + 0x0242, 0x0002 },
  156. { USBTV_BASE + 0x0243, 0x0080 },
  157. { USBTV_BASE + 0x0244, 0x0012 },
  158. { USBTV_BASE + 0x0245, 0x0090 },
  159. { USBTV_BASE + 0x0246, 0x0000 },
  160. { USBTV_BASE + 0x0278, 0x002d },
  161. { USBTV_BASE + 0x0279, 0x000a },
  162. { USBTV_BASE + 0x027a, 0x0032 },
  163. { 0xf890, 0x000c },
  164. { 0xf894, 0x0086 },
  165. { USBTV_BASE + 0x00ac, 0x00c0 },
  166. { USBTV_BASE + 0x00ad, 0x0000 },
  167. { USBTV_BASE + 0x00a2, 0x0012 },
  168. { USBTV_BASE + 0x00a3, 0x00e0 },
  169. { USBTV_BASE + 0x00a4, 0x0028 },
  170. { USBTV_BASE + 0x00a5, 0x0082 },
  171. { USBTV_BASE + 0x00a7, 0x0080 },
  172. { USBTV_BASE + 0x0000, 0x0014 },
  173. { USBTV_BASE + 0x0006, 0x0003 },
  174. { USBTV_BASE + 0x0090, 0x0099 },
  175. { USBTV_BASE + 0x0091, 0x0090 },
  176. { USBTV_BASE + 0x0094, 0x0068 },
  177. { USBTV_BASE + 0x0095, 0x0070 },
  178. { USBTV_BASE + 0x009c, 0x0030 },
  179. { USBTV_BASE + 0x009d, 0x00c0 },
  180. { USBTV_BASE + 0x009e, 0x00e0 },
  181. { USBTV_BASE + 0x0019, 0x0006 },
  182. { USBTV_BASE + 0x008c, 0x00ba },
  183. { USBTV_BASE + 0x0101, 0x00ff },
  184. { USBTV_BASE + 0x010c, 0x00b3 },
  185. { USBTV_BASE + 0x01b2, 0x0080 },
  186. { USBTV_BASE + 0x01b4, 0x00a0 },
  187. { USBTV_BASE + 0x014c, 0x00ff },
  188. { USBTV_BASE + 0x014d, 0x00ca },
  189. { USBTV_BASE + 0x0113, 0x0053 },
  190. { USBTV_BASE + 0x0119, 0x008a },
  191. { USBTV_BASE + 0x013c, 0x0003 },
  192. { USBTV_BASE + 0x0150, 0x009c },
  193. { USBTV_BASE + 0x0151, 0x0071 },
  194. { USBTV_BASE + 0x0152, 0x00c6 },
  195. { USBTV_BASE + 0x0153, 0x0084 },
  196. { USBTV_BASE + 0x0154, 0x00bc },
  197. { USBTV_BASE + 0x0155, 0x00a0 },
  198. { USBTV_BASE + 0x0156, 0x00a0 },
  199. { USBTV_BASE + 0x0157, 0x009c },
  200. { USBTV_BASE + 0x0158, 0x001f },
  201. { USBTV_BASE + 0x0159, 0x0006 },
  202. { USBTV_BASE + 0x015d, 0x0000 },
  203. { USBTV_BASE + 0x0003, 0x0004 },
  204. { USBTV_BASE + 0x0100, 0x00d3 },
  205. { USBTV_BASE + 0x0115, 0x0015 },
  206. { USBTV_BASE + 0x0220, 0x002e },
  207. { USBTV_BASE + 0x0225, 0x0008 },
  208. { USBTV_BASE + 0x024e, 0x0002 },
  209. { USBTV_BASE + 0x024e, 0x0002 },
  210. { USBTV_BASE + 0x024f, 0x0002 },
  211. };
  212. ret = usbtv_set_regs(usbtv, setup, ARRAY_SIZE(setup));
  213. if (ret)
  214. return ret;
  215. ret = usbtv_select_norm(usbtv, usbtv->norm);
  216. if (ret)
  217. return ret;
  218. ret = usbtv_select_input(usbtv, usbtv->input);
  219. if (ret)
  220. return ret;
  221. return 0;
  222. }
  223. /* Copy data from chunk into a frame buffer, deinterlacing the data
  224. * into every second line. Unfortunately, they don't align nicely into
  225. * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
  226. * Therefore, we break down the chunk into two halves before copyting,
  227. * so that we can interleave a line if needed. */
  228. static void usbtv_chunk_to_vbuf(u32 *frame, __be32 *src, int chunk_no, int odd)
  229. {
  230. int half;
  231. for (half = 0; half < 2; half++) {
  232. int part_no = chunk_no * 2 + half;
  233. int line = part_no / 3;
  234. int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
  235. u32 *dst = &frame[part_index * USBTV_CHUNK/2];
  236. memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
  237. src += USBTV_CHUNK/2;
  238. }
  239. }
  240. /* Called for each 256-byte image chunk.
  241. * First word identifies the chunk, followed by 240 words of image
  242. * data and padding. */
  243. static void usbtv_image_chunk(struct usbtv *usbtv, __be32 *chunk)
  244. {
  245. int frame_id, odd, chunk_no;
  246. u32 *frame;
  247. struct usbtv_buf *buf;
  248. unsigned long flags;
  249. /* Ignore corrupted lines. */
  250. if (!USBTV_MAGIC_OK(chunk))
  251. return;
  252. frame_id = USBTV_FRAME_ID(chunk);
  253. odd = USBTV_ODD(chunk);
  254. chunk_no = USBTV_CHUNK_NO(chunk);
  255. if (chunk_no >= usbtv->n_chunks)
  256. return;
  257. /* Beginning of a frame. */
  258. if (chunk_no == 0) {
  259. usbtv->frame_id = frame_id;
  260. usbtv->chunks_done = 0;
  261. }
  262. if (usbtv->frame_id != frame_id)
  263. return;
  264. spin_lock_irqsave(&usbtv->buflock, flags);
  265. if (list_empty(&usbtv->bufs)) {
  266. /* No free buffers. Userspace likely too slow. */
  267. spin_unlock_irqrestore(&usbtv->buflock, flags);
  268. return;
  269. }
  270. /* First available buffer. */
  271. buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
  272. frame = vb2_plane_vaddr(&buf->vb.vb2_buf, 0);
  273. /* Copy the chunk data. */
  274. usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
  275. usbtv->chunks_done++;
  276. /* Last chunk in a frame, signalling an end */
  277. if (odd && chunk_no == usbtv->n_chunks-1) {
  278. int size = vb2_plane_size(&buf->vb.vb2_buf, 0);
  279. enum vb2_buffer_state state = usbtv->chunks_done ==
  280. usbtv->n_chunks ?
  281. VB2_BUF_STATE_DONE :
  282. VB2_BUF_STATE_ERROR;
  283. buf->vb.field = V4L2_FIELD_INTERLACED;
  284. buf->vb.sequence = usbtv->sequence++;
  285. v4l2_get_timestamp(&buf->vb.timestamp);
  286. vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
  287. vb2_buffer_done(&buf->vb.vb2_buf, state);
  288. list_del(&buf->list);
  289. }
  290. spin_unlock_irqrestore(&usbtv->buflock, flags);
  291. }
  292. /* Got image data. Each packet contains a number of 256-word chunks we
  293. * compose the image from. */
  294. static void usbtv_iso_cb(struct urb *ip)
  295. {
  296. int ret;
  297. int i;
  298. struct usbtv *usbtv = (struct usbtv *)ip->context;
  299. switch (ip->status) {
  300. /* All fine. */
  301. case 0:
  302. break;
  303. /* Device disconnected or capture stopped? */
  304. case -ENODEV:
  305. case -ENOENT:
  306. case -ECONNRESET:
  307. case -ESHUTDOWN:
  308. return;
  309. /* Unknown error. Retry. */
  310. default:
  311. dev_warn(usbtv->dev, "Bad response for ISO request.\n");
  312. goto resubmit;
  313. }
  314. for (i = 0; i < ip->number_of_packets; i++) {
  315. int size = ip->iso_frame_desc[i].actual_length;
  316. unsigned char *data = ip->transfer_buffer +
  317. ip->iso_frame_desc[i].offset;
  318. int offset;
  319. for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
  320. usbtv_image_chunk(usbtv,
  321. (__be32 *)&data[USBTV_CHUNK_SIZE * offset]);
  322. }
  323. resubmit:
  324. ret = usb_submit_urb(ip, GFP_ATOMIC);
  325. if (ret < 0)
  326. dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
  327. }
  328. static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
  329. {
  330. struct urb *ip;
  331. int size = usbtv->iso_size;
  332. int i;
  333. ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
  334. if (ip == NULL)
  335. return NULL;
  336. ip->dev = usbtv->udev;
  337. ip->context = usbtv;
  338. ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
  339. ip->interval = 1;
  340. ip->transfer_flags = URB_ISO_ASAP;
  341. ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS,
  342. GFP_KERNEL);
  343. ip->complete = usbtv_iso_cb;
  344. ip->number_of_packets = USBTV_ISOC_PACKETS;
  345. ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
  346. for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
  347. ip->iso_frame_desc[i].offset = size * i;
  348. ip->iso_frame_desc[i].length = size;
  349. }
  350. return ip;
  351. }
  352. static void usbtv_stop(struct usbtv *usbtv)
  353. {
  354. int i;
  355. unsigned long flags;
  356. /* Cancel running transfers. */
  357. for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
  358. struct urb *ip = usbtv->isoc_urbs[i];
  359. if (ip == NULL)
  360. continue;
  361. usb_kill_urb(ip);
  362. kfree(ip->transfer_buffer);
  363. usb_free_urb(ip);
  364. usbtv->isoc_urbs[i] = NULL;
  365. }
  366. /* Return buffers to userspace. */
  367. spin_lock_irqsave(&usbtv->buflock, flags);
  368. while (!list_empty(&usbtv->bufs)) {
  369. struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
  370. struct usbtv_buf, list);
  371. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  372. list_del(&buf->list);
  373. }
  374. spin_unlock_irqrestore(&usbtv->buflock, flags);
  375. }
  376. static int usbtv_start(struct usbtv *usbtv)
  377. {
  378. int i;
  379. int ret;
  380. usbtv_audio_suspend(usbtv);
  381. ret = usb_set_interface(usbtv->udev, 0, 0);
  382. if (ret < 0)
  383. return ret;
  384. ret = usbtv_setup_capture(usbtv);
  385. if (ret < 0)
  386. return ret;
  387. ret = usb_set_interface(usbtv->udev, 0, 1);
  388. if (ret < 0)
  389. return ret;
  390. usbtv_audio_resume(usbtv);
  391. for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
  392. struct urb *ip;
  393. ip = usbtv_setup_iso_transfer(usbtv);
  394. if (ip == NULL) {
  395. ret = -ENOMEM;
  396. goto start_fail;
  397. }
  398. usbtv->isoc_urbs[i] = ip;
  399. ret = usb_submit_urb(ip, GFP_KERNEL);
  400. if (ret < 0)
  401. goto start_fail;
  402. }
  403. return 0;
  404. start_fail:
  405. usbtv_stop(usbtv);
  406. return ret;
  407. }
  408. static int usbtv_querycap(struct file *file, void *priv,
  409. struct v4l2_capability *cap)
  410. {
  411. struct usbtv *dev = video_drvdata(file);
  412. strlcpy(cap->driver, "usbtv", sizeof(cap->driver));
  413. strlcpy(cap->card, "usbtv", sizeof(cap->card));
  414. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  415. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
  416. cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  417. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  418. return 0;
  419. }
  420. static int usbtv_enum_input(struct file *file, void *priv,
  421. struct v4l2_input *i)
  422. {
  423. struct usbtv *dev = video_drvdata(file);
  424. switch (i->index) {
  425. case USBTV_COMPOSITE_INPUT:
  426. strlcpy(i->name, "Composite", sizeof(i->name));
  427. break;
  428. case USBTV_SVIDEO_INPUT:
  429. strlcpy(i->name, "S-Video", sizeof(i->name));
  430. break;
  431. default:
  432. return -EINVAL;
  433. }
  434. i->type = V4L2_INPUT_TYPE_CAMERA;
  435. i->std = dev->vdev.tvnorms;
  436. return 0;
  437. }
  438. static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv,
  439. struct v4l2_fmtdesc *f)
  440. {
  441. if (f->index > 0)
  442. return -EINVAL;
  443. strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
  444. sizeof(f->description));
  445. f->pixelformat = V4L2_PIX_FMT_YUYV;
  446. return 0;
  447. }
  448. static int usbtv_fmt_vid_cap(struct file *file, void *priv,
  449. struct v4l2_format *f)
  450. {
  451. struct usbtv *usbtv = video_drvdata(file);
  452. f->fmt.pix.width = usbtv->width;
  453. f->fmt.pix.height = usbtv->height;
  454. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  455. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  456. f->fmt.pix.bytesperline = usbtv->width * 2;
  457. f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
  458. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  459. return 0;
  460. }
  461. static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  462. {
  463. struct usbtv *usbtv = video_drvdata(file);
  464. *norm = usbtv->norm;
  465. return 0;
  466. }
  467. static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
  468. {
  469. int ret = -EINVAL;
  470. struct usbtv *usbtv = video_drvdata(file);
  471. if ((norm & V4L2_STD_525_60) || (norm & V4L2_STD_PAL))
  472. ret = usbtv_select_norm(usbtv, norm);
  473. return ret;
  474. }
  475. static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
  476. {
  477. struct usbtv *usbtv = video_drvdata(file);
  478. *i = usbtv->input;
  479. return 0;
  480. }
  481. static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
  482. {
  483. struct usbtv *usbtv = video_drvdata(file);
  484. return usbtv_select_input(usbtv, i);
  485. }
  486. static struct v4l2_ioctl_ops usbtv_ioctl_ops = {
  487. .vidioc_querycap = usbtv_querycap,
  488. .vidioc_enum_input = usbtv_enum_input,
  489. .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
  490. .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
  491. .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
  492. .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
  493. .vidioc_g_std = usbtv_g_std,
  494. .vidioc_s_std = usbtv_s_std,
  495. .vidioc_g_input = usbtv_g_input,
  496. .vidioc_s_input = usbtv_s_input,
  497. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  498. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  499. .vidioc_querybuf = vb2_ioctl_querybuf,
  500. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  501. .vidioc_qbuf = vb2_ioctl_qbuf,
  502. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  503. .vidioc_streamon = vb2_ioctl_streamon,
  504. .vidioc_streamoff = vb2_ioctl_streamoff,
  505. };
  506. static struct v4l2_file_operations usbtv_fops = {
  507. .owner = THIS_MODULE,
  508. .unlocked_ioctl = video_ioctl2,
  509. .mmap = vb2_fop_mmap,
  510. .open = v4l2_fh_open,
  511. .release = vb2_fop_release,
  512. .read = vb2_fop_read,
  513. .poll = vb2_fop_poll,
  514. };
  515. static int usbtv_queue_setup(struct vb2_queue *vq,
  516. const void *parg, unsigned int *nbuffers,
  517. unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
  518. {
  519. const struct v4l2_format *fmt = parg;
  520. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  521. unsigned size = USBTV_CHUNK * usbtv->n_chunks * 2 * sizeof(u32);
  522. if (vq->num_buffers + *nbuffers < 2)
  523. *nbuffers = 2 - vq->num_buffers;
  524. *nplanes = 1;
  525. if (fmt && fmt->fmt.pix.sizeimage < size)
  526. return -EINVAL;
  527. sizes[0] = fmt ? fmt->fmt.pix.sizeimage : size;
  528. return 0;
  529. }
  530. static void usbtv_buf_queue(struct vb2_buffer *vb)
  531. {
  532. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  533. struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
  534. struct usbtv_buf *buf = container_of(vbuf, struct usbtv_buf, vb);
  535. unsigned long flags;
  536. if (usbtv->udev == NULL) {
  537. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  538. return;
  539. }
  540. spin_lock_irqsave(&usbtv->buflock, flags);
  541. list_add_tail(&buf->list, &usbtv->bufs);
  542. spin_unlock_irqrestore(&usbtv->buflock, flags);
  543. }
  544. static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
  545. {
  546. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  547. if (usbtv->udev == NULL)
  548. return -ENODEV;
  549. usbtv->sequence = 0;
  550. return usbtv_start(usbtv);
  551. }
  552. static void usbtv_stop_streaming(struct vb2_queue *vq)
  553. {
  554. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  555. if (usbtv->udev)
  556. usbtv_stop(usbtv);
  557. }
  558. static struct vb2_ops usbtv_vb2_ops = {
  559. .queue_setup = usbtv_queue_setup,
  560. .buf_queue = usbtv_buf_queue,
  561. .start_streaming = usbtv_start_streaming,
  562. .stop_streaming = usbtv_stop_streaming,
  563. };
  564. static void usbtv_release(struct v4l2_device *v4l2_dev)
  565. {
  566. struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
  567. v4l2_device_unregister(&usbtv->v4l2_dev);
  568. vb2_queue_release(&usbtv->vb2q);
  569. kfree(usbtv);
  570. }
  571. int usbtv_video_init(struct usbtv *usbtv)
  572. {
  573. int ret;
  574. (void)usbtv_configure_for_norm(usbtv, V4L2_STD_525_60);
  575. spin_lock_init(&usbtv->buflock);
  576. mutex_init(&usbtv->v4l2_lock);
  577. mutex_init(&usbtv->vb2q_lock);
  578. INIT_LIST_HEAD(&usbtv->bufs);
  579. /* videobuf2 structure */
  580. usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  581. usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  582. usbtv->vb2q.drv_priv = usbtv;
  583. usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
  584. usbtv->vb2q.ops = &usbtv_vb2_ops;
  585. usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
  586. usbtv->vb2q.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  587. usbtv->vb2q.lock = &usbtv->vb2q_lock;
  588. ret = vb2_queue_init(&usbtv->vb2q);
  589. if (ret < 0) {
  590. dev_warn(usbtv->dev, "Could not initialize videobuf2 queue\n");
  591. return ret;
  592. }
  593. /* v4l2 structure */
  594. usbtv->v4l2_dev.release = usbtv_release;
  595. ret = v4l2_device_register(usbtv->dev, &usbtv->v4l2_dev);
  596. if (ret < 0) {
  597. dev_warn(usbtv->dev, "Could not register v4l2 device\n");
  598. goto v4l2_fail;
  599. }
  600. /* Video structure */
  601. strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
  602. usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
  603. usbtv->vdev.release = video_device_release_empty;
  604. usbtv->vdev.fops = &usbtv_fops;
  605. usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
  606. usbtv->vdev.tvnorms = USBTV_TV_STD;
  607. usbtv->vdev.queue = &usbtv->vb2q;
  608. usbtv->vdev.lock = &usbtv->v4l2_lock;
  609. video_set_drvdata(&usbtv->vdev, usbtv);
  610. ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1);
  611. if (ret < 0) {
  612. dev_warn(usbtv->dev, "Could not register video device\n");
  613. goto vdev_fail;
  614. }
  615. return 0;
  616. vdev_fail:
  617. v4l2_device_unregister(&usbtv->v4l2_dev);
  618. v4l2_fail:
  619. vb2_queue_release(&usbtv->vb2q);
  620. return ret;
  621. }
  622. void usbtv_video_free(struct usbtv *usbtv)
  623. {
  624. mutex_lock(&usbtv->vb2q_lock);
  625. mutex_lock(&usbtv->v4l2_lock);
  626. usbtv_stop(usbtv);
  627. video_unregister_device(&usbtv->vdev);
  628. v4l2_device_disconnect(&usbtv->v4l2_dev);
  629. mutex_unlock(&usbtv->v4l2_lock);
  630. mutex_unlock(&usbtv->vb2q_lock);
  631. v4l2_device_put(&usbtv->v4l2_dev);
  632. }