em28xx-video.c 57 KB

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