em28xx-video.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  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. switch (format->type) {
  777. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  778. {
  779. format->fmt.pix.width = dev->width;
  780. format->fmt.pix.height = dev->height;
  781. format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  782. format->fmt.pix.bytesperline = dev->bytesperline;
  783. format->fmt.pix.sizeimage = dev->frame_size;
  784. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  785. format->fmt.pix.field = dev->interlaced ? V4L2_FIELD_INTERLACED : V4L2_FIELD_TOP; /* FIXME: TOP? NONE? BOTTOM? ALTENATE? */
  786. em28xx_videodbg("VIDIOC_G_FMT: %dx%d\n", dev->width,
  787. dev->height);
  788. break;
  789. }
  790. case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  791. {
  792. format->fmt.sliced.service_set=0;
  793. em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
  794. if (format->fmt.sliced.service_set==0)
  795. return -EINVAL;
  796. break;
  797. }
  798. default:
  799. return -EINVAL;
  800. }
  801. return (0);
  802. }
  803. static int em28xx_set_fmt(struct em28xx *dev, unsigned int cmd, struct v4l2_format *format)
  804. {
  805. u32 i;
  806. int ret = 0;
  807. int width = format->fmt.pix.width;
  808. int height = format->fmt.pix.height;
  809. unsigned int hscale, vscale;
  810. unsigned int maxh, maxw;
  811. maxw = norm_maxw(dev);
  812. maxh = norm_maxh(dev);
  813. em28xx_videodbg("%s: type=%s\n",
  814. cmd == VIDIOC_TRY_FMT ?
  815. "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
  816. format->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ?
  817. "V4L2_BUF_TYPE_VIDEO_CAPTURE" :
  818. format->type == V4L2_BUF_TYPE_VBI_CAPTURE ?
  819. "V4L2_BUF_TYPE_VBI_CAPTURE " :
  820. "not supported");
  821. if (format->type == V4L2_BUF_TYPE_SLICED_VBI_CAPTURE) {
  822. em28xx_i2c_call_clients(dev,VIDIOC_G_FMT,format);
  823. if (format->fmt.sliced.service_set==0)
  824. return -EINVAL;
  825. return 0;
  826. }
  827. if (format->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  828. return -EINVAL;
  829. em28xx_videodbg("%s: requested %dx%d\n",
  830. cmd == VIDIOC_TRY_FMT ?
  831. "VIDIOC_TRY_FMT" : "VIDIOC_S_FMT",
  832. format->fmt.pix.width, format->fmt.pix.height);
  833. /* FIXME: Move some code away from here */
  834. /* width must even because of the YUYV format */
  835. /* height must be even because of interlacing */
  836. height &= 0xfffe;
  837. width &= 0xfffe;
  838. if (height < 32)
  839. height = 32;
  840. if (height > maxh)
  841. height = maxh;
  842. if (width < 48)
  843. width = 48;
  844. if (width > maxw)
  845. width = maxw;
  846. if(dev->is_em2800){
  847. /* the em2800 can only scale down to 50% */
  848. if(height % (maxh / 2))
  849. height=maxh;
  850. if(width % (maxw / 2))
  851. width=maxw;
  852. /* according to empiatech support */
  853. /* the MaxPacketSize is to small to support */
  854. /* framesizes larger than 640x480 @ 30 fps */
  855. /* or 640x576 @ 25 fps. As this would cut */
  856. /* of a part of the image we prefer */
  857. /* 360x576 or 360x480 for now */
  858. if(width == maxw && height == maxh)
  859. width /= 2;
  860. }
  861. if ((hscale = (((unsigned long)maxw) << 12) / width - 4096L) >= 0x4000)
  862. hscale = 0x3fff;
  863. width = (((unsigned long)maxw) << 12) / (hscale + 4096L);
  864. if ((vscale = (((unsigned long)maxh) << 12) / height - 4096L) >= 0x4000)
  865. vscale = 0x3fff;
  866. height = (((unsigned long)maxh) << 12) / (vscale + 4096L);
  867. format->fmt.pix.width = width;
  868. format->fmt.pix.height = height;
  869. format->fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  870. format->fmt.pix.bytesperline = width * 2;
  871. format->fmt.pix.sizeimage = width * 2 * height;
  872. format->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  873. format->fmt.pix.field = V4L2_FIELD_INTERLACED;
  874. em28xx_videodbg("%s: returned %dx%d (%d, %d)\n",
  875. cmd == VIDIOC_TRY_FMT ?
  876. "VIDIOC_TRY_FMT" :"VIDIOC_S_FMT",
  877. format->fmt.pix.width, format->fmt.pix.height, hscale, vscale);
  878. if (cmd == VIDIOC_TRY_FMT)
  879. return 0;
  880. for (i = 0; i < dev->num_frames; i++)
  881. if (dev->frame[i].vma_use_count) {
  882. em28xx_videodbg("VIDIOC_S_FMT failed. "
  883. "Unmap the buffers first.\n");
  884. return -EINVAL;
  885. }
  886. /* stop io in case it is already in progress */
  887. if (dev->stream == STREAM_ON) {
  888. em28xx_videodbg("VIDIOC_SET_FMT: interupting stream\n");
  889. if ((ret = em28xx_stream_interrupt(dev)))
  890. return ret;
  891. }
  892. em28xx_release_buffers(dev);
  893. dev->io = IO_NONE;
  894. /* set new image size */
  895. dev->width = width;
  896. dev->height = height;
  897. dev->frame_size = dev->width * dev->height * 2;
  898. dev->field_size = dev->frame_size >> 1;
  899. dev->bytesperline = dev->width * 2;
  900. dev->hscale = hscale;
  901. dev->vscale = vscale;
  902. em28xx_uninit_isoc(dev);
  903. em28xx_set_alternate(dev);
  904. em28xx_capture_start(dev, 1);
  905. em28xx_resolution_set(dev);
  906. em28xx_init_isoc(dev);
  907. return 0;
  908. }
  909. /*
  910. * em28xx_v4l2_do_ioctl()
  911. * This function is _not_ called directly, but from
  912. * em28xx_v4l2_ioctl. Userspace
  913. * copying is done already, arg is a kernel pointer.
  914. */
  915. static int em28xx_do_ioctl(struct inode *inode, struct file *filp,
  916. struct em28xx *dev, unsigned int cmd, void *arg,
  917. v4l2_kioctl driver_ioctl)
  918. {
  919. int ret;
  920. switch (cmd) {
  921. /* ---------- tv norms ---------- */
  922. case VIDIOC_ENUMSTD:
  923. {
  924. struct v4l2_standard *e = arg;
  925. unsigned int i;
  926. i = e->index;
  927. if (i >= TVNORMS)
  928. return -EINVAL;
  929. ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
  930. tvnorms[e->index].name);
  931. e->index = i;
  932. if (ret < 0)
  933. return ret;
  934. return 0;
  935. }
  936. case VIDIOC_G_STD:
  937. {
  938. v4l2_std_id *id = arg;
  939. *id = dev->tvnorm->id;
  940. return 0;
  941. }
  942. case VIDIOC_S_STD:
  943. {
  944. v4l2_std_id *id = arg;
  945. unsigned int i;
  946. for (i = 0; i < TVNORMS; i++)
  947. if (*id == tvnorms[i].id)
  948. break;
  949. if (i == TVNORMS)
  950. for (i = 0; i < TVNORMS; i++)
  951. if (*id & tvnorms[i].id)
  952. break;
  953. if (i == TVNORMS)
  954. return -EINVAL;
  955. down(&dev->lock);
  956. dev->tvnorm = &tvnorms[i];
  957. em28xx_set_norm(dev, dev->width, dev->height);
  958. em28xx_i2c_call_clients(dev, DECODER_SET_NORM,
  959. &tvnorms[i].mode);
  960. em28xx_i2c_call_clients(dev, VIDIOC_S_STD,
  961. &dev->tvnorm->id);
  962. up(&dev->lock);
  963. return 0;
  964. }
  965. /* ------ input switching ---------- */
  966. case VIDIOC_ENUMINPUT:
  967. {
  968. struct v4l2_input *i = arg;
  969. unsigned int n;
  970. static const char *iname[] = {
  971. [EM28XX_VMUX_COMPOSITE1] = "Composite1",
  972. [EM28XX_VMUX_COMPOSITE2] = "Composite2",
  973. [EM28XX_VMUX_COMPOSITE3] = "Composite3",
  974. [EM28XX_VMUX_COMPOSITE4] = "Composite4",
  975. [EM28XX_VMUX_SVIDEO] = "S-Video",
  976. [EM28XX_VMUX_TELEVISION] = "Television",
  977. [EM28XX_VMUX_CABLE] = "Cable TV",
  978. [EM28XX_VMUX_DVB] = "DVB",
  979. [EM28XX_VMUX_DEBUG] = "for debug only",
  980. };
  981. n = i->index;
  982. if (n >= MAX_EM28XX_INPUT)
  983. return -EINVAL;
  984. if (0 == INPUT(n)->type)
  985. return -EINVAL;
  986. memset(i, 0, sizeof(*i));
  987. i->index = n;
  988. i->type = V4L2_INPUT_TYPE_CAMERA;
  989. strcpy(i->name, iname[INPUT(n)->type]);
  990. if ((EM28XX_VMUX_TELEVISION == INPUT(n)->type) ||
  991. (EM28XX_VMUX_CABLE == INPUT(n)->type))
  992. i->type = V4L2_INPUT_TYPE_TUNER;
  993. for (n = 0; n < ARRAY_SIZE(tvnorms); n++)
  994. i->std |= tvnorms[n].id;
  995. return 0;
  996. }
  997. case VIDIOC_G_INPUT:
  998. {
  999. int *i = arg;
  1000. *i = dev->ctl_input;
  1001. return 0;
  1002. }
  1003. case VIDIOC_S_INPUT:
  1004. {
  1005. int *index = arg;
  1006. if (*index >= MAX_EM28XX_INPUT)
  1007. return -EINVAL;
  1008. if (0 == INPUT(*index)->type)
  1009. return -EINVAL;
  1010. down(&dev->lock);
  1011. video_mux(dev, *index);
  1012. up(&dev->lock);
  1013. return 0;
  1014. }
  1015. case VIDIOC_G_AUDIO:
  1016. {
  1017. struct v4l2_audio *a = arg;
  1018. unsigned int index = a->index;
  1019. if (a->index > 1)
  1020. return -EINVAL;
  1021. memset(a, 0, sizeof(*a));
  1022. index = dev->ctl_ainput;
  1023. if (index == 0) {
  1024. strcpy(a->name, "Television");
  1025. } else {
  1026. strcpy(a->name, "Line In");
  1027. }
  1028. a->capability = V4L2_AUDCAP_STEREO;
  1029. a->index = index;
  1030. return 0;
  1031. }
  1032. case VIDIOC_S_AUDIO:
  1033. {
  1034. struct v4l2_audio *a = arg;
  1035. if (a->index != dev->ctl_ainput)
  1036. return -EINVAL;
  1037. return 0;
  1038. }
  1039. /* --- controls ---------------------------------------------- */
  1040. case VIDIOC_QUERYCTRL:
  1041. {
  1042. struct v4l2_queryctrl *qc = arg;
  1043. int i, id=qc->id;
  1044. memset(qc,0,sizeof(*qc));
  1045. qc->id=id;
  1046. if (!dev->has_msp34xx) {
  1047. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1048. if (qc->id && qc->id == em28xx_qctrl[i].id) {
  1049. memcpy(qc, &(em28xx_qctrl[i]),
  1050. sizeof(*qc));
  1051. return 0;
  1052. }
  1053. }
  1054. }
  1055. if (dev->decoder == EM28XX_TVP5150) {
  1056. em28xx_i2c_call_clients(dev,cmd,qc);
  1057. if (qc->type)
  1058. return 0;
  1059. else
  1060. return -EINVAL;
  1061. }
  1062. for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
  1063. if (qc->id && qc->id == saa711x_qctrl[i].id) {
  1064. memcpy(qc, &(saa711x_qctrl[i]),
  1065. sizeof(*qc));
  1066. return 0;
  1067. }
  1068. }
  1069. return -EINVAL;
  1070. }
  1071. case VIDIOC_G_CTRL:
  1072. {
  1073. struct v4l2_control *ctrl = arg;
  1074. int retval=-EINVAL;
  1075. if (!dev->has_msp34xx)
  1076. retval=em28xx_get_ctrl(dev, ctrl);
  1077. if (retval==-EINVAL) {
  1078. if (dev->decoder == EM28XX_TVP5150) {
  1079. em28xx_i2c_call_clients(dev,cmd,arg);
  1080. return 0;
  1081. }
  1082. return saa711x_get_ctrl(dev, ctrl);
  1083. } else return retval;
  1084. }
  1085. case VIDIOC_S_CTRL:
  1086. {
  1087. struct v4l2_control *ctrl = arg;
  1088. u8 i;
  1089. if (!dev->has_msp34xx){
  1090. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1091. if (ctrl->id == em28xx_qctrl[i].id) {
  1092. if (ctrl->value <
  1093. em28xx_qctrl[i].minimum
  1094. || ctrl->value >
  1095. em28xx_qctrl[i].maximum)
  1096. return -ERANGE;
  1097. return em28xx_set_ctrl(dev, ctrl);
  1098. }
  1099. }
  1100. }
  1101. if (dev->decoder == EM28XX_TVP5150) {
  1102. em28xx_i2c_call_clients(dev,cmd,arg);
  1103. return 0;
  1104. } else if (!dev->has_msp34xx) {
  1105. for (i = 0; i < ARRAY_SIZE(em28xx_qctrl); i++) {
  1106. if (ctrl->id == em28xx_qctrl[i].id) {
  1107. if (ctrl->value <
  1108. em28xx_qctrl[i].minimum
  1109. || ctrl->value >
  1110. em28xx_qctrl[i].maximum)
  1111. return -ERANGE;
  1112. return em28xx_set_ctrl(dev, ctrl);
  1113. }
  1114. }
  1115. for (i = 0; i < ARRAY_SIZE(saa711x_qctrl); i++) {
  1116. if (ctrl->id == saa711x_qctrl[i].id) {
  1117. if (ctrl->value <
  1118. saa711x_qctrl[i].minimum
  1119. || ctrl->value >
  1120. saa711x_qctrl[i].maximum)
  1121. return -ERANGE;
  1122. return saa711x_set_ctrl(dev, ctrl);
  1123. }
  1124. }
  1125. }
  1126. return -EINVAL;
  1127. }
  1128. /* --- tuner ioctls ------------------------------------------ */
  1129. case VIDIOC_G_TUNER:
  1130. {
  1131. struct v4l2_tuner *t = arg;
  1132. int status = 0;
  1133. if (0 != t->index)
  1134. return -EINVAL;
  1135. memset(t, 0, sizeof(*t));
  1136. strcpy(t->name, "Tuner");
  1137. t->type = V4L2_TUNER_ANALOG_TV;
  1138. t->capability = V4L2_TUNER_CAP_NORM;
  1139. t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
  1140. /* t->signal = 0xffff;*/
  1141. /* em28xx_i2c_call_clients(dev,VIDIOC_G_TUNER,t);*/
  1142. /* No way to get signal strength? */
  1143. down(&dev->lock);
  1144. em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
  1145. &status);
  1146. up(&dev->lock);
  1147. t->signal =
  1148. (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
  1149. em28xx_videodbg("VIDIO_G_TUNER: signal=%x, afc=%x\n", t->signal,
  1150. t->afc);
  1151. return 0;
  1152. }
  1153. case VIDIOC_S_TUNER:
  1154. {
  1155. struct v4l2_tuner *t = arg;
  1156. int status = 0;
  1157. if (0 != t->index)
  1158. return -EINVAL;
  1159. memset(t, 0, sizeof(*t));
  1160. strcpy(t->name, "Tuner");
  1161. t->type = V4L2_TUNER_ANALOG_TV;
  1162. t->capability = V4L2_TUNER_CAP_NORM;
  1163. t->rangehigh = 0xffffffffUL; /* FIXME: set correct range */
  1164. /* t->signal = 0xffff; */
  1165. /* No way to get signal strength? */
  1166. down(&dev->lock);
  1167. em28xx_i2c_call_clients(dev, DECODER_GET_STATUS,
  1168. &status);
  1169. up(&dev->lock);
  1170. t->signal =
  1171. (status & DECODER_STATUS_GOOD) != 0 ? 0xffff : 0;
  1172. em28xx_videodbg("VIDIO_S_TUNER: signal=%x, afc=%x\n",
  1173. t->signal, t->afc);
  1174. return 0;
  1175. }
  1176. case VIDIOC_G_FREQUENCY:
  1177. {
  1178. struct v4l2_frequency *f = arg;
  1179. memset(f, 0, sizeof(*f));
  1180. f->type = V4L2_TUNER_ANALOG_TV;
  1181. f->frequency = dev->ctl_freq;
  1182. return 0;
  1183. }
  1184. case VIDIOC_S_FREQUENCY:
  1185. {
  1186. struct v4l2_frequency *f = arg;
  1187. if (0 != f->tuner)
  1188. return -EINVAL;
  1189. if (V4L2_TUNER_ANALOG_TV != f->type)
  1190. return -EINVAL;
  1191. down(&dev->lock);
  1192. dev->ctl_freq = f->frequency;
  1193. em28xx_i2c_call_clients(dev, VIDIOC_S_FREQUENCY, f);
  1194. up(&dev->lock);
  1195. return 0;
  1196. }
  1197. case VIDIOC_CROPCAP:
  1198. {
  1199. struct v4l2_cropcap *cc = arg;
  1200. if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1201. return -EINVAL;
  1202. cc->bounds.left = 0;
  1203. cc->bounds.top = 0;
  1204. cc->bounds.width = dev->width;
  1205. cc->bounds.height = dev->height;
  1206. cc->defrect = cc->bounds;
  1207. cc->pixelaspect.numerator = 54; /* 4:3 FIXME: remove magic numbers */
  1208. cc->pixelaspect.denominator = 59;
  1209. return 0;
  1210. }
  1211. case VIDIOC_STREAMON:
  1212. {
  1213. int *type = arg;
  1214. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1215. || dev->io != IO_MMAP)
  1216. return -EINVAL;
  1217. if (list_empty(&dev->inqueue))
  1218. return -EINVAL;
  1219. dev->stream = STREAM_ON; /* FIXME: Start video capture here? */
  1220. em28xx_videodbg("VIDIOC_STREAMON: starting stream\n");
  1221. return 0;
  1222. }
  1223. case VIDIOC_STREAMOFF:
  1224. {
  1225. int *type = arg;
  1226. int ret;
  1227. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1228. || dev->io != IO_MMAP)
  1229. return -EINVAL;
  1230. if (dev->stream == STREAM_ON) {
  1231. em28xx_videodbg ("VIDIOC_STREAMOFF: interrupting stream\n");
  1232. if ((ret = em28xx_stream_interrupt(dev)))
  1233. return ret;
  1234. }
  1235. em28xx_empty_framequeues(dev);
  1236. return 0;
  1237. }
  1238. default:
  1239. return v4l_compat_translate_ioctl(inode, filp, cmd, arg,
  1240. driver_ioctl);
  1241. }
  1242. return 0;
  1243. }
  1244. /*
  1245. * em28xx_v4l2_do_ioctl()
  1246. * This function is _not_ called directly, but from
  1247. * em28xx_v4l2_ioctl. Userspace
  1248. * copying is done already, arg is a kernel pointer.
  1249. */
  1250. static int em28xx_video_do_ioctl(struct inode *inode, struct file *filp,
  1251. unsigned int cmd, void *arg)
  1252. {
  1253. struct em28xx *dev = filp->private_data;
  1254. if (!dev)
  1255. return -ENODEV;
  1256. if (video_debug > 1)
  1257. v4l_print_ioctl(dev->name,cmd);
  1258. switch (cmd) {
  1259. /* --- capabilities ------------------------------------------ */
  1260. case VIDIOC_QUERYCAP:
  1261. {
  1262. struct v4l2_capability *cap = arg;
  1263. memset(cap, 0, sizeof(*cap));
  1264. strlcpy(cap->driver, "em28xx", sizeof(cap->driver));
  1265. strlcpy(cap->card, em28xx_boards[dev->model].name,
  1266. sizeof(cap->card));
  1267. strlcpy(cap->bus_info, dev->udev->dev.bus_id,
  1268. sizeof(cap->bus_info));
  1269. cap->version = EM28XX_VERSION_CODE;
  1270. cap->capabilities =
  1271. V4L2_CAP_SLICED_VBI_CAPTURE |
  1272. V4L2_CAP_VIDEO_CAPTURE |
  1273. V4L2_CAP_AUDIO |
  1274. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  1275. if (dev->has_tuner)
  1276. cap->capabilities |= V4L2_CAP_TUNER;
  1277. return 0;
  1278. }
  1279. /* --- capture ioctls ---------------------------------------- */
  1280. case VIDIOC_ENUM_FMT:
  1281. {
  1282. struct v4l2_fmtdesc *fmtd = arg;
  1283. if (fmtd->index != 0)
  1284. return -EINVAL;
  1285. memset(fmtd, 0, sizeof(*fmtd));
  1286. fmtd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1287. strcpy(fmtd->description, "Packed YUY2");
  1288. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  1289. memset(fmtd->reserved, 0, sizeof(fmtd->reserved));
  1290. return 0;
  1291. }
  1292. case VIDIOC_G_FMT:
  1293. return em28xx_get_fmt(dev, (struct v4l2_format *) arg);
  1294. case VIDIOC_TRY_FMT:
  1295. case VIDIOC_S_FMT:
  1296. return em28xx_set_fmt(dev, cmd, (struct v4l2_format *)arg);
  1297. case VIDIOC_REQBUFS:
  1298. {
  1299. struct v4l2_requestbuffers *rb = arg;
  1300. u32 i;
  1301. int ret;
  1302. if (rb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1303. rb->memory != V4L2_MEMORY_MMAP)
  1304. return -EINVAL;
  1305. if (dev->io == IO_READ) {
  1306. em28xx_videodbg ("method is set to read;"
  1307. " close and open the device again to"
  1308. " choose the mmap I/O method\n");
  1309. return -EINVAL;
  1310. }
  1311. for (i = 0; i < dev->num_frames; i++)
  1312. if (dev->frame[i].vma_use_count) {
  1313. em28xx_videodbg ("VIDIOC_REQBUFS failed; previous buffers are still mapped\n");
  1314. return -EINVAL;
  1315. }
  1316. if (dev->stream == STREAM_ON) {
  1317. em28xx_videodbg("VIDIOC_REQBUFS: interrupting stream\n");
  1318. if ((ret = em28xx_stream_interrupt(dev)))
  1319. return ret;
  1320. }
  1321. em28xx_empty_framequeues(dev);
  1322. em28xx_release_buffers(dev);
  1323. if (rb->count)
  1324. rb->count =
  1325. em28xx_request_buffers(dev, rb->count);
  1326. dev->frame_current = NULL;
  1327. em28xx_videodbg ("VIDIOC_REQBUFS: setting io method to mmap: num bufs %i\n",
  1328. rb->count);
  1329. dev->io = rb->count ? IO_MMAP : IO_NONE;
  1330. return 0;
  1331. }
  1332. case VIDIOC_QUERYBUF:
  1333. {
  1334. struct v4l2_buffer *b = arg;
  1335. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1336. b->index >= dev->num_frames || dev->io != IO_MMAP)
  1337. return -EINVAL;
  1338. memcpy(b, &dev->frame[b->index].buf, sizeof(*b));
  1339. if (dev->frame[b->index].vma_use_count) {
  1340. b->flags |= V4L2_BUF_FLAG_MAPPED;
  1341. }
  1342. if (dev->frame[b->index].state == F_DONE)
  1343. b->flags |= V4L2_BUF_FLAG_DONE;
  1344. else if (dev->frame[b->index].state != F_UNUSED)
  1345. b->flags |= V4L2_BUF_FLAG_QUEUED;
  1346. return 0;
  1347. }
  1348. case VIDIOC_QBUF:
  1349. {
  1350. struct v4l2_buffer *b = arg;
  1351. unsigned long lock_flags;
  1352. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  1353. b->index >= dev->num_frames || dev->io != IO_MMAP) {
  1354. return -EINVAL;
  1355. }
  1356. if (dev->frame[b->index].state != F_UNUSED) {
  1357. return -EAGAIN;
  1358. }
  1359. dev->frame[b->index].state = F_QUEUED;
  1360. /* add frame to fifo */
  1361. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  1362. list_add_tail(&dev->frame[b->index].frame,
  1363. &dev->inqueue);
  1364. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  1365. return 0;
  1366. }
  1367. case VIDIOC_DQBUF:
  1368. {
  1369. struct v4l2_buffer *b = arg;
  1370. struct em28xx_frame_t *f;
  1371. unsigned long lock_flags;
  1372. int ret = 0;
  1373. if (b->type != V4L2_BUF_TYPE_VIDEO_CAPTURE
  1374. || dev->io != IO_MMAP)
  1375. return -EINVAL;
  1376. if (list_empty(&dev->outqueue)) {
  1377. if (dev->stream == STREAM_OFF)
  1378. return -EINVAL;
  1379. if (filp->f_flags & O_NONBLOCK)
  1380. return -EAGAIN;
  1381. ret = wait_event_interruptible
  1382. (dev->wait_frame,
  1383. (!list_empty(&dev->outqueue)) ||
  1384. (dev->state & DEV_DISCONNECTED));
  1385. if (ret)
  1386. return ret;
  1387. if (dev->state & DEV_DISCONNECTED)
  1388. return -ENODEV;
  1389. }
  1390. spin_lock_irqsave(&dev->queue_lock, lock_flags);
  1391. f = list_entry(dev->outqueue.next,
  1392. struct em28xx_frame_t, frame);
  1393. list_del(dev->outqueue.next);
  1394. spin_unlock_irqrestore(&dev->queue_lock, lock_flags);
  1395. f->state = F_UNUSED;
  1396. memcpy(b, &f->buf, sizeof(*b));
  1397. if (f->vma_use_count)
  1398. b->flags |= V4L2_BUF_FLAG_MAPPED;
  1399. return 0;
  1400. }
  1401. default:
  1402. return em28xx_do_ioctl(inode, filp, dev, cmd, arg,
  1403. em28xx_video_do_ioctl);
  1404. }
  1405. return 0;
  1406. }
  1407. /*
  1408. * em28xx_v4l2_ioctl()
  1409. * handle v4l2 ioctl the main action happens in em28xx_v4l2_do_ioctl()
  1410. */
  1411. static int em28xx_v4l2_ioctl(struct inode *inode, struct file *filp,
  1412. unsigned int cmd, unsigned long arg)
  1413. {
  1414. int ret = 0;
  1415. struct em28xx *dev = filp->private_data;
  1416. if (down_interruptible(&dev->fileop_lock))
  1417. return -ERESTARTSYS;
  1418. if (dev->state & DEV_DISCONNECTED) {
  1419. em28xx_errdev("v4l2 ioctl: device not present\n");
  1420. up(&dev->fileop_lock);
  1421. return -ENODEV;
  1422. }
  1423. if (dev->state & DEV_MISCONFIGURED) {
  1424. em28xx_errdev
  1425. ("v4l2 ioctl: device is misconfigured; close and open it again\n");
  1426. up(&dev->fileop_lock);
  1427. return -EIO;
  1428. }
  1429. ret = video_usercopy(inode, filp, cmd, arg, em28xx_video_do_ioctl);
  1430. up(&dev->fileop_lock);
  1431. return ret;
  1432. }
  1433. static struct file_operations em28xx_v4l_fops = {
  1434. .owner = THIS_MODULE,
  1435. .open = em28xx_v4l2_open,
  1436. .release = em28xx_v4l2_close,
  1437. .ioctl = em28xx_v4l2_ioctl,
  1438. .read = em28xx_v4l2_read,
  1439. .poll = em28xx_v4l2_poll,
  1440. .mmap = em28xx_v4l2_mmap,
  1441. .llseek = no_llseek,
  1442. .compat_ioctl = v4l_compat_ioctl32,
  1443. };
  1444. /******************************** usb interface *****************************************/
  1445. /*
  1446. * em28xx_init_dev()
  1447. * allocates and inits the device structs, registers i2c bus and v4l device
  1448. */
  1449. static int em28xx_init_dev(struct em28xx **devhandle, struct usb_device *udev,
  1450. int minor, int model)
  1451. {
  1452. struct em28xx *dev = *devhandle;
  1453. int retval = -ENOMEM;
  1454. int errCode, i;
  1455. unsigned int maxh, maxw;
  1456. dev->udev = udev;
  1457. dev->model = model;
  1458. init_MUTEX(&dev->lock);
  1459. init_waitqueue_head(&dev->open);
  1460. dev->em28xx_write_regs = em28xx_write_regs;
  1461. dev->em28xx_read_reg = em28xx_read_reg;
  1462. dev->em28xx_read_reg_req_len = em28xx_read_reg_req_len;
  1463. dev->em28xx_write_regs_req = em28xx_write_regs_req;
  1464. dev->em28xx_read_reg_req = em28xx_read_reg_req;
  1465. dev->is_em2800 = em28xx_boards[model].is_em2800;
  1466. dev->has_tuner = em28xx_boards[model].has_tuner;
  1467. dev->has_msp34xx = em28xx_boards[model].has_msp34xx;
  1468. dev->tda9887_conf = em28xx_boards[model].tda9887_conf;
  1469. dev->decoder = em28xx_boards[model].decoder;
  1470. if (tuner >= 0)
  1471. dev->tuner_type = tuner;
  1472. else
  1473. dev->tuner_type = em28xx_boards[model].tuner_type;
  1474. dev->video_inputs = em28xx_boards[model].vchannels;
  1475. for (i = 0; i < TVNORMS; i++)
  1476. if (em28xx_boards[model].norm == tvnorms[i].mode)
  1477. break;
  1478. if (i == TVNORMS)
  1479. i = 0;
  1480. dev->tvnorm = &tvnorms[i]; /* set default norm */
  1481. em28xx_videodbg("tvnorm=%s\n", dev->tvnorm->name);
  1482. maxw = norm_maxw(dev);
  1483. maxh = norm_maxh(dev);
  1484. /* set default image size */
  1485. dev->width = maxw;
  1486. dev->height = maxh;
  1487. dev->interlaced = EM28XX_INTERLACED_DEFAULT;
  1488. dev->field_size = dev->width * dev->height;
  1489. dev->frame_size =
  1490. dev->interlaced ? dev->field_size << 1 : dev->field_size;
  1491. dev->bytesperline = dev->width * 2;
  1492. dev->hscale = 0;
  1493. dev->vscale = 0;
  1494. dev->ctl_input = 2;
  1495. /* setup video picture settings for saa7113h */
  1496. memset(&dev->vpic, 0, sizeof(dev->vpic));
  1497. dev->vpic.colour = 128 << 8;
  1498. dev->vpic.hue = 128 << 8;
  1499. dev->vpic.brightness = 128 << 8;
  1500. dev->vpic.contrast = 192 << 8;
  1501. dev->vpic.whiteness = 128 << 8; /* This one isn't used */
  1502. dev->vpic.depth = 16;
  1503. dev->vpic.palette = VIDEO_PALETTE_YUV422;
  1504. em28xx_pre_card_setup(dev);
  1505. #ifdef CONFIG_MODULES
  1506. /* request some modules */
  1507. if (dev->decoder == EM28XX_SAA7113 || dev->decoder == EM28XX_SAA7114)
  1508. request_module("saa711x");
  1509. if (dev->decoder == EM28XX_TVP5150)
  1510. request_module("tvp5150");
  1511. if (dev->has_tuner)
  1512. request_module("tuner");
  1513. if (dev->tda9887_conf)
  1514. request_module("tda9887");
  1515. #endif
  1516. errCode = em28xx_config(dev);
  1517. if (errCode) {
  1518. em28xx_errdev("error configuring device\n");
  1519. kfree(dev);
  1520. em28xx_devused&=~(1<<dev->devno);
  1521. return -ENOMEM;
  1522. }
  1523. down(&dev->lock);
  1524. /* register i2c bus */
  1525. em28xx_i2c_register(dev);
  1526. /* Do board specific init and eeprom reading */
  1527. em28xx_card_setup(dev);
  1528. /* configure the device */
  1529. em28xx_config_i2c(dev);
  1530. up(&dev->lock);
  1531. errCode = em28xx_config(dev);
  1532. #ifdef CONFIG_MODULES
  1533. if (dev->has_msp34xx)
  1534. request_module("msp3400");
  1535. #endif
  1536. /* allocate and fill v4l2 device struct */
  1537. dev->vdev = video_device_alloc();
  1538. if (NULL == dev->vdev) {
  1539. em28xx_errdev("cannot allocate video_device.\n");
  1540. kfree(dev);
  1541. em28xx_devused&=~(1<<dev->devno);
  1542. return -ENOMEM;
  1543. }
  1544. dev->vbi_dev = video_device_alloc();
  1545. if (NULL == dev->vbi_dev) {
  1546. em28xx_errdev("cannot allocate video_device.\n");
  1547. kfree(dev->vdev);
  1548. kfree(dev);
  1549. em28xx_devused&=~(1<<dev->devno);
  1550. return -ENOMEM;
  1551. }
  1552. /* Fills VBI device info */
  1553. dev->vbi_dev->type = VFL_TYPE_VBI;
  1554. dev->vbi_dev->hardware = 0;
  1555. dev->vbi_dev->fops = &em28xx_v4l_fops;
  1556. dev->vbi_dev->minor = -1;
  1557. dev->vbi_dev->dev = &dev->udev->dev;
  1558. dev->vbi_dev->release = video_device_release;
  1559. snprintf(dev->vbi_dev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
  1560. "em28xx",dev->devno,"vbi");
  1561. /* Fills CAPTURE device info */
  1562. dev->vdev->type = VID_TYPE_CAPTURE;
  1563. if (dev->has_tuner)
  1564. dev->vdev->type |= VID_TYPE_TUNER;
  1565. dev->vdev->hardware = 0;
  1566. dev->vdev->fops = &em28xx_v4l_fops;
  1567. dev->vdev->minor = -1;
  1568. dev->vdev->dev = &dev->udev->dev;
  1569. dev->vdev->release = video_device_release;
  1570. snprintf(dev->vdev->name, sizeof(dev->vbi_dev->name), "%s#%d %s",
  1571. "em28xx",dev->devno,"video");
  1572. list_add_tail(&dev->devlist,&em28xx_devlist);
  1573. /* register v4l2 device */
  1574. down(&dev->lock);
  1575. if ((retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER,
  1576. video_nr[dev->devno]))) {
  1577. em28xx_errdev("unable to register video device (error=%i).\n",
  1578. retval);
  1579. up(&dev->lock);
  1580. list_del(&dev->devlist);
  1581. video_device_release(dev->vdev);
  1582. kfree(dev);
  1583. em28xx_devused&=~(1<<dev->devno);
  1584. return -ENODEV;
  1585. }
  1586. if (video_register_device(dev->vbi_dev, VFL_TYPE_VBI,
  1587. vbi_nr[dev->devno]) < 0) {
  1588. printk("unable to register vbi device\n");
  1589. up(&dev->lock);
  1590. list_del(&dev->devlist);
  1591. video_device_release(dev->vbi_dev);
  1592. video_device_release(dev->vdev);
  1593. kfree(dev);
  1594. em28xx_devused&=~(1<<dev->devno);
  1595. return -ENODEV;
  1596. } else {
  1597. printk("registered VBI\n");
  1598. }
  1599. if (dev->has_msp34xx) {
  1600. /* Send a reset to other chips via gpio */
  1601. em28xx_write_regs_req(dev, 0x00, 0x08, "\xf7", 1);
  1602. udelay(2500);
  1603. em28xx_write_regs_req(dev, 0x00, 0x08, "\xff", 1);
  1604. udelay(2500);
  1605. }
  1606. video_mux(dev, 0);
  1607. up(&dev->lock);
  1608. em28xx_info("V4L2 device registered as /dev/video%d and /dev/vbi%d\n",
  1609. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN,
  1610. dev->vbi_dev->minor-MINOR_VFL_TYPE_VBI_MIN);
  1611. return 0;
  1612. }
  1613. /*
  1614. * em28xx_usb_probe()
  1615. * checks for supported devices
  1616. */
  1617. static int em28xx_usb_probe(struct usb_interface *interface,
  1618. const struct usb_device_id *id)
  1619. {
  1620. const struct usb_endpoint_descriptor *endpoint;
  1621. struct usb_device *udev;
  1622. struct usb_interface *uif;
  1623. struct em28xx *dev = NULL;
  1624. int retval = -ENODEV;
  1625. int model,i,nr,ifnum;
  1626. udev = usb_get_dev(interface_to_usbdev(interface));
  1627. ifnum = interface->altsetting[0].desc.bInterfaceNumber;
  1628. /* Check to see next free device and mark as used */
  1629. nr=find_first_zero_bit(&em28xx_devused,EM28XX_MAXBOARDS);
  1630. em28xx_devused|=1<<nr;
  1631. /* Don't register audio interfaces */
  1632. if (interface->altsetting[0].desc.bInterfaceClass == USB_CLASS_AUDIO) {
  1633. em28xx_err(DRIVER_NAME " audio device (%04x:%04x): interface %i, class %i\n",
  1634. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1635. ifnum,
  1636. interface->altsetting[0].desc.bInterfaceClass);
  1637. em28xx_devused&=~(1<<nr);
  1638. return -ENODEV;
  1639. }
  1640. em28xx_err(DRIVER_NAME " new video device (%04x:%04x): interface %i, class %i\n",
  1641. udev->descriptor.idVendor,udev->descriptor.idProduct,
  1642. ifnum,
  1643. interface->altsetting[0].desc.bInterfaceClass);
  1644. endpoint = &interface->cur_altsetting->endpoint[1].desc;
  1645. /* check if the the device has the iso in endpoint at the correct place */
  1646. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) !=
  1647. USB_ENDPOINT_XFER_ISOC) {
  1648. em28xx_err(DRIVER_NAME " probing error: endpoint is non-ISO endpoint!\n");
  1649. em28xx_devused&=~(1<<nr);
  1650. return -ENODEV;
  1651. }
  1652. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1653. em28xx_err(DRIVER_NAME " probing error: endpoint is ISO OUT endpoint!\n");
  1654. em28xx_devused&=~(1<<nr);
  1655. return -ENODEV;
  1656. }
  1657. model=id->driver_info;
  1658. if (nr > EM28XX_MAXBOARDS) {
  1659. printk (DRIVER_NAME ": Supports only %i em28xx boards.\n",EM28XX_MAXBOARDS);
  1660. em28xx_devused&=~(1<<nr);
  1661. return -ENOMEM;
  1662. }
  1663. /* allocate memory for our device state and initialize it */
  1664. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1665. if (dev == NULL) {
  1666. em28xx_err(DRIVER_NAME ": out of memory!\n");
  1667. em28xx_devused&=~(1<<nr);
  1668. return -ENOMEM;
  1669. }
  1670. snprintf(dev->name, 29, "em28xx #%d", nr);
  1671. dev->devno=nr;
  1672. /* compute alternate max packet sizes */
  1673. uif = udev->actconfig->interface[0];
  1674. dev->num_alt=uif->num_altsetting;
  1675. em28xx_info("Alternate settings: %i\n",dev->num_alt);
  1676. // dev->alt_max_pkt_size = kmalloc(sizeof(*dev->alt_max_pkt_size)*
  1677. dev->alt_max_pkt_size = kmalloc(32*
  1678. dev->num_alt,GFP_KERNEL);
  1679. if (dev->alt_max_pkt_size == NULL) {
  1680. em28xx_errdev("out of memory!\n");
  1681. em28xx_devused&=~(1<<nr);
  1682. return -ENOMEM;
  1683. }
  1684. for (i = 0; i < dev->num_alt ; i++) {
  1685. u16 tmp = le16_to_cpu(uif->altsetting[i].endpoint[1].desc.
  1686. wMaxPacketSize);
  1687. dev->alt_max_pkt_size[i] =
  1688. (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1);
  1689. em28xx_info("Alternate setting %i, max size= %i\n",i,
  1690. dev->alt_max_pkt_size[i]);
  1691. }
  1692. if ((card[nr]>=0)&&(card[nr]<em28xx_bcount))
  1693. model=card[nr];
  1694. if ((model==EM2800_BOARD_UNKNOWN)||(model==EM2820_BOARD_UNKNOWN)) {
  1695. em28xx_errdev( "Your board has no eeprom inside it and thus can't\n"
  1696. "%s: be autodetected. Please pass card=<n> insmod option to\n"
  1697. "%s: workaround that. Redirect complaints to the vendor of\n"
  1698. "%s: the TV card. Generic type will be used."
  1699. "%s: Best regards,\n"
  1700. "%s: -- tux\n",
  1701. dev->name,dev->name,dev->name,dev->name,dev->name);
  1702. em28xx_errdev("%s: Here is a list of valid choices for the card=<n> insmod option:\n",
  1703. dev->name);
  1704. for (i = 0; i < em28xx_bcount; i++) {
  1705. em28xx_errdev(" card=%d -> %s\n", i,
  1706. em28xx_boards[i].name);
  1707. }
  1708. }
  1709. /* allocate device struct */
  1710. retval = em28xx_init_dev(&dev, udev, nr, model);
  1711. if (retval)
  1712. return retval;
  1713. em28xx_info("Found %s\n", em28xx_boards[model].name);
  1714. /* save our data pointer in this interface device */
  1715. usb_set_intfdata(interface, dev);
  1716. return 0;
  1717. }
  1718. /*
  1719. * em28xx_usb_disconnect()
  1720. * called when the device gets diconencted
  1721. * video device will be unregistered on v4l2_close in case it is still open
  1722. */
  1723. static void em28xx_usb_disconnect(struct usb_interface *interface)
  1724. {
  1725. struct em28xx *dev = usb_get_intfdata(interface);
  1726. usb_set_intfdata(interface, NULL);
  1727. if (!dev)
  1728. return;
  1729. down_write(&em28xx_disconnect);
  1730. down(&dev->lock);
  1731. em28xx_info("disconnecting %s\n", dev->vdev->name);
  1732. wake_up_interruptible_all(&dev->open);
  1733. if (dev->users) {
  1734. em28xx_warn
  1735. ("device /dev/video%d is open! Deregistration and memory "
  1736. "deallocation are deferred on close.\n",
  1737. dev->vdev->minor-MINOR_VFL_TYPE_GRABBER_MIN);
  1738. dev->state |= DEV_MISCONFIGURED;
  1739. em28xx_uninit_isoc(dev);
  1740. dev->state |= DEV_DISCONNECTED;
  1741. wake_up_interruptible(&dev->wait_frame);
  1742. wake_up_interruptible(&dev->wait_stream);
  1743. } else {
  1744. dev->state |= DEV_DISCONNECTED;
  1745. em28xx_release_resources(dev);
  1746. }
  1747. up(&dev->lock);
  1748. if (!dev->users) {
  1749. kfree(dev->alt_max_pkt_size);
  1750. kfree(dev);
  1751. }
  1752. up_write(&em28xx_disconnect);
  1753. }
  1754. static struct usb_driver em28xx_usb_driver = {
  1755. .name = "em28xx",
  1756. .probe = em28xx_usb_probe,
  1757. .disconnect = em28xx_usb_disconnect,
  1758. .id_table = em28xx_id_table,
  1759. };
  1760. static int __init em28xx_module_init(void)
  1761. {
  1762. int result;
  1763. printk(KERN_INFO DRIVER_NAME " v4l2 driver version %d.%d.%d loaded\n",
  1764. (EM28XX_VERSION_CODE >> 16) & 0xff,
  1765. (EM28XX_VERSION_CODE >> 8) & 0xff, EM28XX_VERSION_CODE & 0xff);
  1766. #ifdef SNAPSHOT
  1767. printk(KERN_INFO DRIVER_NAME " snapshot date %04d-%02d-%02d\n",
  1768. SNAPSHOT / 10000, (SNAPSHOT / 100) % 100, SNAPSHOT % 100);
  1769. #endif
  1770. /* register this driver with the USB subsystem */
  1771. result = usb_register(&em28xx_usb_driver);
  1772. if (result)
  1773. em28xx_err(DRIVER_NAME
  1774. " usb_register failed. Error number %d.\n", result);
  1775. return result;
  1776. }
  1777. static void __exit em28xx_module_exit(void)
  1778. {
  1779. /* deregister this driver with the USB subsystem */
  1780. usb_deregister(&em28xx_usb_driver);
  1781. }
  1782. module_init(em28xx_module_init);
  1783. module_exit(em28xx_module_exit);