usbtv.c 18 KB

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