usbvision-video.c 46 KB

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