v4l2-pci-skeleton.c 25 KB

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