v4l2-pci-skeleton.c 25 KB

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