usbvision-video.c 55 KB

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