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