usbvision-video.c 53 KB

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