v4l2-pci-skeleton.c 25 KB

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