v4l2-pci-skeleton.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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. * @field: the field (TOP/BOTTOM/other) of the current buffer
  59. * @sequence: frame sequence counter
  60. */
  61. struct skeleton {
  62. struct pci_dev *pdev;
  63. struct v4l2_device v4l2_dev;
  64. struct video_device vdev;
  65. struct v4l2_ctrl_handler ctrl_handler;
  66. struct mutex lock;
  67. v4l2_std_id std;
  68. struct v4l2_dv_timings timings;
  69. struct v4l2_pix_format format;
  70. unsigned input;
  71. struct vb2_queue queue;
  72. spinlock_t qlock;
  73. struct list_head buf_list;
  74. unsigned field;
  75. unsigned sequence;
  76. };
  77. struct skel_buffer {
  78. struct vb2_v4l2_buffer vb;
  79. struct list_head list;
  80. };
  81. static inline struct skel_buffer *to_skel_buffer(struct vb2_v4l2_buffer *vbuf)
  82. {
  83. return container_of(vbuf, 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. new_buf->vb.vb2_buf.timestamp = ktime_get_ns();
  131. new_buf->vb.sequence = skel->sequence++;
  132. new_buf->vb.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, 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,
  152. unsigned int *nbuffers, unsigned int *nplanes,
  153. unsigned int sizes[], struct device *alloc_devs[])
  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 (*nplanes)
  169. return sizes[0] < skel->format.sizeimage ? -EINVAL : 0;
  170. *nplanes = 1;
  171. sizes[0] = skel->format.sizeimage;
  172. return 0;
  173. }
  174. /*
  175. * Prepare the buffer for queueing to the DMA engine: check and set the
  176. * payload size.
  177. */
  178. static int buffer_prepare(struct vb2_buffer *vb)
  179. {
  180. struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
  181. unsigned long size = skel->format.sizeimage;
  182. if (vb2_plane_size(vb, 0) < size) {
  183. dev_err(&skel->pdev->dev, "buffer too small (%lu < %lu)\n",
  184. vb2_plane_size(vb, 0), size);
  185. return -EINVAL;
  186. }
  187. vb2_set_plane_payload(vb, 0, size);
  188. return 0;
  189. }
  190. /*
  191. * Queue this buffer to the DMA engine.
  192. */
  193. static void buffer_queue(struct vb2_buffer *vb)
  194. {
  195. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  196. struct skeleton *skel = vb2_get_drv_priv(vb->vb2_queue);
  197. struct skel_buffer *buf = to_skel_buffer(vbuf);
  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.vb2_buf, 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 const 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. return 0;
  275. }
  276. /*
  277. * Helper function to check and correct struct v4l2_pix_format. It's used
  278. * not only in VIDIOC_TRY/S_FMT, but also elsewhere if changes to the SDTV
  279. * standard, HDTV timings or the video input would require updating the
  280. * current format.
  281. */
  282. static void skeleton_fill_pix_format(struct skeleton *skel,
  283. struct v4l2_pix_format *pix)
  284. {
  285. pix->pixelformat = V4L2_PIX_FMT_YUYV;
  286. if (skel->input == 0) {
  287. /* S-Video input */
  288. pix->width = 720;
  289. pix->height = (skel->std & V4L2_STD_525_60) ? 480 : 576;
  290. pix->field = V4L2_FIELD_INTERLACED;
  291. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  292. } else {
  293. /* HDMI input */
  294. pix->width = skel->timings.bt.width;
  295. pix->height = skel->timings.bt.height;
  296. if (skel->timings.bt.interlaced) {
  297. pix->field = V4L2_FIELD_ALTERNATE;
  298. pix->height /= 2;
  299. } else {
  300. pix->field = V4L2_FIELD_NONE;
  301. }
  302. pix->colorspace = V4L2_COLORSPACE_REC709;
  303. }
  304. /*
  305. * The YUYV format is four bytes for every two pixels, so bytesperline
  306. * is width * 2.
  307. */
  308. pix->bytesperline = pix->width * 2;
  309. pix->sizeimage = pix->bytesperline * pix->height;
  310. pix->priv = 0;
  311. }
  312. static int skeleton_try_fmt_vid_cap(struct file *file, void *priv,
  313. struct v4l2_format *f)
  314. {
  315. struct skeleton *skel = video_drvdata(file);
  316. struct v4l2_pix_format *pix = &f->fmt.pix;
  317. /*
  318. * Due to historical reasons providing try_fmt with an unsupported
  319. * pixelformat will return -EINVAL for video receivers. Webcam drivers,
  320. * however, will silently correct the pixelformat. Some video capture
  321. * applications rely on this behavior...
  322. */
  323. if (pix->pixelformat != V4L2_PIX_FMT_YUYV)
  324. return -EINVAL;
  325. skeleton_fill_pix_format(skel, pix);
  326. return 0;
  327. }
  328. static int skeleton_s_fmt_vid_cap(struct file *file, void *priv,
  329. struct v4l2_format *f)
  330. {
  331. struct skeleton *skel = video_drvdata(file);
  332. int ret;
  333. ret = skeleton_try_fmt_vid_cap(file, priv, f);
  334. if (ret)
  335. return ret;
  336. /*
  337. * It is not allowed to change the format while buffers for use with
  338. * streaming have already been allocated.
  339. */
  340. if (vb2_is_busy(&skel->queue))
  341. return -EBUSY;
  342. /* TODO: change format */
  343. skel->format = f->fmt.pix;
  344. return 0;
  345. }
  346. static int skeleton_g_fmt_vid_cap(struct file *file, void *priv,
  347. struct v4l2_format *f)
  348. {
  349. struct skeleton *skel = video_drvdata(file);
  350. f->fmt.pix = skel->format;
  351. return 0;
  352. }
  353. static int skeleton_enum_fmt_vid_cap(struct file *file, void *priv,
  354. struct v4l2_fmtdesc *f)
  355. {
  356. if (f->index != 0)
  357. return -EINVAL;
  358. f->pixelformat = V4L2_PIX_FMT_YUYV;
  359. return 0;
  360. }
  361. static int skeleton_s_std(struct file *file, void *priv, v4l2_std_id std)
  362. {
  363. struct skeleton *skel = video_drvdata(file);
  364. /* S_STD is not supported on the HDMI input */
  365. if (skel->input)
  366. return -ENODATA;
  367. /*
  368. * No change, so just return. Some applications call S_STD again after
  369. * the buffers for streaming have been set up, so we have to allow for
  370. * this behavior.
  371. */
  372. if (std == skel->std)
  373. return 0;
  374. /*
  375. * Changing the standard implies a format change, which is not allowed
  376. * while buffers for use with streaming have already been allocated.
  377. */
  378. if (vb2_is_busy(&skel->queue))
  379. return -EBUSY;
  380. /* TODO: handle changing std */
  381. skel->std = std;
  382. /* Update the internal format */
  383. skeleton_fill_pix_format(skel, &skel->format);
  384. return 0;
  385. }
  386. static int skeleton_g_std(struct file *file, void *priv, v4l2_std_id *std)
  387. {
  388. struct skeleton *skel = video_drvdata(file);
  389. /* G_STD is not supported on the HDMI input */
  390. if (skel->input)
  391. return -ENODATA;
  392. *std = skel->std;
  393. return 0;
  394. }
  395. /*
  396. * Query the current standard as seen by the hardware. This function shall
  397. * never actually change the standard, it just detects and reports.
  398. * The framework will initially set *std to tvnorms (i.e. the set of
  399. * supported standards by this input), and this function should just AND
  400. * this value. If there is no signal, then *std should be set to 0.
  401. */
  402. static int skeleton_querystd(struct file *file, void *priv, v4l2_std_id *std)
  403. {
  404. struct skeleton *skel = video_drvdata(file);
  405. /* QUERY_STD is not supported on the HDMI input */
  406. if (skel->input)
  407. return -ENODATA;
  408. #ifdef TODO
  409. /*
  410. * Query currently seen standard. Initial value of *std is
  411. * V4L2_STD_ALL. This function should look something like this:
  412. */
  413. get_signal_info();
  414. if (no_signal) {
  415. *std = 0;
  416. return 0;
  417. }
  418. /* Use signal information to reduce the number of possible standards */
  419. if (signal_has_525_lines)
  420. *std &= V4L2_STD_525_60;
  421. else
  422. *std &= V4L2_STD_625_50;
  423. #endif
  424. return 0;
  425. }
  426. static int skeleton_s_dv_timings(struct file *file, void *_fh,
  427. struct v4l2_dv_timings *timings)
  428. {
  429. struct skeleton *skel = video_drvdata(file);
  430. /* S_DV_TIMINGS is not supported on the S-Video input */
  431. if (skel->input == 0)
  432. return -ENODATA;
  433. /* Quick sanity check */
  434. if (!v4l2_valid_dv_timings(timings, &skel_timings_cap, NULL, NULL))
  435. return -EINVAL;
  436. /* Check if the timings are part of the CEA-861 timings. */
  437. if (!v4l2_find_dv_timings_cap(timings, &skel_timings_cap,
  438. 0, NULL, NULL))
  439. return -EINVAL;
  440. /* Return 0 if the new timings are the same as the current timings. */
  441. if (v4l2_match_dv_timings(timings, &skel->timings, 0, false))
  442. return 0;
  443. /*
  444. * Changing the timings implies a format change, which is not allowed
  445. * while buffers for use with streaming have already been allocated.
  446. */
  447. if (vb2_is_busy(&skel->queue))
  448. return -EBUSY;
  449. /* TODO: Configure new timings */
  450. /* Save timings */
  451. skel->timings = *timings;
  452. /* Update the internal format */
  453. skeleton_fill_pix_format(skel, &skel->format);
  454. return 0;
  455. }
  456. static int skeleton_g_dv_timings(struct file *file, void *_fh,
  457. struct v4l2_dv_timings *timings)
  458. {
  459. struct skeleton *skel = video_drvdata(file);
  460. /* G_DV_TIMINGS is not supported on the S-Video input */
  461. if (skel->input == 0)
  462. return -ENODATA;
  463. *timings = skel->timings;
  464. return 0;
  465. }
  466. static int skeleton_enum_dv_timings(struct file *file, void *_fh,
  467. struct v4l2_enum_dv_timings *timings)
  468. {
  469. struct skeleton *skel = video_drvdata(file);
  470. /* ENUM_DV_TIMINGS is not supported on the S-Video input */
  471. if (skel->input == 0)
  472. return -ENODATA;
  473. return v4l2_enum_dv_timings_cap(timings, &skel_timings_cap,
  474. NULL, NULL);
  475. }
  476. /*
  477. * Query the current timings as seen by the hardware. This function shall
  478. * never actually change the timings, it just detects and reports.
  479. * If no signal is detected, then return -ENOLINK. If the hardware cannot
  480. * lock to the signal, then return -ENOLCK. If the signal is out of range
  481. * of the capabilities of the system (e.g., it is possible that the receiver
  482. * can lock but that the DMA engine it is connected to cannot handle
  483. * pixelclocks above a certain frequency), then -ERANGE is returned.
  484. */
  485. static int skeleton_query_dv_timings(struct file *file, void *_fh,
  486. struct v4l2_dv_timings *timings)
  487. {
  488. struct skeleton *skel = video_drvdata(file);
  489. /* QUERY_DV_TIMINGS is not supported on the S-Video input */
  490. if (skel->input == 0)
  491. return -ENODATA;
  492. #ifdef TODO
  493. /*
  494. * Query currently seen timings. This function should look
  495. * something like this:
  496. */
  497. detect_timings();
  498. if (no_signal)
  499. return -ENOLINK;
  500. if (cannot_lock_to_signal)
  501. return -ENOLCK;
  502. if (signal_out_of_range_of_capabilities)
  503. return -ERANGE;
  504. /* Useful for debugging */
  505. v4l2_print_dv_timings(skel->v4l2_dev.name, "query_dv_timings:",
  506. timings, true);
  507. #endif
  508. return 0;
  509. }
  510. static int skeleton_dv_timings_cap(struct file *file, void *fh,
  511. struct v4l2_dv_timings_cap *cap)
  512. {
  513. struct skeleton *skel = video_drvdata(file);
  514. /* DV_TIMINGS_CAP is not supported on the S-Video input */
  515. if (skel->input == 0)
  516. return -ENODATA;
  517. *cap = skel_timings_cap;
  518. return 0;
  519. }
  520. static int skeleton_enum_input(struct file *file, void *priv,
  521. struct v4l2_input *i)
  522. {
  523. if (i->index > 1)
  524. return -EINVAL;
  525. i->type = V4L2_INPUT_TYPE_CAMERA;
  526. if (i->index == 0) {
  527. i->std = SKEL_TVNORMS;
  528. strlcpy(i->name, "S-Video", sizeof(i->name));
  529. i->capabilities = V4L2_IN_CAP_STD;
  530. } else {
  531. i->std = 0;
  532. strlcpy(i->name, "HDMI", sizeof(i->name));
  533. i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
  534. }
  535. return 0;
  536. }
  537. static int skeleton_s_input(struct file *file, void *priv, unsigned int i)
  538. {
  539. struct skeleton *skel = video_drvdata(file);
  540. if (i > 1)
  541. return -EINVAL;
  542. /*
  543. * Changing the input implies a format change, which is not allowed
  544. * while buffers for use with streaming have already been allocated.
  545. */
  546. if (vb2_is_busy(&skel->queue))
  547. return -EBUSY;
  548. skel->input = i;
  549. /*
  550. * Update tvnorms. The tvnorms value is used by the core to implement
  551. * VIDIOC_ENUMSTD so it has to be correct. If tvnorms == 0, then
  552. * ENUMSTD will return -ENODATA.
  553. */
  554. skel->vdev.tvnorms = i ? 0 : SKEL_TVNORMS;
  555. /* Update the internal format */
  556. skeleton_fill_pix_format(skel, &skel->format);
  557. return 0;
  558. }
  559. static int skeleton_g_input(struct file *file, void *priv, unsigned int *i)
  560. {
  561. struct skeleton *skel = video_drvdata(file);
  562. *i = skel->input;
  563. return 0;
  564. }
  565. /* The control handler. */
  566. static int skeleton_s_ctrl(struct v4l2_ctrl *ctrl)
  567. {
  568. /*struct skeleton *skel =
  569. container_of(ctrl->handler, struct skeleton, ctrl_handler);*/
  570. switch (ctrl->id) {
  571. case V4L2_CID_BRIGHTNESS:
  572. /* TODO: set brightness to ctrl->val */
  573. break;
  574. case V4L2_CID_CONTRAST:
  575. /* TODO: set contrast to ctrl->val */
  576. break;
  577. case V4L2_CID_SATURATION:
  578. /* TODO: set saturation to ctrl->val */
  579. break;
  580. case V4L2_CID_HUE:
  581. /* TODO: set hue to ctrl->val */
  582. break;
  583. default:
  584. return -EINVAL;
  585. }
  586. return 0;
  587. }
  588. /* ------------------------------------------------------------------
  589. File operations for the device
  590. ------------------------------------------------------------------*/
  591. static const struct v4l2_ctrl_ops skel_ctrl_ops = {
  592. .s_ctrl = skeleton_s_ctrl,
  593. };
  594. /*
  595. * The set of all supported ioctls. Note that all the streaming ioctls
  596. * use the vb2 helper functions that take care of all the locking and
  597. * that also do ownership tracking (i.e. only the filehandle that requested
  598. * the buffers can call the streaming ioctls, all other filehandles will
  599. * receive -EBUSY if they attempt to call the same streaming ioctls).
  600. *
  601. * The last three ioctls also use standard helper functions: these implement
  602. * standard behavior for drivers with controls.
  603. */
  604. static const struct v4l2_ioctl_ops skel_ioctl_ops = {
  605. .vidioc_querycap = skeleton_querycap,
  606. .vidioc_try_fmt_vid_cap = skeleton_try_fmt_vid_cap,
  607. .vidioc_s_fmt_vid_cap = skeleton_s_fmt_vid_cap,
  608. .vidioc_g_fmt_vid_cap = skeleton_g_fmt_vid_cap,
  609. .vidioc_enum_fmt_vid_cap = skeleton_enum_fmt_vid_cap,
  610. .vidioc_g_std = skeleton_g_std,
  611. .vidioc_s_std = skeleton_s_std,
  612. .vidioc_querystd = skeleton_querystd,
  613. .vidioc_s_dv_timings = skeleton_s_dv_timings,
  614. .vidioc_g_dv_timings = skeleton_g_dv_timings,
  615. .vidioc_enum_dv_timings = skeleton_enum_dv_timings,
  616. .vidioc_query_dv_timings = skeleton_query_dv_timings,
  617. .vidioc_dv_timings_cap = skeleton_dv_timings_cap,
  618. .vidioc_enum_input = skeleton_enum_input,
  619. .vidioc_g_input = skeleton_g_input,
  620. .vidioc_s_input = skeleton_s_input,
  621. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  622. .vidioc_create_bufs = vb2_ioctl_create_bufs,
  623. .vidioc_querybuf = vb2_ioctl_querybuf,
  624. .vidioc_qbuf = vb2_ioctl_qbuf,
  625. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  626. .vidioc_expbuf = vb2_ioctl_expbuf,
  627. .vidioc_streamon = vb2_ioctl_streamon,
  628. .vidioc_streamoff = vb2_ioctl_streamoff,
  629. .vidioc_log_status = v4l2_ctrl_log_status,
  630. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  631. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  632. };
  633. /*
  634. * The set of file operations. Note that all these ops are standard core
  635. * helper functions.
  636. */
  637. static const struct v4l2_file_operations skel_fops = {
  638. .owner = THIS_MODULE,
  639. .open = v4l2_fh_open,
  640. .release = vb2_fop_release,
  641. .unlocked_ioctl = video_ioctl2,
  642. .read = vb2_fop_read,
  643. .mmap = vb2_fop_mmap,
  644. .poll = vb2_fop_poll,
  645. };
  646. /*
  647. * The initial setup of this device instance. Note that the initial state of
  648. * the driver should be complete. So the initial format, standard, timings
  649. * and video input should all be initialized to some reasonable value.
  650. */
  651. static int skeleton_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  652. {
  653. /* The initial timings are chosen to be 720p60. */
  654. static const struct v4l2_dv_timings timings_def =
  655. V4L2_DV_BT_CEA_1280X720P60;
  656. struct skeleton *skel;
  657. struct video_device *vdev;
  658. struct v4l2_ctrl_handler *hdl;
  659. struct vb2_queue *q;
  660. int ret;
  661. /* Enable PCI */
  662. ret = pci_enable_device(pdev);
  663. if (ret)
  664. return ret;
  665. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  666. if (ret) {
  667. dev_err(&pdev->dev, "no suitable DMA available.\n");
  668. goto disable_pci;
  669. }
  670. /* Allocate a new instance */
  671. skel = devm_kzalloc(&pdev->dev, sizeof(struct skeleton), GFP_KERNEL);
  672. if (!skel) {
  673. ret = -ENOMEM;
  674. goto disable_pci;
  675. }
  676. /* Allocate the interrupt */
  677. ret = devm_request_irq(&pdev->dev, pdev->irq,
  678. skeleton_irq, 0, KBUILD_MODNAME, skel);
  679. if (ret) {
  680. dev_err(&pdev->dev, "request_irq failed\n");
  681. goto disable_pci;
  682. }
  683. skel->pdev = pdev;
  684. /* Fill in the initial format-related settings */
  685. skel->timings = timings_def;
  686. skel->std = V4L2_STD_625_50;
  687. skeleton_fill_pix_format(skel, &skel->format);
  688. /* Initialize the top-level structure */
  689. ret = v4l2_device_register(&pdev->dev, &skel->v4l2_dev);
  690. if (ret)
  691. goto disable_pci;
  692. mutex_init(&skel->lock);
  693. /* Add the controls */
  694. hdl = &skel->ctrl_handler;
  695. v4l2_ctrl_handler_init(hdl, 4);
  696. v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
  697. V4L2_CID_BRIGHTNESS, 0, 255, 1, 127);
  698. v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
  699. V4L2_CID_CONTRAST, 0, 255, 1, 16);
  700. v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
  701. V4L2_CID_SATURATION, 0, 255, 1, 127);
  702. v4l2_ctrl_new_std(hdl, &skel_ctrl_ops,
  703. V4L2_CID_HUE, -128, 127, 1, 0);
  704. if (hdl->error) {
  705. ret = hdl->error;
  706. goto free_hdl;
  707. }
  708. skel->v4l2_dev.ctrl_handler = hdl;
  709. /* Initialize the vb2 queue */
  710. q = &skel->queue;
  711. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  712. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  713. q->dev = &pdev->dev;
  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. INIT_LIST_HEAD(&skel->buf_list);
  744. spin_lock_init(&skel->qlock);
  745. /* Initialize the video_device structure */
  746. vdev = &skel->vdev;
  747. strlcpy(vdev->name, KBUILD_MODNAME, sizeof(vdev->name));
  748. /*
  749. * There is nothing to clean up, so release is set to an empty release
  750. * function. The release callback must be non-NULL.
  751. */
  752. vdev->release = video_device_release_empty;
  753. vdev->fops = &skel_fops,
  754. vdev->ioctl_ops = &skel_ioctl_ops,
  755. vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
  756. V4L2_CAP_STREAMING;
  757. /*
  758. * The main serialization lock. All ioctls are serialized by this
  759. * lock. Exception: if q->lock is set, then the streaming ioctls
  760. * are serialized by that separate lock.
  761. */
  762. vdev->lock = &skel->lock;
  763. vdev->queue = q;
  764. vdev->v4l2_dev = &skel->v4l2_dev;
  765. /* Supported SDTV standards, if any */
  766. vdev->tvnorms = SKEL_TVNORMS;
  767. video_set_drvdata(vdev, skel);
  768. ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
  769. if (ret)
  770. goto free_hdl;
  771. dev_info(&pdev->dev, "V4L2 PCI Skeleton Driver loaded\n");
  772. return 0;
  773. free_hdl:
  774. v4l2_ctrl_handler_free(&skel->ctrl_handler);
  775. v4l2_device_unregister(&skel->v4l2_dev);
  776. disable_pci:
  777. pci_disable_device(pdev);
  778. return ret;
  779. }
  780. static void skeleton_remove(struct pci_dev *pdev)
  781. {
  782. struct v4l2_device *v4l2_dev = pci_get_drvdata(pdev);
  783. struct skeleton *skel = container_of(v4l2_dev, struct skeleton, v4l2_dev);
  784. video_unregister_device(&skel->vdev);
  785. v4l2_ctrl_handler_free(&skel->ctrl_handler);
  786. v4l2_device_unregister(&skel->v4l2_dev);
  787. pci_disable_device(skel->pdev);
  788. }
  789. static struct pci_driver skeleton_driver = {
  790. .name = KBUILD_MODNAME,
  791. .probe = skeleton_probe,
  792. .remove = skeleton_remove,
  793. .id_table = skeleton_pci_tbl,
  794. };
  795. module_pci_driver(skeleton_driver);