em28xx-video.c 55 KB

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