em28xx-video.c 56 KB

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