usbtv.c 20 KB

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