em28xx-video.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514
  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/msp3400.h>
  36. #include <media/tuner.h>
  37. #define DRIVER_AUTHOR "Ludovico Cavedon <cavedon@sssup.it>, " \
  38. "Markus Rechberger <mrechberger@gmail.com>, " \
  39. "Mauro Carvalho Chehab <mchehab@infradead.org>, " \
  40. "Sascha Sommer <saschasommer@freenet.de>"
  41. #define DRIVER_NAME "em28xx"
  42. #define DRIVER_DESC "Empia em28xx based USB video device driver"
  43. #define EM28XX_VERSION_CODE KERNEL_VERSION(0, 1, 0)
  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 LIST_HEAD(em28xx_devlist);
  62. static DEFINE_MUTEX(em28xx_devlist_mutex);
  63. static unsigned int card[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  64. static unsigned int video_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  65. static unsigned int vbi_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  66. static unsigned int radio_nr[] = {[0 ... (EM28XX_MAXBOARDS - 1)] = UNSET };
  67. module_param_array(card, int, NULL, 0444);
  68. module_param_array(video_nr, int, NULL, 0444);
  69. module_param_array(vbi_nr, int, NULL, 0444);
  70. module_param_array(radio_nr, int, NULL, 0444);
  71. MODULE_PARM_DESC(card, "card type");
  72. MODULE_PARM_DESC(video_nr, "video device numbers");
  73. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  74. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  75. static unsigned int video_debug;
  76. module_param(video_debug, int, 0644);
  77. MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
  78. /* Bitmask marking allocated devices from 0 to EM28XX_MAXBOARDS */
  79. static unsigned long em28xx_devused;
  80. /* supported video standards */
  81. static struct em28xx_fmt format[] = {
  82. {
  83. .name = "16bpp YUY2, 4:2:2, packed",
  84. .fourcc = V4L2_PIX_FMT_YUYV,
  85. .depth = 16,
  86. .reg = 0x14,
  87. },
  88. };
  89. /* supported controls */
  90. /* Common to all boards */
  91. static struct v4l2_queryctrl em28xx_qctrl[] = {
  92. {
  93. .id = V4L2_CID_AUDIO_VOLUME,
  94. .type = V4L2_CTRL_TYPE_INTEGER,
  95. .name = "Volume",
  96. .minimum = 0x0,
  97. .maximum = 0x1f,
  98. .step = 0x1,
  99. .default_value = 0x1f,
  100. .flags = 0,
  101. }, {
  102. .id = V4L2_CID_AUDIO_MUTE,
  103. .type = V4L2_CTRL_TYPE_BOOLEAN,
  104. .name = "Mute",
  105. .minimum = 0,
  106. .maximum = 1,
  107. .step = 1,
  108. .default_value = 1,
  109. .flags = 0,
  110. }
  111. };
  112. static struct usb_driver em28xx_usb_driver;
  113. /* ------------------------------------------------------------------
  114. DMA and thread functions
  115. ------------------------------------------------------------------*/
  116. /*
  117. * Announces that a buffer were filled and request the next
  118. */
  119. static inline void buffer_filled(struct em28xx *dev,
  120. struct em28xx_dmaqueue *dma_q,
  121. struct em28xx_buffer *buf)
  122. {
  123. /* Advice that buffer was filled */
  124. em28xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
  125. buf->vb.state = VIDEOBUF_DONE;
  126. buf->vb.field_count++;
  127. do_gettimeofday(&buf->vb.ts);
  128. dev->isoc_ctl.buf = NULL;
  129. list_del(&buf->vb.queue);
  130. wake_up(&buf->vb.done);
  131. }
  132. /*
  133. * Identify the buffer header type and properly handles
  134. */
  135. static void em28xx_copy_video(struct em28xx *dev,
  136. struct em28xx_dmaqueue *dma_q,
  137. struct em28xx_buffer *buf,
  138. unsigned char *p,
  139. unsigned char *outp, unsigned long len)
  140. {
  141. void *fieldstart, *startwrite, *startread;
  142. int linesdone, currlinedone, offset, lencopy, remain;
  143. int bytesperline = dev->width << 1;
  144. if (dma_q->pos + len > buf->vb.size)
  145. len = buf->vb.size - dma_q->pos;
  146. if (p[0] != 0x88 && p[0] != 0x22) {
  147. em28xx_isocdbg("frame is not complete\n");
  148. len += 4;
  149. } else
  150. p += 4;
  151. startread = p;
  152. remain = len;
  153. /* Interlaces frame */
  154. if (buf->top_field)
  155. fieldstart = outp;
  156. else
  157. fieldstart = outp + bytesperline;
  158. linesdone = dma_q->pos / bytesperline;
  159. currlinedone = dma_q->pos % bytesperline;
  160. offset = linesdone * bytesperline * 2 + currlinedone;
  161. startwrite = fieldstart + offset;
  162. lencopy = bytesperline - currlinedone;
  163. lencopy = lencopy > remain ? remain : lencopy;
  164. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  165. em28xx_isocdbg("Overflow of %zi bytes past buffer end (1)\n",
  166. ((char *)startwrite + lencopy) -
  167. ((char *)outp + buf->vb.size));
  168. lencopy = remain = (char *)outp + buf->vb.size - (char *)startwrite;
  169. }
  170. if (lencopy <= 0)
  171. return;
  172. memcpy(startwrite, startread, lencopy);
  173. remain -= lencopy;
  174. while (remain > 0) {
  175. startwrite += lencopy + bytesperline;
  176. startread += lencopy;
  177. if (bytesperline > remain)
  178. lencopy = remain;
  179. else
  180. lencopy = bytesperline;
  181. if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) {
  182. em28xx_isocdbg("Overflow of %zi bytes past buffer end (2)\n",
  183. ((char *)startwrite + lencopy) -
  184. ((char *)outp + buf->vb.size));
  185. lencopy = remain = (char *)outp + buf->vb.size -
  186. (char *)startwrite;
  187. }
  188. if (lencopy <= 0)
  189. break;
  190. memcpy(startwrite, startread, lencopy);
  191. remain -= lencopy;
  192. }
  193. dma_q->pos += len;
  194. }
  195. static inline void print_err_status(struct em28xx *dev,
  196. int packet, int status)
  197. {
  198. char *errmsg = "Unknown";
  199. switch (status) {
  200. case -ENOENT:
  201. errmsg = "unlinked synchronuously";
  202. break;
  203. case -ECONNRESET:
  204. errmsg = "unlinked asynchronuously";
  205. break;
  206. case -ENOSR:
  207. errmsg = "Buffer error (overrun)";
  208. break;
  209. case -EPIPE:
  210. errmsg = "Stalled (device not responding)";
  211. break;
  212. case -EOVERFLOW:
  213. errmsg = "Babble (bad cable?)";
  214. break;
  215. case -EPROTO:
  216. errmsg = "Bit-stuff error (bad cable?)";
  217. break;
  218. case -EILSEQ:
  219. errmsg = "CRC/Timeout (could be anything)";
  220. break;
  221. case -ETIME:
  222. errmsg = "Device does not respond";
  223. break;
  224. }
  225. if (packet < 0) {
  226. em28xx_isocdbg("URB status %d [%s].\n", status, errmsg);
  227. } else {
  228. em28xx_isocdbg("URB packet %d, status %d [%s].\n",
  229. packet, status, errmsg);
  230. }
  231. }
  232. /*
  233. * video-buf generic routine to get the next available buffer
  234. */
  235. static inline void get_next_buf(struct em28xx_dmaqueue *dma_q,
  236. struct em28xx_buffer **buf)
  237. {
  238. struct em28xx *dev = container_of(dma_q, struct em28xx, vidq);
  239. char *outp;
  240. if (list_empty(&dma_q->active)) {
  241. em28xx_isocdbg("No active queue to serve\n");
  242. dev->isoc_ctl.buf = NULL;
  243. *buf = NULL;
  244. return;
  245. }
  246. /* Get the next buffer */
  247. *buf = list_entry(dma_q->active.next, struct em28xx_buffer, vb.queue);
  248. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  249. outp = videobuf_to_vmalloc(&(*buf)->vb);
  250. memset(outp, 0, (*buf)->vb.size);
  251. dev->isoc_ctl.buf = *buf;
  252. return;
  253. }
  254. /*
  255. * Controls the isoc copy of each urb packet
  256. */
  257. static inline int em28xx_isoc_copy(struct em28xx *dev, struct urb *urb)
  258. {
  259. struct em28xx_buffer *buf;
  260. struct em28xx_dmaqueue *dma_q = urb->context;
  261. unsigned char *outp = NULL;
  262. int i, len = 0, rc = 1;
  263. unsigned char *p;
  264. if (!dev)
  265. return 0;
  266. if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
  267. return 0;
  268. if (urb->status < 0) {
  269. print_err_status(dev, -1, urb->status);
  270. if (urb->status == -ENOENT)
  271. return 0;
  272. }
  273. buf = dev->isoc_ctl.buf;
  274. if (buf != NULL)
  275. outp = videobuf_to_vmalloc(&buf->vb);
  276. for (i = 0; i < urb->number_of_packets; i++) {
  277. int status = urb->iso_frame_desc[i].status;
  278. if (status < 0) {
  279. print_err_status(dev, i, status);
  280. if (urb->iso_frame_desc[i].status != -EPROTO)
  281. continue;
  282. }
  283. len = urb->iso_frame_desc[i].actual_length - 4;
  284. if (urb->iso_frame_desc[i].actual_length <= 0) {
  285. /* em28xx_isocdbg("packet %d is empty",i); - spammy */
  286. continue;
  287. }
  288. if (urb->iso_frame_desc[i].actual_length >
  289. dev->max_pkt_size) {
  290. em28xx_isocdbg("packet bigger than packet size");
  291. continue;
  292. }
  293. p = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  294. /* FIXME: incomplete buffer checks where removed to make
  295. logic simpler. Impacts of those changes should be evaluated
  296. */
  297. if (p[0] == 0x33 && p[1] == 0x95 && p[2] == 0x00) {
  298. em28xx_isocdbg("VBI HEADER!!!\n");
  299. /* FIXME: Should add vbi copy */
  300. continue;
  301. }
  302. if (p[0] == 0x22 && p[1] == 0x5a) {
  303. em28xx_isocdbg("Video frame %d, length=%i, %s\n", p[2],
  304. len, (p[2] & 1)? "odd" : "even");
  305. if (!(p[2] & 1)) {
  306. if (buf != NULL)
  307. buffer_filled(dev, dma_q, buf);
  308. get_next_buf(dma_q, &buf);
  309. if (buf == NULL)
  310. outp = NULL;
  311. else
  312. outp = videobuf_to_vmalloc(&buf->vb);
  313. }
  314. if (buf != NULL) {
  315. if (p[2] & 1)
  316. buf->top_field = 0;
  317. else
  318. buf->top_field = 1;
  319. }
  320. dma_q->pos = 0;
  321. }
  322. if (buf != NULL)
  323. em28xx_copy_video(dev, dma_q, buf, p, outp, len);
  324. }
  325. return rc;
  326. }
  327. /* ------------------------------------------------------------------
  328. Videobuf operations
  329. ------------------------------------------------------------------*/
  330. static int
  331. buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
  332. {
  333. struct em28xx_fh *fh = vq->priv_data;
  334. struct em28xx *dev = fh->dev;
  335. struct v4l2_frequency f;
  336. *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
  337. if (0 == *count)
  338. *count = EM28XX_DEF_BUF;
  339. if (*count < EM28XX_MIN_BUF)
  340. *count = EM28XX_MIN_BUF;
  341. /* Ask tuner to go to analog or radio mode */
  342. memset(&f, 0, sizeof(f));
  343. f.frequency = dev->ctl_freq;
  344. f.type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  345. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, &f);
  346. return 0;
  347. }
  348. /* This is called *without* dev->slock held; please keep it that way */
  349. static void free_buffer(struct videobuf_queue *vq, struct em28xx_buffer *buf)
  350. {
  351. struct em28xx_fh *fh = vq->priv_data;
  352. struct em28xx *dev = fh->dev;
  353. unsigned long flags = 0;
  354. if (in_interrupt())
  355. BUG();
  356. /* We used to wait for the buffer to finish here, but this didn't work
  357. because, as we were keeping the state as VIDEOBUF_QUEUED,
  358. videobuf_queue_cancel marked it as finished for us.
  359. (Also, it could wedge forever if the hardware was misconfigured.)
  360. This should be safe; by the time we get here, the buffer isn't
  361. queued anymore. If we ever start marking the buffers as
  362. VIDEOBUF_ACTIVE, it won't be, though.
  363. */
  364. spin_lock_irqsave(&dev->slock, flags);
  365. if (dev->isoc_ctl.buf == buf)
  366. dev->isoc_ctl.buf = NULL;
  367. spin_unlock_irqrestore(&dev->slock, flags);
  368. videobuf_vmalloc_free(&buf->vb);
  369. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  370. }
  371. static int
  372. buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  373. enum v4l2_field field)
  374. {
  375. struct em28xx_fh *fh = vq->priv_data;
  376. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  377. struct em28xx *dev = fh->dev;
  378. int rc = 0, urb_init = 0;
  379. buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth + 7) >> 3;
  380. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  381. return -EINVAL;
  382. buf->vb.width = dev->width;
  383. buf->vb.height = dev->height;
  384. buf->vb.field = field;
  385. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  386. rc = videobuf_iolock(vq, &buf->vb, NULL);
  387. if (rc < 0)
  388. goto fail;
  389. }
  390. if (!dev->isoc_ctl.num_bufs)
  391. urb_init = 1;
  392. if (urb_init) {
  393. rc = em28xx_init_isoc(dev, EM28XX_NUM_PACKETS,
  394. EM28XX_NUM_BUFS, dev->max_pkt_size,
  395. em28xx_isoc_copy);
  396. if (rc < 0)
  397. goto fail;
  398. }
  399. buf->vb.state = VIDEOBUF_PREPARED;
  400. return 0;
  401. fail:
  402. free_buffer(vq, buf);
  403. return rc;
  404. }
  405. static void
  406. buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  407. {
  408. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  409. struct em28xx_fh *fh = vq->priv_data;
  410. struct em28xx *dev = fh->dev;
  411. struct em28xx_dmaqueue *vidq = &dev->vidq;
  412. buf->vb.state = VIDEOBUF_QUEUED;
  413. list_add_tail(&buf->vb.queue, &vidq->active);
  414. }
  415. static void buffer_release(struct videobuf_queue *vq,
  416. struct videobuf_buffer *vb)
  417. {
  418. struct em28xx_buffer *buf = container_of(vb, struct em28xx_buffer, vb);
  419. struct em28xx_fh *fh = vq->priv_data;
  420. struct em28xx *dev = (struct em28xx *)fh->dev;
  421. em28xx_isocdbg("em28xx: called buffer_release\n");
  422. free_buffer(vq, buf);
  423. }
  424. static struct videobuf_queue_ops em28xx_video_qops = {
  425. .buf_setup = buffer_setup,
  426. .buf_prepare = buffer_prepare,
  427. .buf_queue = buffer_queue,
  428. .buf_release = buffer_release,
  429. };
  430. /********************* v4l2 interface **************************************/
  431. /*
  432. * em28xx_config()
  433. * inits registers with sane defaults
  434. */
  435. static int em28xx_config(struct em28xx *dev)
  436. {
  437. int retval;
  438. /* Sets I2C speed to 100 KHz */
  439. if (!dev->board.is_em2800) {
  440. retval = em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
  441. if (retval < 0) {
  442. em28xx_errdev("%s: em28xx_write_regs_req failed! retval [%d]\n",
  443. __func__, retval);
  444. return retval;
  445. }
  446. }
  447. /* enable vbi capturing */
  448. /* em28xx_write_reg(dev, EM28XX_R0E_AUDIOSRC, 0xc0); audio register */
  449. /* em28xx_write_reg(dev, EM28XX_R0F_XCLK, 0x80); clk register */
  450. em28xx_write_reg(dev, EM28XX_R11_VINCTRL, 0x51);
  451. dev->mute = 1; /* maybe not the right place... */
  452. dev->volume = 0x1f;
  453. em28xx_set_outfmt(dev);
  454. em28xx_colorlevels_set_default(dev);
  455. em28xx_compression_disable(dev);
  456. return 0;
  457. }
  458. /*
  459. * em28xx_config_i2c()
  460. * configure i2c attached devices
  461. */
  462. static void em28xx_config_i2c(struct em28xx *dev)
  463. {
  464. struct v4l2_routing route;
  465. int zero = 0;
  466. route.input = INPUT(dev->ctl_input)->vmux;
  467. route.output = 0;
  468. em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, &zero);
  469. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  470. em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
  471. }
  472. static void video_mux(struct em28xx *dev, int index)
  473. {
  474. struct v4l2_routing route;
  475. route.input = INPUT(index)->vmux;
  476. route.output = 0;
  477. dev->ctl_input = index;
  478. dev->ctl_ainput = INPUT(index)->amux;
  479. dev->ctl_aoutput = INPUT(index)->aout;
  480. if (!dev->ctl_aoutput)
  481. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  482. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  483. if (dev->board.has_msp34xx) {
  484. if (dev->i2s_speed) {
  485. em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
  486. &dev->i2s_speed);
  487. }
  488. route.input = dev->ctl_ainput;
  489. route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
  490. /* Note: this is msp3400 specific */
  491. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
  492. &route);
  493. }
  494. em28xx_audio_analog_set(dev);
  495. }
  496. /* Usage lock check functions */
  497. static int res_get(struct em28xx_fh *fh)
  498. {
  499. struct em28xx *dev = fh->dev;
  500. int rc = 0;
  501. /* This instance already has stream_on */
  502. if (fh->stream_on)
  503. return rc;
  504. if (dev->stream_on)
  505. return -EBUSY;
  506. dev->stream_on = 1;
  507. fh->stream_on = 1;
  508. return rc;
  509. }
  510. static int res_check(struct em28xx_fh *fh)
  511. {
  512. return (fh->stream_on);
  513. }
  514. static void res_free(struct em28xx_fh *fh)
  515. {
  516. struct em28xx *dev = fh->dev;
  517. fh->stream_on = 0;
  518. dev->stream_on = 0;
  519. }
  520. /*
  521. * em28xx_get_ctrl()
  522. * return the current saturation, brightness or contrast, mute state
  523. */
  524. static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  525. {
  526. switch (ctrl->id) {
  527. case V4L2_CID_AUDIO_MUTE:
  528. ctrl->value = dev->mute;
  529. return 0;
  530. case V4L2_CID_AUDIO_VOLUME:
  531. ctrl->value = dev->volume;
  532. return 0;
  533. default:
  534. return -EINVAL;
  535. }
  536. }
  537. /*
  538. * em28xx_set_ctrl()
  539. * mute or set new saturation, brightness or contrast
  540. */
  541. static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  542. {
  543. switch (ctrl->id) {
  544. case V4L2_CID_AUDIO_MUTE:
  545. if (ctrl->value != dev->mute) {
  546. dev->mute = ctrl->value;
  547. return em28xx_audio_analog_set(dev);
  548. }
  549. return 0;
  550. case V4L2_CID_AUDIO_VOLUME:
  551. dev->volume = ctrl->value;
  552. return em28xx_audio_analog_set(dev);
  553. default:
  554. return -EINVAL;
  555. }
  556. }
  557. static int check_dev(struct em28xx *dev)
  558. {
  559. if (dev->state & DEV_DISCONNECTED) {
  560. em28xx_errdev("v4l2 ioctl: device not present\n");
  561. return -ENODEV;
  562. }
  563. if (dev->state & DEV_MISCONFIGURED) {
  564. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  565. "close and open it again\n");
  566. return -EIO;
  567. }
  568. return 0;
  569. }
  570. static void get_scale(struct em28xx *dev,
  571. unsigned int width, unsigned int height,
  572. unsigned int *hscale, unsigned int *vscale)
  573. {
  574. unsigned int maxw = norm_maxw(dev);
  575. unsigned int maxh = norm_maxh(dev);
  576. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  577. if (*hscale >= 0x4000)
  578. *hscale = 0x3fff;
  579. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  580. if (*vscale >= 0x4000)
  581. *vscale = 0x3fff;
  582. }
  583. /* ------------------------------------------------------------------
  584. IOCTL vidioc handling
  585. ------------------------------------------------------------------*/
  586. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  587. struct v4l2_format *f)
  588. {
  589. struct em28xx_fh *fh = priv;
  590. struct em28xx *dev = fh->dev;
  591. mutex_lock(&dev->lock);
  592. f->fmt.pix.width = dev->width;
  593. f->fmt.pix.height = dev->height;
  594. f->fmt.pix.pixelformat = dev->format->fourcc;
  595. f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
  596. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  597. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  598. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  599. f->fmt.pix.field = dev->interlaced ?
  600. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  601. mutex_unlock(&dev->lock);
  602. return 0;
  603. }
  604. static struct em28xx_fmt *format_by_fourcc(unsigned int fourcc)
  605. {
  606. unsigned int i;
  607. for (i = 0; i < ARRAY_SIZE(format); i++)
  608. if (format[i].fourcc == fourcc)
  609. return &format[i];
  610. return NULL;
  611. }
  612. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  613. struct v4l2_format *f)
  614. {
  615. struct em28xx_fh *fh = priv;
  616. struct em28xx *dev = fh->dev;
  617. int width = f->fmt.pix.width;
  618. int height = f->fmt.pix.height;
  619. unsigned int maxw = norm_maxw(dev);
  620. unsigned int maxh = norm_maxh(dev);
  621. unsigned int hscale, vscale;
  622. struct em28xx_fmt *fmt;
  623. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  624. if (!fmt) {
  625. em28xx_videodbg("Fourcc format (%08x) invalid.\n",
  626. f->fmt.pix.pixelformat);
  627. return -EINVAL;
  628. }
  629. /* width must even because of the YUYV format
  630. height must be even because of interlacing */
  631. height &= 0xfffe;
  632. width &= 0xfffe;
  633. if (unlikely(height < 32))
  634. height = 32;
  635. if (unlikely(height > maxh))
  636. height = maxh;
  637. if (unlikely(width < 48))
  638. width = 48;
  639. if (unlikely(width > maxw))
  640. width = maxw;
  641. if (dev->board.is_em2800) {
  642. /* the em2800 can only scale down to 50% */
  643. if (height % (maxh / 2))
  644. height = maxh;
  645. if (width % (maxw / 2))
  646. width = maxw;
  647. /* according to empiatech support */
  648. /* the MaxPacketSize is to small to support */
  649. /* framesizes larger than 640x480 @ 30 fps */
  650. /* or 640x576 @ 25 fps. As this would cut */
  651. /* of a part of the image we prefer */
  652. /* 360x576 or 360x480 for now */
  653. if (width == maxw && height == maxh)
  654. width /= 2;
  655. }
  656. get_scale(dev, width, height, &hscale, &vscale);
  657. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  658. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  659. f->fmt.pix.width = width;
  660. f->fmt.pix.height = height;
  661. f->fmt.pix.pixelformat = fmt->fourcc;
  662. f->fmt.pix.bytesperline = (dev->width * fmt->depth + 7) >> 3;
  663. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
  664. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  665. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  666. return 0;
  667. }
  668. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  669. struct v4l2_format *f)
  670. {
  671. struct em28xx_fh *fh = priv;
  672. struct em28xx *dev = fh->dev;
  673. int rc;
  674. struct em28xx_fmt *fmt;
  675. rc = check_dev(dev);
  676. if (rc < 0)
  677. return rc;
  678. mutex_lock(&dev->lock);
  679. vidioc_try_fmt_vid_cap(file, priv, f);
  680. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  681. if (!fmt) {
  682. rc = -EINVAL;
  683. goto out;
  684. }
  685. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  686. em28xx_errdev("%s queue busy\n", __func__);
  687. rc = -EBUSY;
  688. goto out;
  689. }
  690. if (dev->stream_on && !fh->stream_on) {
  691. em28xx_errdev("%s device in use by another fh\n", __func__);
  692. rc = -EBUSY;
  693. goto out;
  694. }
  695. /* set new image size */
  696. dev->width = f->fmt.pix.width;
  697. dev->height = f->fmt.pix.height;
  698. dev->format = fmt;
  699. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  700. em28xx_set_alternate(dev);
  701. em28xx_resolution_set(dev);
  702. rc = 0;
  703. out:
  704. mutex_unlock(&dev->lock);
  705. return rc;
  706. }
  707. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
  708. {
  709. struct em28xx_fh *fh = priv;
  710. struct em28xx *dev = fh->dev;
  711. struct v4l2_format f;
  712. int rc;
  713. rc = check_dev(dev);
  714. if (rc < 0)
  715. return rc;
  716. mutex_lock(&dev->lock);
  717. dev->norm = *norm;
  718. /* Adjusts width/height, if needed */
  719. f.fmt.pix.width = dev->width;
  720. f.fmt.pix.height = dev->height;
  721. vidioc_try_fmt_vid_cap(file, priv, &f);
  722. /* set new image size */
  723. dev->width = f.fmt.pix.width;
  724. dev->height = f.fmt.pix.height;
  725. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  726. em28xx_resolution_set(dev);
  727. em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
  728. mutex_unlock(&dev->lock);
  729. return 0;
  730. }
  731. static const char *iname[] = {
  732. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  733. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  734. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  735. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  736. [EM28XX_VMUX_SVIDEO] = "S-Video",
  737. [EM28XX_VMUX_TELEVISION] = "Television",
  738. [EM28XX_VMUX_CABLE] = "Cable TV",
  739. [EM28XX_VMUX_DVB] = "DVB",
  740. [EM28XX_VMUX_DEBUG] = "for debug only",
  741. };
  742. static int vidioc_enum_input(struct file *file, void *priv,
  743. struct v4l2_input *i)
  744. {
  745. struct em28xx_fh *fh = priv;
  746. struct em28xx *dev = fh->dev;
  747. unsigned int n;
  748. n = i->index;
  749. if (n >= MAX_EM28XX_INPUT)
  750. return -EINVAL;
  751. if (0 == INPUT(n)->type)
  752. return -EINVAL;
  753. i->index = n;
  754. i->type = V4L2_INPUT_TYPE_CAMERA;
  755. strcpy(i->name, iname[INPUT(n)->type]);
  756. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  757. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  758. i->type = V4L2_INPUT_TYPE_TUNER;
  759. i->std = dev->vdev->tvnorms;
  760. return 0;
  761. }
  762. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  763. {
  764. struct em28xx_fh *fh = priv;
  765. struct em28xx *dev = fh->dev;
  766. *i = dev->ctl_input;
  767. return 0;
  768. }
  769. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  770. {
  771. struct em28xx_fh *fh = priv;
  772. struct em28xx *dev = fh->dev;
  773. int rc;
  774. rc = check_dev(dev);
  775. if (rc < 0)
  776. return rc;
  777. if (i >= MAX_EM28XX_INPUT)
  778. return -EINVAL;
  779. if (0 == INPUT(i)->type)
  780. return -EINVAL;
  781. mutex_lock(&dev->lock);
  782. video_mux(dev, i);
  783. mutex_unlock(&dev->lock);
  784. return 0;
  785. }
  786. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  787. {
  788. struct em28xx_fh *fh = priv;
  789. struct em28xx *dev = fh->dev;
  790. switch (a->index) {
  791. case EM28XX_AMUX_VIDEO:
  792. strcpy(a->name, "Television");
  793. break;
  794. case EM28XX_AMUX_LINE_IN:
  795. strcpy(a->name, "Line In");
  796. break;
  797. case EM28XX_AMUX_VIDEO2:
  798. strcpy(a->name, "Television alt");
  799. break;
  800. case EM28XX_AMUX_PHONE:
  801. strcpy(a->name, "Phone");
  802. break;
  803. case EM28XX_AMUX_MIC:
  804. strcpy(a->name, "Mic");
  805. break;
  806. case EM28XX_AMUX_CD:
  807. strcpy(a->name, "CD");
  808. break;
  809. case EM28XX_AMUX_AUX:
  810. strcpy(a->name, "Aux");
  811. break;
  812. case EM28XX_AMUX_PCM_OUT:
  813. strcpy(a->name, "PCM");
  814. break;
  815. default:
  816. return -EINVAL;
  817. }
  818. a->index = dev->ctl_ainput;
  819. a->capability = V4L2_AUDCAP_STEREO;
  820. return 0;
  821. }
  822. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  823. {
  824. struct em28xx_fh *fh = priv;
  825. struct em28xx *dev = fh->dev;
  826. mutex_lock(&dev->lock);
  827. dev->ctl_ainput = INPUT(a->index)->amux;
  828. dev->ctl_aoutput = INPUT(a->index)->aout;
  829. if (!dev->ctl_aoutput)
  830. dev->ctl_aoutput = EM28XX_AOUT_MASTER;
  831. mutex_unlock(&dev->lock);
  832. return 0;
  833. }
  834. static int vidioc_queryctrl(struct file *file, void *priv,
  835. struct v4l2_queryctrl *qc)
  836. {
  837. struct em28xx_fh *fh = priv;
  838. struct em28xx *dev = fh->dev;
  839. int id = qc->id;
  840. int i;
  841. int rc;
  842. rc = check_dev(dev);
  843. if (rc < 0)
  844. return rc;
  845. memset(qc, 0, sizeof(*qc));
  846. qc->id = id;
  847. if (!dev->board.has_msp34xx) {
  848. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  849. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  850. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  851. return 0;
  852. }
  853. }
  854. }
  855. mutex_lock(&dev->lock);
  856. em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
  857. mutex_unlock(&dev->lock);
  858. if (qc->type)
  859. return 0;
  860. else
  861. return -EINVAL;
  862. }
  863. static int vidioc_g_ctrl(struct file *file, void *priv,
  864. struct v4l2_control *ctrl)
  865. {
  866. struct em28xx_fh *fh = priv;
  867. struct em28xx *dev = fh->dev;
  868. int rc;
  869. rc = check_dev(dev);
  870. if (rc < 0)
  871. return rc;
  872. rc = 0;
  873. mutex_lock(&dev->lock);
  874. if (dev->board.has_msp34xx)
  875. em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
  876. else
  877. rc = em28xx_get_ctrl(dev, ctrl);
  878. mutex_unlock(&dev->lock);
  879. return rc;
  880. }
  881. static int vidioc_s_ctrl(struct file *file, void *priv,
  882. struct v4l2_control *ctrl)
  883. {
  884. struct em28xx_fh *fh = priv;
  885. struct em28xx *dev = fh->dev;
  886. u8 i;
  887. int rc;
  888. rc = check_dev(dev);
  889. if (rc < 0)
  890. return rc;
  891. mutex_lock(&dev->lock);
  892. if (dev->board.has_msp34xx)
  893. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  894. else {
  895. rc = 1;
  896. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  897. if (ctrl->id == em28xx_qctrl[i].id) {
  898. if (ctrl->value < em28xx_qctrl[i].minimum ||
  899. ctrl->value > em28xx_qctrl[i].maximum) {
  900. rc = -ERANGE;
  901. break;
  902. }
  903. rc = em28xx_set_ctrl(dev, ctrl);
  904. break;
  905. }
  906. }
  907. }
  908. /* Control not found - try to send it to the attached devices */
  909. if (rc == 1) {
  910. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  911. rc = 0;
  912. }
  913. mutex_unlock(&dev->lock);
  914. return rc;
  915. }
  916. static int vidioc_g_tuner(struct file *file, void *priv,
  917. struct v4l2_tuner *t)
  918. {
  919. struct em28xx_fh *fh = priv;
  920. struct em28xx *dev = fh->dev;
  921. int rc;
  922. rc = check_dev(dev);
  923. if (rc < 0)
  924. return rc;
  925. if (0 != t->index)
  926. return -EINVAL;
  927. strcpy(t->name, "Tuner");
  928. mutex_lock(&dev->lock);
  929. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  930. mutex_unlock(&dev->lock);
  931. return 0;
  932. }
  933. static int vidioc_s_tuner(struct file *file, void *priv,
  934. struct v4l2_tuner *t)
  935. {
  936. struct em28xx_fh *fh = priv;
  937. struct em28xx *dev = fh->dev;
  938. int rc;
  939. rc = check_dev(dev);
  940. if (rc < 0)
  941. return rc;
  942. if (0 != t->index)
  943. return -EINVAL;
  944. mutex_lock(&dev->lock);
  945. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  946. mutex_unlock(&dev->lock);
  947. return 0;
  948. }
  949. static int vidioc_g_frequency(struct file *file, void *priv,
  950. struct v4l2_frequency *f)
  951. {
  952. struct em28xx_fh *fh = priv;
  953. struct em28xx *dev = fh->dev;
  954. mutex_lock(&dev->lock);
  955. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  956. f->frequency = dev->ctl_freq;
  957. mutex_unlock(&dev->lock);
  958. return 0;
  959. }
  960. static int vidioc_s_frequency(struct file *file, void *priv,
  961. struct v4l2_frequency *f)
  962. {
  963. struct em28xx_fh *fh = priv;
  964. struct em28xx *dev = fh->dev;
  965. int rc;
  966. rc = check_dev(dev);
  967. if (rc < 0)
  968. return rc;
  969. if (0 != f->tuner)
  970. return -EINVAL;
  971. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  972. return -EINVAL;
  973. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  974. return -EINVAL;
  975. mutex_lock(&dev->lock);
  976. dev->ctl_freq = f->frequency;
  977. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  978. mutex_unlock(&dev->lock);
  979. return 0;
  980. }
  981. #ifdef CONFIG_VIDEO_ADV_DEBUG
  982. static int em28xx_reg_len(int reg)
  983. {
  984. switch (reg) {
  985. case EM28XX_R40_AC97LSB:
  986. case EM28XX_R30_HSCALELOW:
  987. case EM28XX_R32_VSCALELOW:
  988. return 2;
  989. default:
  990. return 1;
  991. }
  992. }
  993. static int vidioc_g_register(struct file *file, void *priv,
  994. struct v4l2_register *reg)
  995. {
  996. struct em28xx_fh *fh = priv;
  997. struct em28xx *dev = fh->dev;
  998. int ret;
  999. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  1000. return -EINVAL;
  1001. if (em28xx_reg_len(reg->reg) == 1) {
  1002. mutex_lock(&dev->lock);
  1003. ret = em28xx_read_reg(dev, reg->reg);
  1004. mutex_unlock(&dev->lock);
  1005. if (ret < 0)
  1006. return ret;
  1007. reg->val = ret;
  1008. } else {
  1009. __le64 val = 0;
  1010. mutex_lock(&dev->lock);
  1011. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  1012. reg->reg, (char *)&val, 2);
  1013. mutex_unlock(&dev->lock);
  1014. if (ret < 0)
  1015. return ret;
  1016. reg->val = le64_to_cpu(val);
  1017. }
  1018. return 0;
  1019. }
  1020. static int vidioc_s_register(struct file *file, void *priv,
  1021. struct v4l2_register *reg)
  1022. {
  1023. struct em28xx_fh *fh = priv;
  1024. struct em28xx *dev = fh->dev;
  1025. __le64 buf;
  1026. int rc;
  1027. buf = cpu_to_le64(reg->val);
  1028. mutex_lock(&dev->lock);
  1029. rc = em28xx_write_regs(dev, reg->reg, (char *)&buf,
  1030. em28xx_reg_len(reg->reg));
  1031. mutex_unlock(&dev->lock);
  1032. return rc;
  1033. }
  1034. #endif
  1035. static int vidioc_cropcap(struct file *file, void *priv,
  1036. struct v4l2_cropcap *cc)
  1037. {
  1038. struct em28xx_fh *fh = priv;
  1039. struct em28xx *dev = fh->dev;
  1040. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1041. return -EINVAL;
  1042. cc->bounds.left = 0;
  1043. cc->bounds.top = 0;
  1044. cc->bounds.width = dev->width;
  1045. cc->bounds.height = dev->height;
  1046. cc->defrect = cc->bounds;
  1047. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1048. cc->pixelaspect.denominator = 59;
  1049. return 0;
  1050. }
  1051. static int vidioc_streamon(struct file *file, void *priv,
  1052. enum v4l2_buf_type type)
  1053. {
  1054. struct em28xx_fh *fh = priv;
  1055. struct em28xx *dev = fh->dev;
  1056. int rc;
  1057. rc = check_dev(dev);
  1058. if (rc < 0)
  1059. return rc;
  1060. mutex_lock(&dev->lock);
  1061. rc = res_get(fh);
  1062. if (likely(rc >= 0))
  1063. rc = videobuf_streamon(&fh->vb_vidq);
  1064. mutex_unlock(&dev->lock);
  1065. return rc;
  1066. }
  1067. static int vidioc_streamoff(struct file *file, void *priv,
  1068. enum v4l2_buf_type type)
  1069. {
  1070. struct em28xx_fh *fh = priv;
  1071. struct em28xx *dev = fh->dev;
  1072. int rc;
  1073. rc = check_dev(dev);
  1074. if (rc < 0)
  1075. return rc;
  1076. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1077. return -EINVAL;
  1078. if (type != fh->type)
  1079. return -EINVAL;
  1080. mutex_lock(&dev->lock);
  1081. videobuf_streamoff(&fh->vb_vidq);
  1082. res_free(fh);
  1083. mutex_unlock(&dev->lock);
  1084. return 0;
  1085. }
  1086. static int vidioc_querycap(struct file *file, void *priv,
  1087. struct v4l2_capability *cap)
  1088. {
  1089. struct em28xx_fh *fh = priv;
  1090. struct em28xx *dev = fh->dev;
  1091. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1092. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1093. strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
  1094. cap->version = EM28XX_VERSION_CODE;
  1095. cap->capabilities =
  1096. V4L2_CAP_SLICED_VBI_CAPTURE |
  1097. V4L2_CAP_VIDEO_CAPTURE |
  1098. V4L2_CAP_AUDIO |
  1099. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1100. if (dev->tuner_type != TUNER_ABSENT)
  1101. cap->capabilities |= V4L2_CAP_TUNER;
  1102. return 0;
  1103. }
  1104. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1105. struct v4l2_fmtdesc *f)
  1106. {
  1107. if (unlikely(f->index >= ARRAY_SIZE(format)))
  1108. return -EINVAL;
  1109. strlcpy(f->description, format[f->index].name, sizeof(f->description));
  1110. f->pixelformat = format[f->index].fourcc;
  1111. return 0;
  1112. }
  1113. /* Sliced VBI ioctls */
  1114. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1115. struct v4l2_format *f)
  1116. {
  1117. struct em28xx_fh *fh = priv;
  1118. struct em28xx *dev = fh->dev;
  1119. int rc;
  1120. rc = check_dev(dev);
  1121. if (rc < 0)
  1122. return rc;
  1123. mutex_lock(&dev->lock);
  1124. f->fmt.sliced.service_set = 0;
  1125. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1126. if (f->fmt.sliced.service_set == 0)
  1127. rc = -EINVAL;
  1128. mutex_unlock(&dev->lock);
  1129. return rc;
  1130. }
  1131. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1132. struct v4l2_format *f)
  1133. {
  1134. struct em28xx_fh *fh = priv;
  1135. struct em28xx *dev = fh->dev;
  1136. int rc;
  1137. rc = check_dev(dev);
  1138. if (rc < 0)
  1139. return rc;
  1140. mutex_lock(&dev->lock);
  1141. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1142. mutex_unlock(&dev->lock);
  1143. if (f->fmt.sliced.service_set == 0)
  1144. return -EINVAL;
  1145. return 0;
  1146. }
  1147. static int vidioc_reqbufs(struct file *file, void *priv,
  1148. struct v4l2_requestbuffers *rb)
  1149. {
  1150. struct em28xx_fh *fh = priv;
  1151. struct em28xx *dev = fh->dev;
  1152. int rc;
  1153. rc = check_dev(dev);
  1154. if (rc < 0)
  1155. return rc;
  1156. return (videobuf_reqbufs(&fh->vb_vidq, rb));
  1157. }
  1158. static int vidioc_querybuf(struct file *file, void *priv,
  1159. struct v4l2_buffer *b)
  1160. {
  1161. struct em28xx_fh *fh = priv;
  1162. struct em28xx *dev = fh->dev;
  1163. int rc;
  1164. rc = check_dev(dev);
  1165. if (rc < 0)
  1166. return rc;
  1167. return (videobuf_querybuf(&fh->vb_vidq, b));
  1168. }
  1169. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1170. {
  1171. struct em28xx_fh *fh = priv;
  1172. struct em28xx *dev = fh->dev;
  1173. int rc;
  1174. rc = check_dev(dev);
  1175. if (rc < 0)
  1176. return rc;
  1177. return (videobuf_qbuf(&fh->vb_vidq, b));
  1178. }
  1179. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1180. {
  1181. struct em28xx_fh *fh = priv;
  1182. struct em28xx *dev = fh->dev;
  1183. int rc;
  1184. rc = check_dev(dev);
  1185. if (rc < 0)
  1186. return rc;
  1187. return (videobuf_dqbuf(&fh->vb_vidq, b,
  1188. file->f_flags & O_NONBLOCK));
  1189. }
  1190. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1191. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1192. {
  1193. struct em28xx_fh *fh = priv;
  1194. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1195. }
  1196. #endif
  1197. /* ----------------------------------------------------------- */
  1198. /* RADIO ESPECIFIC IOCTLS */
  1199. /* ----------------------------------------------------------- */
  1200. static int radio_querycap(struct file *file, void *priv,
  1201. struct v4l2_capability *cap)
  1202. {
  1203. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1204. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1205. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1206. strlcpy(cap->bus_info, dev_name(&dev->udev->dev), sizeof(cap->bus_info));
  1207. cap->version = EM28XX_VERSION_CODE;
  1208. cap->capabilities = V4L2_CAP_TUNER;
  1209. return 0;
  1210. }
  1211. static int radio_g_tuner(struct file *file, void *priv,
  1212. struct v4l2_tuner *t)
  1213. {
  1214. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1215. if (unlikely(t->index > 0))
  1216. return -EINVAL;
  1217. strcpy(t->name, "Radio");
  1218. t->type = V4L2_TUNER_RADIO;
  1219. mutex_lock(&dev->lock);
  1220. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  1221. mutex_unlock(&dev->lock);
  1222. return 0;
  1223. }
  1224. static int radio_enum_input(struct file *file, void *priv,
  1225. struct v4l2_input *i)
  1226. {
  1227. if (i->index != 0)
  1228. return -EINVAL;
  1229. strcpy(i->name, "Radio");
  1230. i->type = V4L2_INPUT_TYPE_TUNER;
  1231. return 0;
  1232. }
  1233. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1234. {
  1235. if (unlikely(a->index))
  1236. return -EINVAL;
  1237. strcpy(a->name, "Radio");
  1238. return 0;
  1239. }
  1240. static int radio_s_tuner(struct file *file, void *priv,
  1241. struct v4l2_tuner *t)
  1242. {
  1243. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1244. if (0 != t->index)
  1245. return -EINVAL;
  1246. mutex_lock(&dev->lock);
  1247. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  1248. mutex_unlock(&dev->lock);
  1249. return 0;
  1250. }
  1251. static int radio_s_audio(struct file *file, void *fh,
  1252. struct v4l2_audio *a)
  1253. {
  1254. return 0;
  1255. }
  1256. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1257. {
  1258. return 0;
  1259. }
  1260. static int radio_queryctrl(struct file *file, void *priv,
  1261. struct v4l2_queryctrl *qc)
  1262. {
  1263. int i;
  1264. if (qc->id < V4L2_CID_BASE ||
  1265. qc->id >= V4L2_CID_LASTP1)
  1266. return -EINVAL;
  1267. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1268. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1269. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  1270. return 0;
  1271. }
  1272. }
  1273. return -EINVAL;
  1274. }
  1275. /*
  1276. * em28xx_v4l2_open()
  1277. * inits the device and starts isoc transfer
  1278. */
  1279. static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
  1280. {
  1281. int minor = iminor(inode);
  1282. int errCode = 0, radio = 0;
  1283. struct em28xx *h, *dev = NULL;
  1284. struct em28xx_fh *fh;
  1285. enum v4l2_buf_type fh_type = 0;
  1286. mutex_lock(&em28xx_devlist_mutex);
  1287. list_for_each_entry(h, &em28xx_devlist, devlist) {
  1288. if (h->vdev->minor == minor) {
  1289. dev = h;
  1290. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1291. }
  1292. if (h->vbi_dev->minor == minor) {
  1293. dev = h;
  1294. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1295. }
  1296. if (h->radio_dev &&
  1297. h->radio_dev->minor == minor) {
  1298. radio = 1;
  1299. dev = h;
  1300. }
  1301. }
  1302. mutex_unlock(&em28xx_devlist_mutex);
  1303. if (NULL == dev)
  1304. return -ENODEV;
  1305. mutex_lock(&dev->lock);
  1306. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1307. minor, v4l2_type_names[fh_type], dev->users);
  1308. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1309. if (!fh) {
  1310. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1311. mutex_unlock(&dev->lock);
  1312. return -ENOMEM;
  1313. }
  1314. fh->dev = dev;
  1315. fh->radio = radio;
  1316. fh->type = fh_type;
  1317. filp->private_data = fh;
  1318. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1319. dev->width = norm_maxw(dev);
  1320. dev->height = norm_maxh(dev);
  1321. dev->hscale = 0;
  1322. dev->vscale = 0;
  1323. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1324. em28xx_set_alternate(dev);
  1325. em28xx_resolution_set(dev);
  1326. /* Needed, since GPIO might have disabled power of
  1327. some i2c device
  1328. */
  1329. em28xx_config_i2c(dev);
  1330. }
  1331. if (fh->radio) {
  1332. em28xx_videodbg("video_open: setting radio device\n");
  1333. em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
  1334. }
  1335. dev->users++;
  1336. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1337. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  1338. sizeof(struct em28xx_buffer), fh);
  1339. mutex_unlock(&dev->lock);
  1340. return errCode;
  1341. }
  1342. /*
  1343. * em28xx_realease_resources()
  1344. * unregisters the v4l2,i2c and usb devices
  1345. * called when the device gets disconected or at module unload
  1346. */
  1347. static void em28xx_release_resources(struct em28xx *dev)
  1348. {
  1349. /*FIXME: I2C IR should be disconnected */
  1350. list_del(&dev->devlist);
  1351. if (dev->sbutton_input_dev)
  1352. em28xx_deregister_snapshot_button(dev);
  1353. if (dev->ir)
  1354. em28xx_ir_fini(dev);
  1355. if (dev->radio_dev) {
  1356. if (-1 != dev->radio_dev->minor)
  1357. video_unregister_device(dev->radio_dev);
  1358. else
  1359. video_device_release(dev->radio_dev);
  1360. dev->radio_dev = NULL;
  1361. }
  1362. if (dev->vbi_dev) {
  1363. em28xx_info("V4L2 device /dev/vbi%d deregistered\n",
  1364. dev->vbi_dev->num);
  1365. if (-1 != dev->vbi_dev->minor)
  1366. video_unregister_device(dev->vbi_dev);
  1367. else
  1368. video_device_release(dev->vbi_dev);
  1369. dev->vbi_dev = NULL;
  1370. }
  1371. if (dev->vdev) {
  1372. em28xx_info("V4L2 device /dev/video%d deregistered\n",
  1373. dev->vdev->num);
  1374. if (-1 != dev->vdev->minor)
  1375. video_unregister_device(dev->vdev);
  1376. else
  1377. video_device_release(dev->vdev);
  1378. dev->vdev = NULL;
  1379. }
  1380. em28xx_i2c_unregister(dev);
  1381. usb_put_dev(dev->udev);
  1382. /* Mark device as unused */
  1383. em28xx_devused &= ~(1<<dev->devno);
  1384. }
  1385. /*
  1386. * em28xx_v4l2_close()
  1387. * stops streaming and deallocates all resources allocated by the v4l2
  1388. * calls and ioctls
  1389. */
  1390. static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
  1391. {
  1392. struct em28xx_fh *fh = filp->private_data;
  1393. struct em28xx *dev = fh->dev;
  1394. int errCode;
  1395. em28xx_videodbg("users=%d\n", dev->users);
  1396. mutex_lock(&dev->lock);
  1397. if (res_check(fh))
  1398. res_free(fh);
  1399. if (dev->users == 1) {
  1400. videobuf_stop(&fh->vb_vidq);
  1401. videobuf_mmap_free(&fh->vb_vidq);
  1402. /* the device is already disconnect,
  1403. free the remaining resources */
  1404. if (dev->state & DEV_DISCONNECTED) {
  1405. em28xx_release_resources(dev);
  1406. mutex_unlock(&dev->lock);
  1407. kfree(dev);
  1408. return 0;
  1409. }
  1410. /* Save some power by putting tuner to sleep */
  1411. em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
  1412. /* do this before setting alternate! */
  1413. em28xx_uninit_isoc(dev);
  1414. em28xx_set_mode(dev, EM28XX_SUSPEND);
  1415. /* set alternate 0 */
  1416. dev->alt = 0;
  1417. em28xx_videodbg("setting alternate 0\n");
  1418. errCode = usb_set_interface(dev->udev, 0, 0);
  1419. if (errCode < 0) {
  1420. em28xx_errdev("cannot change alternate number to "
  1421. "0 (error=%i)\n", errCode);
  1422. }
  1423. }
  1424. kfree(fh);
  1425. dev->users--;
  1426. wake_up_interruptible_nr(&dev->open, 1);
  1427. mutex_unlock(&dev->lock);
  1428. return 0;
  1429. }
  1430. /*
  1431. * em28xx_v4l2_read()
  1432. * will allocate buffers when called for the first time
  1433. */
  1434. static ssize_t
  1435. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1436. loff_t *pos)
  1437. {
  1438. struct em28xx_fh *fh = filp->private_data;
  1439. struct em28xx *dev = fh->dev;
  1440. int rc;
  1441. rc = check_dev(dev);
  1442. if (rc < 0)
  1443. return rc;
  1444. /* FIXME: read() is not prepared to allow changing the video
  1445. resolution while streaming. Seems a bug at em28xx_set_fmt
  1446. */
  1447. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1448. mutex_lock(&dev->lock);
  1449. rc = res_get(fh);
  1450. mutex_unlock(&dev->lock);
  1451. if (unlikely(rc < 0))
  1452. return rc;
  1453. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1454. filp->f_flags & O_NONBLOCK);
  1455. }
  1456. return 0;
  1457. }
  1458. /*
  1459. * em28xx_v4l2_poll()
  1460. * will allocate buffers when called for the first time
  1461. */
  1462. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
  1463. {
  1464. struct em28xx_fh *fh = filp->private_data;
  1465. struct em28xx *dev = fh->dev;
  1466. int rc;
  1467. rc = check_dev(dev);
  1468. if (rc < 0)
  1469. return rc;
  1470. mutex_lock(&dev->lock);
  1471. rc = res_get(fh);
  1472. mutex_unlock(&dev->lock);
  1473. if (unlikely(rc < 0))
  1474. return POLLERR;
  1475. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1476. return POLLERR;
  1477. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1478. }
  1479. /*
  1480. * em28xx_v4l2_mmap()
  1481. */
  1482. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1483. {
  1484. struct em28xx_fh *fh = filp->private_data;
  1485. struct em28xx *dev = fh->dev;
  1486. int rc;
  1487. rc = check_dev(dev);
  1488. if (rc < 0)
  1489. return rc;
  1490. mutex_lock(&dev->lock);
  1491. rc = res_get(fh);
  1492. mutex_unlock(&dev->lock);
  1493. if (unlikely(rc < 0))
  1494. return rc;
  1495. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1496. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1497. (unsigned long)vma->vm_start,
  1498. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1499. rc);
  1500. return rc;
  1501. }
  1502. static const struct file_operations em28xx_v4l_fops = {
  1503. .owner = THIS_MODULE,
  1504. .open = em28xx_v4l2_open,
  1505. .release = em28xx_v4l2_close,
  1506. .read = em28xx_v4l2_read,
  1507. .poll = em28xx_v4l2_poll,
  1508. .mmap = em28xx_v4l2_mmap,
  1509. .ioctl = video_ioctl2,
  1510. .llseek = no_llseek,
  1511. .compat_ioctl = v4l_compat_ioctl32,
  1512. };
  1513. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1514. .vidioc_querycap = vidioc_querycap,
  1515. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1516. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1517. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1518. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1519. .vidioc_g_audio = vidioc_g_audio,
  1520. .vidioc_s_audio = vidioc_s_audio,
  1521. .vidioc_cropcap = vidioc_cropcap,
  1522. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1523. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1524. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1525. .vidioc_reqbufs = vidioc_reqbufs,
  1526. .vidioc_querybuf = vidioc_querybuf,
  1527. .vidioc_qbuf = vidioc_qbuf,
  1528. .vidioc_dqbuf = vidioc_dqbuf,
  1529. .vidioc_s_std = vidioc_s_std,
  1530. .vidioc_enum_input = vidioc_enum_input,
  1531. .vidioc_g_input = vidioc_g_input,
  1532. .vidioc_s_input = vidioc_s_input,
  1533. .vidioc_queryctrl = vidioc_queryctrl,
  1534. .vidioc_g_ctrl = vidioc_g_ctrl,
  1535. .vidioc_s_ctrl = vidioc_s_ctrl,
  1536. .vidioc_streamon = vidioc_streamon,
  1537. .vidioc_streamoff = vidioc_streamoff,
  1538. .vidioc_g_tuner = vidioc_g_tuner,
  1539. .vidioc_s_tuner = vidioc_s_tuner,
  1540. .vidioc_g_frequency = vidioc_g_frequency,
  1541. .vidioc_s_frequency = vidioc_s_frequency,
  1542. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1543. .vidioc_g_register = vidioc_g_register,
  1544. .vidioc_s_register = vidioc_s_register,
  1545. #endif
  1546. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1547. .vidiocgmbuf = vidiocgmbuf,
  1548. #endif
  1549. };
  1550. static const struct video_device em28xx_video_template = {
  1551. .fops = &em28xx_v4l_fops,
  1552. .release = video_device_release,
  1553. .ioctl_ops = &video_ioctl_ops,
  1554. .minor = -1,
  1555. .tvnorms = V4L2_STD_ALL,
  1556. .current_norm = V4L2_STD_PAL,
  1557. };
  1558. static const struct file_operations radio_fops = {
  1559. .owner = THIS_MODULE,
  1560. .open = em28xx_v4l2_open,
  1561. .release = em28xx_v4l2_close,
  1562. .ioctl = video_ioctl2,
  1563. .compat_ioctl = v4l_compat_ioctl32,
  1564. .llseek = no_llseek,
  1565. };
  1566. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1567. .vidioc_querycap = radio_querycap,
  1568. .vidioc_g_tuner = radio_g_tuner,
  1569. .vidioc_enum_input = radio_enum_input,
  1570. .vidioc_g_audio = radio_g_audio,
  1571. .vidioc_s_tuner = radio_s_tuner,
  1572. .vidioc_s_audio = radio_s_audio,
  1573. .vidioc_s_input = radio_s_input,
  1574. .vidioc_queryctrl = radio_queryctrl,
  1575. .vidioc_g_ctrl = vidioc_g_ctrl,
  1576. .vidioc_s_ctrl = vidioc_s_ctrl,
  1577. .vidioc_g_frequency = vidioc_g_frequency,
  1578. .vidioc_s_frequency = vidioc_s_frequency,
  1579. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1580. .vidioc_g_register = vidioc_g_register,
  1581. .vidioc_s_register = vidioc_s_register,
  1582. #endif
  1583. };
  1584. static struct video_device em28xx_radio_template = {
  1585. .name = "em28xx-radio",
  1586. .fops = &radio_fops,
  1587. .ioctl_ops = &radio_ioctl_ops,
  1588. .minor = -1,
  1589. };
  1590. /******************************** usb interface ******************************/
  1591. static LIST_HEAD(em28xx_extension_devlist);
  1592. static DEFINE_MUTEX(em28xx_extension_devlist_lock);
  1593. int em28xx_register_extension(struct em28xx_ops *ops)
  1594. {
  1595. struct em28xx *dev = NULL;
  1596. mutex_lock(&em28xx_devlist_mutex);
  1597. mutex_lock(&em28xx_extension_devlist_lock);
  1598. list_add_tail(&ops->next, &em28xx_extension_devlist);
  1599. list_for_each_entry(dev, &em28xx_devlist, devlist) {
  1600. if (dev)
  1601. ops->init(dev);
  1602. }
  1603. printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
  1604. mutex_unlock(&em28xx_extension_devlist_lock);
  1605. mutex_unlock(&em28xx_devlist_mutex);
  1606. return 0;
  1607. }
  1608. EXPORT_SYMBOL(em28xx_register_extension);
  1609. void em28xx_unregister_extension(struct em28xx_ops *ops)
  1610. {
  1611. struct em28xx *dev = NULL;
  1612. mutex_lock(&em28xx_devlist_mutex);
  1613. list_for_each_entry(dev, &em28xx_devlist, devlist) {
  1614. if (dev)
  1615. ops->fini(dev);
  1616. }
  1617. mutex_lock(&em28xx_extension_devlist_lock);
  1618. printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
  1619. list_del(&ops->next);
  1620. mutex_unlock(&em28xx_extension_devlist_lock);
  1621. mutex_unlock(&em28xx_devlist_mutex);
  1622. }
  1623. EXPORT_SYMBOL(em28xx_unregister_extension);
  1624. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1625. const struct video_device *template,
  1626. const char *type_name)
  1627. {
  1628. struct video_device *vfd;
  1629. vfd = video_device_alloc();
  1630. if (NULL == vfd)
  1631. return NULL;
  1632. *vfd = *template;
  1633. vfd->minor = -1;
  1634. vfd->parent = &dev->udev->dev;
  1635. vfd->release = video_device_release;
  1636. vfd->debug = video_debug;
  1637. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1638. dev->name, type_name);
  1639. return vfd;
  1640. }
  1641. static int register_analog_devices(struct em28xx *dev)
  1642. {
  1643. int ret;
  1644. /* allocate and fill video video_device struct */
  1645. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1646. if (!dev->vdev) {
  1647. em28xx_errdev("cannot allocate video_device.\n");
  1648. return -ENODEV;
  1649. }
  1650. /* register v4l2 video video_device */
  1651. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1652. video_nr[dev->devno]);
  1653. if (ret) {
  1654. em28xx_errdev("unable to register video device (error=%i).\n",
  1655. ret);
  1656. return ret;
  1657. }
  1658. /* Allocate and fill vbi video_device struct */
  1659. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
  1660. /* register v4l2 vbi video_device */
  1661. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1662. vbi_nr[dev->devno]);
  1663. if (ret < 0) {
  1664. em28xx_errdev("unable to register vbi device\n");
  1665. return ret;
  1666. }
  1667. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1668. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
  1669. if (!dev->radio_dev) {
  1670. em28xx_errdev("cannot allocate video_device.\n");
  1671. return -ENODEV;
  1672. }
  1673. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1674. radio_nr[dev->devno]);
  1675. if (ret < 0) {
  1676. em28xx_errdev("can't register radio device\n");
  1677. return ret;
  1678. }
  1679. em28xx_info("Registered radio device as /dev/radio%d\n",
  1680. dev->radio_dev->num);
  1681. }
  1682. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1683. dev->vdev->num, dev->vbi_dev->num);
  1684. return 0;
  1685. }
  1686. /*
  1687. * em28xx_init_dev()
  1688. * allocates and inits the device structs, registers i2c bus and v4l device
  1689. */
  1690. static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
  1691. int minor)
  1692. {
  1693. struct em28xx_ops *ops = NULL;
  1694. struct em28xx *dev = *devhandle;
  1695. int retval = -ENOMEM;
  1696. int errCode;
  1697. unsigned int maxh, maxw;
  1698. dev->udev = udev;
  1699. mutex_init(&dev->ctrl_urb_lock);
  1700. spin_lock_init(&dev->slock);
  1701. init_waitqueue_head(&dev->open);
  1702. init_waitqueue_head(&dev->wait_frame);
  1703. init_waitqueue_head(&dev->wait_stream);
  1704. dev->em28xx_write_regs = em28xx_write_regs;
  1705. dev->em28xx_read_reg = em28xx_read_reg;
  1706. dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
  1707. dev->em28xx_write_regs_req = em28xx_write_regs_req;
  1708. dev->em28xx_read_reg_req = em28xx_read_reg_req;
  1709. dev->board.is_em2800 = em28xx_boards[dev->model].is_em2800;
  1710. dev->format = &format[0];
  1711. em28xx_pre_card_setup(dev);
  1712. errCode = em28xx_config(dev);
  1713. if (errCode) {
  1714. em28xx_errdev("error configuring device\n");
  1715. return -ENOMEM;
  1716. }
  1717. /* register i2c bus */
  1718. errCode = em28xx_i2c_register(dev);
  1719. if (errCode < 0) {
  1720. em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
  1721. __func__, errCode);
  1722. return errCode;
  1723. }
  1724. /* Do board specific init and eeprom reading */
  1725. em28xx_card_setup(dev);
  1726. /* Configure audio */
  1727. errCode = em28xx_audio_setup(dev);
  1728. if (errCode < 0) {
  1729. em28xx_errdev("%s: Error while setting audio - errCode [%d]!\n",
  1730. __func__, errCode);
  1731. }
  1732. /* configure the device */
  1733. em28xx_config_i2c(dev);
  1734. /* set default norm */
  1735. dev->norm = em28xx_video_template.current_norm;
  1736. maxw = norm_maxw(dev);
  1737. maxh = norm_maxh(dev);
  1738. /* set default image size */
  1739. dev->width = maxw;
  1740. dev->height = maxh;
  1741. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1742. dev->hscale = 0;
  1743. dev->vscale = 0;
  1744. dev->ctl_input = 2;
  1745. errCode = em28xx_config(dev);
  1746. if (errCode < 0) {
  1747. em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
  1748. __func__, errCode);
  1749. return errCode;
  1750. }
  1751. /* init video dma queues */
  1752. INIT_LIST_HEAD(&dev->vidq.active);
  1753. INIT_LIST_HEAD(&dev->vidq.queued);
  1754. if (dev->board.has_msp34xx) {
  1755. /* Send a reset to other chips via gpio */
  1756. errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xf7);
  1757. if (errCode < 0) {
  1758. em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
  1759. __func__, errCode);
  1760. return errCode;
  1761. }
  1762. msleep(3);
  1763. errCode = em28xx_write_reg(dev, EM28XX_R08_GPIO, 0xff);
  1764. if (errCode < 0) {
  1765. em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
  1766. __func__, errCode);
  1767. return errCode;
  1768. }
  1769. msleep(3);
  1770. }
  1771. video_mux(dev, 0);
  1772. mutex_lock(&em28xx_devlist_mutex);
  1773. list_add_tail(&dev->devlist, &em28xx_devlist);
  1774. retval = register_analog_devices(dev);
  1775. if (retval < 0) {
  1776. em28xx_release_resources(dev);
  1777. mutex_unlock(&em28xx_devlist_mutex);
  1778. goto fail_reg_devices;
  1779. }
  1780. mutex_lock(&em28xx_extension_devlist_lock);
  1781. if (!list_empty(&em28xx_extension_devlist)) {
  1782. list_for_each_entry(ops, &em28xx_extension_devlist, next) {
  1783. if (ops->id)
  1784. ops->init(dev);
  1785. }
  1786. }
  1787. mutex_unlock(&em28xx_extension_devlist_lock);
  1788. mutex_unlock(&em28xx_devlist_mutex);
  1789. /* Save some power by putting tuner to sleep */
  1790. em28xx_i2c_call_clients(dev, TUNER_SET_STANDBY, NULL);
  1791. return 0;
  1792. fail_reg_devices:
  1793. return retval;
  1794. }
  1795. #if defined(CONFIG_MODULES) && defined(MODULE)
  1796. static void request_module_async(struct work_struct *work)
  1797. {
  1798. struct em28xx *dev = container_of(work,
  1799. struct em28xx, request_module_wk);
  1800. if (dev->has_audio_class)
  1801. request_module("snd-usb-audio");
  1802. else if (dev->has_alsa_audio)
  1803. request_module("em28xx-alsa");
  1804. if (dev->board.has_dvb)
  1805. request_module("em28xx-dvb");
  1806. }
  1807. static void request_modules(struct em28xx *dev)
  1808. {
  1809. INIT_WORK(&dev->request_module_wk, request_module_async);
  1810. schedule_work(&dev->request_module_wk);
  1811. }
  1812. #else
  1813. #define request_modules(dev)
  1814. #endif /* CONFIG_MODULES */
  1815. /*
  1816. * em28xx_usb_probe()
  1817. * checks for supported devices
  1818. */
  1819. static int em28xx_usb_probe(struct usb_interface *interface,
  1820. const struct usb_device_id *id)
  1821. {
  1822. const struct usb_endpoint_descriptor *endpoint;
  1823. struct usb_device *udev;
  1824. struct usb_interface *uif;
  1825. struct em28xx *dev = NULL;
  1826. int retval = -ENODEV;
  1827. int i, nr, ifnum, isoc_pipe;
  1828. char *speed;
  1829. char descr[255] = "";
  1830. udev = usb_get_dev(interface_to_usbdev(interface));
  1831. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  1832. /* Check to see next free device and mark as used */
  1833. nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
  1834. em28xx_devused |= 1<<nr;
  1835. /* Don't register audio interfaces */
  1836. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1837. em28xx_err(DRIVER_NAME " audio device (%04x:%04x): "
  1838. "interface %i, class %i\n",
  1839. le16_to_cpu(udev->descriptor.idVendor),
  1840. le16_to_cpu(udev->descriptor.idProduct),
  1841. ifnum,
  1842. interface->altsetting[0].desc.bInterfaceClass);
  1843. em28xx_devused &= ~(1<<nr);
  1844. return -ENODEV;
  1845. }
  1846. endpoint = &interface->cur_altsetting->endpoint[0].desc;
  1847. /* check if the device has the iso in endpoint at the correct place */
  1848. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) ==
  1849. USB_ENDPOINT_XFER_ISOC &&
  1850. (interface->altsetting[1].endpoint[0].desc.wMaxPacketSize == 940))
  1851. {
  1852. /* It's a newer em2874/em2875 device */
  1853. isoc_pipe = 0;
  1854. } else {
  1855. int check_interface = 1;
  1856. isoc_pipe = 1;
  1857. endpoint = &interface->cur_altsetting->endpoint[1].desc;
  1858. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1859. USB_ENDPOINT_XFER_ISOC)
  1860. check_interface = 0;
  1861. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
  1862. check_interface = 0;
  1863. if (!check_interface) {
  1864. em28xx_err(DRIVER_NAME " video device (%04x:%04x): "
  1865. "interface %i, class %i found.\n",
  1866. le16_to_cpu(udev->descriptor.idVendor),
  1867. le16_to_cpu(udev->descriptor.idProduct),
  1868. ifnum,
  1869. interface->altsetting[0].desc.bInterfaceClass);
  1870. em28xx_err(DRIVER_NAME " This is an anciliary "
  1871. "interface not used by the driver\n");
  1872. em28xx_devused &= ~(1<<nr);
  1873. return -ENODEV;
  1874. }
  1875. }
  1876. switch (udev->speed) {
  1877. case USB_SPEED_LOW:
  1878. speed = "1.5";
  1879. break;
  1880. case USB_SPEED_UNKNOWN:
  1881. case USB_SPEED_FULL:
  1882. speed = "12";
  1883. break;
  1884. case USB_SPEED_HIGH:
  1885. speed = "480";
  1886. break;
  1887. default:
  1888. speed = "unknown";
  1889. }
  1890. if (udev->manufacturer)
  1891. strlcpy(descr, udev->manufacturer, sizeof(descr));
  1892. if (udev->product) {
  1893. if (*descr)
  1894. strlcat(descr, " ", sizeof(descr));
  1895. strlcat(descr, udev->product, sizeof(descr));
  1896. }
  1897. if (*descr)
  1898. strlcat(descr, " ", sizeof(descr));
  1899. printk(DRIVER_NAME ": New device %s@ %s Mbps "
  1900. "(%04x:%04x, interface %d, class %d)\n",
  1901. descr,
  1902. speed,
  1903. le16_to_cpu(udev->descriptor.idVendor),
  1904. le16_to_cpu(udev->descriptor.idProduct),
  1905. ifnum,
  1906. interface->altsetting->desc.bInterfaceNumber);
  1907. if (nr >= EM28XX_MAXBOARDS) {
  1908. printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
  1909. EM28XX_MAXBOARDS);
  1910. em28xx_devused &= ~(1<<nr);
  1911. return -ENOMEM;
  1912. }
  1913. /* allocate memory for our device state and initialize it */
  1914. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1915. if (dev == NULL) {
  1916. em28xx_err(DRIVER_NAME ": out of memory!\n");
  1917. em28xx_devused &= ~(1<<nr);
  1918. return -ENOMEM;
  1919. }
  1920. snprintf(dev->name, 29, "em28xx #%d", nr);
  1921. dev->devno = nr;
  1922. dev->model = id->driver_info;
  1923. dev->alt = -1;
  1924. /* Checks if audio is provided by some interface */
  1925. for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
  1926. uif = udev->config->interface[i];
  1927. if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1928. dev->has_audio_class = 1;
  1929. break;
  1930. }
  1931. }
  1932. /* compute alternate max packet sizes */
  1933. uif = udev->actconfig->interface[0];
  1934. dev->num_alt = uif->num_altsetting;
  1935. em28xx_videodbg("Alternate settings: %i\n", dev->num_alt);
  1936. /* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
  1937. dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
  1938. if (dev->alt_max_pkt_size == NULL) {
  1939. em28xx_errdev("out of memory!\n");
  1940. em28xx_devused &= ~(1<<nr);
  1941. kfree(dev);
  1942. return -ENOMEM;
  1943. }
  1944. for (i = 0; i < dev->num_alt ; i++) {
  1945. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[isoc_pipe].desc.
  1946. wMaxPacketSize);
  1947. dev->alt_max_pkt_size[i] =
  1948. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1949. em28xx_videodbg("Alternate setting %i, max size= %i\n", i,
  1950. dev->alt_max_pkt_size[i]);
  1951. }
  1952. if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
  1953. dev->model = card[nr];
  1954. /* allocate device struct */
  1955. mutex_init(&dev->lock);
  1956. mutex_lock(&dev->lock);
  1957. retval = em28xx_init_dev(&dev, udev, nr);
  1958. if (retval) {
  1959. em28xx_devused &= ~(1<<dev->devno);
  1960. kfree(dev);
  1961. return retval;
  1962. }
  1963. /* save our data pointer in this interface device */
  1964. usb_set_intfdata(interface, dev);
  1965. request_modules(dev);
  1966. /* Should be the last thing to do, to avoid newer udev's to
  1967. open the device before fully initializing it
  1968. */
  1969. mutex_unlock(&dev->lock);
  1970. return 0;
  1971. }
  1972. /*
  1973. * em28xx_usb_disconnect()
  1974. * called when the device gets diconencted
  1975. * video device will be unregistered on v4l2_close in case it is still open
  1976. */
  1977. static void em28xx_usb_disconnect(struct usb_interface *interface)
  1978. {
  1979. struct em28xx *dev;
  1980. struct em28xx_ops *ops = NULL;
  1981. dev = usb_get_intfdata(interface);
  1982. usb_set_intfdata(interface, NULL);
  1983. if (!dev)
  1984. return;
  1985. em28xx_info("disconnecting %s\n", dev->vdev->name);
  1986. /* wait until all current v4l2 io is finished then deallocate
  1987. resources */
  1988. mutex_lock(&dev->lock);
  1989. wake_up_interruptible_all(&dev->open);
  1990. if (dev->users) {
  1991. em28xx_warn
  1992. ("device /dev/video%d is open! Deregistration and memory "
  1993. "deallocation are deferred on close.\n",
  1994. dev->vdev->num);
  1995. dev->state |= DEV_MISCONFIGURED;
  1996. em28xx_uninit_isoc(dev);
  1997. dev->state |= DEV_DISCONNECTED;
  1998. wake_up_interruptible(&dev->wait_frame);
  1999. wake_up_interruptible(&dev->wait_stream);
  2000. } else {
  2001. dev->state |= DEV_DISCONNECTED;
  2002. em28xx_release_resources(dev);
  2003. }
  2004. mutex_unlock(&dev->lock);
  2005. mutex_lock(&em28xx_extension_devlist_lock);
  2006. if (!list_empty(&em28xx_extension_devlist)) {
  2007. list_for_each_entry(ops, &em28xx_extension_devlist, next) {
  2008. ops->fini(dev);
  2009. }
  2010. }
  2011. mutex_unlock(&em28xx_extension_devlist_lock);
  2012. if (!dev->users) {
  2013. kfree(dev->alt_max_pkt_size);
  2014. kfree(dev);
  2015. }
  2016. }
  2017. static struct usb_driver em28xx_usb_driver = {
  2018. .name = "em28xx",
  2019. .probe = em28xx_usb_probe,
  2020. .disconnect = em28xx_usb_disconnect,
  2021. .id_table = em28xx_id_table,
  2022. };
  2023. static int __init em28xx_module_init(void)
  2024. {
  2025. int result;
  2026. printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
  2027. (EM28XX_VERSION_CODE >> 16) & 0xff,
  2028. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  2029. #ifdef SNAPSHOT
  2030. printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
  2031. SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
  2032. #endif
  2033. /* register this driver with the USB subsystem */
  2034. result = usb_register(&em28xx_usb_driver);
  2035. if (result)
  2036. em28xx_err(DRIVER_NAME
  2037. " usb_register failed. Error number %d.\n", result);
  2038. return result;
  2039. }
  2040. static void __exit em28xx_module_exit(void)
  2041. {
  2042. /* deregister this driver with the USB subsystem */
  2043. usb_deregister(&em28xx_usb_driver);
  2044. }
  2045. module_init(em28xx_module_init);
  2046. module_exit(em28xx_module_exit);