em28xx-video.c 52 KB

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