em28xx-video.c 48 KB

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