usbvision-video.c 50 KB

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