em28xx-video.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  1. /*
  2. em28xx-video.c - driver for Empia EM2800/EM2820/2840 USB
  3. video capture devices
  4. Copyright (C) 2005 Ludovico Cavedon <cavedon@sssup.it>
  5. Markus Rechberger <mrechberger@gmail.com>
  6. Mauro Carvalho Chehab <mchehab@infradead.org>
  7. Sascha Sommer <saschasommer@freenet.de>
  8. Some parts based on SN9C10x PC Camera Controllers GPL driver made
  9. by Luca Risolia <luca.risolia@studio.unibo.it>
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <linux/init.h>
  23. #include <linux/list.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/bitmap.h>
  27. #include <linux/usb.h>
  28. #include <linux/i2c.h>
  29. #include <linux/version.h>
  30. #include <linux/mm.h>
  31. #include <linux/mutex.h>
  32. #include "em28xx.h"
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include <media/v4l2-chip-ident.h>
  36. #include <media/msp3400.h>
  37. #include <media/tuner.h>
  38. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  39. "Markus Rechberger <mrechberger@gmail.com>, " \
  40. "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
  41. "Sascha Sommer <saschasommer@freenet.de>"
  42. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  43. #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 2)
  44. #define em28xx_videodbg(fmt, arg...) do {\
  45. if (video_debug) \
  46. printk(KERN_INFO "%s %s :"fmt, \
  47. dev->name, __func__ , ##arg); } while (0)
  48. static unsigned int isoc_debug;
  49. module_param(isoc_debug, int, 0644);
  50. MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
  51. #define em28xx_isocdbg(fmt, arg...) \
  52. do {\
  53. if (isoc_debug) { \
  54. printk(KERN_INFO "%s %s :"fmt, \
  55. dev->name, __func__ , ##arg); \
  56. } \
  57. } while (0)
  58. MODULE_AUTHOR(DRIVER_AUTHOR);
  59. MODULE_DESCRIPTION(DRIVER_DESC);
  60. MODULE_LICENSE("GPL");
  61. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  62. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  63. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  64. module_param_array(video_nr, int, NULL, 0444);
  65. module_param_array(vbi_nr, int, NULL, 0444);
  66. module_param_array(radio_nr, int, NULL, 0444);
  67. MODULE_PARM_DESC(video_nr, "video device numbers");
  68. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  69. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  70. static unsigned int video_debug;
  71. module_param(video_debug, int, 0644);
  72. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  73. /* supported video standards */
  74. static struct em28xx_fmt format[] = {
  75. {
  76. .name = "16 bpp YUY2, 4:2:2, packed",
  77. .fourcc = V4L2_PIX_FMT_YUYV,
  78. .depth = 16,
  79. .reg = EM28XX_OUTFMT_YUV422_Y0UY1V,
  80. }, {
  81. .name = "16 bpp RGB 565, LE",
  82. .fourcc = V4L2_PIX_FMT_RGB565,
  83. .depth = 16,
  84. .reg = EM28XX_OUTFMT_RGB_16_656,
  85. }, {
  86. .name = "8 bpp Bayer BGBG..GRGR",
  87. .fourcc = V4L2_PIX_FMT_SBGGR8,
  88. .depth = 8,
  89. .reg = EM28XX_OUTFMT_RGB_8_BGBG,
  90. }, {
  91. .name = "8 bpp Bayer GRGR..BGBG",
  92. .fourcc = V4L2_PIX_FMT_SGRBG8,
  93. .depth = 8,
  94. .reg = EM28XX_OUTFMT_RGB_8_GRGR,
  95. }, {
  96. .name = "8 bpp Bayer GBGB..RGRG",
  97. .fourcc = V4L2_PIX_FMT_SGBRG8,
  98. .depth = 8,
  99. .reg = EM28XX_OUTFMT_RGB_8_GBGB,
  100. }, {
  101. .name = "12 bpp YUV411",
  102. .fourcc = V4L2_PIX_FMT_YUV411P,
  103. .depth = 12,
  104. .reg = EM28XX_OUTFMT_YUV411,
  105. },
  106. };
  107. /* supported controls */
  108. /* Common to all boards */
  109. static struct v4l2_queryctrl em28xx_qctrl[] = {
  110. {
  111. .id = V4L2_CID_AUDIO_VOLUME,
  112. .type = V4L2_CTRL_TYPE_INTEGER,
  113. .name = "Volume",
  114. .minimum = 0x0,
  115. .maximum = 0x1f,
  116. .step = 0x1,
  117. .default_value = 0x1f,
  118. .flags = 0,
  119. }, {
  120. .id = V4L2_CID_AUDIO_MUTE,
  121. .type = V4L2_CTRL_TYPE_BOOLEAN,
  122. .name = "Mute",
  123. .minimum = 0,
  124. .maximum = 1,
  125. .step = 1,
  126. .default_value = 1,
  127. .flags = 0,
  128. }
  129. };
  130. /* ------------------------------------------------------------------
  131. DMA and thread functions
  132. ------------------------------------------------------------------*/
  133. /*
  134. * Announces that a buffer were filled and request the next
  135. */
  136. static inline void buffer_filled(struct em28xx *dev,
  137. struct em28xx_dmaqueue *dma_q,
  138. struct em28xx_buffer *buf)
  139. {
  140. /* Advice that buffer was filled */
  141. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  142. buf->vb.state = VIDEOBUF_DONE;
  143. buf->vb.field_count++;
  144. do_gettimeofday(&buf->vb.ts);
  145. dev->isoc_ctl.buf = NULL;
  146. list_del(&buf->vb.queue);
  147. wake_up(&buf->vb.done);
  148. }
  149. /*
  150. * Identify the buffer header type and properly handles
  151. */
  152. static void em28xx_copy_video(struct em28xx *dev,
  153. struct em28xx_dmaqueue *dma_q,
  154. struct em28xx_buffer *buf,
  155. unsigned char *p,
  156. unsigned char *outp, unsigned long len)
  157. {
  158. void *fieldstart, *startwrite, *startread;
  159. int linesdone, currlinedone, offset, lencopy, remain;
  160. int bytesperline = dev->width << 1;
  161. if (dma_q->pos + len > buf->vb.size)
  162. len = buf->vb.size - dma_q->pos;
  163. if (p[0] != 0x88 && p[0] != 0x22) {
  164. em28xx_isocdbg("frame is not complete\n");
  165. len += 4;
  166. } else
  167. p += 4;
  168. startread = p;
  169. remain = len;
  170. /* Interlaces frame */
  171. if (buf->top_field)
  172. fieldstart = outp;
  173. else
  174. fieldstart = outp + bytesperline;
  175. linesdone = dma_q->pos / bytesperline;
  176. currlinedone = dma_q->pos % bytesperline;
  177. offset = linesdone * bytesperline * 2 + currlinedone;
  178. startwrite = fieldstart + offset;
  179. lencopy = bytesperline - currlinedone;
  180. lencopy = lencopy > remain ? remain : lencopy;
  181. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  182. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  183. ((char *)startwrite + lencopy) -
  184. ((char *)outp + buf->vb.size));
  185. remain = (char *)outp + buf->vb.size - (char *)startwrite;
  186. lencopy = remain;
  187. }
  188. if (lencopy <= 0)
  189. return;
  190. memcpy(startwrite, startread, lencopy);
  191. remain -= lencopy;
  192. while (remain > 0) {
  193. startwrite += lencopy + bytesperline;
  194. startread += lencopy;
  195. if (bytesperline > remain)
  196. lencopy = remain;
  197. else
  198. lencopy = bytesperline;
  199. if ((char *)startwrite + lencopy > (char *)outp +
  200. buf->vb.size) {
  201. em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
  202. ((char *)startwrite + lencopy) -
  203. ((char *)outp + buf->vb.size));
  204. lencopy = remain = (char *)outp + buf->vb.size -
  205. (char *)startwrite;
  206. }
  207. if (lencopy <= 0)
  208. break;
  209. memcpy(startwrite, startread, lencopy);
  210. remain -= lencopy;
  211. }
  212. dma_q->pos += len;
  213. }
  214. static inline void print_err_status(struct em28xx *dev,
  215. int packet, int status)
  216. {
  217. char *errmsg = "Unknown";
  218. switch (status) {
  219. case -ENOENT:
  220. errmsg = "unlinked synchronuously";
  221. break;
  222. case -ECONNRESET:
  223. errmsg = "unlinked asynchronuously";
  224. break;
  225. case -ENOSR:
  226. errmsg = "Buffer error (overrun)";
  227. break;
  228. case -EPIPE:
  229. errmsg = "Stalled (device not responding)";
  230. break;
  231. case -EOVERFLOW:
  232. errmsg = "Babble (bad cable?)";
  233. break;
  234. case -EPROTO:
  235. errmsg = "Bit-stuff error (bad cable?)";
  236. break;
  237. case -EILSEQ:
  238. errmsg = "CRC/Timeout (could be anything)";
  239. break;
  240. case -ETIME:
  241. errmsg = "Device does not respond";
  242. break;
  243. }
  244. if (packet < 0) {
  245. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  246. } else {
  247. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  248. packet, status, errmsg);
  249. }
  250. }
  251. /*
  252. * video-buf generic routine to get the next available buffer
  253. */
  254. static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
  255. struct em28xx_buffer **buf)
  256. {
  257. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  258. char *outp;
  259. if (list_empty(&dma_q->active)) {
  260. em28xx_isocdbg("No active queue to serve\n");
  261. dev->isoc_ctl.buf = NULL;
  262. *buf = NULL;
  263. return;
  264. }
  265. /* Get the next buffer */
  266. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  267. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  268. outp = videobuf_to_vmalloc(&(*buf)->vb);
  269. memset(outp, 0, (*buf)->vb.size);
  270. dev->isoc_ctl.buf = *buf;
  271. return;
  272. }
  273. /*
  274. * Controls the isoc copy of each urb packet
  275. */
  276. static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
  277. {
  278. struct em28xx_buffer *buf;
  279. struct em28xx_dmaqueue *dma_q = urb->context;
  280. unsigned char *outp = NULL;
  281. int i, len = 0, rc = 1;
  282. unsigned char *p;
  283. if (!dev)
  284. return 0;
  285. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  286. return 0;
  287. if (urb->status < 0) {
  288. print_err_status(dev, -1, urb->status);
  289. if (urb->status == -ENOENT)
  290. return 0;
  291. }
  292. buf = dev->isoc_ctl.buf;
  293. if (buf != NULL)
  294. outp = videobuf_to_vmalloc(&buf->vb);
  295. for (i = 0; i < urb->number_of_packets; i++) {
  296. int status = urb->iso_frame_desc[i].status;
  297. if (status < 0) {
  298. print_err_status(dev, i, status);
  299. if (urb->iso_frame_desc[i].status != -EPROTO)
  300. continue;
  301. }
  302. len = urb->iso_frame_desc[i].actual_length - 4;
  303. if (urb->iso_frame_desc[i].actual_length <= 0) {
  304. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  305. continue;
  306. }
  307. if (urb->iso_frame_desc[i].actual_length >
  308. dev->max_pkt_size) {
  309. em28xx_isocdbg("packet bigger than packet size");
  310. continue;
  311. }
  312. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  313. /* FIXME: incomplete buffer checks where removed to make
  314. logic simpler. Impacts of those changes should be evaluated
  315. */
  316. if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
  317. em28xx_isocdbg("VBI HEADER!!!\n");
  318. /* FIXME: Should add vbi copy */
  319. continue;
  320. }
  321. if (p[0] == 0x22 && p[1] == 0x5a) {
  322. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  323. len, (p[2] & 1) ? "odd" : "even");
  324. if (!(p[2] & 1)) {
  325. if (buf != NULL)
  326. buffer_filled(dev, dma_q, buf);
  327. get_next_buf(dma_q, &buf);
  328. if (buf == NULL)
  329. outp = NULL;
  330. else
  331. outp = videobuf_to_vmalloc(&buf->vb);
  332. }
  333. if (buf != NULL) {
  334. if (p[2] & 1)
  335. buf->top_field = 0;
  336. else
  337. buf->top_field = 1;
  338. }
  339. dma_q->pos = 0;
  340. }
  341. if (buf != NULL)
  342. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  343. }
  344. return rc;
  345. }
  346. /* ------------------------------------------------------------------
  347. Videobuf operations
  348. ------------------------------------------------------------------*/
  349. static int
  350. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  351. {
  352. struct em28xx_fh *fh = vq->priv_data;
  353. struct em28xx *dev = fh->dev;
  354. struct v4l2_frequency f;
  355. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
  356. if (0 == *count)
  357. *count = EM28XX_DEF_BUF;
  358. if (*count < EM28XX_MIN_BUF)
  359. *count = EM28XX_MIN_BUF;
  360. /* Ask tuner to go to analog or radio mode */
  361. memset(&f, 0, sizeof(f));
  362. f.frequency = dev->ctl_freq;
  363. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  364. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f);
  365. return 0;
  366. }
  367. /* This is called *without* dev->slock held; please keep it that way */
  368. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  369. {
  370. struct em28xx_fh *fh = vq->priv_data;
  371. struct em28xx *dev = fh->dev;
  372. unsigned long flags = 0;
  373. if (in_interrupt())
  374. BUG();
  375. /* We used to wait for the buffer to finish here, but this didn't work
  376. because, as we were keeping the state as VIDEOBUF_QUEUED,
  377. videobuf_queue_cancel marked it as finished for us.
  378. (Also, it could wedge forever if the hardware was misconfigured.)
  379. This should be safe; by the time we get here, the buffer isn't
  380. queued anymore. If we ever start marking the buffers as
  381. VIDEOBUF_ACTIVE, it won't be, though.
  382. */
  383. spin_lock_irqsave(&dev->slock, flags);
  384. if (dev->isoc_ctl.buf == buf)
  385. dev->isoc_ctl.buf = NULL;
  386. spin_unlock_irqrestore(&dev->slock, flags);
  387. videobuf_vmalloc_free(&buf->vb);
  388. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  389. }
  390. static int
  391. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  392. enum v4l2_field field)
  393. {
  394. struct em28xx_fh *fh = vq->priv_data;
  395. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  396. struct em28xx *dev = fh->dev;
  397. int rc = 0, urb_init = 0;
  398. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
  399. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  400. return -EINVAL;
  401. buf->vb.width = dev->width;
  402. buf->vb.height = dev->height;
  403. buf->vb.field = field;
  404. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  405. rc = videobuf_iolock(vq, &buf->vb, NULL);
  406. if (rc < 0)
  407. goto fail;
  408. }
  409. if (!dev->isoc_ctl.num_bufs)
  410. urb_init = 1;
  411. if (urb_init) {
  412. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  413. EM28XX_NUM_BUFS, dev->max_pkt_size,
  414. em28xx_isoc_copy);
  415. if (rc < 0)
  416. goto fail;
  417. }
  418. buf->vb.state = VIDEOBUF_PREPARED;
  419. return 0;
  420. fail:
  421. free_buffer(vq, buf);
  422. return rc;
  423. }
  424. static void
  425. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  426. {
  427. struct em28xx_buffer *buf = container_of(vb,
  428. struct em28xx_buffer,
  429. vb);
  430. struct em28xx_fh *fh = vq->priv_data;
  431. struct em28xx *dev = fh->dev;
  432. struct em28xx_dmaqueue *vidq = &dev->vidq;
  433. buf->vb.state = VIDEOBUF_QUEUED;
  434. list_add_tail(&buf->vb.queue, &vidq->active);
  435. }
  436. static void buffer_release(struct videobuf_queue *vq,
  437. struct videobuf_buffer *vb)
  438. {
  439. struct em28xx_buffer *buf = container_of(vb,
  440. struct em28xx_buffer,
  441. vb);
  442. struct em28xx_fh *fh = vq->priv_data;
  443. struct em28xx *dev = (struct em28xx *)fh->dev;
  444. em28xx_isocdbg("em28xx: called buffer_release\n");
  445. free_buffer(vq, buf);
  446. }
  447. static struct videobuf_queue_ops em28xx_video_qops = {
  448. .buf_setup = buffer_setup,
  449. .buf_prepare = buffer_prepare,
  450. .buf_queue = buffer_queue,
  451. .buf_release = buffer_release,
  452. };
  453. /********************* v4l2 interface **************************************/
  454. static void video_mux(struct em28xx *dev, int index)
  455. {
  456. dev->ctl_input = index;
  457. dev->ctl_ainput = INPUT(index)->amux;
  458. dev->ctl_aoutput = INPUT(index)->aout;
  459. if (!dev->ctl_aoutput)
  460. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  461. v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_routing,
  462. INPUT(index)->vmux, 0, 0);
  463. if (dev->board.has_msp34xx) {
  464. if (dev->i2s_speed) {
  465. v4l2_device_call_all(&dev->v4l2_dev, 0, audio,
  466. s_i2s_clock_freq, dev->i2s_speed);
  467. }
  468. /* Note: this is msp3400 specific */
  469. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  470. dev->ctl_ainput, MSP_OUTPUT(MSP_SC_IN_DSP_SCART1), 0);
  471. }
  472. if (dev->board.adecoder != EM28XX_NOADECODER) {
  473. v4l2_device_call_all(&dev->v4l2_dev, 0, audio, s_routing,
  474. dev->ctl_ainput, dev->ctl_aoutput, 0);
  475. }
  476. em28xx_audio_analog_set(dev);
  477. }
  478. /* Usage lock check functions */
  479. static int res_get(struct em28xx_fh *fh)
  480. {
  481. struct em28xx *dev = fh->dev;
  482. int rc = 0;
  483. /* This instance already has stream_on */
  484. if (fh->stream_on)
  485. return rc;
  486. if (dev->stream_on)
  487. return -EBUSY;
  488. dev->stream_on = 1;
  489. fh->stream_on = 1;
  490. return rc;
  491. }
  492. static int res_check(struct em28xx_fh *fh)
  493. {
  494. return fh->stream_on;
  495. }
  496. static void res_free(struct em28xx_fh *fh)
  497. {
  498. struct em28xx *dev = fh->dev;
  499. fh->stream_on = 0;
  500. dev->stream_on = 0;
  501. }
  502. /*
  503. * em28xx_get_ctrl()
  504. * return the current saturation, brightness or contrast, mute state
  505. */
  506. static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  507. {
  508. switch (ctrl->id) {
  509. case V4L2_CID_AUDIO_MUTE:
  510. ctrl->value = dev->mute;
  511. return 0;
  512. case V4L2_CID_AUDIO_VOLUME:
  513. ctrl->value = dev->volume;
  514. return 0;
  515. default:
  516. return -EINVAL;
  517. }
  518. }
  519. /*
  520. * em28xx_set_ctrl()
  521. * mute or set new saturation, brightness or contrast
  522. */
  523. static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  524. {
  525. switch (ctrl->id) {
  526. case V4L2_CID_AUDIO_MUTE:
  527. if (ctrl->value != dev->mute) {
  528. dev->mute = ctrl->value;
  529. return em28xx_audio_analog_set(dev);
  530. }
  531. return 0;
  532. case V4L2_CID_AUDIO_VOLUME:
  533. dev->volume = ctrl->value;
  534. return em28xx_audio_analog_set(dev);
  535. default:
  536. return -EINVAL;
  537. }
  538. }
  539. static int check_dev(struct em28xx *dev)
  540. {
  541. if (dev->state & DEV_DISCONNECTED) {
  542. em28xx_errdev("v4l2 ioctl: device not present\n");
  543. return -ENODEV;
  544. }
  545. if (dev->state & DEV_MISCONFIGURED) {
  546. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  547. "close and open it again\n");
  548. return -EIO;
  549. }
  550. return 0;
  551. }
  552. static void get_scale(struct em28xx *dev,
  553. unsigned int width, unsigned int height,
  554. unsigned int *hscale, unsigned int *vscale)
  555. {
  556. unsigned int maxw = norm_maxw(dev);
  557. unsigned int maxh = norm_maxh(dev);
  558. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  559. if (*hscale >= 0x4000)
  560. *hscale = 0x3fff;
  561. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  562. if (*vscale >= 0x4000)
  563. *vscale = 0x3fff;
  564. }
  565. /* ------------------------------------------------------------------
  566. IOCTL vidioc handling
  567. ------------------------------------------------------------------*/
  568. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  569. struct v4l2_format *f)
  570. {
  571. struct em28xx_fh *fh = priv;
  572. struct em28xx *dev = fh->dev;
  573. mutex_lock(&dev->lock);
  574. f->fmt.pix.width = dev->width;
  575. f->fmt.pix.height = dev->height;
  576. f->fmt.pix.pixelformat = dev->format->fourcc;
  577. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  578. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  579. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  580. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  581. f->fmt.pix.field = dev->interlaced ?
  582. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  583. mutex_unlock(&dev->lock);
  584. return 0;
  585. }
  586. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  587. {
  588. unsigned int i;
  589. for (i = 0; i < ARRAY_SIZE(format); i++)
  590. if (format[i].fourcc == fourcc)
  591. return &format[i];
  592. return NULL;
  593. }
  594. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  595. struct v4l2_format *f)
  596. {
  597. struct em28xx_fh *fh = priv;
  598. struct em28xx *dev = fh->dev;
  599. unsigned int width = f->fmt.pix.width;
  600. unsigned int height = f->fmt.pix.height;
  601. unsigned int maxw = norm_maxw(dev);
  602. unsigned int maxh = norm_maxh(dev);
  603. unsigned int hscale, vscale;
  604. struct em28xx_fmt *fmt;
  605. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  606. if (!fmt) {
  607. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  608. f->fmt.pix.pixelformat);
  609. return -EINVAL;
  610. }
  611. if (dev->board.is_webcam) {
  612. /* FIXME: This is the only supported fmt */
  613. width = 640;
  614. height = 480;
  615. } else if (dev->board.is_em2800) {
  616. /* the em2800 can only scale down to 50% */
  617. height = height > (3 * maxh / 4) ? maxh : maxh / 2;
  618. width = width > (3 * maxw / 4) ? maxw : maxw / 2;
  619. /* According to empiatech support the MaxPacketSize is too small
  620. * to support framesizes larger than 640x480 @ 30 fps or 640x576
  621. * @ 25 fps. As this would cut of a part of the image we prefer
  622. * 360x576 or 360x480 for now */
  623. if (width == maxw && height == maxh)
  624. width /= 2;
  625. } else {
  626. /* width must even because of the YUYV format
  627. height must be even because of interlacing */
  628. v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
  629. }
  630. get_scale(dev, width, height, &hscale, &vscale);
  631. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  632. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  633. f->fmt.pix.width = width;
  634. f->fmt.pix.height = height;
  635. f->fmt.pix.pixelformat = fmt->fourcc;
  636. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  637. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  638. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  639. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  640. return 0;
  641. }
  642. static int em28xx_set_video_format(struct em28xx *dev, unsigned int fourcc,
  643. unsigned width, unsigned height)
  644. {
  645. struct em28xx_fmt *fmt;
  646. /* FIXME: This is the only supported fmt */
  647. if (dev->board.is_webcam) {
  648. width = 640;
  649. height = 480;
  650. }
  651. fmt = format_by_fourcc(fourcc);
  652. if (!fmt)
  653. return -EINVAL;
  654. dev->format = fmt;
  655. dev->width = width;
  656. dev->height = height;
  657. /* set new image size */
  658. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  659. em28xx_set_alternate(dev);
  660. em28xx_resolution_set(dev);
  661. return 0;
  662. }
  663. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  664. struct v4l2_format *f)
  665. {
  666. struct em28xx_fh *fh = priv;
  667. struct em28xx *dev = fh->dev;
  668. int rc;
  669. rc = check_dev(dev);
  670. if (rc < 0)
  671. return rc;
  672. mutex_lock(&dev->lock);
  673. vidioc_try_fmt_vid_cap(file, priv, f);
  674. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  675. em28xx_errdev("%s queue busy\n", __func__);
  676. rc = -EBUSY;
  677. goto out;
  678. }
  679. if (dev->stream_on && !fh->stream_on) {
  680. em28xx_errdev("%s device in use by another fh\n", __func__);
  681. rc = -EBUSY;
  682. goto out;
  683. }
  684. rc = em28xx_set_video_format(dev, f->fmt.pix.pixelformat,
  685. f->fmt.pix.width, f->fmt.pix.height);
  686. out:
  687. mutex_unlock(&dev->lock);
  688. return rc;
  689. }
  690. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *norm)
  691. {
  692. struct em28xx_fh *fh = priv;
  693. struct em28xx *dev = fh->dev;
  694. struct v4l2_format f;
  695. int rc;
  696. rc = check_dev(dev);
  697. if (rc < 0)
  698. return rc;
  699. mutex_lock(&dev->lock);
  700. dev->norm = *norm;
  701. /* Adjusts width/height, if needed */
  702. f.fmt.pix.width = dev->width;
  703. f.fmt.pix.height = dev->height;
  704. vidioc_try_fmt_vid_cap(file, priv, &f);
  705. /* set new image size */
  706. dev->width = f.fmt.pix.width;
  707. dev->height = f.fmt.pix.height;
  708. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  709. em28xx_resolution_set(dev);
  710. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm);
  711. mutex_unlock(&dev->lock);
  712. return 0;
  713. }
  714. static const char *iname[] = {
  715. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  716. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  717. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  718. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  719. [EM28XX_VMUX_SVIDEO] = "S-Video",
  720. [EM28XX_VMUX_TELEVISION] = "Television",
  721. [EM28XX_VMUX_CABLE] = "Cable TV",
  722. [EM28XX_VMUX_DVB] = "DVB",
  723. [EM28XX_VMUX_DEBUG] = "for debug only",
  724. };
  725. static int vidioc_enum_input(struct file *file, void *priv,
  726. struct v4l2_input *i)
  727. {
  728. struct em28xx_fh *fh = priv;
  729. struct em28xx *dev = fh->dev;
  730. unsigned int n;
  731. n = i->index;
  732. if (n >= MAX_EM28XX_INPUT)
  733. return -EINVAL;
  734. if (0 == INPUT(n)->type)
  735. return -EINVAL;
  736. i->index = n;
  737. i->type = V4L2_INPUT_TYPE_CAMERA;
  738. strcpy(i->name, iname[INPUT(n)->type]);
  739. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  740. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  741. i->type = V4L2_INPUT_TYPE_TUNER;
  742. i->std = dev->vdev->tvnorms;
  743. return 0;
  744. }
  745. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  746. {
  747. struct em28xx_fh *fh = priv;
  748. struct em28xx *dev = fh->dev;
  749. *i = dev->ctl_input;
  750. return 0;
  751. }
  752. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  753. {
  754. struct em28xx_fh *fh = priv;
  755. struct em28xx *dev = fh->dev;
  756. int rc;
  757. rc = check_dev(dev);
  758. if (rc < 0)
  759. return rc;
  760. if (i >= MAX_EM28XX_INPUT)
  761. return -EINVAL;
  762. if (0 == INPUT(i)->type)
  763. return -EINVAL;
  764. dev->ctl_input = i;
  765. mutex_lock(&dev->lock);
  766. video_mux(dev, dev->ctl_input);
  767. mutex_unlock(&dev->lock);
  768. return 0;
  769. }
  770. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  771. {
  772. struct em28xx_fh *fh = priv;
  773. struct em28xx *dev = fh->dev;
  774. switch (a->index) {
  775. case EM28XX_AMUX_VIDEO:
  776. strcpy(a->name, "Television");
  777. break;
  778. case EM28XX_AMUX_LINE_IN:
  779. strcpy(a->name, "Line In");
  780. break;
  781. case EM28XX_AMUX_VIDEO2:
  782. strcpy(a->name, "Television alt");
  783. break;
  784. case EM28XX_AMUX_PHONE:
  785. strcpy(a->name, "Phone");
  786. break;
  787. case EM28XX_AMUX_MIC:
  788. strcpy(a->name, "Mic");
  789. break;
  790. case EM28XX_AMUX_CD:
  791. strcpy(a->name, "CD");
  792. break;
  793. case EM28XX_AMUX_AUX:
  794. strcpy(a->name, "Aux");
  795. break;
  796. case EM28XX_AMUX_PCM_OUT:
  797. strcpy(a->name, "PCM");
  798. break;
  799. default:
  800. return -EINVAL;
  801. }
  802. a->index = dev->ctl_ainput;
  803. a->capability = V4L2_AUDCAP_STEREO;
  804. return 0;
  805. }
  806. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  807. {
  808. struct em28xx_fh *fh = priv;
  809. struct em28xx *dev = fh->dev;
  810. if (a->index >= MAX_EM28XX_INPUT)
  811. return -EINVAL;
  812. if (0 == INPUT(a->index)->type)
  813. return -EINVAL;
  814. mutex_lock(&dev->lock);
  815. dev->ctl_ainput = INPUT(a->index)->amux;
  816. dev->ctl_aoutput = INPUT(a->index)->aout;
  817. if (!dev->ctl_aoutput)
  818. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  819. mutex_unlock(&dev->lock);
  820. return 0;
  821. }
  822. static int vidioc_queryctrl(struct file *file, void *priv,
  823. struct v4l2_queryctrl *qc)
  824. {
  825. struct em28xx_fh *fh = priv;
  826. struct em28xx *dev = fh->dev;
  827. int id = qc->id;
  828. int i;
  829. int rc;
  830. rc = check_dev(dev);
  831. if (rc < 0)
  832. return rc;
  833. memset(qc, 0, sizeof(*qc));
  834. qc->id = id;
  835. if (!dev->board.has_msp34xx) {
  836. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  837. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  838. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  839. return 0;
  840. }
  841. }
  842. }
  843. mutex_lock(&dev->lock);
  844. v4l2_device_call_all(&dev->v4l2_dev, 0, core, queryctrl, qc);
  845. mutex_unlock(&dev->lock);
  846. if (qc->type)
  847. return 0;
  848. else
  849. return -EINVAL;
  850. }
  851. static int vidioc_g_ctrl(struct file *file, void *priv,
  852. struct v4l2_control *ctrl)
  853. {
  854. struct em28xx_fh *fh = priv;
  855. struct em28xx *dev = fh->dev;
  856. int rc;
  857. rc = check_dev(dev);
  858. if (rc < 0)
  859. return rc;
  860. rc = 0;
  861. mutex_lock(&dev->lock);
  862. if (dev->board.has_msp34xx)
  863. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  864. else {
  865. rc = em28xx_get_ctrl(dev, ctrl);
  866. if (rc < 0) {
  867. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_ctrl, ctrl);
  868. rc = 0;
  869. }
  870. }
  871. mutex_unlock(&dev->lock);
  872. return rc;
  873. }
  874. static int vidioc_s_ctrl(struct file *file, void *priv,
  875. struct v4l2_control *ctrl)
  876. {
  877. struct em28xx_fh *fh = priv;
  878. struct em28xx *dev = fh->dev;
  879. u8 i;
  880. int rc;
  881. rc = check_dev(dev);
  882. if (rc < 0)
  883. return rc;
  884. mutex_lock(&dev->lock);
  885. if (dev->board.has_msp34xx)
  886. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
  887. else {
  888. rc = 1;
  889. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  890. if (ctrl->id == em28xx_qctrl[i].id) {
  891. if (ctrl->value < em28xx_qctrl[i].minimum ||
  892. ctrl->value > em28xx_qctrl[i].maximum) {
  893. rc = -ERANGE;
  894. break;
  895. }
  896. rc = em28xx_set_ctrl(dev, ctrl);
  897. break;
  898. }
  899. }
  900. }
  901. /* Control not found - try to send it to the attached devices */
  902. if (rc == 1) {
  903. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_ctrl, ctrl);
  904. rc = 0;
  905. }
  906. mutex_unlock(&dev->lock);
  907. return rc;
  908. }
  909. static int vidioc_g_tuner(struct file *file, void *priv,
  910. struct v4l2_tuner *t)
  911. {
  912. struct em28xx_fh *fh = priv;
  913. struct em28xx *dev = fh->dev;
  914. int rc;
  915. rc = check_dev(dev);
  916. if (rc < 0)
  917. return rc;
  918. if (0 != t->index)
  919. return -EINVAL;
  920. strcpy(t->name, "Tuner");
  921. mutex_lock(&dev->lock);
  922. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  923. mutex_unlock(&dev->lock);
  924. return 0;
  925. }
  926. static int vidioc_s_tuner(struct file *file, void *priv,
  927. struct v4l2_tuner *t)
  928. {
  929. struct em28xx_fh *fh = priv;
  930. struct em28xx *dev = fh->dev;
  931. int rc;
  932. rc = check_dev(dev);
  933. if (rc < 0)
  934. return rc;
  935. if (0 != t->index)
  936. return -EINVAL;
  937. mutex_lock(&dev->lock);
  938. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  939. mutex_unlock(&dev->lock);
  940. return 0;
  941. }
  942. static int vidioc_g_frequency(struct file *file, void *priv,
  943. struct v4l2_frequency *f)
  944. {
  945. struct em28xx_fh *fh = priv;
  946. struct em28xx *dev = fh->dev;
  947. mutex_lock(&dev->lock);
  948. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  949. f->frequency = dev->ctl_freq;
  950. mutex_unlock(&dev->lock);
  951. return 0;
  952. }
  953. static int vidioc_s_frequency(struct file *file, void *priv,
  954. struct v4l2_frequency *f)
  955. {
  956. struct em28xx_fh *fh = priv;
  957. struct em28xx *dev = fh->dev;
  958. int rc;
  959. rc = check_dev(dev);
  960. if (rc < 0)
  961. return rc;
  962. if (0 != f->tuner)
  963. return -EINVAL;
  964. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  965. return -EINVAL;
  966. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  967. return -EINVAL;
  968. mutex_lock(&dev->lock);
  969. dev->ctl_freq = f->frequency;
  970. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f);
  971. mutex_unlock(&dev->lock);
  972. return 0;
  973. }
  974. #ifdef CONFIG_VIDEO_ADV_DEBUG
  975. static int em28xx_reg_len(int reg)
  976. {
  977. switch (reg) {
  978. case EM28XX_R40_AC97LSB:
  979. case EM28XX_R30_HSCALELOW:
  980. case EM28XX_R32_VSCALELOW:
  981. return 2;
  982. default:
  983. return 1;
  984. }
  985. }
  986. static int vidioc_g_chip_ident(struct file *file, void *priv,
  987. struct v4l2_dbg_chip_ident *chip)
  988. {
  989. struct em28xx_fh *fh = priv;
  990. struct em28xx *dev = fh->dev;
  991. chip->ident = V4L2_IDENT_NONE;
  992. chip->revision = 0;
  993. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip);
  994. return 0;
  995. }
  996. static int vidioc_g_register(struct file *file, void *priv,
  997. struct v4l2_dbg_register *reg)
  998. {
  999. struct em28xx_fh *fh = priv;
  1000. struct em28xx *dev = fh->dev;
  1001. int ret;
  1002. switch (reg->match.type) {
  1003. case V4L2_CHIP_MATCH_AC97:
  1004. mutex_lock(&dev->lock);
  1005. ret = em28xx_read_ac97(dev, reg->reg);
  1006. mutex_unlock(&dev->lock);
  1007. if (ret < 0)
  1008. return ret;
  1009. reg->val = ret;
  1010. reg->size = 1;
  1011. return 0;
  1012. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1013. v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg);
  1014. return 0;
  1015. case V4L2_CHIP_MATCH_I2C_ADDR:
  1016. /* Not supported yet */
  1017. return -EINVAL;
  1018. default:
  1019. if (!v4l2_chip_match_host(&reg->match))
  1020. return -EINVAL;
  1021. }
  1022. /* Match host */
  1023. reg->size = em28xx_reg_len(reg->reg);
  1024. if (reg->size == 1) {
  1025. mutex_lock(&dev->lock);
  1026. ret = em28xx_read_reg(dev, reg->reg);
  1027. mutex_unlock(&dev->lock);
  1028. if (ret < 0)
  1029. return ret;
  1030. reg->val = ret;
  1031. } else {
  1032. __le16 val = 0;
  1033. mutex_lock(&dev->lock);
  1034. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1035. reg->reg, (char *)&val, 2);
  1036. mutex_unlock(&dev->lock);
  1037. if (ret < 0)
  1038. return ret;
  1039. reg->val = le16_to_cpu(val);
  1040. }
  1041. return 0;
  1042. }
  1043. static int vidioc_s_register(struct file *file, void *priv,
  1044. struct v4l2_dbg_register *reg)
  1045. {
  1046. struct em28xx_fh *fh = priv;
  1047. struct em28xx *dev = fh->dev;
  1048. __le16 buf;
  1049. int rc;
  1050. switch (reg->match.type) {
  1051. case V4L2_CHIP_MATCH_AC97:
  1052. mutex_lock(&dev->lock);
  1053. rc = em28xx_write_ac97(dev, reg->reg, reg->val);
  1054. mutex_unlock(&dev->lock);
  1055. return rc;
  1056. case V4L2_CHIP_MATCH_I2C_DRIVER:
  1057. v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg);
  1058. return 0;
  1059. case V4L2_CHIP_MATCH_I2C_ADDR:
  1060. /* Not supported yet */
  1061. return -EINVAL;
  1062. default:
  1063. if (!v4l2_chip_match_host(&reg->match))
  1064. return -EINVAL;
  1065. }
  1066. /* Match host */
  1067. buf = cpu_to_le16(reg->val);
  1068. mutex_lock(&dev->lock);
  1069. rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1070. em28xx_reg_len(reg->reg));
  1071. mutex_unlock(&dev->lock);
  1072. return rc;
  1073. }
  1074. #endif
  1075. static int vidioc_cropcap(struct file *file, void *priv,
  1076. struct v4l2_cropcap *cc)
  1077. {
  1078. struct em28xx_fh *fh = priv;
  1079. struct em28xx *dev = fh->dev;
  1080. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1081. return -EINVAL;
  1082. cc->bounds.left = 0;
  1083. cc->bounds.top = 0;
  1084. cc->bounds.width = dev->width;
  1085. cc->bounds.height = dev->height;
  1086. cc->defrect = cc->bounds;
  1087. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1088. cc->pixelaspect.denominator = 59;
  1089. return 0;
  1090. }
  1091. static int vidioc_streamon(struct file *file, void *priv,
  1092. enum v4l2_buf_type type)
  1093. {
  1094. struct em28xx_fh *fh = priv;
  1095. struct em28xx *dev = fh->dev;
  1096. int rc;
  1097. rc = check_dev(dev);
  1098. if (rc < 0)
  1099. return rc;
  1100. mutex_lock(&dev->lock);
  1101. rc = res_get(fh);
  1102. if (likely(rc >= 0))
  1103. rc = videobuf_streamon(&fh->vb_vidq);
  1104. mutex_unlock(&dev->lock);
  1105. return rc;
  1106. }
  1107. static int vidioc_streamoff(struct file *file, void *priv,
  1108. enum v4l2_buf_type type)
  1109. {
  1110. struct em28xx_fh *fh = priv;
  1111. struct em28xx *dev = fh->dev;
  1112. int rc;
  1113. rc = check_dev(dev);
  1114. if (rc < 0)
  1115. return rc;
  1116. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1117. return -EINVAL;
  1118. if (type != fh->type)
  1119. return -EINVAL;
  1120. mutex_lock(&dev->lock);
  1121. videobuf_streamoff(&fh->vb_vidq);
  1122. res_free(fh);
  1123. mutex_unlock(&dev->lock);
  1124. return 0;
  1125. }
  1126. static int vidioc_querycap(struct file *file, void *priv,
  1127. struct v4l2_capability *cap)
  1128. {
  1129. struct em28xx_fh *fh = priv;
  1130. struct em28xx *dev = fh->dev;
  1131. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1132. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1133. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1134. cap->version = EM28XX_VERSION_CODE;
  1135. cap->capabilities =
  1136. V4L2_CAP_SLICED_VBI_CAPTURE |
  1137. V4L2_CAP_VIDEO_CAPTURE |
  1138. V4L2_CAP_AUDIO |
  1139. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1140. if (dev->tuner_type != TUNER_ABSENT)
  1141. cap->capabilities |= V4L2_CAP_TUNER;
  1142. return 0;
  1143. }
  1144. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1145. struct v4l2_fmtdesc *f)
  1146. {
  1147. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1148. return -EINVAL;
  1149. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1150. f->pixelformat = format[f->index].fourcc;
  1151. return 0;
  1152. }
  1153. /* Sliced VBI ioctls */
  1154. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1155. struct v4l2_format *f)
  1156. {
  1157. struct em28xx_fh *fh = priv;
  1158. struct em28xx *dev = fh->dev;
  1159. int rc;
  1160. rc = check_dev(dev);
  1161. if (rc < 0)
  1162. return rc;
  1163. mutex_lock(&dev->lock);
  1164. f->fmt.sliced.service_set = 0;
  1165. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1166. if (f->fmt.sliced.service_set == 0)
  1167. rc = -EINVAL;
  1168. mutex_unlock(&dev->lock);
  1169. return rc;
  1170. }
  1171. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1172. struct v4l2_format *f)
  1173. {
  1174. struct em28xx_fh *fh = priv;
  1175. struct em28xx *dev = fh->dev;
  1176. int rc;
  1177. rc = check_dev(dev);
  1178. if (rc < 0)
  1179. return rc;
  1180. mutex_lock(&dev->lock);
  1181. v4l2_device_call_all(&dev->v4l2_dev, 0, video, g_fmt, f);
  1182. mutex_unlock(&dev->lock);
  1183. if (f->fmt.sliced.service_set == 0)
  1184. return -EINVAL;
  1185. return 0;
  1186. }
  1187. static int vidioc_reqbufs(struct file *file, void *priv,
  1188. struct v4l2_requestbuffers *rb)
  1189. {
  1190. struct em28xx_fh *fh = priv;
  1191. struct em28xx *dev = fh->dev;
  1192. int rc;
  1193. rc = check_dev(dev);
  1194. if (rc < 0)
  1195. return rc;
  1196. return videobuf_reqbufs(&fh->vb_vidq, rb);
  1197. }
  1198. static int vidioc_querybuf(struct file *file, void *priv,
  1199. struct v4l2_buffer *b)
  1200. {
  1201. struct em28xx_fh *fh = priv;
  1202. struct em28xx *dev = fh->dev;
  1203. int rc;
  1204. rc = check_dev(dev);
  1205. if (rc < 0)
  1206. return rc;
  1207. return videobuf_querybuf(&fh->vb_vidq, b);
  1208. }
  1209. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1210. {
  1211. struct em28xx_fh *fh = priv;
  1212. struct em28xx *dev = fh->dev;
  1213. int rc;
  1214. rc = check_dev(dev);
  1215. if (rc < 0)
  1216. return rc;
  1217. return videobuf_qbuf(&fh->vb_vidq, b);
  1218. }
  1219. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1220. {
  1221. struct em28xx_fh *fh = priv;
  1222. struct em28xx *dev = fh->dev;
  1223. int rc;
  1224. rc = check_dev(dev);
  1225. if (rc < 0)
  1226. return rc;
  1227. return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
  1228. }
  1229. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1230. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1231. {
  1232. struct em28xx_fh *fh = priv;
  1233. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1234. }
  1235. #endif
  1236. /* ----------------------------------------------------------- */
  1237. /* RADIO ESPECIFIC IOCTLS */
  1238. /* ----------------------------------------------------------- */
  1239. static int radio_querycap(struct file *file, void *priv,
  1240. struct v4l2_capability *cap)
  1241. {
  1242. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1243. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1244. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1245. usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
  1246. cap->version = EM28XX_VERSION_CODE;
  1247. cap->capabilities = V4L2_CAP_TUNER;
  1248. return 0;
  1249. }
  1250. static int radio_g_tuner(struct file *file, void *priv,
  1251. struct v4l2_tuner *t)
  1252. {
  1253. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1254. if (unlikely(t->index > 0))
  1255. return -EINVAL;
  1256. strcpy(t->name, "Radio");
  1257. t->type = V4L2_TUNER_RADIO;
  1258. mutex_lock(&dev->lock);
  1259. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t);
  1260. mutex_unlock(&dev->lock);
  1261. return 0;
  1262. }
  1263. static int radio_enum_input(struct file *file, void *priv,
  1264. struct v4l2_input *i)
  1265. {
  1266. if (i->index != 0)
  1267. return -EINVAL;
  1268. strcpy(i->name, "Radio");
  1269. i->type = V4L2_INPUT_TYPE_TUNER;
  1270. return 0;
  1271. }
  1272. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1273. {
  1274. if (unlikely(a->index))
  1275. return -EINVAL;
  1276. strcpy(a->name, "Radio");
  1277. return 0;
  1278. }
  1279. static int radio_s_tuner(struct file *file, void *priv,
  1280. struct v4l2_tuner *t)
  1281. {
  1282. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1283. if (0 != t->index)
  1284. return -EINVAL;
  1285. mutex_lock(&dev->lock);
  1286. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t);
  1287. mutex_unlock(&dev->lock);
  1288. return 0;
  1289. }
  1290. static int radio_s_audio(struct file *file, void *fh,
  1291. struct v4l2_audio *a)
  1292. {
  1293. return 0;
  1294. }
  1295. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1296. {
  1297. return 0;
  1298. }
  1299. static int radio_queryctrl(struct file *file, void *priv,
  1300. struct v4l2_queryctrl *qc)
  1301. {
  1302. int i;
  1303. if (qc->id < V4L2_CID_BASE ||
  1304. qc->id >= V4L2_CID_LASTP1)
  1305. return -EINVAL;
  1306. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1307. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1308. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  1309. return 0;
  1310. }
  1311. }
  1312. return -EINVAL;
  1313. }
  1314. /*
  1315. * em28xx_v4l2_open()
  1316. * inits the device and starts isoc transfer
  1317. */
  1318. static int em28xx_v4l2_open(struct file *filp)
  1319. {
  1320. int minor = video_devdata(filp)->minor;
  1321. int errCode = 0, radio;
  1322. struct em28xx *dev;
  1323. enum v4l2_buf_type fh_type;
  1324. struct em28xx_fh *fh;
  1325. dev = em28xx_get_device(minor, &fh_type, &radio);
  1326. if (NULL == dev)
  1327. return -ENODEV;
  1328. mutex_lock(&dev->lock);
  1329. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1330. minor, v4l2_type_names[fh_type], dev->users);
  1331. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1332. if (!fh) {
  1333. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1334. mutex_unlock(&dev->lock);
  1335. return -ENOMEM;
  1336. }
  1337. fh->dev = dev;
  1338. fh->radio = radio;
  1339. fh->type = fh_type;
  1340. filp->private_data = fh;
  1341. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1342. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1343. em28xx_set_alternate(dev);
  1344. em28xx_resolution_set(dev);
  1345. /* Needed, since GPIO might have disabled power of
  1346. some i2c device
  1347. */
  1348. em28xx_wake_i2c(dev);
  1349. }
  1350. if (fh->radio) {
  1351. em28xx_videodbg("video_open: setting radio device\n");
  1352. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio);
  1353. }
  1354. dev->users++;
  1355. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1356. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  1357. sizeof(struct em28xx_buffer), fh);
  1358. mutex_unlock(&dev->lock);
  1359. return errCode;
  1360. }
  1361. /*
  1362. * em28xx_realease_resources()
  1363. * unregisters the v4l2,i2c and usb devices
  1364. * called when the device gets disconected or at module unload
  1365. */
  1366. void em28xx_release_analog_resources(struct em28xx *dev)
  1367. {
  1368. /*FIXME: I2C IR should be disconnected */
  1369. if (dev->radio_dev) {
  1370. if (-1 != dev->radio_dev->minor)
  1371. video_unregister_device(dev->radio_dev);
  1372. else
  1373. video_device_release(dev->radio_dev);
  1374. dev->radio_dev = NULL;
  1375. }
  1376. if (dev->vbi_dev) {
  1377. em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
  1378. dev->vbi_dev->num);
  1379. if (-1 != dev->vbi_dev->minor)
  1380. video_unregister_device(dev->vbi_dev);
  1381. else
  1382. video_device_release(dev->vbi_dev);
  1383. dev->vbi_dev = NULL;
  1384. }
  1385. if (dev->vdev) {
  1386. em28xx_info("V4L2 device /dev/video%d deregistered\n",
  1387. dev->vdev->num);
  1388. if (-1 != dev->vdev->minor)
  1389. video_unregister_device(dev->vdev);
  1390. else
  1391. video_device_release(dev->vdev);
  1392. dev->vdev = NULL;
  1393. }
  1394. }
  1395. /*
  1396. * em28xx_v4l2_close()
  1397. * stops streaming and deallocates all resources allocated by the v4l2
  1398. * calls and ioctls
  1399. */
  1400. static int em28xx_v4l2_close(struct file *filp)
  1401. {
  1402. struct em28xx_fh *fh = filp->private_data;
  1403. struct em28xx *dev = fh->dev;
  1404. int errCode;
  1405. em28xx_videodbg("users=%d\n", dev->users);
  1406. mutex_lock(&dev->lock);
  1407. if (res_check(fh))
  1408. res_free(fh);
  1409. if (dev->users == 1) {
  1410. videobuf_stop(&fh->vb_vidq);
  1411. videobuf_mmap_free(&fh->vb_vidq);
  1412. /* the device is already disconnect,
  1413. free the remaining resources */
  1414. if (dev->state & DEV_DISCONNECTED) {
  1415. em28xx_release_resources(dev);
  1416. mutex_unlock(&dev->lock);
  1417. kfree(dev);
  1418. return 0;
  1419. }
  1420. /* Save some power by putting tuner to sleep */
  1421. v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_standby);
  1422. /* do this before setting alternate! */
  1423. em28xx_uninit_isoc(dev);
  1424. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1425. /* set alternate 0 */
  1426. dev->alt = 0;
  1427. em28xx_videodbg("setting alternate 0\n");
  1428. errCode = usb_set_interface(dev->udev, 0, 0);
  1429. if (errCode < 0) {
  1430. em28xx_errdev("cannot change alternate number to "
  1431. "0 (error=%i)\n", errCode);
  1432. }
  1433. }
  1434. kfree(fh);
  1435. dev->users--;
  1436. wake_up_interruptible_nr(&dev->open, 1);
  1437. mutex_unlock(&dev->lock);
  1438. return 0;
  1439. }
  1440. /*
  1441. * em28xx_v4l2_read()
  1442. * will allocate buffers when called for the first time
  1443. */
  1444. static ssize_t
  1445. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1446. loff_t *pos)
  1447. {
  1448. struct em28xx_fh *fh = filp->private_data;
  1449. struct em28xx *dev = fh->dev;
  1450. int rc;
  1451. rc = check_dev(dev);
  1452. if (rc < 0)
  1453. return rc;
  1454. /* FIXME: read() is not prepared to allow changing the video
  1455. resolution while streaming. Seems a bug at em28xx_set_fmt
  1456. */
  1457. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1458. mutex_lock(&dev->lock);
  1459. rc = res_get(fh);
  1460. mutex_unlock(&dev->lock);
  1461. if (unlikely(rc < 0))
  1462. return rc;
  1463. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1464. filp->f_flags & O_NONBLOCK);
  1465. }
  1466. return 0;
  1467. }
  1468. /*
  1469. * em28xx_v4l2_poll()
  1470. * will allocate buffers when called for the first time
  1471. */
  1472. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table *wait)
  1473. {
  1474. struct em28xx_fh *fh = filp->private_data;
  1475. struct em28xx *dev = fh->dev;
  1476. int rc;
  1477. rc = check_dev(dev);
  1478. if (rc < 0)
  1479. return rc;
  1480. mutex_lock(&dev->lock);
  1481. rc = res_get(fh);
  1482. mutex_unlock(&dev->lock);
  1483. if (unlikely(rc < 0))
  1484. return POLLERR;
  1485. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1486. return POLLERR;
  1487. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1488. }
  1489. /*
  1490. * em28xx_v4l2_mmap()
  1491. */
  1492. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1493. {
  1494. struct em28xx_fh *fh = filp->private_data;
  1495. struct em28xx *dev = fh->dev;
  1496. int rc;
  1497. rc = check_dev(dev);
  1498. if (rc < 0)
  1499. return rc;
  1500. mutex_lock(&dev->lock);
  1501. rc = res_get(fh);
  1502. mutex_unlock(&dev->lock);
  1503. if (unlikely(rc < 0))
  1504. return rc;
  1505. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1506. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1507. (unsigned long)vma->vm_start,
  1508. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1509. rc);
  1510. return rc;
  1511. }
  1512. static const struct v4l2_file_operations em28xx_v4l_fops = {
  1513. .owner = THIS_MODULE,
  1514. .open = em28xx_v4l2_open,
  1515. .release = em28xx_v4l2_close,
  1516. .read = em28xx_v4l2_read,
  1517. .poll = em28xx_v4l2_poll,
  1518. .mmap = em28xx_v4l2_mmap,
  1519. .ioctl = video_ioctl2,
  1520. };
  1521. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1522. .vidioc_querycap = vidioc_querycap,
  1523. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1524. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1525. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1526. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1527. .vidioc_g_audio = vidioc_g_audio,
  1528. .vidioc_s_audio = vidioc_s_audio,
  1529. .vidioc_cropcap = vidioc_cropcap,
  1530. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1531. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1532. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1533. .vidioc_reqbufs = vidioc_reqbufs,
  1534. .vidioc_querybuf = vidioc_querybuf,
  1535. .vidioc_qbuf = vidioc_qbuf,
  1536. .vidioc_dqbuf = vidioc_dqbuf,
  1537. .vidioc_s_std = vidioc_s_std,
  1538. .vidioc_enum_input = vidioc_enum_input,
  1539. .vidioc_g_input = vidioc_g_input,
  1540. .vidioc_s_input = vidioc_s_input,
  1541. .vidioc_queryctrl = vidioc_queryctrl,
  1542. .vidioc_g_ctrl = vidioc_g_ctrl,
  1543. .vidioc_s_ctrl = vidioc_s_ctrl,
  1544. .vidioc_streamon = vidioc_streamon,
  1545. .vidioc_streamoff = vidioc_streamoff,
  1546. .vidioc_g_tuner = vidioc_g_tuner,
  1547. .vidioc_s_tuner = vidioc_s_tuner,
  1548. .vidioc_g_frequency = vidioc_g_frequency,
  1549. .vidioc_s_frequency = vidioc_s_frequency,
  1550. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1551. .vidioc_g_register = vidioc_g_register,
  1552. .vidioc_s_register = vidioc_s_register,
  1553. .vidioc_g_chip_ident = vidioc_g_chip_ident,
  1554. #endif
  1555. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1556. .vidiocgmbuf = vidiocgmbuf,
  1557. #endif
  1558. };
  1559. static const struct video_device em28xx_video_template = {
  1560. .fops = &em28xx_v4l_fops,
  1561. .release = video_device_release,
  1562. .ioctl_ops = &video_ioctl_ops,
  1563. .minor = -1,
  1564. .tvnorms = V4L2_STD_ALL,
  1565. .current_norm = V4L2_STD_PAL,
  1566. };
  1567. static const struct v4l2_file_operations radio_fops = {
  1568. .owner = THIS_MODULE,
  1569. .open = em28xx_v4l2_open,
  1570. .release = em28xx_v4l2_close,
  1571. .ioctl = video_ioctl2,
  1572. };
  1573. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1574. .vidioc_querycap = radio_querycap,
  1575. .vidioc_g_tuner = radio_g_tuner,
  1576. .vidioc_enum_input = radio_enum_input,
  1577. .vidioc_g_audio = radio_g_audio,
  1578. .vidioc_s_tuner = radio_s_tuner,
  1579. .vidioc_s_audio = radio_s_audio,
  1580. .vidioc_s_input = radio_s_input,
  1581. .vidioc_queryctrl = radio_queryctrl,
  1582. .vidioc_g_ctrl = vidioc_g_ctrl,
  1583. .vidioc_s_ctrl = vidioc_s_ctrl,
  1584. .vidioc_g_frequency = vidioc_g_frequency,
  1585. .vidioc_s_frequency = vidioc_s_frequency,
  1586. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1587. .vidioc_g_register = vidioc_g_register,
  1588. .vidioc_s_register = vidioc_s_register,
  1589. #endif
  1590. };
  1591. static struct video_device em28xx_radio_template = {
  1592. .name = "em28xx-radio",
  1593. .fops = &radio_fops,
  1594. .ioctl_ops = &radio_ioctl_ops,
  1595. .minor = -1,
  1596. };
  1597. /******************************** usb interface ******************************/
  1598. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1599. const struct video_device *template,
  1600. const char *type_name)
  1601. {
  1602. struct video_device *vfd;
  1603. vfd = video_device_alloc();
  1604. if (NULL == vfd)
  1605. return NULL;
  1606. *vfd = *template;
  1607. vfd->minor = -1;
  1608. vfd->v4l2_dev = &dev->v4l2_dev;
  1609. vfd->release = video_device_release;
  1610. vfd->debug = video_debug;
  1611. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1612. dev->name, type_name);
  1613. return vfd;
  1614. }
  1615. int em28xx_register_analog_devices(struct em28xx *dev)
  1616. {
  1617. u8 val;
  1618. int ret;
  1619. printk(KERN_INFO "%s: v4l2 driver version %d.%d.%d\n",
  1620. dev->name,
  1621. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1622. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1623. /* set default norm */
  1624. dev->norm = em28xx_video_template.current_norm;
  1625. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1626. dev->ctl_input = 0;
  1627. /* Analog specific initialization */
  1628. dev->format = &format[0];
  1629. em28xx_set_video_format(dev, format[0].fourcc,
  1630. norm_maxw(dev), norm_maxh(dev));
  1631. video_mux(dev, dev->ctl_input);
  1632. /* Audio defaults */
  1633. dev->mute = 1;
  1634. dev->volume = 0x1f;
  1635. /* enable vbi capturing */
  1636. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  1637. val = (u8)em28xx_read_reg(dev, EM28XX_R0F_XCLK);
  1638. em28xx_write_reg(dev, EM28XX_R0F_XCLK,
  1639. (EM28XX_XCLK_AUDIO_UNMUTE | val));
  1640. em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
  1641. em28xx_set_outfmt(dev);
  1642. em28xx_colorlevels_set_default(dev);
  1643. em28xx_compression_disable(dev);
  1644. /* allocate and fill video video_device struct */
  1645. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1646. if (!dev->vdev) {
  1647. em28xx_errdev("cannot allocate video_device.\n");
  1648. return -ENODEV;
  1649. }
  1650. /* register v4l2 video video_device */
  1651. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1652. video_nr[dev->devno]);
  1653. if (ret) {
  1654. em28xx_errdev("unable to register video device (error=%i).\n",
  1655. ret);
  1656. return ret;
  1657. }
  1658. /* Allocate and fill vbi video_device struct */
  1659. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
  1660. /* register v4l2 vbi video_device */
  1661. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1662. vbi_nr[dev->devno]);
  1663. if (ret < 0) {
  1664. em28xx_errdev("unable to register vbi device\n");
  1665. return ret;
  1666. }
  1667. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1668. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template,
  1669. "radio");
  1670. if (!dev->radio_dev) {
  1671. em28xx_errdev("cannot allocate video_device.\n");
  1672. return -ENODEV;
  1673. }
  1674. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1675. radio_nr[dev->devno]);
  1676. if (ret < 0) {
  1677. em28xx_errdev("can't register radio device\n");
  1678. return ret;
  1679. }
  1680. em28xx_info("Registered radio device as /dev/radio%d\n",
  1681. dev->radio_dev->num);
  1682. }
  1683. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1684. dev->vdev->num, dev->vbi_dev->num);
  1685. return 0;
  1686. }