solo6x10-v4l2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. /*
  2. * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
  3. *
  4. * Original author:
  5. * Ben Collins <bcollins@ubuntu.com>
  6. *
  7. * Additional work by:
  8. * John Brooks <john.brooks@bluecherry.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/kthread.h>
  23. #include <linux/freezer.h>
  24. #include <media/v4l2-ioctl.h>
  25. #include <media/v4l2-common.h>
  26. #include <media/v4l2-event.h>
  27. #include <media/videobuf2-dma-contig.h>
  28. #include "solo6x10.h"
  29. #include "solo6x10-tw28.h"
  30. /* Image size is two fields, SOLO_HW_BPL is one horizontal line in hardware */
  31. #define SOLO_HW_BPL 2048
  32. #define solo_vlines(__solo) (__solo->video_vsize * 2)
  33. #define solo_image_size(__solo) (solo_bytesperline(__solo) * \
  34. solo_vlines(__solo))
  35. #define solo_bytesperline(__solo) (__solo->video_hsize * 2)
  36. #define MIN_VID_BUFFERS 2
  37. static inline void erase_on(struct solo_dev *solo_dev)
  38. {
  39. solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, SOLO_VO_DISP_ERASE_ON);
  40. solo_dev->erasing = 1;
  41. solo_dev->frame_blank = 0;
  42. }
  43. static inline int erase_off(struct solo_dev *solo_dev)
  44. {
  45. if (!solo_dev->erasing)
  46. return 0;
  47. /* First time around, assert erase off */
  48. if (!solo_dev->frame_blank)
  49. solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, 0);
  50. /* Keep the erasing flag on for 8 frames minimum */
  51. if (solo_dev->frame_blank++ >= 8)
  52. solo_dev->erasing = 0;
  53. return 1;
  54. }
  55. void solo_video_in_isr(struct solo_dev *solo_dev)
  56. {
  57. wake_up_interruptible_all(&solo_dev->disp_thread_wait);
  58. }
  59. static void solo_win_setup(struct solo_dev *solo_dev, u8 ch,
  60. int sx, int sy, int ex, int ey, int scale)
  61. {
  62. if (ch >= solo_dev->nr_chans)
  63. return;
  64. /* Here, we just keep window/channel the same */
  65. solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL0(ch),
  66. SOLO_VI_WIN_CHANNEL(ch) |
  67. SOLO_VI_WIN_SX(sx) |
  68. SOLO_VI_WIN_EX(ex) |
  69. SOLO_VI_WIN_SCALE(scale));
  70. solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL1(ch),
  71. SOLO_VI_WIN_SY(sy) |
  72. SOLO_VI_WIN_EY(ey));
  73. }
  74. static int solo_v4l2_ch_ext_4up(struct solo_dev *solo_dev, u8 idx, int on)
  75. {
  76. u8 ch = idx * 4;
  77. if (ch >= solo_dev->nr_chans)
  78. return -EINVAL;
  79. if (!on) {
  80. u8 i;
  81. for (i = ch; i < ch + 4; i++)
  82. solo_win_setup(solo_dev, i, solo_dev->video_hsize,
  83. solo_vlines(solo_dev),
  84. solo_dev->video_hsize,
  85. solo_vlines(solo_dev), 0);
  86. return 0;
  87. }
  88. /* Row 1 */
  89. solo_win_setup(solo_dev, ch, 0, 0, solo_dev->video_hsize / 2,
  90. solo_vlines(solo_dev) / 2, 3);
  91. solo_win_setup(solo_dev, ch + 1, solo_dev->video_hsize / 2, 0,
  92. solo_dev->video_hsize, solo_vlines(solo_dev) / 2, 3);
  93. /* Row 2 */
  94. solo_win_setup(solo_dev, ch + 2, 0, solo_vlines(solo_dev) / 2,
  95. solo_dev->video_hsize / 2, solo_vlines(solo_dev), 3);
  96. solo_win_setup(solo_dev, ch + 3, solo_dev->video_hsize / 2,
  97. solo_vlines(solo_dev) / 2, solo_dev->video_hsize,
  98. solo_vlines(solo_dev), 3);
  99. return 0;
  100. }
  101. static int solo_v4l2_ch_ext_16up(struct solo_dev *solo_dev, int on)
  102. {
  103. int sy, ysize, hsize, i;
  104. if (!on) {
  105. for (i = 0; i < 16; i++)
  106. solo_win_setup(solo_dev, i, solo_dev->video_hsize,
  107. solo_vlines(solo_dev),
  108. solo_dev->video_hsize,
  109. solo_vlines(solo_dev), 0);
  110. return 0;
  111. }
  112. ysize = solo_vlines(solo_dev) / 4;
  113. hsize = solo_dev->video_hsize / 4;
  114. for (sy = 0, i = 0; i < 4; i++, sy += ysize) {
  115. solo_win_setup(solo_dev, i * 4, 0, sy, hsize,
  116. sy + ysize, 5);
  117. solo_win_setup(solo_dev, (i * 4) + 1, hsize, sy,
  118. hsize * 2, sy + ysize, 5);
  119. solo_win_setup(solo_dev, (i * 4) + 2, hsize * 2, sy,
  120. hsize * 3, sy + ysize, 5);
  121. solo_win_setup(solo_dev, (i * 4) + 3, hsize * 3, sy,
  122. solo_dev->video_hsize, sy + ysize, 5);
  123. }
  124. return 0;
  125. }
  126. static int solo_v4l2_ch(struct solo_dev *solo_dev, u8 ch, int on)
  127. {
  128. u8 ext_ch;
  129. if (ch < solo_dev->nr_chans) {
  130. solo_win_setup(solo_dev, ch, on ? 0 : solo_dev->video_hsize,
  131. on ? 0 : solo_vlines(solo_dev),
  132. solo_dev->video_hsize, solo_vlines(solo_dev),
  133. on ? 1 : 0);
  134. return 0;
  135. }
  136. if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
  137. return -EINVAL;
  138. ext_ch = ch - solo_dev->nr_chans;
  139. /* 4up's first */
  140. if (ext_ch < 4)
  141. return solo_v4l2_ch_ext_4up(solo_dev, ext_ch, on);
  142. /* Remaining case is 16up for 16-port */
  143. return solo_v4l2_ch_ext_16up(solo_dev, on);
  144. }
  145. static int solo_v4l2_set_ch(struct solo_dev *solo_dev, u8 ch)
  146. {
  147. if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
  148. return -EINVAL;
  149. erase_on(solo_dev);
  150. solo_v4l2_ch(solo_dev, solo_dev->cur_disp_ch, 0);
  151. solo_v4l2_ch(solo_dev, ch, 1);
  152. solo_dev->cur_disp_ch = ch;
  153. return 0;
  154. }
  155. static void solo_fillbuf(struct solo_dev *solo_dev,
  156. struct vb2_buffer *vb)
  157. {
  158. dma_addr_t vbuf;
  159. unsigned int fdma_addr;
  160. int error = -1;
  161. int i;
  162. vbuf = vb2_dma_contig_plane_dma_addr(vb, 0);
  163. if (!vbuf)
  164. goto finish_buf;
  165. if (erase_off(solo_dev)) {
  166. void *p = vb2_plane_vaddr(vb, 0);
  167. int image_size = solo_image_size(solo_dev);
  168. for (i = 0; i < image_size; i += 2) {
  169. ((u8 *)p)[i] = 0x80;
  170. ((u8 *)p)[i + 1] = 0x00;
  171. }
  172. error = 0;
  173. } else {
  174. fdma_addr = SOLO_DISP_EXT_ADDR + (solo_dev->old_write *
  175. (SOLO_HW_BPL * solo_vlines(solo_dev)));
  176. error = solo_p2m_dma_t(solo_dev, 0, vbuf, fdma_addr,
  177. solo_bytesperline(solo_dev),
  178. solo_vlines(solo_dev), SOLO_HW_BPL);
  179. }
  180. finish_buf:
  181. if (!error) {
  182. vb2_set_plane_payload(vb, 0,
  183. solo_vlines(solo_dev) * solo_bytesperline(solo_dev));
  184. vb->v4l2_buf.sequence = solo_dev->sequence++;
  185. v4l2_get_timestamp(&vb->v4l2_buf.timestamp);
  186. }
  187. vb2_buffer_done(vb, error ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
  188. }
  189. static void solo_thread_try(struct solo_dev *solo_dev)
  190. {
  191. struct solo_vb2_buf *vb;
  192. /* Only "break" from this loop if slock is held, otherwise
  193. * just return. */
  194. for (;;) {
  195. unsigned int cur_write;
  196. cur_write = SOLO_VI_STATUS0_PAGE(
  197. solo_reg_read(solo_dev, SOLO_VI_STATUS0));
  198. if (cur_write == solo_dev->old_write)
  199. return;
  200. spin_lock(&solo_dev->slock);
  201. if (list_empty(&solo_dev->vidq_active))
  202. break;
  203. vb = list_first_entry(&solo_dev->vidq_active, struct solo_vb2_buf,
  204. list);
  205. solo_dev->old_write = cur_write;
  206. list_del(&vb->list);
  207. spin_unlock(&solo_dev->slock);
  208. solo_fillbuf(solo_dev, &vb->vb);
  209. }
  210. assert_spin_locked(&solo_dev->slock);
  211. spin_unlock(&solo_dev->slock);
  212. }
  213. static int solo_thread(void *data)
  214. {
  215. struct solo_dev *solo_dev = data;
  216. DECLARE_WAITQUEUE(wait, current);
  217. set_freezable();
  218. add_wait_queue(&solo_dev->disp_thread_wait, &wait);
  219. for (;;) {
  220. long timeout = schedule_timeout_interruptible(HZ);
  221. if (timeout == -ERESTARTSYS || kthread_should_stop())
  222. break;
  223. solo_thread_try(solo_dev);
  224. try_to_freeze();
  225. }
  226. remove_wait_queue(&solo_dev->disp_thread_wait, &wait);
  227. return 0;
  228. }
  229. static int solo_start_thread(struct solo_dev *solo_dev)
  230. {
  231. int ret = 0;
  232. solo_dev->kthread = kthread_run(solo_thread, solo_dev, SOLO6X10_NAME "_disp");
  233. if (IS_ERR(solo_dev->kthread)) {
  234. ret = PTR_ERR(solo_dev->kthread);
  235. solo_dev->kthread = NULL;
  236. return ret;
  237. }
  238. solo_irq_on(solo_dev, SOLO_IRQ_VIDEO_IN);
  239. return ret;
  240. }
  241. static void solo_stop_thread(struct solo_dev *solo_dev)
  242. {
  243. if (!solo_dev->kthread)
  244. return;
  245. solo_irq_off(solo_dev, SOLO_IRQ_VIDEO_IN);
  246. kthread_stop(solo_dev->kthread);
  247. solo_dev->kthread = NULL;
  248. }
  249. static int solo_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
  250. unsigned int *num_buffers, unsigned int *num_planes,
  251. unsigned int sizes[], void *alloc_ctxs[])
  252. {
  253. struct solo_dev *solo_dev = vb2_get_drv_priv(q);
  254. sizes[0] = solo_image_size(solo_dev);
  255. alloc_ctxs[0] = solo_dev->alloc_ctx;
  256. *num_planes = 1;
  257. if (*num_buffers < MIN_VID_BUFFERS)
  258. *num_buffers = MIN_VID_BUFFERS;
  259. return 0;
  260. }
  261. static int solo_start_streaming(struct vb2_queue *q, unsigned int count)
  262. {
  263. struct solo_dev *solo_dev = vb2_get_drv_priv(q);
  264. solo_dev->sequence = 0;
  265. return solo_start_thread(solo_dev);
  266. }
  267. static void solo_stop_streaming(struct vb2_queue *q)
  268. {
  269. struct solo_dev *solo_dev = vb2_get_drv_priv(q);
  270. solo_stop_thread(solo_dev);
  271. INIT_LIST_HEAD(&solo_dev->vidq_active);
  272. }
  273. static void solo_buf_queue(struct vb2_buffer *vb)
  274. {
  275. struct vb2_queue *vq = vb->vb2_queue;
  276. struct solo_dev *solo_dev = vb2_get_drv_priv(vq);
  277. struct solo_vb2_buf *solo_vb =
  278. container_of(vb, struct solo_vb2_buf, vb);
  279. spin_lock(&solo_dev->slock);
  280. list_add_tail(&solo_vb->list, &solo_dev->vidq_active);
  281. spin_unlock(&solo_dev->slock);
  282. wake_up_interruptible(&solo_dev->disp_thread_wait);
  283. }
  284. static const struct vb2_ops solo_video_qops = {
  285. .queue_setup = solo_queue_setup,
  286. .buf_queue = solo_buf_queue,
  287. .start_streaming = solo_start_streaming,
  288. .stop_streaming = solo_stop_streaming,
  289. .wait_prepare = vb2_ops_wait_prepare,
  290. .wait_finish = vb2_ops_wait_finish,
  291. };
  292. static int solo_querycap(struct file *file, void *priv,
  293. struct v4l2_capability *cap)
  294. {
  295. struct solo_dev *solo_dev = video_drvdata(file);
  296. strcpy(cap->driver, SOLO6X10_NAME);
  297. strcpy(cap->card, "Softlogic 6x10");
  298. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  299. pci_name(solo_dev->pdev));
  300. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  301. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  302. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  303. return 0;
  304. }
  305. static int solo_enum_ext_input(struct solo_dev *solo_dev,
  306. struct v4l2_input *input)
  307. {
  308. static const char * const dispnames_1[] = { "4UP" };
  309. static const char * const dispnames_2[] = { "4UP-1", "4UP-2" };
  310. static const char * const dispnames_5[] = {
  311. "4UP-1", "4UP-2", "4UP-3", "4UP-4", "16UP"
  312. };
  313. const char * const *dispnames;
  314. if (input->index >= (solo_dev->nr_chans + solo_dev->nr_ext))
  315. return -EINVAL;
  316. if (solo_dev->nr_ext == 5)
  317. dispnames = dispnames_5;
  318. else if (solo_dev->nr_ext == 2)
  319. dispnames = dispnames_2;
  320. else
  321. dispnames = dispnames_1;
  322. snprintf(input->name, sizeof(input->name), "Multi %s",
  323. dispnames[input->index - solo_dev->nr_chans]);
  324. return 0;
  325. }
  326. static int solo_enum_input(struct file *file, void *priv,
  327. struct v4l2_input *input)
  328. {
  329. struct solo_dev *solo_dev = video_drvdata(file);
  330. if (input->index >= solo_dev->nr_chans) {
  331. int ret = solo_enum_ext_input(solo_dev, input);
  332. if (ret < 0)
  333. return ret;
  334. } else {
  335. snprintf(input->name, sizeof(input->name), "Camera %d",
  336. input->index + 1);
  337. /* We can only check this for normal inputs */
  338. if (!tw28_get_video_status(solo_dev, input->index))
  339. input->status = V4L2_IN_ST_NO_SIGNAL;
  340. }
  341. input->type = V4L2_INPUT_TYPE_CAMERA;
  342. input->std = solo_dev->vfd->tvnorms;
  343. return 0;
  344. }
  345. static int solo_set_input(struct file *file, void *priv, unsigned int index)
  346. {
  347. struct solo_dev *solo_dev = video_drvdata(file);
  348. int ret = solo_v4l2_set_ch(solo_dev, index);
  349. if (!ret) {
  350. while (erase_off(solo_dev))
  351. /* Do nothing */;
  352. }
  353. return ret;
  354. }
  355. static int solo_get_input(struct file *file, void *priv, unsigned int *index)
  356. {
  357. struct solo_dev *solo_dev = video_drvdata(file);
  358. *index = solo_dev->cur_disp_ch;
  359. return 0;
  360. }
  361. static int solo_enum_fmt_cap(struct file *file, void *priv,
  362. struct v4l2_fmtdesc *f)
  363. {
  364. if (f->index)
  365. return -EINVAL;
  366. f->pixelformat = V4L2_PIX_FMT_UYVY;
  367. strlcpy(f->description, "UYUV 4:2:2 Packed", sizeof(f->description));
  368. return 0;
  369. }
  370. static int solo_try_fmt_cap(struct file *file, void *priv,
  371. struct v4l2_format *f)
  372. {
  373. struct solo_dev *solo_dev = video_drvdata(file);
  374. struct v4l2_pix_format *pix = &f->fmt.pix;
  375. int image_size = solo_image_size(solo_dev);
  376. if (pix->pixelformat != V4L2_PIX_FMT_UYVY)
  377. return -EINVAL;
  378. pix->width = solo_dev->video_hsize;
  379. pix->height = solo_vlines(solo_dev);
  380. pix->sizeimage = image_size;
  381. pix->field = V4L2_FIELD_INTERLACED;
  382. pix->pixelformat = V4L2_PIX_FMT_UYVY;
  383. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  384. pix->priv = 0;
  385. return 0;
  386. }
  387. static int solo_set_fmt_cap(struct file *file, void *priv,
  388. struct v4l2_format *f)
  389. {
  390. struct solo_dev *solo_dev = video_drvdata(file);
  391. if (vb2_is_busy(&solo_dev->vidq))
  392. return -EBUSY;
  393. /* For right now, if it doesn't match our running config,
  394. * then fail */
  395. return solo_try_fmt_cap(file, priv, f);
  396. }
  397. static int solo_get_fmt_cap(struct file *file, void *priv,
  398. struct v4l2_format *f)
  399. {
  400. struct solo_dev *solo_dev = video_drvdata(file);
  401. struct v4l2_pix_format *pix = &f->fmt.pix;
  402. pix->width = solo_dev->video_hsize;
  403. pix->height = solo_vlines(solo_dev);
  404. pix->pixelformat = V4L2_PIX_FMT_UYVY;
  405. pix->field = V4L2_FIELD_INTERLACED;
  406. pix->sizeimage = solo_image_size(solo_dev);
  407. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  408. pix->bytesperline = solo_bytesperline(solo_dev);
  409. pix->priv = 0;
  410. return 0;
  411. }
  412. static int solo_g_std(struct file *file, void *priv, v4l2_std_id *i)
  413. {
  414. struct solo_dev *solo_dev = video_drvdata(file);
  415. if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
  416. *i = V4L2_STD_NTSC_M;
  417. else
  418. *i = V4L2_STD_PAL;
  419. return 0;
  420. }
  421. int solo_set_video_type(struct solo_dev *solo_dev, bool is_50hz)
  422. {
  423. int i;
  424. /* Make sure all video nodes are idle */
  425. if (vb2_is_busy(&solo_dev->vidq))
  426. return -EBUSY;
  427. for (i = 0; i < solo_dev->nr_chans; i++)
  428. if (vb2_is_busy(&solo_dev->v4l2_enc[i]->vidq))
  429. return -EBUSY;
  430. solo_dev->video_type = is_50hz ? SOLO_VO_FMT_TYPE_PAL :
  431. SOLO_VO_FMT_TYPE_NTSC;
  432. /* Reconfigure for the new standard */
  433. solo_disp_init(solo_dev);
  434. solo_enc_init(solo_dev);
  435. solo_tw28_init(solo_dev);
  436. for (i = 0; i < solo_dev->nr_chans; i++)
  437. solo_update_mode(solo_dev->v4l2_enc[i]);
  438. return solo_v4l2_set_ch(solo_dev, solo_dev->cur_disp_ch);
  439. }
  440. static int solo_s_std(struct file *file, void *priv, v4l2_std_id std)
  441. {
  442. struct solo_dev *solo_dev = video_drvdata(file);
  443. return solo_set_video_type(solo_dev, std & V4L2_STD_625_50);
  444. }
  445. static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
  446. {
  447. struct solo_dev *solo_dev =
  448. container_of(ctrl->handler, struct solo_dev, disp_hdl);
  449. switch (ctrl->id) {
  450. case V4L2_CID_MOTION_TRACE:
  451. if (ctrl->val) {
  452. solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER,
  453. SOLO_VI_MOTION_Y_ADD |
  454. SOLO_VI_MOTION_Y_VALUE(0x20) |
  455. SOLO_VI_MOTION_CB_VALUE(0x10) |
  456. SOLO_VI_MOTION_CR_VALUE(0x10));
  457. solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR,
  458. SOLO_VI_MOTION_CR_ADD |
  459. SOLO_VI_MOTION_Y_VALUE(0x10) |
  460. SOLO_VI_MOTION_CB_VALUE(0x80) |
  461. SOLO_VI_MOTION_CR_VALUE(0x10));
  462. } else {
  463. solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, 0);
  464. solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, 0);
  465. }
  466. return 0;
  467. default:
  468. break;
  469. }
  470. return -EINVAL;
  471. }
  472. static const struct v4l2_file_operations solo_v4l2_fops = {
  473. .owner = THIS_MODULE,
  474. .open = v4l2_fh_open,
  475. .release = vb2_fop_release,
  476. .read = vb2_fop_read,
  477. .poll = vb2_fop_poll,
  478. .mmap = vb2_fop_mmap,
  479. .unlocked_ioctl = video_ioctl2,
  480. };
  481. static const struct v4l2_ioctl_ops solo_v4l2_ioctl_ops = {
  482. .vidioc_querycap = solo_querycap,
  483. .vidioc_s_std = solo_s_std,
  484. .vidioc_g_std = solo_g_std,
  485. /* Input callbacks */
  486. .vidioc_enum_input = solo_enum_input,
  487. .vidioc_s_input = solo_set_input,
  488. .vidioc_g_input = solo_get_input,
  489. /* Video capture format callbacks */
  490. .vidioc_enum_fmt_vid_cap = solo_enum_fmt_cap,
  491. .vidioc_try_fmt_vid_cap = solo_try_fmt_cap,
  492. .vidioc_s_fmt_vid_cap = solo_set_fmt_cap,
  493. .vidioc_g_fmt_vid_cap = solo_get_fmt_cap,
  494. /* Streaming I/O */
  495. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  496. .vidioc_querybuf = vb2_ioctl_querybuf,
  497. .vidioc_qbuf = vb2_ioctl_qbuf,
  498. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  499. .vidioc_streamon = vb2_ioctl_streamon,
  500. .vidioc_streamoff = vb2_ioctl_streamoff,
  501. /* Logging and events */
  502. .vidioc_log_status = v4l2_ctrl_log_status,
  503. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  504. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  505. };
  506. static struct video_device solo_v4l2_template = {
  507. .name = SOLO6X10_NAME,
  508. .fops = &solo_v4l2_fops,
  509. .ioctl_ops = &solo_v4l2_ioctl_ops,
  510. .minor = -1,
  511. .release = video_device_release,
  512. .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
  513. };
  514. static const struct v4l2_ctrl_ops solo_ctrl_ops = {
  515. .s_ctrl = solo_s_ctrl,
  516. };
  517. static const struct v4l2_ctrl_config solo_motion_trace_ctrl = {
  518. .ops = &solo_ctrl_ops,
  519. .id = V4L2_CID_MOTION_TRACE,
  520. .name = "Motion Detection Trace",
  521. .type = V4L2_CTRL_TYPE_BOOLEAN,
  522. .max = 1,
  523. .step = 1,
  524. };
  525. int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
  526. {
  527. int ret;
  528. int i;
  529. init_waitqueue_head(&solo_dev->disp_thread_wait);
  530. spin_lock_init(&solo_dev->slock);
  531. mutex_init(&solo_dev->lock);
  532. INIT_LIST_HEAD(&solo_dev->vidq_active);
  533. solo_dev->vfd = video_device_alloc();
  534. if (!solo_dev->vfd)
  535. return -ENOMEM;
  536. *solo_dev->vfd = solo_v4l2_template;
  537. solo_dev->vfd->v4l2_dev = &solo_dev->v4l2_dev;
  538. solo_dev->vfd->queue = &solo_dev->vidq;
  539. solo_dev->vfd->lock = &solo_dev->lock;
  540. v4l2_ctrl_handler_init(&solo_dev->disp_hdl, 1);
  541. v4l2_ctrl_new_custom(&solo_dev->disp_hdl, &solo_motion_trace_ctrl, NULL);
  542. if (solo_dev->disp_hdl.error) {
  543. ret = solo_dev->disp_hdl.error;
  544. goto fail;
  545. }
  546. solo_dev->vfd->ctrl_handler = &solo_dev->disp_hdl;
  547. video_set_drvdata(solo_dev->vfd, solo_dev);
  548. solo_dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  549. solo_dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  550. solo_dev->vidq.ops = &solo_video_qops;
  551. solo_dev->vidq.mem_ops = &vb2_dma_contig_memops;
  552. solo_dev->vidq.drv_priv = solo_dev;
  553. solo_dev->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  554. solo_dev->vidq.gfp_flags = __GFP_DMA32;
  555. solo_dev->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
  556. solo_dev->vidq.lock = &solo_dev->lock;
  557. ret = vb2_queue_init(&solo_dev->vidq);
  558. if (ret < 0)
  559. goto fail;
  560. solo_dev->alloc_ctx = vb2_dma_contig_init_ctx(&solo_dev->pdev->dev);
  561. if (IS_ERR(solo_dev->alloc_ctx)) {
  562. dev_err(&solo_dev->pdev->dev, "Can't allocate buffer context");
  563. return PTR_ERR(solo_dev->alloc_ctx);
  564. }
  565. /* Cycle all the channels and clear */
  566. for (i = 0; i < solo_dev->nr_chans; i++) {
  567. solo_v4l2_set_ch(solo_dev, i);
  568. while (erase_off(solo_dev))
  569. /* Do nothing */;
  570. }
  571. /* Set the default display channel */
  572. solo_v4l2_set_ch(solo_dev, 0);
  573. while (erase_off(solo_dev))
  574. /* Do nothing */;
  575. ret = video_register_device(solo_dev->vfd, VFL_TYPE_GRABBER, nr);
  576. if (ret < 0)
  577. goto fail;
  578. snprintf(solo_dev->vfd->name, sizeof(solo_dev->vfd->name), "%s (%i)",
  579. SOLO6X10_NAME, solo_dev->vfd->num);
  580. dev_info(&solo_dev->pdev->dev, "Display as /dev/video%d with "
  581. "%d inputs (%d extended)\n", solo_dev->vfd->num,
  582. solo_dev->nr_chans, solo_dev->nr_ext);
  583. return 0;
  584. fail:
  585. video_device_release(solo_dev->vfd);
  586. vb2_dma_contig_cleanup_ctx(solo_dev->alloc_ctx);
  587. v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
  588. solo_dev->vfd = NULL;
  589. return ret;
  590. }
  591. void solo_v4l2_exit(struct solo_dev *solo_dev)
  592. {
  593. if (solo_dev->vfd == NULL)
  594. return;
  595. video_unregister_device(solo_dev->vfd);
  596. vb2_dma_contig_cleanup_ctx(solo_dev->alloc_ctx);
  597. v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
  598. solo_dev->vfd = NULL;
  599. }