usbvision-video.c 47 KB

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