em28xx-video.c 58 KB

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