em28xx-video.c 54 KB

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