stv680.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  1. /*
  2. * STV0680 USB Camera Driver, by Kevin Sisson (kjsisson@bellsouth.net)
  3. *
  4. * Thanks to STMicroelectronics for information on the usb commands, and
  5. * to Steve Miller at STM for his help and encouragement while I was
  6. * writing this driver.
  7. *
  8. * This driver is based heavily on the
  9. * Endpoints (formerly known as AOX) se401 USB Camera Driver
  10. * Copyright (c) 2000 Jeroen B. Vreeken (pe1rxq@amsat.org)
  11. *
  12. * Still somewhat based on the Linux ov511 driver.
  13. *
  14. * This program is free software; you can redistribute it and/or modify it
  15. * under the terms of the GNU General Public License as published by the
  16. * Free Software Foundation; either version 2 of the License, or (at your
  17. * option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  21. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  22. * for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software Foundation,
  26. * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27. *
  28. * History:
  29. * ver 0.1 October, 2001. Initial attempt.
  30. *
  31. * ver 0.2 November, 2001. Fixed asbility to resize, added brightness
  32. * function, made more stable (?)
  33. *
  34. * ver 0.21 Nov, 2001. Added gamma correction and white balance,
  35. * due to Alexander Schwartz. Still trying to
  36. * improve stablility. Moved stuff into stv680.h
  37. *
  38. * ver 0.22 Nov, 2001. Added sharpen function (by Michael Sweet,
  39. * mike@easysw.com) from GIMP, also used in pencam.
  40. * Simple, fast, good integer math routine.
  41. *
  42. * ver 0.23 Dec, 2001 (gkh)
  43. * Took out sharpen function, ran code through
  44. * Lindent, and did other minor tweaks to get
  45. * things to work properly with 2.5.1
  46. *
  47. * ver 0.24 Jan, 2002 (kjs)
  48. * Fixed the problem with webcam crashing after
  49. * two pictures. Changed the way pic is halved to
  50. * improve quality. Got rid of green line around
  51. * frame. Fix brightness reset when changing size
  52. * bug. Adjusted gamma filters slightly.
  53. *
  54. * ver 0.25 Jan, 2002 (kjs)
  55. * Fixed a bug in which the driver sometimes attempted
  56. * to set to a non-supported size. This allowed
  57. * gnomemeeting to work.
  58. * Fixed proc entry removal bug.
  59. */
  60. #include <linux/module.h>
  61. #include <linux/init.h>
  62. #include <linux/vmalloc.h>
  63. #include <linux/slab.h>
  64. #include <linux/pagemap.h>
  65. #include <linux/errno.h>
  66. #include <linux/videodev.h>
  67. #include <media/v4l2-common.h>
  68. #include <linux/usb.h>
  69. #include <linux/mutex.h>
  70. #include "stv680.h"
  71. static int video_nr = -1;
  72. static int swapRGB = 0; /* default for auto sleect */
  73. static int swapRGB_on = 0; /* default to allow auto select; -1=swap never, +1= swap always */
  74. static unsigned int debug = 0;
  75. #define PDEBUG(level, fmt, args...) \
  76. do { \
  77. if (debug >= level) \
  78. info("[%s:%d] " fmt, __FUNCTION__, __LINE__ , ## args); \
  79. } while (0)
  80. /*
  81. * Version Information
  82. */
  83. #define DRIVER_VERSION "v0.25"
  84. #define DRIVER_AUTHOR "Kevin Sisson <kjsisson@bellsouth.net>"
  85. #define DRIVER_DESC "STV0680 USB Camera Driver"
  86. MODULE_AUTHOR (DRIVER_AUTHOR);
  87. MODULE_DESCRIPTION (DRIVER_DESC);
  88. MODULE_LICENSE ("GPL");
  89. module_param(debug, int, S_IRUGO | S_IWUSR);
  90. MODULE_PARM_DESC (debug, "Debug enabled or not");
  91. module_param(swapRGB_on, int, 0);
  92. MODULE_PARM_DESC (swapRGB_on, "Red/blue swap: 1=always, 0=auto, -1=never");
  93. module_param(video_nr, int, 0);
  94. /********************************************************************
  95. *
  96. * Memory management
  97. *
  98. * This is a shameless copy from the USB-cpia driver (linux kernel
  99. * version 2.3.29 or so, I have no idea what this code actually does ;).
  100. * Actually it seems to be a copy of a shameless copy of the bttv-driver.
  101. * Or that is a copy of a shameless copy of ... (To the powers: is there
  102. * no generic kernel-function to do this sort of stuff?)
  103. *
  104. * Yes, it was a shameless copy from the bttv-driver. IIRC, Alan says
  105. * there will be one, but apparentely not yet -jerdfelt
  106. *
  107. * So I copied it again for the ov511 driver -claudio
  108. *
  109. * Same for the se401 driver -Jeroen
  110. *
  111. * And the STV0680 driver - Kevin
  112. ********************************************************************/
  113. static void *rvmalloc (unsigned long size)
  114. {
  115. void *mem;
  116. unsigned long adr;
  117. size = PAGE_ALIGN(size);
  118. mem = vmalloc_32 (size);
  119. if (!mem)
  120. return NULL;
  121. memset (mem, 0, size); /* Clear the ram out, no junk to the user */
  122. adr = (unsigned long) mem;
  123. while (size > 0) {
  124. SetPageReserved(vmalloc_to_page((void *)adr));
  125. adr += PAGE_SIZE;
  126. size -= PAGE_SIZE;
  127. }
  128. return mem;
  129. }
  130. static void rvfree (void *mem, unsigned long size)
  131. {
  132. unsigned long adr;
  133. if (!mem)
  134. return;
  135. adr = (unsigned long) mem;
  136. while ((long) size > 0) {
  137. ClearPageReserved(vmalloc_to_page((void *)adr));
  138. adr += PAGE_SIZE;
  139. size -= PAGE_SIZE;
  140. }
  141. vfree (mem);
  142. }
  143. /*********************************************************************
  144. * pencam read/write functions
  145. ********************************************************************/
  146. static int stv_sndctrl (int set, struct usb_stv *stv680, unsigned short req, unsigned short value, unsigned char *buffer, int size)
  147. {
  148. int ret = -1;
  149. switch (set) {
  150. case 0: /* 0xc1 */
  151. ret = usb_control_msg (stv680->udev,
  152. usb_rcvctrlpipe (stv680->udev, 0),
  153. req,
  154. (USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
  155. value, 0, buffer, size, PENCAM_TIMEOUT);
  156. break;
  157. case 1: /* 0x41 */
  158. ret = usb_control_msg (stv680->udev,
  159. usb_sndctrlpipe (stv680->udev, 0),
  160. req,
  161. (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT),
  162. value, 0, buffer, size, PENCAM_TIMEOUT);
  163. break;
  164. case 2: /* 0x80 */
  165. ret = usb_control_msg (stv680->udev,
  166. usb_rcvctrlpipe (stv680->udev, 0),
  167. req,
  168. (USB_DIR_IN | USB_RECIP_DEVICE),
  169. value, 0, buffer, size, PENCAM_TIMEOUT);
  170. break;
  171. case 3: /* 0x40 */
  172. ret = usb_control_msg (stv680->udev,
  173. usb_sndctrlpipe (stv680->udev, 0),
  174. req,
  175. (USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE),
  176. value, 0, buffer, size, PENCAM_TIMEOUT);
  177. break;
  178. }
  179. if ((ret < 0) && (req != 0x0a)) {
  180. PDEBUG (1, "STV(e): usb_control_msg error %i, request = 0x%x, error = %i", set, req, ret);
  181. }
  182. return ret;
  183. }
  184. static int stv_set_config (struct usb_stv *dev, int configuration, int interface, int alternate)
  185. {
  186. if (configuration != dev->udev->actconfig->desc.bConfigurationValue
  187. || usb_reset_configuration (dev->udev) < 0) {
  188. PDEBUG (1, "STV(e): FAILED to reset configuration %i", configuration);
  189. return -1;
  190. }
  191. if (usb_set_interface (dev->udev, interface, alternate) < 0) {
  192. PDEBUG (1, "STV(e): FAILED to set alternate interface %i", alternate);
  193. return -1;
  194. }
  195. return 0;
  196. }
  197. static int stv_stop_video (struct usb_stv *dev)
  198. {
  199. int i;
  200. unsigned char *buf;
  201. buf = kmalloc (40, GFP_KERNEL);
  202. if (buf == NULL) {
  203. PDEBUG (0, "STV(e): Out of (small buf) memory");
  204. return -1;
  205. }
  206. /* this is a high priority command; it stops all lower order commands */
  207. if ((i = stv_sndctrl (1, dev, 0x04, 0x0000, buf, 0x0)) < 0) {
  208. i = stv_sndctrl (0, dev, 0x80, 0, buf, 0x02); /* Get Last Error; 2 = busy */
  209. PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buf[0], buf[1]);
  210. } else {
  211. PDEBUG (1, "STV(i): Camera reset to idle mode.");
  212. }
  213. if ((i = stv_set_config (dev, 1, 0, 0)) < 0)
  214. PDEBUG (1, "STV(e): Reset config during exit failed");
  215. /* get current mode */
  216. buf[0] = 0xf0;
  217. if ((i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08)) != 0x08) /* get mode */
  218. PDEBUG (0, "STV(e): Stop_video: problem setting original mode");
  219. if (dev->origMode != buf[0]) {
  220. memset (buf, 0, 8);
  221. buf[0] = (unsigned char) dev->origMode;
  222. if ((i = stv_sndctrl (3, dev, 0x07, 0x0100, buf, 0x08)) != 0x08) {
  223. PDEBUG (0, "STV(e): Stop_video: Set_Camera_Mode failed");
  224. i = -1;
  225. }
  226. buf[0] = 0xf0;
  227. i = stv_sndctrl (0, dev, 0x87, 0, buf, 0x08);
  228. if ((i != 0x08) || (buf[0] != dev->origMode)) {
  229. PDEBUG (0, "STV(e): camera NOT set to original resolution.");
  230. i = -1;
  231. } else
  232. PDEBUG (0, "STV(i): Camera set to original resolution");
  233. }
  234. /* origMode */
  235. kfree(buf);
  236. return i;
  237. }
  238. static int stv_set_video_mode (struct usb_stv *dev)
  239. {
  240. int i, stop_video = 1;
  241. unsigned char *buf;
  242. buf = kmalloc (40, GFP_KERNEL);
  243. if (buf == NULL) {
  244. PDEBUG (0, "STV(e): Out of (small buf) memory");
  245. return -1;
  246. }
  247. if ((i = stv_set_config (dev, 1, 0, 0)) < 0) {
  248. kfree(buf);
  249. return i;
  250. }
  251. i = stv_sndctrl (2, dev, 0x06, 0x0100, buf, 0x12);
  252. if (!(i > 0) && (buf[8] == 0x53) && (buf[9] == 0x05)) {
  253. PDEBUG (1, "STV(e): Could not get descriptor 0100.");
  254. goto error;
  255. }
  256. /* set alternate interface 1 */
  257. if ((i = stv_set_config (dev, 1, 0, 1)) < 0)
  258. goto error;
  259. if ((i = stv_sndctrl (0, dev, 0x85, 0, buf, 0x10)) != 0x10)
  260. goto error;
  261. PDEBUG (1, "STV(i): Setting video mode.");
  262. /* Switch to Video mode: 0x0100 = VGA (640x480), 0x0000 = CIF (352x288) 0x0300 = QVGA (320x240) */
  263. if ((i = stv_sndctrl (1, dev, 0x09, dev->VideoMode, buf, 0x0)) < 0) {
  264. stop_video = 0;
  265. goto error;
  266. }
  267. goto exit;
  268. error:
  269. kfree(buf);
  270. if (stop_video == 1)
  271. stv_stop_video (dev);
  272. return -1;
  273. exit:
  274. kfree(buf);
  275. return 0;
  276. }
  277. static int stv_init (struct usb_stv *stv680)
  278. {
  279. int i = 0;
  280. unsigned char *buffer;
  281. unsigned long int bufsize;
  282. buffer = kzalloc (40, GFP_KERNEL);
  283. if (buffer == NULL) {
  284. PDEBUG (0, "STV(e): Out of (small buf) memory");
  285. return -1;
  286. }
  287. udelay (100);
  288. /* set config 1, interface 0, alternate 0 */
  289. if ((i = stv_set_config (stv680, 1, 0, 0)) < 0) {
  290. kfree(buffer);
  291. PDEBUG (0, "STV(e): set config 1,0,0 failed");
  292. return -1;
  293. }
  294. /* ping camera to be sure STV0680 is present */
  295. if ((i = stv_sndctrl (0, stv680, 0x88, 0x5678, buffer, 0x02)) != 0x02)
  296. goto error;
  297. if ((buffer[0] != 0x56) || (buffer[1] != 0x78)) {
  298. PDEBUG (1, "STV(e): camera ping failed!!");
  299. goto error;
  300. }
  301. /* get camera descriptor */
  302. if ((i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x09)) != 0x09)
  303. goto error;
  304. i = stv_sndctrl (2, stv680, 0x06, 0x0200, buffer, 0x22);
  305. if (!(i >= 0) && (buffer[7] == 0xa0) && (buffer[8] == 0x23)) {
  306. PDEBUG (1, "STV(e): Could not get descriptor 0200.");
  307. goto error;
  308. }
  309. if ((i = stv_sndctrl (0, stv680, 0x8a, 0, buffer, 0x02)) != 0x02)
  310. goto error;
  311. if ((i = stv_sndctrl (0, stv680, 0x8b, 0, buffer, 0x24)) != 0x24)
  312. goto error;
  313. if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
  314. goto error;
  315. stv680->SupportedModes = buffer[7];
  316. i = stv680->SupportedModes;
  317. stv680->CIF = 0;
  318. stv680->VGA = 0;
  319. stv680->QVGA = 0;
  320. if (i & 1)
  321. stv680->CIF = 1;
  322. if (i & 2)
  323. stv680->VGA = 1;
  324. if (i & 8)
  325. stv680->QVGA = 1;
  326. if (stv680->SupportedModes == 0) {
  327. PDEBUG (0, "STV(e): There are NO supported STV680 modes!!");
  328. i = -1;
  329. goto error;
  330. } else {
  331. if (stv680->CIF)
  332. PDEBUG (0, "STV(i): CIF is supported");
  333. if (stv680->QVGA)
  334. PDEBUG (0, "STV(i): QVGA is supported");
  335. }
  336. /* FW rev, ASIC rev, sensor ID */
  337. PDEBUG (1, "STV(i): Firmware rev is %i.%i", buffer[0], buffer[1]);
  338. PDEBUG (1, "STV(i): ASIC rev is %i.%i", buffer[2], buffer[3]);
  339. PDEBUG (1, "STV(i): Sensor ID is %i", (buffer[4]*16) + (buffer[5]>>4));
  340. /* set alternate interface 1 */
  341. if ((i = stv_set_config (stv680, 1, 0, 1)) < 0)
  342. goto error;
  343. if ((i = stv_sndctrl (0, stv680, 0x85, 0, buffer, 0x10)) != 0x10)
  344. goto error;
  345. if ((i = stv_sndctrl (0, stv680, 0x8d, 0, buffer, 0x08)) != 0x08)
  346. goto error;
  347. i = buffer[3];
  348. PDEBUG (0, "STV(i): Camera has %i pictures.", i);
  349. /* get current mode */
  350. if ((i = stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08)) != 0x08)
  351. goto error;
  352. stv680->origMode = buffer[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
  353. /* This will attemp CIF mode, if supported. If not, set to QVGA */
  354. memset (buffer, 0, 8);
  355. if (stv680->CIF)
  356. buffer[0] = 0x00;
  357. else if (stv680->QVGA)
  358. buffer[0] = 0x03;
  359. if ((i = stv_sndctrl (3, stv680, 0x07, 0x0100, buffer, 0x08)) != 0x08) {
  360. PDEBUG (0, "STV(i): Set_Camera_Mode failed");
  361. i = -1;
  362. goto error;
  363. }
  364. buffer[0] = 0xf0;
  365. stv_sndctrl (0, stv680, 0x87, 0, buffer, 0x08);
  366. if (((stv680->CIF == 1) && (buffer[0] != 0x00)) || ((stv680->QVGA == 1) && (buffer[0] != 0x03))) {
  367. PDEBUG (0, "STV(e): Error setting camera video mode!");
  368. i = -1;
  369. goto error;
  370. } else {
  371. if (buffer[0] == 0) {
  372. stv680->VideoMode = 0x0000;
  373. PDEBUG (0, "STV(i): Video Mode set to CIF");
  374. }
  375. if (buffer[0] == 0x03) {
  376. stv680->VideoMode = 0x0300;
  377. PDEBUG (0, "STV(i): Video Mode set to QVGA");
  378. }
  379. }
  380. if ((i = stv_sndctrl (0, stv680, 0x8f, 0, buffer, 0x10)) != 0x10)
  381. goto error;
  382. bufsize = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | (buffer[3]);
  383. stv680->cwidth = (buffer[4] << 8) | (buffer[5]); /* ->camera = 322, 356, 644 */
  384. stv680->cheight = (buffer[6] << 8) | (buffer[7]); /* ->camera = 242, 292, 484 */
  385. stv680->origGain = buffer[12];
  386. goto exit;
  387. error:
  388. i = stv_sndctrl (0, stv680, 0x80, 0, buffer, 0x02); /* Get Last Error */
  389. PDEBUG (1, "STV(i): last error: %i, command = 0x%x", buffer[0], buffer[1]);
  390. kfree(buffer);
  391. return -1;
  392. exit:
  393. kfree(buffer);
  394. /* video = 320x240, 352x288 */
  395. if (stv680->CIF == 1) {
  396. stv680->maxwidth = 352;
  397. stv680->maxheight = 288;
  398. stv680->vwidth = 352;
  399. stv680->vheight = 288;
  400. }
  401. if (stv680->QVGA == 1) {
  402. stv680->maxwidth = 320;
  403. stv680->maxheight = 240;
  404. stv680->vwidth = 320;
  405. stv680->vheight = 240;
  406. }
  407. stv680->rawbufsize = bufsize; /* must be ./. by 8 */
  408. stv680->maxframesize = bufsize * 3; /* RGB size */
  409. PDEBUG (2, "STV(i): cwidth = %i, cheight = %i", stv680->cwidth, stv680->cheight);
  410. PDEBUG (1, "STV(i): width = %i, height = %i, rawbufsize = %li", stv680->vwidth, stv680->vheight, stv680->rawbufsize);
  411. /* some default values */
  412. stv680->bulk_in_endpointAddr = 0x82;
  413. stv680->dropped = 0;
  414. stv680->error = 0;
  415. stv680->framecount = 0;
  416. stv680->readcount = 0;
  417. stv680->streaming = 0;
  418. /* bright, white, colour, hue, contrast are set by software, not in stv0680 */
  419. stv680->brightness = 32767;
  420. stv680->chgbright = 0;
  421. stv680->whiteness = 0; /* only for greyscale */
  422. stv680->colour = 32767;
  423. stv680->contrast = 32767;
  424. stv680->hue = 32767;
  425. stv680->palette = STV_VIDEO_PALETTE;
  426. stv680->depth = 24; /* rgb24 bits */
  427. if ((swapRGB_on == 0) && (swapRGB == 0))
  428. PDEBUG (1, "STV(i): swapRGB is (auto) OFF");
  429. else if ((swapRGB_on == 0) && (swapRGB == 1))
  430. PDEBUG (1, "STV(i): swapRGB is (auto) ON");
  431. else if (swapRGB_on == 1)
  432. PDEBUG (1, "STV(i): swapRGB is (forced) ON");
  433. else if (swapRGB_on == -1)
  434. PDEBUG (1, "STV(i): swapRGB is (forced) OFF");
  435. if (stv_set_video_mode (stv680) < 0) {
  436. PDEBUG (0, "STV(e): Could not set video mode in stv_init");
  437. return -1;
  438. }
  439. return 0;
  440. }
  441. /***************** last of pencam routines *******************/
  442. /****************************************************************************
  443. * sysfs
  444. ***************************************************************************/
  445. #define stv680_file(name, variable, field) \
  446. static ssize_t show_##name(struct device *class_dev, \
  447. struct device_attribute *attr, char *buf) \
  448. { \
  449. struct video_device *vdev = to_video_device(class_dev); \
  450. struct usb_stv *stv = video_get_drvdata(vdev); \
  451. return sprintf(buf, field, stv->variable); \
  452. } \
  453. static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  454. stv680_file(model, camera_name, "%s\n");
  455. stv680_file(in_use, user, "%d\n");
  456. stv680_file(streaming, streaming, "%d\n");
  457. stv680_file(palette, palette, "%i\n");
  458. stv680_file(frames_total, readcount, "%d\n");
  459. stv680_file(frames_read, framecount, "%d\n");
  460. stv680_file(packets_dropped, dropped, "%d\n");
  461. stv680_file(decoding_errors, error, "%d\n");
  462. static int stv680_create_sysfs_files(struct video_device *vdev)
  463. {
  464. int rc;
  465. rc = video_device_create_file(vdev, &dev_attr_model);
  466. if (rc) goto err;
  467. rc = video_device_create_file(vdev, &dev_attr_in_use);
  468. if (rc) goto err_model;
  469. rc = video_device_create_file(vdev, &dev_attr_streaming);
  470. if (rc) goto err_inuse;
  471. rc = video_device_create_file(vdev, &dev_attr_palette);
  472. if (rc) goto err_stream;
  473. rc = video_device_create_file(vdev, &dev_attr_frames_total);
  474. if (rc) goto err_pal;
  475. rc = video_device_create_file(vdev, &dev_attr_frames_read);
  476. if (rc) goto err_framtot;
  477. rc = video_device_create_file(vdev, &dev_attr_packets_dropped);
  478. if (rc) goto err_framread;
  479. rc = video_device_create_file(vdev, &dev_attr_decoding_errors);
  480. if (rc) goto err_dropped;
  481. return 0;
  482. err_dropped:
  483. video_device_remove_file(vdev, &dev_attr_packets_dropped);
  484. err_framread:
  485. video_device_remove_file(vdev, &dev_attr_frames_read);
  486. err_framtot:
  487. video_device_remove_file(vdev, &dev_attr_frames_total);
  488. err_pal:
  489. video_device_remove_file(vdev, &dev_attr_palette);
  490. err_stream:
  491. video_device_remove_file(vdev, &dev_attr_streaming);
  492. err_inuse:
  493. video_device_remove_file(vdev, &dev_attr_in_use);
  494. err_model:
  495. video_device_remove_file(vdev, &dev_attr_model);
  496. err:
  497. return rc;
  498. }
  499. static void stv680_remove_sysfs_files(struct video_device *vdev)
  500. {
  501. video_device_remove_file(vdev, &dev_attr_model);
  502. video_device_remove_file(vdev, &dev_attr_in_use);
  503. video_device_remove_file(vdev, &dev_attr_streaming);
  504. video_device_remove_file(vdev, &dev_attr_palette);
  505. video_device_remove_file(vdev, &dev_attr_frames_total);
  506. video_device_remove_file(vdev, &dev_attr_frames_read);
  507. video_device_remove_file(vdev, &dev_attr_packets_dropped);
  508. video_device_remove_file(vdev, &dev_attr_decoding_errors);
  509. }
  510. /********************************************************************
  511. * Camera control
  512. *******************************************************************/
  513. static int stv680_get_pict (struct usb_stv *stv680, struct video_picture *p)
  514. {
  515. /* This sets values for v4l interface. max/min = 65535/0 */
  516. p->brightness = stv680->brightness;
  517. p->whiteness = stv680->whiteness; /* greyscale */
  518. p->colour = stv680->colour;
  519. p->contrast = stv680->contrast;
  520. p->hue = stv680->hue;
  521. p->palette = stv680->palette;
  522. p->depth = stv680->depth;
  523. return 0;
  524. }
  525. static int stv680_set_pict (struct usb_stv *stv680, struct video_picture *p)
  526. {
  527. /* See above stv680_get_pict */
  528. if (p->palette != STV_VIDEO_PALETTE) {
  529. PDEBUG (2, "STV(e): Palette set error in _set_pic");
  530. return 1;
  531. }
  532. if (stv680->brightness != p->brightness) {
  533. stv680->chgbright = 1;
  534. stv680->brightness = p->brightness;
  535. }
  536. stv680->whiteness = p->whiteness; /* greyscale */
  537. stv680->colour = p->colour;
  538. stv680->contrast = p->contrast;
  539. stv680->hue = p->hue;
  540. stv680->palette = p->palette;
  541. stv680->depth = p->depth;
  542. return 0;
  543. }
  544. static void stv680_video_irq (struct urb *urb)
  545. {
  546. struct usb_stv *stv680 = urb->context;
  547. int length = urb->actual_length;
  548. if (length < stv680->rawbufsize)
  549. PDEBUG (2, "STV(i): Lost data in transfer: exp %li, got %i", stv680->rawbufsize, length);
  550. /* ohoh... */
  551. if (!stv680->streaming)
  552. return;
  553. if (!stv680->udev) {
  554. PDEBUG (0, "STV(e): device vapourished in video_irq");
  555. return;
  556. }
  557. /* 0 sized packets happen if we are to fast, but sometimes the camera
  558. keeps sending them forever...
  559. */
  560. if (length && !urb->status) {
  561. stv680->nullpackets = 0;
  562. switch (stv680->scratch[stv680->scratch_next].state) {
  563. case BUFFER_READY:
  564. case BUFFER_BUSY:
  565. stv680->dropped++;
  566. break;
  567. case BUFFER_UNUSED:
  568. memcpy (stv680->scratch[stv680->scratch_next].data,
  569. (unsigned char *) urb->transfer_buffer, length);
  570. stv680->scratch[stv680->scratch_next].state = BUFFER_READY;
  571. stv680->scratch[stv680->scratch_next].length = length;
  572. if (waitqueue_active (&stv680->wq)) {
  573. wake_up_interruptible (&stv680->wq);
  574. }
  575. stv680->scratch_overflow = 0;
  576. stv680->scratch_next++;
  577. if (stv680->scratch_next >= STV680_NUMSCRATCH)
  578. stv680->scratch_next = 0;
  579. break;
  580. } /* switch */
  581. } else {
  582. stv680->nullpackets++;
  583. if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
  584. if (waitqueue_active (&stv680->wq)) {
  585. wake_up_interruptible (&stv680->wq);
  586. }
  587. }
  588. } /* if - else */
  589. /* Resubmit urb for new data */
  590. urb->status = 0;
  591. urb->dev = stv680->udev;
  592. if (usb_submit_urb (urb, GFP_ATOMIC))
  593. PDEBUG (0, "STV(e): urb burned down in video irq");
  594. return;
  595. } /* _video_irq */
  596. static int stv680_start_stream (struct usb_stv *stv680)
  597. {
  598. struct urb *urb;
  599. int err = 0, i;
  600. stv680->streaming = 1;
  601. /* Do some memory allocation */
  602. for (i = 0; i < STV680_NUMFRAMES; i++) {
  603. stv680->frame[i].data = stv680->fbuf + i * stv680->maxframesize;
  604. stv680->frame[i].curpix = 0;
  605. }
  606. /* packet size = 4096 */
  607. for (i = 0; i < STV680_NUMSBUF; i++) {
  608. stv680->sbuf[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
  609. if (stv680->sbuf[i].data == NULL) {
  610. PDEBUG (0, "STV(e): Could not kmalloc raw data buffer %i", i);
  611. goto nomem_err;
  612. }
  613. }
  614. stv680->scratch_next = 0;
  615. stv680->scratch_use = 0;
  616. stv680->scratch_overflow = 0;
  617. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  618. stv680->scratch[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
  619. if (stv680->scratch[i].data == NULL) {
  620. PDEBUG (0, "STV(e): Could not kmalloc raw scratch buffer %i", i);
  621. goto nomem_err;
  622. }
  623. stv680->scratch[i].state = BUFFER_UNUSED;
  624. }
  625. for (i = 0; i < STV680_NUMSBUF; i++) {
  626. urb = usb_alloc_urb (0, GFP_KERNEL);
  627. if (!urb)
  628. goto nomem_err;
  629. /* sbuf is urb->transfer_buffer, later gets memcpyed to scratch */
  630. usb_fill_bulk_urb (urb, stv680->udev,
  631. usb_rcvbulkpipe (stv680->udev, stv680->bulk_in_endpointAddr),
  632. stv680->sbuf[i].data, stv680->rawbufsize,
  633. stv680_video_irq, stv680);
  634. stv680->urb[i] = urb;
  635. err = usb_submit_urb (stv680->urb[i], GFP_KERNEL);
  636. if (err) {
  637. PDEBUG (0, "STV(e): urb burned down with err "
  638. "%d in start stream %d", err, i);
  639. goto nomem_err;
  640. }
  641. } /* i STV680_NUMSBUF */
  642. stv680->framecount = 0;
  643. return 0;
  644. nomem_err:
  645. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  646. kfree(stv680->scratch[i].data);
  647. stv680->scratch[i].data = NULL;
  648. }
  649. for (i = 0; i < STV680_NUMSBUF; i++) {
  650. usb_kill_urb(stv680->urb[i]);
  651. usb_free_urb(stv680->urb[i]);
  652. stv680->urb[i] = NULL;
  653. kfree(stv680->sbuf[i].data);
  654. stv680->sbuf[i].data = NULL;
  655. }
  656. return -ENOMEM;
  657. }
  658. static int stv680_stop_stream (struct usb_stv *stv680)
  659. {
  660. int i;
  661. if (!stv680->streaming || !stv680->udev)
  662. return 1;
  663. stv680->streaming = 0;
  664. for (i = 0; i < STV680_NUMSBUF; i++)
  665. if (stv680->urb[i]) {
  666. usb_kill_urb (stv680->urb[i]);
  667. usb_free_urb (stv680->urb[i]);
  668. stv680->urb[i] = NULL;
  669. kfree(stv680->sbuf[i].data);
  670. }
  671. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  672. kfree(stv680->scratch[i].data);
  673. stv680->scratch[i].data = NULL;
  674. }
  675. return 0;
  676. }
  677. static int stv680_set_size (struct usb_stv *stv680, int width, int height)
  678. {
  679. int wasstreaming = stv680->streaming;
  680. /* Check to see if we need to change */
  681. if ((stv680->vwidth == width) && (stv680->vheight == height))
  682. return 0;
  683. PDEBUG (1, "STV(i): size request for %i x %i", width, height);
  684. /* Check for a valid mode */
  685. if ((!width || !height) || ((width & 1) || (height & 1))) {
  686. PDEBUG (1, "STV(e): set_size error: request: v.width = %i, v.height = %i actual: stv.width = %i, stv.height = %i", width, height, stv680->vwidth, stv680->vheight);
  687. return 1;
  688. }
  689. if ((width < (stv680->maxwidth / 2)) || (height < (stv680->maxheight / 2))) {
  690. width = stv680->maxwidth / 2;
  691. height = stv680->maxheight / 2;
  692. } else if ((width >= 158) && (width <= 166) && (stv680->QVGA == 1)) {
  693. width = 160;
  694. height = 120;
  695. } else if ((width >= 172) && (width <= 180) && (stv680->CIF == 1)) {
  696. width = 176;
  697. height = 144;
  698. } else if ((width >= 318) && (width <= 350) && (stv680->QVGA == 1)) {
  699. width = 320;
  700. height = 240;
  701. } else if ((width >= 350) && (width <= 358) && (stv680->CIF == 1)) {
  702. width = 352;
  703. height = 288;
  704. } else {
  705. PDEBUG (1, "STV(e): request for non-supported size: request: v.width = %i, v.height = %i actual: stv.width = %i, stv.height = %i", width, height, stv680->vwidth, stv680->vheight);
  706. return 1;
  707. }
  708. /* Stop a current stream and start it again at the new size */
  709. if (wasstreaming)
  710. stv680_stop_stream (stv680);
  711. stv680->vwidth = width;
  712. stv680->vheight = height;
  713. PDEBUG (1, "STV(i): size set to %i x %i", stv680->vwidth, stv680->vheight);
  714. if (wasstreaming)
  715. stv680_start_stream (stv680);
  716. return 0;
  717. }
  718. /**********************************************************************
  719. * Video Decoding
  720. **********************************************************************/
  721. /******* routines from the pencam program; hey, they work! ********/
  722. /*
  723. * STV0680 Vision Camera Chipset Driver
  724. * Copyright (C) 2000 Adam Harrison <adam@antispin.org>
  725. */
  726. #define RED 0
  727. #define GREEN 1
  728. #define BLUE 2
  729. #define AD(x, y, w) (((y)*(w)+(x))*3)
  730. static void bayer_unshuffle (struct usb_stv *stv680, struct stv680_scratch *buffer)
  731. {
  732. int x, y, i;
  733. int w = stv680->cwidth;
  734. int vw = stv680->cwidth, vh = stv680->cheight;
  735. unsigned int p = 0;
  736. int colour = 0, bayer = 0;
  737. unsigned char *raw = buffer->data;
  738. struct stv680_frame *frame = &stv680->frame[stv680->curframe];
  739. unsigned char *output = frame->data;
  740. unsigned char *temp = frame->data;
  741. int offset = buffer->offset;
  742. if (frame->curpix == 0) {
  743. if (frame->grabstate == FRAME_READY) {
  744. frame->grabstate = FRAME_GRABBING;
  745. }
  746. }
  747. if (offset != frame->curpix) { /* Regard frame as lost :( */
  748. frame->curpix = 0;
  749. stv680->error++;
  750. return;
  751. }
  752. if ((stv680->vwidth == 320) || (stv680->vwidth == 160)) {
  753. vw = 320;
  754. vh = 240;
  755. }
  756. if ((stv680->vwidth == 352) || (stv680->vwidth == 176)) {
  757. vw = 352;
  758. vh = 288;
  759. }
  760. memset (output, 0, 3 * vw * vh); /* clear output matrix. */
  761. for (y = 0; y < vh; y++) {
  762. for (x = 0; x < vw; x++) {
  763. if (x & 1)
  764. p = *(raw + y * w + (x >> 1));
  765. else
  766. p = *(raw + y * w + (x >> 1) + (w >> 1));
  767. if (y & 1)
  768. bayer = 2;
  769. else
  770. bayer = 0;
  771. if (x & 1)
  772. bayer++;
  773. switch (bayer) {
  774. case 0:
  775. case 3:
  776. colour = 1;
  777. break;
  778. case 1:
  779. colour = 0;
  780. break;
  781. case 2:
  782. colour = 2;
  783. break;
  784. }
  785. i = (y * vw + x) * 3;
  786. *(output + i + colour) = (unsigned char) p;
  787. } /* for x */
  788. } /* for y */
  789. /****** gamma correction plus hardcoded white balance */
  790. /* Thanks to Alexander Schwartx <alexander.schwartx@gmx.net> for this code.
  791. Correction values red[], green[], blue[], are generated by
  792. (pow(i/256.0, GAMMA)*255.0)*white balanceRGB where GAMMA=0.55, 1<i<255.
  793. White balance (RGB)= 1.0, 1.17, 1.48. Values are calculated as double float and
  794. converted to unsigned char. Values are in stv680.h */
  795. for (y = 0; y < vh; y++) {
  796. for (x = 0; x < vw; x++) {
  797. i = (y * vw + x) * 3;
  798. *(output + i) = red[*(output + i)];
  799. *(output + i + 1) = green[*(output + i + 1)];
  800. *(output + i + 2) = blue[*(output + i + 2)];
  801. }
  802. }
  803. /****** bayer demosaic ******/
  804. for (y = 1; y < (vh - 1); y++) {
  805. for (x = 1; x < (vw - 1); x++) { /* work out pixel type */
  806. if (y & 1)
  807. bayer = 0;
  808. else
  809. bayer = 2;
  810. if (!(x & 1))
  811. bayer++;
  812. switch (bayer) {
  813. case 0: /* green. blue lr, red tb */
  814. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y, vw) + BLUE) + (int) *(output + AD (x + 1, y, vw) + BLUE)) >> 1;
  815. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x, y - 1, vw) + RED) + (int) *(output + AD (x, y + 1, vw) + RED)) >> 1;
  816. break;
  817. case 1: /* blue. green lrtb, red diagonals */
  818. *(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
  819. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y - 1, vw) + RED) + (int) *(output + AD (x - 1, y + 1, vw) + RED) + (int) *(output + AD (x + 1, y - 1, vw) + RED) + (int) *(output + AD (x + 1, y + 1, vw) + RED)) >> 2;
  820. break;
  821. case 2: /* red. green lrtb, blue diagonals */
  822. *(output + AD (x, y, vw) + GREEN) = ((int) *(output + AD (x - 1, y, vw) + GREEN) + (int) *(output + AD (x + 1, y, vw) + GREEN) + (int) *(output + AD (x, y - 1, vw) + GREEN) + (int) *(output + AD (x, y + 1, vw) + GREEN)) >> 2;
  823. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y - 1, vw) + BLUE) + (int) *(output + AD (x + 1, y - 1, vw) + BLUE) + (int) *(output + AD (x - 1, y + 1, vw) + BLUE) + (int) *(output + AD (x + 1, y + 1, vw) + BLUE)) >> 2;
  824. break;
  825. case 3: /* green. red lr, blue tb */
  826. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y, vw) + RED) + (int) *(output + AD (x + 1, y, vw) + RED)) >> 1;
  827. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x, y - 1, vw) + BLUE) + (int) *(output + AD (x, y + 1, vw) + BLUE)) >> 1;
  828. break;
  829. } /* switch */
  830. } /* for x */
  831. } /* for y - end demosaic */
  832. /* fix top and bottom row, left and right side */
  833. i = vw * 3;
  834. memcpy (output, (output + i), i);
  835. memcpy ((output + (vh * i)), (output + ((vh - 1) * i)), i);
  836. for (y = 0; y < vh; y++) {
  837. i = y * vw * 3;
  838. memcpy ((output + i), (output + i + 3), 3);
  839. memcpy ((output + i + (vw * 3)), (output + i + (vw - 1) * 3), 3);
  840. }
  841. /* process all raw data, then trim to size if necessary */
  842. if ((stv680->vwidth == 160) || (stv680->vwidth == 176)) {
  843. i = 0;
  844. for (y = 0; y < vh; y++) {
  845. if (!(y & 1)) {
  846. for (x = 0; x < vw; x++) {
  847. p = (y * vw + x) * 3;
  848. if (!(x & 1)) {
  849. *(output + i) = *(output + p);
  850. *(output + i + 1) = *(output + p + 1);
  851. *(output + i + 2) = *(output + p + 2);
  852. i += 3;
  853. }
  854. } /* for x */
  855. }
  856. } /* for y */
  857. }
  858. /* reset to proper width */
  859. if ((stv680->vwidth == 160)) {
  860. vw = 160;
  861. vh = 120;
  862. }
  863. if ((stv680->vwidth == 176)) {
  864. vw = 176;
  865. vh = 144;
  866. }
  867. /* output is RGB; some programs want BGR */
  868. /* swapRGB_on=0 -> program decides; swapRGB_on=1, always swap */
  869. /* swapRGB_on=-1, never swap */
  870. if (((swapRGB == 1) && (swapRGB_on != -1)) || (swapRGB_on == 1)) {
  871. for (y = 0; y < vh; y++) {
  872. for (x = 0; x < vw; x++) {
  873. i = (y * vw + x) * 3;
  874. *(temp) = *(output + i);
  875. *(output + i) = *(output + i + 2);
  876. *(output + i + 2) = *(temp);
  877. }
  878. }
  879. }
  880. /* brightness */
  881. if (stv680->chgbright == 1) {
  882. if (stv680->brightness >= 32767) {
  883. p = (stv680->brightness - 32767) / 256;
  884. for (x = 0; x < (vw * vh * 3); x++) {
  885. if ((*(output + x) + (unsigned char) p) > 255)
  886. *(output + x) = 255;
  887. else
  888. *(output + x) += (unsigned char) p;
  889. } /* for */
  890. } else {
  891. p = (32767 - stv680->brightness) / 256;
  892. for (x = 0; x < (vw * vh * 3); x++) {
  893. if ((unsigned char) p > *(output + x))
  894. *(output + x) = 0;
  895. else
  896. *(output + x) -= (unsigned char) p;
  897. } /* for */
  898. } /* else */
  899. }
  900. /* if */
  901. frame->curpix = 0;
  902. frame->curlinepix = 0;
  903. frame->grabstate = FRAME_DONE;
  904. stv680->framecount++;
  905. stv680->readcount++;
  906. if (stv680->frame[(stv680->curframe + 1) & (STV680_NUMFRAMES - 1)].grabstate == FRAME_READY) {
  907. stv680->curframe = (stv680->curframe + 1) & (STV680_NUMFRAMES - 1);
  908. }
  909. } /* bayer_unshuffle */
  910. /******* end routines from the pencam program *********/
  911. static int stv680_newframe (struct usb_stv *stv680, int framenr)
  912. {
  913. int errors = 0;
  914. while (stv680->streaming && (stv680->frame[framenr].grabstate == FRAME_READY || stv680->frame[framenr].grabstate == FRAME_GRABBING)) {
  915. if (!stv680->frame[framenr].curpix) {
  916. errors++;
  917. }
  918. wait_event_interruptible (stv680->wq, (stv680->scratch[stv680->scratch_use].state == BUFFER_READY));
  919. if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
  920. stv680->nullpackets = 0;
  921. PDEBUG (2, "STV(i): too many null length packets, restarting capture");
  922. stv680_stop_stream (stv680);
  923. stv680_start_stream (stv680);
  924. } else {
  925. if (stv680->scratch[stv680->scratch_use].state != BUFFER_READY) {
  926. stv680->frame[framenr].grabstate = FRAME_ERROR;
  927. PDEBUG (2, "STV(e): FRAME_ERROR in _newframe");
  928. return -EIO;
  929. }
  930. stv680->scratch[stv680->scratch_use].state = BUFFER_BUSY;
  931. bayer_unshuffle (stv680, &stv680->scratch[stv680->scratch_use]);
  932. stv680->scratch[stv680->scratch_use].state = BUFFER_UNUSED;
  933. stv680->scratch_use++;
  934. if (stv680->scratch_use >= STV680_NUMSCRATCH)
  935. stv680->scratch_use = 0;
  936. if (errors > STV680_MAX_ERRORS) {
  937. errors = 0;
  938. PDEBUG (2, "STV(i): too many errors, restarting capture");
  939. stv680_stop_stream (stv680);
  940. stv680_start_stream (stv680);
  941. }
  942. } /* else */
  943. } /* while */
  944. return 0;
  945. }
  946. /*********************************************************************
  947. * Video4Linux
  948. *********************************************************************/
  949. static int stv_open (struct inode *inode, struct file *file)
  950. {
  951. struct video_device *dev = video_devdata(file);
  952. struct usb_stv *stv680 = video_get_drvdata(dev);
  953. int err = 0;
  954. /* we are called with the BKL held */
  955. stv680->user = 1;
  956. err = stv_init (stv680); /* main initialization routine for camera */
  957. if (err >= 0) {
  958. stv680->fbuf = rvmalloc (stv680->maxframesize * STV680_NUMFRAMES);
  959. if (!stv680->fbuf) {
  960. PDEBUG (0, "STV(e): Could not rvmalloc frame bufer");
  961. err = -ENOMEM;
  962. }
  963. file->private_data = dev;
  964. }
  965. if (err)
  966. stv680->user = 0;
  967. return err;
  968. }
  969. static int stv_close (struct inode *inode, struct file *file)
  970. {
  971. struct video_device *dev = file->private_data;
  972. struct usb_stv *stv680 = video_get_drvdata(dev);
  973. int i;
  974. for (i = 0; i < STV680_NUMFRAMES; i++)
  975. stv680->frame[i].grabstate = FRAME_UNUSED;
  976. if (stv680->streaming)
  977. stv680_stop_stream (stv680);
  978. if ((i = stv_stop_video (stv680)) < 0)
  979. PDEBUG (1, "STV(e): stop_video failed in stv_close");
  980. rvfree (stv680->fbuf, stv680->maxframesize * STV680_NUMFRAMES);
  981. stv680->user = 0;
  982. if (stv680->removed) {
  983. kfree(stv680);
  984. stv680 = NULL;
  985. PDEBUG (0, "STV(i): device unregistered");
  986. }
  987. file->private_data = NULL;
  988. return 0;
  989. }
  990. static int stv680_do_ioctl (struct inode *inode, struct file *file,
  991. unsigned int cmd, void *arg)
  992. {
  993. struct video_device *vdev = file->private_data;
  994. struct usb_stv *stv680 = video_get_drvdata(vdev);
  995. if (!stv680->udev)
  996. return -EIO;
  997. switch (cmd) {
  998. case VIDIOCGCAP:{
  999. struct video_capability *b = arg;
  1000. strcpy (b->name, stv680->camera_name);
  1001. b->type = VID_TYPE_CAPTURE;
  1002. b->channels = 1;
  1003. b->audios = 0;
  1004. b->maxwidth = stv680->maxwidth;
  1005. b->maxheight = stv680->maxheight;
  1006. b->minwidth = stv680->maxwidth / 2;
  1007. b->minheight = stv680->maxheight / 2;
  1008. return 0;
  1009. }
  1010. case VIDIOCGCHAN:{
  1011. struct video_channel *v = arg;
  1012. if (v->channel != 0)
  1013. return -EINVAL;
  1014. v->flags = 0;
  1015. v->tuners = 0;
  1016. v->type = VIDEO_TYPE_CAMERA;
  1017. strcpy (v->name, "STV Camera");
  1018. return 0;
  1019. }
  1020. case VIDIOCSCHAN:{
  1021. struct video_channel *v = arg;
  1022. if (v->channel != 0)
  1023. return -EINVAL;
  1024. return 0;
  1025. }
  1026. case VIDIOCGPICT:{
  1027. struct video_picture *p = arg;
  1028. stv680_get_pict (stv680, p);
  1029. return 0;
  1030. }
  1031. case VIDIOCSPICT:{
  1032. struct video_picture *p = arg;
  1033. if (stv680_set_pict (stv680, p))
  1034. return -EINVAL;
  1035. return 0;
  1036. }
  1037. case VIDIOCSWIN:{
  1038. struct video_window *vw = arg;
  1039. if (vw->flags)
  1040. return -EINVAL;
  1041. if (vw->clipcount)
  1042. return -EINVAL;
  1043. if (vw->width != stv680->vwidth) {
  1044. if (stv680_set_size (stv680, vw->width, vw->height)) {
  1045. PDEBUG (2, "STV(e): failed (from user) set size in VIDIOCSWIN");
  1046. return -EINVAL;
  1047. }
  1048. }
  1049. return 0;
  1050. }
  1051. case VIDIOCGWIN:{
  1052. struct video_window *vw = arg;
  1053. vw->x = 0; /* FIXME */
  1054. vw->y = 0;
  1055. vw->chromakey = 0;
  1056. vw->flags = 0;
  1057. vw->clipcount = 0;
  1058. vw->width = stv680->vwidth;
  1059. vw->height = stv680->vheight;
  1060. return 0;
  1061. }
  1062. case VIDIOCGMBUF:{
  1063. struct video_mbuf *vm = arg;
  1064. int i;
  1065. memset (vm, 0, sizeof (*vm));
  1066. vm->size = STV680_NUMFRAMES * stv680->maxframesize;
  1067. vm->frames = STV680_NUMFRAMES;
  1068. for (i = 0; i < STV680_NUMFRAMES; i++)
  1069. vm->offsets[i] = stv680->maxframesize * i;
  1070. return 0;
  1071. }
  1072. case VIDIOCMCAPTURE:{
  1073. struct video_mmap *vm = arg;
  1074. if (vm->format != STV_VIDEO_PALETTE) {
  1075. PDEBUG (2, "STV(i): VIDIOCMCAPTURE vm.format (%i) != VIDEO_PALETTE (%i)",
  1076. vm->format, STV_VIDEO_PALETTE);
  1077. if ((vm->format == 3) && (swapRGB_on == 0)) {
  1078. PDEBUG (2, "STV(i): VIDIOCMCAPTURE swapRGB is (auto) ON");
  1079. /* this may fix those apps (e.g., xawtv) that want BGR */
  1080. swapRGB = 1;
  1081. }
  1082. return -EINVAL;
  1083. }
  1084. if (vm->frame >= STV680_NUMFRAMES) {
  1085. PDEBUG (2, "STV(e): VIDIOCMCAPTURE vm.frame > NUMFRAMES");
  1086. return -EINVAL;
  1087. }
  1088. if ((stv680->frame[vm->frame].grabstate == FRAME_ERROR)
  1089. || (stv680->frame[vm->frame].grabstate == FRAME_GRABBING)) {
  1090. PDEBUG (2, "STV(e): VIDIOCMCAPTURE grabstate (%i) error",
  1091. stv680->frame[vm->frame].grabstate);
  1092. return -EBUSY;
  1093. }
  1094. /* Is this according to the v4l spec??? */
  1095. if (stv680->vwidth != vm->width) {
  1096. if (stv680_set_size (stv680, vm->width, vm->height)) {
  1097. PDEBUG (2, "STV(e): VIDIOCMCAPTURE set_size failed");
  1098. return -EINVAL;
  1099. }
  1100. }
  1101. stv680->frame[vm->frame].grabstate = FRAME_READY;
  1102. if (!stv680->streaming)
  1103. stv680_start_stream (stv680);
  1104. return 0;
  1105. }
  1106. case VIDIOCSYNC:{
  1107. int *frame = arg;
  1108. int ret = 0;
  1109. if (*frame < 0 || *frame >= STV680_NUMFRAMES) {
  1110. PDEBUG (2, "STV(e): Bad frame # in VIDIOCSYNC");
  1111. return -EINVAL;
  1112. }
  1113. ret = stv680_newframe (stv680, *frame);
  1114. stv680->frame[*frame].grabstate = FRAME_UNUSED;
  1115. return ret;
  1116. }
  1117. case VIDIOCGFBUF:{
  1118. struct video_buffer *vb = arg;
  1119. memset (vb, 0, sizeof (*vb));
  1120. return 0;
  1121. }
  1122. case VIDIOCKEY:
  1123. return 0;
  1124. case VIDIOCCAPTURE:
  1125. {
  1126. PDEBUG (2, "STV(e): VIDIOCCAPTURE failed");
  1127. return -EINVAL;
  1128. }
  1129. case VIDIOCSFBUF:
  1130. case VIDIOCGTUNER:
  1131. case VIDIOCSTUNER:
  1132. case VIDIOCGFREQ:
  1133. case VIDIOCSFREQ:
  1134. case VIDIOCGAUDIO:
  1135. case VIDIOCSAUDIO:
  1136. return -EINVAL;
  1137. default:
  1138. return -ENOIOCTLCMD;
  1139. } /* end switch */
  1140. return 0;
  1141. }
  1142. static int stv680_ioctl(struct inode *inode, struct file *file,
  1143. unsigned int cmd, unsigned long arg)
  1144. {
  1145. return video_usercopy(inode, file, cmd, arg, stv680_do_ioctl);
  1146. }
  1147. static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
  1148. {
  1149. struct video_device *dev = file->private_data;
  1150. struct usb_stv *stv680 = video_get_drvdata(dev);
  1151. unsigned long start = vma->vm_start;
  1152. unsigned long size = vma->vm_end-vma->vm_start;
  1153. unsigned long page, pos;
  1154. mutex_lock(&stv680->lock);
  1155. if (stv680->udev == NULL) {
  1156. mutex_unlock(&stv680->lock);
  1157. return -EIO;
  1158. }
  1159. if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1)
  1160. & ~(PAGE_SIZE - 1))) {
  1161. mutex_unlock(&stv680->lock);
  1162. return -EINVAL;
  1163. }
  1164. pos = (unsigned long) stv680->fbuf;
  1165. while (size > 0) {
  1166. page = vmalloc_to_pfn((void *)pos);
  1167. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  1168. mutex_unlock(&stv680->lock);
  1169. return -EAGAIN;
  1170. }
  1171. start += PAGE_SIZE;
  1172. pos += PAGE_SIZE;
  1173. if (size > PAGE_SIZE)
  1174. size -= PAGE_SIZE;
  1175. else
  1176. size = 0;
  1177. }
  1178. mutex_unlock(&stv680->lock);
  1179. return 0;
  1180. }
  1181. static ssize_t stv680_read (struct file *file, char __user *buf,
  1182. size_t count, loff_t *ppos)
  1183. {
  1184. struct video_device *dev = file->private_data;
  1185. unsigned long int realcount = count;
  1186. int ret = 0;
  1187. struct usb_stv *stv680 = video_get_drvdata(dev);
  1188. unsigned long int i;
  1189. if (STV680_NUMFRAMES != 2) {
  1190. PDEBUG (0, "STV(e): STV680_NUMFRAMES needs to be 2!");
  1191. return -1;
  1192. }
  1193. if (stv680->udev == NULL)
  1194. return -EIO;
  1195. if (realcount > (stv680->vwidth * stv680->vheight * 3))
  1196. realcount = stv680->vwidth * stv680->vheight * 3;
  1197. /* Shouldn't happen: */
  1198. if (stv680->frame[0].grabstate == FRAME_GRABBING) {
  1199. PDEBUG (2, "STV(e): FRAME_GRABBING in stv680_read");
  1200. return -EBUSY;
  1201. }
  1202. stv680->frame[0].grabstate = FRAME_READY;
  1203. stv680->frame[1].grabstate = FRAME_UNUSED;
  1204. stv680->curframe = 0;
  1205. if (!stv680->streaming)
  1206. stv680_start_stream (stv680);
  1207. if (!stv680->streaming) {
  1208. ret = stv680_newframe (stv680, 0); /* ret should = 0 */
  1209. }
  1210. ret = stv680_newframe (stv680, 0);
  1211. if (!ret) {
  1212. if ((i = copy_to_user (buf, stv680->frame[0].data, realcount)) != 0) {
  1213. PDEBUG (2, "STV(e): copy_to_user frame 0 failed, ret count = %li", i);
  1214. return -EFAULT;
  1215. }
  1216. } else {
  1217. realcount = ret;
  1218. }
  1219. stv680->frame[0].grabstate = FRAME_UNUSED;
  1220. return realcount;
  1221. } /* stv680_read */
  1222. static const struct file_operations stv680_fops = {
  1223. .owner = THIS_MODULE,
  1224. .open = stv_open,
  1225. .release = stv_close,
  1226. .read = stv680_read,
  1227. .mmap = stv680_mmap,
  1228. .ioctl = stv680_ioctl,
  1229. .compat_ioctl = v4l_compat_ioctl32,
  1230. .llseek = no_llseek,
  1231. };
  1232. static struct video_device stv680_template = {
  1233. .owner = THIS_MODULE,
  1234. .name = "STV0680 USB camera",
  1235. .type = VID_TYPE_CAPTURE,
  1236. .hardware = VID_HARDWARE_SE401,
  1237. .fops = &stv680_fops,
  1238. .release = video_device_release,
  1239. .minor = -1,
  1240. };
  1241. static int stv680_probe (struct usb_interface *intf, const struct usb_device_id *id)
  1242. {
  1243. struct usb_device *dev = interface_to_usbdev(intf);
  1244. struct usb_host_interface *interface;
  1245. struct usb_stv *stv680 = NULL;
  1246. char *camera_name = NULL;
  1247. int retval = 0;
  1248. /* We don't handle multi-config cameras */
  1249. if (dev->descriptor.bNumConfigurations != 1) {
  1250. PDEBUG (0, "STV(e): Number of Configurations != 1");
  1251. return -ENODEV;
  1252. }
  1253. interface = &intf->altsetting[0];
  1254. /* Is it a STV680? */
  1255. if ((le16_to_cpu(dev->descriptor.idVendor) == USB_PENCAM_VENDOR_ID) &&
  1256. (le16_to_cpu(dev->descriptor.idProduct) == USB_PENCAM_PRODUCT_ID)) {
  1257. camera_name = "STV0680";
  1258. PDEBUG (0, "STV(i): STV0680 camera found.");
  1259. } else if ((le16_to_cpu(dev->descriptor.idVendor) == USB_CREATIVEGOMINI_VENDOR_ID) &&
  1260. (le16_to_cpu(dev->descriptor.idProduct) == USB_CREATIVEGOMINI_PRODUCT_ID)) {
  1261. camera_name = "Creative WebCam Go Mini";
  1262. PDEBUG (0, "STV(i): Creative WebCam Go Mini found.");
  1263. } else {
  1264. PDEBUG (0, "STV(e): Vendor/Product ID do not match STV0680 or Creative WebCam Go Mini values.");
  1265. PDEBUG (0, "STV(e): Check that the STV0680 or Creative WebCam Go Mini camera is connected to the computer.");
  1266. retval = -ENODEV;
  1267. goto error;
  1268. }
  1269. /* We found one */
  1270. if ((stv680 = kzalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) {
  1271. PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct.");
  1272. retval = -ENOMEM;
  1273. goto error;
  1274. }
  1275. stv680->udev = dev;
  1276. stv680->camera_name = camera_name;
  1277. stv680->vdev = video_device_alloc();
  1278. if (!stv680->vdev) {
  1279. retval = -ENOMEM;
  1280. goto error;
  1281. }
  1282. memcpy(stv680->vdev, &stv680_template, sizeof(stv680_template));
  1283. stv680->vdev->dev = &intf->dev;
  1284. video_set_drvdata(stv680->vdev, stv680);
  1285. memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name));
  1286. init_waitqueue_head (&stv680->wq);
  1287. mutex_init (&stv680->lock);
  1288. wmb ();
  1289. if (video_register_device (stv680->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
  1290. PDEBUG (0, "STV(e): video_register_device failed");
  1291. retval = -EIO;
  1292. goto error_vdev;
  1293. }
  1294. PDEBUG (0, "STV(i): registered new video device: video%d", stv680->vdev->minor);
  1295. usb_set_intfdata (intf, stv680);
  1296. retval = stv680_create_sysfs_files(stv680->vdev);
  1297. if (retval)
  1298. goto error_unreg;
  1299. return 0;
  1300. error_unreg:
  1301. video_unregister_device(stv680->vdev);
  1302. error_vdev:
  1303. video_device_release(stv680->vdev);
  1304. error:
  1305. kfree(stv680);
  1306. return retval;
  1307. }
  1308. static inline void usb_stv680_remove_disconnected (struct usb_stv *stv680)
  1309. {
  1310. int i;
  1311. stv680->udev = NULL;
  1312. stv680->frame[0].grabstate = FRAME_ERROR;
  1313. stv680->frame[1].grabstate = FRAME_ERROR;
  1314. stv680->streaming = 0;
  1315. wake_up_interruptible (&stv680->wq);
  1316. for (i = 0; i < STV680_NUMSBUF; i++)
  1317. if (stv680->urb[i]) {
  1318. usb_kill_urb (stv680->urb[i]);
  1319. usb_free_urb (stv680->urb[i]);
  1320. stv680->urb[i] = NULL;
  1321. kfree(stv680->sbuf[i].data);
  1322. }
  1323. for (i = 0; i < STV680_NUMSCRATCH; i++)
  1324. kfree(stv680->scratch[i].data);
  1325. PDEBUG (0, "STV(i): %s disconnected", stv680->camera_name);
  1326. /* Free the memory */
  1327. kfree(stv680);
  1328. }
  1329. static void stv680_disconnect (struct usb_interface *intf)
  1330. {
  1331. struct usb_stv *stv680 = usb_get_intfdata (intf);
  1332. usb_set_intfdata (intf, NULL);
  1333. if (stv680) {
  1334. /* We don't want people trying to open up the device */
  1335. if (stv680->vdev) {
  1336. stv680_remove_sysfs_files(stv680->vdev);
  1337. video_unregister_device(stv680->vdev);
  1338. stv680->vdev = NULL;
  1339. }
  1340. if (!stv680->user) {
  1341. usb_stv680_remove_disconnected (stv680);
  1342. } else {
  1343. stv680->removed = 1;
  1344. }
  1345. }
  1346. }
  1347. static struct usb_driver stv680_driver = {
  1348. .name = "stv680",
  1349. .probe = stv680_probe,
  1350. .disconnect = stv680_disconnect,
  1351. .id_table = device_table
  1352. };
  1353. /********************************************************************
  1354. * Module routines
  1355. ********************************************************************/
  1356. static int __init usb_stv680_init (void)
  1357. {
  1358. if (usb_register (&stv680_driver) < 0) {
  1359. PDEBUG (0, "STV(e): Could not setup STV0680 driver");
  1360. return -1;
  1361. }
  1362. PDEBUG (0, "STV(i): usb camera driver version %s registering", DRIVER_VERSION);
  1363. info(DRIVER_DESC " " DRIVER_VERSION);
  1364. return 0;
  1365. }
  1366. static void __exit usb_stv680_exit (void)
  1367. {
  1368. usb_deregister (&stv680_driver);
  1369. PDEBUG (0, "STV(i): driver deregistered");
  1370. }
  1371. module_init (usb_stv680_init);
  1372. module_exit (usb_stv680_exit);