stv680.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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 class_device *class_dev, char *buf) \
  447. { \
  448. struct video_device *vdev = to_video_device(class_dev); \
  449. struct usb_stv *stv = video_get_drvdata(vdev); \
  450. return sprintf(buf, field, stv->variable); \
  451. } \
  452. static CLASS_DEVICE_ATTR(name, S_IRUGO, show_##name, NULL);
  453. stv680_file(model, camera_name, "%s\n");
  454. stv680_file(in_use, user, "%d\n");
  455. stv680_file(streaming, streaming, "%d\n");
  456. stv680_file(palette, palette, "%i\n");
  457. stv680_file(frames_total, readcount, "%d\n");
  458. stv680_file(frames_read, framecount, "%d\n");
  459. stv680_file(packets_dropped, dropped, "%d\n");
  460. stv680_file(decoding_errors, error, "%d\n");
  461. static int stv680_create_sysfs_files(struct video_device *vdev)
  462. {
  463. int rc;
  464. rc = video_device_create_file(vdev, &class_device_attr_model);
  465. if (rc) goto err;
  466. rc = video_device_create_file(vdev, &class_device_attr_in_use);
  467. if (rc) goto err_model;
  468. rc = video_device_create_file(vdev, &class_device_attr_streaming);
  469. if (rc) goto err_inuse;
  470. rc = video_device_create_file(vdev, &class_device_attr_palette);
  471. if (rc) goto err_stream;
  472. rc = video_device_create_file(vdev, &class_device_attr_frames_total);
  473. if (rc) goto err_pal;
  474. rc = video_device_create_file(vdev, &class_device_attr_frames_read);
  475. if (rc) goto err_framtot;
  476. rc = video_device_create_file(vdev, &class_device_attr_packets_dropped);
  477. if (rc) goto err_framread;
  478. rc = video_device_create_file(vdev, &class_device_attr_decoding_errors);
  479. if (rc) goto err_dropped;
  480. return 0;
  481. err_dropped:
  482. video_device_remove_file(vdev, &class_device_attr_packets_dropped);
  483. err_framread:
  484. video_device_remove_file(vdev, &class_device_attr_frames_read);
  485. err_framtot:
  486. video_device_remove_file(vdev, &class_device_attr_frames_total);
  487. err_pal:
  488. video_device_remove_file(vdev, &class_device_attr_palette);
  489. err_stream:
  490. video_device_remove_file(vdev, &class_device_attr_streaming);
  491. err_inuse:
  492. video_device_remove_file(vdev, &class_device_attr_in_use);
  493. err_model:
  494. video_device_remove_file(vdev, &class_device_attr_model);
  495. err:
  496. return rc;
  497. }
  498. static void stv680_remove_sysfs_files(struct video_device *vdev)
  499. {
  500. video_device_remove_file(vdev, &class_device_attr_model);
  501. video_device_remove_file(vdev, &class_device_attr_in_use);
  502. video_device_remove_file(vdev, &class_device_attr_streaming);
  503. video_device_remove_file(vdev, &class_device_attr_palette);
  504. video_device_remove_file(vdev, &class_device_attr_frames_total);
  505. video_device_remove_file(vdev, &class_device_attr_frames_read);
  506. video_device_remove_file(vdev, &class_device_attr_packets_dropped);
  507. video_device_remove_file(vdev, &class_device_attr_decoding_errors);
  508. }
  509. /********************************************************************
  510. * Camera control
  511. *******************************************************************/
  512. static int stv680_get_pict (struct usb_stv *stv680, struct video_picture *p)
  513. {
  514. /* This sets values for v4l interface. max/min = 65535/0 */
  515. p->brightness = stv680->brightness;
  516. p->whiteness = stv680->whiteness; /* greyscale */
  517. p->colour = stv680->colour;
  518. p->contrast = stv680->contrast;
  519. p->hue = stv680->hue;
  520. p->palette = stv680->palette;
  521. p->depth = stv680->depth;
  522. return 0;
  523. }
  524. static int stv680_set_pict (struct usb_stv *stv680, struct video_picture *p)
  525. {
  526. /* See above stv680_get_pict */
  527. if (p->palette != STV_VIDEO_PALETTE) {
  528. PDEBUG (2, "STV(e): Palette set error in _set_pic");
  529. return 1;
  530. }
  531. if (stv680->brightness != p->brightness) {
  532. stv680->chgbright = 1;
  533. stv680->brightness = p->brightness;
  534. }
  535. stv680->whiteness = p->whiteness; /* greyscale */
  536. stv680->colour = p->colour;
  537. stv680->contrast = p->contrast;
  538. stv680->hue = p->hue;
  539. stv680->palette = p->palette;
  540. stv680->depth = p->depth;
  541. return 0;
  542. }
  543. static void stv680_video_irq (struct urb *urb)
  544. {
  545. struct usb_stv *stv680 = urb->context;
  546. int length = urb->actual_length;
  547. if (length < stv680->rawbufsize)
  548. PDEBUG (2, "STV(i): Lost data in transfer: exp %li, got %i", stv680->rawbufsize, length);
  549. /* ohoh... */
  550. if (!stv680->streaming)
  551. return;
  552. if (!stv680->udev) {
  553. PDEBUG (0, "STV(e): device vapourished in video_irq");
  554. return;
  555. }
  556. /* 0 sized packets happen if we are to fast, but sometimes the camera
  557. keeps sending them forever...
  558. */
  559. if (length && !urb->status) {
  560. stv680->nullpackets = 0;
  561. switch (stv680->scratch[stv680->scratch_next].state) {
  562. case BUFFER_READY:
  563. case BUFFER_BUSY:
  564. stv680->dropped++;
  565. break;
  566. case BUFFER_UNUSED:
  567. memcpy (stv680->scratch[stv680->scratch_next].data,
  568. (unsigned char *) urb->transfer_buffer, length);
  569. stv680->scratch[stv680->scratch_next].state = BUFFER_READY;
  570. stv680->scratch[stv680->scratch_next].length = length;
  571. if (waitqueue_active (&stv680->wq)) {
  572. wake_up_interruptible (&stv680->wq);
  573. }
  574. stv680->scratch_overflow = 0;
  575. stv680->scratch_next++;
  576. if (stv680->scratch_next >= STV680_NUMSCRATCH)
  577. stv680->scratch_next = 0;
  578. break;
  579. } /* switch */
  580. } else {
  581. stv680->nullpackets++;
  582. if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
  583. if (waitqueue_active (&stv680->wq)) {
  584. wake_up_interruptible (&stv680->wq);
  585. }
  586. }
  587. } /* if - else */
  588. /* Resubmit urb for new data */
  589. urb->status = 0;
  590. urb->dev = stv680->udev;
  591. if (usb_submit_urb (urb, GFP_ATOMIC))
  592. PDEBUG (0, "STV(e): urb burned down in video irq");
  593. return;
  594. } /* _video_irq */
  595. static int stv680_start_stream (struct usb_stv *stv680)
  596. {
  597. struct urb *urb;
  598. int err = 0, i;
  599. stv680->streaming = 1;
  600. /* Do some memory allocation */
  601. for (i = 0; i < STV680_NUMFRAMES; i++) {
  602. stv680->frame[i].data = stv680->fbuf + i * stv680->maxframesize;
  603. stv680->frame[i].curpix = 0;
  604. }
  605. /* packet size = 4096 */
  606. for (i = 0; i < STV680_NUMSBUF; i++) {
  607. stv680->sbuf[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
  608. if (stv680->sbuf[i].data == NULL) {
  609. PDEBUG (0, "STV(e): Could not kmalloc raw data buffer %i", i);
  610. return -1;
  611. }
  612. }
  613. stv680->scratch_next = 0;
  614. stv680->scratch_use = 0;
  615. stv680->scratch_overflow = 0;
  616. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  617. stv680->scratch[i].data = kmalloc (stv680->rawbufsize, GFP_KERNEL);
  618. if (stv680->scratch[i].data == NULL) {
  619. PDEBUG (0, "STV(e): Could not kmalloc raw scratch buffer %i", i);
  620. return -1;
  621. }
  622. stv680->scratch[i].state = BUFFER_UNUSED;
  623. }
  624. for (i = 0; i < STV680_NUMSBUF; i++) {
  625. urb = usb_alloc_urb (0, GFP_KERNEL);
  626. if (!urb)
  627. return -ENOMEM;
  628. /* sbuf is urb->transfer_buffer, later gets memcpyed to scratch */
  629. usb_fill_bulk_urb (urb, stv680->udev,
  630. usb_rcvbulkpipe (stv680->udev, stv680->bulk_in_endpointAddr),
  631. stv680->sbuf[i].data, stv680->rawbufsize,
  632. stv680_video_irq, stv680);
  633. stv680->urb[i] = urb;
  634. err = usb_submit_urb (stv680->urb[i], GFP_KERNEL);
  635. if (err)
  636. PDEBUG (0, "STV(e): urb burned down in start stream");
  637. } /* i STV680_NUMSBUF */
  638. stv680->framecount = 0;
  639. return 0;
  640. }
  641. static int stv680_stop_stream (struct usb_stv *stv680)
  642. {
  643. int i;
  644. if (!stv680->streaming || !stv680->udev)
  645. return 1;
  646. stv680->streaming = 0;
  647. for (i = 0; i < STV680_NUMSBUF; i++)
  648. if (stv680->urb[i]) {
  649. usb_kill_urb (stv680->urb[i]);
  650. usb_free_urb (stv680->urb[i]);
  651. stv680->urb[i] = NULL;
  652. kfree(stv680->sbuf[i].data);
  653. }
  654. for (i = 0; i < STV680_NUMSCRATCH; i++) {
  655. kfree(stv680->scratch[i].data);
  656. stv680->scratch[i].data = NULL;
  657. }
  658. return 0;
  659. }
  660. static int stv680_set_size (struct usb_stv *stv680, int width, int height)
  661. {
  662. int wasstreaming = stv680->streaming;
  663. /* Check to see if we need to change */
  664. if ((stv680->vwidth == width) && (stv680->vheight == height))
  665. return 0;
  666. PDEBUG (1, "STV(i): size request for %i x %i", width, height);
  667. /* Check for a valid mode */
  668. if ((!width || !height) || ((width & 1) || (height & 1))) {
  669. 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);
  670. return 1;
  671. }
  672. if ((width < (stv680->maxwidth / 2)) || (height < (stv680->maxheight / 2))) {
  673. width = stv680->maxwidth / 2;
  674. height = stv680->maxheight / 2;
  675. } else if ((width >= 158) && (width <= 166) && (stv680->QVGA == 1)) {
  676. width = 160;
  677. height = 120;
  678. } else if ((width >= 172) && (width <= 180) && (stv680->CIF == 1)) {
  679. width = 176;
  680. height = 144;
  681. } else if ((width >= 318) && (width <= 350) && (stv680->QVGA == 1)) {
  682. width = 320;
  683. height = 240;
  684. } else if ((width >= 350) && (width <= 358) && (stv680->CIF == 1)) {
  685. width = 352;
  686. height = 288;
  687. } else {
  688. 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);
  689. return 1;
  690. }
  691. /* Stop a current stream and start it again at the new size */
  692. if (wasstreaming)
  693. stv680_stop_stream (stv680);
  694. stv680->vwidth = width;
  695. stv680->vheight = height;
  696. PDEBUG (1, "STV(i): size set to %i x %i", stv680->vwidth, stv680->vheight);
  697. if (wasstreaming)
  698. stv680_start_stream (stv680);
  699. return 0;
  700. }
  701. /**********************************************************************
  702. * Video Decoding
  703. **********************************************************************/
  704. /******* routines from the pencam program; hey, they work! ********/
  705. /*
  706. * STV0680 Vision Camera Chipset Driver
  707. * Copyright (C) 2000 Adam Harrison <adam@antispin.org>
  708. */
  709. #define RED 0
  710. #define GREEN 1
  711. #define BLUE 2
  712. #define AD(x, y, w) (((y)*(w)+(x))*3)
  713. static void bayer_unshuffle (struct usb_stv *stv680, struct stv680_scratch *buffer)
  714. {
  715. int x, y, i;
  716. int w = stv680->cwidth;
  717. int vw = stv680->cwidth, vh = stv680->cheight;
  718. unsigned int p = 0;
  719. int colour = 0, bayer = 0;
  720. unsigned char *raw = buffer->data;
  721. struct stv680_frame *frame = &stv680->frame[stv680->curframe];
  722. unsigned char *output = frame->data;
  723. unsigned char *temp = frame->data;
  724. int offset = buffer->offset;
  725. if (frame->curpix == 0) {
  726. if (frame->grabstate == FRAME_READY) {
  727. frame->grabstate = FRAME_GRABBING;
  728. }
  729. }
  730. if (offset != frame->curpix) { /* Regard frame as lost :( */
  731. frame->curpix = 0;
  732. stv680->error++;
  733. return;
  734. }
  735. if ((stv680->vwidth == 320) || (stv680->vwidth == 160)) {
  736. vw = 320;
  737. vh = 240;
  738. }
  739. if ((stv680->vwidth == 352) || (stv680->vwidth == 176)) {
  740. vw = 352;
  741. vh = 288;
  742. }
  743. memset (output, 0, 3 * vw * vh); /* clear output matrix. */
  744. for (y = 0; y < vh; y++) {
  745. for (x = 0; x < vw; x++) {
  746. if (x & 1)
  747. p = *(raw + y * w + (x >> 1));
  748. else
  749. p = *(raw + y * w + (x >> 1) + (w >> 1));
  750. if (y & 1)
  751. bayer = 2;
  752. else
  753. bayer = 0;
  754. if (x & 1)
  755. bayer++;
  756. switch (bayer) {
  757. case 0:
  758. case 3:
  759. colour = 1;
  760. break;
  761. case 1:
  762. colour = 0;
  763. break;
  764. case 2:
  765. colour = 2;
  766. break;
  767. }
  768. i = (y * vw + x) * 3;
  769. *(output + i + colour) = (unsigned char) p;
  770. } /* for x */
  771. } /* for y */
  772. /****** gamma correction plus hardcoded white balance */
  773. /* Thanks to Alexander Schwartx <alexander.schwartx@gmx.net> for this code.
  774. Correction values red[], green[], blue[], are generated by
  775. (pow(i/256.0, GAMMA)*255.0)*white balanceRGB where GAMMA=0.55, 1<i<255.
  776. White balance (RGB)= 1.0, 1.17, 1.48. Values are calculated as double float and
  777. converted to unsigned char. Values are in stv680.h */
  778. for (y = 0; y < vh; y++) {
  779. for (x = 0; x < vw; x++) {
  780. i = (y * vw + x) * 3;
  781. *(output + i) = red[*(output + i)];
  782. *(output + i + 1) = green[*(output + i + 1)];
  783. *(output + i + 2) = blue[*(output + i + 2)];
  784. }
  785. }
  786. /****** bayer demosaic ******/
  787. for (y = 1; y < (vh - 1); y++) {
  788. for (x = 1; x < (vw - 1); x++) { /* work out pixel type */
  789. if (y & 1)
  790. bayer = 0;
  791. else
  792. bayer = 2;
  793. if (!(x & 1))
  794. bayer++;
  795. switch (bayer) {
  796. case 0: /* green. blue lr, red tb */
  797. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x - 1, y, vw) + BLUE) + (int) *(output + AD (x + 1, y, vw) + BLUE)) >> 1;
  798. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x, y - 1, vw) + RED) + (int) *(output + AD (x, y + 1, vw) + RED)) >> 1;
  799. break;
  800. case 1: /* blue. green lrtb, red diagonals */
  801. *(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;
  802. *(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;
  803. break;
  804. case 2: /* red. green lrtb, blue diagonals */
  805. *(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;
  806. *(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;
  807. break;
  808. case 3: /* green. red lr, blue tb */
  809. *(output + AD (x, y, vw) + RED) = ((int) *(output + AD (x - 1, y, vw) + RED) + (int) *(output + AD (x + 1, y, vw) + RED)) >> 1;
  810. *(output + AD (x, y, vw) + BLUE) = ((int) *(output + AD (x, y - 1, vw) + BLUE) + (int) *(output + AD (x, y + 1, vw) + BLUE)) >> 1;
  811. break;
  812. } /* switch */
  813. } /* for x */
  814. } /* for y - end demosaic */
  815. /* fix top and bottom row, left and right side */
  816. i = vw * 3;
  817. memcpy (output, (output + i), i);
  818. memcpy ((output + (vh * i)), (output + ((vh - 1) * i)), i);
  819. for (y = 0; y < vh; y++) {
  820. i = y * vw * 3;
  821. memcpy ((output + i), (output + i + 3), 3);
  822. memcpy ((output + i + (vw * 3)), (output + i + (vw - 1) * 3), 3);
  823. }
  824. /* process all raw data, then trim to size if necessary */
  825. if ((stv680->vwidth == 160) || (stv680->vwidth == 176)) {
  826. i = 0;
  827. for (y = 0; y < vh; y++) {
  828. if (!(y & 1)) {
  829. for (x = 0; x < vw; x++) {
  830. p = (y * vw + x) * 3;
  831. if (!(x & 1)) {
  832. *(output + i) = *(output + p);
  833. *(output + i + 1) = *(output + p + 1);
  834. *(output + i + 2) = *(output + p + 2);
  835. i += 3;
  836. }
  837. } /* for x */
  838. }
  839. } /* for y */
  840. }
  841. /* reset to proper width */
  842. if ((stv680->vwidth == 160)) {
  843. vw = 160;
  844. vh = 120;
  845. }
  846. if ((stv680->vwidth == 176)) {
  847. vw = 176;
  848. vh = 144;
  849. }
  850. /* output is RGB; some programs want BGR */
  851. /* swapRGB_on=0 -> program decides; swapRGB_on=1, always swap */
  852. /* swapRGB_on=-1, never swap */
  853. if (((swapRGB == 1) && (swapRGB_on != -1)) || (swapRGB_on == 1)) {
  854. for (y = 0; y < vh; y++) {
  855. for (x = 0; x < vw; x++) {
  856. i = (y * vw + x) * 3;
  857. *(temp) = *(output + i);
  858. *(output + i) = *(output + i + 2);
  859. *(output + i + 2) = *(temp);
  860. }
  861. }
  862. }
  863. /* brightness */
  864. if (stv680->chgbright == 1) {
  865. if (stv680->brightness >= 32767) {
  866. p = (stv680->brightness - 32767) / 256;
  867. for (x = 0; x < (vw * vh * 3); x++) {
  868. if ((*(output + x) + (unsigned char) p) > 255)
  869. *(output + x) = 255;
  870. else
  871. *(output + x) += (unsigned char) p;
  872. } /* for */
  873. } else {
  874. p = (32767 - stv680->brightness) / 256;
  875. for (x = 0; x < (vw * vh * 3); x++) {
  876. if ((unsigned char) p > *(output + x))
  877. *(output + x) = 0;
  878. else
  879. *(output + x) -= (unsigned char) p;
  880. } /* for */
  881. } /* else */
  882. }
  883. /* if */
  884. frame->curpix = 0;
  885. frame->curlinepix = 0;
  886. frame->grabstate = FRAME_DONE;
  887. stv680->framecount++;
  888. stv680->readcount++;
  889. if (stv680->frame[(stv680->curframe + 1) & (STV680_NUMFRAMES - 1)].grabstate == FRAME_READY) {
  890. stv680->curframe = (stv680->curframe + 1) & (STV680_NUMFRAMES - 1);
  891. }
  892. } /* bayer_unshuffle */
  893. /******* end routines from the pencam program *********/
  894. static int stv680_newframe (struct usb_stv *stv680, int framenr)
  895. {
  896. int errors = 0;
  897. while (stv680->streaming && (stv680->frame[framenr].grabstate == FRAME_READY || stv680->frame[framenr].grabstate == FRAME_GRABBING)) {
  898. if (!stv680->frame[framenr].curpix) {
  899. errors++;
  900. }
  901. wait_event_interruptible (stv680->wq, (stv680->scratch[stv680->scratch_use].state == BUFFER_READY));
  902. if (stv680->nullpackets > STV680_MAX_NULLPACKETS) {
  903. stv680->nullpackets = 0;
  904. PDEBUG (2, "STV(i): too many null length packets, restarting capture");
  905. stv680_stop_stream (stv680);
  906. stv680_start_stream (stv680);
  907. } else {
  908. if (stv680->scratch[stv680->scratch_use].state != BUFFER_READY) {
  909. stv680->frame[framenr].grabstate = FRAME_ERROR;
  910. PDEBUG (2, "STV(e): FRAME_ERROR in _newframe");
  911. return -EIO;
  912. }
  913. stv680->scratch[stv680->scratch_use].state = BUFFER_BUSY;
  914. bayer_unshuffle (stv680, &stv680->scratch[stv680->scratch_use]);
  915. stv680->scratch[stv680->scratch_use].state = BUFFER_UNUSED;
  916. stv680->scratch_use++;
  917. if (stv680->scratch_use >= STV680_NUMSCRATCH)
  918. stv680->scratch_use = 0;
  919. if (errors > STV680_MAX_ERRORS) {
  920. errors = 0;
  921. PDEBUG (2, "STV(i): too many errors, restarting capture");
  922. stv680_stop_stream (stv680);
  923. stv680_start_stream (stv680);
  924. }
  925. } /* else */
  926. } /* while */
  927. return 0;
  928. }
  929. /*********************************************************************
  930. * Video4Linux
  931. *********************************************************************/
  932. static int stv_open (struct inode *inode, struct file *file)
  933. {
  934. struct video_device *dev = video_devdata(file);
  935. struct usb_stv *stv680 = video_get_drvdata(dev);
  936. int err = 0;
  937. /* we are called with the BKL held */
  938. stv680->user = 1;
  939. err = stv_init (stv680); /* main initialization routine for camera */
  940. if (err >= 0) {
  941. stv680->fbuf = rvmalloc (stv680->maxframesize * STV680_NUMFRAMES);
  942. if (!stv680->fbuf) {
  943. PDEBUG (0, "STV(e): Could not rvmalloc frame bufer");
  944. err = -ENOMEM;
  945. }
  946. file->private_data = dev;
  947. }
  948. if (err)
  949. stv680->user = 0;
  950. return err;
  951. }
  952. static int stv_close (struct inode *inode, struct file *file)
  953. {
  954. struct video_device *dev = file->private_data;
  955. struct usb_stv *stv680 = video_get_drvdata(dev);
  956. int i;
  957. for (i = 0; i < STV680_NUMFRAMES; i++)
  958. stv680->frame[i].grabstate = FRAME_UNUSED;
  959. if (stv680->streaming)
  960. stv680_stop_stream (stv680);
  961. if ((i = stv_stop_video (stv680)) < 0)
  962. PDEBUG (1, "STV(e): stop_video failed in stv_close");
  963. rvfree (stv680->fbuf, stv680->maxframesize * STV680_NUMFRAMES);
  964. stv680->user = 0;
  965. if (stv680->removed) {
  966. kfree(stv680);
  967. stv680 = NULL;
  968. PDEBUG (0, "STV(i): device unregistered");
  969. }
  970. file->private_data = NULL;
  971. return 0;
  972. }
  973. static int stv680_do_ioctl (struct inode *inode, struct file *file,
  974. unsigned int cmd, void *arg)
  975. {
  976. struct video_device *vdev = file->private_data;
  977. struct usb_stv *stv680 = video_get_drvdata(vdev);
  978. if (!stv680->udev)
  979. return -EIO;
  980. switch (cmd) {
  981. case VIDIOCGCAP:{
  982. struct video_capability *b = arg;
  983. strcpy (b->name, stv680->camera_name);
  984. b->type = VID_TYPE_CAPTURE;
  985. b->channels = 1;
  986. b->audios = 0;
  987. b->maxwidth = stv680->maxwidth;
  988. b->maxheight = stv680->maxheight;
  989. b->minwidth = stv680->maxwidth / 2;
  990. b->minheight = stv680->maxheight / 2;
  991. return 0;
  992. }
  993. case VIDIOCGCHAN:{
  994. struct video_channel *v = arg;
  995. if (v->channel != 0)
  996. return -EINVAL;
  997. v->flags = 0;
  998. v->tuners = 0;
  999. v->type = VIDEO_TYPE_CAMERA;
  1000. strcpy (v->name, "STV Camera");
  1001. return 0;
  1002. }
  1003. case VIDIOCSCHAN:{
  1004. struct video_channel *v = arg;
  1005. if (v->channel != 0)
  1006. return -EINVAL;
  1007. return 0;
  1008. }
  1009. case VIDIOCGPICT:{
  1010. struct video_picture *p = arg;
  1011. stv680_get_pict (stv680, p);
  1012. return 0;
  1013. }
  1014. case VIDIOCSPICT:{
  1015. struct video_picture *p = arg;
  1016. if (stv680_set_pict (stv680, p))
  1017. return -EINVAL;
  1018. return 0;
  1019. }
  1020. case VIDIOCSWIN:{
  1021. struct video_window *vw = arg;
  1022. if (vw->flags)
  1023. return -EINVAL;
  1024. if (vw->clipcount)
  1025. return -EINVAL;
  1026. if (vw->width != stv680->vwidth) {
  1027. if (stv680_set_size (stv680, vw->width, vw->height)) {
  1028. PDEBUG (2, "STV(e): failed (from user) set size in VIDIOCSWIN");
  1029. return -EINVAL;
  1030. }
  1031. }
  1032. return 0;
  1033. }
  1034. case VIDIOCGWIN:{
  1035. struct video_window *vw = arg;
  1036. vw->x = 0; /* FIXME */
  1037. vw->y = 0;
  1038. vw->chromakey = 0;
  1039. vw->flags = 0;
  1040. vw->clipcount = 0;
  1041. vw->width = stv680->vwidth;
  1042. vw->height = stv680->vheight;
  1043. return 0;
  1044. }
  1045. case VIDIOCGMBUF:{
  1046. struct video_mbuf *vm = arg;
  1047. int i;
  1048. memset (vm, 0, sizeof (*vm));
  1049. vm->size = STV680_NUMFRAMES * stv680->maxframesize;
  1050. vm->frames = STV680_NUMFRAMES;
  1051. for (i = 0; i < STV680_NUMFRAMES; i++)
  1052. vm->offsets[i] = stv680->maxframesize * i;
  1053. return 0;
  1054. }
  1055. case VIDIOCMCAPTURE:{
  1056. struct video_mmap *vm = arg;
  1057. if (vm->format != STV_VIDEO_PALETTE) {
  1058. PDEBUG (2, "STV(i): VIDIOCMCAPTURE vm.format (%i) != VIDEO_PALETTE (%i)",
  1059. vm->format, STV_VIDEO_PALETTE);
  1060. if ((vm->format == 3) && (swapRGB_on == 0)) {
  1061. PDEBUG (2, "STV(i): VIDIOCMCAPTURE swapRGB is (auto) ON");
  1062. /* this may fix those apps (e.g., xawtv) that want BGR */
  1063. swapRGB = 1;
  1064. }
  1065. return -EINVAL;
  1066. }
  1067. if (vm->frame >= STV680_NUMFRAMES) {
  1068. PDEBUG (2, "STV(e): VIDIOCMCAPTURE vm.frame > NUMFRAMES");
  1069. return -EINVAL;
  1070. }
  1071. if ((stv680->frame[vm->frame].grabstate == FRAME_ERROR)
  1072. || (stv680->frame[vm->frame].grabstate == FRAME_GRABBING)) {
  1073. PDEBUG (2, "STV(e): VIDIOCMCAPTURE grabstate (%i) error",
  1074. stv680->frame[vm->frame].grabstate);
  1075. return -EBUSY;
  1076. }
  1077. /* Is this according to the v4l spec??? */
  1078. if (stv680->vwidth != vm->width) {
  1079. if (stv680_set_size (stv680, vm->width, vm->height)) {
  1080. PDEBUG (2, "STV(e): VIDIOCMCAPTURE set_size failed");
  1081. return -EINVAL;
  1082. }
  1083. }
  1084. stv680->frame[vm->frame].grabstate = FRAME_READY;
  1085. if (!stv680->streaming)
  1086. stv680_start_stream (stv680);
  1087. return 0;
  1088. }
  1089. case VIDIOCSYNC:{
  1090. int *frame = arg;
  1091. int ret = 0;
  1092. if (*frame < 0 || *frame >= STV680_NUMFRAMES) {
  1093. PDEBUG (2, "STV(e): Bad frame # in VIDIOCSYNC");
  1094. return -EINVAL;
  1095. }
  1096. ret = stv680_newframe (stv680, *frame);
  1097. stv680->frame[*frame].grabstate = FRAME_UNUSED;
  1098. return ret;
  1099. }
  1100. case VIDIOCGFBUF:{
  1101. struct video_buffer *vb = arg;
  1102. memset (vb, 0, sizeof (*vb));
  1103. return 0;
  1104. }
  1105. case VIDIOCKEY:
  1106. return 0;
  1107. case VIDIOCCAPTURE:
  1108. {
  1109. PDEBUG (2, "STV(e): VIDIOCCAPTURE failed");
  1110. return -EINVAL;
  1111. }
  1112. case VIDIOCSFBUF:
  1113. case VIDIOCGTUNER:
  1114. case VIDIOCSTUNER:
  1115. case VIDIOCGFREQ:
  1116. case VIDIOCSFREQ:
  1117. case VIDIOCGAUDIO:
  1118. case VIDIOCSAUDIO:
  1119. return -EINVAL;
  1120. default:
  1121. return -ENOIOCTLCMD;
  1122. } /* end switch */
  1123. return 0;
  1124. }
  1125. static int stv680_ioctl(struct inode *inode, struct file *file,
  1126. unsigned int cmd, unsigned long arg)
  1127. {
  1128. return video_usercopy(inode, file, cmd, arg, stv680_do_ioctl);
  1129. }
  1130. static int stv680_mmap (struct file *file, struct vm_area_struct *vma)
  1131. {
  1132. struct video_device *dev = file->private_data;
  1133. struct usb_stv *stv680 = video_get_drvdata(dev);
  1134. unsigned long start = vma->vm_start;
  1135. unsigned long size = vma->vm_end-vma->vm_start;
  1136. unsigned long page, pos;
  1137. mutex_lock(&stv680->lock);
  1138. if (stv680->udev == NULL) {
  1139. mutex_unlock(&stv680->lock);
  1140. return -EIO;
  1141. }
  1142. if (size > (((STV680_NUMFRAMES * stv680->maxframesize) + PAGE_SIZE - 1)
  1143. & ~(PAGE_SIZE - 1))) {
  1144. mutex_unlock(&stv680->lock);
  1145. return -EINVAL;
  1146. }
  1147. pos = (unsigned long) stv680->fbuf;
  1148. while (size > 0) {
  1149. page = vmalloc_to_pfn((void *)pos);
  1150. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) {
  1151. mutex_unlock(&stv680->lock);
  1152. return -EAGAIN;
  1153. }
  1154. start += PAGE_SIZE;
  1155. pos += PAGE_SIZE;
  1156. if (size > PAGE_SIZE)
  1157. size -= PAGE_SIZE;
  1158. else
  1159. size = 0;
  1160. }
  1161. mutex_unlock(&stv680->lock);
  1162. return 0;
  1163. }
  1164. static ssize_t stv680_read (struct file *file, char __user *buf,
  1165. size_t count, loff_t *ppos)
  1166. {
  1167. struct video_device *dev = file->private_data;
  1168. unsigned long int realcount = count;
  1169. int ret = 0;
  1170. struct usb_stv *stv680 = video_get_drvdata(dev);
  1171. unsigned long int i;
  1172. if (STV680_NUMFRAMES != 2) {
  1173. PDEBUG (0, "STV(e): STV680_NUMFRAMES needs to be 2!");
  1174. return -1;
  1175. }
  1176. if (stv680->udev == NULL)
  1177. return -EIO;
  1178. if (realcount > (stv680->vwidth * stv680->vheight * 3))
  1179. realcount = stv680->vwidth * stv680->vheight * 3;
  1180. /* Shouldn't happen: */
  1181. if (stv680->frame[0].grabstate == FRAME_GRABBING) {
  1182. PDEBUG (2, "STV(e): FRAME_GRABBING in stv680_read");
  1183. return -EBUSY;
  1184. }
  1185. stv680->frame[0].grabstate = FRAME_READY;
  1186. stv680->frame[1].grabstate = FRAME_UNUSED;
  1187. stv680->curframe = 0;
  1188. if (!stv680->streaming)
  1189. stv680_start_stream (stv680);
  1190. if (!stv680->streaming) {
  1191. ret = stv680_newframe (stv680, 0); /* ret should = 0 */
  1192. }
  1193. ret = stv680_newframe (stv680, 0);
  1194. if (!ret) {
  1195. if ((i = copy_to_user (buf, stv680->frame[0].data, realcount)) != 0) {
  1196. PDEBUG (2, "STV(e): copy_to_user frame 0 failed, ret count = %li", i);
  1197. return -EFAULT;
  1198. }
  1199. } else {
  1200. realcount = ret;
  1201. }
  1202. stv680->frame[0].grabstate = FRAME_UNUSED;
  1203. return realcount;
  1204. } /* stv680_read */
  1205. static struct file_operations stv680_fops = {
  1206. .owner = THIS_MODULE,
  1207. .open = stv_open,
  1208. .release = stv_close,
  1209. .read = stv680_read,
  1210. .mmap = stv680_mmap,
  1211. .ioctl = stv680_ioctl,
  1212. .compat_ioctl = v4l_compat_ioctl32,
  1213. .llseek = no_llseek,
  1214. };
  1215. static struct video_device stv680_template = {
  1216. .owner = THIS_MODULE,
  1217. .name = "STV0680 USB camera",
  1218. .type = VID_TYPE_CAPTURE,
  1219. .hardware = VID_HARDWARE_SE401,
  1220. .fops = &stv680_fops,
  1221. .release = video_device_release,
  1222. .minor = -1,
  1223. };
  1224. static int stv680_probe (struct usb_interface *intf, const struct usb_device_id *id)
  1225. {
  1226. struct usb_device *dev = interface_to_usbdev(intf);
  1227. struct usb_host_interface *interface;
  1228. struct usb_stv *stv680 = NULL;
  1229. char *camera_name = NULL;
  1230. int retval = 0;
  1231. /* We don't handle multi-config cameras */
  1232. if (dev->descriptor.bNumConfigurations != 1) {
  1233. PDEBUG (0, "STV(e): Number of Configurations != 1");
  1234. return -ENODEV;
  1235. }
  1236. interface = &intf->altsetting[0];
  1237. /* Is it a STV680? */
  1238. if ((le16_to_cpu(dev->descriptor.idVendor) == USB_PENCAM_VENDOR_ID) &&
  1239. (le16_to_cpu(dev->descriptor.idProduct) == USB_PENCAM_PRODUCT_ID)) {
  1240. camera_name = "STV0680";
  1241. PDEBUG (0, "STV(i): STV0680 camera found.");
  1242. } else if ((le16_to_cpu(dev->descriptor.idVendor) == USB_CREATIVEGOMINI_VENDOR_ID) &&
  1243. (le16_to_cpu(dev->descriptor.idProduct) == USB_CREATIVEGOMINI_PRODUCT_ID)) {
  1244. camera_name = "Creative WebCam Go Mini";
  1245. PDEBUG (0, "STV(i): Creative WebCam Go Mini found.");
  1246. } else {
  1247. PDEBUG (0, "STV(e): Vendor/Product ID do not match STV0680 or Creative WebCam Go Mini values.");
  1248. PDEBUG (0, "STV(e): Check that the STV0680 or Creative WebCam Go Mini camera is connected to the computer.");
  1249. retval = -ENODEV;
  1250. goto error;
  1251. }
  1252. /* We found one */
  1253. if ((stv680 = kzalloc (sizeof (*stv680), GFP_KERNEL)) == NULL) {
  1254. PDEBUG (0, "STV(e): couldn't kmalloc stv680 struct.");
  1255. retval = -ENOMEM;
  1256. goto error;
  1257. }
  1258. stv680->udev = dev;
  1259. stv680->camera_name = camera_name;
  1260. stv680->vdev = video_device_alloc();
  1261. if (!stv680->vdev) {
  1262. retval = -ENOMEM;
  1263. goto error;
  1264. }
  1265. memcpy(stv680->vdev, &stv680_template, sizeof(stv680_template));
  1266. stv680->vdev->dev = &intf->dev;
  1267. video_set_drvdata(stv680->vdev, stv680);
  1268. memcpy (stv680->vdev->name, stv680->camera_name, strlen (stv680->camera_name));
  1269. init_waitqueue_head (&stv680->wq);
  1270. mutex_init (&stv680->lock);
  1271. wmb ();
  1272. if (video_register_device (stv680->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
  1273. PDEBUG (0, "STV(e): video_register_device failed");
  1274. retval = -EIO;
  1275. goto error_vdev;
  1276. }
  1277. PDEBUG (0, "STV(i): registered new video device: video%d", stv680->vdev->minor);
  1278. usb_set_intfdata (intf, stv680);
  1279. retval = stv680_create_sysfs_files(stv680->vdev);
  1280. if (retval)
  1281. goto error_unreg;
  1282. return 0;
  1283. error_unreg:
  1284. video_unregister_device(stv680->vdev);
  1285. error_vdev:
  1286. video_device_release(stv680->vdev);
  1287. error:
  1288. kfree(stv680);
  1289. return retval;
  1290. }
  1291. static inline void usb_stv680_remove_disconnected (struct usb_stv *stv680)
  1292. {
  1293. int i;
  1294. stv680->udev = NULL;
  1295. stv680->frame[0].grabstate = FRAME_ERROR;
  1296. stv680->frame[1].grabstate = FRAME_ERROR;
  1297. stv680->streaming = 0;
  1298. wake_up_interruptible (&stv680->wq);
  1299. for (i = 0; i < STV680_NUMSBUF; i++)
  1300. if (stv680->urb[i]) {
  1301. usb_kill_urb (stv680->urb[i]);
  1302. usb_free_urb (stv680->urb[i]);
  1303. stv680->urb[i] = NULL;
  1304. kfree(stv680->sbuf[i].data);
  1305. }
  1306. for (i = 0; i < STV680_NUMSCRATCH; i++)
  1307. kfree(stv680->scratch[i].data);
  1308. PDEBUG (0, "STV(i): %s disconnected", stv680->camera_name);
  1309. /* Free the memory */
  1310. kfree(stv680);
  1311. }
  1312. static void stv680_disconnect (struct usb_interface *intf)
  1313. {
  1314. struct usb_stv *stv680 = usb_get_intfdata (intf);
  1315. usb_set_intfdata (intf, NULL);
  1316. if (stv680) {
  1317. /* We don't want people trying to open up the device */
  1318. if (stv680->vdev) {
  1319. stv680_remove_sysfs_files(stv680->vdev);
  1320. video_unregister_device(stv680->vdev);
  1321. stv680->vdev = NULL;
  1322. }
  1323. if (!stv680->user) {
  1324. usb_stv680_remove_disconnected (stv680);
  1325. } else {
  1326. stv680->removed = 1;
  1327. }
  1328. }
  1329. }
  1330. static struct usb_driver stv680_driver = {
  1331. .name = "stv680",
  1332. .probe = stv680_probe,
  1333. .disconnect = stv680_disconnect,
  1334. .id_table = device_table
  1335. };
  1336. /********************************************************************
  1337. * Module routines
  1338. ********************************************************************/
  1339. static int __init usb_stv680_init (void)
  1340. {
  1341. if (usb_register (&stv680_driver) < 0) {
  1342. PDEBUG (0, "STV(e): Could not setup STV0680 driver");
  1343. return -1;
  1344. }
  1345. PDEBUG (0, "STV(i): usb camera driver version %s registering", DRIVER_VERSION);
  1346. info(DRIVER_DESC " " DRIVER_VERSION);
  1347. return 0;
  1348. }
  1349. static void __exit usb_stv680_exit (void)
  1350. {
  1351. usb_deregister (&stv680_driver);
  1352. PDEBUG (0, "STV(i): driver deregistered");
  1353. }
  1354. module_init (usb_stv680_init);
  1355. module_exit (usb_stv680_exit);