videobook.tmpl 62 KB

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