usbvision-video.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  1. /*
  2. * USB USBVISION Video device driver 0.9.9
  3. *
  4. *
  5. *
  6. * Copyright (c) 1999-2005 Joerg Heckenbach <joerg@heckenbach-aw.de>
  7. *
  8. * This module is part of usbvision driver project.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * Let's call the version 0.... until compression decoding is completely
  25. * implemented.
  26. *
  27. * This driver is written by Jose Ignacio Gijon and Joerg Heckenbach.
  28. * It was based on USB CPiA driver written by Peter Pregler,
  29. * Scott J. Bertin and Johannes Erdfelt
  30. * Ideas are taken from bttv driver by Ralph Metzler, Marcus Metzler &
  31. * Gerd Knorr and zoran 36120/36125 driver by Pauline Middelink
  32. * Updates to driver completed by Dwaine P. Garden
  33. *
  34. *
  35. * TODO:
  36. * - use submit_urb for all setup packets
  37. * - Fix memory settings for nt1004. It is 4 times as big as the
  38. * nt1003 memory.
  39. * - Add audio on endpoint 3 for nt1004 chip. Seems impossible, needs a codec interface. Which one?
  40. * - Clean up the driver.
  41. * - optimization for performance.
  42. * - Add Videotext capability (VBI). Working on it.....
  43. * - Check audio for other devices
  44. *
  45. */
  46. #include <linux/version.h>
  47. #include <linux/kernel.h>
  48. #include <linux/sched.h>
  49. #include <linux/list.h>
  50. #include <linux/timer.h>
  51. #include <linux/slab.h>
  52. #include <linux/mm.h>
  53. #include <linux/utsname.h>
  54. #include <linux/highmem.h>
  55. #include <linux/smp_lock.h>
  56. #include <linux/videodev.h>
  57. #include <linux/vmalloc.h>
  58. #include <linux/module.h>
  59. #include <linux/init.h>
  60. #include <linux/spinlock.h>
  61. #include <asm/io.h>
  62. #include <linux/videodev2.h>
  63. #include <linux/video_decoder.h>
  64. #include <linux/i2c.h>
  65. #include <media/saa7115.h>
  66. #include <media/v4l2-common.h>
  67. #include <media/tuner.h>
  68. #include <media/audiochip.h>
  69. #include <linux/moduleparam.h>
  70. #include <linux/workqueue.h>
  71. #ifdef CONFIG_KMOD
  72. #include <linux/kmod.h>
  73. #endif
  74. #include "usbvision.h"
  75. #define DRIVER_AUTHOR "Joerg Heckenbach <joerg@heckenbach-aw.de>, Dwaine Garden <DwaineGarden@rogers.com>"
  76. #define DRIVER_NAME "usbvision"
  77. #define DRIVER_ALIAS "USBVision"
  78. #define DRIVER_DESC "USBVision USB Video Device Driver for Linux"
  79. #define DRIVER_LICENSE "GPL"
  80. #define USBVISION_DRIVER_VERSION_MAJOR 0
  81. #define USBVISION_DRIVER_VERSION_MINOR 9
  82. #define USBVISION_DRIVER_VERSION_PATCHLEVEL 9
  83. #define USBVISION_DRIVER_VERSION KERNEL_VERSION(USBVISION_DRIVER_VERSION_MAJOR,USBVISION_DRIVER_VERSION_MINOR,USBVISION_DRIVER_VERSION_PATCHLEVEL)
  84. #define USBVISION_VERSION_STRING __stringify(USBVISION_DRIVER_VERSION_MAJOR) "." __stringify(USBVISION_DRIVER_VERSION_MINOR) "." __stringify(USBVISION_DRIVER_VERSION_PATCHLEVEL)
  85. #define ENABLE_HEXDUMP 0 /* Enable if you need it */
  86. #ifdef USBVISION_DEBUG
  87. #define PDEBUG(level, fmt, args...) \
  88. if (video_debug & (level)) info("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__ , ## args)
  89. #else
  90. #define PDEBUG(level, fmt, args...) do {} while(0)
  91. #endif
  92. #define DBG_IOCTL 1<<0
  93. #define DBG_IO 1<<1
  94. #define DBG_PROBE 1<<2
  95. #define DBG_MMAP 1<<3
  96. //String operations
  97. #define rmspace(str) while(*str==' ') str++;
  98. #define goto2next(str) while(*str!=' ') str++; while(*str==' ') str++;
  99. static int usbvision_nr = 0; // sequential number of usbvision device
  100. static struct usbvision_v4l2_format_st usbvision_v4l2_format[] = {
  101. { 1, 1, 8, V4L2_PIX_FMT_GREY , "GREY" },
  102. { 1, 2, 16, V4L2_PIX_FMT_RGB565 , "RGB565" },
  103. { 1, 3, 24, V4L2_PIX_FMT_RGB24 , "RGB24" },
  104. { 1, 4, 32, V4L2_PIX_FMT_RGB32 , "RGB32" },
  105. { 1, 2, 16, V4L2_PIX_FMT_RGB555 , "RGB555" },
  106. { 1, 2, 16, V4L2_PIX_FMT_YUYV , "YUV422" },
  107. { 1, 2, 12, V4L2_PIX_FMT_YVU420 , "YUV420P" }, // 1.5 !
  108. { 1, 2, 16, V4L2_PIX_FMT_YUV422P , "YUV422P" }
  109. };
  110. /* supported tv norms */
  111. static struct usbvision_tvnorm tvnorms[] = {
  112. {
  113. .name = "PAL",
  114. .id = V4L2_STD_PAL,
  115. }, {
  116. .name = "NTSC",
  117. .id = V4L2_STD_NTSC,
  118. }, {
  119. .name = "SECAM",
  120. .id = V4L2_STD_SECAM,
  121. }, {
  122. .name = "PAL-M",
  123. .id = V4L2_STD_PAL_M,
  124. }
  125. };
  126. #define TVNORMS ARRAY_SIZE(tvnorms)
  127. // Function prototypes
  128. static void usbvision_release(struct usb_usbvision *usbvision);
  129. // Default initalization of device driver parameters
  130. static int isocMode = ISOC_MODE_COMPRESS; // Set the default format for ISOC endpoint
  131. static int video_debug = 0; // Set the default Debug Mode of the device driver
  132. static int PowerOnAtOpen = 1; // Set the default device to power on at startup
  133. static int video_nr = -1; // Sequential Number of Video Device
  134. static int radio_nr = -1; // Sequential Number of Radio Device
  135. static int vbi_nr = -1; // Sequential Number of VBI Device
  136. static char *CustomDevice=NULL; // Set as nothing....
  137. // Grab parameters for the device driver
  138. #if defined(module_param) // Showing parameters under SYSFS
  139. module_param(isocMode, int, 0444);
  140. module_param(video_debug, int, 0444);
  141. module_param(PowerOnAtOpen, int, 0444);
  142. module_param(video_nr, int, 0444);
  143. module_param(radio_nr, int, 0444);
  144. module_param(vbi_nr, int, 0444);
  145. module_param(CustomDevice, charp, 0444);
  146. #else // Old Style
  147. MODULE_PARAM(isocMode, "i");
  148. MODULE_PARM(video_debug, "i"); // Grab the Debug Mode of the device driver
  149. MODULE_PARM(adjustCompression, "i"); // Grab the compression to be adaptive
  150. MODULE_PARM(PowerOnAtOpen, "i"); // Grab the device to power on at startup
  151. MODULE_PARM(SwitchSVideoInput, "i"); // To help people with Black and White output with using s-video input. Some cables and input device are wired differently.
  152. MODULE_PARM(video_nr, "i"); // video_nr option allows to specify a certain /dev/videoX device (like /dev/video0 or /dev/video1 ...)
  153. MODULE_PARM(radio_nr, "i"); // radio_nr option allows to specify a certain /dev/radioX device (like /dev/radio0 or /dev/radio1 ...)
  154. MODULE_PARM(vbi_nr, "i"); // vbi_nr option allows to specify a certain /dev/vbiX device (like /dev/vbi0 or /dev/vbi1 ...)
  155. MODULE_PARM(CustomDevice, "s"); // .... CustomDevice
  156. #endif
  157. MODULE_PARM_DESC(isocMode, " Set the default format for ISOC endpoint. Default: 0x60 (Compression On)");
  158. MODULE_PARM_DESC(video_debug, " Set the default Debug Mode of the device driver. Default: 0 (Off)");
  159. MODULE_PARM_DESC(PowerOnAtOpen, " Set the default device to power on when device is opened. Default: 1 (On)");
  160. MODULE_PARM_DESC(video_nr, "Set video device number (/dev/videoX). Default: -1 (autodetect)");
  161. MODULE_PARM_DESC(radio_nr, "Set radio device number (/dev/radioX). Default: -1 (autodetect)");
  162. MODULE_PARM_DESC(vbi_nr, "Set vbi device number (/dev/vbiX). Default: -1 (autodetect)");
  163. MODULE_PARM_DESC(CustomDevice, " Define the fine tuning parameters for the device. Default: null");
  164. // Misc stuff
  165. MODULE_AUTHOR(DRIVER_AUTHOR);
  166. MODULE_DESCRIPTION(DRIVER_DESC);
  167. MODULE_LICENSE(DRIVER_LICENSE);
  168. MODULE_VERSION(USBVISION_VERSION_STRING);
  169. MODULE_ALIAS(DRIVER_ALIAS);
  170. /****************************************************************************************/
  171. /* SYSFS Code - Copied from the stv680.c usb module. */
  172. /* Device information is located at /sys/class/video4linux/video0 */
  173. /* Device parameters information is located at /sys/module/usbvision */
  174. /* Device USB Information is located at /sys/bus/usb/drivers/USBVision Video Grabber */
  175. /****************************************************************************************/
  176. #define YES_NO(x) ((x) ? "Yes" : "No")
  177. static inline struct usb_usbvision *cd_to_usbvision(struct class_device *cd)
  178. {
  179. struct video_device *vdev = container_of(cd, struct video_device, class_dev);
  180. return video_get_drvdata(vdev);
  181. }
  182. static ssize_t show_version(struct class_device *cd, char *buf)
  183. {
  184. return sprintf(buf, "%s\n", USBVISION_VERSION_STRING);
  185. }
  186. static CLASS_DEVICE_ATTR(version, S_IRUGO, show_version, NULL);
  187. static ssize_t show_model(struct class_device *cd, char *buf)
  188. {
  189. struct video_device *vdev = container_of(cd, struct video_device, class_dev);
  190. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  191. return sprintf(buf, "%s\n", usbvision_device_data[usbvision->DevModel].ModelString);
  192. }
  193. static CLASS_DEVICE_ATTR(model, S_IRUGO, show_model, NULL);
  194. static ssize_t show_hue(struct class_device *cd, char *buf)
  195. {
  196. struct video_device *vdev = container_of(cd, struct video_device, class_dev);
  197. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  198. struct v4l2_control ctrl;
  199. ctrl.id = V4L2_CID_HUE;
  200. ctrl.value = 0;
  201. if(usbvision->user)
  202. call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
  203. return sprintf(buf, "%d\n", ctrl.value >> 8);
  204. }
  205. static CLASS_DEVICE_ATTR(hue, S_IRUGO, show_hue, NULL);
  206. static ssize_t show_contrast(struct class_device *cd, char *buf)
  207. {
  208. struct video_device *vdev = container_of(cd, struct video_device, class_dev);
  209. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  210. struct v4l2_control ctrl;
  211. ctrl.id = V4L2_CID_CONTRAST;
  212. ctrl.value = 0;
  213. if(usbvision->user)
  214. call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
  215. return sprintf(buf, "%d\n", ctrl.value >> 8);
  216. }
  217. static CLASS_DEVICE_ATTR(contrast, S_IRUGO, show_contrast, NULL);
  218. static ssize_t show_brightness(struct class_device *cd, char *buf)
  219. {
  220. struct video_device *vdev = container_of(cd, struct video_device, class_dev);
  221. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  222. struct v4l2_control ctrl;
  223. ctrl.id = V4L2_CID_BRIGHTNESS;
  224. ctrl.value = 0;
  225. if(usbvision->user)
  226. call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
  227. return sprintf(buf, "%d\n", ctrl.value >> 8);
  228. }
  229. static CLASS_DEVICE_ATTR(brightness, S_IRUGO, show_brightness, NULL);
  230. static ssize_t show_saturation(struct class_device *cd, char *buf)
  231. {
  232. struct video_device *vdev = container_of(cd, struct video_device, class_dev);
  233. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  234. struct v4l2_control ctrl;
  235. ctrl.id = V4L2_CID_SATURATION;
  236. ctrl.value = 0;
  237. if(usbvision->user)
  238. call_i2c_clients(usbvision, VIDIOC_G_CTRL, &ctrl);
  239. return sprintf(buf, "%d\n", ctrl.value >> 8);
  240. }
  241. static CLASS_DEVICE_ATTR(saturation, S_IRUGO, show_saturation, NULL);
  242. static ssize_t show_streaming(struct class_device *cd, char *buf)
  243. {
  244. struct video_device *vdev = container_of(cd, struct video_device, class_dev);
  245. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  246. return sprintf(buf, "%s\n", YES_NO(usbvision->streaming==Stream_On?1:0));
  247. }
  248. static CLASS_DEVICE_ATTR(streaming, S_IRUGO, show_streaming, NULL);
  249. static ssize_t show_compression(struct class_device *cd, char *buf)
  250. {
  251. struct video_device *vdev = container_of(cd, struct video_device, class_dev);
  252. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  253. return sprintf(buf, "%s\n", YES_NO(usbvision->isocMode==ISOC_MODE_COMPRESS));
  254. }
  255. static CLASS_DEVICE_ATTR(compression, S_IRUGO, show_compression, NULL);
  256. static ssize_t show_device_bridge(struct class_device *cd, char *buf)
  257. {
  258. struct video_device *vdev = container_of(cd, struct video_device, class_dev);
  259. struct usb_usbvision *usbvision = video_get_drvdata(vdev);
  260. return sprintf(buf, "%d\n", usbvision->bridgeType);
  261. }
  262. static CLASS_DEVICE_ATTR(bridge, S_IRUGO, show_device_bridge, NULL);
  263. static void usbvision_create_sysfs(struct video_device *vdev)
  264. {
  265. int res;
  266. if (!vdev)
  267. return;
  268. do {
  269. res=class_device_create_file(&vdev->class_dev,
  270. &class_device_attr_version);
  271. if (res<0)
  272. break;
  273. res=class_device_create_file(&vdev->class_dev,
  274. &class_device_attr_model);
  275. if (res<0)
  276. break;
  277. res=class_device_create_file(&vdev->class_dev,
  278. &class_device_attr_hue);
  279. if (res<0)
  280. break;
  281. res=class_device_create_file(&vdev->class_dev,
  282. &class_device_attr_contrast);
  283. if (res<0)
  284. break;
  285. res=class_device_create_file(&vdev->class_dev,
  286. &class_device_attr_brightness);
  287. if (res<0)
  288. break;
  289. res=class_device_create_file(&vdev->class_dev,
  290. &class_device_attr_saturation);
  291. if (res<0)
  292. break;
  293. res=class_device_create_file(&vdev->class_dev,
  294. &class_device_attr_streaming);
  295. if (res<0)
  296. break;
  297. res=class_device_create_file(&vdev->class_dev,
  298. &class_device_attr_compression);
  299. if (res<0)
  300. break;
  301. res=class_device_create_file(&vdev->class_dev,
  302. &class_device_attr_bridge);
  303. if (res>=0)
  304. return;
  305. } while (0);
  306. err("%s error: %d\n", __FUNCTION__, res);
  307. }
  308. static void usbvision_remove_sysfs(struct video_device *vdev)
  309. {
  310. if (vdev) {
  311. class_device_remove_file(&vdev->class_dev,
  312. &class_device_attr_version);
  313. class_device_remove_file(&vdev->class_dev,
  314. &class_device_attr_model);
  315. class_device_remove_file(&vdev->class_dev,
  316. &class_device_attr_hue);
  317. class_device_remove_file(&vdev->class_dev,
  318. &class_device_attr_contrast);
  319. class_device_remove_file(&vdev->class_dev,
  320. &class_device_attr_brightness);
  321. class_device_remove_file(&vdev->class_dev,
  322. &class_device_attr_saturation);
  323. class_device_remove_file(&vdev->class_dev,
  324. &class_device_attr_streaming);
  325. class_device_remove_file(&vdev->class_dev,
  326. &class_device_attr_compression);
  327. class_device_remove_file(&vdev->class_dev,
  328. &class_device_attr_bridge);
  329. }
  330. }
  331. /*
  332. * usbvision_open()
  333. *
  334. * This is part of Video 4 Linux API. The driver can be opened by one
  335. * client only (checks internal counter 'usbvision->user'). The procedure
  336. * then allocates buffers needed for video processing.
  337. *
  338. */
  339. static int usbvision_v4l2_open(struct inode *inode, struct file *file)
  340. {
  341. struct video_device *dev = video_devdata(file);
  342. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  343. int errCode = 0;
  344. PDEBUG(DBG_IO, "open");
  345. usbvision_reset_powerOffTimer(usbvision);
  346. if (usbvision->user)
  347. errCode = -EBUSY;
  348. else {
  349. /* Allocate memory for the frame buffers */
  350. errCode = usbvision_frames_alloc(usbvision);
  351. if(!errCode) {
  352. /* Allocate memory for the scratch ring buffer */
  353. errCode = usbvision_scratch_alloc(usbvision);
  354. if ((!errCode) && (isocMode==ISOC_MODE_COMPRESS)) {
  355. /* Allocate intermediate decompression buffers only if needed */
  356. errCode = usbvision_decompress_alloc(usbvision);
  357. }
  358. }
  359. if (errCode) {
  360. /* Deallocate all buffers if trouble */
  361. usbvision_frames_free(usbvision);
  362. usbvision_scratch_free(usbvision);
  363. usbvision_decompress_free(usbvision);
  364. }
  365. }
  366. /* If so far no errors then we shall start the camera */
  367. if (!errCode) {
  368. down(&usbvision->lock);
  369. if (usbvision->power == 0) {
  370. usbvision_power_on(usbvision);
  371. usbvision_init_i2c(usbvision);
  372. }
  373. /* Send init sequence only once, it's large! */
  374. if (!usbvision->initialized) {
  375. int setup_ok = 0;
  376. setup_ok = usbvision_setup(usbvision,isocMode);
  377. if (setup_ok)
  378. usbvision->initialized = 1;
  379. else
  380. errCode = -EBUSY;
  381. }
  382. if (!errCode) {
  383. usbvision_begin_streaming(usbvision);
  384. errCode = usbvision_init_isoc(usbvision);
  385. /* device needs to be initialized before isoc transfer */
  386. usbvision_muxsel(usbvision,0);
  387. usbvision->user++;
  388. }
  389. else {
  390. if (PowerOnAtOpen) {
  391. usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
  392. usbvision_power_off(usbvision);
  393. usbvision->initialized = 0;
  394. }
  395. }
  396. up(&usbvision->lock);
  397. }
  398. if (errCode) {
  399. }
  400. /* prepare queues */
  401. usbvision_empty_framequeues(usbvision);
  402. PDEBUG(DBG_IO, "success");
  403. return errCode;
  404. }
  405. /*
  406. * usbvision_v4l2_close()
  407. *
  408. * This is part of Video 4 Linux API. The procedure
  409. * stops streaming and deallocates all buffers that were earlier
  410. * allocated in usbvision_v4l2_open().
  411. *
  412. */
  413. static int usbvision_v4l2_close(struct inode *inode, struct file *file)
  414. {
  415. struct video_device *dev = video_devdata(file);
  416. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  417. PDEBUG(DBG_IO, "close");
  418. down(&usbvision->lock);
  419. usbvision_audio_off(usbvision);
  420. usbvision_restart_isoc(usbvision);
  421. usbvision_stop_isoc(usbvision);
  422. usbvision_decompress_free(usbvision);
  423. usbvision_frames_free(usbvision);
  424. usbvision_scratch_free(usbvision);
  425. usbvision->user--;
  426. if (PowerOnAtOpen) {
  427. /* power off in a little while to avoid off/on every close/open short sequences */
  428. usbvision_set_powerOffTimer(usbvision);
  429. usbvision->initialized = 0;
  430. }
  431. up(&usbvision->lock);
  432. if (usbvision->remove_pending) {
  433. info("%s: Final disconnect", __FUNCTION__);
  434. usbvision_release(usbvision);
  435. }
  436. PDEBUG(DBG_IO, "success");
  437. return 0;
  438. }
  439. /*
  440. * usbvision_ioctl()
  441. *
  442. * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
  443. *
  444. */
  445. static int usbvision_v4l2_do_ioctl(struct inode *inode, struct file *file,
  446. unsigned int cmd, void *arg)
  447. {
  448. struct video_device *dev = video_devdata(file);
  449. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  450. if (!USBVISION_IS_OPERATIONAL(usbvision))
  451. return -EFAULT;
  452. switch (cmd) {
  453. #ifdef CONFIG_VIDEO_ADV_DEBUG
  454. /* ioctls to allow direct acces to the NT100x registers */
  455. case VIDIOC_INT_G_REGISTER:
  456. {
  457. struct v4l2_register *reg = arg;
  458. int errCode;
  459. if (reg->i2c_id != 0)
  460. return -EINVAL;
  461. /* NT100x has a 8-bit register space */
  462. errCode = usbvision_read_reg(usbvision, reg->reg&0xff);
  463. if (errCode < 0) {
  464. err("%s: VIDIOC_INT_G_REGISTER failed: error %d", __FUNCTION__, errCode);
  465. }
  466. else {
  467. reg->val=(unsigned char)errCode;
  468. PDEBUG(DBG_IOCTL, "VIDIOC_INT_G_REGISTER reg=0x%02X, value=0x%02X",
  469. (unsigned int)reg->reg, reg->val);
  470. errCode = 0; // No error
  471. }
  472. return errCode;
  473. }
  474. case VIDIOC_INT_S_REGISTER:
  475. {
  476. struct v4l2_register *reg = arg;
  477. int errCode;
  478. if (reg->i2c_id != 0)
  479. return -EINVAL;
  480. if (!capable(CAP_SYS_ADMIN))
  481. return -EPERM;
  482. errCode = usbvision_write_reg(usbvision, reg->reg&0xff, reg->val);
  483. if (errCode < 0) {
  484. err("%s: VIDIOC_INT_S_REGISTER failed: error %d", __FUNCTION__, errCode);
  485. }
  486. else {
  487. PDEBUG(DBG_IOCTL, "VIDIOC_INT_S_REGISTER reg=0x%02X, value=0x%02X",
  488. (unsigned int)reg->reg, reg->val);
  489. errCode = 0;
  490. }
  491. return 0;
  492. }
  493. #endif
  494. case VIDIOC_QUERYCAP:
  495. {
  496. struct v4l2_capability *vc=arg;
  497. memset(vc, 0, sizeof(*vc));
  498. strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
  499. strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
  500. sizeof(vc->card));
  501. strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
  502. sizeof(vc->bus_info));
  503. vc->version = USBVISION_DRIVER_VERSION;
  504. vc->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  505. V4L2_CAP_AUDIO |
  506. V4L2_CAP_READWRITE |
  507. V4L2_CAP_STREAMING |
  508. (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
  509. PDEBUG(DBG_IOCTL, "VIDIOC_QUERYCAP");
  510. return 0;
  511. }
  512. case VIDIOC_ENUMINPUT:
  513. {
  514. struct v4l2_input *vi = arg;
  515. int chan;
  516. if ((vi->index >= usbvision->video_inputs) || (vi->index < 0) )
  517. return -EINVAL;
  518. if (usbvision->have_tuner) {
  519. chan = vi->index;
  520. }
  521. else {
  522. chan = vi->index + 1; //skip Television string
  523. }
  524. switch(chan) {
  525. case 0:
  526. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  527. strcpy(vi->name, "White Video Input");
  528. }
  529. else {
  530. strcpy(vi->name, "Television");
  531. vi->type = V4L2_INPUT_TYPE_TUNER;
  532. vi->audioset = 1;
  533. vi->tuner = chan;
  534. vi->std = V4L2_STD_PAL | V4L2_STD_NTSC | V4L2_STD_SECAM;
  535. }
  536. break;
  537. case 1:
  538. vi->type = V4L2_INPUT_TYPE_CAMERA;
  539. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  540. strcpy(vi->name, "Green Video Input");
  541. }
  542. else {
  543. strcpy(vi->name, "Composite Video Input");
  544. }
  545. vi->std = V4L2_STD_PAL;
  546. break;
  547. case 2:
  548. vi->type = V4L2_INPUT_TYPE_CAMERA;
  549. if (usbvision_device_data[usbvision->DevModel].VideoChannels == 4) {
  550. strcpy(vi->name, "Yellow Video Input");
  551. }
  552. else {
  553. strcpy(vi->name, "S-Video Input");
  554. }
  555. vi->std = V4L2_STD_PAL;
  556. break;
  557. case 3:
  558. vi->type = V4L2_INPUT_TYPE_CAMERA;
  559. strcpy(vi->name, "Red Video Input");
  560. vi->std = V4L2_STD_PAL;
  561. break;
  562. }
  563. PDEBUG(DBG_IOCTL, "VIDIOC_ENUMINPUT name=%s:%d tuners=%d type=%d norm=%x",
  564. vi->name, vi->index, vi->tuner,vi->type,(int)vi->std);
  565. return 0;
  566. }
  567. case VIDIOC_ENUMSTD:
  568. {
  569. struct v4l2_standard *e = arg;
  570. unsigned int i;
  571. int ret;
  572. i = e->index;
  573. if (i >= TVNORMS)
  574. return -EINVAL;
  575. ret = v4l2_video_std_construct(e, tvnorms[e->index].id,
  576. tvnorms[e->index].name);
  577. e->index = i;
  578. if (ret < 0)
  579. return ret;
  580. return 0;
  581. }
  582. case VIDIOC_G_INPUT:
  583. {
  584. int *input = arg;
  585. *input = usbvision->ctl_input;
  586. return 0;
  587. }
  588. case VIDIOC_S_INPUT:
  589. {
  590. int *input = arg;
  591. if ((*input >= usbvision->video_inputs) || (*input < 0) )
  592. return -EINVAL;
  593. usbvision->ctl_input = *input;
  594. down(&usbvision->lock);
  595. usbvision_muxsel(usbvision, usbvision->ctl_input);
  596. usbvision_set_input(usbvision);
  597. usbvision_set_output(usbvision, usbvision->curwidth, usbvision->curheight);
  598. up(&usbvision->lock);
  599. return 0;
  600. }
  601. case VIDIOC_G_STD:
  602. {
  603. v4l2_std_id *id = arg;
  604. *id = usbvision->tvnorm->id;
  605. PDEBUG(DBG_IOCTL, "VIDIOC_G_STD std_id=%s", usbvision->tvnorm->name);
  606. return 0;
  607. }
  608. case VIDIOC_S_STD:
  609. {
  610. v4l2_std_id *id = arg;
  611. unsigned int i;
  612. for (i = 0; i < TVNORMS; i++)
  613. if (*id == tvnorms[i].id)
  614. break;
  615. if (i == TVNORMS)
  616. for (i = 0; i < TVNORMS; i++)
  617. if (*id & tvnorms[i].id)
  618. break;
  619. if (i == TVNORMS)
  620. return -EINVAL;
  621. down(&usbvision->lock);
  622. usbvision->tvnorm = &tvnorms[i];
  623. call_i2c_clients(usbvision, VIDIOC_S_STD,
  624. &usbvision->tvnorm->id);
  625. up(&usbvision->lock);
  626. PDEBUG(DBG_IOCTL, "VIDIOC_S_STD std_id=%s", usbvision->tvnorm->name);
  627. return 0;
  628. }
  629. case VIDIOC_G_TUNER:
  630. {
  631. struct v4l2_tuner *vt = arg;
  632. if (!usbvision->have_tuner || vt->index) // Only tuner 0
  633. return -EINVAL;
  634. strcpy(vt->name, "Television");
  635. /* Let clients fill in the remainder of this struct */
  636. call_i2c_clients(usbvision,VIDIOC_G_TUNER,vt);
  637. PDEBUG(DBG_IOCTL, "VIDIOC_G_TUNER signal=%x, afc=%x",vt->signal,vt->afc);
  638. return 0;
  639. }
  640. case VIDIOC_S_TUNER:
  641. {
  642. struct v4l2_tuner *vt = arg;
  643. // Only no or one tuner for now
  644. if (!usbvision->have_tuner || vt->index)
  645. return -EINVAL;
  646. /* let clients handle this */
  647. call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
  648. PDEBUG(DBG_IOCTL, "VIDIOC_S_TUNER");
  649. return 0;
  650. }
  651. case VIDIOC_G_FREQUENCY:
  652. {
  653. struct v4l2_frequency *freq = arg;
  654. freq->tuner = 0; // Only one tuner
  655. freq->type = V4L2_TUNER_ANALOG_TV;
  656. freq->frequency = usbvision->freq;
  657. PDEBUG(DBG_IOCTL, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
  658. return 0;
  659. }
  660. case VIDIOC_S_FREQUENCY:
  661. {
  662. struct v4l2_frequency *freq = arg;
  663. // Only no or one tuner for now
  664. if (!usbvision->have_tuner || freq->tuner)
  665. return -EINVAL;
  666. usbvision->freq = freq->frequency;
  667. call_i2c_clients(usbvision, cmd, freq);
  668. PDEBUG(DBG_IOCTL, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)freq->frequency);
  669. return 0;
  670. }
  671. case VIDIOC_G_AUDIO:
  672. {
  673. struct v4l2_audio *v = arg;
  674. memset(v,0, sizeof(v));
  675. strcpy(v->name, "TV");
  676. PDEBUG(DBG_IOCTL, "VIDIOC_G_AUDIO");
  677. return 0;
  678. }
  679. case VIDIOC_S_AUDIO:
  680. {
  681. struct v4l2_audio *v = arg;
  682. if(v->index) {
  683. return -EINVAL;
  684. }
  685. PDEBUG(DBG_IOCTL, "VIDIOC_S_AUDIO");
  686. return 0;
  687. }
  688. case VIDIOC_QUERYCTRL:
  689. {
  690. struct v4l2_queryctrl *ctrl = arg;
  691. int id=ctrl->id;
  692. memset(ctrl,0,sizeof(*ctrl));
  693. ctrl->id=id;
  694. call_i2c_clients(usbvision, cmd, arg);
  695. if (ctrl->type)
  696. return 0;
  697. else
  698. return -EINVAL;
  699. PDEBUG(DBG_IOCTL,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
  700. }
  701. case VIDIOC_G_CTRL:
  702. {
  703. struct v4l2_control *ctrl = arg;
  704. PDEBUG(DBG_IOCTL,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
  705. call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
  706. return 0;
  707. }
  708. case VIDIOC_S_CTRL:
  709. {
  710. struct v4l2_control *ctrl = arg;
  711. PDEBUG(DBG_IOCTL, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
  712. call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
  713. return 0;
  714. }
  715. case VIDIOC_REQBUFS:
  716. {
  717. struct v4l2_requestbuffers *vr = arg;
  718. int ret;
  719. RESTRICT_TO_RANGE(vr->count,1,USBVISION_NUMFRAMES);
  720. // Check input validity : the user must do a VIDEO CAPTURE and MMAP method.
  721. if((vr->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
  722. (vr->memory != V4L2_MEMORY_MMAP))
  723. return -EINVAL;
  724. if(usbvision->streaming == Stream_On) {
  725. if ((ret = usbvision_stream_interrupt(usbvision)))
  726. return ret;
  727. }
  728. usbvision_empty_framequeues(usbvision);
  729. usbvision->curFrame = NULL;
  730. PDEBUG(DBG_IOCTL, "VIDIOC_REQBUFS count=%d",vr->count);
  731. return 0;
  732. }
  733. case VIDIOC_QUERYBUF:
  734. {
  735. struct v4l2_buffer *vb = arg;
  736. struct usbvision_frame *frame;
  737. // FIXME : must control that buffers are mapped (VIDIOC_REQBUFS has been called)
  738. if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
  739. return -EINVAL;
  740. }
  741. if(vb->index>=USBVISION_NUMFRAMES) {
  742. return -EINVAL;
  743. }
  744. // Updating the corresponding frame state
  745. vb->flags = 0;
  746. frame = &usbvision->frame[vb->index];
  747. if(frame->grabstate >= FrameState_Ready)
  748. vb->flags |= V4L2_BUF_FLAG_QUEUED;
  749. if(frame->grabstate >= FrameState_Done)
  750. vb->flags |= V4L2_BUF_FLAG_DONE;
  751. if(frame->grabstate == FrameState_Unused)
  752. vb->flags |= V4L2_BUF_FLAG_MAPPED;
  753. vb->memory = V4L2_MEMORY_MMAP;
  754. vb->m.offset = vb->index*usbvision->max_frame_size;
  755. vb->memory = V4L2_MEMORY_MMAP;
  756. vb->field = V4L2_FIELD_NONE;
  757. vb->length = usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel;
  758. vb->timestamp = usbvision->frame[vb->index].timestamp;
  759. vb->sequence = usbvision->frame[vb->index].sequence;
  760. return 0;
  761. }
  762. case VIDIOC_QBUF:
  763. {
  764. struct v4l2_buffer *vb = arg;
  765. struct usbvision_frame *frame;
  766. unsigned long lock_flags;
  767. // FIXME : works only on VIDEO_CAPTURE MODE, MMAP.
  768. if(vb->type != V4L2_CAP_VIDEO_CAPTURE) {
  769. return -EINVAL;
  770. }
  771. if(vb->index>=USBVISION_NUMFRAMES) {
  772. return -EINVAL;
  773. }
  774. frame = &usbvision->frame[vb->index];
  775. if (frame->grabstate != FrameState_Unused) {
  776. return -EAGAIN;
  777. }
  778. /* Mark it as ready and enqueue frame */
  779. frame->grabstate = FrameState_Ready;
  780. frame->scanstate = ScanState_Scanning;
  781. frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
  782. vb->flags &= ~V4L2_BUF_FLAG_DONE;
  783. /* set v4l2_format index */
  784. frame->v4l2_format = usbvision->palette;
  785. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  786. list_add_tail(&usbvision->frame[vb->index].frame, &usbvision->inqueue);
  787. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  788. PDEBUG(DBG_IOCTL, "VIDIOC_QBUF frame #%d",vb->index);
  789. return 0;
  790. }
  791. case VIDIOC_DQBUF:
  792. {
  793. struct v4l2_buffer *vb = arg;
  794. int ret;
  795. struct usbvision_frame *f;
  796. unsigned long lock_flags;
  797. if (vb->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  798. return -EINVAL;
  799. if (list_empty(&(usbvision->outqueue))) {
  800. if (usbvision->streaming == Stream_Idle)
  801. return -EINVAL;
  802. ret = wait_event_interruptible
  803. (usbvision->wait_frame,
  804. !list_empty(&(usbvision->outqueue)));
  805. if (ret)
  806. return ret;
  807. }
  808. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  809. f = list_entry(usbvision->outqueue.next,
  810. struct usbvision_frame, frame);
  811. list_del(usbvision->outqueue.next);
  812. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  813. f->grabstate = FrameState_Unused;
  814. vb->memory = V4L2_MEMORY_MMAP;
  815. vb->flags = V4L2_BUF_FLAG_MAPPED | V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE;
  816. vb->index = f->index;
  817. vb->sequence = f->sequence;
  818. vb->timestamp = f->timestamp;
  819. vb->field = V4L2_FIELD_NONE;
  820. vb->bytesused = f->scanlength;
  821. return 0;
  822. }
  823. case VIDIOC_STREAMON:
  824. {
  825. int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
  826. usbvision->streaming = Stream_On;
  827. call_i2c_clients(usbvision,VIDIOC_STREAMON , &b);
  828. PDEBUG(DBG_IOCTL, "VIDIOC_STREAMON");
  829. return 0;
  830. }
  831. case VIDIOC_STREAMOFF:
  832. {
  833. int *type = arg;
  834. int b=V4L2_BUF_TYPE_VIDEO_CAPTURE;
  835. if (*type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  836. return -EINVAL;
  837. if(usbvision->streaming == Stream_On) {
  838. usbvision_stream_interrupt(usbvision);
  839. // Stop all video streamings
  840. call_i2c_clients(usbvision,VIDIOC_STREAMOFF , &b);
  841. }
  842. usbvision_empty_framequeues(usbvision);
  843. PDEBUG(DBG_IOCTL, "VIDIOC_STREAMOFF");
  844. return 0;
  845. }
  846. case VIDIOC_ENUM_FMT:
  847. {
  848. struct v4l2_fmtdesc *vfd = arg;
  849. if(vfd->index>=USBVISION_SUPPORTED_PALETTES-1) {
  850. return -EINVAL;
  851. }
  852. vfd->flags = 0;
  853. vfd->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  854. strcpy(vfd->description,usbvision_v4l2_format[vfd->index].desc);
  855. vfd->pixelformat = usbvision_v4l2_format[vfd->index].format;
  856. memset(vfd->reserved, 0, sizeof(vfd->reserved));
  857. return 0;
  858. }
  859. case VIDIOC_G_FMT:
  860. {
  861. struct v4l2_format *vf = arg;
  862. switch (vf->type) {
  863. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  864. {
  865. vf->fmt.pix.width = usbvision->curwidth;
  866. vf->fmt.pix.height = usbvision->curheight;
  867. vf->fmt.pix.pixelformat = usbvision->palette.format;
  868. vf->fmt.pix.bytesperline = usbvision->curwidth*usbvision->palette.bytes_per_pixel;
  869. vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*usbvision->curheight;
  870. vf->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
  871. vf->fmt.pix.field = V4L2_FIELD_NONE; /* Always progressive image */
  872. PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT w=%d, h=%d, format=%s",
  873. vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
  874. return 0;
  875. }
  876. default:
  877. PDEBUG(DBG_IOCTL, "VIDIOC_G_FMT invalid type %d",vf->type);
  878. return -EINVAL;
  879. }
  880. return 0;
  881. }
  882. case VIDIOC_TRY_FMT:
  883. case VIDIOC_S_FMT:
  884. {
  885. struct v4l2_format *vf = arg;
  886. int formatIdx,ret;
  887. switch(vf->type) {
  888. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  889. {
  890. /* Find requested format in available ones */
  891. for(formatIdx=0;formatIdx<USBVISION_SUPPORTED_PALETTES;formatIdx++) {
  892. if(vf->fmt.pix.pixelformat == usbvision_v4l2_format[formatIdx].format) {
  893. usbvision->palette = usbvision_v4l2_format[formatIdx];
  894. break;
  895. }
  896. }
  897. /* robustness */
  898. if(formatIdx == USBVISION_SUPPORTED_PALETTES) {
  899. return -EINVAL;
  900. }
  901. RESTRICT_TO_RANGE(vf->fmt.pix.width, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH);
  902. RESTRICT_TO_RANGE(vf->fmt.pix.height, MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT);
  903. vf->fmt.pix.bytesperline = vf->fmt.pix.width*usbvision->palette.bytes_per_pixel;
  904. vf->fmt.pix.sizeimage = vf->fmt.pix.bytesperline*vf->fmt.pix.height;
  905. if(cmd == VIDIOC_TRY_FMT) {
  906. PDEBUG(DBG_IOCTL, "VIDIOC_TRY_FMT grabdisplay w=%d, h=%d, format=%s",
  907. vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
  908. return 0;
  909. }
  910. /* stop io in case it is already in progress */
  911. if(usbvision->streaming == Stream_On) {
  912. if ((ret = usbvision_stream_interrupt(usbvision)))
  913. return ret;
  914. }
  915. usbvision_empty_framequeues(usbvision);
  916. usbvision->curFrame = NULL;
  917. // by now we are committed to the new data...
  918. down(&usbvision->lock);
  919. usbvision_set_output(usbvision, vf->fmt.pix.width, vf->fmt.pix.height);
  920. up(&usbvision->lock);
  921. PDEBUG(DBG_IOCTL, "VIDIOC_S_FMT grabdisplay w=%d, h=%d, format=%s",
  922. vf->fmt.pix.width, vf->fmt.pix.height,usbvision->palette.desc);
  923. return 0;
  924. }
  925. default:
  926. return -EINVAL;
  927. }
  928. }
  929. default:
  930. return -ENOIOCTLCMD;
  931. }
  932. return 0;
  933. }
  934. static int usbvision_v4l2_ioctl(struct inode *inode, struct file *file,
  935. unsigned int cmd, unsigned long arg)
  936. {
  937. return video_usercopy(inode, file, cmd, arg, usbvision_v4l2_do_ioctl);
  938. }
  939. static ssize_t usbvision_v4l2_read(struct file *file, char __user *buf,
  940. size_t count, loff_t *ppos)
  941. {
  942. struct video_device *dev = video_devdata(file);
  943. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  944. int noblock = file->f_flags & O_NONBLOCK;
  945. unsigned long lock_flags;
  946. int ret,i;
  947. struct usbvision_frame *frame;
  948. PDEBUG(DBG_IO, "%s: %ld bytes, noblock=%d", __FUNCTION__, (unsigned long)count, noblock);
  949. if (!USBVISION_IS_OPERATIONAL(usbvision) || (buf == NULL))
  950. return -EFAULT;
  951. /* no stream is running, make it running ! */
  952. usbvision->streaming = Stream_On;
  953. call_i2c_clients(usbvision,VIDIOC_STREAMON , NULL);
  954. /* First, enqueue as many frames as possible (like a user of VIDIOC_QBUF would do) */
  955. for(i=0;i<USBVISION_NUMFRAMES;i++) {
  956. frame = &usbvision->frame[i];
  957. if(frame->grabstate == FrameState_Unused) {
  958. /* Mark it as ready and enqueue frame */
  959. frame->grabstate = FrameState_Ready;
  960. frame->scanstate = ScanState_Scanning;
  961. frame->scanlength = 0; /* Accumulated in usbvision_parse_data() */
  962. /* set v4l2_format index */
  963. frame->v4l2_format = usbvision->palette;
  964. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  965. list_add_tail(&frame->frame, &usbvision->inqueue);
  966. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  967. }
  968. }
  969. /* Then try to steal a frame (like a VIDIOC_DQBUF would do) */
  970. if (list_empty(&(usbvision->outqueue))) {
  971. if(noblock)
  972. return -EAGAIN;
  973. ret = wait_event_interruptible
  974. (usbvision->wait_frame,
  975. !list_empty(&(usbvision->outqueue)));
  976. if (ret)
  977. return ret;
  978. }
  979. spin_lock_irqsave(&usbvision->queue_lock, lock_flags);
  980. frame = list_entry(usbvision->outqueue.next,
  981. struct usbvision_frame, frame);
  982. list_del(usbvision->outqueue.next);
  983. spin_unlock_irqrestore(&usbvision->queue_lock, lock_flags);
  984. /* An error returns an empty frame */
  985. if (frame->grabstate == FrameState_Error) {
  986. frame->bytes_read = 0;
  987. return 0;
  988. }
  989. PDEBUG(DBG_IO, "%s: frmx=%d, bytes_read=%ld, scanlength=%ld", __FUNCTION__,
  990. frame->index, frame->bytes_read, frame->scanlength);
  991. /* copy bytes to user space; we allow for partials reads */
  992. if ((count + frame->bytes_read) > (unsigned long)frame->scanlength)
  993. count = frame->scanlength - frame->bytes_read;
  994. if (copy_to_user(buf, frame->data + frame->bytes_read, count)) {
  995. return -EFAULT;
  996. }
  997. frame->bytes_read += count;
  998. PDEBUG(DBG_IO, "%s: {copy} count used=%ld, new bytes_read=%ld", __FUNCTION__,
  999. (unsigned long)count, frame->bytes_read);
  1000. // For now, forget the frame if it has not been read in one shot.
  1001. /* if (frame->bytes_read >= frame->scanlength) {// All data has been read */
  1002. frame->bytes_read = 0;
  1003. /* Mark it as available to be used again. */
  1004. frame->grabstate = FrameState_Unused;
  1005. /* } */
  1006. return count;
  1007. }
  1008. static int usbvision_v4l2_mmap(struct file *file, struct vm_area_struct *vma)
  1009. {
  1010. unsigned long size = vma->vm_end - vma->vm_start,
  1011. start = vma->vm_start;
  1012. void *pos;
  1013. u32 i;
  1014. struct video_device *dev = video_devdata(file);
  1015. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  1016. down(&usbvision->lock);
  1017. if (!USBVISION_IS_OPERATIONAL(usbvision)) {
  1018. up(&usbvision->lock);
  1019. return -EFAULT;
  1020. }
  1021. if (!(vma->vm_flags & VM_WRITE) ||
  1022. size != PAGE_ALIGN(usbvision->curwidth*usbvision->curheight*usbvision->palette.bytes_per_pixel)) {
  1023. up(&usbvision->lock);
  1024. return -EINVAL;
  1025. }
  1026. for (i = 0; i < USBVISION_NUMFRAMES; i++) {
  1027. if (((usbvision->max_frame_size*i) >> PAGE_SHIFT) == vma->vm_pgoff)
  1028. break;
  1029. }
  1030. if (i == USBVISION_NUMFRAMES) {
  1031. PDEBUG(DBG_MMAP, "mmap: user supplied mapping address is out of range");
  1032. up(&usbvision->lock);
  1033. return -EINVAL;
  1034. }
  1035. /* VM_IO is eventually going to replace PageReserved altogether */
  1036. vma->vm_flags |= VM_IO;
  1037. vma->vm_flags |= VM_RESERVED; /* avoid to swap out this VMA */
  1038. pos = usbvision->frame[i].data;
  1039. while (size > 0) {
  1040. if (vm_insert_page(vma, start, vmalloc_to_page(pos))) {
  1041. PDEBUG(DBG_MMAP, "mmap: vm_insert_page failed");
  1042. up(&usbvision->lock);
  1043. return -EAGAIN;
  1044. }
  1045. start += PAGE_SIZE;
  1046. pos += PAGE_SIZE;
  1047. size -= PAGE_SIZE;
  1048. }
  1049. up(&usbvision->lock);
  1050. return 0;
  1051. }
  1052. /*
  1053. * Here comes the stuff for radio on usbvision based devices
  1054. *
  1055. */
  1056. static int usbvision_radio_open(struct inode *inode, struct file *file)
  1057. {
  1058. struct video_device *dev = video_devdata(file);
  1059. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  1060. struct v4l2_frequency freq;
  1061. int errCode = 0;
  1062. PDEBUG(DBG_IO, "%s:", __FUNCTION__);
  1063. down(&usbvision->lock);
  1064. if (usbvision->user) {
  1065. err("%s: Someone tried to open an already opened USBVision Radio!", __FUNCTION__);
  1066. errCode = -EBUSY;
  1067. }
  1068. else {
  1069. if(PowerOnAtOpen) {
  1070. usbvision_reset_powerOffTimer(usbvision);
  1071. if (usbvision->power == 0) {
  1072. usbvision_power_on(usbvision);
  1073. usbvision_init_i2c(usbvision);
  1074. }
  1075. }
  1076. // If so far no errors then we shall start the radio
  1077. usbvision->radio = 1;
  1078. call_i2c_clients(usbvision,AUDC_SET_RADIO,&usbvision->tuner_type);
  1079. freq.frequency = 1517; //SWR3 @ 94.8MHz
  1080. call_i2c_clients(usbvision, VIDIOC_S_FREQUENCY, &freq);
  1081. usbvision_set_audio(usbvision, USBVISION_AUDIO_RADIO);
  1082. usbvision->user++;
  1083. }
  1084. if (errCode) {
  1085. if (PowerOnAtOpen) {
  1086. usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
  1087. usbvision_power_off(usbvision);
  1088. usbvision->initialized = 0;
  1089. }
  1090. }
  1091. up(&usbvision->lock);
  1092. return errCode;
  1093. }
  1094. static int usbvision_radio_close(struct inode *inode, struct file *file)
  1095. {
  1096. struct video_device *dev = video_devdata(file);
  1097. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  1098. int errCode = 0;
  1099. PDEBUG(DBG_IO, "");
  1100. down(&usbvision->lock);
  1101. usbvision_audio_off(usbvision);
  1102. usbvision->radio=0;
  1103. usbvision->user--;
  1104. if (PowerOnAtOpen) {
  1105. usbvision_set_powerOffTimer(usbvision);
  1106. usbvision->initialized = 0;
  1107. }
  1108. up(&usbvision->lock);
  1109. if (usbvision->remove_pending) {
  1110. info("%s: Final disconnect", __FUNCTION__);
  1111. usbvision_release(usbvision);
  1112. }
  1113. PDEBUG(DBG_IO, "success");
  1114. return errCode;
  1115. }
  1116. static int usbvision_do_radio_ioctl(struct inode *inode, struct file *file,
  1117. unsigned int cmd, void *arg)
  1118. {
  1119. struct video_device *dev = video_devdata(file);
  1120. struct usb_usbvision *usbvision = (struct usb_usbvision *) video_get_drvdata(dev);
  1121. if (!USBVISION_IS_OPERATIONAL(usbvision))
  1122. return -EIO;
  1123. switch (cmd) {
  1124. case VIDIOC_QUERYCAP:
  1125. {
  1126. struct v4l2_capability *vc=arg;
  1127. memset(vc, 0, sizeof(*vc));
  1128. strlcpy(vc->driver, "USBVision", sizeof(vc->driver));
  1129. strlcpy(vc->card, usbvision_device_data[usbvision->DevModel].ModelString,
  1130. sizeof(vc->card));
  1131. strlcpy(vc->bus_info, usbvision->dev->dev.bus_id,
  1132. sizeof(vc->bus_info));
  1133. vc->version = USBVISION_DRIVER_VERSION;
  1134. vc->capabilities = (usbvision->have_tuner ? V4L2_CAP_TUNER : 0);
  1135. PDEBUG(DBG_IO, "VIDIOC_QUERYCAP");
  1136. return 0;
  1137. }
  1138. case VIDIOC_QUERYCTRL:
  1139. {
  1140. struct v4l2_queryctrl *ctrl = arg;
  1141. int id=ctrl->id;
  1142. memset(ctrl,0,sizeof(*ctrl));
  1143. ctrl->id=id;
  1144. call_i2c_clients(usbvision, cmd, arg);
  1145. PDEBUG(DBG_IO,"VIDIOC_QUERYCTRL id=%x value=%x",ctrl->id,ctrl->type);
  1146. if (ctrl->type)
  1147. return 0;
  1148. else
  1149. return -EINVAL;
  1150. }
  1151. case VIDIOC_G_CTRL:
  1152. {
  1153. struct v4l2_control *ctrl = arg;
  1154. call_i2c_clients(usbvision, VIDIOC_G_CTRL, ctrl);
  1155. PDEBUG(DBG_IO,"VIDIOC_G_CTRL id=%x value=%x",ctrl->id,ctrl->value);
  1156. return 0;
  1157. }
  1158. case VIDIOC_S_CTRL:
  1159. {
  1160. struct v4l2_control *ctrl = arg;
  1161. call_i2c_clients(usbvision, VIDIOC_S_CTRL, ctrl);
  1162. PDEBUG(DBG_IO, "VIDIOC_S_CTRL id=%x value=%x",ctrl->id,ctrl->value);
  1163. return 0;
  1164. }
  1165. case VIDIOC_G_TUNER:
  1166. {
  1167. struct v4l2_tuner *t = arg;
  1168. if (t->index > 0)
  1169. return -EINVAL;
  1170. memset(t,0,sizeof(*t));
  1171. strcpy(t->name, "Radio");
  1172. t->type = V4L2_TUNER_RADIO;
  1173. /* Let clients fill in the remainder of this struct */
  1174. call_i2c_clients(usbvision,VIDIOC_G_TUNER,t);
  1175. PDEBUG(DBG_IO, "VIDIOC_G_TUNER signal=%x, afc=%x",t->signal,t->afc);
  1176. return 0;
  1177. }
  1178. case VIDIOC_S_TUNER:
  1179. {
  1180. struct v4l2_tuner *vt = arg;
  1181. // Only no or one tuner for now
  1182. if (!usbvision->have_tuner || vt->index)
  1183. return -EINVAL;
  1184. /* let clients handle this */
  1185. call_i2c_clients(usbvision,VIDIOC_S_TUNER,vt);
  1186. PDEBUG(DBG_IO, "VIDIOC_S_TUNER");
  1187. return 0;
  1188. }
  1189. case VIDIOC_G_AUDIO:
  1190. {
  1191. struct v4l2_audio *a = arg;
  1192. memset(a,0,sizeof(*a));
  1193. strcpy(a->name,"Radio");
  1194. PDEBUG(DBG_IO, "VIDIOC_G_AUDIO");
  1195. return 0;
  1196. }
  1197. case VIDIOC_S_AUDIO:
  1198. case VIDIOC_S_INPUT:
  1199. case VIDIOC_S_STD:
  1200. return 0;
  1201. case VIDIOC_G_FREQUENCY:
  1202. {
  1203. struct v4l2_frequency *f = arg;
  1204. memset(f,0,sizeof(*f));
  1205. f->type = V4L2_TUNER_RADIO;
  1206. f->frequency = usbvision->freq;
  1207. call_i2c_clients(usbvision, cmd, f);
  1208. PDEBUG(DBG_IO, "VIDIOC_G_FREQUENCY freq=0x%X", (unsigned)f->frequency);
  1209. return 0;
  1210. }
  1211. case VIDIOC_S_FREQUENCY:
  1212. {
  1213. struct v4l2_frequency *f = arg;
  1214. if (f->tuner != 0)
  1215. return -EINVAL;
  1216. usbvision->freq = f->frequency;
  1217. call_i2c_clients(usbvision, cmd, f);
  1218. PDEBUG(DBG_IO, "VIDIOC_S_FREQUENCY freq=0x%X", (unsigned)f->frequency);
  1219. return 0;
  1220. }
  1221. default:
  1222. {
  1223. PDEBUG(DBG_IO, "%s: Unknown command %x", __FUNCTION__, cmd);
  1224. return -ENOIOCTLCMD;
  1225. }
  1226. }
  1227. return 0;
  1228. }
  1229. static int usbvision_radio_ioctl(struct inode *inode, struct file *file,
  1230. unsigned int cmd, unsigned long arg)
  1231. {
  1232. return video_usercopy(inode, file, cmd, arg, usbvision_do_radio_ioctl);
  1233. }
  1234. /*
  1235. * Here comes the stuff for vbi on usbvision based devices
  1236. *
  1237. */
  1238. static int usbvision_vbi_open(struct inode *inode, struct file *file)
  1239. {
  1240. /* TODO */
  1241. return -EINVAL;
  1242. }
  1243. static int usbvision_vbi_close(struct inode *inode, struct file *file)
  1244. {
  1245. /* TODO */
  1246. return -EINVAL;
  1247. }
  1248. static int usbvision_do_vbi_ioctl(struct inode *inode, struct file *file,
  1249. unsigned int cmd, void *arg)
  1250. {
  1251. /* TODO */
  1252. return -EINVAL;
  1253. }
  1254. static int usbvision_vbi_ioctl(struct inode *inode, struct file *file,
  1255. unsigned int cmd, unsigned long arg)
  1256. {
  1257. return video_usercopy(inode, file, cmd, arg, usbvision_do_vbi_ioctl);
  1258. }
  1259. //
  1260. // Video registration stuff
  1261. //
  1262. // Video template
  1263. static struct file_operations usbvision_fops = {
  1264. .owner = THIS_MODULE,
  1265. .open = usbvision_v4l2_open,
  1266. .release = usbvision_v4l2_close,
  1267. .read = usbvision_v4l2_read,
  1268. .mmap = usbvision_v4l2_mmap,
  1269. .ioctl = usbvision_v4l2_ioctl,
  1270. .llseek = no_llseek,
  1271. };
  1272. static struct video_device usbvision_video_template = {
  1273. .owner = THIS_MODULE,
  1274. .type = VID_TYPE_TUNER | VID_TYPE_CAPTURE,
  1275. .hardware = VID_HARDWARE_USBVISION,
  1276. .fops = &usbvision_fops,
  1277. .name = "usbvision-video",
  1278. .release = video_device_release,
  1279. .minor = -1,
  1280. };
  1281. // Radio template
  1282. static struct file_operations usbvision_radio_fops = {
  1283. .owner = THIS_MODULE,
  1284. .open = usbvision_radio_open,
  1285. .release = usbvision_radio_close,
  1286. .ioctl = usbvision_radio_ioctl,
  1287. .llseek = no_llseek,
  1288. };
  1289. static struct video_device usbvision_radio_template=
  1290. {
  1291. .owner = THIS_MODULE,
  1292. .type = VID_TYPE_TUNER,
  1293. .hardware = VID_HARDWARE_USBVISION,
  1294. .fops = &usbvision_radio_fops,
  1295. .release = video_device_release,
  1296. .name = "usbvision-radio",
  1297. .minor = -1,
  1298. };
  1299. // vbi template
  1300. static struct file_operations usbvision_vbi_fops = {
  1301. .owner = THIS_MODULE,
  1302. .open = usbvision_vbi_open,
  1303. .release = usbvision_vbi_close,
  1304. .ioctl = usbvision_vbi_ioctl,
  1305. .llseek = no_llseek,
  1306. };
  1307. static struct video_device usbvision_vbi_template=
  1308. {
  1309. .owner = THIS_MODULE,
  1310. .type = VID_TYPE_TUNER,
  1311. .hardware = VID_HARDWARE_USBVISION,
  1312. .fops = &usbvision_vbi_fops,
  1313. .release = video_device_release,
  1314. .name = "usbvision-vbi",
  1315. .minor = -1,
  1316. };
  1317. static struct video_device *usbvision_vdev_init(struct usb_usbvision *usbvision,
  1318. struct video_device *vdev_template,
  1319. char *name)
  1320. {
  1321. struct usb_device *usb_dev = usbvision->dev;
  1322. struct video_device *vdev;
  1323. if (usb_dev == NULL) {
  1324. err("%s: usbvision->dev is not set", __FUNCTION__);
  1325. return NULL;
  1326. }
  1327. vdev = video_device_alloc();
  1328. if (NULL == vdev) {
  1329. return NULL;
  1330. }
  1331. *vdev = *vdev_template;
  1332. // vdev->minor = -1;
  1333. vdev->dev = &usb_dev->dev;
  1334. snprintf(vdev->name, sizeof(vdev->name), "%s", name);
  1335. video_set_drvdata(vdev, usbvision);
  1336. return vdev;
  1337. }
  1338. // unregister video4linux devices
  1339. static void usbvision_unregister_video(struct usb_usbvision *usbvision)
  1340. {
  1341. // vbi Device:
  1342. if (usbvision->vbi) {
  1343. PDEBUG(DBG_PROBE, "unregister /dev/vbi%d [v4l2]", usbvision->vbi->minor & 0x1f);
  1344. if (usbvision->vbi->minor != -1) {
  1345. video_unregister_device(usbvision->vbi);
  1346. }
  1347. else {
  1348. video_device_release(usbvision->vbi);
  1349. }
  1350. usbvision->vbi = NULL;
  1351. }
  1352. // Radio Device:
  1353. if (usbvision->rdev) {
  1354. PDEBUG(DBG_PROBE, "unregister /dev/radio%d [v4l2]", usbvision->rdev->minor & 0x1f);
  1355. if (usbvision->rdev->minor != -1) {
  1356. video_unregister_device(usbvision->rdev);
  1357. }
  1358. else {
  1359. video_device_release(usbvision->rdev);
  1360. }
  1361. usbvision->rdev = NULL;
  1362. }
  1363. // Video Device:
  1364. if (usbvision->vdev) {
  1365. PDEBUG(DBG_PROBE, "unregister /dev/video%d [v4l2]", usbvision->vdev->minor & 0x1f);
  1366. if (usbvision->vdev->minor != -1) {
  1367. video_unregister_device(usbvision->vdev);
  1368. }
  1369. else {
  1370. video_device_release(usbvision->vdev);
  1371. }
  1372. usbvision->vdev = NULL;
  1373. }
  1374. }
  1375. // register video4linux devices
  1376. static int __devinit usbvision_register_video(struct usb_usbvision *usbvision)
  1377. {
  1378. // Video Device:
  1379. usbvision->vdev = usbvision_vdev_init(usbvision, &usbvision_video_template, "USBVision Video");
  1380. if (usbvision->vdev == NULL) {
  1381. goto err_exit;
  1382. }
  1383. if (video_register_device(usbvision->vdev, VFL_TYPE_GRABBER, video_nr)<0) {
  1384. goto err_exit;
  1385. }
  1386. info("USBVision[%d]: registered USBVision Video device /dev/video%d [v4l2]", usbvision->nr,usbvision->vdev->minor & 0x1f);
  1387. // Radio Device:
  1388. if (usbvision_device_data[usbvision->DevModel].Radio) {
  1389. // usbvision has radio
  1390. usbvision->rdev = usbvision_vdev_init(usbvision, &usbvision_radio_template, "USBVision Radio");
  1391. if (usbvision->rdev == NULL) {
  1392. goto err_exit;
  1393. }
  1394. if (video_register_device(usbvision->rdev, VFL_TYPE_RADIO, radio_nr)<0) {
  1395. goto err_exit;
  1396. }
  1397. info("USBVision[%d]: registered USBVision Radio device /dev/radio%d [v4l2]", usbvision->nr, usbvision->rdev->minor & 0x1f);
  1398. }
  1399. // vbi Device:
  1400. if (usbvision_device_data[usbvision->DevModel].vbi) {
  1401. usbvision->vbi = usbvision_vdev_init(usbvision, &usbvision_vbi_template, "USBVision VBI");
  1402. if (usbvision->vdev == NULL) {
  1403. goto err_exit;
  1404. }
  1405. if (video_register_device(usbvision->vbi, VFL_TYPE_VBI, vbi_nr)<0) {
  1406. goto err_exit;
  1407. }
  1408. info("USBVision[%d]: registered USBVision VBI device /dev/vbi%d [v4l2] (Not Working Yet!)", usbvision->nr,usbvision->vbi->minor & 0x1f);
  1409. }
  1410. // all done
  1411. return 0;
  1412. err_exit:
  1413. err("USBVision[%d]: video_register_device() failed", usbvision->nr);
  1414. usbvision_unregister_video(usbvision);
  1415. return -1;
  1416. }
  1417. /*
  1418. * usbvision_alloc()
  1419. *
  1420. * This code allocates the struct usb_usbvision. It is filled with default values.
  1421. *
  1422. * Returns NULL on error, a pointer to usb_usbvision else.
  1423. *
  1424. */
  1425. static struct usb_usbvision *usbvision_alloc(struct usb_device *dev)
  1426. {
  1427. struct usb_usbvision *usbvision;
  1428. if ((usbvision = kzalloc(sizeof(struct usb_usbvision), GFP_KERNEL)) == NULL) {
  1429. goto err_exit;
  1430. }
  1431. usbvision->dev = dev;
  1432. init_MUTEX(&usbvision->lock); /* to 1 == available */
  1433. // prepare control urb for control messages during interrupts
  1434. usbvision->ctrlUrb = usb_alloc_urb(USBVISION_URB_FRAMES, GFP_KERNEL);
  1435. if (usbvision->ctrlUrb == NULL) {
  1436. goto err_exit;
  1437. }
  1438. init_waitqueue_head(&usbvision->ctrlUrb_wq);
  1439. init_MUTEX(&usbvision->ctrlUrbLock); /* to 1 == available */
  1440. usbvision_init_powerOffTimer(usbvision);
  1441. return usbvision;
  1442. err_exit:
  1443. if (usbvision && usbvision->ctrlUrb) {
  1444. usb_free_urb(usbvision->ctrlUrb);
  1445. }
  1446. if (usbvision) {
  1447. kfree(usbvision);
  1448. }
  1449. return NULL;
  1450. }
  1451. /*
  1452. * usbvision_release()
  1453. *
  1454. * This code does final release of struct usb_usbvision. This happens
  1455. * after the device is disconnected -and- all clients closed their files.
  1456. *
  1457. */
  1458. static void usbvision_release(struct usb_usbvision *usbvision)
  1459. {
  1460. PDEBUG(DBG_PROBE, "");
  1461. down(&usbvision->lock);
  1462. usbvision_reset_powerOffTimer(usbvision);
  1463. usbvision->initialized = 0;
  1464. up(&usbvision->lock);
  1465. usbvision_remove_sysfs(usbvision->vdev);
  1466. usbvision_unregister_video(usbvision);
  1467. if (usbvision->ctrlUrb) {
  1468. usb_free_urb(usbvision->ctrlUrb);
  1469. }
  1470. kfree(usbvision);
  1471. PDEBUG(DBG_PROBE, "success");
  1472. }
  1473. /******************************** usb interface *****************************************/
  1474. static void usbvision_configure_video(struct usb_usbvision *usbvision)
  1475. {
  1476. int model,i;
  1477. if (usbvision == NULL)
  1478. return;
  1479. model = usbvision->DevModel;
  1480. usbvision->palette = usbvision_v4l2_format[2]; // V4L2_PIX_FMT_RGB24;
  1481. if (usbvision_device_data[usbvision->DevModel].Vin_Reg2 >= 0) {
  1482. usbvision->Vin_Reg2_Preset = usbvision_device_data[usbvision->DevModel].Vin_Reg2 & 0xff;
  1483. } else {
  1484. usbvision->Vin_Reg2_Preset = 0;
  1485. }
  1486. for (i = 0; i < TVNORMS; i++)
  1487. if (usbvision_device_data[model].VideoNorm == tvnorms[i].mode)
  1488. break;
  1489. if (i == TVNORMS)
  1490. i = 0;
  1491. usbvision->tvnorm = &tvnorms[i]; /* set default norm */
  1492. usbvision->video_inputs = usbvision_device_data[model].VideoChannels;
  1493. usbvision->ctl_input = 0;
  1494. /* This should be here to make i2c clients to be able to register */
  1495. usbvision_audio_off(usbvision); //first switch off audio
  1496. if (!PowerOnAtOpen) {
  1497. usbvision_power_on(usbvision); //and then power up the noisy tuner
  1498. usbvision_init_i2c(usbvision);
  1499. }
  1500. }
  1501. /*
  1502. * usbvision_probe()
  1503. *
  1504. * This procedure queries device descriptor and accepts the interface
  1505. * if it looks like USBVISION video device
  1506. *
  1507. */
  1508. static int __devinit usbvision_probe(struct usb_interface *intf, const struct usb_device_id *devid)
  1509. {
  1510. struct usb_device *dev = interface_to_usbdev(intf);
  1511. __u8 ifnum = intf->altsetting->desc.bInterfaceNumber;
  1512. const struct usb_host_interface *interface;
  1513. struct usb_usbvision *usbvision = NULL;
  1514. const struct usb_endpoint_descriptor *endpoint;
  1515. int model;
  1516. PDEBUG(DBG_PROBE, "VID=%#04x, PID=%#04x, ifnum=%u",
  1517. dev->descriptor.idVendor, dev->descriptor.idProduct, ifnum);
  1518. /* Is it an USBVISION video dev? */
  1519. model = 0;
  1520. for(model = 0; usbvision_device_data[model].idVendor; model++) {
  1521. if (le16_to_cpu(dev->descriptor.idVendor) != usbvision_device_data[model].idVendor) {
  1522. continue;
  1523. }
  1524. if (le16_to_cpu(dev->descriptor.idProduct) != usbvision_device_data[model].idProduct) {
  1525. continue;
  1526. }
  1527. info("%s: %s found", __FUNCTION__, usbvision_device_data[model].ModelString);
  1528. break;
  1529. }
  1530. if (usbvision_device_data[model].idVendor == 0) {
  1531. return -ENODEV; //no matching device
  1532. }
  1533. if (usbvision_device_data[model].Interface >= 0) {
  1534. interface = &dev->actconfig->interface[usbvision_device_data[model].Interface]->altsetting[0];
  1535. }
  1536. else {
  1537. interface = &dev->actconfig->interface[ifnum]->altsetting[0];
  1538. }
  1539. endpoint = &interface->endpoint[1].desc;
  1540. if ((endpoint->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) != USB_ENDPOINT_XFER_ISOC) {
  1541. err("%s: interface %d. has non-ISO endpoint!", __FUNCTION__, ifnum);
  1542. err("%s: Endpoint attribures %d", __FUNCTION__, endpoint->bmAttributes);
  1543. return -ENODEV;
  1544. }
  1545. if ((endpoint->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
  1546. err("%s: interface %d. has ISO OUT endpoint!", __FUNCTION__, ifnum);
  1547. return -ENODEV;
  1548. }
  1549. usb_get_dev(dev);
  1550. if ((usbvision = usbvision_alloc(dev)) == NULL) {
  1551. err("%s: couldn't allocate USBVision struct", __FUNCTION__);
  1552. return -ENOMEM;
  1553. }
  1554. if (dev->descriptor.bNumConfigurations > 1) {
  1555. usbvision->bridgeType = BRIDGE_NT1004;
  1556. }
  1557. else if (usbvision_device_data[model].ModelString == "Dazzle Fusion Model DVC-90 Rev 1 (SECAM)") {
  1558. usbvision->bridgeType = BRIDGE_NT1005;
  1559. }
  1560. else {
  1561. usbvision->bridgeType = BRIDGE_NT1003;
  1562. }
  1563. PDEBUG(DBG_PROBE, "bridgeType %d", usbvision->bridgeType);
  1564. down(&usbvision->lock);
  1565. usbvision->nr = usbvision_nr++;
  1566. usbvision->have_tuner = usbvision_device_data[model].Tuner;
  1567. if (usbvision->have_tuner) {
  1568. usbvision->tuner_type = usbvision_device_data[model].TunerType;
  1569. }
  1570. usbvision->tuner_addr = ADDR_UNSET;
  1571. usbvision->DevModel = model;
  1572. usbvision->remove_pending = 0;
  1573. usbvision->iface = ifnum;
  1574. usbvision->ifaceAltInactive = 0;
  1575. usbvision->ifaceAltActive = 1;
  1576. usbvision->video_endp = endpoint->bEndpointAddress;
  1577. usbvision->isocPacketSize = 0;
  1578. usbvision->usb_bandwidth = 0;
  1579. usbvision->user = 0;
  1580. usbvision->streaming = Stream_Off;
  1581. usbvision_register_video(usbvision);
  1582. usbvision_configure_video(usbvision);
  1583. up(&usbvision->lock);
  1584. usb_set_intfdata (intf, usbvision);
  1585. usbvision_create_sysfs(usbvision->vdev);
  1586. PDEBUG(DBG_PROBE, "success");
  1587. return 0;
  1588. }
  1589. /*
  1590. * usbvision_disconnect()
  1591. *
  1592. * This procedure stops all driver activity, deallocates interface-private
  1593. * structure (pointed by 'ptr') and after that driver should be removable
  1594. * with no ill consequences.
  1595. *
  1596. */
  1597. static void __devexit usbvision_disconnect(struct usb_interface *intf)
  1598. {
  1599. struct usb_usbvision *usbvision = usb_get_intfdata(intf);
  1600. PDEBUG(DBG_PROBE, "");
  1601. if (usbvision == NULL) {
  1602. err("%s: usb_get_intfdata() failed", __FUNCTION__);
  1603. return;
  1604. }
  1605. usb_set_intfdata (intf, NULL);
  1606. down(&usbvision->lock);
  1607. // At this time we ask to cancel outstanding URBs
  1608. usbvision_stop_isoc(usbvision);
  1609. if (usbvision->power) {
  1610. usbvision_i2c_usb_del_bus(&usbvision->i2c_adap);
  1611. usbvision_power_off(usbvision);
  1612. }
  1613. usbvision->remove_pending = 1; // Now all ISO data will be ignored
  1614. usb_put_dev(usbvision->dev);
  1615. usbvision->dev = NULL; // USB device is no more
  1616. up(&usbvision->lock);
  1617. if (usbvision->user) {
  1618. info("%s: In use, disconnect pending", __FUNCTION__);
  1619. wake_up_interruptible(&usbvision->wait_frame);
  1620. wake_up_interruptible(&usbvision->wait_stream);
  1621. }
  1622. else {
  1623. usbvision_release(usbvision);
  1624. }
  1625. PDEBUG(DBG_PROBE, "success");
  1626. }
  1627. static struct usb_driver usbvision_driver = {
  1628. .name = "usbvision",
  1629. .id_table = usbvision_table,
  1630. .probe = usbvision_probe,
  1631. .disconnect = usbvision_disconnect
  1632. };
  1633. /*
  1634. * customdevice_process()
  1635. *
  1636. * This procedure preprocesses CustomDevice parameter if any
  1637. *
  1638. */
  1639. static void customdevice_process(void)
  1640. {
  1641. usbvision_device_data[0]=usbvision_device_data[1];
  1642. usbvision_table[0]=usbvision_table[1];
  1643. if(CustomDevice)
  1644. {
  1645. char *parse=CustomDevice;
  1646. PDEBUG(DBG_PROBE, "CustomDevide=%s", CustomDevice);
  1647. /*format is CustomDevice="0x0573 0x4D31 0 7113 3 PAL 1 1 1 5 -1 -1 -1 -1 -1"
  1648. usbvision_device_data[0].idVendor;
  1649. usbvision_device_data[0].idProduct;
  1650. usbvision_device_data[0].Interface;
  1651. usbvision_device_data[0].Codec;
  1652. usbvision_device_data[0].VideoChannels;
  1653. usbvision_device_data[0].VideoNorm;
  1654. usbvision_device_data[0].AudioChannels;
  1655. usbvision_device_data[0].Radio;
  1656. usbvision_device_data[0].Tuner;
  1657. usbvision_device_data[0].TunerType;
  1658. usbvision_device_data[0].Vin_Reg1;
  1659. usbvision_device_data[0].Vin_Reg2;
  1660. usbvision_device_data[0].X_Offset;
  1661. usbvision_device_data[0].Y_Offset;
  1662. usbvision_device_data[0].Dvi_yuv;
  1663. usbvision_device_data[0].ModelString;
  1664. */
  1665. rmspace(parse);
  1666. usbvision_device_data[0].ModelString="USBVISION Custom Device";
  1667. parse+=2;
  1668. sscanf(parse,"%x",&usbvision_device_data[0].idVendor);
  1669. goto2next(parse);
  1670. PDEBUG(DBG_PROBE, "idVendor=0x%.4X", usbvision_device_data[0].idVendor);
  1671. parse+=2;
  1672. sscanf(parse,"%x",&usbvision_device_data[0].idProduct);
  1673. goto2next(parse);
  1674. PDEBUG(DBG_PROBE, "idProduct=0x%.4X", usbvision_device_data[0].idProduct);
  1675. sscanf(parse,"%d",&usbvision_device_data[0].Interface);
  1676. goto2next(parse);
  1677. PDEBUG(DBG_PROBE, "Interface=%d", usbvision_device_data[0].Interface);
  1678. sscanf(parse,"%d",&usbvision_device_data[0].Codec);
  1679. goto2next(parse);
  1680. PDEBUG(DBG_PROBE, "Codec=%d", usbvision_device_data[0].Codec);
  1681. sscanf(parse,"%d",&usbvision_device_data[0].VideoChannels);
  1682. goto2next(parse);
  1683. PDEBUG(DBG_PROBE, "VideoChannels=%d", usbvision_device_data[0].VideoChannels);
  1684. switch(*parse)
  1685. {
  1686. case 'P':
  1687. PDEBUG(DBG_PROBE, "VideoNorm=PAL");
  1688. usbvision_device_data[0].VideoNorm=V4L2_STD_PAL;
  1689. break;
  1690. case 'S':
  1691. PDEBUG(DBG_PROBE, "VideoNorm=SECAM");
  1692. usbvision_device_data[0].VideoNorm=V4L2_STD_SECAM;
  1693. break;
  1694. case 'N':
  1695. PDEBUG(DBG_PROBE, "VideoNorm=NTSC");
  1696. usbvision_device_data[0].VideoNorm=V4L2_STD_NTSC;
  1697. break;
  1698. default:
  1699. PDEBUG(DBG_PROBE, "VideoNorm=PAL (by default)");
  1700. usbvision_device_data[0].VideoNorm=V4L2_STD_PAL;
  1701. break;
  1702. }
  1703. goto2next(parse);
  1704. sscanf(parse,"%d",&usbvision_device_data[0].AudioChannels);
  1705. goto2next(parse);
  1706. PDEBUG(DBG_PROBE, "AudioChannels=%d", usbvision_device_data[0].AudioChannels);
  1707. sscanf(parse,"%d",&usbvision_device_data[0].Radio);
  1708. goto2next(parse);
  1709. PDEBUG(DBG_PROBE, "Radio=%d", usbvision_device_data[0].Radio);
  1710. sscanf(parse,"%d",&usbvision_device_data[0].Tuner);
  1711. goto2next(parse);
  1712. PDEBUG(DBG_PROBE, "Tuner=%d", usbvision_device_data[0].Tuner);
  1713. sscanf(parse,"%d",&usbvision_device_data[0].TunerType);
  1714. goto2next(parse);
  1715. PDEBUG(DBG_PROBE, "TunerType=%d", usbvision_device_data[0].TunerType);
  1716. sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg1);
  1717. goto2next(parse);
  1718. PDEBUG(DBG_PROBE, "Vin_Reg1=%d", usbvision_device_data[0].Vin_Reg1);
  1719. sscanf(parse,"%d",&usbvision_device_data[0].Vin_Reg2);
  1720. goto2next(parse);
  1721. PDEBUG(DBG_PROBE, "Vin_Reg2=%d", usbvision_device_data[0].Vin_Reg2);
  1722. sscanf(parse,"%d",&usbvision_device_data[0].X_Offset);
  1723. goto2next(parse);
  1724. PDEBUG(DBG_PROBE, "X_Offset=%d", usbvision_device_data[0].X_Offset);
  1725. sscanf(parse,"%d",&usbvision_device_data[0].Y_Offset);
  1726. goto2next(parse);
  1727. PDEBUG(DBG_PROBE, "Y_Offset=%d", usbvision_device_data[0].Y_Offset);
  1728. sscanf(parse,"%d",&usbvision_device_data[0].Dvi_yuv);
  1729. PDEBUG(DBG_PROBE, "Dvi_yuv=%d", usbvision_device_data[0].Dvi_yuv);
  1730. //add to usbvision_table also
  1731. usbvision_table[0].match_flags=USB_DEVICE_ID_MATCH_DEVICE;
  1732. usbvision_table[0].idVendor=usbvision_device_data[0].idVendor;
  1733. usbvision_table[0].idProduct=usbvision_device_data[0].idProduct;
  1734. }
  1735. }
  1736. /*
  1737. * usbvision_init()
  1738. *
  1739. * This code is run to initialize the driver.
  1740. *
  1741. */
  1742. static int __init usbvision_init(void)
  1743. {
  1744. int errCode;
  1745. PDEBUG(DBG_PROBE, "");
  1746. PDEBUG(DBG_IOCTL, "IOCTL debugging is enabled [video]");
  1747. PDEBUG(DBG_IO, "IO debugging is enabled [video]");
  1748. PDEBUG(DBG_PROBE, "PROBE debugging is enabled [video]");
  1749. PDEBUG(DBG_MMAP, "MMAP debugging is enabled [video]");
  1750. /* disable planar mode support unless compression enabled */
  1751. if (isocMode != ISOC_MODE_COMPRESS ) {
  1752. // FIXME : not the right way to set supported flag
  1753. usbvision_v4l2_format[6].supported = 0; // V4L2_PIX_FMT_YVU420
  1754. usbvision_v4l2_format[7].supported = 0; // V4L2_PIX_FMT_YUV422P
  1755. }
  1756. customdevice_process();
  1757. errCode = usb_register(&usbvision_driver);
  1758. if (errCode == 0) {
  1759. info(DRIVER_DESC " : " USBVISION_VERSION_STRING);
  1760. PDEBUG(DBG_PROBE, "success");
  1761. }
  1762. return errCode;
  1763. }
  1764. static void __exit usbvision_exit(void)
  1765. {
  1766. PDEBUG(DBG_PROBE, "");
  1767. usb_deregister(&usbvision_driver);
  1768. PDEBUG(DBG_PROBE, "success");
  1769. }
  1770. module_init(usbvision_init);
  1771. module_exit(usbvision_exit);
  1772. /*
  1773. * Overrides for Emacs so that we follow Linus's tabbing style.
  1774. * ---------------------------------------------------------------------------
  1775. * Local variables:
  1776. * c-basic-offset: 8
  1777. * End:
  1778. */