v4l2-pci-skeleton.c 26 KB

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