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