em28xx-video.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208
  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("em28xx-alsa");
  1601. }
  1602. static void request_modules(struct em28xx *dev)
  1603. {
  1604. INIT_WORK(&dev->request_module_wk, request_module_async);
  1605. schedule_work(&dev->request_module_wk);
  1606. }
  1607. #else
  1608. #define request_modules(dev)
  1609. #endif /* CONFIG_MODULES */
  1610. /*
  1611. * em28xx_usb_probe()
  1612. * checks for supported devices
  1613. */
  1614. static int em28xx_usb_probe(struct usb_interface *interface,
  1615. const struct usb_device_id *id)
  1616. {
  1617. const struct usb_endpoint_descriptor *endpoint;
  1618. struct usb_device *udev;
  1619. struct usb_interface *uif;
  1620. struct em28xx *dev = NULL;
  1621. int retval = -ENODEV;
  1622. int i, nr, ifnum;
  1623. udev = usb_get_dev(interface_to_usbdev(interface));
  1624. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  1625. /* Check to see next free device and mark as used */
  1626. nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
  1627. em28xx_devused|=1<<nr;
  1628. /* Don't register audio interfaces */
  1629. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1630. em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
  1631. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1632. ifnum,
  1633. interface->altsetting[0].desc.bInterfaceClass);
  1634. em28xx_devused&=~(1<<nr);
  1635. return -ENODEV;
  1636. }
  1637. em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
  1638. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1639. ifnum,
  1640. interface->altsetting[0].desc.bInterfaceClass);
  1641. endpoint = &interface->cur_altsetting->endpoint[1].desc;
  1642. /* check if the device has the iso in endpoint at the correct place */
  1643. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1644. USB_ENDPOINT_XFER_ISOC) {
  1645. em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
  1646. em28xx_devused&=~(1<<nr);
  1647. return -ENODEV;
  1648. }
  1649. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1650. em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
  1651. em28xx_devused&=~(1<<nr);
  1652. return -ENODEV;
  1653. }
  1654. if (nr >= EM28XX_MAXBOARDS) {
  1655. printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
  1656. em28xx_devused&=~(1<<nr);
  1657. return -ENOMEM;
  1658. }
  1659. /* allocate memory for our device state and initialize it */
  1660. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1661. if (dev == NULL) {
  1662. em28xx_err(DRIVER_NAME ": out of memory!\n");
  1663. em28xx_devused&=~(1<<nr);
  1664. return -ENOMEM;
  1665. }
  1666. snprintf(dev->name, 29, "em28xx #%d", nr);
  1667. dev->devno = nr;
  1668. dev->model = id->driver_info;
  1669. /* Checks if audio is provided by some interface */
  1670. for (i = 0; i < udev->config->desc.bNumInterfaces; i++) {
  1671. uif = udev->config->interface[i];
  1672. if (uif->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1673. dev->has_audio_class = 1;
  1674. break;
  1675. }
  1676. }
  1677. printk(KERN_INFO DRIVER_NAME " %s usb audio class\n",
  1678. dev->has_audio_class ? "Has" : "Doesn't have");
  1679. /* compute alternate max packet sizes */
  1680. uif = udev->actconfig->interface[0];
  1681. dev->num_alt=uif->num_altsetting;
  1682. em28xx_info("Alternate settings: %i\n",dev->num_alt);
  1683. // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
  1684. dev->alt_max_pkt_size = kmalloc(32*
  1685. dev->num_alt,GFP_KERNEL);
  1686. if (dev->alt_max_pkt_size == NULL) {
  1687. em28xx_errdev("out of memory!\n");
  1688. em28xx_devused&=~(1<<nr);
  1689. kfree(dev);
  1690. return -ENOMEM;
  1691. }
  1692. for (i = 0; i < dev->num_alt ; i++) {
  1693. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1694. wMaxPacketSize);
  1695. dev->alt_max_pkt_size[i] =
  1696. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1697. em28xx_info("Alternate setting %i, max size= %i\n",i,
  1698. dev->alt_max_pkt_size[i]);
  1699. }
  1700. if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
  1701. dev->model = card[nr];
  1702. /* allocate device struct */
  1703. retval = em28xx_init_dev(&dev, udev, nr);
  1704. if (retval)
  1705. return retval;
  1706. em28xx_info("Found %s\n", em28xx_boards[dev->model].name);
  1707. /* save our data pointer in this interface device */
  1708. usb_set_intfdata(interface, dev);
  1709. request_modules(dev);
  1710. return 0;
  1711. }
  1712. /*
  1713. * em28xx_usb_disconnect()
  1714. * called when the device gets diconencted
  1715. * video device will be unregistered on v4l2_close in case it is still open
  1716. */
  1717. static void em28xx_usb_disconnect(struct usb_interface *interface)
  1718. {
  1719. struct em28xx *dev;
  1720. struct em28xx_ops *ops = NULL;
  1721. dev = usb_get_intfdata(interface);
  1722. usb_set_intfdata(interface, NULL);
  1723. if (!dev)
  1724. return;
  1725. em28xx_info("disconnecting %s\n", dev->vdev->name);
  1726. /* wait until all current v4l2 io is finished then deallocate resources */
  1727. mutex_lock(&dev->lock);
  1728. wake_up_interruptible_all(&dev->open);
  1729. if (dev->users) {
  1730. em28xx_warn
  1731. ("device /dev/video%d is open! Deregistration and memory "
  1732. "deallocation are deferred on close.\n",
  1733. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
  1734. dev->state |= DEV_MISCONFIGURED;
  1735. em28xx_uninit_isoc(dev);
  1736. dev->state |= DEV_DISCONNECTED;
  1737. wake_up_interruptible(&dev->wait_frame);
  1738. wake_up_interruptible(&dev->wait_stream);
  1739. } else {
  1740. dev->state |= DEV_DISCONNECTED;
  1741. em28xx_release_resources(dev);
  1742. }
  1743. mutex_unlock(&dev->lock);
  1744. mutex_lock(&em28xx_extension_devlist_lock);
  1745. if (!list_empty(&em28xx_extension_devlist)) {
  1746. list_for_each_entry(ops, &em28xx_extension_devlist, next) {
  1747. ops->fini(dev);
  1748. }
  1749. }
  1750. mutex_unlock(&em28xx_extension_devlist_lock);
  1751. if (!dev->users) {
  1752. kfree(dev->alt_max_pkt_size);
  1753. kfree(dev);
  1754. }
  1755. }
  1756. static struct usb_driver em28xx_usb_driver = {
  1757. .name = "em28xx",
  1758. .probe = em28xx_usb_probe,
  1759. .disconnect = em28xx_usb_disconnect,
  1760. .id_table = em28xx_id_table,
  1761. };
  1762. static int __init em28xx_module_init(void)
  1763. {
  1764. int result;
  1765. printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
  1766. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1767. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1768. #ifdef SNAPSHOT
  1769. printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
  1770. SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
  1771. #endif
  1772. /* register this driver with the USB subsystem */
  1773. result = usb_register(&em28xx_usb_driver);
  1774. if (result)
  1775. em28xx_err(DRIVER_NAME
  1776. " usb_register failed. Error number %d.\n", result);
  1777. return result;
  1778. }
  1779. static void __exit em28xx_module_exit(void)
  1780. {
  1781. /* deregister this driver with the USB subsystem */
  1782. usb_deregister(&em28xx_usb_driver);
  1783. }
  1784. module_init(em28xx_module_init);
  1785. module_exit(em28xx_module_exit);