em28xx-video.c 55 KB

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