solo6x10-v4l2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. spin_lock(&solo_dev->slock);
  273. while (!list_empty(&solo_dev->vidq_active)) {
  274. struct solo_vb2_buf *buf = list_entry(
  275. solo_dev->vidq_active.next,
  276. struct solo_vb2_buf, list);
  277. list_del(&buf->list);
  278. vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
  279. }
  280. spin_unlock(&solo_dev->slock);
  281. INIT_LIST_HEAD(&solo_dev->vidq_active);
  282. }
  283. static void solo_buf_queue(struct vb2_buffer *vb)
  284. {
  285. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  286. struct vb2_queue *vq = vb->vb2_queue;
  287. struct solo_dev *solo_dev = vb2_get_drv_priv(vq);
  288. struct solo_vb2_buf *solo_vb =
  289. container_of(vbuf, struct solo_vb2_buf, vb);
  290. spin_lock(&solo_dev->slock);
  291. list_add_tail(&solo_vb->list, &solo_dev->vidq_active);
  292. spin_unlock(&solo_dev->slock);
  293. wake_up_interruptible(&solo_dev->disp_thread_wait);
  294. }
  295. static const struct vb2_ops solo_video_qops = {
  296. .queue_setup = solo_queue_setup,
  297. .buf_queue = solo_buf_queue,
  298. .start_streaming = solo_start_streaming,
  299. .stop_streaming = solo_stop_streaming,
  300. .wait_prepare = vb2_ops_wait_prepare,
  301. .wait_finish = vb2_ops_wait_finish,
  302. };
  303. static int solo_querycap(struct file *file, void *priv,
  304. struct v4l2_capability *cap)
  305. {
  306. struct solo_dev *solo_dev = video_drvdata(file);
  307. strcpy(cap->driver, SOLO6X10_NAME);
  308. strcpy(cap->card, "Softlogic 6x10");
  309. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  310. pci_name(solo_dev->pdev));
  311. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  312. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  313. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  314. return 0;
  315. }
  316. static int solo_enum_ext_input(struct solo_dev *solo_dev,
  317. struct v4l2_input *input)
  318. {
  319. int ext = input->index - solo_dev->nr_chans;
  320. unsigned int nup, first;
  321. if (ext >= solo_dev->nr_ext)
  322. return -EINVAL;
  323. nup = (ext == 4) ? 16 : 4;
  324. first = (ext & 3) << 2; /* first channel in the n-up */
  325. snprintf(input->name, sizeof(input->name),
  326. "Multi %d-up (cameras %d-%d)",
  327. nup, first + 1, first + nup);
  328. /* Possible outputs:
  329. * Multi 4-up (cameras 1-4)
  330. * Multi 4-up (cameras 5-8)
  331. * Multi 4-up (cameras 9-12)
  332. * Multi 4-up (cameras 13-16)
  333. * Multi 16-up (cameras 1-16)
  334. */
  335. return 0;
  336. }
  337. static int solo_enum_input(struct file *file, void *priv,
  338. struct v4l2_input *input)
  339. {
  340. struct solo_dev *solo_dev = video_drvdata(file);
  341. if (input->index >= solo_dev->nr_chans) {
  342. int ret = solo_enum_ext_input(solo_dev, input);
  343. if (ret < 0)
  344. return ret;
  345. } else {
  346. snprintf(input->name, sizeof(input->name), "Camera %d",
  347. input->index + 1);
  348. /* We can only check this for normal inputs */
  349. if (!tw28_get_video_status(solo_dev, input->index))
  350. input->status = V4L2_IN_ST_NO_SIGNAL;
  351. }
  352. input->type = V4L2_INPUT_TYPE_CAMERA;
  353. input->std = solo_dev->vfd->tvnorms;
  354. return 0;
  355. }
  356. static int solo_set_input(struct file *file, void *priv, unsigned int index)
  357. {
  358. struct solo_dev *solo_dev = video_drvdata(file);
  359. int ret = solo_v4l2_set_ch(solo_dev, index);
  360. if (!ret) {
  361. while (erase_off(solo_dev))
  362. /* Do nothing */;
  363. }
  364. return ret;
  365. }
  366. static int solo_get_input(struct file *file, void *priv, unsigned int *index)
  367. {
  368. struct solo_dev *solo_dev = video_drvdata(file);
  369. *index = solo_dev->cur_disp_ch;
  370. return 0;
  371. }
  372. static int solo_enum_fmt_cap(struct file *file, void *priv,
  373. struct v4l2_fmtdesc *f)
  374. {
  375. if (f->index)
  376. return -EINVAL;
  377. f->pixelformat = V4L2_PIX_FMT_UYVY;
  378. strlcpy(f->description, "UYUV 4:2:2 Packed", sizeof(f->description));
  379. return 0;
  380. }
  381. static int solo_try_fmt_cap(struct file *file, void *priv,
  382. struct v4l2_format *f)
  383. {
  384. struct solo_dev *solo_dev = video_drvdata(file);
  385. struct v4l2_pix_format *pix = &f->fmt.pix;
  386. int image_size = solo_image_size(solo_dev);
  387. if (pix->pixelformat != V4L2_PIX_FMT_UYVY)
  388. return -EINVAL;
  389. pix->width = solo_dev->video_hsize;
  390. pix->height = solo_vlines(solo_dev);
  391. pix->sizeimage = image_size;
  392. pix->field = V4L2_FIELD_INTERLACED;
  393. pix->pixelformat = V4L2_PIX_FMT_UYVY;
  394. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  395. pix->priv = 0;
  396. return 0;
  397. }
  398. static int solo_set_fmt_cap(struct file *file, void *priv,
  399. struct v4l2_format *f)
  400. {
  401. struct solo_dev *solo_dev = video_drvdata(file);
  402. if (vb2_is_busy(&solo_dev->vidq))
  403. return -EBUSY;
  404. /* For right now, if it doesn't match our running config,
  405. * then fail */
  406. return solo_try_fmt_cap(file, priv, f);
  407. }
  408. static int solo_get_fmt_cap(struct file *file, void *priv,
  409. struct v4l2_format *f)
  410. {
  411. struct solo_dev *solo_dev = video_drvdata(file);
  412. struct v4l2_pix_format *pix = &f->fmt.pix;
  413. pix->width = solo_dev->video_hsize;
  414. pix->height = solo_vlines(solo_dev);
  415. pix->pixelformat = V4L2_PIX_FMT_UYVY;
  416. pix->field = V4L2_FIELD_INTERLACED;
  417. pix->sizeimage = solo_image_size(solo_dev);
  418. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  419. pix->bytesperline = solo_bytesperline(solo_dev);
  420. pix->priv = 0;
  421. return 0;
  422. }
  423. static int solo_g_std(struct file *file, void *priv, v4l2_std_id *i)
  424. {
  425. struct solo_dev *solo_dev = video_drvdata(file);
  426. if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
  427. *i = V4L2_STD_NTSC_M;
  428. else
  429. *i = V4L2_STD_PAL;
  430. return 0;
  431. }
  432. int solo_set_video_type(struct solo_dev *solo_dev, bool is_50hz)
  433. {
  434. int i;
  435. /* Make sure all video nodes are idle */
  436. if (vb2_is_busy(&solo_dev->vidq))
  437. return -EBUSY;
  438. for (i = 0; i < solo_dev->nr_chans; i++)
  439. if (vb2_is_busy(&solo_dev->v4l2_enc[i]->vidq))
  440. return -EBUSY;
  441. solo_dev->video_type = is_50hz ? SOLO_VO_FMT_TYPE_PAL :
  442. SOLO_VO_FMT_TYPE_NTSC;
  443. /* Reconfigure for the new standard */
  444. solo_disp_init(solo_dev);
  445. solo_enc_init(solo_dev);
  446. solo_tw28_init(solo_dev);
  447. for (i = 0; i < solo_dev->nr_chans; i++)
  448. solo_update_mode(solo_dev->v4l2_enc[i]);
  449. return solo_v4l2_set_ch(solo_dev, solo_dev->cur_disp_ch);
  450. }
  451. static int solo_s_std(struct file *file, void *priv, v4l2_std_id std)
  452. {
  453. struct solo_dev *solo_dev = video_drvdata(file);
  454. return solo_set_video_type(solo_dev, std & V4L2_STD_625_50);
  455. }
  456. static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
  457. {
  458. struct solo_dev *solo_dev =
  459. container_of(ctrl->handler, struct solo_dev, disp_hdl);
  460. switch (ctrl->id) {
  461. case V4L2_CID_MOTION_TRACE:
  462. if (ctrl->val) {
  463. solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER,
  464. SOLO_VI_MOTION_Y_ADD |
  465. SOLO_VI_MOTION_Y_VALUE(0x20) |
  466. SOLO_VI_MOTION_CB_VALUE(0x10) |
  467. SOLO_VI_MOTION_CR_VALUE(0x10));
  468. solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR,
  469. SOLO_VI_MOTION_CR_ADD |
  470. SOLO_VI_MOTION_Y_VALUE(0x10) |
  471. SOLO_VI_MOTION_CB_VALUE(0x80) |
  472. SOLO_VI_MOTION_CR_VALUE(0x10));
  473. } else {
  474. solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, 0);
  475. solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, 0);
  476. }
  477. return 0;
  478. default:
  479. break;
  480. }
  481. return -EINVAL;
  482. }
  483. static const struct v4l2_file_operations solo_v4l2_fops = {
  484. .owner = THIS_MODULE,
  485. .open = v4l2_fh_open,
  486. .release = vb2_fop_release,
  487. .read = vb2_fop_read,
  488. .poll = vb2_fop_poll,
  489. .mmap = vb2_fop_mmap,
  490. .unlocked_ioctl = video_ioctl2,
  491. };
  492. static const struct v4l2_ioctl_ops solo_v4l2_ioctl_ops = {
  493. .vidioc_querycap = solo_querycap,
  494. .vidioc_s_std = solo_s_std,
  495. .vidioc_g_std = solo_g_std,
  496. /* Input callbacks */
  497. .vidioc_enum_input = solo_enum_input,
  498. .vidioc_s_input = solo_set_input,
  499. .vidioc_g_input = solo_get_input,
  500. /* Video capture format callbacks */
  501. .vidioc_enum_fmt_vid_cap = solo_enum_fmt_cap,
  502. .vidioc_try_fmt_vid_cap = solo_try_fmt_cap,
  503. .vidioc_s_fmt_vid_cap = solo_set_fmt_cap,
  504. .vidioc_g_fmt_vid_cap = solo_get_fmt_cap,
  505. /* Streaming I/O */
  506. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  507. .vidioc_querybuf = vb2_ioctl_querybuf,
  508. .vidioc_qbuf = vb2_ioctl_qbuf,
  509. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  510. .vidioc_streamon = vb2_ioctl_streamon,
  511. .vidioc_streamoff = vb2_ioctl_streamoff,
  512. /* Logging and events */
  513. .vidioc_log_status = v4l2_ctrl_log_status,
  514. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  515. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  516. };
  517. static const struct video_device solo_v4l2_template = {
  518. .name = SOLO6X10_NAME,
  519. .fops = &solo_v4l2_fops,
  520. .ioctl_ops = &solo_v4l2_ioctl_ops,
  521. .minor = -1,
  522. .release = video_device_release,
  523. .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
  524. };
  525. static const struct v4l2_ctrl_ops solo_ctrl_ops = {
  526. .s_ctrl = solo_s_ctrl,
  527. };
  528. static const struct v4l2_ctrl_config solo_motion_trace_ctrl = {
  529. .ops = &solo_ctrl_ops,
  530. .id = V4L2_CID_MOTION_TRACE,
  531. .name = "Motion Detection Trace",
  532. .type = V4L2_CTRL_TYPE_BOOLEAN,
  533. .max = 1,
  534. .step = 1,
  535. };
  536. int solo_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
  537. {
  538. int ret;
  539. int i;
  540. init_waitqueue_head(&solo_dev->disp_thread_wait);
  541. spin_lock_init(&solo_dev->slock);
  542. mutex_init(&solo_dev->lock);
  543. INIT_LIST_HEAD(&solo_dev->vidq_active);
  544. solo_dev->vfd = video_device_alloc();
  545. if (!solo_dev->vfd)
  546. return -ENOMEM;
  547. *solo_dev->vfd = solo_v4l2_template;
  548. solo_dev->vfd->v4l2_dev = &solo_dev->v4l2_dev;
  549. solo_dev->vfd->queue = &solo_dev->vidq;
  550. solo_dev->vfd->lock = &solo_dev->lock;
  551. v4l2_ctrl_handler_init(&solo_dev->disp_hdl, 1);
  552. v4l2_ctrl_new_custom(&solo_dev->disp_hdl, &solo_motion_trace_ctrl, NULL);
  553. if (solo_dev->disp_hdl.error) {
  554. ret = solo_dev->disp_hdl.error;
  555. goto fail;
  556. }
  557. solo_dev->vfd->ctrl_handler = &solo_dev->disp_hdl;
  558. video_set_drvdata(solo_dev->vfd, solo_dev);
  559. solo_dev->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  560. solo_dev->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
  561. solo_dev->vidq.ops = &solo_video_qops;
  562. solo_dev->vidq.mem_ops = &vb2_dma_contig_memops;
  563. solo_dev->vidq.drv_priv = solo_dev;
  564. solo_dev->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  565. solo_dev->vidq.gfp_flags = __GFP_DMA32 | __GFP_KSWAPD_RECLAIM;
  566. solo_dev->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
  567. solo_dev->vidq.lock = &solo_dev->lock;
  568. solo_dev->vidq.dev = &solo_dev->pdev->dev;
  569. ret = vb2_queue_init(&solo_dev->vidq);
  570. if (ret < 0)
  571. goto fail;
  572. /* Cycle all the channels and clear */
  573. for (i = 0; i < solo_dev->nr_chans; i++) {
  574. solo_v4l2_set_ch(solo_dev, i);
  575. while (erase_off(solo_dev))
  576. /* Do nothing */;
  577. }
  578. /* Set the default display channel */
  579. solo_v4l2_set_ch(solo_dev, 0);
  580. while (erase_off(solo_dev))
  581. /* Do nothing */;
  582. ret = video_register_device(solo_dev->vfd, VFL_TYPE_GRABBER, nr);
  583. if (ret < 0)
  584. goto fail;
  585. snprintf(solo_dev->vfd->name, sizeof(solo_dev->vfd->name), "%s (%i)",
  586. SOLO6X10_NAME, solo_dev->vfd->num);
  587. dev_info(&solo_dev->pdev->dev, "Display as /dev/video%d with %d inputs (%d extended)\n",
  588. solo_dev->vfd->num,
  589. solo_dev->nr_chans, solo_dev->nr_ext);
  590. return 0;
  591. fail:
  592. video_device_release(solo_dev->vfd);
  593. v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
  594. solo_dev->vfd = NULL;
  595. return ret;
  596. }
  597. void solo_v4l2_exit(struct solo_dev *solo_dev)
  598. {
  599. if (solo_dev->vfd == NULL)
  600. return;
  601. video_unregister_device(solo_dev->vfd);
  602. v4l2_ctrl_handler_free(&solo_dev->disp_hdl);
  603. solo_dev->vfd = NULL;
  604. }