em28xx-video.c 60 KB

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