em28xx-video.c 53 KB

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