videobook.tmpl 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
  4. <book id="V4LGuide">
  5. <bookinfo>
  6. <title>Video4Linux Programming</title>
  7. <authorgroup>
  8. <author>
  9. <firstname>Alan</firstname>
  10. <surname>Cox</surname>
  11. <affiliation>
  12. <address>
  13. <email>alan@redhat.com</email>
  14. </address>
  15. </affiliation>
  16. </author>
  17. </authorgroup>
  18. <copyright>
  19. <year>2000</year>
  20. <holder>Alan Cox</holder>
  21. </copyright>
  22. <legalnotice>
  23. <para>
  24. This documentation is free software; you can redistribute
  25. it and/or modify it under the terms of the GNU General Public
  26. License as published by the Free Software Foundation; either
  27. version 2 of the License, or (at your option) any later
  28. version.
  29. </para>
  30. <para>
  31. This program is distributed in the hope that it will be
  32. useful, but WITHOUT ANY WARRANTY; without even the implied
  33. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  34. See the GNU General Public License for more details.
  35. </para>
  36. <para>
  37. You should have received a copy of the GNU General Public
  38. License along with this program; if not, write to the Free
  39. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  40. MA 02111-1307 USA
  41. </para>
  42. <para>
  43. For more details see the file COPYING in the source
  44. distribution of Linux.
  45. </para>
  46. </legalnotice>
  47. </bookinfo>
  48. <toc></toc>
  49. <chapter id="intro">
  50. <title>Introduction</title>
  51. <para>
  52. Parts of this document first appeared in Linux Magazine under a
  53. ninety day exclusivity.
  54. </para>
  55. <para>
  56. Video4Linux is intended to provide a common programming interface
  57. for the many TV and capture cards now on the market, as well as
  58. parallel port and USB video cameras. Radio, teletext decoders and
  59. vertical blanking data interfaces are also provided.
  60. </para>
  61. </chapter>
  62. <chapter id="radio">
  63. <title>Radio Devices</title>
  64. <para>
  65. There are a wide variety of radio interfaces available for PC's, and these
  66. are generally very simple to program. The biggest problem with supporting
  67. such devices is normally extracting documentation from the vendor.
  68. </para>
  69. <para>
  70. The radio interface supports a simple set of control ioctls standardised
  71. across all radio and tv interfaces. It does not support read or write, which
  72. are used for video streams. The reason radio cards do not allow you to read
  73. the audio stream into an application is that without exception they provide
  74. a connection on to a soundcard. Soundcards can be used to read the radio
  75. data just fine.
  76. </para>
  77. <sect1 id="registerradio">
  78. <title>Registering Radio Devices</title>
  79. <para>
  80. The Video4linux core provides an interface for registering devices. The
  81. first step in writing our radio card driver is to register it.
  82. </para>
  83. <programlisting>
  84. static struct video_device my_radio
  85. {
  86. "My radio",
  87. VID_TYPE_TUNER,
  88. radio_open.
  89. radio_close,
  90. NULL, /* no read */
  91. NULL, /* no write */
  92. NULL, /* no poll */
  93. radio_ioctl,
  94. NULL, /* no special init function */
  95. NULL /* no private data */
  96. };
  97. </programlisting>
  98. <para>
  99. This declares our video4linux device driver interface. The VID_TYPE_ value
  100. defines what kind of an interface we are, and defines basic capabilities.
  101. </para>
  102. <para>
  103. The only defined value relevant for a radio card is VID_TYPE_TUNER which
  104. indicates that the device can be tuned. Clearly our radio is going to have some
  105. way to change channel so it is tuneable.
  106. </para>
  107. <para>
  108. We declare an open and close routine, but we do not need read or write,
  109. which are used to read and write video data to or from the card itself. As
  110. we have no read or write there is no poll function.
  111. </para>
  112. <para>
  113. The private initialise function is run when the device is registered. In
  114. this driver we've already done all the work needed. The final pointer is a
  115. private data pointer that can be used by the device driver to attach and
  116. retrieve private data structures. We set this field "priv" to NULL for
  117. the moment.
  118. </para>
  119. <para>
  120. Having the structure defined is all very well but we now need to register it
  121. with the kernel.
  122. </para>
  123. <programlisting>
  124. static int io = 0x320;
  125. int __init myradio_init(struct video_init *v)
  126. {
  127. if(!request_region(io, MY_IO_SIZE, "myradio"))
  128. {
  129. printk(KERN_ERR
  130. "myradio: port 0x%03X is in use.\n", io);
  131. return -EBUSY;
  132. }
  133. if(video_device_register(&amp;my_radio, VFL_TYPE_RADIO)==-1) {
  134. release_region(io, MY_IO_SIZE);
  135. return -EINVAL;
  136. }
  137. return 0;
  138. }
  139. </programlisting>
  140. <para>
  141. The first stage of the initialisation, as is normally the case, is to check
  142. that the I/O space we are about to fiddle with doesn't belong to some other
  143. driver. If it is we leave well alone. If the user gives the address of the
  144. wrong device then we will spot this. These policies will generally avoid
  145. crashing the machine.
  146. </para>
  147. <para>
  148. Now we ask the Video4Linux layer to register the device for us. We hand it
  149. our carefully designed video_device structure and also tell it which group
  150. of devices we want it registered with. In this case VFL_TYPE_RADIO.
  151. </para>
  152. <para>
  153. The types available are
  154. </para>
  155. <table frame="all" id="Device_Types"><title>Device Types</title>
  156. <tgroup cols="3" align="left">
  157. <tbody>
  158. <row>
  159. <entry>VFL_TYPE_RADIO</entry><entry>/dev/radio{n}</entry><entry>
  160. Radio devices are assigned in this block. As with all of these
  161. selections the actual number assignment is done by the video layer
  162. accordijng to what is free.</entry>
  163. </row><row>
  164. <entry>VFL_TYPE_GRABBER</entry><entry>/dev/video{n}</entry><entry>
  165. Video capture devices and also -- counter-intuitively for the name --
  166. hardware video playback devices such as MPEG2 cards.</entry>
  167. </row><row>
  168. <entry>VFL_TYPE_VBI</entry><entry>/dev/vbi{n}</entry><entry>
  169. The VBI devices capture the hidden lines on a television picture
  170. that carry further information like closed caption data, teletext
  171. (primarily in Europe) and now Intercast and the ATVEC internet
  172. television encodings.</entry>
  173. </row><row>
  174. <entry>VFL_TYPE_VTX</entry><entry>/dev/vtx[n}</entry><entry>
  175. VTX is 'Videotext' also known as 'Teletext'. This is a system for
  176. sending numbered, 40x25, mostly textual page images over the hidden
  177. lines. Unlike the /dev/vbi interfaces, this is for 'smart' decoder
  178. chips. (The use of the word smart here has to be taken in context,
  179. the smartest teletext chips are fairly dumb pieces of technology).
  180. </entry>
  181. </row>
  182. </tbody>
  183. </tgroup>
  184. </table>
  185. <para>
  186. We are most definitely a radio.
  187. </para>
  188. <para>
  189. Finally we allocate our I/O space so that nobody treads on us and return 0
  190. to signify general happiness with the state of the universe.
  191. </para>
  192. </sect1>
  193. <sect1 id="openradio">
  194. <title>Opening And Closing The Radio</title>
  195. <para>
  196. The functions we declared in our video_device are mostly very simple.
  197. Firstly we can drop in what is basically standard code for open and close.
  198. </para>
  199. <programlisting>
  200. static int users = 0;
  201. static int radio_open(struct video_device *dev, int flags)
  202. {
  203. if(users)
  204. return -EBUSY;
  205. users++;
  206. return 0;
  207. }
  208. </programlisting>
  209. <para>
  210. At open time we need to do nothing but check if someone else is also using
  211. the radio card. If nobody is using it we make a note that we are using it,
  212. then we ensure that nobody unloads our driver on us.
  213. </para>
  214. <programlisting>
  215. static int radio_close(struct video_device *dev)
  216. {
  217. users--;
  218. }
  219. </programlisting>
  220. <para>
  221. At close time we simply need to reduce the user count and allow the module
  222. to become unloadable.
  223. </para>
  224. <para>
  225. If you are sharp you will have noticed neither the open nor the close
  226. routines attempt to reset or change the radio settings. This is intentional.
  227. It allows an application to set up the radio and exit. It avoids a user
  228. having to leave an application running all the time just to listen to the
  229. radio.
  230. </para>
  231. </sect1>
  232. <sect1 id="ioctlradio">
  233. <title>The Ioctl Interface</title>
  234. <para>
  235. This leaves the ioctl routine, without which the driver will not be
  236. terribly useful to anyone.
  237. </para>
  238. <programlisting>
  239. static int radio_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
  240. {
  241. switch(cmd)
  242. {
  243. case VIDIOCGCAP:
  244. {
  245. struct video_capability v;
  246. v.type = VID_TYPE_TUNER;
  247. v.channels = 1;
  248. v.audios = 1;
  249. v.maxwidth = 0;
  250. v.minwidth = 0;
  251. v.maxheight = 0;
  252. v.minheight = 0;
  253. strcpy(v.name, "My Radio");
  254. if(copy_to_user(arg, &amp;v, sizeof(v)))
  255. return -EFAULT;
  256. return 0;
  257. }
  258. </programlisting>
  259. <para>
  260. VIDIOCGCAP is the first ioctl all video4linux devices must support. It
  261. allows the applications to find out what sort of a card they have found and
  262. to figure out what they want to do about it. The fields in the structure are
  263. </para>
  264. <table frame="all" id="video_capability_fields"><title>struct video_capability fields</title>
  265. <tgroup cols="2" align="left">
  266. <tbody>
  267. <row>
  268. <entry>name</entry><entry>The device text name. This is intended for the user.</entry>
  269. </row><row>
  270. <entry>channels</entry><entry>The number of different channels you can tune on
  271. this card. It could even by zero for a card that has
  272. no tuning capability. For our simple FM radio it is 1.
  273. An AM/FM radio would report 2.</entry>
  274. </row><row>
  275. <entry>audios</entry><entry>The number of audio inputs on this device. For our
  276. radio there is only one audio input.</entry>
  277. </row><row>
  278. <entry>minwidth,minheight</entry><entry>The smallest size the card is capable of capturing
  279. images in. We set these to zero. Radios do not
  280. capture pictures</entry>
  281. </row><row>
  282. <entry>maxwidth,maxheight</entry><entry>The largest image size the card is capable of
  283. capturing. For our radio we report 0.
  284. </entry>
  285. </row><row>
  286. <entry>type</entry><entry>This reports the capabilities of the device, and
  287. matches the field we filled in in the struct
  288. video_device when registering.</entry>
  289. </row>
  290. </tbody>
  291. </tgroup>
  292. </table>
  293. <para>
  294. Having filled in the fields, we use copy_to_user to copy the structure into
  295. the users buffer. If the copy fails we return an EFAULT to the application
  296. so that it knows it tried to feed us garbage.
  297. </para>
  298. <para>
  299. The next pair of ioctl operations select which tuner is to be used and let
  300. the application find the tuner properties. We have only a single FM band
  301. tuner in our example device.
  302. </para>
  303. <programlisting>
  304. case VIDIOCGTUNER:
  305. {
  306. struct video_tuner v;
  307. if(copy_from_user(&amp;v, arg, sizeof(v))!=0)
  308. return -EFAULT;
  309. if(v.tuner)
  310. return -EINVAL;
  311. v.rangelow=(87*16000);
  312. v.rangehigh=(108*16000);
  313. v.flags = VIDEO_TUNER_LOW;
  314. v.mode = VIDEO_MODE_AUTO;
  315. v.signal = 0xFFFF;
  316. strcpy(v.name, "FM");
  317. if(copy_to_user(&amp;v, arg, sizeof(v))!=0)
  318. return -EFAULT;
  319. return 0;
  320. }
  321. </programlisting>
  322. <para>
  323. The VIDIOCGTUNER ioctl allows applications to query a tuner. The application
  324. sets the tuner field to the tuner number it wishes to query. The query does
  325. not change the tuner that is being used, it merely enquires about the tuner
  326. in question.
  327. </para>
  328. <para>
  329. We have exactly one tuner so after copying the user buffer to our temporary
  330. structure we complain if they asked for a tuner other than tuner 0.
  331. </para>
  332. <para>
  333. The video_tuner structure has the following fields
  334. </para>
  335. <table frame="all" id="video_tuner_fields"><title>struct video_tuner fields</title>
  336. <tgroup cols="2" align="left">
  337. <tbody>
  338. <row>
  339. <entry>int tuner</entry><entry>The number of the tuner in question</entry>
  340. </row><row>
  341. <entry>char name[32]</entry><entry>A text description of this tuner. "FM" will do fine.
  342. This is intended for the application.</entry>
  343. </row><row>
  344. <entry>u32 flags</entry>
  345. <entry>Tuner capability flags</entry>
  346. </row>
  347. <row>
  348. <entry>u16 mode</entry><entry>The current reception mode</entry>
  349. </row><row>
  350. <entry>u16 signal</entry><entry>The signal strength scaled between 0 and 65535. If
  351. a device cannot tell the signal strength it should
  352. report 65535. Many simple cards contain only a
  353. signal/no signal bit. Such cards will report either
  354. 0 or 65535.</entry>
  355. </row><row>
  356. <entry>u32 rangelow, rangehigh</entry><entry>
  357. The range of frequencies supported by the radio
  358. or TV. It is scaled according to the VIDEO_TUNER_LOW
  359. flag.</entry>
  360. </row>
  361. </tbody>
  362. </tgroup>
  363. </table>
  364. <table frame="all" id="video_tuner_flags"><title>struct video_tuner flags</title>
  365. <tgroup cols="2" align="left">
  366. <tbody>
  367. <row>
  368. <entry>VIDEO_TUNER_PAL</entry><entry>A PAL TV tuner</entry>
  369. </row><row>
  370. <entry>VIDEO_TUNER_NTSC</entry><entry>An NTSC (US) TV tuner</entry>
  371. </row><row>
  372. <entry>VIDEO_TUNER_SECAM</entry><entry>A SECAM (French) TV tuner</entry>
  373. </row><row>
  374. <entry>VIDEO_TUNER_LOW</entry><entry>
  375. The tuner frequency is scaled in 1/16th of a KHz
  376. steps. If not it is in 1/16th of a MHz steps
  377. </entry>
  378. </row><row>
  379. <entry>VIDEO_TUNER_NORM</entry><entry>The tuner can set its format</entry>
  380. </row><row>
  381. <entry>VIDEO_TUNER_STEREO_ON</entry><entry>The tuner is currently receiving a stereo signal</entry>
  382. </row>
  383. </tbody>
  384. </tgroup>
  385. </table>
  386. <table frame="all" id="video_tuner_modes"><title>struct video_tuner modes</title>
  387. <tgroup cols="2" align="left">
  388. <tbody>
  389. <row>
  390. <entry>VIDEO_MODE_PAL</entry><entry>PAL Format</entry>
  391. </row><row>
  392. <entry>VIDEO_MODE_NTSC</entry><entry>NTSC Format (USA)</entry>
  393. </row><row>
  394. <entry>VIDEO_MODE_SECAM</entry><entry>French Format</entry>
  395. </row><row>
  396. <entry>VIDEO_MODE_AUTO</entry><entry>A device that does not need to do
  397. TV format switching</entry>
  398. </row>
  399. </tbody>
  400. </tgroup>
  401. </table>
  402. <para>
  403. The settings for the radio card are thus fairly simple. We report that we
  404. are a tuner called "FM" for FM radio. In order to get the best tuning
  405. resolution we report VIDEO_TUNER_LOW and select tuning to 1/16th of KHz. Its
  406. unlikely our card can do that resolution but it is a fair bet the card can
  407. do better than 1/16th of a MHz. VIDEO_TUNER_LOW is appropriate to almost all
  408. radio usage.
  409. </para>
  410. <para>
  411. We report that the tuner automatically handles deciding what format it is
  412. receiving - true enough as it only handles FM radio. Our example card is
  413. also incapable of detecting stereo or signal strengths so it reports a
  414. strength of 0xFFFF (maximum) and no stereo detected.
  415. </para>
  416. <para>
  417. To finish off we set the range that can be tuned to be 87-108Mhz, the normal
  418. FM broadcast radio range. It is important to find out what the card is
  419. actually capable of tuning. It is easy enough to simply use the FM broadcast
  420. range. Unfortunately if you do this you will discover the FM broadcast
  421. ranges in the USA, Europe and Japan are all subtly different and some users
  422. cannot receive all the stations they wish.
  423. </para>
  424. <para>
  425. The application also needs to be able to set the tuner it wishes to use. In
  426. our case, with a single tuner this is rather simple to arrange.
  427. </para>
  428. <programlisting>
  429. case VIDIOCSTUNER:
  430. {
  431. struct video_tuner v;
  432. if(copy_from_user(&amp;v, arg, sizeof(v)))
  433. return -EFAULT;
  434. if(v.tuner != 0)
  435. return -EINVAL;
  436. return 0;
  437. }
  438. </programlisting>
  439. <para>
  440. We copy the user supplied structure into kernel memory so we can examine it.
  441. If the user has selected a tuner other than zero we reject the request. If
  442. they wanted tuner 0 then, surprisingly enough, that is the current tuner already.
  443. </para>
  444. <para>
  445. The next two ioctls we need to provide are to get and set the frequency of
  446. the radio. These both use an unsigned long argument which is the frequency.
  447. The scale of the frequency depends on the VIDEO_TUNER_LOW flag as I
  448. mentioned earlier on. Since we have VIDEO_TUNER_LOW set this will be in
  449. 1/16ths of a KHz.
  450. </para>
  451. <programlisting>
  452. static unsigned long current_freq;
  453. case VIDIOCGFREQ:
  454. if(copy_to_user(arg, &amp;current_freq,
  455. sizeof(unsigned long))
  456. return -EFAULT;
  457. return 0;
  458. </programlisting>
  459. <para>
  460. Querying the frequency in our case is relatively simple. Our radio card is
  461. too dumb to let us query the signal strength so we remember our setting if
  462. we know it. All we have to do is copy it to the user.
  463. </para>
  464. <programlisting>
  465. case VIDIOCSFREQ:
  466. {
  467. u32 freq;
  468. if(copy_from_user(arg, &amp;freq,
  469. sizeof(unsigned long))!=0)
  470. return -EFAULT;
  471. if(hardware_set_freq(freq)&lt;0)
  472. return -EINVAL;
  473. current_freq = freq;
  474. return 0;
  475. }
  476. </programlisting>
  477. <para>
  478. Setting the frequency is a little more complex. We begin by copying the
  479. desired frequency into kernel space. Next we call a hardware specific routine
  480. to set the radio up. This might be as simple as some scaling and a few
  481. writes to an I/O port. For most radio cards it turns out a good deal more
  482. complicated and may involve programming things like a phase locked loop on
  483. the card. This is what documentation is for.
  484. </para>
  485. <para>
  486. The final set of operations we need to provide for our radio are the
  487. volume controls. Not all radio cards can even do volume control. After all
  488. there is a perfectly good volume control on the sound card. We will assume
  489. our radio card has a simple 4 step volume control.
  490. </para>
  491. <para>
  492. There are two ioctls with audio we need to support
  493. </para>
  494. <programlisting>
  495. static int current_volume=0;
  496. case VIDIOCGAUDIO:
  497. {
  498. struct video_audio v;
  499. if(copy_from_user(&amp;v, arg, sizeof(v)))
  500. return -EFAULT;
  501. if(v.audio != 0)
  502. return -EINVAL;
  503. v.volume = 16384*current_volume;
  504. v.step = 16384;
  505. strcpy(v.name, "Radio");
  506. v.mode = VIDEO_SOUND_MONO;
  507. v.balance = 0;
  508. v.base = 0;
  509. v.treble = 0;
  510. if(copy_to_user(arg. &amp;v, sizeof(v)))
  511. return -EFAULT;
  512. return 0;
  513. }
  514. </programlisting>
  515. <para>
  516. Much like the tuner we start by copying the user structure into kernel
  517. space. Again we check if the user has asked for a valid audio input. We have
  518. only input 0 and we punt if they ask for another input.
  519. </para>
  520. <para>
  521. Then we fill in the video_audio structure. This has the following format
  522. </para>
  523. <table frame="all" id="video_audio_fields"><title>struct video_audio fields</title>
  524. <tgroup cols="2" align="left">
  525. <tbody>
  526. <row>
  527. <entry>audio</entry><entry>The input the user wishes to query</entry>
  528. </row><row>
  529. <entry>volume</entry><entry>The volume setting on a scale of 0-65535</entry>
  530. </row><row>
  531. <entry>base</entry><entry>The base level on a scale of 0-65535</entry>
  532. </row><row>
  533. <entry>treble</entry><entry>The treble level on a scale of 0-65535</entry>
  534. </row><row>
  535. <entry>flags</entry><entry>The features this audio device supports
  536. </entry>
  537. </row><row>
  538. <entry>name</entry><entry>A text name to display to the user. We picked
  539. "Radio" as it explains things quite nicely.</entry>
  540. </row><row>
  541. <entry>mode</entry><entry>The current reception mode for the audio
  542. We report MONO because our card is too stupid to know if it is in
  543. mono or stereo.
  544. </entry>
  545. </row><row>
  546. <entry>balance</entry><entry>The stereo balance on a scale of 0-65535, 32768 is
  547. middle.</entry>
  548. </row><row>
  549. <entry>step</entry><entry>The step by which the volume control jumps. This is
  550. used to help make it easy for applications to set
  551. slider behaviour.</entry>
  552. </row>
  553. </tbody>
  554. </tgroup>
  555. </table>
  556. <table frame="all" id="video_audio_flags"><title>struct video_audio flags</title>
  557. <tgroup cols="2" align="left">
  558. <tbody>
  559. <row>
  560. <entry>VIDEO_AUDIO_MUTE</entry><entry>The audio is currently muted. We
  561. could fake this in our driver but we
  562. choose not to bother.</entry>
  563. </row><row>
  564. <entry>VIDEO_AUDIO_MUTABLE</entry><entry>The input has a mute option</entry>
  565. </row><row>
  566. <entry>VIDEO_AUDIO_TREBLE</entry><entry>The input has a treble control</entry>
  567. </row><row>
  568. <entry>VIDEO_AUDIO_BASS</entry><entry>The input has a base control</entry>
  569. </row>
  570. </tbody>
  571. </tgroup>
  572. </table>
  573. <table frame="all" id="video_audio_modes"><title>struct video_audio modes</title>
  574. <tgroup cols="2" align="left">
  575. <tbody>
  576. <row>
  577. <entry>VIDEO_SOUND_MONO</entry><entry>Mono sound</entry>
  578. </row><row>
  579. <entry>VIDEO_SOUND_STEREO</entry><entry>Stereo sound</entry>
  580. </row><row>
  581. <entry>VIDEO_SOUND_LANG1</entry><entry>Alternative language 1 (TV specific)</entry>
  582. </row><row>
  583. <entry>VIDEO_SOUND_LANG2</entry><entry>Alternative language 2 (TV specific)</entry>
  584. </row>
  585. </tbody>
  586. </tgroup>
  587. </table>
  588. <para>
  589. Having filled in the structure we copy it back to user space.
  590. </para>
  591. <para>
  592. The VIDIOCSAUDIO ioctl allows the user to set the audio parameters in the
  593. video_audio structure. The driver does its best to honour the request.
  594. </para>
  595. <programlisting>
  596. case VIDIOCSAUDIO:
  597. {
  598. struct video_audio v;
  599. if(copy_from_user(&amp;v, arg, sizeof(v)))
  600. return -EFAULT;
  601. if(v.audio)
  602. return -EINVAL;
  603. current_volume = v/16384;
  604. hardware_set_volume(current_volume);
  605. return 0;
  606. }
  607. </programlisting>
  608. <para>
  609. In our case there is very little that the user can set. The volume is
  610. basically the limit. Note that we could pretend to have a mute feature
  611. by rewriting this to
  612. </para>
  613. <programlisting>
  614. case VIDIOCSAUDIO:
  615. {
  616. struct video_audio v;
  617. if(copy_from_user(&amp;v, arg, sizeof(v)))
  618. return -EFAULT;
  619. if(v.audio)
  620. return -EINVAL;
  621. current_volume = v/16384;
  622. if(v.flags&amp;VIDEO_AUDIO_MUTE)
  623. hardware_set_volume(0);
  624. else
  625. hardware_set_volume(current_volume);
  626. current_muted = v.flags &amp;
  627. VIDEO_AUDIO_MUTE;
  628. return 0;
  629. }
  630. </programlisting>
  631. <para>
  632. This with the corresponding changes to the VIDIOCGAUDIO code to report the
  633. state of the mute flag we save and to report the card has a mute function,
  634. will allow applications to use a mute facility with this card. It is
  635. questionable whether this is a good idea however. User applications can already
  636. fake this themselves and kernel space is precious.
  637. </para>
  638. <para>
  639. We now have a working radio ioctl handler. So we just wrap up the function
  640. </para>
  641. <programlisting>
  642. }
  643. return -ENOIOCTLCMD;
  644. }
  645. </programlisting>
  646. <para>
  647. and pass the Video4Linux layer back an error so that it knows we did not
  648. understand the request we got passed.
  649. </para>
  650. </sect1>
  651. <sect1 id="modradio">
  652. <title>Module Wrapper</title>
  653. <para>
  654. Finally we add in the usual module wrapping and the driver is done.
  655. </para>
  656. <programlisting>
  657. #ifndef MODULE
  658. static int io = 0x300;
  659. #else
  660. static int io = -1;
  661. #endif
  662. MODULE_AUTHOR("Alan Cox");
  663. MODULE_DESCRIPTION("A driver for an imaginary radio card.");
  664. module_param(io, int, 0444);
  665. MODULE_PARM_DESC(io, "I/O address of the card.");
  666. static int __init init(void)
  667. {
  668. if(io==-1)
  669. {
  670. printk(KERN_ERR
  671. "You must set an I/O address with io=0x???\n");
  672. return -EINVAL;
  673. }
  674. return myradio_init(NULL);
  675. }
  676. static void __exit cleanup(void)
  677. {
  678. video_unregister_device(&amp;my_radio);
  679. release_region(io, MY_IO_SIZE);
  680. }
  681. module_init(init);
  682. module_exit(cleanup);
  683. </programlisting>
  684. <para>
  685. In this example we set the IO base by default if the driver is compiled into
  686. the kernel: you can still set it using "my_radio.irq" if this file is called <filename>my_radio.c</filename>. For the module we require the
  687. user sets the parameter. We set io to a nonsense port (-1) so that we can
  688. tell if the user supplied an io parameter or not.
  689. </para>
  690. <para>
  691. We use MODULE_ defines to give an author for the card driver and a
  692. description. We also use them to declare that io is an integer and it is the
  693. address of the card, and can be read by anyone from sysfs.
  694. </para>
  695. <para>
  696. The clean-up routine unregisters the video_device we registered, and frees
  697. up the I/O space. Note that the unregister takes the actual video_device
  698. structure as its argument. Unlike the file operations structure which can be
  699. shared by all instances of a device a video_device structure as an actual
  700. instance of the device. If you are registering multiple radio devices you
  701. need to fill in one structure per device (most likely by setting up a
  702. template and copying it to each of the actual device structures).
  703. </para>
  704. </sect1>
  705. </chapter>
  706. <chapter id="Video_Capture_Devices">
  707. <title>Video Capture Devices</title>
  708. <sect1 id="introvid">
  709. <title>Video Capture Device Types</title>
  710. <para>
  711. The video capture devices share the same interfaces as radio devices. In
  712. order to explain the video capture interface I will use the example of a
  713. camera that has no tuners or audio input. This keeps the example relatively
  714. clean. To get both combine the two driver examples.
  715. </para>
  716. <para>
  717. Video capture devices divide into four categories. A little technology
  718. backgrounder. Full motion video even at television resolution (which is
  719. actually fairly low) is pretty resource-intensive. You are continually
  720. passing megabytes of data every second from the capture card to the display.
  721. several alternative approaches have emerged because copying this through the
  722. processor and the user program is a particularly bad idea .
  723. </para>
  724. <para>
  725. The first is to add the television image onto the video output directly.
  726. This is also how some 3D cards work. These basic cards can generally drop the
  727. video into any chosen rectangle of the display. Cards like this, which
  728. include most mpeg1 cards that used the feature connector, aren't very
  729. friendly in a windowing environment. They don't understand windows or
  730. clipping. The video window is always on the top of the display.
  731. </para>
  732. <para>
  733. Chroma keying is a technique used by cards to get around this. It is an old
  734. television mixing trick where you mark all the areas you wish to replace
  735. with a single clear colour that isn't used in the image - TV people use an
  736. incredibly bright blue while computing people often use a particularly
  737. virulent purple. Bright blue occurs on the desktop. Anyone with virulent
  738. purple windows has another problem besides their TV overlay.
  739. </para>
  740. <para>
  741. The third approach is to copy the data from the capture card to the video
  742. card, but to do it directly across the PCI bus. This relieves the processor
  743. from doing the work but does require some smartness on the part of the video
  744. capture chip, as well as a suitable video card. Programming this kind of
  745. card and more so debugging it can be extremely tricky. There are some quite
  746. complicated interactions with the display and you may also have to cope with
  747. various chipset bugs that show up when PCI cards start talking to each
  748. other.
  749. </para>
  750. <para>
  751. To keep our example fairly simple we will assume a card that supports
  752. overlaying a flat rectangular image onto the frame buffer output, and which
  753. can also capture stuff into processor memory.
  754. </para>
  755. </sect1>
  756. <sect1 id="regvid">
  757. <title>Registering Video Capture Devices</title>
  758. <para>
  759. This time we need to add more functions for our camera device.
  760. </para>
  761. <programlisting>
  762. static struct video_device my_camera
  763. {
  764. "My Camera",
  765. VID_TYPE_OVERLAY|VID_TYPE_SCALES|\
  766. VID_TYPE_CAPTURE|VID_TYPE_CHROMAKEY,
  767. camera_open.
  768. camera_close,
  769. camera_read, /* no read */
  770. NULL, /* no write */
  771. camera_poll, /* no poll */
  772. camera_ioctl,
  773. NULL, /* no special init function */
  774. NULL /* no private data */
  775. };
  776. </programlisting>
  777. <para>
  778. We need a read() function which is used for capturing data from
  779. the card, and we need a poll function so that a driver can wait for the next
  780. frame to be captured.
  781. </para>
  782. <para>
  783. We use the extra video capability flags that did not apply to the
  784. radio interface. The video related flags are
  785. </para>
  786. <table frame="all" id="Capture_Capabilities"><title>Capture Capabilities</title>
  787. <tgroup cols="2" align="left">
  788. <tbody>
  789. <row>
  790. <entry>VID_TYPE_CAPTURE</entry><entry>We support image capture</entry>
  791. </row><row>
  792. <entry>VID_TYPE_TELETEXT</entry><entry>A teletext capture device (vbi{n])</entry>
  793. </row><row>
  794. <entry>VID_TYPE_OVERLAY</entry><entry>The image can be directly overlaid onto the
  795. frame buffer</entry>
  796. </row><row>
  797. <entry>VID_TYPE_CHROMAKEY</entry><entry>Chromakey can be used to select which parts
  798. of the image to display</entry>
  799. </row><row>
  800. <entry>VID_TYPE_CLIPPING</entry><entry>It is possible to give the board a list of
  801. rectangles to draw around. </entry>
  802. </row><row>
  803. <entry>VID_TYPE_FRAMERAM</entry><entry>The video capture goes into the video memory
  804. and actually changes it. Applications need
  805. to know this so they can clean up after the
  806. card</entry>
  807. </row><row>
  808. <entry>VID_TYPE_SCALES</entry><entry>The image can be scaled to various sizes,
  809. rather than being a single fixed size.</entry>
  810. </row><row>
  811. <entry>VID_TYPE_MONOCHROME</entry><entry>The capture will be monochrome. This isn't a
  812. complete answer to the question since a mono
  813. camera on a colour capture card will still
  814. produce mono output.</entry>
  815. </row><row>
  816. <entry>VID_TYPE_SUBCAPTURE</entry><entry>The card allows only part of its field of
  817. view to be captured. This enables
  818. applications to avoid copying all of a large
  819. image into memory when only some section is
  820. relevant.</entry>
  821. </row>
  822. </tbody>
  823. </tgroup>
  824. </table>
  825. <para>
  826. We set VID_TYPE_CAPTURE so that we are seen as a capture card,
  827. VID_TYPE_CHROMAKEY so the application knows it is time to draw in virulent
  828. purple, and VID_TYPE_SCALES because we can be resized.
  829. </para>
  830. <para>
  831. Our setup is fairly similar. This time we also want an interrupt line
  832. for the 'frame captured' signal. Not all cards have this so some of them
  833. cannot handle poll().
  834. </para>
  835. <programlisting>
  836. static int io = 0x320;
  837. static int irq = 11;
  838. int __init mycamera_init(struct video_init *v)
  839. {
  840. if(!request_region(io, MY_IO_SIZE, "mycamera"))
  841. {
  842. printk(KERN_ERR
  843. "mycamera: port 0x%03X is in use.\n", io);
  844. return -EBUSY;
  845. }
  846. if(video_device_register(&amp;my_camera,
  847. VFL_TYPE_GRABBER)==-1) {
  848. release_region(io, MY_IO_SIZE);
  849. return -EINVAL;
  850. }
  851. return 0;
  852. }
  853. </programlisting>
  854. <para>
  855. This is little changed from the needs of the radio card. We specify
  856. VFL_TYPE_GRABBER this time as we want to be allocated a /dev/video name.
  857. </para>
  858. </sect1>
  859. <sect1 id="opvid">
  860. <title>Opening And Closing The Capture Device</title>
  861. <programlisting>
  862. static int users = 0;
  863. static int camera_open(struct video_device *dev, int flags)
  864. {
  865. if(users)
  866. return -EBUSY;
  867. if(request_irq(irq, camera_irq, 0, "camera", dev)&lt;0)
  868. return -EBUSY;
  869. users++;
  870. return 0;
  871. }
  872. static int camera_close(struct video_device *dev)
  873. {
  874. users--;
  875. free_irq(irq, dev);
  876. }
  877. </programlisting>
  878. <para>
  879. The open and close routines are also quite similar. The only real change is
  880. that we now request an interrupt for the camera device interrupt line. If we
  881. cannot get the interrupt we report EBUSY to the application and give up.
  882. </para>
  883. </sect1>
  884. <sect1 id="irqvid">
  885. <title>Interrupt Handling</title>
  886. <para>
  887. Our example handler is for an ISA bus device. If it was PCI you would be
  888. able to share the interrupt and would have set IRQF_SHARED to indicate a
  889. shared IRQ. We pass the device pointer as the interrupt routine argument. We
  890. don't need to since we only support one card but doing this will make it
  891. easier to upgrade the driver for multiple devices in the future.
  892. </para>
  893. <para>
  894. Our interrupt routine needs to do little if we assume the card can simply
  895. queue one frame to be read after it captures it.
  896. </para>
  897. <programlisting>
  898. static struct wait_queue *capture_wait;
  899. static int capture_ready = 0;
  900. static void camera_irq(int irq, void *dev_id,
  901. struct pt_regs *regs)
  902. {
  903. capture_ready=1;
  904. wake_up_interruptible(&amp;capture_wait);
  905. }
  906. </programlisting>
  907. <para>
  908. The interrupt handler is nice and simple for this card as we are assuming
  909. the card is buffering the frame for us. This means we have little to do but
  910. wake up anybody interested. We also set a capture_ready flag, as we may
  911. capture a frame before an application needs it. In this case we need to know
  912. that a frame is ready. If we had to collect the frame on the interrupt life
  913. would be more complex.
  914. </para>
  915. <para>
  916. The two new routines we need to supply are camera_read which returns a
  917. frame, and camera_poll which waits for a frame to become ready.
  918. </para>
  919. <programlisting>
  920. static int camera_poll(struct video_device *dev,
  921. struct file *file, struct poll_table *wait)
  922. {
  923. poll_wait(file, &amp;capture_wait, wait);
  924. if(capture_read)
  925. return POLLIN|POLLRDNORM;
  926. return 0;
  927. }
  928. </programlisting>
  929. <para>
  930. Our wait queue for polling is the capture_wait queue. This will cause the
  931. task to be woken up by our camera_irq routine. We check capture_read to see
  932. if there is an image present and if so report that it is readable.
  933. </para>
  934. </sect1>
  935. <sect1 id="rdvid">
  936. <title>Reading The Video Image</title>
  937. <programlisting>
  938. static long camera_read(struct video_device *dev, char *buf,
  939. unsigned long count)
  940. {
  941. struct wait_queue wait = { current, NULL };
  942. u8 *ptr;
  943. int len;
  944. int i;
  945. add_wait_queue(&amp;capture_wait, &amp;wait);
  946. while(!capture_ready)
  947. {
  948. if(file->flags&amp;O_NDELAY)
  949. {
  950. remove_wait_queue(&amp;capture_wait, &amp;wait);
  951. current->state = TASK_RUNNING;
  952. return -EWOULDBLOCK;
  953. }
  954. if(signal_pending(current))
  955. {
  956. remove_wait_queue(&amp;capture_wait, &amp;wait);
  957. current->state = TASK_RUNNING;
  958. return -ERESTARTSYS;
  959. }
  960. schedule();
  961. current->state = TASK_INTERRUPTIBLE;
  962. }
  963. remove_wait_queue(&amp;capture_wait, &amp;wait);
  964. current->state = TASK_RUNNING;
  965. </programlisting>
  966. <para>
  967. The first thing we have to do is to ensure that the application waits until
  968. the next frame is ready. The code here is almost identical to the mouse code
  969. we used earlier in this chapter. It is one of the common building blocks of
  970. Linux device driver code and probably one which you will find occurs in any
  971. drivers you write.
  972. </para>
  973. <para>
  974. We wait for a frame to be ready, or for a signal to interrupt our waiting. If a
  975. signal occurs we need to return from the system call so that the signal can
  976. be sent to the application itself. We also check to see if the user actually
  977. wanted to avoid waiting - ie if they are using non-blocking I/O and have other things
  978. to get on with.
  979. </para>
  980. <para>
  981. Next we copy the data from the card to the user application. This is rarely
  982. as easy as our example makes out. We will add capture_w, and capture_h here
  983. to hold the width and height of the captured image. We assume the card only
  984. supports 24bit RGB for now.
  985. </para>
  986. <programlisting>
  987. capture_ready = 0;
  988. ptr=(u8 *)buf;
  989. len = capture_w * 3 * capture_h; /* 24bit RGB */
  990. if(len>count)
  991. len=count; /* Doesn't all fit */
  992. for(i=0; i&lt;len; i++)
  993. {
  994. put_user(inb(io+IMAGE_DATA), ptr);
  995. ptr++;
  996. }
  997. hardware_restart_capture();
  998. return i;
  999. }
  1000. </programlisting>
  1001. <para>
  1002. For a real hardware device you would try to avoid the loop with put_user().
  1003. Each call to put_user() has a time overhead checking whether the accesses to user
  1004. space are allowed. It would be better to read a line into a temporary buffer
  1005. then copy this to user space in one go.
  1006. </para>
  1007. <para>
  1008. Having captured the image and put it into user space we can kick the card to
  1009. get the next frame acquired.
  1010. </para>
  1011. </sect1>
  1012. <sect1 id="iocvid">
  1013. <title>Video Ioctl Handling</title>
  1014. <para>
  1015. As with the radio driver the major control interface is via the ioctl()
  1016. function. Video capture devices support the same tuner calls as a radio
  1017. device and also support additional calls to control how the video functions
  1018. are handled. In this simple example the card has no tuners to avoid making
  1019. the code complex.
  1020. </para>
  1021. <programlisting>
  1022. static int camera_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
  1023. {
  1024. switch(cmd)
  1025. {
  1026. case VIDIOCGCAP:
  1027. {
  1028. struct video_capability v;
  1029. v.type = VID_TYPE_CAPTURE|\
  1030. VID_TYPE_CHROMAKEY|\
  1031. VID_TYPE_SCALES|\
  1032. VID_TYPE_OVERLAY;
  1033. v.channels = 1;
  1034. v.audios = 0;
  1035. v.maxwidth = 640;
  1036. v.minwidth = 16;
  1037. v.maxheight = 480;
  1038. v.minheight = 16;
  1039. strcpy(v.name, "My Camera");
  1040. if(copy_to_user(arg, &amp;v, sizeof(v)))
  1041. return -EFAULT;
  1042. return 0;
  1043. }
  1044. </programlisting>
  1045. <para>
  1046. The first ioctl we must support and which all video capture and radio
  1047. devices are required to support is VIDIOCGCAP. This behaves exactly the same
  1048. as with a radio device. This time, however, we report the extra capabilities
  1049. we outlined earlier on when defining our video_dev structure.
  1050. </para>
  1051. <para>
  1052. We now set the video flags saying that we support overlay, capture,
  1053. scaling and chromakey. We also report size limits - our smallest image is
  1054. 16x16 pixels, our largest is 640x480.
  1055. </para>
  1056. <para>
  1057. To keep things simple we report no audio and no tuning capabilities at all.
  1058. </para>
  1059. <programlisting>
  1060. case VIDIOCGCHAN:
  1061. {
  1062. struct video_channel v;
  1063. if(copy_from_user(&amp;v, arg, sizeof(v)))
  1064. return -EFAULT;
  1065. if(v.channel != 0)
  1066. return -EINVAL;
  1067. v.flags = 0;
  1068. v.tuners = 0;
  1069. v.type = VIDEO_TYPE_CAMERA;
  1070. v.norm = VIDEO_MODE_AUTO;
  1071. strcpy(v.name, "Camera Input");break;
  1072. if(copy_to_user(&amp;v, arg, sizeof(v)))
  1073. return -EFAULT;
  1074. return 0;
  1075. }
  1076. </programlisting>
  1077. <para>
  1078. This follows what is very much the standard way an ioctl handler looks
  1079. in Linux. We copy the data into a kernel space variable and we check that the
  1080. request is valid (in this case that the input is 0). Finally we copy the
  1081. camera info back to the user.
  1082. </para>
  1083. <para>
  1084. The VIDIOCGCHAN ioctl allows a user to ask about video channels (that is
  1085. inputs to the video card). Our example card has a single camera input. The
  1086. fields in the structure are
  1087. </para>
  1088. <table frame="all" id="video_channel_fields"><title>struct video_channel fields</title>
  1089. <tgroup cols="2" align="left">
  1090. <tbody>
  1091. <row>
  1092. <entry>channel</entry><entry>The channel number we are selecting</entry>
  1093. </row><row>
  1094. <entry>name</entry><entry>The name for this channel. This is intended
  1095. to describe the port to the user.
  1096. Appropriate names are therefore things like
  1097. "Camera" "SCART input"</entry>
  1098. </row><row>
  1099. <entry>flags</entry><entry>Channel properties</entry>
  1100. </row><row>
  1101. <entry>type</entry><entry>Input type</entry>
  1102. </row><row>
  1103. <entry>norm</entry><entry>The current television encoding being used
  1104. if relevant for this channel.
  1105. </entry>
  1106. </row>
  1107. </tbody>
  1108. </tgroup>
  1109. </table>
  1110. <table frame="all" id="video_channel_flags"><title>struct video_channel flags</title>
  1111. <tgroup cols="2" align="left">
  1112. <tbody>
  1113. <row>
  1114. <entry>VIDEO_VC_TUNER</entry><entry>Channel has a tuner.</entry>
  1115. </row><row>
  1116. <entry>VIDEO_VC_AUDIO</entry><entry>Channel has audio.</entry>
  1117. </row>
  1118. </tbody>
  1119. </tgroup>
  1120. </table>
  1121. <table frame="all" id="video_channel_types"><title>struct video_channel types</title>
  1122. <tgroup cols="2" align="left">
  1123. <tbody>
  1124. <row>
  1125. <entry>VIDEO_TYPE_TV</entry><entry>Television input.</entry>
  1126. </row><row>
  1127. <entry>VIDEO_TYPE_CAMERA</entry><entry>Fixed camera input.</entry>
  1128. </row><row>
  1129. <entry>0</entry><entry>Type is unknown.</entry>
  1130. </row>
  1131. </tbody>
  1132. </tgroup>
  1133. </table>
  1134. <table frame="all" id="video_channel_norms"><title>struct video_channel norms</title>
  1135. <tgroup cols="2" align="left">
  1136. <tbody>
  1137. <row>
  1138. <entry>VIDEO_MODE_PAL</entry><entry>PAL encoded Television</entry>
  1139. </row><row>
  1140. <entry>VIDEO_MODE_NTSC</entry><entry>NTSC (US) encoded Television</entry>
  1141. </row><row>
  1142. <entry>VIDEO_MODE_SECAM</entry><entry>SECAM (French) Television </entry>
  1143. </row><row>
  1144. <entry>VIDEO_MODE_AUTO</entry><entry>Automatic switching, or format does not
  1145. matter</entry>
  1146. </row>
  1147. </tbody>
  1148. </tgroup>
  1149. </table>
  1150. <para>
  1151. The corresponding VIDIOCSCHAN ioctl allows a user to change channel and to
  1152. request the norm is changed - for example to switch between a PAL or an NTSC
  1153. format camera.
  1154. </para>
  1155. <programlisting>
  1156. case VIDIOCSCHAN:
  1157. {
  1158. struct video_channel v;
  1159. if(copy_from_user(&amp;v, arg, sizeof(v)))
  1160. return -EFAULT;
  1161. if(v.channel != 0)
  1162. return -EINVAL;
  1163. if(v.norm != VIDEO_MODE_AUTO)
  1164. return -EINVAL;
  1165. return 0;
  1166. }
  1167. </programlisting>
  1168. <para>
  1169. The implementation of this call in our driver is remarkably easy. Because we
  1170. are assuming fixed format hardware we need only check that the user has not
  1171. tried to change anything.
  1172. </para>
  1173. <para>
  1174. The user also needs to be able to configure and adjust the picture they are
  1175. seeing. This is much like adjusting a television set. A user application
  1176. also needs to know the palette being used so that it knows how to display
  1177. the image that has been captured. The VIDIOCGPICT and VIDIOCSPICT ioctl
  1178. calls provide this information.
  1179. </para>
  1180. <programlisting>
  1181. case VIDIOCGPICT
  1182. {
  1183. struct video_picture v;
  1184. v.brightness = hardware_brightness();
  1185. v.hue = hardware_hue();
  1186. v.colour = hardware_saturation();
  1187. v.contrast = hardware_brightness();
  1188. /* Not settable */
  1189. v.whiteness = 32768;
  1190. v.depth = 24; /* 24bit */
  1191. v.palette = VIDEO_PALETTE_RGB24;
  1192. if(copy_to_user(&amp;v, arg,
  1193. sizeof(v)))
  1194. return -EFAULT;
  1195. return 0;
  1196. }
  1197. </programlisting>
  1198. <para>
  1199. The brightness, hue, color, and contrast provide the picture controls that
  1200. are akin to a conventional television. Whiteness provides additional
  1201. control for greyscale images. All of these values are scaled between 0-65535
  1202. and have 32768 as the mid point setting. The scaling means that applications
  1203. do not have to worry about the capability range of the hardware but can let
  1204. it make a best effort attempt.
  1205. </para>
  1206. <para>
  1207. Our depth is 24, as this is in bits. We will be returning RGB24 format. This
  1208. has one byte of red, then one of green, then one of blue. This then repeats
  1209. for every other pixel in the image. The other common formats the interface
  1210. defines are
  1211. </para>
  1212. <table frame="all" id="Framebuffer_Encodings"><title>Framebuffer Encodings</title>
  1213. <tgroup cols="2" align="left">
  1214. <tbody>
  1215. <row>
  1216. <entry>GREY</entry><entry>Linear greyscale. This is for simple cameras and the
  1217. like</entry>
  1218. </row><row>
  1219. <entry>RGB565</entry><entry>The top 5 bits hold 32 red levels, the next six bits
  1220. hold green and the low 5 bits hold blue. </entry>
  1221. </row><row>
  1222. <entry>RGB555</entry><entry>The top bit is clear. The red green and blue levels
  1223. each occupy five bits.</entry>
  1224. </row>
  1225. </tbody>
  1226. </tgroup>
  1227. </table>
  1228. <para>
  1229. Additional modes are support for YUV capture formats. These are common for
  1230. TV and video conferencing applications.
  1231. </para>
  1232. <para>
  1233. The VIDIOCSPICT ioctl allows a user to set some of the picture parameters.
  1234. Exactly which ones are supported depends heavily on the card itself. It is
  1235. possible to support many modes and effects in software. In general doing
  1236. this in the kernel is a bad idea. Video capture is a performance-sensitive
  1237. application and the programs can often do better if they aren't being
  1238. 'helped' by an overkeen driver writer. Thus for our device we will report
  1239. RGB24 only and refuse to allow a change.
  1240. </para>
  1241. <programlisting>
  1242. case VIDIOCSPICT:
  1243. {
  1244. struct video_picture v;
  1245. if(copy_from_user(&amp;v, arg, sizeof(v)))
  1246. return -EFAULT;
  1247. if(v.depth!=24 ||
  1248. v.palette != VIDEO_PALETTE_RGB24)
  1249. return -EINVAL;
  1250. set_hardware_brightness(v.brightness);
  1251. set_hardware_hue(v.hue);
  1252. set_hardware_saturation(v.colour);
  1253. set_hardware_brightness(v.contrast);
  1254. return 0;
  1255. }
  1256. </programlisting>
  1257. <para>
  1258. We check the user has not tried to change the palette or the depth. We do
  1259. not want to carry out some of the changes and then return an error. This may
  1260. confuse the application which will be assuming no change occurred.
  1261. </para>
  1262. <para>
  1263. In much the same way as you need to be able to set the picture controls to
  1264. get the right capture images, many cards need to know what they are
  1265. displaying onto when generating overlay output. In some cases getting this
  1266. wrong even makes a nasty mess or may crash the computer. For that reason
  1267. the VIDIOCSBUF ioctl used to set up the frame buffer information may well
  1268. only be usable by root.
  1269. </para>
  1270. <para>
  1271. We will assume our card is one of the old ISA devices with feature connector
  1272. and only supports a couple of standard video modes. Very common for older
  1273. cards although the PCI devices are way smarter than this.
  1274. </para>
  1275. <programlisting>
  1276. static struct video_buffer capture_fb;
  1277. case VIDIOCGFBUF:
  1278. {
  1279. if(copy_to_user(arg, &amp;capture_fb,
  1280. sizeof(capture_fb)))
  1281. return -EFAULT;
  1282. return 0;
  1283. }
  1284. </programlisting>
  1285. <para>
  1286. We keep the frame buffer information in the format the ioctl uses. This
  1287. makes it nice and easy to work with in the ioctl calls.
  1288. </para>
  1289. <programlisting>
  1290. case VIDIOCSFBUF:
  1291. {
  1292. struct video_buffer v;
  1293. if(!capable(CAP_SYS_ADMIN))
  1294. return -EPERM;
  1295. if(copy_from_user(&amp;v, arg, sizeof(v)))
  1296. return -EFAULT;
  1297. if(v.width!=320 &amp;&amp; v.width!=640)
  1298. return -EINVAL;
  1299. if(v.height!=200 &amp;&amp; v.height!=240
  1300. &amp;&amp; v.height!=400
  1301. &amp;&amp; v.height !=480)
  1302. return -EINVAL;
  1303. memcpy(&amp;capture_fb, &amp;v, sizeof(v));
  1304. hardware_set_fb(&amp;v);
  1305. return 0;
  1306. }
  1307. </programlisting>
  1308. <para>
  1309. The capable() function checks a user has the required capability. The Linux
  1310. operating system has a set of about 30 capabilities indicating privileged
  1311. access to services. The default set up gives the superuser (uid 0) all of
  1312. them and nobody else has any.
  1313. </para>
  1314. <para>
  1315. We check that the user has the SYS_ADMIN capability, that is they are
  1316. allowed to operate as the machine administrator. We don't want anyone but
  1317. the administrator making a mess of the display.
  1318. </para>
  1319. <para>
  1320. Next we check for standard PC video modes (320 or 640 wide with either
  1321. EGA or VGA depths). If the mode is not a standard video mode we reject it as
  1322. not supported by our card. If the mode is acceptable we save it so that
  1323. VIDIOCFBUF will give the right answer next time it is called. The
  1324. hardware_set_fb() function is some undescribed card specific function to
  1325. program the card for the desired mode.
  1326. </para>
  1327. <para>
  1328. Before the driver can display an overlay window it needs to know where the
  1329. window should be placed, and also how large it should be. If the card
  1330. supports clipping it needs to know which rectangles to omit from the
  1331. display. The video_window structure is used to describe the way the image
  1332. should be displayed.
  1333. </para>
  1334. <table frame="all" id="video_window_fields"><title>struct video_window fields</title>
  1335. <tgroup cols="2" align="left">
  1336. <tbody>
  1337. <row>
  1338. <entry>width</entry><entry>The width in pixels of the desired image. The card
  1339. may use a smaller size if this size is not available</entry>
  1340. </row><row>
  1341. <entry>height</entry><entry>The height of the image. The card may use a smaller
  1342. size if this size is not available.</entry>
  1343. </row><row>
  1344. <entry>x</entry><entry> The X position of the top left of the window. This
  1345. is in pixels relative to the left hand edge of the
  1346. picture. Not all cards can display images aligned on
  1347. any pixel boundary. If the position is unsuitable
  1348. the card adjusts the image right and reduces the
  1349. width.</entry>
  1350. </row><row>
  1351. <entry>y</entry><entry> The Y position of the top left of the window. This
  1352. is counted in pixels relative to the top edge of the
  1353. picture. As with the width if the card cannot
  1354. display starting on this line it will adjust the
  1355. values.</entry>
  1356. </row><row>
  1357. <entry>chromakey</entry><entry>The colour (expressed in RGB32 format) for the
  1358. chromakey colour if chroma keying is being used. </entry>
  1359. </row><row>
  1360. <entry>clips</entry><entry>An array of rectangles that must not be drawn
  1361. over.</entry>
  1362. </row><row>
  1363. <entry>clipcount</entry><entry>The number of clips in this array.</entry>
  1364. </row>
  1365. </tbody>
  1366. </tgroup>
  1367. </table>
  1368. <para>
  1369. Each clip is a struct video_clip which has the following fields
  1370. </para>
  1371. <table frame="all" id="video_clip_fields"><title>video_clip fields</title>
  1372. <tgroup cols="2" align="left">
  1373. <tbody>
  1374. <row>
  1375. <entry>x, y</entry><entry>Co-ordinates relative to the display</entry>
  1376. </row><row>
  1377. <entry>width, height</entry><entry>Width and height in pixels</entry>
  1378. </row><row>
  1379. <entry>next</entry><entry>A spare field for the application to use</entry>
  1380. </row>
  1381. </tbody>
  1382. </tgroup>
  1383. </table>
  1384. <para>
  1385. The driver is required to ensure it always draws in the area requested or a smaller area, and that it never draws in any of the areas that are clipped.
  1386. This may well mean it has to leave alone. small areas the application wished to be
  1387. drawn.
  1388. </para>
  1389. <para>
  1390. Our example card uses chromakey so does not have to address most of the
  1391. clipping. We will add a video_window structure to our global variables to
  1392. remember our parameters, as we did with the frame buffer.
  1393. </para>
  1394. <programlisting>
  1395. case VIDIOCGWIN:
  1396. {
  1397. if(copy_to_user(arg, &amp;capture_win,
  1398. sizeof(capture_win)))
  1399. return -EFAULT;
  1400. return 0;
  1401. }
  1402. case VIDIOCSWIN:
  1403. {
  1404. struct video_window v;
  1405. if(copy_from_user(&amp;v, arg, sizeof(v)))
  1406. return -EFAULT;
  1407. if(v.width &gt; 640 || v.height &gt; 480)
  1408. return -EINVAL;
  1409. if(v.width &lt; 16 || v.height &lt; 16)
  1410. return -EINVAL;
  1411. hardware_set_key(v.chromakey);
  1412. hardware_set_window(v);
  1413. memcpy(&amp;capture_win, &amp;v, sizeof(v));
  1414. capture_w = v.width;
  1415. capture_h = v.height;
  1416. return 0;
  1417. }
  1418. </programlisting>
  1419. <para>
  1420. Because we are using Chromakey our setup is fairly simple. Mostly we have to
  1421. check the values are sane and load them into the capture card.
  1422. </para>
  1423. <para>
  1424. With all the setup done we can now turn on the actual capture/overlay. This
  1425. is done with the VIDIOCCAPTURE ioctl. This takes a single integer argument
  1426. where 0 is on and 1 is off.
  1427. </para>
  1428. <programlisting>
  1429. case VIDIOCCAPTURE:
  1430. {
  1431. int v;
  1432. if(get_user(v, (int *)arg))
  1433. return -EFAULT;
  1434. if(v==0)
  1435. hardware_capture_off();
  1436. else
  1437. {
  1438. if(capture_fb.width == 0
  1439. || capture_w == 0)
  1440. return -EINVAL;
  1441. hardware_capture_on();
  1442. }
  1443. return 0;
  1444. }
  1445. </programlisting>
  1446. <para>
  1447. We grab the flag from user space and either enable or disable according to
  1448. its value. There is one small corner case we have to consider here. Suppose
  1449. that the capture was requested before the video window or the frame buffer
  1450. had been set up. In those cases there will be unconfigured fields in our
  1451. card data, as well as unconfigured hardware settings. We check for this case and
  1452. return an error if the frame buffer or the capture window width is zero.
  1453. </para>
  1454. <programlisting>
  1455. default:
  1456. return -ENOIOCTLCMD;
  1457. }
  1458. }
  1459. </programlisting>
  1460. <para>
  1461. We don't need to support any other ioctls, so if we get this far, it is time
  1462. to tell the video layer that we don't now what the user is talking about.
  1463. </para>
  1464. </sect1>
  1465. <sect1 id="endvid">
  1466. <title>Other Functionality</title>
  1467. <para>
  1468. The Video4Linux layer supports additional features, including a high
  1469. performance mmap() based capture mode and capturing part of the image.
  1470. These features are out of the scope of the book. You should however have enough
  1471. example code to implement most simple video4linux devices for radio and TV
  1472. cards.
  1473. </para>
  1474. </sect1>
  1475. </chapter>
  1476. <chapter id="bugs">
  1477. <title>Known Bugs And Assumptions</title>
  1478. <para>
  1479. <variablelist>
  1480. <varlistentry><term>Multiple Opens</term>
  1481. <listitem>
  1482. <para>
  1483. The driver assumes multiple opens should not be allowed. A driver
  1484. can work around this but not cleanly.
  1485. </para>
  1486. </listitem></varlistentry>
  1487. <varlistentry><term>API Deficiencies</term>
  1488. <listitem>
  1489. <para>
  1490. The existing API poorly reflects compression capable devices. There
  1491. are plans afoot to merge V4L, V4L2 and some other ideas into a
  1492. better interface.
  1493. </para>
  1494. </listitem></varlistentry>
  1495. </variablelist>
  1496. </para>
  1497. </chapter>
  1498. <chapter id="pubfunctions">
  1499. <title>Public Functions Provided</title>
  1500. !Edrivers/media/video/v4l2-dev.c
  1501. </chapter>
  1502. </book>