em28xx-video.c 55 KB

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