em28xx-video.c 60 KB

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