usbtv.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. /*
  2. * Fushicai USBTV007 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 <linux/init.h>
  31. #include <linux/list.h>
  32. #include <linux/module.h>
  33. #include <linux/slab.h>
  34. #include <linux/usb.h>
  35. #include <linux/version.h>
  36. #include <linux/videodev2.h>
  37. #include <media/v4l2-device.h>
  38. #include <media/v4l2-ioctl.h>
  39. #include <media/videobuf2-core.h>
  40. #include <media/videobuf2-vmalloc.h>
  41. /* Hardware. */
  42. #define USBTV_VIDEO_ENDP 0x81
  43. #define USBTV_BASE 0xc000
  44. #define USBTV_REQUEST_REG 12
  45. /* Number of concurrent isochronous urbs submitted.
  46. * Higher numbers was seen to overly saturate the USB bus. */
  47. #define USBTV_ISOC_TRANSFERS 16
  48. #define USBTV_ISOC_PACKETS 8
  49. #define USBTV_WIDTH 720
  50. #define USBTV_HEIGHT 480
  51. #define USBTV_CHUNK_SIZE 256
  52. #define USBTV_CHUNK 240
  53. #define USBTV_CHUNKS (USBTV_WIDTH * USBTV_HEIGHT \
  54. / 4 / USBTV_CHUNK)
  55. /* Chunk header. */
  56. #define USBTV_MAGIC_OK(chunk) ((be32_to_cpu(chunk[0]) & 0xff000000) \
  57. == 0x88000000)
  58. #define USBTV_FRAME_ID(chunk) ((be32_to_cpu(chunk[0]) & 0x00ff0000) >> 16)
  59. #define USBTV_ODD(chunk) ((be32_to_cpu(chunk[0]) & 0x0000f000) >> 15)
  60. #define USBTV_CHUNK_NO(chunk) (be32_to_cpu(chunk[0]) & 0x00000fff)
  61. /* A single videobuf2 frame buffer. */
  62. struct usbtv_buf {
  63. struct vb2_buffer vb;
  64. struct list_head list;
  65. };
  66. /* Per-device structure. */
  67. struct usbtv {
  68. struct device *dev;
  69. struct usb_device *udev;
  70. struct v4l2_device v4l2_dev;
  71. struct video_device vdev;
  72. struct vb2_queue vb2q;
  73. struct mutex v4l2_lock;
  74. struct mutex vb2q_lock;
  75. /* List of videobuf2 buffers protected by a lock. */
  76. spinlock_t buflock;
  77. struct list_head bufs;
  78. /* Number of currently processed frame, useful find
  79. * out when a new one begins. */
  80. u32 frame_id;
  81. int chunks_done;
  82. int iso_size;
  83. unsigned int sequence;
  84. struct urb *isoc_urbs[USBTV_ISOC_TRANSFERS];
  85. };
  86. static int usbtv_setup_capture(struct usbtv *usbtv)
  87. {
  88. int ret;
  89. int pipe = usb_rcvctrlpipe(usbtv->udev, 0);
  90. int i;
  91. static const u16 protoregs[][2] = {
  92. /* These seem to enable the device. */
  93. { USBTV_BASE + 0x0008, 0x0001 },
  94. { USBTV_BASE + 0x01d0, 0x00ff },
  95. { USBTV_BASE + 0x01d9, 0x0002 },
  96. /* These seem to influence color parameters, such as
  97. * brightness, etc. */
  98. { USBTV_BASE + 0x0239, 0x0040 },
  99. { USBTV_BASE + 0x0240, 0x0000 },
  100. { USBTV_BASE + 0x0241, 0x0000 },
  101. { USBTV_BASE + 0x0242, 0x0002 },
  102. { USBTV_BASE + 0x0243, 0x0080 },
  103. { USBTV_BASE + 0x0244, 0x0012 },
  104. { USBTV_BASE + 0x0245, 0x0090 },
  105. { USBTV_BASE + 0x0246, 0x0000 },
  106. { USBTV_BASE + 0x0278, 0x002d },
  107. { USBTV_BASE + 0x0279, 0x000a },
  108. { USBTV_BASE + 0x027a, 0x0032 },
  109. { 0xf890, 0x000c },
  110. { 0xf894, 0x0086 },
  111. { USBTV_BASE + 0x00ac, 0x00c0 },
  112. { USBTV_BASE + 0x00ad, 0x0000 },
  113. { USBTV_BASE + 0x00a2, 0x0012 },
  114. { USBTV_BASE + 0x00a3, 0x00e0 },
  115. { USBTV_BASE + 0x00a4, 0x0028 },
  116. { USBTV_BASE + 0x00a5, 0x0082 },
  117. { USBTV_BASE + 0x00a7, 0x0080 },
  118. { USBTV_BASE + 0x0000, 0x0014 },
  119. { USBTV_BASE + 0x0006, 0x0003 },
  120. { USBTV_BASE + 0x0090, 0x0099 },
  121. { USBTV_BASE + 0x0091, 0x0090 },
  122. { USBTV_BASE + 0x0094, 0x0068 },
  123. { USBTV_BASE + 0x0095, 0x0070 },
  124. { USBTV_BASE + 0x009c, 0x0030 },
  125. { USBTV_BASE + 0x009d, 0x00c0 },
  126. { USBTV_BASE + 0x009e, 0x00e0 },
  127. { USBTV_BASE + 0x0019, 0x0006 },
  128. { USBTV_BASE + 0x008c, 0x00ba },
  129. { USBTV_BASE + 0x0101, 0x00ff },
  130. { USBTV_BASE + 0x010c, 0x00b3 },
  131. { USBTV_BASE + 0x01b2, 0x0080 },
  132. { USBTV_BASE + 0x01b4, 0x00a0 },
  133. { USBTV_BASE + 0x014c, 0x00ff },
  134. { USBTV_BASE + 0x014d, 0x00ca },
  135. { USBTV_BASE + 0x0113, 0x0053 },
  136. { USBTV_BASE + 0x0119, 0x008a },
  137. { USBTV_BASE + 0x013c, 0x0003 },
  138. { USBTV_BASE + 0x0150, 0x009c },
  139. { USBTV_BASE + 0x0151, 0x0071 },
  140. { USBTV_BASE + 0x0152, 0x00c6 },
  141. { USBTV_BASE + 0x0153, 0x0084 },
  142. { USBTV_BASE + 0x0154, 0x00bc },
  143. { USBTV_BASE + 0x0155, 0x00a0 },
  144. { USBTV_BASE + 0x0156, 0x00a0 },
  145. { USBTV_BASE + 0x0157, 0x009c },
  146. { USBTV_BASE + 0x0158, 0x001f },
  147. { USBTV_BASE + 0x0159, 0x0006 },
  148. { USBTV_BASE + 0x015d, 0x0000 },
  149. { USBTV_BASE + 0x0284, 0x0088 },
  150. { USBTV_BASE + 0x0003, 0x0004 },
  151. { USBTV_BASE + 0x001a, 0x0079 },
  152. { USBTV_BASE + 0x0100, 0x00d3 },
  153. { USBTV_BASE + 0x010e, 0x0068 },
  154. { USBTV_BASE + 0x010f, 0x009c },
  155. { USBTV_BASE + 0x0112, 0x00f0 },
  156. { USBTV_BASE + 0x0115, 0x0015 },
  157. { USBTV_BASE + 0x0117, 0x0000 },
  158. { USBTV_BASE + 0x0118, 0x00fc },
  159. { USBTV_BASE + 0x012d, 0x0004 },
  160. { USBTV_BASE + 0x012f, 0x0008 },
  161. { USBTV_BASE + 0x0220, 0x002e },
  162. { USBTV_BASE + 0x0225, 0x0008 },
  163. { USBTV_BASE + 0x024e, 0x0002 },
  164. { USBTV_BASE + 0x024f, 0x0001 },
  165. { USBTV_BASE + 0x0254, 0x005f },
  166. { USBTV_BASE + 0x025a, 0x0012 },
  167. { USBTV_BASE + 0x025b, 0x0001 },
  168. { USBTV_BASE + 0x0263, 0x001c },
  169. { USBTV_BASE + 0x0266, 0x0011 },
  170. { USBTV_BASE + 0x0267, 0x0005 },
  171. { USBTV_BASE + 0x024e, 0x0002 },
  172. { USBTV_BASE + 0x024f, 0x0002 },
  173. };
  174. for (i = 0; i < ARRAY_SIZE(protoregs); i++) {
  175. u16 index = protoregs[i][0];
  176. u16 value = protoregs[i][1];
  177. ret = usb_control_msg(usbtv->udev, pipe, USBTV_REQUEST_REG,
  178. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  179. value, index, NULL, 0, 0);
  180. if (ret < 0)
  181. return ret;
  182. }
  183. return 0;
  184. }
  185. /* Copy data from chunk into a frame buffer, deinterlacing the data
  186. * into every second line. Unfortunately, they don't align nicely into
  187. * 720 pixel lines, as the chunk is 240 words long, which is 480 pixels.
  188. * Therefore, we break down the chunk into two halves before copyting,
  189. * so that we can interleave a line if needed. */
  190. static void usbtv_chunk_to_vbuf(u32 *frame, u32 *src, int chunk_no, int odd)
  191. {
  192. int half;
  193. for (half = 0; half < 2; half++) {
  194. int part_no = chunk_no * 2 + half;
  195. int line = part_no / 3;
  196. int part_index = (line * 2 + !odd) * 3 + (part_no % 3);
  197. u32 *dst = &frame[part_index * USBTV_CHUNK/2];
  198. memcpy(dst, src, USBTV_CHUNK/2 * sizeof(*src));
  199. src += USBTV_CHUNK/2;
  200. }
  201. }
  202. /* Called for each 256-byte image chunk.
  203. * First word identifies the chunk, followed by 240 words of image
  204. * data and padding. */
  205. static void usbtv_image_chunk(struct usbtv *usbtv, u32 *chunk)
  206. {
  207. int frame_id, odd, chunk_no;
  208. u32 *frame;
  209. struct usbtv_buf *buf;
  210. unsigned long flags;
  211. /* Ignore corrupted lines. */
  212. if (!USBTV_MAGIC_OK(chunk))
  213. return;
  214. frame_id = USBTV_FRAME_ID(chunk);
  215. odd = USBTV_ODD(chunk);
  216. chunk_no = USBTV_CHUNK_NO(chunk);
  217. if (chunk_no >= USBTV_CHUNKS)
  218. return;
  219. /* Beginning of a frame. */
  220. if (chunk_no == 0) {
  221. usbtv->frame_id = frame_id;
  222. usbtv->chunks_done = 0;
  223. }
  224. if (usbtv->frame_id != frame_id)
  225. return;
  226. spin_lock_irqsave(&usbtv->buflock, flags);
  227. if (list_empty(&usbtv->bufs)) {
  228. /* No free buffers. Userspace likely too slow. */
  229. spin_unlock_irqrestore(&usbtv->buflock, flags);
  230. return;
  231. }
  232. /* First available buffer. */
  233. buf = list_first_entry(&usbtv->bufs, struct usbtv_buf, list);
  234. frame = vb2_plane_vaddr(&buf->vb, 0);
  235. /* Copy the chunk data. */
  236. usbtv_chunk_to_vbuf(frame, &chunk[1], chunk_no, odd);
  237. usbtv->chunks_done++;
  238. /* Last chunk in a frame, signalling an end */
  239. if (odd && chunk_no == USBTV_CHUNKS-1) {
  240. int size = vb2_plane_size(&buf->vb, 0);
  241. enum vb2_buffer_state state = usbtv->chunks_done ==
  242. USBTV_CHUNKS ?
  243. VB2_BUF_STATE_DONE :
  244. VB2_BUF_STATE_ERROR;
  245. buf->vb.v4l2_buf.field = V4L2_FIELD_INTERLACED;
  246. buf->vb.v4l2_buf.sequence = usbtv->sequence++;
  247. v4l2_get_timestamp(&buf->vb.v4l2_buf.timestamp);
  248. vb2_set_plane_payload(&buf->vb, 0, size);
  249. vb2_buffer_done(&buf->vb, state);
  250. list_del(&buf->list);
  251. }
  252. spin_unlock_irqrestore(&usbtv->buflock, flags);
  253. }
  254. /* Got image data. Each packet contains a number of 256-word chunks we
  255. * compose the image from. */
  256. static void usbtv_iso_cb(struct urb *ip)
  257. {
  258. int ret;
  259. int i;
  260. struct usbtv *usbtv = (struct usbtv *)ip->context;
  261. switch (ip->status) {
  262. /* All fine. */
  263. case 0:
  264. break;
  265. /* Device disconnected or capture stopped? */
  266. case -ENODEV:
  267. case -ENOENT:
  268. case -ECONNRESET:
  269. case -ESHUTDOWN:
  270. return;
  271. /* Unknown error. Retry. */
  272. default:
  273. dev_warn(usbtv->dev, "Bad response for ISO request.\n");
  274. goto resubmit;
  275. }
  276. for (i = 0; i < ip->number_of_packets; i++) {
  277. int size = ip->iso_frame_desc[i].actual_length;
  278. unsigned char *data = ip->transfer_buffer +
  279. ip->iso_frame_desc[i].offset;
  280. int offset;
  281. for (offset = 0; USBTV_CHUNK_SIZE * offset < size; offset++)
  282. usbtv_image_chunk(usbtv,
  283. (u32 *)&data[USBTV_CHUNK_SIZE * offset]);
  284. }
  285. resubmit:
  286. ret = usb_submit_urb(ip, GFP_ATOMIC);
  287. if (ret < 0)
  288. dev_warn(usbtv->dev, "Could not resubmit ISO URB\n");
  289. }
  290. static struct urb *usbtv_setup_iso_transfer(struct usbtv *usbtv)
  291. {
  292. struct urb *ip;
  293. int size = usbtv->iso_size;
  294. int i;
  295. ip = usb_alloc_urb(USBTV_ISOC_PACKETS, GFP_KERNEL);
  296. if (ip == NULL)
  297. return NULL;
  298. ip->dev = usbtv->udev;
  299. ip->context = usbtv;
  300. ip->pipe = usb_rcvisocpipe(usbtv->udev, USBTV_VIDEO_ENDP);
  301. ip->interval = 1;
  302. ip->transfer_flags = URB_ISO_ASAP;
  303. ip->transfer_buffer = kzalloc(size * USBTV_ISOC_PACKETS,
  304. GFP_KERNEL);
  305. ip->complete = usbtv_iso_cb;
  306. ip->number_of_packets = USBTV_ISOC_PACKETS;
  307. ip->transfer_buffer_length = size * USBTV_ISOC_PACKETS;
  308. for (i = 0; i < USBTV_ISOC_PACKETS; i++) {
  309. ip->iso_frame_desc[i].offset = size * i;
  310. ip->iso_frame_desc[i].length = size;
  311. }
  312. return ip;
  313. }
  314. static void usbtv_stop(struct usbtv *usbtv)
  315. {
  316. int i;
  317. unsigned long flags;
  318. /* Cancel running transfers. */
  319. for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
  320. struct urb *ip = usbtv->isoc_urbs[i];
  321. if (ip == NULL)
  322. continue;
  323. usb_kill_urb(ip);
  324. kfree(ip->transfer_buffer);
  325. usb_free_urb(ip);
  326. usbtv->isoc_urbs[i] = NULL;
  327. }
  328. /* Return buffers to userspace. */
  329. spin_lock_irqsave(&usbtv->buflock, flags);
  330. while (!list_empty(&usbtv->bufs)) {
  331. struct usbtv_buf *buf = list_first_entry(&usbtv->bufs,
  332. struct usbtv_buf, list);
  333. vb2_buffer_done(&buf->vb, VB2_BUF_STATE_ERROR);
  334. list_del(&buf->list);
  335. }
  336. spin_unlock_irqrestore(&usbtv->buflock, flags);
  337. }
  338. static int usbtv_start(struct usbtv *usbtv)
  339. {
  340. int i;
  341. int ret;
  342. ret = usb_set_interface(usbtv->udev, 0, 0);
  343. if (ret < 0)
  344. return ret;
  345. ret = usbtv_setup_capture(usbtv);
  346. if (ret < 0)
  347. return ret;
  348. ret = usb_set_interface(usbtv->udev, 0, 1);
  349. if (ret < 0)
  350. return ret;
  351. for (i = 0; i < USBTV_ISOC_TRANSFERS; i++) {
  352. struct urb *ip;
  353. ip = usbtv_setup_iso_transfer(usbtv);
  354. if (ip == NULL) {
  355. ret = -ENOMEM;
  356. goto start_fail;
  357. }
  358. usbtv->isoc_urbs[i] = ip;
  359. ret = usb_submit_urb(ip, GFP_KERNEL);
  360. if (ret < 0)
  361. goto start_fail;
  362. }
  363. return 0;
  364. start_fail:
  365. usbtv_stop(usbtv);
  366. return ret;
  367. }
  368. struct usb_device_id usbtv_id_table[] = {
  369. { USB_DEVICE(0x1b71, 0x3002) },
  370. {}
  371. };
  372. MODULE_DEVICE_TABLE(usb, usbtv_id_table);
  373. static int usbtv_querycap(struct file *file, void *priv,
  374. struct v4l2_capability *cap)
  375. {
  376. struct usbtv *dev = video_drvdata(file);
  377. strlcpy(cap->driver, "usbtv", sizeof(cap->driver));
  378. strlcpy(cap->card, "usbtv", sizeof(cap->card));
  379. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  380. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE;
  381. cap->device_caps |= V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  382. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  383. return 0;
  384. }
  385. static int usbtv_enum_input(struct file *file, void *priv,
  386. struct v4l2_input *i)
  387. {
  388. if (i->index > 0)
  389. return -EINVAL;
  390. strlcpy(i->name, "Composite", sizeof(i->name));
  391. i->type = V4L2_INPUT_TYPE_CAMERA;
  392. i->std = V4L2_STD_525_60;
  393. return 0;
  394. }
  395. static int usbtv_enum_fmt_vid_cap(struct file *file, void *priv,
  396. struct v4l2_fmtdesc *f)
  397. {
  398. if (f->index > 0)
  399. return -EINVAL;
  400. strlcpy(f->description, "16 bpp YUY2, 4:2:2, packed",
  401. sizeof(f->description));
  402. f->pixelformat = V4L2_PIX_FMT_YUYV;
  403. return 0;
  404. }
  405. static int usbtv_fmt_vid_cap(struct file *file, void *priv,
  406. struct v4l2_format *f)
  407. {
  408. f->fmt.pix.width = USBTV_WIDTH;
  409. f->fmt.pix.height = USBTV_HEIGHT;
  410. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  411. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  412. f->fmt.pix.bytesperline = USBTV_WIDTH * 2;
  413. f->fmt.pix.sizeimage = (f->fmt.pix.bytesperline * f->fmt.pix.height);
  414. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  415. f->fmt.pix.priv = 0;
  416. return 0;
  417. }
  418. static int usbtv_g_std(struct file *file, void *priv, v4l2_std_id *norm)
  419. {
  420. *norm = V4L2_STD_525_60;
  421. return 0;
  422. }
  423. static int usbtv_g_input(struct file *file, void *priv, unsigned int *i)
  424. {
  425. *i = 0;
  426. return 0;
  427. }
  428. static int usbtv_s_input(struct file *file, void *priv, unsigned int i)
  429. {
  430. if (i > 0)
  431. return -EINVAL;
  432. return 0;
  433. }
  434. static int usbtv_s_std(struct file *file, void *priv, v4l2_std_id norm)
  435. {
  436. if (norm & V4L2_STD_525_60)
  437. return 0;
  438. return -EINVAL;
  439. }
  440. struct v4l2_ioctl_ops usbtv_ioctl_ops = {
  441. .vidioc_querycap = usbtv_querycap,
  442. .vidioc_enum_input = usbtv_enum_input,
  443. .vidioc_enum_fmt_vid_cap = usbtv_enum_fmt_vid_cap,
  444. .vidioc_g_fmt_vid_cap = usbtv_fmt_vid_cap,
  445. .vidioc_try_fmt_vid_cap = usbtv_fmt_vid_cap,
  446. .vidioc_s_fmt_vid_cap = usbtv_fmt_vid_cap,
  447. .vidioc_g_std = usbtv_g_std,
  448. .vidioc_s_std = usbtv_s_std,
  449. .vidioc_g_input = usbtv_g_input,
  450. .vidioc_s_input = usbtv_s_input,
  451. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  452. .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
  453. .vidioc_querybuf = vb2_ioctl_querybuf,
  454. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  455. .vidioc_qbuf = vb2_ioctl_qbuf,
  456. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  457. .vidioc_streamon = vb2_ioctl_streamon,
  458. .vidioc_streamoff = vb2_ioctl_streamoff,
  459. };
  460. struct v4l2_file_operations usbtv_fops = {
  461. .owner = THIS_MODULE,
  462. .unlocked_ioctl = video_ioctl2,
  463. .mmap = vb2_fop_mmap,
  464. .open = v4l2_fh_open,
  465. .release = vb2_fop_release,
  466. .read = vb2_fop_read,
  467. .poll = vb2_fop_poll,
  468. };
  469. static int usbtv_queue_setup(struct vb2_queue *vq,
  470. const struct v4l2_format *v4l_fmt, unsigned int *nbuffers,
  471. unsigned int *nplanes, unsigned int sizes[], void *alloc_ctxs[])
  472. {
  473. if (*nbuffers < 2)
  474. *nbuffers = 2;
  475. *nplanes = 1;
  476. sizes[0] = USBTV_WIDTH * USBTV_HEIGHT / 2 * sizeof(u32);
  477. return 0;
  478. }
  479. static void usbtv_buf_queue(struct vb2_buffer *vb)
  480. {
  481. struct usbtv *usbtv = vb2_get_drv_priv(vb->vb2_queue);
  482. struct usbtv_buf *buf = container_of(vb, struct usbtv_buf, vb);
  483. unsigned long flags;
  484. if (usbtv->udev == NULL) {
  485. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  486. return;
  487. }
  488. spin_lock_irqsave(&usbtv->buflock, flags);
  489. list_add_tail(&buf->list, &usbtv->bufs);
  490. spin_unlock_irqrestore(&usbtv->buflock, flags);
  491. }
  492. static int usbtv_start_streaming(struct vb2_queue *vq, unsigned int count)
  493. {
  494. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  495. if (usbtv->udev == NULL)
  496. return -ENODEV;
  497. return usbtv_start(usbtv);
  498. }
  499. static int usbtv_stop_streaming(struct vb2_queue *vq)
  500. {
  501. struct usbtv *usbtv = vb2_get_drv_priv(vq);
  502. if (usbtv->udev == NULL)
  503. return -ENODEV;
  504. usbtv_stop(usbtv);
  505. return 0;
  506. }
  507. struct vb2_ops usbtv_vb2_ops = {
  508. .queue_setup = usbtv_queue_setup,
  509. .buf_queue = usbtv_buf_queue,
  510. .start_streaming = usbtv_start_streaming,
  511. .stop_streaming = usbtv_stop_streaming,
  512. };
  513. static void usbtv_release(struct v4l2_device *v4l2_dev)
  514. {
  515. struct usbtv *usbtv = container_of(v4l2_dev, struct usbtv, v4l2_dev);
  516. v4l2_device_unregister(&usbtv->v4l2_dev);
  517. vb2_queue_release(&usbtv->vb2q);
  518. kfree(usbtv);
  519. }
  520. static int usbtv_probe(struct usb_interface *intf,
  521. const struct usb_device_id *id)
  522. {
  523. int ret;
  524. int size;
  525. struct device *dev = &intf->dev;
  526. struct usbtv *usbtv;
  527. /* Checks that the device is what we think it is. */
  528. if (intf->num_altsetting != 2)
  529. return -ENODEV;
  530. if (intf->altsetting[1].desc.bNumEndpoints != 4)
  531. return -ENODEV;
  532. /* Packet size is split into 11 bits of base size and count of
  533. * extra multiplies of it.*/
  534. size = usb_endpoint_maxp(&intf->altsetting[1].endpoint[0].desc);
  535. size = (size & 0x07ff) * (((size & 0x1800) >> 11) + 1);
  536. /* Device structure */
  537. usbtv = kzalloc(sizeof(struct usbtv), GFP_KERNEL);
  538. if (usbtv == NULL)
  539. return -ENOMEM;
  540. usbtv->dev = dev;
  541. usbtv->udev = usb_get_dev(interface_to_usbdev(intf));
  542. usbtv->iso_size = size;
  543. spin_lock_init(&usbtv->buflock);
  544. mutex_init(&usbtv->v4l2_lock);
  545. mutex_init(&usbtv->vb2q_lock);
  546. INIT_LIST_HEAD(&usbtv->bufs);
  547. /* videobuf2 structure */
  548. usbtv->vb2q.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  549. usbtv->vb2q.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  550. usbtv->vb2q.drv_priv = usbtv;
  551. usbtv->vb2q.buf_struct_size = sizeof(struct usbtv_buf);
  552. usbtv->vb2q.ops = &usbtv_vb2_ops;
  553. usbtv->vb2q.mem_ops = &vb2_vmalloc_memops;
  554. usbtv->vb2q.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  555. usbtv->vb2q.lock = &usbtv->vb2q_lock;
  556. ret = vb2_queue_init(&usbtv->vb2q);
  557. if (ret < 0) {
  558. dev_warn(dev, "Could not initialize videobuf2 queue\n");
  559. goto usbtv_fail;
  560. }
  561. /* v4l2 structure */
  562. usbtv->v4l2_dev.release = usbtv_release;
  563. ret = v4l2_device_register(dev, &usbtv->v4l2_dev);
  564. if (ret < 0) {
  565. dev_warn(dev, "Could not register v4l2 device\n");
  566. goto v4l2_fail;
  567. }
  568. usb_set_intfdata(intf, usbtv);
  569. /* Video structure */
  570. strlcpy(usbtv->vdev.name, "usbtv", sizeof(usbtv->vdev.name));
  571. usbtv->vdev.v4l2_dev = &usbtv->v4l2_dev;
  572. usbtv->vdev.release = video_device_release_empty;
  573. usbtv->vdev.fops = &usbtv_fops;
  574. usbtv->vdev.ioctl_ops = &usbtv_ioctl_ops;
  575. usbtv->vdev.tvnorms = V4L2_STD_525_60;
  576. usbtv->vdev.queue = &usbtv->vb2q;
  577. usbtv->vdev.lock = &usbtv->v4l2_lock;
  578. set_bit(V4L2_FL_USE_FH_PRIO, &usbtv->vdev.flags);
  579. video_set_drvdata(&usbtv->vdev, usbtv);
  580. ret = video_register_device(&usbtv->vdev, VFL_TYPE_GRABBER, -1);
  581. if (ret < 0) {
  582. dev_warn(dev, "Could not register video device\n");
  583. goto vdev_fail;
  584. }
  585. dev_info(dev, "Fushicai USBTV007 Video Grabber\n");
  586. return 0;
  587. vdev_fail:
  588. v4l2_device_unregister(&usbtv->v4l2_dev);
  589. v4l2_fail:
  590. vb2_queue_release(&usbtv->vb2q);
  591. usbtv_fail:
  592. kfree(usbtv);
  593. return ret;
  594. }
  595. static void usbtv_disconnect(struct usb_interface *intf)
  596. {
  597. struct usbtv *usbtv = usb_get_intfdata(intf);
  598. mutex_lock(&usbtv->vb2q_lock);
  599. mutex_lock(&usbtv->v4l2_lock);
  600. usbtv_stop(usbtv);
  601. usb_set_intfdata(intf, NULL);
  602. video_unregister_device(&usbtv->vdev);
  603. v4l2_device_disconnect(&usbtv->v4l2_dev);
  604. usb_put_dev(usbtv->udev);
  605. usbtv->udev = NULL;
  606. mutex_unlock(&usbtv->v4l2_lock);
  607. mutex_unlock(&usbtv->vb2q_lock);
  608. v4l2_device_put(&usbtv->v4l2_dev);
  609. }
  610. MODULE_AUTHOR("Lubomir Rintel");
  611. MODULE_DESCRIPTION("Fushicai USBTV007 Video Grabber Driver");
  612. MODULE_LICENSE("Dual BSD/GPL");
  613. struct usb_driver usbtv_usb_driver = {
  614. .name = "usbtv",
  615. .id_table = usbtv_id_table,
  616. .probe = usbtv_probe,
  617. .disconnect = usbtv_disconnect,
  618. };
  619. module_usb_driver(usbtv_usb_driver);