usbvision-video.c 46 KB

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