solo6x10-v4l2.c 19 KB

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