v4l2.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. /*
  2. * Copyright (C) 2010 Bluecherry, LLC www.bluecherrydvr.com
  3. * Copyright (C) 2010 Ben Collins <bcollins@bluecherry.net>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/kthread.h>
  22. #include <linux/freezer.h>
  23. #include <media/v4l2-ioctl.h>
  24. #include <media/v4l2-common.h>
  25. #include <media/videobuf-dma-sg.h>
  26. #include "solo6x10.h"
  27. #include "tw28.h"
  28. #define SOLO_HW_BPL 2048
  29. #define SOLO_DISP_PIX_FIELD V4L2_FIELD_INTERLACED
  30. /* Image size is two fields, SOLO_HW_BPL is one horizontal line */
  31. #define solo_vlines(__solo) (__solo->video_vsize * 2)
  32. #define solo_image_size(__solo) (solo_bytesperline(__solo) * \
  33. solo_vlines(__solo))
  34. #define solo_bytesperline(__solo) (__solo->video_hsize * 2)
  35. #define MIN_VID_BUFFERS 4
  36. /* Simple file handle */
  37. struct solo_filehandle {
  38. struct solo_dev *solo_dev;
  39. struct videobuf_queue vidq;
  40. struct task_struct *kthread;
  41. spinlock_t slock;
  42. int old_write;
  43. struct list_head vidq_active;
  44. struct p2m_desc desc[SOLO_NR_P2M_DESC];
  45. int desc_idx;
  46. };
  47. unsigned video_nr = -1;
  48. module_param(video_nr, uint, 0644);
  49. MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect (default)");
  50. static void erase_on(struct solo_dev *solo_dev)
  51. {
  52. solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, SOLO_VO_DISP_ERASE_ON);
  53. solo_dev->erasing = 1;
  54. solo_dev->frame_blank = 0;
  55. }
  56. static int erase_off(struct solo_dev *solo_dev)
  57. {
  58. if (!solo_dev->erasing)
  59. return 0;
  60. /* First time around, assert erase off */
  61. if (!solo_dev->frame_blank)
  62. solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, 0);
  63. /* Keep the erasing flag on for 8 frames minimum */
  64. if (solo_dev->frame_blank++ >= 8)
  65. solo_dev->erasing = 0;
  66. return 1;
  67. }
  68. void solo_video_in_isr(struct solo_dev *solo_dev)
  69. {
  70. solo_reg_write(solo_dev, SOLO_IRQ_STAT, SOLO_IRQ_VIDEO_IN);
  71. wake_up_interruptible(&solo_dev->disp_thread_wait);
  72. }
  73. static void solo_win_setup(struct solo_dev *solo_dev, u8 ch,
  74. int sx, int sy, int ex, int ey, int scale)
  75. {
  76. if (ch >= solo_dev->nr_chans)
  77. return;
  78. /* Here, we just keep window/channel the same */
  79. solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL0(ch),
  80. SOLO_VI_WIN_CHANNEL(ch) |
  81. SOLO_VI_WIN_SX(sx) |
  82. SOLO_VI_WIN_EX(ex) |
  83. SOLO_VI_WIN_SCALE(scale));
  84. solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL1(ch),
  85. SOLO_VI_WIN_SY(sy) |
  86. SOLO_VI_WIN_EY(ey));
  87. }
  88. static int solo_v4l2_ch_ext_4up(struct solo_dev *solo_dev, u8 idx, int on)
  89. {
  90. u8 ch = idx * 4;
  91. if (ch >= solo_dev->nr_chans)
  92. return -EINVAL;
  93. if (!on) {
  94. u8 i;
  95. for (i = ch; i < ch + 4; i++)
  96. solo_win_setup(solo_dev, i, solo_dev->video_hsize,
  97. solo_vlines(solo_dev),
  98. solo_dev->video_hsize,
  99. solo_vlines(solo_dev), 0);
  100. return 0;
  101. }
  102. /* Row 1 */
  103. solo_win_setup(solo_dev, ch, 0, 0, solo_dev->video_hsize / 2,
  104. solo_vlines(solo_dev) / 2, 3);
  105. solo_win_setup(solo_dev, ch + 1, solo_dev->video_hsize / 2, 0,
  106. solo_dev->video_hsize, solo_vlines(solo_dev) / 2, 3);
  107. /* Row 2 */
  108. solo_win_setup(solo_dev, ch + 2, 0, solo_vlines(solo_dev) / 2,
  109. solo_dev->video_hsize / 2, solo_vlines(solo_dev), 3);
  110. solo_win_setup(solo_dev, ch + 3, solo_dev->video_hsize / 2,
  111. solo_vlines(solo_dev) / 2, solo_dev->video_hsize,
  112. solo_vlines(solo_dev), 3);
  113. return 0;
  114. }
  115. static int solo_v4l2_ch_ext_16up(struct solo_dev *solo_dev, int on)
  116. {
  117. int sy, ysize, hsize, i;
  118. if (!on) {
  119. for (i = 0; i < 16; i++)
  120. solo_win_setup(solo_dev, i, solo_dev->video_hsize,
  121. solo_vlines(solo_dev),
  122. solo_dev->video_hsize,
  123. solo_vlines(solo_dev), 0);
  124. return 0;
  125. }
  126. ysize = solo_vlines(solo_dev) / 4;
  127. hsize = solo_dev->video_hsize / 4;
  128. for (sy = 0, i = 0; i < 4; i++, sy += ysize) {
  129. solo_win_setup(solo_dev, i * 4, 0, sy, hsize,
  130. sy + ysize, 5);
  131. solo_win_setup(solo_dev, (i * 4) + 1, hsize, sy,
  132. hsize * 2, sy + ysize, 5);
  133. solo_win_setup(solo_dev, (i * 4) + 2, hsize * 2, sy,
  134. hsize * 3, sy + ysize, 5);
  135. solo_win_setup(solo_dev, (i * 4) + 3, hsize * 3, sy,
  136. solo_dev->video_hsize, sy + ysize, 5);
  137. }
  138. return 0;
  139. }
  140. static int solo_v4l2_ch(struct solo_dev *solo_dev, u8 ch, int on)
  141. {
  142. u8 ext_ch;
  143. if (ch < solo_dev->nr_chans) {
  144. solo_win_setup(solo_dev, ch, on ? 0 : solo_dev->video_hsize,
  145. on ? 0 : solo_vlines(solo_dev),
  146. solo_dev->video_hsize, solo_vlines(solo_dev),
  147. on ? 1 : 0);
  148. return 0;
  149. }
  150. if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
  151. return -EINVAL;
  152. ext_ch = ch - solo_dev->nr_chans;
  153. /* 4up's first */
  154. if (ext_ch < 4)
  155. return solo_v4l2_ch_ext_4up(solo_dev, ext_ch, on);
  156. /* Remaining case is 16up for 16-port */
  157. return solo_v4l2_ch_ext_16up(solo_dev, on);
  158. }
  159. static int solo_v4l2_set_ch(struct solo_dev *solo_dev, u8 ch)
  160. {
  161. if (ch >= solo_dev->nr_chans + solo_dev->nr_ext)
  162. return -EINVAL;
  163. erase_on(solo_dev);
  164. solo_v4l2_ch(solo_dev, solo_dev->cur_disp_ch, 0);
  165. solo_v4l2_ch(solo_dev, ch, 1);
  166. solo_dev->cur_disp_ch = ch;
  167. return 0;
  168. }
  169. static void disp_reset_desc(struct solo_filehandle *fh)
  170. {
  171. /* We use desc mode, which ignores desc 0 */
  172. memset(fh->desc, 0, sizeof(*fh->desc));
  173. fh->desc_idx = 1;
  174. }
  175. static int disp_flush_descs(struct solo_filehandle *fh)
  176. {
  177. int ret;
  178. if (!fh->desc_idx)
  179. return 0;
  180. ret = solo_p2m_dma_desc(fh->solo_dev, SOLO_P2M_DMA_ID_DISP,
  181. fh->desc, fh->desc_idx);
  182. disp_reset_desc(fh);
  183. return ret;
  184. }
  185. static int disp_push_desc(struct solo_filehandle *fh, dma_addr_t dma_addr,
  186. u32 ext_addr, int size, int repeat, int ext_size)
  187. {
  188. if (fh->desc_idx >= SOLO_NR_P2M_DESC) {
  189. int ret = disp_flush_descs(fh);
  190. if (ret)
  191. return ret;
  192. }
  193. solo_p2m_push_desc(&fh->desc[fh->desc_idx], 0, dma_addr, ext_addr,
  194. size, repeat, ext_size);
  195. fh->desc_idx++;
  196. return 0;
  197. }
  198. static void solo_fillbuf(struct solo_filehandle *fh,
  199. struct videobuf_buffer *vb)
  200. {
  201. struct solo_dev *solo_dev = fh->solo_dev;
  202. struct videobuf_dmabuf *vbuf;
  203. unsigned int fdma_addr;
  204. int error = 1;
  205. int i;
  206. struct scatterlist *sg;
  207. dma_addr_t sg_dma;
  208. int sg_size_left;
  209. vbuf = videobuf_to_dma(vb);
  210. if (!vbuf)
  211. goto finish_buf;
  212. if (erase_off(solo_dev)) {
  213. int i;
  214. /* Just blit to the entire sg list, ignoring size */
  215. for_each_sg(vbuf->sglist, sg, vbuf->sglen, i) {
  216. void *p = sg_virt(sg);
  217. size_t len = sg_dma_len(sg);
  218. for (i = 0; i < len; i += 2) {
  219. ((u8 *)p)[i] = 0x80;
  220. ((u8 *)p)[i + 1] = 0x00;
  221. }
  222. }
  223. error = 0;
  224. goto finish_buf;
  225. }
  226. disp_reset_desc(fh);
  227. sg = vbuf->sglist;
  228. sg_dma = sg_dma_address(sg);
  229. sg_size_left = sg_dma_len(sg);
  230. fdma_addr = SOLO_DISP_EXT_ADDR + (fh->old_write *
  231. (SOLO_HW_BPL * solo_vlines(solo_dev)));
  232. for (i = 0; i < solo_vlines(solo_dev); i++) {
  233. int line_len = solo_bytesperline(solo_dev);
  234. int lines;
  235. if (!sg_size_left) {
  236. sg = sg_next(sg);
  237. if (sg == NULL)
  238. goto finish_buf;
  239. sg_dma = sg_dma_address(sg);
  240. sg_size_left = sg_dma_len(sg);
  241. }
  242. /* No room for an entire line, so chunk it up */
  243. if (sg_size_left < line_len) {
  244. int this_addr = fdma_addr;
  245. while (line_len > 0) {
  246. int this_write;
  247. if (!sg_size_left) {
  248. sg = sg_next(sg);
  249. if (sg == NULL)
  250. goto finish_buf;
  251. sg_dma = sg_dma_address(sg);
  252. sg_size_left = sg_dma_len(sg);
  253. }
  254. this_write = min(sg_size_left, line_len);
  255. if (disp_push_desc(fh, sg_dma, this_addr,
  256. this_write, 0, 0))
  257. goto finish_buf;
  258. line_len -= this_write;
  259. sg_size_left -= this_write;
  260. sg_dma += this_write;
  261. this_addr += this_write;
  262. }
  263. fdma_addr += SOLO_HW_BPL;
  264. continue;
  265. }
  266. /* Shove as many lines into a repeating descriptor as possible */
  267. lines = min(sg_size_left / line_len,
  268. solo_vlines(solo_dev) - i);
  269. if (disp_push_desc(fh, sg_dma, fdma_addr, line_len,
  270. lines - 1, SOLO_HW_BPL))
  271. goto finish_buf;
  272. i += lines - 1;
  273. fdma_addr += SOLO_HW_BPL * lines;
  274. sg_dma += lines * line_len;
  275. sg_size_left -= lines * line_len;
  276. }
  277. error = disp_flush_descs(fh);
  278. finish_buf:
  279. if (error) {
  280. vb->state = VIDEOBUF_ERROR;
  281. } else {
  282. vb->size = solo_vlines(solo_dev) * solo_bytesperline(solo_dev);
  283. vb->state = VIDEOBUF_DONE;
  284. vb->field_count++;
  285. do_gettimeofday(&vb->ts);
  286. }
  287. wake_up(&vb->done);
  288. return;
  289. }
  290. static void solo_thread_try(struct solo_filehandle *fh)
  291. {
  292. struct videobuf_buffer *vb;
  293. unsigned int cur_write;
  294. for (;;) {
  295. spin_lock(&fh->slock);
  296. if (list_empty(&fh->vidq_active))
  297. break;
  298. vb = list_first_entry(&fh->vidq_active, struct videobuf_buffer,
  299. queue);
  300. if (!waitqueue_active(&vb->done))
  301. break;
  302. cur_write = SOLO_VI_STATUS0_PAGE(solo_reg_read(fh->solo_dev,
  303. SOLO_VI_STATUS0));
  304. if (cur_write == fh->old_write)
  305. break;
  306. fh->old_write = cur_write;
  307. list_del(&vb->queue);
  308. spin_unlock(&fh->slock);
  309. solo_fillbuf(fh, vb);
  310. }
  311. assert_spin_locked(&fh->slock);
  312. spin_unlock(&fh->slock);
  313. }
  314. static int solo_thread(void *data)
  315. {
  316. struct solo_filehandle *fh = data;
  317. struct solo_dev *solo_dev = fh->solo_dev;
  318. DECLARE_WAITQUEUE(wait, current);
  319. set_freezable();
  320. add_wait_queue(&solo_dev->disp_thread_wait, &wait);
  321. for (;;) {
  322. long timeout = schedule_timeout_interruptible(HZ);
  323. if (timeout == -ERESTARTSYS || kthread_should_stop())
  324. break;
  325. solo_thread_try(fh);
  326. try_to_freeze();
  327. }
  328. remove_wait_queue(&solo_dev->disp_thread_wait, &wait);
  329. return 0;
  330. }
  331. static int solo_start_thread(struct solo_filehandle *fh)
  332. {
  333. fh->kthread = kthread_run(solo_thread, fh, SOLO6X10_NAME "_disp");
  334. if (IS_ERR(fh->kthread))
  335. return PTR_ERR(fh->kthread);
  336. return 0;
  337. }
  338. static void solo_stop_thread(struct solo_filehandle *fh)
  339. {
  340. if (fh->kthread) {
  341. kthread_stop(fh->kthread);
  342. fh->kthread = NULL;
  343. }
  344. }
  345. static int solo_buf_setup(struct videobuf_queue *vq, unsigned int *count,
  346. unsigned int *size)
  347. {
  348. struct solo_filehandle *fh = vq->priv_data;
  349. struct solo_dev *solo_dev = fh->solo_dev;
  350. *size = solo_image_size(solo_dev);
  351. if (*count < MIN_VID_BUFFERS)
  352. *count = MIN_VID_BUFFERS;
  353. return 0;
  354. }
  355. static int solo_buf_prepare(struct videobuf_queue *vq,
  356. struct videobuf_buffer *vb, enum v4l2_field field)
  357. {
  358. struct solo_filehandle *fh = vq->priv_data;
  359. struct solo_dev *solo_dev = fh->solo_dev;
  360. vb->size = solo_image_size(solo_dev);
  361. if (vb->baddr != 0 && vb->bsize < vb->size)
  362. return -EINVAL;
  363. /* XXX: These properties only change when queue is idle */
  364. vb->width = solo_dev->video_hsize;
  365. vb->height = solo_vlines(solo_dev);
  366. vb->bytesperline = solo_bytesperline(solo_dev);
  367. vb->field = field;
  368. if (vb->state == VIDEOBUF_NEEDS_INIT) {
  369. int rc = videobuf_iolock(vq, vb, NULL);
  370. if (rc < 0) {
  371. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  372. videobuf_dma_unmap(vq->dev, dma);
  373. videobuf_dma_free(dma);
  374. vb->state = VIDEOBUF_NEEDS_INIT;
  375. return rc;
  376. }
  377. }
  378. vb->state = VIDEOBUF_PREPARED;
  379. return 0;
  380. }
  381. static void solo_buf_queue(struct videobuf_queue *vq,
  382. struct videobuf_buffer *vb)
  383. {
  384. struct solo_filehandle *fh = vq->priv_data;
  385. struct solo_dev *solo_dev = fh->solo_dev;
  386. vb->state = VIDEOBUF_QUEUED;
  387. list_add_tail(&vb->queue, &fh->vidq_active);
  388. wake_up_interruptible(&solo_dev->disp_thread_wait);
  389. }
  390. static void solo_buf_release(struct videobuf_queue *vq,
  391. struct videobuf_buffer *vb)
  392. {
  393. struct videobuf_dmabuf *dma = videobuf_to_dma(vb);
  394. videobuf_dma_unmap(vq->dev, dma);
  395. videobuf_dma_free(dma);
  396. vb->state = VIDEOBUF_NEEDS_INIT;
  397. }
  398. static struct videobuf_queue_ops solo_video_qops = {
  399. .buf_setup = solo_buf_setup,
  400. .buf_prepare = solo_buf_prepare,
  401. .buf_queue = solo_buf_queue,
  402. .buf_release = solo_buf_release,
  403. };
  404. static unsigned int solo_v4l2_poll(struct file *file,
  405. struct poll_table_struct *wait)
  406. {
  407. struct solo_filehandle *fh = file->private_data;
  408. return videobuf_poll_stream(file, &fh->vidq, wait);
  409. }
  410. static int solo_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  411. {
  412. struct solo_filehandle *fh = file->private_data;
  413. return videobuf_mmap_mapper(&fh->vidq, vma);
  414. }
  415. static int solo_v4l2_open(struct file *file)
  416. {
  417. struct solo_dev *solo_dev = video_drvdata(file);
  418. struct solo_filehandle *fh;
  419. int ret;
  420. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  421. if (fh == NULL)
  422. return -ENOMEM;
  423. spin_lock_init(&fh->slock);
  424. INIT_LIST_HEAD(&fh->vidq_active);
  425. fh->solo_dev = solo_dev;
  426. file->private_data = fh;
  427. ret = solo_start_thread(fh);
  428. if (ret) {
  429. kfree(fh);
  430. return ret;
  431. }
  432. videobuf_queue_sg_init(&fh->vidq, &solo_video_qops,
  433. &solo_dev->pdev->dev, &fh->slock,
  434. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  435. SOLO_DISP_PIX_FIELD,
  436. sizeof(struct videobuf_buffer), fh, NULL);
  437. return 0;
  438. }
  439. static ssize_t solo_v4l2_read(struct file *file, char __user *data,
  440. size_t count, loff_t *ppos)
  441. {
  442. struct solo_filehandle *fh = file->private_data;
  443. return videobuf_read_stream(&fh->vidq, data, count, ppos, 0,
  444. file->f_flags & O_NONBLOCK);
  445. }
  446. static int solo_v4l2_release(struct file *file)
  447. {
  448. struct solo_filehandle *fh = file->private_data;
  449. videobuf_stop(&fh->vidq);
  450. videobuf_mmap_free(&fh->vidq);
  451. solo_stop_thread(fh);
  452. kfree(fh);
  453. return 0;
  454. }
  455. static int solo_querycap(struct file *file, void *priv,
  456. struct v4l2_capability *cap)
  457. {
  458. struct solo_filehandle *fh = priv;
  459. struct solo_dev *solo_dev = fh->solo_dev;
  460. strcpy(cap->driver, SOLO6X10_NAME);
  461. strcpy(cap->card, "Softlogic 6x10");
  462. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI %s",
  463. pci_name(solo_dev->pdev));
  464. cap->version = SOLO6X10_VER_NUM;
  465. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  466. V4L2_CAP_READWRITE |
  467. V4L2_CAP_STREAMING;
  468. return 0;
  469. }
  470. static int solo_enum_ext_input(struct solo_dev *solo_dev,
  471. struct v4l2_input *input)
  472. {
  473. static const char *dispnames_1[] = { "4UP" };
  474. static const char *dispnames_2[] = { "4UP-1", "4UP-2" };
  475. static const char *dispnames_5[] = {
  476. "4UP-1", "4UP-2", "4UP-3", "4UP-4", "16UP"
  477. };
  478. const char **dispnames;
  479. if (input->index >= (solo_dev->nr_chans + solo_dev->nr_ext))
  480. return -EINVAL;
  481. if (solo_dev->nr_ext == 5)
  482. dispnames = dispnames_5;
  483. else if (solo_dev->nr_ext == 2)
  484. dispnames = dispnames_2;
  485. else
  486. dispnames = dispnames_1;
  487. snprintf(input->name, sizeof(input->name), "Multi %s",
  488. dispnames[input->index - solo_dev->nr_chans]);
  489. return 0;
  490. }
  491. static int solo_enum_input(struct file *file, void *priv,
  492. struct v4l2_input *input)
  493. {
  494. struct solo_filehandle *fh = priv;
  495. struct solo_dev *solo_dev = fh->solo_dev;
  496. if (input->index >= solo_dev->nr_chans) {
  497. int ret = solo_enum_ext_input(solo_dev, input);
  498. if (ret < 0)
  499. return ret;
  500. } else {
  501. snprintf(input->name, sizeof(input->name), "Camera %d",
  502. input->index + 1);
  503. /* We can only check this for normal inputs */
  504. if (!tw28_get_video_status(solo_dev, input->index))
  505. input->status = V4L2_IN_ST_NO_SIGNAL;
  506. }
  507. input->type = V4L2_INPUT_TYPE_CAMERA;
  508. if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
  509. input->std = V4L2_STD_NTSC_M;
  510. else
  511. input->std = V4L2_STD_PAL_B;
  512. return 0;
  513. }
  514. static int solo_set_input(struct file *file, void *priv, unsigned int index)
  515. {
  516. struct solo_filehandle *fh = priv;
  517. return solo_v4l2_set_ch(fh->solo_dev, index);
  518. }
  519. static int solo_get_input(struct file *file, void *priv, unsigned int *index)
  520. {
  521. struct solo_filehandle *fh = priv;
  522. *index = fh->solo_dev->cur_disp_ch;
  523. return 0;
  524. }
  525. static int solo_enum_fmt_cap(struct file *file, void *priv,
  526. struct v4l2_fmtdesc *f)
  527. {
  528. if (f->index)
  529. return -EINVAL;
  530. f->pixelformat = V4L2_PIX_FMT_UYVY;
  531. strlcpy(f->description, "UYUV 4:2:2 Packed", sizeof(f->description));
  532. return 0;
  533. }
  534. static int solo_try_fmt_cap(struct file *file, void *priv,
  535. struct v4l2_format *f)
  536. {
  537. struct solo_filehandle *fh = priv;
  538. struct solo_dev *solo_dev = fh->solo_dev;
  539. struct v4l2_pix_format *pix = &f->fmt.pix;
  540. int image_size = solo_image_size(solo_dev);
  541. /* Check supported sizes */
  542. if (pix->width != solo_dev->video_hsize)
  543. pix->width = solo_dev->video_hsize;
  544. if (pix->height != solo_vlines(solo_dev))
  545. pix->height = solo_vlines(solo_dev);
  546. if (pix->sizeimage != image_size)
  547. pix->sizeimage = image_size;
  548. /* Check formats */
  549. if (pix->field == V4L2_FIELD_ANY)
  550. pix->field = SOLO_DISP_PIX_FIELD;
  551. if (pix->pixelformat != V4L2_PIX_FMT_UYVY ||
  552. pix->field != SOLO_DISP_PIX_FIELD ||
  553. pix->colorspace != V4L2_COLORSPACE_SMPTE170M)
  554. return -EINVAL;
  555. return 0;
  556. }
  557. static int solo_set_fmt_cap(struct file *file, void *priv,
  558. struct v4l2_format *f)
  559. {
  560. struct solo_filehandle *fh = priv;
  561. if (videobuf_queue_is_busy(&fh->vidq))
  562. return -EBUSY;
  563. /* For right now, if it doesn't match our running config,
  564. * then fail */
  565. return solo_try_fmt_cap(file, priv, f);
  566. }
  567. static int solo_get_fmt_cap(struct file *file, void *priv,
  568. struct v4l2_format *f)
  569. {
  570. struct solo_filehandle *fh = priv;
  571. struct solo_dev *solo_dev = fh->solo_dev;
  572. struct v4l2_pix_format *pix = &f->fmt.pix;
  573. pix->width = solo_dev->video_hsize;
  574. pix->height = solo_vlines(solo_dev);
  575. pix->pixelformat = V4L2_PIX_FMT_UYVY;
  576. pix->field = SOLO_DISP_PIX_FIELD;
  577. pix->sizeimage = solo_image_size(solo_dev);
  578. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  579. pix->bytesperline = solo_bytesperline(solo_dev);
  580. return 0;
  581. }
  582. static int solo_reqbufs(struct file *file, void *priv,
  583. struct v4l2_requestbuffers *req)
  584. {
  585. struct solo_filehandle *fh = priv;
  586. return videobuf_reqbufs(&fh->vidq, req);
  587. }
  588. static int solo_querybuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  589. {
  590. struct solo_filehandle *fh = priv;
  591. return videobuf_querybuf(&fh->vidq, buf);
  592. }
  593. static int solo_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  594. {
  595. struct solo_filehandle *fh = priv;
  596. return videobuf_qbuf(&fh->vidq, buf);
  597. }
  598. static int solo_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  599. {
  600. struct solo_filehandle *fh = priv;
  601. return videobuf_dqbuf(&fh->vidq, buf, file->f_flags & O_NONBLOCK);
  602. }
  603. static int solo_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  604. {
  605. struct solo_filehandle *fh = priv;
  606. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  607. return -EINVAL;
  608. return videobuf_streamon(&fh->vidq);
  609. }
  610. static int solo_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  611. {
  612. struct solo_filehandle *fh = priv;
  613. if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  614. return -EINVAL;
  615. return videobuf_streamoff(&fh->vidq);
  616. }
  617. static int solo_s_std(struct file *file, void *priv, v4l2_std_id *i)
  618. {
  619. return 0;
  620. }
  621. static const u32 solo_motion_ctrls[] = {
  622. V4L2_CID_MOTION_TRACE,
  623. 0
  624. };
  625. static const u32 *solo_ctrl_classes[] = {
  626. solo_motion_ctrls,
  627. NULL
  628. };
  629. static int solo_disp_queryctrl(struct file *file, void *priv,
  630. struct v4l2_queryctrl *qc)
  631. {
  632. qc->id = v4l2_ctrl_next(solo_ctrl_classes, qc->id);
  633. if (!qc->id)
  634. return -EINVAL;
  635. switch (qc->id) {
  636. #ifdef PRIVATE_CIDS
  637. case V4L2_CID_MOTION_TRACE:
  638. qc->type = V4L2_CTRL_TYPE_BOOLEAN;
  639. qc->minimum = 0;
  640. qc->maximum = qc->step = 1;
  641. qc->default_value = 0;
  642. strlcpy(qc->name, "Motion Detection Trace", sizeof(qc->name));
  643. return 0;
  644. #else
  645. case V4L2_CID_MOTION_TRACE:
  646. return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0);
  647. #endif
  648. }
  649. return -EINVAL;
  650. }
  651. static int solo_disp_g_ctrl(struct file *file, void *priv,
  652. struct v4l2_control *ctrl)
  653. {
  654. struct solo_filehandle *fh = priv;
  655. struct solo_dev *solo_dev = fh->solo_dev;
  656. switch (ctrl->id) {
  657. case V4L2_CID_MOTION_TRACE:
  658. ctrl->value = solo_reg_read(solo_dev, SOLO_VI_MOTION_BAR)
  659. ? 1 : 0;
  660. return 0;
  661. }
  662. return -EINVAL;
  663. }
  664. static int solo_disp_s_ctrl(struct file *file, void *priv,
  665. struct v4l2_control *ctrl)
  666. {
  667. struct solo_filehandle *fh = priv;
  668. struct solo_dev *solo_dev = fh->solo_dev;
  669. switch (ctrl->id) {
  670. case V4L2_CID_MOTION_TRACE:
  671. if (ctrl->value) {
  672. solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER,
  673. SOLO_VI_MOTION_Y_ADD |
  674. SOLO_VI_MOTION_Y_VALUE(0x20) |
  675. SOLO_VI_MOTION_CB_VALUE(0x10) |
  676. SOLO_VI_MOTION_CR_VALUE(0x10));
  677. solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR,
  678. SOLO_VI_MOTION_CR_ADD |
  679. SOLO_VI_MOTION_Y_VALUE(0x10) |
  680. SOLO_VI_MOTION_CB_VALUE(0x80) |
  681. SOLO_VI_MOTION_CR_VALUE(0x10));
  682. } else {
  683. solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, 0);
  684. solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, 0);
  685. }
  686. return 0;
  687. }
  688. return -EINVAL;
  689. }
  690. static const struct v4l2_file_operations solo_v4l2_fops = {
  691. .owner = THIS_MODULE,
  692. .open = solo_v4l2_open,
  693. .release = solo_v4l2_release,
  694. .read = solo_v4l2_read,
  695. .poll = solo_v4l2_poll,
  696. .mmap = solo_v4l2_mmap,
  697. .ioctl = video_ioctl2,
  698. };
  699. static const struct v4l2_ioctl_ops solo_v4l2_ioctl_ops = {
  700. .vidioc_querycap = solo_querycap,
  701. .vidioc_s_std = solo_s_std,
  702. /* Input callbacks */
  703. .vidioc_enum_input = solo_enum_input,
  704. .vidioc_s_input = solo_set_input,
  705. .vidioc_g_input = solo_get_input,
  706. /* Video capture format callbacks */
  707. .vidioc_enum_fmt_vid_cap = solo_enum_fmt_cap,
  708. .vidioc_try_fmt_vid_cap = solo_try_fmt_cap,
  709. .vidioc_s_fmt_vid_cap = solo_set_fmt_cap,
  710. .vidioc_g_fmt_vid_cap = solo_get_fmt_cap,
  711. /* Streaming I/O */
  712. .vidioc_reqbufs = solo_reqbufs,
  713. .vidioc_querybuf = solo_querybuf,
  714. .vidioc_qbuf = solo_qbuf,
  715. .vidioc_dqbuf = solo_dqbuf,
  716. .vidioc_streamon = solo_streamon,
  717. .vidioc_streamoff = solo_streamoff,
  718. /* Controls */
  719. .vidioc_queryctrl = solo_disp_queryctrl,
  720. .vidioc_g_ctrl = solo_disp_g_ctrl,
  721. .vidioc_s_ctrl = solo_disp_s_ctrl,
  722. };
  723. static struct video_device solo_v4l2_template = {
  724. .name = SOLO6X10_NAME,
  725. .fops = &solo_v4l2_fops,
  726. .ioctl_ops = &solo_v4l2_ioctl_ops,
  727. .minor = -1,
  728. .release = video_device_release,
  729. .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_B,
  730. .current_norm = V4L2_STD_NTSC_M,
  731. };
  732. int solo_v4l2_init(struct solo_dev *solo_dev)
  733. {
  734. int ret;
  735. int i;
  736. init_waitqueue_head(&solo_dev->disp_thread_wait);
  737. solo_dev->vfd = video_device_alloc();
  738. if (!solo_dev->vfd)
  739. return -ENOMEM;
  740. *solo_dev->vfd = solo_v4l2_template;
  741. solo_dev->vfd->parent = &solo_dev->pdev->dev;
  742. ret = video_register_device(solo_dev->vfd, VFL_TYPE_GRABBER, video_nr);
  743. if (ret < 0) {
  744. video_device_release(solo_dev->vfd);
  745. solo_dev->vfd = NULL;
  746. return ret;
  747. }
  748. video_set_drvdata(solo_dev->vfd, solo_dev);
  749. snprintf(solo_dev->vfd->name, sizeof(solo_dev->vfd->name), "%s (%i)",
  750. SOLO6X10_NAME, solo_dev->vfd->num);
  751. if (video_nr != -1)
  752. video_nr++;
  753. dev_info(&solo_dev->pdev->dev, "Display as /dev/video%d with "
  754. "%d inputs (%d extended)\n", solo_dev->vfd->num,
  755. solo_dev->nr_chans, solo_dev->nr_ext);
  756. /* Cycle all the channels and clear */
  757. for (i = 0; i < solo_dev->nr_chans; i++) {
  758. solo_v4l2_set_ch(solo_dev, i);
  759. while (erase_off(solo_dev))
  760. ;/* Do nothing */
  761. }
  762. /* Set the default display channel */
  763. solo_v4l2_set_ch(solo_dev, 0);
  764. while (erase_off(solo_dev))
  765. ;/* Do nothing */
  766. solo_irq_on(solo_dev, SOLO_IRQ_VIDEO_IN);
  767. return 0;
  768. }
  769. void solo_v4l2_exit(struct solo_dev *solo_dev)
  770. {
  771. solo_irq_off(solo_dev, SOLO_IRQ_VIDEO_IN);
  772. if (solo_dev->vfd) {
  773. video_unregister_device(solo_dev->vfd);
  774. solo_dev->vfd = NULL;
  775. }
  776. }