usbtv.c 20 KB

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