em28xx-video.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361
  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->is_em2800) {
  432. retval = em28xx_write_regs_req(dev, 0x00, 0x06, "\x40", 1);
  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_regs_req(dev, 0x00, 0x0e, "\xC0", 1); audio register */
  441. /* em28xx_write_regs_req(dev, 0x00, 0x0f, "\x80", 1); clk register */
  442. em28xx_write_regs_req(dev, 0x00, 0x11, "\x51", 1);
  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. route.input = INPUT(dev->ctl_input)->vmux;
  458. route.output = 0;
  459. em28xx_i2c_call_clients(dev, VIDIOC_INT_RESET, NULL);
  460. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  461. em28xx_i2c_call_clients(dev, VIDIOC_STREAMON, NULL);
  462. }
  463. static void video_mux(struct em28xx *dev, int index)
  464. {
  465. struct v4l2_routing route;
  466. route.input = INPUT(index)->vmux;
  467. route.output = 0;
  468. dev->ctl_input = index;
  469. dev->ctl_ainput = INPUT(index)->amux;
  470. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_VIDEO_ROUTING, &route);
  471. if (dev->has_msp34xx) {
  472. if (dev->i2s_speed) {
  473. em28xx_i2c_call_clients(dev, VIDIOC_INT_I2S_CLOCK_FREQ,
  474. &dev->i2s_speed);
  475. }
  476. route.input = dev->ctl_ainput;
  477. route.output = MSP_OUTPUT(MSP_SC_IN_DSP_SCART1);
  478. /* Note: this is msp3400 specific */
  479. em28xx_i2c_call_clients(dev, VIDIOC_INT_S_AUDIO_ROUTING,
  480. &route);
  481. }
  482. em28xx_audio_analog_set(dev);
  483. }
  484. /* Usage lock check functions */
  485. static int res_get(struct em28xx_fh *fh)
  486. {
  487. struct em28xx *dev = fh->dev;
  488. int rc = 0;
  489. /* This instance already has stream_on */
  490. if (fh->stream_on)
  491. return rc;
  492. if (dev->stream_on)
  493. return -EINVAL;
  494. mutex_lock(&dev->lock);
  495. dev->stream_on = 1;
  496. fh->stream_on = 1;
  497. mutex_unlock(&dev->lock);
  498. return rc;
  499. }
  500. static int res_check(struct em28xx_fh *fh)
  501. {
  502. return (fh->stream_on);
  503. }
  504. static void res_free(struct em28xx_fh *fh)
  505. {
  506. struct em28xx *dev = fh->dev;
  507. mutex_lock(&dev->lock);
  508. fh->stream_on = 0;
  509. dev->stream_on = 0;
  510. mutex_unlock(&dev->lock);
  511. }
  512. /*
  513. * em28xx_get_ctrl()
  514. * return the current saturation, brightness or contrast, mute state
  515. */
  516. static int em28xx_get_ctrl(struct em28xx *dev, struct v4l2_control *ctrl)
  517. {
  518. switch (ctrl->id) {
  519. case V4L2_CID_AUDIO_MUTE:
  520. ctrl->value = dev->mute;
  521. return 0;
  522. case V4L2_CID_AUDIO_VOLUME:
  523. ctrl->value = dev->volume;
  524. return 0;
  525. default:
  526. return -EINVAL;
  527. }
  528. }
  529. /*
  530. * em28xx_set_ctrl()
  531. * mute or set new saturation, brightness or contrast
  532. */
  533. static int em28xx_set_ctrl(struct em28xx *dev, const struct v4l2_control *ctrl)
  534. {
  535. switch (ctrl->id) {
  536. case V4L2_CID_AUDIO_MUTE:
  537. if (ctrl->value != dev->mute) {
  538. dev->mute = ctrl->value;
  539. return em28xx_audio_analog_set(dev);
  540. }
  541. return 0;
  542. case V4L2_CID_AUDIO_VOLUME:
  543. dev->volume = ctrl->value;
  544. return em28xx_audio_analog_set(dev);
  545. default:
  546. return -EINVAL;
  547. }
  548. }
  549. static int check_dev(struct em28xx *dev)
  550. {
  551. if (dev->state & DEV_DISCONNECTED) {
  552. em28xx_errdev("v4l2 ioctl: device not present\n");
  553. return -ENODEV;
  554. }
  555. if (dev->state & DEV_MISCONFIGURED) {
  556. em28xx_errdev("v4l2 ioctl: device is misconfigured; "
  557. "close and open it again\n");
  558. return -EIO;
  559. }
  560. return 0;
  561. }
  562. static void get_scale(struct em28xx *dev,
  563. unsigned int width, unsigned int height,
  564. unsigned int *hscale, unsigned int *vscale)
  565. {
  566. unsigned int maxw = norm_maxw(dev);
  567. unsigned int maxh = norm_maxh(dev);
  568. *hscale = (((unsigned long)maxw) << 12) / width - 4096L;
  569. if (*hscale >= 0x4000)
  570. *hscale = 0x3fff;
  571. *vscale = (((unsigned long)maxh) << 12) / height - 4096L;
  572. if (*vscale >= 0x4000)
  573. *vscale = 0x3fff;
  574. }
  575. /* ------------------------------------------------------------------
  576. IOCTL vidioc handling
  577. ------------------------------------------------------------------*/
  578. static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  579. struct v4l2_format *f)
  580. {
  581. struct em28xx_fh *fh = priv;
  582. struct em28xx *dev = fh->dev;
  583. mutex_lock(&dev->lock);
  584. f->fmt.pix.width = dev->width;
  585. f->fmt.pix.height = dev->height;
  586. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  587. f->fmt.pix.bytesperline = dev->width * 2;
  588. f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
  589. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  590. /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  591. f->fmt.pix.field = dev->interlaced ?
  592. V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP;
  593. mutex_unlock(&dev->lock);
  594. return 0;
  595. }
  596. static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  597. struct v4l2_format *f)
  598. {
  599. struct em28xx_fh *fh = priv;
  600. struct em28xx *dev = fh->dev;
  601. int width = f->fmt.pix.width;
  602. int height = f->fmt.pix.height;
  603. unsigned int maxw = norm_maxw(dev);
  604. unsigned int maxh = norm_maxh(dev);
  605. unsigned int hscale, vscale;
  606. /* width must even because of the YUYV format
  607. height must be even because of interlacing */
  608. height &= 0xfffe;
  609. width &= 0xfffe;
  610. if (height < 32)
  611. height = 32;
  612. if (height > maxh)
  613. height = maxh;
  614. if (width < 48)
  615. width = 48;
  616. if (width > maxw)
  617. width = maxw;
  618. mutex_lock(&dev->lock);
  619. if (dev->is_em2800) {
  620. /* the em2800 can only scale down to 50% */
  621. if (height % (maxh / 2))
  622. height = maxh;
  623. if (width % (maxw / 2))
  624. width = maxw;
  625. /* according to empiatech support */
  626. /* the MaxPacketSize is to small to support */
  627. /* framesizes larger than 640x480 @ 30 fps */
  628. /* or 640x576 @ 25 fps. As this would cut */
  629. /* of a part of the image we prefer */
  630. /* 360x576 or 360x480 for now */
  631. if (width == maxw && height == maxh)
  632. width /= 2;
  633. }
  634. get_scale(dev, width, height, &hscale, &vscale);
  635. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  636. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  637. f->fmt.pix.width = width;
  638. f->fmt.pix.height = height;
  639. f->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  640. f->fmt.pix.bytesperline = width * 2;
  641. f->fmt.pix.sizeimage = width * 2 * height;
  642. f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  643. f->fmt.pix.field = V4L2_FIELD_INTERLACED;
  644. mutex_unlock(&dev->lock);
  645. return 0;
  646. }
  647. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  648. struct v4l2_format *f)
  649. {
  650. struct em28xx_fh *fh = priv;
  651. struct em28xx *dev = fh->dev;
  652. int rc;
  653. rc = check_dev(dev);
  654. if (rc < 0)
  655. return rc;
  656. vidioc_try_fmt_vid_cap(file, priv, f);
  657. mutex_lock(&dev->lock);
  658. if (videobuf_queue_is_busy(&fh->vb_vidq)) {
  659. em28xx_errdev("%s queue busy\n", __func__);
  660. rc = -EBUSY;
  661. goto out;
  662. }
  663. if (dev->stream_on && !fh->stream_on) {
  664. em28xx_errdev("%s device in use by another fh\n", __func__);
  665. rc = -EBUSY;
  666. goto out;
  667. }
  668. /* set new image size */
  669. dev->width = f->fmt.pix.width;
  670. dev->height = f->fmt.pix.height;
  671. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  672. em28xx_set_alternate(dev);
  673. em28xx_resolution_set(dev);
  674. rc = 0;
  675. out:
  676. mutex_unlock(&dev->lock);
  677. return rc;
  678. }
  679. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id * norm)
  680. {
  681. struct em28xx_fh *fh = priv;
  682. struct em28xx *dev = fh->dev;
  683. struct v4l2_format f;
  684. int rc;
  685. rc = check_dev(dev);
  686. if (rc < 0)
  687. return rc;
  688. mutex_lock(&dev->lock);
  689. dev->norm = *norm;
  690. mutex_unlock(&dev->lock);
  691. /* Adjusts width/height, if needed */
  692. f.fmt.pix.width = dev->width;
  693. f.fmt.pix.height = dev->height;
  694. vidioc_try_fmt_vid_cap(file, priv, &f);
  695. mutex_lock(&dev->lock);
  696. /* set new image size */
  697. dev->width = f.fmt.pix.width;
  698. dev->height = f.fmt.pix.height;
  699. get_scale(dev, dev->width, dev->height, &dev->hscale, &dev->vscale);
  700. em28xx_resolution_set(dev);
  701. em28xx_i2c_call_clients(dev, VIDIOC_S_STD, &dev->norm);
  702. mutex_unlock(&dev->lock);
  703. return 0;
  704. }
  705. static const char *iname[] = {
  706. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  707. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  708. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  709. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  710. [EM28XX_VMUX_SVIDEO] = "S-Video",
  711. [EM28XX_VMUX_TELEVISION] = "Television",
  712. [EM28XX_VMUX_CABLE] = "Cable TV",
  713. [EM28XX_VMUX_DVB] = "DVB",
  714. [EM28XX_VMUX_DEBUG] = "for debug only",
  715. };
  716. static int vidioc_enum_input(struct file *file, void *priv,
  717. struct v4l2_input *i)
  718. {
  719. struct em28xx_fh *fh = priv;
  720. struct em28xx *dev = fh->dev;
  721. unsigned int n;
  722. n = i->index;
  723. if (n >= MAX_EM28XX_INPUT)
  724. return -EINVAL;
  725. if (0 == INPUT(n)->type)
  726. return -EINVAL;
  727. i->index = n;
  728. i->type = V4L2_INPUT_TYPE_CAMERA;
  729. strcpy(i->name, iname[INPUT(n)->type]);
  730. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  731. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  732. i->type = V4L2_INPUT_TYPE_TUNER;
  733. i->std = dev->vdev->tvnorms;
  734. return 0;
  735. }
  736. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  737. {
  738. struct em28xx_fh *fh = priv;
  739. struct em28xx *dev = fh->dev;
  740. *i = dev->ctl_input;
  741. return 0;
  742. }
  743. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  744. {
  745. struct em28xx_fh *fh = priv;
  746. struct em28xx *dev = fh->dev;
  747. int rc;
  748. rc = check_dev(dev);
  749. if (rc < 0)
  750. return rc;
  751. if (i >= MAX_EM28XX_INPUT)
  752. return -EINVAL;
  753. if (0 == INPUT(i)->type)
  754. return -EINVAL;
  755. mutex_lock(&dev->lock);
  756. video_mux(dev, i);
  757. mutex_unlock(&dev->lock);
  758. return 0;
  759. }
  760. static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  761. {
  762. struct em28xx_fh *fh = priv;
  763. struct em28xx *dev = fh->dev;
  764. unsigned int index = a->index;
  765. if (a->index > 1)
  766. return -EINVAL;
  767. index = dev->ctl_ainput;
  768. if (index == 0)
  769. strcpy(a->name, "Television");
  770. else
  771. strcpy(a->name, "Line In");
  772. a->capability = V4L2_AUDCAP_STEREO;
  773. a->index = index;
  774. return 0;
  775. }
  776. static int vidioc_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  777. {
  778. struct em28xx_fh *fh = priv;
  779. struct em28xx *dev = fh->dev;
  780. if (a->index != dev->ctl_ainput)
  781. return -EINVAL;
  782. return 0;
  783. }
  784. static int vidioc_queryctrl(struct file *file, void *priv,
  785. struct v4l2_queryctrl *qc)
  786. {
  787. struct em28xx_fh *fh = priv;
  788. struct em28xx *dev = fh->dev;
  789. int id = qc->id;
  790. int i;
  791. int rc;
  792. rc = check_dev(dev);
  793. if (rc < 0)
  794. return rc;
  795. memset(qc, 0, sizeof(*qc));
  796. qc->id = id;
  797. if (!dev->has_msp34xx) {
  798. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  799. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  800. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  801. return 0;
  802. }
  803. }
  804. }
  805. mutex_lock(&dev->lock);
  806. em28xx_i2c_call_clients(dev, VIDIOC_QUERYCTRL, qc);
  807. mutex_unlock(&dev->lock);
  808. if (qc->type)
  809. return 0;
  810. else
  811. return -EINVAL;
  812. }
  813. static int vidioc_g_ctrl(struct file *file, void *priv,
  814. struct v4l2_control *ctrl)
  815. {
  816. struct em28xx_fh *fh = priv;
  817. struct em28xx *dev = fh->dev;
  818. int rc;
  819. rc = check_dev(dev);
  820. if (rc < 0)
  821. return rc;
  822. mutex_lock(&dev->lock);
  823. if (!dev->has_msp34xx)
  824. rc = em28xx_get_ctrl(dev, ctrl);
  825. else
  826. rc = -EINVAL;
  827. if (rc == -EINVAL) {
  828. em28xx_i2c_call_clients(dev, VIDIOC_G_CTRL, ctrl);
  829. rc = 0;
  830. }
  831. mutex_unlock(&dev->lock);
  832. return rc;
  833. }
  834. static int vidioc_s_ctrl(struct file *file, void *priv,
  835. struct v4l2_control *ctrl)
  836. {
  837. struct em28xx_fh *fh = priv;
  838. struct em28xx *dev = fh->dev;
  839. u8 i;
  840. int rc;
  841. rc = check_dev(dev);
  842. if (rc < 0)
  843. return rc;
  844. mutex_lock(&dev->lock);
  845. if (dev->has_msp34xx)
  846. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  847. else {
  848. rc = 1;
  849. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  850. if (ctrl->id == em28xx_qctrl[i].id) {
  851. if (ctrl->value < em28xx_qctrl[i].minimum ||
  852. ctrl->value > em28xx_qctrl[i].maximum) {
  853. rc = -ERANGE;
  854. break;
  855. }
  856. rc = em28xx_set_ctrl(dev, ctrl);
  857. break;
  858. }
  859. }
  860. }
  861. /* Control not found - try to send it to the attached devices */
  862. if (rc == 1) {
  863. em28xx_i2c_call_clients(dev, VIDIOC_S_CTRL, ctrl);
  864. rc = 0;
  865. }
  866. mutex_unlock(&dev->lock);
  867. return rc;
  868. }
  869. static int vidioc_g_tuner(struct file *file, void *priv,
  870. struct v4l2_tuner *t)
  871. {
  872. struct em28xx_fh *fh = priv;
  873. struct em28xx *dev = fh->dev;
  874. int rc;
  875. rc = check_dev(dev);
  876. if (rc < 0)
  877. return rc;
  878. if (0 != t->index)
  879. return -EINVAL;
  880. strcpy(t->name, "Tuner");
  881. mutex_lock(&dev->lock);
  882. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  883. mutex_unlock(&dev->lock);
  884. return 0;
  885. }
  886. static int vidioc_s_tuner(struct file *file, void *priv,
  887. struct v4l2_tuner *t)
  888. {
  889. struct em28xx_fh *fh = priv;
  890. struct em28xx *dev = fh->dev;
  891. int rc;
  892. rc = check_dev(dev);
  893. if (rc < 0)
  894. return rc;
  895. if (0 != t->index)
  896. return -EINVAL;
  897. mutex_lock(&dev->lock);
  898. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  899. mutex_unlock(&dev->lock);
  900. return 0;
  901. }
  902. static int vidioc_g_frequency(struct file *file, void *priv,
  903. struct v4l2_frequency *f)
  904. {
  905. struct em28xx_fh *fh = priv;
  906. struct em28xx *dev = fh->dev;
  907. f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  908. f->frequency = dev->ctl_freq;
  909. return 0;
  910. }
  911. static int vidioc_s_frequency(struct file *file, void *priv,
  912. struct v4l2_frequency *f)
  913. {
  914. struct em28xx_fh *fh = priv;
  915. struct em28xx *dev = fh->dev;
  916. int rc;
  917. rc = check_dev(dev);
  918. if (rc < 0)
  919. return rc;
  920. if (0 != f->tuner)
  921. return -EINVAL;
  922. if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV))
  923. return -EINVAL;
  924. if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO))
  925. return -EINVAL;
  926. mutex_lock(&dev->lock);
  927. dev->ctl_freq = f->frequency;
  928. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  929. mutex_unlock(&dev->lock);
  930. return 0;
  931. }
  932. #ifdef CONFIG_VIDEO_ADV_DEBUG
  933. static int em28xx_reg_len(int reg)
  934. {
  935. switch (reg) {
  936. case EM28XX_R40_AC97LSB:
  937. case EM28XX_R30_HSCALELOW:
  938. case EM28XX_R32_VSCALELOW:
  939. return 2;
  940. default:
  941. return 1;
  942. }
  943. }
  944. static int vidioc_g_register(struct file *file, void *priv,
  945. struct v4l2_register *reg)
  946. {
  947. struct em28xx_fh *fh = priv;
  948. struct em28xx *dev = fh->dev;
  949. int ret;
  950. if (!v4l2_chip_match_host(reg->match_type, reg->match_chip))
  951. return -EINVAL;
  952. if (em28xx_reg_len(reg->reg) == 1) {
  953. ret = em28xx_read_reg(dev, reg->reg);
  954. if (ret < 0)
  955. return ret;
  956. reg->val = ret;
  957. } else {
  958. __le64 val = 0;
  959. ret = em28xx_read_reg_req_len(dev, USB_REQ_GET_STATUS,
  960. reg->reg, (char *)&val, 2);
  961. if (ret < 0)
  962. return ret;
  963. reg->val = le64_to_cpu(val);
  964. }
  965. return 0;
  966. }
  967. static int vidioc_s_register(struct file *file, void *priv,
  968. struct v4l2_register *reg)
  969. {
  970. struct em28xx_fh *fh = priv;
  971. struct em28xx *dev = fh->dev;
  972. __le64 buf;
  973. buf = cpu_to_le64(reg->val);
  974. return em28xx_write_regs(dev, reg->reg, (char *)&buf,
  975. em28xx_reg_len(reg->reg));
  976. }
  977. #endif
  978. static int vidioc_cropcap(struct file *file, void *priv,
  979. struct v4l2_cropcap *cc)
  980. {
  981. struct em28xx_fh *fh = priv;
  982. struct em28xx *dev = fh->dev;
  983. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  984. return -EINVAL;
  985. cc->bounds.left = 0;
  986. cc->bounds.top = 0;
  987. cc->bounds.width = dev->width;
  988. cc->bounds.height = dev->height;
  989. cc->defrect = cc->bounds;
  990. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  991. cc->pixelaspect.denominator = 59;
  992. return 0;
  993. }
  994. static int vidioc_streamon(struct file *file, void *priv,
  995. enum v4l2_buf_type type)
  996. {
  997. struct em28xx_fh *fh = priv;
  998. struct em28xx *dev = fh->dev;
  999. int rc;
  1000. rc = check_dev(dev);
  1001. if (rc < 0)
  1002. return rc;
  1003. if (unlikely(res_get(fh) < 0))
  1004. return -EBUSY;
  1005. return (videobuf_streamon(&fh->vb_vidq));
  1006. }
  1007. static int vidioc_streamoff(struct file *file, void *priv,
  1008. enum v4l2_buf_type type)
  1009. {
  1010. struct em28xx_fh *fh = priv;
  1011. struct em28xx *dev = fh->dev;
  1012. int rc;
  1013. rc = check_dev(dev);
  1014. if (rc < 0)
  1015. return rc;
  1016. if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1017. return -EINVAL;
  1018. if (type != fh->type)
  1019. return -EINVAL;
  1020. videobuf_streamoff(&fh->vb_vidq);
  1021. res_free(fh);
  1022. return 0;
  1023. }
  1024. static int vidioc_querycap(struct file *file, void *priv,
  1025. struct v4l2_capability *cap)
  1026. {
  1027. struct em28xx_fh *fh = priv;
  1028. struct em28xx *dev = fh->dev;
  1029. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1030. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1031. strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
  1032. cap->version = EM28XX_VERSION_CODE;
  1033. cap->capabilities =
  1034. V4L2_CAP_SLICED_VBI_CAPTURE |
  1035. V4L2_CAP_VIDEO_CAPTURE |
  1036. V4L2_CAP_AUDIO |
  1037. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1038. if (dev->tuner_type != TUNER_ABSENT)
  1039. cap->capabilities |= V4L2_CAP_TUNER;
  1040. return 0;
  1041. }
  1042. static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
  1043. struct v4l2_fmtdesc *fmtd)
  1044. {
  1045. if (fmtd->index != 0)
  1046. return -EINVAL;
  1047. fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1048. strcpy(fmtd->description, "Packed YUY2");
  1049. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  1050. memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
  1051. return 0;
  1052. }
  1053. /* Sliced VBI ioctls */
  1054. static int vidioc_g_fmt_sliced_vbi_cap(struct file *file, void *priv,
  1055. struct v4l2_format *f)
  1056. {
  1057. struct em28xx_fh *fh = priv;
  1058. struct em28xx *dev = fh->dev;
  1059. int rc;
  1060. rc = check_dev(dev);
  1061. if (rc < 0)
  1062. return rc;
  1063. mutex_lock(&dev->lock);
  1064. f->fmt.sliced.service_set = 0;
  1065. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1066. if (f->fmt.sliced.service_set == 0)
  1067. rc = -EINVAL;
  1068. mutex_unlock(&dev->lock);
  1069. return rc;
  1070. }
  1071. static int vidioc_try_set_sliced_vbi_cap(struct file *file, void *priv,
  1072. struct v4l2_format *f)
  1073. {
  1074. struct em28xx_fh *fh = priv;
  1075. struct em28xx *dev = fh->dev;
  1076. int rc;
  1077. rc = check_dev(dev);
  1078. if (rc < 0)
  1079. return rc;
  1080. mutex_lock(&dev->lock);
  1081. em28xx_i2c_call_clients(dev, VIDIOC_G_FMT, f);
  1082. mutex_unlock(&dev->lock);
  1083. if (f->fmt.sliced.service_set == 0)
  1084. return -EINVAL;
  1085. return 0;
  1086. }
  1087. static int vidioc_reqbufs(struct file *file, void *priv,
  1088. struct v4l2_requestbuffers *rb)
  1089. {
  1090. struct em28xx_fh *fh = priv;
  1091. struct em28xx *dev = fh->dev;
  1092. int rc;
  1093. rc = check_dev(dev);
  1094. if (rc < 0)
  1095. return rc;
  1096. return (videobuf_reqbufs(&fh->vb_vidq, rb));
  1097. }
  1098. static int vidioc_querybuf(struct file *file, void *priv,
  1099. struct v4l2_buffer *b)
  1100. {
  1101. struct em28xx_fh *fh = priv;
  1102. struct em28xx *dev = fh->dev;
  1103. int rc;
  1104. rc = check_dev(dev);
  1105. if (rc < 0)
  1106. return rc;
  1107. return (videobuf_querybuf(&fh->vb_vidq, b));
  1108. }
  1109. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1110. {
  1111. struct em28xx_fh *fh = priv;
  1112. struct em28xx *dev = fh->dev;
  1113. int rc;
  1114. rc = check_dev(dev);
  1115. if (rc < 0)
  1116. return rc;
  1117. return (videobuf_qbuf(&fh->vb_vidq, b));
  1118. }
  1119. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  1120. {
  1121. struct em28xx_fh *fh = priv;
  1122. struct em28xx *dev = fh->dev;
  1123. int rc;
  1124. rc = check_dev(dev);
  1125. if (rc < 0)
  1126. return rc;
  1127. return (videobuf_dqbuf(&fh->vb_vidq, b,
  1128. file->f_flags & O_NONBLOCK));
  1129. }
  1130. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1131. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  1132. {
  1133. struct em28xx_fh *fh = priv;
  1134. return videobuf_cgmbuf(&fh->vb_vidq, mbuf, 8);
  1135. }
  1136. #endif
  1137. /* ----------------------------------------------------------- */
  1138. /* RADIO ESPECIFIC IOCTLS */
  1139. /* ----------------------------------------------------------- */
  1140. static int radio_querycap(struct file *file, void *priv,
  1141. struct v4l2_capability *cap)
  1142. {
  1143. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1144. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1145. strlcpy(cap->card, em28xx_boards[dev->model].name, sizeof(cap->card));
  1146. strlcpy(cap->bus_info, dev->udev->dev.bus_id, sizeof(cap->bus_info));
  1147. cap->version = EM28XX_VERSION_CODE;
  1148. cap->capabilities = V4L2_CAP_TUNER;
  1149. return 0;
  1150. }
  1151. static int radio_g_tuner(struct file *file, void *priv,
  1152. struct v4l2_tuner *t)
  1153. {
  1154. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1155. if (unlikely(t->index > 0))
  1156. return -EINVAL;
  1157. strcpy(t->name, "Radio");
  1158. t->type = V4L2_TUNER_RADIO;
  1159. em28xx_i2c_call_clients(dev, VIDIOC_G_TUNER, t);
  1160. return 0;
  1161. }
  1162. static int radio_enum_input(struct file *file, void *priv,
  1163. struct v4l2_input *i)
  1164. {
  1165. if (i->index != 0)
  1166. return -EINVAL;
  1167. strcpy(i->name, "Radio");
  1168. i->type = V4L2_INPUT_TYPE_TUNER;
  1169. return 0;
  1170. }
  1171. static int radio_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  1172. {
  1173. if (unlikely(a->index))
  1174. return -EINVAL;
  1175. strcpy(a->name, "Radio");
  1176. return 0;
  1177. }
  1178. static int radio_s_tuner(struct file *file, void *priv,
  1179. struct v4l2_tuner *t)
  1180. {
  1181. struct em28xx *dev = ((struct em28xx_fh *)priv)->dev;
  1182. if (0 != t->index)
  1183. return -EINVAL;
  1184. em28xx_i2c_call_clients(dev, VIDIOC_S_TUNER, t);
  1185. return 0;
  1186. }
  1187. static int radio_s_audio(struct file *file, void *fh,
  1188. struct v4l2_audio *a)
  1189. {
  1190. return 0;
  1191. }
  1192. static int radio_s_input(struct file *file, void *fh, unsigned int i)
  1193. {
  1194. return 0;
  1195. }
  1196. static int radio_queryctrl(struct file *file, void *priv,
  1197. struct v4l2_queryctrl *qc)
  1198. {
  1199. int i;
  1200. if (qc->id < V4L2_CID_BASE ||
  1201. qc->id >= V4L2_CID_LASTP1)
  1202. return -EINVAL;
  1203. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1204. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1205. memcpy(qc, &(em28xx_qctrl[i]), sizeof(*qc));
  1206. return 0;
  1207. }
  1208. }
  1209. return -EINVAL;
  1210. }
  1211. /*
  1212. * em28xx_v4l2_open()
  1213. * inits the device and starts isoc transfer
  1214. */
  1215. static int em28xx_v4l2_open(struct inode *inode, struct file *filp)
  1216. {
  1217. int minor = iminor(inode);
  1218. int errCode = 0, radio = 0;
  1219. struct em28xx *h, *dev = NULL;
  1220. struct em28xx_fh *fh;
  1221. enum v4l2_buf_type fh_type = 0;
  1222. mutex_lock(&em28xx_devlist_mutex);
  1223. list_for_each_entry(h, &em28xx_devlist, devlist) {
  1224. if (h->vdev->minor == minor) {
  1225. dev = h;
  1226. fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1227. }
  1228. if (h->vbi_dev->minor == minor) {
  1229. dev = h;
  1230. fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
  1231. }
  1232. if (h->radio_dev &&
  1233. h->radio_dev->minor == minor) {
  1234. radio = 1;
  1235. dev = h;
  1236. }
  1237. }
  1238. mutex_unlock(&em28xx_devlist_mutex);
  1239. if (NULL == dev)
  1240. return -ENODEV;
  1241. mutex_lock(&dev->lock);
  1242. em28xx_videodbg("open minor=%d type=%s users=%d\n",
  1243. minor, v4l2_type_names[fh_type], dev->users);
  1244. fh = kzalloc(sizeof(struct em28xx_fh), GFP_KERNEL);
  1245. if (!fh) {
  1246. em28xx_errdev("em28xx-video.c: Out of memory?!\n");
  1247. mutex_unlock(&dev->lock);
  1248. return -ENOMEM;
  1249. }
  1250. fh->dev = dev;
  1251. fh->radio = radio;
  1252. fh->type = fh_type;
  1253. filp->private_data = fh;
  1254. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
  1255. dev->width = norm_maxw(dev);
  1256. dev->height = norm_maxh(dev);
  1257. dev->hscale = 0;
  1258. dev->vscale = 0;
  1259. em28xx_set_mode(dev, EM28XX_ANALOG_MODE);
  1260. em28xx_set_alternate(dev);
  1261. em28xx_resolution_set(dev);
  1262. /* Needed, since GPIO might have disabled power of
  1263. some i2c device
  1264. */
  1265. em28xx_config_i2c(dev);
  1266. }
  1267. if (fh->radio) {
  1268. em28xx_videodbg("video_open: setting radio device\n");
  1269. em28xx_i2c_call_clients(dev, AUDC_SET_RADIO, NULL);
  1270. }
  1271. dev->users++;
  1272. videobuf_queue_vmalloc_init(&fh->vb_vidq, &em28xx_video_qops,
  1273. NULL, &dev->slock, fh->type, V4L2_FIELD_INTERLACED,
  1274. sizeof(struct em28xx_buffer), fh);
  1275. mutex_unlock(&dev->lock);
  1276. return errCode;
  1277. }
  1278. /*
  1279. * em28xx_realease_resources()
  1280. * unregisters the v4l2,i2c and usb devices
  1281. * called when the device gets disconected or at module unload
  1282. */
  1283. static void em28xx_release_resources(struct em28xx *dev)
  1284. {
  1285. /*FIXME: I2C IR should be disconnected */
  1286. em28xx_info("V4L2 devices /dev/video%d and /dev/vbi%d deregistered\n",
  1287. dev->vdev->num, dev->vbi_dev->num);
  1288. list_del(&dev->devlist);
  1289. if (dev->sbutton_input_dev)
  1290. em28xx_deregister_snapshot_button(dev);
  1291. if (dev->radio_dev) {
  1292. if (-1 != dev->radio_dev->minor)
  1293. video_unregister_device(dev->radio_dev);
  1294. else
  1295. video_device_release(dev->radio_dev);
  1296. dev->radio_dev = NULL;
  1297. }
  1298. if (dev->vbi_dev) {
  1299. if (-1 != dev->vbi_dev->minor)
  1300. video_unregister_device(dev->vbi_dev);
  1301. else
  1302. video_device_release(dev->vbi_dev);
  1303. dev->vbi_dev = NULL;
  1304. }
  1305. if (dev->vdev) {
  1306. if (-1 != dev->vdev->minor)
  1307. video_unregister_device(dev->vdev);
  1308. else
  1309. video_device_release(dev->vdev);
  1310. dev->vdev = NULL;
  1311. }
  1312. em28xx_i2c_unregister(dev);
  1313. usb_put_dev(dev->udev);
  1314. /* Mark device as unused */
  1315. em28xx_devused &= ~(1<<dev->devno);
  1316. }
  1317. /*
  1318. * em28xx_v4l2_close()
  1319. * stops streaming and deallocates all resources allocated by the v4l2
  1320. * calls and ioctls
  1321. */
  1322. static int em28xx_v4l2_close(struct inode *inode, struct file *filp)
  1323. {
  1324. struct em28xx_fh *fh = filp->private_data;
  1325. struct em28xx *dev = fh->dev;
  1326. int errCode;
  1327. em28xx_videodbg("users=%d\n", dev->users);
  1328. if (res_check(fh))
  1329. res_free(fh);
  1330. mutex_lock(&dev->lock);
  1331. if (dev->users == 1) {
  1332. videobuf_stop(&fh->vb_vidq);
  1333. videobuf_mmap_free(&fh->vb_vidq);
  1334. /* the device is already disconnect,
  1335. free the remaining resources */
  1336. if (dev->state & DEV_DISCONNECTED) {
  1337. em28xx_release_resources(dev);
  1338. mutex_unlock(&dev->lock);
  1339. kfree(dev);
  1340. return 0;
  1341. }
  1342. /* do this before setting alternate! */
  1343. em28xx_uninit_isoc(dev);
  1344. em28xx_set_mode(dev, EM28XX_MODE_UNDEFINED);
  1345. /* set alternate 0 */
  1346. dev->alt = 0;
  1347. em28xx_videodbg("setting alternate 0\n");
  1348. errCode = usb_set_interface(dev->udev, 0, 0);
  1349. if (errCode < 0) {
  1350. em28xx_errdev("cannot change alternate number to "
  1351. "0 (error=%i)\n", errCode);
  1352. }
  1353. }
  1354. kfree(fh);
  1355. dev->users--;
  1356. wake_up_interruptible_nr(&dev->open, 1);
  1357. mutex_unlock(&dev->lock);
  1358. return 0;
  1359. }
  1360. /*
  1361. * em28xx_v4l2_read()
  1362. * will allocate buffers when called for the first time
  1363. */
  1364. static ssize_t
  1365. em28xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
  1366. loff_t *pos)
  1367. {
  1368. struct em28xx_fh *fh = filp->private_data;
  1369. struct em28xx *dev = fh->dev;
  1370. int rc;
  1371. rc = check_dev(dev);
  1372. if (rc < 0)
  1373. return rc;
  1374. /* FIXME: read() is not prepared to allow changing the video
  1375. resolution while streaming. Seems a bug at em28xx_set_fmt
  1376. */
  1377. if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1378. if (unlikely(res_get(fh)))
  1379. return -EBUSY;
  1380. return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
  1381. filp->f_flags & O_NONBLOCK);
  1382. }
  1383. return 0;
  1384. }
  1385. /*
  1386. * em28xx_v4l2_poll()
  1387. * will allocate buffers when called for the first time
  1388. */
  1389. static unsigned int em28xx_v4l2_poll(struct file *filp, poll_table * wait)
  1390. {
  1391. struct em28xx_fh *fh = filp->private_data;
  1392. struct em28xx *dev = fh->dev;
  1393. int rc;
  1394. rc = check_dev(dev);
  1395. if (rc < 0)
  1396. return rc;
  1397. if (unlikely(res_get(fh) < 0))
  1398. return POLLERR;
  1399. if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type)
  1400. return POLLERR;
  1401. return videobuf_poll_stream(filp, &fh->vb_vidq, wait);
  1402. }
  1403. /*
  1404. * em28xx_v4l2_mmap()
  1405. */
  1406. static int em28xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
  1407. {
  1408. struct em28xx_fh *fh = filp->private_data;
  1409. struct em28xx *dev = fh->dev;
  1410. int rc;
  1411. if (unlikely(res_get(fh) < 0))
  1412. return -EBUSY;
  1413. rc = check_dev(dev);
  1414. if (rc < 0)
  1415. return rc;
  1416. rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
  1417. em28xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
  1418. (unsigned long)vma->vm_start,
  1419. (unsigned long)vma->vm_end-(unsigned long)vma->vm_start,
  1420. rc);
  1421. return rc;
  1422. }
  1423. static const struct file_operations em28xx_v4l_fops = {
  1424. .owner = THIS_MODULE,
  1425. .open = em28xx_v4l2_open,
  1426. .release = em28xx_v4l2_close,
  1427. .read = em28xx_v4l2_read,
  1428. .poll = em28xx_v4l2_poll,
  1429. .mmap = em28xx_v4l2_mmap,
  1430. .ioctl = video_ioctl2,
  1431. .llseek = no_llseek,
  1432. .compat_ioctl = v4l_compat_ioctl32,
  1433. };
  1434. static const struct v4l2_ioctl_ops video_ioctl_ops = {
  1435. .vidioc_querycap = vidioc_querycap,
  1436. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
  1437. .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
  1438. .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
  1439. .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
  1440. .vidioc_g_audio = vidioc_g_audio,
  1441. .vidioc_s_audio = vidioc_s_audio,
  1442. .vidioc_cropcap = vidioc_cropcap,
  1443. .vidioc_g_fmt_sliced_vbi_cap = vidioc_g_fmt_sliced_vbi_cap,
  1444. .vidioc_try_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1445. .vidioc_s_fmt_sliced_vbi_cap = vidioc_try_set_sliced_vbi_cap,
  1446. .vidioc_reqbufs = vidioc_reqbufs,
  1447. .vidioc_querybuf = vidioc_querybuf,
  1448. .vidioc_qbuf = vidioc_qbuf,
  1449. .vidioc_dqbuf = vidioc_dqbuf,
  1450. .vidioc_s_std = vidioc_s_std,
  1451. .vidioc_enum_input = vidioc_enum_input,
  1452. .vidioc_g_input = vidioc_g_input,
  1453. .vidioc_s_input = vidioc_s_input,
  1454. .vidioc_queryctrl = vidioc_queryctrl,
  1455. .vidioc_g_ctrl = vidioc_g_ctrl,
  1456. .vidioc_s_ctrl = vidioc_s_ctrl,
  1457. .vidioc_streamon = vidioc_streamon,
  1458. .vidioc_streamoff = vidioc_streamoff,
  1459. .vidioc_g_tuner = vidioc_g_tuner,
  1460. .vidioc_s_tuner = vidioc_s_tuner,
  1461. .vidioc_g_frequency = vidioc_g_frequency,
  1462. .vidioc_s_frequency = vidioc_s_frequency,
  1463. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1464. .vidioc_g_register = vidioc_g_register,
  1465. .vidioc_s_register = vidioc_s_register,
  1466. #endif
  1467. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  1468. .vidiocgmbuf = vidiocgmbuf,
  1469. #endif
  1470. };
  1471. static const struct video_device em28xx_video_template = {
  1472. .fops = &em28xx_v4l_fops,
  1473. .release = video_device_release,
  1474. .ioctl_ops = &video_ioctl_ops,
  1475. .minor = -1,
  1476. .tvnorms = V4L2_STD_ALL,
  1477. .current_norm = V4L2_STD_PAL,
  1478. };
  1479. static const struct file_operations radio_fops = {
  1480. .owner = THIS_MODULE,
  1481. .open = em28xx_v4l2_open,
  1482. .release = em28xx_v4l2_close,
  1483. .ioctl = video_ioctl2,
  1484. .compat_ioctl = v4l_compat_ioctl32,
  1485. .llseek = no_llseek,
  1486. };
  1487. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  1488. .vidioc_querycap = radio_querycap,
  1489. .vidioc_g_tuner = radio_g_tuner,
  1490. .vidioc_enum_input = radio_enum_input,
  1491. .vidioc_g_audio = radio_g_audio,
  1492. .vidioc_s_tuner = radio_s_tuner,
  1493. .vidioc_s_audio = radio_s_audio,
  1494. .vidioc_s_input = radio_s_input,
  1495. .vidioc_queryctrl = radio_queryctrl,
  1496. .vidioc_g_ctrl = vidioc_g_ctrl,
  1497. .vidioc_s_ctrl = vidioc_s_ctrl,
  1498. .vidioc_g_frequency = vidioc_g_frequency,
  1499. .vidioc_s_frequency = vidioc_s_frequency,
  1500. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1501. .vidioc_g_register = vidioc_g_register,
  1502. .vidioc_s_register = vidioc_s_register,
  1503. #endif
  1504. };
  1505. static struct video_device em28xx_radio_template = {
  1506. .name = "em28xx-radio",
  1507. .fops = &radio_fops,
  1508. .ioctl_ops = &radio_ioctl_ops,
  1509. .minor = -1,
  1510. };
  1511. /******************************** usb interface ******************************/
  1512. static LIST_HEAD(em28xx_extension_devlist);
  1513. static DEFINE_MUTEX(em28xx_extension_devlist_lock);
  1514. int em28xx_register_extension(struct em28xx_ops *ops)
  1515. {
  1516. struct em28xx *dev = NULL;
  1517. mutex_lock(&em28xx_devlist_mutex);
  1518. mutex_lock(&em28xx_extension_devlist_lock);
  1519. list_add_tail(&ops->next, &em28xx_extension_devlist);
  1520. list_for_each_entry(dev, &em28xx_devlist, devlist) {
  1521. if (dev)
  1522. ops->init(dev);
  1523. }
  1524. printk(KERN_INFO "Em28xx: Initialized (%s) extension\n", ops->name);
  1525. mutex_unlock(&em28xx_extension_devlist_lock);
  1526. mutex_unlock(&em28xx_devlist_mutex);
  1527. return 0;
  1528. }
  1529. EXPORT_SYMBOL(em28xx_register_extension);
  1530. void em28xx_unregister_extension(struct em28xx_ops *ops)
  1531. {
  1532. struct em28xx *dev = NULL;
  1533. mutex_lock(&em28xx_devlist_mutex);
  1534. list_for_each_entry(dev, &em28xx_devlist, devlist) {
  1535. if (dev)
  1536. ops->fini(dev);
  1537. }
  1538. mutex_lock(&em28xx_extension_devlist_lock);
  1539. printk(KERN_INFO "Em28xx: Removed (%s) extension\n", ops->name);
  1540. list_del(&ops->next);
  1541. mutex_unlock(&em28xx_extension_devlist_lock);
  1542. mutex_unlock(&em28xx_devlist_mutex);
  1543. }
  1544. EXPORT_SYMBOL(em28xx_unregister_extension);
  1545. static struct video_device *em28xx_vdev_init(struct em28xx *dev,
  1546. const struct video_device *template,
  1547. const char *type_name)
  1548. {
  1549. struct video_device *vfd;
  1550. vfd = video_device_alloc();
  1551. if (NULL == vfd)
  1552. return NULL;
  1553. *vfd = *template;
  1554. vfd->minor = -1;
  1555. vfd->parent = &dev->udev->dev;
  1556. vfd->release = video_device_release;
  1557. vfd->debug = video_debug;
  1558. snprintf(vfd->name, sizeof(vfd->name), "%s %s",
  1559. dev->name, type_name);
  1560. return vfd;
  1561. }
  1562. static int register_analog_devices(struct em28xx *dev)
  1563. {
  1564. int ret;
  1565. /* allocate and fill video video_device struct */
  1566. dev->vdev = em28xx_vdev_init(dev, &em28xx_video_template, "video");
  1567. if (!dev->vdev) {
  1568. em28xx_errdev("cannot allocate video_device.\n");
  1569. return -ENODEV;
  1570. }
  1571. /* register v4l2 video video_device */
  1572. ret = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1573. video_nr[dev->devno]);
  1574. if (ret) {
  1575. em28xx_errdev("unable to register video device (error=%i).\n",
  1576. ret);
  1577. return ret;
  1578. }
  1579. /* Allocate and fill vbi video_device struct */
  1580. dev->vbi_dev = em28xx_vdev_init(dev, &em28xx_video_template, "vbi");
  1581. /* register v4l2 vbi video_device */
  1582. ret = video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1583. vbi_nr[dev->devno]);
  1584. if (ret < 0) {
  1585. em28xx_errdev("unable to register vbi device\n");
  1586. return ret;
  1587. }
  1588. if (em28xx_boards[dev->model].radio.type == EM28XX_RADIO) {
  1589. dev->radio_dev = em28xx_vdev_init(dev, &em28xx_radio_template, "radio");
  1590. if (!dev->radio_dev) {
  1591. em28xx_errdev("cannot allocate video_device.\n");
  1592. return -ENODEV;
  1593. }
  1594. ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO,
  1595. radio_nr[dev->devno]);
  1596. if (ret < 0) {
  1597. em28xx_errdev("can't register radio device\n");
  1598. return ret;
  1599. }
  1600. em28xx_info("Registered radio device as /dev/radio%d\n",
  1601. dev->radio_dev->num);
  1602. }
  1603. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1604. dev->vdev->num, dev->vbi_dev->num);
  1605. return 0;
  1606. }
  1607. /*
  1608. * em28xx_init_dev()
  1609. * allocates and inits the device structs, registers i2c bus and v4l device
  1610. */
  1611. static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
  1612. int minor)
  1613. {
  1614. struct em28xx_ops *ops = NULL;
  1615. struct em28xx *dev = *devhandle;
  1616. int retval = -ENOMEM;
  1617. int errCode;
  1618. unsigned int maxh, maxw;
  1619. dev->udev = udev;
  1620. mutex_init(&dev->lock);
  1621. mutex_init(&dev->ctrl_urb_lock);
  1622. spin_lock_init(&dev->slock);
  1623. init_waitqueue_head(&dev->open);
  1624. init_waitqueue_head(&dev->wait_frame);
  1625. init_waitqueue_head(&dev->wait_stream);
  1626. dev->em28xx_write_regs = em28xx_write_regs;
  1627. dev->em28xx_read_reg = em28xx_read_reg;
  1628. dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
  1629. dev->em28xx_write_regs_req = em28xx_write_regs_req;
  1630. dev->em28xx_read_reg_req = em28xx_read_reg_req;
  1631. dev->is_em2800 = em28xx_boards[dev->model].is_em2800;
  1632. em28xx_pre_card_setup(dev);
  1633. errCode = em28xx_config(dev);
  1634. if (errCode) {
  1635. em28xx_errdev("error configuring device\n");
  1636. return -ENOMEM;
  1637. }
  1638. /* register i2c bus */
  1639. errCode = em28xx_i2c_register(dev);
  1640. if (errCode < 0) {
  1641. em28xx_errdev("%s: em28xx_i2c_register - errCode [%d]!\n",
  1642. __func__, errCode);
  1643. return errCode;
  1644. }
  1645. /* Do board specific init and eeprom reading */
  1646. em28xx_card_setup(dev);
  1647. /* Configure audio */
  1648. errCode = em28xx_audio_analog_set(dev);
  1649. if (errCode < 0) {
  1650. em28xx_errdev("%s: em28xx_audio_analog_set - errCode [%d]!\n",
  1651. __func__, errCode);
  1652. return errCode;
  1653. }
  1654. /* configure the device */
  1655. em28xx_config_i2c(dev);
  1656. /* set default norm */
  1657. dev->norm = em28xx_video_template.current_norm;
  1658. maxw = norm_maxw(dev);
  1659. maxh = norm_maxh(dev);
  1660. /* set default image size */
  1661. dev->width = maxw;
  1662. dev->height = maxh;
  1663. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1664. dev->hscale = 0;
  1665. dev->vscale = 0;
  1666. dev->ctl_input = 2;
  1667. errCode = em28xx_config(dev);
  1668. if (errCode < 0) {
  1669. em28xx_errdev("%s: em28xx_config - errCode [%d]!\n",
  1670. __func__, errCode);
  1671. return errCode;
  1672. }
  1673. /* init video dma queues */
  1674. INIT_LIST_HEAD(&dev->vidq.active);
  1675. INIT_LIST_HEAD(&dev->vidq.queued);
  1676. if (dev->has_msp34xx) {
  1677. /* Send a reset to other chips via gpio */
  1678. errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
  1679. if (errCode < 0) {
  1680. em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(1) failed! errCode [%d]\n",
  1681. __func__, errCode);
  1682. return errCode;
  1683. }
  1684. msleep(3);
  1685. errCode = em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
  1686. if (errCode < 0) {
  1687. em28xx_errdev("%s: em28xx_write_regs_req - msp34xx(2) failed! errCode [%d]\n",
  1688. __func__, errCode);
  1689. return errCode;
  1690. }
  1691. msleep(3);
  1692. }
  1693. video_mux(dev, 0);
  1694. mutex_lock(&em28xx_devlist_mutex);
  1695. list_add_tail(&dev->devlist, &em28xx_devlist);
  1696. retval = register_analog_devices(dev);
  1697. if (retval < 0) {
  1698. em28xx_release_resources(dev);
  1699. mutex_unlock(&em28xx_devlist_mutex);
  1700. goto fail_reg_devices;
  1701. }
  1702. mutex_lock(&em28xx_extension_devlist_lock);
  1703. if (!list_empty(&em28xx_extension_devlist)) {
  1704. list_for_each_entry(ops, &em28xx_extension_devlist, next) {
  1705. if (ops->id)
  1706. ops->init(dev);
  1707. }
  1708. }
  1709. mutex_unlock(&em28xx_extension_devlist_lock);
  1710. mutex_unlock(&em28xx_devlist_mutex);
  1711. return 0;
  1712. fail_unreg:
  1713. em28xx_release_resources(dev);
  1714. fail_reg_devices:
  1715. mutex_unlock(&dev->lock);
  1716. return retval;
  1717. }
  1718. #if defined(CONFIG_MODULES) && defined(MODULE)
  1719. static void request_module_async(struct work_struct *work)
  1720. {
  1721. struct em28xx *dev = container_of(work,
  1722. struct em28xx, request_module_wk);
  1723. if (dev->has_audio_class)
  1724. request_module("snd-usb-audio");
  1725. else
  1726. request_module("em28xx-alsa");
  1727. if (dev->has_dvb)
  1728. request_module("em28xx-dvb");
  1729. }
  1730. static void request_modules(struct em28xx *dev)
  1731. {
  1732. INIT_WORK(&dev->request_module_wk, request_module_async);
  1733. schedule_work(&dev->request_module_wk);
  1734. }
  1735. #else
  1736. #define request_modules(dev)
  1737. #endif /* CONFIG_MODULES */
  1738. /*
  1739. * em28xx_usb_probe()
  1740. * checks for supported devices
  1741. */
  1742. static int em28xx_usb_probe(struct usb_interface *interface,
  1743. const struct usb_device_id *id)
  1744. {
  1745. const struct usb_endpoint_descriptor *endpoint;
  1746. struct usb_device *udev;
  1747. struct usb_interface *uif;
  1748. struct em28xx *dev = NULL;
  1749. int retval = -ENODEV;
  1750. int i, nr, ifnum;
  1751. udev = usb_get_dev(interface_to_usbdev(interface));
  1752. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  1753. /* Check to see next free device and mark as used */
  1754. nr = find_first_zero_bit(&em28xx_devused, EM28XX_MAXBOARDS);
  1755. em28xx_devused |= 1<<nr;
  1756. /* Don't register audio interfaces */
  1757. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1758. em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
  1759. udev->descriptor.idVendor,
  1760. udev->descriptor.idProduct,
  1761. ifnum,
  1762. interface->altsetting[0].desc.bInterfaceClass);
  1763. em28xx_devused &= ~(1<<nr);
  1764. return -ENODEV;
  1765. }
  1766. em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
  1767. udev->descriptor.idVendor,
  1768. udev->descriptor.idProduct,
  1769. ifnum,
  1770. interface->altsetting[0].desc.bInterfaceClass);
  1771. endpoint = &interface->cur_altsetting->endpoint[1].desc;
  1772. /* check if the device has the iso in endpoint at the correct place */
  1773. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1774. USB_ENDPOINT_XFER_ISOC) {
  1775. em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
  1776. em28xx_devused &= ~(1<<nr);
  1777. return -ENODEV;
  1778. }
  1779. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1780. em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
  1781. em28xx_devused &= ~(1<<nr);
  1782. return -ENODEV;
  1783. }
  1784. if (nr >= EM28XX_MAXBOARDS) {
  1785. printk(DRIVER_NAME ": Supports only %i em28xx boards.\n",
  1786. EM28XX_MAXBOARDS);
  1787. em28xx_devused &= ~(1<<nr);
  1788. return -ENOMEM;
  1789. }
  1790. /* allocate memory for our device state and initialize it */
  1791. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1792. if (dev == NULL) {
  1793. em28xx_err(DRIVER_NAME ": out of memory!\n");
  1794. em28xx_devused &= ~(1<<nr);
  1795. return -ENOMEM;
  1796. }
  1797. snprintf(dev->name, 29, "em28xx #%d", nr);
  1798. dev->devno = nr;
  1799. dev->model = id->driver_info;
  1800. dev->alt = -1;
  1801. /* Checks if audio is provided by some interface */
  1802. for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
  1803. uif = udev->config->interface[i];
  1804. if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1805. dev->has_audio_class = 1;
  1806. break;
  1807. }
  1808. }
  1809. printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
  1810. dev->has_audio_class ? "Has" : "Doesn't have");
  1811. /* compute alternate max packet sizes */
  1812. uif = udev->actconfig->interface[0];
  1813. dev->num_alt = uif->num_altsetting;
  1814. em28xx_info("Alternate settings: %i\n", dev->num_alt);
  1815. /* dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)* */
  1816. dev->alt_max_pkt_size = kmalloc(32 * dev->num_alt, GFP_KERNEL);
  1817. if (dev->alt_max_pkt_size == NULL) {
  1818. em28xx_errdev("out of memory!\n");
  1819. em28xx_devused &= ~(1<<nr);
  1820. kfree(dev);
  1821. return -ENOMEM;
  1822. }
  1823. for (i = 0; i < dev->num_alt ; i++) {
  1824. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1825. wMaxPacketSize);
  1826. dev->alt_max_pkt_size[i] =
  1827. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1828. em28xx_info("Alternate setting %i, max size= %i\n", i,
  1829. dev->alt_max_pkt_size[i]);
  1830. }
  1831. if ((card[nr] >= 0) && (card[nr] < em28xx_bcount))
  1832. dev->model = card[nr];
  1833. /* allocate device struct */
  1834. retval = em28xx_init_dev(&dev, udev, nr);
  1835. if (retval) {
  1836. em28xx_devused &= ~(1<<dev->devno);
  1837. kfree(dev);
  1838. return retval;
  1839. }
  1840. em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
  1841. /* save our data pointer in this interface device */
  1842. usb_set_intfdata(interface, dev);
  1843. request_modules(dev);
  1844. return 0;
  1845. }
  1846. /*
  1847. * em28xx_usb_disconnect()
  1848. * called when the device gets diconencted
  1849. * video device will be unregistered on v4l2_close in case it is still open
  1850. */
  1851. static void em28xx_usb_disconnect(struct usb_interface *interface)
  1852. {
  1853. struct em28xx *dev;
  1854. struct em28xx_ops *ops = NULL;
  1855. dev = usb_get_intfdata(interface);
  1856. usb_set_intfdata(interface, NULL);
  1857. if (!dev)
  1858. return;
  1859. em28xx_info("disconnecting %s\n", dev->vdev->name);
  1860. /* wait until all current v4l2 io is finished then deallocate
  1861. resources */
  1862. mutex_lock(&dev->lock);
  1863. wake_up_interruptible_all(&dev->open);
  1864. if (dev->users) {
  1865. em28xx_warn
  1866. ("device /dev/video%d is open! Deregistration and memory "
  1867. "deallocation are deferred on close.\n",
  1868. dev->vdev->num);
  1869. dev->state |= DEV_MISCONFIGURED;
  1870. em28xx_uninit_isoc(dev);
  1871. dev->state |= DEV_DISCONNECTED;
  1872. wake_up_interruptible(&dev->wait_frame);
  1873. wake_up_interruptible(&dev->wait_stream);
  1874. } else {
  1875. dev->state |= DEV_DISCONNECTED;
  1876. em28xx_release_resources(dev);
  1877. }
  1878. mutex_unlock(&dev->lock);
  1879. mutex_lock(&em28xx_extension_devlist_lock);
  1880. if (!list_empty(&em28xx_extension_devlist)) {
  1881. list_for_each_entry(ops, &em28xx_extension_devlist, next) {
  1882. ops->fini(dev);
  1883. }
  1884. }
  1885. mutex_unlock(&em28xx_extension_devlist_lock);
  1886. if (!dev->users) {
  1887. kfree(dev->alt_max_pkt_size);
  1888. kfree(dev);
  1889. }
  1890. }
  1891. static struct usb_driver em28xx_usb_driver = {
  1892. .name = "em28xx",
  1893. .probe = em28xx_usb_probe,
  1894. .disconnect = em28xx_usb_disconnect,
  1895. .id_table = em28xx_id_table,
  1896. };
  1897. static int __init em28xx_module_init(void)
  1898. {
  1899. int result;
  1900. printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
  1901. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1902. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1903. #ifdef SNAPSHOT
  1904. printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
  1905. SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
  1906. #endif
  1907. /* register this driver with the USB subsystem */
  1908. result = usb_register(&em28xx_usb_driver);
  1909. if (result)
  1910. em28xx_err(DRIVER_NAME
  1911. " usb_register failed. Error number %d.\n", result);
  1912. return result;
  1913. }
  1914. static void __exit em28xx_module_exit(void)
  1915. {
  1916. /* deregister this driver with the USB subsystem */
  1917. usb_deregister(&em28xx_usb_driver);
  1918. }
  1919. module_init(em28xx_module_init);
  1920. module_exit(em28xx_module_exit);