em28xx-video.c 59 KB

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