usbvision-video.c 57 KB

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