usbvision-video.c 48 KB

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