stv680.c 44 KB

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