v4l2-pci-skeleton.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  1. /*
  2. * This is a V4L2 PCI Skeleton Driver. It gives an initial skeleton source
  3. * for use with other PCI drivers.
  4. *
  5. * This skeleton PCI driver assumes that the card has an S-Video connector as
  6. * input 0 and an HDMI connector as input 1.
  7. *
  8. * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
  9. *
  10. * This program is free software; you may redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; version 2 of the License.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #include <linux/types.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/kmod.h>
  28. #include <linux/mutex.h>
  29. #include <linux/pci.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/videodev2.h>
  32. #include <linux/v4l2-dv-timings.h>
  33. #include <media/v4l2-device.h>
  34. #include <media/v4l2-dev.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-dv-timings.h>
  37. #include <media/v4l2-ctrls.h>
  38. #include <media/v4l2-event.h>
  39. #include <media/videobuf2-v4l2.h>
  40. #include <media/videobuf2-dma-contig.h>
  41. MODULE_DESCRIPTION("V4L2 PCI Skeleton Driver");
  42. MODULE_AUTHOR("Hans Verkuil");
  43. MODULE_LICENSE("GPL v2");
  44. /**
  45. * struct skeleton - All internal data for one instance of device
  46. * @pdev: PCI device
  47. * @v4l2_dev: top-level v4l2 device struct
  48. * @vdev: video node structure
  49. * @ctrl_handler: control handler structure
  50. * @lock: ioctl serialization mutex
  51. * @std: current SDTV standard
  52. * @timings: current HDTV timings
  53. * @format: current pix format
  54. * @input: current video input (0 = SDTV, 1 = HDTV)
  55. * @queue: vb2 video capture queue
  56. * @alloc_ctx: vb2 contiguous DMA context
  57. * @qlock: spinlock controlling access to buf_list and sequence
  58. * @buf_list: list of buffers queued for DMA
  59. * @sequence: frame sequence counter
  60. */
  61. struct skeleton {
  62. struct pci_dev *pdev;
  63. struct v4l2_device v4l2_dev;
  64. struct video_device vdev;
  65. struct v4l2_ctrl_handler ctrl_handler;
  66. struct mutex lock;
  67. v4l2_std_id std;
  68. struct v4l2_dv_timings timings;
  69. struct v4l2_pix_format format;
  70. unsigned input;
  71. struct vb2_queue queue;
  72. struct vb2_alloc_ctx *alloc_ctx;
  73. spinlock_t qlock;
  74. struct list_head buf_list;
  75. unsigned field;
  76. unsigned sequence;
  77. };
  78. struct skel_buffer {
  79. struct vb2_buffer vb;
  80. struct list_head list;
  81. };
  82. static inline struct skel_buffer *to_skel_buffer(struct vb2_buffer *vb2)
  83. {
  84. return container_of(vb2, struct skel_buffer, vb);
  85. }
  86. static const struct pci_device_id skeleton_pci_tbl[] = {
  87. /* { PCI_DEVICE(PCI_VENDOR_ID_, PCI_DEVICE_ID_) }, */
  88. { 0, }
  89. };
  90. MODULE_DEVICE_TABLE(pci, skeleton_pci_tbl);
  91. /*
  92. * HDTV: this structure has the capabilities of the HDTV receiver.
  93. * It is used to constrain the huge list of possible formats based
  94. * upon the hardware capabilities.
  95. */
  96. static const struct v4l2_dv_timings_cap skel_timings_cap = {
  97. .type = V4L2_DV_BT_656_1120,
  98. /* keep this initialization for compatibility with GCC < 4.4.6 */
  99. .reserved = { 0 },
  100. V4L2_INIT_BT_TIMINGS(
  101. 720, 1920, /* min/max width */
  102. 480, 1080, /* min/max height */
  103. 27000000, 74250000, /* min/max pixelclock*/
  104. V4L2_DV_BT_STD_CEA861, /* Supported standards */
  105. /* capabilities */
  106. V4L2_DV_BT_CAP_INTERLACED | V4L2_DV_BT_CAP_PROGRESSIVE
  107. )
  108. };
  109. /*
  110. * Supported SDTV standards. This does the same job as skel_timings_cap, but
  111. * for standard TV formats.
  112. */
  113. #define SKEL_TVNORMS V4L2_STD_ALL
  114. /*
  115. * Interrupt handler: typically interrupts happen after a new frame has been
  116. * captured. It is the job of the handler to remove the new frame from the
  117. * internal list and give it back to the vb2 framework, updating the sequence
  118. * counter, field and timestamp at the same time.
  119. */
  120. static irqreturn_t skeleton_irq(int irq, void *dev_id)
  121. {
  122. #ifdef TODO
  123. struct skeleton *skel = dev_id;
  124. /* handle interrupt */
  125. /* Once a new frame has been captured, mark it as done like this: */
  126. if (captured_new_frame) {
  127. ...
  128. spin_lock(&skel->qlock);
  129. list_del(&new_buf->list);
  130. spin_unlock(&skel->qlock);
  131. v4l2_get_timestamp(&new_buf->vb.v4l2_buf.timestamp);
  132. new_buf->vb.v4l2_buf.sequence = skel->sequence++;
  133. new_buf->vb.v4l2_buf.field = skel->field;
  134. if (skel->format.field == V4L2_FIELD_ALTERNATE) {
  135. if (skel->field == V4L2_FIELD_BOTTOM)
  136. skel->field = V4L2_FIELD_TOP;
  137. else if (skel->field == V4L2_FIELD_TOP)
  138. skel->field = V4L2_FIELD_BOTTOM;
  139. }
  140. vb2_buffer_done(&new_buf->vb, VB2_BUF_STATE_DONE);
  141. }
  142. #endif
  143. return IRQ_HANDLED;
  144. }
  145. /*
  146. * Setup the constraints of the queue: besides setting the number of planes
  147. * per buffer and the size and allocation context of each plane, it also
  148. * checks if sufficient buffers have been allocated. Usually 3 is a good
  149. * minimum number: many DMA engines need a minimum of 2 buffers in the
  150. * queue and you need to have another available for userspace processing.
  151. */
  152. static int queue_setup(struct vb2_queue *vq, const void *parg,
  153. unsigned int *nbuffers, unsigned int *nplanes,
  154. unsigned int sizes[], void *alloc_ctxs[])
  155. {
  156. const struct v4l2_format *fmt = parg;
  157. struct skeleton *skel = vb2_get_drv_priv(vq);
  158. skel->field = skel->format.field;
  159. if (skel->field == V4L2_FIELD_ALTERNATE) {
  160. /*
  161. * You cannot use read() with FIELD_ALTERNATE since the field
  162. * information (TOP/BOTTOM) cannot be passed back to the user.
  163. */
  164. if (vb2_fileio_is_active(vq))
  165. return -EINVAL;
  166. skel->field = V4L2_FIELD_TOP;
  167. }
  168. if (vq->num_buffers + *nbuffers < 3)
  169. *nbuffers = 3 - vq->num_buffers;
  170. if (fmt && fmt->fmt.pix.sizeimage < skel->format.sizeimage)
  171. return -EINVAL;
  172. *nplanes = 1;
  173. sizes[0] = fmt ? fmt->fmt.pix.sizeimage : skel->format.sizeimage;
  174. alloc_ctxs[0] = skel->alloc_ctx;
  175. return 0;
  176. }
  177. /*
  178. * Prepare the buffer for queueing to the DMA engine: check and set the
  179. * payload size.
  180. */
  181. static int buffer_prepare(struct vb2_buffer *vb)
  182. {
  183. struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
  184. unsigned long size = skel->format.sizeimage;
  185. if (vb2_plane_size(vb, 0) < size) {
  186. dev_err(&skel->pdev->dev, "buffer too small (%lu < %lu)\n",
  187. vb2_plane_size(vb, 0), size);
  188. return -EINVAL;
  189. }
  190. vb2_set_plane_payload(vb, 0, size);
  191. return 0;
  192. }
  193. /*
  194. * Queue this buffer to the DMA engine.
  195. */
  196. static void buffer_queue(struct vb2_buffer *vb)
  197. {
  198. struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
  199. struct skel_buffer *buf = to_skel_buffer(vb);
  200. unsigned long flags;
  201. spin_lock_irqsave(&skel->qlock, flags);
  202. list_add_tail(&buf->list, &skel->buf_list);
  203. /* TODO: Update any DMA pointers if necessary */
  204. spin_unlock_irqrestore(&skel->qlock, flags);
  205. }
  206. static void return_all_buffers(struct skeleton *skel,
  207. enum vb2_buffer_state state)
  208. {
  209. struct skel_buffer *buf, *node;
  210. unsigned long flags;
  211. spin_lock_irqsave(&skel->qlock, flags);
  212. list_for_each_entry_safe(buf, node, &skel->buf_list, list) {
  213. vb2_buffer_done(&buf->vb, state);
  214. list_del(&buf->list);
  215. }
  216. spin_unlock_irqrestore(&skel->qlock, flags);
  217. }
  218. /*
  219. * Start streaming. First check if the minimum number of buffers have been
  220. * queued. If not, then return -ENOBUFS and the vb2 framework will call
  221. * this function again the next time a buffer has been queued until enough
  222. * buffers are available to actually start the DMA engine.
  223. */
  224. static int start_streaming(struct vb2_queue *vq, unsigned int count)
  225. {
  226. struct skeleton *skel = vb2_get_drv_priv(vq);
  227. int ret = 0;
  228. skel->sequence = 0;
  229. /* TODO: start DMA */
  230. if (ret) {
  231. /*
  232. * In case of an error, return all active buffers to the
  233. * QUEUED state
  234. */
  235. return_all_buffers(skel, VB2_BUF_STATE_QUEUED);
  236. }
  237. return ret;
  238. }
  239. /*
  240. * Stop the DMA engine. Any remaining buffers in the DMA queue are dequeued
  241. * and passed on to the vb2 framework marked as STATE_ERROR.
  242. */
  243. static void stop_streaming(struct vb2_queue *vq)
  244. {
  245. struct skeleton *skel = vb2_get_drv_priv(vq);
  246. /* TODO: stop DMA */
  247. /* Release all active buffers */
  248. return_all_buffers(skel, VB2_BUF_STATE_ERROR);
  249. }
  250. /*
  251. * The vb2 queue ops. Note that since q->lock is set we can use the standard
  252. * vb2_ops_wait_prepare/finish helper functions. If q->lock would be NULL,
  253. * then this driver would have to provide these ops.
  254. */
  255. static struct vb2_ops skel_qops = {
  256. .queue_setup = queue_setup,
  257. .buf_prepare = buffer_prepare,
  258. .buf_queue = buffer_queue,
  259. .start_streaming = start_streaming,
  260. .stop_streaming = stop_streaming,
  261. .wait_prepare = vb2_ops_wait_prepare,
  262. .wait_finish = vb2_ops_wait_finish,
  263. };
  264. /*
  265. * Required ioctl querycap. Note that the version field is prefilled with
  266. * the version of the kernel.
  267. */
  268. static int skeleton_querycap(struct file *file, void *priv,
  269. struct v4l2_capability *cap)
  270. {
  271. struct skeleton *skel = video_drvdata(file);
  272. strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
  273. strlcpy(cap->card, "V4L2 PCI Skeleton", sizeof(cap->card));
  274. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  275. pci_name(skel->pdev));
  276. cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  277. V4L2_CAP_STREAMING;
  278. cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
  279. return 0;
  280. }
  281. /*
  282. * Helper function to check and correct struct v4l2_pix_format. It's used
  283. * not only in VIDIOC_TRY/S_FMT, but also elsewhere if changes to the SDTV
  284. * standard, HDTV timings or the video input would require updating the
  285. * current format.
  286. */
  287. static void skeleton_fill_pix_format(struct skeleton *skel,
  288. struct v4l2_pix_format *pix)
  289. {
  290. pix->pixelformat = V4L2_PIX_FMT_YUYV;
  291. if (skel->input == 0) {
  292. /* S-Video input */
  293. pix->width = 720;
  294. pix->height = (skel->std & V4L2_STD_525_60) ? 480 : 576;
  295. pix->field = V4L2_FIELD_INTERLACED;
  296. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  297. } else {
  298. /* HDMI input */
  299. pix->width = skel->timings.bt.width;
  300. pix->height = skel->timings.bt.height;
  301. if (skel->timings.bt.interlaced) {
  302. pix->field = V4L2_FIELD_ALTERNATE;
  303. pix->height /= 2;
  304. } else {
  305. pix->field = V4L2_FIELD_NONE;
  306. }
  307. pix->colorspace = V4L2_COLORSPACE_REC709;
  308. }
  309. /*
  310. * The YUYV format is four bytes for every two pixels, so bytesperline
  311. * is width * 2.
  312. */
  313. pix->bytesperline = pix->width * 2;
  314. pix->sizeimage = pix->bytesperline * pix->height;
  315. pix->priv = 0;
  316. }
  317. static int skeleton_try_fmt_vid_cap(struct file *file, void *priv,
  318. struct v4l2_format *f)
  319. {
  320. struct skeleton *skel = video_drvdata(file);
  321. struct v4l2_pix_format *pix = &f->fmt.pix;
  322. /*
  323. * Due to historical reasons providing try_fmt with an unsupported
  324. * pixelformat will return -EINVAL for video receivers. Webcam drivers,
  325. * however, will silently correct the pixelformat. Some video capture
  326. * applications rely on this behavior...
  327. */
  328. if (pix->pixelformat != V4L2_PIX_FMT_YUYV)
  329. return -EINVAL;
  330. skeleton_fill_pix_format(skel, pix);
  331. return 0;
  332. }
  333. static int skeleton_s_fmt_vid_cap(struct file *file, void *priv,
  334. struct v4l2_format *f)
  335. {
  336. struct skeleton *skel = video_drvdata(file);
  337. int ret;
  338. ret = skeleton_try_fmt_vid_cap(file, priv, f);
  339. if (ret)
  340. return ret;
  341. /*
  342. * It is not allowed to change the format while buffers for use with
  343. * streaming have already been allocated.
  344. */
  345. if (vb2_is_busy(&skel->queue))
  346. return -EBUSY;
  347. /* TODO: change format */
  348. skel->format = f->fmt.pix;
  349. return 0;
  350. }
  351. static int skeleton_g_fmt_vid_cap(struct file *file, void *priv,
  352. struct v4l2_format *f)
  353. {
  354. struct skeleton *skel = video_drvdata(file);
  355. f->fmt.pix = skel->format;
  356. return 0;
  357. }
  358. static int skeleton_enum_fmt_vid_cap(struct file *file, void *priv,
  359. struct v4l2_fmtdesc *f)
  360. {
  361. if (f->index != 0)
  362. return -EINVAL;
  363. f->pixelformat = V4L2_PIX_FMT_YUYV;
  364. return 0;
  365. }
  366. static int skeleton_s_std(struct file *file, void *priv, v4l2_std_id std)
  367. {
  368. struct skeleton *skel = video_drvdata(file);
  369. /* S_STD is not supported on the HDMI input */
  370. if (skel->input)
  371. return -ENODATA;
  372. /*
  373. * No change, so just return. Some applications call S_STD again after
  374. * the buffers for streaming have been set up, so we have to allow for
  375. * this behavior.
  376. */
  377. if (std == skel->std)
  378. return 0;
  379. /*
  380. * Changing the standard implies a format change, which is not allowed
  381. * while buffers for use with streaming have already been allocated.
  382. */
  383. if (vb2_is_busy(&skel->queue))
  384. return -EBUSY;
  385. /* TODO: handle changing std */
  386. skel->std = std;
  387. /* Update the internal format */
  388. skeleton_fill_pix_format(skel, &skel->format);
  389. return 0;
  390. }
  391. static int skeleton_g_std(struct file *file, void *priv, v4l2_std_id *std)
  392. {
  393. struct skeleton *skel = video_drvdata(file);
  394. /* G_STD is not supported on the HDMI input */
  395. if (skel->input)
  396. return -ENODATA;
  397. *std = skel->std;
  398. return 0;
  399. }
  400. /*
  401. * Query the current standard as seen by the hardware. This function shall
  402. * never actually change the standard, it just detects and reports.
  403. * The framework will initially set *std to tvnorms (i.e. the set of
  404. * supported standards by this input), and this function should just AND
  405. * this value. If there is no signal, then *std should be set to 0.
  406. */
  407. static int skeleton_querystd(struct file *file, void *priv, v4l2_std_id *std)
  408. {
  409. struct skeleton *skel = video_drvdata(file);
  410. /* QUERY_STD is not supported on the HDMI input */
  411. if (skel->input)
  412. return -ENODATA;
  413. #ifdef TODO
  414. /*
  415. * Query currently seen standard. Initial value of *std is
  416. * V4L2_STD_ALL. This function should look something like this:
  417. */
  418. get_signal_info();
  419. if (no_signal) {
  420. *std = 0;
  421. return 0;
  422. }
  423. /* Use signal information to reduce the number of possible standards */
  424. if (signal_has_525_lines)
  425. *std &= V4L2_STD_525_60;
  426. else
  427. *std &= V4L2_STD_625_50;
  428. #endif
  429. return 0;
  430. }
  431. static int skeleton_s_dv_timings(struct file *file, void *_fh,
  432. struct v4l2_dv_timings *timings)
  433. {
  434. struct skeleton *skel = video_drvdata(file);
  435. /* S_DV_TIMINGS is not supported on the S-Video input */
  436. if (skel->input == 0)
  437. return -ENODATA;
  438. /* Quick sanity check */
  439. if (!v4l2_valid_dv_timings(timings, &skel_timings_cap, NULL, NULL))
  440. return -EINVAL;
  441. /* Check if the timings are part of the CEA-861 timings. */
  442. if (!v4l2_find_dv_timings_cap(timings, &skel_timings_cap,
  443. 0, NULL, NULL))
  444. return -EINVAL;
  445. /* Return 0 if the new timings are the same as the current timings. */
  446. if (v4l2_match_dv_timings(timings, &skel->timings, 0))
  447. return 0;
  448. /*
  449. * Changing the timings implies a format change, which is not allowed
  450. * while buffers for use with streaming have already been allocated.
  451. */
  452. if (vb2_is_busy(&skel->queue))
  453. return -EBUSY;
  454. /* TODO: Configure new timings */
  455. /* Save timings */
  456. skel->timings = *timings;
  457. /* Update the internal format */
  458. skeleton_fill_pix_format(skel, &skel->format);
  459. return 0;
  460. }
  461. static int skeleton_g_dv_timings(struct file *file, void *_fh,
  462. struct v4l2_dv_timings *timings)
  463. {
  464. struct skeleton *skel = video_drvdata(file);
  465. /* G_DV_TIMINGS is not supported on the S-Video input */
  466. if (skel->input == 0)
  467. return -ENODATA;
  468. *timings = skel->timings;
  469. return 0;
  470. }
  471. static int skeleton_enum_dv_timings(struct file *file, void *_fh,
  472. struct v4l2_enum_dv_timings *timings)
  473. {
  474. struct skeleton *skel = video_drvdata(file);
  475. /* ENUM_DV_TIMINGS is not supported on the S-Video input */
  476. if (skel->input == 0)
  477. return -ENODATA;
  478. return v4l2_enum_dv_timings_cap(timings, &skel_timings_cap,
  479. NULL, NULL);
  480. }
  481. /*
  482. * Query the current timings as seen by the hardware. This function shall
  483. * never actually change the timings, it just detects and reports.
  484. * If no signal is detected, then return -ENOLINK. If the hardware cannot
  485. * lock to the signal, then return -ENOLCK. If the signal is out of range
  486. * of the capabilities of the system (e.g., it is possible that the receiver
  487. * can lock but that the DMA engine it is connected to cannot handle
  488. * pixelclocks above a certain frequency), then -ERANGE is returned.
  489. */
  490. static int skeleton_query_dv_timings(struct file *file, void *_fh,
  491. struct v4l2_dv_timings *timings)
  492. {
  493. struct skeleton *skel = video_drvdata(file);
  494. /* QUERY_DV_TIMINGS is not supported on the S-Video input */
  495. if (skel->input == 0)
  496. return -ENODATA;
  497. #ifdef TODO
  498. /*
  499. * Query currently seen timings. This function should look
  500. * something like this:
  501. */
  502. detect_timings();
  503. if (no_signal)
  504. return -ENOLINK;
  505. if (cannot_lock_to_signal)
  506. return -ENOLCK;
  507. if (signal_out_of_range_of_capabilities)
  508. return -ERANGE;
  509. /* Useful for debugging */
  510. v4l2_print_dv_timings(skel->v4l2_dev.name, "query_dv_timings:",
  511. timings, true);
  512. #endif
  513. return 0;
  514. }
  515. static int skeleton_dv_timings_cap(struct file *file, void *fh,
  516. struct v4l2_dv_timings_cap *cap)
  517. {
  518. struct skeleton *skel = video_drvdata(file);
  519. /* DV_TIMINGS_CAP is not supported on the S-Video input */
  520. if (skel->input == 0)
  521. return -ENODATA;
  522. *cap = skel_timings_cap;
  523. return 0;
  524. }
  525. static int skeleton_enum_input(struct file *file, void *priv,
  526. struct v4l2_input *i)
  527. {
  528. if (i->index > 1)
  529. return -EINVAL;
  530. i->type = V4L2_INPUT_TYPE_CAMERA;
  531. if (i->index == 0) {
  532. i->std = SKEL_TVNORMS;
  533. strlcpy(i->name, "S-Video", sizeof(i->name));
  534. i->capabilities = V4L2_IN_CAP_STD;
  535. } else {
  536. i->std = 0;
  537. strlcpy(i->name, "HDMI", sizeof(i->name));
  538. i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
  539. }
  540. return 0;
  541. }
  542. static int skeleton_s_input(struct file *file, void *priv, unsigned int i)
  543. {
  544. struct skeleton *skel = video_drvdata(file);
  545. if (i > 1)
  546. return -EINVAL;
  547. /*
  548. * Changing the input implies a format change, which is not allowed
  549. * while buffers for use with streaming have already been allocated.
  550. */
  551. if (vb2_is_busy(&skel->queue))
  552. return -EBUSY;
  553. skel->input = i;
  554. /*
  555. * Update tvnorms. The tvnorms value is used by the core to implement
  556. * VIDIOC_ENUMSTD so it has to be correct. If tvnorms == 0, then
  557. * ENUMSTD will return -ENODATA.
  558. */
  559. skel->vdev.tvnorms = i ? 0 : SKEL_TVNORMS;
  560. /* Update the internal format */
  561. skeleton_fill_pix_format(skel, &skel->format);
  562. return 0;
  563. }
  564. static int skeleton_g_input(struct file *file, void *priv, unsigned int *i)
  565. {
  566. struct skeleton *skel = video_drvdata(file);
  567. *i = skel->input;
  568. return 0;
  569. }
  570. /* The control handler. */
  571. static int skeleton_s_ctrl(struct v4l2_ctrl *ctrl)
  572. {
  573. /*struct skeleton *skel =
  574. container_of(ctrl->handler, struct skeleton, ctrl_handler);*/
  575. switch (ctrl->id) {
  576. case V4L2_CID_BRIGHTNESS:
  577. /* TODO: set brightness to ctrl->val */
  578. break;
  579. case V4L2_CID_CONTRAST:
  580. /* TODO: set contrast to ctrl->val */
  581. break;
  582. case V4L2_CID_SATURATION:
  583. /* TODO: set saturation to ctrl->val */
  584. break;
  585. case V4L2_CID_HUE:
  586. /* TODO: set hue to ctrl->val */
  587. break;
  588. default:
  589. return -EINVAL;
  590. }
  591. return 0;
  592. }
  593. /* ------------------------------------------------------------------
  594. File operations for the device
  595. ------------------------------------------------------------------*/
  596. static const struct v4l2_ctrl_ops skel_ctrl_ops = {
  597. .s_ctrl = skeleton_s_ctrl,
  598. };
  599. /*
  600. * The set of all supported ioctls. Note that all the streaming ioctls
  601. * use the vb2 helper functions that take care of all the locking and
  602. * that also do ownership tracking (i.e. only the filehandle that requested
  603. * the buffers can call the streaming ioctls, all other filehandles will
  604. * receive -EBUSY if they attempt to call the same streaming ioctls).
  605. *
  606. * The last three ioctls also use standard helper functions: these implement
  607. * standard behavior for drivers with controls.
  608. */
  609. static const struct v4l2_ioctl_ops skel_ioctl_ops = {
  610. .vidioc_querycap = skeleton_querycap,
  611. .vidioc_try_fmt_vid_cap = skeleton_try_fmt_vid_cap,
  612. .vidioc_s_fmt_vid_cap = skeleton_s_fmt_vid_cap,
  613. .vidioc_g_fmt_vid_cap = skeleton_g_fmt_vid_cap,
  614. .vidioc_enum_fmt_vid_cap = skeleton_enum_fmt_vid_cap,
  615. .vidioc_g_std = skeleton_g_std,
  616. .vidioc_s_std = skeleton_s_std,
  617. .vidioc_querystd = skeleton_querystd,
  618. .vidioc_s_dv_timings = skeleton_s_dv_timings,
  619. .vidioc_g_dv_timings = skeleton_g_dv_timings,
  620. .vidioc_enum_dv_timings = skeleton_enum_dv_timings,
  621. .vidioc_query_dv_timings = skeleton_query_dv_timings,
  622. .vidioc_dv_timings_cap = skeleton_dv_timings_cap,
  623. .vidioc_enum_input = skeleton_enum_input,
  624. .vidioc_g_input = skeleton_g_input,
  625. .vidioc_s_input = skeleton_s_input,
  626. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  627. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  628. .vidioc_querybuf = vb2_ioctl_querybuf,
  629. .vidioc_qbuf = vb2_ioctl_qbuf,
  630. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  631. .vidioc_expbuf = vb2_ioctl_expbuf,
  632. .vidioc_streamon = vb2_ioctl_streamon,
  633. .vidioc_streamoff = vb2_ioctl_streamoff,
  634. .vidioc_log_status = v4l2_ctrl_log_status,
  635. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  636. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  637. };
  638. /*
  639. * The set of file operations. Note that all these ops are standard core
  640. * helper functions.
  641. */
  642. static const struct v4l2_file_operations skel_fops = {
  643. .owner = THIS_MODULE,
  644. .open = v4l2_fh_open,
  645. .release = vb2_fop_release,
  646. .unlocked_ioctl = video_ioctl2,
  647. .read = vb2_fop_read,
  648. .mmap = vb2_fop_mmap,
  649. .poll = vb2_fop_poll,
  650. };
  651. /*
  652. * The initial setup of this device instance. Note that the initial state of
  653. * the driver should be complete. So the initial format, standard, timings
  654. * and video input should all be initialized to some reasonable value.
  655. */
  656. static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  657. {
  658. /* The initial timings are chosen to be 720p60. */
  659. static const struct v4l2_dv_timings timings_def =
  660. V4L2_DV_BT_CEA_1280X720P60;
  661. struct skeleton *skel;
  662. struct video_device *vdev;
  663. struct v4l2_ctrl_handler *hdl;
  664. struct vb2_queue *q;
  665. int ret;
  666. /* Enable PCI */
  667. ret = pci_enable_device(pdev);
  668. if (ret)
  669. return ret;
  670. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  671. if (ret) {
  672. dev_err(&pdev->dev, "no suitable DMA available.\n");
  673. goto disable_pci;
  674. }
  675. /* Allocate a new instance */
  676. skel = devm_kzalloc(&pdev->dev, sizeof(struct skeleton), GFP_KERNEL);
  677. if (!skel)
  678. return -ENOMEM;
  679. /* Allocate the interrupt */
  680. ret = devm_request_irq(&pdev->dev, pdev->irq,
  681. skeleton_irq, 0, KBUILD_MODNAME, skel);
  682. if (ret) {
  683. dev_err(&pdev->dev, "request_irq failed\n");
  684. goto disable_pci;
  685. }
  686. skel->pdev = pdev;
  687. /* Fill in the initial format-related settings */
  688. skel->timings = timings_def;
  689. skel->std = V4L2_STD_625_50;
  690. skeleton_fill_pix_format(skel, &skel->format);
  691. /* Initialize the top-level structure */
  692. ret = v4l2_device_register(&pdev->dev, &skel->v4l2_dev);
  693. if (ret)
  694. goto disable_pci;
  695. mutex_init(&skel->lock);
  696. /* Add the controls */
  697. hdl = &skel->ctrl_handler;
  698. v4l2_ctrl_handler_init(hdl, 4);
  699. v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
  700. V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
  701. v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
  702. V4L2_CID_CONTRAST, 0, 255, 1, 16);
  703. v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
  704. V4L2_CID_SATURATION, 0, 255, 1, 127);
  705. v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
  706. V4L2_CID_HUE, -128, 127, 1, 0);
  707. if (hdl->error) {
  708. ret = hdl->error;
  709. goto free_hdl;
  710. }
  711. skel->v4l2_dev.ctrl_handler = hdl;
  712. /* Initialize the vb2 queue */
  713. q = &skel->queue;
  714. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  715. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  716. q->drv_priv = skel;
  717. q->buf_struct_size = sizeof(struct skel_buffer);
  718. q->ops = &skel_qops;
  719. q->mem_ops = &vb2_dma_contig_memops;
  720. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  721. /*
  722. * Assume that this DMA engine needs to have at least two buffers
  723. * available before it can be started. The start_streaming() op
  724. * won't be called until at least this many buffers are queued up.
  725. */
  726. q->min_buffers_needed = 2;
  727. /*
  728. * The serialization lock for the streaming ioctls. This is the same
  729. * as the main serialization lock, but if some of the non-streaming
  730. * ioctls could take a long time to execute, then you might want to
  731. * have a different lock here to prevent VIDIOC_DQBUF from being
  732. * blocked while waiting for another action to finish. This is
  733. * generally not needed for PCI devices, but USB devices usually do
  734. * want a separate lock here.
  735. */
  736. q->lock = &skel->lock;
  737. /*
  738. * Since this driver can only do 32-bit DMA we must make sure that
  739. * the vb2 core will allocate the buffers in 32-bit DMA memory.
  740. */
  741. q->gfp_flags = GFP_DMA32;
  742. ret = vb2_queue_init(q);
  743. if (ret)
  744. goto free_hdl;
  745. skel->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  746. if (IS_ERR(skel->alloc_ctx)) {
  747. dev_err(&pdev->dev, "Can't allocate buffer context");
  748. ret = PTR_ERR(skel->alloc_ctx);
  749. goto free_hdl;
  750. }
  751. INIT_LIST_HEAD(&skel->buf_list);
  752. spin_lock_init(&skel->qlock);
  753. /* Initialize the video_device structure */
  754. vdev = &skel->vdev;
  755. strlcpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
  756. /*
  757. * There is nothing to clean up, so release is set to an empty release
  758. * function. The release callback must be non-NULL.
  759. */
  760. vdev->release = video_device_release_empty;
  761. vdev->fops = &skel_fops,
  762. vdev->ioctl_ops = &skel_ioctl_ops,
  763. /*
  764. * The main serialization lock. All ioctls are serialized by this
  765. * lock. Exception: if q->lock is set, then the streaming ioctls
  766. * are serialized by that separate lock.
  767. */
  768. vdev->lock = &skel->lock;
  769. vdev->queue = q;
  770. vdev->v4l2_dev = &skel->v4l2_dev;
  771. /* Supported SDTV standards, if any */
  772. vdev->tvnorms = SKEL_TVNORMS;
  773. video_set_drvdata(vdev, skel);
  774. ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
  775. if (ret)
  776. goto free_ctx;
  777. dev_info(&pdev->dev, "V4L2 PCI Skeleton Driver loaded\n");
  778. return 0;
  779. free_ctx:
  780. vb2_dma_contig_cleanup_ctx(skel->alloc_ctx);
  781. free_hdl:
  782. v4l2_ctrl_handler_free(&skel->ctrl_handler);
  783. v4l2_device_unregister(&skel->v4l2_dev);
  784. disable_pci:
  785. pci_disable_device(pdev);
  786. return ret;
  787. }
  788. static void skeleton_remove(struct pci_dev *pdev)
  789. {
  790. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  791. struct skeleton *skel = container_of(v4l2_dev, struct skeleton, v4l2_dev);
  792. video_unregister_device(&skel->vdev);
  793. v4l2_ctrl_handler_free(&skel->ctrl_handler);
  794. vb2_dma_contig_cleanup_ctx(skel->alloc_ctx);
  795. v4l2_device_unregister(&skel->v4l2_dev);
  796. pci_disable_device(skel->pdev);
  797. }
  798. static struct pci_driver skeleton_driver = {
  799. .name = KBUILD_MODNAME,
  800. .probe = skeleton_probe,
  801. .remove = skeleton_remove,
  802. .id_table = skeleton_pci_tbl,
  803. };
  804. module_pci_driver(skeleton_driver);